Home | History | Annotate | Line # | Download | only in trek
computer.c revision 1.10
      1 /*	$NetBSD: computer.c,v 1.10 2004/01/27 20:30:30 jsm 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.10 2004/01/27 20:30:30 jsm 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 struct cvntab	Cputab[] =
     94 {
     95 	{ "ch",		"art",			(cmdfun)1,		0 },
     96 	{ "t",		"rajectory",		(cmdfun)2,		0 },
     97 	{ "c",		"ourse",		(cmdfun)3,		0 },
     98 	{ "m",		"ove",			(cmdfun)3,		1 },
     99 	{ "s",		"core",			(cmdfun)4,		0 },
    100 	{ "p",		"heff",			(cmdfun)5,		0 },
    101 	{ "w",		"arpcost",		(cmdfun)6,		0 },
    102 	{ "i",		"mpcost",		(cmdfun)7,		0 },
    103 	{ "d",		"istresslist",		(cmdfun)8,		0 },
    104 	{ NULL,		NULL,			NULL,			0 }
    105 };
    106 
    107 static int kalc(int, int, int, int, double *);
    108 static void prkalc(int, double);
    109 
    110 /*ARGSUSED*/
    111 void
    112 computer(v)
    113 	int v __attribute__((__unused__));
    114 {
    115 	int		ix, iy;
    116 	int		i, j;
    117 	int		tqx, tqy;
    118 	const struct cvntab	*r;
    119 	int		cost;
    120 	int		course;
    121 	double		dist, time;
    122 	double		warpfact;
    123 	struct quad	*q;
    124 	struct event	*e;
    125 
    126 	if (check_out(COMPUTER))
    127 		return;
    128 	while (1)
    129 	{
    130 		r = getcodpar("\nRequest", Cputab);
    131 		switch ((long)r->value)
    132 		{
    133 
    134 		  case 1:			/* star chart */
    135 			printf("Computer record of galaxy for all long range sensor scans\n\n");
    136 			printf("  ");
    137 			/* print top header */
    138 			for (i = 0; i < NQUADS; i++)
    139 				printf("-%d- ", i);
    140 			printf("\n");
    141 			for (i = 0; i < NQUADS; i++)
    142 			{
    143 				printf("%d ", i);
    144 				for (j = 0; j < NQUADS; j++)
    145 				{
    146 					if (i == Ship.quadx && j == Ship.quady)
    147 					{
    148 						printf("$$$ ");
    149 						continue;
    150 					}
    151 					q = &Quad[i][j];
    152 					/* 1000 or 1001 is special case */
    153 					if (q->scanned >= 1000)
    154 						if (q->scanned > 1000)
    155 							printf(".1. ");
    156 						else
    157 							printf("/// ");
    158 					else
    159 						if (q->scanned < 0)
    160 							printf("... ");
    161 						else
    162 							printf("%3d ", q->scanned);
    163 				}
    164 				printf("%d\n", i);
    165 			}
    166 			printf("  ");
    167 			/* print bottom footer */
    168 			for (i = 0; i < NQUADS; i++)
    169 				printf("-%d- ", i);
    170 			printf("\n");
    171 			break;
    172 
    173 		  case 2:			/* trajectory */
    174 			if (check_out(SRSCAN))
    175 			{
    176 				break;
    177 			}
    178 			if (Etc.nkling <= 0)
    179 			{
    180 				printf("No Klingons in this quadrant\n");
    181 				break;
    182 			}
    183 			/* for each Klingon, give the course & distance */
    184 			for (i = 0; i < Etc.nkling; i++)
    185 			{
    186 				printf("Klingon at %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
    187 				course = kalc(Ship.quadx, Ship.quady, Etc.klingon[i].x, Etc.klingon[i].y, &dist);
    188 				prkalc(course, dist);
    189 			}
    190 			break;
    191 
    192 		  case 3:			/* course calculation */
    193 			if (readdelim('/'))
    194 			{
    195 				tqx = Ship.quadx;
    196 				tqy = Ship.quady;
    197 			}
    198 			else
    199 			{
    200 				ix = getintpar("Quadrant");
    201 				if (ix < 0 || ix >= NSECTS)
    202 					break;
    203 				iy = getintpar("q-y");
    204 				if (iy < 0 || iy >= NSECTS)
    205 					break;
    206 				tqx = ix;
    207 				tqy = iy;
    208 			}
    209 			ix = getintpar("Sector");
    210 			if (ix < 0 || ix >= NSECTS)
    211 				break;
    212 			iy = getintpar("s-y");
    213 			if (iy < 0 || iy >= NSECTS)
    214 				break;
    215 			course = kalc(tqx, tqy, ix, iy, &dist);
    216 			if (r->value2)
    217 			{
    218 				warp(-1, course, dist);
    219 				break;
    220 			}
    221 			printf("%d,%d/%d,%d to %d,%d/%d,%d",
    222 				Ship.quadx, Ship.quady, Ship.sectx, Ship.secty, tqx, tqy, ix, iy);
    223 			prkalc(course, dist);
    224 			break;
    225 
    226 		  case 4:			/* score */
    227 			score();
    228 			break;
    229 
    230 		  case 5:			/* phaser effectiveness */
    231 			dist = getfltpar("range");
    232 			if (dist < 0.0)
    233 				break;
    234 			dist *= 10.0;
    235 			cost = pow(0.90, dist) * 98.0 + 0.5;
    236 			printf("Phasers are %d%% effective at that range\n", cost);
    237 			break;
    238 
    239 		  case 6:			/* warp cost (time/energy) */
    240 			dist = getfltpar("distance");
    241 			if (dist < 0.0)
    242 				break;
    243 			warpfact = getfltpar("warp factor");
    244 			if (warpfact <= 0.0)
    245 				warpfact = Ship.warp;
    246 			cost = (dist + 0.05) * warpfact * warpfact * warpfact;
    247 			time = Param.warptime * dist / (warpfact * warpfact);
    248 			printf("Warp %.2f distance %.2f cost %.2f stardates %d (%d w/ shlds up) units\n",
    249 				warpfact, dist, time, cost, cost + cost);
    250 			break;
    251 
    252 		  case 7:			/* impulse cost */
    253 			dist = getfltpar("distance");
    254 			if (dist < 0.0)
    255 				break;
    256 			cost = 20 + 100 * dist;
    257 			time = dist / 0.095;
    258 			printf("Distance %.2f cost %.2f stardates %d units\n",
    259 				dist, time, cost);
    260 			break;
    261 
    262 		  case 8:			/* distresslist */
    263 			j = 1;
    264 			printf("\n");
    265 			/* scan the event list */
    266 			for (i = 0; i < MAXEVENTS; i++)
    267 			{
    268 				e = &Event[i];
    269 				/* ignore hidden entries */
    270 				if (e->evcode & E_HIDDEN)
    271 					continue;
    272 				switch (e->evcode & E_EVENT)
    273 				{
    274 
    275 				  case E_KDESB:
    276 					printf("Klingon is attacking starbase in quadrant %d,%d\n",
    277 						e->x, e->y);
    278 					j = 0;
    279 					break;
    280 
    281 				  case E_ENSLV:
    282 				  case E_REPRO:
    283 					printf("Starsystem %s in quadrant %d,%d is distressed\n",
    284 						Systemname[e->systemname], e->x, e->y);
    285 					j = 0;
    286 					break;
    287 				}
    288 			}
    289 			if (j)
    290 				printf("No known distress calls are active\n");
    291 			break;
    292 
    293 		}
    294 
    295 		/* skip to next semicolon or newline.  Semicolon
    296 		 * means get new computer request; newline means
    297 		 * exit computer mode. */
    298 		while ((i = cgetc(0)) != ';')
    299 		{
    300 			if (i == '\0')
    301 				exit(1);
    302 			if (i == '\n')
    303 			{
    304 				ungetc(i, stdin);
    305 				return;
    306 			}
    307 		}
    308 	}
    309 }
    310 
    311 
    312 /*
    313 **  Course Calculation
    314 **
    315 **	Computes and outputs the course and distance from position
    316 **	sqx,sqy/ssx,ssy to tqx,tqy/tsx,tsy.
    317 */
    318 
    319 static int
    320 kalc(tqx, tqy, tsx, tsy, dist)
    321 int	tqx;
    322 int	tqy;
    323 int	tsx;
    324 int	tsy;
    325 double	*dist;
    326 {
    327 	double			dx, dy;
    328 	double			quadsize;
    329 	double			angle;
    330 	int		course;
    331 
    332 	/* normalize to quadrant distances */
    333 	quadsize = NSECTS;
    334 	dx = (Ship.quadx + Ship.sectx / quadsize) - (tqx + tsx / quadsize);
    335 	dy = (tqy + tsy / quadsize) - (Ship.quady + Ship.secty / quadsize);
    336 
    337 	/* get the angle */
    338 	angle = atan2(dy, dx);
    339 	/* make it 0 -> 2 pi */
    340 	if (angle < 0.0)
    341 		angle += 6.283185307;
    342 	/* convert from radians to degrees */
    343 	course = angle * 57.29577951 + 0.5;
    344 	dx = dx * dx + dy * dy;
    345 	*dist = sqrt(dx);
    346 	return (course);
    347 }
    348 
    349 static void
    350 prkalc(course, dist)
    351 int	course;
    352 double	dist;
    353 {
    354 	printf(": course %d  dist %.3f\n", course, dist);
    355 }
    356