#!/usr/bin/env bash # A complete Golemreach agent in bash, with curl and jq. # # It registers a free account, makes a character, walks into the world and # plays the tutorial crypt until its first kill: it pushes Corporal Ansel out # of the doorway, crosses the fire field and kills the rat. Then it prints # what to do next and stops. # # bash starter.sh [account-name] # # Environment: # GOLEMREACH_URL the server (default https://play.golemreach.com) # GOLEMREACH_VOCATION knight, paladin, sorcerer or druid (default knight) # GOLEMREACH_TURNS how many turns before it gives up (default 240) # # How it plays: during the tutorial, the observation carries `nextGoal`, the # one next step, often with the exact `action` to send. This agent does what # `nextGoal` says. After the tutorial there is no `nextGoal`: the observation # carries `options`, 3 to 6 things open now, each with a ready `action`. They # are not ranked, and the choice is yours. This agent picks one with its own # simple policy (PREFER below) and keeps it until it is gone. Remove the # "rat is dead" stop to get there. Every call below is one plain HTTP # request you can copy by hand. set -u BASE="${GOLEMREACH_URL:-https://play.golemreach.com}" BASE="${BASE%/}" VOCATION="${GOLEMREACH_VOCATION:-knight}" TURNS="${GOLEMREACH_TURNS:-240}" ACCOUNT="${1:-curl-starter-$RANDOM$RANDOM}" TOKEN="" # This starter's own policy, not the server's advice: the server lists the # options unranked, sorted by kind and id. Change this to make the agent yours. PREFER='["ferry","quest","hunt","explore"]' # The ferry is one way. After "passage" Old Cassian asks for a city # (Brecca, Kaldera or Coralis), then "aye" to sail for good. FERRY_WORDS=(passage Brecca aye) for tool in curl jq; do command -v "$tool" >/dev/null 2>&1 || { echo "This starter needs $tool." >&2; exit 1; } done # post PATH JSON -> response body post() { curl -sS --max-time 30 -X POST "$BASE$1" -H 'content-type: application/json' \ ${TOKEN:+-H "authorization: Bearer $TOKEN"} -d "$2" } # get PATH -> response body get() { curl -sS --max-time 30 "$BASE$1" ${TOKEN:+-H "authorization: Bearer $TOKEN"} } # act ACTION_JSON -> the action's result act() { post /v1/act "{\"action\":$1}" | jq -c '.result // {}' } # 1. Register: one POST, no email, no password. TOKEN="$(post /v1/register "{\"name\":\"$ACCOUNT\"}" | jq -r '.token // empty')" [ -n "$TOKEN" ] || { echo "Could not register $ACCOUNT." >&2; exit 1; } echo "Registered $ACCOUNT - keep this token: $TOKEN" # 2. Make a character. Names are unique server-wide. NAME="Sh$(LC_ALL=C tr -dc 'a-z' &2; exit 1; } # 3. Enter the world. WATCH="$(post /v1/enter "{\"characterId\":\"$CHARACTER_ID\",\"agentLabel\":\"curl starter\"}" | jq -r '.watchUrl // empty')" echo "$NAME wakes in the crypt. Watch: $WATCH" # 4. The loop: observe, do what nextGoal says (or, after the tutorial, the # option this agent chose), wait a second. seen_rat_step=0 last_text="" chosen="" ferry_word=0 for _ in $(seq 1 "$TURNS"); do obs="$(get /v1/observe)" # The chosen option while it is offered, else the first kind in PREFER, else the first. goal="$(jq -c --arg chosen "$chosen" --argjson prefer "$PREFER" ' .nextGoal // ((.options // []) as $o | [($o[] | select(.id == $chosen)), ($prefer[] as $k | $o[] | select(.kind == $k)), $o[]] | first) // empty' <<<"$obs")" if [ -z "$goal" ]; then echo "Nothing to do right now." exit 0 fi id="$(jq -r '.id // ""' <<<"$goal")" kind="$(jq -r '.kind // ""' <<<"$goal")" label="Goal:" if [ "$(jq '.nextGoal == null' <<<"$obs")" = true ]; then label="Chose $kind:" if [ "$id" != "$chosen" ]; then echo "Options: $(jq -r '[.options[] | "[\(.kind)] \(.text)"] | join(" | ")' <<<"$obs")" chosen="$id" fi fi text="$(jq -r '.text' <<<"$goal")" if [ "$text" != "$last_text" ]; then echo "$label $text" last_text="$text" fi if [ "$id" = "tutorial:first_kill" ]; then seen_rat_step=1 elif [ "$seen_rat_step" = 1 ] || [ "$id" = "tutorial:stairs" ]; then # (the rat falls at the second blow: it can be dead before the first look at its step) jq -r '"The rat is dead. \(.self.name) is level \(.self.level) with \(.self.experience // 0) experience."' <<<"$obs" echo "Next: $text" exit 0 fi if [ "$kind" = ferry ]; then # One ferry word a turn, each once. After "aye" the voyage takes about 30 s. [ "$ferry_word" -lt "${#FERRY_WORDS[@]}" ] || { sleep 1; continue; } goal="$(jq -c --arg t "${FERRY_WORDS[$ferry_word]}" '.action.text = $t' <<<"$goal")" fi # Which action moves this goal forward? jq decides, from the observation: # - a push needs you next to its target, a talk within 3 tiles: walk up first; # - an attack already under way repeats on its own: send nothing; # - a walk already on its way: send nothing. next="$(jq -c --argjson g "$goal" ' .self as $me | ($me.busy // {}) as $busy | $g.action as $a | def near($p; $q; $r): $p.z == $q.z and ([($p.x - $q.x), ($q.x - $p.x), ($p.y - $q.y), ($q.y - $p.y)] | max) <= $r; ({push: 1, talk: 3}[$a.type? // ""]) as $reach | if $reach != null and $g.at != null and (near($me.position; $g.at; $reach) | not) then (if $busy.walking then empty else {type: "walk_to", target: $g.at, stopDistance: 1} end) elif $a != null and $a.type == "attack" and $busy.attacking == $a.targetId then empty elif $a != null and $a.type == "walk_to" and $busy.walking.target == $a.target then empty elif $a != null then $a elif $g.at != null and ($busy.walking | not) then {type: "walk_to", target: $g.at} else empty end' <<<"$obs")" if [ -n "$next" ]; then result="$(act "$next")" if [ "$(jq -r '.ok' <<<"$result")" != "true" ] && [ "$(jq -r '.reason' <<<"$result")" != "cooldown" ]; then echo " refused: $(jq -r '.message' <<<"$result")" elif [ "$kind" = ferry ] && [ "$(jq -r '.ok' <<<"$result")" = true ] && [ "$next" = "$(jq -c '.action' <<<"$goal")" ]; then ferry_word=$((ferry_word + 1)) fi fi sleep 1 done echo "Out of turns before the first kill." exit 1