Botbattle short tutorial - 31 Jan 2003 - by Siderite

0. Introduction

  Botbattle is a game of wits. One programs its virtual robots to kill other virtual robots and then watches them fight. No ammount of key pressing or mouse moving will make a bit of a difference during a fight, only the code you created for your own bot.
  Programming is not that hard to do. Many programming tutorials make the analogy between a program and the instructions a mother gives to her son when sending him to the market (go there, get that, but not if its price is above..., etc). Of course, some programming languages are harder to learn and more complex, other are very simple.
  The Botbattle programming language is very close to the English language. Anyone understanding English would understand:
   if
   i am closest to powerup
   set direction to powerup
   endif
  Just as the mother instructs her son, just so you are instructing your bot. Go there, get that, use it to kill everything in sight! :)
  After playing a bit of Botbattle you will find easier to understand programming languages, that is, talk to your computer better and get the most of it. My hope is that young people learning from this game will themselves build games that maybe I can play and enjoy imensely.

  Good luck and have fun!

1. Basics

  A bot source code uses the special procedure "main" to execute commands. Procedures are chunks of commands that can be called by a single name. A procedure is a subprogram.
  Every bot code looks like this:

  main:
  commands_to_execute
  :main

  end

  "end" signifies the end of the source. Between ":main" and "end" you may add procedures you create. When the main procedure ends, it starts all over again, so you never have to call it from within your code.
  All procedures start their name followed by ":". You exit from a procedure using the commands "return" or "return from ".
  Take a look at the siderite procedure:

  siderite:
  say this is my procedure!
  return

  This procedure makes the bot say "this is my procedure!". It should be called from within the source of the bot with "siderite". like this:
  
  main:
  siderite
  :main

  Procedures can be called from within other procedures, of course. A procedure can have more returns, like this:

  setbot:
  set abot to closest bot  // this is a comment sign. everything after "//" is ignored.
  if  // "if" on a separate line.
  abots botmaster is siderite   // the condition to be met
  return                        //return inside if
  endif
  set direction to abot
  return

  This procedure exits if the closest bot is one of siderite's. If it isn't, it will move towards the bot. Then it will exit.
  The commands you can use inside a bot source are displayed in the right side of the editor page. But first, let's get started.

  Above we used an "if block". The if block structure is like this:

  if
  condition
  do things
  else
  do other things
  endif

  In this example if "condition" is true, the procedure "do things" is called, _else_ "do other things" is called.

  Also the "elseif" keyword is available that allows you to do several condition checks before ending an endif block. The so extended if block structure looks like this:

  if
  condition1
  do1
  elseif
  condition2
  do2
  .
  .
  .
  elseif
  conditionN
  doN
  else
  do whatever
  endif

  In this example procedure "doN" is executed when "conditionN" is true. When none of those conditions are true "do whatever" is executed.

  This extended "if block":

  if
  abot health low
  set direction to abot
  elseif
  abot energy low
  set direction to abot
  else
  set direction away from abot
  endif

  is indentical in functionality with:

  if
  abot health low
  set direction to abot
  else
  if
  abot energy low
  set direction to abot
  else
  set direction away from abot
  endif
  endif

  Logical operations can be used on conditions as well by using the keywords "not" "or" "and " and "xor" (exclusive or).

  This is a perfectly functioning if block:

  if
  bots alive equals 2
  set direction to abot
  elseif
  not
  abot health ok
  or
  abot energy low
  set direction to abot
  else
  set direction to powerup
  endif

  The order in which the logical operators are applied is bottom-up, except the NOT operator which is applied on the next condition. Example:

  if
  condition1
  or
  not
  cond2
  or
  cond3
  and
  cond4
  xor
  cond5
  exec
  endif

  The "exec" procedure will be called only if (cond1 or ((not cond2) or (cond3 and (cond4 xor cond5)))) is true. In other words, if conditions 4 and 5 are both either true or false and condition 3 is true exec will be called. If condition 1 is true or condition 2 is false, exec will also be called.
  Let us hope no one will ever have to use such an if block except in boring tutorials! :)

