2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)computer.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/trek/computer.c,v 1.5 1999/11/30 03:49:45 billf Exp $
35 * $DragonFly: src/games/trek/computer.c,v 1.3 2006/09/07 21:19:44 pavalos Exp $
41 static int kalc(int, int, int, int, double *);
42 static void prkalc(int, double);
47 ** A computer request is fetched from the captain. The requests
50 ** chart -- print a star chart of the known galaxy. This includes
51 ** every quadrant that has ever had a long range or
52 ** a short range scan done of it, plus the location of
53 ** all starbases. This is of course updated by any sub-
54 ** space radio broadcasts (unless the radio is out).
55 ** The format is the same as that of a long range scan
56 ** except that ".1." indicates that a starbase exists
57 ** but we know nothing else.
59 ** trajectory -- gives the course and distance to every know
60 ** Klingon in the quadrant. Obviously this fails if the
61 ** short range scanners are out.
63 ** course -- gives a course computation from whereever you are
64 ** to any specified location. If the course begins
65 ** with a slash, the current quadrant is taken.
66 ** Otherwise the input is quadrant and sector coordi-
67 ** nates of the target sector.
69 ** move -- identical to course, except that the move is performed.
71 ** score -- prints out the current score.
73 ** pheff -- "PHaser EFFectiveness" at a given distance. Tells
74 ** you how much stuff you need to make it work.
76 ** warpcost -- Gives you the cost in time and units to move for
77 ** a given distance under a given warp speed.
79 ** impcost -- Same for the impulse engines.
81 ** distresslist -- Gives a list of the currently known starsystems
82 ** or starbases which are distressed, together with their
83 ** quadrant coordinates.
85 ** If a command is terminated with a semicolon, you remain in
86 ** the computer; otherwise, you escape immediately to the main
90 struct cvntab Cputab
[] =
92 { "ch", "art", (void (*)(int))1, 0 },
93 { "t", "rajectory", (void (*)(int))2, 0 },
94 { "c", "ourse", (void (*)(int))3, 0 },
95 { "m", "ove", (void (*)(int))3, 1 },
96 { "s", "core", (void (*)(int))4, 0 },
97 { "p", "heff", (void (*)(int))5, 0 },
98 { "w", "arpcost", (void (*)(int))6, 0 },
99 { "i", "mpcost", (void (*)(int))7, 0 },
100 { "d", "istresslist", (void (*)(int))8, 0 },
101 { NULL
, NULL
, NULL
, 0 }
105 computer(__unused
int unused
)
118 if (check_out(COMPUTER
))
122 r
= getcodpar("\nRequest", Cputab
);
123 switch ((long)r
->value
)
126 case 1: /* star chart */
127 printf("Computer record of galaxy for all long range sensor scans\n\n");
129 /* print top header */
130 for (i
= 0; i
< NQUADS
; i
++)
133 for (i
= 0; i
< NQUADS
; i
++)
136 for (j
= 0; j
< NQUADS
; j
++)
138 if (i
== Ship
.quadx
&& j
== Ship
.quady
)
144 /* 1000 or 1001 is special case */
145 if (q
->scanned
>= 1000)
146 if (q
->scanned
> 1000)
154 printf("%3d ", q
->scanned
);
159 /* print bottom footer */
160 for (i
= 0; i
< NQUADS
; i
++)
165 case 2: /* trajectory */
166 if (check_out(SRSCAN
))
172 printf("No Klingons in this quadrant\n");
175 /* for each Klingon, give the course & distance */
176 for (i
= 0; i
< Etc
.nkling
; i
++)
178 printf("Klingon at %d,%d", Etc
.klingon
[i
].x
, Etc
.klingon
[i
].y
);
179 course
= kalc(Ship
.quadx
, Ship
.quady
, Etc
.klingon
[i
].x
, Etc
.klingon
[i
].y
, &dist
);
180 prkalc(course
, dist
);
184 case 3: /* course calculation */
192 ix
= getintpar("Quadrant");
193 if (ix
< 0 || ix
>= NSECTS
)
195 iy
= getintpar("q-y");
196 if (iy
< 0 || iy
>= NSECTS
)
201 ix
= getintpar("Sector");
202 if (ix
< 0 || ix
>= NSECTS
)
204 iy
= getintpar("s-y");
205 if (iy
< 0 || iy
>= NSECTS
)
207 course
= kalc(tqx
, tqy
, ix
, iy
, &dist
);
210 warp(-1, course
, dist
);
213 printf("%d,%d/%d,%d to %d,%d/%d,%d",
214 Ship
.quadx
, Ship
.quady
, Ship
.sectx
, Ship
.secty
, tqx
, tqy
, ix
, iy
);
215 prkalc(course
, dist
);
222 case 5: /* phaser effectiveness */
223 dist
= getfltpar("range");
227 cost
= pow(0.90, dist
) * 98.0 + 0.5;
228 printf("Phasers are %d%% effective at that range\n", cost
);
231 case 6: /* warp cost (time/energy) */
232 dist
= getfltpar("distance");
235 warpfact
= getfltpar("warp factor");
237 warpfact
= Ship
.warp
;
238 cost
= (dist
+ 0.05) * warpfact
* warpfact
* warpfact
;
239 p_time
= Param
.warptime
* dist
/ (warpfact
* warpfact
);
240 printf("Warp %.2f distance %.2f cost %.2f stardates %d (%d w/ shlds up) units\n",
241 warpfact
, dist
, p_time
, cost
, cost
+ cost
);
244 case 7: /* impulse cost */
245 dist
= getfltpar("distance");
248 cost
= 20 + 100 * dist
;
249 p_time
= dist
/ 0.095;
250 printf("Distance %.2f cost %.2f stardates %d units\n",
254 case 8: /* distresslist */
257 /* scan the event list */
258 for (i
= 0; i
< MAXEVENTS
; i
++)
261 /* ignore hidden entries */
262 if (e
->evcode
& E_HIDDEN
)
264 switch (e
->evcode
& E_EVENT
)
268 printf("Klingon is attacking starbase in quadrant %d,%d\n",
275 printf("Starsystem %s in quadrant %d,%d is distressed\n",
276 Systemname
[e
->systemname
], e
->x
, e
->y
);
282 printf("No known distress calls are active\n");
287 /* skip to next semicolon or newline. Semicolon
288 * means get new computer request; newline means
289 * exit computer mode. */
290 while ((i
= cgetc(0)) != ';')
305 ** Course Calculation
307 ** Computes and outputs the course and distance from position
308 ** sqx,sqy/ssx,ssy to tqx,tqy/tsx,tsy.
312 kalc(int tqx
, int tqy
, int tsx
, int tsy
, double *dist
)
319 /* normalize to quadrant distances */
321 dx
= (Ship
.quadx
+ Ship
.sectx
/ quadsize
) - (tqx
+ tsx
/ quadsize
);
322 dy
= (tqy
+ tsy
/ quadsize
) - (Ship
.quady
+ Ship
.secty
/ quadsize
);
325 angle
= atan2(dy
, dx
);
326 /* make it 0 -> 2 pi */
328 angle
+= 6.283185307;
329 /* convert from radians to degrees */
330 course
= angle
* 57.29577951 + 0.5;
331 dx
= dx
* dx
+ dy
* dy
;
337 prkalc(int course
, double dist
)
339 printf(": course %d dist %.3f\n", course
, dist
);