worm.c revision 1.15 1 1.15 jsm /* $NetBSD: worm.c,v 1.15 1999/09/09 17:28:00 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.15 jsm __RCSID("$NetBSD: worm.c,v 1.15 1999/09/09 17:28:00 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.1 cgd char 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.8 lukem void process __P((char));
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.1 cgd char ch;
104 1.1 cgd
105 1.1 cgd if (argc == 2)
106 1.1 cgd start_len = atoi(argv[1]);
107 1.1 cgd if ((start_len <= 0) || (start_len > 500))
108 1.1 cgd start_len = LENGTH;
109 1.1 cgd setbuf(stdout, outbuf);
110 1.1 cgd srand(getpid());
111 1.1 cgd signal(SIGALRM, wake);
112 1.1 cgd signal(SIGINT, leave);
113 1.1 cgd signal(SIGQUIT, leave);
114 1.1 cgd initscr();
115 1.1 cgd crmode();
116 1.1 cgd noecho();
117 1.3 mycroft slow = (baudrate() <= 1200);
118 1.1 cgd clear();
119 1.1 cgd stw = newwin(1, COLS-1, 0, 0);
120 1.1 cgd tv = newwin(LINES-1, COLS-1, 1, 0);
121 1.1 cgd box(tv, '*', '*');
122 1.1 cgd scrollok(tv, FALSE);
123 1.1 cgd scrollok(stw, FALSE);
124 1.1 cgd wmove(stw, 0, 0);
125 1.1 cgd wprintw(stw, " Worm");
126 1.1 cgd refresh();
127 1.1 cgd wrefresh(stw);
128 1.1 cgd wrefresh(tv);
129 1.1 cgd life(); /* Create the worm */
130 1.1 cgd prize(); /* Put up a goal */
131 1.1 cgd while(1)
132 1.1 cgd {
133 1.1 cgd if (running)
134 1.1 cgd {
135 1.1 cgd running--;
136 1.1 cgd process(lastch);
137 1.1 cgd }
138 1.1 cgd else
139 1.1 cgd {
140 1.1 cgd fflush(stdout);
141 1.1 cgd if (read(0, &ch, 1) >= 0)
142 1.1 cgd process(ch);
143 1.1 cgd }
144 1.1 cgd }
145 1.1 cgd }
146 1.1 cgd
147 1.8 lukem void
148 1.1 cgd life()
149 1.1 cgd {
150 1.8 lukem struct body *bp, *np;
151 1.8 lukem int i;
152 1.1 cgd
153 1.8 lukem np = NULL;
154 1.1 cgd head = newlink();
155 1.15 jsm if (head == NULL)
156 1.15 jsm errx(1, "out of memory");
157 1.1 cgd head->x = start_len+2;
158 1.1 cgd head->y = 12;
159 1.1 cgd head->next = NULL;
160 1.1 cgd display(head, HEAD);
161 1.1 cgd for (i = 0, bp = head; i < start_len; i++, bp = np) {
162 1.1 cgd np = newlink();
163 1.15 jsm if (np == NULL)
164 1.15 jsm errx(1, "out of memory");
165 1.1 cgd np->next = bp;
166 1.1 cgd bp->prev = np;
167 1.1 cgd np->x = bp->x - 1;
168 1.1 cgd np->y = bp->y;
169 1.1 cgd display(np, BODY);
170 1.1 cgd }
171 1.1 cgd tail = np;
172 1.1 cgd tail->prev = NULL;
173 1.1 cgd }
174 1.1 cgd
175 1.8 lukem void
176 1.1 cgd display(pos, chr)
177 1.13 jsm const struct body *pos;
178 1.8 lukem char chr;
179 1.1 cgd {
180 1.1 cgd wmove(tv, pos->y, pos->x);
181 1.1 cgd waddch(tv, chr);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd void
185 1.8 lukem leave(dummy)
186 1.8 lukem int dummy;
187 1.1 cgd {
188 1.1 cgd endwin();
189 1.11 hubertf
190 1.11 hubertf if (dummy == 0){ /* called via crash() */
191 1.11 hubertf printf("\nWell, you ran into something and the game is over.\n");
192 1.11 hubertf printf("Your final score was %d\n\n", score);
193 1.11 hubertf }
194 1.1 cgd exit(0);
195 1.1 cgd }
196 1.1 cgd
197 1.1 cgd void
198 1.8 lukem wake(dummy)
199 1.14 jsm int dummy __attribute__((__unused__));
200 1.1 cgd {
201 1.1 cgd signal(SIGALRM, wake);
202 1.1 cgd fflush(stdout);
203 1.1 cgd process(lastch);
204 1.1 cgd }
205 1.1 cgd
206 1.8 lukem int
207 1.1 cgd rnd(range)
208 1.8 lukem int range;
209 1.1 cgd {
210 1.1 cgd return abs((rand()>>5)+(rand()>>5)) % range;
211 1.1 cgd }
212 1.1 cgd
213 1.8 lukem void
214 1.1 cgd newpos(bp)
215 1.8 lukem struct body * bp;
216 1.1 cgd {
217 1.1 cgd do {
218 1.1 cgd bp->y = rnd(LINES-3)+ 2;
219 1.1 cgd bp->x = rnd(COLS-3) + 1;
220 1.1 cgd wmove(tv, bp->y, bp->x);
221 1.1 cgd } while(winch(tv) != ' ');
222 1.1 cgd }
223 1.1 cgd
224 1.8 lukem void
225 1.1 cgd prize()
226 1.1 cgd {
227 1.1 cgd int value;
228 1.1 cgd
229 1.1 cgd value = rnd(9) + 1;
230 1.1 cgd newpos(&goody);
231 1.1 cgd waddch(tv, value+'0');
232 1.1 cgd wrefresh(tv);
233 1.1 cgd }
234 1.1 cgd
235 1.8 lukem void
236 1.1 cgd process(ch)
237 1.8 lukem char ch;
238 1.1 cgd {
239 1.8 lukem int x,y;
240 1.1 cgd struct body *nh;
241 1.1 cgd
242 1.1 cgd alarm(0);
243 1.1 cgd x = head->x;
244 1.1 cgd y = head->y;
245 1.1 cgd switch(ch)
246 1.1 cgd {
247 1.4 mycroft case 'h': x--; break;
248 1.4 mycroft case 'j': y++; break;
249 1.4 mycroft case 'k': y--; break;
250 1.4 mycroft case 'l': x++; break;
251 1.4 mycroft case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
252 1.4 mycroft case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
253 1.4 mycroft case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
254 1.4 mycroft case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
255 1.4 mycroft case '\f': setup(); return;
256 1.4 mycroft case CNTRL('C'): crash(); return;
257 1.4 mycroft case CNTRL('D'): crash(); return;
258 1.4 mycroft default: if (! running) alarm(1);
259 1.1 cgd return;
260 1.1 cgd }
261 1.1 cgd lastch = ch;
262 1.1 cgd if (growing == 0)
263 1.1 cgd {
264 1.1 cgd display(tail, ' ');
265 1.1 cgd tail->next->prev = NULL;
266 1.1 cgd nh = tail->next;
267 1.1 cgd free(tail);
268 1.1 cgd tail = nh;
269 1.1 cgd }
270 1.1 cgd else growing--;
271 1.1 cgd display(head, BODY);
272 1.1 cgd wmove(tv, y, x);
273 1.1 cgd if (isdigit(ch = winch(tv)))
274 1.1 cgd {
275 1.1 cgd growing += ch-'0';
276 1.1 cgd prize();
277 1.1 cgd score += growing;
278 1.1 cgd running = 0;
279 1.1 cgd wmove(stw, 0, 68);
280 1.1 cgd wprintw(stw, "Score: %3d", score);
281 1.1 cgd wrefresh(stw);
282 1.1 cgd }
283 1.1 cgd else if(ch != ' ') crash();
284 1.1 cgd nh = newlink();
285 1.15 jsm if (nh == NULL)
286 1.15 jsm errx(1, "out of memory");
287 1.1 cgd nh->next = NULL;
288 1.1 cgd nh->prev = head;
289 1.1 cgd head->next = nh;
290 1.1 cgd nh->y = y;
291 1.1 cgd nh->x = x;
292 1.1 cgd display(nh, HEAD);
293 1.1 cgd head = nh;
294 1.1 cgd if (!(slow && running))
295 1.10 hubertf {
296 1.10 hubertf wmove(tv, head->y, head->x);
297 1.1 cgd wrefresh(tv);
298 1.10 hubertf }
299 1.1 cgd if (!running)
300 1.1 cgd alarm(1);
301 1.1 cgd }
302 1.1 cgd
303 1.8 lukem void
304 1.1 cgd crash()
305 1.1 cgd {
306 1.8 lukem leave(0);
307 1.1 cgd }
308 1.1 cgd
309 1.8 lukem void
310 1.1 cgd setup()
311 1.1 cgd {
312 1.1 cgd clear();
313 1.1 cgd refresh();
314 1.1 cgd touchwin(stw);
315 1.1 cgd wrefresh(stw);
316 1.1 cgd touchwin(tv);
317 1.1 cgd wrefresh(tv);
318 1.1 cgd alarm(1);
319 1.1 cgd }
320