1/*	$OpenBSD: warp.c,v 1.8 2016/01/07 14:37:51 mestre Exp $	*/
2/*	$NetBSD: warp.c,v 1.3 1995/04/22 10:59:40 cgd Exp $	*/
3
4/*
5 * Copyright (c) 1980, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <stdio.h>
34#include <string.h>
35#include <unistd.h>
36
37#include "getpar.h"
38#include "trek.h"
39
40/*
41**  MOVE UNDER WARP POWER
42**
43**	This is both the "move" and the "ram" commands, differing
44**	only in the flag 'fl'.  It is also used for automatic
45**	emergency override mode, when 'fl' is < 0 and 'c' and 'd'
46**	are the course and distance to be moved.  If 'fl' >= 0,
47**	the course and distance are asked of the captain.
48**
49**	The guts of this routine are in the routine move(), which
50**	is shared with impulse().  Also, the working part of this
51**	routine is very small; the rest is to handle the slight chance
52**	that you may be moving at some riduculous speed.  In that
53**	case, there is code to handle time warps, etc.
54*/
55
56void
57dowarp(int fl)
58{
59	int	c;
60	double	d;
61
62	if (getcodi(&c, &d))
63		return;
64	warp(fl, c, d);
65}
66
67void
68warp(int fl, int c, double d)
69{
70	char	*p;
71	double	power, dist, time, speed, frac;
72	int	course, percent, i;
73
74	if (Ship.cond == DOCKED)
75	{
76		printf("%s is docked\n", Ship.shipname);
77		return;
78	}
79	if (damaged(WARP))
80	{
81		out(WARP);
82		return;
83	}
84
85	course = c;
86	dist = d;
87
88	/* check to see that we are not using an absurd amount of power */
89	power = (dist + 0.05) * Ship.warp3;
90	percent = 100 * power / Ship.energy + 0.5;
91	if (percent >= 85)
92	{
93		printf("Scotty: That would consume %d%% of our remaining energy.\n",
94			percent);
95		if (!getynpar("Are you sure that is wise"))
96			return;
97	}
98
99	/* compute the speed we will move at, and the time it will take */
100	speed = Ship.warp2 / Param.warptime;
101	time = dist / speed;
102
103	/* check to see that that value is not ridiculous */
104	percent = 100 * time / Now.time + 0.5;
105	if (percent >= 85)
106	{
107		printf("Spock: That would take %d%% of our remaining time.\n",
108			percent);
109		if (!getynpar("Are you sure that is wise"))
110			return;
111	}
112
113	/* compute how far we will go if we get damages */
114	if (Ship.warp > 6.0 && ranf(100) < 20 + 15 * (Ship.warp - 6.0))
115	{
116		frac = franf();
117		dist *= frac;
118		time *= frac;
119		damage(WARP, (frac + 1.0) * Ship.warp * (franf() + 0.25) * 0.20);
120	}
121
122	/* do the move */
123	Move.time = move(fl, course, time, speed);
124
125	/* see how far we actually went, and decrement energy appropriately */
126	dist = Move.time * speed;
127	Ship.energy -= dist * Ship.warp3 * (Ship.shldup + 1);
128
129	/* test for bizarre events */
130	if (Ship.warp <= 9.0)
131		return;
132	printf("\n\n  ___ Speed exceeding warp nine ___\n\n");
133	sleep(2);
134	printf("Ship's safety systems malfunction\n");
135	sleep(2);
136	printf("Crew experiencing extreme sensory distortion\n");
137	sleep(4);
138	if (ranf(100) >= 100 * dist)
139	{
140		printf("Equilibrium restored -- all systems normal\n");
141		return;
142	}
143
144	/* select a bizarre 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		p = (char *) Etc.snapshot;
168		memcpy(p, Quad, sizeof Quad);
169		p += sizeof Quad;
170		memcpy(p, Event, sizeof Event);
171		p += sizeof Event;
172		memcpy(p, &Now, sizeof Now);
173		printf("Negative time portal entered -- it is now Stardate %.2f\n",
174			Now.date);
175		for (i = 0; i < MAXEVENTS; i++)
176			if (Event[i].evcode == E_FIXDV)
177				reschedule(&Event[i], Event[i].date - time);
178		return;
179	}
180
181	/* test for just a lot of damage */
182	if (percent < 80)
183		lose(L_TOOFAST);
184	printf("Equilibrium restored -- extreme damage occurred to ship systems\n");
185	for (i = 0; i < NDEV; i++)
186		damage(i, (3.0 * (franf() + franf()) + 1.0) * Param.damfac[i]);
187	Ship.shldup = 0;
188}
189