]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/phaser.c
1 /* $NetBSD: phaser.c,v 1.3 1995/04/22 10:59:17 cgd 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
38 static char sccsid
[] = "@(#)phaser.c 8.1 (Berkeley) 5/31/93";
40 static char rcsid
[] = "$NetBSD: phaser.c,v 1.3 1995/04/22 10:59:17 cgd Exp $";
47 /* factors for phaser hits; see description below */
49 # define ALPHA 3.0 /* spread */
50 # define BETA 3.0 /* franf() */
51 # define GAMMA 0.30 /* cos(angle) */
52 # define EPSILON 150.0 /* dist ** 2 */
53 # define OMEGA 10.596 /* overall scaling factor */
55 /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
60 ** There are up to NBANKS phaser banks which may be fired
61 ** simultaneously. There are two modes, "manual" and
62 ** "automatic". In manual mode, you specify exactly which
63 ** direction you want each bank to be aimed, the number
64 ** of units to fire, and the spread angle. In automatic
65 ** mode, you give only the total number of units to fire.
67 ** The spread is specified as a number between zero and
68 ** one, with zero being minimum spread and one being maximum
69 ** spread. You will normally want zero spread, unless your
70 ** short range scanners are out, in which case you probably
71 ** don't know exactly where the Klingons are. In that case,
72 ** you really don't have any choice except to specify a
73 ** fairly large spread.
75 ** Phasers spread slightly, even if you specify zero spread.
80 struct cvntab Matab
[] =
82 "m", "anual", (int (*)())1, 0,
83 "a", "utomatic", 0, 0,
100 register struct kling
*k
;
102 double anglefactor
, distfactor
;
103 register struct banks
*b
;
104 int manual
, flag
, extra
;
109 struct banks bank
[NBANKS
];
112 if (Ship
.cond
== DOCKED
)
113 return(printf("Phasers cannot fire through starbase shields\n"));
115 return (out(PHASER
));
117 return (printf("Sulu: Captain, we cannot fire through shields.\n"));
120 printf("Sulu: Captain, surely you must realize that we cannot fire\n");
121 printf(" phasers with the cloaking device up.\n");
125 /* decide if we want manual or automatic mode */
129 if (damaged(COMPUTER
))
131 printf(Device
[COMPUTER
].name
);
137 printf(Device
[SRSCAN
].name
);
141 printf(" damaged, manual mode selected\n");
146 ptr
= getcodpar("Manual or automatic", Matab
);
147 manual
= (int) ptr
->value
;
149 if (!manual
&& damaged(COMPUTER
))
151 printf("Computer damaged, manual selected\n");
156 /* initialize the bank[] array */
158 for (i
= 0; i
< NBANKS
; i
++)
162 /* collect manual mode statistics */
165 printf("%d units available\n", Ship
.energy
);
168 for (i
= 0; i
< NBANKS
; i
++)
171 printf("\nBank %d:\n", i
);
172 hit
= getintpar("units");
178 if (extra
> Ship
.energy
)
180 printf("available energy exceeded. ");
186 hit
= getintpar("course");
187 if (hit
< 0 || hit
> 360)
189 b
->angle
= hit
* 0.0174532925;
190 b
->spread
= getfltpar("spread");
191 if (b
->spread
< 0 || b
->spread
> 1)
194 Ship
.energy
-= extra
;
200 /* automatic distribution of power */
202 return (printf("Sulu: But there are no Klingons in this quadrant\n"));
203 printf("Phasers locked on target. ");
206 printf("%d units available\n", Ship
.energy
);
207 hit
= getintpar("Units to fire");
210 if (hit
> Ship
.energy
)
212 printf("available energy exceeded. ");
222 tot
= n
* (n
+ 1) / 2;
223 for (i
= 0; i
< n
; i
++)
227 distfactor
= k
->dist
;
228 anglefactor
= ALPHA
* BETA
* OMEGA
/ (distfactor
* distfactor
+ EPSILON
);
229 anglefactor
*= GAMMA
;
230 distfactor
= k
->power
;
231 distfactor
/= anglefactor
;
232 hitreqd
[i
] = distfactor
+ 0.5;
233 dx
= Ship
.sectx
- k
->x
;
234 dy
= k
->y
- Ship
.secty
;
235 b
->angle
= atan2(dy
, dx
);
237 b
->units
= ((n
- i
) / tot
) * extra
;
241 printf("b%d hr%d u%d df%.2f af%.2f\n",
242 i
, hitreqd
[i
], b
->units
,
243 distfactor
, anglefactor
);
247 hit
= b
->units
- hitreqd
[i
];
255 /* give out any extra energy we might have around */
258 for (i
= 0; i
< n
; i
++)
261 hit
= hitreqd
[i
] - b
->units
;
270 b
->units
= hitreqd
[i
];
274 printf("%d units overkill\n", extra
);
282 for (i
= 0; i
< NBANKS
; i
++)
285 printf("b%d u%d", i
, b
->units
);
287 printf(" a%.2f s%.2f\n", b
->angle
, b
->spread
);
294 /* actually fire the shots */
296 for (i
= 0; i
< NBANKS
; i
++)
303 printf("\nPhaser bank %d fires:\n", i
);
306 for (j
= 0; j
< n
; j
++)
311 ** The formula for hit is as follows:
313 ** zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
314 ** / (dist ** 2 + EPSILON)]
315 ** * [cos(delta * sigma) + GAMMA]
318 ** where sigma is the spread factor,
319 ** rho is a random number (0 -> 1),
320 ** GAMMA is a crud factor for angle (essentially
321 ** cruds up the spread factor),
322 ** delta is the difference in radians between the
323 ** angle you are shooting at and the actual
324 ** angle of the klingon,
325 ** ALPHA scales down the significance of sigma,
326 ** BETA scales down the significance of rho,
327 ** OMEGA is the magic number which makes everything
328 ** up to "* hit" between zero and one,
329 ** dist is the distance to the klingon
330 ** hit is the number of units in the bank, and
331 ** zap is the amount of the actual hit.
333 ** Everything up through dist squared should maximize
334 ** at 1.0, so that the distance factor is never
335 ** greater than one. Conveniently, cos() is
336 ** never greater than one, but the same restric-
339 distfactor
= BETA
+ franf();
340 distfactor
*= ALPHA
+ b
->spread
;
342 anglefactor
= k
->dist
;
343 distfactor
/= anglefactor
* anglefactor
+ EPSILON
;
344 distfactor
*= b
->units
;
345 dx
= Ship
.sectx
- k
->x
;
346 dy
= k
->y
- Ship
.secty
;
347 anglefactor
= atan2(dy
, dx
) - b
->angle
;
348 anglefactor
= cos((anglefactor
* b
->spread
) + GAMMA
);
349 if (anglefactor
< 0.0)
354 hit
= anglefactor
* distfactor
+ 0.5;
356 printf("%d unit hit on Klingon", hit
);
357 if (!damaged(SRSCAN
))
358 printf(" at %d,%d", k
->x
, k
->y
);
370 /* compute overkill */
371 for (i
= 0; i
< NBANKS
; i
++)
372 extra
+= bank
[i
].units
;
374 printf("\n%d units expended on empty space\n", extra
);