update.c 9.8 KB

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