[BACK]Return to input.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / games / atc

Annotation of src/games/atc/input.c, Revision 1.24.6.1

1.24.6.1! yamt        1: /*     $NetBSD: input.c,v 1.24 2009/08/12 04:48:03 dholland Exp $      */
1.3       cgd         2:
1.1       cgd         3: /*-
1.3       cgd         4:  * Copyright (c) 1990, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Ed James.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
1.15      agc        18:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
                     35: /*
                     36:  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
                     37:  *
                     38:  * Copy permission is hereby granted provided that this notice is
                     39:  * retained on all partial or complete copies.
                     40:  *
                     41:  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
                     42:  */
                     43:
1.6       lukem      44: #include <sys/cdefs.h>
1.1       cgd        45: #ifndef lint
1.3       cgd        46: #if 0
                     47: static char sccsid[] = "@(#)input.c    8.1 (Berkeley) 5/31/93";
                     48: #else
1.24.6.1! yamt       49: __RCSID("$NetBSD: input.c,v 1.24 2009/08/12 04:48:03 dholland Exp $");
1.3       cgd        50: #endif
1.14      cgd        51: #endif /* not lint */
1.1       cgd        52:
                     53: #include "include.h"
                     54: #include "pathnames.h"
                     55:
1.24      dholland   56: static void rezero(void);
                     57: static void noise(void);
                     58: static int gettoken(void);
                     59: static const char *setplane(int);
                     60: static const char *turn(int);
                     61: static const char *circle(int);
                     62: static const char *left(int);
                     63: static const char *right(int);
                     64: static const char *Left(int);
                     65: static const char *Right(int);
                     66: static const char *delayb(int);
                     67: static const char *beacon(int);
                     68: static const char *ex_it(int);
                     69: static const char *airport(int);
                     70: static const char *climb(int);
                     71: static const char *descend(int);
                     72: static const char *setalt(int);
                     73: static const char *setrelalt(int);
                     74: static const char *benum(int);
                     75: static const char *to_dir(int);
                     76: static const char *rel_dir(int);
                     77: static const char *mark(int);
                     78: static const char *unmark(int);
                     79: static const char *ignore(int);
                     80:
                     81:
                     82:
1.1       cgd        83: #define MAXRULES       6
                     84: #define MAXDEPTH       15
                     85:
                     86: #define RETTOKEN       '\n'
                     87: #define REDRAWTOKEN    '\014'  /* CTRL(L) */
                     88: #define        SHELLTOKEN      '!'
                     89: #define HELPTOKEN      '?'
                     90: #define ALPHATOKEN     256
                     91: #define NUMTOKEN       257
                     92:
                     93: typedef struct {
                     94:        int     token;
                     95:        int     to_state;
1.11      hubertf    96:        const char      *str;
1.18      jmc        97:        const char      *(*func)(int);
1.1       cgd        98: } RULE;
                     99:
                    100: typedef struct {
                    101:        int     num_rules;
                    102:        RULE    *rule;
                    103: } STATE;
                    104:
                    105: typedef struct {
                    106:        char    str[20];
                    107:        int     state;
                    108:        int     rule;
                    109:        int     ch;
                    110:        int     pos;
                    111: } STACK;
                    112:
                    113: #define T_RULE         stack[level].rule
                    114: #define T_STATE                stack[level].state
                    115: #define T_STR          stack[level].str
                    116: #define T_POS          stack[level].pos
                    117: #define        T_CH            stack[level].ch
                    118:
                    119: #define NUMELS(a)      (sizeof (a) / sizeof (*(a)))
                    120:
                    121: #define NUMSTATES      NUMELS(st)
                    122:
