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