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