The Absolute Beginners Guide to AI Wars

Version: 1.1

Modified: 7-31-98

Volume: 1

Author: Tony Dwyer

Contributors: Dave Parks, Jeremy Frydenlund

If you would like to add anything to this tutorial/FAQ, please email me at krazy_ivan@mindless.com

Table of Contents - Volume 1

___________________________________________________________

1 - Getting Ready

1.a - Downloading AI Wars

2 - Learn the basics of AI Wars

2.a - The Basic Idea

2.b - Simple Bug

2.c - Speed, speed, speed

____________________________________________________________

1 - Getting Ready

1.a - Downloading AI Wars

The first step in getting ready to use AI Wars is to actually get it (if you haven't done so already. There are many places to get the latest version. The best place is right here:

AI Wars is approximately 20 mb in size. To install it, simply run the downloaded exe file.

2 - Learn the Basics of AI Wars

2.a - The Basic Idea

The basic idea behind AI Wars is that you write a program, and then you run it against other programs in an arena. The program (or bug) interacts with objects (barriers, flags, mines) and other programs (friends, enemies). The object is to score the highest, which is usually accomplished by killing all the other bugs. Instead of trying to teach you everything about AI Wars by explaining, I will attempt to teach you by having you code actual stuff. If you have never programmed before in any computer language I suggest you try to follow along as best as you can.

2.b - Simple Bug

The first thing we will do is make a simple bug. First, start up AI Wars and get into the command editor. Then, copy the following text out of here then

paste it into AI Wars and save it as a bug. To do that, write a filename in the

A.I. Filename Field at the top of the screen. Then click on the "Save File" button. Here is the text to copy:

name ExampleBug

author Tony Dwyer

iff code Example1

raise shield

Main:

long range scan

if scan found enemy then

lower shield

launch missile

raise shield

turn right

move forward

turn left

goto Main

end if

if scan found flag then

move forward

move forward

goto Main

end if

move forward

if bump barrier then

Bump:

generate random

if random is 1 then turn right

if random is 2 then turn left

if random is 3 then turn right

if random is 4 then turn left

move forward

if bump barrier then goto Bump

goto Main

end if

goto Main

Stick into a battle with turtle1.ai, using open.map and the default ammo values. Run it and watch them. Most of the time turtle1 will beat ExampleBug. If I were to enter this bug into a tournament I would be dead last. The code is written beautifully, but it is very inefficient. Before we tighten it up a bit, lets take a more detailed look at it.

name ExampleBug

author Tony Dwyer

iff code Example1

This section of code just declares the name of the bug, who made it, and sets the iff code. The iff code lets you determine wether another bug is a friend or enemy. For a demonstration of this, run another battle with 3 ExampleBugs, and a turtle1. Notice that the ExampleBugs do not fire on each other. This is because they have the same iff code.

raise shield

This section of code, does just as it implies, it makes your bug raise it's shield. This should always be one of the first things a bug does unless you specifically don't want your bugs shield up. Note that in later version of AI Wars, there is a chance you could overheat if you have your shield up too long.

Main:

long range scan

if scan found enemy then

lower shield

launch missile

raise shield

turn right

move forward

turn left

goto Main

end if

This section of code is part of the main loop in the bug. This section scans at long range, and if an enemy is found, launches a missile at it, then jumps out of the way of any possibly incoming missiles. This is doen with an if block. If blocks are simply groups of instructions that only execute if the If Condition is true. In this case, it only executes if the last scan found an enemy. All If blocks must end with the "end if " statement. This lets the compiler know where the end of it is.

if scan found flag then

move forward

move forward

goto Main

end if

This part of code is checking wether the last scan we did (the long range scan), found a flag. If it does, the bug will move forward to get it. Notice that there is an instruction that forces the bug to go back to the beginning of the main loop, essentially scanning the flag all over again. This is to make the bug recover faster once it has gotten the flag, and it also

makes the bug continually scan to see if an enemy got the flag first. Remember that if a flag is taken when the bug has no damage, the bug will suffer 50% damage. This examplebug does not check its own damage level, and so is susceptable to this mistake. We will correct this later.

move forward

if bump barrier then

Bump:

generate random

if random is 1 then turn right

if random is 2 then turn left

if random is 3 then turn right

if random is 4 then turn left

move forward

if bump barrier then goto Bump

goto Main

end if

goto Main

This portion of code moves the bug forward each time it goes through the main loop, then checks to see if it ran into something. The "if bump barrier" conditional is only true if the bug tried to move and was unable to do so because of something blocking the way. It does not "detect" barriers in front of the bug, so if the bug doesn't actually run into the barrier, "if bump barrier" will be false, and the if block will not execute. In this code, if the bug runs into a barrier, it does the equivalent of flipping a coin to see wether it should turn right or left, then turns right or left, and tries to move again. If it runs into a barrier yet again, it will repeat the process.

2.c - Speed, speed, speed

Now I will introduce you to some techniques of optimizing your bugs. This is one of the most important things, because the battle revolves around speed. Most of the bugs nowadays are pretty evenly matched, so it comes down to which bug reacts faster.

The measure of time in AI Wars is the click. In v2.1c the click is equal to one instruction. This is very inefficient, because many parts of the code are counted as lines and evaluated. This includes labels, gotos, gosubs, and if blocks. That means that the following code would count as 5 clicks, wether the conditional is true or not!

if scan found enemy then

turn right

move forward

turn left

end if

That makes if-blocks extremely inefficient. the same code could be written:

if scan found enemy then turn right

if scan found enemy then move forward

if scan found enemy then turn left

This code only takes 3 clicks, much better then the if-block version. You can generally do without if-blocks in your code. So lets rewrite the example bug shown earlier to be more efficient without the if-blocks:

name ExampleBug2

author Tony Dwyer

iff code Example2

raise shield

Main:

long range scan

if scan found enemy then lower shield

if scan found enemy then launch missile

if scan found enemy raise shield

if scan found enemy then turn right

if scan found enemy then move forward

if scan found enemy then turn left

if scan found enemy then goto Main

if scan found flag then move forward

if scan found flag then move forward

if scan found flag then goto Main

move forward

Bump:

if bump barrier then generate random

if bump barrier then if random is 1 then turn right

if bump barrier then if random is 2 then turn left

if bump barrier then if random is 3 then turn right

if bump barrier then if random is 4 then turn left

if bump barrier then move forward

if bump barrier then goto Bump

goto Main

Run this bug a few times with turtle1. Notice that it is just slightly faster. The code looks very bad, but it is more efficient then the first one, but still not all the way there. If you have v2.1c or later, you can use system variables (like #enemy_x) in labels. This provides the oppurtunity for extremely fast processing of scan results. Lets rewrite the above code yet again using this technique:

name ExampleBug3

author Tony Dwyer

iff code Example3

raise shield

Main:

long range scan

goto #scan

move forward

if bump barrier then goto Bump

goto Main

2:

lower shield

launch missile

raise shield

turn right

move forward

turn left

goto Main

5:

move forward

move forward

goto Main

Bump:

generate random

if random is 1 then turn right

if random is 2 then turn left

if random is 3 then turn right

if random is 4 then turn left

move forward

if bump barrier then goto Bump

goto Main

Notice that this code makes the bug's response to an enemy slower. In the earlier example, the bug could send a missile screaming within 2 clicks of detecting the enemy. This is very fast, in fact, faster then most tournament class bugs, but the earlier example's flag routine doesn't kick in for 8 clicks. The above example has a response time of 4 clicks for the missile routine and 4 clicks for the flag routine. This is a better rounded bug, since most bugs have a response time of 3-5 clicks after detecting an enemy, 4 clicks is not bad at all.

__________________________________________________________________

End of Volume 1