date and time created 88/06/01 18:32:07 by bostic
[unix-history] / usr / src / games / worm / worm.c
CommitLineData
e0bbfbf9
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
c54d1a23
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
e0bbfbf9
DF
11 */
12
13#ifndef lint
14char copyright[] =
15"@(#) Copyright (c) 1980 Regents of the University of California.\n\
16 All rights reserved.\n";
c54d1a23 17#endif /* not lint */
e0bbfbf9
DF
18
19#ifndef lint
c54d1a23
KB
20static char sccsid[] = "@(#)worm.c 5.4 (Berkeley) %G%";
21#endif /* not lint */
c4e6edfa
KM
22
23/*
24 * Worm. Written by Michael Toy
25 * UCSC
26 */
27
28#include <ctype.h>
29#include <curses.h>
30#include <signal.h>
31
32#define newlink() (struct body *) malloc(sizeof (struct body));
33#define HEAD '@'
34#define BODY 'o'
35#define LENGTH 7
36#define RUNLEN 8
37#define when break;case
38#define otherwise break;default
3831612d 39#define CNTRL(p) (p-'A'+1)
aba3284a 40#ifndef baudrate
73ead3d6
KA
41# define baudrate() _tty.sg_ospeed
42#endif
c4e6edfa
KM
43
44WINDOW *tv;
45WINDOW *stw;
46struct body {
47 int x;
48 int y;
49 struct body *prev;
50 struct body *next;
51} *head, *tail, goody;
52int growing = 0;
53int running = 0;
73ead3d6 54int slow = 0;
c4e6edfa
KM
55int score = 0;
56int start_len = LENGTH;
57char lastch;
58char outbuf[BUFSIZ];
59
60main(argc, argv)
61char **argv;
62{
63 int leave(), wake(), suspend();
64 char ch;
65
66 if (argc == 2)
67 start_len = atoi(argv[1]);
68 if ((start_len <= 0) || (start_len > 500))
69 start_len = LENGTH;
70 setbuf(stdout, outbuf);
71 srand(getpid());
72 signal(SIGALRM, wake);
73 signal(SIGINT, leave);
74 signal(SIGQUIT, leave);
75#ifdef SIGTSTP
76 signal(SIGTSTP, suspend); /* process control signal */
77#endif
78 initscr();
79 crmode();
80 noecho();
73ead3d6 81 slow = (baudrate() <= B1200);
c4e6edfa
KM
82 clear();
83 stw = newwin(1, COLS-1, 0, 0);
84 tv = newwin(LINES-1, COLS-1, 1, 0);
85 box(tv, '*', '*');
86 scrollok(tv, FALSE);
87 scrollok(stw, FALSE);
88 wmove(stw, 0, 0);
89 wprintw(stw, " Worm");
90 refresh();
91 wrefresh(stw);
92 wrefresh(tv);
93 life(); /* Create the worm */
94 prize(); /* Put up a goal */
95 while(1)
96 {
97 if (running)
98 {
99 running--;
100 process(lastch);
101 }
102 else
103 {
104 fflush(stdout);
105 if (read(0, &ch, 1) >= 0)
106 process(ch);
107 }
108 }
109}
110
111life()
112{
113 register struct body *bp, *np;
114 register int i;
115
116 head = newlink();
117 head->x = start_len+2;
118 head->y = 12;
119 head->next = NULL;
120 display(head, HEAD);
121 for (i = 0, bp = head; i < start_len; i++, bp = np) {
122 np = newlink();
123 np->next = bp;
124 bp->prev = np;
125 np->x = bp->x - 1;
126 np->y = bp->y;
127 display(np, BODY);
128 }
129 tail = np;
130 tail->prev = NULL;
c4e6edfa
KM
131}
132
133display(pos, chr)
134struct body *pos;
135char chr;
136{
137 wmove(tv, pos->y, pos->x);
138 waddch(tv, chr);
139}
140
141leave()
142{
143 endwin();
144 exit(0);
145}
146
147wake()
148{
149 signal(SIGALRM, wake);
150 fflush(stdout);
151 process(lastch);
152}
153
154rnd(range)
155{
156 return abs((rand()>>5)+(rand()>>5)) % range;
157}
158
159newpos(bp)
160struct body * bp;
161{
162 do {
163 bp->y = rnd(LINES-3)+ 2;
164 bp->x = rnd(COLS-3) + 1;
165 wmove(tv, bp->y, bp->x);
166 } while(winch(tv) != ' ');
167}
168
169prize()
170{
171 int value;
172
173 value = rnd(9) + 1;
174 newpos(&goody);
175 waddch(tv, value+'0');
176 wrefresh(tv);
177}
178
179process(ch)
180char ch;
181{
182 register int x,y;
183 struct body *nh;
184
185 alarm(0);
186 x = head->x;
187 y = head->y;
188 switch(ch)
189 {
190 when 'h': x--;
191 when 'j': y++;
192 when 'k': y--;
193 when 'l': x++;
194 when 'H': x--; running = RUNLEN; ch = tolower(ch);
195 when 'J': y++; running = RUNLEN/2; ch = tolower(ch);
196 when 'K': y--; running = RUNLEN/2; ch = tolower(ch);
197 when 'L': x++; running = RUNLEN; ch = tolower(ch);
198 when '\f': setup(); return;
3831612d
KB
199 when CNTRL('Z'): suspend(); return;
200 when CNTRL('C'): crash(); return;
201 when CNTRL('D'): crash(); return;
c4e6edfa
KM
202 otherwise: if (! running) alarm(1);
203 return;
204 }
205 lastch = ch;
206 if (growing == 0)
207 {
208 display(tail, ' ');
209 tail->next->prev = NULL;
210 nh = tail->next;
211 free(tail);
212 tail = nh;
213 }
214 else growing--;
215 display(head, BODY);
216 wmove(tv, y, x);
217 if (isdigit(ch = winch(tv)))
218 {
219 growing += ch-'0';
220 prize();
221 score += growing;
222 running = 0;
223 wmove(stw, 0, 68);
224 wprintw(stw, "Score: %3d", score);
225 wrefresh(stw);
226 }
227 else if(ch != ' ') crash();
228 nh = newlink();
229 nh->next = NULL;
230 nh->prev = head;
231 head->next = nh;
232 nh->y = y;
233 nh->x = x;
234 display(nh, HEAD);
235 head = nh;
73ead3d6
KA
236 if (!(slow && running))
237 wrefresh(tv);
238 if (!running)
239 alarm(1);
c4e6edfa
KM
240}
241
242crash()
243{
244 sleep(2);
245 clear();
246 move(23, 0);
247 refresh();
248 printf("Well you ran into something and the game is over.\n");
249 printf("Your final score was %d\n", score);
250 leave();
251}
252
253suspend()
254{
255 char *sh;
256
257 move(LINES-1, 0);
258 refresh();
259 endwin();
260 fflush(stdout);
261#ifdef SIGTSTP
262 kill(getpid(), SIGTSTP);
263 signal(SIGTSTP, suspend);
264#else
265 sh = getenv("SHELL");
266 if (sh == NULL)
267 sh = "/bin/sh";
268 system(sh);
269#endif
270 crmode();
271 noecho();
272 setup();
273}
274
275setup()
276{
277 clear();
278 refresh();
279 touchwin(stw);
280 wrefresh(stw);
281 touchwin(tv);
282 wrefresh(tv);
283 alarm(1);
284}