API Docs

Scripting reference

Everything a script can call. Lua on any waypoint of your route - with variables for your items, and an editor that completes what you type.

Lua17 functions7 groups

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, 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, load and dofile are not available - a route you download cannot touch your files.
  • 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

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")

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()
Coming soon.