1.24      dholland  123: static
1.1       cgd       124: RULE   state0[] = {    { ALPHATOKEN,   1,      "%c:",          setplane},
                    125:                        { RETTOKEN,     -1,     "",             NULL    },
                    126:                        { HELPTOKEN,    12,     " [a-z]<ret>",  NULL    }},
                    127:        state1[] = {    { 't',          2,      " turn",        turn    },
                    128:                        { 'a',          3,      " altitude:",   NULL    },
                    129:                        { 'c',          4,      " circle",      circle  },
                    130:                        { 'm',          7,      " mark",        mark    },
                    131:                        { 'u',          7,      " unmark",      unmark  },
                    132:                        { 'i',          7,      " ignore",      ignore  },
                    133:                        { HELPTOKEN,    12,     " tacmui",      NULL    }},
                    134:        state2[] = {    { 'l',          6,      " left",        left    },
                    135:                        { 'r',          6,      " right",       right   },
                    136:                        { 'L',          4,      " left 90",     Left    },
                    137:                        { 'R',          4,      " right 90",    Right   },
                    138:                        { 't',          11,     " towards",     NULL    },
                    139:                        { 'w',          4,      " to 0",        to_dir  },
                    140:                        { 'e',          4,      " to 45",       to_dir  },
                    141:                        { 'd',          4,      " to 90",       to_dir  },
                    142:                        { 'c',          4,      " to 135",      to_dir  },
                    143:                        { 'x',          4,      " to 180",      to_dir  },
                    144:                        { 'z',          4,      " to 225",      to_dir  },
                    145:                        { 'a',          4,      " to 270",      to_dir  },
                    146:                        { 'q',          4,      " to 315",      to_dir  },
                    147:                        { HELPTOKEN,    12,     " lrLRt<dir>",  NULL    }},
                    148:        state3[] = {    { '+',          10,     " climb",       climb   },
                    149:                        { 'c',          10,     " climb",       climb   },
                    150:                        { '-',          10,     " descend",     descend },
                    151:                        { 'd',          10,     " descend",     descend },
                    152:                        { NUMTOKEN,     7,      " %c000 feet",  setalt  },
                    153:                        { HELPTOKEN,    12,     " +-cd[0-9]",   NULL    }},
                    154:        state4[] = {    { '@',          9,      " at",          NULL    },
                    155:                        { 'a',          9,      " at",          NULL    },
                    156:                        { RETTOKEN,     -1,     "",             NULL    },
                    157:                        { HELPTOKEN,    12,     " @a<ret>",     NULL    }},
                    158:        state5[] = {    { NUMTOKEN,     7,      "%c",           delayb  },
                    159:                        { HELPTOKEN,    12,     " [0-9]",       NULL    }},
                    160:        state6[] = {    { '@',          9,      " at",          NULL    },
                    161:                        { 'a',          9,      " at",          NULL    },
                    162:                        { 'w',          4,      " 0",           rel_dir },
                    163:                        { 'e',          4,      " 45",          rel_dir },
                    164:                        { 'd',          4,      " 90",          rel_dir },
                    165:                        { 'c',          4,      " 135",         rel_dir },
                    166:                        { 'x',          4,      " 180",         rel_dir },
                    167:                        { 'z',          4,      " 225",         rel_dir },
                    168:                        { 'a',          4,      " 270",         rel_dir },
                    169:                        { 'q',          4,      " 315",         rel_dir },
                    170:                        { RETTOKEN,     -1,     "",             NULL    },
                    171:                        { HELPTOKEN,    12,     " @a<dir><ret>",NULL    }},
                    172:        state7[] = {    { RETTOKEN,     -1,     "",             NULL    },
                    173:                        { HELPTOKEN,    12,     " <ret>",       NULL    }},
                    174:        state8[] = {    { NUMTOKEN,     4,      "%c",           benum   },
                    175:                        { HELPTOKEN,    12,     " [0-9]",       NULL    }},
                    176:        state9[] = {    { 'b',          5,      " beacon #",    NULL    },
                    177:                        { '*',          5,      " beacon #",    NULL    },
                    178:                        { HELPTOKEN,    12,     " b*",          NULL    }},
                    179:        state10[] = {   { NUMTOKEN,     7,      " %c000 ft",    setrelalt},
                    180:                        { HELPTOKEN,    12,     " [0-9]",       NULL    }},
                    181:        state11[] = {   { 'b',          8,      " beacon #",    beacon  },
                    182:                        { '*',          8,      " beacon #",    beacon  },
                    183:                        { 'e',          8,      " exit #",      ex_it   },
                    184:                        { 'a',          8,      " airport #",   airport },
                    185:                        { HELPTOKEN,    12,     " b*ea",        NULL    }},
                    186:        state12[] = {   { -1,           -1,     "",             NULL    }};
                    187:
                    188: #define DEF_STATE(s)   { NUMELS(s),    (s)     }
                    189:
