The language

Scripts are Lua - the whole language, not a subset or a dialect, so a script written for another bot reads the same here. They run on the Cavebot's Action waypoints, as scripts of their own in the Scripts menu - which can stay loaded the whole hunt, listening to the chat - and in the editor's test run, which tries a script on the spot.

While a script runs, the rest of the bot carries on: healing, targeting and every other module keep playing.

  • Safe by design. io, os.execute, require and load are not available - a route you download cannot touch your files.
  • The internet is yours to allow. Scripts reach the web only after you turn it on, on your own computer - a script you download cannot switch it on by itself.
  • A typo is an error, not nil. An unknown name stops the script and says what you probably meant.
  • A script cannot hang the bot. One that stops making progress is stopped after 30 seconds, and switching the module off stops it at once.
line 1: 'getCapcity' is not a function or a variable this bot knows.  Did you mean getCapacity()?

Variables

Your items, groups of items and chat channels are variables, set up once in the Cavebot's Script variables and saved with the route. They are ordinary globals, and $name is shorthand for name - these three are the same call:

getItemCount($manapotion)
getItemCount(manapotion)
getItemCount("manapotion")

A group works wherever an item does. A function given one does to every item in it what it would have done to one - dropItems($trash) drops each of them.

Script

print([value])#

Writes a line to the bot's log.

ParameterType
valueanyAnything printable - several are joined with spaces. optional

The line appears in the log of the module that ran the script, next to what it was doing at the time.

print("cap is " .. getCapacity())

wait(milliseconds [, upToMilliseconds])#

Holds the script still for a while.

ParameterType
millisecondsnumberHow long to wait.
upToMillisecondsnumberWhen given, wait a random time between the two. optional

Returnsboolean - false when the wait was cut short by the module stopping.

The character keeps playing while a script waits; only the route pauses.

wait(500)
wait(800, 1600)  -- somewhere between 0.8s and 1.6s

Navigation

goToLabelAndSection(label [, section])#

Carries the route on from a named Label.

ParameterType
labelstringThe Label waypoint to carry on from.
sectionstringThe route section the label is in. Without it, the section being walked. optional

Returnsboolean - true when the label was found.

The script does not stop at the call: it runs to the end, and the route moves once it is finished - so it is safe to jump and then still log, wait or press a key. When two jumps succeed, the last one wins. A label that does not exist is logged, with the labels the section does have, and the route carries on from the next waypoint.

if getCapacity() < 200 then
  goToLabelAndSection("leave", "Refill")
end

Scripts menu

The Scripts menu holds scripts that run on their own, beside the hunt: a watch that logs out when a player shows up, a check on your capacity every few seconds, helpers shared by every Action on your routes. Each script is a file of its own, listed by name in the menu. It can be loaded from there, when the profile starts, or by another script.

A script from the menu runs its body once, top to bottom, and stays loaded while it has something to do: a timer() still running or an onMessage() still listening. When the last one stops, the script unloads itself.

timer(milliseconds, callback [, upToMilliseconds])#

Calls a function every so often, for as long as the script stays loaded.

ParameterType
millisecondsnumberHow long between two calls, in milliseconds.
callbackfunctionThe function to call.
upToMillisecondsnumberWhen given, each gap is a random time between the two. Without it, every gap is exactly the first number. optional

Returnsnumber - the timer's number, for stopTimer().

Only in a script from the Scripts menu: an Action ends with its last line, so nothing would be left to call the function. The first call comes one interval after timer(), and each gap is counted from the end of the call before it. Timers, listeners and the script's body take turns, so a callback that waits holds the others back. An error in a callback stops the whole script; wrap the risky part in pcall to carry on. Up to 32 timers, none faster than 100 ms.

timer(1000, function()
  if getCapacity() < 100 then print("low cap") end
end)

-- a key now and then, every 5 to 9 seconds
local nudge = timer(5000, function() keyEvent("F1") end, 9000)

stopTimer(timer)#

Stops a timer started with timer().

ParameterType
timernumberThe number timer() returned.

Returnsboolean - false when there was no timer by that number.

