Merge from vendor branch TCPDUMP:
[games.git] / games / trek / computer.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
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 $
36  */
37
38 # include       "trek.h"
39 # include       "getpar.h"
40
41 static int      kalc(int, int, int, int, double *);
42 static void     prkalc(int, double);
43
44 /*
45 **  On-Board Computer
46 **
47 **      A computer request is fetched from the captain.  The requests
48 **      are:
49 **
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.
58 **
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.
62 **
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.
68 **
69 **      move -- identical to course, except that the move is performed.
70 **
71 **      score -- prints out the current score.
72 **
73 **      pheff -- "PHaser EFFectiveness" at a given distance.  Tells
74 **              you how much stuff you need to make it work.
75 **
76 **      warpcost -- Gives you the cost in time and units to move for
77 **              a given distance under a given warp speed.
78 **
79 **      impcost -- Same for the impulse engines.
80 **
81 **      distresslist -- Gives a list of the currently known starsystems
82 **              or starbases which are distressed, together with their
83 **              quadrant coordinates.
84 **
85 **      If a command is terminated with a semicolon, you remain in
86 **      the computer; otherwise, you escape immediately to the main
87 **      command processor.
88 */
89
90 struct cvntab   Cputab[] =
91 {
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 }
102 };
103
104 void
105 computer(__unused int unused)
106 {
107         int                     ix, iy;
108         int             i, j;
109         int                     tqx, tqy;
110         struct cvntab           *r;
111         int                     cost;
112         int                     course;
113         double                  dist, p_time;
114         double                  warpfact;
115         struct quad             *q;
116         struct event    *e;
117
118         if (check_out(COMPUTER))
119                 return;
120         while (1)
121         {
122                 r = getcodpar("\nRequest", Cputab);
123                 switch ((long)r->value)
124                 {
125
126                   case 1:                       /* star chart */
127                         printf("Computer record of galaxy for all long range sensor scans\n\n");
128                         printf("  ");
129                         /* print top header */
130                         for (i = 0; i < NQUADS; i++)
131                                 printf("-%d- ", i);
132                         printf("\n");
133                         for (i = 0; i < NQUADS; i++)
134                         {
135                                 printf("%d ", i);
136                                 for (j = 0; j < NQUADS; j++)
137                                 {
138                                         if (i == Ship.quadx && j == Ship.quady)
139                                         {
140                                                 printf("$$$ ");
141                                                 continue;
142                                         }
143                                         q = &Quad[i][j];
144                                         /* 1000 or 1001 is special case */
145                                         if (q->scanned >= 1000)
146                                                 if (q->scanned > 1000)
147                                                         printf(".1. ");
148                                                 else
149                                                         printf("/// ");
150                                         else
151                                                 if (q->scanned < 0)
152                                                         printf("... ");
153                                                 else
154                                                         printf("%3d ", q->scanned);
155                                 }
156                                 printf("%d\n", i);
157                         }
158                         printf("  ");
159                         /* print bottom footer */
160                         for (i = 0; i < NQUADS; i++)
161                                 printf("-%d- ", i);
162                         printf("\n");
163                         break;
164
165                   case 2:                       /* trajectory */
166                         if (check_out(SRSCAN))
167                         {
168                                 break;
169                         }
170                         if (Etc.nkling <= 0)
171                         {
172                                 printf("No Klingons in this quadrant\n");
173                                 break;
174                         }
175                         /* for each Klingon, give the course & distance */
176                         for (i = 0; i < Etc.nkling; i++)
177                         {
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);
181                         }
182                         break;
183
184                   case 3:                       /* course calculation */
185                         if (readdelim('/'))
186                         {
187                                 tqx = Ship.quadx;
188                                 tqy = Ship.quady;
189                         }
190                         else
191                         {
192                                 ix = getintpar("Quadrant");
193                                 if (ix < 0 || ix >= NSECTS)
194                                         break;
195                                 iy = getintpar("q-y");
196                                 if (iy < 0 || iy >= NSECTS)
197                                         break;
198                                 tqx = ix;
199                                 tqy = iy;
200                         }
201                         ix = getintpar("Sector");
202                         if (ix < 0 || ix >= NSECTS)
203                                 break;
204                         iy = getintpar("s-y");
205                         if (iy < 0 || iy >= NSECTS)
206                                 break;
207                         course = kalc(tqx, tqy, ix, iy, &dist);
208                         if (r->value2)
209                         {
210                                 warp(-1, course, dist);
211                                 break;
212                         }
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);
216                         break;
217
218                   case 4:                       /* score */
219                         score();
220                         break;
221
222                   case 5:                       /* phaser effectiveness */
223                         dist = getfltpar("range");
224                         if (dist < 0.0)
225                                 break;
226                         dist *= 10.0;
227                         cost = pow(0.90, dist) * 98.0 + 0.5;
228                         printf("Phasers are %d%% effective at that range\n", cost);
229                         break;
230
231                   case 6:                       /* warp cost (time/energy) */
232                         dist = getfltpar("distance");
233                         if (dist < 0.0)
234                                 break;
235                         warpfact = getfltpar("warp factor");
236                         if (warpfact <= 0.0)
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);
242                         break;
243
244                   case 7:                       /* impulse cost */
245                         dist = getfltpar("distance");
246                         if (dist < 0.0)
247                                 break;
248                         cost = 20 + 100 * dist;
249                         p_time = dist / 0.095;
250                         printf("Distance %.2f cost %.2f stardates %d units\n",
251                                 dist, p_time, cost);
252                         break;
253
254                   case 8:                       /* distresslist */
255                         j = 1;
256                         printf("\n");
257                         /* scan the event list */
258                         for (i = 0; i < MAXEVENTS; i++)
259                         {
260                                 e = &Event[i];
261                                 /* ignore hidden entries */
262                                 if (e->evcode & E_HIDDEN)
263                                         continue;
264                                 switch (e->evcode & E_EVENT)
265                                 {
266
267                                   case E_KDESB:
268                                         printf("Klingon is attacking starbase in quadrant %d,%d\n",
269                                                 e->x, e->y);
270                                         j = 0;
271                                         break;
272
273                                   case E_ENSLV:
274                                   case E_REPRO:
275                                         printf("Starsystem %s in quadrant %d,%d is distressed\n",
276                                                 Systemname[e->systemname], e->x, e->y);
277                                         j = 0;
278                                         break;
279                                 }
280                         }
281                         if (j)
282                                 printf("No known distress calls are active\n");
283                         break;
284
285                 }
286
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)) != ';')
291                 {
292                         if (i == '\0')
293                                 exit(1);
294                         if (i == '\n')
295                         {
296                                 ungetc(i, stdin);
297                                 return;
298                         }
299                 }
300         }
301 }
302
303
304 /*
305 **  Course Calculation
306 **
307 **      Computes and outputs the course and distance from position
308 **      sqx,sqy/ssx,ssy to tqx,tqy/tsx,tsy.
309 */
310
311 static int
312 kalc(int tqx, int tqy, int tsx, int tsy, double *dist)
313 {
314         double                  dx, dy;
315         double                  quadsize;
316         double                  angle;
317         int             course;
318
319         /* normalize to quadrant distances */
320         quadsize = NSECTS;
321         dx = (Ship.quadx + Ship.sectx / quadsize) - (tqx + tsx / quadsize);
322         dy = (tqy + tsy / quadsize) - (Ship.quady + Ship.secty / quadsize);
323
324         /* get the angle */
325         angle = atan2(dy, dx);
326         /* make it 0 -> 2 pi */
327         if (angle < 0.0)
328                 angle += 6.283185307;
329         /* convert from radians to degrees */
330         course = angle * 57.29577951 + 0.5;
331         dx = dx * dx + dy * dy;
332         *dist = sqrt(dx);
333         return (course);
334 }
335
336 static void
337 prkalc(int course, double dist)
338 {
339         printf(": course %d  dist %.3f\n", course, dist);
340 }