worm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* $NetBSD: worm.c,v 1.25 2004/01/27 20:30:31 jsm 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. __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
  33. The Regents of the University of California. All rights reserved.\n");
  34. #endif /* not lint */
  35. #ifndef lint
  36. #if 0
  37. static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
  38. #else
  39. __RCSID("$NetBSD: worm.c,v 1.25 2004/01/27 20:30:31 jsm Exp $");
  40. #endif
  41. #endif /* not lint */
  42. /*
  43. * Worm. Written by Michael Toy
  44. * UCSC
  45. */
  46. #include <ctype.h>
  47. #include <curses.h>
  48. #include <err.h>
  49. #include <signal.h>
  50. #include <stdlib.h>
  51. #include <termios.h>
  52. #include <unistd.h>
  53. #define newlink() (struct body *) malloc(sizeof (struct body));
  54. #define HEAD '@'
  55. #define BODY 'o'
  56. #define LENGTH 7
  57. #define RUNLEN 8
  58. #define CNTRL(p) (p-'A'+1)
  59. WINDOW *tv;
  60. WINDOW *stw;
  61. struct body {
  62. int x;
  63. int y;
  64. struct body *prev;
  65. struct body *next;
  66. } *head, *tail, goody;
  67. int growing = 0;
  68. int running = 0;
  69. int slow = 0;
  70. int score = 0;
  71. int start_len = LENGTH;
  72. int visible_len;
  73. int lastch;
  74. char outbuf[BUFSIZ];
  75. void crash(void) __attribute__((__noreturn__));
  76. void display(const struct body *, char);
  77. int main(int, char **);
  78. void leave(int) __attribute__((__noreturn__));
  79. void life(void);
  80. void newpos(struct body *);
  81. void process(int);
  82. void prize(void);
  83. int rnd(int);
  84. void setup(void);
  85. void wake(int);
  86. int
  87. main(argc, argv)
  88. int argc;
  89. char **argv;
  90. {
  91. /* Revoke setgid privileges */
  92. setregid(getgid(), getgid());
  93. setbuf(stdout, outbuf);
  94. srand(getpid());
  95. signal(SIGALRM, wake);
  96. signal(SIGINT, leave);
  97. signal(SIGQUIT, leave);
  98. initscr();
  99. cbreak();
  100. noecho();
  101. #ifdef KEY_LEFT
  102. keypad(stdscr, TRUE);
  103. #endif
  104. slow = (baudrate() <= 1200);
  105. clear();
  106. if (COLS < 18 || LINES < 5) {
  107. /*
  108. * Insufficient room for the line with " Worm" and the
  109. * score if fewer than 18 columns; insufficient room for
  110. * anything much if fewer than 5 lines.
  111. */
  112. endwin();
  113. errx(1, "screen too small");
  114. }
  115. if (argc == 2)
  116. start_len = atoi(argv[1]);
  117. if ((start_len <= 0) || (start_len > ((LINES-3) * (COLS-2)) / 3))
  118. start_len = LENGTH;
  119. stw = newwin(1, COLS-1, 0, 0);
  120. tv = newwin(LINES-1, COLS-1, 1, 0);
  121. box(tv, '*', '*');
  122. scrollok(tv, FALSE);
  123. scrollok(stw, FALSE);
  124. wmove(stw, 0, 0);
  125. wprintw(stw, " Worm");
  126. refresh();
  127. wrefresh(stw);
  128. wrefresh(tv);
  129. life(); /* Create the worm */
  130. prize(); /* Put up a goal */
  131. while(1)
  132. {
  133. if (running)
  134. {
  135. running--;
  136. process(lastch);
  137. }
  138. else
  139. {
  140. fflush(stdout);
  141. process(getch());
  142. }
  143. }
  144. }
  145. void
  146. life()
  147. {
  148. struct body *bp, *np;
  149. int i, j = 1;
  150. np = NULL;
  151. head = newlink();
  152. if (head == NULL)
  153. err(1, NULL);
  154. head->x = start_len % (COLS-5) + 2;
  155. head->y = LINES / 2;
  156. head->next = NULL;
  157. display(head, HEAD);
  158. for (i = 0, bp = head; i < start_len; i++, bp = np) {
  159. np = newlink();
  160. if (np == NULL)
  161. err(1, NULL);
  162. np->next = bp;
  163. bp->prev = np;
  164. if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4) && (j == -1))) {
  165. j *= -1;
  166. np->x = bp->x;
  167. np->y = bp->y + 1;
  168. } else {
  169. np->x = bp->x - j;
  170. np->y = bp->y;
  171. }
  172. display(np, BODY);
  173. }
  174. tail = np;
  175. tail->prev = NULL;
  176. visible_len = start_len + 1;
  177. }
  178. void
  179. display(pos, chr)
  180. const struct body *pos;
  181. char chr;
  182. {
  183. wmove(tv, pos->y, pos->x);
  184. waddch(tv, chr);
  185. }
  186. void
  187. leave(dummy)
  188. int dummy;
  189. {
  190. endwin();
  191. if (dummy == 0){ /* called via crash() */
  192. printf("\nWell, you ran into something and the game is over.\n");
  193. printf("Your final score was %d\n\n", score);
  194. }
  195. exit(0);
  196. }
  197. void
  198. wake(dummy)
  199. int dummy __attribute__((__unused__));
  200. {
  201. signal(SIGALRM, wake);
  202. fflush(stdout);
  203. process(lastch);
  204. }
  205. int
  206. rnd(range)
  207. int range;
  208. {
  209. return abs((rand()>>5)+(rand()>>5)) % range;
  210. }
  211. void
  212. newpos(bp)
  213. struct body * bp;
  214. {
  215. if (visible_len == (LINES-3) * (COLS-3) - 1) {
  216. endwin();
  217. printf("\nYou won!\n");
  218. printf("Your final score was %d\n\n", score);
  219. exit(0);
  220. }
  221. do {
  222. bp->y = rnd(LINES-3)+ 1;
  223. bp->x = rnd(COLS-3) + 1;
  224. wmove(tv, bp->y, bp->x);
  225. } while(winch(tv) != ' ');
  226. }
  227. void
  228. prize()
  229. {
  230. int value;
  231. value = rnd(9) + 1;
  232. newpos(&goody);
  233. waddch(tv, value+'0');
  234. wrefresh(tv);
  235. }
  236. void
  237. process(ch)
  238. int ch;
  239. {
  240. int x,y;
  241. struct body *nh;
  242. alarm(0);
  243. x = head->x;
  244. y = head->y;
  245. switch(ch)
  246. {
  247. #ifdef KEY_LEFT
  248. case KEY_LEFT:
  249. #endif
  250. case 'h':
  251. x--; break;
  252. #ifdef KEY_DOWN
  253. case KEY_DOWN:
  254. #endif
  255. case 'j':
  256. y++; break;
  257. #ifdef KEY_UP
  258. case KEY_UP:
  259. #endif
  260. case 'k':
  261. y--; break;
  262. #ifdef KEY_RIGHT
  263. case KEY_RIGHT:
  264. #endif
  265. case 'l':
  266. x++; break;
  267. case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
  268. case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
  269. case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
  270. case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
  271. case '\f': setup(); return;
  272. case ERR:
  273. case CNTRL('C'):
  274. case CNTRL('D'):
  275. crash();
  276. return;
  277. default: if (! running) alarm(1);
  278. return;
  279. }
  280. lastch = ch;
  281. if (growing == 0)
  282. {
  283. display(tail, ' ');
  284. tail->next->prev = NULL;
  285. nh = tail->next;
  286. free(tail);
  287. tail = nh;
  288. visible_len--;
  289. }
  290. else growing--;
  291. display(head, BODY);
  292. wmove(tv, y, x);
  293. if (isdigit(ch = winch(tv)))
  294. {
  295. growing += ch-'0';
  296. prize();
  297. score += growing;
  298. running = 0;
  299. wmove(stw, 0, COLS - 12);
  300. wprintw(stw, "Score: %3d", score);
  301. wrefresh(stw);
  302. }
  303. else if(ch != ' ') crash();
  304. nh = newlink();
  305. if (nh == NULL)
  306. err(1, NULL);
  307. nh->next = NULL;
  308. nh->prev = head;
  309. head->next = nh;
  310. nh->y = y;
  311. nh->x = x;
  312. display(nh, HEAD);
  313. head = nh;
  314. visible_len++;
  315. if (!(slow && running))
  316. {
  317. wmove(tv, head->y, head->x);
  318. wrefresh(tv);
  319. }
  320. if (!running)
  321. alarm(1);
  322. }
  323. void
  324. crash()
  325. {
  326. leave(0);
  327. }
  328. void
  329. setup()
  330. {
  331. clear();
  332. refresh();
  333. touchwin(stw);
  334. wrefresh(stw);
  335. touchwin(tv);
  336. wrefresh(tv);
  337. alarm(1);
  338. }