More boolean cleanups. More consistent use of coord type
authorEric S. Raymond <esr@thyrsus.com>
Wed, 20 Sep 2006 03:26:16 +0000 (03:26 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 20 Sep 2006 03:26:16 +0000 (03:26 +0000)
Anong other things, this allows us to write a distance() macro and get
rid of a lot of gnarly expressions.

src/ai.c
src/battle.c
src/events.c
src/moving.c
src/reports.c
src/setup.c
src/sst.c
src/sst.h

index 6fc79080ed1112d1579c8327009a3688ba453a28..e59657144774c73ba2ef8fdb48ee6c001d5e02b5 100644 (file)
--- a/src/ai.c
+++ b/src/ai.c
@@ -241,8 +241,7 @@ static void movebaddy(coord com, int loccom, int ienm)
        /* it moved */
        game.ks[loccom].x = next.x;
        game.ks[loccom].y = next.y;
        /* it moved */
        game.ks[loccom].x = next.x;
        game.ks[loccom].y = next.y;
-       game.kdist[loccom] = game.kavgd[loccom] =
-           sqrt(square(game.sector.x-next.x)+square(game.sector.y-next.y));
+       game.kdist[loccom] = game.kavgd[loccom] = distance(game.sector, next);
        if (!damaged(DSRSENS) || game.condit == IHDOCKED) {
            proutn("***");
            cramen(ienm);
        if (!damaged(DSRSENS) || game.condit == IHDOCKED) {
            proutn("***");
            cramen(ienm);
@@ -387,9 +386,7 @@ void scom(bool *ipage)
        sc = game.state.kscmdr;
        for_starbases(i) {
            basetbl[i] = i;
        sc = game.state.kscmdr;
        for_starbases(i) {
            basetbl[i] = i;
-           ibq.x = game.state.baseq[i].x;
-           ibq.y = game.state.baseq[i].y;
-           bdist[i] = sqrt(square(ibq.x-sc.x) + square(ibq.y-sc.y));
+           bdist[i] = distance(game.state.baseq[i], sc);
        }
        if (game.state.rembase > 1) {
            /* sort into nearest first order */
        }
        if (game.state.rembase > 1) {
            /* sort into nearest first order */
@@ -532,7 +529,6 @@ void movetho(void)
 /* move the Tholian */
 {
     int idx, idy, im, i;
 /* move the Tholian */
 {
     int idx, idy, im, i;
-    coord dummy;
     /* Move the Tholian */
     if (!game.ithere || game.justin) return;
 
     /* Move the Tholian */
     if (!game.ithere || game.justin) return;
 
@@ -586,7 +582,7 @@ void movetho(void)
     }
     /* All plugged up -- Tholian splits */
     game.quad[game.tholian.x][game.tholian.y]=IHWEB;
     }
     /* All plugged up -- Tholian splits */
     game.quad[game.tholian.x][game.tholian.y]=IHWEB;
-    dropin(IHBLANK, &dummy);
+    dropin(IHBLANK);
     crmena(true, IHT, sector, game.tholian);
     prout(_(" completes web."));
     game.ithere = false;
     crmena(true, IHT, sector, game.tholian);
     prout(_(" completes web."));
     game.ithere = false;
index dba9f295c13a762a20a5c78056af1eb4de7b3b07..a1bacfab1a672ad652e3230247f4b6109c463104 100644 (file)
@@ -409,10 +409,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int i, int
            game.ithere = false;
            game.tholian.x = game.tholian.y = 0;
            game.nenhere--;
            game.ithere = false;
            game.tholian.x = game.tholian.y = 0;
            game.nenhere--;
-           {
-               coord dummy;
-               dropin(IHBLANK, &dummy);
-           }
+           dropin(IHBLANK);
            return;
                                        
        default: /* Problem! */
            return;
                                        
        default: /* Problem! */
@@ -430,9 +427,10 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int i, int
     if (shoved) {
        game.quad[w.x][w.y]=IHDOT;
        game.quad[jx][jy]=iquad;
     if (shoved) {
        game.quad[w.x][w.y]=IHDOT;
        game.quad[jx][jy]=iquad;
+       w.x = jx; w.y = jy;
        prout(_(" displaced by blast to %s "), cramlc(sector, w));
        for_local_enemies(ll)
        prout(_(" displaced by blast to %s "), cramlc(sector, w));
        for_local_enemies(ll)
-           game.kdist[ll] = game.kavgd[ll] = sqrt(square(game.sector.x-game.ks[ll].x)+square(game.sector.y-game.ks[ll].y));
+           game.kdist[ll] = game.kavgd[ll] = distance(game.sector,game.ks[ll]);
        sortkl();
        return;
     }
        sortkl();
        return;
     }
index 95a3c44517c1e37650a296e2321a4dec87c1f765..1e04dd0e2c8f3e09bf6465194be75b352ff2dc9f 100644 (file)
@@ -93,6 +93,7 @@ void events(void)
 
     radio_was_broken = damaged(DRADIO);
 
 
     radio_was_broken = damaged(DRADIO);
 
+    hold.x = hold.y = 0;
     for (;;) {
        /* Select earliest extraneous event, evcode==0 if no events */
        evcode = FSPY;
     for (;;) {
        /* Select earliest extraneous event, evcode==0 if no events */
        evcode = FSPY;
@@ -223,7 +224,7 @@ void events(void)
                game.quadrant = game.state.kscmdr;
            else
                game.quadrant = game.state.kcmdr[i];
                game.quadrant = game.state.kscmdr;
            else
                game.quadrant = game.state.kcmdr[i];
-           iran(QUADSIZE, &game.sector.x, &game.sector.y);
+           game.sector = iran(QUADSIZE);
            crmshp();
            proutn(_(" is pulled to "));
            proutn(cramlc(quadrant, game.quadrant));
            crmshp();
            proutn(_(" is pulled to "));
            proutn(cramlc(quadrant, game.quadrant));
@@ -431,7 +432,7 @@ void events(void)
                   which has some stars which are inhabited and
                   not already under attack, which is not
                   supernova'ed, and which has some Klingons in it */
                   which has some stars which are inhabited and
                   not already under attack, which is not
                   supernova'ed, and which has some Klingons in it */
-               iran(GALSIZE, &w.x, &w.y);
+               w = iran(GALSIZE);
                q = &game.state.galaxy[w.x][w.y];
            } while (--i &&
                     (same(game.quadrant, w) || q->planet == NOPLANET ||
                q = &game.state.galaxy[w.x][w.y];
            } while (--i &&
                     (same(game.quadrant, w) || q->planet == NOPLANET ||
@@ -521,7 +522,7 @@ void events(void)
            game.state.remkl++;
            q->klingons++;
            if (same(game.quadrant, w))
            game.state.remkl++;
            q->klingons++;
            if (same(game.quadrant, w))
-               newkling(++game.klhere, &hold);
+               newkling(++game.klhere);
 
            /* recompute time left */
            game.state.remtime = game.state.remres/(game.state.remkl+4*game.state.remcom);
 
            /* recompute time left */
            game.state.remtime = game.state.remres/(game.state.remkl+4*game.state.remcom);
@@ -752,8 +753,7 @@ void nova(coord nov)
                        game.quad[scratch.x][scratch.y] = IHDOT;
                        game.quad[newc.x][newc.y] = iquad;
                        game.ks[ll] = newc;
                        game.quad[scratch.x][scratch.y] = IHDOT;
                        game.quad[newc.x][newc.y] = iquad;
                        game.ks[ll] = newc;
-                       game.kavgd[ll] = sqrt(square(game.sector.x-newc.x)+square(game.sector.y-newc.y));
-                       game.kdist[ll] = game.kavgd[ll];
+                       game.kdist[ll] = game.kavgd[ll] = distance(game.sector, newc);
                        skip(1);
                        break;
                    }
                        skip(1);
                        break;
                    }
@@ -849,7 +849,7 @@ void snova(bool induced, coord *w)
            prouts("***************");
            skip(1);
            stars();
            prouts("***************");
            skip(1);
            stars();
-           game.alldone=1;
+           game.alldone = true;
        }
     }
 
        }
     }
 
index 64ad0a70efd829d38c93a1c0b42deb5403e4f130..99c18916da63bfaea1a42a31084b3d08ea53e4b0 100644 (file)
@@ -945,12 +945,12 @@ void mayday(void)
     game.nhelp++;
     if (game.base.x!=0) {
        /* There's one in this quadrant */
     game.nhelp++;
     if (game.base.x!=0) {
        /* There's one in this quadrant */
-       ddist = sqrt(square(game.base.x-game.sector.x)+square(game.base.y-game.sector.y));
+       ddist = distance(game.base, game.sector);
     }
     else {
        ddist = FOREVER;
        for_starbases(m) {
     }
     else {
        ddist = FOREVER;
        for_starbases(m) {
-           xdist=10.0*sqrt(square(game.state.baseq[m].x-game.quadrant.x)+square(game.state.baseq[m].y-game.quadrant.y));
+           xdist = QUADSIZE * distance(game.state.baseq[m], game.quadrant);
            if (xdist < ddist) {
                ddist = xdist;
                line = m;
            if (xdist < ddist) {
                ddist = xdist;
                line = m;
index 7ea7c171f3374ba5dad7d6b7f8b8cd48eeeac2ea..57297f567e0e3c7f812da96cd6de2076320ae956 100644 (file)
@@ -416,7 +416,7 @@ int srscan(int l)
 void eta(void)
 /* use computer to get estimated time of arrival for a warp jump */
 {
 void eta(void)
 /* use computer to get estimated time of arrival for a warp jump */
 {
-    int ix1, ix2, iy1, iy2;
+    coord w1, w2;
     bool wfl, prompt = false;
     double ttime, twarp, tpower;
     if (damaged(DCOMPTR)) {
     bool wfl, prompt = false;
     double ttime, twarp, tpower;
     if (damaged(DCOMPTR)) {
@@ -433,33 +433,33 @@ void eta(void)
            return;
        }
     }
            return;
        }
     }
-    iy1 = aaitem +0.5;
+    w1.y = aaitem +0.5;
     if (scan() != IHREAL) {
        huh();
        return;
     }
     if (scan() != IHREAL) {
        huh();
        return;
     }
-    ix1 = aaitem + 0.5;
+    w1.x = aaitem + 0.5;
     if (scan() == IHREAL) {
     if (scan() == IHREAL) {
-       iy2 = aaitem + 0.5;
+       w2.y = aaitem + 0.5;
        if (scan() != IHREAL) {
            huh();
            return;
        }
        if (scan() != IHREAL) {
            huh();
            return;
        }
-       ix2 = aaitem + 0.5;
+       w2.x = aaitem + 0.5;
     }
     else {
     }
     else {
-       if (game.quadrant.y>ix1) ix2 = 1;
-       else ix2=QUADSIZE;
-       if (game.quadrant.x>iy1) iy2 = 1;
-       else iy2=QUADSIZE;
+       if (game.quadrant.y>w1.x) w2.x = 1;
+       else w2.x=QUADSIZE;
+       if (game.quadrant.x>w1.y) w2.y = 1;
+       else w2.y=QUADSIZE;
     }
 
     }
 
-    if (!VALID_QUADRANT(ix1, iy1) || !VALID_SECTOR(ix2, iy2)) {
+    if (!VALID_QUADRANT(w1.x, w1.y) || !VALID_SECTOR(w2.x, w2.y)) {
        huh();
        return;
     }
        huh();
        return;
     }
-    game.dist = sqrt(square(iy1-game.quadrant.x+0.1*(iy2-game.sector.x))+
-               square(ix1-game.quadrant.y+0.1*(ix2-game.sector.y)));
+    game.dist = sqrt(square(w1.y-game.quadrant.y+0.1*(w2.y-game.sector.y))+
+               square(w1.x-game.quadrant.x+0.1*(w2.x-game.sector.x)));
     wfl = false;
 
     if (prompt) prout(_("Answer \"no\" if you don't know the value:"));
     wfl = false;
 
     if (prompt) prout(_("Answer \"no\" if you don't know the value:"));
@@ -537,9 +537,9 @@ void eta(void)
            prout(_("Unfortunately, the Federation will be destroyed by then."));
        if (twarp > 6.0)
            prout(_("You'll be taking risks at that speed, Captain"));
            prout(_("Unfortunately, the Federation will be destroyed by then."));
        if (twarp > 6.0)
            prout(_("You'll be taking risks at that speed, Captain"));
-       if ((game.isatb==1 && game.state.kscmdr.y == iy1 && game.state.kscmdr.x == ix1 &&
+       if ((game.isatb==1 && same(game.state.kscmdr, w1) &&
             scheduled(FSCDBAS)< ttime+game.state.date)||
             scheduled(FSCDBAS)< ttime+game.state.date)||
-           (scheduled(FCDBAS)<ttime+game.state.date && game.battle.y==iy1 && game.battle.x == ix1))
+           (scheduled(FCDBAS)<ttime+game.state.date && same(game.battle, w1)))
            prout(_("The starbase there will be destroyed by then."));
        proutn(_("New warp factor to try? "));
        if (scan() == IHREAL) {
            prout(_("The starbase there will be destroyed by then."));
        proutn(_("New warp factor to try? "));
        if (scan() == IHREAL) {
index d464616f04714d21812b37364fa8c5e07f982d67..80ab2e4bca4db1d3ec9e0b3248ec16a15c4f2da1 100644 (file)
@@ -8,7 +8,8 @@ struct stat buf;
     return buf.st_size;
 }
 
     return buf.st_size;
 }
 
-void prelim(void) 
+void prelim(void)
+/* issue a historically correct banner */
 {
     skip(2);
     prout("-SUPER- STAR TREK");
 {
     skip(2);
     prout("-SUPER- STAR TREK");
@@ -19,7 +20,8 @@ void prelim(void)
 #endif /* __HISTORICAL__ */
 }
 
 #endif /* __HISTORICAL__ */
 }
 
-void freeze(bool boss) 
+void freeze(bool boss)
+/* save game */
 {
     FILE *fp;
     int key;
 {
     FILE *fp;
     int key;
@@ -50,12 +52,11 @@ void freeze(bool boss)
     fwrite(&game, sizeof(game), 1, fp);
 
     fclose(fp);
     fwrite(&game, sizeof(game), 1, fp);
 
     fclose(fp);
-
-    /* I hope that's enough! */
 }
 
 
 }
 
 
-int thaw(void) 
+bool thaw(void) 
+/* retrieve saved game */
 {
     FILE *fp;
     int key;
 {
     FILE *fp;
     int key;
@@ -67,7 +68,7 @@ int thaw(void)
     }
     if (key != IHALPHA) {
        huh();
     }
     if (key != IHALPHA) {
        huh();
-       return 1;
+       return true;
     }
     chew();
     if (strchr(citem, '.') == NULL) {
     }
     chew();
     if (strchr(citem, '.') == NULL) {
@@ -89,7 +90,7 @@ int thaw(void)
 
     fclose(fp);
 
 
     fclose(fp);
 
-    return 0;
+    return false;
 }
 
 /*
 }
 
 /*
@@ -114,6 +115,7 @@ int thaw(void)
 */
 
 void abandn(void) 
 */
 
 void abandn(void) 
+/* abandon ship */
 {
     int nb, l;
     struct quadrant *q;
 {
     int nb, l;
     struct quadrant *q;
@@ -226,9 +228,10 @@ void abandn(void)
 }
        
 void setup(bool needprompt) 
 }
        
 void setup(bool needprompt) 
+/* prepare to play, set up cosmos */
 {
     int i,j, krem, klumper;
 {
     int i,j, krem, klumper;
-    int ix, iy;
+    coord w;
     //  Decide how many of everything
     if (choose(needprompt)) return; // frozen game
     // Prepare the Enterprise
     //  Decide how many of everything
     if (choose(needprompt)) return; // frozen game
     // Prepare the Enterprise
@@ -241,8 +244,8 @@ void setup(bool needprompt)
     game.shldup = false;
     game.inlsr = 4.0;
     game.lsupres = 4.0;
     game.shldup = false;
     game.inlsr = 4.0;
     game.lsupres = 4.0;
-    iran(GALSIZE, &game.quadrant.x, &game.quadrant.y);
-    iran(QUADSIZE, &game.sector.x, &game.sector.y);
+    game.quadrant = iran(GALSIZE);
+    game.sector = iran(QUADSIZE);
     game.torps = game.intorps = 10;
     game.nprobes = (int)(3.0*Rand() + 2.0);    /* Give them 2-4 of these wonders */
     game.warpfac = 5.0;
     game.torps = game.intorps = 10;
     game.nprobes = (int)(3.0*Rand() + 2.0);    /* Give them 2-4 of these wonders */
     game.warpfac = 5.0;
@@ -303,16 +306,16 @@ void setup(bool needprompt)
     for (i = 1; i <= game.inbase; i++) {
        bool contflag;
        do {
     for (i = 1; i <= game.inbase; i++) {
        bool contflag;
        do {
-           do iran(GALSIZE, &ix, &iy);
-           while (game.state.galaxy[ix][iy].starbase);
+           do w = iran(GALSIZE);
+           while (game.state.galaxy[w.x][w.y].starbase);
            contflag = false;
            for (j = i-1; j > 0; j--) {
                /* Improved placement algorithm to spread out bases */
            contflag = false;
            for (j = i-1; j > 0; j--) {
                /* Improved placement algorithm to spread out bases */
-               double distq = square(ix-game.state.baseq[j].x) + square(iy-game.state.baseq[j].y);
+               double distq = square(w.x-game.state.baseq[j].x) + square(w.y-game.state.baseq[j].y);
                if (distq < 6.0*(BASEMAX+1-game.inbase) && Rand() < 0.75) {
                    contflag = true;
                    if (idebug)
                if (distq < 6.0*(BASEMAX+1-game.inbase) && Rand() < 0.75) {
                    contflag = true;
                    if (idebug)
-                       prout("=== Abandoning base #%d at %d-%d", i, ix, iy);
+                       prout("=== Abandoning base #%d at %d-%d", i, w.x, w.y);
                    break;
                }
                else if (distq < 6.0 * (BASEMAX+1-game.inbase)) {
                    break;
                }
                else if (distq < 6.0 * (BASEMAX+1-game.inbase)) {
@@ -322,10 +325,9 @@ void setup(bool needprompt)
            }
        } while (contflag);
                        
            }
        } while (contflag);
                        
-       game.state.baseq[i].x = ix;
-       game.state.baseq[i].y = iy;
-       game.state.galaxy[ix][iy].starbase = 1;
-       game.state.chart[ix][iy].starbase = 1;
+       game.state.baseq[i] = w;
+       game.state.galaxy[w.x][w.y].starbase = 1;
+       game.state.chart[w.x][w.y].starbase = 1;
     }
     // Position ordinary Klingon Battle Cruisers
     krem = game.inkling;
     }
     // Position ordinary Klingon Battle Cruisers
     krem = game.inkling;
@@ -337,10 +339,10 @@ void setup(bool needprompt)
        int klump = (1.0 - r*r)*klumper;
        if (klump > krem) klump = krem;
        krem -= klump;
        int klump = (1.0 - r*r)*klumper;
        if (klump > krem) klump = krem;
        krem -= klump;
-       do iran(GALSIZE,&ix,&iy);
-       while (game.state.galaxy[ix][iy].supernova ||
-               game.state.galaxy[ix][iy].klingons + klump > 9);
-       game.state.galaxy[ix][iy].klingons += klump;
+       do w = iran(GALSIZE);
+       while (game.state.galaxy[w.x][w.y].supernova ||
+               game.state.galaxy[w.x][w.y].klingons + klump > 9);
+       game.state.galaxy[w.x][w.y].klingons += klump;
     } while (krem > 0);
     // Position Klingon Commander Ships
 #ifdef ODEBUG
     } while (krem > 0);
     // Position Klingon Commander Ships
 #ifdef ODEBUG
@@ -351,30 +353,28 @@ void setup(bool needprompt)
            do { /* IF debugging, put commanders by bases, always! */
 #ifdef ODEBUG
                if (game.idebug && klumper <= game.inbase) {
            do { /* IF debugging, put commanders by bases, always! */
 #ifdef ODEBUG
                if (game.idebug && klumper <= game.inbase) {
-                   ix = game.state.baseq[klumper].x;
-                   iy = game.state.baseq[klumper].y;
+                   w = game.state.baseq[klumper];
                    klumper++;
                }
                else
 #endif /* ODEBUG */
                    klumper++;
                }
                else
 #endif /* ODEBUG */
-                   iran(GALSIZE, &ix, &iy);
+                   w = iran(GALSIZE);
            }
            }
-           while ((!game.state.galaxy[ix][iy].klingons && Rand() < 0.75)||
-                  game.state.galaxy[ix][iy].supernova||
-                  game.state.galaxy[ix][iy].klingons > 8);
+           while ((!game.state.galaxy[w.x][w.y].klingons && Rand() < 0.75)||
+                  game.state.galaxy[w.x][w.y].supernova||
+                  game.state.galaxy[w.x][w.y].klingons > 8);
            // check for duplicate
            for (j = 1; j < i; j++)
            // check for duplicate
            for (j = 1; j < i; j++)
-               if (game.state.kcmdr[j].x==ix && game.state.kcmdr[j].y==iy) break;
+               if (game.state.kcmdr[j].x==w.x && game.state.kcmdr[j].y==w.y) break;
        } while (j < i);
        } while (j < i);
-       game.state.galaxy[ix][iy].klingons++;
-       game.state.kcmdr[i].x = ix;
-       game.state.kcmdr[i].y = iy;
+       game.state.galaxy[w.x][w.y].klingons++;
+       game.state.kcmdr[i] = w;
     }
     // Locate planets in galaxy
     for (i = 0; i < game.inplan; i++) {
     }
     // Locate planets in galaxy
     for (i = 0; i < game.inplan; i++) {
-       do iran(GALSIZE, &ix, &iy); while (game.state.galaxy[ix][iy].planet != NOPLANET);
-       game.state.plnets[i].w.x = ix;
-       game.state.plnets[i].w.y = iy;
+       do w = iran(GALSIZE); 
+       while (game.state.galaxy[w.x][w.y].planet != NOPLANET);
+       game.state.plnets[i].w = w;
        if (i < NINHAB) {
            game.state.plnets[i].pclass = M;    // All inhabited planets are class M
            game.state.plnets[i].crystals = 0;
        if (i < NINHAB) {
            game.state.plnets[i].pclass = M;    // All inhabited planets are class M
            game.state.plnets[i].crystals = 0;
@@ -387,24 +387,23 @@ void setup(bool needprompt)
            game.state.plnets[i].inhabited = UNINHABITED;
        }
        if ((game.options & OPTION_WORLDS) || i >= NINHAB)
            game.state.plnets[i].inhabited = UNINHABITED;
        }
        if ((game.options & OPTION_WORLDS) || i >= NINHAB)
-           game.state.galaxy[ix][iy].planet = i;
+           game.state.galaxy[w.x][w.y].planet = i;
     }
     // Locate Romulans
     for (i = 1; i <= game.state.nromrem; i++) {
     }
     // Locate Romulans
     for (i = 1; i <= game.state.nromrem; i++) {
-       iran(GALSIZE, &ix, &iy);
-       game.state.galaxy[ix][iy].romulans = 1;
+       w = iran(GALSIZE);
+       game.state.galaxy[w.x][w.y].romulans = 1;
     }
     // Locate the Super Commander
     if (game.state.nscrem > 0) {
     }
     // Locate the Super Commander
     if (game.state.nscrem > 0) {
-       do iran(GALSIZE, &ix, &iy);
-       while (game.state.galaxy[ix][iy].supernova || game.state.galaxy[ix][iy].klingons > 8);
-       game.state.kscmdr.x = ix;
-       game.state.kscmdr.y = iy;
-       game.state.galaxy[ix][iy].klingons++;
+       do w = iran(GALSIZE);
+       while (game.state.galaxy[w.x][w.y].supernova || game.state.galaxy[w.x][w.y].klingons > 8);
+       game.state.kscmdr = w;
+       game.state.galaxy[w.x][w.y].klingons++;
     }
     // Place thing (in tournament game, thingx == -1, don't want one!)
     if (thing.x != -1) {
     }
     // Place thing (in tournament game, thingx == -1, don't want one!)
     if (thing.x != -1) {
-       iran(GALSIZE, &thing.x, &thing.y);
+       thing = iran(GALSIZE);
     }
     else {
        thing.x = thing.y = 0;
     }
     else {
        thing.x = thing.y = 0;
@@ -455,10 +454,11 @@ void setup(bool needprompt)
 }
 
 bool choose(bool needprompt) 
 }
 
 bool choose(bool needprompt) 
+/* choose your game type */
 {
     for(;;) {
        game.tourn = 0;
 {
     for(;;) {
        game.tourn = 0;
-       game.thawed = 0;
+       game.thawed = false;
        game.skill = SKILL_NONE;
        game.length = 0;
        if (needprompt) /* Can start with command line options */
        game.skill = SKILL_NONE;
        game.length = 0;
        if (needprompt) /* Can start with command line options */
@@ -566,14 +566,18 @@ bool choose(bool needprompt)
     return false;
 }
 
     return false;
 }
 
-void dropin(int iquad, coord *w) 
+coord dropin(int iquad)
+/* drop a feature on a random dot in the current quadrant */
 {
 {
-    do iran(QUADSIZE, &w->x, &w->y);
-    while (game.quad[w->x][w->y] != IHDOT);
-    game.quad[w->x][w->y] = iquad;
+    coord w;
+    do w = iran(QUADSIZE);
+    while (game.quad[w.x][w.y] != IHDOT);
+    game.quad[w.x][w.y] = iquad;
+    return w;
 }
 
 }
 
-void newcnd(void) 
+void newcnd(void)
+/* update our alert status */
 {
     game.condit = IHGREEN;
     if (game.energy < 1000.0) game.condit = IHYELLOW;
 {
     game.condit = IHGREEN;
     if (game.energy < 1000.0) game.condit = IHYELLOW;
@@ -582,16 +586,18 @@ void newcnd(void)
     if (!game.alive) game.condit=IHDEAD;
 }
 
     if (!game.alive) game.condit=IHDEAD;
 }
 
-void newkling(int i, coord *pi)
+coord newkling(int i)
 /* drop new Klingon into current quadrant */
 {
 /* drop new Klingon into current quadrant */
 {
-    dropin(IHK, pi);
-    game.ks[i] = *pi;
-    game.kdist[i] = game.kavgd[i] = sqrt(square(game.sector.x-pi->x) + square(game.sector.y-pi->y));
+    coord pi = dropin(IHK);
+    game.ks[i] = pi;
+    game.kdist[i] = game.kavgd[i] = distance(game.sector, pi);
     game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
     game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
+    return pi;
 }
 
 }
 
-void newqad(bool shutup) 
+void newqad(bool shutup)
+/* set up a new state of quadrant, for when we enter or re-enter it */
 {
     int i, j;
     coord w;
 {
     int i, j;
     coord w;
@@ -636,12 +642,14 @@ void newqad(bool shutup)
     game.quad[game.sector.x][game.sector.y] = game.ship;
 
     if (q->klingons) {
     game.quad[game.sector.x][game.sector.y] = game.ship;
 
     if (q->klingons) {
+       w.x = w.y = 0;  /* quiet a gcc warning */
        // Position ordinary Klingons
        for (i = 1; i <= game.klhere; i++)
        // Position ordinary Klingons
        for (i = 1; i <= game.klhere; i++)
-           newkling(i, &w);
+           w = newkling(i);
        // If we need a commander, promote a Klingon
        for_commanders(i)
        // If we need a commander, promote a Klingon
        for_commanders(i)
-           if (game.state.kcmdr[i].x==game.quadrant.x && game.state.kcmdr[i].y==game.quadrant.y) break;
+           if (same(game.state.kcmdr[i], game.quadrant))
+               break;
                        
        if (i <= game.state.remcom) {
            game.quad[w.x][w.y] = IHC;
                        
        if (i <= game.state.remcom) {
            game.quad[w.x][w.y] = IHC;
@@ -650,7 +658,7 @@ void newqad(bool shutup)
        }
 
        // If we need a super-commander, promote a Klingon
        }
 
        // If we need a super-commander, promote a Klingon
-       if (game.quadrant.x == game.state.kscmdr.x && game.quadrant.y == game.state.kscmdr.y) {
+       if (same(game.quadrant, game.state.kscmdr)) {
            game.quad[game.ks[1].x][game.ks[1].y] = IHS;
            game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
            game.iscate = game.state.remkl>1;
            game.quad[game.ks[1].x][game.ks[1].y] = IHS;
            game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
            game.iscate = game.state.remkl>1;
@@ -659,28 +667,28 @@ void newqad(bool shutup)
     }
     // Put in Romulans if needed
     for (i = game.klhere+1; i <= game.nenhere; i++) {
     }
     // Put in Romulans if needed
     for (i = game.klhere+1; i <= game.nenhere; i++) {
-       dropin(IHR, &w);
+       w = dropin(IHR);
        game.ks[i] = w;
        game.ks[i] = w;
-       game.kdist[i] = game.kavgd[i] = sqrt(square(game.sector.x-w.x) + square(game.sector.y-w.y));
+       game.kdist[i] = game.kavgd[i] = distance(game.sector, w);
        game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
     }
     // If quadrant needs a starbase, put it in
     if (q->starbase)
        game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
     }
     // If quadrant needs a starbase, put it in
     if (q->starbase)
-       dropin(IHB, &game.base);
+       game.base = dropin(IHB);
        
     // If quadrant needs a planet, put it in
     if (q->planet != NOPLANET) {
        game.iplnet = q->planet;
        if (game.state.plnets[q->planet].inhabited == UNINHABITED)
        
     // If quadrant needs a planet, put it in
     if (q->planet != NOPLANET) {
        game.iplnet = q->planet;
        if (game.state.plnets[q->planet].inhabited == UNINHABITED)
-           dropin(IHP, &game.plnet);
+           game.plnet = dropin(IHP);
        else
        else
-           dropin(IHW, &game.plnet);
+           game.plnet = dropin(IHW);
     }
     // Check for game.condition
     newcnd();
     // And finally the stars
     for (i = 1; i <= q->stars; i++) 
     }
     // Check for game.condition
     newcnd();
     // And finally the stars
     for (i = 1; i <= q->stars; i++) 
-       dropin(IHSTAR, &w);
+       dropin(IHSTAR);
 
     // Check for RNZ
     if (game.irhere > 0 && game.klhere == 0 && (q->planet == NOPLANET || game.state.plnets[q->planet].inhabited == UNINHABITED)) {
 
     // Check for RNZ
     if (game.irhere > 0 && game.klhere == 0 && (q->planet == NOPLANET || game.state.plnets[q->planet].inhabited == UNINHABITED)) {
@@ -698,13 +706,13 @@ void newqad(bool shutup)
     if (shutup==0) {
        // Put in THING if needed
        if (same(thing, game.quadrant)) {
     if (shutup==0) {
        // Put in THING if needed
        if (same(thing, game.quadrant)) {
-           dropin(IHQUEST, &w);
-           iran(GALSIZE, &thing.x, &thing.y);
+           w = dropin(IHQUEST);
+           thing = iran(GALSIZE);
            game.nenhere++;
            iqhere=1;
            game.ks[game.nenhere] = w;
            game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
            game.nenhere++;
            iqhere=1;
            game.ks[game.nenhere] = w;
            game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
-               sqrt(square(game.sector.x-w.x) + square(game.sector.y-w.y));
+               distance(game.sector, w);
            game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
            if (!damaged(DSRSENS)) {
                skip(1);
            game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
            if (!damaged(DSRSENS)) {
                skip(1);
@@ -730,7 +738,7 @@ void newqad(bool shutup)
            game.ks[game.nenhere].x = game.tholian.x;
            game.ks[game.nenhere].y = game.tholian.y;
            game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
            game.ks[game.nenhere].x = game.tholian.x;
            game.ks[game.nenhere].y = game.tholian.y;
            game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
-               sqrt(square(game.sector.x-game.tholian.x) + square(game.sector.y-game.tholian.y));
+               distance(game.sector, game.tholian);
            game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
            /* Reserve unocupied corners */
            if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
            game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
            /* Reserve unocupied corners */
            if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
@@ -745,7 +753,7 @@ void newqad(bool shutup)
     // Put in a few black holes
     for (i = 1; i <= 3; i++)
        if (Rand() > 0.5) 
     // Put in a few black holes
     for (i = 1; i <= 3; i++)
        if (Rand() > 0.5) 
-           dropin(IHBLANK, &w);
+           dropin(IHBLANK);
 
     // Take out X's in corners if Tholian present
     if (game.ithere) {
 
     // Take out X's in corners if Tholian present
     if (game.ithere) {
@@ -757,6 +765,7 @@ void newqad(bool shutup)
 }
 
 void sortkl(void) 
 }
 
 void sortkl(void) 
+/* sort Klingons by distance from us */
 {
     double t;
     int j, k;
 {
     double t;
     int j, k;
@@ -790,7 +799,8 @@ void sortkl(void)
     } while (sw);
 }
 
     } while (sw);
 }
 
-void setpassword(void) 
+void setpassword(void)
+/* set the self-destruct password */
 {
     if (!(game.options & OPTION_CURSES)) {
        while (TRUE) {
 {
     if (!(game.options & OPTION_CURSES)) {
        while (TRUE) {
index 16199e6df68ccb8acd5fd54b1fcf2ab7e4b7f966..cef3f7a8dd9ca9f9f1d40f6e4edc04fc2ee17fd7 100644 (file)
--- a/src/sst.c
+++ b/src/sst.c
@@ -290,7 +290,9 @@ commands[] = {
 #define NUMCOMMANDS    sizeof(commands)/sizeof(commands[0])
 #define ACCEPT(i)      (!commands[i].option || (commands[i].option & game.options))
 
 #define NUMCOMMANDS    sizeof(commands)/sizeof(commands[0])
 #define ACCEPT(i)      (!commands[i].option || (commands[i].option & game.options))
 
-static void listCommands(void) {
+static void listCommands(void) 
+/* generate a list of legal commands */
+{
     int i, k = 0;
     proutn("LEGAL COMMANDS ARE:");
     for (i = 0; i < NUMCOMMANDS; i++) {
     int i, k = 0;
     proutn("LEGAL COMMANDS ARE:");
     for (i = 0; i < NUMCOMMANDS; i++) {
@@ -304,7 +306,8 @@ static void listCommands(void) {
     skip(1);
 }
 
     skip(1);
 }
 
-static void helpme(void) 
+static void helpme(void)
+/* browse on-line help */
 {
     int i, j;
     char cmdbuf[32], *cp;
 {
     int i, j;
     char cmdbuf[32], *cp;
@@ -383,12 +386,14 @@ static void helpme(void)
     fclose(fp);
 }
 
     fclose(fp);
 }
 
-void enqueue(char *s) 
+void enqueue(char *s)
+/* enqueue input for the command parser */
 {
     strcpy(line, s);
 }
 
 {
     strcpy(line, s);
 }
 
-static void makemoves(void) 
+static void makemoves(void)
+/* command-interpretation loop */
 {
     int key, i, v = 0;
     bool hitme;
 {
     int key, i, v = 0;
     bool hitme;
@@ -551,7 +556,7 @@ static void makemoves(void)
            if (game.ididit) hitme = true;
            break;
        case QUIT:
            if (game.ididit) hitme = true;
            break;
        case QUIT:
-           game.alldone = 1;           // quit the game
+           game.alldone = true;                // quit the game
            break;
        case HELP:
            helpme();                   // get help
            break;
        case HELP:
            helpme();                   // get help
@@ -647,7 +652,7 @@ int main(int argc, char **argv)
        setup(line[0] == '\0');
        if (game.alldone) {
            score();
        setup(line[0] == '\0');
        if (game.alldone) {
            score();
-           game.alldone = 0;
+           game.alldone = false;
        }
        else makemoves();
        skip(1);
        }
        else makemoves();
        skip(1);
@@ -671,6 +676,7 @@ int main(int argc, char **argv)
 
 
 void cramen(int i) 
 
 
 void cramen(int i) 
+/* print the name of an enemy */
 {
     /* return an enemy */
     char *s;
 {
     /* return an enemy */
     char *s;
@@ -693,6 +699,7 @@ void cramen(int i)
 }
 
 char *cramlc(enum loctype key, coord w)
 }
 
 char *cramlc(enum loctype key, coord w)
+/* name a location */
 {
     static char buf[32];
     buf[0] = '\0';
 {
     static char buf[32];
     buf[0] = '\0';
@@ -703,6 +710,7 @@ char *cramlc(enum loctype key, coord w)
 }
 
 void crmena(bool stars, int enemy, enum loctype key, coord w) 
 }
 
 void crmena(bool stars, int enemy, enum loctype key, coord w) 
+/* print an enemy and his location */
 {
     if (stars) proutn("***");
     cramen(enemy);
 {
     if (stars) proutn("***");
     cramen(enemy);
@@ -710,7 +718,8 @@ void crmena(bool stars, int enemy, enum loctype key, coord w)
     proutn(cramlc(key, w));
 }
 
     proutn(cramlc(key, w));
 }
 
-void crmshp(void) 
+void crmshp(void)
+/* print our ship name */
 {
     char *s;
     switch (game.ship) {
 {
     char *s;
     switch (game.ship) {
@@ -721,7 +730,8 @@ void crmshp(void)
     proutn(s);
 }
 
     proutn(s);
 }
 
-void stars(void) 
+void stars(void)
+/* print a line of stars */
 {
     prouts("******************************************************");
     skip(1);
 {
     prouts("******************************************************");
     skip(1);
@@ -737,10 +747,13 @@ double Rand(void)
     return rand()/(1.0 + (double)RAND_MAX);
 }
 
     return rand()/(1.0 + (double)RAND_MAX);
 }
 
-void iran(int size, int *i, int *j) 
+coord iran(int size)
+/* choose a random location */ 
 {
 {
-    *i = Rand()*(size*1.0) + 1.0;
-    *j = Rand()*(size*1.0) + 1.0;
+    coord w;
+    w.x = Rand()*(size*1.0) + 1.0;
+    w.y = Rand()*(size*1.0) + 1.0;
+    return w;
 }
 
 void chew(void)
 }
 
 void chew(void)
@@ -811,7 +824,8 @@ int scan(void)
     return IHALPHA;
 }
 
     return IHALPHA;
 }
 
-bool ja(void) 
+bool ja(void)
+/* yes-or-no confirmation */
 {
     chew();
     for(;;) {
 {
     chew();
     for(;;) {
@@ -823,7 +837,8 @@ bool ja(void)
     }
 }
 
     }
 }
 
-void huh(void) 
+void huh(void)
+/* complain about unparseable input */
 {
     chew();
     skip(1);
 {
     chew();
     skip(1);
@@ -831,15 +846,13 @@ void huh(void)
 }
 
 int isit(char *s) 
 }
 
 int isit(char *s) 
+/* compares s to citem and returns true if it matches to the length of s */
 {
 {
-    /* New function -- compares s to scanned citem and returns true if it
-       matches to the length of s */
-
     return strncasecmp(s, citem, max(1, strlen(citem))) == 0;
     return strncasecmp(s, citem, max(1, strlen(citem))) == 0;
-
 }
 
 }
 
-void debugme(void) 
+void debugme(void)
+/* access to the internals for debugging */
 {
     proutn("Reset levels? ");
     if (ja() == true) {
 {
     proutn("Reset levels? ");
     if (ja() == true) {
index 1c40d1b946d43a3e932b0814c363decca37f4e49..07e7a2bc37d3516b143435e31bc7bdd7cd9a16b7 100644 (file)
--- a/src/sst.h
+++ b/src/sst.h
@@ -42,7 +42,9 @@
 
 typedef struct {int x; int y;} coord;
 
 
 typedef struct {int x; int y;} coord;
 
-#define same(c1, c2)   (c1.x == c2.x && c1.y == c2.y)
+#define square(i)              ((i)*(i))
+#define same(c1, c2)           ((c1.x == c2.x) && (c1.y == c2.y))
+#define distance(c1, c2)       sqrt(square(c1.x - c2.x) + square(c1.y - c2.y))
 
 typedef struct {
     coord w;
 
 typedef struct {
     coord w;
@@ -355,7 +357,7 @@ void finish(FINTYPE);
 void selfdestruct(void);
 void kaboom(void);
 void freeze(bool);
 void selfdestruct(void);
 void kaboom(void);
 void freeze(bool);
-int thaw(void);
+bool thaw(void);
 void plaque(void);
 int scan(void);
 #define IHEOL (0)
 void plaque(void);
 int scan(void);
 #define IHEOL (0)
@@ -374,9 +376,8 @@ void crmshp(void);
 char *cramlc(enum loctype, coord w);
 double expran(double);
 double Rand(void);
 char *cramlc(enum loctype, coord w);
 double expran(double);
 double Rand(void);
-void iran(int, int *, int *);
-#define square(i) ((i)*(i))
-void dropin(int, coord*);
+coord iran(int);
+coord dropin(int);
 void newcnd(void);
 void sortkl(void);
 void imove(void);
 void newcnd(void);
 void sortkl(void);
 void imove(void);
@@ -419,7 +420,7 @@ void commandhook(char *, bool);
 void makechart(void);
 void enqueue(char *);
 char *systemname(int);
 void makechart(void);
 void enqueue(char *);
 char *systemname(int);
-void newkling(int, coord *);
+coord newkling(int);
 
 /* mode arguments for srscan() */
 #define SCAN_FULL              1
 
 /* mode arguments for srscan() */
 #define SCAN_FULL              1