move.c revision 1.3 1 1.3 cgd /* $NetBSD: move.c,v 1.3 1995/04/22 10:59:12 cgd 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.3 cgd #if 0
38 1.3 cgd static char sccsid[] = "@(#)move.c 8.1 (Berkeley) 5/31/93";
39 1.3 cgd #else
40 1.3 cgd static char rcsid[] = "$NetBSD: move.c,v 1.3 1995/04/22 10:59:12 cgd Exp $";
41 1.3 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd # 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.1 cgd double move(ramflag, course, time, speed)
82 1.1 cgd int ramflag;
83 1.1 cgd int course;
84 1.1 cgd double time;
85 1.1 cgd double speed;
86 1.1 cgd {
87 1.1 cgd double angle;
88 1.1 cgd double x, y, dx, dy;
89 1.1 cgd register int ix, iy;
90 1.1 cgd double bigger;
91 1.1 cgd int n;
92 1.1 cgd register int i;
93 1.1 cgd double dist;
94 1.1 cgd double sectsize;
95 1.1 cgd double xn;
96 1.1 cgd double evtime;
97 1.1 cgd
98 1.1 cgd # ifdef xTRACE
99 1.1 cgd if (Trace)
100 1.1 cgd printf("move: ramflag %d course %d time %.2f speed %.2f\n",
101 1.1 cgd ramflag, course, time, speed);
102 1.1 cgd # endif
103 1.1 cgd sectsize = NSECTS;
104 1.1 cgd /* initialize delta factors for move */
105 1.1 cgd angle = course * 0.0174532925;
106 1.1 cgd if (damaged(SINS))
107 1.1 cgd angle += Param.navigcrud[1] * (franf() - 0.5);
108 1.1 cgd else
109 1.1 cgd if (Ship.sinsbad)
110 1.1 cgd angle += Param.navigcrud[0] * (franf() - 0.5);
111 1.1 cgd dx = -cos(angle);
112 1.1 cgd dy = sin(angle);
113 1.1 cgd bigger = fabs(dx);
114 1.1 cgd dist = fabs(dy);
115 1.1 cgd if (dist > bigger)
116 1.1 cgd bigger = dist;
117 1.1 cgd dx /= bigger;
118 1.1 cgd dy /= bigger;
119 1.1 cgd
120 1.1 cgd /* check for long range tractor beams */
121 1.1 cgd /**** TEMPORARY CODE == DEBUGGING ****/
122 1.1 cgd evtime = Now.eventptr[E_LRTB]->date - Now.date;
123 1.1 cgd # ifdef xTRACE
124 1.1 cgd if (Trace)
125 1.1 cgd printf("E.ep = %u, ->evcode = %d, ->date = %.2f, evtime = %.2f\n",
126 1.1 cgd Now.eventptr[E_LRTB], Now.eventptr[E_LRTB]->evcode,
127 1.1 cgd Now.eventptr[E_LRTB]->date, evtime);
128 1.1 cgd # endif
129 1.1 cgd if (time > evtime && Etc.nkling < 3)
130 1.1 cgd {
131 1.1 cgd /* then we got a LRTB */
132 1.1 cgd evtime += 0.005;
133 1.1 cgd time = evtime;
134 1.1 cgd }
135 1.1 cgd else
136 1.1 cgd evtime = -1.0e50;
137 1.1 cgd dist = time * speed;
138 1.1 cgd
139 1.1 cgd /* move within quadrant */
140 1.1 cgd Sect[Ship.sectx][Ship.secty] = EMPTY;
141 1.1 cgd x = Ship.sectx + 0.5;
142 1.1 cgd y = Ship.secty + 0.5;
143 1.1 cgd xn = NSECTS * dist * bigger;
144 1.1 cgd n = xn + 0.5;
145 1.1 cgd # ifdef xTRACE
146 1.1 cgd if (Trace)
147 1.1 cgd printf("dx = %.2f, dy = %.2f, xn = %.2f, n = %d\n", dx, dy, xn, n);
148 1.1 cgd # endif
149 1.1 cgd Move.free = 0;
150 1.1 cgd
151 1.1 cgd for (i = 0; i < n; i++)
152 1.1 cgd {
153 1.1 cgd ix = (x += dx);
154 1.1 cgd iy = (y += dy);
155 1.1 cgd # ifdef xTRACE
156 1.1 cgd if (Trace)
157 1.1 cgd printf("ix = %d, x = %.2f, iy = %d, y = %.2f\n", ix, x, iy, y);
158 1.1 cgd # endif
159 1.1 cgd if (x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize)
160 1.1 cgd {
161 1.1 cgd /* enter new quadrant */
162 1.1 cgd dx = Ship.quadx * NSECTS + Ship.sectx + dx * xn;
163 1.1 cgd dy = Ship.quady * NSECTS + Ship.secty + dy * xn;
164 1.1 cgd if (dx < 0.0)
165 1.1 cgd ix = -1;
166 1.1 cgd else
167 1.1 cgd ix = dx + 0.5;
168 1.1 cgd if (dy < 0.0)
169 1.1 cgd iy = -1;
170 1.1 cgd else
171 1.1 cgd iy = dy + 0.5;
172 1.1 cgd # ifdef xTRACE
173 1.1 cgd if (Trace)
174 1.1 cgd printf("New quad: ix = %d, iy = %d\n", ix, iy);
175 1.1 cgd # endif
176 1.1 cgd Ship.sectx = x;
177 1.1 cgd Ship.secty = y;
178 1.1 cgd compkldist(0);
179 1.1 cgd Move.newquad = 2;
180 1.1 cgd attack(0);
181 1.1 cgd checkcond();
182 1.1 cgd Ship.quadx = ix / NSECTS;
183 1.1 cgd Ship.quady = iy / NSECTS;
184 1.1 cgd Ship.sectx = ix % NSECTS;
185 1.1 cgd Ship.secty = iy % NSECTS;
186 1.1 cgd if (ix < 0 || Ship.quadx >= NQUADS || iy < 0 || Ship.quady >= NQUADS)
187 1.1 cgd if (!damaged(COMPUTER))
188 1.1 cgd {
189 1.1 cgd dumpme(0);
190 1.1 cgd }
191 1.1 cgd else
192 1.1 cgd lose(L_NEGENB);
193 1.1 cgd initquad(0);
194 1.1 cgd n = 0;
195 1.1 cgd break;
196 1.1 cgd }
197 1.1 cgd if (Sect[ix][iy] != EMPTY)
198 1.1 cgd {
199 1.1 cgd /* we just hit something */
200 1.1 cgd if (!damaged(COMPUTER) && ramflag <= 0)
201 1.1 cgd {
202 1.1 cgd ix = x - dx;
203 1.1 cgd iy = y - dy;
204 1.1 cgd printf("Computer reports navigation error; %s stopped at %d,%d\n",
205 1.1 cgd Ship.shipname, ix, iy);
206 1.1 cgd Ship.energy -= Param.stopengy * speed;
207 1.1 cgd break;
208 1.1 cgd }
209 1.1 cgd /* test for a black hole */
210 1.1 cgd if (Sect[ix][iy] == HOLE)
211 1.1 cgd {
212 1.1 cgd /* get dumped elsewhere in the galaxy */
213 1.1 cgd dumpme(1);
214 1.1 cgd initquad(0);
215 1.1 cgd n = 0;
216 1.1 cgd break;
217 1.1 cgd }
218 1.1 cgd ram(ix, iy);
219 1.1 cgd break;
220 1.1 cgd }
221 1.1 cgd }
222 1.1 cgd if (n > 0)
223 1.1 cgd {
224 1.1 cgd dx = Ship.sectx - ix;
225 1.1 cgd dy = Ship.secty - iy;
226 1.1 cgd dist = sqrt(dx * dx + dy * dy) / NSECTS;
227 1.1 cgd time = dist / speed;
228 1.1 cgd if (evtime > time)
229 1.1 cgd time = evtime; /* spring the LRTB trap */
230 1.1 cgd Ship.sectx = ix;
231 1.1 cgd Ship.secty = iy;
232 1.1 cgd }
233 1.1 cgd Sect[Ship.sectx][Ship.secty] = Ship.ship;
234 1.1 cgd compkldist(0);
235 1.1 cgd return (time);
236 1.1 cgd }
237