worm.c revision 1.12 1 /* $NetBSD: worm.c,v 1.12 1999/08/10 21:52:43 hubertf Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
45 #else
46 __RCSID("$NetBSD: worm.c,v 1.12 1999/08/10 21:52:43 hubertf Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * Worm. Written by Michael Toy
52 * UCSC
53 */
54
55 #include <ctype.h>
56 #include <curses.h>
57 #include <signal.h>
58 #include <stdlib.h>
59 #include <termios.h>
60 #include <unistd.h>
61
62 #define newlink() (struct body *) malloc(sizeof (struct body));
63 #define HEAD '@'
64 #define BODY 'o'
65 #define LENGTH 7
66 #define RUNLEN 8
67 #define CNTRL(p) (p-'A'+1)
68
69 WINDOW *tv;
70 WINDOW *stw;
71 struct body {
72 int x;
73 int y;
74 struct body *prev;
75 struct body *next;
76 } *head, *tail, goody;
77 int growing = 0;
78 int running = 0;
79 int slow = 0;
80 int score = 0;
81 int start_len = LENGTH;
82 char lastch;
83 char outbuf[BUFSIZ];
84
85 void crash __P((void)) __attribute__((__noreturn__));
86 void display __P((struct body *, char));
87 int main __P((int, char **));
88 void leave __P((int)) __attribute__((__noreturn__));
89 void life __P((void));
90 void newpos __P((struct body *));
91 void process __P((char));
92 void prize __P((void));
93 int rnd __P((int));
94 void setup __P((void));
95 void wake __P((int));
96
97 int
98 main(argc, argv)
99 int argc;
100 char **argv;
101 {
102 char ch;
103
104 if (argc == 2)
105 start_len = atoi(argv[1]);
106 if ((start_len <= 0) || (start_len > 500))
107 start_len = LENGTH;
108 setbuf(stdout, outbuf);
109 srand(getpid());
110 signal(SIGALRM, wake);
111 signal(SIGINT, leave);
112 signal(SIGQUIT, leave);
113 initscr();
114 crmode();
115 noecho();
116 slow = (baudrate() <= 1200);
117 clear();
118 stw = newwin(1, COLS-1, 0, 0);
119 tv = newwin(LINES-1, COLS-1, 1, 0);
120 box(tv, '*', '*');
121 scrollok(tv, FALSE);
122 scrollok(stw, FALSE);
123 wmove(stw, 0, 0);
124 wprintw(stw, " Worm");
125 refresh();
126 wrefresh(stw);
127 wrefresh(tv);
128 life(); /* Create the worm */
129 prize(); /* Put up a goal */
130 while(1)
131 {
132 if (running)
133 {
134 running--;
135 process(lastch);
136 }
137 else
138 {
139 fflush(stdout);
140 if (read(0, &ch, 1) >= 0)
141 process(ch);
142 }
143 }
144 }
145
146 void
147 life()
148 {
149 struct body *bp, *np;
150 int i;
151
152 np = NULL;
153 head = newlink();
154 head->x = start_len+2;
155 head->y = 12;
156 head->next = NULL;
157 display(head, HEAD);
158 for (i = 0, bp = head; i < start_len; i++, bp = np) {
159 np = newlink();
160 np->next = bp;
161 bp->prev = np;
162 np->x = bp->x - 1;
163 np->y = bp->y;
164 display(np, BODY);
165 }
166 tail = np;
167 tail->prev = NULL;
168 }
169
170 void
171 display(pos, chr)
172 struct body *pos;
173 char chr;
174 {
175 wmove(tv, pos->y, pos->x);
176 waddch(tv, chr);
177 }
178
179 void
180 leave(dummy)
181 int dummy;
182 {
183 endwin();
184
185 if (dummy == 0){ /* called via crash() */
186 printf("\nWell, you ran into something and the game is over.\n");
187 printf("Your final score was %d\n\n", score);
188 }
189 exit(0);
190 }
191
192 void
193 wake(dummy)
194 int dummy;
195 {
196 signal(SIGALRM, wake);
197 fflush(stdout);
198 process(lastch);
199 }
200
201 int
202 rnd(range)
203 int range;
204 {
205 return abs((rand()>>5)+(rand()>>5)) % range;
206 }
207
208 void
209 newpos(bp)
210 struct body * bp;
211 {
212 do {
213 bp->y = rnd(LINES-3)+ 2;
214 bp->x = rnd(COLS-3) + 1;
215 wmove(tv, bp->y, bp->x);
216 } while(winch(tv) != ' ');
217 }
218
219 void
220 prize()
221 {
222 int value;
223
224 value = rnd(9) + 1;
225 newpos(&goody);
226 waddch(tv, value+'0');
227 wrefresh(tv);
228 }
229
230 void
231 process(ch)
232 char ch;
233 {
234 int x,y;
235 struct body *nh;
236
237 alarm(0);
238 x = head->x;
239 y = head->y;
240 switch(ch)
241 {
242 case 'h': x--; break;
243 case 'j': y++; break;
244 case 'k': y--; break;
245 case 'l': x++; break;
246 case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
247 case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
248 case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
249 case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
250 case '\f': setup(); return;
251 case CNTRL('C'): crash(); return;
252 case CNTRL('D'): crash(); return;
253 default: if (! running) alarm(1);
254 return;
255 }
256 lastch = ch;
257 if (growing == 0)
258 {
259 display(tail, ' ');
260 tail->next->prev = NULL;
261 nh = tail->next;
262 free(tail);
263 tail = nh;
264 }
265 else growing--;
266 display(head, BODY);
267 wmove(tv, y, x);
268 if (isdigit(ch = winch(tv)))
269 {
270 growing += ch-'0';
271 prize();
272 score += growing;
273 running = 0;
274 wmove(stw, 0, 68);
275 wprintw(stw, "Score: %3d", score);
276 wrefresh(stw);
277 }
278 else if(ch != ' ') crash();
279 nh = newlink();
280 nh->next = NULL;
281 nh->prev = head;
282 head->next = nh;
283 nh->y = y;
284 nh->x = x;
285 display(nh, HEAD);
286 head = nh;
287 if (!(slow && running))
288 {
289 wmove(tv, head->y, head->x);
290 wrefresh(tv);
291 }
292 if (!running)
293 alarm(1);
294 }
295
296 void
297 crash()
298 {
299 leave(0);
300 }
301
302 void
303 setup()
304 {
305 clear();
306 refresh();
307 touchwin(stw);
308 wrefresh(stw);
309 touchwin(tv);
310 wrefresh(tv);
311 alarm(1);
312 }
313