Difference between revisions of "AI"

From The Perfect Tower II
Jump to navigation Jump to search
(Begin to reformat the AI page)
(Begin to split the "all APIs" table into multiple tables)
Line 16: Line 16:
 
*Actions
 
*Actions
  
The function signatures (combination of the return type and types of the arguments for the function) for each function in the external editor are expressed in this way <code><Return type> <namespace?>.<function>(type: arg1 ...)</code>
+
All scripts have a limit of impulses, conditions and actions. Impulses and conditions have a cap of 10, actions a cap pf 50.
  
For consistency with common function signature formats the signatures here have been rewritten into the following Python-like format <code><namespace?>.<function>(arg1: type) -> ReturnType</code>. Impulses return the special type <code>impulse</code> and functions that no not return a value (a "void" type) are not followed by a return type. Namespaces and function names are unchanged.  
+
===Impulses===
 +
An Impulse can refer to any button press or event that directly activates an AI script. It is not to be confused with the Basic:Execute command which is not considered an impulse, as it is not a way for a player to directly trigger an AI script, but rather a way of calling the script inside of another AI script much like helper methods in high level programming languages. Each impulse can trigger the AI script, regardless if it has already been triggered. It is possible that a script can trigger multiple times before it has finished execution. In this case multiple instances of this script can run in parallel.
  
===Impulse===
+
===Conditions===
An Impulse can refer to any button press or event that directly activates an AI script. It is not to be confused with the Basic:Execute command which is not considered an impulse, as it is not a way for a player to directly trigger an AI script, but rather a way of calling the script inside of another AI script much like helper methods in high level programming languages. An AI script can contain up to 10 impulses, each of which can trigger the AI script, regardless if it has already been triggered. It is possible that a script can trigger multiple times before it has finished execution. In this case multiple instances of this script can run in parallel.
+
A Condition is a requirement that has to be fulfilled in order for the script to start executing. If any condition in the script is false then the script will not be executed when an impulse is triggered by a player action. During the execution the specified conditions have no effect. All conditions are of data Type:Bool, and any expression that returns a bool can be used as a condition
  
===Condition===
+
===Actions===
A Condition is a requirement that has to be fulfilled in order for the script to start executing. An AI script can contain up to 10 conditions of which if any is false then the script will not be executed when an impulse is triggered by a player action. During the execution the specified conditions have no effect. All conditions are of data Type:Bool, and any expression that returns a bool can be used as a condition
+
An Action is something that the AI does when the script becomes active. The Actions are executed one by one, in order from top to bottom. Actions will continue to be executed until either the AI menu is closed, or the script terminates normally. Within one instance of a script, as many actions as the script budget will allow can be executed in one frame. Atomic actions (those with a red symbol) cost 0 budget, all others cost 100. The script is executed in sequence until the budget is reached at which point execution is given to the next script.
 
 
===Action===
 
An Action is something that the AI does when the script becomes active. The Actions are executed one by one, in order from top to bottom. Actions will continue to be executed until either the AI menu is closed, or the script terminates normally. Within one instance of a script, as many actions as the script budget will allow can be executed in one frame. Atomic actions (those with a red symbol) cost 0 budget, all others cost 100. The script is executed in sequence until the budget is reached at which point execution is given to the next script. A script can have up to 50 actions.
 
  
 
*
 
*
Line 36: Line 34:
  
  
There is a maximum budget per script. This is determined by the total amount of RAM installed in all servers. The amounts you get from your RAM are <code>floor(7 + Log<sub>7.98</sub>(RAM))</code>, <code>floor(1 + (actions / 7))</code> and <code>floor(1 + (actions / 11))</code> bytes, rounded up. Here is a Desmos Graph that displays it for you https://www.desmos.com/calculator/12aiwozkjj
+
There is a maximum budget per script. This is determined by the total amount of RAM installed in all servers.  
 
 
{| class="wikitable"
 
!RAM (bytes)!!Actions!!Conditions!!Impulses
 
|-
 
|1||7||2||1
 
|-
 
|8||8||2||1
 
|-
 
|64||9||2||1
 
|-
 
|509||10||2||1
 
|-
 
|4056||11||2||2
 
|-
 
|32361||12||2||2
 
|-
 
|2.5823e5||13||2||2
 
|-
 
|2.0607e6||14||2||2
 
|-
 
|1.6445e7||15||2||2
 
|-
 
|1.3123e8||16||2||2
 
|-
 
|1.0472e9||17||2||2
 
|-
 
|8.3566e9||18||2||2
 
|-
 
|6.6686e10||19||2||2
 
|-
 
|5.3215e11||20||2||2
 
|-
 
|4.2466e12||21||3||2
 
|-
 
|3.3888e13||22||3||3
 
|-
 
|2.7042e14||23||3||3
 
|-
 
|2.1580e15||24||3||3
 
|-
 
|1.7221e16||25||3||3
 
|}
 
 
 
To get all 25 actions requires fully upgrading the RAM of all 16 servers.
 
  
<br />
+
To get a budget of 10,000 requires fully upgrading the RAM of all 16 servers.
  
== AI Script Language ==
+
==AI Script Language==
  
 
===Data Types===
 
===Data Types===
Line 183: Line 137:
 
The various inputs to a function are called '''arguments'''. Each argument of a function has a specific datatype and accepts either a constant value or a function with a return value of the same type.
 
The various inputs to a function are called '''arguments'''. Each argument of a function has a specific datatype and accepts either a constant value or a function with a return value of the same type.
  
<br />
+
=== APIs (Application Programming Interfaces) (as of v0.49) ===
 
