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