2. A simple bot.

  The basic things a bot must do are:
  a. move
  b. fire weapons
  c. defend itself

  Let's try a bot that does exactly that. It will move towards the closest bot if that bot has less health or run away from it if the target has more health. It will try to fire weapons at all nearby bots and defend itself from bullets.
  This bot will use 4 basic items: saw, shotgun, repeller, shield.
  The source:
  
  main:
  set abot to closest bot        // the bot will do everything depending on its closest bot
  domove                         // the procedure that moves the bot
  doattack                       // it fires at bots
  dodefend                       // defends from attacks
  :main

  domove:
  if
  i have more health than abot   // checks if it has more health than the bot we set with "set abot"
  set direction to abot          // if it has, move towards it
  else
  set direction away from abot   // if it hasn't, move away from it
  endif
  return                         // that's all

  doattack:
  if
  abot too close                 // if the bot is too close (really close)
  fire item1                     // fire saw! (massive damage, very short range) 
  endif
  if
  abot close range               // if the bot is close range (200 pixels away)
  if                             // if inside if , so called "nested" ifs
  energy ok                      // AND if you have energy to fire
  fire item3                     // fire repeller
  else                           // if you don't have energy, fire shotgun
  set target to abot             // you have to target the bot
  set leading to on              // try to fire in the bots path
  fire item2                     // shoot!
  endif                          // end the energy if
  endif                          // end the range if
  return

  dodefend:
  if
  abot close range               // if the bot is close
  if
  abot repeller on               // and fires repeller
  fire item4                     // fire shield
  endif
  if
  abot attractor on              // if he uses attractor
  fire item4                     // fire shield
  endif
  endif
  if
  bullet too close               // if there is a bullet really close to you
  fire item4                     // fire shield
  endif
  return
  
  end

3. Add to the bot.

  This is the skeleton of the bot. You may want to add lots of stuff to it. For example make the bot go after a powerup.
  Let's use this:

  getpowerup:
  if
  i am closest to powerup
  set direction to powerup
  endif
  return

  now, change the name of the "domove" procedure in "domove2". Then create another domove procedure:

  domove:
  if
  powerup on
  getpowerup
  else
  domove2
  endif
  return

  Of course, this means that if there is a powerup and the bot is not closest to the powerup, it will do NOTHING, just move in the same direction it was moving before. These are the things you must look inside your source after you solved all syntax errors in your bot code.
  To solve the problem use "return from " like this:

  domove:
  if
  powerup on
  getpowerup
  endif
  domove2
  return

  getpowerup:
  if
  i am closest to powerup
  set direction to powerup
  return from 2
  endif
  return

  This means that if the bot is closest to the powerup it will not only return from getpowerup, but from domove , as well. (return from 2 procedures)
  If the bot is not closest to the powerup, after executing the getpowerup procedure and returning from it, domove2 will be executed.
  Best not to use "return from" unless absolutely necessary, though.

4. Finishing touch.
Now, learn the different commands, items, play the game, make your bot get better and better. The only way to do this is play and learn from each victory and loss.

5. Items in the game.
The game is in constant change so this document could be already obsolete. Don't depend solely on this document to understand the multiple possibilities this game has.
At this time these are the items you may use in the game:

