Wednesday, October 28, 2009

3.1 Standard Algorithm











 < Day Day Up > 







3.1 Standard Algorithm



The standard pattern movement algorithm uses lists or arrays of encoded instructions, or control instructions, that tell the computer-controlled character how to move each step through the game loop. The array is indexed each time through the loop so that a new set of movement instructions gets processed each time through.



Example 3-1 shows a typical set of control instructions.





Example 3-1. Control instructions data structure




ControlData {

double turnRight;

double turnLeft;

double stepForward;

double stepBackward;

};








In this example, turnRight and turnLeft would contain the number of degrees by which to turn right or left. If this were a tile-based game in which the number of directions in which a character could head is limited, turnRight and turnLeft could mean turn right or left by one increment. stepForward and stepBackward would contain the number of distance units, or tiles, by which to step forward or backward.



This control structure also could include other instructions, such as fire weapon, drop bomb, release chaff, do nothing, speed up, and slow down, among many other actions appropriate to your game.



Typically you set up a global array or set of arrays of the control structure type to store the pattern data. The data used to initialize these pattern arrays can be loaded in from a data file or can be hardcoded within the game; it really depends on your coding style and on your game's requirements.



Initialization of a pattern array, one that was hardcoded, might look something such as that shown in Example 3-2.





Example 3-2. Pattern initialization




Pattern[0].turnRight = 0;

Pattern[0].turnLeft = 0;

Pattern[0].stepForward = 2;

Pattern[0].stepBackward = 0;

Pattern[1].turnRight = 0;

Pattern[1].turnLeft = 0;

Pattern[1].stepForward = 2;

Pattern[1].stepBackward = 0;

Pattern[2].turnRight = 10;

Pattern[2].turnLeft = 0;

Pattern[2].stepForward = 0;

Pattern[2].stepBackward = 0;

Pattern[3].turnRight = 10;

Pattern[3].turnLeft = 0;

Pattern[3].stepForward = 0;

Pattern[3].stepBackward = 0;

Pattern[4].turnRight = 0;

Pattern[4].turnLeft = 0;

Pattern[4].stepForward = 2;

Pattern[4].stepBackward = 0;

Pattern[5].turnRight = 0;

Pattern[5].turnLeft = 0;

Pattern[5].stepForward = 2;

Pattern[5].stepBackward = 0;

Pattern[6].turnRight = 0;

Pattern[6].turnLeft = 10;

Pattern[6].stepForward = 0;

Pattern[6].stepBackward = 0;

.

.

.








In this example, the pattern instructs the computer-controlled character to move forward 2 distance units, move forward again 2 distance units, turn right 10 degrees, turn right again 10 degrees, move forward 2 distance units, move forward again 2 distance units, and turn left 10 degrees. This specific pattern causes the computer-controlled character to move in a zigzag pattern.



To process this pattern, you need to maintain and increment an index to the pattern array each time through the game loop. Further, each time through the loop, the control instructions corresponding to the current index in the pattern array must be read and executed. Example 3-3 shows how such steps might look in code.





Example 3-3. Processing the pattern array




void GameLoop(void)

{

.

.

.

Object.orientation + = Pattern[CurrentIndex].turnRight;

Object.orientation -- = Pattern[CurrentIndex].turnLeft;

Object.x + = Pattern[CurrentIndex].stepForward;

Object.x -- = Pattern[CurrentIndex].stepBackward;

CurrentIndex++;

.

.

.

}








As you can see, the basic algorithm is fairly simple. Of course, implementation details will vary depending on the structure of your game.



It's also common practice to encode several different patterns in different arrays and have the computer select a pattern to execute at random or via some other decision logic within the game. Such techniques enhance the illusion of intelligence and lend more variety to the computer-controlled character's behavior.















     < Day Day Up > 



    No comments: