Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/worms Add a little optional colour, optionally disting...



details:   https://anonhg.NetBSD.org/src/rev/7a999472dcb9
branches:  trunk
changeset: 374483:7a999472dcb9
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Apr 26 22:58:09 2023 +0000

description:
Add a little optional colour, optionally distinguish worm heads,
and optionally randomise worm lengths.   Just exit instead of
continuing with a nonsense display if the window shrinks (and
for consistency if it grows).

Most of the ideas and code from RVP.   Bugs and man page mangling
from me.

diffstat:

 games/worms/worms.6 |   41 ++++++-
 games/worms/worms.c |  276 ++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 263 insertions(+), 54 deletions(-)

diffs (truncated from 565 to 300 lines):

diff -r 84fae393ba88 -r 7a999472dcb9 games/worms/worms.6
--- a/games/worms/worms.6       Wed Apr 26 18:42:41 2023 +0000
+++ b/games/worms/worms.6       Wed Apr 26 22:58:09 2023 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: worms.6,v 1.17 2023/04/18 15:02:22 kre Exp $
+.\"    $NetBSD: worms.6,v 1.18 2023/04/26 22:58:09 kre Exp $
 .\"
 .\" Copyright (c) 1989, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    @(#)worms.6     8.1 (Berkeley) 5/31/93
 .\"
-.Dd April 17, 2023
+.Dd April 26, 2023
 .Dt WORMS 6
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd animate worms on a display terminal
 .Sh SYNOPSIS
 .Nm
-.Op Fl ft
+.Op Fl CfHrt
 .Op Fl d Ar delay
 .Op Fl l Ar length
 .Op Fl n Ar number
@@ -50,7 +50,11 @@ version of the DEC-2136 program
 .Dq worms .
 .Pp
 The options are as follows:
-.Bl -tag -width Fl
+.Bl -compact -tag -width Fl
+.Pp
+.It Fl C
+Use colours, if available, to make the worms easier to distinguish.
+.Pp
 .It Fl d Ar delay
 Specifies a
 .Ar delay ,
@@ -58,24 +62,53 @@ in milliseconds, between each update.
 This is useful for fast terminals.
 Reasonable values are around 20-200;
 the default is 20.
+.Pp
 .It Fl f
 Makes a
 .Dq field
 for the worm(s) to eat.
+.Pp
+.It Fl H
+Display the head of the worm differently than its body.
+.Pp
 .It Fl l Ar length