1.24      dholland  190: static STATE st[] = {
1.1       cgd       191:        DEF_STATE(state0), DEF_STATE(state1), DEF_STATE(state2),
                    192:        DEF_STATE(state3), DEF_STATE(state4), DEF_STATE(state5),
                    193:        DEF_STATE(state6), DEF_STATE(state7), DEF_STATE(state8),
                    194:        DEF_STATE(state9), DEF_STATE(state10), DEF_STATE(state11),
                    195:        DEF_STATE(state12)
                    196: };
                    197:
1.24      dholland  198: static PLANE p;
                    199: static STACK stack[MAXDEPTH];
                    200: static int level;
                    201: static int tval;
1.24.6.1! yamt      202: static int dir;
        !           203: static enum places dest_type;
        !           204: static unsigned dest_no;
1.1       cgd       205:
1.24      dholland  206: static int
1.18      jmc       207: pop(void)
1.1       cgd       208: {
                    209:        if (level == 0)
                    210:                return (-1);
                    211:        level--;
                    212:
                    213:        ioclrtoeol(T_POS);
                    214:
1.19      rpaulo    215:        (void)strcpy(T_STR, "");
1.1       cgd       216:        T_RULE = -1;
                    217:        T_CH = -1;
                    218:        return (0);
                    219: }
                    220:
1.24      dholland  221: static void
1.18      jmc       222: rezero(void)
1.1       cgd       223: {
                    224:        iomove(0);
                    225:
                    226:        level = 0;
                    227:        T_STATE = 0;
                    228:        T_RULE = -1;
                    229:        T_CH = -1;
                    230:        T_POS = 0;
1.19      rpaulo    231:        (void)strcpy(T_STR, "");
1.1       cgd       232: }
                    233:
1.24      dholland  234: static void
1.18      jmc       235: push(int ruleno, int ch)
1.1       cgd       236: {
                    237:        int     newstate, newpos;
                    238:
1.20      jnemeth   239:        assert(level < (MAXDEPTH - 1));
1.23      dholland  240:        (void)snprintf(T_STR, sizeof(T_STR),
                    241:                st[T_STATE].rule[ruleno].str, tval);
1.1       cgd       242:        T_RULE = ruleno;
                    243:        T_CH = ch;
                    244:        newstate = st[T_STATE].rule[ruleno].to_state;
                    245:        newpos = T_POS + strlen(T_STR);
                    246:
                    247:        ioaddstr(T_POS, T_STR);
                    248:
                    249:        if (level == 0)
                    250:                ioclrtobot();
                    251:        level++;
                    252:        T_STATE = newstate;
                    253:        T_POS = newpos;
                    254:        T_RULE = -1;
1.19      rpaulo    255:        (void)strcpy(T_STR, "");
1.1       cgd       256: }
                    257:
1.6       lukem     258: int
1.18      jmc       259: getcommand(void)
1.1       cgd       260: {
                    261:        int     c, i, done;
1.18      jmc       262:        const char      *s, *(*func)(int);
1.1       cgd       263:        PLANE   *pp;
                    264:
                    265:        rezero();
                    266:
                    267:        do {
                    268:                c = gettoken();
1.4       mycroft   269:                if (c == tty_new.c_cc[VERASE]) {
1.1       cgd       270:                        if (pop() < 0)
                    271:                                noise();
1.4       mycroft   272:                } else if (c == tty_new.c_cc[VKILL]) {
1.1       cgd       273:                        while (pop() >= 0)
                    274:                                ;
                    275:                } else {
                    276:                        done = 0;
                    277:                        for (i = 0; i < st[T_STATE].num_rules; i++) {
                    278:                                if (st[T_STATE].rule[i].token == c ||
                    279:                                    st[T_STATE].rule[i].token == tval) {
                    280:                                        push(i, (c >= ALPHATOKEN) ? tval : c);
                    281:                                        done = 1;
                    282:                                        break;
                    283:                                }
                    284:                        }
                    285:                        if (!done)
                    286:                                noise();
                    287:                }
                    288:        } while (T_STATE != -1);
                    289:
                    290:        if (level == 1)
                    291:                return (1);     /* forced update */
                    292:
                    293:        dest_type = T_NODEST;
                    294:
                    295:        for (i = 0; i < level; i++) {
                    296:                func = st[stack[i].state].rule[stack[i].rule].func;
                    297:                if (func != NULL)
                    298:                        if ((s = (*func)(stack[i].ch)) != NULL) {
1.19      rpaulo    299:                                ioerror(stack[i].pos,
                    300:                                    (int)strlen(stack[i].str), s);
1.1       cgd       301:                                return (-1);
                    302:                        }
                    303:        }
                    304:
                    305:        pp = findplane(p.plane_no);
                    306:        if (pp->new_altitude != p.new_altitude)
                    307:                pp->new_altitude = p.new_altitude;
                    308:        else if (pp->status != p.status)
                    309:                pp->status = p.status;
                    310:        else {
                    311:                pp->new_dir = p.new_dir;
                    312:                pp->delayd = p.delayd;
                    313:                pp->delayd_no = p.delayd_no;
                    314:        }
                    315:        return (0);
                    316: }
                    317:
