xref: /netbsd-src/games/robots/move.c (revision 5305281b0c6d5ac1e32a81589acdabcb6df90102)
1*5305281bSdholland /*	$NetBSD: move.c,v 1.16 2009/08/12 08:30:55 dholland Exp $	*/
26045e6e0Scgd 
361f28255Scgd /*
46045e6e0Scgd  * Copyright (c) 1980, 1993
56045e6e0Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
329007937fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
346045e6e0Scgd #if 0
356045e6e0Scgd static char sccsid[] = "@(#)move.c	8.1 (Berkeley) 5/31/93";
366045e6e0Scgd #else
37*5305281bSdholland __RCSID("$NetBSD: move.c,v 1.16 2009/08/12 08:30:55 dholland Exp $");
386045e6e0Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
41a4cc1f4fSdholland #include <sys/types.h>
42a4cc1f4fSdholland #include <sys/ttydefaults.h>  /* for CTRL */
43a4cc1f4fSdholland #include <ctype.h>
44a4cc1f4fSdholland #include <curses.h>
45a4cc1f4fSdholland #include <unistd.h>
466045e6e0Scgd #include "robots.h"
4761f28255Scgd 
4861f28255Scgd #define ESC	'\033'
4961f28255Scgd 
50*5305281bSdholland static bool do_move(int, int);
51*5305281bSdholland static bool eaten(const COORD *);
52*5305281bSdholland static bool must_telep(void);
53*5305281bSdholland 
5461f28255Scgd /*
5561f28255Scgd  * get_move:
5661f28255Scgd  *	Get and execute a move from the player
5761f28255Scgd  */
589007937fSlukem void
get_move(void)5930870bd5Sdholland get_move(void)
6061f28255Scgd {
619007937fSlukem 	int c;
6225ef9825Shubertf #ifdef FANCY
6325ef9825Shubertf 	int lastmove;
6425ef9825Shubertf #endif /*FANCY*/
6561f28255Scgd 
6661f28255Scgd 	if (Waiting)
6761f28255Scgd 		return;
6861f28255Scgd 
6961f28255Scgd #ifdef FANCY
7061f28255Scgd 	if (Pattern_roll) {
7161f28255Scgd 		if (Next_move >= Move_list)
7261f28255Scgd 			lastmove = *Next_move;
7361f28255Scgd 		else
7461f28255Scgd 			lastmove = -1;	/* flag for "first time in" */
7525ef9825Shubertf 	} else
7625ef9825Shubertf 		lastmove = 0; /* Shut up gcc */
7761f28255Scgd #endif
7861f28255Scgd 	for (;;) {
7961f28255Scgd 		if (Teleport && must_telep())
8061f28255Scgd 			goto teleport;
8161f28255Scgd 		if (Running)
8261f28255Scgd 			c = Run_ch;
8361f28255Scgd 		else if (Count != 0)
8461f28255Scgd 			c = Cnt_move;
8561f28255Scgd #ifdef FANCY
8661f28255Scgd 		else if (Num_robots > 1 && Stand_still)
8761f28255Scgd 			c = '>';
8861f28255Scgd 		else if (Num_robots > 1 && Pattern_roll) {
8961f28255Scgd 			if (*++Next_move == '\0') {
9061f28255Scgd 				if (lastmove < 0)
9161f28255Scgd 					goto over;
9261f28255Scgd 				Next_move = Move_list;
9361f28255Scgd 			}
9461f28255Scgd 			c = *Next_move;
9561f28255Scgd 			mvaddch(0, 0, c);
9661f28255Scgd 			if (c == lastmove)
9761f28255Scgd 				goto over;
9861f28255Scgd 		}
9961f28255Scgd #endif
10061f28255Scgd 		else {
10161f28255Scgd over:
102111956c5Schristos 			if (Auto_bot) {
10318dfb39eSchristos 				c = automove();
104111956c5Schristos 				if (!Jump) {
105111956c5Schristos 					usleep(10000);
106111956c5Schristos 					refresh();
107111956c5Schristos 				}
108111956c5Schristos 			} else
10961f28255Scgd 				c = getchar();
11061f28255Scgd 			if (isdigit(c)) {
11161f28255Scgd 				Count = (c - '0');
11261f28255Scgd 				while (isdigit(c = getchar()))
11361f28255Scgd 					Count = Count * 10 + (c - '0');
11461f28255Scgd 				if (c == ESC)
11561f28255Scgd 					goto over;
11661f28255Scgd 				Cnt_move = c;
11761f28255Scgd 				if (Count)
11861f28255Scgd 					leaveok(stdscr, TRUE);
11961f28255Scgd 			}
12061f28255Scgd 		}
12161f28255Scgd 
12261f28255Scgd 		switch (c) {
12361f28255Scgd 		  case ' ':
12461f28255Scgd 		  case '.':
12561f28255Scgd 			if (do_move(0, 0))
12661f28255Scgd 				goto ret;
12761f28255Scgd 			break;
12861f28255Scgd 		  case 'y':
12961f28255Scgd 			if (do_move(-1, -1))
13061f28255Scgd 				goto ret;
13161f28255Scgd 			break;
13261f28255Scgd 		  case 'k':
13361f28255Scgd 			if (do_move(-1, 0))
13461f28255Scgd 				goto ret;
13561f28255Scgd 			break;
13661f28255Scgd 		  case 'u':
13761f28255Scgd 			if (do_move(-1, 1))
13861f28255Scgd 				goto ret;
13961f28255Scgd 			break;
14061f28255Scgd 		  case 'h':
14161f28255Scgd 			if (do_move(0, -1))
14261f28255Scgd 				goto ret;
14361f28255Scgd 			break;
14461f28255Scgd 		  case 'l':
14561f28255Scgd 			if (do_move(0, 1))
14661f28255Scgd 				goto ret;
14761f28255Scgd 			break;
14861f28255Scgd 		  case 'b':
14961f28255Scgd 			if (do_move(1, -1))
15061f28255Scgd 				goto ret;
15161f28255Scgd 			break;
15261f28255Scgd 		  case 'j':
15361f28255Scgd 			if (do_move(1, 0))
15461f28255Scgd 				goto ret;
15561f28255Scgd 			break;
15661f28255Scgd 		  case 'n':
15761f28255Scgd 			if (do_move(1, 1))
15861f28255Scgd 				goto ret;
15961f28255Scgd 			break;
16061f28255Scgd 		  case 'Y': case 'U': case 'H': case 'J':
16161f28255Scgd 		  case 'K': case 'L': case 'B': case 'N':
16261f28255Scgd 		  case '>':
163a4cc1f4fSdholland 			Running = true;
16461f28255Scgd 			if (c == '>')
16561f28255Scgd 				Run_ch = ' ';
16661f28255Scgd 			else
16761f28255Scgd 				Run_ch = tolower(c);
16861f28255Scgd 			leaveok(stdscr, TRUE);
16961f28255Scgd 			break;
17061f28255Scgd 		  case 'q':
17161f28255Scgd 		  case 'Q':
17261f28255Scgd 			if (query("Really quit?"))
1739007937fSlukem 				quit(0);
17461f28255Scgd 			refresh();
17561f28255Scgd 			break;
17661f28255Scgd 		  case 'w':
17761f28255Scgd 		  case 'W':
178a4cc1f4fSdholland 			Waiting = true;
17961f28255Scgd 			leaveok(stdscr, TRUE);
18061f28255Scgd 			goto ret;
18161f28255Scgd 		  case 't':
18261f28255Scgd 		  case 'T':
18361f28255Scgd teleport:
184a4cc1f4fSdholland 			Running = false;
18561f28255Scgd 			mvaddch(My_pos.y, My_pos.x, ' ');
18661f28255Scgd 			My_pos = *rnd_pos();
187fc92c2f7Schristos 			telmsg(1);
188fc92c2f7Schristos 			refresh();
189fc92c2f7Schristos 			sleep(1);
190fc92c2f7Schristos 			telmsg(0);
19161f28255Scgd 			mvaddch(My_pos.y, My_pos.x, PLAYER);
19261f28255Scgd 			leaveok(stdscr, FALSE);
19361f28255Scgd 			refresh();
19461f28255Scgd 			flush_in();
19561f28255Scgd 			goto ret;
196d89727e0Smycroft 		  case CTRL('L'):
197111956c5Schristos 			refresh();
19861f28255Scgd 			break;
19961f28255Scgd 		  case EOF:
20061f28255Scgd 			break;
20161f28255Scgd 		  default:
202d89727e0Smycroft 			putchar(CTRL('G'));
20361f28255Scgd 			reset_count();
20461f28255Scgd 			fflush(stdout);
20561f28255Scgd 			break;
20661f28255Scgd 		}
20761f28255Scgd 	}
20861f28255Scgd ret:
20961f28255Scgd 	if (Count > 0)
21061f28255Scgd 		if (--Count == 0)
21161f28255Scgd 			leaveok(stdscr, FALSE);
21261f28255Scgd }
21361f28255Scgd 
21461f28255Scgd /*
21561f28255Scgd  * must_telep:
21661f28255Scgd  *	Must I teleport; i.e., is there anywhere I can move without
21761f28255Scgd  * being eaten?
21861f28255Scgd  */
219*5305281bSdholland static bool
must_telep(void)22030870bd5Sdholland must_telep(void)
22161f28255Scgd {
2229007937fSlukem 	int x, y;
22361f28255Scgd 	static COORD newpos;
22461f28255Scgd 
22561f28255Scgd #ifdef FANCY
22661f28255Scgd 	if (Stand_still && Num_robots > 1 && eaten(&My_pos))
227a4cc1f4fSdholland 		return true;
22861f28255Scgd #endif
22961f28255Scgd 
23061f28255Scgd 	for (y = -1; y <= 1; y++) {
23161f28255Scgd 		newpos.y = My_pos.y + y;
23261f28255Scgd 		if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE)
23361f28255Scgd 			continue;
23461f28255Scgd 		for (x = -1; x <= 1; x++) {
23561f28255Scgd 			newpos.x = My_pos.x + x;
23661f28255Scgd 			if (newpos.x <= 0 || newpos.x >= X_FIELDSIZE)
23761f28255Scgd 				continue;
23861f28255Scgd 			if (Field[newpos.y][newpos.x] > 0)
23961f28255Scgd 				continue;
24061f28255Scgd 			if (!eaten(&newpos))
241a4cc1f4fSdholland 				return false;
24261f28255Scgd 		}
24361f28255Scgd 	}
244a4cc1f4fSdholland 	return true;
24561f28255Scgd }
24661f28255Scgd 
24761f28255Scgd /*
24861f28255Scgd  * do_move:
24961f28255Scgd  *	Execute a move
25061f28255Scgd  */
251*5305281bSdholland static bool
do_move(int dy,int dx)25230870bd5Sdholland do_move(int dy, int dx)
25361f28255Scgd {
25461f28255Scgd 	static COORD newpos;
25561f28255Scgd 
25661f28255Scgd 	newpos.y = My_pos.y + dy;
25761f28255Scgd 	newpos.x = My_pos.x + dx;
25861f28255Scgd 	if (newpos.y <= 0 || newpos.y >= Y_FIELDSIZE ||
25961f28255Scgd 	    newpos.x <= 0 || newpos.x >= X_FIELDSIZE ||
26061f28255Scgd 	    Field[newpos.y][newpos.x] > 0 || eaten(&newpos)) {
26161f28255Scgd 		if (Running) {
262a4cc1f4fSdholland 			Running = false;
26361f28255Scgd 			leaveok(stdscr, FALSE);
26461f28255Scgd 			move(My_pos.y, My_pos.x);
26561f28255Scgd 			refresh();
26661f28255Scgd 		}
26761f28255Scgd 		else {
268d89727e0Smycroft 			putchar(CTRL('G'));
26961f28255Scgd 			reset_count();
27061f28255Scgd 		}
271a4cc1f4fSdholland 		return false;
27261f28255Scgd 	}
27361f28255Scgd 	else if (dy == 0 && dx == 0)
274a4cc1f4fSdholland 		return true;
27561f28255Scgd 	mvaddch(My_pos.y, My_pos.x, ' ');
27661f28255Scgd 	My_pos = newpos;
27761f28255Scgd 	mvaddch(My_pos.y, My_pos.x, PLAYER);
27861f28255Scgd 	if (!jumping())
27961f28255Scgd 		refresh();
280a4cc1f4fSdholland 	return true;
28161f28255Scgd }
28261f28255Scgd 
28361f28255Scgd /*
28461f28255Scgd  * eaten:
28561f28255Scgd  *	Player would get eaten at this place
28661f28255Scgd  */
287*5305281bSdholland static bool
eaten(const COORD * pos)28830870bd5Sdholland eaten(const COORD *pos)
28961f28255Scgd {
2909007937fSlukem 	int x, y;
29161f28255Scgd 
29261f28255Scgd 	for (y = pos->y - 1; y <= pos->y + 1; y++) {
29361f28255Scgd 		if (y <= 0 || y >= Y_FIELDSIZE)
29461f28255Scgd 			continue;
29561f28255Scgd 		for (x = pos->x - 1; x <= pos->x + 1; x++) {
29661f28255Scgd 			if (x <= 0 || x >= X_FIELDSIZE)
29761f28255Scgd 				continue;
29861f28255Scgd 			if (Field[y][x] == 1)
299a4cc1f4fSdholland 				return true;
30061f28255Scgd 		}
30161f28255Scgd 	}
302a4cc1f4fSdholland 	return false;
30361f28255Scgd }
30461f28255Scgd 
30561f28255Scgd /*
30661f28255Scgd  * reset_count:
30761f28255Scgd  *	Reset the count variables
30861f28255Scgd  */
3099007937fSlukem void
reset_count(void)31030870bd5Sdholland reset_count(void)
31161f28255Scgd {
31261f28255Scgd 	Count = 0;
313a4cc1f4fSdholland 	Running = false;
31461f28255Scgd 	leaveok(stdscr, FALSE);
31561f28255Scgd 	refresh();
31661f28255Scgd }
31761f28255Scgd 
31861f28255Scgd /*
31961f28255Scgd  * jumping:
32061f28255Scgd  *	See if we are jumping, i.e., we should not refresh.
32161f28255Scgd  */
3229007937fSlukem bool
jumping(void)32330870bd5Sdholland jumping(void)
32461f28255Scgd {
32561f28255Scgd 	return (Jump && (Count || Running || Waiting));
32661f28255Scgd }
327