Terminator — 6 of 33

Matt Weiner

Release 0

Chapter 4 - Grid Mechanics and Miscellaneous Math

The grid size is always 18. [Maybe make this a variable for difficulty settings?]

A thing has a number called x-coordinate. A thing has a number called y-coordinate. The x-coordinate of a thing is usually -99. The y-coordinate of a thing is usually -99. [-99 means off the grid.]

To decide whether (x - a number) by (y - a number) is on the grid:

if x < 1, no;

if y < 1, no;

if x > grid size, no;

if y > grid size, no;

yes.

To decide whether (x - a number) by (y - a number) is off the grid:

if x by y is on the grid, no;

yes.

Table of Direction Increments

directionx-incrementy-increment
north01
west-10
south0-1
east10
northwest-11
northeast11
southwest-1-1
southeast1-1

Definition: a direction is compass if it is a direction listed in the Table of Direction Increments.

To decide what number is the squared distance from (x - number) by (y - number) to (z - number) by (w - number):

decide on ((x - z) * (x - z)) + ((y - w) * (y - w)).

To decide what number is the squared distance from (item - thing) to (x - number) by (y - number):

decide on the squared distance from the x-coordinate of the item by the y-coordinate of the item to x by y.

To decide what number is the squared distance from (item one - thing) to (item two - thing):

decide on the squared distance from the x-coordinate of item one by the y-coordinate of item one to the x-coordinate of item two by the y-coordinate of item two.

To decide what number is (m - a number) roundly divided by (n - a number):

if m < 0: [This is m/n + 1/2 rounded down, which is the same as m/n rounded to the nearest whole number; except that when m is negative we need to make the 1/2 negative too. Don't risk this if n is negative.]

decide on ((2 * m) - n) divided by (2 * n);

otherwise:

decide on ((2 * m) + n) divided by (2 * n).

To decide which direction is the direction (item1 - thing) lies from (item2 - thing):

let delta-x be the x-coordinate of item1 minus the x-coordinate of item2;

let delta-y be the y-coordinate of item1 minus the y-coordinate of item2;

if delta-x is greater than 0: [item1 is somewhere to the east]

if (41 * delta-y) is greater than (100 * delta-x), decide on north; [the tangent of 22.5 degrees is about .41, so this basically checks to see if item1 is more north than northeast]

if (100 * delta-y) is greater than (41 * delta-x), decide on northeast;

if (delta-y * -41) is greater than (100 * delta-x), decide on south;

if (delta-y * -100) is greater than (41 * delta-x), decide on southeast; [note that the order we do these in matters. If the item1 is almost due south, it'll pass this check too; but we've already decided on south. And if it fails all the checks it falls in between southeast and southwest, so we decide on east]

decide on east;

if delta-x is less than 0: [item1 is somewhere to the west]

if (41 * delta-y) is greater than (delta-x * -100), decide on north; [the tangent of 22.5 degrees is about .41]

if (100 * delta-y) is greater than (delta-x * -41), decide on northwest;

if (delta-y * -41) is greater than (delta-x * -100), decide on south;

if (delta-y * -100) is greater than (delta-x * -41), decide on southwest;

decide on west;

if delta-y is greater than 0, decide on north; [if no decision has been made, delta-x is 0, so item1 is directly north-south]

if delta-y is less than 0, decide on south;

decide on down. [item1 is at the same coordinates as item2]