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