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 $
35 * Worm. Written by Michael Toy
46 #define newlink() malloc(sizeof(struct body));
51 #define CNTRL(p) (p-'A'+1)
60 } *head
, *tail
, goody
;
61 static int growing
= 0;
62 static int running
= 0;
65 static int start_len
= LENGTH
;
67 static char outbuf
[BUFSIZ
];
69 static void crash(void) __dead2
;
70 static void display(struct body
*, char);
71 static void leave(int) __dead2
;
72 static void life(void);
73 static void newpos(struct body
*);
74 static void prize(void);
75 static void process(char);
77 static void setup(void);
78 static void suspend(int);
79 static void wake(int);
82 main(int argc
, char **argv
)
86 /* Revoke setgid privileges */
90 start_len
= atoi(argv
[1]);
91 if ((start_len
<= 0) || (start_len
> 500))
93 setbuf(stdout
, outbuf
);
95 signal(SIGALRM
, wake
);
96 signal(SIGINT
, leave
);
97 signal(SIGQUIT
, leave
);
98 signal(SIGTSTP
, suspend
); /* process control signal */
102 slow
= (baudrate() <= B1200
);
104 stw
= newwin(1, COLS
-1, 0, 0);
105 tv
= newwin(LINES
-1, COLS
-1, 1, 0);
108 scrollok(stw
, FALSE
);
110 wprintw(stw
, " Worm");
114 life(); /* Create the worm */
115 prize(); /* Put up a goal */
126 if (read(0, &ch
, 1) >= 0)
135 struct body
*bp
, *np
;
140 head
->x
= start_len
+2;
144 for (i
= 0, bp
= head
; i
< start_len
; i
++, bp
= np
) {
157 display(struct body
*pos
, char chr
)
159 wmove(tv
, pos
->y
, pos
->x
);
164 leave(int sig __unused
)
171 wake(int sig __unused
)
173 signal(SIGALRM
, wake
);
181 return (random() % range
);
185 newpos(struct body
*bp
)
188 bp
->y
= rnd(LINES
-3)+ 2;
189 bp
->x
= rnd(COLS
-3) + 1;
190 wmove(tv
, bp
->y
, bp
->x
);
191 } while(winch(tv
) != ' ');
201 waddch(tv
, value
+'0');
216 case 'h': x
--; break;
217 case 'j': y
++; break;
218 case 'k': y
--; break;
219 case 'l': x
++; break;
220 case 'H': x
--; running
= RUNLEN
; ch
= tolower(ch
); break;
221 case 'J': y
++; running
= RUNLEN
/2; ch
= tolower(ch
); break;
222 case 'K': y
--; running
= RUNLEN
/2; ch
= tolower(ch
); break;
223 case 'L': x
++; running
= RUNLEN
; ch
= tolower(ch
); break;
224 case '\f': setup(); return;
225 case CNTRL('Z'): suspend(0); return;
226 case CNTRL('C'): crash();
227 case CNTRL('D'): crash();
228 default: if (! running
) alarm(1);
235 tail
->next
->prev
= NULL
;
243 if (isdigit(ch
= winch(tv
)))
250 wprintw(stw
, "Score: %3d", score
);
253 else if(ch
!= ' ') crash();
262 if (!(slow
&& running
))
275 printf("Well, you ran into something and the game is over.\n");
276 printf("Your final score was %d\n", score
);
281 suspend(int sig __unused
)
287 kill(getpid(), SIGTSTP
);
288 signal(SIGTSTP
, suspend
);