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