Safe to call from inside the timer's own callback: the call that is running finishes, and there is no next one. When the last timer stops, no listener is left and the body has finished, the script unloads itself.

local tries = 0
local t
t = timer(2000, function()
  tries = tries + 1
  if tries >= 5 then stopTimer(t) end
end)

loadScript(name)#

Loads a script from the Scripts menu.

ParameterType
namestringThe script's name, as the Scripts menu shows it.

Returnsboolean - true when the script is loaded now: just started, or already running.

The script runs on its own, beside whatever asked for it: the call returns at once and does not wait for it. A script that is already loaded is left as it is, never started twice. It works from anywhere a script runs, so an Action can load the scripts a hunt needs and unload them on the way out. A name the menu does not have is reported in the log, with the names it does have, and the call returns false.

loadScript("anti-pk")

unloadScript([name])#

Unloads a script from the Scripts menu, or this one.

ParameterType
namestringThe script's name, as the Scripts menu shows it. Without it, the script making the call. optional

Returnsboolean - false when no script by that name was loaded.

Stops the script wherever it is, in a wait or between two timer calls, and its timers with it. Without a name, a script from the menu unloads itself and stops right there: nothing after the call runs.

unloadScript("anti-pk")

if getCapacity() < 50 then unloadScript() end

isScriptLoaded(name)#

Whether a script from the Scripts menu is loaded right now.

ParameterType
namestringThe script's name, as the Scripts menu shows it.

Returnsboolean - true while it is loaded: running its body, or serving its timers and listeners.

if not isScriptLoaded("anti-pk") then loadScript("anti-pk") end

dofile(name)#

Runs a script from the Scripts menu inside this one, and gives back what it returns.

ParameterType
namestringThe script's name, as the Scripts menu shows it.

Returnsany - whatever the file returns; nothing, when it returns nothing.

The way to write something once and use it everywhere: a file of helpers, run from any Action or any script. The file runs right there, inside the call, so its functions and globals land beside your script's own, as with Lua's dofile. A file that ends in return { ... } can be kept in a local; one that defines globals can simply be run.

It takes the script's name, with or without .lua, never a path: only the Scripts menu's own files can be run. The file is read every time, so a change saved in the menu is picked up on the next call. A file that runs itself, directly or through another, is refused, and files can run each other at most 8 deep.

local utils = dofile("utils")
if utils.lowCap(100) then goToLabelAndSection("refill") end

dofile("helpers")  -- defines its functions as globals
dropTrashIfHeavy()

Modules

A script can switch the bot's own modules on and off - the same switch as the one on each module's screen. Let Targeting rest on the way to the depot and wake it again at the hunt, or hold the Cavebot while a script deals with something. Each module can also have a key of its own, set right beside its switch, to start and stop it during the hunt.

The modules are "alerts", "healer", "equipment", "conditions", "cavebot", "targeting" and "caster" - the names in the sidebar, in upper or lower case alike. A script does not change how your profile starts: the next session opens with the modules you left running.

startModule(module)#

Switches a module on, as its switch on screen does.

ParameterType
modulestringWhich module - "healer", "targeting", "cavebot"...

Returnsboolean - true when the module is on now: just started, or already running.

Starting is refused for the same reasons the switch would refuse it - nothing set up yet, the client not open, the Cavebot not synced. The reason goes to the log and the call returns false. Once it returns true the module is already running, so the next line can count on it.

startModule("targeting")  -- back at the hunt

if not startModule("caster") then print("the Caster would not start") end

stopModule(module)#

Switches a module off, until something starts it again.

ParameterType
modulestringWhich module - "healer", "targeting", "cavebot"...

Returnsboolean - true when the module is off now: just stopped, or already off.

The same as switching it off on its screen. startModule(), the switch or the module's own key brings it back. Stopping the Cavebot from an Action on its own route ends that Action right there, and the route waits where it is; from a script in the Scripts menu, the walker stops and the script carries on.

stopModule("targeting")  -- on the way to the depot

