sbin/hammer2/cmd_debug.c: Clear errno
[dragonfly.git] / games / trek / move.c
blob8af941dcce75b53af994dd55021746b042c596bc
1 /* @(#)move.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: move.c,v 1.11 2011/07/03 06:44:01 mrg Exp $ */
4 /*
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
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.
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.
33 #include <stdio.h>
34 #include <math.h>
35 #include <float.h>
36 #include "trek.h"
39 ** Move Under Warp or Impulse Power
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.
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.
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.
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.
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).
67 ** Return value is the actual amount of time used.
70 ** Uses trace flag 4.
73 double
74 move(int ramflag, int course, double time, double speed)
76 double angle;
77 double x, y, dx, dy;
78 int ix = 0, iy = 0;
79 double bigger;
80 int n;
81 int i;
82 double dist;
83 double sectsize;
84 double xn;
85 double evtime;
87 #ifdef xTRACE
88 if (Trace)
89 printf("move: ramflag %d course %d time %.2f speed %.2f\n",
90 ramflag, course, time, speed);
91 #endif
92 sectsize = NSECTS;
93 /* initialize delta factors for move */
94 angle = course * 0.0174532925;
95 if (damaged(SINS))
96 angle += Param.navigcrud[1] * (franf() - 0.5);
97 else
98 if (Ship.sinsbad)
99 angle += Param.navigcrud[0] * (franf() - 0.5);
100 dx = -cos(angle);
101 dy = sin(angle);
102 bigger = fabs(dx);
103 dist = fabs(dy);
104 if (dist > bigger)
105 bigger = dist;
106 dx /= bigger;
107 dy /= bigger;
109 /* check for long range tractor beams */
110 /**** TEMPORARY CODE == DEBUGGING ****/
111 evtime = Now.eventptr[E_LRTB]->date - Now.date;
112 #ifdef xTRACE
113 if (Trace)
114 printf("E.ep = %p, ->evcode = %d, ->date = %.2f, "
115 "evtime = %.2f\n",
116 Now.eventptr[E_LRTB], Now.eventptr[E_LRTB]->evcode,
117 Now.eventptr[E_LRTB]->date, evtime);
118 #endif
119 if (time > evtime && Etc.nkling < 3) {
120 /* then we got a LRTB */
121 evtime += 0.005;
122 time = evtime;
123 } else
124 evtime = DBL_MIN;
125 dist = time * speed;
127 /* move within quadrant */
128 Sect[Ship.sectx][Ship.secty] = EMPTY;
129 x = Ship.sectx + 0.5;
130 y = Ship.secty + 0.5;
131 xn = NSECTS * dist * bigger;
132 n = xn + 0.5;
133 #ifdef xTRACE
134 if (Trace)
135 printf("dx = %.2f, dy = %.2f, xn = %.2f, n = %d\n",
136 dx, dy, xn, n);
137 #endif
138 Move.free = 0;
140 for (i = 0; i < n; i++) {
141 ix = (x += dx);
142 iy = (y += dy);
143 #ifdef xTRACE
144 if (Trace)
145 printf("ix = %d, x = %.2f, iy = %d, y = %.2f\n",
146 ix, x, iy, y);
147 #endif
148 if (x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize) {
149 /* enter new quadrant */
150 dx = Ship.quadx * NSECTS + Ship.sectx + dx * xn;
151 dy = Ship.quady * NSECTS + Ship.secty + dy * xn;
152 if (dx < 0.0)
153 ix = -1;
154 else
155 ix = dx + 0.5;
156 if (dy < 0.0)
157 iy = -1;
158 else
159 iy = dy + 0.5;
160 #ifdef xTRACE
161 if (Trace)
162 printf("New quad: ix = %d, iy = %d\n", ix, iy);
163 #endif
164 Ship.sectx = x;
165 Ship.secty = y;
166 compkldist(0);
167 Move.newquad = 2;
168 attack(0);
169 checkcond();
170 Ship.quadx = ix / NSECTS;
171 Ship.quady = iy / NSECTS;
172 Ship.sectx = ix % NSECTS;
173 Ship.secty = iy % NSECTS;
174 if (ix < 0 || Ship.quadx >= NQUADS || iy < 0 ||
175 Ship.quady >= NQUADS) {
176 if (!damaged(COMPUTER)) {
177 dumpme(0);
178 } else
179 lose(L_NEGENB);
181 initquad(0);
182 n = 0;
183 break;
185 if (Sect[ix][iy] != EMPTY) {
186 /* we just hit something */
187 if (!damaged(COMPUTER) && ramflag <= 0) {
188 ix = x - dx;
189 iy = y - dy;
190 printf("Computer reports navigation error; "
191 "%s stopped at %d,%d\n",
192 Ship.shipname, ix, iy);
193 Ship.energy -= Param.stopengy * speed;
194 break;
196 /* test for a black hole */
197 if (Sect[ix][iy] == HOLE) {
198 /* get dumped elsewhere in the galaxy */
199 dumpme(1);
200 initquad(0);
201 n = 0;
202 break;
204 ram(ix, iy);
205 break;
208 if (n > 0) {
209 dx = Ship.sectx - ix;
210 dy = Ship.secty - iy;
211 dist = sqrt(dx * dx + dy * dy) / NSECTS;
212 time = dist / speed;
213 if (evtime > time) {
214 /* spring the LRTB trap */
215 time = evtime;
217 Ship.sectx = ix;
218 Ship.secty = iy;
220 Sect[Ship.sectx][Ship.secty] = Ship.ship;
221 compkldist(0);
222 return (time);