diff options
author | Steve Price <steve@FreeBSD.org> | 1999-05-03 02:08:31 +0000 |
---|---|---|
committer | Steve Price <steve@FreeBSD.org> | 1999-05-03 02:08:31 +0000 |
commit | 5b85aead34c48cf51de64773a8ad1e7a85df25ca (patch) | |
tree | 241169b54d0dd430463949c9fc989c0e0527e3eb /games/worms/worms.c | |
parent | 9045b882d55d151a5fcd41cb18eb834cb07b0996 (diff) |
Round two of MetBSD games merges.
Submitted by: Stephen J. Roznowski <sjr@home.net>
Notes
Notes:
svn path=/head/; revision=46351
Diffstat (limited to 'games/worms/worms.c')
-rw-r--r-- | games/worms/worms.c | 195 |
1 files changed, 50 insertions, 145 deletions
diff --git a/games/worms/worms.c b/games/worms/worms.c index 0a0371b15461..ca0658ee435b 100644 --- a/games/worms/worms.c +++ b/games/worms/worms.c @@ -59,13 +59,14 @@ static char sccsid[] = "@(#)worms.c 8.1 (Berkeley) 5/31/93"; * */ #include <sys/types.h> -#include <sgtty.h> +#include <curses.h> +#include <err.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> -static struct options { +static const struct options { int nopts; int opts[3]; } @@ -159,13 +160,11 @@ static struct options { { 0, { 0, 0, 0 } } }; -#define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar) -char *tcp; -static char flavor[] = { +static const char flavor[] = { 'O', '*', '#', '$', '%', '0', '@', '~' }; -static short xinc[] = { +static const short xinc[] = { 1, 1, 1, 0, -1, -1, -1, 0 }, yinc[] = { -1, 0, 1, 1, 1, 0, -1, -1 @@ -175,11 +174,9 @@ static struct worm { short *xpos, *ypos; } *worm; -void fputchar __P((int)); +volatile sig_atomic_t sig_caught = 0; void onsig __P((int)); -char *tgetstr __P((char *, char **)); -char *tgoto __P((char *, int, int)); -int tputs __P((char *, int, void (*)(int))); +void nomem(void); int main(argc, argv) @@ -187,42 +184,42 @@ main(argc, argv) char *argv[]; { extern int optind; - extern char *optarg, *UP; - register int x, y, h, n; - register struct worm *w; - register struct options *op; - register short *ip; - register char *term; - int CO, IN, LI, last, bottom, ch, length, number, trail, Wrap; + extern char *optarg; + int x, y, h, n; + struct worm *w; + const struct options *op; + short *ip; + int CO, LI, last, bottom, ch, length, number, trail; short **ref; - char *AL, *BC, *CM, *EI, *HO, *IC, *IM, *IP, *SR; - char *field, tcb[100], *mp; long random(); - struct sgttyb tt; - extern short ospeed; + const char *field; + char *mp; + unsigned int delay = 0; + mp = NULL; length = 16; number = 3; trail = ' '; field = NULL; - while ((ch = getopt(argc, argv, "fl:n:t")) != -1) + while ((ch = getopt(argc, argv, "d:fl:n:t")) != -1) switch(ch) { + case 'd': + if ((delay = (unsigned int)strtoul(optarg, (char **)NULL, 10)) < 1 || delay > 1000) + errx(1, "invalid delay (1-1000)"); + delay *= 1000; /* ms -> us */ + break; case 'f': field = "WORM"; break; case 'l': if ((length = atoi(optarg)) < 2 || length > 1024) { - (void)fprintf(stderr, - "worms: invalid length (%d - %d).\n", + errx(1, "invalid length (%d - %d).", 2, 1024); - exit(1); } break; case 'n': if ((number = atoi(optarg)) < 1) { - (void)fprintf(stderr, - "worms: invalid number of worms.\n"); - exit(1); + errx(1, "invalid number of worms."); } break; case 't': @@ -231,47 +228,18 @@ main(argc, argv) case '?': default: (void)fprintf(stderr, - "usage: worms [-ft] [-l length] [-n number]\n"); + "usage: worms [-ft] [-d delay] [-l length] [-n number]\n"); exit(1); } - if (!(term = getenv("TERM"))) { - (void)fprintf(stderr, "worms: no TERM environment variable.\n"); - exit(1); - } if (!(worm = malloc((size_t)number * sizeof(struct worm))) || !(mp = malloc((size_t)1024))) nomem(); - if (tgetent(mp, term) <= 0) { - (void)fprintf(stderr, "worms: %s: unknown terminal type.\n", - term); - exit(1); - } - tcp = tcb; - if (gtty(1, &tt) == 0) - ospeed = tt.sg_ospeed; - if (!(CM = tgetstr("cm", &tcp))) { - (void)fprintf(stderr, - "worms: terminal incapable of cursor motion.\n"); - exit(1); - } - AL = tgetstr("al", &tcp); - BC = tgetflag("bs") ? "\b" : tgetstr("bc", &tcp); - if ((CO = tgetnum("co")) <= 0) - CO = 80; + initscr(); + CO = COLS; + LI = LINES; last = CO - 1; - EI = tgetstr("ei", &tcp); - HO = tgetstr("ho", &tcp); - IC = tgetstr("ic", &tcp); - IM = tgetstr("im", &tcp); - IN = tgetflag("in"); - IP = tgetstr("ip", &tcp); - if ((LI = tgetnum("li")) <= 0) - LI = 24; bottom = LI - 1; - SR = tgetstr("sr", &tcp); - UP = tgetstr("up", &tcp); - Wrap = tgetflag("am"); if (!(ip = malloc((size_t)(LI * CO * sizeof(short))))) nomem(); if (!(ref = malloc((size_t)(LI * sizeof(short *))))) @@ -282,8 +250,6 @@ main(argc, argv) } for (ip = ref[0], n = LI * CO; --n >= 0;) *ip++ = 0; - if (Wrap) - ref[bottom][last] = 1; for (n = number, w = &worm[0]; --n >= 0; w++) { w->orientation = w->head = 0; if (!(ip = malloc((size_t)(length * sizeof(short))))) @@ -305,78 +271,29 @@ main(argc, argv) (void)signal(SIGTSTP, onsig); (void)signal(SIGTERM, onsig); - tputs(tgetstr("ti", &tcp), 1, fputchar); - tputs(tgetstr("cl", &tcp), 1, fputchar); if (field) { - register char *p = field; + const char *p = field; - for (y = bottom; --y >= 0;) { + for (y = LI; --y >= 0;) { for (x = CO; --x >= 0;) { - fputchar(*p++); + addch(*p++); if (!*p) p = field; } - if (!Wrap) - fputchar('\n'); - (void)fflush(stdout); - } - if (Wrap) { - if (IM && !IN) { - for (x = last; --x > 0;) { - fputchar(*p++); - if (!*p) - p = field; - } - y = *p++; - if (!*p) - p = field; - fputchar(*p); - if (BC) - tputs(BC, 1, fputchar); - else - cursor(last - 1, bottom); - tputs(IM, 1, fputchar); - if (IC) - tputs(IC, 1, fputchar); - fputchar(y); - if (IP) - tputs(IP, 1, fputchar); - tputs(EI, 1, fputchar); - } - else if (SR || AL) { - if (HO) - tputs(HO, 1, fputchar); - else - cursor(0, 0); - if (SR) - tputs(SR, 1, fputchar); - else - tputs(AL, LI, fputchar); - for (x = CO; --x >= 0;) { - fputchar(*p++); - if (!*p) - p = field; - } - } - else for (x = last; --x >= 0;) { - fputchar(*p++); - if (!*p) - p = field; - } - } - else for (x = CO; --x >= 0;) { - fputchar(*p++); - if (!*p) - p = field; + refresh(); } } for (;;) { - (void)fflush(stdout); + refresh(); + if (sig_caught) { + endwin(); + exit(0); + } for (n = 0, w = &worm[0]; n < number; n++, w++) { if ((x = w->xpos[h = w->head]) < 0) { - cursor(x = w->xpos[h] = 0, - y = w->ypos[h] = bottom); - fputchar(flavor[n % sizeof(flavor)]); + mvaddch(y = w->ypos[h] = bottom, + x = w->xpos[h] = 0, + flavor[n % sizeof(flavor)]); ref[y][x]++; } else @@ -384,22 +301,20 @@ main(argc, argv) if (++h == length) h = 0; if (w->xpos[w->head = h] >= 0) { - register int x1, y1; + int x1, y1; x1 = w->xpos[h]; y1 = w->ypos[h]; if (--ref[y1][x1] == 0) { - cursor(x1, y1); - if (trail) - fputchar(trail); + mvaddch(y1, x1, trail); } } op = &(!x ? (!y ? upleft : (y == bottom ? lowleft : left)) : (x == last ? (!y ? upright : (y == bottom ? lowright : right)) : (!y ? upper : (y == bottom ? lower : normal))))[w->orientation]; switch (op->nopts) { case 0: - (void)fflush(stdout); + refresh(); abort(); - return; + return(1); case 1: w->orientation = op->opts[0]; break; @@ -407,10 +322,9 @@ main(argc, argv) w->orientation = op->opts[(int)random() % op->nopts]; } - cursor(x += xinc[w->orientation], - y += yinc[w->orientation]); - if (!Wrap || x != last || y != bottom) - fputchar(flavor[n % sizeof(flavor)]); + mvaddch(y += yinc[w->orientation], + x += xinc[w->orientation], + flavor[n % sizeof(flavor)]); ref[w->ypos[h] = y][w->xpos[h] = x]++; } } @@ -420,20 +334,11 @@ void onsig(signo) int signo; { - tputs(tgetstr("cl", &tcp), 1, fputchar); - tputs(tgetstr("te", &tcp), 1, fputchar); - exit(0); + sig_caught = 1; } void -fputchar(c) - int c; -{ - (void)putchar(c); -} - nomem() { - (void)fprintf(stderr, "worms: not enough memory.\n"); - exit(1); + errx(1, "not enough memory."); } |