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