worm.c revision 1.31.28.1 1 1.31.28.1 perseant /* $NetBSD: worm.c,v 1.31.28.1 2025/08/02 05:54:27 perseant 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.24 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.8 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.27 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 1.27 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.5 cgd #if 0
40 1.5 cgd static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93";
41 1.5 cgd #else
42 1.31.28.1 perseant __RCSID("$NetBSD: worm.c,v 1.31.28.1 2025/08/02 05:54:27 perseant Exp $");
43 1.5 cgd #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd /*
47 1.1 cgd * Worm. Written by Michael Toy
48 1.1 cgd * UCSC
49 1.1 cgd */
50 1.1 cgd
51 1.1 cgd #include <ctype.h>
52 1.1 cgd #include <curses.h>
53 1.15 jsm #include <err.h>
54 1.1 cgd #include <signal.h>
55 1.6 cgd #include <stdlib.h>
56 1.5 cgd #include <termios.h>
57 1.8 lukem #include <unistd.h>
58 1.1 cgd
59 1.1 cgd #define HEAD '@'
60 1.1 cgd #define BODY 'o'
61 1.1 cgd #define LENGTH 7
62 1.1 cgd #define RUNLEN 8
63 1.1 cgd #define CNTRL(p) (p-'A'+1)
64 1.1 cgd
65 1.1 cgd struct body {
66 1.1 cgd int x;
67 1.1 cgd int y;
68 1.1 cgd struct body *prev;
69 1.1 cgd struct body *next;
70 1.29 dholland };
71 1.29 dholland
72 1.29 dholland static WINDOW *tv;
73 1.29 dholland static WINDOW *stw;
74 1.29 dholland static struct body *head, *tail, goody;
75 1.29 dholland static int growing = 0;
76 1.29 dholland static int running = 0;
77 1.29 dholland static int slow = 0;
78 1.29 dholland static int score = 0;
79 1.29 dholland static int start_len = LENGTH;
80 1.29 dholland static int visible_len;
81 1.29 dholland static int lastch;
82 1.29 dholland static char outbuf[BUFSIZ];
83 1.1 cgd
84 1.25 jsm int main(int, char **);
85 1.29 dholland static void crash(void) __dead;
86 1.29 dholland static void display(const struct body *, char);
87 1.29 dholland static void leave(int) __dead;
88 1.29 dholland static void life(void);
89 1.29 dholland static void newpos(struct body *);
90 1.29 dholland static void process(int);
91 1.29 dholland static void prize(void);
92 1.29 dholland static int rnd(int);
93 1.29 dholland static void setup(void);
94 1.29 dholland static void wake(int);
95 1.1 cgd
96 1.31 dholland static struct body *
97 1.31 dholland newlink(void)
98 1.31 dholland {
99 1.31 dholland struct body *b;
100 1.31 dholland
101 1.31 dholland b = malloc(sizeof(*b));
102 1.31 dholland if (b == NULL) {
103 1.31.28.1 perseant err(EXIT_FAILURE, "malloc failed");
104 1.31 dholland }
105 1.31 dholland return b;
106 1.31 dholland }
107 1.31 dholland
108 1.8 lukem int
109 1.30 joerg main(int argc, char **argv)
110 1.1 cgd {
111 1.16 jsm
112 1.16 jsm /* Revoke setgid privileges */
113 1.19 mycroft setgid(getgid());
114 1.1 cgd
115 1.1 cgd setbuf(stdout, outbuf);
116 1.1 cgd srand(getpid());
117 1.1 cgd signal(SIGALRM, wake);
118 1.1 cgd signal(SIGINT, leave);
119 1.1 cgd signal(SIGQUIT, leave);
120 1.28 drochner if (!initscr())
121 1.28 drochner errx(0, "couldn't initialize screen");
122 1.23 blymn cbreak();
123 1.1 cgd noecho();
124 1.17 cgd #ifdef KEY_LEFT
125 1.17 cgd keypad(stdscr, TRUE);
126 1.17 cgd #endif
127 1.3 mycroft slow = (baudrate() <= 1200);
128 1.1 cgd clear();
129 1.21 jsm if (COLS < 18 || LINES < 5) {
130 1.21 jsm /*
131 1.21 jsm * Insufficient room for the line with " Worm" and the
132 1.21 jsm * score if fewer than 18 columns; insufficient room for
133 1.21 jsm * anything much if fewer than 5 lines.
134 1.21 jsm */
135 1.21 jsm endwin();
136 1.21 jsm errx(1, "screen too small");
137 1.21 jsm }
138 1.20 jsm if (argc == 2)
139 1.20 jsm start_len = atoi(argv[1]);
140 1.20 jsm if ((start_len <= 0) || (start_len > ((LINES-3) * (COLS-2)) / 3))
141 1.20 jsm start_len = LENGTH;
142 1.1 cgd stw = newwin(1, COLS-1, 0, 0);
143 1.1 cgd tv = newwin(LINES-1, COLS-1, 1, 0);
144 1.1 cgd box(tv, '*', '*');
145 1.1 cgd scrollok(tv, FALSE);
146 1.1 cgd scrollok(stw, FALSE);
147 1.1 cgd wmove(stw, 0, 0);
148 1.1 cgd wprintw(stw, " Worm");
149 1.1 cgd refresh();
150 1.1 cgd wrefresh(stw);
151 1.1 cgd wrefresh(tv);
152 1.1 cgd life(); /* Create the worm */
153 1.1 cgd prize(); /* Put up a goal */
154 1.1 cgd while(1)
155 1.1 cgd {
156 1.1 cgd if (running)
157 1.1 cgd {
158 1.1 cgd running--;
159 1.1 cgd process(lastch);
160 1.1 cgd }
161 1.1 cgd else
162 1.1 cgd {
163 1.1 cgd fflush(stdout);
164 1.17 cgd process(getch());
165 1.1 cgd }
166 1.1 cgd }
167 1.1 cgd }
168 1.1 cgd
169 1.29 dholland static void
170 1.30 joerg life(void)
171 1.1 cgd {
172 1.8 lukem struct body *bp, *np;
173 1.20 jsm int i, j = 1;
174 1.1 cgd
175 1.8 lukem np = NULL;
176 1.1 cgd head = newlink();
177 1.20 jsm head->x = start_len % (COLS-5) + 2;
178 1.20 jsm head->y = LINES / 2;
179 1.1 cgd head->next = NULL;
180 1.1 cgd display(head, HEAD);
181 1.1 cgd for (i = 0, bp = head; i < start_len; i++, bp = np) {
182 1.1 cgd np = newlink();
183 1.1 cgd np->next = bp;
184 1.1 cgd bp->prev = np;
185 1.20 jsm if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4) && (j == -1))) {
186 1.20 jsm j *= -1;
187 1.20 jsm np->x = bp->x;
188 1.20 jsm np->y = bp->y + 1;
189 1.20 jsm } else {
190 1.20 jsm np->x = bp->x - j;
191 1.20 jsm np->y = bp->y;
192 1.20 jsm }
193 1.1 cgd display(np, BODY);
194 1.1 cgd }
195 1.1 cgd tail = np;
196 1.1 cgd tail->prev = NULL;
197 1.22 jsm visible_len = start_len + 1;
198 1.1 cgd }
199 1.1 cgd
200 1.29 dholland static void
201 1.30 joerg display(const struct body *pos, char chr)
202 1.1 cgd {
203 1.1 cgd wmove(tv, pos->y, pos->x);
204 1.1 cgd waddch(tv, chr);
205 1.1 cgd }
206 1.1 cgd
207 1.29 dholland static void
208 1.30 joerg leave(int dummy)
209 1.1 cgd {
210 1.1 cgd endwin();
211 1.11 hubertf
212 1.11 hubertf if (dummy == 0){ /* called via crash() */
213 1.11 hubertf printf("\nWell, you ran into something and the game is over.\n");
214 1.11 hubertf printf("Your final score was %d\n\n", score);
215 1.11 hubertf }
216 1.1 cgd exit(0);
217 1.1 cgd }
218 1.1 cgd
219 1.29 dholland static void
220 1.30 joerg wake(int dummy)
221 1.1 cgd {
222 1.1 cgd signal(SIGALRM, wake);
223 1.1 cgd fflush(stdout);
224 1.1 cgd process(lastch);
225 1.1 cgd }
226 1.1 cgd
227 1.29 dholland static int
228 1.30 joerg rnd(int range)
229 1.1 cgd {
230 1.1 cgd return abs((rand()>>5)+(rand()>>5)) % range;
231 1.1 cgd }
232 1.1 cgd
233 1.29 dholland static void
234 1.30 joerg newpos(struct body *bp)
235 1.1 cgd {
236 1.22 jsm if (visible_len == (LINES-3) * (COLS-3) - 1) {
237 1.22 jsm endwin();
238 1.22 jsm
239 1.22 jsm printf("\nYou won!\n");
240 1.22 jsm printf("Your final score was %d\n\n", score);
241 1.22 jsm exit(0);
242 1.22 jsm }
243 1.1 cgd do {
244 1.21 jsm bp->y = rnd(LINES-3)+ 1;
245 1.1 cgd bp->x = rnd(COLS-3) + 1;
246 1.1 cgd wmove(tv, bp->y, bp->x);
247 1.1 cgd } while(winch(tv) != ' ');
248 1.1 cgd }
249 1.1 cgd
250 1.29 dholland static void
251 1.30 joerg prize(void)
252 1.1 cgd {
253 1.1 cgd int value;
254 1.1 cgd
255 1.1 cgd value = rnd(9) + 1;
256 1.1 cgd newpos(&goody);
257 1.1 cgd waddch(tv, value+'0');
258 1.1 cgd wrefresh(tv);
259 1.1 cgd }
260 1.1 cgd
261 1.29 dholland static void
262 1.30 joerg process(int ch)
263 1.1 cgd {
264 1.8 lukem int x,y;
265 1.1 cgd struct body *nh;
266 1.1 cgd
267 1.1 cgd alarm(0);
268 1.1 cgd x = head->x;
269 1.1 cgd y = head->y;
270 1.1 cgd switch(ch)
271 1.1 cgd {
272 1.17 cgd #ifdef KEY_LEFT
273 1.17 cgd case KEY_LEFT:
274 1.17 cgd #endif
275 1.17 cgd case 'h':
276 1.17 cgd x--; break;
277 1.17 cgd
278 1.17 cgd #ifdef KEY_DOWN
279 1.17 cgd case KEY_DOWN:
280 1.17 cgd #endif
281 1.17 cgd case 'j':
282 1.17 cgd y++; break;
283 1.17 cgd
284 1.17 cgd #ifdef KEY_UP
285 1.17 cgd case KEY_UP:
286 1.17 cgd #endif
287 1.17 cgd case 'k':
288 1.17 cgd y--; break;
289 1.17 cgd
290 1.17 cgd #ifdef KEY_RIGHT
291 1.17 cgd case KEY_RIGHT:
292 1.17 cgd #endif
293 1.17 cgd case 'l':
294 1.17 cgd x++; break;
295 1.17 cgd
296 1.4 mycroft case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
297 1.4 mycroft case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
298 1.4 mycroft case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
299 1.4 mycroft case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
300 1.4 mycroft case '\f': setup(); return;
301 1.17 cgd
302 1.17 cgd case ERR:
303 1.17 cgd case CNTRL('C'):
304 1.17 cgd case CNTRL('D'):
305 1.17 cgd crash();
306 1.17 cgd return;
307 1.17 cgd
308 1.4 mycroft default: if (! running) alarm(1);
309 1.1 cgd return;
310 1.1 cgd }
311 1.1 cgd lastch = ch;
312 1.1 cgd if (growing == 0)
313 1.1 cgd {
314 1.1 cgd display(tail, ' ');
315 1.1 cgd tail->next->prev = NULL;
316 1.1 cgd nh = tail->next;
317 1.1 cgd free(tail);
318 1.1 cgd tail = nh;
319 1.22 jsm visible_len--;
320 1.1 cgd }
321 1.1 cgd else growing--;
322 1.1 cgd display(head, BODY);
323 1.1 cgd wmove(tv, y, x);
324 1.1 cgd if (isdigit(ch = winch(tv)))
325 1.1 cgd {
326 1.1 cgd growing += ch-'0';
327 1.1 cgd prize();
328 1.1 cgd score += growing;
329 1.1 cgd running = 0;
330 1.21 jsm wmove(stw, 0, COLS - 12);
331 1.1 cgd wprintw(stw, "Score: %3d", score);
332 1.1 cgd wrefresh(stw);
333 1.1 cgd }
334 1.1 cgd else if(ch != ' ') crash();
335 1.1 cgd nh = newlink();
336 1.1 cgd nh->next = NULL;
337 1.1 cgd nh->prev = head;
338 1.1 cgd head->next = nh;
339 1.1 cgd nh->y = y;
340 1.1 cgd nh->x = x;
341 1.1 cgd display(nh, HEAD);
342 1.1 cgd head = nh;
343 1.22 jsm visible_len++;
344 1.1 cgd if (!(slow && running))
345 1.10 hubertf {
346 1.10 hubertf wmove(tv, head->y, head->x);
347 1.1 cgd wrefresh(tv);
348 1.10 hubertf }
349 1.1 cgd if (!running)
350 1.1 cgd alarm(1);
351 1.1 cgd }
352 1.1 cgd
353 1.29 dholland static void
354 1.30 joerg crash(void)
355 1.1 cgd {
356 1.8 lukem leave(0);
357 1.1 cgd }
358 1.1 cgd
359 1.29 dholland static void
360 1.30 joerg setup(void)
361 1.1 cgd {
362 1.1 cgd clear();
363 1.1 cgd refresh();
364 1.1 cgd touchwin(stw);
365 1.1 cgd wrefresh(stw);
366 1.1 cgd touchwin(tv);
367 1.1 cgd wrefresh(tv);
368 1.1 cgd alarm(1);
369 1.1 cgd }
370