Annotation of 43BSDReno/games/robots/move.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1980 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)move.c     5.4 (Berkeley) 6/1/90";
                     22: #endif /* not lint */
                     23: 
                     24: # include      "robots.h"
                     25: # include      <ctype.h>
                     26: 
                     27: # define       ESC     '\033'
                     28: 
                     29: /*
                     30:  * get_move:
                     31:  *     Get and execute a move from the player
                     32:  */
                     33: get_move()
                     34: {
                     35:        register int    c;
                     36:        register int    y, x, lastmove;
                     37:        static COORD    newpos;
                     38: 
                     39:        if (Waiting)
                     40:                return;
                     41: 
                     42: #ifdef FANCY
                     43:        if (Pattern_roll) {
                     44:                if (Next_move >= Move_list)
                     45:                        lastmove = *Next_move;
                     46:                else
                     47:                        lastmove = -1;  /* flag for "first time in" */
                     48:        }
                     49: #endif
                     50:        for (;;) {
                     51:                if (Teleport && must_telep())
                     52:                        goto teleport;
                     53:                if (Running)
                     54:                        c = Run_ch;
                     55:                else if (Count != 0)
                     56:                        c = Cnt_move;
                     57: #ifdef FANCY
                     58:                else if (Num_robots > 1 && Stand_still)
                     59:                        c = '>';
                     60:                else if (Num_robots > 1 && Pattern_roll) {
                     61:                        if (*++Next_move == '\0') {
                     62:                                if (lastmove < 0)
                     63:                                        goto over;
                     64:                                Next_move = Move_list;
                     65:                        }
                     66:                        c = *Next_move;
                     67:                        mvaddch(0, 0, c);
                     68:                        if (c == lastmove)
                     69:                                goto over;
                     70:                }
                     71: #endif
                     72:                else {
                     73: over:
                     74:                        c = getchar();
                     75:                        if (isdigit(c)) {
                     76:                                Count = (c - '0');
                     77:                                while (isdigit(c = getchar()))
                     78:                                        Count = Count * 10 + (c - '0');
                     79:                                if (c == ESC)
                     80:                                        goto over;
                     81:                                Cnt_move = c;
                     82:                                if (Count)
                     83:                                        leaveok(stdscr, TRUE);
                     84:                        }
                     85:                }
                     86: 
                     87:                switch (c) {
                     88:                  case ' ':
                     89:                  case '.':
                     90:                        if (do_move(0, 0))
                     91:                                goto ret;
                     92:                        break;
                     93:                  case 'y':
                     94:                        if (do_move(-1, -1))
                     95:                                goto ret;
                     96:                        break;
                     97:                  case 'k':
                     98:                        if (do_move(-1, 0))
                     99:                                goto ret;
                    100:                        break;
                    101:                  case 'u':
                    102:                        if (do_move(-1, 1))
                    103:                                goto ret;
                    104:                        break;
                    105:                  case 'h':
                    106:                        if (do_move(0, -1))
                    107:                                goto ret;
                    108:                        break;
                    109:                  case 'l':
                    110:                        if (do_move(0, 1))
                    111:                                goto ret;
                    112:                        break;
                    113:                  case 'b':
                    114:                        if (do_move(1, -1))
                    115:                                goto ret;
                    116:                        break;
                    117:                  case 'j':
                    118:                        if (do_move(1, 0))
                    119:                                goto ret;
                    120:                        break;
                    121:                  case 'n':
                    122:                        if (do_move(1, 1))
                    123:                                goto ret;
                    124:                        break;
                    125:                  case 'Y': case 'U': case 'H': case 'J':
                    126:                  case 'K': case 'L': case 'B': case 'N':
                    127:                  case '>':
                    128:                        Running = TRUE;
                    129:                        if (c == '>')
                    130:                                Run_ch = ' ';
                    131:                        else
                    132:                                Run_ch = tolower(c);
                    133:                        leaveok(stdscr, TRUE);
                    134:                        break;
                    135:                  case 'q':
                    136:                  case 'Q':
                    137:                        if (query("Really quit?"))
                    138:                                quit();
                    139:                        refresh();
                    140:                        break;
                    141:                  case 'w':
                    142:                  case 'W':
                    143:                        Waiting = TRUE;
                    144:                        leaveok(stdscr, TRUE);
                    145:                        flushok(stdscr, FALSE);
                    146:                        goto ret;
                    147:                  case 't':
                    148:                  case 'T':
                    149: teleport:
                    150:                        Running = FALSE;
                    151:                        mvaddch(My_pos.y, My_pos.x, ' ');
                    152:                        My_pos = *rnd_pos();
                    153:                        mvaddch(My_pos.y, My_pos.x, PLAYER);
                    154:                        leaveok(stdscr, FALSE);
                    155:                        refresh();
                    156:                        flush_in();
                    157:                        goto ret;
                    158:                  case CTRL(L):
                    159:                        wrefresh(curscr);
                    160:                        break;
                    161:                  case EOF:
                    162:                        break;
                    163:                  default:
                    164:                        putchar(CTRL(G));
                    165:                        reset_count();
                    166:                        fflush(stdout);
                    167:                        break;
                    168:                }
                    169:        }
                    170: ret:
                    171:        if (Count > 0)
                    172:                if (--Count == 0)
                    173:                        leaveok(stdscr, FALSE);
                    174: }
                    175: 
                    176: /*
                    177:  * must_telep:
                    178:  *     Must I teleport; i.e., is there anywhere I can move without
                    179:  * being eaten?
                    180:  */
                    181: must_telep()
                    182: {
                    183:        register int    x, y;
                    184:        static COORD    newpos;
                    185: 
                    186: #ifdef FANCY
                    187:        if (Stand_still && Num_robots > 1 && eaten(&My_pos))
                    188:                return TRUE;
                    189: #endif
                    190: 
                    191:        for (y = -1; y <= 1; y++) {
                    192:                newpos.y = My_pos.y + y;
                    193:                if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE)
                    194:                        continue;
                    195:                for (x = -1; x <= 1; x++) {
                    196:                        newpos.x = My_pos.x + x;
                    197:                        if (newpos.x <= 0 || newpos.x >= X_FIELDSIZE)
                    198:                                continue;
                    199:                        if (Field[newpos.y][newpos.x] > 0)
                    200:                                continue;
                    201:                        if (!eaten(&newpos))
                    202:                                return FALSE;
                    203:                }
                    204:        }
                    205:        return TRUE;
                    206: }
                    207: 
                    208: /*
                    209:  * do_move:
                    210:  *     Execute a move
                    211:  */
                    212: do_move(dy, dx)
                    213: int    dy, dx;
                    214: {
                    215:        static COORD    newpos;
                    216: 
                    217:        newpos.y = My_pos.y + dy;
                    218:        newpos.x = My_pos.x + dx;
                    219:        if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE ||
                    220:            newpos.x <= 0 || newpos.x >= X_FIELDSIZE ||
                    221:            Field[newpos.y][newpos.x] > 0 || eaten(&newpos)) {
                    222:                if (Running) {
                    223:                        Running = FALSE;
                    224:                        leaveok(stdscr, FALSE);
                    225:                        move(My_pos.y, My_pos.x);
                    226:                        refresh();
                    227:                }
                    228:                else {
                    229:                        putchar(CTRL(G));
                    230:                        reset_count();
                    231:                }
                    232:                return FALSE;
                    233:        }
                    234:        else if (dy == 0 && dx == 0)
                    235:                return TRUE;
                    236:        mvaddch(My_pos.y, My_pos.x, ' ');
                    237:        My_pos = newpos;
                    238:        mvaddch(My_pos.y, My_pos.x, PLAYER);
                    239:        if (!jumping())
                    240:                refresh();
                    241:        return TRUE;
                    242: }
                    243: 
                    244: /*
                    245:  * eaten:
                    246:  *     Player would get eaten at this place
                    247:  */
                    248: eaten(pos)
                    249: register COORD *pos;
                    250: {
                    251:        register int    x, y;
                    252: 
                    253:        for (y = pos->y - 1; y <= pos->y + 1; y++) {
                    254:                if (y <= 0 || y >= Y_FIELDSIZE)
                    255:                        continue;
                    256:                for (x = pos->x - 1; x <= pos->x + 1; x++) {
                    257:                        if (x <= 0 || x >= X_FIELDSIZE)
                    258:                                continue;
                    259:                        if (Field[y][x] == 1)
                    260:                                return TRUE;
                    261:                }
                    262:        }
                    263:        return FALSE;
                    264: }
                    265: 
                    266: /*
                    267:  * reset_count:
                    268:  *     Reset the count variables
                    269:  */
                    270: reset_count()
                    271: {
                    272:        Count = 0;
                    273:        Running = FALSE;
                    274:        leaveok(stdscr, FALSE);
                    275:        refresh();
                    276: }
                    277: 
                    278: /*
                    279:  * jumping:
                    280:  *     See if we are jumping, i.e., we should not refresh.
                    281:  */
                    282: jumping()
                    283: {
                    284:        return (Jump && (Count || Running || Waiting));
                    285: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.