Initial import from FreeBSD RELENG_4:
[dragonfly.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
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)computer.c  8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/trek/computer.c,v 1.5 1999/11/30 03:49:45 billf Exp $";
40 #endif /* not lint */
41
42 # include       "trek.h"
43 # include       "getpar.h"
44 # include       <stdio.h>
45 /*
46 **  On-Board Computer
47 **
48 **      A computer request is fetched from the captain.  The requests
49 **      are:
50 **
51 **      chart -- print a star chart of the known galaxy.  This includes
52 **              every quadrant that has ever had a long range or
53 **              a short range scan done of it, plus the location of
54 **              all starbases.  This is of course updated by any sub-
55 **              space radio broadcasts (unless the radio is out).
56 **              The format is the same as that of a long range scan
57 **              except that ".1." indicates that a starbase exists
58 **              but we know nothing else.
59 **
60 **      trajectory -- gives the course and distance to every know
61 **              Klingon in the quadrant.  Obviously this fails if the
62 **              short range scanners are out.
63 **
64 **      course -- gives a course computation from whereever you are
65 **              to any specified location.  If the course begins
66 **              with a slash, the current quadrant is taken.
67 **              Otherwise the input is quadrant and sector coordi-
68 **              nates of the target sector.
69 **
70 **      move -- identical to course, except that the move is performed.
71 **
72 **      score -- prints out the current score.
73 **
74 **      pheff -- "PHaser EFFectiveness" at a given distance.  Tells
75 **              you how much stuff you need to make it work.
76 **
77 **      warpcost -- Gives you the cost in time and units to move for
78 **              a given distance under a given warp speed.
79 **
80 **      impcost -- Same for the impulse engines.
81 **
82 **      distresslist -- Gives a list of the currently known starsystems
83 **              or starbases which are distressed, together with their
84 **              quadrant coordinates.
85 **
86 **      If a command is terminated with a semicolon, you remain in
87 **      the computer; otherwise, you escape immediately to the main
88 **      command processor.
89 */
90
91 struct cvntab   Cputab[] =
92 {
93         "ch",                   "art",                  (int (*)())1,           0,
94         "t",                    "rajectory",            (int (*)())2,           0,
95         "c",                    "ourse",                (int (*)())3,           0,
96         "m",                    "ove",                  (int (*)())3,           1,
97         "s",                    "core",                 (int (*)())4,           0,
98         "p",                    "heff",                 (int (*)())5,           0,
99         "w",                    "arpcost",              (int (*)())6,           0,
100         "i",                    "mpcost",               (int (*)())7,           0,
101         "d",                    "istresslist",          (int (*)())8,           0,
102         0
103 };
104
105 computer()
106 {
107         int                     ix, iy;
108         int             i, j;
109         int                     numout;
110         int                     tqx, tqy;
111         struct cvntab           *r;
112         int                     cost;
113         int                     course;
114         double                  dist, time;
115         double                  warpfact;
116         struct quad             *q;
117         struct event    *e;
118
119         if (check_out(COMPUTER))
120                 return;
121         while (1)
122         {
123                 r = getcodpar("\nRequest", Cputab);
124                 switch ((long)r->value)
125                 {
126
127                   case 1:                       /* star chart */
128                         printf("Computer record of galaxy for all long range sensor scans\n\n");
129                         printf("  ");
130                         /* print top header */
131                         for (i = 0; i < NQUADS; i++)
132                                 printf("-%d- ", i);
133                         printf("\n");
134                         for (i = 0; i < NQUADS; i++)
135                         {
136                                 printf("%d ", i);
137                                 for (j = 0; j < NQUADS; j++)
138                                 {
139                                         if (i == Ship.quadx && j == Ship.quady)
140                                         {
141                                                 printf("$$$ ");
142                                                 continue;
143                                         }
144                                         q = &Quad[i][j];
145                                         /* 1000 or 1001 is special case */
146                                         if (q->scanned >= 1000)
147                                                 if (q->scanned > 1000)
148                                                         printf(".1. ");
149                                                 else
150                                                         printf("/// ");
151                                         else
152                                                 if (q->scanned < 0)
153                                                         printf("... ");
154                                                 else
155                                                         printf("%3d ", q->scanned);
156                                 }
157                                 printf("%d\n", i);
158                         }
159                         printf("  ");
160                         /* print bottom footer */
161                         for (i = 0; i < NQUADS; i++)
162                                 printf("-%d- ", i);
163                         printf("\n");
164                         break;
165
166                   case 2:                       /* trajectory */
167                         if (check_out(SRSCAN))
168                         {
169                                 break;
170                         }
171                         if (Etc.nkling <= 0)
172                         {
173                                 printf("No Klingons in this quadrant\n");
174                                 break;
175                         }
176                         /* for each Klingon, give the course & distance */
177                         for (i = 0; i < Etc.nkling; i++)
178                         {
179                                 printf("Klingon at %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
180                                 course = kalc(Ship.quadx, Ship.quady, Etc.klingon[i].x, Etc.klingon[i].y, &dist);
181                                 prkalc(course, dist);
182                         }
183                         break;
184
185                   case 3:                       /* course calculation */
186                         if (readdelim('/'))
187                         {
188                                 tqx = Ship.quadx;
189                                 tqy = Ship.quady;
190                         }
191                         else
192                         {
193                                 ix = getintpar("Quadrant");
194                                 if (ix < 0 || ix >= NSECTS)
195                                         break;
196                                 iy = getintpar("q-y");
197                                 if (iy < 0 || iy >= NSECTS)
198                                         break;
199                                 tqx = ix;
200                                 tqy = iy;
201                         }
202                         ix = getintpar("Sector");
203                         if (ix < 0 || ix >= NSECTS)
204                                 break;
205                         iy = getintpar("s-y");
206                         if (iy < 0 || iy >= NSECTS)
207                                 break;
208                         course = kalc(tqx, tqy, ix, iy, &dist);
209                         if (r->value2)
210                         {
211                                 warp(-1, course, dist);
212                                 break;
213                         }
214                         printf("%d,%d/%d,%d to %d,%d/%d,%d",
215                                 Ship.quadx, Ship.quady, Ship.sectx, Ship.secty, tqx, tqy, ix, iy);
216                         prkalc(course, dist);
217                         break;
218
219                   case 4:                       /* score */
220                         score();
221                         break;
222
223                   case 5:                       /* phaser effectiveness */
224                         dist = getfltpar("range");
225                         if (dist < 0.0)
226                                 break;
227                         dist *= 10.0;
228                         cost = pow(0.90, dist) * 98.0 + 0.5;
229                         printf("Phasers are %d%% effective at that range\n", cost);
230                         break;
231
232                   case 6:                       /* warp cost (time/energy) */
233                         dist = getfltpar("distance");
234                         if (dist < 0.0)
235                                 break;
236                         warpfact = getfltpar("warp factor");
237                         if (warpfact <= 0.0)
238                                 warpfact = Ship.warp;
239                         cost = (dist + 0.05) * warpfact * warpfact * warpfact;
240                         time = Param.warptime * dist / (warpfact * warpfact);
241                         printf("Warp %.2f distance %.2f cost %.2f stardates %d (%d w/ shlds up) units\n",
242                                 warpfact, dist, time, cost, cost + cost);
243                         break;
244
245                   case 7:                       /* impulse cost */
246                         dist = getfltpar("distance");
247                         if (dist < 0.0)
248                                 break;
249                         cost = 20 + 100 * dist;
250                         time = dist / 0.095;
251                         printf("Distance %.2f cost %.2f stardates %d units\n",
252                                 dist, time, cost);
253                         break;
254
255                   case 8:                       /* distresslist */
256                         j = 1;
257                         printf("\n");
258                         /* scan the event list */
259                         for (i = 0; i < MAXEVENTS; i++)
260                         {
261                                 e = &Event[i];
262                                 /* ignore hidden entries */
263                                 if (e->evcode & E_HIDDEN)
264                                         continue;
265                                 switch (e->evcode & E_EVENT)
266                                 {
267
268                                   case E_KDESB:
269                                         printf("Klingon is attacking starbase in quadrant %d,%d\n",
270                                                 e->x, e->y);
271                                         j = 0;
272                                         break;
273
274                                   case E_ENSLV:
275                                   case E_REPRO:
276                                         printf("Starsystem %s in quadrant %d,%d is distressed\n",
277                                                 Systemname[e->systemname], e->x, e->y);
278                                         j = 0;
279                                         break;
280                                 }
281                         }
282                         if (j)
283                                 printf("No known distress calls are active\n");
284                         break;
285
286                 }
287
288                 /* skip to next semicolon or newline.  Semicolon
289                  * means get new computer request; newline means
290                  * exit computer mode. */
291                 while ((i = cgetc(0)) != ';')
292                 {
293                         if (i == '\0')
294                                 exit(1);
295                         if (i == '\n')
296                         {
297                                 ungetc(i, stdin);
298                                 return;
299                         }
300                 }
301         }
302 }
303
304
305 /*
306 **  Course Calculation
307 **
308 **      Computes and outputs the course and distance from position
309 **      sqx,sqy/ssx,ssy to tqx,tqy/tsx,tsy.
310 */
311
312 kalc(tqx, tqy, tsx, tsy, dist)
313 int     tqx;
314 int     tqy;
315 int     tsx;
316 int     tsy;
317 double  *dist;
318 {
319         double                  dx, dy;
320         double                  quadsize;
321         double                  angle;
322         int             course;
323
324         /* normalize to quadrant distances */
325         quadsize = NSECTS;
326         dx = (Ship.quadx + Ship.sectx / quadsize) - (tqx + tsx / quadsize);
327         dy = (tqy + tsy / quadsize) - (Ship.quady + Ship.secty / quadsize);
328
329         /* get the angle */
330         angle = atan2(dy, dx);
331         /* make it 0 -> 2 pi */
332         if (angle < 0.0)
333                 angle += 6.283185307;
334         /* convert from radians to degrees */
335         course = angle * 57.29577951 + 0.5;
336         dx = dx * dx + dy * dy;
337         *dist = sqrt(dx);
338         return (course);
339 }
340
341
342 prkalc(course, dist)
343 int     course;
344 double  dist;
345 {
346         printf(": course %d  dist %.3f\n", course, dist);
347 }