home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / y / bsdgames / bsd-game.000 / bsd-game / games / robots / move.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-21  |  6.2 KB  |  300 lines

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