computer.c revision 1.8 1 1.8 matt /* $NetBSD: computer.c,v 1.8 2000/07/03 03:57:44 matt Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1980, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.5 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.3 cgd #if 0
39 1.3 cgd static char sccsid[] = "@(#)computer.c 8.1 (Berkeley) 5/31/93";
40 1.3 cgd #else
41 1.8 matt __RCSID("$NetBSD: computer.c,v 1.8 2000/07/03 03:57:44 matt Exp $");
42 1.3 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.5 christos #include <stdio.h>
46 1.8 matt #include <stdlib.h>
47 1.5 christos #include <math.h>
48 1.5 christos #include "trek.h"
49 1.5 christos #include "getpar.h"
50 1.5 christos
51 1.1 cgd /*
52 1.1 cgd ** On-Board Computer
53 1.1 cgd **
54 1.1 cgd ** A computer request is fetched from the captain. The requests
55 1.1 cgd ** are:
56 1.1 cgd **
57 1.1 cgd ** chart -- print a star chart of the known galaxy. This includes
58 1.1 cgd ** every quadrant that has ever had a long range or
59 1.1 cgd ** a short range scan done of it, plus the location of
60 1.1 cgd ** all starbases. This is of course updated by any sub-
61 1.1 cgd ** space radio broadcasts (unless the radio is out).
62 1.1 cgd ** The format is the same as that of a long range scan
63 1.1 cgd ** except that ".1." indicates that a starbase exists
64 1.1 cgd ** but we know nothing else.
65 1.1 cgd **
66 1.1 cgd ** trajectory -- gives the course and distance to every know
67 1.1 cgd ** Klingon in the quadrant. Obviously this fails if the
68 1.1 cgd ** short range scanners are out.
69 1.1 cgd **
70 1.1 cgd ** course -- gives a course computation from whereever you are
71 1.1 cgd ** to any specified location. If the course begins
72 1.1 cgd ** with a slash, the current quadrant is taken.
73 1.1 cgd ** Otherwise the input is quadrant and sector coordi-
74 1.1 cgd ** nates of the target sector.
75 1.1 cgd **
76 1.1 cgd ** move -- identical to course, except that the move is performed.
77 1.1 cgd **
78 1.1 cgd ** score -- prints out the current score.
79 1.1 cgd **
80 1.1 cgd ** pheff -- "PHaser EFFectiveness" at a given distance. Tells
81 1.1 cgd ** you how much stuff you need to make it work.
82 1.1 cgd **
83 1.1 cgd ** warpcost -- Gives you the cost in time and units to move for
84 1.1 cgd ** a given distance under a given warp speed.
85 1.1 cgd **
86 1.1 cgd ** impcost -- Same for the impulse engines.
87 1.1 cgd **
88 1.1 cgd ** distresslist -- Gives a list of the currently known starsystems
89 1.1 cgd ** or starbases which are distressed, together with their
90 1.1 cgd ** quadrant coordinates.
91 1.1 cgd **
92 1.1 cgd ** If a command is terminated with a semicolon, you remain in
93 1.1 cgd ** the computer; otherwise, you escape immediately to the main
94 1.1 cgd ** command processor.
95 1.1 cgd */
96 1.1 cgd
97 1.1 cgd struct cvntab Cputab[] =
98 1.1 cgd {
99 1.5 christos { "ch", "art", (cmdfun)1, 0 },
100 1.5 christos { "t", "rajectory", (cmdfun)2, 0 },
101 1.5 christos { "c", "ourse", (cmdfun)3, 0 },
102 1.5 christos { "m", "ove", (cmdfun)3, 1 },
103 1.5 christos { "s", "core", (cmdfun)4, 0 },
104 1.5 christos { "p", "heff", (cmdfun)5, 0 },
105 1.5 christos { "w", "arpcost", (cmdfun)6, 0 },
106 1.5 christos { "i", "mpcost", (cmdfun)7, 0 },
107 1.5 christos { "d", "istresslist", (cmdfun)8, 0 },
108 1.5 christos { NULL, NULL, NULL, 0 }
109 1.1 cgd };
110 1.1 cgd
111 1.5 christos static int kalc __P((int, int, int, int, double *));
112 1.5 christos static void prkalc __P((int, double));
113 1.5 christos
114 1.5 christos /*ARGSUSED*/
115 1.5 christos void
116 1.5 christos computer(v)
117 1.7 jsm int v __attribute__((__unused__));
118 1.1 cgd {
119 1.5 christos int ix, iy;
120 1.5 christos int i, j;
121 1.5 christos int tqx, tqy;
122 1.6 hubertf const struct cvntab *r;
123 1.5 christos int cost;
124 1.5 christos int course;
125 1.5 christos double dist, time;
126 1.5 christos double warpfact;
127 1.5 christos struct quad *q;
128 1.5 christos struct event *e;
129 1.1 cgd
130 1.1 cgd if (check_out(COMPUTER))
131 1.1 cgd return;
132 1.1 cgd while (1)
133 1.1 cgd {
134 1.1 cgd r = getcodpar("\nRequest", Cputab);
135 1.4 cgd switch ((long)r->value)
136 1.1 cgd {
137 1.1 cgd
138 1.1 cgd case 1: /* star chart */
139 1.1 cgd printf("Computer record of galaxy for all long range sensor scans\n\n");
140 1.1 cgd printf(" ");
141 1.1 cgd /* print top header */
142 1.1 cgd for (i = 0; i < NQUADS; i++)
143 1.1 cgd printf("-%d- ", i);
144 1.1 cgd printf("\n");
145 1.1 cgd for (i = 0; i < NQUADS; i++)
146 1.1 cgd {
147 1.1 cgd printf("%d ", i);
148 1.1 cgd for (j = 0; j < NQUADS; j++)
149 1.1 cgd {
150 1.1 cgd if (i == Ship.quadx && j == Ship.quady)
151 1.1 cgd {
152 1.1 cgd printf("$$$ ");
153 1.1 cgd continue;
154 1.1 cgd }
155 1.1 cgd q = &Quad[i][j];
156 1.1 cgd /* 1000 or 1001 is special case */
157 1.1 cgd if (q->scanned >= 1000)
158 1.1 cgd if (q->scanned > 1000)
159 1.1 cgd printf(".1. ");
160 1.1 cgd else
161 1.1 cgd printf("/// ");
162 1.1 cgd else
163 1.1 cgd if (q->scanned < 0)
164 1.1 cgd printf("... ");
165 1.1 cgd else
166 1.1 cgd printf("%3d ", q->scanned);
167 1.1 cgd }
168 1.1 cgd printf("%d\n", i);
169 1.1 cgd }
170 1.1 cgd printf(" ");
171 1.1 cgd /* print bottom footer */
172 1.1 cgd for (i = 0; i < NQUADS; i++)
173 1.1 cgd printf("-%d- ", i);
174 1.1 cgd printf("\n");
175 1.1 cgd break;
176 1.1 cgd
177 1.1 cgd case 2: /* trajectory */
178 1.1 cgd if (check_out(SRSCAN))
179 1.1 cgd {
180 1.1 cgd break;
181 1.1 cgd }
182 1.1 cgd if (Etc.nkling <= 0)
183 1.1 cgd {
184 1.1 cgd printf("No Klingons in this quadrant\n");
185 1.1 cgd break;
186 1.1 cgd }
187 1.1 cgd /* for each Klingon, give the course & distance */
188 1.1 cgd for (i = 0; i < Etc.nkling; i++)
189 1.1 cgd {
190 1.1 cgd printf("Klingon at %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
191 1.1 cgd course = kalc(Ship.quadx, Ship.quady, Etc.klingon[i].x, Etc.klingon[i].y, &dist);
192 1.1 cgd prkalc(course, dist);
193 1.1 cgd }
194 1.1 cgd break;
195 1.1 cgd
196 1.1 cgd case 3: /* course calculation */
197 1.1 cgd if (readdelim('/'))
198 1.1 cgd {
199 1.1 cgd tqx = Ship.quadx;
200 1.1 cgd tqy = Ship.quady;
201 1.1 cgd }
202 1.1 cgd else
203 1.1 cgd {
204 1.1 cgd ix = getintpar("Quadrant");
205 1.1 cgd if (ix < 0 || ix >= NSECTS)
206 1.1 cgd break;
207 1.1 cgd iy = getintpar("q-y");
208 1.1 cgd if (iy < 0 || iy >= NSECTS)
209 1.1 cgd break;
210 1.1 cgd tqx = ix;
211 1.1 cgd tqy = iy;
212 1.1 cgd }
213 1.1 cgd ix = getintpar("Sector");
214 1.1 cgd if (ix < 0 || ix >= NSECTS)
215 1.1 cgd break;
216 1.1 cgd iy = getintpar("s-y");
217 1.1 cgd if (iy < 0 || iy >= NSECTS)
218 1.1 cgd break;
219 1.1 cgd course = kalc(tqx, tqy, ix, iy, &dist);
220 1.1 cgd if (r->value2)
221 1.1 cgd {
222 1.1 cgd warp(-1, course, dist);
223 1.1 cgd break;
224 1.1 cgd }
225 1.1 cgd printf("%d,%d/%d,%d to %d,%d/%d,%d",
226 1.1 cgd Ship.quadx, Ship.quady, Ship.sectx, Ship.secty, tqx, tqy, ix, iy);
227 1.1 cgd prkalc(course, dist);
228 1.1 cgd break;
229 1.1 cgd
230 1.1 cgd case 4: /* score */
231 1.1 cgd score();
232 1.1 cgd break;
233 1.1 cgd
234 1.1 cgd case 5: /* phaser effectiveness */
235 1.1 cgd dist = getfltpar("range");
236 1.1 cgd if (dist < 0.0)
237 1.1 cgd break;
238 1.1 cgd dist *= 10.0;
239 1.1 cgd cost = pow(0.90, dist) * 98.0 + 0.5;
240 1.1 cgd printf("Phasers are %d%% effective at that range\n", cost);
241 1.1 cgd break;
242 1.1 cgd
243 1.1 cgd case 6: /* warp cost (time/energy) */
244 1.1 cgd dist = getfltpar("distance");
245 1.1 cgd if (dist < 0.0)
246 1.1 cgd break;
247 1.1 cgd warpfact = getfltpar("warp factor");
248 1.1 cgd if (warpfact <= 0.0)
249 1.1 cgd warpfact = Ship.warp;
250 1.1 cgd cost = (dist + 0.05) * warpfact * warpfact * warpfact;
251 1.1 cgd time = Param.warptime * dist / (warpfact * warpfact);
252 1.1 cgd printf("Warp %.2f distance %.2f cost %.2f stardates %d (%d w/ shlds up) units\n",
253 1.1 cgd warpfact, dist, time, cost, cost + cost);
254 1.1 cgd break;
255 1.1 cgd
256 1.1 cgd case 7: /* impulse cost */
257 1.1 cgd dist = getfltpar("distance");
258 1.1 cgd if (dist < 0.0)
259 1.1 cgd break;
260 1.1 cgd cost = 20 + 100 * dist;
261 1.1 cgd time = dist / 0.095;
262 1.1 cgd printf("Distance %.2f cost %.2f stardates %d units\n",
263 1.1 cgd dist, time, cost);
264 1.1 cgd break;
265 1.1 cgd
266 1.1 cgd case 8: /* distresslist */
267 1.1 cgd j = 1;
268 1.1 cgd printf("\n");
269 1.1 cgd /* scan the event list */
270 1.1 cgd for (i = 0; i < MAXEVENTS; i++)
271 1.1 cgd {
272 1.1 cgd e = &Event[i];
273 1.1 cgd /* ignore hidden entries */
274 1.1 cgd if (e->evcode & E_HIDDEN)
275 1.1 cgd continue;
276 1.1 cgd switch (e->evcode & E_EVENT)
277 1.1 cgd {
278 1.1 cgd
279 1.1 cgd case E_KDESB:
280 1.1 cgd printf("Klingon is attacking starbase in quadrant %d,%d\n",
281 1.1 cgd e->x, e->y);
282 1.1 cgd j = 0;
283 1.1 cgd break;
284 1.1 cgd
285 1.1 cgd case E_ENSLV:
286 1.1 cgd case E_REPRO:
287 1.1 cgd printf("Starsystem %s in quadrant %d,%d is distressed\n",
288 1.1 cgd Systemname[e->systemname], e->x, e->y);
289 1.1 cgd j = 0;
290 1.1 cgd break;
291 1.1 cgd }
292 1.1 cgd }
293 1.1 cgd if (j)
294 1.1 cgd printf("No known distress calls are active\n");
295 1.1 cgd break;
296 1.1 cgd
297 1.1 cgd }
298 1.1 cgd
299 1.1 cgd /* skip to next semicolon or newline. Semicolon
300 1.1 cgd * means get new computer request; newline means
301 1.1 cgd * exit computer mode. */
302 1.1 cgd while ((i = cgetc(0)) != ';')
303 1.1 cgd {
304 1.1 cgd if (i == '\0')
305 1.1 cgd exit(1);
306 1.1 cgd if (i == '\n')
307 1.1 cgd {
308 1.1 cgd ungetc(i, stdin);
309 1.1 cgd return;
310 1.1 cgd }
311 1.1 cgd }
312 1.1 cgd }
313 1.1 cgd }
314 1.1 cgd
315 1.1 cgd
316 1.1 cgd /*
317 1.1 cgd ** Course Calculation
318 1.1 cgd **
319 1.1 cgd ** Computes and outputs the course and distance from position
320 1.1 cgd ** sqx,sqy/ssx,ssy to tqx,tqy/tsx,tsy.
321 1.1 cgd */
322 1.1 cgd
323 1.5 christos static int
324 1.1 cgd kalc(tqx, tqy, tsx, tsy, dist)
325 1.1 cgd int tqx;
326 1.1 cgd int tqy;
327 1.1 cgd int tsx;
328 1.1 cgd int tsy;
329 1.1 cgd double *dist;
330 1.1 cgd {
331 1.1 cgd double dx, dy;
332 1.1 cgd double quadsize;
333 1.1 cgd double angle;
334 1.5 christos int course;
335 1.1 cgd
336 1.1 cgd /* normalize to quadrant distances */
337 1.1 cgd quadsize = NSECTS;
338 1.1 cgd dx = (Ship.quadx + Ship.sectx / quadsize) - (tqx + tsx / quadsize);
339 1.1 cgd dy = (tqy + tsy / quadsize) - (Ship.quady + Ship.secty / quadsize);
340 1.1 cgd
341 1.1 cgd /* get the angle */
342 1.1 cgd angle = atan2(dy, dx);
343 1.1 cgd /* make it 0 -> 2 pi */
344 1.1 cgd if (angle < 0.0)
345 1.1 cgd angle += 6.283185307;
346 1.1 cgd /* convert from radians to degrees */
347 1.1 cgd course = angle * 57.29577951 + 0.5;
348 1.1 cgd dx = dx * dx + dy * dy;
349 1.1 cgd *dist = sqrt(dx);
350 1.1 cgd return (course);
351 1.1 cgd }
352 1.1 cgd
353 1.5 christos static void
354 1.1 cgd prkalc(course, dist)
355 1.1 cgd int course;
356 1.1 cgd double dist;
357 1.1 cgd {
358 1.1 cgd printf(": course %d dist %.3f\n", course, dist);
359 1.1 cgd }
360