DarwinBotsManual
Open the app EspañolES

A bot that looks for food

The second bot, step by step: the nine eyes, a DNA tournament to head for the eye that sees the most, eating with shots and reproducing when energy is left over.

In A bot that moves you finished a bot that roams the world turning at random: it moves, but it isn't going anywhere. Here we add what it needs to make a living: eyes to find food, a way to decide which way to go, a way to eat it and, when it has energy to spare, children.

Step 1: a world with food

The food in DarwinBots is vegetables: bots marked as such, which carry chloroplasts and make energy from the sun (Chloroplasts and vegetables).

  1. Open a run; the one from the previous tutorial will do.
  2. In Observe, click Seed on the bottom bar. Under Bot, pick the Alga Minimalis (vegetable) preset: it marks it as a vegetable by itself and suggests 15 copies with 3000 energy. If you seed another DNA, check Vegetable (photosynthesizes) by hand.
  3. In the same dialog, seed your bot: 5 copies, 3000 energy, a color that stands out.

Two things the simulation does on its own. In the sun phase, at the end of every cycle, every vegetable with chloroplasts collects its share of the light. And if vegetables become scarce, every so often it seeds more (Chloroplasts and vegetables). All the energy in the world comes in through there; your bot is going to live by stealing it from the algae.

Step 2: the eyes

Your bot has nine eyes, from .eye1 to .eye9, open in a 90-degree fan around its heading: .eye5 looks straight ahead, the low-numbered ones to the left and the high-numbered ones to the right, 10 degrees per eye. Each one is 0 if it sees nothing, and grows the closer what it sees is: 1 at the edge of its range (about 1440 units from edge to edge), 100 at about 134 and 32000 when they overlap. The full detail is in Vision.

To watch them in action, start with a bot that does nothing but turn one eye per cycle:

' Turn one eye per cycle, to see what each one sees
cond
start
 35 .aimdx store
stop

.aimdx turns right by whatever amount you write; 35 units are about 10 degrees, exactly one eye. When we ran it next to three vegetables, with one about 360 units away from edge to edge, the bot's eyes read this:

Cycle.eye1.eye2.eye3.eye4.eye5.eye6.eye7.eye8.eye9
1000015151553
2000151515530
3001515155300
4015151553000
5151515530000
71553000000
9300000000
10000000000

The vegetable comes in on the right of the fan, crosses the front and leaves on the left, one eye per cycle: it is the world that seems to turn, because it is the bot that turns. The patch of 15 takes up three eyes in a row: a big blob fills several at once. And a full turn is 36 cycles, so the vegetable goes through the fan once per turn; the rest of the time, all zeros.

You see the same thing in the app: seed this bot, click it and look at the inspector's Memory tab: any eye drops to 0 and lights up again as it turns (see The inspector).

Step 3: the tournament for the eye that sees the most

To head for the food you have to turn toward the eye with the highest reading, and the DNA has no maximum that compares nine values in one go (floor compares in pairs). The solution is a tournament, with two cells of free memory:

The tournament is played again every cycle. First, the table is set to zero:

' 50 holds the best value seen; 51, the turn to that eye
cond
start
 0 50 store
 0 51 store
stop

Then, one gene per eye: if that eye sees more than the best so far, it takes its place. The one for .eye9, for example:

' If eye 9 sees more than the best so far, take its place
cond
 *.eye9 *50 >
start
 *.eye9 50 store
 140 51 store
stop

The 140 it stores is the turn that centers eye 9: with 35 units per eye, the eyes are at 35, 70, 105 and 140 from the front. The gene for .eye1 stores −140, the one for .eye2 −105… and the one for .eye5, 0: if what sees the most is the front eye, there is nothing to turn.

The comparison is a plain >, not “greater or equal”: if several eyes tie, the first one to reach that value wins, the one with the lowest number. And since the start gene leaves cell 50 at 0, that same cell will serve you as the “do I see anything?” question in the next step.

The nine genes — one per eye, identical except for the number and the turn — are in the complete bot at the end: the tournament always leaves what is seen the most in 50, and which way to turn in 51.

Step 4: turning and moving forward

Three more genes and the bot is already searching:

' Turn toward the eye that sees the most
cond
 *51 0 !=
start
 *51 .aimdx store
stop

' Always move forward
cond
start
 10 .up store
stop

' Nothing in sight: turn at random so the fan sweeps
cond
 *50 0 =
start
 314 rnd .aimdx store
stop

When we ran it, the correction looks like this cycle by cycle (a vegetable coming in from the right):

CycleWhat the DNA read (eye 9 / eye 5)Turn command
42nothing yet—
431072 / 070 to the right
444814 / 035 to the right
4532000 / 320000: straight on, it has it in front

