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