-- from the Scripts menu: hold the walker for five seconds
stopModule("cavebot")
wait(5000)
startModule("cavebot")

isModuleRunning(module)#

Whether a module is switched on right now.

ParameterType
modulestringWhich module - "healer", "targeting", "cavebot"...

Returnsboolean - true while the module is on.

Pausing the whole bot with its key does not change the answer: a module that is on still answers true, and goes back to work as soon as the bot resumes.

if not isModuleRunning("healer") then startModule("healer") end

Targeting

A script can change Lure mode on the Targeting screen: switch it on and off, and set how a lured fight starts. Lure the pack through the hunt, then walk to town clearing only what stands in the way, and lure again once you are back.

A change made by a script shows on the Targeting screen straight away and the bot follows it at once. It is not saved on its own: the profile shows as changed, and saving the profile keeps it for the next session.

setLureMode(on)#

Switches Lure mode on or off, as its switch on the Targeting screen does.

ParameterType
onbooleantrue to lure, false to fight everything as it comes.

Returnsboolean - true once Lure mode is as asked.

With Lure mode off, every creature on your list is fought as soon as it shows up: what a route wants on a road it only has to clear. Switched back on, the fight starts the way Fight starts says. Lure mode works together with the Cavebot: with the Cavebot off, Targeting fights every creature.

setLureMode(false)  -- leaving the hunt: clear the road

-- back at the hunt's first waypoint
setLureMode(true)
setFightStarts("waypoints", 3)

isLureMode()#

Whether Lure mode is switched on.

Returnsboolean - true while Lure mode is on, whether you or a script switched it.

if isLureMode() then setLureMode(false) end

setFightStarts(style [, count])#

Sets how a lured fight starts, and its number.

ParameterType
stylestring"count" (at a creature count) or "waypoints" (only at waypoints).
countnumberOptional. Stop at (count, 0 to 50) or Fight with (waypoints, 1 to 50). Left out, the number stays as it was.

Returnsboolean - true once the fight starts as asked.

At a creature count, the character stops and fights once that many creatures follow it. Only at waypoints, the fight happens on the route's waypoints, with at least that many creatures there; fewer walk on to the next one. The style can be set with Lure mode off too, ready for the next lured stretch.

setFightStarts("count", 4)      -- on the road: stop only for a real pack
setFightStarts("waypoints", 3)  -- back in the hunt

getFightStarts()#

How a lured fight starts, and its number.

Returnsstyle, count - "count" or "waypoints", and its Stop at or Fight with.

local style, count = getFightStarts()
print(style .. " " .. count)

Character

getCapacity()#

Reads the character's free capacity.

Returnsnumber - the free capacity, or nil when it could not be read.

The same number the client shows, in the same units. Set up once on the Client screen; until then it returns nil and the log says what is missing.

local cap = getCapacity()
if cap == nil then
  print("cannot read capacity - carrying on")
elseif cap < 100 then
  goToLabelAndSection("leave")
end

Inventory

getItemCount(item)#

Counts how many of an item the character is carrying.

ParameterType
itemitem or groupAn item with Count on - $manapotion - or a group of them, which adds them up.

Returnsnumber - how many, 0 when none, or nil when it could not be counted.

Zero and nil are different answers. Zero means none are there - what a script asking "have I run out?" wants to hear. Nil means the count could not be made, and the log says why. For a group, one member that cannot be counted makes the total nil.

if getItemCount($manapotion) < 20 then
  goToLabelAndSection("refill", "Town")
end

-- $potions is a group: mana potion + strong mana potion
if getItemCount($potions) < 50 then goToLabelAndSection("refill") end

stowItems(item, itemStash [, confirm])#

Moves an item - or every item of a group - from the backpack into the stash.

ParameterType
itemitem or groupWhat to stow - $goldcoin, or a group like $stowGroup.
itemStashitemThe stash.
confirmitemThe stash's "Yes" button, for accounts that ask to confirm. Clicked if the dialog shows up. optional

Returnsboolean - true when at least one item reached the stash.

Every stack it finds, not just the first - up to 40 per call. The character stops walking first.