+
Function signatures have been slightly modified to conform with a C-like syntax for similarity with other function signatures. The special return type <code>impulse</code> denotes a function is an input (rather than an action), actions which previously did not have a return type now have the <code>void</code> return type and the colon between type and argument name is removed.
=== APIs (Application Programming Interfaces) ===
+
{| class="wikitable"
{| class="wikitable sortable mw-collapsible"
+
|+Impulses
|+List of all AI APIs (as of v0.49 [2024:11:5])
 
 
!Name
 
!Name
!External editor code
+
!Function Signature
 
!Description
 
!Description
!Type
 
 
|-
 
|-
 
|Wake Up
 
|Wake Up
|<code>wakeup() -> impulse</code>
+
|<code>impulse wakeup()</code>
 
|Triggers whenever the AI switches from inactive to active.
 
|Triggers whenever the AI switches from inactive to active.
|Impulse
 
 
|-
 
|-
 
|New Round
 
|New Round
|<code>game.newround() -> impulse</code>
+
|<code>impulse game.newround()</code>
 
|Triggers whenever a new [[Tower Testing]] round starts.
 
|Triggers whenever a new [[Tower Testing]] round starts.
|Impulse
 
 
|-
 
|-
 
|Open: <[[Buildings|Building]]>
 
|Open: <[[Buildings|Building]]>
|<code>open.&lt;building&gt;() -> impulse</code>
+
|<code>impulse open.&lt;building&gt;()</code>
 
|Triggers whenever the window of the building is being opened (ex: Open: Arcade)
 
|Triggers whenever the window of the building is being opened (ex: Open: Arcade)
|Impulse
 
 
|-
 
|-
 
|Close: <[[Buildings|Building]]>
 
|Close: <[[Buildings|Building]]>
|<code>close.&lt;building&gt;() -> impulse</code>
+
|<code>impulse close.&lt;building&gt;()</code>
 
|Triggers whenever the window of the building is being closed (ex: Close: Arcade)
 
|Triggers whenever the window of the building is being closed (ex: Close: Arcade)
|Impulse
 
|-
 
|Key: <0-9, A-Z>
 
|<code>key.#() -> impulse</code>
 
|Triggers whenever the 0-9 or A-Z key is being pressed (case-insensitive, not numpad). (ex: Key: 0)
 
|Impulse
 
 
|-
 
|-
 
|<nowiki>Mouse: <Left|Middle|Right> Up</nowiki>
 
|<nowiki>Mouse: <Left|Middle|Right> Up</nowiki>
|<code>mouse.<id>.up() -> impulse</code>
+
|<code>impulse mouse.<id>.up()</code>
 
|<nowiki>Triggers whenever the <left|middle|right> mouse button is being pressed down. (ex: Mouse: Left Down)</nowiki>
 
|<nowiki>Triggers whenever the <left|middle|right> mouse button is being pressed down. (ex: Mouse: Left Down)</nowiki>
|Impulse
 
 
|-
 
|-
 
|<nowiki>Mouse: <Left|Middle|Right> Down</nowiki>
 
|<nowiki>Mouse: <Left|Middle|Right> Down</nowiki>
|<code>mouse.<id>.down() -> impulse</code>
+
|<code>impulse mouse.<id>.down()</code>
 
|<nowiki>Triggers whenever the <left|middle|right> mouse button is being pressed down. (ex: Mouse: Left Down)</nowiki>
 
|<nowiki>Triggers whenever the <left|middle|right> mouse button is being pressed down. (ex: Mouse: Left Down)</nowiki>
|Impulse
 
 
|-
 
|-
|Comparison: Bool
+
|Key: <0-9, A-Z>
|<code>comparison.bool(lhs: bool, op_comp: string, rhs: bool) -> bool</code>
+
|<code>impulse key.#()</code>
|<nowiki>Compares two boolean values based on the selected operator. Allowed operators are "=="/"=" (Equals), "!=" (Not Equal), "&&"/"&" (Both must be true), "||"/"|" (Either has to be true).</nowiki>
+
|Triggers whenever the 0-9 or A-Z key is being pressed (case-insensitive, not numpad). (ex: Key: 0)
|Condition
+
|}
 +
{| class="wikitable"
 +
|+Script Execution Context
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|Mouse: <Key>
 +
|<code>bool mouse.<key>.state()</code>
 +
|Returns true if the <key> mouse button is currently being held down.
 +
|-
 +
|Screen: Height
 +
|<code>int height()</code>
 +
|Returns the height of the screen in pixels.
 +
|-
 +
|Screen: Width
 +
|<code>int width()</code>
 +
|Returns the width of the screen in pixels.
 +
|-
 +
|UI: Size
 +
|<code>double ui.size()</code>
 +
|Returns the value of the UI size option from from the options menu as a double ranging from 0.5 (50%) to 1.0 (100%)
 +
|-
 +
|Script: Triggering Impulse
 +
|<code>string impulse()</code>
 +
|Returns the ID of the impulse which triggered this script instance. If the script was triggered by another script then this function will return the name of that script
 +
|-
 +
|Basic: Remaining Budget
 +
|<code>int budget()</code>
 +
|Returns the remaining execution budget of this script during execution.
 +
|-
 +
|Time: Frame
 +
|<code>int time.frame()</code>
 +
|Returns the total number of frames since the start of the game.
 +
|-
 +
|Time: Delta
 +
|<code>double time.delta()</code>
 +
|Returns the total time in seconds between the current frame and the last frame. Pausing or accelerating the game affects this value.
 +
|-
 +
|Time: Scale
 +
|<code>double time.scale()</code>
 +
|Returns the factor at which the game is currently accelerated. (0 if the game is paused, 1 if the game runs at normal speed 2 if the game runs at 2x speed and so on)
 +
|-
 +
|Time: Unscaled Delta
 +
|<code>double time.unscaled()</code>
 +
|Returns the total time in seconds between the current frame and the last frame. This value is neither affected by pausing nor accelerating the game.
 +
|-
 +
|Timestamp: Now
 +
|<code>double now()</code>
 +
|Returns the current local time in ticks representing the time from midnight , January 1st, 0001 until now. (UNIX time)
 +
|-
 +
|Timestamp: UTC Now
 +
|<code>double utcnow()</code>
 +
|Returns the UTC time in ticks representing the time from midnight , January 1st, 0001 until now. (UNIX time)
 +
|}
 +
{| class="wikitable"
 +
|+Execution Primitives
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|Basic: Goto
 +
|<code>void goto(int line)</code>
 +
|Jumps to line <code>line</code>. The first line in the script equals 1. Entering an invalid line number will result in the value being clamped between the min. and max. boundaries during execution.
 +
|-
 +
|Basic: Goto-If
 +
|<code>void goto(int line, bool condition)</code>
 +
|Jumps to line <code>line</code> in the current script if <code>condition</code> is True. First line equals 1.
 +
|-
 +
|Basic: Click
 +
|<code>void click(vector2 pos)</code>
 +
|Performs a mouse click at following location: <code>pos</code>.
 +
|-
 +
|Basic: Slider
 +
|<code>void slider(vector2 where, double value)</code>
 +
|Sets the slider position <code>where</code> on the screen to <code>value</code> with 0 being the leftmost and 1 being the rightmost end.
 +
|-
 +
|Basic: Scrollrect
 +
|<code>void scrollbar(vector2 where, double horizontal, double vertical)</code>
 +
|Scrolls within a scrollable container at <code>where</code> to <code>horizontal</code> (horizontally) and <code>vertical</code> (vertically) with 0 being the left/lower end and 1 being the right/upper end. Use a negative value to ignore an axis.
 +
|-
 +
|Basic: Wait
 +
|<code>void wait(double value)</code>
 +
|A simple wait function that stops the current script for a total of <code>value</code> seconds.
 +
|-
 +
|Basic: Wait Until
 +
|<code>void waituntil(bool condition)</code>
 +
|Stops the current script until <code>condition</code> becomes True.
 +
|-
 +
|Basic: Wait While
 +
|<code>void waitwhile(bool condition)</code>
 +
|Stops the current script as long as <code>condition</code> is True.
 +
|-
 +
|Basic: Wait Frame
 +
|<code>void waitframe()</code>
 +
|Stops execution of this script for this frame and continues in the next one.
 +
|-
 +
|Basic: Execute
 +
|<code>void execute(string script)</code>
 +
|Executes the first script in the list which is named <code>script</code> without requiring an impulse. Conditions of the script called still have to be fulfilled. Executed scripts are put at the end of the script list, and because of this they (usually) will run their first action in the same frame, since the script list pointer is still on the calling script, earlier in the script list.
 +
|-
 +
|Basic: Execute (Sync)
 +
|<code>void executesync(string script)</code>
 +
|Executes the first script in the list which is named <code>script</code> and waits until the execution is completed. Conditions of the script still have to be fulfilled.
 +
|-
 +
|Basic: Stop
 +
|<code>void stop(string script)</code>
 +
|Stops the execution of all scripts and script instances which are named <code>script</code>. '''Important bug/feature:''' If a script stops ''itself'', the script list pointer is reset to the beginning, effectively giving everything before this script an "extra" cycle. Because scripts that have reached the end aren't cleaned up until the end-of-frame processing, this means that scripts on their last line (or that have jumped to 99) will re-execute their last line, which can cause bugs. This behavior is known as "Turbo Exec," since it can be used to build programs that execute much faster than the typical one-action-per-frame.
 +
|-
 +
|Local: Get <T>
 +
|<code>T local.get(string var)</code>
 +
|Returns the value of the the local variable <code>var</code>.
 +
|-
 +
|Local: Set <T>
 +
|<code>void local.set(string var, T value)</code>
 +
|Creates or changes the the local variable <code>var</code> to contain the value <code>value</code>.
 +
|-
 +
|Local: Unset
 +
|<code>void local.unset(string var)</code>
 +
|Deletes the local variable with the name <code>var</code>.
 +
|-
 +
|Global: Get <T>
 +
|<code>T global.get(string var)</code>
 +
|Returns the value of the the global variable <code>var</code>.
 +
|-
 +
|Global: Set <T>
 +
|<code>void global.set(string var, T value)</code>
 +
|Creates or changes the the global variable <code>var</code> to contain the value <code>value</code>.
 +
|-
 +
|Global: Unset
 +
|<code>void global.unset(string var)</code>
 +
|Deletes the global variable with the name <code>var</code>.
 +
|}
 +
{| class="wikitable"
 +
|+Boolean Primitives
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Numerical Primitives
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Integer Primitives
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Double Primitives
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+String Primitives
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|String: Contains
 +
|<code>bool contains(string str, string substr)</code>
 +
|Returns true if the string <code>str</code> contains the string <code>substr</code>. (ex: "Catch" Contains "Cat")
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Vector Primitives
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Town APIs
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|<code>bool isopen(string: window)</code>
 +
|Returns true if the [[Buildings]] window is active and visible on the screen. (ex: [[Tower Testing]] is open)
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Tower Testing APIs
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|Tower: Is Stunned
 +
|<code>bool stunned()</code>
 +
|Returns true if the tower is stunned and false if the tower is either not stunned or does not exist.
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Power Plant APIs
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Mine APIs
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 
|-
 
|-
|Comparison: Int/Double
+
|
|<code>comparison.&lt;type&gt;(lhs: type, op_comp: string, rhs: type) -> bool</code>
+
|
|Compares two &lt;type&gt; values based on the selected operator. Allowed operators are "=="/"=" (Equals), "!=" (Not Equal), ">", ">=", "<", "<=".
+
|
|Condition
+
|}
 +
{| class="wikitable"
 +
|+Factory APIs
 +
!Name
 +
!Function Signature
 +
!Description
 
|-
 
|-
|Comparison: String
+
|
|<code>comparison.string(lhs: string, op_comp: string, rhs: string) -> bool</code>
+
|
|Compares two string values based on the selected operator. Allowed operators are "=="/"=" (Equals), "!=" (Not Equal).
+
|
|Condition
 
 
|-
 
|-
|Factory: Is Processing
+
|
|<code>active(machine: string) -> bool</code>
+
|
|Returns true if the machine with ID [[Machine]] is currently processing items. (ex: Factory: Is Processing (Oven)). Always returns false outside the factory.
+
|
|Condition
 
 
|-
 
|-
|Mine: Has Layers
+
|
|<code>hasLayers() -> bool</code>
+
|
|Returns true if the current active mining tab can generate at least 1 layer. Always returns false outside the mine.
+
|
|Condition
+
|}
 +
{| class="wikitable"
 +
|+Arcade APIs
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Trading Post APIs
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Museum APIs
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|Museum: Fill Inventory (deprecated)
 +
|<code>bool isfill()</code>
 +
|Returns the current Fill Inventory state. Always returns false outside the museum.
 +
|-
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+Worker APIs
 +
!
 +
!
 +
!
 +
!
 +
|-
 +
|
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|
 +
|-
 +
|
 +
|
 +
|
 +
|
 +
|}
 +
{| class="wikitable"
 +
|+User Interface APIs (requires boots.d0s)
 +
!Name
 +
!Function Signature
 +
!Description
 +
|-
 +
|Canvas: Draw Rect
 +
|<code>void canvas.rect(vector2 pos, vector2 size, string colour</code>
 +
|Draws a rectangle on the drawable canvas at location <code>pos</code> with a size of <code>size</code> and colored in <code>colour</code>. (Accepts #RRGGBB and #RRGGBBAA as inputs for color.)
 +
|-
 +
|Canvas: Clear
 +
|<code>void canvas.clear()</code>
 +
|Clears the drawable canvas instantly.
 +
|-
 +
|Window: Create
 +
|<code>void create(string windowId, string windowType)</code>
 +
|Creates a window with the unique identifier <code>windowId</code> (used to address the new instance) of type <code>windowType</code> (window name inside windows list).
 +
|-
 +
|Window: Set Text
 +
|
 +
|Sets the content inside the window with the id <code>str identifier</code> of the text label with the id <code>str id</code> to <code>str text</code>.
 +
|-
 +
|Window: Set Sprite
 +
|
 +
|Sets the sprite inside the window with the id <code>str identifier</code> of the button/image with the id <code>str id</code> to <code>str sprite</code>.
 +
|-
 +
|Window: Set Visibility
 +
|
 +
|Set the window with id <code>str id</code> to visible if <code>bool condition</code> is true. Otherwise it will be hidden.
 +
|-
 +
|Window: Set Child Visibility
 +
|
 +
|Set child of window with id <code>str id</code> with id <code>str id</code> to visible if <code>bool condition</code> is true. Otherwise window will be hidden.
 +
|-
 +
|Window: Set Position
 +
|
 +
|Changes the position of the window with id <code>str id</code> to <code>vector x, y</code> based on the anchor of its root element. Per default the anchor is set to the center of the window and 0,0 represents the center of the screen.
 +
|-
 +
|Window: Destroy
 +
|
 +
|Destroys the window with the unique identifier <code>str id</code>.
 +
|-
 +
|Window: Destroy All
 +
|
 +
|Destroys all active windows.
 
|}
 
|}
 
*Museum: Fill Inventory (<code>bool isfill()</code>)
 
**Returns the current Fill Inventory state. Always returns false outside the museum.
 
*String: Contains (<code>bool contains(string: str, string: substr)</code>)
 
**Returns true if the string <code>str</code> contains the string <code>substr</code>. (ex: "Catch" Contains "Cat")
 
*Tower: Is Stunned (<code>bool stunned()</code>)
 
**Returns true if the tower is stunned and false if the tower is either not stunned or does not exist.
 
*Town: Window Open (<code>bool isopen(string: window)</code>)
 
**Returns true if the [[Buildings]] window is active and visible on the screen. (ex: [[Tower Testing]] is open)
 
  
  
 
'''Action List:'''
 
'''Action List:'''
  
*Basic: Goto (<code>goto(int: line)</code>)
 
**Jumps to line <code>line</code>. The first line in the script equals 1. Entering an invalid line number will result in the value being clamped between the min. and max. boundaries during execution.
 
*Basic: Goto-If (<code>goto(int: line, bool: condition)</code>)
 
**Jumps to line <code>line</code> in the current script if <code>condition</code> is True. First line equals 1.
 
*Basic: Click (<code>click(vector: pos)</code>)
 
**Performs a mouse click at following location: <code>pos</code>.
 
*Basic: Slider (<code>slider(vector: where, double: value)</code>)
 
**Sets the slider position <code>where</code> on the screen to <code>value</code> with 0 being the leftmost and 1 being the rightmost end.
 
*Basic: Scrollrect (<code>scrollbar(vector: where, double: horizontal, double: vertical)</code>)
 
**Scrolls within a scrollable container at <code>where</code> to <code>horizontal</code> (horizontally) and <code>vertical</code> (vertically) with 0 being the left/lower end and 1 being the right/upper end. Use a negative value to ignore an axis.
 
*Basic: Wait (<code>wait(double: value)</code>)
 
**A simple wait function that stops the current script for a total of <code>value</code> seconds.
 
*Basic: Wait Until (<code>waituntil(bool: condition)</code>)
 
**Stops the current script until <code>condition</code> becomes True.
 
*Basic: Wait While (<code>waitwhile(bool: condition)</code>)
 
**Stops the current script as long as <code>condition</code> is True.
 
*Basic: Execute (<code>execute(string: script)</code>)
 
**Executes the first script in the list which is named <code>script</code> without requiring an impulse. Conditions of the script called still have to be fulfilled. Executed scripts are put at the end of the script list, and because of this they (usually) will run their first action in the same frame, since the script list pointer is still on the calling script, earlier in the script list.
 
*Basic: Execute (Sync) (<code>executesync(string: script)</code>)
 
**Executes the first script in the list which is named <code>script</code> and waits until the execution is completed. Conditions of the script still have to be fulfilled. '''As of 0.9.2, this has some bugs:''' If the conditions of the script are ''not'' fulfilled, <code>executesync</code> will wait forever without running anything. Also, if the executed script is killed with <code>stop</code>, execution will not continue.
 
*Basic: Stop (<code>stop(string: script)</code>)
 
**Stops the execution of all scripts and script instances which are named <code>script</code>. '''Important bug/feature:''' If a script stops ''itself'', the script list pointer is reset to the beginning, effectively giving everything before this script an "extra" cycle. Because scripts that have reached the end aren't cleaned up until the end-of-frame processing, this means that scripts on their last line (or that have jumped to 99) will re-execute their last line, which can cause bugs. This behavior is known as "Turbo Exec," since it can be used to build programs that execute much faster than the typical one-action-per-frame.
 
*Global: Set (Double / Int / String / Bool / Vector)
 
**Creates or changes the global variable <code>str variable</code> to contain the value <code>double/int/string/bool/vector2 value</code> .
 
*Global (Unset)
 
**Deletes the global variable with the name  <code>str name</code>.
 
*Local: Set (Double / Int / String / Bool / Vector))
 
**Creates or changes the local variable <code>str name</code> to contain the value <code>double/int/string/bool/vector2 value</code>.
 
*Local: (Unset)
 
**Deletes the local variable with the name <code>str name</code>.
 
*Canvas: Draw Rect
 
**Draws a rectangle on the drawable canvas at location <code>vector x, y</code> with a size of <code>vector x, y</code> and colored in <code>str color</code>. (Accepts #RRGGBB and #RRGGBBAA as inputs for color.)
 
*Canvas: Clear
 
**Clears the drawable canvas instantly.
 
*Window: Create
 
**Creates a window with the unique identifier <code>str identifier</code> (used to address the new instance) of type <code>str type</code> (window name inside windows list).
 
*Window: Set Text
 
**Sets the content inside the window with the id <code>str identifier</code> of the text label with the id <code>str id</code> to <code>str text</code>.
 
*Window: Set Sprite
 
**Sets the sprite inside the window with the id <code>str identifier</code> of the button/image with the id <code>str id</code> to <code>str sprite</code>.
 
*Window: Set Visibility
 
**Set the window with id <code>str id</code> to visible if <code>bool condition</code> is true. Otherwise it will be hidden.
 
*Window: Set Child Visibility
 
**Set child of window with id <code>str id</code> with id <code>str id</code> to visible if <code>bool condition</code> is true. Otherwise window will be hidden.
 
*Window: Set Position
 
**Changes the position of the window with id <code>str id</code> to <code>vector x, y</code> based on the anchor of its root element. Per default the anchor is set to the center of the window and 0,0 represents the center of the screen.
 
*Window: Destroy
 
**Destroys the window with the unique identifier <code>str id</code>.
 
*Window: Destroy All
 
**Destroys all active windows.
 
 
*Tower: Use (Instantly)
 
*Tower: Use (Instantly)
 
**Orders the Tower to use the [[Modules]] at slot <code>int slot</code> where slot 1 refers to the module at the very top of the skill menu. Entering an invalid slot will do nothing.
 
**Orders the Tower to use the [[Modules]] at slot <code>int slot</code> where slot 1 refers to the module at the very top of the skill menu. Entering an invalid slot will do nothing.

Revision as of 18:37, 8 November 2024

The facility AI (short for "Artificial Intelligence") is a feature of the Headquarters that unlocks upon reaching Military Tier 4 and finishing the associated software. It is an extremely versatile tool that allows the user to automate almost any task in the game.

Compared to workers, AI scripts are far more versatile as they can interact with almost every system in the game and even for systems for which there is no current APIs that the AI could use to interact it can always perform mouse clicks to simulate such behavior. This is currently limited as the AI can only click on in-game UI buttons so things like region artifacts must still be performed manually, additionally the AI cannot perform right or middle mouse clicks. However AI cannot run while the game is closed (or more accurately during the simulated time when opening a save file) or interact with buildings which are not open. Workers can do both of these things, appear before AI (MT1 rather than MT4) and are far simpler to use without community assistance so while AI may have more power to automate the game, neither is a replacement for the other.

To use the AI you must first obtain a script to execute. There are several ways to do this:

  • Create an AI script in the headquarters. For more information on learning how to create scripts, see Using the AI.
  • Obtain import codes for scripts that other players have written. For a list of available scripts, see the discord forum.
  • Use machine learning (a macro system). This is done by pressing F7 while the AI overlay is active.

AI Script Format

An AI script contains three sections that control its behavior:

  • Impulses
  • Conditions
  • Actions

All scripts have a limit of impulses, conditions and actions. Impulses and conditions have a cap of 10, actions a cap pf 50.

Impulses

An Impulse can refer to any button press or event that directly activates an AI script. It is not to be confused with the Basic:Execute command which is not considered an impulse, as it is not a way for a player to directly trigger an AI script, but rather a way of calling the script inside of another AI script much like helper methods in high level programming languages. Each impulse can trigger the AI script, regardless if it has already been triggered. It is possible that a script can trigger multiple times before it has finished execution. In this case multiple instances of this script can run in parallel.

Conditions

A Condition is a requirement that has to be fulfilled in order for the script to start executing. If any condition in the script is false then the script will not be executed when an impulse is triggered by a player action. During the execution the specified conditions have no effect. All conditions are of data Type:Bool, and any expression that returns a bool can be used as a condition

Actions

An Action is something that the AI does when the script becomes active. The Actions are executed one by one, in order from top to bottom. Actions will continue to be executed until either the AI menu is closed, or the script terminates normally. Within one instance of a script, as many actions as the script budget will allow can be executed in one frame. Atomic actions (those with a red symbol) cost 0 budget, all others cost 100. The script is executed in sequence until the budget is reached at which point execution is given to the next script.

AI-Script-Editor (Ingame)
AI-Script-Editor (In game)


There is a maximum budget per script. This is determined by the total amount of RAM installed in all servers.

To get a budget of 10,000 requires fully upgrading the RAM of all 16 servers.

AI Script Language

Data Types

There are various data types that are usable within AI scripts, all of which are incredibly versatile and useful in creating a script as complex or simple as one desires.

Type Description Example Values Default Value Notes
double A integer that allows decimal precision. Can be positive or negative. 3.2, 0.29, -10.2, 7.9999993 0.0 Doubles have a max value of approximately 1.797 × 10308.
int A number that does not allow decimal precision. Can be positive or negative. 20, 69, 420, -1029, 0 0 Ints range from -2147483648 to 2147483647
string An array of characters. Basically a way to format and use text. "meow", "hello", " ", "I am a text", "!BanBudE" "" The quotes in the examples are not included in the actual string value.
bool A binary value that can either be true or false. true, false false
Vector2 A container type that contains two double values called x and y. (-30.0, 0.0), (28.38, 13) (0.0, 0.0)

Some datatypes can be converted to others by using a function. Check the table below to see which datatypes are currently interchangeable.

Source / Target double int string bool Vector2
double - Yes Yes No Partially
int Yes - Yes No No
string Yes Yes - No No
bool No No No - No
Vector2 Partially No No No -

(The table will be updated as soon as more AI features are available.)

Functions

Any line in an AI script (apart from impulses) represents a function.

There are two major types of functions:

  • Without a return value (= Actions)
  • With a return value

Functions without a return value appear as actions in the sidebar of the AI-script editor. In general these functions do something specific but require some sort of input.

The various inputs to a function are called arguments. Each argument of a function has a specific datatype and accepts either a constant value or a function with a return value of the same type.

APIs (Application Programming Interfaces) (as of v0.49)

Function signatures have been slightly modified to conform with a C-like syntax for similarity with other function signatures. The special return type impulse denotes a function is an input (rather than an action), actions which previously did not have a return type now have the void return type and the colon between type and argument name is removed.

Impulses
Name Function Signature Description
Wake Up impulse wakeup() Triggers whenever the AI switches from inactive to active.
New Round impulse game.newround() Triggers whenever a new Tower Testing round starts.
Open: <Building> impulse open.<building>() Triggers whenever the window of the building is being opened (ex: Open: Arcade)
Close: <Building> impulse close.<building>() Triggers whenever the window of the building is being closed (ex: Close: Arcade)
Mouse: <Left|Middle|Right> Up impulse mouse.<id>.up() Triggers whenever the <left|middle|right> mouse button is being pressed down. (ex: Mouse: Left Down)
Mouse: <Left|Middle|Right> Down impulse mouse.<id>.down() Triggers whenever the <left|middle|right> mouse button is being pressed down. (ex: Mouse: Left Down)
Key: <0-9, A-Z> impulse key.#() Triggers whenever the 0-9 or A-Z key is being pressed (case-insensitive, not numpad). (ex: Key: 0)
Script Execution Context
Name Function Signature Description
Mouse: <Key> bool mouse.<key>.state() Returns true if the <key> mouse button is currently being held down.
Screen: Height int height() Returns the height of the screen in pixels.
Screen: Width int width() Returns the width of the screen in pixels.
UI: Size double ui.size() Returns the value of the UI size option from from the options menu as a double ranging from 0.5 (50%) to 1.0 (100%)
Script: Triggering Impulse string impulse() Returns the ID of the impulse which triggered this script instance. If the script was triggered by another script then this function will return the name of that script
Basic: Remaining Budget int budget() Returns the remaining execution budget of this script during execution.
Time: Frame int time.frame() Returns the total number of frames since the start of the game.
Time: Delta double time.delta() Returns the total time in seconds between the current frame and the last frame. Pausing or accelerating the game affects this value.
Time: Scale double time.scale() Returns the factor at which the game is currently accelerated. (0 if the game is paused, 1 if the game runs at normal speed 2 if the game runs at 2x speed and so on)
Time: Unscaled Delta double time.unscaled() Returns the total time in seconds between the current frame and the last frame. This value is neither affected by pausing nor accelerating the game.
Timestamp: Now double now() Returns the current local time in ticks representing the time from midnight , January 1st, 0001 until now. (UNIX time)
Timestamp: UTC Now double utcnow() Returns the UTC time in ticks representing the time from midnight , January 1st, 0001 until now. (UNIX time)
Execution Primitives
Name Function Signature Description
Basic: Goto void goto(int line) Jumps to line line. The first line in the script equals 1. Entering an invalid line number will result in the value being clamped between the min. and max. boundaries during execution.
Basic: Goto-If void goto(int line, bool condition) Jumps to line line in the current script if condition is True. First line equals 1.
Basic: Click void click(vector2 pos) Performs a mouse click at following location: pos.
Basic: Slider void slider(vector2 where, double value) Sets the slider position where on the screen to value with 0 being the leftmost and 1 being the rightmost end.
Basic: Scrollrect void scrollbar(vector2 where, double horizontal, double vertical) Scrolls within a scrollable container at where to horizontal (horizontally) and vertical (vertically) with 0 being the left/lower end and 1 being the right/upper end. Use a negative value to ignore an axis.
Basic: Wait void wait(double value) A simple wait function that stops the current script for a total of value seconds.
Basic: Wait Until void waituntil(bool condition) Stops the current script until condition becomes True.
Basic: Wait While void waitwhile(bool condition) Stops the current script as long as condition is True.
Basic: Wait Frame void waitframe() Stops execution of this script for this frame and continues in the next one.
Basic: Execute void execute(string script) Executes the first script in the list which is named script without requiring an impulse. Conditions of the script called still have to be fulfilled. Executed scripts are put at the end of the script list, and because of this they (usually) will run their first action in the same frame, since the script list pointer is still on the calling script, earlier in the script list.
Basic: Execute (Sync) void executesync(string script) Executes the first script in the list which is named script and waits until the execution is completed. Conditions of the script still have to be fulfilled.
Basic: Stop void stop(string script) Stops the execution of all scripts and script instances which are named script. Important bug/feature: If a script stops itself, the script list pointer is reset to the beginning, effectively giving everything before this script an "extra" cycle. Because scripts that have reached the end aren't cleaned up until the end-of-frame processing, this means that scripts on their last line (or that have jumped to 99) will re-execute their last line, which can cause bugs. This behavior is known as "Turbo Exec," since it can be used to build programs that execute much faster than the typical one-action-per-frame.
Local: Get <T> T local.get(string var) Returns the value of the the local variable var.
Local: Set <T> void local.set(string var, T value) Creates or changes the the local variable var to contain the value value.
Local: Unset void local.unset(string var) Deletes the local variable with the name var.
Global: Get <T> T global.get(string var) Returns the value of the the global variable var.
Global: Set <T> void global.set(string var, T value) Creates or changes the the global variable var to contain the value value.
Global: Unset void global.unset(string var) Deletes the global variable with the name var.
Boolean Primitives
Name Function Signature Description
Numerical Primitives
Name Function Signature Description
Integer Primitives
Name Function Signature Description
Double Primitives
Name Function Signature Description
String Primitives
Name Function Signature Description
String: Contains bool contains(string str, string substr) Returns true if the string str contains the string substr. (ex: "Catch" Contains "Cat")
Vector Primitives
Name Function Signature Description
Town APIs
Name Function Signature Description
bool isopen(string: window) Returns true if the Buildings window is active and visible on the screen. (ex: Tower Testing is open)
Tower Testing APIs
Name Function Signature Description
Tower: Is Stunned bool stunned() Returns true if the tower is stunned and false if the tower is either not stunned or does not exist.
Power Plant APIs
Name Function Signature Description
Mine APIs
Name Function Signature Description
Factory APIs
Name Function Signature Description
Arcade APIs
Name Function Signature Description
Trading Post APIs
Name Function Signature Description
Museum APIs
Name Function Signature Description
Museum: Fill Inventory (deprecated) bool isfill() Returns the current Fill Inventory state. Always returns false outside the museum.
Worker APIs
User Interface APIs (requires boots.d0s)
Name Function Signature Description
Canvas: Draw Rect void canvas.rect(vector2 pos, vector2 size, string colour Draws a rectangle on the drawable canvas at location pos with a size of size and colored in colour. (Accepts #RRGGBB and #RRGGBBAA as inputs for color.)
Canvas: Clear void canvas.clear() Clears the drawable canvas instantly.
Window: Create void create(string windowId, string windowType) Creates a window with the unique identifier windowId (used to address the new instance) of type windowType (window name inside windows list).
Window: Set Text Sets the content inside the window with the id str identifier of the text label with the id str id to str text.
Window: Set Sprite Sets the sprite inside the window with the id str identifier of the button/image with the id str id to str sprite.
Window: Set Visibility Set the window with id str id to visible if bool condition is true. Otherwise it will be hidden.
Window: Set Child Visibility Set child of window with id str id with id str id to visible if bool condition is true. Otherwise window will be hidden.
Window: Set Position Changes the position of the window with id str id to vector x, y based on the anchor of its root element. Per default the anchor is set to the center of the window and 0,0 represents the center of the screen.
Window: Destroy Destroys the window with the unique identifier str id.
Window: Destroy All Destroys all active windows.


Action List:

  • Tower: Use (Instantly)
    • Orders the Tower to use the Modules at slot int slot where slot 1 refers to the module at the very top of the skill menu. Entering an invalid slot will do nothing.
  • Tower: Use (Position)
    • Orders the tower to use the module at slot int slot where slot 1 refers to the module at the very top of the skill menu at an offset of vector x,y
  • Tower: Restart
    • Restarts the current Tower Testing run. Can only be can only be executing during or at the end of a run that took equal or longer than a second.
  • Tower: Exit
    • Exits the current Tower Testing run.
  • Software: Toggle
    • Enables software software if the condition bool condition is true or disables it if the condition is false.
  • Era: Disable Power
    • Tries to disable the era powers of element enemies by purchasing the according upgrade using xp.
  • Era: Upgrade Divider
    • Attempts to upgrade the era str (damage, health) divider a total of int x times by using xp.
  • Infinity: Secure Module
    • Attempts to secure the module with id str id to prevent enemies from mimicking it during the infinity phase.
  • Town: Open Window
    • Opens the str building window if bool conditionis True, otherwise it will be closed. Opening or closing windows this way does not play the transition animation!
  • Workers: Toggle Group
    • Toggles the paused state of all workers in group int group. (Click on the parameter to see which color belongs to which group id.)
    • Colors: White (0), Red (1), Blue (2), Green(3), Yellow (4), Magenta (5)
  • Workers: Pause Group
    • Sets the paused state of all workers in group int group to bool value. (Click on the parameter to see which color belongs to which group id.)
  • Workers: Toggle
    • Toggles the paused state of all workers with name str name.
  • Workers: Pause
    • Sets the paused state of all workers with the name str name to bool value.
  • Workers: Assign Group
    • Assigns the task with id str id and optional parameter int param (0 is the leftmost) to all workers in group int group. (Click on the groups parameter to see which color belongs to which group.)
  • Workers: Assign
    • Assigns Task with ID str id and optional parameter int param (0 is the leftmost) to all workers with the name str name.
    • TaskIDs
  • Workers: Set Name
    • Sets the name of the worker at index int index to str name.
  • Worker: Set Group
    • Sets the group of the worker at index int index (0 is the first worker) to the group with id int id.
  • Mine: Dig
    • Digs up the tile at X: int x and Y: int y of the currently selected resource in the Mine with (0, 0) being the top left corner. Only works if the Mine window is active!
  • Mine: New Layer
    • Generates a new layer of the currently selected resource in the Mine. Only works if the Mine window is active!
  • Mine: Open Tab
    • Opens the mining tab at position int position. Position 1 is the first tab (Orange) and position 12 is the last tab (Black).
  • Mine: Delete Cluster
    • Removes the asteroid cluster at list position int position where 1 represents the first cluster in the list.
  • Arcade: Spin Lucky Wheel
    • Spins the Lucky Wheel with a wager of double wager.
  • Arcade: Jumble New Game
    • Starts a new game of Jumble with a wager of double wager.
  • Arcade: Jumble Stop
    • Stops the current column.
  • Adventure: Move
    • Moves the player in the direction of vector x,y in Adventure.
  • Adventure: Wait
    • Skips a turn in Adventure.
  • Adventure: Place Bomb
    • Places a Bomb at the Player location in Adventure.
  • Factory: Try Craft
    • Tries to craft str item (Tier int tier) a total of double total times by using items inside the inventory or crafting grid. If that is not possible then no items are being crafted. Only works if the Factory screen is visible.
  • Factory: Try Produce
    • Tries to put double amount x str item (Tint tier) into machine str machine. If too few items are available or the selected machine is currently busy with a different item type then nothing will happen. Ignores the tier if the selected item ID is rubber.
  • Factory: Trash
    • Tries to put double amount x str item (Tint tier) into the trash can of the Factory. If too few items are present in the inventory and the crafting grid of the Factory then it will still try to remove as many as possible. Ignores the tier for items that don't have a tier.
  • Factory: Cancel Machine
    • Stops production of the Factory machine with the id str machine and ejects all items back to the inventory if possible.
  • Powerplant: Sell
    • Sells the Power Plant component at X: int x and Y: int y and automatically refunds a part of the selling price. If the selected slot is empty then nothing will happen. (0, 0) is the bottom left corner of the grid.
  • Tradingpost: Refresh
    • Generates a new set of offers in the Trading Post. Requires a specific upgrade for the Trading Post later in the game in order to be used!
  • Tradingpost: Trade
    • Trades the offer at position int position in the list of offers (first offer is at position 0) using double percent (0.15 = 15%) of the available input resources.
  • Museum: Buy
    • Buys a str element Power Stone of tier int tier from the shop or market int amount times.
  • Museum: Buy Range
    • Buys a str element Power Stone between tier int tier and int tier from the shop or market int amount times.
  • Museum: Combine
    • Combine Power Stones with the usual combine rules up to Tier int tier (<1 means no limit).
  • Museum: Transmute
    • Transmute Power Stones currently inside the Cubos Cube.
  • Museum: Move
    • Move a Power Stone from str location in slot int slot to str location.
  • Museum: Move Slot
    • Move a Power Stone from str location slot int slot to str location int slot.
  • Museum: Swap
    • Swap the Power Stones in str location slot int slot and str location int slot.
  • Museum: Sell
    • Sell the Power Stone from str location in slot int slot.
  • Museum: Sell All
    • Sell all Power Stones from str location.
  • Museum: Set Preferred Tier
    • Sets the preferred market tier to int tier.
  • Museum: Set Preference
    • Set the market preference for element str element to bool value.
  • Museum: Market Refresh
    • Refreshes Market Offers
  • Museum: Buy Market
    • Buy the Power Stone from market in slot int slot int amount times.
  • Museum: Lock Market Slot
    • Set the lock state of Market slot int slot to bool value.
  • Museum: Rebuy
    • Rebuy the Power Stone from Trash slot int slot.
  • Museum: Fill Inventory (Deprecated)
    • Sets the Fill Inventory button to the bool state state.
  • Museum: Buy (Deprecated)
    • Buy Power Stone(s) with element str element
  • Museum: Buy Market (Deprecated)
    • Buy Power Stone(s) with element str element up to Tier int tier.