xref: /original-bsd/games/trek/move.c (revision 2ce9ec30)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)move.c	5.2 (Berkeley) 05/05/88";
15 #endif /* not lint */
16 
17 # include	"trek.h"
18 
19 /*
20 **  Move Under Warp or Impulse Power
21 **
22 **	`Ramflag' is set if we are to be allowed to ram stars,
23 **	Klingons, etc.  This is passed from warp(), which gets it from
24 **	either play() or ram().  Course is the course (0 -> 360) at
25 **	which we want to move.  `Speed' is the speed we
26 **	want to go, and `time' is the expected time.  It
27 **	can get cut short if a long range tractor beam is to occur.  We
28 **	cut short the move so that the user doesn't get docked time and
29 **	energy for distance which he didn't travel.
30 **
31 **	We check the course through the current quadrant to see that he
32 **	doesn't run into anything.  After that, though, space sort of
33 **	bends around him.  Note that this puts us in the awkward posi-
34 **	tion of being able to be dropped into a sector which is com-
35 **	pletely surrounded by stars.  Oh Well.
36 **
37 **	If the SINS (Space Inertial Navigation System) is out, we ran-
38 **	domize the course accordingly before ever starting to move.
39 **	We will still move in a straight line.
40 **
41 **	Note that if your computer is out, you ram things anyway.  In
42 **	other words, if your computer and sins are both out, you're in
43 **	potentially very bad shape.
44 **
45 **	Klingons get a chance to zap you as you leave the quadrant.
46 **	By the way, they also try to follow you (heh heh).
47 **
48 **	Return value is the actual amount of time used.
49 **
50 **
51 **	Uses trace flag 4.
52 */
53 
54 double move(ramflag, course, time, speed)
55 int	ramflag;
56 int	course;
57 double	time;
58 double	speed;
59 {
60 	double			angle;
61 	double			x, y, dx, dy;
62 	register int		ix, iy;
63 	double			bigger;
64 	int			n;
65 	register int		i;
66 	double			dist;
67 	double			sectsize;
68 	double			xn;
69 	double			evtime;
70 
71 #	ifdef xTRACE
72 	if (Trace)
73 		printf("move: ramflag %d course %d time %.2f speed %.2f\n",
74 			ramflag, course, time, speed);
75 #	endif
76 	sectsize = NSECTS;
77 	/* initialize delta factors for move */
78 	angle = course * 0.0174532925;
79 	if (damaged(SINS))
80 		angle += Param.navigcrud[1] * (franf() - 0.5);
81 	else
82 		if (Ship.sinsbad)
83 			angle += Param.navigcrud[0] * (franf() - 0.5);
84 	dx = -cos(angle);
85 	dy = sin(angle);
86 	bigger = fabs(dx);
87 	dist = fabs(dy);
88 	if (dist > bigger)
89 		bigger = dist;
90 	dx /= bigger;
91 	dy /= bigger;
92 
93 	/* check for long range tractor beams */
94 	/****  TEMPORARY CODE == DEBUGGING  ****/
95 	evtime = Now.eventptr[E_LRTB]->date - Now.date;
96 #	ifdef xTRACE
97 	if (Trace)
98 		printf("E.ep = %u, ->evcode = %d, ->date = %.2f, evtime = %.2f\n",
99 			Now.eventptr[E_LRTB], Now.eventptr[E_LRTB]->evcode,
100 			Now.eventptr[E_LRTB]->date, evtime);
101 #	endif
102 	if (time > evtime && Etc.nkling < 3)
103 	{
104 		/* then we got a LRTB */
105 		evtime += 0.005;
106 		time = evtime;
107 	}
108 	else
109 		evtime = -1.0e50;
110 	dist = time * speed;
111 
112 	/* move within quadrant */
113 	Sect[Ship.sectx][Ship.secty] = EMPTY;
114 	x = Ship.sectx + 0.5;
115 	y = Ship.secty + 0.5;
116 	xn = NSECTS * dist * bigger;
117 	n = xn + 0.5;
118 #	ifdef xTRACE
119 	if (Trace)
120 		printf("dx = %.2f, dy = %.2f, xn = %.2f, n = %d\n", dx, dy, xn, n);
121 #	endif
122 	Move.free = 0;
123 
124 	for (i = 0; i < n; i++)
125 	{
126 		ix = (x += dx);
127 		iy = (y += dy);
128 #		ifdef xTRACE
129 		if (Trace)
130 			printf("ix = %d, x = %.2f, iy = %d, y = %.2f\n", ix, x, iy, y);
131 #		endif
132 		if (x < 0.0 || y < 0.0 || x >= sectsize || y >= sectsize)
133 		{
134 			/* enter new quadrant */
135 			dx = Ship.quadx * NSECTS + Ship.sectx + dx * xn;
136 			dy = Ship.quady * NSECTS + Ship.secty + dy * xn;
137 			if (dx < 0.0)
138 				ix = -1;
139 			else
140 				ix = dx + 0.5;
141 			if (dy < 0.0)
142 				iy = -1;
143 			else
144 				iy = dy + 0.5;
145 #			ifdef xTRACE
146 			if (Trace)
147 				printf("New quad: ix = %d, iy = %d\n", ix, iy);
148 #			endif
149 			Ship.sectx = x;
150 			Ship.secty = y;
151 			compkldist(0);
152 			Move.newquad = 2;
153 			attack(0);
154 			checkcond();
155 			Ship.quadx = ix / NSECTS;
156 			Ship.quady = iy / NSECTS;
157 			Ship.sectx = ix % NSECTS;
158 			Ship.secty = iy % NSECTS;
159 			if (ix < 0 || Ship.quadx >= NQUADS || iy < 0 || Ship.quady >= NQUADS)
160 				if (!damaged(COMPUTER))
161 				{
162 					dumpme(0);
163 				}
164 				else
165 					lose(L_NEGENB);
166 			initquad(0);
167 			n = 0;
168 			break;
169 		}
170 		if (Sect[ix][iy] != EMPTY)
171 		{
172 			/* we just hit something */
173 			if (!damaged(COMPUTER) && ramflag <= 0)
174 			{
175 				ix = x - dx;
176 				iy = y - dy;
177 				printf("Computer reports navigation error; %s stopped at %d,%d\n",
178 					Ship.shipname, ix, iy);
179 				Ship.energy -= Param.stopengy * speed;
180 				break;
181 			}
182 			/* test for a black hole */
183 			if (Sect[ix][iy] == HOLE)
184 			{
185 				/* get dumped elsewhere in the galaxy */
186 				dumpme(1);
187 				initquad(0);
188 				n = 0;
189 				break;
190 			}
191 			ram(ix, iy);
192 			break;
193 		}
194 	}
195 	if (n > 0)
196 	{
197 		dx = Ship.sectx - ix;
198 		dy = Ship.secty - iy;
199 		dist = sqrt(dx * dx + dy * dy) / NSECTS;
200 		time = dist / speed;
201 		if (evtime > time)
202 			time = evtime;		/* spring the LRTB trap */
203 		Ship.sectx = ix;
204 		Ship.secty = iy;
205 	}
206 	Sect[Ship.sectx][Ship.secty] = Ship.ship;
207 	compkldist(0);
208 	return (time);
209 }
210