1.24      dholland  318: static void
1.18      jmc       319: noise(void)
1.1       cgd       320: {
1.19      rpaulo    321:        (void)putchar('\07');
                    322:        (void)fflush(stdout);
1.1       cgd       323: }
                    324:
1.24      dholland  325: static int
1.18      jmc       326: gettoken(void)
1.1       cgd       327: {
                    328:        while ((tval = getAChar()) == REDRAWTOKEN || tval == SHELLTOKEN)
                    329:        {
                    330:                if (tval == SHELLTOKEN)
                    331:                {
                    332: #ifdef BSD
                    333:                        struct itimerval        itv;
                    334:                        itv.it_value.tv_sec = 0;
                    335:                        itv.it_value.tv_usec = 0;
1.19      rpaulo    336:                        (void)setitimer(ITIMER_REAL, &itv, NULL);
1.1       cgd       337: #endif
                    338: #ifdef SYSV
                    339:                        int aval;
                    340:                        aval = alarm(0);
                    341: #endif
                    342:                        if (fork() == 0)        /* child */
                    343:                        {
1.6       lukem     344:                                char *shell, *base;
1.1       cgd       345:
                    346:                                done_screen();
                    347:
                    348:                                                 /* run user's favorite shell */
                    349:                                if ((shell = getenv("SHELL")) != NULL)
                    350:                                {
                    351:                                        base = strrchr(shell, '/');
                    352:                                        if (base == NULL)
                    353:                                                base = shell;
                    354:                                        else
                    355:                                                base++;
1.19      rpaulo    356:                                        (void)execl(shell, base, (char *) 0);
1.1       cgd       357:                                }
                    358:                                else
1.19      rpaulo    359:                                        (void)execl(_PATH_BSHELL, "sh",
                    360:                                            (char *) 0);
1.1       cgd       361:
                    362:                                exit(0);        /* oops */
                    363:                        }
                    364:
1.19      rpaulo    365:                        (void)wait(0);
                    366:                        (void)tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
1.1       cgd       367: #ifdef BSD
                    368:                        itv.it_value.tv_sec = 0;
                    369:                        itv.it_value.tv_usec = 1;
                    370:                        itv.it_interval.tv_sec = sp->update_secs;
                    371:                        itv.it_interval.tv_usec = 0;
1.19      rpaulo    372:                        (void)setitimer(ITIMER_REAL, &itv, NULL);
1.1       cgd       373: #endif
                    374: #ifdef SYSV
                    375:                        alarm(aval);
                    376: #endif
                    377:                }
1.19      rpaulo    378:                (void)redraw();
1.1       cgd       379:        }
                    380:
                    381:        if (isdigit(tval))
                    382:                return (NUMTOKEN);
                    383:        else if (isalpha(tval))
                    384:                return (ALPHATOKEN);
                    385:        else
                    386:                return (tval);
                    387: }
                    388:
1.24      dholland  389: static const char *
1.18      jmc       390: setplane(int c)
1.1       cgd       391: {
                    392:        PLANE   *pp;
                    393:
                    394:        pp = findplane(number(c));
                    395:        if (pp == NULL)
                    396:                return ("Unknown Plane");
1.19      rpaulo    397:        (void)memcpy(&p, pp, sizeof (p));
1.24.6.1! yamt      398:        p.delayd = false;
1.1       cgd       399:        return (NULL);
                    400: }
                    401:
1.19      rpaulo    402: /* ARGSUSED */
1.24      dholland  403: static const char *
1.22      perry     404: turn(int c __unused)
1.1       cgd       405: {
                    406:        if (p.altitude == 0)
                    407:                return ("Planes at airports may not change direction");
                    408:        return (NULL);
                    409: }
                    410:
