Magic number changes, get rid of nulplanet structure.
authorEric S. Raymond <esr@thyrsus.com>
Mon, 1 Nov 2004 01:23:39 +0000 (01:23 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 1 Nov 2004 01:23:39 +0000 (01:23 +0000)
...in favor of DESTROY macro, change planet indices to be zero-based.

ai.c
battle.c
events.c
moving.c
planets.c
reports.c
setup.c
sst.c
sst.h

diff --git a/ai.c b/ai.c
index 9dcb1a01261941591e7a99c64721a2b7d7314e7a..d2223cab385e9ab9892bffb269b8da889aeceb53 100644 (file)
--- a/ai.c
+++ b/ai.c
@@ -344,11 +344,11 @@ static int checkdest(int iqx, int iqy, int flag, int *ipage) {
                sortkl();\r
        }\r
        /* check for a helpful planet */\r
-       for (i = 1; i <= inplan; i++) {\r
+       for (i = 0; i < inplan; i++) {\r
                if (game.state.plnets[i].x==game.state.isx && game.state.plnets[i].y==game.state.isy &&\r
                        game.state.plnets[i].crystals == 1) {\r
                        /* destroy the planet */\r
-                       game.state.plnets[i] = nulplanet;\r
+                       DESTROY(&game.state.plnets[i]);\r
                        game.state.newstuf[game.state.isx][game.state.isy] -= 1;\r
                        if (game.damage[DRADIO] == 0.0 || condit == IHDOCKED) {\r
                                if (*ipage==0) pause(1);\r
index 1f043c61d9700a1dcbccf7ce35ac1dde23e6deec..590d08cdb4c8498ac4f67d7c314cd203fa7817cb 100644 (file)
--- a/battle.c
+++ b/battle.c
@@ -156,7 +156,7 @@ void ram(int ibumpd, int ienm, int ix, int iy) {
        crami(icas, 1);\r
        prout(" casualties.");\r
        casual += icas;\r
-       for (l=1; l <= ndevice; l++) {\r
+       for (l=1; l <= NDEVICES; l++) {\r
                if (l == DDRAY) continue; // Don't damage deathray \r
                if (game.damage[l] < 0) continue;\r
                extradm = (10.0*type*Rand()+1.0)*damfac;\r
@@ -311,7 +311,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit) {
                                prout(" destroyed.");\r
                                game.state.nplankl++;\r
                                game.state.newstuf[quadx][quady] -= 1;\r
-                               game.state.plnets[iplnet] = nulplanet;\r
+                               DESTROY(&game.state.plnets[iplnet]);\r
                                iplnet = 0;\r
                                plnetx = plnety = 0;\r
                                game.quad[ix][iy] = IHDOT;\r
@@ -412,7 +412,7 @@ static void fry(double hit) {
        /* Select devices and cause damage */\r
        for (l = 1; l <= ncrit; l++) {\r
                do {\r
-                       j = ndevice*Rand()+1.0;\r
+                       j = NDEVICES*Rand()+1.0;\r
                        /* Cheat to prevent shuttle damage unless on ship */\r
                } while (game.damage[j] < 0.0 || (j == DSHUTTL && iscraft != 1) ||\r
                                 j == DDRAY);\r
@@ -1017,7 +1017,7 @@ void phasers(void) {
                                for (i = 1; i <= nenhere; i++) {\r
                                        hits[i] = 0.0;\r
                                        if (powrem <= 0) continue;\r
-                                       hits[i] = fabs(game.kpower[i])/(phasefac*pow(0.90,game.kdist[i]));\r
+                                       hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));\r
                                        over = (0.01 + 0.05*Rand())*hits[i];\r
                                        temp = powrem;\r
                                        powrem -= hits[i] + over;\r
@@ -1080,7 +1080,7 @@ void phasers(void) {
                                if (key == IHEOL) {\r
                                        chew();\r
                                        if (ipoop && k > kz) {\r
-                                               int irec=(fabs(game.kpower[k])/(phasefac*pow(0.9,game.kdist[k])))*\r
+                                               int irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*\r
                                                                 (1.01+0.05*Rand()) + 1.0;\r
                                                kz = k;\r
                                                proutn("(");\r
@@ -1179,7 +1179,7 @@ void hittem(double *hits) {
                hit = wham*pow(dustfac,game.kdist[kk]);\r
                kpini = game.kpower[kk];\r
                kp = fabs(kpini);\r
-               if (phasefac*hit < kp) kp = phasefac*hit;\r
+               if (PHASEFAC*hit < kp) kp = PHASEFAC*hit;\r
                game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);\r
                kpow = game.kpower[kk];\r
                ii = game.kx[kk];\r
index 1fad7134a47b2b0ae5acbdf5e1059003826e05b3..0cbc88071c8060f71d49a272290b4855d76d2c0e 100644 (file)
--- a/events.c
+++ b/events.c
@@ -51,7 +51,7 @@ void events(void) {
                repair = xtime;\r
                if (condit == IHDOCKED) repair /= docfac;\r
                /* Don't fix Deathray here */\r
-               for (l=1; l<=ndevice; l++)\r
+               for (l=1; l<=NDEVICES; l++)\r
                        if (game.damage[l] > 0.0 && l != DDRAY)\r
                                game.damage[l] -= (game.damage[l]-repair > 0.0 ? repair : game.damage[l]);\r
                /* If radio repaired, update star chart and attack reports */\r
@@ -473,7 +473,7 @@ void nova(int ix, int iy) {
                                                game.state.nplankl++;\r
                                                crmena(1, IHP, 2, ii, jj);\r
                                                prout(" destroyed.");\r
-                                               game.state.plnets[iplnet] = nulplanet;\r
+                                               DESTROY(&game.state.plnets[iplnet]);\r
                                                iplnet = plnetx = plnety = 0;\r
                                                if (landed == 1) {\r
                                                        finish(FPNOVA);\r
@@ -715,9 +715,9 @@ void snova(int insx, int insy) {
        npdead = num - nrmdead*10;\r
        if (npdead) {\r
                int l;\r
-               for (l = 1; l <= inplan; l++)\r
+               for (l = 0; l < inplan; l++)\r
                        if (game.state.plnets[l].x == nqx && game.state.plnets[l].y == nqy) {\r
-                               game.state.plnets[l] = nulplanet;\r
+                           DESTROY(&game.state.plnets[l]);\r
                        }\r
        }\r
        /* Destroy any base in supernovaed quadrant */\r
index 26011bfb0960dfd89d51fd51acc9bab6e5b082fe..0b9ecbed686f32c5b5997607e835ec29fc4fe9dc 100644 (file)
--- a/moving.c
+++ b/moving.c
@@ -764,7 +764,7 @@ void timwrp() {
                /* Make sure Galileo is consistant -- Snapshot may have been taken\r
                   when on planet, which would give us two Galileos! */\r
                gotit = 0;\r
-               for (l = 1; l <= inplan; l++) {\r
+               for (l = 0; l < inplan; l++) {\r
                        if (game.state.plnets[l].known == shuttle_down) {\r
                                gotit = 1;\r
                                if (iscraft==1 && ship==IHE) {\r
index a98235ffa1f8934f72090c0aa1cdf0c0f993302f..3f9bfc3e4381ef0b447d5e0b9c21ce9881102518 100644 (file)
--- a/planets.c
+++ b/planets.c
@@ -1,8 +1,9 @@
 #include "sst.h"\r
 \r
-static char classes[4][2]={"","M","N","O"};\r
 static int height;\r
 \r
+static char *classes[] = {"M","N","O"};\r
+\r
 static int consumeTime(void) {\r
 /* I think most of this avoidance was caused by overlay scheme.\r
    Let's see what happens if all events can occur here */\r
@@ -31,7 +32,7 @@ void preport(void) {
        chew();\r
        prout("Spock-  \"Planet report follows, Captain.\"");\r
        skip(1);\r
-       for (i = 1; i <= inplan; i++) {\r
+       for (i = 0; i < inplan; i++) {\r
                if (game.state.plnets[i].known != unknown\r
 #ifdef DEBUG\r
                        || ( idebug && game.state.plnets[i].x !=0)\r
index 9475ab3b4ac1eeb469ed0e7945860f13db5ced71..436900e5e6044716eaaa66faf14e9ef0b6de55c9 100644 (file)
--- a/reports.c
+++ b/reports.c
@@ -147,7 +147,7 @@ void dreprt(void) {
        int jdam = FALSE, i;\r
        chew();\r
 \r
-       for (i = 1; i <= ndevice; i++) {\r
+       for (i = 1; i <= NDEVICES; i++) {\r
                if (game.damage[i] > 0.0) {\r
                        if (!jdam) {\r
                                skip(1);\r
diff --git a/setup.c b/setup.c
index 6b24e06c2f1697076e92357a8315c917bc0e39a8..24c5f6f6d25e58a82da1c78e1650ed046cd1f875 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -161,7 +161,7 @@ void abandn(void) {
        iscraft=0; /* Gallileo disappears */\r
        /* Resupply ship */\r
        condit=IHDOCKED;\r
-       for (l = 1; l <= ndevice; l++) game.damage[l] = 0.0;\r
+       for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;\r
        game.damage[DSHUTTL] = -1;\r
        energy = inenrg = 3000.0;\r
        shield = inshld = 1250.0;\r
@@ -195,7 +195,7 @@ void setup(void) {
        nprobes = (int)(3.0*Rand() + 2.0);      /* Give them 2-4 of these wonders */\r
        warpfac = 5.0;\r
        wfacsq = warpfac * warpfac;\r
-       for (i=0; i <= ndevice; i++) game.damage[i] = 0.0;\r
+       for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;\r
        // Set up assorted game parameters\r
        batx = baty = 0;\r
        game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);\r
@@ -298,13 +298,13 @@ void setup(void) {
                game.state.cy[i] = iy;\r
        }\r
        // Locate planets in galaxy\r
-       for (i = 1; i <= inplan; i++) {\r
+       for (i = 0; i < inplan; i++) {\r
                do iran8(&ix, &iy);\r
                while (game.state.newstuf[ix][iy] > 0);\r
                game.state.newstuf[ix][iy] = 1;\r
                game.state.plnets[i].x = ix;\r
                game.state.plnets[i].y = iy;\r
-               game.state.plnets[i].pclass = Rand()*3.0 + 1.0; // Planet class M N or O\r
+               game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O\r
                game.state.plnets[i].crystals = 1.5*Rand();             // 1 in 3 chance of crystals\r
                game.state.plnets[i].known = unknown;\r
        }\r
@@ -593,9 +593,9 @@ void newqad(int shutup) {
        \r
        if (nplan) {\r
                // If quadrant needs a planet, put it in\r
-               for (i=1; i <= inplan; i++)\r
+               for (i=0; i < inplan; i++)\r
                        if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;\r
-               if (i <= inplan) {\r
+               if (i < inplan) {\r
                        iplnet = i;\r
                        dropin(IHP, &plnetx, &plnety);\r
                }\r
diff --git a/sst.c b/sst.c
index 31202254ae78c2a785f392a4c7d46970522c777b..f6db8870c9ae1856cf8eb55d345ac0981475a7c6 100644 (file)
--- a/sst.c
+++ b/sst.c
@@ -637,7 +637,7 @@ void debugme(void) {
        proutn("Reset damage? ");\r
        if (ja() != 0) {\r
                int i;\r
-               for (i=0; i <= ndevice; i++) if (damage[i] > 0.0) damage[i] = 0.0;\r
+               for (i=0; i <= NDEVICES; i++) if (damage[i] > 0.0) damage[i] = 0.0;\r
                stdamtim = 1e30;\r
        }\r
        proutn("Toggle idebug? ");\r
@@ -649,7 +649,7 @@ void debugme(void) {
        proutn("Cause selective damage? ");\r
        if (ja() != 0) {\r
                int i, key;\r
-               for (i=1; i <= ndevice; i++) {\r
+               for (i=1; i <= NDEVICES; i++) {\r
                        proutn("Kill ");\r
                        proutn(device[i]);\r
                        proutn("? ");\r
diff --git a/sst.h b/sst.h
index 5617ae3ce2b4964e4c55a6a1bb1a3963576ed4c8..a2c4c92cbcac44e638945b80bc0c76cdc04c6314 100644 (file)
--- a/sst.h
+++ b/sst.h
 \r
 // #define DEBUG\r
 \r
-#define ndevice (15)   // Number of devices\r
-#define phasefac (2.0)\r
+#define NDEVICES (15)  // Number of devices\r
+#define PHASEFAC (2.0)\r
 #define PLNETMAX (10)\r
 #define NEVENTS (8)\r
 \r
 typedef struct {\r
     int x;     /* Quadrant location of planet */\r
     int y;\r
-    int pclass; /* class M, N, or O (1, 2, or 3) */\r
+    enum {M=0, N=1, O=2} pclass;\r
     int crystals; /* has crystals */\r
     enum {unknown, known, shuttle_down} known;\r
 } planet;\r
 \r
+#define DESTROY(pl)    memset(pl, '\0', sizeof(planet))\r
+\r
 typedef struct {\r
     int snap,          // snapshot taken\r
        remkl,                  // remaining klingons\r
@@ -43,7 +45,7 @@ typedef struct {
        nromrem,                // Romulans remaining\r
        nsckill,                // super commanders killed\r
        nplankl;                // destroyed planets\r
-       planet plnets[PLNETMAX+1];  // Planet information\r
+       planet plnets[PLNETMAX];  // Planet information\r
        double date,            // stardate\r
            remres,             // remaining resources\r
            remtime;            // remaining time\r
@@ -54,14 +56,17 @@ typedef struct {
 // original names. Gee, I could have done this with the d structure,\r
 // but I just didn't think of it back when I started.\r
 \r
+#define SSTMAGIC       "SST2.0\n"\r
+\r
 EXTERN struct {\r
+    char magic[sizeof(SSTMAGIC)];\r
     snapshot state;\r
     snapshot snapsht;\r
     char quad[11][11];         // contents of our quadrant\r
     double kpower[21];         // enemy energy levels\r
     double kdist[21];          // enemy distances\r
     double kavgd[21];          // average distances\r
-    double damage[ndevice+1];  // damage encountered\r
+    double damage[NDEVICES+1]; // damage encountered\r
     double future[NEVENTS+1];  // future events\r
     char passwd[10];           // Self Destruct password\r
     int kx[21];                        // enemy sector locations\r
@@ -247,8 +252,7 @@ EXTERN struct {
 \r
 /* the following global state doesn't need to be saved */\r
 EXTERN int fromcommandline; // Game start from command line options\r
-EXTERN char    *device[ndevice+1];\r
-EXTERN planet nulplanet;       // zeroed planet structure\r
+EXTERN char    *device[NDEVICES+1];\r
 EXTERN int iscore, iskill; // Common PLAQ\r
 EXTERN double perdate;\r
 EXTERN double aaitem;\r
@@ -290,8 +294,7 @@ typedef enum {FWON, FDEPLETE, FLIFESUP, FNRG, FBATTLE,
 #define FDSPROB 8   // Move deep space probe\r
 \r
 #ifdef INCLUDED\r
-planet nulplanet = {0};\r
-char *device[ndevice+1] = {\r
+char *device[NDEVICES+1] = {\r
        "",\r
        "S. R. Sensors",\r
        "L. R. Sensors",\r