Difference between pages "Batfactors" and "Data Types"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
(link to batfactors.txt data file)
 
(→‎int: is 64 bit)
 
Line 1: Line 1:
{{TOCright}}{{DISPLAYTITLE:batfactors (batfactors.txt)}}
+
{{TOCright}}
== What is batfactors? ==
+
==Primitive Datatypes==
[https://zachbardon.com/mafiatools/autoupdate.php?f=batfactors&act=getmap batfactors.txt] is a data file used by [[BatBrain]], containing information about all the various factors pertinent to battle.  These are saved in a data-entry-friendly format rather than just saving a map of advevents, which would be bulkier and harder to edit.  The vast majority of BatBrain's knowledge about items, skills, equipment, monsters, etc. is found in this file.  As it is publicly editable on the Map Manager, a guide to understanding the format could be handy for someone looking to either add new content or fix old or previously unspaded content.
 
  
== The Format ==
+
===void===
 +
Can be thought of better as the absence of a datatype. No value can be assigned to '''void''' nor can a value be returned from a function of datatype '''void'''.
  
Like all mafia data files, batfactors is a map file, in the following format:
+
===boolean===
 +
A boolean value is either '''true''' or '''false'''. By default, a boolean variable is set to false.
  
 +
===int===
 +
 +
A whole number (short for "integer"), either positive or negative (or 0).  The int used by KoLmafia is a 64-bit signed int, meaning it has a maximum value of 9,223,372,036,854,775,807 and a minimum value of -9,223,372,036,854,775,808. The default value of a integer variable is 0.
 +
 +
Be careful when doing math with integers! As with some other strongly-typed languages, numbers are converted to integers at every step of the operation when only integer types are used. For example:
 +
{{
 +
CodeSample|
 +
code=
 +
<syntaxhighlight>
 +
int a = 1;
 +
int b = 2;
 +
print( a / b * 2 );
 +
</syntaxhighlight>}}
 +
Will give the output "0," not "1" as you may expect. Changing either variable to a float type will "correct" this.
 +
 +
===float===
 +
 +
The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in [http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 section 4.2.3 of the Java Language Specification], and a tool to help understand / quickly see how/where imprecisions happen can be found [https://www.h-schmidt.net/FloatConverter/IEEE754.html here].
 +
 +
When assigning to a variable of type float, one should be careful to always enter numbers in decimal form, as unwanted behavior can result from supplying a value that KoLmafia may interpret as an int type without the decimal point.
 +
 +
Note that float is not infinitely precise; it intrinsically rounds off after a certain point. This loss of accuracy is for the sake of storage, but beware of the possibility of small errors compounding from multiple float types.
 +
{{
 +
CodeSample|
 +
description=For instance, try the following code as an example of how rather long post-decimal portions are handled:|
 +
code=
 +
<syntaxhighlight>
 +
float f;
 +
f = 4.9999999;
 +
print( f );
 +
</syntaxhighlight>}}
 +
The default value of a <code>float</code> variable is 0.0.
 +
 +
===string===
 +
 +
A group of characters including, but not limited to: lowercase letters, uppercase characters, numbers, and various control characters. When assigning a value to a string, enclose the desired value in either single quotes (<code>'</code>), double quotes (<code>"</code>) or backticks (<code>`</code>) (note you must use the same quote type on both ends of the string assignment). If you need to include the same character inside of the string itself, you will need to escape it first with a backslash.
 +
 +
{{
 +
CodeSample
 +
|description=For example:
 +
|code=<syntaxhighlight>string s = "This is my \"friend\" Pete.";</syntaxhighlight>
 +
|moreinfo=Will result in the following being stored to s:<pre>This is my "friend" Pete</pre>
 +
}}
 +
 +
The default value of a string is an empty string, or literally <code>""</code>
 +
 +
When using backticks, you can concatenate expressions into your string using curly braces.
 +
 +
{{
 +
CodeSample
 +
|description=For example:
 +
|code=<syntaxhighlight>string s = `Hello {my_name()}, you have {$item[lime].item_amount()} lime(s)!`;</syntaxhighlight>
 +
}}
 +
 +
===buffer===
 +
 +
Similar to a string, but more efficient in certain operations, including concatenation and passing as function arguments. For the most part, you can interchange references to strings and buffers. However, you should test all such actions first, as a few functions require a specific datatype to be supplied as a parameter. (Most notably, those listed under [[String Handling Routines#Regular Expressions|Regular Expressions]].)
 +
 +
==Special Datatypes==
 +
 +
Several datatypes are included in KoLmafia to represent common categories within the KoL universe.
 +
 +
Note that while variables of these types are declared in the same way as for Primitive Datatypes; assigning and referencing them is done differently.
 +
{{CodeSample|
 +
description=For example, to declare an item datatype and assign it a value, you would use the following line of code:|
 +
code=
 +
<syntaxhighlight>
 +
item it = $item[ broken skull ];
 +
</syntaxhighlight>}}
 +
The default values of any variable of one of the following types is <code>$''type''[ none ]</code>. For example, the default value of a <code>item</code> variable is <code>$item[ none ]</code>.
 +
 +
===bounty===
 +
 +
([[Bounty#Related Functions|Related Functions]])  ([[Bounty#Proxy Record Fields|Proxy Record Fields]])
 +
 +
These are the non-items that the bounty hunter asks you to retrieve from monsters around the kingdom. There are quite a lot of them (42 in total) so I won't list them all here. You can find more about bounties at the {{kolwiki|The Bounty Hunter Hunter's Shack}}.
 +
 +
===class===
 +
 +
([[Class#Related Functions|Related Functions]])  ([[Class#Proxy Record Fields|Proxy Record Fields]])
 +
 +
Besides $class[ none ], there are six possible values for this datatype:
 +
 +
* Seal Clubber
 +
* Turtle Tamer
 +
* Pastamancer
 +
* Sauceror
 +
* Disco Bandit
 +
* Accordion Thief
 +
 +
===coinmaster===
 +
 +
([[Coinmaster#Related Functions|Related Functions]])  ([[Coinmaster#Proxy Record Fields|Proxy Record Fields]])
 +
 +
All shops that deal with currency other than meat. Known values include:
 +
 +
* {{kolwiki|The Bounty Hunter Hunter's Shack|Bounty Hunter Hunter}}
 +
* {{kolwiki|Mr. Store}}
 +
* {{kolwiki|Hermit}}
 +
* {{kolwiki|The Shore, Inc. Gift Shop}}
 +
* {{kolwiki|The Trapper's Cabin|The Trapper}}
 +
* {{kolwiki|A Vending Machine|Vending Machine}}
 +
* {{kolwiki|The Swagger Shop}}
 +
* {{kolwiki|The Hippy Camp (Wartime)|Dimemaster}}
 +
* {{kolwiki|The Orcish Frat House|Quartersmaster}}
 +
* {{kolwiki|BURT|Bugbear Token}}
 +
* {{kolwiki|Freshwater Fishbonery}}
 +
* {{kolwiki|Big Brother}}
 +
* {{kolwiki|The Terrified Eagle Inn}}
 +
* {{kolwiki|Ticket Redemption Counter|Arcade Ticket Counter}}
 +
* {{kolwiki|Cashier|Game Shoppe}}
 +
* {{kolwiki|Cashier|Game Shoppe Snacks}}
 +
* {{kolwiki|The Isotope Smithery|Isotope Smithery}}
 +
* {{kolwiki|Dollhawker's Emporium}}
 +
* {{kolwiki|The Lunar Lunch-o-Mat|Lunar Lunch-o-Mat}}
 +
* {{kolwiki|Paul's Boutique}}
 +
* {{kolwiki|The Frozen Brogurt Stand}}
 +
* {{kolwiki|Buff Jimmy's Souvenir Shop}}
 +
* {{kolwiki|Taco Dan's Taco Stand}}
 +
* {{kolwiki|You're the Fudge Wizard Now, Dog|Fudge Wand}}
 +
* {{kolwiki|The Neandermall}}
 +
* {{kolwiki|Legitimate Shoe Repair, Inc.}}
 +
* {{kolwiki|warbear black box|Warbear Black Box}}
 +
* {{kolwiki|The Dinsey Company Store}}
 +
* {{kolwiki|Internet Meme Shop}}
 +
 +
===effect===
 +
 +
([[Effect#Related Functions|Related Functions]])  ([[Effect#Proxy Record Fields|Proxy Record Fields]])
 +
 +
Any effect you can be under in KoL, whether from items, skills, or what-have-you, is valid for this datatype.
 +
 +
The full range, besides $effect[ none ], is too much to list and keep up with here: please see the Wiki {{kolwiki|Effects}} page for more information.
 +
 +
===element===
 +
 +
([[Element#Related Functions|Related Functions]])  ([[Element#Proxy Record Fields|Proxy Record Fields]])
 +
 +
Besides $element[ none ], there are seven possible values for this datatype. (Note that "Bad Spelling" is not considered a true element.)
 +
Also note that these names are case-sensitive (referencing $element[ Spooky ] will generate an error).
 +
 +
* cold
 +
* hot
 +
* sleaze
 +
* spooky
 +
* stench
 +
* slime
 +
* supercold
 +
 +
===familiar===
 +
 +
([[Familiar#Related Functions|Related Functions]])  ([[Familiar#Proxy Record Fields|Proxy Record Fields]])
 +
 +
Any familiar available in KoL is valid for this datatype.
 +
 +
The full range, besides $familiar[ none ], is too much to list and keep up with here: please see the Wiki {{kolwiki|Familiars}} page for more information.
 +
 +
===item===
 +
 +
([[Item#Related Functions|Related Functions]])  ([[Item#Proxy Record Fields|Proxy Record Fields]])
 +
 +
Any item in all of KoL is valid for this datatype. Note that unlike most special datatypes, item references can make use of the item ID number.
 +
{{CodeSample|
 +
description=For example, you could assign the item plexiglass pants as follows:|
 +
code=
 
<syntaxhighlight>
 
<syntaxhighlight>
record combat_rec {
+
item it = $item[ 1234 ];
  string ufname;          // user-friendly name, not really used by BatBrain
+
</syntaxhighlight>}}
  string dmg;            // damage to monster
+
The full range, besides $item[ none ], is too much to list and keep up with here: please see the Wiki {{kolwiki|Items}} page for more information.
  string pdmg;            // damage to player
+
 
  string special;        // comma-delimited list of other action results
+
===location===
};
+
 
combat_rec [string, int] factors;
+
([[Location#Related Functions|Related Functions]])  ([[Location#Proxy Record Fields|Proxy Record Fields]])
</syntaxhighlight>
+
 
 +
Any location one can adventure at in KoL is valid for this datatype.
 +
 
 +
The full range, besides $location[ none ], is too much to list and keep up with here: please see the Wiki {{kolwiki|Locations}} page for more information.
 +
 
 +
===monster===
 +
 
 +
([[Monster#Related Functions|Related Functions]])  ([[Monster#Proxy Record Fields|Proxy Record Fields]])
 +
 
 +
Any monster you can encounter in KoL is valid for this datatype.
 +
 
 +
The full range, besides $monster[ none ], is too much to list and keep up with here: please see the Wiki {{kolwiki|Monster Compendium}} page for more information.
 +
 
 +
===path===
 +
 
 +
([[Path#Related Functions|Related Functions]]) ([[Path#Proxy Record Fields|Proxy Record Fields]])
 +
 
 +
Any ascension path you can choose in Valhalla is valid for this datatype. This includes all {{kolwiki|special challenge paths}}, as well as Standard and the dietary restriction paths (Boozetafarian, Teetotaler, Oxygenarian). The "Unrestricted" path and aftercore are represented as $path[ none ].
 +
 
 +
The full range, besides $path[ none ], is too much to list and keep up with here. See {{kolwiki|Ascension#Paths}} for more information.
 +
 
 +
===phylum===
 +
 
 +
([[Phylum#Related Functions|Related Functions]])  ([[Phylum#Proxy Record Fields|Proxy Record Fields]])
 +
 
 +
Each monster has a {{kolwiki|Phylum}}. Besides $phylum[none], the possible values for this datatype are:
 +
 
 +
* beast
 +
* bug
 +
* constellation
 +
* construct
 +
* demon
 +
* dude
 +
* elemental
 +
* elf
 +
* fish
 +
* goblin
 +
* hippy
 +
* hobo
 +
* humanoid
 +
* horror
 +
* mer-kin
 +
* orc
 +
* penguin
 +
* pirate
 +
* plant
 +
* slime
 +
* undead
 +
* weird
 +
 
 +
===servant===
 +
 
 +
([[Servant#Related Functions|Related Functions]])  ([[Servant#Proxy Record Fields|Proxy Record Fields]])
 +
 
 +
Servants are unique to the {{kolwiki|Actually Ed the Undying}} path and located {{kolwiki|The Servants' Quarters}}, replacing familiars.
 +
 
 +
* Assassin
 +
* Belly-Dancer
 +
* Bodyguard
 +
* Cat
 +
* Maid
 +
* Priest
 +
* Scribe
 +
 
 +
===skill===
 +
 
 +
([[Skill#Related Functions|Related Functions]])  ([[Skill#Proxy Record Fields|Proxy Record Fields]])
 +
 
 +
Any skill you can have in KoL, whether permable or not, granted by items, ''etc''., is valid for this datatype.
 +
 
 +
The full range, besides $skill[ none ], is too much to list and keep up with here: please see the Wiki {{kolwiki|Skills}} page for more information.
 +
 
 +
===slot===
 +
 
 +
([[Slot#Related Functions|Related Functions]])  (slot has no proxy record fields)
 +
 
 +
Besides $slot[ none ], there are 16 possible values for this datatype.
 +
 
 +
* hat
 +
* back
 +
* weapon
 +
* off-hand
 +
* shirt
 +
* pants
 +
* acc1
 +
* acc2
 +
* acc3
 +
* familiar
 +
* sticker1
 +
* sticker2
 +
* sticker3
 +
* fakehand
 +
* bootspur
 +
* bootskin
  
Note that the map has two indices.  The first string represents the category.  The second index is the integer identifying the relevant item/skill/familiar.  For example the "bander" category, which contains information about your Bandersnatch's enhancements to combat skills, is indexed by skill number.
+
===stat===
  
{| class="wikitable"
+
([[Stat#Related Functions|Related Functions]])  (stat has no proxy record fields)
!Category
 
!Indexed by
 
!What?
 
|-
 
|bander
 
|skill number
 
|Your Bandersnatch's enhancements to the specified skill.
 
|-
 
|cadenza
 
|accordion item number
 
|The results of Cadenza using the specified accordion.
 
|-
 
|canhandle
 
|beans item number
 
|The results of Canhandle when wielding the specified can of beans.
 
|-
 
|chef
 
|staff item number
 
|Jiggle results for the specified chefstaff.
 
|-
 
|crown
 
|familiar number
 
|Results for the Crown of Thrones when the specified familiar is enthroned.
 
|-
 
|effect
 
|effect number
 
|Per-round results when you have the specified effect active (e.g. passive damage).
 
|-
 
|fam
 
|familiar number
 
|Results for the specified familiar.
 
|-
 
|gear
 
|equipment item number
 
|Per-round results when you have the specified item equipped.
 
|-
 
|hatrack
 
|hat item number
 
|Familiar results for your Mad Hatrack when it has the specified hat equipped.
 
|-
 
|headbutt
 
|hat item number
 
|Bonus damage from the specified turtle helmet.
 
|-
 
|item
 
|item number
 
|Results for throwing the specified combat item.
 
|-
 
|monster
 
|monster ID
 
|Special monster attributes such as resistances, damage caps, and immunities. This category is a bit special (see below).
 
|-
 
|scare
 
|pants item number
 
|Familiar results for your Fancypants Scarecrow when it has the specified pants equipped.
 
|-
 
|servant
 
|servant ID
 
|Results for your servant as Ed the Undying.
 
|-
 
|skill
 
|skill number
 
|Results for casting the specified combat skill.
 
|-
 
|thrall
 
|thrall ID
 
|Per-round results from having the specified pasta thrall active.
 
|}
 
  
After these important identifiers comes the actual information.  The first field, ufname (user-friendly name), exists solely to make the data file human-readable and isn't even used by BatBrain when reading the file.  The remaining three fields contain all the information and need to be formatted in a certain way to be understood, but I believe you'll find that format both intuitive and easy to edit.
+
Besides $stat[ none ], there are six possible values for this datatype (the last three are for referencing sub-stats).
  
== Spreads in Batfactors ==
+
* muscle
 +
* mysticality
 +
* moxie
 +
* submuscle
 +
* submysticality
 +
* submoxie
  
The dmg and pdmg fields contain damage information, which is converted to a spread (float[element] map) in BatBrain.  The format is versatile and can include formulas and any of BatBrain's fvars.  Since this value is handled by ASH's [[modifier_eval]](), any of those key letters or text functions will also work.  Basically damage is expressed as
+
===thrall===
  
  amount elements
+
([[Thrall#Related Functions|Related Functions]]) ([[Thrall#Proxy Record Fields|Proxy Record Fields]])
  
where amount is the damage formula and elements is a comma (but not space!)-delimited list of elements, using "none" for physical damage or "perfect" for always-correctly-tuned damage.  You may also use "prismatic" as shorthand for specifying all five elements.  If the action does only physical damage, elements (and the preceding space) may be omitted.  If multiple elements are present, the damage in amount will be equally distributed among those elements.
+
Pastamancers have the ability to summon {{kolwiki|Pasta Thralls}}.  
  
{| class="wikitable"
+
* Angel Hair Wisp
|+Examples
+
* Elbow Macaroni
|-
+
* Lasagmbie
|10
+
* Penne Dreadful
|deals 10 physical damage
+
* Spaghetti Elemental
|-
+
* Spice Ghost
| -10
+
* Vampieroghi
|heals 10 hit points
+
* Vermincelli
|-
 
|10*L
 
|deals 10 times your level in physical damage
 
|-
 
|myhp/2
 
|deals half of your current hitpoints in physical damage
 
|-
 
|10 spooky
 
|deals 10 spooky damage
 
|-
 
|10 spooky,hot
 
|deals 5 spooky damage and 5 hot damage
 
|-
 
|10 prismatic
 
|deals 2 each of hot, cold, spooky, sleaze, and stench damage
 
|}
 
  
For cases where the damage is not equally distributed among elements, the above pattern may be repeated, separated by a pipe:
+
==aggregate==
  
{| class="wikitable"
+
An aggregate is a complex datatype composed of two or more primitive or special datatypes. For more information, see [[Data Structures]].
|+Examples
 
|-
 
|<nowiki>3 hot|7 spooky</nowiki>
 
|deals 3 hot damage and 7 spooky damage
 
|-
 
|<nowiki>50 hot,cold|4 hot</nowiki>
 
|deals 29 hot damage and 25 cold damage
 
|}
 
  
== Special ==
+
==record==
  
Of course, events may have many results besides just damage, and that is all contained in the special field. The special field, quite simply, is a comma (and space)-delimited list of keywords and values, like so:
+
Records are user-defined datatypes that hold as many sub-datatypes as desired. For more information, see the page for [[Data Structures]].
  
keyword1 value1, keyword2 value2, keyword3 value3
+
==Plural Typed Constants==
  
As few as 0 keywords and values may be present. For numeric values such as MP or meat, formulas may be used since (as with the spreads above) these values will be handled by [[modifier_eval]]().  Here are all the keywords BatBrain presently understands:
+
(see http://kolmafia.us/showthread.php?p=15592, from which this section is reproduced)
  
{| class="wikitable"
+
Plural typed constants allow you to easily do something with a list of specified objects, without having to replicate code or laboriously build up an array of the objects so that you can iterate over it. Here's a quick example:
!Keyword
+
{{CodeSample|code=<syntaxhighlight>
!Meaning of Value
+
foreach weapon in $items[star sword, star staff, star crossbow] {
|-
+
  if (available_amount(weapon) > 0) {
|aoe
+
      equip(weapon);
|For skills with an area of effect, specifies the maximum number of monsters the skill affects.
+
      break;
|-
+
  }
|att
+
}</syntaxhighlight>}}
|Monster attack modifier.
+
The syntax is basically the same as the existing typed constant feature, but with an "s" or "es" after the type name. (The "es" case is there so that you can properly pluralize "class".) The text between the square brackets is interpreted as a comma-separated list of elements, each of which is converted to the specified type as if it were an individual constant. More details:
|-
+
* The list can span multiple lines.
|def
+
* Whitespace before or after elements is ignored.
|Monster defense modifier.
+
* Completely empty elements are ignored (so that you can leave a comma at the end of the list).
|-
+
* You can include a comma or closing square bracket in an element by writing it as "\," or "\]".
|stun
+
* All the other escape sequences allowed in strings are possible, such as "\n" (newline), "\t" (tab), and "\uXXXX" (Unicode character value). To put an actual backslash in an element, you have to write it as "\\".
|Number of rounds the monster is stunned by this action, on average. Defaults to 1 if no value is specified.
 
|-
 
|mp
 
|Amount of MP gained/lost.
 
|-
 
|meat
 
|Amount of meat gained/lost.
 
|-
 
|item
 
|A semicolon+space-delimited list of item names.  This action results in one of the items from this list.  Estimated profit averages the value of these items.
 
|-
 
|itemcost
 
|An item required and expended by this action.  This event uses up one of the specified item, and will be ignored if you lack it.
 
|-
 
|monster
 
|A monster name, or multiple pipe-delimited names. This event only happens for monsters specified here.
 
|-
 
|notmonster
 
|A monster name, or multiple pipe-delimited names. This event happens for any monster except those specified here.
 
|-
 
|phylum
 
|A phylum name.  This event only applies to monsters of the specified phylum.
 
|-
 
|stats
 
|Substats earned, formatted as <nowiki>Mus|Mys|Mox</nowiki>.
 
|-
 
|custom
 
|If specified at all, this action is a custom action and should not be used in regular automation.  The optional value represents the type of custom action (runaway, yellow, attract, banish, copy, etc).
 
|-
 
|mutex
 
|A mutually exclusive group this action belongs to.  Any action in the group makes all other actions in the same-named group unavailable (e.g. Sauceror curses, sixguns, etc).
 
|-
 
|rate
 
|For familiars only (which includes hatrack and scare categories).  The action rate of the familiar, expressed as a percent (1.0 = 100%).
 
|-
 
|!!
 
|Note. Usually used to explain something BatBrain is currently unable of knowing or tracking, i.e. unspaded data, ongoing damage, or some other strange mechanic.
 
|}
 
  
Some keywords have no values. BatBrain merely checks for the presence of the keyword to determine the relevant information.
+
The value generated by a plural constant is of type boolean[type], with the keys being the specified elements, and the boolean value always being true - although you won't normally do anything with the boolean, you'd use a foreach loop to iterate over the keys. You can assign a plural constant to a variable declared as that type, but note that the value differs from a normal map in three important respects:
 +
* Since the expression that generates it is syntactically a constant, the value has to be immutable. If you were allowed to change it in any way, those changes would appear in every future use of the same constant.
 +
* There can be multiple instances of the same key - $ints[1,1,2,3,5,8] is perfectly valid, and will result in the value 1 appearing twice in a foreach loop.
 +
* The keys will appear in the order you wrote them, rather than being sorted alphanumerically as maps usually do.
  
{| class="wikitable"
+
In addition to being used in a foreach loop, plural constants also efficiently support membership testing via the 'contains' operator. Here's another example:
!Keyword
+
{{CodeSample|code=<syntaxhighlight>
!Presence Indicates
+
for hour from 1 to 12 {
|-
+
  print("It's " + hour + " o'clock.");
|once
+
  if ($ints[10, 2, 4] contains hour) {
|The action can only be used once per combat.
+
      print("Time to drink a Dr Pepper!");
|-
+
  }
|endscombat
+
}</syntaxhighlight>}}
|The action automatically ends combat.
+
(Yes, that example could just as easily have been done with a switch statement.)
|-
 
|underwater
 
|The action is only valid underwater.
 
|-
 
|retal
 
|The results happen when the monster successfully hits you.
 
|-
 
|onhit
 
|The results happen when you hit the monster with a melee attack.
 
|-
 
|oncrit
 
|The results happen when you get a critical hit.
 
|}
 
  
== The "monster" Category ==
+
Iterating over an empty list is rather pointless, so plural constants with no elements are given a different meaning: they represent every value of the specified type, where this is practical. (The 'none' value, if defined for a given type, is omitted.) The biggest benefit here is $items[], which lets you loop over every defined item, more efficiently than you could otherwise write in a script (since the list is generated once per session and then cached), and without having to hard-code a maximum item ID number in your script. Example:
 +
{{CodeSample|code=<syntaxhighlight>
 +
foreach it in $items[] {
 +
  if (autosell_price(it) == 42) print(it);
 +
}</syntaxhighlight>}}
 +
Enumeration of all possible values works with the following types:
 +
* $booleans[] - false and true.
 +
* $items[]
 +
* $locations[]
 +
* $classes[]
 +
* $stats[] - Muscle, Mysticality, Moxie: the substat values are omitted.
 +
* $skills[]
 +
* $effects[]
 +
* $familiars[]
 +
* $slots[] - includes sticker slots and fake hands, which you might not want to consider as normal slots.
 +
* $monsters[]
 +
* $elements[] - includes slime now, and possibly other not-quite-elements like cute in the future.
  
The monster category is handled differently to the other categories.  The monster attributes are not loaded into an advevent, so it is not handled by to_event() as all the other categories are. Here are the key differences:
+
The remaining types that can be used in plural constants require an explicit list of elements, since there are too many possible values:
 +
* $ints[] - you don't have enough RAM to store a list with 4 billion elements.
 +
* $floats[] - ditto.
 +
* $strings[] - nobody has that much RAM.
  
First of all, the integer index is the monster's ID, which is accessible in the $monster.id proxy field.  Since some monsters' IDs are unknown, and therefore are listed as 0, batfactors uses an arbitrary negative index for those monsters, checking to_monster(ufname) to find a match against the monster name.
+
==Custom==
  
Secondly, the dmg field does not contain damage information, but rather monster resistances/vulnerabilities, still expressed as a spread.  For each element, 0 is normal, 1.0 is immunity, and -1.0 is vulnerability.  Normal elemental resistances/vulnerabilities are already calculated by BatBrain, and physical resistance, though it can be specified, is accessible in the physical_resistance proxy field, so you only need to include resistance information if the monster has exceptional resistances.  These resistances will overwrite the existing resistances on a per-element basis.
+
===matcher===
  
The pdmg field is also re-purposed -- for monsters, it specifies the monster's "penetration", also expressed as a spread. For each element, the monster treats your resistance as being lower by the amount of levels specified.  Since $element[none] is usually used for physical damage, it is used here to specify DA penetration.
+
A matcher isn't really a datatype so much as it's a class, but it is included here for reference, as it is used much as datatypes are in ASH. It can only be declared through the function {{f|create_matcher}}, using two strings. One is the string to find matches in, the other a regular expression to test against. For more information on using a matcher, see [[Regular Expressions]].
  
Finally, the special field contains an entirely new set of keywords, which are only for use in the monster category:
+
==Notes==
  
{| class="wikitable"
+
Buffers, aggregates, records and matchers are [https://developer.mozilla.org/en-US/docs/Glossary/Mutable mutable]. All other datatypes are immutable.
!Keyword
 
!Value/Indicates
 
|-
 
|variable
 
|The monster is variable and should not be loaded from cache.
 
|-
 
|nopotato
 
|Potato-type familiars do not work vs. this monster.
 
|-
 
|nofamiliar
 
|Your familiar will not act vs. this monster.
 
|-
 
|onlyhurtby X
 
|The monster can only be damaged by X.  Currently supported: aoe, pottery, healing, club (and all other weapon types)
 
|-
 
|aura X
 
|The monster has an aura dealing X player damage (specified as a spread) every round.
 
|-
 
|retal X
 
|Use this to specify retaliation damage (damage dealt to you when you succeed with a melee attack) as a spread.
 
|-
 
|maxround X
 
|Use this to specify combats of unusual durations (e.g. "maxround 50" for basement monsters).
 
|-
 
|group X
 
|Use this to specify group monsters, where X is the amount of monsters in the group (integer).
 
|-
 
|dmgformula X
 
|Use this to specify a monster's unique damage formula as a spread (for example, Your Shadow uses "95+maxhp/6")
 
|-
 
|damagecap X
 
|Use this to specify the monster's soft damage cap boundary (integer).
 
|-
 
|capexp X
 
|This is only meaningful with a soft damage cap, and specifies the exponent used to reduce damage above the boundary.
 
|-
 
|dodge (X)
 
|The monster has an X percent chance to dodge melee attacks.
 
|-
 
|autohit (X)
 
|The monster has an X percent chance to automatically hit you (e.g. gremlins).
 
|-
 
|automiss (X)
 
|The monster has an X percent chance to automatically miss you (e.g. crates).
 
|-
 
|nostagger (X)
 
|The monster has an X percent chance to ignore staggers.
 
|-
 
|nostun (X)
 
|The monster has an X percent chance to shrug a multi-round stunner each round.
 
|-
 
|noitems (X)
 
|The monster has an X percent chance of blocking items (e.g. Bonerdagon).
 
|-
 
|noskills (X)
 
|The monster has an X percent chance of blocking skills (e.g. Naughty Sorceress).
 
|-
 
|delevelres (X)
 
|The monster resists X percent of delevel effects.
 
|-
 
|spellres (X)
 
|The monster resists X percent of damage from combat spells.
 
|-
 
|dmgformula X
 
|Specify a monster's unique formula for dealing damage.  For example, the entry for Your Shadow includes "dmgformula 95+maxhp/6".
 
|-
 
|drpenetration X
 
|The monster ignores your first X DR. The Doctor is IN!
 
|}
 
  
Any keyword that expresses a "percent" should be specified as a float between 0 and 1.  For any keyword listed with (X) in parentheses, the value is optional and defaults to 1.0.
+
[[Category:Scripting]]

Revision as of 03:07, 2 March 2023

Primitive Datatypes

void

Can be thought of better as the absence of a datatype. No value can be assigned to void nor can a value be returned from a function of datatype void.

boolean

A boolean value is either true or false. By default, a boolean variable is set to false.

int

A whole number (short for "integer"), either positive or negative (or 0). The int used by KoLmafia is a 64-bit signed int, meaning it has a maximum value of 9,223,372,036,854,775,807 and a minimum value of -9,223,372,036,854,775,808. The default value of a integer variable is 0.

Be careful when doing math with integers! As with some other strongly-typed languages, numbers are converted to integers at every step of the operation when only integer types are used. For example:

int a = 1;
int b = 2;
print( a / b * 2 );

Will give the output "0," not "1" as you may expect. Changing either variable to a float type will "correct" this.

float

The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification, and a tool to help understand / quickly see how/where imprecisions happen can be found here.

When assigning to a variable of type float, one should be careful to always enter numbers in decimal form, as unwanted behavior can result from supplying a value that KoLmafia may interpret as an int type without the decimal point.

Note that float is not infinitely precise; it intrinsically rounds off after a certain point. This loss of accuracy is for the sake of storage, but beware of the possibility of small errors compounding from multiple float types.

For instance, try the following code as an example of how rather long post-decimal portions are handled:

float f;
f = 4.9999999;
print( f );

The default value of a float variable is 0.0.

string

A group of characters including, but not limited to: lowercase letters, uppercase characters, numbers, and various control characters. When assigning a value to a string, enclose the desired value in either single quotes ('), double quotes (") or backticks (`) (note you must use the same quote type on both ends of the string assignment). If you need to include the same character inside of the string itself, you will need to escape it first with a backslash.

For example:

string s = "This is my \"friend\" Pete.";

Will result in the following being stored to s:

This is my "friend" Pete


The default value of a string is an empty string, or literally ""

When using backticks, you can concatenate expressions into your string using curly braces.

For example:

string s = `Hello {my_name()}, you have {$item[lime].item_amount()} lime(s)!`;


buffer

Similar to a string, but more efficient in certain operations, including concatenation and passing as function arguments. For the most part, you can interchange references to strings and buffers. However, you should test all such actions first, as a few functions require a specific datatype to be supplied as a parameter. (Most notably, those listed under Regular Expressions.)

Special Datatypes

Several datatypes are included in KoLmafia to represent common categories within the KoL universe.

Note that while variables of these types are declared in the same way as for Primitive Datatypes; assigning and referencing them is done differently.

For example, to declare an item datatype and assign it a value, you would use the following line of code:

item it = $item[ broken skull ];

The default values of any variable of one of the following types is $type[ none ]. For example, the default value of a item variable is $item[ none ].

bounty

(Related Functions) (Proxy Record Fields)

These are the non-items that the bounty hunter asks you to retrieve from monsters around the kingdom. There are quite a lot of them (42 in total) so I won't list them all here. You can find more about bounties at the The Bounty Hunter Hunter's Shack.

class

(Related Functions) (Proxy Record Fields)

Besides $class[ none ], there are six possible values for this datatype:

  • Seal Clubber
  • Turtle Tamer
  • Pastamancer
  • Sauceror
  • Disco Bandit
  • Accordion Thief

coinmaster

(Related Functions) (Proxy Record Fields)

All shops that deal with currency other than meat. Known values include:

effect

(Related Functions) (Proxy Record Fields)

Any effect you can be under in KoL, whether from items, skills, or what-have-you, is valid for this datatype.

The full range, besides $effect[ none ], is too much to list and keep up with here: please see the Wiki Effects page for more information.

element

(Related Functions) (Proxy Record Fields)

Besides $element[ none ], there are seven possible values for this datatype. (Note that "Bad Spelling" is not considered a true element.) Also note that these names are case-sensitive (referencing $element[ Spooky ] will generate an error).

  • cold
  • hot
  • sleaze
  • spooky
  • stench
  • slime
  • supercold

familiar

(Related Functions) (Proxy Record Fields)

Any familiar available in KoL is valid for this datatype.

The full range, besides $familiar[ none ], is too much to list and keep up with here: please see the Wiki Familiars page for more information.

item

(Related Functions) (Proxy Record Fields)

Any item in all of KoL is valid for this datatype. Note that unlike most special datatypes, item references can make use of the item ID number.

For example, you could assign the item plexiglass pants as follows:

item it = $item[ 1234 ];

The full range, besides $item[ none ], is too much to list and keep up with here: please see the Wiki Items page for more information.

location

(Related Functions) (Proxy Record Fields)

Any location one can adventure at in KoL is valid for this datatype.

The full range, besides $location[ none ], is too much to list and keep up with here: please see the Wiki Locations page for more information.

monster

(Related Functions) (Proxy Record Fields)

Any monster you can encounter in KoL is valid for this datatype.

The full range, besides $monster[ none ], is too much to list and keep up with here: please see the Wiki Monster Compendium page for more information.

path

(Related Functions) (Proxy Record Fields)

Any ascension path you can choose in Valhalla is valid for this datatype. This includes all special challenge paths, as well as Standard and the dietary restriction paths (Boozetafarian, Teetotaler, Oxygenarian). The "Unrestricted" path and aftercore are represented as $path[ none ].

The full range, besides $path[ none ], is too much to list and keep up with here. See Ascension#Paths for more information.

phylum

(Related Functions) (Proxy Record Fields)

Each monster has a Phylum. Besides $phylum[none], the possible values for this datatype are:

  • beast
  • bug
  • constellation
  • construct
  • demon
  • dude
  • elemental
  • elf
  • fish
  • goblin
  • hippy
  • hobo
  • humanoid
  • horror
  • mer-kin
  • orc
  • penguin
  • pirate
  • plant
  • slime
  • undead
  • weird

servant

(Related Functions) (Proxy Record Fields)

Servants are unique to the Actually Ed the Undying path and located The Servants' Quarters, replacing familiars.

  • Assassin
  • Belly-Dancer
  • Bodyguard
  • Cat
  • Maid
  • Priest
  • Scribe

skill

(Related Functions) (Proxy Record Fields)

Any skill you can have in KoL, whether permable or not, granted by items, etc., is valid for this datatype.

The full range, besides $skill[ none ], is too much to list and keep up with here: please see the Wiki Skills page for more information.

slot

(Related Functions) (slot has no proxy record fields)

Besides $slot[ none ], there are 16 possible values for this datatype.

  • hat
  • back
  • weapon
  • off-hand
  • shirt
  • pants
  • acc1
  • acc2
  • acc3
  • familiar
  • sticker1
  • sticker2
  • sticker3
  • fakehand
  • bootspur
  • bootskin

stat

(Related Functions) (stat has no proxy record fields)

Besides $stat[ none ], there are six possible values for this datatype (the last three are for referencing sub-stats).

  • muscle
  • mysticality
  • moxie
  • submuscle
  • submysticality
  • submoxie

thrall

(Related Functions) (Proxy Record Fields)

Pastamancers have the ability to summon Pasta Thralls.

  • Angel Hair Wisp
  • Elbow Macaroni
  • Lasagmbie
  • Penne Dreadful
  • Spaghetti Elemental
  • Spice Ghost
  • Vampieroghi
  • Vermincelli

aggregate

An aggregate is a complex datatype composed of two or more primitive or special datatypes. For more information, see Data Structures.

record

Records are user-defined datatypes that hold as many sub-datatypes as desired. For more information, see the page for Data Structures.

Plural Typed Constants

(see http://kolmafia.us/showthread.php?p=15592, from which this section is reproduced)

Plural typed constants allow you to easily do something with a list of specified objects, without having to replicate code or laboriously build up an array of the objects so that you can iterate over it. Here's a quick example:

foreach weapon in $items[star sword, star staff, star crossbow] {
   if (available_amount(weapon) > 0) {
      equip(weapon);
      break;
   }
}

The syntax is basically the same as the existing typed constant feature, but with an "s" or "es" after the type name. (The "es" case is there so that you can properly pluralize "class".) The text between the square brackets is interpreted as a comma-separated list of elements, each of which is converted to the specified type as if it were an individual constant. More details:

  • The list can span multiple lines.
  • Whitespace before or after elements is ignored.
  • Completely empty elements are ignored (so that you can leave a comma at the end of the list).
  • You can include a comma or closing square bracket in an element by writing it as "\," or "\]".
  • All the other escape sequences allowed in strings are possible, such as "\n" (newline), "\t" (tab), and "\uXXXX" (Unicode character value). To put an actual backslash in an element, you have to write it as "\\".

The value generated by a plural constant is of type boolean[type], with the keys being the specified elements, and the boolean value always being true - although you won't normally do anything with the boolean, you'd use a foreach loop to iterate over the keys. You can assign a plural constant to a variable declared as that type, but note that the value differs from a normal map in three important respects:

  • Since the expression that generates it is syntactically a constant, the value has to be immutable. If you were allowed to change it in any way, those changes would appear in every future use of the same constant.
  • There can be multiple instances of the same key - $ints[1,1,2,3,5,8] is perfectly valid, and will result in the value 1 appearing twice in a foreach loop.
  • The keys will appear in the order you wrote them, rather than being sorted alphanumerically as maps usually do.

In addition to being used in a foreach loop, plural constants also efficiently support membership testing via the 'contains' operator. Here's another example:

for hour from 1 to 12 {
   print("It's " + hour + " o'clock.");
   if ($ints[10, 2, 4] contains hour) {
      print("Time to drink a Dr Pepper!");
   }
}

(Yes, that example could just as easily have been done with a switch statement.)

Iterating over an empty list is rather pointless, so plural constants with no elements are given a different meaning: they represent every value of the specified type, where this is practical. (The 'none' value, if defined for a given type, is omitted.) The biggest benefit here is $items[], which lets you loop over every defined item, more efficiently than you could otherwise write in a script (since the list is generated once per session and then cached), and without having to hard-code a maximum item ID number in your script. Example:

foreach it in $items[] {
   if (autosell_price(it) == 42) print(it);
}

Enumeration of all possible values works with the following types:

  • $booleans[] - false and true.
  • $items[]
  • $locations[]
  • $classes[]
  • $stats[] - Muscle, Mysticality, Moxie: the substat values are omitted.
  • $skills[]
  • $effects[]
  • $familiars[]
  • $slots[] - includes sticker slots and fake hands, which you might not want to consider as normal slots.
  • $monsters[]
  • $elements[] - includes slime now, and possibly other not-quite-elements like cute in the future.

The remaining types that can be used in plural constants require an explicit list of elements, since there are too many possible values:

  • $ints[] - you don't have enough RAM to store a list with 4 billion elements.
  • $floats[] - ditto.
  • $strings[] - nobody has that much RAM.

Custom

matcher

A matcher isn't really a datatype so much as it's a class, but it is included here for reference, as it is used much as datatypes are in ASH. It can only be declared through the function create_matcher(), using two strings. One is the string to find matches in, the other a regular expression to test against. For more information on using a matcher, see Regular Expressions.

Notes

Buffers, aggregates, records and matchers are mutable. All other datatypes are immutable.