1.19      rpaulo    411: /* ARGSUSED */
1.24      dholland  412: static const char *
1.22      perry     413: circle(int c __unused)
1.1       cgd       414: {
                    415:        if (p.altitude == 0)
                    416:                return ("Planes cannot circle on the ground");
                    417:        p.new_dir = MAXDIR;
                    418:        return (NULL);
                    419: }
                    420:
1.19      rpaulo    421: /* ARGSUSED */
1.24      dholland  422: static const char *
1.22      perry     423: left(int c __unused)
1.1       cgd       424: {
                    425:        dir = D_LEFT;
                    426:        p.new_dir = p.dir - 1;
                    427:        if (p.new_dir < 0)
                    428:                p.new_dir += MAXDIR;
                    429:        return (NULL);
                    430: }
                    431:
1.19      rpaulo    432: /* ARGSUSED */
1.24      dholland  433: static const char *
1.22      perry     434: right(int c __unused)
1.1       cgd       435: {
                    436:        dir = D_RIGHT;
                    437:        p.new_dir = p.dir + 1;
1.9       briggs    438:        if (p.new_dir >= MAXDIR)
1.1       cgd       439:                p.new_dir -= MAXDIR;
                    440:        return (NULL);
                    441: }
                    442:
1.19      rpaulo    443: /* ARGSUSED */
1.24      dholland  444: static const char *
1.22      perry     445: Left(int c __unused)
1.1       cgd       446: {
                    447:        p.new_dir = p.dir - 2;
                    448:        if (p.new_dir < 0)
                    449:                p.new_dir += MAXDIR;
                    450:        return (NULL);
                    451: }
                    452:
1.19      rpaulo    453: /* ARGSUSED */
1.24      dholland  454: static const char *
1.22      perry     455: Right(int c __unused)
1.1       cgd       456: {
                    457:        p.new_dir = p.dir + 2;
1.8       briggs    458:        if (p.new_dir >= MAXDIR)
1.1       cgd       459:                p.new_dir -= MAXDIR;
                    460:        return (NULL);
                    461: }
                    462:
1.24      dholland  463: static const char *
1.24.6.1! yamt      464: delayb(int ch)
1.1       cgd       465: {
                    466:        int     xdiff, ydiff;
1.24.6.1! yamt      467:        unsigned bn;
1.1       cgd       468:
1.24.6.1! yamt      469:        bn = ch -= '0';
1.1       cgd       470:
1.24.6.1! yamt      471:        if (bn >= sp->num_beacons)
1.1       cgd       472:                return ("Unknown beacon");
1.24.6.1! yamt      473:        xdiff = sp->beacon[bn].x - p.xpos;
1.1       cgd       474:        xdiff = SGN(xdiff);
1.24.6.1! yamt      475:        ydiff = sp->beacon[bn].y - p.ypos;
1.1       cgd       476:        ydiff = SGN(ydiff);
                    477:        if (xdiff != displacement[p.dir].dx || ydiff != displacement[p.dir].dy)
                    478:                return ("Beacon is not in flight path");
1.24.6.1! yamt      479:        p.delayd = true;
        !           480:        p.delayd_no = bn;
1.1       cgd       481:
                    482:        if (dest_type != T_NODEST) {
                    483:                switch (dest_type) {
                    484:                case T_BEACON:
1.24.6.1! yamt      485:                        xdiff = sp->beacon[dest_no].x - sp->beacon[bn].x;
        !           486:                        ydiff = sp->beacon[dest_no].y - sp->beacon[bn].y;
1.1       cgd       487:                        break;
                    488:                case T_EXIT:
1.24.6.1! yamt      489:                        xdiff = sp->exit[dest_no].x - sp->beacon[bn].x;
        !           490:                        ydiff = sp->exit[dest_no].y - sp->beacon[bn].y;
1.1       cgd       491:                        break;
                    492:                case T_AIRPORT:
1.24.6.1! yamt      493:                        xdiff = sp->airport[dest_no].x - sp->beacon[bn].x;
        !           494:                        ydiff = sp->airport[dest_no].y - sp->beacon[bn].y;
1.1       cgd       495:                        break;
                    496:                default:
                    497:                        return ("Bad case in delayb!  Get help!");
                    498:                }
                    499:                if (xdiff == 0 && ydiff == 0)
                    500:                        return ("Would already be there");
                    501:                p.new_dir = DIR_FROM_DXDY(xdiff, ydiff);
                    502:                if (p.new_dir == p.dir)
                    503:                        return ("Already going in that direction");
                    504:        }
                    505:        return (NULL);
                    506: }
                    507:
