1 /* @(#)computer.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: computer.c,v 1.16 2009/08/12 08:54:54 dholland Exp $ */
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 ** A computer request is fetched from the captain. The requests
45 ** chart -- print a star chart of the known galaxy. This includes
46 ** every quadrant that has ever had a long range or
47 ** a short range scan done of it, plus the location of
48 ** all starbases. This is of course updated by any sub-
49 ** space radio broadcasts (unless the radio is out).
50 ** The format is the same as that of a long range scan
51 ** except that ".1." indicates that a starbase exists
52 ** but we know nothing else.
54 ** trajectory -- gives the course and distance to every know
55 ** Klingon in the quadrant. Obviously this fails if the
56 ** short range scanners are out.
58 ** course -- gives a course computation from whereever you are
59 ** to any specified location. If the course begins
60 ** with a slash, the current quadrant is taken.
61 ** Otherwise the input is quadrant and sector coordi-
62 ** nates of the target sector.
64 ** move -- identical to course, except that the move is performed.
66 ** score -- prints out the current score.
68 ** pheff -- "PHaser EFFectiveness" at a given distance. Tells
69 ** you how much stuff you need to make it work.
71 ** warpcost -- Gives you the cost in time and units to move for
72 ** a given distance under a given warp speed.
74 ** impcost -- Same for the impulse engines.
76 ** distresslist -- Gives a list of the currently known starsystems
77 ** or starbases which are distressed, together with their
78 ** quadrant coordinates.
80 ** If a command is terminated with a semicolon, you remain in
81 ** the computer; otherwise, you escape immediately to the main
85 static struct cvntab Cputab
[] = {
86 { "ch", "art", (cmdfun
)1, 0 },
87 { "t", "rajectory", (cmdfun
)2, 0 },
88 { "c", "ourse", (cmdfun
)3, 0 },
89 { "m", "ove", (cmdfun
)3, 1 },
90 { "s", "core", (cmdfun
)4, 0 },
91 { "p", "heff", (cmdfun
)5, 0 },
92 { "w", "arpcost", (cmdfun
)6, 0 },
93 { "i", "mpcost", (cmdfun
)7, 0 },
94 { "d", "istresslist", (cmdfun
)8, 0 },
95 { NULL
, NULL
, NULL
, 0 }
98 static int kalc(int, int, int, int, double *);
99 static void prkalc(int, double);
103 computer(int v __unused
)
108 const struct cvntab
*r
;
116 if (check_out(COMPUTER
))
119 r
= getcodpar("\nRequest", Cputab
);
120 switch ((long)r
->value
) {
122 case 1: /* star chart */
123 printf("Computer record of galaxy for all long range "
126 /* print top header */
127 for (i
= 0; i
< NQUADS
; i
++)
130 for (i
= 0; i
< NQUADS
; i
++) {
132 for (j
= 0; j
< NQUADS
; j
++) {
133 if (i
== Ship
.quadx
&&
139 /* 1000 or 1001 is special case */
140 if (q
->scanned
>= 1000)
141 if (q
->scanned
> 1000)
155 /* print bottom footer */
156 for (i
= 0; i
< NQUADS
; i
++)
161 case 2: /* trajectory */
162 if (check_out(SRSCAN
)) {
165 if (Etc
.nkling
<= 0) {
166 printf("No Klingons in this quadrant\n");
169 /* for each Klingon, give the course & distance */
170 for (i
= 0; i
< Etc
.nkling
; i
++) {
171 printf("Klingon at %d,%d",
172 Etc
.klingon
[i
].x
, Etc
.klingon
[i
].y
);
173 course
= kalc(Ship
.quadx
, Ship
.quady
,
175 Etc
.klingon
[i
].y
, &dist
);
176 prkalc(course
, dist
);
180 case 3: /* course calculation */
181 if (readdelim('/')) {
185 ix
= getintpar("Quadrant");
186 if (ix
< 0 || ix
>= NSECTS
)
188 iy
= getintpar("q-y");
189 if (iy
< 0 || iy
>= NSECTS
)
194 ix
= getintpar("Sector");
195 if (ix
< 0 || ix
>= NSECTS
)
197 iy
= getintpar("s-y");
198 if (iy
< 0 || iy
>= NSECTS
)
200 course
= kalc(tqx
, tqy
, ix
, iy
, &dist
);
202 warp(-1, course
, dist
);
205 printf("%d,%d/%d,%d to %d,%d/%d,%d",
206 Ship
.quadx
, Ship
.quady
, Ship
.sectx
, Ship
.secty
,
208 prkalc(course
, dist
);
215 case 5: /* phaser effectiveness */
216 dist
= getfltpar("range");
220 cost
= pow(0.90, dist
) * 98.0 + 0.5;
221 printf("Phasers are %d%% effective at that range\n",
225 case 6: /* warp cost (time/energy) */
226 dist
= getfltpar("distance");
229 warpfact
= getfltpar("warp factor");
231 warpfact
= Ship
.warp
;
232 cost
= (dist
+ 0.05) * warpfact
* warpfact
* warpfact
;
233 time
= Param
.warptime
* dist
/ (warpfact
* warpfact
);
234 printf("Warp %.2f distance %.2f cost %.2f "
235 "stardates %d (%d w/ shlds up) units\n",
236 warpfact
, dist
, time
, cost
, cost
+ cost
);
239 case 7: /* impulse cost */
240 dist
= getfltpar("distance");
243 cost
= 20 + 100 * dist
;
245 printf("Distance %.2f cost %.2f stardates %d units\n",
249 case 8: /* distresslist */
252 /* scan the event list */
253 for (i
= 0; i
< MAXEVENTS
; i
++) {
255 /* ignore hidden entries */
256 if (e
->evcode
& E_HIDDEN
)
258 switch (e
->evcode
& E_EVENT
) {
261 printf("Klingon is attacking starbase "
262 "in quadrant %d,%d\n",
269 printf("Starsystem %s in quadrant "
270 "%d,%d is distressed\n",
271 Systemname
[e
->systemname
],
278 printf("No known distress calls are active\n");
284 * Skip to next semicolon or newline. Semicolon
285 * means get new computer request; newline means
286 * exit computer mode.
288 while ((i
= getchar()) != ';') {
301 ** Course Calculation
303 ** Computes and outputs the course and distance from position
304 ** sqx,sqy/ssx,ssy to tqx,tqy/tsx,tsy.
308 kalc(int tqx
, int tqy
, int tsx
, int tsy
, double *dist
)
315 /* normalize to quadrant distances */
317 dx
= (Ship
.quadx
+ Ship
.sectx
/ quadsize
) - (tqx
+ tsx
/ quadsize
);
318 dy
= (tqy
+ tsy
/ quadsize
) - (Ship
.quady
+ Ship
.secty
/ quadsize
);
321 angle
= atan2(dy
, dx
);
322 /* make it 0 -> 2 pi */
324 angle
+= 6.283185307;
325 /* convert from radians to degrees */
326 course
= angle
* 57.29577951 + 0.5;
327 dx
= dx
* dx
+ dy
* dy
;
333 prkalc(int course
, double dist
)
335 printf(": course %d dist %.3f\n", course
, dist
);