Inline Conditions Tutorial

From WikiManual
Jump to: navigation, search

Introduction

In this tutorial I will assume you have a basic understanding of how the Darwinbots DNA system works. If you do not, you may still be able to understand the tutorial, but please look at one of the other tutorials available.

Basics

To use inline conditions, you must first understand how they work.

Commands

If you don't already know the logical comparison and logic operators, read up on them on the Operator page. Take special note of the operators that directly affect the Boolean stack: clearbool, dropbool, dupbool, swapbool, and overbool.

How the Boolean stack affects DNA expression

Lets look at an example:

start
 true
  -1 .shoot store
 false
  10 .up store
stop

In this, the bot will always be shooting, and never move up. Why? The logical comparison and logic operators are always evaluated. All the other bits of DNA will be evaluated depending on what value is on the top of the Boolean stack.

If, Else-If, and Else

If

If statements are easy, so here is a quick example and we will move on:

start
 *.eye5 0 != *.refeye *.myeye != and
  'Code to be run when the bot is looking at an enemy
 *.nrg 1000 >
  'Reproduction code or similar
stop

Else

Lets continue on with the eye example:

start
 *.eye5 0 != *.refeye *.myeye != and
  'Deal with enemy
 not
  'Deal with family
stop

Here the not acts like an else statement, anything after it will only be evaluated if the proceeding statement is false

Nesting

This is were everything starts to get complex. So lets write out what we want to do:

If *.eye5 0 !=
 If *.refeye *.myeye !=
  If *.refeye 0 =
   'Deal with veggie
  Else
   'Eat
 Else
  'Deal with family
Else
 'Find food

Which translates to:

*.eye5 0 != dupbool
 *.refeye *.myeye != and dupbool
  *.refeye 0 = overbool overbool and
   'Deal with veggie
  dropbool not and
   'Eat
 dropbool not
  'Deal with family
dropbool not
 'Find food

Lets step through this section by section:

*.eye5 0 != dupbool

The dupbool here is so we leave the original condition intact for the last else statement

*.refeye *.myeye != and dupbool

Here we use dupbool again so we can use the value of the and statement again for the second to last else statement, see a pattern here?

*.refeye 0 = overbool overbool and

Here we are duplicating the top two values on the stack as we will use them both again later, but also want to and them together.

dropbool not and

Here we remove the top value from the stack (the last and's combined value), flip the value of the refeye comparison, then and the result to the previous condition (*.refeye *.myeye !=).

dropbool not

This one works exactly the same both times, it removes the top, and flips the second value (These were the reasons for the dupbools earlier).