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