Home | History | Annotate | Line # | Download | only in trek
move.c revision 1.7
      1  1.7  dholland /*	$NetBSD: move.c,v 1.7 2009/05/24 19:18:44 dholland Exp $	*/
      2  1.3       cgd 
      3  1.1       cgd /*
      4  1.3       cgd  * Copyright (c) 1980, 1993
      5  1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.6       agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.1       cgd  *    may be used to endorse or promote products derived from this software
     17  1.1       cgd  *    without specific prior written permission.
     18  1.1       cgd  *
     19  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1       cgd  * SUCH DAMAGE.
     30  1.1       cgd  */
     31  1.1       cgd 
     32  1.4  christos #include <sys/cdefs.h>
     33  1.1       cgd #ifndef lint
     34  1.3       cgd #if 0
     35  1.3       cgd static char sccsid[] = "@(#)move.c	8.1 (Berkeley) 5/31/93";
     36  1.3       cgd #else
     37  1.7  dholland __RCSID("$NetBSD: move.c,v 1.7 2009/05/24 19:18:44 dholland Exp $");
     38  1.3       cgd #endif
     39  1.1       cgd #endif /* not lint */
     40  1.1       cgd 
     41  1.4  christos #include <stdio.h>
     42  1.4  christos #include <math.h>
     43  1.4  christos #include "trek.h"
     44  1.1       cgd 
     45  1.1       cgd /*
     46  1.1       cgd **  Move Under Warp or Impulse Power
     47  1.1       cgd **
     48  1.1       cgd **	`Ramflag' is set if we are to be allowed to ram stars,
     49  1.1       cgd **	Klingons, etc.  This is passed from warp(), which gets it from
     50  1.1       cgd **	either play() or ram().  Course is the course (0 -> 360) at
     51  1.1       cgd **	which we want to move.  `Speed' is the speed we
     52  1.1       cgd **	want to go, and `time' is the expected time.  It
     53  1.1       cgd **	can get cut short if a long range tractor beam is to occur.  We
     54  1.1       cgd **	cut short the move so that the user doesn't get docked time and
     55  1.1       cgd **	energy for distance which he didn't travel.
     56  1.1       cgd **
     57  1.1       cgd **	We check the course through the current quadrant to see that he
     58  1.1       cgd **	doesn't run into anything.  After that, though, space sort of
     59  1.1       cgd **	bends around him.  Note that this puts us in the awkward posi-
     60  1.1       cgd **	tion of being able to be dropped into a sector which is com-
     61  1.1       cgd **	pletely surrounded by stars.  Oh Well.
     62  1.1       cgd **
     63  1.1       cgd **	If the SINS (Space Inertial Navigation System) is out, we ran-
     64  1.1       cgd **	domize the course accordingly before ever starting to move.
     65  1.1       cgd **	We will still move in a straight line.
     66  1.1       cgd **
     67  1.1       cgd **	Note that if your computer is out, you ram things anyway.  In
     68  1.1       cgd **	other words, if your computer and sins are both out, you're in
     69  1.1       cgd **	potentially very bad shape.
     70  1.1       cgd **
     71  1.1       cgd **	Klingons get a chance to zap you as you leave the quadrant.
     72  1.1       cgd **	By the way, they also try to follow you (heh heh).
     73  1.1       cgd **
     74  1.1       cgd **	Return value is the actual amount of time used.
     75  1.1       cgd **
     76  1.1       cgd **
     77  1.1       cgd **	Uses trace flag 4.
     78  1.1       cgd */
     79  1.1       cgd 
     80  1.7  dholland double
     81  1.7  dholland move(int ramflag, int course, double time, double speed)
     82  1.1       cgd {
     83  1.1       cgd 	double			angle;
     84  1.1       cgd 	double			x, y, dx, dy;
     85  1.4  christos 	int		ix = 0, iy = 0;
     86  1.1       cgd 	double			bigger;
     87  1.1       cgd 	int			n;
     88  1.4  christos 	int		i;
     89  1.1       cgd 	double			dist;
     90  1.1       cgd 	double			sectsize;
     91  1.1       cgd 	double			xn;
     92  1.1       cgd 	double			evtime;
     93  1.1       cgd 
     94  1.1       cgd #	ifdef xTRACE
     95  1.1       cgd 	if (Trace)
     96  1.1       cgd 		printf("move: ramflag %d course %d time %.2f speed %.2f\n",
     97  1.1       cgd 			ramflag, course, time, speed);
     98  1.1       cgd #	endif
     99  1.1       cgd 	sectsize = NSECTS;
    100  1.1       cgd 	/* initialize delta factors for move */
    101  1.1       cgd 	angle = course * 0.0174532925;
    102  1.1       cgd 	if (damaged(SINS))
    103  1.1       cgd 		angle += Param.navigcrud[1] * (franf() - 0.5);
    104  1.1       cgd 	else
    105  1.1       cgd 		if (Ship.sinsbad)
    106  1.1       cgd 			angle += Param.navigcrud[0] * (franf() - 0.5);
    107  1.1       cgd 	dx = -cos(angle);
    108  1.1       cgd 	dy = sin(angle);
    109  1.1       cgd 	bigger = fabs(dx);
    110  1.1       cgd 	dist = fabs(dy);
    111  1.1       cgd 	if (dist > bigger)
    112  1.1       cgd 		bigger = dist;
    113  1.1       cgd 	dx /= bigger;
    114  1.1       cgd 	dy /= bigger;
    115  1.1       cgd 
    116  1.1       cgd 	/* check for long range tractor beams */
    117  1.1       cgd 	/****  TEMPORARY CODE == DEBUGGING  ****/
    118  1.1       cgd 	evtime = Now.eventptr[E_LRTB]->date - Now.date;
    119  1.1       cgd #	ifdef xTRACE
    120  1.1       cgd 	if (Trace)
    121  1.4  christos 		printf("E.ep = %p, ->evcode = %d, ->date = %.2f, evtime = %.2f\n",
    122  1.1       cgd 			Now.eventptr[E_LRTB], Now.eventptr[E_LRTB]->evcode,
    123  1.1       cgd 			Now.eventptr[E_LRTB]->date, evtime);
    124  1.1       cgd #	endif
    125  1.1       cgd 	if (time > evtime && Etc.nkling < 3)
    126  1.1       cgd 	{
    127  1.1       cgd 		/* then we got a LRTB */
    128  1.1       cgd 		evtime += 0.005;
    129  1.1       cgd 		time = evtime;
    130  1.1       cgd 	}
    131  1.1       cgd 	else
    132  1.1       cgd 		evtime = -1.0e50;
    133  1.1       cgd 	dist = time * speed;
    134  1.1       cgd 
    135  1.1       cgd 	/* move within quadrant */
    136  1.1       cgd 	Sect[Ship.sectx][Ship.secty] = EMPTY;
    137  1.1       cgd 	x = Ship.sectx + 0.5;
    138  1.1       cgd 	y = Ship.secty + 0.5;
    139  1.1       cgd 	xn = NSECTS * dist * bigger;
    140  1.1       cgd 	n = xn + 0.5;
    141  1.1       cgd #	ifdef xTRACE
    142  1.1       cgd 	if (Trace)
    143  1.1       cgd 		printf("dx = %.2f, dy = %.2f, xn = %.2f, n = %d\n", dx, dy, xn, n);
    144  1.1       cgd #	endif
    145  1.1       cgd 	Move.free = 0;
    146  1.1       cgd 
    147  1.1       cgd 	for (i = 0; i < n; i++)
    148  1.1       cgd 	{
    149  1.1       cgd 		ix = (x += dx);
    150  1.1       cgd 		iy = (y += dy);
    151  1.1       cgd #		ifdef xTRACE
    152  1.1       cgd 		if (Trace)
    153  1.1       cgd 			printf("ix = %d, x = %.2f, iy = %d, y = %.2f\n", ix, x, iy, y);
    154  1.1       cgd #		endif
    155  1.1       cgd 		if (x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize)
    156  1.1       cgd 		{
    157  1.1       cgd 			/* enter new quadrant */
    158  1.1       cgd 			dx = Ship.quadx * NSECTS + Ship.sectx + dx * xn;
    159  1.1       cgd 			dy = Ship.quady * NSECTS + Ship.secty + dy * xn;
    160  1.1       cgd 			if (dx < 0.0)
    161  1.1       cgd 				ix = -1;
    162  1.1       cgd 			else
    163  1.1       cgd 				ix = dx + 0.5;
    164  1.1       cgd 			if (dy < 0.0)
    165  1.1       cgd 				iy = -1;
    166  1.1       cgd 			else
    167  1.1       cgd 				iy = dy + 0.5;
    168  1.1       cgd #			ifdef xTRACE
    169  1.1       cgd 			if (Trace)
    170  1.1       cgd 				printf("New quad: ix = %d, iy = %d\n", ix, iy);
    171  1.1       cgd #			endif
    172  1.1       cgd 			Ship.sectx = x;
    173  1.1       cgd 			Ship.secty = y;
    174  1.1       cgd 			compkldist(0);
    175  1.1       cgd 			Move.newquad = 2;
    176  1.1       cgd 			attack(0);
    177  1.1       cgd 			checkcond();
    178  1.1       cgd 			Ship.quadx = ix / NSECTS;
    179  1.1       cgd 			Ship.quady = iy / NSECTS;
    180  1.1       cgd 			Ship.sectx = ix % NSECTS;
    181  1.1       cgd 			Ship.secty = iy % NSECTS;
    182  1.5     veego 			if (ix < 0 || Ship.quadx >= NQUADS || iy < 0 ||
    183  1.5     veego 			    Ship.quady >= NQUADS) {
    184  1.5     veego 				if (!damaged(COMPUTER)) {
    185  1.1       cgd 					dumpme(0);
    186  1.5     veego 				} else
    187  1.1       cgd 					lose(L_NEGENB);
    188  1.5     veego 			}
    189  1.1       cgd 			initquad(0);
    190  1.1       cgd 			n = 0;
    191  1.1       cgd 			break;
    192  1.1       cgd 		}
    193  1.1       cgd 		if (Sect[ix][iy] != EMPTY)
    194  1.1       cgd 		{
    195  1.1       cgd 			/* we just hit something */
    196  1.1       cgd 			if (!damaged(COMPUTER) && ramflag <= 0)
    197  1.1       cgd 			{
    198  1.1       cgd 				ix = x - dx;
    199  1.1       cgd 				iy = y - dy;
    200  1.1       cgd 				printf("Computer reports navigation error; %s stopped at %d,%d\n",
    201  1.1       cgd 					Ship.shipname, ix, iy);
    202  1.1       cgd 				Ship.energy -= Param.stopengy * speed;
    203  1.1       cgd 				break;
    204  1.1       cgd 			}
    205  1.1       cgd 			/* test for a black hole */
    206  1.1       cgd 			if (Sect[ix][iy] == HOLE)
    207  1.1       cgd 			{
    208  1.1       cgd 				/* get dumped elsewhere in the galaxy */
    209  1.1       cgd 				dumpme(1);
    210  1.1       cgd 				initquad(0);
    211  1.1       cgd 				n = 0;
    212  1.1       cgd 				break;
    213  1.1       cgd 			}
    214  1.1       cgd 			ram(ix, iy);
    215  1.1       cgd 			break;
    216  1.1       cgd 		}
    217  1.1       cgd 	}
    218  1.1       cgd 	if (n > 0)
    219  1.1       cgd 	{
    220  1.1       cgd 		dx = Ship.sectx - ix;
    221  1.1       cgd 		dy = Ship.secty - iy;
    222  1.1       cgd 		dist = sqrt(dx * dx + dy * dy) / NSECTS;
    223  1.1       cgd 		time = dist / speed;
    224  1.1       cgd 		if (evtime > time)
    225  1.1       cgd 			time = evtime;		/* spring the LRTB trap */
    226  1.1       cgd 		Ship.sectx = ix;
    227  1.1       cgd 		Ship.secty = iy;
    228  1.1       cgd 	}
    229  1.1       cgd 	Sect[Ship.sectx][Ship.secty] = Ship.ship;
    230  1.1       cgd 	compkldist(0);
    231  1.1       cgd 	return (time);
    232  1.1       cgd }
    233