1 /* @(#)phaser.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: phaser.c,v 1.15 2009/08/12 08:54:54 dholland Exp $ */
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 /* factors for phaser hits; see description below */
40 #define ALPHA 3.0 /* spread */
41 #define BETA 3.0 /* franf() */
42 #define GAMMA 0.30 /* cos(angle) */
43 #define EPSILON 150.0 /* dist ** 2 */
44 #define OMEGA 10.596 /* overall scaling factor */
46 /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
51 ** There are up to NBANKS phaser banks which may be fired
52 ** simultaneously. There are two modes, "manual" and
53 ** "automatic". In manual mode, you specify exactly which
54 ** direction you want each bank to be aimed, the number
55 ** of units to fire, and the spread angle. In automatic
56 ** mode, you give only the total number of units to fire.
58 ** The spread is specified as a number between zero and
59 ** one, with zero being minimum spread and one being maximum
60 ** spread. You will normally want zero spread, unless your
61 ** short range scanners are out, in which case you probably
62 ** don't know exactly where the Klingons are. In that case,
63 ** you really don't have any choice except to specify a
64 ** fairly large spread.
66 ** Phasers spread slightly, even if you specify zero spread.
71 static struct cvntab Matab[] = {
72 { "m", "anual", (cmdfun) 1, 0 },
73 { "a", "utomatic", (cmdfun) 0, 0 },
74 { NULL, NULL, NULL, 0 }
87 phaser(int v __unused)
93 double anglefactor, distfactor;
95 int manual, flag, extra = 0;
100 struct banks bank[NBANKS];
101 const struct cvntab *ptr;
103 if (Ship.cond == DOCKED) {
104 printf("Phasers cannot fire through starbase shields\n");
107 if (damaged(PHASER)) {
112 printf("Sulu: Captain, we cannot fire through shields.\n");
116 printf("Sulu: Captain, surely you must realize that we cannot "
118 printf(" phasers with the cloaking device up.\n");
122 /* decide if we want manual or automatic mode */
125 if (damaged(COMPUTER)) {
126 printf("%s", Device[COMPUTER].name);
128 } else if (damaged(SRSCAN)) {
129 printf("%s", Device[SRSCAN].name);
133 printf(" damaged, manual mode selected\n");
137 ptr = getcodpar("Manual or automatic", Matab);
138 manual = (long) ptr->value;
140 if (!manual && damaged(COMPUTER)) {
141 printf("Computer damaged, manual selected\n");
146 /* initialize the bank[] array */
148 for (i = 0; i < NBANKS; i++)
151 /* collect manual mode statistics */
153 printf("%d units available\n", Ship.energy);
156 for (i = 0; i < NBANKS; i++) {
158 printf("\nBank %d:\n", i);
159 hit = getintpar("units");
165 if (extra > Ship.energy) {
166 printf("available energy exceeded. ");
172 hit = getintpar("course");
173 if (hit < 0 || hit > 360)
175 b->angle = hit * 0.0174532925;
176 b->spread = getfltpar("spread");
177 if (b->spread < 0 || b->spread > 1)
180 Ship.energy -= extra;
184 /* automatic distribution of power */
185 if (Etc.nkling <= 0) {
186 printf("Sulu: But there are no Klingons in this "
190 printf("Phasers locked on target. ");
192 printf("%d units available\n", Ship.energy);
193 hit = getintpar("Units to fire");
196 if (hit > Ship.energy) {
197 printf("available energy exceeded. ");
207 tot = n * (n + 1) / 2;
208 for (i = 0; i < n; i++) {
211 distfactor = k->dist;
212 anglefactor = ALPHA * BETA * OMEGA /
213 (distfactor * distfactor + EPSILON);
214 anglefactor *= GAMMA;
215 distfactor = k->power;
216 distfactor /= anglefactor;
217 hitreqd[i] = distfactor + 0.5;
218 dx = Ship.sectx - k->x;
219 dy = k->y - Ship.secty;
220 b->angle = atan2(dy, dx);
222 b->units = ((n - i) / tot) * extra;
225 printf("b%d hr%d u%d df%.2f af%.2f\n",
226 i, hitreqd[i], b->units,
227 distfactor, anglefactor);
231 hit = b->units - hitreqd[i];
238 /* give out any extra energy we might have around */
240 for (i = 0; i < n; i++) {
242 hit = hitreqd[i] - b->units;
250 b->units = hitreqd[i];
254 printf("%d units overkill\n", extra);
261 for (i = 0; i < NBANKS; i++) {
263 printf("b%d u%d", i, b->units);
265 printf(" a%.2f s%.2f\n", b->angle, b->spread);
272 /* actually fire the shots */
274 for (i = 0; i < NBANKS; i++) {
279 printf("\nPhaser bank %d fires:\n", i);
282 for (j = 0; j < n; j++) {
286 ** The formula for hit is as follows:
288 ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
289 ** / (dist ** 2 + EPSILON)]
290 ** * [cos(delta * sigma) + GAMMA]
293 ** where sigma is the spread factor,
294 ** rho is a random number (0 -> 1),
295 ** GAMMA is a crud factor for angle (essentially
296 ** cruds up the spread factor),
297 ** delta is the difference in radians between the
298 ** angle you are shooting at and the actual
299 ** angle of the klingon,
300 ** ALPHA scales down the significance of sigma,
301 ** BETA scales down the significance of rho,
302 ** OMEGA is the magic number which makes everything
303 ** up to "* hit" between zero and one,
304 ** dist is the distance to the klingon
305 ** hit is the number of units in the bank, and
306 ** zap is the amount of the actual hit.
308 ** Everything up through dist squared should maximize
309 ** at 1.0, so that the distance factor is never
310 ** greater than one. Conveniently, cos() is
311 ** never greater than one, but the same restric-
314 distfactor = BETA + franf();
315 distfactor *= ALPHA + b->spread;
317 anglefactor = k->dist;
318 distfactor /= anglefactor * anglefactor + EPSILON;
319 distfactor *= b->units;
320 dx = Ship.sectx - k->x;
321 dy = k->y - Ship.secty;
322 anglefactor = atan2(dy, dx) - b->angle;
323 anglefactor = cos((anglefactor * b->spread) + GAMMA);
324 if (anglefactor < 0.0) {
328 hit = anglefactor * distfactor + 0.5;
330 printf("%d unit hit on Klingon", hit);
331 if (!damaged(SRSCAN))
332 printf(" at %d,%d", k->x, k->y);
343 /* compute overkill */
344 for (i = 0; i < NBANKS; i++)
345 extra += bank[i].units;
347 printf("\n%d units expended on empty space\n", extra);