Home | History | Annotate | Line # | Download | only in worm
worm.c revision 1.16
      1 /*	$NetBSD: worm.c,v 1.16 1999/09/12 09:02:24 jsm Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)worm.c	8.1 (Berkeley) 5/31/93";
     45 #else
     46 __RCSID("$NetBSD: worm.c,v 1.16 1999/09/12 09:02:24 jsm Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 /*
     51  * Worm.  Written by Michael Toy
     52  * UCSC
     53  */
     54 
     55 #include <ctype.h>
     56 #include <curses.h>
     57 #include <err.h>
     58 #include <signal.h>
     59 #include <stdlib.h>
     60 #include <termios.h>
     61 #include <unistd.h>
     62 
     63 #define newlink() (struct body *) malloc(sizeof (struct body));
     64 #define HEAD '@'
     65 #define BODY 'o'
     66 #define LENGTH 7
     67 #define RUNLEN 8
     68 #define CNTRL(p) (p-'A'+1)
     69 
     70 WINDOW *tv;
     71 WINDOW *stw;
     72 struct body {
     73 	int x;
     74 	int y;
     75 	struct body *prev;
     76 	struct body *next;
     77 } *head, *tail, goody;
     78 int growing = 0;
     79 int running = 0;
     80 int slow = 0;
     81 int score = 0;
     82 int start_len = LENGTH;
     83 char lastch;
     84 char outbuf[BUFSIZ];
     85 
     86 void	crash __P((void)) __attribute__((__noreturn__));
     87 void	display __P((const struct body *, char));
     88 int	main __P((int, char **));
     89 void	leave __P((int)) __attribute__((__noreturn__));
     90 void	life __P((void));
     91 void	newpos __P((struct body *));
     92 void	process __P((char));
     93 void	prize __P((void));
     94 int	rnd __P((int));
     95 void	setup __P((void));
     96 void	wake __P((int));
     97 
     98 int
     99 main(argc, argv)
    100 	int argc;
    101 	char **argv;
    102 {
    103 	char ch;
    104 
    105 	/* Revoke setgid privileges */
    106 	setregid(getgid(), getgid());
    107 
    108 	if (argc == 2)
    109 		start_len = atoi(argv[1]);
    110 	if ((start_len <= 0) || (start_len > 500))
    111 		start_len = LENGTH;
    112 	setbuf(stdout, outbuf);
    113 	srand(getpid());
    114 	signal(SIGALRM, wake);
    115 	signal(SIGINT, leave);
    116 	signal(SIGQUIT, leave);
    117 	initscr();
    118 	crmode();
    119 	noecho();
    120 	slow = (baudrate() <= 1200);
    121 	clear();
    122 	stw = newwin(1, COLS-1, 0, 0);
    123 	tv = newwin(LINES-1, COLS-1, 1, 0);
    124 	box(tv, '*', '*');
    125 	scrollok(tv, FALSE);
    126 	scrollok(stw, FALSE);
    127 	wmove(stw, 0, 0);
    128 	wprintw(stw, " Worm");
    129 	refresh();
    130 	wrefresh(stw);
    131 	wrefresh(tv);
    132 	life();			/* Create the worm */
    133 	prize();		/* Put up a goal */
    134 	while(1)
    135 	{
    136 		if (running)
    137 		{
    138 			running--;
    139 			process(lastch);
    140 		}
    141 		else
    142 		{
    143 		    fflush(stdout);
    144 		    if (read(0, &ch, 1) >= 0)
    145 			process(ch);
    146 		}
    147 	}
    148 }
    149 
    150 void
    151 life()
    152 {
    153 	struct body *bp, *np;
    154 	int i;
    155 
    156 	np = NULL;
    157 	head = newlink();
    158 	if (head == NULL)
    159 		errx(1, "out of memory");
    160 	head->x = start_len+2;
    161 	head->y = 12;
    162 	head->next = NULL;
    163 	display(head, HEAD);
    164 	for (i = 0, bp = head; i < start_len; i++, bp = np) {
    165 		np = newlink();
    166 		if (np == NULL)
    167 			errx(1, "out of memory");
    168 		np->next = bp;
    169 		bp->prev = np;
    170 		np->x = bp->x - 1;
    171 		np->y = bp->y;
    172 		display(np, BODY);
    173 	}
    174 	tail = np;
    175 	tail->prev = NULL;
    176 }
    177 
    178 void
    179 display(pos, chr)
    180 	const struct body *pos;
    181 	char chr;
    182 {
    183 	wmove(tv, pos->y, pos->x);
    184 	waddch(tv, chr);
    185 }
    186 
    187 void
    188 leave(dummy)
    189 	int dummy;
    190 {
    191 	endwin();
    192 
    193 	if (dummy == 0){	/* called via crash() */
    194 		printf("\nWell, you ran into something and the game is over.\n");
    195 		printf("Your final score was %d\n\n", score);
    196 	}
    197 	exit(0);
    198 }
    199 
    200 void
    201 wake(dummy)
    202 	int dummy __attribute__((__unused__));
    203 {
    204 	signal(SIGALRM, wake);
    205 	fflush(stdout);
    206 	process(lastch);
    207 }
    208 
    209 int
    210 rnd(range)
    211 	int range;
    212 {
    213 	return abs((rand()>>5)+(rand()>>5)) % range;
    214 }
    215 
    216 void
    217 newpos(bp)
    218 	struct body * bp;
    219 {
    220 	do {
    221 		bp->y = rnd(LINES-3)+ 2;
    222 		bp->x = rnd(COLS-3) + 1;
    223 		wmove(tv, bp->y, bp->x);
    224 	} while(winch(tv) != ' ');
    225 }
    226 
    227 void
    228 prize()
    229 {
    230 	int value;
    231 
    232 	value = rnd(9) + 1;
    233 	newpos(&goody);
    234 	waddch(tv, value+'0');
    235 	wrefresh(tv);
    236 }
    237 
    238 void
    239 process(ch)
    240 	char ch;
    241 {
    242 	int x,y;
    243 	struct body *nh;
    244 
    245 	alarm(0);
    246 	x = head->x;
    247 	y = head->y;
    248 	switch(ch)
    249 	{
    250 		case 'h': x--; break;
    251 		case 'j': y++; break;
    252 		case 'k': y--; break;
    253 		case 'l': x++; break;
    254 		case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
    255 		case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
    256 		case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
    257 		case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
    258 		case '\f': setup(); return;
    259 		case CNTRL('C'): crash(); return;
    260 		case CNTRL('D'): crash(); return;
    261 		default: if (! running) alarm(1);
    262 			   return;
    263 	}
    264 	lastch = ch;
    265 	if (growing == 0)
    266 	{
    267 		display(tail, ' ');
    268 		tail->next->prev = NULL;
    269 		nh = tail->next;
    270 		free(tail);
    271 		tail = nh;
    272 	}
    273 	else growing--;
    274 	display(head, BODY);
    275 	wmove(tv, y, x);
    276 	if (isdigit(ch = winch(tv)))
    277 	{
    278 		growing += ch-'0';
    279 		prize();
    280 		score += growing;
    281 		running = 0;
    282 		wmove(stw, 0, 68);
    283 		wprintw(stw, "Score: %3d", score);
    284 		wrefresh(stw);
    285 	}
    286 	else if(ch != ' ') crash();
    287 	nh = newlink();
    288 	if (nh == NULL)
    289 		errx(1, "out of memory");
    290 	nh->next = NULL;
    291 	nh->prev = head;
    292 	head->next = nh;
    293 	nh->y = y;
    294 	nh->x = x;
    295 	display(nh, HEAD);
    296 	head = nh;
    297 	if (!(slow && running))
    298 	{
    299 		wmove(tv, head->y, head->x);
    300 		wrefresh(tv);
    301 	}
    302 	if (!running)
    303 		alarm(1);
    304 }
    305 
    306 void
    307 crash()
    308 {
    309 	leave(0);
    310 }
    311 
    312 void
    313 setup()
    314 {
    315 	clear();
    316 	refresh();
    317 	touchwin(stw);
    318 	wrefresh(stw);
    319 	touchwin(tv);
    320 	wrefresh(tv);
    321 	alarm(1);
    322 }
    323