2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)worm.c 8.1 (Berkeley) 5/31/93
31 * $FreeBSD: src/games/worm/worm.c,v 1.9 1999/12/07 02:01:27 billf Exp $
32 * $DragonFly: src/games/worm/worm.c,v 1.5 2006/09/07 21:28:27 pavalos Exp $
36 * Worm. Written by Michael Toy
47 #define newlink() malloc(sizeof(struct body));
52 #define CNTRL(p) (p-'A'+1)
61 } *head, *tail, goody;
66 int start_len = LENGTH;
71 void display(struct body *, char);
74 void newpos(struct body *);
83 main(int argc, char **argv)
87 /* Revoke setgid privileges */
91 start_len = atoi(argv[1]);
92 if ((start_len <= 0) || (start_len > 500))
94 setbuf(stdout, outbuf);
96 signal(SIGALRM, wake);
97 signal(SIGINT, leave);
98 signal(SIGQUIT, leave);
99 signal(SIGTSTP, suspend); /* process control signal */
103 slow = (baudrate() <= B1200);
105 stw = newwin(1, COLS-1, 0, 0);
106 tv = newwin(LINES-1, COLS-1, 1, 0);
109 scrollok(stw, FALSE);
111 wprintw(stw, " Worm");
115 life(); /* Create the worm */
116 prize(); /* Put up a goal */
127 if (read(0, &ch, 1) >= 0)
136 struct body *bp, *np;
141 head->x = start_len+2;
145 for (i = 0, bp = head; i < start_len; i++, bp = np) {
158 display(struct body *pos, char chr)
160 wmove(tv, pos->y, pos->x);
165 leave(int sig __unused)
172 wake(int sig __unused)
174 signal(SIGALRM, wake);
182 return (random() % range);
186 newpos(struct body *bp)
189 bp->y = rnd(LINES-3)+ 2;
190 bp->x = rnd(COLS-3) + 1;
191 wmove(tv, bp->y, bp->x);
192 } while(winch(tv) != ' ');
202 waddch(tv, value+'0');
217 case 'h': x--; break;
218 case 'j': y++; break;
219 case 'k': y--; break;
220 case 'l': x++; break;
221 case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
222 case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
223 case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
224 case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
225 case '\f': setup(); return;
226 case CNTRL('Z'): suspend(0); return;
227 case CNTRL('C'): crash(); return;
228 case CNTRL('D'): crash(); return;
229 default: if (! running) alarm(1);
236 tail->next->prev = NULL;
244 if (isdigit(ch = winch(tv)))
251 wprintw(stw, "Score: %3d", score);
254 else if(ch != ' ') crash();
263 if (!(slow && running))
276 printf("Well, you ran into something and the game is over.\n");
277 printf("Your final score was %d\n", score);
282 suspend(int sig __unused)
288 kill(getpid(), SIGTSTP);
289 signal(SIGTSTP, suspend);