X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/29936b86526850aa49ea5459a24842e02f77a016..a7b9dc4a78226b842ead8ccfe243df5f798c272f:/worm/worm.c diff --git a/worm/worm.c b/worm/worm.c index 25d84c3b..225ed0a1 100644 --- a/worm/worm.c +++ b/worm/worm.c @@ -1,4 +1,4 @@ -/* $NetBSD: worm.c,v 1.17 1999/10/26 06:35:49 cgd Exp $ */ +/* $NetBSD: worm.c,v 1.31 2015/08/17 17:17:01 dholland Exp $ */ /* * Copyright (c) 1980, 1993 @@ -12,11 +12,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -35,15 +31,15 @@ #include #ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); +__COPYRIGHT("@(#) Copyright (c) 1980, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)worm.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: worm.c,v 1.17 1999/10/26 06:35:49 cgd Exp $"); +__RCSID("$NetBSD: worm.c,v 1.31 2015/08/17 17:17:01 dholland Exp $"); #endif #endif /* not lint */ @@ -60,67 +56,89 @@ __RCSID("$NetBSD: worm.c,v 1.17 1999/10/26 06:35:49 cgd Exp $"); #include #include -#define newlink() (struct body *) malloc(sizeof (struct body)); #define HEAD '@' #define BODY 'o' #define LENGTH 7 #define RUNLEN 8 #define CNTRL(p) (p-'A'+1) -WINDOW *tv; -WINDOW *stw; struct body { int x; int y; struct body *prev; struct body *next; -} *head, *tail, goody; -int growing = 0; -int running = 0; -int slow = 0; -int score = 0; -int start_len = LENGTH; -int lastch; -char outbuf[BUFSIZ]; - -void crash __P((void)) __attribute__((__noreturn__)); -void display __P((const struct body *, char)); -int main __P((int, char **)); -void leave __P((int)) __attribute__((__noreturn__)); -void life __P((void)); -void newpos __P((struct body *)); -void process __P((int)); -void prize __P((void)); -int rnd __P((int)); -void setup __P((void)); -void wake __P((int)); +}; + +static WINDOW *tv; +static WINDOW *stw; +static struct body *head, *tail, goody; +static int growing = 0; +static int running = 0; +static int slow = 0; +static int score = 0; +static int start_len = LENGTH; +static int visible_len; +static int lastch; +static char outbuf[BUFSIZ]; + +int main(int, char **); +static void crash(void) __dead; +static void display(const struct body *, char); +static void leave(int) __dead; +static void life(void); +static void newpos(struct body *); +static void process(int); +static void prize(void); +static int rnd(int); +static void setup(void); +static void wake(int); + +static struct body * +newlink(void) +{ + struct body *b; + + b = malloc(sizeof(*b)); + if (b == NULL) { + err(EXIT_FAILURE, NULL); + } + return b; +} int -main(argc, argv) - int argc; - char **argv; +main(int argc, char **argv) { /* Revoke setgid privileges */ - setregid(getgid(), getgid()); + setgid(getgid()); - if (argc == 2) - start_len = atoi(argv[1]); - if ((start_len <= 0) || (start_len > 500)) - start_len = LENGTH; setbuf(stdout, outbuf); srand(getpid()); signal(SIGALRM, wake); signal(SIGINT, leave); signal(SIGQUIT, leave); - initscr(); - crmode(); + if (!initscr()) + errx(0, "couldn't initialize screen"); + cbreak(); noecho(); #ifdef KEY_LEFT keypad(stdscr, TRUE); #endif slow = (baudrate() <= 1200); clear(); + if (COLS < 18 || LINES < 5) { + /* + * Insufficient room for the line with " Worm" and the + * score if fewer than 18 columns; insufficient room for + * anything much if fewer than 5 lines. + */ + endwin(); + errx(1, "screen too small"); + } + if (argc == 2) + start_len = atoi(argv[1]); + if ((start_len <= 0) || (start_len > ((LINES-3) * (COLS-2)) / 3)) + start_len = LENGTH; stw = newwin(1, COLS-1, 0, 0); tv = newwin(LINES-1, COLS-1, 1, 0); box(tv, '*', '*'); @@ -148,46 +166,46 @@ main(argc, argv) } } -void -life() +static void +life(void) { struct body *bp, *np; - int i; + int i, j = 1; np = NULL; head = newlink(); - if (head == NULL) - errx(1, "out of memory"); - head->x = start_len+2; - head->y = 12; + head->x = start_len % (COLS-5) + 2; + head->y = LINES / 2; head->next = NULL; display(head, HEAD); for (i = 0, bp = head; i < start_len; i++, bp = np) { np = newlink(); - if (np == NULL) - errx(1, "out of memory"); np->next = bp; bp->prev = np; - np->x = bp->x - 1; - np->y = bp->y; + if (((bp->x <= 2) && (j == 1)) || ((bp->x >= COLS-4) && (j == -1))) { + j *= -1; + np->x = bp->x; + np->y = bp->y + 1; + } else { + np->x = bp->x - j; + np->y = bp->y; + } display(np, BODY); } tail = np; tail->prev = NULL; + visible_len = start_len + 1; } -void -display(pos, chr) - const struct body *pos; - char chr; +static void +display(const struct body *pos, char chr) { wmove(tv, pos->y, pos->x); waddch(tv, chr); } -void -leave(dummy) - int dummy; +static void +leave(int dummy) { endwin(); @@ -198,35 +216,39 @@ leave(dummy) exit(0); } -void -wake(dummy) - int dummy __attribute__((__unused__)); +static void +wake(int dummy) { signal(SIGALRM, wake); fflush(stdout); process(lastch); } -int -rnd(range) - int range; +static int +rnd(int range) { return abs((rand()>>5)+(rand()>>5)) % range; } -void -newpos(bp) - struct body * bp; +static void +newpos(struct body *bp) { + if (visible_len == (LINES-3) * (COLS-3) - 1) { + endwin(); + + printf("\nYou won!\n"); + printf("Your final score was %d\n\n", score); + exit(0); + } do { - bp->y = rnd(LINES-3)+ 2; + bp->y = rnd(LINES-3)+ 1; bp->x = rnd(COLS-3) + 1; wmove(tv, bp->y, bp->x); } while(winch(tv) != ' '); } -void -prize() +static void +prize(void) { int value; @@ -236,9 +258,8 @@ prize() wrefresh(tv); } -void -process(ch) - int ch; +static void +process(int ch) { int x,y; struct body *nh; @@ -295,6 +316,7 @@ process(ch) nh = tail->next; free(tail); tail = nh; + visible_len--; } else growing--; display(head, BODY); @@ -305,14 +327,12 @@ process(ch) prize(); score += growing; running = 0; - wmove(stw, 0, 68); + wmove(stw, 0, COLS - 12); wprintw(stw, "Score: %3d", score); wrefresh(stw); } else if(ch != ' ') crash(); nh = newlink(); - if (nh == NULL) - errx(1, "out of memory"); nh->next = NULL; nh->prev = head; head->next = nh; @@ -320,6 +340,7 @@ process(ch) nh->x = x; display(nh, HEAD); head = nh; + visible_len++; if (!(slow && running)) { wmove(tv, head->y, head->x); @@ -329,14 +350,14 @@ process(ch) alarm(1); } -void -crash() +static void +crash(void) { leave(0); } -void -setup() +static void +setup(void) { clear(); refresh();