fly.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* $NetBSD: fly.c,v 1.12 2004/01/27 20:30:29 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1983, 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[] = "@(#)fly.c 8.2 (Berkeley) 4/28/95";
  34. #else
  35. __RCSID("$NetBSD: fly.c,v 1.12 2004/01/27 20:30:29 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "extern.h"
  39. #undef UP
  40. #include <curses.h>
  41. #define MIDR (LINES/2 - 1)
  42. #define MIDC (COLS/2 - 1)
  43. static int row, column;
  44. static int dr = 0, dc = 0;
  45. static char destroyed;
  46. int ourclock = 120; /* time for all the flights in the game */
  47. static char cross = 0;
  48. static sig_t oldsig;
  49. static void blast(void);
  50. static void endfly(void);
  51. static void moveenemy(int);
  52. static void notarget(void);
  53. static void screen(void);
  54. static void succumb(int);
  55. static void target(void);
  56. static void
  57. succumb(dummy)
  58. int dummy __attribute__((__unused__));
  59. {
  60. if (oldsig == SIG_DFL) {
  61. endfly();
  62. exit(1);
  63. }
  64. if (oldsig != SIG_IGN) {
  65. endfly();
  66. (*oldsig) (SIGINT);
  67. }
  68. }
  69. int
  70. visual()
  71. {
  72. destroyed = 0;
  73. if (initscr() == NULL) {
  74. puts("Whoops! No more memory...");
  75. return (0);
  76. }
  77. oldsig = signal(SIGINT, succumb);
  78. cbreak();
  79. noecho();
  80. screen();
  81. row = rnd(LINES - 3) + 1;
  82. column = rnd(COLS - 2) + 1;
  83. moveenemy(0);
  84. for (;;) {
  85. switch (getchar()) {
  86. case 'h':
  87. case 'r':
  88. dc = -1;
  89. fuel--;
  90. break;
  91. case 'H':
  92. case 'R':
  93. dc = -5;
  94. fuel -= 10;
  95. break;
  96. case 'l':
  97. dc = 1;
  98. fuel--;
  99. break;
  100. case 'L':
  101. dc = 5;
  102. fuel -= 10;
  103. break;
  104. case 'j':
  105. case 'u':
  106. dr = 1;
  107. fuel--;
  108. break;
  109. case 'J':
  110. case 'U':
  111. dr = 5;
  112. fuel -= 10;
  113. break;
  114. case 'k':
  115. case 'd':
  116. dr = -1;
  117. fuel--;
  118. break;
  119. case 'K':
  120. case 'D':
  121. dr = -5;
  122. fuel -= 10;
  123. break;
  124. case '+':
  125. if (cross) {
  126. cross = 0;
  127. notarget();
  128. } else
  129. cross = 1;
  130. break;
  131. case ' ':
  132. case 'f':
  133. if (torps) {
  134. torps -= 2;
  135. blast();
  136. if (row == MIDR && column - MIDC < 2 && MIDC - column < 2) {
  137. destroyed = 1;
  138. alarm(0);
  139. }
  140. } else
  141. mvaddstr(0, 0, "*** Out of torpedoes. ***");
  142. break;
  143. case 'q':
  144. endfly();
  145. return (0);
  146. default:
  147. mvaddstr(0, 26, "Commands = r,R,l,L,u,U,d,D,f,+,q");
  148. continue;
  149. case EOF:
  150. break;
  151. }
  152. if (destroyed) {
  153. endfly();
  154. return (1);
  155. }
  156. if (ourclock <= 0) {
  157. endfly();
  158. die();
  159. }
  160. }
  161. }
  162. static void
  163. screen()
  164. {
  165. int r, c, n;
  166. int i;
  167. clear();
  168. i = rnd(100);
  169. for (n = 0; n < i; n++) {
  170. r = rnd(LINES - 3) + 1;
  171. c = rnd(COLS);
  172. mvaddch(r, c, '.');
  173. }
  174. mvaddstr(LINES - 1 - 1, 21, "TORPEDOES FUEL TIME");
  175. refresh();
  176. }
  177. static void
  178. target()
  179. {
  180. int n;
  181. move(MIDR, MIDC - 10);
  182. addstr("------- + -------");
  183. for (n = MIDR - 4; n < MIDR - 1; n++) {
  184. mvaddch(n, MIDC, '|');
  185. mvaddch(n + 6, MIDC, '|');
  186. }
  187. }
  188. static void
  189. notarget()
  190. {
  191. int n;
  192. move(MIDR, MIDC - 10);
  193. addstr(" ");
  194. for (n = MIDR - 4; n < MIDR - 1; n++) {
  195. mvaddch(n, MIDC, ' ');
  196. mvaddch(n + 6, MIDC, ' ');
  197. }
  198. }
  199. static void
  200. blast()
  201. {
  202. int n;
  203. alarm(0);
  204. move(LINES - 1, 24);
  205. printw("%3d", torps);
  206. for (n = LINES - 1 - 2; n >= MIDR + 1; n--) {
  207. mvaddch(n, MIDC + MIDR - n, '/');
  208. mvaddch(n, MIDC - MIDR + n, '\\');
  209. refresh();
  210. }
  211. mvaddch(MIDR, MIDC, '*');
  212. for (n = LINES - 1 - 2; n >= MIDR + 1; n--) {
  213. mvaddch(n, MIDC + MIDR - n, ' ');
  214. mvaddch(n, MIDC - MIDR + n, ' ');
  215. refresh();
  216. }
  217. alarm(1);
  218. }
  219. static void
  220. moveenemy(dummy)
  221. int dummy __attribute__((__unused__));
  222. {
  223. double d;
  224. int oldr, oldc;
  225. oldr = row;
  226. oldc = column;
  227. if (fuel > 0) {
  228. if (row + dr <= LINES - 3 && row + dr > 0)
  229. row += dr;
  230. if (column + dc < COLS - 1 && column + dc > 0)
  231. column += dc;
  232. } else
  233. if (fuel < 0) {
  234. fuel = 0;
  235. mvaddstr(0, 60, "*** Out of fuel ***");
  236. }
  237. d = (double) ((row - MIDR) * (row - MIDR) + (column - MIDC) * (column - MIDC));
  238. if (d < 16) {
  239. row += (rnd(9) - 4) % (4 - abs(row - MIDR));
  240. column += (rnd(9) - 4) % (4 - abs(column - MIDC));
  241. }
  242. ourclock--;
  243. mvaddstr(oldr, oldc - 1, " ");
  244. if (cross)
  245. target();
  246. mvaddstr(row, column - 1, "/-\\");
  247. move(LINES - 1, 24);
  248. printw("%3d", torps);
  249. move(LINES - 1, 42);
  250. printw("%3d", fuel);
  251. move(LINES - 1, 57);
  252. printw("%3d", ourclock);
  253. refresh();
  254. signal(SIGALRM, moveenemy);
  255. alarm(1);
  256. }
  257. static void
  258. endfly()
  259. {
  260. alarm(0);
  261. signal(SIGALRM, SIG_DFL);
  262. mvcur(0, COLS - 1, LINES - 1, 0);
  263. endwin();
  264. setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Needed for ncurses */
  265. signal(SIGTSTP, SIG_DFL);
  266. signal(SIGINT, oldsig);
  267. }