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