Understanding Syntax

Many of the players whom have had some kind of programming experience often times forget that syntax can be a hurdle for those without the experience. It comes naturally to those of us who've put together at least a few lines of code and understand how finicky a computer can be in understanding its instructions. Even the most strictest of English teachers we've all had are quite lax when compared to the rigid guidelines needed for a working code. Roboforge AI operates in much the same manner.

Listed above are the operands that are available for use within the Roboforge AI. I'm sure that many of them are quite recognizable, but lets go over each of them for a quick description. Forgive my initial simple descriptions. Describing how to multiply and divide is something that I'm assuming anyone playing Roboforge knows.

Equals Operand - Equals can be used for both a query and a command. You use it to compare two items, or use it to assign a value to a variable.
Not Equal - Not Equal is used to determine if two items are NOT equal in value. When you use this operand in Roboforge it will appear like "!="
Add - Add is used to combine two integer (number) variables.
Subtract - Subtract is used to remove the value on the right from the value on the left.
Multiply - Multiply ... um.. er... well... it multiplies two values. 2 x 2 = 4, etc.
Divide - Divide appears in Roboforge like "/"
And - AND is used in an expression where you want to determine if two values are true.
Or - OR is used in an expression where you want to determine if either of two values is true.
NOT - NOT is used to find when a value is not True.
Greater Than - Greater Than describes when the expression on the Left is higher in value than the expression on the Right. Ex. 8 > 2 (8 is greater than 2)
Less Than - Less Than describes when the expression on the Left is lower in value than the expression on the Right. Ex 2 < 8 (2 is less than 8)
Greater Than or Equal To - Greater Than or Equal to describes when the expression on the Left is Greater than or Equal to the Expression on the Right. In Roboforge it appears like ">=". Ex. 8 >= 2 AND 8 >= 8 are both True Statements.
Less Than or Equal To - Less Than or Equal To describes when the expression on the Left is Less then or Equal to the Expression on the Right. In Roboforge it appears like "<=". Ex. 2 <= 8 AND 2 <= 2 are both True Statements

Understanding Boolean True/False Values

Now that you've looked at the Basic descriptions of them I will try and explain how they can be used in the Roboforge AI. Roboforge bases its much of its functionality on Boolean Expressions. Boolean expressions have only 2 values - True or False. When setting up a query in AI you generally ask about something to determine if it is True or False so that you can then act on those results.

"Is the enemy greater than 10 meters away?" or "Is my bot's health Less than or Equal to 50%?"

Both of these questions are trying to determine a state of True or False. If the enemy bot is more than 10 meters away then the first question is True, if it is less than 10 meters away then the first question is False. The same holds true for the second question. If your bots health is greater than 50% or if it equals 50% EXACTLY then the question would return a True value. If your bot's health is less than 50% then the question would return a value of False. me.distanceToEnemy > 10
        Y
        N
me.health >= 50
        Y
        N

Each of these examples returns a value of True (Yes) or False (No) dependant on the values of me.distanceToEnemy and me.health. We decide what we want our bot to do depending on which value is returned. If we are looking for a False return then we set up the action activated by this being false in the "N" line. If we are looking for a True return then we place our code on the "Y" line. In either case our bots AI will perfom its next actions based on the result of our query. Let me introduce you to the AND statement.

Combining Queries

The above examples make a simple query to determine the state of single expressions, a distance check and a health check. But sometimes you may want several specific expressions to occur as True or False (or even a combination of the two!) before you take an action. Let's set up a query that checks to see if both of those expressions are True in a single query. In other words, we want to make sure that our Health is greater than 50% AND the enemy's distance to us is greater than 10 meters and only take action when BOTH of these states are True.

me.distanceToEnemy > 10 AND me.health >= 50
        Y
        N

Now, whatever action we want to take when the enemy is further than 10 meters and our health is higher than 50% will not occur until both states are True. Combining queries is a powerful tool for your robot to determining what specifically is going on in its environment before it takes action. You COULD simply ask the first question and, if True, check for the answer of the second question before taking action, but this requires more lines of code, which is detrimental to the speed of your bot's thinking speed (See "AI, how it works and How to Trim it").

If you wanted to, you could even set this up so that an action would be taken as long as at least one of the queries was True. We use the OR to determine this :

me.distanceToEnemy > 10 OR me.health >= 50
        Y
        N

If me.distanceToEnemy is greater than 10 meters (True) and me.health is less than 50% (False) then the query we have is still True overall : If THIS is true or THAT is true then treat the whole thing as TRUE. If this confuses you try and think of it this way. If apples are on sale OR oranges are on sale then buy some. As long as one or the other (or both) is on sale then we will buy them. If neither is on sale then we don't buy them.

Finally we have NOT. NOT is usually useful in queries where you are checking for specifically false statements. Perhaps we want something to happen, but only if the state of something specific is not True.

NOT (me.distanceToEnemy > 10 AND me.health >= 50)
        Y
        N

Here we only want to take an action only if both statements are False, ie. NOT True. Notice also the use of the Parenthisis. This groups the two statements into one query as a whole which is then checked with NOT. Without the parenthisis the NOT statement would have only worked with the me.distanceToEnemy variable and wouldn't have worked with the me.health variable and we'd have ended up checking to make sure the distance wasnt greater than 10 AND the health was greater or equal to 50. Parenthisis are another powerful tool for grouping whole statements together to compare against one another, all in one line of code :

NOT (me.distanceToEnemy > 10 AND me.health >= 50) or (me.health > enemy.health AND me.energy < 10)
        Y
        N

This query is a little more complicated and is very easy to get lost in. There are several checks going on for the states of True and False and the NOT statement can darken already muddy waters. But by taking the whole thing one section at a time you can manage your way thru it.

With as many global variables you see up there, its actually a simple query checking only 2 states. Those two states are defined within the borders of the parenthisis. The first set of parenthesis checks to see if the enemy's distance is greater than 10 and also if your bot's health is equal to or higher than 50%. If BOTH of those queries is True then the whole expression within the parenthisis is considered True. The NOT statement in front of that first set of parenthesis tells us we DONT want those queries to be true. So we are looking to see if those 2 queries are NOT True.

The second set of parenthesis is seperated from the first by an OR statement. Remember the OR as we'll get back to it in just a second.

Inside the second set of parenthesis we see a check of our bot's health against that of the enemy. If our bot's health is more than the enemy's health then this statement is True. If the enemy's health is greater than ours then the statement is False. We are also checking to see if our energy levels are less than 10%. If our health is greater than our enemys AND our energy is less than 10 then the statement within the second parenthesis is true.

Whew, like i said, alot going on and alot to keep track of. With the OR operand seperating the two parenthisis groups we know that we are looking for either state to be True for this WHOLE query to be True and to follow the "Y" branch of the question. So if our distance is less than 10 and our health is less than and not equal to 50 (because of the NOT operand) OR our health is greater than the enemy's health AND our energy is less then 10 THEN the whole statement is considered TRUE and our AI will perfrom whatever action or ask the next question found on the "Y" line, ignoring the "N" line. If the whole query comes out False then our AI will ignore the "Y" line and follow the instructions on the "N" line.

I know this last example can prove quite confusing. Again, work thru it slowly, seperating the sections out by using the parenthesis to group things together. If you are still having trouble following how things are determined then email me with your questions and I'll try my best to explain further.