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