If Y>N:N+1->N
If Y<N:N-1->N
If X>O:O+1->O
If X<O:O-1->O
I'm thinking that the best way to deal with the problem earthnite suggested would be to use lists. For example:
Y-(A=25)+(A=34->Y //player coord 1
X-(A=24)+(A=26->X //player coord 2
{Y,X->L5 //save player coords as a list
{N,O->L4 //save AI as a list
L5-L4->L3 //take the difference of the lists to determine "distance" from the player"
N-(abs(L3(1))>abs(L3(2)) and L3(1)<0)+(abs(L3(1))>abs(L3(2)) and L3(1)>0)->N //If the N distance is greater than the O distance and the direction is negative, move left. If the N distance is greater than the O distance and the direction is positive, move right.
O+(abs(L3(1))≤abs(L3(2)) and L3(2)<0)-(abs(L3(1))≤abs(L3(2)) and L3(2)>0)->O //If the O distance is greater than or equal to the N distance and The direction is negative, move up. If the O distance is greater than or equal to the N distance and the direction is positive, move down
Using lists allows us to store the values of each player or AI. The way I have it set up above, it calculates the distance vertically and horizontally to the player. By using the sign of the result, we can determine which way to move the AI in relation to the axis.
Detailed explaination:
Assume L3 is the list in which we store the distance from the player
Since the distance to the player can never be 0,0 we can ignore this in our value testing.
First we need to determine if the vertical distance or the horizontal distance is farther. Since the values could be positive, negative, or a combination we use abs(). This allows us to see the "true" distance. Whichever distance is greater, that is the direction we want to move.
Next, we need to determine which way to move on that axis. Since the values have signs, we can use the sign to determine which way we need to move. If we are moving vertically, and the sign is negative, then we need to move up. If we are moving Horizontally and the sign is negative, then we move left. The inverses of the directional movement is also true (positive vertical moves down, positive horizontal moves right). Thats great, but what If a value equals zero? well, thats why its important to remember that we specifically exclude 0,0 as a distance. That means that there will always be one axis with a greater true distance than the other, and by using peicewise expressions, we can determine which axis to move without too much complexity.
So, lets start on the peicewise expression. lets assume that N is our horizontal axis and O is the vertical axis.
First, lets check the horizontal axis. The pseudo code looks like this:
- N-[(the horizontal distance is greater than the vertical) and (the direction is negative)]+[(the horizontal distance is greater than the vertical) and (the direction is positive)]->N
by making sure that the horizontal distance is greater in each section, we avoid overlap of scenarios. By using the direction, we can determine whether to add or subtract 1, since the value of the (this and that) statement will always be one or zero.
The above code is the code that checks which "true" distance is greater, if the horizontal is greater, it will return a 1 else it will return a zero. To determine the sign of the value, we check if it is greater or less than zero
Well, what if it is exactly zero. Again, we will never have a case of 0,0 without triggering the end game sequence. Therefore the other axis will take care of the event.
Putting one section of the peicewise together, we get:
- [(the horizontal distance is greater than the vertical) and (the direction is negative)] = (abs(L3(1))>abs(L3(2)) and L3(1)<0)
- [(the horizontal distance is greater than the vertical) and (the direction is positive)] = (abs(L3(1))>abs(L3(2)) and L3(1)>0)
Now if both statments in the (this and that) expression, are true; then that expression will return a value of 1. Since we have a set value of one which we want to subtract or add from/to the axis, we simply say, subtract (expression for negative direction) from the axis and add (expression for positive direction) from the axis. Since only one expression will be true, you will only be adding or subtracting one since a true expression returns a value of 1, and a false a value of 0.
That means that the full horizontal axis equation is:
- N-(abs(L3(1))>abs(L3(20) and L3(1)<0)+(abs(L3(1))>abs(L3(2)) and L3(1)>0)->N
It is basically the same thing for the veritcal axis with one small change. If the distance vertically and horizontally is EQUAL (1,1 or 2,2 or 3,3 etc.) we will move vertically first. This prevents the AI from being completely stuck in that situation.
- O+(abs(L3(1))≤abs(L3(2)) and L3(2)<0)-(abs(L3(1))≤abs(L3(2)) and L3(2)>0)->O
By using lists, we are able to modify the data without changing our source of data. That is to say, we aren't evaluating two different sets of data. If we simply evaluated the variables without using lists the data could change after the first expression. If the data changes after the first expression, it could affect the result of the second expression. By saving the data in a list, evaluating the expressions and changing the variables, then recreating the lists, we avoid the problem of more than one case being true at once.