DarwinBotsManual
Open the app EspañolES

A bot that recognizes its own species

The DNA signature and a public password, step by step, so your bot saves its shots for strangers.

In A bot that looks for food your bot learned to move toward food, and in A bot that shoots, to take it from another bot by shooting. This tutorial adds the missing piece for many copies of the same bot to live together: knowing who you're facing, and saving your shots for strangers.

The shot doesn't choose

The shot doesn't tell species apart: it hits any bot that crosses its path. A bot never hits itself with its own shot, and a newborn is protected from its parent's shots during its first cycles; from nobody else's. The DNA can't read the species name either: for the engine it's a name that gets inherited (see Species and lineage), and no sysvar brings it. The closest thing is .totalmyspecies, which counts how many living bots carry your name: it's useful for knowing how many are left, not who you're facing.

Seed four copies of this unfiltered hunter (the one from the previous tutorial, cut down to the essentials) in a small world with no vegetables:

cond
 *.eye5 0 =
start
 314 rnd .aimdx store
stop

cond
 *.eye5 0 >
start
 -1 .shoot store
stop

In the run, by cycle 20 one was already missing, and by 30, another. One of the survivors closed with two deaths of its own siblings in .kills; the other was being bled slowly (from 3000 down to 2441 energy, while the first went up to 9175). With no vegetables to repopulate, a species like this eats itself. (This same pitfall, with its fix, is in Common mistakes.)

The two signals

Everything a bot knows about the one in front of it arrives through sight (and through collisions), and none of those things is a name. When the focus eye sees something, the ref* cells describe it: where it is, how it moves, how much energy and body it has (Vision). Among that data comes a signature of its DNA: the same counters you have in the my* ones, but for the other bot.

