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