1.19      rpaulo    508: /* ARGSUSED */
1.24      dholland  509: static const char *
1.22      perry     510: beacon(int c __unused)
1.1       cgd       511: {
                    512:        dest_type = T_BEACON;
                    513:        return (NULL);
                    514: }
                    515:
1.19      rpaulo    516: /* ARGSUSED */
1.24      dholland  517: static const char *
1.22      perry     518: ex_it(int c __unused)
1.1       cgd       519: {
                    520:        dest_type = T_EXIT;
                    521:        return (NULL);
                    522: }
                    523:
1.19      rpaulo    524: /* ARGSUSED */
1.24      dholland  525: static const char *
1.22      perry     526: airport(int c __unused)
1.1       cgd       527: {
                    528:        dest_type = T_AIRPORT;
                    529:        return (NULL);
                    530: }
                    531:
1.19      rpaulo    532: /* ARGSUSED */
1.24      dholland  533: static const char *
1.22      perry     534: climb(int c __unused)
1.1       cgd       535: {
                    536:        dir = D_UP;
                    537:        return (NULL);
                    538: }
                    539:
1.19      rpaulo    540: /* ARGSUSED */
1.24      dholland  541: static const char *
1.22      perry     542: descend(int c __unused)
1.1       cgd       543: {
                    544:        dir = D_DOWN;
                    545:        return (NULL);
                    546: }
                    547:
1.24      dholland  548: static const char *
1.18      jmc       549: setalt(int c)
1.1       cgd       550: {
1.21      elad      551:        int newalt = c - '0';
                    552:        if ((p.altitude == newalt) && (p.new_altitude == p.altitude))
1.1       cgd       553:                return ("Already at that altitude");
1.21      elad      554:        if (p.new_altitude == newalt) {
                    555:                return ("Already going to that altitude");
                    556:        }
                    557:        p.new_altitude = newalt;
1.1       cgd       558:        return (NULL);
                    559: }
                    560:
1.24      dholland  561: static const char *
1.18      jmc       562: setrelalt(int c)
1.1       cgd       563: {
1.21      elad      564:        int newalt;
                    565:
1.1       cgd       566:        if (c == 0)
                    567:                return ("altitude not changed");
                    568:
                    569:        switch (dir) {
                    570:        case D_UP:
1.21      elad      571:                newalt = p.altitude + c - '0';
1.1       cgd       572:                break;
                    573:        case D_DOWN:
1.21      elad      574:                newalt = p.altitude - (c - '0');
1.1       cgd       575:                break;
                    576:        default:
                    577:                return ("Unknown case in setrelalt!  Get help!");
                    578:        }
1.21      elad      579:
                    580:        if (p.new_altitude == newalt)
                    581:                return ("Already going to that altitude");
                    582:
                    583:        p.new_altitude = newalt;
                    584:
1.1       cgd       585:        if (p.new_altitude < 0)
                    586:                return ("Altitude would be too low");
                    587:        else if (p.new_altitude > 9)
                    588:                return ("Altitude would be too high");
                    589:        return (NULL);
                    590: }
                    591:
1.24      dholland  592: static const char *
1.24.6.1! yamt      593: benum(int ch)
1.1       cgd       594: {
1.24.6.1! yamt      595:        unsigned n;
        !           596:
        !           597:        n = ch - '0';
        !           598:        dest_no = n;
1.1       cgd       599:
                    600:        switch (dest_type) {
                    601:        case T_BEACON:
1.24.6.1! yamt      602:                if (n >= sp->num_beacons)
1.1       cgd       603:                        return ("Unknown beacon");
1.24.6.1! yamt      604:                p.new_dir = DIR_FROM_DXDY(sp->beacon[n].x - p.xpos,
        !           605:                        sp->beacon[n].y - p.ypos);
1.1       cgd       606:                break;
                    607:        case T_EXIT:
1.24.6.1! yamt      608:                if (n >= sp->num_exits)
1.1       cgd       609:                        return ("Unknown exit");
1.24.6.1! yamt      610:                p.new_dir = DIR_FROM_DXDY(sp->exit[n].x - p.xpos,
        !           611:                        sp->exit[n].y - p.ypos);
1.1       cgd       612:                break;
                    613:        case T_AIRPORT:
1.24.6.1! yamt      614:                if (n >= sp->num_airports)
1.1       cgd       615:                        return ("Unknown airport");
1.24.6.1! yamt      616:                p.new_dir = DIR_FROM_DXDY(sp->airport[n].x - p.xpos,
        !           617:                        sp->airport[n].y - p.ypos);
1.1       cgd       618:                break;
                    619:        default:
                    620:                return ("Unknown case in benum!  Get help!");
                    621:        }
                    622:        return (NULL);
                    623: }
                    624:
