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