Template:Op mathematical operations

Mathematical operations
Mathematical operations deal with numbers. Warband Module System can only deal with integers. Floating point numbers are emulated by the so-called "fixed point numbers". Wherever you encounter a fixed point parameter for some Module System operation, keep in mind that it is actually just a regular integer number, HOWEVER it is supposed to represent a floating point number equal to fixed_point_number / fixed_point_multiplier. As you might have guessed, to convert a floating point number to fixed point, you have to multiply it by fixed_point_multiplier. You can change the value of multiplier with the operation (set_fixed_point_multiplier), thus influencing the precision of all operations dealing with fixed point numbers.

A notion very important for Warband modding is that you reference all Warband objects by their numeric values. In other words, you can do maths with your items, troops, agents, scenes, parties et cetera. This is used extensively in the code, so don't be surprised to see code looking like (store_add, ":value", "itm_pike", 4). This code is just calculating a reference to an item which is located 4 positions after "itm_pike" inside the module_items.py file.

Conditional operations
gt = 32
 * (gt,, ),
 * Checks that value1 > value2

ge = 30
 * (ge,, ),
 * Checks that value1 >= value2

eq = 31
 * (eq,, ),
 * Checks that value1 == value2

neq = neg|eq
 * (neq,, ),
 * Checks that value1 != value2

le = neg|gt
 * (le,, ),
 * Checks that value1 <= value2

lt = neg|ge
 * (lt,, ),
 * Checks that value1 < value2

is_between = 33
 * (is_between,, , ),
 * Checks that lower_bound <= value < upper_bound

Mathematical and assignment operations
assign = 2133
 * (assign,, ),
 * Directly assigns a value to a variable or register.

store_add = 2120
 * (store_add,, , ),
 * Assigns := +

store_sub = 2121
 * (store_sub,, , ),
 * Assigns := -

store_mul = 2122
 * (store_mul,, , ),
 * Assigns := *

store_div = 2123
 * (store_div,, , ),
 * Assigns := /
 * Fractional part will be truncated. Negative values will be rounded up.

store_mod = 2119
 * (store_mod,, , ),
 * Assigns := MOD

val_add = 2105
 * (val_add,, ),
 * Assigns := +

val_sub = 2106
 * (val_sub,, ),
 * Assigns := -

val_mul = 2107
 * (val_mul,, ),
 * Assigns := *

val_div = 2108
 * (val_div,, ),
 * Assigns := /
 * Fractional part will be truncated. Negative values will be rounded up.

val_mod = 2109
 * (val_mod,, ),
 * Assigns := MOD

val_min = 2110
 * (val_min,, ),
 * Assigns the right value if the right value is less than left.

val_max = 2111
 * (val_max,, ),
 * Assigns the right value if the right value is greater than left.

val_clamp = 2112
 * (val_clamp,, , ),
 * Enforces value to be within ..-1 range.

val_abs = 2113
 * (val_abs, ),
 * Assigns := ABS

store_or = 2116
 * (store_or,, , ),
 * Binary OR

store_and = 2117
 * (store_and,, , ),
 * Binary AND

val_or = 2114
 * (val_or,, ),
 * Binary OR, overwriting first operand.

val_and = 2115
 * (val_and,, ),
 * Binary AND, overwriting first operand.

val_lshift = 2100
 * (val_lshift,, ),
 * Bitwise shift left (dest = dest * 2 ^ value)

val_rshift = 2101
 * (val_rshift,, ),
 * Bitwise shift right (dest = dest / 2 ^ value)

store_sqrt = 2125
 * (store_sqrt, , ),
 * Assigns dest:= SQRT (value)

store_pow = 2126
 * (store_pow, , , , ),
 * Assigns dest:= SIN (value)

store_cos = 2128
 * (store_cos, , ),
 * Assigns dest:= COS (value)

store_tan = 2129
 * (store_tan, , ),
 * Assigns dest:= TAN (value)

store_asin = 2140
 * (store_asin, <destination_fixed_point>, <value_fixed_point>),
 * Assigns dest:= ARCSIN (value)

store_acos = 2141
 * (store_acos, <destination_fixed_point>, <value_fixed_point>),
 * Assigns dest:= ARCCOS (value)

store_atan = 2142
 * (store_atan, <destination_fixed_point>, <value_fixed_point>),
 * Assigns dest:= ARCTAN (value)

store_atan2 = 2143
 * (store_atan2, <destination_fixed_point>, <y_fixed_point>, <x_fixed_point>),
 * Returns the angle between the x axis and a point with coordinates (X,Y) in degrees. Note the angle is calculated counter-clockwise, i.e. (1,1) will return 45, not -45.

Random number generation
store_random = 2135
 * Stores a random value in the range of 0..<upper_range>-1. Deprecated, use (store_random_in_range) instead.
 * Stores a random value in the range of 0..<upper_range>-1. Deprecated, use (store_random_in_range) instead.

store_random_in_range = 2136
 * Stores a random value in the range of <range_low>..<range_high>-1.
 * Stores a random value in the range of <range_low>..<range_high>-1.

shuffle_range = 2134
 * Randomly shuffles a range of registers from <reg_no> up to <upper_bound - 1>, reordering the values contained in them. Commonly used for list randomization.
 * Randomly shuffles a range of registers from <reg_no> up to <upper_bound - 1>, reordering the values contained in them. Commonly used for list randomization.

Fixed point values handling
set_fixed_point_multiplier = 2124
 * (set_fixed_point_multiplier, ),
 * Affects all operations dealing with fixed point numbers. Default value is 1.

convert_to_fixed_point = 2130
 * (convert_to_fixed_point, <destination_fixed_point>),
 * Converts integer value to fixed point (multiplies by the fixed point multiplier).

convert_from_fixed_point = 2131
 * (convert_from_fixed_point, ),
 * Converts fixed point value to integer (divides by the fixed point multiplier).