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