2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)phaser.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
45 /* factors for phaser hits; see description below */
47 # define ALPHA 3.0 /* spread */
48 # define BETA 3.0 /* franf() */
49 # define GAMMA 0.30 /* cos(angle) */
50 # define EPSILON 150.0 /* dist ** 2 */
51 # define OMEGA 10.596 /* overall scaling factor */
53 /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
58 ** There are up to NBANKS phaser banks which may be fired
59 ** simultaneously. There are two modes, "manual" and
60 ** "automatic". In manual mode, you specify exactly which
61 ** direction you want each bank to be aimed, the number
62 ** of units to fire, and the spread angle. In automatic
63 ** mode, you give only the total number of units to fire.
65 ** The spread is specified as a number between zero and
66 ** one, with zero being minimum spread and one being maximum
67 ** spread. You will normally want zero spread, unless your
68 ** short range scanners are out, in which case you probably
69 ** don't know exactly where the Klingons are. In that case,
70 ** you really don't have any choice except to specify a
71 ** fairly large spread.
73 ** Phasers spread slightly, even if you specify zero spread.
78 struct cvntab Matab[] =
80 "m", "anual", (int (*)())1, 0,
81 "a", "utomatic", 0, 0,
100 double anglefactor, distfactor;
102 int manual, flag, extra;
107 struct banks bank[NBANKS];
110 if (Ship.cond == DOCKED)
111 return(printf("Phasers cannot fire through starbase shields\n"));
113 return (out(PHASER));
115 return (printf("Sulu: Captain, we cannot fire through shields.\n"));
118 printf("Sulu: Captain, surely you must realize that we cannot fire\n");
119 printf(" phasers with the cloaking device up.\n");
123 /* decide if we want manual or automatic mode */
127 if (damaged(COMPUTER))
129 printf("%s", Device[COMPUTER].name);
135 printf("%s", Device[SRSCAN].name);
139 printf(" damaged, manual mode selected\n");
144 ptr = getcodpar("Manual or automatic", Matab);
145 manual = (long) ptr->value;
147 if (!manual && damaged(COMPUTER))
149 printf("Computer damaged, manual selected\n");
154 /* initialize the bank[] array */
156 for (i = 0; i < NBANKS; i++)
160 /* collect manual mode statistics */
163 printf("%d units available\n", Ship.energy);
166 for (i = 0; i < NBANKS; i++)
169 printf("\nBank %d:\n", i);
170 hit = getintpar("units");
176 if (extra > Ship.energy)
178 printf("available energy exceeded. ");
184 hit = getintpar("course");
185 if (hit < 0 || hit > 360)
187 b->angle = hit * 0.0174532925;
188 b->spread = getfltpar("spread");
189 if (b->spread < 0 || b->spread > 1)
192 Ship.energy -= extra;
198 /* automatic distribution of power */
200 return (printf("Sulu: But there are no Klingons in this quadrant\n"));
201 printf("Phasers locked on target. ");
204 printf("%d units available\n", Ship.energy);
205 hit = getintpar("Units to fire");
208 if (hit > Ship.energy)
210 printf("available energy exceeded. ");
220 tot = n * (n + 1) / 2;
221 for (i = 0; i < n; i++)
225 distfactor = k->dist;
226 anglefactor = ALPHA * BETA * OMEGA / (distfactor * distfactor + EPSILON);
227 anglefactor *= GAMMA;
228 distfactor = k->power;
229 distfactor /= anglefactor;
230 hitreqd[i] = distfactor + 0.5;
231 dx = Ship.sectx - k->x;
232 dy = k->y - Ship.secty;
233 b->angle = atan2(dy, dx);
235 b->units = ((n - i) / tot) * extra;
239 printf("b%d hr%d u%d df%.2f af%.2f\n",
240 i, hitreqd[i], b->units,
241 distfactor, anglefactor);
245 hit = b->units - hitreqd[i];
253 /* give out any extra energy we might have around */
256 for (i = 0; i < n; i++)
259 hit = hitreqd[i] - b->units;
268 b->units = hitreqd[i];
272 printf("%d units overkill\n", extra);
280 for (i = 0; i < NBANKS; i++)
283 printf("b%d u%d", i, b->units);
285 printf(" a%.2f s%.2f\n", b->angle, b->spread);
292 /* actually fire the shots */
294 for (i = 0; i < NBANKS; i++)
301 printf("\nPhaser bank %d fires:\n", i);
304 for (j = 0; j < n; j++)
309 ** The formula for hit is as follows:
311 ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
312 ** / (dist ** 2 + EPSILON)]
313 ** * [cos(delta * sigma) + GAMMA]
316 ** where sigma is the spread factor,
317 ** rho is a random number (0 -> 1),
318 ** GAMMA is a crud factor for angle (essentially
319 ** cruds up the spread factor),
320 ** delta is the difference in radians between the
321 ** angle you are shooting at and the actual
322 ** angle of the klingon,
323 ** ALPHA scales down the significance of sigma,
324 ** BETA scales down the significance of rho,
325 ** OMEGA is the magic number which makes everything
326 ** up to "* hit" between zero and one,
327 ** dist is the distance to the klingon
328 ** hit is the number of units in the bank, and
329 ** zap is the amount of the actual hit.
331 ** Everything up through dist squared should maximize
332 ** at 1.0, so that the distance factor is never
333 ** greater than one. Conveniently, cos() is
334 ** never greater than one, but the same restric-
337 distfactor = BETA + franf();
338 distfactor *= ALPHA + b->spread;
340 anglefactor = k->dist;
341 distfactor /= anglefactor * anglefactor + EPSILON;
342 distfactor *= b->units;
343 dx = Ship.sectx - k->x;
344 dy = k->y - Ship.secty;
345 anglefactor = atan2(dy, dx) - b->angle;
346 anglefactor = cos((anglefactor * b->spread) + GAMMA);
347 if (anglefactor < 0.0)
352 hit = anglefactor * distfactor + 0.5;
354 printf("%d unit hit on Klingon", hit);
355 if (!damaged(SRSCAN))
356 printf(" at %d,%d", k->x, k->y);
368 /* compute overkill */
369 for (i = 0; i < NBANKS; i++)
370 extra += bank[i].units;
372 printf("\n%d units expended on empty space\n", extra);