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