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