Home | History | Annotate | Line # | Download | only in trek
      1  1.11       mrg /*	$NetBSD: move.c,v 1.11 2011/07/03 06:44:01 mrg 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.11       mrg __RCSID("$NetBSD: move.c,v 1.11 2011/07/03 06:44:01 mrg 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.11       mrg #include <float.h>
     44   1.4  christos #include "trek.h"
     45   1.1       cgd 
     46   1.1       cgd /*
     47   1.1       cgd **  Move Under Warp or Impulse Power
     48   1.1       cgd **
     49   1.1       cgd **	`Ramflag' is set if we are to be allowed to ram stars,
     50   1.1       cgd **	Klingons, etc.  This is passed from warp(), which gets it from
     51   1.1       cgd **	either play() or ram().  Course is the course (0 -> 360) at
     52   1.1       cgd **	which we want to move.  `Speed' is the speed we
     53   1.1       cgd **	want to go, and `time' is the expected time.  It
     54   1.1       cgd **	can get cut short if a long range tractor beam is to occur.  We
     55   1.1       cgd **	cut short the move so that the user doesn't get docked time and
     56   1.1       cgd **	energy for distance which he didn't travel.
     57   1.1       cgd **
     58   1.1       cgd **	We check the course through the current quadrant to see that he
     59   1.1       cgd **	doesn't run into anything.  After that, though, space sort of
     60   1.1       cgd **	bends around him.  Note that this puts us in the awkward posi-
     61   1.1       cgd **	tion of being able to be dropped into a sector which is com-
     62   1.1       cgd **	pletely surrounded by stars.  Oh Well.
     63   1.1       cgd **
     64   1.1       cgd **	If the SINS (Space Inertial Navigation System) is out, we ran-
     65   1.1       cgd **	domize the course accordingly before ever starting to move.
     66   1.1       cgd **	We will still move in a straight line.
     67   1.1       cgd **
     68   1.1       cgd **	Note that if your computer is out, you ram things anyway.  In
     69   1.1       cgd **	other words, if your computer and sins are both out, you're in
     70   1.1       cgd **	potentially very bad shape.
     71   1.1       cgd **
     72   1.1       cgd **	Klingons get a chance to zap you as you leave the quadrant.
     73   1.1       cgd **	By the way, they also try to follow you (heh heh).
     74   1.1       cgd **
     75   1.1       cgd **	Return value is the actual amount of time used.
     76   1.1       cgd **
     77   1.1       cgd **
     78   1.1       cgd **	Uses trace flag 4.
     79   1.1       cgd */
     80   1.1       cgd 
     81   1.7  dholland double
     82   1.7  dholland move(int ramflag, int course, double time, double speed)
     83   1.1       cgd {
     84   1.1       cgd 	double			angle;
     85   1.1       cgd 	double			x, y, dx, dy;
     86   1.4  christos 	int		ix = 0, iy = 0;
     87   1.1       cgd 	double			bigger;
     88   1.1       cgd 	int			n;
     89   1.4  christos 	int		i;
     90   1.1       cgd 	double			dist;
     91   1.1       cgd 	double			sectsize;
     92   1.1       cgd 	double			xn;
     93   1.1       cgd 	double			evtime;
     94   1.1       cgd 
     95   1.8  dholland #ifdef xTRACE
     96   1.1       cgd 	if (Trace)
     97   1.1       cgd 		printf("move: ramflag %d course %d time %.2f speed %.2f\n",
     98   1.1       cgd 			ramflag, course, time, speed);
     99   1.8  dholland #endif
    100   1.1       cgd 	sectsize = NSECTS;
    101   1.1       cgd 	/* initialize delta factors for move */
    102   1.1       cgd 	angle = course * 0.0174532925;
    103   1.1       cgd 	if (damaged(SINS))
    104   1.1       cgd 		angle += Param.navigcrud[1] * (franf() - 0.5);
    105   1.1       cgd 	else
    106   1.1       cgd 		if (Ship.sinsbad)
    107   1.1       cgd 			angle += Param.navigcrud[0] * (franf() - 0.5);
    108   1.1       cgd 	dx = -cos(angle);
    109   1.1       cgd 	dy = sin(angle);
    110   1.1       cgd 	bigger = fabs(dx);
    111   1.1       cgd 	dist = fabs(dy);
    112   1.1       cgd 	if (dist > bigger)
    113   1.1       cgd 		bigger = dist;
    114   1.1       cgd 	dx /= bigger;
    115   1.1       cgd 	dy /= bigger;
    116   1.1       cgd 
    117   1.1       cgd 	/* check for long range tractor beams */
    118   1.1       cgd 	/****  TEMPORARY CODE == DEBUGGING  ****/
    119   1.1       cgd 	evtime = Now.eventptr[E_LRTB]->date - Now.date;
    120   1.8  dholland #ifdef xTRACE
    121   1.1       cgd 	if (Trace)
    122  1.10  dholland 		printf("E.ep = %p, ->evcode = %d, ->date = %.2f, "
    123  1.10  dholland 		       "evtime = %.2f\n",
    124   1.1       cgd 			Now.eventptr[E_LRTB], Now.eventptr[E_LRTB]->evcode,
    125   1.1       cgd 			Now.eventptr[E_LRTB]->date, evtime);
    126   1.8  dholland #endif
    127   1.9  dholland 	if (time > evtime && Etc.nkling < 3) {
    128   1.1       cgd 		/* then we got a LRTB */
    129   1.1       cgd 		evtime += 0.005;
    130   1.1       cgd 		time = evtime;
    131   1.9  dholland 	} else
    132  1.11       mrg 		evtime = DBL_MIN;
    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.8  dholland #ifdef xTRACE
    142   1.1       cgd 	if (Trace)
    143  1.10  dholland 		printf("dx = %.2f, dy = %.2f, xn = %.2f, n = %d\n",
    144  1.10  dholland 			dx, dy, xn, n);
    145   1.8  dholland #endif
    146   1.1       cgd 	Move.free = 0;
    147   1.1       cgd 
    148   1.9  dholland 	for (i = 0; i < n; i++) {
    149   1.1       cgd 		ix = (x += dx);
    150   1.1       cgd 		iy = (y += dy);
    151   1.8  dholland #ifdef xTRACE
    152   1.1       cgd 		if (Trace)
    153  1.10  dholland 			printf("ix = %d, x = %.2f, iy = %d, y = %.2f\n",
    154  1.10  dholland 				ix, x, iy, y);
    155   1.8  dholland #endif
    156   1.9  dholland 		if (x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize) {
    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.8  dholland #ifdef xTRACE
    169   1.1       cgd 			if (Trace)
    170   1.1       cgd 				printf("New quad: ix = %d, iy = %d\n", ix, iy);
    171   1.8  dholland #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.9  dholland 		if (Sect[ix][iy] != EMPTY) {
    194   1.1       cgd 			/* we just hit something */
    195   1.9  dholland 			if (!damaged(COMPUTER) && ramflag <= 0) {
    196   1.1       cgd 				ix = x - dx;
    197   1.1       cgd 				iy = y - dy;
    198  1.10  dholland 				printf("Computer reports navigation error; "
    199  1.10  dholland 				       "%s stopped at %d,%d\n",
    200   1.1       cgd 					Ship.shipname, ix, iy);
    201   1.1       cgd 				Ship.energy -= Param.stopengy * speed;
    202   1.1       cgd 				break;
    203   1.1       cgd 			}
    204   1.1       cgd 			/* test for a black hole */
    205   1.9  dholland 			if (Sect[ix][iy] == HOLE) {
    206   1.1       cgd 				/* get dumped elsewhere in the galaxy */
    207   1.1       cgd 				dumpme(1);
    208   1.1       cgd 				initquad(0);
    209   1.1       cgd 				n = 0;
    210   1.1       cgd 				break;
    211   1.1       cgd 			}
    212   1.1       cgd 			ram(ix, iy);
    213   1.1       cgd 			break;
    214   1.1       cgd 		}
    215   1.1       cgd 	}
    216   1.9  dholland 	if (n > 0) {
    217   1.1       cgd 		dx = Ship.sectx - ix;
    218   1.1       cgd 		dy = Ship.secty - iy;
    219   1.1       cgd 		dist = sqrt(dx * dx + dy * dy) / NSECTS;
    220   1.1       cgd 		time = dist / speed;
    221   1.9  dholland 		if (evtime > time) {
    222   1.9  dholland 			/* spring the LRTB trap */
    223   1.9  dholland 			time = evtime;
    224   1.9  dholland 		}
    225   1.1       cgd 		Ship.sectx = ix;
    226   1.1       cgd 		Ship.secty = iy;
    227   1.1       cgd 	}
    228   1.1       cgd 	Sect[Ship.sectx][Ship.secty] = Ship.ship;
    229   1.1       cgd 	compkldist(0);
    230   1.1       cgd 	return (time);
    231   1.1       cgd }
    232