Home | History | Annotate | Line # | Download | only in trek
warp.c revision 1.5
      1 /*	$NetBSD: warp.c,v 1.5 1997/10/13 22:21:14 cjs 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)warp.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: warp.c,v 1.5 1997/10/13 22:21:14 cjs Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <stdio.h>
     46 #include <math.h>
     47 #include <string.h>
     48 #include <unistd.h>
     49 #include "trek.h"
     50 #include "getpar.h"
     51 
     52 /*
     53 **  MOVE UNDER WARP POWER
     54 **
     55 **	This is both the "move" and the "ram" commands, differing
     56 **	only in the flag 'fl'.  It is also used for automatic
     57 **	emergency override mode, when 'fl' is < 0 and 'c' and 'd'
     58 **	are the course and distance to be moved.  If 'fl' >= 0,
     59 **	the course and distance are asked of the captain.
     60 **
     61 **	The guts of this routine are in the routine move(), which
     62 **	is shared with impulse().  Also, the working part of this
     63 **	routine is very small; the rest is to handle the slight chance
     64 **	that you may be moving at some riduculous speed.  In that
     65 **	case, there is code to handle time warps, etc.
     66 */
     67 
     68 void
     69 dowarp(fl)
     70 {
     71 	int c;
     72 	double d;
     73 
     74 	if (getcodi(&c, &d))
     75 		return;
     76 	warp(fl, c, d);
     77 }
     78 
     79 void
     80 warp(fl, c, d)
     81 int	fl, c;
     82 double	d;
     83 {
     84 	char	       *p;
     85 	int		course;
     86 	double		power;
     87 	double		dist;
     88 	double		time;
     89 	double		speed;
     90 	double		frac;
     91 	int		percent;
     92 	int		i;
     93 
     94 	if (Ship.cond == DOCKED) {
     95 		printf("%s is docked\n", Ship.shipname);
     96 		return;
     97 	}
     98 	if (damaged(WARP))
     99 	{
    100 		out(WARP);
    101 		return;
    102 	}
    103 
    104 	course = c;
    105 	dist = d;
    106 
    107 	/* check to see that we are not using an absurd amount of power */
    108 	power = (dist + 0.05) * Ship.warp3;
    109 	percent = 100 * power / Ship.energy + 0.5;
    110 	if (percent >= 85)
    111 	{
    112 		printf("Scotty: That would consume %d%% of our remaining energy.\n",
    113 			percent);
    114 		if (!getynpar("Are you sure that is wise"))
    115 			return;
    116 	}
    117 
    118 	/* compute the speed we will move at, and the time it will take */
    119 	speed = Ship.warp2 / Param.warptime;
    120 	time = dist / speed;
    121 
    122 	/* check to see that that value is not ridiculous */
    123 	percent = 100 * time / Now.time + 0.5;
    124 	if (percent >= 85)
    125 	{
    126 		printf("Spock: That would take %d%% of our remaining time.\n",
    127 			percent);
    128 		if (!getynpar("Are you sure that is wise"))
    129 			return;
    130 	}
    131 
    132 	/* compute how far we will go if we get damages */
    133 	if (Ship.warp > 6.0 && ranf(100) < 20 + 15 * (Ship.warp - 6.0))
    134 	{
    135 		frac = franf();
    136 		dist *= frac;
    137 		time *= frac;
    138 		damage(WARP, (frac + 1.0) * Ship.warp * (franf() + 0.25) * 0.20);
    139 	}
    140 
    141 	/* do the move */
    142 	Move.time = move(fl, course, time, speed);
    143 
    144 	/* see how far we actually went, and decrement energy appropriately */
    145 	dist = Move.time * speed;
    146 	Ship.energy -= dist * Ship.warp3 * (Ship.shldup + 1);
    147 
    148 	/* test for bizarre events */
    149 	if (Ship.warp <= 9.0)
    150 		return;
    151 	printf("\n\n  ___ Speed exceeding warp nine ___\n\n");
    152 	sleep(2);
    153 	printf("Ship's safety systems malfunction\n");
    154 	sleep(2);
    155 	printf("Crew experiencing extreme sensory distortion\n");
    156 	sleep(4);
    157 	if (ranf(100) >= 100 * dist)
    158 	{
    159 		printf("Equilibrium restored -- all systems normal\n");
    160 		return;
    161 	}
    162 
    163 	/* select a bizzare thing to happen to us */
    164 	percent = ranf(100);
    165 	if (percent < 70)
    166 	{
    167 		/* time warp */
    168 		if (percent < 35 || !Game.snap)
    169 		{
    170 			/* positive time warp */
    171 			time = (Ship.warp - 8.0) * dist * (franf() + 1.0);
    172 			Now.date += time;
    173 			printf("Positive time portal entered -- it is now Stardate %.2f\n",
    174 				Now.date);
    175 			for (i = 0; i < MAXEVENTS; i++)
    176 			{
    177 				percent = Event[i].evcode;
    178 				if (percent == E_FIXDV || percent == E_LRTB)
    179 					Event[i].date += time;
    180 			}
    181 			return;
    182 		}
    183 
    184 		/* s/he got lucky: a negative time portal */
    185 		time = Now.date;
    186 		p = (char *) Etc.snapshot;
    187 		memcpy(p, Quad, sizeof Quad);
    188 		p += sizeof Quad;
    189 		memcpy(p, Event, sizeof Event);
    190 		p += sizeof Event;
    191 		memcpy(p, &Now, sizeof Now);
    192 		printf("Negative time portal entered -- it is now Stardate %.2f\n",
    193 			Now.date);
    194 		for (i = 0; i < MAXEVENTS; i++)
    195 			if (Event[i].evcode == E_FIXDV)
    196 				reschedule(&Event[i], Event[i].date - time);
    197 		return;
    198 	}
    199 
    200 	/* test for just a lot of damage */
    201 	if (percent < 80)
    202 		lose(L_TOOFAST);
    203 	printf("Equilibrium restored -- extreme damage occured to ship systems\n");
    204 	for (i = 0; i < NDEV; i++)
    205 		damage(i, (3.0 * (franf() + franf()) + 1.0) * Param.damfac[i]);
    206 	Ship.shldup = 0;
    207 }
    208