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