Chapter 9 PAGE 1 STARTREK THE COMPUTER PROGRAM CHAPTER 9 9.1 Shootback Now that the Enterprise has been armed with offensive weapons (Phasers and Photon Torpedoes), and has been provided with shields, it is time to provide the enemy with a means to protect and defend themselves. The Klingons are thus allowed to take hostile action against the Enterprise and are also allowed to move around inside the quadrant. The flowchart for the SHOOTBACK subroutine which perform the hostile actions is shown in figure 9.1 . It is invoked by the main loop if there are any Klingons in the quadrant, or when the Enterprise moves in a quadrant containing Klingons. The operation can take place in any quadrant at any time, so the first thing to test is if the Enterprise is docked at a Starbase. If it is, the Starbases's shields protect the Enterprise so there is no need for any further action in the subroutine. A loop then takes place in which all eight elements in the Klingon Array are tested. If any Klingon is active, namely it has a positive amount of energy, and the "game over flag" is not set, signifying that the Enterprise has been wiped out by previous Klingon action, then it may take hostile action. If it does, it can either fire a photon torpedo or use its phasers. If it fires off a torpedo, the torpedo can either hit or miss the Enterprise. if it hits the Enterprise, the sub-system closest to the point of impact is damaged, if it misses, a message to that effect is displayed. If the Klingon has fired a phaser burst, the amount of energy fired must be computed and deducted from the energy of the enemy ship. The amount of energy impacting the shields of the Enterprise must then be determined and subtracted from the amount of energy that were in the shields. A test must then be performed to determine if the Enterprise's shields have been beaten down to zero. If they have, the game is over, the player has lost so the game over flag is set signifying that event and the loop is terminated because there is no point in letting the remaining Klingons in the quadrant fire at a non existent ship. If the Enterprise is still alive after the hit, the size of the hit must be examined to see if it was massive enough to momentarily overload the shields. If such an overload took place, then one of the ship's sub-systems closest to the point of impact is damaged. Each Klingon gets a turn to take hostile action and may or may not do so depending on how favorable a position it is on at any time. Once all of them have had their chance, the computer checks to see if the energy in the shields of the Enterprise is low, and if it is, displays a message warning the player of that fact, before the subroutine terminates. A BASIC language version of the SHOOTBACK function is shown in figure 9.2. It begins in line 600 with the usual REMark. Copyright (c) Joe Kasser 1989 Chapter 9 PAGE 2 STARTREK THE COMPUTER PROGRAM Line 610 examines the state of the condition flag 'C$' to see if the Enterprise is docked. If it is, the program line counter is advanced to line 680 bypassing the remainder of the subroutine. The loop begins at line 620 using the 'FOR/NEXT' technique. The amount of energy available to the Klingon stored in the K3(I) array for each possible Klingon is tested. If it is equal to or less than zero, the inside of the loop is bypassed and the program counter advanced on to line 670. Also, there is a random probability that the Klingon, even if it is active will not fire. In that event, the program counter is also advanced to line 670 bypassing the rest of the loop. The function 'RND(Z)<.1*(I+Z)' is used to determine the probability of the Klingon taking hostile action. A random number is generated by the computer. It is then compared with a number that increases for every iteration of the loop as follows. The loop counter (I) ranges from 0 to 7. Each time around, a constant value of one represented by the parameter 'Z' is added to the loop counter and the result is multiplied by 0.1. Each time the loop counter is incremented, the probability that the Klingon will take hostile action is reduced. For example, the limits are as follows I PROBABILITY 0 0.1 1 0.2 2 0.3 3 0.4 4 0.5 5 0.6 6 0.7 7 0.8 thus the probability that the first Klingon will not fire is 10%, that the second in the quadrant will not fire is 20% etc. This technique minimizes the probability that all the Klingons in the quadrant will fire at the Enterprise so that the player stands some chance when going into a quadrant containing seven Klingons. It is "realistic" to expect that not all Klingons will be in a position to fire at any one time. The decision as to whether the Klingon fires a torpedo or its phasers at the Enterprise is taken at the start of line 630. A random number is generated and compared with the amount of energy that the Klingon has, divided by five hundred. This ensures that as the energy level of the Klingon gets lower the probability of the Klingon Captain deciding to use photon torpedoes increases and the probability of the Klingon using its phasers decreases. This is logical because phaser bursts are ineffectual at very low energy levels. Two "fallouts" from using the 'IF RND(Z)>K3(I)/500 ' statement are that the Klingon will never use its torpedoes when it has more than five hundred units of energy, and, when you are trying to get it to surrender by beating its shields down to zero slowly, it is liable to keep firing torpedoes at you. These torpedoes however home in on the Copyright (c) Joe Kasser 1989 Chapter 9 PAGE 3 STARTREK THE COMPUTER PROGRAM energy in your shields, so that if you remember to keep your shields at a minimum level, you should be able to dodge the enemy torpedoes. If a torpedo is fired, the program flow continues along line 630 and displays a message stating that a torpedo has been detected. The program now has to determine if the torpedo hit and if so, if any damage was caused. 'H' is thus used as a temporary variable. It is first set to a random percentage of the amount of energy in the shields of the Enterprise by the statement 'H=RND(Z)*E1'. Since both the phaser and torpedo hit sections of the subroutine use the same nested subroutine starting at line 130 to process the hit and determine if any damage occurred to the Enterprise, and this subroutine subtracts the amount of the hit from the energy levels of the Enterprise, that operation in line 140 must be compensated by adding the amount of the hit to the energy levels before the nested subroutine subtracts it so that the net result of the hit will not effect the energy levels aboard the ship. Line 630 then advances the program counter to line 660. The phaser burst is processed in line 640. A random percentage of up to forty percent of the energy aboard each Klingon is taken from the Klingon by the statements 'H=K3(I)*.4*RND(Z) : K3(I)=K3(I)-H' and then the amount of that energy that actually hits the Enterprise is determined as a function of the distance between the Klingon and the Enterprise by the statement 'H=H/(SQR((K1(I)- S1)^2+(K2(I)-S2)^2))^.4' which utilizes basic geometry to determine the distance based on the row and column co-ordinates (remember, the square of the sum of the sides of a right triangle is equal to .....). Line 650 then displays the amount of the phaser hit, and both the phaser and torpedo hit routines come together at line 660 which tells the player where the offensive was launched from. Line 660 displays the point of origin message even if the short range sensors are damaged, if you wanted to inhibit that you'd have to modify the line to read 660 IF D(1)=0 THEN PRINT " from sector";K1(I)+Z;",";K2(I)+Z 665 GOSUB 130 : IF F9>0 THEN 680 and introduce a new line (665). Once the message has been displayed the subroutine beginning at line 130 is called. This subroutine processes the hit by subtracting the amount of the hit from the shields of the Enterprise and determining if it was a massive hit. If it was, then something aboard the ship is damaged. After returning from the subroutine, the state of the "end of game flag" (F9) is tested to determine if the Enterprise was destroyed by the enemy. If it was, the program counter is advanced forward to line 680 effectively terminating the subroutine. This is an abnormal exit from the loop, but what is the point of staying in the loop if the game is over. The display generated by successive Klingons will be nonsense. The loop terminates at line 670 when all the enemy ships have had a turn to fire. The last statement on the line, checks the state of the shields. If they contain less than one hundred Copyright (c) Joe Kasser 1989 Chapter 9 PAGE 4 STARTREK THE COMPUTER PROGRAM units of energy , the computer will display a warning message to the player. The message contains the 'CHR$(7)' statement to also provide an audible warning. Line 680 terminate the subroutine with the usual 'RETURN' statement. The flow of the SHOOTBACK subroutine may be easier to understand if it was written in the form shown in figure 9.3 in a cross between "flowchart" and BASIC. The second part of the Klingon's defense capability is the ability to move around. Many versions of the game do not feature movement by the Klingons. The player thus plays against "sitting ducks" which makes the game very easy. On the other hand allowing the Klingons to move in between quadrants complicates the programming because separate matrices or arrays would have to be used to store both the actual contents of a quadrant at any time, and the value the computer placed in it at the time that the quadrant was scanned. If a Klingon moves out of one quadrant into another, the arrays will then contain different values. You may be interested in modifying the game later to add that capability, letting the enemy slowly move towards the Enterprise or towards the Starbases, or just move them at random, for they too can travel at warp speed. That would certainly complicate the game, because then the map records would not be of any use in remembering where the Klingons are, but only serve to remember where the bases are and which quadrants contain large numbers of stars. There are a number of algorithms that can be used for moving Klingons around the quadrant. for example, they can be positioned at random. They can be moved plus or minus a number of sectors from their previous position. They can be moved towards the Enterprise so that they seem to be slowly "coming in for the kill". Each algorithm requires a different implementation. The simplest one to use is that shown in figure 9.4 which moves the Klingons about at random. It can be argued that since they are traveling at high speed, maneuvering both to attack and to avoid the weapons fired by the Enterprise, their movement could appear to be random. The flowchart is basically one big loop. The state of each possible enemy ship in the quadrant is tested. If it is found to be alive, a random sector is chosen. If that sector is blank, the Klingon is moved into it. If the sector is occupied by anything, the Klingon stays where it is. An alternative approach could be to try again for a blank sector, but why bother. The sector it is occupying could just as easily show up as the chosen random sector. This approach does have one side effect. The more things that there are in the quadrant, the greater the probability that the sector selected at random will be occupied and thus the greater the probability that the Klingon will not move. Each approach used in implementing most functions of the game may also show up side effects that were not specified. Copyright (c) Joe Kasser 1989 Chapter 9 PAGE 5 STARTREK THE COMPUTER PROGRAM Sometimes these side effects work in your favor, sometimes they don't matter and sometimes an alternative approach has to be chosen because the side effects are detrimental to the program. In our case, there can only be up to seven stars, up to seven Klingons, one starbase and the Enterprise in any particular quadrant. That means that out of sixty four sectors in the quadrant, only sixteen can be occupied in the worst case. Nobody should ever notice that there is a bias towards not moving the Klingons as a function of the number of objects in a sector. The BASIC language version of the flowchart could take the form shown in figure 9.5 which starts at line 700 with the usual REMark. The loop begins at line 710. The loop used takes the form of a FOR/NEXT statement. K3(I) is the flag that contains the state of the Klingon. If the value stored in the element of the array associated with a particular Klingon is positive, the Klingon is alive and well. The test is performed using the statement 'IF K3(I)<=0 THEN 740'. which causes the remainder of the subroutine to be bypassed if the contents of the element are equal to or are less than zero. In other words, if the Klingon is not active. Line 720 calls the subroutine in line 50 to find a pair of random numbers between 0 and 7. The last part of the line tests a sector defined by those co-ordinates to see if it is blank. If it is not blank, the program counter is advanced forward to line 740 bypassing the remainder of the subroutine. Line 730 moves the Klingon. The contents of the sector matrix represented by S(I,J) are set to one, effectively emptying the sector by the statement 'S(K1(I),K2(I)) = Z'. Remember, K1(I) and K2(I) represent the position of the Klingon in the quadrant. The new position is set up by the pair of statements 'K1(I) = X : K2(I) = Y' and lastly, the contents of the sector array associated with the Klingon are set to show the presence of the Klingon by the statement 'S(X,Y) = 4'. The loop terminates in line 740 as does the subroutine once the loop is over. In this version of the game, the Klingons are moved before they shoot back at the Enterprise. The MOVEKLINGON subroutine is called before the SHOOTBACK subroutine allowing the Klingon to shoot after it moved., The hit routine which displays the co- ordinates occupied by the enemy ship when It fired on the Enterprise is then up to date when is your turn to shoot. If the Klingons shot first and then moved, you would not be able to locate them by the 'hit' message but would have to request a full short range sensor display each time you wanted to fire. 9.3 Resign Now that the Klingons can fight back, you ought to be given the option of resigning from the game. This way, when you are playing it in its current configuration you can give up and start Copyright (c) Joe Kasser 1989 Chapter 9 PAGE 6 STARTREK THE COMPUTER PROGRAM again if you get too badly damaged. A flowchart of a typical resign sequence is shown in figure 9.6. The flowchart begins by testing to see if any Klingons are present in the quadrant. An attempt to resign in a battle situation is teated as an attempt to surrender by the game simulator. Since Klingons do not take prisoners, your surrender is not accepted and they continue to try to destroy the Enterprise. If the simulator allows the player to resign, a verification must be performed to make sure that the player realy intends to resign and did not call the function by mistake. It is good practice to always verify any critical operation in any program. In this case, what could be more critical than ending the game. If the player really did intend to resign, the 'end of game' flag is set accordingly, if not the flowchart terminates. 9.4 Transporter and Shuttlecraft The Transporter and the Shuttle allow you to possible obtain energy and spare parts/materials without docking at a starbase. You can use the computer to scan the quadrant you are in and possibly locate sources of energy and materials or parts. You cannot however stock up on parts to expedite future repairs, you can only obtain parts to repair anything damaged at the time. The Enterprise is a starship not a freighter. Should you mine a lot of energy, and have an excess when you dock at a starbase, the base will relieve you of the excess and send you on your way with the standard amount. The Transporter allows you to beam a landing party down to a star system. The Shuttlecraft allows you to send a landing party to the sustem. The transporter uses energy, the shuttlecraft uses time. Once the landing party have reached their destination, Mr Spok will notify you how much time the landing party have to spend on the ground to achieve their aims. The function of the landing party depends on where they are. If they have been landed near a source of Dilithium crystals, they will mine the cystals and then purify them using some equipment that Mr Scott will lash together. Mr Spok will give you an estimate of the amount of energy that you can expect to receive from the cystals that can be mined in a day. You then decide how long the mining operation lasts, they stay there for that amount of time and then return. If the landing party is on an inhabited planet, they will make a quick estimate of the amount of parts and materials that they can find so that the repairs to the Enterprise can be expedited. You will again be asked to decide how much time the landing party is to spend on the ground. They will stay there for that amount of time, and then return. The Transporter cannot be operated, or the Shuttle launched Copyright (c) Joe Kasser 1989 Chapter 9 PAGE 7 STARTREK THE COMPUTER PROGRAM if the shields are up. On board sub-system repair goes on while the landing party are doing their thing. The flowchart for the transporter is shown in figure 9.8. Copyright (c) Joe Kasser 1989