Laser             - uses little energy to produce a very fast beam-like projectile that causes damage.
Blaster           - uses very little energy to produce a fast beam-like projectile that causes damage. It has a lower accuracy than the laser, but it causes 50% more damage to unshielded bots (compared to the energy used to fire, of course).
Shocker           - uses little energy to produce an electric discharge than drains energy from the shields of a robot if its shields are up or stuns the robot, meaning it cannot move for a short period of time.
Disruptor         - uses little energy to produce an electric discharge than delays the robot's ability to fire items. The effect is cumulative, meaning two bullets will have an increased effect than just one.
Deimos            - uses little energy to fire a randomly hitting energy bolt which does massive damage and shock effects.
Shotgun           - uses ammo to fire many small projectiles. The weapon has more chance to cause damage when the distance to the target is smaller.
Plasma Shotgun    - same as shotgun, but uses a small amount of energy too. The projectiles go a little longer and bounce off walls.
P90               - uses very little ammo to produce a fast armor piercing bullet that causes damage. It has a 10% lower accuracy.
Bullet            - uses ammo to create a bullet that moves slow and causes medium damage.
Flamethrower      - uses ammo to create flames that have a short range and cause massive damage. It sets targets on fire.
Saw               - uses energy to cause massive damage to nearby bots (in too close range)
Repeller          - uses energy to repel bots that are in close range and also cause damage. You cannot repel bots that have attractors on or deflectors on. The damage still applies.
Attractor         - uses energy to attract bots that are in close range and also cause damage. You cannot attract bots that have repellers on or deflectors on. The damage still applies.
Shield            - uses energy to create a protective field that lasts a few seconds. Also uses energy when hit by anything.
Deflector         - same as shield, but makes bullets bounce and nullifies the pulling or pushing effects of attractors and repellers. When on, your bot is immune to the sonic bullets.
Cloaking Device   - uses energy to hide the bot. The cloak lasts until stopped or the energy is used up, time during which no other bot can tell where you are, even if they can select abot. The only command one can use to find your position when cloaked are the zone commands. Also, when in close range and cloaked, use the "rob abot" command to steal energy and ammo from your enemy. The cloak stops when you get hit or when firing anything or when using the "stop cloak" command.
Armor             - uses nothing, but makes your bot move slower by a third. It also protects from one third of all attacks.
Drone             - uses ammo to create a protective drone. The more drones, the more chance that attacks will be stopped. Drones protect from anything except field weapons like repellers and attractors, plasma burst, sonic cannon. Drones get destroyed in enemy attacks and last a long but limited time. They also have a small chance of doing repairs to the bot.
Grenade           - uses ammo to fire a grenade. It doesn't hit bots, but explodes at target, hitting everything around, like a small and powerful repeller. It hits your bot, too!
Sonic cannon      - uses energy to create a wave of damaging sound. It goes through the bot causing damage no matter the armor or shield or drones. The only defense from it is running and the deflector shield.
Gauss cannon      - uses energy and ammo to fire an instant Eraser like bullet. It hits instantly, but it takes 2 botseconds to charge and the rate of reload is very long and increases if your bot is under attack or using other items. The hit is less accurate if the target is moving fast.
Acid gun          - uses ammo to fire acid blobs that take half the health from a bot. Shields protect you from acid. The Acid gun cannot kill a bot; in fact, if the target has low health, you would spend more ammo to fire than the damage caused.
Teleporter        - uses a lot of energy to transport your bot to the target. If trying to teleport over another bot, the transport fails, but the energy is still used.
Polarmor          - uses nothing but energy from the engines, so it slows the bot down half of like normal armor. It protects from two thirds of most energy based attacks, but not from lasers. Protects from one third of all Gauss attacks and has no effect on bullets.
Absorber          - the absorber is basically a saw that causes half damage. If the enemy has no polarmor or active shields, the damage you do will come back to your bot as health (vampiric attack).
Turbo             - the turbo uses a bit of ammo to temporarily increase the speed of the bot with 4. With the turbo, the speed of 8 can be exceeded.
Chaingun          - uses ammo to produce many fast armor piercing bullets that cause damage. It has a low accuracy, slows your bot with 1, stops your bot when firing, pushes back victim.
Plasma Burst      - uses ammo to repel bots that are in close range and also cause damage. You cannot repel bots that have deflectors on. The damage still applies.
Camoarmor         - uses nothing but energy from the engines, so it slows the bot down half of like normal armor. It acts like an erratic cloaking device and you cannot rob bots with it. The faster you move, the less camoumflage you get. When firing a weapon you are not camoumflaged.
Also, an empty item adds to the speed of the bot.

6. Tips
a. bot types.
You will notice that bots fall into one of two categories: the agressive and the defensive bots. Depending on the stage of development of the Botbattle game, one or the other category will be most succesful. Very agressive bots will use most of their resources to kill another bot, while depending on a saving powerup after their attack. Defensive bots will try to survive as long as possible, then attack when the number of oponents is smaller or there is a very weak target around.
Each item can be used for something, resulting in many possible strategies:
A defensive bot could use the cloak to stay hidden and get its energy from another defensive bot, that stays in one place, waiting for the other bots to fight each other off.
Another defensive bot could choose to steal kills from other bots using the instant hitting gauss gun.
Agressive bots could use the attractor to pull victims to them, the saw to kill them off.
The acid gun could be used on health full or health ok bots, while using shotgun to fire when the target is low in health.
Use the sonic cannon as a powerful repeller, while making sure the target doesn't have a deflector on.
Use the grenade to destroy a bot that waits for others to fight or to fire at a powerup when you are not closest to it.
Use the teleporter to get first to the powerup or to get out of a corner.
Drain your adversaries energy by using a shocker to fire at their shields, then at the bots themselves, while you steal their powerups.
The items in the game are always changing, mostly because people like you, the player, come with new ideas and proposals every time. Not only you can choose a strategy to fit the items at hand, but you can request items to fit to a particular strategy you have thought of.