Two things to read correctly in that table. Each cycle's command answers to the previous cycle's eyes: vision is computed in the actions phase, with the final positions, and your DNA only reads it in the next cycle (Execution and costs). And in cycle 43 the turn is 70 and not 140: eyes 7, 8 and 9 had tied at 1072 and 7 won, the first of the tied ones.

What you should see: the bot turns at random until the food enters the fan; then it turns toward it, gets right up against it and, since it always moves forward, pushes it. In our runs, the first vegetable it found ended up cornered against a wall. It doesn't know how to eat yet: the vegetable's energy doesn't go down (in the app it even goes up, slowly, as long as the sun shines on it). That is what comes next.

Step 5: eating

A vegetable is not eaten by touching it: your bot has no mouth. It is eaten with shots. The −1 shot is an energy request: when it hits a live bot, the victim loses 90% of the hit's strength as energy and another 1% as body, and from the point of impact a gift shot carrying the loot flies back toward the shooter. On arrival, the shooter keeps 95% as energy, a little as body and a 1% that turns into waste for it (Shots). With about 1000 body, each shot takes about 200 from the vegetable and gives you about 210.

The gene:

' Food close, ahead: ask it for energy with a shot
cond
 *.eye5 50 >
start
 -1 .shoot store
stop

You shoot only when the front eye is over 50, that is, when the food is less than about 200 units away from edge to edge (the eye's 100 is 134). That is a safe shooting distance: the shot of a 1000-body bot flies about 440 and loses strength along the way, so from afar it arrives empty. It is also the same threshold as the Bestiary's Animal Minimalis.

When we ran it, the vegetable fell from 3000 to 28 energy in 40 cycles and died; the hunter, meanwhile, went from 3000 to over 6100. While it eats, it pushes the vegetable against the wall, so the victim can't get away. If the vegetable has no energy left, it dies and leaves a corpse (Death and corpses); the −1 no longer takes anything from the corpse, because it has no energy anymore. To empty bodies there is the −6 shot (Shots).

Step 6: the complete bot

The cycle already sustains itself: search, get closer, eat. Only the last part is missing: when energy is left over, have children. This is the whole bot:

' A bot that looks for food
' 50 = the highest value seen this pass; 51 = turn toward that eye

' Start every cycle with no candidate
cond
start
 0 50 store
 0 51 store
stop

' Eye by eye: if it sees more than the best so far, take its place
cond
 *.eye1 *50 >
start
 *.eye1 50 store
 -140 51 store
stop

cond
 *.eye2 *50 >
start
 *.eye2 50 store
 -105 51 store
stop

cond
 *.eye3 *50 >
start
 *.eye3 50 store
 -70 51 store
stop

cond
 *.eye4 *50 >
start
 *.eye4 50 store
 -35 51 store
stop

cond
 *.eye5 *50 >
start
 *.eye5 50 store
 0 51 store
stop

cond
 *.eye6 *50 >
start
 *.eye6 50 store
 35 51 store
stop

cond
 *.eye7 *50 >
start
 *.eye7 50 store
 70 51 store
stop

cond
 *.eye8 *50 >
start
 *.eye8 50 store
 105 51 store
stop

cond
 *.eye9 *50 >
start
 *.eye9 50 store
 140 51 store
stop

' Turn toward the eye that sees the most
cond
 *51 0 !=
start
 *51 .aimdx store
stop

' Always move forward
cond
start
 10 .up store
stop

' Nothing in sight: turn at random so the fan sweeps
cond
 *50 0 =
start
 314 rnd .aimdx store
stop

' Food close, ahead: shoot it to eat
cond
 *.eye5 50 >
start
 -1 .shoot store
stop

' Energy to spare: have a child
cond
 *.nrg 5000 >
start
 30 .repro store
stop
end

The new gene says: with more than 5000 energy, a child that takes 30% (.repro). You are born with 3000, so 5000 means “I ate more than enough”: from a parent at 5400, the child is born with about 1600 and the parent ends at about 3800. The child carries the same DNA and hunts just like the parent.

The 200-cycle run, against four vegetables: it crossed 5000 at cycle 40, but the child was only born around 90. The birth needs free room ahead of the parent, where it is aiming; if it is occupied, the command stays written and is retried every cycle, and your bot lives stuck to its food and to the walls. At 200 cycles the parent was at 7100 with another birth pending, and the child — born with about 1600 — had already eaten its way up to 3000. In the app, the birth shows up as a flash next to the parent, and the generation jumps of your lineage appear in Events.

The Bestiary's Animal Minimalis is this same bot with fewer pieces: it only looks at the front eye; while it sees nothing it turns at random; when .eye5 goes over 50, it shoots; and at 20000 energy it reproduces with 10%. It also has a detail ours lacks: before shooting, it checks that what it sees is not of its own species. A good starting point for what follows.

What to try next