1 1.9 dholland /* $NetBSD: attack.c,v 1.9 2009/05/24 22:55:03 dholland Exp $ */ 2 1.3 cgd 3 1.1 cgd /* 4 1.3 cgd * Copyright (c) 1980, 1993 5 1.3 cgd * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.5 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 cgd * may be used to endorse or promote products derived from this software 17 1.1 cgd * without specific prior written permission. 18 1.1 cgd * 19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 cgd * SUCH DAMAGE. 30 1.1 cgd */ 31 1.1 cgd 32 1.4 christos #include <sys/cdefs.h> 33 1.1 cgd #ifndef lint 34 1.3 cgd #if 0 35 1.3 cgd static char sccsid[] = "@(#)attack.c 8.1 (Berkeley) 5/31/93"; 36 1.3 cgd #else 37 1.9 dholland __RCSID("$NetBSD: attack.c,v 1.9 2009/05/24 22:55:03 dholland Exp $"); 38 1.3 cgd #endif 39 1.1 cgd #endif /* not lint */ 40 1.1 cgd 41 1.4 christos #include <stdio.h> 42 1.4 christos #include <math.h> 43 1.4 christos #include "trek.h" 44 1.1 cgd 45 1.1 cgd /* 46 1.1 cgd ** Klingon Attack Routine 47 1.1 cgd ** 48 1.1 cgd ** This routine performs the Klingon attack provided that 49 1.1 cgd ** (1) Something happened this move (i.e., not free), and 50 1.1 cgd ** (2) You are not cloaked. Note that if you issue the 51 1.1 cgd ** cloak command, you are not considered cloaked until you 52 1.1 cgd ** expend some time. 53 1.1 cgd ** 54 1.1 cgd ** Klingons are permitted to move both before and after the 55 1.1 cgd ** attack. They will tend to move toward you before the 56 1.1 cgd ** attack and away from you after the attack. 57 1.1 cgd ** 58 1.1 cgd ** Under certain conditions you can get a critical hit. This 59 1.1 cgd ** sort of hit damages devices. The probability that a given 60 1.1 cgd ** device is damaged depends on the device. Well protected 61 1.1 cgd ** devices (such as the computer, which is in the core of the 62 1.1 cgd ** ship and has considerable redundancy) almost never get 63 1.1 cgd ** damaged, whereas devices which are exposed (such as the 64 1.1 cgd ** warp engines) or which are particularly delicate (such as 65 1.1 cgd ** the transporter) have a much higher probability of being 66 1.1 cgd ** damaged. 67 1.1 cgd ** 68 1.1 cgd ** The actual amount of damage (i.e., how long it takes to fix 69 1.1 cgd ** it) depends on the amount of the hit and the "damfac[]" 70 1.1 cgd ** entry for the particular device. 71 1.1 cgd ** 72 1.1 cgd ** Casualties can also occur. 73 1.6 dholland ** 74 1.6 dholland ** resting -- set if attack while resting 75 1.1 cgd */ 76 1.1 cgd 77 1.4 christos void 78 1.6 dholland attack(int resting) 79 1.1 cgd { 80 1.4 christos int hit, i, l; 81 1.4 christos int maxhit, tothit, shldabsb; 82 1.4 christos double chgfac, propor, extradm; 83 1.4 christos double dustfac, tothe; 84 1.4 christos int cas; 85 1.4 christos int hitflag; 86 1.1 cgd 87 1.1 cgd if (Move.free) 88 1.1 cgd return; 89 1.1 cgd if (Etc.nkling <= 0 || Quad[Ship.quadx][Ship.quady].stars < 0) 90 1.1 cgd return; 91 1.1 cgd if (Ship.cloaked && Ship.cloakgood) 92 1.1 cgd return; 93 1.1 cgd /* move before attack */ 94 1.1 cgd klmove(0); 95 1.7 dholland if (Ship.cond == DOCKED) { 96 1.1 cgd if (!resting) 97 1.9 dholland printf("Starbase shields protect the %s\n", 98 1.9 dholland Ship.shipname); 99 1.1 cgd return; 100 1.1 cgd } 101 1.1 cgd /* setup shield effectiveness */ 102 1.1 cgd chgfac = 1.0; 103 1.1 cgd if (Move.shldchg) 104 1.1 cgd chgfac = 0.25 + 0.50 * franf(); 105 1.1 cgd maxhit = tothit = 0; 106 1.1 cgd hitflag = 0; 107 1.1 cgd 108 1.1 cgd /* let each Klingon do his damndest */ 109 1.7 dholland for (i = 0; i < Etc.nkling; i++) { 110 1.1 cgd /* if he's low on power he won't attack */ 111 1.1 cgd if (Etc.klingon[i].power < 20) 112 1.1 cgd continue; 113 1.7 dholland if (!hitflag) { 114 1.1 cgd printf("\nStardate %.2f: Klingon attack:\n", 115 1.1 cgd Now.date); 116 1.1 cgd hitflag++; 117 1.1 cgd } 118 1.1 cgd /* complete the hit */ 119 1.1 cgd dustfac = 0.90 + 0.01 * franf(); 120 1.1 cgd tothe = Etc.klingon[i].avgdist; 121 1.1 cgd hit = Etc.klingon[i].power * pow(dustfac, tothe) * Param.hitfac; 122 1.1 cgd /* deplete his energy */ 123 1.1 cgd dustfac = Etc.klingon[i].power; 124 1.9 dholland Etc.klingon[i].power = dustfac * Param.phasfac * 125 1.9 dholland (1.0 + (franf() - 0.5) * 0.2); 126 1.1 cgd /* see how much of hit shields will absorb */ 127 1.1 cgd shldabsb = 0; 128 1.7 dholland if (Ship.shldup || Move.shldchg) { 129 1.1 cgd propor = Ship.shield; 130 1.1 cgd propor /= Param.shield; 131 1.1 cgd shldabsb = propor * chgfac * hit; 132 1.1 cgd if (shldabsb > Ship.shield) 133 1.1 cgd shldabsb = Ship.shield; 134 1.1 cgd Ship.shield -= shldabsb; 135 1.1 cgd } 136 1.1 cgd /* actually do the hit */ 137 1.8 dholland printf("\aHIT: %d units", hit); 138 1.1 cgd if (!damaged(SRSCAN)) 139 1.9 dholland printf(" from %d,%d", 140 1.9 dholland Etc.klingon[i].x, Etc.klingon[i].y); 141 1.1 cgd cas = (shldabsb * 100) / hit; 142 1.1 cgd hit -= shldabsb; 143 1.1 cgd if (shldabsb > 0) 144 1.1 cgd printf(", shields absorb %d%%, effective hit %d\n", 145 1.1 cgd cas, hit); 146 1.1 cgd else 147 1.1 cgd printf("\n"); 148 1.1 cgd tothit += hit; 149 1.1 cgd if (hit > maxhit) 150 1.1 cgd maxhit = hit; 151 1.1 cgd Ship.energy -= hit; 152 1.1 cgd /* see if damages occurred */ 153 1.7 dholland if (hit >= (15 - Game.skill) * (25 - ranf(12))) { 154 1.8 dholland printf("\aCRITICAL HIT!!!\a\n"); 155 1.1 cgd /* select a device from probability vector */ 156 1.1 cgd cas = ranf(1000); 157 1.1 cgd for (l = 0; cas >= 0; l++) 158 1.1 cgd cas -= Param.damprob[l]; 159 1.1 cgd l -= 1; 160 1.1 cgd /* compute amount of damage */ 161 1.9 dholland extradm = (hit * Param.damfac[l]) / 162 1.9 dholland (75 + ranf(25)) + 0.5; 163 1.1 cgd /* damage the device */ 164 1.1 cgd damage(l, extradm); 165 1.7 dholland if (damaged(SHIELD)) { 166 1.1 cgd if (Ship.shldup) 167 1.9 dholland printf("Sulu: Shields knocked down, " 168 1.9 dholland "captain.\n"); 169 1.1 cgd Ship.shldup = 0; 170 1.1 cgd Move.shldchg = 0; 171 1.1 cgd } 172 1.1 cgd } 173 1.1 cgd if (Ship.energy <= 0) 174 1.1 cgd lose(L_DSTRYD); 175 1.1 cgd } 176 1.1 cgd 177 1.1 cgd /* see what our casualities are like */ 178 1.7 dholland if (maxhit >= 200 || tothit >= 500) { 179 1.1 cgd cas = tothit * 0.015 * franf(); 180 1.7 dholland if (cas >= 2) { 181 1.9 dholland printf("McCoy: we suffered %d casualties in that " 182 1.9 dholland "attack.\n", 183 1.1 cgd cas); 184 1.1 cgd Game.deaths += cas; 185 1.1 cgd Ship.crew -= cas; 186 1.1 cgd } 187 1.1 cgd } 188 1.1 cgd 189 1.1 cgd /* allow Klingons to move after attacking */ 190 1.1 cgd klmove(1); 191 1.1 cgd 192 1.1 cgd return; 193 1.1 cgd } 194