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