From: Eric S. Raymond Date: Wed, 20 Sep 2006 03:26:16 +0000 (+0000) Subject: More boolean cleanups. More consistent use of coord type X-Git-Tag: 2.0~222 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=093f7d420e9cf92fbaf751f57a199c56562ae4b5;p=super-star-trek.git More boolean cleanups. More consistent use of coord type Anong other things, this allows us to write a distance() macro and get rid of a lot of gnarly expressions. --- diff --git a/src/ai.c b/src/ai.c index 6fc7908..e596571 100644 --- 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; - 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); @@ -387,9 +386,7 @@ void scom(bool *ipage) 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 */ @@ -532,7 +529,6 @@ void movetho(void) /* move the Tholian */ { int idx, idy, im, i; - coord dummy; /* 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; - dropin(IHBLANK, &dummy); + dropin(IHBLANK); crmena(true, IHT, sector, game.tholian); prout(_(" completes web.")); game.ithere = false; diff --git a/src/battle.c b/src/battle.c index dba9f29..a1bacfa 100644 --- a/src/battle.c +++ b/src/battle.c @@ -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--; - { - coord dummy; - dropin(IHBLANK, &dummy); - } + dropin(IHBLANK); 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; + w.x = jx; w.y = jy; 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; } diff --git a/src/events.c b/src/events.c index 95a3c44..1e04dd0 100644 --- a/src/events.c +++ b/src/events.c @@ -93,6 +93,7 @@ void events(void) radio_was_broken = damaged(DRADIO); + hold.x = hold.y = 0; 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]; - iran(QUADSIZE, &game.sector.x, &game.sector.y); + game.sector = iran(QUADSIZE); 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 */ - 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 || @@ -521,7 +522,7 @@ void events(void) 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); @@ -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.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; } @@ -849,7 +849,7 @@ void snova(bool induced, coord *w) prouts("***************"); skip(1); stars(); - game.alldone=1; + game.alldone = true; } } diff --git a/src/moving.c b/src/moving.c index 64ad0a7..99c1891 100644 --- a/src/moving.c +++ b/src/moving.c @@ -945,12 +945,12 @@ void mayday(void) 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) { - 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; diff --git a/src/reports.c b/src/reports.c index 7ea7c17..57297f5 100644 --- a/src/reports.c +++ b/src/reports.c @@ -416,7 +416,7 @@ int srscan(int l) 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)) { @@ -433,33 +433,33 @@ void eta(void) return; } } - iy1 = aaitem +0.5; + w1.y = aaitem +0.5; if (scan() != IHREAL) { huh(); return; } - ix1 = aaitem + 0.5; + w1.x = aaitem + 0.5; if (scan() == IHREAL) { - iy2 = aaitem + 0.5; + w2.y = aaitem + 0.5; if (scan() != IHREAL) { huh(); return; } - ix2 = aaitem + 0.5; + w2.x = aaitem + 0.5; } 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; } - 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:")); @@ -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")); - 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(FCDBAS) 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) - 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)) { @@ -322,10 +325,9 @@ void setup(bool needprompt) } } 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; @@ -337,10 +339,10 @@ void setup(bool needprompt) 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 @@ -351,30 +353,28 @@ void setup(bool needprompt) 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 */ - 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++) - 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); - 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++) { - 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; @@ -387,24 +387,23 @@ void setup(bool needprompt) 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++) { - 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) { - 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) { - iran(GALSIZE, &thing.x, &thing.y); + thing = iran(GALSIZE); } else { thing.x = thing.y = 0; @@ -455,10 +454,11 @@ void setup(bool needprompt) } bool choose(bool needprompt) +/* choose your game type */ { 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 */ @@ -566,14 +566,18 @@ bool choose(bool needprompt) 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; @@ -582,16 +586,18 @@ void newcnd(void) if (!game.alive) game.condit=IHDEAD; } -void newkling(int i, coord *pi) +coord newkling(int i) /* 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; + 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; @@ -636,12 +642,14 @@ void newqad(bool shutup) 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++) - newkling(i, &w); + w = newkling(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; @@ -650,7 +658,7 @@ void newqad(bool shutup) } // 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; @@ -659,28 +667,28 @@ void newqad(bool shutup) } // 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.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) - 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) - dropin(IHP, &game.plnet); + game.plnet = dropin(IHP); 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++) - 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)) { @@ -698,13 +706,13 @@ void newqad(bool shutup) 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] = - 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); @@ -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] = - 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'; @@ -745,7 +753,7 @@ void newqad(bool shutup) // 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) { @@ -757,6 +765,7 @@ void newqad(bool shutup) } void sortkl(void) +/* sort Klingons by distance from us */ { double t; int j, k; @@ -790,7 +799,8 @@ void sortkl(void) } while (sw); } -void setpassword(void) +void setpassword(void) +/* set the self-destruct password */ { if (!(game.options & OPTION_CURSES)) { while (TRUE) { diff --git a/src/sst.c b/src/sst.c index 16199e6..cef3f7a 100644 --- 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)) -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++) { @@ -304,7 +306,8 @@ static void listCommands(void) { skip(1); } -static void helpme(void) +static void helpme(void) +/* browse on-line help */ { int i, j; char cmdbuf[32], *cp; @@ -383,12 +386,14 @@ static void helpme(void) fclose(fp); } -void enqueue(char *s) +void enqueue(char *s) +/* enqueue input for the command parser */ { strcpy(line, s); } -static void makemoves(void) +static void makemoves(void) +/* command-interpretation loop */ { int key, i, v = 0; bool hitme; @@ -551,7 +556,7 @@ static void makemoves(void) 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 @@ -647,7 +652,7 @@ int main(int argc, char **argv) setup(line[0] == '\0'); if (game.alldone) { score(); - game.alldone = 0; + game.alldone = false; } else makemoves(); skip(1); @@ -671,6 +676,7 @@ int main(int argc, char **argv) void cramen(int i) +/* print the name of an enemy */ { /* return an enemy */ char *s; @@ -693,6 +699,7 @@ void cramen(int i) } char *cramlc(enum loctype key, coord w) +/* name a location */ { 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) +/* print an enemy and his location */ { if (stars) proutn("***"); cramen(enemy); @@ -710,7 +718,8 @@ void crmena(bool stars, int enemy, enum loctype key, coord w) proutn(cramlc(key, w)); } -void crmshp(void) +void crmshp(void) +/* print our ship name */ { char *s; switch (game.ship) { @@ -721,7 +730,8 @@ void crmshp(void) proutn(s); } -void stars(void) +void stars(void) +/* print a line of stars */ { prouts("******************************************************"); skip(1); @@ -737,10 +747,13 @@ double Rand(void) 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) @@ -811,7 +824,8 @@ int scan(void) return IHALPHA; } -bool ja(void) +bool ja(void) +/* yes-or-no confirmation */ { chew(); for(;;) { @@ -823,7 +837,8 @@ bool ja(void) } } -void huh(void) +void huh(void) +/* complain about unparseable input */ { chew(); skip(1); @@ -831,15 +846,13 @@ void huh(void) } 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; - } -void debugme(void) +void debugme(void) +/* access to the internals for debugging */ { proutn("Reset levels? "); if (ja() == true) { diff --git a/src/sst.h b/src/sst.h index 1c40d1b..07e7a2b 100644 --- a/src/sst.h +++ b/src/sst.h @@ -42,7 +42,9 @@ 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; @@ -355,7 +357,7 @@ void finish(FINTYPE); void selfdestruct(void); void kaboom(void); void freeze(bool); -int thaw(void); +bool thaw(void); 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); -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); @@ -419,7 +420,7 @@ void commandhook(char *, bool); void makechart(void); void enqueue(char *); char *systemname(int); -void newkling(int, coord *); +coord newkling(int); /* mode arguments for srscan() */ #define SCAN_FULL 1