1 /* $NetBSD: phaser.c,v 1.14 2009/05/24 22:55:03 dholland Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)phaser.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: phaser.c,v 1.14 2009/05/24 22:55:03 dholland Exp $");
46 /* factors for phaser hits; see description below */
48 #define ALPHA 3.0 /* spread */
49 #define BETA 3.0 /* franf() */
50 #define GAMMA 0.30 /* cos(angle) */
51 #define EPSILON 150.0 /* dist ** 2 */
52 #define OMEGA 10.596 /* overall scaling factor */
54 /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
59 ** There are up to NBANKS phaser banks which may be fired
60 ** simultaneously. There are two modes, "manual" and
61 ** "automatic". In manual mode, you specify exactly which
62 ** direction you want each bank to be aimed, the number
63 ** of units to fire, and the spread angle. In automatic
64 ** mode, you give only the total number of units to fire.
66 ** The spread is specified as a number between zero and
67 ** one, with zero being minimum spread and one being maximum
68 ** spread. You will normally want zero spread, unless your
69 ** short range scanners are out, in which case you probably
70 ** don't know exactly where the Klingons are. In that case,
71 ** you really don't have any choice except to specify a
72 ** fairly large spread.
74 ** Phasers spread slightly, even if you specify zero spread.
79 static struct cvntab Matab
[] = {
80 { "m", "anual", (cmdfun
) 1, 0 },
81 { "a", "utomatic", (cmdfun
) 0, 0 },
82 { NULL
, NULL
, NULL
, 0 }
95 phaser(int v __unused
)
101 double anglefactor
, distfactor
;
103 int manual
, flag
, extra
= 0;
108 struct banks bank
[NBANKS
];
109 const struct cvntab
*ptr
;
111 if (Ship
.cond
== DOCKED
) {
112 printf("Phasers cannot fire through starbase shields\n");
115 if (damaged(PHASER
)) {
120 printf("Sulu: Captain, we cannot fire through shields.\n");
124 printf("Sulu: Captain, surely you must realize that we cannot "
126 printf(" phasers with the cloaking device up.\n");
130 /* decide if we want manual or automatic mode */
133 if (damaged(COMPUTER
)) {
134 printf("%s", Device
[COMPUTER
].name
);
136 } else if (damaged(SRSCAN
)) {
137 printf("%s", Device
[SRSCAN
].name
);
141 printf(" damaged, manual mode selected\n");
145 ptr
= getcodpar("Manual or automatic", Matab
);
146 manual
= (long) ptr
->value
;
148 if (!manual
&& damaged(COMPUTER
)) {
149 printf("Computer damaged, manual selected\n");
154 /* initialize the bank[] array */
156 for (i
= 0; i
< NBANKS
; i
++)
159 /* collect manual mode statistics */
161 printf("%d units available\n", Ship
.energy
);
164 for (i
= 0; i
< NBANKS
; i
++) {
166 printf("\nBank %d:\n", i
);
167 hit
= getintpar("units");
173 if (extra
> Ship
.energy
) {
174 printf("available energy exceeded. ");
180 hit
= getintpar("course");
181 if (hit
< 0 || hit
> 360)
183 b
->angle
= hit
* 0.0174532925;
184 b
->spread
= getfltpar("spread");
185 if (b
->spread
< 0 || b
->spread
> 1)
188 Ship
.energy
-= extra
;
192 /* automatic distribution of power */
193 if (Etc
.nkling
<= 0) {
194 printf("Sulu: But there are no Klingons in this "
198 printf("Phasers locked on target. ");
200 printf("%d units available\n", Ship
.energy
);
201 hit
= getintpar("Units to fire");
204 if (hit
> Ship
.energy
) {
205 printf("available energy exceeded. ");
215 tot
= n
* (n
+ 1) / 2;
216 for (i
= 0; i
< n
; i
++) {
219 distfactor
= k
->dist
;
220 anglefactor
= ALPHA
* BETA
* OMEGA
/
221 (distfactor
* distfactor
+ EPSILON
);
222 anglefactor
*= GAMMA
;
223 distfactor
= k
->power
;
224 distfactor
/= anglefactor
;
225 hitreqd
[i
] = distfactor
+ 0.5;
226 dx
= Ship
.sectx
- k
->x
;
227 dy
= k
->y
- Ship
.secty
;
228 b
->angle
= atan2(dy
, dx
);
230 b
->units
= ((n
- i
) / tot
) * extra
;
233 printf("b%d hr%d u%d df%.2f af%.2f\n",
234 i
, hitreqd
[i
], b
->units
,
235 distfactor
, anglefactor
);
239 hit
= b
->units
- hitreqd
[i
];
246 /* give out any extra energy we might have around */
248 for (i
= 0; i
< n
; i
++) {
250 hit
= hitreqd
[i
] - b
->units
;
258 b
->units
= hitreqd
[i
];
262 printf("%d units overkill\n", extra
);
269 for (i
= 0; i
< NBANKS
; i
++) {
271 printf("b%d u%d", i
, b
->units
);
273 printf(" a%.2f s%.2f\n", b
->angle
, b
->spread
);
280 /* actually fire the shots */
282 for (i
= 0; i
< NBANKS
; i
++) {
287 printf("\nPhaser bank %d fires:\n", i
);
290 for (j
= 0; j
< n
; j
++) {
294 ** The formula for hit is as follows:
296 ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
297 ** / (dist ** 2 + EPSILON)]
298 ** * [cos(delta * sigma) + GAMMA]
301 ** where sigma is the spread factor,
302 ** rho is a random number (0 -> 1),
303 ** GAMMA is a crud factor for angle (essentially
304 ** cruds up the spread factor),
305 ** delta is the difference in radians between the
306 ** angle you are shooting at and the actual
307 ** angle of the klingon,
308 ** ALPHA scales down the significance of sigma,
309 ** BETA scales down the significance of rho,
310 ** OMEGA is the magic number which makes everything
311 ** up to "* hit" between zero and one,
312 ** dist is the distance to the klingon
313 ** hit is the number of units in the bank, and
314 ** zap is the amount of the actual hit.
316 ** Everything up through dist squared should maximize
317 ** at 1.0, so that the distance factor is never
318 ** greater than one. Conveniently, cos() is
319 ** never greater than one, but the same restric-
322 distfactor
= BETA
+ franf();
323 distfactor
*= ALPHA
+ b
->spread
;
325 anglefactor
= k
->dist
;
326 distfactor
/= anglefactor
* anglefactor
+ EPSILON
;
327 distfactor
*= b
->units
;
328 dx
= Ship
.sectx
- k
->x
;
329 dy
= k
->y
- Ship
.secty
;
330 anglefactor
= atan2(dy
, dx
) - b
->angle
;
331 anglefactor
= cos((anglefactor
* b
->spread
) + GAMMA
);
332 if (anglefactor
< 0.0) {
336 hit
= anglefactor
* distfactor
+ 0.5;
338 printf("%d unit hit on Klingon", hit
);
339 if (!damaged(SRSCAN
))
340 printf(" at %d,%d", k
->x
, k
->y
);
351 /* compute overkill */
352 for (i
= 0; i
< NBANKS
; i
++)
353 extra
+= bank
[i
].units
;
355 printf("\n%d units expended on empty space\n", extra
);