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