]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/phaser.c
1 /* $NetBSD: phaser.c,v 1.10 2007/12/15 19:44:44 perry 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.10 2007/12/15 19:44:44 perry 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 struct cvntab Matab
[] =
81 { "m", "anual", (cmdfun
) 1, 0 },
82 { "a", "utomatic", (cmdfun
) 0, 0 },
83 { NULL
, NULL
, NULL
, 0 }
104 double anglefactor
, distfactor
;
106 int manual
, flag
, extra
= 0;
111 struct banks bank
[NBANKS
];
112 const struct cvntab
*ptr
;
114 if (Ship
.cond
== DOCKED
) {
115 printf("Phasers cannot fire through starbase shields\n");
118 if (damaged(PHASER
)) {
123 printf("Sulu: Captain, we cannot fire through shields.\n");
128 printf("Sulu: Captain, surely you must realize that we cannot fire\n");
129 printf(" phasers with the cloaking device up.\n");
133 /* decide if we want manual or automatic mode */
137 if (damaged(COMPUTER
))
139 printf("%s", Device
[COMPUTER
].name
);
145 printf("%s", Device
[SRSCAN
].name
);
149 printf(" damaged, manual mode selected\n");
154 ptr
= getcodpar("Manual or automatic", Matab
);
155 manual
= (long) ptr
->value
;
157 if (!manual
&& damaged(COMPUTER
))
159 printf("Computer damaged, manual selected\n");
164 /* initialize the bank[] array */
166 for (i
= 0; i
< NBANKS
; i
++)
170 /* collect manual mode statistics */
173 printf("%d units available\n", Ship
.energy
);
176 for (i
= 0; i
< NBANKS
; i
++)
179 printf("\nBank %d:\n", i
);
180 hit
= getintpar("units");
186 if (extra
> Ship
.energy
)
188 printf("available energy exceeded. ");
194 hit
= getintpar("course");
195 if (hit
< 0 || hit
> 360)
197 b
->angle
= hit
* 0.0174532925;
198 b
->spread
= getfltpar("spread");
199 if (b
->spread
< 0 || b
->spread
> 1)
202 Ship
.energy
-= extra
;
208 /* automatic distribution of power */
209 if (Etc
.nkling
<= 0) {
210 printf("Sulu: But there are no Klingons in this quadrant\n");
213 printf("Phasers locked on target. ");
216 printf("%d units available\n", Ship
.energy
);
217 hit
= getintpar("Units to fire");
220 if (hit
> Ship
.energy
)
222 printf("available energy exceeded. ");
232 tot
= n
* (n
+ 1) / 2;
233 for (i
= 0; i
< n
; i
++)
237 distfactor
= k
->dist
;
238 anglefactor
= ALPHA
* BETA
* OMEGA
/ (distfactor
* distfactor
+ EPSILON
);
239 anglefactor
*= GAMMA
;
240 distfactor
= k
->power
;
241 distfactor
/= anglefactor
;
242 hitreqd
[i
] = distfactor
+ 0.5;
243 dx
= Ship
.sectx
- k
->x
;
244 dy
= k
->y
- Ship
.secty
;
245 b
->angle
= atan2(dy
, dx
);
247 b
->units
= ((n
- i
) / tot
) * extra
;
251 printf("b%d hr%d u%d df%.2f af%.2f\n",
252 i
, hitreqd
[i
], b
->units
,
253 distfactor
, anglefactor
);
257 hit
= b
->units
- hitreqd
[i
];
265 /* give out any extra energy we might have around */
268 for (i
= 0; i
< n
; i
++)
271 hit
= hitreqd
[i
] - b
->units
;
280 b
->units
= hitreqd
[i
];
284 printf("%d units overkill\n", extra
);
292 for (i
= 0; i
< NBANKS
; i
++)
295 printf("b%d u%d", i
, b
->units
);
297 printf(" a%.2f s%.2f\n", b
->angle
, b
->spread
);
304 /* actually fire the shots */
306 for (i
= 0; i
< NBANKS
; i
++)
313 printf("\nPhaser bank %d fires:\n", i
);
316 for (j
= 0; j
< n
; j
++)
321 ** The formula for hit is as follows:
323 ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
324 ** / (dist ** 2 + EPSILON)]
325 ** * [cos(delta * sigma) + GAMMA]
328 ** where sigma is the spread factor,
329 ** rho is a random number (0 -> 1),
330 ** GAMMA is a crud factor for angle (essentially
331 ** cruds up the spread factor),
332 ** delta is the difference in radians between the
333 ** angle you are shooting at and the actual
334 ** angle of the klingon,
335 ** ALPHA scales down the significance of sigma,
336 ** BETA scales down the significance of rho,
337 ** OMEGA is the magic number which makes everything
338 ** up to "* hit" between zero and one,
339 ** dist is the distance to the klingon
340 ** hit is the number of units in the bank, and
341 ** zap is the amount of the actual hit.
343 ** Everything up through dist squared should maximize
344 ** at 1.0, so that the distance factor is never
345 ** greater than one. Conveniently, cos() is
346 ** never greater than one, but the same restric-
349 distfactor
= BETA
+ franf();
350 distfactor
*= ALPHA
+ b
->spread
;
352 anglefactor
= k
->dist
;
353 distfactor
/= anglefactor
* anglefactor
+ EPSILON
;
354 distfactor
*= b
->units
;
355 dx
= Ship
.sectx
- k
->x
;
356 dy
= k
->y
- Ship
.secty
;
357 anglefactor
= atan2(dy
, dx
) - b
->angle
;
358 anglefactor
= cos((anglefactor
* b
->spread
) + GAMMA
);
359 if (anglefactor
< 0.0)
364 hit
= anglefactor
* distfactor
+ 0.5;
366 printf("%d unit hit on Klingon", hit
);
367 if (!damaged(SRSCAN
))
368 printf(" at %d,%d", k
->x
, k
->y
);
380 /* compute overkill */
381 for (i
= 0; i
< NBANKS
; i
++)
382 extra
+= bank
[i
].units
;
384 printf("\n%d units expended on empty space\n", extra
);