ADVANCED
This is part two of the tutorial, where we will go through some more
advanced stuff. None of this is necessary to create a basic robot,
but if you would
like to make your robot more deadly, this stuff is vital. If you have any
questions that are unanswered on my Robowar page please go to the HELP
section.
The Stack
Learning how the stack works is necessary in order to create a deadly 'bot.
This will also give you the power to command your bot to do exactly what you
want it to do.
Every robot has their own stack. The stack is just what it sounds like, a pile
of things (numbers and variables). The Stack is what the robot uses to do
calculations and shoot. When the robot runs its code, every number or variable
in the code will be added to the stack. Things on the stack can be removed with
the DROP operator (as well as many other operators too). DROP will remove the
number that's on top of the stack = the most recent number added to it.
Confusing? Let me give an example:
A robot with this code:
Drop Things from Stack....Open |
21
22
23
24 drop 25 |
will have a stack content of 21 22 23 25 when it reaches loop. The number 25 will be the top of stack, since 25 was added most recently. 24 won't appear in the stack since it was removed by DROP right after it was added.
A robot with this code:
Add things in the Stack....Open |
31
32
33
34
35 + |
will have a stack content of
31 32 33 69
when it reaches loop. This is since the + operator removes
34 and
35, sums them
and add 69 to the stack.
Try running the robot again, but now click the robot
and choose "Use Debugger" from the Help-menu. This will turn of the
debugger, which allow you to step though the code and watch the content of you
robot's stack.
**************************************************************************************
When I debug the stack additor bot: Where does "6 JUMP" come from? Why not 0
JUMP? Or 959 JUMP?
In the code 21 22 23 24
drop 25 loop: loop jump, loop is the 6th instruction (counting
starts at 0). Hence writing loop refers to the 6th instruction since the
loop:-label definition is placed right before the 6th instruction in the
code.
If you would have written 21 22 loop:
23 24 drop 25 loop jump, that would have
been displayed as 21 22 23 24 DROP 25 2 JUMP, since loop: is right before
the second instruction (remember that counting starts at 0).
Does this seem confusing? Don't worry about it then. The vital part is that you
grasp that loop: loop jump is represented by 6 JUMP in this case.
**************************************************************************************
Also, you can use variables in your stack calculations
Rotator....Open |
rotate:
|
This will be interpreted like this at the stack:
Instruction |
What it does |
Top of stack |
|
aim |
The turret's angle is 90 degrees when the battle starts. Writing aim
will place aim value on the stack. |
90 |
|
10 |
10 will be added to the stack. 90 stays since it hasn't been removed |
10 |
90 |
+ |
The - operator adds the first number to the second number, and leaves
the results. |
100 |
|
aim' |
Quoted variables are
different to unquoted. When we wrote just aim, then the value of aim
was added to the stack. But writing aim' will add aim' to the
stack instead. Quoted variables like aim' is used to interact with Store. |
aim' |
100 |
Store |
The Store operator sets aim' to
100. This is since aim' is the top of
the stack and
100 is second on the stack. You'll notice how the turret moves
when Store is interpreted. |
|
|
That wasn't so complicated? Was it? Remember that most
things a robot can do affects the stack somehow.
Here's a list of all operators in the game, they're quite many but don't let
that discourage you.
Calculate things using the stack
If you hate math, click here.
Since you've understood the concept of the stack and
operators, your robot can calculate things using the stack. Here's how to make
a robot that calculates how far away from the middle to arena it stands.
To do this, we use the following formula
d = |
![]() |
Here's how we'll type it in:
Check Distance....Open |
x 150 - dup * y 150 - dup * + sqrt print loop: loop jump |
Here’s how it works:
Instruction |
What it does |
Top of stack |
|
|
x |
The robots current x position will be added to the stack. Since the
robot starts at a random position, this number will be different every time. |
200 |
|
|
150 |
150 will be added to the stack. The number
200 will now be the second
number of the stack, since 150 now is the first |
150 |
200 |
|
- |
The - operator subtracts the first number of the stack and the second
number, and leaves the results. |
50 |
|
|
DUP |
The DUP operator clones the top number of the stack. |
50 |
50 |
|
* |
The * operator multiplies the first and the second number and
leaves the result. Just like the - operator. |
2500 |
|
|
y |
The robots current x position will be added to the stack. Since 2500
hasn't been dropped it stays on the stack. This is intentional. We'll use it
later. |
125 |
2500 |
|
150 |
150 will be added to the stack. The number
125 will now be the second number
of the stack, since 150 now is the first |
150 |
125 |
2500 |
- |
The - operator subtracts the first number of the stack and the second
number, and leaves the results. |
-25 |
|
|
DUP |
The DUP operator clones the top number of the stack. |
-25 |
-25 |
2500 |
* |
The * operator multiplicities the first and the second number and
leaves the result. Just like the - operator. |
625 |
2500 |
|
+ |
The + adds the first number with the second number. |
3125 |
|
|
SQRT |
Takes the Square Root of the top number of the stack leaving the
result. |
55 |
|
|
A challenge:
Can you figure out why this bot
occasionally
dies?
Have your bot
making strategic decisions
While the first two examples were purely demonstration
examples to show how things worked, and calculate the distance to the middle
might be somewhat useful, let's have a look at another more applicable
example.
This robot goes to the nearest wall, whichever of the 4 walls that may be
nearest.
Nearest Wall....Open |
# Watch out for walls x 270 > #First do a preliminary check to see if x 30 < or #we are very near them. y 270 > #Skipping this may result in wall colliding y 30 < or #and death... or StopGoing ifg
# Select Wall x 300 x - min #Choose the nearest wall y 300 y - min > TopBotIsNearer RightLeftIsNearer ifeg
TopBotIsNearer: #If the top or bot is nearer go for one of them y 150 > 10 * 5 - speedy' store SetAim jump
RightLeftIsNearer: #If the right or left wall is nearer x 150 > 10 * 5 - speedx' store #go for one of them SetAim jump
SetAim: speedx speedy arctan aim' store #Set aim in the direction we are moving
MoveLoop: x 275 > x 25 < or #If we've reached the wall... y 275 > y 25 < or or StopGoing ifg #...then stop MoveLoop jump
StopGoing: 0 speedx' store 0 speedy' store
loop: loop jump |
Here’s how it works:
Instruction |
What it does |
Top of stack |
|
||
x |
The
robots current x position will be added to the stack. Since the robot starts
at a random position, this number will be different every time. |
200 |
|||
300 |
300
will be added to the stack. The number 200 will now be the second number of
the stack, since 300 now is the first. |
300 | 200 | ||
x |
Adds x (which
was 200) to the stack again. |
200 |
300 | 200 | |
- |
Subtracts the
first number of the stack and the second number, and leaves the results. |
100 | 200 | ||
MIN |
The MIN operator
removes the largest and leaves the smaller of the two top numbers on the
stack. |
100 | |||
y |
Same as x, but
the robots current y position will be added to the stack. Since the robot
starts at a random position, this number will be different every time. 100 remain untouched on the stack. |
125 | 100 | ||
300 |
300 will be
added to the stack. The number 200 will now be the second number of the stack,
since 300 now is the first. |
300 | 125 | 100 | |
y |
Adds x (which
was 125) to the stack again. |
125 | 300 | 125 | 100 |
- |
Subtracts. | 175 | 125 | 100 | |
MIN |
MIN again leaves
the smaller of the first two numbers on the stack, while removing the largest. |
125 | 100 | ||
> |
> is a very
important operator. If THE FIRST number of the stack is HIGHER than the second
number on the stack, it leaves 1. OTHERWISE it leaves 0. |
0 | |||
TopBotIsNearer |
What the... a
label definition of the stack? When all you see while debugging the bot is a
number? The explanation is simple. Every time you compile the robot your label definitions are replaced by numbers, which referring to a place in the code. The number 39 refers to "TopBotIsNearer". |
TopBotIsNearer | 0 | ||
RightLeftIsNearer |
...and 51 refers
to
"RightLeftIsNearer"
|
RightLeftIsNearer | TopBotIsNearer | 0 | |
IFEG | If the third number on the stack is non-zero, then the robot will jump to "RightLeftIsNearer", and if it's 0 it'll jump to "TopBotIsNearer". |
Controlling Icons
Icons doesn't affect yours performance at all, but they'll make it look way
much better.
To look at a robot’s icons, select your robot and choose
Icon Factory from the Arena Menu (or press F4). Then you'll see
the robots icons. If you've created a new robot, you'll find 10 empty boxes
where you can add icons you've painted. Now paint your icon in your favorite
painting program. Make sure it's 32x32. Copy it and go back to RoboWar, and
paste it with Crtl-V. The cursor will turn into a cross. Click the box where it
says Standard below. Your robot now has an icon!
Icons can be activated from the code as well.
Icon Changer....Open |
SelectIcon:
#Chooses an icon based on a
random number
#set icon 3 |
Sounds
Sound Robot....Open |
{CODE
WHAT THE CODE DOES} 0 speedx' store
#Stops moving |
You might want to be interested were you can find suitable enemy robots for your new robot(s). The robots in the Robots and More Robots folders are suitable opponents. If you find them too easy or too hard, you're ready for the next step in the tutorial: Interrupts. Sorry the interrupts section is currently not available yet.
If you find an error in this site or have a suggestion
please Email Me.