]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/phaser.c
1 /* $NetBSD: phaser.c,v 1.5 1997/10/12 21:25:06 christos 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)phaser.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: phaser.c,v 1.5 1997/10/12 21:25:06 christos Exp $");
50 /* factors for phaser hits; see description below */
52 # define ALPHA 3.0 /* spread */
53 # define BETA 3.0 /* franf() */
54 # define GAMMA 0.30 /* cos(angle) */
55 # define EPSILON 150.0 /* dist ** 2 */
56 # define OMEGA 10.596 /* overall scaling factor */
58 /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
63 ** There are up to NBANKS phaser banks which may be fired
64 ** simultaneously. There are two modes, "manual" and
65 ** "automatic". In manual mode, you specify exactly which
66 ** direction you want each bank to be aimed, the number
67 ** of units to fire, and the spread angle. In automatic
68 ** mode, you give only the total number of units to fire.
70 ** The spread is specified as a number between zero and
71 ** one, with zero being minimum spread and one being maximum
72 ** spread. You will normally want zero spread, unless your
73 ** short range scanners are out, in which case you probably
74 ** don't know exactly where the Klingons are. In that case,
75 ** you really don't have any choice except to specify a
76 ** fairly large spread.
78 ** Phasers spread slightly, even if you specify zero spread.
83 struct cvntab Matab
[] =
85 { "m", "anual", (cmdfun
) 1, 0 },
86 { "a", "utomatic", (cmdfun
) 0, 0 },
87 { NULL
, NULL
, NULL
, 0 }
108 double anglefactor
, distfactor
;
110 int manual
, flag
, extra
= 0;
115 struct banks bank
[NBANKS
];
118 if (Ship
.cond
== DOCKED
) {
119 printf("Phasers cannot fire through starbase shields\n");
122 if (damaged(PHASER
)) {
127 printf("Sulu: Captain, we cannot fire through shields.\n");
132 printf("Sulu: Captain, surely you must realize that we cannot fire\n");
133 printf(" phasers with the cloaking device up.\n");
137 /* decide if we want manual or automatic mode */
141 if (damaged(COMPUTER
))
143 printf(Device
[COMPUTER
].name
);
149 printf(Device
[SRSCAN
].name
);
153 printf(" damaged, manual mode selected\n");
158 ptr
= getcodpar("Manual or automatic", Matab
);
159 manual
= (long) ptr
->value
;
161 if (!manual
&& damaged(COMPUTER
))
163 printf("Computer damaged, manual selected\n");
168 /* initialize the bank[] array */
170 for (i
= 0; i
< NBANKS
; i
++)
174 /* collect manual mode statistics */
177 printf("%d units available\n", Ship
.energy
);
180 for (i
= 0; i
< NBANKS
; i
++)
183 printf("\nBank %d:\n", i
);
184 hit
= getintpar("units");
190 if (extra
> Ship
.energy
)
192 printf("available energy exceeded. ");
198 hit
= getintpar("course");
199 if (hit
< 0 || hit
> 360)
201 b
->angle
= hit
* 0.0174532925;
202 b
->spread
= getfltpar("spread");
203 if (b
->spread
< 0 || b
->spread
> 1)
206 Ship
.energy
-= extra
;
212 /* automatic distribution of power */
213 if (Etc
.nkling
<= 0) {
214 printf("Sulu: But there are no Klingons in this quadrant\n");
217 printf("Phasers locked on target. ");
220 printf("%d units available\n", Ship
.energy
);
221 hit
= getintpar("Units to fire");
224 if (hit
> Ship
.energy
)
226 printf("available energy exceeded. ");
236 tot
= n
* (n
+ 1) / 2;
237 for (i
= 0; i
< n
; i
++)
241 distfactor
= k
->dist
;
242 anglefactor
= ALPHA
* BETA
* OMEGA
/ (distfactor
* distfactor
+ EPSILON
);
243 anglefactor
*= GAMMA
;
244 distfactor
= k
->power
;
245 distfactor
/= anglefactor
;
246 hitreqd
[i
] = distfactor
+ 0.5;
247 dx
= Ship
.sectx
- k
->x
;
248 dy
= k
->y
- Ship
.secty
;
249 b
->angle
= atan2(dy
, dx
);
251 b
->units
= ((n
- i
) / tot
) * extra
;
255 printf("b%d hr%d u%d df%.2f af%.2f\n",
256 i
, hitreqd
[i
], b
->units
,
257 distfactor
, anglefactor
);
261 hit
= b
->units
- hitreqd
[i
];
269 /* give out any extra energy we might have around */
272 for (i
= 0; i
< n
; i
++)
275 hit
= hitreqd
[i
] - b
->units
;
284 b
->units
= hitreqd
[i
];
288 printf("%d units overkill\n", extra
);
296 for (i
= 0; i
< NBANKS
; i
++)
299 printf("b%d u%d", i
, b
->units
);
301 printf(" a%.2f s%.2f\n", b
->angle
, b
->spread
);
308 /* actually fire the shots */
310 for (i
= 0; i
< NBANKS
; i
++)
317 printf("\nPhaser bank %d fires:\n", i
);
320 for (j
= 0; j
< n
; j
++)
325 ** The formula for hit is as follows:
327 ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
328 ** / (dist ** 2 + EPSILON)]
329 ** * [cos(delta * sigma) + GAMMA]
332 ** where sigma is the spread factor,
333 ** rho is a random number (0 -> 1),
334 ** GAMMA is a crud factor for angle (essentially
335 ** cruds up the spread factor),
336 ** delta is the difference in radians between the
337 ** angle you are shooting at and the actual
338 ** angle of the klingon,
339 ** ALPHA scales down the significance of sigma,
340 ** BETA scales down the significance of rho,
341 ** OMEGA is the magic number which makes everything
342 ** up to "* hit" between zero and one,
343 ** dist is the distance to the klingon
344 ** hit is the number of units in the bank, and
345 ** zap is the amount of the actual hit.
347 ** Everything up through dist squared should maximize
348 ** at 1.0, so that the distance factor is never
349 ** greater than one. Conveniently, cos() is
350 ** never greater than one, but the same restric-
353 distfactor
= BETA
+ franf();
354 distfactor
*= ALPHA
+ b
->spread
;
356 anglefactor
= k
->dist
;
357 distfactor
/= anglefactor
* anglefactor
+ EPSILON
;
358 distfactor
*= b
->units
;
359 dx
= Ship
.sectx
- k
->x
;
360 dy
= k
->y
- Ship
.secty
;
361 anglefactor
= atan2(dy
, dx
) - b
->angle
;
362 anglefactor
= cos((anglefactor
* b
->spread
) + GAMMA
);
363 if (anglefactor
< 0.0)
368 hit
= anglefactor
* distfactor
+ 0.5;
370 printf("%d unit hit on Klingon", hit
);
371 if (!damaged(SRSCAN
))
372 printf(" at %d,%d", k
->x
, k
->y
);
384 /* compute overkill */
385 for (i
= 0; i
< NBANKS
; i
++)
386 extra
+= bank
[i
].units
;
388 printf("\n%d units expended on empty space\n", extra
);