b. good strategies.
   - try to conserve ammo and energy, you will need them to defend yourself more than to attack. Remember there are three bots after your ass and generally you want to kill just one at a time.
   - analyse the way the other bots work and try to counteract their strategies.
   - try to get powerups at all costs. That means moving near the center to get a higher chance to get the powerups, finding ways of pushing away or stunning bots that want to reach the powerup and analyse the type of the nearby bots so you know if you can safely get the powerup.
   - mix the items you have to create better strategies.
   - don't waste energy on the shields. Try to avoid threats before they force you to use shields.
   - avoid getting cornered.
   - avoid getting too close to a bot when you are low on health
   - use good coding to defeat your opponents, do not depend on cool devices.
   - have a cool thing to say about anything you do :)

c. values.
Health, energy and ammo have a maximum value of 38.
The default bot speed is 6. Armors reduce it with 2. The Turbo increases the speed with 2 for 20 botseconds. A chaingun slows the bot with 1. Empty item slots increase speed with 1, but not to more than 8.
"empty" means 0.
"low" means under 10. "ok" means above or equal to 10.
"full" means 38 (energy, health or ammo).
"too close" means a distance of 60 from the center of your bot.
"sector range" means a distance of 1.5 sector sizes from the center of your bot.
The default sector size is 60, but you can change it in the bot code.
"close range" = 60-200
"medium range" = 200-500
"long range" = >500
The arena is 750x750 pixels large, but that can change.

7. Debugging and error codes.
The most useful tool for finding errors in your bot source is the debug mode. During a fight, move the mouse to the top-right corner of the arena to enter debug mode.
If the game found errors in your bot code, it will display them in red in the debug window.
These are the possible errors and their significance:

Missing and endif - it means that at the start of the code, right at "main:" the number of open (without endif) "if" blocks is greater than 0.
Missing a return - at the start of the code, the number of unreturned procedures is greater than 0. 
nr: endif without if - "endif" found, but no open if block. nr is the line of code where the error resulted.
nr: else without if - "else" found, but no open if block. nr is the line of code where the error resulted.
nr: elseif without if - "elseif" found, but no open if block. nr is the line of code where the error resulted.
nr: elseif after else - "elseif" is found, but it comes right after else. Everything from "elseif" to the first "endif" will be ignored by the interpreter.
nr: missed an endif - the code met the start of a procedure ("name:") but there is an open if block somewhere.
nr: Missed a return - the code met the start of a procedure ("name:") while inside of a procedure.
nr: Missed :main - the code met the start of a procedure ("name:") while in the main loop. The "main:" procedure is a special one and must be the first one in the code and end with ":main".
nr: Armor cannot be fired - you tried to fire the armor. This special item needs only be equipped in order to work.
nr: Polarmor cannot be fired - you tried to fire the polarmor. This special item needs only be equipped in order to work.
nr: No installed item to be fired - you tried to fire an item with an empty slot.
nr: Recursive procedure overload! Resetting code! - a procedure can be called from within itself. In order for this mechanism to work, you have to condition the exit from the procedure or else it goes on forever, calling itself. Botbattle allows you to only call 100 procedures before resetting the code and restarting the bot.
nr: Function function_name not found! - an invalid command has been reached. It is either the name of a procedure you forgot to build or you misspelled a word.
nr: if/unknown_word is not a valid command - same as above
nr: IF overload! Resetting code! - one can have as many as 100 "if" blocks opened in the same time. Reaching 100 means the bot code is reset. Usually this also is a recursive procedure error.
nr: item_name is not a valid item - one has queried for a certain item name, but such an item does not exist.
nr: weapon_name is not a valid weapon - one has queried for a certain weapon name, but such a weapon does not exist.
nr: bullet_name is not a valid bullet type - one has queried for a certain bullet type, but such a bullet does not exist.
nr: cannot convert  to a number - you entered a command that uses numbers and instead of a number you entered a string botbattle doesn't understand.
nr: no zone specified - you used a command with zones and you entered no zone number.
nr: zone number out of range - you used a command with zones and you entered a wrong zone number.
nr: no sector specified - you used a command with sectors and you entered no sector number.
nr: sector number out of range - you used a command with sectors and you entered a wrong sector number.
nr: no state value specified - you used a command with states and you entered no state value.
nr: state out of range - you used a command with states and you entered a wrong state value.

Also, the debug mode will show you important variables in your bot.
One of the most important is the "atempted instructions per click". Your bot executes a maximum of 100 instructions per frame or until the bot moves or fires. If you move or fire too much, this variable will show a low value of instructions. If you "think" too much, the frame will end at 100 instructions, therefore missing an opportunity to move or fire in that frame. Too low or too high values will be shown in pink.
8. Contact
This tutorial was made by Siderite. He does Java programming on the Botbattle game and is also addicted to the game itself. For any questions, flamings, money, programming job offers, etc mail me at siderite@go.ro.