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