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