2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)input.c 8.1 (Berkeley) 5/31/93
37 * $FreeBSD: src/games/atc/input.c,v 1.7 2000/02/27 23:02:47 mph Exp $
38 * $DragonFly: src/games/atc/input.c,v 1.2 2003/06/17 04:25:22 dillon Exp $
42 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
44 * Copy permission is hereby granted provided that this notice is
45 * retained on all partial or complete copies.
47 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
53 #include "pathnames.h"
62 #define REDRAWTOKEN '\014' /* CTRL(L) */
63 #define SHELLTOKEN '!'
65 #define ALPHATOKEN 256
72 const char *(*func)();
88 #define T_RULE stack[level].rule
89 #define T_STATE stack[level].state
90 #define T_STR stack[level].str
91 #define T_POS stack[level].pos
92 #define T_CH stack[level].ch
94 #define NUMELS(a) (sizeof (a) / sizeof (*(a)))
96 #define NUMSTATES NUMELS(st)
98 const char *setplane(), *circle(), *left(), *right(), *Left(), *Right(),
99 *beacon(), *ex_it(), *climb(), *descend(), *setalt(), *setrelalt(),
100 *benum(), *to_dir(), *rel_dir(), *delayb(), *mark(), *unmark(),
101 *airport(), *turn(), *ignore();
103 RULE state0[] = { { ALPHATOKEN, 1, "%c:", setplane},
104 { RETTOKEN, -1, "", NULL },
106 { CRTOKEN, -1, "", NULL },
108 { HELPTOKEN, 12, " [a-z]<ret>", NULL }},
109 state1[] = { { 't', 2, " turn", turn },
110 { 'a', 3, " altitude:", NULL },
111 { 'c', 4, " circle", circle },
112 { 'm', 7, " mark", mark },
113 { 'u', 7, " unmark", unmark },
114 { 'i', 7, " ignore", ignore },
115 { HELPTOKEN, 12, " tacmui", NULL }},
116 state2[] = { { 'l', 6, " left", left },
117 { 'r', 6, " right", right },
118 { 'L', 4, " left 90", Left },
119 { 'R', 4, " right 90", Right },
120 { 't', 11, " towards", NULL },
121 { 'w', 4, " to 0", to_dir },
122 { 'e', 4, " to 45", to_dir },
123 { 'd', 4, " to 90", to_dir },
124 { 'c', 4, " to 135", to_dir },
125 { 'x', 4, " to 180", to_dir },
126 { 'z', 4, " to 225", to_dir },
127 { 'a', 4, " to 270", to_dir },
128 { 'q', 4, " to 315", to_dir },
129 { HELPTOKEN, 12, " lrLRt<dir>", NULL }},
130 state3[] = { { '+', 10, " climb", climb },
131 { 'c', 10, " climb", climb },
132 { '-', 10, " descend", descend },
133 { 'd', 10, " descend", descend },
134 { NUMTOKEN, 7, " %c000 feet", setalt },
135 { HELPTOKEN, 12, " +-cd[0-9]", NULL }},
136 state4[] = { { '@', 9, " at", NULL },
137 { 'a', 9, " at", NULL },
138 { RETTOKEN, -1, "", NULL },
140 { CRTOKEN, -1, "", NULL },
142 { HELPTOKEN, 12, " @a<ret>", NULL }},
143 state5[] = { { NUMTOKEN, 7, "%c", delayb },
144 { HELPTOKEN, 12, " [0-9]", NULL }},
145 state6[] = { { '@', 9, " at", NULL },
146 { 'a', 9, " at", NULL },
147 { 'w', 4, " 0", rel_dir },
148 { 'e', 4, " 45", rel_dir },
149 { 'd', 4, " 90", rel_dir },
150 { 'c', 4, " 135", rel_dir },
151 { 'x', 4, " 180", rel_dir },
152 { 'z', 4, " 225", rel_dir },
153 { 'a', 4, " 270", rel_dir },
154 { 'q', 4, " 315", rel_dir },
155 { RETTOKEN, -1, "", NULL },
157 { CRTOKEN, -1, "", NULL },
159 { HELPTOKEN, 12, " @a<dir><ret>",NULL }},
160 state7[] = { { RETTOKEN, -1, "", NULL },
162 { CRTOKEN, -1, "", NULL },
164 { HELPTOKEN, 12, " <ret>", NULL }},
165 state8[] = { { NUMTOKEN, 4, "%c", benum },
166 { HELPTOKEN, 12, " [0-9]", NULL }},
167 state9[] = { { 'b', 5, " beacon #", NULL },
168 { '*', 5, " beacon #", NULL },
169 { HELPTOKEN, 12, " b*", NULL }},
170 state10[] = { { NUMTOKEN, 7, " %c000 ft", setrelalt},
171 { HELPTOKEN, 12, " [0-9]", NULL }},
172 state11[] = { { 'b', 8, " beacon #", beacon },
173 { '*', 8, " beacon #", beacon },
174 { 'e', 8, " exit #", ex_it },
175 { 'a', 8, " airport #", airport },
176 { HELPTOKEN, 12, " b*ea", NULL }},
177 state12[] = { { -1, -1, "", NULL }};
179 #define DEF_STATE(s) { NUMELS(s), (s) }
182 DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
183 DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
184 DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
185 DEF_STATE(state9), DEF_STATE(state10), DEF_STATE(state11),
190 STACK stack[MAXDEPTH];
193 int dest_type, dest_no, dir;
223 int newstate, newpos;
225 (void)sprintf(T_STR, st[T_STATE].rule[ruleno].str, tval);
228 newstate = st[T_STATE].rule[ruleno].to_state;
229 newpos = T_POS + strlen(T_STR);
231 ioaddstr(T_POS, T_STR);
245 const char *s, *(*func)();
252 if (c == tty_new.sg_erase) {
255 } else if (c == tty_new.sg_kill) {
260 for (i = 0; i < st[T_STATE].num_rules; i++) {
261 if (st[T_STATE].rule[i].token == c ||
262 st[T_STATE].rule[i].token == tval) {
263 push(i, (c >= ALPHATOKEN) ? tval : c);
271 } while (T_STATE != -1);
274 return (1); /* forced update */
276 dest_type = T_NODEST;
278 for (i = 0; i < level; i++) {
279 func = st[stack[i].state].rule[stack[i].rule].func;
281 if ((s = (*func)(stack[i].ch)) != NULL) {
282 ioerror(stack[i].pos, strlen(stack[i].str), s);
287 pp = findplane(p.plane_no);
288 if (pp->new_altitude != p.new_altitude)
289 pp->new_altitude = p.new_altitude;
290 else if (pp->status != p.status)
291 pp->status = p.status;
293 pp->new_dir = p.new_dir;
294 pp->delayd = p.delayd;
295 pp->delayd_no = p.delayd_no;
308 while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
310 if (tval == SHELLTOKEN)
313 struct itimerval itv;
314 itv.it_value.tv_sec = 0;
315 itv.it_value.tv_usec = 0;
316 setitimer(ITIMER_REAL, &itv, NULL);
322 if (fork() == 0) /* child */
330 /* run user's favorite shell */
331 if ((shell = getenv("SHELL")) != NULL)
333 base = strrchr(shell, '/');
338 execl(shell, base, 0);
341 execl(_PATH_BSHELL, "sh", 0);
348 ioctl(fileno(stdin), TIOCSETP, &tty_new);
349 itv.it_value.tv_sec = 0;
350 itv.it_value.tv_usec = 1;
351 itv.it_interval.tv_sec = sp->update_secs;
352 itv.it_interval.tv_usec = 0;
353 setitimer(ITIMER_REAL, &itv, NULL);
356 ioctl(fileno(stdin), TCSETAW, &tty_new);
365 else if (isalpha(tval))
376 pp = findplane(number(c));
378 return ("Unknown Plane");
379 bcopy(pp, &p, sizeof (p));
388 return ("Planes at airports may not change direction");
396 return ("Planes cannot circle on the ground");
405 p.new_dir = p.dir - 1;
415 p.new_dir = p.dir + 1;
416 if (p.new_dir >= MAXDIR)
424 p.new_dir = p.dir - 2;
433 p.new_dir = p.dir + 2;
434 if (p.new_dir >= MAXDIR)
446 if (c >= sp->num_beacons)
447 return ("Unknown beacon");
448 xdiff = sp->beacon[c].x - p.xpos;
450 ydiff = sp->beacon[c].y - p.ypos;
452 if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
453 return ("Beacon is not in flight path");
457 if (dest_type != T_NODEST) {
460 xdiff = sp->beacon[dest_no].x - sp->beacon[c].x;
461 ydiff = sp->beacon[dest_no].y - sp->beacon[c].y;
464 xdiff = sp->exit[dest_no].x - sp->beacon[c].x;
465 ydiff = sp->exit[dest_no].y - sp->beacon[c].y;
468 xdiff = sp->airport[dest_no].x - sp->beacon[c].x;
469 ydiff = sp->airport[dest_no].y - sp->beacon[c].y;
472 return ("Bad case in delayb! Get help!");
475 if (xdiff == 0 && ydiff == 0)
476 return ("Would already be there");
477 p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
478 if (p.new_dir == p.dir)
479 return ("Already going in that direction");
487 dest_type = T_BEACON;
501 dest_type = T_AIRPORT;
522 if ((p.altitude == c - '0') && (p.new_altitude == p.altitude))
523 return ("Already at that altitude");
524 p.new_altitude = c - '0';
532 return ("altitude not changed");
536 p.new_altitude = p.altitude + c - '0';
539 p.new_altitude = p.altitude - (c - '0');
542 return ("Unknown case in setrelalt! Get help!");
545 if (p.new_altitude < 0)
546 return ("Altitude would be too low");
547 else if (p.new_altitude > 9)
548 return ("Altitude would be too high");
559 if (c >= sp->num_beacons)
560 return ("Unknown beacon");
561 p.new_dir = DIR_FROM_DXDY(sp->beacon[c].x - p.xpos,
562 sp->beacon[c].y - p.ypos);
565 if (c >= sp->num_exits)
566 return ("Unknown exit");
567 p.new_dir = DIR_FROM_DXDY(sp->exit[c].x - p.xpos,
568 sp->exit[c].y - p.ypos);
571 if (c >= sp->num_airports)
572 return ("Unknown airport");
573 p.new_dir = DIR_FROM_DXDY(sp->airport[c].x - p.xpos,
574 sp->airport[c].y - p.ypos);
577 return ("Unknown case in benum! Get help!");
586 p.new_dir = dir_no(c);
598 p.new_dir = p.dir - angle;
603 p.new_dir = p.dir + angle;
604 if (p.new_dir >= MAXDIR)
608 return ("Bizarre direction in rel_dir! Get help!");
618 return ("Cannot mark planes on the ground");
619 if (p.status == S_MARKED)
620 return ("Already marked");
629 return ("Cannot unmark planes on the ground");
630 if (p.status == S_UNMARKED)
631 return ("Already unmarked");
632 p.status = S_UNMARKED;
640 return ("Cannot ignore planes on the ground");
641 if (p.status == S_IGNORED)
642 return ("Already ignored");
643 p.status = S_IGNORED;
653 case 'w': dir = 0; break;
654 case 'e': dir = 1; break;
655 case 'd': dir = 2; break;
656 case 'c': dir = 3; break;
657 case 'x': dir = 4; break;
658 case 'z': dir = 5; break;
659 case 'a': dir = 6; break;
660 case 'q': dir = 7; break;
662 fprintf(stderr, "bad character in dir_no\n");