Difference between revisions of "Inline Conditions Tutorial"
m (In-Gene Conditions tutorial moved to Inline Conditions Tutorial: Better name) |
m (Fixed typos) |
||
(2 intermediate revisions by 2 users not shown) | |||
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 | + | 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 | + | ===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 | + | 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 31: | Line 31: | ||
'Deal with family | 'Deal with family | ||
stop | stop | ||
− | Here the not acts like an else statement, | + | Here the not acts like an else statement, anything after it will only be evaluated if the proceeding statement is false |
+ | |||
===Nesting=== | ===Nesting=== | ||
This is were everything starts to get complex. So lets write out what we want to do: | This is were everything starts to get complex. So lets write out what we want to do: | ||
Line 57: | 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 | + | 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? | ||
Line 65: | Line 66: | ||
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 !=). | 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 | 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 | + | 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). |
Latest revision as of 20:36, 14 February 2014
Contents
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).