Inline conditions
Comparisons leave trues and falses on the boolean stack; this page covers how they combine with and, or and not, and how they decide which stores run, before and after start.
A bot makes decisions by comparing numbers. Each comparison pops numbers off the integer stack and leaves a true or a false on the boolean stack. What's on that stack then decides which writes to memory happen and which don't.
Conditions can go in two places in a gene: between cond and start, where they decide whether the body runs (see Genes: cond, start, else and stop), or inside the body, after start. The latter are the inline conditions, and they give this page its name.
The comparisons
They all read the stack from left to right: a b > asks “is a greater than b?”. So *.nrg 5000 > is true when the energy is above 5000.
There are ten: <, >, <=, >=, =, != and the “approximate” ones %=, !%=, ~= and !~=. The table of what each one asks is in Operators by family.
The two “approximate” ones take a as the reference. 100 109%= is true (109 is within 10 of 100) and 100 111%= is false. ~= pops a third number, the percentage: 100 120 25 ~= is true and 100 130 25 ~= is not.
If the integer stack runs short of numbers, the comparisons use zeros: = on an empty stack compares 0 with 0 and gives true. The complete list, with the details of each one, is in Comparisons.
In the cond section
Between cond and start you can put as many conditions as you like. When it reaches start, the engine joins them all with an and: the body runs only if all of them are true. A gene with no conditions at all always runs, because an empty boolean stack counts as true.
In that section, numbers and comparisons are computed, but stores never run. This gene writes nothing to cell 50:
cond
5 50 store
start
stopand, or and not
If you want something other than the and of all of them, combine them yourself with the logical operators, which work on the two booleans at the top of the stack:
| Word | Leaves |
|---|---|
and | true if both are true |
or | true if either one is |
xor | true if one is and the other isn't |
not | the opposite of the top one |
true / false | a true / a false, without looking at anything |
This gene, the third gene of Animal Minimalis (Numsgil), from the Bestiary, turns at random if it sees nothing or if what it sees is of its own species:
' turns if it sees nothing or if what it sees is its own species
cond
*.eye5 0 =
*.refeye *.myeye = or
start
314 rnd .aimdx store
stopWithout that or, the two conditions would be joined with and on reaching start, and the gene would do something else. Read it like a stack calculator: .eye5 0 = leaves one boolean, .refeye .myeye = leaves another on top, and or replaces the two with a single one.
When a logical operator is missing an operand, it substitutes true: a lone or on an empty stack leaves true; a lone not leaves false (the opposite of “empty”, which is true).
Conditions inside the body
After a cond … start, the boolean stack starts out empty, that is, true, and all the stores run. If you put a condition in the middle of the body, its result stays on top of the stack and every store that comes after it checks it: if it's true, the store runs; if it's false, it's skipped. The store looks at the top without popping it, so the same condition governs all the following stores, until something else changes it.
With not that gives you an “else” inside the same gene:
' goes forward for the first few cycles and then backward
cond
start
*.robage 5 <
20 .up store
not
20 .dn store
stopWhile .robage is less than 5, the condition is true: it writes to .up and the .dn is skipped. From the sixth cycle on it's the other way around and the bot pushes backward; because of inertia, it takes a couple of cycles to brake and turn around.
Inline conditions govern only stores (including inc, dec and the rest of the family in Writing to memory). The numbers, the arithmetic and the other comparisons in the body always run, whether what's on the stack is true or false.
Chaining conditions
A new condition doesn't add to the previous one: it's pushed on top and rules alone. If you want a store to depend on two things, join them with and:
start
1 2 =
3 3 =
1 50 store
and
1 51 store
stopThe first store runs, even though 1 2 = is false, because 3 3 = is on top, and it's true. After the and, a false is left on top, and the second store is skipped: cell 50 ends up as 1 and cell 51 as 0.
Enabling stores again
To make the stores that follow run unconditionally again, you have three options:
truepushes a true on top. What's underneath is still there, but no longer rules.dropboolpops the top one and lets the previous one rule.clearboolempties the stack, and empty counts as true.
The Animal_Minimalis Antivirus variant (Shasta), from the Bestiary, writes all its conditions inline and uses the first one. Its food-seeking gene moves the bot only if it sees something that isn't of its own species, and at the end it records the gene number no matter what (in the original, address 71 has a name set with Variables with def):
' Gene 1 of Animal_Minimalis Antivirus: looks for food
cond
start
*.eye5 0 >
*.refeye *.myeye != and
*.refveldx .dx store
*.refvelup 30 add .up store
true
*.thisgene 71 store
stopIf you run it on its own, with no food in sight, the bot doesn't move, but cell 71 gets filled anyway: the true left the last store enabled.
Two traps
A skipped store pops nothing from the integer stack. Its value and its address stay there, and the next store that runs may find them:
start
false
5 50 store
true
60 store
stopThe first store is skipped and leaves the 5 and the 50 on the stack. After the true, the 60 store pops the 60 as the address and the 50 as the value: cell 60 ends up as 50, not 5. If a store can be skipped, make sure there are no “half-done” stores after it that count on a tidy stack.
A start without cond doesn't clear the boolean stack. Only cond empties it. If a gene leaves a false inline and the next one starts directly with start, that false keeps blocking its stores (the example is in The integer and boolean stacks). To be safe, end your inline conditions with a clearbool or open every gene with cond.