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