DarwinBotsManual
Open the app EspañolES

The cycle and the order of actions

What the world does in each cycle and in what order, why the senses arrive one cycle late, and how each bot's slot number, dynamic costs and mutation oscillation come into play.

The simulation moves in discrete steps: the cycles. In each one, the world always does the same things in the same order: it lets every bot think, moves the shots, computes forces, moves everyone, carries out what each bot asked for, makes the right bots be born and die, and hands out the sunlight. Understanding that order is half of programming a bot: it explains why what you see always arrives a little late and why what you ask for happens right away.

The eight phases

One cycle, from end to end:

  DNA ─────► senses ────► shots ──► forces ───► movement ──► actions ───► births ─────► the sun
             cleared                and collisions                        and deaths
  (thinks)  └──────────────────────── the world responds ─────────────────────────────────┘
PhaseWhat happens
DNAEvery living bot runs its whole DNA, top to bottom. It reads its senses and writes its commands to memory. Corpses don't think.
Senses clearedContact, shot flavors and the data about the bot you were facing are set to 0: what you already read doesn't carry over to the next cycle. The eyes are not cleared here.
ShotsShots that were already in flight hit whatever is in front of them and move forward. A hit writes to the memory of the bot that was hit (its flavor, or the value the shot carried).
Forces and collisionsUpkeep is charged (body, DNA length, age), the forces are added up (friction, gravity, ties, and the thrust you asked for with .up and friends) and collisions between bots are resolved, which write the contact senses. Ties pass messages to each other.
MovementTies share energy and matter, the bot turns as much as it asked, and all the accumulated forces are applied at once: speed and position change. .vel and the mass are published.
ActionsEach bot does what it left requested: it mutates if it's its turn, makes its defenses (shell, slime, venom, poison), shoots, handles chloroplasts and body, and requests reproduction. Then it looks: the eyes sweep the world with the positions already moved. At the end it ages and, if it ran out of energy, becomes a corpse or is marked to die.
Births and deathsAll the requested children are born, and then all the bots marked to die die.
The sunThe world's chloroplasts are counted, new vegetables are seeded if needed, and the light feeds the ones that photosynthesize.

The details of each phase are on its own page: Shots, Physics, Vision, Reproduction, Death and corpses and Chloroplasts and vegetables. The sysvar profiles use these same phase names to say who writes and who clears each address.

Why you see late and act right away

The DNA runs first and the world responds afterwards. That has two sides.

The senses arrive one cycle late. Contact is written in forces and collisions; the eyes, in actions; the flavors, in the shots phase. All of that happens after the DNA, so your DNA in cycle N reads what the world wrote during cycle N−1. When a bot sees another one in .eye5, it is seeing it where it was at the end of the previous cycle. The table in Execution and costs shows it with .vel: the speed the DNA records is always one step behind the real one.

Commands are carried out in the same cycle. What you write to .up, .shoot or .repro sits in memory immediately, and the phases that come after read it in that same cycle. On top of that, almost all commands are consumed: the phase that carries them out sets them back to 0. This bot reads its own commands before giving them:

' Reads its own commands before giving them
cond
start
  *.up 50 store
  *.shoot 51 store
  10 .up store
  -1 .shoot store
stop
end

Cycle after cycle, 50 and 51 stay at 0, even though the bot always pushes and shoots: the thrust was applied (and .up was cleared) in movement, and the shot went out (and .shoot was cleared) in actions, both before the DNA ran again. If you want to repeat a command, you have to give it again every cycle. The Movement and Shots groups say, sysvar by sysvar, who clears it and when.

Putting the two sides together: a bot that pushes in cycle N has already moved by the end of that cycle, but its DNA only finds out in N+1.

Two more delays

This bot counts its cycles in position 50 and requests a child when it reaches 3:

' Counts its DNA cycles and requests a child on the third
cond
start
  *50 1 add 50 store
stop

cond
  *50 3 =
start
  50 .repro store
stop
end
At the end of the cycleParent: 50Child: 50Child: .robage
330 (just born)0
4411
5522

