Home | History | Annotate | Line # | Download | only in trek
move.c revision 1.6
      1  1.6       agc /*	$NetBSD: move.c,v 1.6 2003/08/07 09:37:52 agc 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.6       agc __RCSID("$NetBSD: move.c,v 1.6 2003/08/07 09:37:52 agc 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.1       cgd double move(ramflag, course, time, speed)
     81  1.1       cgd int	ramflag;
     82  1.1       cgd int	course;
     83  1.1       cgd double	time;
     84  1.1       cgd double	speed;
     85  1.1       cgd {
     86  1.1       cgd 	double			angle;
     87  1.1       cgd 	double			x, y, dx, dy;
     88  1.4  christos 	int		ix = 0, iy = 0;
     89  1.1       cgd 	double			bigger;
     90  1.1       cgd 	int			n;
     91  1.4  christos 	int		i;
     92  1.1       cgd 	double			dist;
     93  1.1       cgd 	double			sectsize;
     94  1.1       cgd 	double			xn;
     95  1.1       cgd 	double			evtime;
     96  1.1       cgd 
     97  1.1       cgd #	ifdef xTRACE
     98  1.1       cgd 	if (Trace)
     99  1.1       cgd 		printf("move: ramflag %d course %d time %.2f speed %.2f\n",
    100  1.1       cgd 			ramflag, course, time, speed);
    101  1.1       cgd #	endif
    102  1.1       cgd 	sectsize = NSECTS;
    103  1.1       cgd 	/* initialize delta factors for move */
    104  1.1       cgd 	angle = course * 0.0174532925;
    105  1.1       cgd 	if (damaged(SINS))
    106  1.1       cgd 		angle += Param.navigcrud[1] * (franf() - 0.5);
    107  1.1       cgd 	else
    108  1.1       cgd 		if (Ship.sinsbad)
    109  1.1       cgd 			angle += Param.navigcrud[0] * (franf() - 0.5);
    110  1.1       cgd 	dx = -cos(angle);
    111  1.1       cgd 	dy = sin(angle);
    112  1.1       cgd 	bigger = fabs(dx);
    113  1.1       cgd 	dist = fabs(dy);
    114  1.1       cgd 	if (dist > bigger)
    115  1.1       cgd 		bigger = dist;
    116  1.1       cgd 	dx /= bigger;
    117  1.1       cgd 	dy /= bigger;
    118  1.1       cgd 
    119  1.1       cgd 	/* check for long range tractor beams */
    120  1.1       cgd 	/****  TEMPORARY CODE == DEBUGGING  ****/
    121  1.1       cgd 	evtime = Now.eventptr[E_LRTB]->date - Now.date;
    122  1.1       cgd #	ifdef xTRACE
    123  1.1       cgd 	if (Trace)
    124  1.4  christos 		printf("E.ep = %p, ->evcode = %d, ->date = %.2f, evtime = %.2f\n",
    125  1.1       cgd 			Now.eventptr[E_LRTB], Now.eventptr[E_LRTB]->evcode,
    126  1.1       cgd 			Now.eventptr[E_LRTB]->date, evtime);
    127  1.1       cgd #	endif
    128  1.1       cgd 	if (time > evtime && Etc.nkling < 3)
    129  1.1       cgd 	{
    130  1.1       cgd 		/* then we got a LRTB */
    131  1.1       cgd 		evtime += 0.005;
    132  1.1       cgd 		time = evtime;
    133  1.1       cgd 	}
    134  1.1       cgd 	else
    135  1.1       cgd 		evtime = -1.0e50;
    136  1.1       cgd 	dist = time * speed;
    137  1.1       cgd 
    138  1.1       cgd 	/* move within quadrant */
    139  1.1       cgd 	Sect[Ship.sectx][Ship.secty] = EMPTY;
    140  1.1       cgd 	x = Ship.sectx + 0.5;
    141  1.1       cgd 	y = Ship.secty + 0.5;
    142  1.1       cgd 	xn = NSECTS * dist * bigger;
    143  1.1       cgd 	n = xn + 0.5;
    144  1.1       cgd #	ifdef xTRACE
    145  1.1       cgd 	if (Trace)
    146  1.1       cgd 		printf("dx = %.2f, dy = %.2f, xn = %.2f, n = %d\n", dx, dy, xn, n);
    147  1.1       cgd #	endif
    148  1.1       cgd 	Move.free = 0;
    149  1.1       cgd 
    150  1.1       cgd 	for (i = 0; i < n; i++)
    151  1.1       cgd 	{
    152  1.1       cgd 		ix = (x += dx);
    153  1.1       cgd 		iy = (y += dy);
    154  1.1       cgd #		ifdef xTRACE
    155  1.1       cgd 		if (Trace)
    156  1.1       cgd 			printf("ix = %d, x = %.2f, iy = %d, y = %.2f\n", ix, x, iy, y);
    157  1.1       cgd #		endif
    158  1.1       cgd 		if (x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize)
    159  1.1       cgd 		{
    160  1.1       cgd 			/* enter new quadrant */
    161  1.1       cgd 			dx = Ship.quadx * NSECTS + Ship.sectx + dx * xn;
    162  1.1       cgd 			dy = Ship.quady * NSECTS + Ship.secty + dy * xn;
    163  1.1       cgd 			if (dx < 0.0)
    164  1.1       cgd 				ix = -1;
    165  1.1       cgd 			else
    166  1.1       cgd 				ix = dx + 0.5;
    167  1.1       cgd 			if (dy < 0.0)
    168  1.1       cgd 				iy = -1;
    169  1.1       cgd 			else
    170  1.1       cgd 				iy = dy + 0.5;
    171  1.1       cgd #			ifdef xTRACE
    172  1.1       cgd 			if (Trace)
    173  1.1       cgd 				printf("New quad: ix = %d, iy = %d\n", ix, iy);
    174  1.1       cgd #			endif
    175  1.1       cgd 			Ship.sectx = x;
    176  1.1       cgd 			Ship.secty = y;
    177  1.1       cgd 			compkldist(0);
    178  1.1       cgd 			Move.newquad = 2;
    179  1.1       cgd 			attack(0);
    180  1.1       cgd 			checkcond();
    181  1.1       cgd 			Ship.quadx = ix / NSECTS;
    182  1.1       cgd 			Ship.quady = iy / NSECTS;
    183  1.1       cgd 			Ship.sectx = ix % NSECTS;
    184  1.1       cgd 			Ship.secty = iy % NSECTS;
    185  1.5     veego 			if (ix < 0 || Ship.quadx >= NQUADS || iy < 0 ||
    186  1.5     veego 			    Ship.quady >= NQUADS) {
    187  1.5     veego 				if (!damaged(COMPUTER)) {
    188  1.1       cgd 					dumpme(0);
    189  1.5     veego 				} else
    190  1.1       cgd 					lose(L_NEGENB);
    191  1.5     veego 			}
    192  1.1       cgd 			initquad(0);
    193  1.1       cgd 			n = 0;
    194  1.1       cgd 			break;
    195  1.1       cgd 		}
    196  1.1       cgd 		if (Sect[ix][iy] != EMPTY)
    197  1.1       cgd 		{
    198  1.1       cgd 			/* we just hit something */
    199  1.1       cgd 			if (!damaged(COMPUTER) && ramflag <= 0)
    200  1.1       cgd 			{
    201  1.1       cgd 				ix = x - dx;
    202  1.1       cgd 				iy = y - dy;
    203  1.1       cgd 				printf("Computer reports navigation error; %s stopped at %d,%d\n",
    204  1.1       cgd 					Ship.shipname, ix, iy);
    205  1.1       cgd 				Ship.energy -= Param.stopengy * speed;
    206  1.1       cgd 				break;
    207  1.1       cgd 			}
    208  1.1       cgd 			/* test for a black hole */
    209  1.1       cgd 			if (Sect[ix][iy] == HOLE)
    210  1.1       cgd 			{
    211  1.1       cgd 				/* get dumped elsewhere in the galaxy */
    212  1.1       cgd 				dumpme(1);
    213  1.1       cgd 				initquad(0);
    214  1.1       cgd 				n = 0;
    215  1.1       cgd 				break;
    216  1.1       cgd 			}
    217  1.1       cgd 			ram(ix, iy);
    218  1.1       cgd 			break;
    219  1.1       cgd 		}
    220  1.1       cgd 	}
    221  1.1       cgd 	if (n > 0)
    222  1.1       cgd 	{
    223  1.1       cgd 		dx = Ship.sectx - ix;
    224  1.1       cgd 		dy = Ship.secty - iy;
    225  1.1       cgd 		dist = sqrt(dx * dx + dy * dy) / NSECTS;
    226  1.1       cgd 		time = dist / speed;
    227  1.1       cgd 		if (evtime > time)
    228  1.1       cgd 			time = evtime;		/* spring the LRTB trap */
    229  1.1       cgd 		Ship.sectx = ix;
    230  1.1       cgd 		Ship.secty = iy;
    231  1.1       cgd 	}
    232  1.1       cgd 	Sect[Ship.sectx][Ship.secty] = Ship.ship;
    233  1.1       cgd 	compkldist(0);
    234  1.1       cgd 	return (time);
    235  1.1       cgd }
    236