Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / trek / warp.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. 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
35 #if 0
36 static char sccsid[] = "@(#)warp.c      8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/trek/warp.c,v 1.4 1999/11/30 03:49:56 billf Exp $";
40 #endif /* not lint */
41
42 # include       "trek.h"
43
44 /*
45 **  MOVE UNDER WARP POWER
46 **
47 **      This is both the "move" and the "ram" commands, differing
48 **      only in the flag 'fl'.  It is also used for automatic
49 **      emergency override mode, when 'fl' is < 0 and 'c' and 'd'
50 **      are the course and distance to be moved.  If 'fl' >= 0,
51 **      the course and distance are asked of the captain.
52 **
53 **      The guts of this routine are in the routine move(), which
54 **      is shared with impulse().  Also, the working part of this
55 **      routine is very small; the rest is to handle the slight chance
56 **      that you may be moving at some riduculous speed.  In that
57 **      case, there is code to handle time warps, etc.
58 */
59
60 warp(fl, c, d)
61 int     fl, c;
62 double  d;
63 {
64         int                     course;
65         double                  power;
66         double                  dist;
67         double                  time;
68         double                  speed;
69         double                  frac;
70         int             percent;
71         int             i;
72         extern double           move();
73
74         if (Ship.cond == DOCKED)
75                 return (printf("%s is docked\n", Ship.shipname));
76         if (damaged(WARP))
77         {
78                 return (out(WARP));
79         }
80         if (fl < 0)
81         {
82                 course = c;
83                 dist = d;
84         }
85         else
86                 if (getcodi(&course, &dist))
87                         return;
88
89         /* check to see that we are not using an absurd amount of power */
90         power = (dist + 0.05) * Ship.warp3;
91         percent = 100 * power / Ship.energy + 0.5;
92         if (percent >= 85)
93         {
94                 printf("Scotty: That would consume %d%% of our remaining energy.\n",
95                         percent);
96                 if (!getynpar("Are you sure that is wise"))
97                         return;
98         }
99
100         /* compute the speed we will move at, and the time it will take */
101         speed = Ship.warp2 / Param.warptime;
102         time = dist / speed;
103
104         /* check to see that that value is not ridiculous */
105         percent = 100 * time / Now.time + 0.5;
106         if (percent >= 85)
107         {
108                 printf("Spock: That would take %d%% of our remaining time.\n",
109                         percent);
110                 if (!getynpar("Are you sure that is wise"))
111                         return;
112         }
113
114         /* compute how far we will go if we get damages */
115         if (Ship.warp > 6.0 && ranf(100) < 20 + 15 * (Ship.warp - 6.0))
116         {
117                 frac = franf();
118                 dist *= frac;
119                 time *= frac;
120                 damage(WARP, (frac + 1.0) * Ship.warp * (franf() + 0.25) * 0.20);
121         }
122
123         /* do the move */
124         Move.time = move(fl, course, time, speed);
125
126         /* see how far we actually went, and decrement energy appropriately */
127         dist = Move.time * speed;
128         Ship.energy -= dist * Ship.warp3 * (Ship.shldup + 1);
129
130         /* test for bizarre events */
131         if (Ship.warp <= 9.0)
132                 return;
133         printf("\n\n  ___ Speed exceeding warp nine ___\n\n");
134         sleep(2);
135         printf("Ship's safety systems malfunction\n");
136         sleep(2);
137         printf("Crew experiencing extreme sensory distortion\n");
138         sleep(4);
139         if (ranf(100) >= 100 * dist)
140         {
141                 return (printf("Equilibrium restored -- all systems normal\n"));
142         }
143
144         /* select a bizzare thing to happen to us */
145         percent = ranf(100);
146         if (percent < 70)
147         {
148                 /* time warp */
149                 if (percent < 35 || !Game.snap)
150                 {
151                         /* positive time warp */
152                         time = (Ship.warp - 8.0) * dist * (franf() + 1.0);
153                         Now.date += time;
154                         printf("Positive time portal entered -- it is now Stardate %.2f\n",
155                                 Now.date);
156                         for (i = 0; i < MAXEVENTS; i++)
157                         {
158                                 percent = Event[i].evcode;
159                                 if (percent == E_FIXDV || percent == E_LRTB)
160                                         Event[i].date += time;
161                         }
162                         return;
163                 }
164
165                 /* s/he got lucky: a negative time portal */
166                 time = Now.date;
167                 i = (int) Etc.snapshot;
168                 bmove(i, Quad, sizeof Quad);
169                 bmove(i += sizeof Quad, Event, sizeof Event);
170                 bmove(i += sizeof Event, &Now, sizeof Now);
171                 printf("Negative time portal entered -- it is now Stardate %.2f\n",
172                         Now.date);
173                 for (i = 0; i < MAXEVENTS; i++)
174                         if (Event[i].evcode == E_FIXDV)
175                                 reschedule(&Event[i], Event[i].date - time);
176                 return;
177         }
178
179         /* test for just a lot of damage */
180         if (percent < 80)
181                 lose(L_TOOFAST);
182         printf("Equilibrium restored -- extreme damage occured to ship systems\n");
183         for (i = 0; i < NDEV; i++)
184                 damage(i, (3.0 * (franf() + franf()) + 1.0) * Param.damfac[i]);
185         Ship.shldup = 0;
186 }