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