stowItems($goldcoin, $stash)
stowItems($stowGroup, $stash, $stashConfirmYes)

dropItems(item)#

Drops an item - or every item of a group - on the square the character stands on.

ParameterType
itemitem or groupWhat to drop - $emptyvial, or a group like $trash.

Returnsboolean - true when at least one item left the backpack.

The counterpart of stowItems() for things worth nothing. Every stack it finds, up to 40 per call.

if getCapacity() < 100 then
  dropItems($trash)
end

Client

keyEvent(key [, modifiers])#

Presses a key in the game client.

ParameterType
keystringThe key, optionally with ctrl+, shift+ or alt+ in front.
modifiersstringModifiers given apart - "ctrl", "ctrl+shift". optional

Returnsboolean - true when the key was delivered to the client.

Keys: F1-F24 · a letter or a digit · Enter · Escape · Space · Tab · Backspace · Delete · Insert · Home · End · PageUp · PageDown · Up · Down · Left · Right · NumPad0-NumPad9. keyEvent("ctrl+f5") and keyEvent("f5", "ctrl") are the same press.

keyEvent("F5")
keyEvent("ctrl+f1")
keyEvent("Escape")

logout()#

Logs the character out.

Returnsboolean - true when the character is out of the world.

The proper way out. Set up once on the Client screen. The script ends here in practice - there is no game left to talk to.

if getItemCount($manapotion) == 0 then
  logout()
end

xLog()#

Exits the client at once - the emergency way out.

Returnsboolean - true when the client was closed.

Instant, which is the point when seconds matter - but exiting is not logging out: the server keeps the character in the world for its own timeout. To leave the world safely, use logout().

xLog()

Chat & NPCs

sendMessage(channel, message [, more…])#

Says something in a chat channel.

ParameterType
channelchannelA channel set up on the Client screen - $npcs, $localchat.
messagestringOne line, up to 255 characters.
morestringMore lines, as many as you like, sent one after another. optional

Returnsboolean - true when every line was sent.

The chat always ends switched off, so your hotkeys keep working. The channel has to be open in the client.

sendMessage($localchat, "hi")
sendMessage($npcs, "hi", "trade")

onMessage(callback)#

Calls a function with every new line that appears in the chat.

ParameterType
callbackfunctionThe function to call, with the line's text.

Returnsnumber - the listener's number, for stopListening.

Each line comes as the chat shows it, the time and the speaker included: "21:06:19 Sir Knight [120]: hi". A message long enough to wrap comes as two lines.

Only new lines count: the ones already in the chat when you call it are not sent. It follows the channel the chat is showing, and when the chat moves to another channel - sendMessage does - that channel's lines count as already seen.

For scripts in the Scripts menu. An Action waypoint ends with its last line, so nothing would be left to listen. A script that listens stays loaded until its listeners are stopped.

Set up the chat once on Client » Chat. Up to 16 listeners per script.

onMessage(function(line)
  if line:find("GM ") then logout() end
end)

stopListening(listener)#

Stops a listener set with onMessage.

ParameterType
listenernumberThe number onMessage returned.

Returnsboolean - false when there was no listener by that number.

Safe to call from inside the listener itself: the call that is running finishes, and there is no next one. With nothing left running, the script unloads itself.

local heard
heard = onMessage(function(line)
  print(line)
  if line:find("bye") then stopListening(heard) end
end)

npcBuy(item, amount)#

Buys an item from the NPC whose trade window is open.

ParameterType
itemitemThe item to buy - $manapotion.
amountnumberHow many, 1 to 10000.

Returnsboolean - true when the purchase was made.

Open the trade first with sendMessage(). The deal goes through only for the exact amount asked - when the client cannot sell that many (not enough gold or capacity), nothing is bought and it returns false. The window stays open after a deal, so you can buy and sell several items in a row; close it with keyEvent("Escape").

sendMessage($npcs, "hi", "trade")
if getItemCount($manapotion) < 50 then
  npcBuy($manapotion, 200 - getItemCount($manapotion))
end

npcSell(item, amount)#

