Death Adder's Great FunkBots Tutorial By: Death Adder (Suprise...) Version 1.6 Covering FunkBots 1.15 Table Of Contents: 1. Introduction 2. Language Syntax 3. Variables and Their Usage 4. Mathematical Operations 5. Conditional Operations 6. Repetitive Operations 7. Shooting At Things 8. Putting It All Together 9. Contacting Me 1. Intro The purpose of this document is to teach the general populus how to program FunkBots so that they can waste hours and hours of their time trying to write the most intelligent bot around. Theres going to be a contest for the best bot so hopefully everyone is paying attention. ;-) Oh, and this document is more of a refresher than a complete guide. If you don't know how to program at all then this document might be hard reading. If you've done any programming whatsoever you should be able to understand me. ;) 2. Language Syntax As the author describes it: FunkScript (the language the bots are programmed in) is a mishmash of C and BASIC. You do not need a ; at the end of the line as in C. In fact ; signals the start of a comment. Every character after a ; is a comment. If you've ever programmed in BASIC the syntax will be very very familiar. Like BASIC, FunkScript is not case sensitive. CASE = case. Each line of the script maxes out at 80 characters. If you are inclined to write a line of code longer than 80 characters then just go up to 80 and then continue the code on the next line. The next few sections detail the exact syntax of the various language elements. 3. Variables and Their Usage Just like BASIC (and C) variables are assigned value in what is called explicit form. What this means: Variable = Value. Variables are always assigned a value in this way. It should however be noted that a variable can be assigned the value of another variable. In this case the general form is Variable1 = Variable2. In both cases there must be a space before and after the =. Later, when mathematical operators are described, we'll cover other forms of assigning a value to a variable. Whatever form is used, the left of the equal sign always contains just the variable to put the value in. Variables in FunkScript can be subscripted. This means that each variable (that is subscripted) is really many variables in one. Each variable within a subscripted variable is accessed by telling FunkScript which variable you want to access. The general syntax is: SubscriptVariable[SubScript]. The subscript is the # of the child variable (member of the parent variable, in this case: SubscriptVariable.) Examples of Variables: robot.x, item.type[num.items], item.type[data[5] * 2] FunkScript has many built-in variables. These variables are listed below and described in Detail: robot.x[robot] - A subscripted variable that tracks the X location of every robot in play. Replace robot with the number of the robot that you want the X location of. Don't use a subscript when changing your bot's x location. Just -> robot.x = Something robot.y[robot] - The same as robot.x but for the Y location. You still don't use a subscript when changing your own y location. me - The # robot that you are. robot.energy[robot] - How much energy 'robot' robot.ammo[robot][type] - How much of 'type' ammo 'robot' has robots.alive - How many robots are still alive robot.alive[robot] - TRUE/FALSE tells if specified robot is alive robot.hits[robot1][robot2] - # of times robot1 has hit robot2 robot.total.hits[robot] - How many times the specified bot has hit another bot robot.kills[robot] - How many other bots the specified bot killed total.robots - How robots there were at the start of battle. item.delay - The delay set in battle file. After this many rounds another item will appear. num.items - The number of items currently on the map. item.x[Item] - The X location of the specified item item.y[Item] - The Y location of the specified item item.type[Item] - The type of the specified item. See below for type codes item.chance[Item] - The chance of item 'Item' appearing. This is a very deceiving variable. It's the chance out of the total possibilities. Lets say this variable is 5. Now, if the total of all the chances is 50 then the percentage chance is really 10%. Unfortunately there is no variable that tells the total of all of the chances. Valid values of Item: 0 - 8 0 = Normal Ammo (normal) 1 = Power Ammo (power) 2 = Seeking Ammo (seeking) 3 = Power Seeking Ammo (power.seek) 4 = Mine (mine) 5 = Power Mine (power.mine) 6 = +1 Energy (energy.1) 7 = +2 Energy (energy.2) 8 = +5 Energy (energy.5) Each value also has an alias (see below for an explaination of aliases) The alias for each type is in parentheses() above. num.shots - The number of shots currently on the screen shot.x[Shot] - The x location of the specified shot. It's decimal in v1.15 shot.y[Shot] - The y location of the specifed shot. Decimal in v1.15 shot.type[Shot] - The type of the specified shot. Valid types range 0 - 5 0 = Normal Ammo 1 = Power Ammo 2 = Seeking Ammo 3 = Power Seeking Ammo 4 = Mine 5 = Power Mine shot.speed.x[Shot] - The horizontal speed of the specified shot (+ or -) + is to the right - is to the left Returns a floating point # shot.speed.y[Shot] - The vertical speed of the specified shot (+ or -) + is down - is up Returns a floating point # data[#] - General purpose subscripted variable. What you store in here is up to you. (It has to be a number though....) # can be any integer from 0 - 999. Each entry is set to zero when the bot is initialized. rand[MaxRand] - returns a random number from 0 - (MaxRand - 1) debug - The value of this variable is printed on screen below your bot. Use it as you will to debug your bot. The scripting language allows aliasing the variables. What does this mean? You can assign an arbitrary name to another variable. This arbitrary name can then be used instead of the real name. Why do this? Lets say that you are storing the closest item in data[5]. Since you are always using it for that same purpose you can assign an easy to remember alias (lets use ClosestItem). The syntax is: define Alias OriginalVariable OriginalVariable can be a subscripted variable or a non-subscripted variable. Since Ver 1.15 of FunkBots you can make defines that are part of another variable. Ex: DEFINE x data[0] is valid even though several variables have x's in them. 4. Mathematical Operations By this time I'm sure you all realize that it'd be nice to be able to do math in the script. Well, you can. You can do math on the right side of an equation. As previously mentioned, the left side must contain only a variable. The valid mathematical symbols are: + (Addition... Pretty self explainatory) - (Subtraction) * (Multiplication) / (Division) ^ (Exponentiation) % (Modulus Division - Returns the remainder of a division) < (Less than - Returns TRUE or FALSE) > (More than - Also returns TRUE/FALSE) <= (Less than or equal to - TRUE/FALSE) >= (More than or equal to - TRUE/FALSE) = (Equal To - Can return TRUE/FALSE or assign) <> (Not equal to - TRUE/FALSE) && (Boolean AND - If both values are TRUE then it returns TRUE, else FALSE) || (Boolean OR - If either are TRUE it returns TRUE, else FALSE) sqrt[Num] - A variable not an operation. It returns the square root of Num. Order of Operations: ^ *, /, % +, - >, <, >=, <= =, <> && || A good portion of the above symbols are of little or no use just yet. Once we cover conditional operations they'll come in handy. Parentheses can be used to force sections of the equation to evaluate in a different order than they otherwise would. For instance: 2 * 2 / 3 (4 / 3) is not the same as 2 * (2 / 3) (2 * .66666666666) 3 + 3 * 2 (3 + 6) is not the same as (3 + 3) * 2 (6 * 2) Quick Note: As of ver 1.10 exponents are allowed. There is a square root variable. If you don't want to use the variable you can use exponentiatation. Remember that raising a number by .5 is the same as taking the square root. In general The Nth Root of NUM is the same as NUM ^ (1 / N) So, if you want the Nth root (> 2) you HAVE to use this method. 5. Conditional Operations There is but one conditional operator. It's called IF. It compares one expression to another. The valid operators are =, <, >, >=, <=, <>, &&, || The syntax of an if statement: if (Something Operator SomethingElse) Stuff to do if it's true . . . endif Operator can be one of the valid operators and Something and SomethingElse can be any valid expression. EX: if (robot.x[me] > 100) robot.x = robot.x[me] - 1 endif Also || and && can be used to string multiple conditions together. Ex: if (robot.x[me] > 100 && robot.x[me] < 200) robot.y = robot[me].y + 1 end if You may also nest if statements (put one if statement completely in another if statement) EX: if (robot.x[me] > 50) if (robot.y[me] < 50) robot.x = robot.x[me] - 1 robot.y = robot.y[me] + 1 end if endif And still yet, you can also (as of FB 1.15) use the else construct. This allows you to execute code if the if statement is NOT true. EX: if (robot.x[me] > 5) robot.x = robot.x[me] - 1 else robot.y = robot.y[me] + 1 endif 6. Repetitive Operations What if you want to do something over and over? Well, do loops come to the rescue. There is no FOR statement or equ. The syntax of do is: do stuff to do . . . if (Certain condition is met) break endif loop The code loops though all of the code between do and loop until the if statement becomes true and then break exits the loop. EX: do data[0] = data[0] + 1 if (data[data[0]] > 3) break endif loop As with IF, do-loop can be nested. 7. Shooting At Things Everyone's favorite section... What war game would be complete without violence and bullet fire?!? Shooting is very simple. The syntax: shoot ammotype x_destination y_destination The ammotype is one of the defined values from above. (normal, seeking, etc) x_destination & y_destination can point at anywhere on the map. Multiple calls to shoot will be ignored. Only the first valid shoot order will be accepted. In fact, you can only fire once every 30 cycles. So, if you call shoot again before 30 cycles is up you will not actually fire. You also will not fire if you don't have the ammo type you are requesting. You can however call shoot for each ammotype in order to fire the most powerful first and then go down though ammo. Basic.FB does this. EX: shoot power.seek robot.x[4] robot.y[4] shoot power.mine robot.x[4] robot.y[4] shoot power robot.x[4] robot.y[4] shoot seeking robot.x[4] robot.y[4] shoot mine robot.x[4] robot.y[4] shoot normal robot.x[4] robot.y[4] The above code will shoot at robot 4 with the most powerful weapon that it can. 8. Putting It All Together OK, so now we have a lot of commands that look something like a mutant form of BASIC. What can we do with it all? Where to start? Well, pasted below is the full source of BASIC.FB The author (who also coincidentally wrote the FunkBots program) did a pretty good job of commenting the source. The bot even stands up pretty well in the arena. Granted, a real attempt at AI could waste this bot but it's a very good example for beginning AI. ; This is an example FunkScript. It is for a simple FunkBot that goes ; to the nearest item and shoots at the nearest FunkBot all the time. ; These are the variables we will use define # data[0] define short.dist data[1] define x.dist data[2] define y.dist data[3] define distance data[4] define closest data[5] ;find closest item # = 0 short.dist = 1000 ; do a loop to check every item ; # tells us which item we are checking do ; find x difference and store it in x.dist x.dist = robot.x[me] - item.x[#] if (x.dist < 0) x.dist = 0 - x.dist endif ; find y difference and store it in y.dist y.dist = robot.y[me] - item.y[#] if (y.dist < 0) y.dist = 0 - y.dist endif ; make distance equal the larger of the 2 differences distance = x.dist if (y.dist > x.dist) distance = y.dist endif ; shot.dist holds the shortest distance. see if item is new shortest if (distance < short.dist) short.dist = distance ; if it is, set new shortest distance closest = # ; closest holds which item it is endif # = # + 1 ; increase # to check new item if (# >= num.items) ; if all items are checked, then break break endif loop ;go to the closest item robot.x = item.x[closest] robot.y = item.y[closest] ; now we do the exact same thing to find the closest robot # = 0 short.dist = 1000 do if (robot.alive[#] && # <> me) x.dist = robot.x[me] - robot.x[#] if (x.dist < 0) x.dist = 0 - x.dist endif y.dist = robot.y[me] - robot.y[#] if (y.dist < 0) y.dist = 0 - y.dist endif distance = x.dist if (y.dist > x.dist) distance = y.dist endif if (distance < short.dist) short.dist = distance closest = # endif endif # = # + 1 if (# >= total.robots) break endif loop ; try to shoot whatever we can, attempting with the most powerfull ; weapons first shoot power.seek robot.x[closest] robot.y[closest] shoot power.mine robot.x[closest] robot.y[closest] shoot power robot.x[closest] robot.y[closest] shoot seeking robot.x[closest] robot.y[closest] shoot mine robot.x[closest] robot.y[closest] shoot normal robot.x[closest] robot.y[closest] I will now explain a few things that all of you could do that would improve this bot many times over: 1. You can track bullets (even ones you didn't shoot) sooooo, dodge the fricken things! 2. Don't shoot directly at another bot. Theres this thing called motion. Bots move. So do bullets. By the time your shot gets to your target the target will have likely moved. Can you figure out how to more accurately aim??? Hmmm 3. Other bots will probably be going after the same items as your bot is. Can you think of a way to help prevent that? 4. Any shot that doesn't move from round to round is bound to be a mine. You don't want to hit one. ;-) So don't. 5. You can track the energy of other bots. Go for the kill.... 6. Go for the most appropriate item. If you have only 1 energy try going for energy instead of ammo You may notice that most of the 6 tips are rather vague and none have code samples. Why you ask? Because I plan on entering the AI competition and if I give you all code that does all 6 tips you'll all have a HUGE head start in the competition and I'll have given away my AI. As it is I've dropped quite a few very good hints. Those 6 should definately get you thinking and get you started on your entry. I hope to have a lot of good competition. May the war begin. Whahahahahahaha ;-) 9. Contacting Me So, you found an error in my tutorial or you want to suggest something? Should I provide more sample code? How about more suggestions? If you would like to contact me then look below: E-mail: Adderd@iserv.net or ckidder@raider.grcc.cc.mi.us Website: members.xoom.com/AcidSoft/ ICQ #: 22462652 IRC: Anyone want to start a IRC channel for FunkBots? I can provide the maintainance bot. I just need to know if anyone is interested. It'd be on EFNET and we could call it #FUNKBOTS Whatcha think???