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