xref: /openbsd/games/atc/update.c (revision a6445c1d)
1 /*	$OpenBSD: update.c,v 1.14 2014/07/13 14:01:04 tedu Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ed James.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
37  *
38  * Copy permission is hereby granted provided that this notice is
39  * retained on all partial or complete copies.
40  *
41  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
42  */
43 
44 #include "include.h"
45 
46 int seeded;
47 void
48 setseed(const char *seed)
49 {
50 	seeded = 1;
51 	srandom(atol(seed));
52 }
53 
54 uint32_t
55 atcrandom()
56 {
57 	if (seeded)
58 		return random();
59 	else
60 		return arc4random();
61 }
62 
63 void
64 update(int dummy)
65 {
66 	int	i, dir_diff, unclean;
67 	PLANE	*pp, *p1, *p2;
68 
69 	clck++;
70 
71 	erase_all();
72 
73 	/* put some planes in the air */
74 	do {
75 		unclean = 0;
76 		for (pp = ground.head; pp != NULL; pp = pp->next) {
77 			if (pp->new_altitude > 0) {
78 				delete(&ground, pp);
79 				append(&air, pp);
80 				unclean = 1;
81 				break;
82 			}
83 		}
84 	} while (unclean);
85 
86 	/* do altitude change and basic movement */
87 	for (pp = air.head; pp != NULL; pp = pp->next) {
88 		/* type 0 only move every other turn */
89 		if (pp->plane_type == 0 && clck & 1)
90 			continue;
91 
92 		pp->fuel--;
93 		if (pp->fuel < 0)
94 			loser(pp, "ran out of fuel.");
95 
96 		pp->altitude += SGN(pp->new_altitude - pp->altitude);
97 
98 		if (!pp->delayd) {
99 			dir_diff = pp->new_dir - pp->dir;
100 			/*
101 			 * Allow for circle commands
102 			 */
103 			if (pp->new_dir >= 0 && pp->new_dir < MAXDIR) {
104 				if (dir_diff > MAXDIR/2)
105 					dir_diff -= MAXDIR;
106 				else if (dir_diff < -(MAXDIR/2))
107 					dir_diff += MAXDIR;
108 			}
109 			if (dir_diff > 2)
110 				dir_diff = 2;
111 			else if (dir_diff < -2)
112 				dir_diff = -2;
113 			pp->dir += dir_diff;
114 			if (pp->dir >= MAXDIR)
115 				pp->dir -= MAXDIR;
116 			else if (pp->dir < 0)
117 				pp->dir += MAXDIR;
118 		}
119 		pp->xpos += displacement[pp->dir].dx;
120 		pp->ypos += displacement[pp->dir].dy;
121 
122 		if (pp->delayd && pp->xpos == sp->beacon[pp->delayd_no].x &&
123 		    pp->ypos == sp->beacon[pp->delayd_no].y) {
124 			pp->delayd = 0;
125 			if (pp->status == S_UNMARKED)
126 				pp->status = S_MARKED;
127 		}
128 
129 		switch (pp->dest_type) {
130 		case T_AIRPORT:
131 			if (pp->xpos == sp->airport[pp->dest_no].x &&
132 			    pp->ypos == sp->airport[pp->dest_no].y &&
133 			    pp->altitude == 0) {
134 				if (pp->dir != sp->airport[pp->dest_no].dir)
135 				    loser(pp, "landed in the wrong direction.");
136 				else {
137 				    pp->status = S_GONE;
138 				    continue;
139 				}
140 			}
141 			break;
142 		case T_EXIT:
143 			if (pp->xpos == sp->exit[pp->dest_no].x &&
144 			    pp->ypos == sp->exit[pp->dest_no].y) {
145 			    	if (pp->altitude != 9)
146 				    loser(pp, "exited at the wrong altitude.");
147 				else {
148 				    pp->status = S_GONE;
149 				    continue;
150 				}
151 			}
152 			break;
153 		default:
154 			loser(pp, "has a bizarre destination, get help!");
155 		}
156 		if (pp->altitude > 9)
157 			/* "this is impossible" */
158 			loser(pp, "exceeded flight ceiling.");
159 		if (pp->altitude <= 0) {
160 			for (i = 0; i < sp->num_airports; i++)
161 				if (pp->xpos == sp->airport[i].x &&
162 				    pp->ypos == sp->airport[i].y) {
163 					if (pp->dest_type == T_AIRPORT)
164 					    loser(pp,
165 						"landed at the wrong airport.");
166 					else
167 					    loser(pp,
168 						"landed instead of exited.");
169 				}
170 			loser(pp, "crashed on the ground.");
171 		}
172 		if (pp->xpos < 1 || pp->xpos >= sp->width - 1 ||
173 		    pp->ypos < 1 || pp->ypos >= sp->height - 1) {
174 			for (i = 0; i < sp->num_exits; i++)
175 				if (pp->xpos == sp->exit[i].x &&
176 				    pp->ypos == sp->exit[i].y) {
177 					if (pp->dest_type == T_EXIT)
178 					    loser(pp,
179 						"exited via the wrong exit.");
180 					else
181 					    loser(pp,
182 						"exited instead of landed.");
183 				}
184 			loser(pp, "illegally left the flight arena.");
185 		}
186 	}
187 
188 	/*
189 	 * Traverse the list once, deleting the planes that are gone.
190 	 */
191 	for (pp = air.head; pp != NULL; pp = p2) {
192 		p2 = pp->next;
193 		if (pp->status == S_GONE) {
194 			safe_planes++;
195 			delete(&air, pp);
196 		}
197 	}
198 
199 	draw_all();
200 
201 	for (p1 = air.head; p1 != NULL; p1 = p1->next)
202 		for (p2 = p1->next; p2 != NULL; p2 = p2->next)
203 			if (too_close(p1, p2, 1)) {
204 				static char	buf[80];
205 
206 				(void)snprintf(buf, sizeof buf,
207 				    "collided with plane '%c'.",
208 				    name(p2));
209 				loser(p1, buf);
210 			}
211 	/*
212 	 * Check every other update.  Actually, only add on even updates.
213 	 * Otherwise, prop jobs show up *on* entrance.  Remember that
214 	 * we don't update props on odd updates.
215 	 */
216 	if ((atcrandom() % sp->newplane_time) == 0)
217 		addplane();
218 }
219 
220 const char *
221 command(PLANE *pp)
222 {
223 	static char	buf[50], *bp, *comm_start;
224 
225 	buf[0] = '\0';
226 	bp = buf;
227 	(void)snprintf(bp, buf + sizeof buf - bp,
228 		"%c%d%c%c%d: ", name(pp), pp->altitude,
229 		(pp->fuel < LOWFUEL) ? '*' : ' ',
230 		(pp->dest_type == T_AIRPORT) ? 'A' : 'E', pp->dest_no);
231 
232 	comm_start = bp = strchr(buf, '\0');
233 	if (pp->altitude == 0)
234 		(void)snprintf(bp, buf + sizeof buf - bp,
235 			"Holding @ A%d", pp->orig_no);
236 	else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
237 		strlcpy(bp, "Circle", buf + sizeof buf - bp);
238 	else if (pp->new_dir != pp->dir)
239 		(void)snprintf(bp, buf + sizeof buf - bp,
240 			"%d", dir_deg(pp->new_dir));
241 
242 	bp = strchr(buf, '\0');
243 	if (pp->delayd)
244 		(void)snprintf(bp, buf + sizeof buf - bp,
245 			" @ B%d", pp->delayd_no);
246 
247 	bp = strchr(buf, '\0');
248 	if (*comm_start == '\0' &&
249 	    (pp->status == S_UNMARKED || pp->status == S_IGNORED))
250 		strlcpy(bp, "---------", buf + sizeof buf - bp);
251 	return (buf);
252 }
253 
254 char
255 name(const PLANE *p)
256 {
257 	if (p->plane_type == 0)
258 		return ('A' + p->plane_no);
259 	else
260 		return ('a' + p->plane_no);
261 }
262 
263 int
264 number(char l)
265 {
266 	if (l >= 'a' && l <= 'z')
267 		return (l - 'a');
268 	else if (l >= 'A' && l <= 'Z')
269 		return (l - 'A');
270 	else
271 		return (-1);
272 }
273 
274 int
275 next_plane(void)
276 {
277 	static int	last_plane = -1;
278 	PLANE		*pp;
279 	int		found, start_plane = last_plane;
280 
281 	do {
282 		found = 0;
283 		last_plane++;
284 		if (last_plane >= 26)
285 			last_plane = 0;
286 		for (pp = air.head; pp != NULL; pp = pp->next)
287 			if (pp->plane_no == last_plane) {
288 				found++;
289 				break;
290 			}
291 		if (!found)
292 			for (pp = ground.head; pp != NULL; pp = pp->next)
293 				if (pp->plane_no == last_plane) {
294 					found++;
295 					break;
296 				}
297 	} while (found && last_plane != start_plane);
298 	if (last_plane == start_plane)
299 		return (-1);
300 	return (last_plane);
301 }
302 
303 int
304 addplane(void)
305 {
306 	PLANE	p, *pp, *p1;
307 	int	i, num_starts, close, rnd, rnd2, pnum;
308 
309 	memset(&p, 0, sizeof (p));
310 
311 	p.status = S_MARKED;
312 	p.plane_type = atcrandom() % 2;
313 
314 	num_starts = sp->num_exits + sp->num_airports;
315 	rnd = atcrandom() % num_starts;
316 
317 	if (rnd < sp->num_exits) {
318 		p.dest_type = T_EXIT;
319 		p.dest_no = rnd;
320 	} else {
321 		p.dest_type = T_AIRPORT;
322 		p.dest_no = rnd - sp->num_exits;
323 	}
324 
325 	/* loop until we get a plane not near another */
326 	for (i = 0; i < num_starts; i++) {
327 		/* loop till we get a different start point */
328 		while ((rnd2 = atcrandom() % num_starts) == rnd)
329 			;
330 		if (rnd2 < sp->num_exits) {
331 			p.orig_type = T_EXIT;
332 			p.orig_no = rnd2;
333 			p.xpos = sp->exit[rnd2].x;
334 			p.ypos = sp->exit[rnd2].y;
335 			p.new_dir = p.dir = sp->exit[rnd2].dir;
336 			p.altitude = p.new_altitude = 7;
337 			close = 0;
338 			for (p1 = air.head; p1 != NULL; p1 = p1->next)
339 				if (too_close(p1, &p, 4)) {
340 					close++;
341 					break;
342 				}
343 			if (close)
344 				continue;
345 		} else {
346 			p.orig_type = T_AIRPORT;
347 			p.orig_no = rnd2 - sp->num_exits;
348 			p.xpos = sp->airport[p.orig_no].x;
349 			p.ypos = sp->airport[p.orig_no].y;
350 			p.new_dir = p.dir = sp->airport[p.orig_no].dir;
351 			p.altitude = p.new_altitude = 0;
352 		}
353 		p.fuel = sp->width + sp->height;
354 		break;
355 	}
356 	if (i >= num_starts)
357 		return (-1);
358 	pnum = next_plane();
359 	if (pnum < 0)
360 		return (-1);
361 	p.plane_no = pnum;
362 
363 	pp = newplane();
364 	memcpy(pp, &p, sizeof (p));
365 
366 	if (pp->orig_type == T_AIRPORT)
367 		append(&ground, pp);
368 	else
369 		append(&air, pp);
370 
371 	return (pp->dest_type);
372 }
373 
374 PLANE	*
375 findplane(int n)
376 {
377 	PLANE	*pp;
378 
379 	for (pp = air.head; pp != NULL; pp = pp->next)
380 		if (pp->plane_no == n)
381 			return (pp);
382 	for (pp = ground.head; pp != NULL; pp = pp->next)
383 		if (pp->plane_no == n)
384 			return (pp);
385 	return (NULL);
386 }
387 
388 int
389 too_close(const PLANE *p1, const PLANE *p2, int dist)
390 {
391 	if (ABS(p1->altitude - p2->altitude) <= dist &&
392 	    ABS(p1->xpos - p2->xpos) <= dist && ABS(p1->ypos - p2->ypos) <= dist)
393 		return (1);
394 	else
395 		return (0);
396 }
397 
398 int
399 dir_deg(int d)
400 {
401 	switch (d) {
402 	case 0: return (0);
403 	case 1: return (45);
404 	case 2: return (90);
405 	case 3: return (135);
406 	case 4: return (180);
407 	case 5: return (225);
408 	case 6: return (270);
409 	case 7: return (315);
410 	default:
411 		return (-1);
412 	}
413 }
414