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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)phaser.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/trek/phaser.c,v 1.5.2.1 2000/07/20 10:35:07 kris Exp $
31 * $DragonFly: src/games/trek/phaser.c,v 1.3 2006/09/07 21:19:44 pavalos Exp $
37 /* factors for phaser hits; see description below */
39 #define ALPHA 3.0 /* spread */
40 #define BETA 3.0 /* franf() */
41 #define GAMMA 0.30 /* cos(angle) */
42 #define EPSILON 150.0 /* dist ** 2 */
43 #define OMEGA 10.596 /* overall scaling factor */
45 /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
50 ** There are up to NBANKS phaser banks which may be fired
51 ** simultaneously. There are two modes, "manual" and
52 ** "automatic". In manual mode, you specify exactly which
53 ** direction you want each bank to be aimed, the number
54 ** of units to fire, and the spread angle. In automatic
55 ** mode, you give only the total number of units to fire.
57 ** The spread is specified as a number between zero and
58 ** one, with zero being minimum spread and one being maximum
59 ** spread. You will normally want zero spread, unless your
60 ** short range scanners are out, in which case you probably
61 ** don't know exactly where the Klingons are. In that case,
62 ** you really don't have any choice except to specify a
63 ** fairly large spread.
65 ** Phasers spread slightly, even if you specify zero spread.
70 struct cvntab Matab[] = {
71 { "m", "anual", (cmdfun)1, 0 },
72 { "a", "utomatic", (cmdfun)0, 0 },
73 { NULL, NULL, NULL, 0 }
84 phaser(int v __unused)
90 double anglefactor, distfactor;
92 int manual, flag, extra = 0;
97 struct banks bank[NBANKS];
100 if (Ship.cond == DOCKED) {
101 printf("Phasers cannot fire through starbase shields\n");
104 if (damaged(PHASER)) {
109 printf("Sulu: Captain, we cannot fire through shields.\n");
113 printf("Sulu: Captain, surely you must realize that we cannot fire\n");
114 printf(" phasers with the cloaking device up.\n");
118 /* decide if we want manual or automatic mode */
121 if (damaged(COMPUTER)) {
122 printf("%s", Device[COMPUTER].name);
124 } else if (damaged(SRSCAN)) {
125 printf("%s", Device[SRSCAN].name);
129 printf(" damaged, manual mode selected\n");
133 ptr = getcodpar("Manual or automatic", Matab);
134 manual = (long) ptr->value;
136 if (!manual && damaged(COMPUTER)) {
137 printf("Computer damaged, manual selected\n");
142 /* initialize the bank[] array */
144 for (i = 0; i < NBANKS; i++)
147 /* collect manual mode statistics */
149 printf("%d units available\n", Ship.energy);
152 for (i = 0; i < NBANKS; i++) {
154 printf("\nBank %d:\n", i);
155 hit = getintpar("units");
161 if (extra > Ship.energy) {
162 printf("available energy exceeded. ");
168 hit = getintpar("course");
169 if (hit < 0 || hit > 360)
171 b->angle = hit * 0.0174532925;
172 b->spread = getfltpar("spread");
173 if (b->spread < 0 || b->spread > 1)
176 Ship.energy -= extra;
180 /* automatic distribution of power */
181 if (Etc.nkling <= 0) {
182 printf("Sulu: But there are no Klingons in this quadrant\n");
185 printf("Phasers locked on target. ");
187 printf("%d units available\n", Ship.energy);
188 hit = getintpar("Units to fire");
191 if (hit > Ship.energy) {
192 printf("available energy exceeded. ");
202 tot = n * (n + 1) / 2;
203 for (i = 0; i < n; i++) {
206 distfactor = k->dist;
207 anglefactor = ALPHA * BETA * OMEGA / (distfactor * distfactor + EPSILON);
208 anglefactor *= GAMMA;
209 distfactor = k->power;
210 distfactor /= anglefactor;
211 hitreqd[i] = distfactor + 0.5;
212 dx = Ship.sectx - k->x;
213 dy = k->y - Ship.secty;
214 b->angle = atan2(dy, dx);
216 b->units = ((n - i) / tot) * extra;
219 printf("b%d hr%d u%d df%.2f af%.2f\n",
220 i, hitreqd[i], b->units,
221 distfactor, anglefactor);
225 hit = b->units - hitreqd[i];
232 /* give out any extra energy we might have around */
234 for (i = 0; i < n; i++) {
236 hit = hitreqd[i] - b->units;
244 b->units = hitreqd[i];
248 printf("%d units overkill\n", extra);
255 for (i = 0; i < NBANKS; i++) {
257 printf("b%d u%d", i, b->units);
259 printf(" a%.2f s%.2f\n", b->angle, b->spread);
266 /* actually fire the shots */
268 for (i = 0; i < NBANKS; i++) {
273 printf("\nPhaser bank %d fires:\n", i);
276 for (j = 0; j < n; j++) {
280 ** The formula for hit is as follows:
282 ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
283 ** / (dist ** 2 + EPSILON)]
284 ** * [cos(delta * sigma) + GAMMA]
287 ** where sigma is the spread factor,
288 ** rho is a random number (0 -> 1),
289 ** GAMMA is a crud factor for angle (essentially
290 ** cruds up the spread factor),
291 ** delta is the difference in radians between the
292 ** angle you are shooting at and the actual
293 ** angle of the klingon,
294 ** ALPHA scales down the significance of sigma,
295 ** BETA scales down the significance of rho,
296 ** OMEGA is the magic number which makes everything
297 ** up to "* hit" between zero and one,
298 ** dist is the distance to the klingon
299 ** hit is the number of units in the bank, and
300 ** zap is the amount of the actual hit.
302 ** Everything up through dist squared should maximize
303 ** at 1.0, so that the distance factor is never
304 ** greater than one. Conveniently, cos() is
305 ** never greater than one, but the same restric-
308 distfactor = BETA + franf();
309 distfactor *= ALPHA + b->spread;
311 anglefactor = k->dist;
312 distfactor /= anglefactor * anglefactor + EPSILON;
313 distfactor *= b->units;
314 dx = Ship.sectx - k->x;
315 dy = k->y - Ship.secty;
316 anglefactor = atan2(dy, dx) - b->angle;
317 anglefactor = cos((anglefactor * b->spread) + GAMMA);
318 if (anglefactor < 0.0) {
322 hit = anglefactor * distfactor + 0.5;
324 printf("%d unit hit on Klingon", hit);
325 if (!damaged(SRSCAN))
326 printf(" at %d,%d", k->x, k->y);
337 /* compute overkill */
338 for (i = 0; i < NBANKS; i++)
339 extra += bank[i].units;
341 printf("\n%d units expended on empty space\n", extra);