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