move.c 6.3 KB

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