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