1.24      dholland  625: static const char *
1.18      jmc       626: to_dir(int c)
1.1       cgd       627: {
                    628:        p.new_dir = dir_no(c);
                    629:        return (NULL);
                    630: }
                    631:
1.24      dholland  632: static const char *
1.18      jmc       633: rel_dir(int c)
1.1       cgd       634: {
                    635:        int     angle;
                    636:
                    637:        angle = dir_no(c);
                    638:        switch (dir) {
                    639:        case D_LEFT:
                    640:                p.new_dir = p.dir - angle;
                    641:                if (p.new_dir < 0)
                    642:                        p.new_dir += MAXDIR;
                    643:                break;
                    644:        case D_RIGHT:
                    645:                p.new_dir = p.dir + angle;
                    646:                if (p.new_dir >= MAXDIR)
                    647:                        p.new_dir -= MAXDIR;
                    648:                break;
                    649:        default:
                    650:                return ("Bizarre direction in rel_dir!  Get help!");
                    651:        }
                    652:        return (NULL);
                    653: }
                    654:
1.19      rpaulo    655: /* ARGSUSED */
1.24      dholland  656: static const char *
1.22      perry     657: mark(int c __unused)
1.1       cgd       658: {
                    659:        if (p.altitude == 0)
                    660:                return ("Cannot mark planes on the ground");
                    661:        if (p.status == S_MARKED)
                    662:                return ("Already marked");
                    663:        p.status = S_MARKED;
                    664:        return (NULL);
                    665: }
                    666:
1.19      rpaulo    667: /* ARGSUSED */
1.24      dholland  668: static const char *
1.22      perry     669: unmark(int c __unused)
1.1       cgd       670: {
                    671:        if (p.altitude == 0)
                    672:                return ("Cannot unmark planes on the ground");
                    673:        if (p.status == S_UNMARKED)
                    674:                return ("Already unmarked");
                    675:        p.status = S_UNMARKED;
                    676:        return (NULL);
                    677: }
                    678:
1.19      rpaulo    679: /* ARGSUSED */
1.24      dholland  680: static const char *
1.22      perry     681: ignore(int c __unused)
1.1       cgd       682: {
                    683:        if (p.altitude == 0)
                    684:                return ("Cannot ignore planes on the ground");
                    685:        if (p.status == S_IGNORED)
                    686:                return ("Already ignored");
                    687:        p.status = S_IGNORED;
                    688:        return (NULL);
                    689: }
                    690:
1.6       lukem     691: int
1.18      jmc       692: dir_no(int ch)
1.1       cgd       693: {
1.18      jmc       694:        int     dirno;
1.1       cgd       695:
1.18      jmc       696:        dirno = -1;
1.1       cgd       697:        switch (ch) {
1.18      jmc       698:        case 'w':       dirno = 0;      break;
                    699:        case 'e':       dirno = 1;      break;
                    700:        case 'd':       dirno = 2;      break;
                    701:        case 'c':       dirno = 3;      break;
                    702:        case 'x':       dirno = 4;      break;
                    703:        case 'z':       dirno = 5;      break;
                    704:        case 'a':       dirno = 6;      break;
                    705:        case 'q':       dirno = 7;      break;
1.1       cgd       706:        default:
1.19      rpaulo    707:                (void)fprintf(stderr, "bad character in dir_no\n");
1.1       cgd       708:                break;
                    709:        }
1.18      jmc       710:        return (dirno);
1.1       cgd       711: }

CVSweb <webmaster@jp.NetBSD.org>