Home | History | Annotate | Line # | Download | only in robots
move.c revision 1.11
      1 /*	$NetBSD: move.c,v 1.11 2003/08/07 09:37:36 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)move.c	8.1 (Berkeley) 5/31/93";
     36 #else
     37 __RCSID("$NetBSD: move.c,v 1.11 2003/08/07 09:37:36 agc Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include "robots.h"
     42 
     43 # define	ESC	'\033'
     44 
     45 /*
     46  * get_move:
     47  *	Get and execute a move from the player
     48  */
     49 void
     50 get_move()
     51 {
     52 	int		c;
     53 #ifdef FANCY
     54 	int		lastmove;
     55 #endif /*FANCY*/
     56 
     57 	if (Waiting)
     58 		return;
     59 
     60 #ifdef	FANCY
     61 	if (Pattern_roll) {
     62 		if (Next_move >= Move_list)
     63 			lastmove = *Next_move;
     64 		else
     65 			lastmove = -1;	/* flag for "first time in" */
     66 	} else
     67 		lastmove = 0; /* Shut up gcc */
     68 #endif
     69 	for (;;) {
     70 		if (Teleport && must_telep())
     71 			goto teleport;
     72 		if (Running)
     73 			c = Run_ch;
     74 		else if (Count != 0)
     75 			c = Cnt_move;
     76 #ifdef	FANCY
     77 		else if (Num_robots > 1 && Stand_still)
     78 			c = '>';
     79 		else if (Num_robots > 1 && Pattern_roll) {
     80 			if (*++Next_move == '\0') {
     81 				if (lastmove < 0)
     82 					goto over;
     83 				Next_move = Move_list;
     84 			}
     85 			c = *Next_move;
     86 			mvaddch(0, 0, c);
     87 			if (c == lastmove)
     88 				goto over;
     89 		}
     90 #endif
     91 		else {
     92 over:
     93 			if (Auto_bot)
     94 				c = automove();
     95 			else
     96 				c = getchar();
     97 			if (isdigit(c)) {
     98 				Count = (c - '0');
     99 				while (isdigit(c = getchar()))
    100 					Count = Count * 10 + (c - '0');
    101 				if (c == ESC)
    102 					goto over;
    103 				Cnt_move = c;
    104 				if (Count)
    105 					leaveok(stdscr, TRUE);
    106 			}
    107 		}
    108 
    109 		switch (c) {
    110 		  case ' ':
    111 		  case '.':
    112 			if (do_move(0, 0))
    113 				goto ret;
    114 			break;
    115 		  case 'y':
    116 			if (do_move(-1, -1))
    117 				goto ret;
    118 			break;
    119 		  case 'k':
    120 			if (do_move(-1, 0))
    121 				goto ret;
    122 			break;
    123 		  case 'u':
    124 			if (do_move(-1, 1))
    125 				goto ret;
    126 			break;
    127 		  case 'h':
    128 			if (do_move(0, -1))
    129 				goto ret;
    130 			break;
    131 		  case 'l':
    132 			if (do_move(0, 1))
    133 				goto ret;
    134 			break;
    135 		  case 'b':
    136 			if (do_move(1, -1))
    137 				goto ret;
    138 			break;
    139 		  case 'j':
    140 			if (do_move(1, 0))
    141 				goto ret;
    142 			break;
    143 		  case 'n':
    144 			if (do_move(1, 1))
    145 				goto ret;
    146 			break;
    147 		  case 'Y': case 'U': case 'H': case 'J':
    148 		  case 'K': case 'L': case 'B': case 'N':
    149 		  case '>':
    150 			Running = TRUE;
    151 			if (c == '>')
    152 				Run_ch = ' ';
    153 			else
    154 				Run_ch = tolower(c);
    155 			leaveok(stdscr, TRUE);
    156 			break;
    157 		  case 'q':
    158 		  case 'Q':
    159 			if (query("Really quit?"))
    160 				quit(0);
    161 			refresh();
    162 			break;
    163 		  case 'w':
    164 		  case 'W':
    165 			Waiting = TRUE;
    166 			leaveok(stdscr, TRUE);
    167 			goto ret;
    168 		  case 't':
    169 		  case 'T':
    170 teleport:
    171 			Running = FALSE;
    172 			mvaddch(My_pos.y, My_pos.x, ' ');
    173 			My_pos = *rnd_pos();
    174 			telmsg(1);
    175 			refresh();
    176 			sleep(1);
    177 			telmsg(0);
    178 			mvaddch(My_pos.y, My_pos.x, PLAYER);
    179 			leaveok(stdscr, FALSE);
    180 			refresh();
    181 			flush_in();
    182 			goto ret;
    183 		  case CTRL('L'):
    184 			wrefresh(curscr);
    185 			break;
    186 		  case EOF:
    187 			break;
    188 		  default:
    189 			putchar(CTRL('G'));
    190 			reset_count();
    191 			fflush(stdout);
    192 			break;
    193 		}
    194 	}
    195 ret:
    196 	if (Count > 0)
    197 		if (--Count == 0)
    198 			leaveok(stdscr, FALSE);
    199 }
    200 
    201 /*
    202  * must_telep:
    203  *	Must I teleport; i.e., is there anywhere I can move without
    204  * being eaten?
    205  */
    206 bool
    207 must_telep()
    208 {
    209 	int		x, y;
    210 	static COORD	newpos;
    211 
    212 #ifdef	FANCY
    213 	if (Stand_still && Num_robots > 1 && eaten(&My_pos))
    214 		return TRUE;
    215 #endif
    216 
    217 	for (y = -1; y <= 1; y++) {
    218 		newpos.y = My_pos.y + y;
    219 		if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE)
    220 			continue;
    221 		for (x = -1; x <= 1; x++) {
    222 			newpos.x = My_pos.x + x;
    223 			if (newpos.x <= 0 || newpos.x >= X_FIELDSIZE)
    224 				continue;
    225 			if (Field[newpos.y][newpos.x] > 0)
    226 				continue;
    227 			if (!eaten(&newpos))
    228 				return FALSE;
    229 		}
    230 	}
    231 	return TRUE;
    232 }
    233 
    234 /*
    235  * do_move:
    236  *	Execute a move
    237  */
    238 bool
    239 do_move(dy, dx)
    240 	int	dy, dx;
    241 {
    242 	static COORD	newpos;
    243 
    244 	newpos.y = My_pos.y + dy;
    245 	newpos.x = My_pos.x + dx;
    246 	if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE ||
    247 	    newpos.x <= 0 || newpos.x >= X_FIELDSIZE ||
    248 	    Field[newpos.y][newpos.x] > 0 || eaten(&newpos)) {
    249 		if (Running) {
    250 			Running = FALSE;
    251 			leaveok(stdscr, FALSE);
    252 			move(My_pos.y, My_pos.x);
    253 			refresh();
    254 		}
    255 		else {
    256 			putchar(CTRL('G'));
    257 			reset_count();
    258 		}
    259 		return FALSE;
    260 	}
    261 	else if (dy == 0 && dx == 0)
    262 		return TRUE;
    263 	mvaddch(My_pos.y, My_pos.x, ' ');
    264 	My_pos = newpos;
    265 	mvaddch(My_pos.y, My_pos.x, PLAYER);
    266 	if (!jumping())
    267 		refresh();
    268 	return TRUE;
    269 }
    270 
    271 /*
    272  * eaten:
    273  *	Player would get eaten at this place
    274  */
    275 bool
    276 eaten(pos)
    277 	const COORD	*pos;
    278 {
    279 	int	x, y;
    280 
    281 	for (y = pos->y - 1; y <= pos->y + 1; y++) {
    282 		if (y <= 0 || y >= Y_FIELDSIZE)
    283 			continue;
    284 		for (x = pos->x - 1; x <= pos->x + 1; x++) {
    285 			if (x <= 0 || x >= X_FIELDSIZE)
    286 				continue;
    287 			if (Field[y][x] == 1)
    288 				return TRUE;
    289 		}
    290 	}
    291 	return FALSE;
    292 }
    293 
    294 /*
    295  * reset_count:
    296  *	Reset the count variables
    297  */
    298 void
    299 reset_count()
    300 {
    301 	Count = 0;
    302 	Running = FALSE;
    303 	leaveok(stdscr, FALSE);
    304 	refresh();
    305 }
    306 
    307 /*
    308  * jumping:
    309  *	See if we are jumping, i.e., we should not refresh.
    310  */
    311 bool
    312 jumping()
    313 {
    314 	return (Jump && (Count || Running || Waiting));
    315 }
    316