]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - trek/setup.c
A revision of "The purpose of computing is insight, not numbers" by
[bsdgames-darwin.git] / trek / setup.c
index ce75202685c7a0c290e8802692e74fa9931d9c5f..769cab262a4a53a1020bbaec7004eed625ba8942 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: setup.c,v 1.4 1995/04/24 12:26:06 cgd Exp $    */
+/*     $NetBSD: setup.c,v 1.13 2009/05/25 00:37:27 dholland Exp $      */
 
 /*
  * Copyright (c) 1980, 1993
  * 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.
  *
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)setup.c    8.1 (Berkeley) 5/31/93";
 #else
-static char rcsid[] = "$NetBSD: setup.c,v 1.4 1995/04/24 12:26:06 cgd Exp $";
+__RCSID("$NetBSD: setup.c,v 1.13 2009/05/25 00:37:27 dholland Exp $");
 #endif
 #endif /* not lint */
 
-# include      "trek.h"
-# include      "getpar.h"
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <err.h>
+#include <limits.h>
+#include "trek.h"
+#include "getpar.h"
 
 /*
 **  INITIALIZE THE GAME
@@ -55,44 +59,40 @@ static char rcsid[] = "$NetBSD: setup.c,v 1.4 1995/04/24 12:26:06 cgd Exp $";
 **     Game restart and tournament games are handled here.
 */
 
-struct cvntab  Lentab[] =
-{
-       "s",            "hort",                 (int (*)())1,           0,
-       "m",            "edium",                (int (*)())2,           0,
-       "l",            "ong",                  (int (*)())4,           0,
-       "restart",      "",                     0,              0,
-       0
+const struct cvntab    Lentab[] = {
+       { "s",          "hort",         (cmdfun)1,      0 },
+       { "m",          "edium",        (cmdfun)2,      0 },
+       { "l",          "ong",          (cmdfun)4,      0 },
+       { "restart",    "",             (cmdfun)0,      0 },
+       { NULL,         NULL,           NULL,           0 }
 };
 
-struct cvntab  Skitab[] =
-{
-       "n",            "ovice",                (int (*)())1,           0,
-       "f",            "air",                  (int (*)())2,           0,
-       "g",            "ood",                  (int (*)())3,           0,
-       "e",            "xpert",                (int (*)())4,           0,
-       "c",            "ommodore",             (int (*)())5,           0,
-       "i",            "mpossible",            (int (*)())6,           0,
-       0
+const struct cvntab    Skitab[] = {
+       { "n",          "ovice",        (cmdfun)1,      0 },
+       { "f",          "air",          (cmdfun)2,      0 },
+       { "g",          "ood",          (cmdfun)3,      0 },
+       { "e",          "xpert",        (cmdfun)4,      0 },
+       { "c",          "ommodore",     (cmdfun)5,      0 },
+       { "i",          "mpossible",    (cmdfun)6,      0 },
+       { NULL,         NULL,           NULL,           0 }
 };
 
-setup()
+void
+setup(void)
 {
-       struct cvntab           *r;
-       register int            i, j;
+       const struct cvntab             *r;
+       int             i, j;
        double                  f;
        int                     d;
-       int                     fd;
        int                     klump;
        int                     ix, iy;
-       register struct quad    *q;
+       struct quad     *q;
        struct event            *e;
 
-       while (1)
-       {
+       while (1) {
                r = getcodpar("What length game", Lentab);
                Game.length = (long) r->value;
-               if (Game.length == 0)
-               {
+               if (Game.length == 0) {
                        if (restartgame())
                                continue;
                        return;
@@ -103,14 +103,13 @@ setup()
        Game.skill = (long) r->value;
        Game.tourn = 0;
        getstrpar("Enter a password", Game.passwd, 14, 0);
-       if (sequal(Game.passwd, "tournament"))
-       {
+       if (strcmp(Game.passwd, "tournament") == 0) {
                getstrpar("Enter tournament code", Game.passwd, 14, 0);
                Game.tourn = 1;
                d = 0;
                for (i = 0; Game.passwd[i]; i++)
                        d += Game.passwd[i] << i;
-               srand(d);
+               srandom(d);
        }
        Param.bases = Now.bases = ranf(6 - Game.skill) + 2;
        if (Game.skill == 6)
@@ -165,7 +164,7 @@ setup()
        for (i = j = 0; i < NDEV; i++)
                j += Param.damprob[i];
        if (j != 1000)
-               syserr("Device probabilities sum to %d", j);
+               errx(1, "Device probabilities sum to %d", j);
        Param.dockfac = 0.5;
        Param.regenfac = (5 - Game.skill) * 0.05;
        if (Param.regenfac < 0.0)
@@ -205,10 +204,9 @@ setup()
        Param.navigcrud[1] = 0.75;
        Param.cloakenergy = 1000;
        Param.energylow = 1000;
-       for (i = 0; i < MAXEVENTS; i++)
-       {
+       for (i = 0; i < MAXEVENTS; i++) {
                e = &Event[i];
-               e->date = 1e50;
+               e->date = TOOLARGE;
                e->evcode = 0;
        }
        xsched(E_SNOVA, 1, 0, 0, 0);
@@ -228,22 +226,23 @@ setup()
        Move.endgame = 0;
 
        /* setup stars */
-       for (i = 0; i < NQUADS; i++)
-               for (j = 0; j < NQUADS; j++)
-               {
+       for (i = 0; i < NQUADS; i++) {
+               for (j = 0; j < NQUADS; j++) {
+                       short s5;
                        q = &Quad[i][j];
                        q->klings = q->bases = 0;
                        q->scanned = -1;
                        q->stars = ranf(9) + 1;
-                       q->holes = ranf(3) - q->stars / 5;
+                       q->holes = ranf(3);
+                       s5 = q->stars / 5;
+                       q->holes = q->holes > s5 ? q->holes - s5 : 0;
                        q->qsystemname = 0;
                }
+       }
 
        /* select inhabited starsystems */
-       for (d = 1; d < NINHAB; d++)
-       {
-               do
-               {
+       for (d = 1; d < NINHAB; d++) {
+               do {
                        i = ranf(NQUADS);
                        j = ranf(NQUADS);
                        q = &Quad[i][j];
@@ -252,10 +251,8 @@ setup()
        }
 
        /* position starbases */
-       for (i = 0; i < Param.bases; i++)
-       {
-               while (1)
-               {
+       for (i = 0; i < Param.bases; i++) {
+               while (1) {
                        ix = ranf(NQUADS);
                        iy = ranf(NQUADS);
                        q = &Quad[ix][iy];
@@ -268,21 +265,18 @@ setup()
                Now.base[i].y = iy;
                q->scanned = 1001;
                /* start the Enterprise near starbase */
-               if (i == 0)
-               {
+               if (i == 0) {
                        Ship.quadx = ix;
                        Ship.quady = iy;
                }
        }
 
        /* position klingons */
-       for (i = Param.klings; i > 0; )
-       {
+       for (i = Param.klings; i > 0; ) {
                klump = ranf(4) + 1;
                if (klump > i)
                        klump = i;
-               while (1)
-               {
+               while (1) {
                        ix = ranf(NQUADS);
                        iy = ranf(NQUADS);
                        q = &Quad[ix][iy];