The signature doesn't come from what the other bot does, but from the text of its DNA. .refeye, for example, says how many times its DNA reads its eyes; .refshoot, how many times it commands a shot; .refup, how many times it pushes. Since the text is what's counted, it counts even if that code never runs, and it can't be faked: writing your own my* by hand doesn't change what others see. Two bots with the same DNA have exactly the same signature. The most widely used comparison (it comes from Numsgil's Animal Minimalis, and hundreds of Bestiary bots carry it) is .refeye against your .myeye.

The other signal works the other way around: you don't deduce it from the other bot, it publishes it. You write a number into .out1 and it stays there: the engine never clears it. Any bot that has you in its focus eye receives it in its .in1, with the one-cycle delay that all senses have. It's a public channel: the 555 of Artemis Minimalis, from the Bestiary, is the classic example.

SignalStrengthWeakness
The signature (ref* against my*)It can't be copiedTwo different DNAs can count the same
The password (in1 against out1)It doesn't match by chanceAnyone who sees you reads it

We'll go through both, in order, because each one covers the other's gap.

Step 1: the signature

The hunter reads *.eye5 twice, so its .myeye is 2: that's its signature. Add the comparison to the gene that shoots:

' Shoot only if the other's signature doesn't match mine
cond
 *.eye5 0 =
start
 314 rnd .aimdx store
stop

cond
 *.eye5 0 >
 *.refeye *.myeye !=
start
 -1 .shoot store
stop

What you should see:

  1. Against a clone (the same species, two copies), not a single shot. In the run they ended up stuck face to face (the front eye read over 20000) with *.refeye and *.myeye at 2, and energy untouched at 3000 for 40 cycles.
  2. Against a stranger that reads its eyes only once, the signature doesn't match (1 against 2) and the hunter acts as usual: it turned until it saw it, stole energy from it until it died before cycle 25, and closed at 6160, up from the initial 3000.

Step 2: the traps of the signature

The signature is an approximation, and it has three pitfalls:

The fix is to compare more than one figure and shoot if any of them doesn't match, with or in the condition section:

' Shoot if any figure of the signature doesn't match
cond
 *.eye5 0 =
start
 314 rnd .aimdx store
stop

cond
 *.eye5 0 >
 *.refeye *.myeye !=
 *.refshoot *.myshoot != or
start
 -1 .shoot store
stop

The false sibling above writes .shoot zero times, so its .refshoot (0) no longer matches your .myshoot (1): in the run it ended up dead before cycle 25, with the hunter closing at 6160.

You can add a third figure (.refup, .reftie…): each independent one makes a chance match harder.

Step 3: the password

Each bot publishes the password on its own. It's enough to publish it once: since .out1 is never cleared and starts at 0, “while it's 0” means “the first time”. Afterwards, before shooting, you compare *.in1 with your own *.out1.

' Publish the species code, just once
cond
 *.out1 0 =
start
 555 .out1 store
stop

cond
 *.eye5 0 =
start
 314 rnd .aimdx store
stop

' Shoot whoever doesn't publish my code
cond
 *.eye5 0 >
 *.in1 *.out1 !=
start
 -1 .shoot store
stop

Two cautions. First, *.in1 is 0 both when you're not seeing anyone and when the other bot publishes nothing: the *.eye5 0 > is what warns you that someone is in front of you. Second, pick a code other than 0, which is what any bot that doesn't use the channel “publishes”.

What you should see:

  1. Against a stranger that publishes nothing, the password works like one more signature: in the run it hunted it down just as before and closed at 6152.
  2. Against a spy, the password shows its price: a bot that just copies into its .out1 what it reads in *.in1 publishes your code as soon as it sees you. In the run, the spy was already announcing 555 at cycle 5.
  3. And the consequence: against an intruder that publishes your code, a bot that only looks at passwords doesn't shoot. The intruder spent 40 cycles in plain sight, with its energy untouched.

It's a public channel: it's good for picking out your own, not for keeping a secret.

Step 4: the complete bot

Combined, each signal covers the other's gap. But note how: you have to shoot if any of them doesn't match, with or. If you required both to fail, each trap would stay open: the signature would save the false sibling, and the password would save the intruder with the stolen code.

In frontSignaturePasswordShoots
A clonematchesmatchesno
A stranger that counts the samematchesdoesn'tyes
An intruder with the stolen codedoesn'tmatchesyes

The complete bot (the skeleton is that of Animal Minimalis, plus the password) ends up like this:

' Publish the species code, just once
cond
 *.out1 0 =
start
 555 .out1 store
stop

' Something ahead that isn't one of mine: chase it, copying its velocity
cond
 *.eye5 0 >
 *.refeye *.myeye !=
start
 *.refveldx .dx store
 *.refvelup 30 add .up store
stop

' Stranger nearby: signature or password that don't match
cond
 *.eye5 50 >
 *.refeye *.myeye !=
 *.refshoot *.myshoot != or
 *.in1 *.out1 != or
start
 -1 .shoot store
stop

' Nothing ahead, or one of mine: turn, looking around
cond
 *.eye5 0 =
 *.refeye *.myeye = or
start
 314 rnd .aimdx store
stop

' Reproduce when there's energy to spare (10 percent for the child)
cond
 *.nrg 20000 >
start
 10 .repro store
stop

Each gene does one thing: publish the code, chase strangers by copying their velocity, shoot them when they're close, turn when there's nothing (or what's there is a sibling), and divide when there's energy to spare. The child is born with the same DNA, that is, with the same signature, and publishes its 555 in its first cycle; meanwhile, its parent's shots don't hit it.

What you should see:

  1. Against its clones, not a single shot. In the run, two copies each turned its own way; at cycle 35 one had the other straight ahead (*.refeye at 3, *.in1 at 555) and went on its way, with its energy untouched.
  2. Against the intruder with the stolen password, the signature gave it away: it chased it, kept taking its energy and left it dead before cycle 35, with the final bot closing at 5958 from the initial 3000.
  3. Against a stranger that counts the same (three eye reads and one shot, like yours, but without publishing a code), the password gave it away: as soon as it saw it, it started shooting at it, and in 60 cycles it brought it down from 3000 to 2244 energy.

The whole block passes the lint with no warnings. Change the 555 to a number of your own (anything but 0) and it should keep working just the same.

What to try next

The complete spy. The two signals together can still be fooled: a bot that copies your password and also, by chance or by design, counts the same as you fools the complete bot. Getting that by luck is hard; the more independent signals you compare, the harder the deception.

Relatives that mutated. With mutations turned on (Mutations), every mutation redoes the bot's signature: a descendant can end up with a different count and start receiving friendly fire from its relatives. A mutation can also hit the 555 or the gene that publishes it. Both signals drift with evolution; see Species and lineage.

Without seeing: through ties. A bot tied to another recognizes it with .trefeye instead of refeye, and compared with %= it allows up to a 10% difference, useful if you expect relatives that mutated a little. To learn how to build ties, see A multibot.

Hidden passwords. With .memloc you can spy on any cell of the bot you see and keep your code in a private variable, instead of publishing it in out1 (see Vision). Spoiler: a spy with .memloc finds it too: vision in DarwinBots has no secrets.