A SPECIAL INCLUSION
A C version of Capture the Flag
by Todd Kepus
Introduced to the Atari 400 just five years ago (in the seventh grade), Todd Kepus is now an Atari ST programmer. He purchased his ST from a local user group immediately after reading about it in Atari Explorer two years ago. His ST interests include fast animation routines and 3D graphic programming. He is currently a Computer Science major at California State University in Sacramento.
Strathello is an adaptation of the famous game Capture the Flag—always a favorite among computer enthusiasts—and this version will run on the Atari 520ST in low resolution. It's written in C and 68000 assembly language, using the Megamax C compiler, and is available on this issue's disk version or on the Atari SIG on Delphi.
Strathello is played on a 14×9 grid, with the player's pieces on the left side and the computer's on the right. Both the player and computer have thirty-six pieces, occupying four columns of the screen on each side. Two water holes will appear in the upper middle and lower middle portions of the screen to further complicate strategy. The object of the game is to capture the opponent's base.
The player and computer have nine different pieces each. Their ranking is as follows: Seeker—lowest rank; Investigator—destroys Seeker; Mercenary—destroys the Seeker and the Investigator; Equalizer—destroys Seeker, Investigator, and Mercenary; and Terminator—destroys everything above.
In addition to the regular rankings, there are three special pieces that have their own special powers: Mine—a stationary object that destroys anything that lands on it—it can only be removed by a Decoder; Secret Service—destroys only the Terminator; and Decoder—destroys the Seeker, Investigator and a Mine.
Finally, there's the base, which can be captured by any of the opponent's movable pieces. The game ends when either base is captured.
When the program is run, the credits and playing board appear. To set your pieces on the screen, place the square cursor anywhere in any of the four left columns by moving the mouse. Then press the left mouse button to set the piece. A piece may not be set upon another. The pieces are set on the board in ascending order of rank, that is, the Seekers are placed first, followed by the Investigators, and so on. It's a good idea to place the Seekers in the fourth column and move in to the left as you set the pieces. That way, the base will be placed behind all the players, protecting it from a quick computer victory.
Playing and moving
After all of the player's pieces are set, the computer's pieces immediately appear and the game begins. To move, place the cursor on a piece that's not surrounded. The message Click Source will appear in the upper left corner of the screen with the name of the piece directly beneath it. If the cursor is on a piece that can be moved, the message Click Source will begin to blink, indicating that it is a valid source piece. Click the left mouse button. The message Click Destination will appear in the upper left-hand corner of the screen, indicating that you must now choose a box in which to move.
A piece can only move one space vertically or horizontally. When the cursor is over a valid destination, the message will begin to blink, indicating a possible choice. Click the mouse when the cursor is on the square to which you wish to move. The computer then makes its move. The game continues until either one of the bases is captured, or until it's impossible to win the game.
How the program works
Strathello was written using the Megamax C compiler, one of the best C compilers for the Atari ST. I recommend it to any ST programmer, whether hobbyist or professional. One of the nicest features about Megamax is that it allows in-line assembly code. Strathello allocates a background screen for drawing images on, and all buffer and screen moves are done with assembly language for quick results.
Two of the most important assembly routines included in the program are object movement routines. They are specifically designed to move 16×16 color objects for maximum speed.
The first routine, position, places a 16×16 object on the screen. It takes the X- and Y-coordinates, the location of the object data, and the address of the screen or buffer in which to copy the image. The data block that defines the object is the same format of screen memory layout. Each object data block takes 256 bytes, including the mask. A special routine, makemask, is used to create the mask from the object's image data.
The second important routine is the replace function, which takes the same X- and Y-coordinates, the buffer to copy from, and the buffer or screen in which to copy. In Strathello, replace copies the buffer to the screen, replacing the object and thus, restoring the background. Immediately after the screen is restored, the new object is drawn to avoid blinking.
Before the main loop is called, which activates player and computer movement, the player must first set his players on the screen. This is accomplished by the function set__player. Afterwards, the computer's pieces are randomly established by the function comp__set. The main loop of the program is the function players__turn. This routine handles the cursor, player and computer movement routines. At the end of the function, it checks for victory by either side and exits the loop if one side has won.
The two important functions in the evaluation of the player's movements are uncndmove and do__attack. If the player moves to a vacant position, an unconditional move is made (uncndmove). If the player happens to move on an opponent, do__attack is called to determine the victor of the move. Two error-checking routines are called before each routine, to determine if the player has correctly chosen a source and destination. They will return an error value if the cursor is over an invalid position. The program determines the mouse's position on the screen by calling get__pos, which returns two numbers for the horizontal and vertical positions.
After the player has moved, computurn is called to move the computer's piece. The computer then establishes a list of all its possible moves, to decide on the best one. The highest priority moves are placed at the top of the list, and if not executed, the second most important move will be checked, continuing down the list until a move is made.
One important function the computer uses to move a piece is victory, which checks for victory of a computer's piece over a player's piece. Another function, protects__piece, protects the specified piece by moving to intercept the opponent. The home__in function moves a computer piece toward another specified position, such as the opponent's base, and is also called when the computer's Secret Service is trying to search and destroy the player's Terminator. This function is the backbone of the computer's strategy, selectively picking targets and reselecting after the target is destroyed.
Before the computer does anything else, it first checks the board for an instant victory. This is accomplished with do__flag, which returns a nonzero value if it can achieve a victory. A quick victory is defined as one that can be made in the next turn. Since the computer relies on its powerful pieces to achieve a quick victory, the retreat function is used to retract a piece if it's about to be attacked by a player's piece. This function is given second priority, following the flag check. When the game ends, you can play again or quit.
The assembly language graphics routines were created to be directly compatible with the Color Object Editor's source code, from which the graphics characters were created. Since the Color Object Editor doesn't create a mask for the data, the makemask function is used to create the mask.
Strathello was programmed to be easily enhanced. For example, it takes little effort to add more intelligent computer algorithms, to make the computer more logical when determining how to attack the other side. The array realscrn contains all of the piece's codes, and whatever is contained in that array determines the actions of the computer's strategy. For example, one could change the number of Terminators on either side to give a distinct advantage.
Another possible application would be to take the computer's algorithm out and replace it with some code, allowing the array to be sent over the modem line, to play with a friend. In addition, since each piece has a different code in the array, all you have to do is change the value of the piece desired. The codes for all of the pieces are as follows:
|Space (no piece)||0|
For a real challenge, you could make all of the computer's pieces Terminators. Just be sure to increase the array sizes near the beginning of the program. For instance, if you want to increase the amount of Terminators the player has, change the 3 in arrays p5x, p5y to the number of Terminators desired.
It's a simple matter to add pieces to the screen, by changing the values in the array and plotting the character to the screen with the function setguy. To use this function, supply the X- and Y-coordinates, along with the address of the graphic form of that particular piece. For example, to place an additional player's mine in the middle of the screen, place this fragment of code directly after set__player in the main loop:
/* sets player's nine at pos. 7,4 */ realscrn [71 = 8; /* draw image on screen */setguy (7, 4, (long) (mine_p));
You must then change the number of allowed mines from five to six. To do this, change the arrays pmx and pmy to pmx and pmy. Changing the number of pieces allowed is the same process as above for any piece; however, you must change the corresponding array.
Notes of strategy
Since the first pieces set up are the Seekers, it's a good idea to place them at the front of the battle line and reserve a space at the far left side of the screen for the base. Then, place mines around the base to block the computer's Decoders. To beat the computer, simply knock out the computer's Decoders; the computer cannot win if it has no Decoders to destroy the mines around your base. If possible, protect the Secret Service piece, since the computer will try to quickly overtake you with its Terminators.