Difference between revisions of "Inline Conditions Tutorial"

From WikiManual
Jump to: navigation, search
m (If, Else-If, and Else)
m (Fixed typos)
 
Line 4: Line 4:
 
To use inline conditions, you must first understand how they work.
 
To use inline conditions, you must first understand how they work.
 
===Commands===
 
===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]].
+
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===
+
===How the Boolean stack affects DNA expression===
 
Lets look at an example:
 
Lets look at an example:
 
  start
 
  start
Line 13: Line 13:
 
   10 .up store
 
   10 .up store
 
  stop
 
  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''.
+
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, Else-If, and Else==
 
===If===
 
===If===
Line 58: Line 58:
 
Lets step through this section by section:
 
Lets step through this section by section:
 
  *.eye5 0 != dupbool
 
  *.eye5 0 != dupbool
The dupbool here is so we leave the orginal condition intact for the last else statement
+
The dupbool here is so we leave the original condition intact for the last else statement
 
  *.refeye *.myeye != and dupbool
 
  *.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?
 
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?

Latest revision as of 20:36, 14 February 2014

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).