1/*	$OpenBSD: update.c,v 1.19 2016/01/08 13:40:05 tb 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 <stdlib.h>
45#include <string.h>
46
47#include "extern.h"
48
49void
50setseed(const char *seed)
51{
52	seeded = 1;
53	srandom_deterministic(atol(seed));
54}
55
56uint32_t
57atcrandom(void)
58{
59	if (seeded)
60		return random();
61	else
62		return arc4random();
63}
64
65uint32_t
66atcrandom_uniform(uint32_t upper_bound)
67{
68	if (seeded)
69		return random() % upper_bound;
70	else
71		return arc4random_uniform(upper_bound);
72}
73
74void
75update(int dummy)
76{
77	int	i, dir_diff, unclean;
78	PLANE	*pp, *p1, *p2;
79
80	clck++;
81
82	erase_all();
83
84	/* put some planes in the air */
85	do {
86		unclean = 0;
87		for (pp = ground.head; pp != NULL; pp = pp->next) {
88			if (pp->new_altitude > 0) {
89				delete(&ground, pp);
90				append(&air, pp);
91				unclean = 1;
92				break;
93			}
94		}
95	} while (unclean);
96
97	/* do altitude change and basic movement */
98	for (pp = air.head; pp != NULL; pp = pp->next) {
99		/* type 0 only move every other turn */
100		if (pp->plane_type == 0 && clck & 1)
101			continue;
102
103		pp->fuel--;
104		if (pp->fuel < 0)
105			loser(pp, "ran out of fuel.");
106
107		pp->altitude += SGN(pp->new_altitude - pp->altitude);
108
109		if (!pp->delayd) {
110			dir_diff = pp->new_dir - pp->dir;
111			/*
112			 * Allow for circle commands
113			 */
114			if (pp->new_dir >= 0 && pp->new_dir < MAXDIR) {
115				if (dir_diff > MAXDIR/2)
116					dir_diff -= MAXDIR;
117				else if (dir_diff < -(MAXDIR/2))
118					dir_diff += MAXDIR;
119			}
120			if (dir_diff > 2)
121				dir_diff = 2;
122			else if (dir_diff < -2)
123				dir_diff = -2;
124			pp->dir += dir_diff;
125			if (pp->dir >= MAXDIR)
126				pp->dir -= MAXDIR;
127			else if (pp->dir < 0)
128				pp->dir += MAXDIR;
129		}
130		pp->xpos += displacement[pp->dir].dx;
131		pp->ypos += displacement[pp->dir].dy;
132
133		if (pp->delayd && pp->xpos == sp->beacon[pp->delayd_no].x &&
134		    pp->ypos == sp->beacon[pp->delayd_no].y) {
135			pp->delayd = 0;
136			if (pp->status == S_UNMARKED)
137				pp->status = S_MARKED;
138		}
139
140		switch (pp->dest_type) {
141		case T_AIRPORT:
142			if (pp->xpos == sp->airport[pp->dest_no].x &&
143			    pp->ypos == sp->airport[pp->dest_no].y &&
144			    pp->altitude == 0) {
145				if (pp->dir != sp->airport[pp->dest_no].dir)
146				    loser(pp, "landed in the wrong direction.");
147				else {
148				    pp->status = S_GONE;
149				    continue;
150				}
151			}
152			break;
153		case T_EXIT:
154			if (pp->xpos == sp->exit[pp->dest_no].x &&
155			    pp->ypos == sp->exit[pp->dest_no].y) {
156			    	if (pp->altitude != 9)
157				    loser(pp, "exited at the wrong altitude.");
158				else {
159				    pp->status = S_GONE;
160				    continue;
161				}
162			}
163			break;
164		default:
165			loser(pp, "has a bizarre destination, get help!");
166		}
167		if (pp->altitude > 9)
168			/* "this is impossible" */
169			loser(pp, "exceeded flight ceiling.");
170		if (pp->altitude <= 0) {
171			for (i = 0; i < sp->num_airports; i++)
172				if (pp->xpos == sp->airport[i].x &&
173				    pp->ypos == sp->airport[i].y) {
174					if (pp->dest_type == T_AIRPORT)
175					    loser(pp,
176						"landed at the wrong airport.");
177					else
178					    loser(pp,
179						"landed instead of exited.");
180				}
181			loser(pp, "crashed on the ground.");
182		}
183		if (pp->xpos < 1 || pp->xpos >= sp->width - 1 ||
184		    pp->ypos < 1 || pp->ypos >= sp->height - 1) {
185			for (i = 0; i < sp->num_exits; i++)
186				if (pp->xpos == sp->exit[i].x &&
187				    pp->ypos == sp->exit[i].y) {
188					if (pp->dest_type == T_EXIT)
189					    loser(pp,
190						"exited via the wrong exit.");
191					else
192					    loser(pp,
193						"exited instead of landed.");
194				}
195			loser(pp, "illegally left the flight arena.");
196		}
197	}
198
199	/*
200	 * Traverse the list once, deleting the planes that are gone.
201	 */
202	for (pp = air.head; pp != NULL; pp = p2) {
203		p2 = pp->next;
204		if (pp->status == S_GONE) {
205			safe_planes++;
206			delete(&air, pp);
207		}
208	}
209
210	draw_all();
211
212	for (p1 = air.head; p1 != NULL; p1 = p1->next)
213		for (p2 = p1->next; p2 != NULL; p2 = p2->next)
214			if (too_close(p1, p2, 1)) {
215				static char	buf[80];
216
217				(void)snprintf(buf, sizeof buf,
218				    "collided with plane '%c'.",
219				    name(p2));
220				loser(p1, buf);
221			}
222	/*
223	 * Check every other update.  Actually, only add on even updates.
224	 * Otherwise, prop jobs show up *on* entrance.  Remember that
225	 * we don't update props on odd updates.
226	 */
227	if (atcrandom_uniform(sp->newplane_time) == 0)
228		addplane();
229}
230
231const char *
232command(PLANE *pp)
233{
234	static char	buf[50], *bp, *comm_start;
235
236	buf[0] = '\0';
237	bp = buf;
238	(void)snprintf(bp, buf + sizeof buf - bp,
239		"%c%d%c%c%d: ", name(pp), pp->altitude,
240		(pp->fuel < LOWFUEL) ? '*' : ' ',
241		(pp->dest_type == T_AIRPORT) ? 'A' : 'E', pp->dest_no);
242
243	comm_start = bp = strchr(buf, '\0');
244	if (pp->altitude == 0)
245		(void)snprintf(bp, buf + sizeof buf - bp,
246			"Holding @ A%d", pp->orig_no);
247	else if (pp->new_dir >= MAXDIR || pp->new_dir < 0)
248		strlcpy(bp, "Circle", buf + sizeof buf - bp);
249	else if (pp->new_dir != pp->dir)
250		(void)snprintf(bp, buf + sizeof buf - bp,
251			"%d", dir_deg(pp->new_dir));
252
253	bp = strchr(buf, '\0');
254	if (pp->delayd)
255		(void)snprintf(bp, buf + sizeof buf - bp,
256			" @ B%d", pp->delayd_no);
257
258	bp = strchr(buf, '\0');
259	if (*comm_start == '\0' &&
260	    (pp->status == S_UNMARKED || pp->status == S_IGNORED))
261		strlcpy(bp, "---------", buf + sizeof buf - bp);
262	return (buf);
263}
264
265char
266name(const PLANE *p)
267{
268	if (p->plane_type == 0)
269		return ('A' + p->plane_no);
270	else
271		return ('a' + p->plane_no);
272}
273
274int
275number(char l)
276{
277	if (l >= 'a' && l <= 'z')
278		return (l - 'a');
279	else if (l >= 'A' && l <= 'Z')
280		return (l - 'A');
281	else
282		return (-1);
283}
284
285int
286next_plane(void)
287{
288	static int	last_plane = -1;
289	PLANE		*pp;
290	int		found, start_plane = last_plane;
291
292	do {
293		found = 0;
294		last_plane++;
295		if (last_plane >= 26)
296			last_plane = 0;
297		for (pp = air.head; pp != NULL; pp = pp->next)
298			if (pp->plane_no == last_plane) {
299				found++;
300				break;
301			}
302		if (!found)
303			for (pp = ground.head; pp != NULL; pp = pp->next)
304				if (pp->plane_no == last_plane) {
305					found++;
306					break;
307				}
308	} while (found && last_plane != start_plane);
309	if (last_plane == start_plane)
310		return (-1);
311	return (last_plane);
312}
313
314int
315addplane(void)
316{
317	PLANE	p, *pp, *p1;
318	int	i, num_starts, close, rnd, rnd2, pnum;
319
320	memset(&p, 0, sizeof (p));
321
322	p.status = S_MARKED;
323	p.plane_type = atcrandom_uniform(2);
324
325	num_starts = sp->num_exits + sp->num_airports;
326	rnd = atcrandom_uniform(num_starts);
327
328	if (rnd < sp->num_exits) {
329		p.dest_type = T_EXIT;
330		p.dest_no = rnd;
331	} else {
332		p.dest_type = T_AIRPORT;
333		p.dest_no = rnd - sp->num_exits;
334	}
335
336	/* loop until we get a plane not near another */
337	for (i = 0; i < num_starts; i++) {
338		/* loop till we get a different start point */
339		while ((rnd2 = atcrandom_uniform(num_starts)) == rnd)
340			;
341		if (rnd2 < sp->num_exits) {
342			p.orig_type = T_EXIT;
343			p.orig_no = rnd2;
344			p.xpos = sp->exit[rnd2].x;
345			p.ypos = sp->exit[rnd2].y;
346			p.new_dir = p.dir = sp->exit[rnd2].dir;
347			p.altitude = p.new_altitude = 7;
348			close = 0;
349			for (p1 = air.head; p1 != NULL; p1 = p1->next)
350				if (too_close(p1, &p, 4)) {
351					close++;
352					break;
353				}
354			if (close)
355				continue;
356		} else {
357			p.orig_type = T_AIRPORT;
358			p.orig_no = rnd2 - sp->num_exits;
359			p.xpos = sp->airport[p.orig_no].x;
360			p.ypos = sp->airport[p.orig_no].y;
361			p.new_dir = p.dir = sp->airport[p.orig_no].dir;
362			p.altitude = p.new_altitude = 0;
363		}
364		p.fuel = sp->width + sp->height;
365		break;
366	}
367	if (i >= num_starts)
368		return (-1);
369	pnum = next_plane();
370	if (pnum < 0)
371		return (-1);
372	p.plane_no = pnum;
373
374	pp = newplane();
375	memcpy(pp, &p, sizeof (p));
376
377	if (pp->orig_type == T_AIRPORT)
378		append(&ground, pp);
379	else
380		append(&air, pp);
381
382	return (pp->dest_type);
383}
384
385PLANE	*
386findplane(int n)
387{
388	PLANE	*pp;
389
390	for (pp = air.head; pp != NULL; pp = pp->next)
391		if (pp->plane_no == n)
392			return (pp);
393	for (pp = ground.head; pp != NULL; pp = pp->next)
394		if (pp->plane_no == n)
395			return (pp);
396	return (NULL);
397}
398
399int
400too_close(const PLANE *p1, const PLANE *p2, int dist)
401{
402	if (ABS(p1->altitude - p2->altitude) <= dist &&
403	    ABS(p1->xpos - p2->xpos) <= dist && ABS(p1->ypos - p2->ypos) <= dist)
404		return (1);
405	else
406		return (0);
407}
408
409int
410dir_deg(int d)
411{
412	switch (d) {
413	case 0: return (0);
414	case 1: return (45);
415	case 2: return (90);
416	case 3: return (135);
417	case 4: return (180);
418	case 5: return (225);
419	case 6: return (270);
420	case 7: return (315);
421	default:
422		return (-1);
423	}
424}
425