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