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