Difference between revisions of "Wesnoth Formula Language"
(Add dice operator) |
(Add section on variables and where clauses, plus a note on the remaining two basic types) |
||
Line 2: | Line 2: | ||
The Wesnoth Formula Language is a functional language used to evaluate expressions within the game engine. The most common use of the WFL is in the <tt>$(formula)</tt> substitution syntax, but it can also be used in unit filters, GUI2 dialogs, and even [[FormulaAI|AI]]. | The Wesnoth Formula Language is a functional language used to evaluate expressions within the game engine. The most common use of the WFL is in the <tt>$(formula)</tt> substitution syntax, but it can also be used in unit filters, GUI2 dialogs, and even [[FormulaAI|AI]]. | ||
− | == Numbers == | + | == Data Types and Operators == |
+ | |||
+ | === Numbers === | ||
The most common use of WFL is for simple calculations involving numbers. For this, the standard arithmetic operators (<tt>+ - * / %</tt>) work as you would expect, performing addition, subtraction, multiplication, division, and remainder. The only caveat to watch out for is that <tt>/</tt> rounds down when used on integers. For example, <tt>5 / 2</tt> will evaluate to <tt>2</tt>. To avoid this, make sure at least one of the numbers includes a decimal point - <tt>5.0 / 2</tt> will evaluate to <tt>2.5</tt>. Note that WFL supports only three decimal places of precision; beyond that it will still be rounded down. (So <tt>1.0 / 16</tt> will evaluate to <tt>0.63</tt> instead of <tt>0.625</tt>.) The <tt>^</tt> operator performs exponentiation (raising to a power) - for example, <tt>2 ^ 3</tt> evaluates to <tt>8</tt> | The most common use of WFL is for simple calculations involving numbers. For this, the standard arithmetic operators (<tt>+ - * / %</tt>) work as you would expect, performing addition, subtraction, multiplication, division, and remainder. The only caveat to watch out for is that <tt>/</tt> rounds down when used on integers. For example, <tt>5 / 2</tt> will evaluate to <tt>2</tt>. To avoid this, make sure at least one of the numbers includes a decimal point - <tt>5.0 / 2</tt> will evaluate to <tt>2.5</tt>. Note that WFL supports only three decimal places of precision; beyond that it will still be rounded down. (So <tt>1.0 / 16</tt> will evaluate to <tt>0.63</tt> instead of <tt>0.625</tt>.) The <tt>^</tt> operator performs exponentiation (raising to a power) - for example, <tt>2 ^ 3</tt> evaluates to <tt>8</tt> | ||
Line 10: | Line 12: | ||
One final numeric operator exists - the dice roll. The syntax <tt>3d12</tt> will roll three 12-sided dice and return the sum of the results. Note however that this is not multiplayer-safe, so using it can and will produce OOS errors. | One final numeric operator exists - the dice roll. The syntax <tt>3d12</tt> will roll three 12-sided dice and return the sum of the results. Note however that this is not multiplayer-safe, so using it can and will produce OOS errors. | ||
− | == Strings == | + | === Strings === |
WFL also supports strings, which must be enclosed in single quotes (<tt>'like this'</tt>). The comparison operators also work on strings, performing lexicographical comparison (ie, alphabetical order). The comparison is case sensitive. | WFL also supports strings, which must be enclosed in single quotes (<tt>'like this'</tt>). The comparison operators also work on strings, performing lexicographical comparison (ie, alphabetical order). The comparison is case sensitive. | ||
− | == Lists == | + | === Lists === |
A list is a sequence of values represented as square brackets, [], surrounding a comma-separated list. For instance: | A list is a sequence of values represented as square brackets, [], surrounding a comma-separated list. For instance: | ||
Line 22: | Line 24: | ||
The comparison operators work on lists, performing lexicographical comparison. A specific list index can be obtained with the indexing operator, like this: <tt>my_list[index]</tt>. The first element of a list is numbered 0. | The comparison operators work on lists, performing lexicographical comparison. A specific list index can be obtained with the indexing operator, like this: <tt>my_list[index]</tt>. The first element of a list is numbered 0. | ||
− | == Maps == | + | === Maps === |
Maps: A map is a sequence of key-value pairs. For example: | Maps: A map is a sequence of key-value pairs. For example: | ||
Line 33: | Line 35: | ||
* <tt>self[[1,2]] = 9</tt> | * <tt>self[[1,2]] = 9</tt> | ||
* <tt>self['abc'] = 1.5</tt> | * <tt>self['abc'] = 1.5</tt> | ||
+ | |||
+ | === Other Types === | ||
+ | |||
+ | There are two other basic types in WFL. One is the null type, which is returned when you attempt something invalid (such as dividing a string), and is guaranteed to be returned by the <tt>null()</tt> function, allowing you to check whether a value is null. The other is an object or container type, which possesses attributes that can be accessed using the dot (<tt>.</tt>) operator. | ||
+ | |||
+ | == Variables == | ||
+ | |||
+ | Formulas may have a variety of variables, depending on the context in which they are evaluated. A string substitution formula like <tt>$(3 + 5)</tt> has no variables, but a [[StandardUnitFilter|unit filter]] formula has variables such as <tt>hitpoints</tt> and <tt>max_hitpoints</tt> which contain various properties of the unit being tested (the same unit which is also referred to in variable substitution as <tt>$this_unit</tt>). The special variable <tt>self</tt> typically refers to the "global context" - in the case of a unit filter formula, the unit itself. This means for example that <tt>self.hitpoints</tt> and <tt>hitpoints</tt> are equivalent when used in a unit filter formula. In a string substitution formula, <tt>self</tt> is null. | ||
+ | |||
+ | A formula may declare additional variables using a <tt>where</tt> clause. This assigns a meaning to any unknown variables in the preceding formula. The general syntax is: | ||
+ | |||
+ | <formula> where <variable> = <value> [, <variable> = value ...] | ||
+ | |||
+ | This functions similarly to an operator, so if desired, you could have multiple where clauses in a single formula. Note that all variables in WFL are read-only - they are variables because they may have different values depending on the context in which the formula is evaluated, but for a given context, they are constant. |
Revision as of 22:37, 4 March 2016
The Wesnoth Formula Language is a functional language used to evaluate expressions within the game engine. The most common use of the WFL is in the $(formula) substitution syntax, but it can also be used in unit filters, GUI2 dialogs, and even AI.
Contents
Data Types and Operators
Numbers
The most common use of WFL is for simple calculations involving numbers. For this, the standard arithmetic operators (+ - * / %) work as you would expect, performing addition, subtraction, multiplication, division, and remainder. The only caveat to watch out for is that / rounds down when used on integers. For example, 5 / 2 will evaluate to 2. To avoid this, make sure at least one of the numbers includes a decimal point - 5.0 / 2 will evaluate to 2.5. Note that WFL supports only three decimal places of precision; beyond that it will still be rounded down. (So 1.0 / 16 will evaluate to 0.63 instead of 0.625.) The ^ operator performs exponentiation (raising to a power) - for example, 2 ^ 3 evaluates to 8
You can also use the standard comparison operators (= != < <= > >=) on numbers. This is often useful in unit filters - for example, a formula of hitpoints < max_hitpoints / 2 will match only if the unit is at less than half health. Comparison operators return 1 for true and 0 for false; there is no boolean type.
One final numeric operator exists - the dice roll. The syntax 3d12 will roll three 12-sided dice and return the sum of the results. Note however that this is not multiplayer-safe, so using it can and will produce OOS errors.
Strings
WFL also supports strings, which must be enclosed in single quotes ('like this'). The comparison operators also work on strings, performing lexicographical comparison (ie, alphabetical order). The comparison is case sensitive.
Lists
A list is a sequence of values represented as square brackets, [], surrounding a comma-separated list. For instance:
[ 1, 5, 'abc' ]
The comparison operators work on lists, performing lexicographical comparison. A specific list index can be obtained with the indexing operator, like this: my_list[index]. The first element of a list is numbered 0.
Maps
Maps: A map is a sequence of key-value pairs. For example:
[12 -> 'Hello', [1,2] -> 9, 'abc' -> 1.5]
The comparison operators work on maps. A specific element of a map can be obtained with the indexing operation. In the above example, the following conditions are all true (assuming the map is in a variable called self):
- self[12] = 'Hello'
- self1,2 = 9
- self['abc'] = 1.5
Other Types
There are two other basic types in WFL. One is the null type, which is returned when you attempt something invalid (such as dividing a string), and is guaranteed to be returned by the null() function, allowing you to check whether a value is null. The other is an object or container type, which possesses attributes that can be accessed using the dot (.) operator.
Variables
Formulas may have a variety of variables, depending on the context in which they are evaluated. A string substitution formula like $(3 + 5) has no variables, but a unit filter formula has variables such as hitpoints and max_hitpoints which contain various properties of the unit being tested (the same unit which is also referred to in variable substitution as $this_unit). The special variable self typically refers to the "global context" - in the case of a unit filter formula, the unit itself. This means for example that self.hitpoints and hitpoints are equivalent when used in a unit filter formula. In a string substitution formula, self is null.
A formula may declare additional variables using a where clause. This assigns a meaning to any unknown variables in the preceding formula. The general syntax is:
<formula> where <variable> = <value> [, <variable> = value ...]
This functions similarly to an operator, so if desired, you could have multiple where clauses in a single formula. Note that all variables in WFL are read-only - they are variables because they may have different values depending on the context in which the formula is evaluated, but for a given context, they are constant.