The child is born in cycle 3 but doesn't run its DNA until 4. That's why so many Bestiary bots open with a *.robage 0 = gene: it is the gene that runs only once, in the first cycle of life. 4-d_Swarmer, for example, uses it to orient its eyes with .eye1dir and friends.

The order between bots

Each bot has a numbered slot in the world, and within each phase the engine goes through them in order, from slot 1 onward, changing the state as it goes. Each phase finishes with all the bots before the next one starts: the number matters within a phase, not between phases.

For your DNA this is barely noticeable, because while a bot thinks it only touches its own memory and its own energy. What does depend on the order:

WhatHow the number matters
Random numbersThere is a single generator for the whole world. What rnd gives you depends on how many numbers were drawn before you, in your DNA or in others', and every birth or death shifts that count.
CollisionsEach pair of bots is resolved once, when the engine reaches the lower-numbered one, and pushes both right then. The higher-numbered one gets the push before computing its own forces.
TiesMessages through ties are written straight into the other bot's memory (Ties and multicellular bots). In a chain of tied bots, whether a value advances one link or several in the same cycle depends on each bot's number.
VisionWhen they look, everyone is already in their final position; but the lower-numbered ones have already done their actions for the cycle (shot, changed shell or body) and the higher-numbered ones haven't yet.
BirthsChildren are created in the order of their parents and each one takes the lowest free slot. Low-numbered parents get low slots for their children.

Births and deaths: births come first

During actions, bots only request: reproducing or dying are noted down in two lists. In the births and deaths phase, the whole list of births is processed first and then the whole list of deaths. Two rules follow from that:

Not all deaths wait for this phase: a corpse that has run out of body disappears right after forces and collisions finishes, before movement (Death and corpses).

Where energy is paid

Expenses have their phase too: instructions are charged while the DNA runs, upkeep and thrust in forces and collisions, turning in movement, defenses and shots in actions, DNA copying in births and deaths, and photosynthesis comes in with the sun. Between one phase and the next the energy can go negative; only in actions is it decided whether the bot ran out of it. The full table is in Energy, body and waste, and all the prices are multiplied by the Cost multiplier.

Dynamic costs: the price follows the population

Before the DNA runs, the engine can adjust the Cost multiplier to steer the population toward a target. It is turned on with Dynamic cost adjustment and configured in Parameters: Dynamic costs.

The population that counts is the bots that aren't vegetables (with Count vegetables too, vegetables too), according to the engine's latest count. In each cycle:

Each adjustment is small: 0.0000001 × (bots outside the band) × Sensitivity. With the target at 100, margins at 0, sensitivity at 50 and 200 bots, the multiplier goes up 0.0005 per cycle: half a point every thousand cycles. It's a slow thermostat, meant for long runs. The multiplier doesn't go below 0 unless you turn on Allow a negative multiplier.

Separately there is an emergency brake that works even when the adjustment is off: if the population falls below Zero costs below population, the multiplier goes to 0 (nothing costs anything) until the population exceeds Reinstate above population, and then the value it had comes back. With −1 in the first one, the brake never kicks in.

Since the adjustment is made at the start of the cycle, that same cycle's DNA already pays the new price. A bot has no direct way to read the multiplier: it notices it in how fast its .nrg drops.

The oscillation of the mutations

Also at the start of the cycle, the engine can raise and lower the mutation rates of all the bots at once, alternating periods of lots of change with periods of calm. The oscillation has two stretches that repeat: a rise of a certain number of cycles and a fall of another.

Each bot's heritable rates don't change: the factor is applied on top, at the moment each mutation is drawn (Mutations).

The oscillation is off by default and the app has no control to turn it on: it only acts if you load a saved simulation that has it turned on.

At the end of the cycle

There are two more things that aren't bot phases. Between births and deaths and the sun, the shapes and teleporters move (The world). And after the sun there is a safety rule: if the sum of the lengths of all the genomes in the world goes over 4 million instructions, the engine eliminates, all at once, a batch of the poorest bots (those with the least energy plus ten times their body), a bigger batch the more bots there are. It only happens with huge populations of very long genomes, but it is real pressure against weak bots in long evolution runs.