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