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