+.It Fl l Ar min Ns \(mi Ns Ar max
 Specifies the
 .Ar length
 of each worm; the default is 16, the minimum is 2.
+In the second form, worm lengths are randomly chosen
+between
+.Ar min
+.Pq which must be at least 2
+and
+.Ar max .
+The
+.Ar max
+worm length will be reduced if required by the screen
+size and the
+.Ar number
+of worms selected.
+This option overrides any earlier
+.Fl r .
+.Pp
 .It Fl n Ar number
 Specifies the
 .Ar number
 of worms; the default is 3.
 There must be at least one.
+.Pp
+.It Fl r
+Use random lengths for the worms, within a range of
+sizes chosen to suit the screen size.
+Note this option overrides any earlier
+.Fl l .
+.Pp
 .It Fl S Ar seed
 Provide an integer
 .Ar seed
 for the random number generator.
 Specifying zero (0, the default) causes a random seed to be used.
+.Pp
 .It Fl t
 Makes each worm leave a trail behind it.
 .El
diff -r 84fae393ba88 -r 7a999472dcb9 games/worms/worms.c
--- a/games/worms/worms.c       Wed Apr 26 18:42:41 2023 +0000
+++ b/games/worms/worms.c       Wed Apr 26 22:58:09 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: worms.c,v 1.29 2023/04/19 09:39:29 kre Exp $   */
+/*     $NetBSD: worms.c,v 1.30 2023/04/26 22:58:09 kre Exp $   */
 
 /*
  * Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)worms.c    8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: worms.c,v 1.29 2023/04/19 09:39:29 kre Exp $");
+__RCSID("$NetBSD: worms.c,v 1.30 2023/04/26 22:58:09 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -71,6 +71,7 @@ static char sccsid[] = "@(#)worms.c   8.1 
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
+#include <term.h>
 #include <unistd.h>
 
 static const struct options {
@@ -86,7 +87,8 @@ static const struct options {
        { 3, { 4, 5, 6 } },
        { 3, { 5, 6, 7 } },
        { 3, { 6, 7, 0 } }
-},     upper[8] = {
+},
+       upper[8] = {
        { 1, { 1, 0, 0 } },
        { 2, { 1, 2, 0 } },
        { 0, { 0, 0, 0 } },
@@ -166,50 +168,110 @@ static const struct options {
        { 0, { 0, 0, 0 } },
        { 0, { 0, 0, 0 } }
 };
-
+static const char      flavor[] = {
+       'O', '*', '#', '$', '%', '0', 'o', '~',
+       '+', 'x', ':', '^', '_', '&', '@', 'w'
+};
+static const int flavors = __arraycount(flavor);
 
-static const char      flavor[] = {
-       'O', '*', '#', '$', '%', '0', '@', '~',
-       '+', 'w', ':', '^', '_', '&', 'x', 'o'
-};
 static const short     xinc[] = {
        1,  1,  1,  0, -1, -1, -1,  0
 }, yinc[] = {
        -1,  0,  1,  1,  1,  0, -1, -1
 };
 static struct  worm {
-       int orientation, head;
+       int orientation, head, len;
        short *xpos, *ypos;
+       chtype ch, attr;
 } *worm;
 
-static volatile sig_atomic_t sig_caught = 0;
+static volatile sig_atomic_t sig_caught;
 
+static int initclr(int**);
 static void nomem(void) __dead;
 static void onsig(int);
+static int worm_length(int, int);
 
 int
 main(int argc, char *argv[])
 {
-       int x, y, h, n;
+       int CO, LI, last, bottom, ch, number, trail;
+       int x, y, h, n, nc;
+       int maxlength, minlength;
+       unsigned int seed, delay;
+       const struct options *op;
        struct worm *w;
-       const struct options *op;
+       short **ref;
        short *ip;
-       int CO, LI, last, bottom, ch, length, number, trail;
-       unsigned int seed;
-       short **ref;
        const char *field;
        char *ep;
-       unsigned int delay = 20000;
-       unsigned long ul;
+       unsigned long ul, up;
        bool argerror = false;
+       bool docolour = false;          /* -C, use coloured worms */
+       bool docaput = false;           /* -H, show which end of worm is head */
+       int *ctab = NULL;
 
-       length = 16;
+       delay = 20000;
+       maxlength = minlength = 16;
        number = 3;
+       seed = 0;
        trail = ' ';
        field = NULL;
-       seed = 0;
-       while ((ch = getopt(argc, argv, "d:fl:n:S:t")) != -1) {
+
+       if ((ep = getenv("WORMS")) != NULL) {
+               ul = up = 0;
+               while ((ch = *ep++) != '\0') {
+                       switch (ch) {
+                       case 'C':
+                               docolour = !docolour;
+                               continue;
+                       case 'f':
+                               if (field)
+                                       field = NULL;
+                               else
+                                       field = "WORM";
+                               continue;
+                       case 'H':
+                               docaput = !docaput;
+                               continue;
+                       case 'r':
+                               minlength = 5;
+                               maxlength = 0;
+                               continue;
+                       case 't':
+                               if (trail == ' ')
+                                       trail = '.';
+                               else
+                                       trail = ' ';
+                               continue;
+                       case '0': case '1': case '2': case '3': case '4':
+                       case '5': case '6': case '7': case '8': case '9':
+                               if (up > 1)
+                                       continue;
+                               if (ul >= 100000)       /* 1/10 second, in us */
+                                       continue;
+                               ul *= 10;
+                               ul += (ch - '0');
+                               up = 1;
+                               continue;
+                       case 'm':
+                               if (up == 1 && ul <= 1000)
+                                       ul *= 1000;
+                               up += 2;
+                               continue;
+                       default:
+                               continue;
+                       }
+               }
+               if ((up & 1) != 0)      /* up == 1 || up == 3 */
+                       delay = ul;
+       }
+
+       while ((ch = getopt(argc, argv, "Cd:fHl:n:rS:t")) != -1) {
                switch(ch) {
+               case 'C':
+                       docolour = !docolour;
+                       continue;
                case 'd':
                        ul = strtoul(optarg, &ep, 10);
                        if (ep != optarg) {
@@ -239,16 +301,29 @@ main(int argc, char *argv[])
                        delay = (unsigned int)ul;
                        continue;
                case 'f':
-                       field = "WORM";
+                       if (field == NULL)
+                               field = "WORM";
+                       else
+                               field = NULL;
+                       continue;
+               case 'H':
+                       docaput = !docaput;
                        continue;
                case 'l':
-                       ul = strtoul(optarg, &ep, 10);
+                       up = ul = strtoul(optarg, &ep, 10);
+                       if (ep != optarg) {
+                               while (isspace(*(unsigned char *)ep))
+                                       ep++;
+                               if (*ep == '-')
+                                       up = strtoul(++ep, &ep, 10);
+                       }
                        if (ep == optarg || *ep != '\0' ||
-                           ul < 2 || ul > 1024) {
+                           ul < 2 || up < ul || up > 1024) {
                                errx(1, "-l: invalid length (%s) [%d - %d].",
                                     optarg, 2, 1024);
                        }
-                       length = (int)ul;
+                       minlength = (int)ul;
+                       maxlength = (int)up;
                        continue;
                case 'n':
                        ul = strtoul(optarg, &ep, 10);
@@ -260,6 +335,10 @@ main(int argc, char *argv[])
                        /* upper bound is further limited later */
                        number = (int)ul;
                        continue;
+               case 'r':
+                       minlength = 5;
+                       maxlength = 0;
+                       continue;



Home | Main Index | Thread Index | Old Index