Expressions

Page last edited 1,635 days 14 hours ago
From COG Docs
Jump to: navigation, search

Expressions are a special feature of the CustomOreGen xml parser. Any attribute value that begins with ":=" is evaluated as an expression and the result is then assigned to the attribute.

Expressions can contain literal numbers or other values as well as mathematical symbols and functions, logical comparisons, string concatenation, and many other operations.

Config options, World Properties, and Mystcraft Symbols may be used like variables. The name of the option/property/symbol is replaced by its current value in the expression. The special variable "_default_" is also available, containing the default value of the attribute being set.

Expression values fall into 3 basic types: numerical, string, or boolean. It is the user's responsibility to keep them straight and to make sure that operators and functions are being used with values of the correct type. Use the conversion operators #, $, and ? when necessary to convert between types.

Some examples:

:= 42
:= 5 * 3 + 6
:= 5 * (3 + 6)
:= max(12,-19)
:= _default_ * 3.2
:= "Hello" ~ "World"
:= someConfigOption >= 17.76
:= world.height / 2
:= "foo" != "bar" 
:= #"6.2" + 5.4


Supported Literal Values

Type Examples Notes
Decimal numbers -6.5, 1.2E-6, 42 May include unary plus/minus signs, decimal periods, and scientific 'E' notation.
Hex numbers -0x4, 0x1F8C Must begin with the prefix "0x". May include unary plus/minus signs and the letters A-F.
Strings "foo", 'bar' Must be enclosed with matching single or double quotes.
Booleans true, false


Supported Operators

Type Operator Description
Parenthesis ( ) Used to structure expressions
[ ]
{ }
Seperators , Used to separate function arguments
 ;
Arithmetic + Addition
- Subtraction
* Multiplication
/ Division
 % Modulus
^ Exponentiation
Numerical abs(x) Absolute value of x
sign(x) Sign value (or signum) of x
min(a,b) Lesser of a and b
max(a,b) Greater of a and b
floor(x) Greatest integer <= x
ceil(x) Smallest integer >= x
round(x) Round x to nearest integer
Trigonometry sin(x) Sine of x (x in radians)
cos(x) Cosine of x (x in radians)
tan(x) Tangent of x (x in radians)
asin(x) Inverse Sine of x in radians
acos(x) Inverse Cosine of x in radians
atan(x) Inverse Tangent of x in radians
Exponential exp(x) Natural exponential of x
log(x) Natural logarithm of x
Strings ~ Concatenate two strings into one.
match(string,regex) Returns true if the string matches the regular expression regex, and false otherwise.
replace(string,regex,replacement) Replaces all instances of the regular expression regex in string with replacement.
Conditional if(condition,trueResult,falseResult) Returns trueResult if the condition is true and falseResult otherwise.
Minecraft isModInstalled(modName) Given the modName as a string, returns true if the mod is installed and false otherwise.
blockExists(block) Returns true if the block string matches at least one valid registered block.
biomeExists(biome) Returns true if the biome string matches at least one valid registered biome.
world.nextRandom Returns a pseudorandom number in the range [0,1) based on the world seed. This function takes no arguments but it does return a different number each time it is used.
Comparison &lt; Less-than & Less-than-or-equal.

The symbol "<" is not allowed because it interferes with the xml parser, so the entity &lt; is used instead.

&lt;=
> Greater-than & Greater-than-or-equal
>=
First-order logic &amp; Logical AND.

The symbol "&" is not allowed because it interferes with the xml parser, so the entity &amp; is used instead.

| Logical OR
 ! Logical NOT
Equality = Equal & Not-equal
 !=
Conversion #x Convert x to a number. Strings are parsed as if they were numeric literals. Booleans become 1 if true, 0 if false.
$x Convert x to a string. Numbers are converted to string form. Booleans are converted to "true" and "false".
 ?x Convert x to a boolean. The string "true" is converted to True; all other strings are converted to False. The number 0 is converted to False; all other numbers are converted to True.