Sells an item to the NPC whose trade window is open - a number of them, or all.

ParameterType
itemitemThe item to sell - $goldenlegs.
amountnumber or trueHow many, or true for all of them.

Returnsboolean - true when the sale was made.

Asking for more than you carry sells nothing and returns false - use true to sell everything.

for _, loot in ipairs({$goldenlegs, $knightarmor, $crownshield}) do
  npcSell(loot, true)
end
keyEvent("Escape")

Depot

openFreeDepot()#

Walks to a free depot and opens its locker.

Returnsboolean - true when the locker was opened.

Call it with the depot on screen. It takes the nearest free one, and if somebody gets there first, the next. Already standing at one, it just opens it.

if not openFreeDepot() then
  wait(3000)
  openFreeDepot()
end

openStash()#

Opens the supply stash from the opened locker.

Returnsboolean - true when the stash window is open.

Open it once, take everything, close it once. Already open, it does nothing and returns true.

openFreeDepot()
openStash()
stashRetrieveItem("strong health potion", 100)
stashRetrieveItem("great mana potion", 200)
closeStash()

stashRetrieveItem(name [, amount])#

Takes an item out of the open stash, by its name.

ParameterType
namestring or itemThe item's name as the stash lists it - "strong health potion" - or an item variable.
amountnumberHow many, 1 to 100000. Without it, the amount the stash offers. optional

Returnsboolean - true when the item was retrieved.

Give a name only that item has. The stash lists every item whose name contains the words, A to Z - "mana potion" finds the great mana potion first.

if getItemCount($stronghealthpotion) < 100 then
  openStash()
  stashRetrieveItem("strong health potion", 100 - getItemCount($stronghealthpotion))
  closeStash()
end

closeStash()#

Closes the stash window.

Returnsboolean - true when the stash window is closed.

Already closed, it does nothing and returns true.

closeStash()

Internet

Scripts can talk to web APIs: report a finished task to a tracker, post to a Discord webhook, or read settings from a server of your own. It is off until you turn it on, with the Internet access switch at the top of the Scripts screen. The switch belongs to your computer, not to a profile or a script, so nothing you download can turn it on for you.

httpGet(url [, headers])#

Asks a web address for something, and gives back its answer.

ParameterType
urlstringThe address, starting with https:// or http://.
headerstableExtra headers - { Authorization = "Bearer x" }. optional

Returnsbody, status - the answer's text and its HTTP status, or nil and the reason when no answer came.

A 404 or a 500 is an answer too, so your script decides what a status means. The script waits for the answer, up to 10 seconds, while the rest of the bot keeps playing.

local _, status = httpGet("https://tracker.example.com/task/" .. points)
if status ~= 200 then print("tracker failed: " .. tostring(status)) end

httpPost(url, body [, headers])#

Sends something to a web address, and gives back its answer.

ParameterType
urlstringThe address, starting with https:// or http://.
bodytable or stringA table is sent as JSON; a string is sent as it is.
headerstableExtra headers. optional

Returnsbody, status - or nil and the reason when no answer came.

A table goes out as JSON, which is what a Discord webhook and most APIs expect. Each script can make up to 60 requests a minute.

httpPost(WEBHOOK, { content = "Task done: " .. points .. " points" })

jsonEncode(value)#

Writes a table - or a string, a number, a boolean - as JSON text.

ParameterType
valueanyA table, a string, a number or a boolean.

Returnsstring - the JSON text.

A table of 1, 2, 3... is an array, any other table an object, and an empty table {}. Whole numbers stay whole: 5, not 5.0. Works with the internet off.

print(jsonEncode({ name = "Knight", level = 120 }))

jsonDecode(text)#

Reads JSON text into a table - or a string, a number, a boolean.

ParameterType
textstringJSON, as an API sends it.

Returnsany - the value, or nil and the reason when the text is not JSON.

A JSON null becomes nil. Works with the internet off.

local body, status = httpGet("https://api.example.com/config")
if status == 200 then
  local config = jsonDecode(body)
  if config then print("hunt until level " .. config.level) end
end
Coming soon.