Home | History | Annotate | Line # | Download | only in worm
worm.c revision 1.5
      1 /*	$NetBSD: worm.c,v 1.5 1995/04/22 07:56:23 cgd 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 #ifndef lint
     37 static char copyright[] =
     38 "@(#) 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 static char rcsid[] = "$NetBSD: worm.c,v 1.5 1995/04/22 07:56:23 cgd 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 <signal.h>
     58 #include <termios.h>
     59 
     60 #define newlink() (struct body *) malloc(sizeof (struct body));
     61 #define HEAD '@'
     62 #define BODY 'o'
     63 #define LENGTH 7
     64 #define RUNLEN 8
     65 #define CNTRL(p) (p-'A'+1)
     66 #ifndef baudrate
     67 # define	baudrate()	_tty.sg_ospeed
     68 #endif
     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 leave(), wake(), suspend();
     87 
     88 main(argc, argv)
     89 	int argc;
     90 	char **argv;
     91 {
     92 	char ch;
     93 
     94 	if (argc == 2)
     95 		start_len = atoi(argv[1]);
     96 	if ((start_len <= 0) || (start_len > 500))
     97 		start_len = LENGTH;
     98 	setbuf(stdout, outbuf);
     99 	srand(getpid());
    100 	signal(SIGALRM, wake);
    101 	signal(SIGINT, leave);
    102 	signal(SIGQUIT, leave);
    103 	signal(SIGTSTP, suspend);	/* process control signal */
    104 	initscr();
    105 	crmode();
    106 	noecho();
    107 	slow = (baudrate() <= 1200);
    108 	clear();
    109 	stw = newwin(1, COLS-1, 0, 0);
    110 	tv = newwin(LINES-1, COLS-1, 1, 0);
    111 	box(tv, '*', '*');
    112 	scrollok(tv, FALSE);
    113 	scrollok(stw, FALSE);
    114 	wmove(stw, 0, 0);
    115 	wprintw(stw, " Worm");
    116 	refresh();
    117 	wrefresh(stw);
    118 	wrefresh(tv);
    119 	life();			/* Create the worm */
    120 	prize();		/* Put up a goal */
    121 	while(1)
    122 	{
    123 		if (running)
    124 		{
    125 			running--;
    126 			process(lastch);
    127 		}
    128 		else
    129 		{
    130 		    fflush(stdout);
    131 		    if (read(0, &ch, 1) >= 0)
    132 			process(ch);
    133 		}
    134 	}
    135 }
    136 
    137 life()
    138 {
    139 	register struct body *bp, *np;
    140 	register int i;
    141 
    142 	head = newlink();
    143 	head->x = start_len+2;
    144 	head->y = 12;
    145 	head->next = NULL;
    146 	display(head, HEAD);
    147 	for (i = 0, bp = head; i < start_len; i++, bp = np) {
    148 		np = newlink();
    149 		np->next = bp;
    150 		bp->prev = np;
    151 		np->x = bp->x - 1;
    152 		np->y = bp->y;
    153 		display(np, BODY);
    154 	}
    155 	tail = np;
    156 	tail->prev = NULL;
    157 }
    158 
    159 display(pos, chr)
    160 struct body *pos;
    161 char chr;
    162 {
    163 	wmove(tv, pos->y, pos->x);
    164 	waddch(tv, chr);
    165 }
    166 
    167 void
    168 leave()
    169 {
    170 	endwin();
    171 	exit(0);
    172 }
    173 
    174 void
    175 wake()
    176 {
    177 	signal(SIGALRM, wake);
    178 	fflush(stdout);
    179 	process(lastch);
    180 }
    181 
    182 rnd(range)
    183 {
    184 	return abs((rand()>>5)+(rand()>>5)) % range;
    185 }
    186 
    187 newpos(bp)
    188 struct body * bp;
    189 {
    190 	do {
    191 		bp->y = rnd(LINES-3)+ 2;
    192 		bp->x = rnd(COLS-3) + 1;
    193 		wmove(tv, bp->y, bp->x);
    194 	} while(winch(tv) != ' ');
    195 }
    196 
    197 prize()
    198 {
    199 	int value;
    200 
    201 	value = rnd(9) + 1;
    202 	newpos(&goody);
    203 	waddch(tv, value+'0');
    204 	wrefresh(tv);
    205 }
    206 
    207 process(ch)
    208 char ch;
    209 {
    210 	register int x,y;
    211 	struct body *nh;
    212 
    213 	alarm(0);
    214 	x = head->x;
    215 	y = head->y;
    216 	switch(ch)
    217 	{
    218 		case 'h': x--; break;
    219 		case 'j': y++; break;
    220 		case 'k': y--; break;
    221 		case 'l': x++; break;
    222 		case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
    223 		case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
    224 		case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
    225 		case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
    226 		case '\f': setup(); return;
    227 		case CNTRL('Z'): suspend(); return;
    228 		case CNTRL('C'): crash(); return;
    229 		case CNTRL('D'): crash(); return;
    230 		default: if (! running) alarm(1);
    231 			   return;
    232 	}
    233 	lastch = ch;
    234 	if (growing == 0)
    235 	{
    236 		display(tail, ' ');
    237 		tail->next->prev = NULL;
    238 		nh = tail->next;
    239 		free(tail);
    240 		tail = nh;
    241 	}
    242 	else growing--;
    243 	display(head, BODY);
    244 	wmove(tv, y, x);
    245 	if (isdigit(ch = winch(tv)))
    246 	{
    247 		growing += ch-'0';
    248 		prize();
    249 		score += growing;
    250 		running = 0;
    251 		wmove(stw, 0, 68);
    252 		wprintw(stw, "Score: %3d", score);
    253 		wrefresh(stw);
    254 	}
    255 	else if(ch != ' ') crash();
    256 	nh = newlink();
    257 	nh->next = NULL;
    258 	nh->prev = head;
    259 	head->next = nh;
    260 	nh->y = y;
    261 	nh->x = x;
    262 	display(nh, HEAD);
    263 	head = nh;
    264 	if (!(slow && running))
    265 		wrefresh(tv);
    266 	if (!running)
    267 		alarm(1);
    268 }
    269 
    270 crash()
    271 {
    272 	sleep(2);
    273 	clear();
    274 	move(23, 0);
    275 	refresh();
    276 	printf("Well, you ran into something and the game is over.\n");
    277 	printf("Your final score was %d\n", score);
    278 	leave();
    279 }
    280 
    281 void
    282 suspend()
    283 {
    284 	char *sh;
    285 
    286 	move(LINES-1, 0);
    287 	refresh();
    288 	endwin();
    289 	fflush(stdout);
    290 	kill(getpid(), SIGTSTP);
    291 	signal(SIGTSTP, suspend);
    292 	crmode();
    293 	noecho();
    294 	setup();
    295 }
    296 
    297 setup()
    298 {
    299 	clear();
    300 	refresh();
    301 	touchwin(stw);
    302 	wrefresh(stw);
    303 	touchwin(tv);
    304 	wrefresh(tv);
    305 	alarm(1);
    306 }
    307