worm.c revision 1.3 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.3 mycroft static char rcsid[] = "$Id: worm.c,v 1.3 1993/08/10 02:05:36 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 when break;case
60 1.1 cgd #define otherwise break;default
61 1.1 cgd #define CNTRL(p) (p-'A'+1)
62 1.1 cgd #ifndef baudrate
63 1.1 cgd # define baudrate() _tty.sg_ospeed
64 1.1 cgd #endif
65 1.1 cgd
66 1.1 cgd WINDOW *tv;
67 1.1 cgd WINDOW *stw;
68 1.1 cgd struct body {
69 1.1 cgd int x;
70 1.1 cgd int y;
71 1.1 cgd struct body *prev;
72 1.1 cgd struct body *next;
73 1.1 cgd } *head, *tail, goody;
74 1.1 cgd int growing = 0;
75 1.1 cgd int running = 0;
76 1.1 cgd int slow = 0;
77 1.1 cgd int score = 0;
78 1.1 cgd int start_len = LENGTH;
79 1.1 cgd char lastch;
80 1.1 cgd char outbuf[BUFSIZ];
81 1.1 cgd
82 1.1 cgd void leave(), wake(), suspend();
83 1.1 cgd
84 1.1 cgd main(argc, argv)
85 1.1 cgd int argc;
86 1.1 cgd char **argv;
87 1.1 cgd {
88 1.1 cgd char ch;
89 1.1 cgd
90 1.1 cgd if (argc == 2)
91 1.1 cgd start_len = atoi(argv[1]);
92 1.1 cgd if ((start_len <= 0) || (start_len > 500))
93 1.1 cgd start_len = LENGTH;
94 1.1 cgd setbuf(stdout, outbuf);
95 1.1 cgd srand(getpid());
96 1.1 cgd signal(SIGALRM, wake);
97 1.1 cgd signal(SIGINT, leave);
98 1.1 cgd signal(SIGQUIT, leave);
99 1.1 cgd signal(SIGTSTP, suspend); /* process control signal */
100 1.1 cgd initscr();
101 1.1 cgd crmode();
102 1.1 cgd noecho();
103 1.3 mycroft slow = (baudrate() <= 1200);
104 1.1 cgd clear();
105 1.1 cgd stw = newwin(1, COLS-1, 0, 0);
106 1.1 cgd tv = newwin(LINES-1, COLS-1, 1, 0);
107 1.1 cgd box(tv, '*', '*');
108 1.1 cgd scrollok(tv, FALSE);
109 1.1 cgd scrollok(stw, FALSE);
110 1.1 cgd wmove(stw, 0, 0);
111 1.1 cgd wprintw(stw, " Worm");
112 1.1 cgd refresh();
113 1.1 cgd wrefresh(stw);
114 1.1 cgd wrefresh(tv);
115 1.1 cgd life(); /* Create the worm */
116 1.1 cgd prize(); /* Put up a goal */
117 1.1 cgd while(1)
118 1.1 cgd {
119 1.1 cgd if (running)
120 1.1 cgd {
121 1.1 cgd running--;
122 1.1 cgd process(lastch);
123 1.1 cgd }
124 1.1 cgd else
125 1.1 cgd {
126 1.1 cgd fflush(stdout);
127 1.1 cgd if (read(0, &ch, 1) >= 0)
128 1.1 cgd process(ch);
129 1.1 cgd }
130 1.1 cgd }
131 1.1 cgd }
132 1.1 cgd
133 1.1 cgd life()
134 1.1 cgd {
135 1.1 cgd register struct body *bp, *np;
136 1.1 cgd register int i;
137 1.1 cgd
138 1.1 cgd head = newlink();
139 1.1 cgd head->x = start_len+2;
140 1.1 cgd head->y = 12;
141 1.1 cgd head->next = NULL;
142 1.1 cgd display(head, HEAD);
143 1.1 cgd for (i = 0, bp = head; i < start_len; i++, bp = np) {
144 1.1 cgd np = newlink();
145 1.1 cgd np->next = bp;
146 1.1 cgd bp->prev = np;
147 1.1 cgd np->x = bp->x - 1;
148 1.1 cgd np->y = bp->y;
149 1.1 cgd display(np, BODY);
150 1.1 cgd }
151 1.1 cgd tail = np;
152 1.1 cgd tail->prev = NULL;
153 1.1 cgd }
154 1.1 cgd
155 1.1 cgd display(pos, chr)
156 1.1 cgd struct body *pos;
157 1.1 cgd char chr;
158 1.1 cgd {
159 1.1 cgd wmove(tv, pos->y, pos->x);
160 1.1 cgd waddch(tv, chr);
161 1.1 cgd }
162 1.1 cgd
163 1.1 cgd void
164 1.1 cgd leave()
165 1.1 cgd {
166 1.1 cgd endwin();
167 1.1 cgd exit(0);
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd void
171 1.1 cgd wake()
172 1.1 cgd {
173 1.1 cgd signal(SIGALRM, wake);
174 1.1 cgd fflush(stdout);
175 1.1 cgd process(lastch);
176 1.1 cgd }
177 1.1 cgd
178 1.1 cgd rnd(range)
179 1.1 cgd {
180 1.1 cgd return abs((rand()>>5)+(rand()>>5)) % range;
181 1.1 cgd }
182 1.1 cgd
183 1.1 cgd newpos(bp)
184 1.1 cgd struct body * bp;
185 1.1 cgd {
186 1.1 cgd do {
187 1.1 cgd bp->y = rnd(LINES-3)+ 2;
188 1.1 cgd bp->x = rnd(COLS-3) + 1;
189 1.1 cgd wmove(tv, bp->y, bp->x);
190 1.1 cgd } while(winch(tv) != ' ');
191 1.1 cgd }
192 1.1 cgd
193 1.1 cgd prize()
194 1.1 cgd {
195 1.1 cgd int value;
196 1.1 cgd
197 1.1 cgd value = rnd(9) + 1;
198 1.1 cgd newpos(&goody);
199 1.1 cgd waddch(tv, value+'0');
200 1.1 cgd wrefresh(tv);
201 1.1 cgd }
202 1.1 cgd
203 1.1 cgd process(ch)
204 1.1 cgd char ch;
205 1.1 cgd {
206 1.1 cgd register int x,y;
207 1.1 cgd struct body *nh;
208 1.1 cgd
209 1.1 cgd alarm(0);
210 1.1 cgd x = head->x;
211 1.1 cgd y = head->y;
212 1.1 cgd switch(ch)
213 1.1 cgd {
214 1.1 cgd when 'h': x--;
215 1.1 cgd when 'j': y++;
216 1.1 cgd when 'k': y--;
217 1.1 cgd when 'l': x++;
218 1.1 cgd when 'H': x--; running = RUNLEN; ch = tolower(ch);
219 1.1 cgd when 'J': y++; running = RUNLEN/2; ch = tolower(ch);
220 1.1 cgd when 'K': y--; running = RUNLEN/2; ch = tolower(ch);
221 1.1 cgd when 'L': x++; running = RUNLEN; ch = tolower(ch);
222 1.1 cgd when '\f': setup(); return;
223 1.1 cgd when CNTRL('Z'): suspend(); return;
224 1.1 cgd when CNTRL('C'): crash(); return;
225 1.1 cgd when CNTRL('D'): crash(); return;
226 1.1 cgd otherwise: if (! running) alarm(1);
227 1.1 cgd return;
228 1.1 cgd }
229 1.1 cgd lastch = ch;
230 1.1 cgd if (growing == 0)
231 1.1 cgd {
232 1.1 cgd display(tail, ' ');
233 1.1 cgd tail->next->prev = NULL;
234 1.1 cgd nh = tail->next;
235 1.1 cgd free(tail);
236 1.1 cgd tail = nh;
237 1.1 cgd }
238 1.1 cgd else growing--;
239 1.1 cgd display(head, BODY);
240 1.1 cgd wmove(tv, y, x);
241 1.1 cgd if (isdigit(ch = winch(tv)))
242 1.1 cgd {
243 1.1 cgd growing += ch-'0';
244 1.1 cgd prize();
245 1.1 cgd score += growing;
246 1.1 cgd running = 0;
247 1.1 cgd wmove(stw, 0, 68);
248 1.1 cgd wprintw(stw, "Score: %3d", score);
249 1.1 cgd wrefresh(stw);
250 1.1 cgd }
251 1.1 cgd else if(ch != ' ') crash();
252 1.1 cgd nh = newlink();
253 1.1 cgd nh->next = NULL;
254 1.1 cgd nh->prev = head;
255 1.1 cgd head->next = nh;
256 1.1 cgd nh->y = y;
257 1.1 cgd nh->x = x;
258 1.1 cgd display(nh, HEAD);
259 1.1 cgd head = nh;
260 1.1 cgd if (!(slow && running))
261 1.1 cgd wrefresh(tv);
262 1.1 cgd if (!running)
263 1.1 cgd alarm(1);
264 1.1 cgd }
265 1.1 cgd
266 1.1 cgd crash()
267 1.1 cgd {
268 1.1 cgd sleep(2);
269 1.1 cgd clear();
270 1.1 cgd move(23, 0);
271 1.1 cgd refresh();
272 1.1 cgd printf("Well, you ran into something and the game is over.\n");
273 1.1 cgd printf("Your final score was %d\n", score);
274 1.1 cgd leave();
275 1.1 cgd }
276 1.1 cgd
277 1.1 cgd void
278 1.1 cgd suspend()
279 1.1 cgd {
280 1.1 cgd char *sh;
281 1.1 cgd
282 1.1 cgd move(LINES-1, 0);
283 1.1 cgd refresh();
284 1.1 cgd endwin();
285 1.1 cgd fflush(stdout);
286 1.1 cgd kill(getpid(), SIGTSTP);
287 1.1 cgd signal(SIGTSTP, suspend);
288 1.1 cgd crmode();
289 1.1 cgd noecho();
290 1.1 cgd setup();
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd setup()
294 1.1 cgd {
295 1.1 cgd clear();
296 1.1 cgd refresh();
297 1.1 cgd touchwin(stw);
298 1.1 cgd wrefresh(stw);
299 1.1 cgd touchwin(tv);
300 1.1 cgd wrefresh(tv);
301 1.1 cgd alarm(1);
302 1.1 cgd }
303