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