Math Explanation:
 
OK, so you probably looked at that equation and thought "what the hell is going on there????". Well, take comfort in the fact that I thought the same at first, but I did manage to work it out.

Basically, these types of equation are just a clever way of combining several 'current heading'-sensitive routines. Imagine that you wanted to find the coordinates of the square behind an enemy which is directly in front of you. The calculation differs depending on which way you are facing.
 

  • If you are facing east, then the coordinates of the square behind the enemy are (#enemy_x + 1  ,  #enemy_y). That's a simple enough formula to use.
  • However, if instead you are facing south, then the coordinates of the square behind the enemy are now (#enemy_x  ,  #enemy_y + 1).
  • Here's how the four different equations can be built up into one big equation which does the same thing:
     

  • The format of this type of equation is the following:
  • When you are facing north, your #cur_head variable contains the number 1. That means that all of the brackets containing (#cur_head-1) will be evaluated to zero. As we all (should) know, anything multiplied by zero is zero, so in the case of a cybug facing north, the parts of the formula for west, south and east are all multiplied by zero, and so have no effect on the result. The only part which is non-zero is the line with the stuff for facing north.

    The division at the end of each line is just a scaling factor. For example, when the cybug is facing north, the first three brackets of the north-facing part of the equation will be (1-2)*(1-3)*(1-4) which equals -6. This is multiplied with the stuff that we want to calculate, so we need to then divide by -6 to end up with the correct values.

    So how do you figure out what Torque is calculating in these huge equations? Well, I've tried to simplify things a bit by color-coding the equations. The important bits are colored violet, and then other stuff is white (as in the example here). Also, they are always in the same order, so the first violet part is what is calculated if Torque is facing north. The second part is calculated when Torque is facing east. The third part is for south and the last violet-colored part is for west.

    It all seems a little complicated, but if you use the equation and just change the violet parts to suit your cybug, then it should all work well.