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