From c94108fede4723af905386473f6435467e96e630 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 20 Sep 2006 06:25:57 +0000 Subject: [PATCH] Replace more #defines with enums. --- src/ai.c | 10 +++--- src/battle.c | 29 ++++++++------- src/events.c | 18 +++++----- src/io.c | 10 +++--- src/moving.c | 3 +- src/reports.c | 9 +++-- src/setup.c | 24 ++++++------- src/sst.c | 10 +++--- src/sst.h | 98 +++++++++++++++++++++++++++------------------------ 9 files changed, 111 insertions(+), 100 deletions(-) diff --git a/src/ai.c b/src/ai.c index 91d7ab4..7adbfdf 100644 --- a/src/ai.c +++ b/src/ai.c @@ -28,7 +28,7 @@ static bool tryexit(coord look, int ienm, int loccom, bool irun) We know this if either short or long range sensors are working */ if (!damaged(DSRSENS) || !damaged(DLRSENS) || game.condition == docked) { - crmena(1, ienm, sector, game.ks[loccom]); + crmena(true, ienm, sector, game.ks[loccom]); prout(_(" escapes to %s (and regains strength)."), cramlc(quadrant, iq)); } @@ -66,7 +66,7 @@ static bool tryexit(coord look, int ienm, int loccom, bool irun) } -static void movebaddy(coord com, int loccom, int ienm) +static void movebaddy(coord com, int loccom, feature ienm) /* tactical movement for the bad guys */ { int motion, mdist, nsteps, mx, my, ll; @@ -335,7 +335,7 @@ static bool movescom(coord iq, bool flag, bool *ipage) DESTROY(&game.state.plnets[i]); game.state.galaxy[game.state.kscmdr.x][game.state.kscmdr.y].planet = NOPLANET; if (!damaged(DRADIO) || game.condition == docked) { - if (*ipage==0) pause_game(1); + if (*ipage==0) pause_game(true); *ipage = 1; prout(_("Lt. Uhura- \"Captain, Starfleet Intelligence reports")); proutn(_(" a planet in ")); @@ -490,7 +490,7 @@ void scom(bool *ipage) if (damaged(DRADIO) && game.condition != docked) return; /* no warning */ game.iseenit = true; - if (*ipage == 0) pause_game(1); + if (*ipage == 0) pause_game(true); *ipage=1; proutn(_("Lt. Uhura- \"Captain, the starbase in ")); proutn(cramlc(quadrant, game.state.kscmdr)); @@ -513,7 +513,7 @@ void scom(bool *ipage) (damaged(DRADIO) && game.condition != docked) || !game.state.galaxy[game.state.kscmdr.x][game.state.kscmdr.y].charted)) return; - if (*ipage==0) pause_game(1); + if (*ipage==0) pause_game(true); *ipage = 1; prout(_("Lt. Uhura- \"Captain, Starfleet Intelligence reports")); proutn(_(" the Super-commander is in ")); diff --git a/src/battle.c b/src/battle.c index 6e171b2..59d2d93 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1,6 +1,6 @@ #include "sst.h" -void doshield(int i) +void doshield(bool raise) /* change shield status */ { int key; @@ -8,7 +8,8 @@ void doshield(int i) game.ididit = false; - if (i == 2) action = SHUP; + if (raise) + action = SHUP; else { key = scan(); if (key == IHALPHA) { @@ -130,10 +131,10 @@ void doshield(int i) } } -void ram(bool ibumpd, int ienm, coord w) +void ram(bool ibumpd, feature ienm, coord w) /* make our ship ram something */ { - double type = 1.0, extradm; + double hardness, extradm; int icas, m; prouts(_("***RED ALERT! RED ALERT!")); @@ -143,11 +144,12 @@ void ram(bool ibumpd, int ienm, coord w) proutn("***"); crmshp(); switch (ienm) { - case IHR: type = 1.5; break; - case IHC: type = 2.0; break; - case IHS: type = 2.5; break; - case IHT: type = 0.5; break; - case IHQUEST: type = 4.0; break; + case IHR: hardness = 1.5; break; + case IHC: hardness = 2.0; break; + case IHS: hardness = 2.5; break; + case IHT: hardness = 0.5; break; + case IHQUEST: hardness = 4.0; break; + default: hardness = 1.0; break; } proutn(ibumpd ? _(" rammed by ") : _(" rams ")); crmena(false, ienm, sector, w); @@ -166,12 +168,12 @@ void ram(bool ibumpd, int ienm, coord w) continue; // Don't damage deathray if (game.damage[m] < 0) continue; - extradm = (10.0*type*Rand()+1.0)*game.damfac; + extradm = (10.0*hardness*Rand()+1.0)*game.damfac; game.damage[m] += game.optime + extradm; /* Damage for at least time of travel! */ } game.shldup = false; if (KLINGREM) { - pause_game(2); + pause_game(true); dreprt(); } else finish(FWON); @@ -613,7 +615,7 @@ void attack(bool torps_ok) return; } -void deadkl(coord w, int type, coord mv) +void deadkl(coord w, feature type, coord mv) /* kill a Klingon, Tholian, Romulan, or Thingy */ { /* Added mv to allow enemy to "move" before dying */ @@ -664,6 +666,9 @@ void deadkl(coord w, int type, coord mv) unschedule(FSCMOVE); unschedule(FSCDBAS); break; + default: /* avoids a gcc warning */ + prout("*** Internal error, deadkl() called on %c\n", type); + break; } } diff --git a/src/events.c b/src/events.c index 466867c..1cf1492 100644 --- a/src/events.c +++ b/src/events.c @@ -153,7 +153,7 @@ void events(void) game.optime -= xtime; switch (evcode) { case FSNOVA: /* Supernova */ - if (!ipage) pause_game(1); + if (!ipage) pause_game(true); ipage=true; snova(false, NULL); schedule(FSNOVA, expran(0.5*game.intime)); @@ -192,7 +192,7 @@ void events(void) } /* tractor beaming cases merge here */ yank = sqrt(yank); - if (!ipage) pause_game(1); + if (!ipage) pause_game(true); ipage=true; game.optime = (10.0/(7.5*7.5))*yank; /* 7.5 is yank rate (warp 7.5) */ ictbeam = 1; @@ -224,7 +224,7 @@ void events(void) game.quadrant = game.state.kscmdr; else game.quadrant = game.state.kcmdr[i]; - game.sector = iran(QUADSIZE); + game.sector = randplace(QUADSIZE); crmshp(); proutn(_(" is pulled to ")); proutn(cramlc(quadrant, game.quadrant)); @@ -236,7 +236,7 @@ void events(void) } if (!game.shldup) { if (!damaged(DSHIELD) && game.shield > 0) { - doshield(2); /* Shldsup */ + doshield(true); /* raise shields */ game.shldchg=0; } else prout(_("(Shields not currently useable.)")); @@ -287,7 +287,7 @@ void events(void) if (!damaged(DRADIO) && game.condition != docked) break; /* No warning :-( */ game.iseenit = true; - if (!ipage) pause_game(1); + if (!ipage) pause_game(true); ipage = true; skip(1); proutn(_("Lt. Uhura- \"Captain, the starbase in ")); @@ -335,7 +335,7 @@ void events(void) else if (game.state.rembase != 1 && (!damaged(DRADIO) || game.condition == docked)) { /* Get word via subspace radio */ - if (!ipage) pause_game(1); + if (!ipage) pause_game(true); ipage = true; skip(1); prout(_("Lt. Uhura- \"Captain, Starfleet Command reports that")); @@ -381,7 +381,7 @@ void events(void) game.state.galaxy[game.probec.x][game.probec.y].supernova) { // Left galaxy or ran into supernova if (!damaged(DRADIO) || game.condition == docked) { - if (ipage==0) pause_game(1); + if (ipage==0) pause_game(true); ipage = 1; skip(1); proutn(_("Lt. Uhura- \"The deep space probe ")); @@ -395,7 +395,7 @@ void events(void) break; } if (!damaged(DRADIO) || game.condition == docked) { - if (ipage==0) pause_game(1); + if (ipage==0) pause_game(true); ipage = 1; skip(1); proutn(_("Lt. Uhura- \"The deep space probe is now in ")); @@ -432,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 */ - w = iran(GALSIZE); + w = randplace(GALSIZE); q = &game.state.galaxy[w.x][w.y]; } while (--i && (same(game.quadrant, w) || q->planet == NOPLANET || diff --git a/src/io.c b/src/io.c index 74b3fce..7ea0b2c 100644 --- a/src/io.c +++ b/src/io.c @@ -85,11 +85,11 @@ void waitfor(void) getch(); } -void pause_game(int i) +void pause_game(bool announcement) { char *prompt; char buf[BUFSIZ]; - if (i==1) { + if (announcement) { if (game.skill > SKILL_FAIR) prompt = _("[ANOUNCEMENT ARRIVING...]"); else @@ -115,7 +115,7 @@ void pause_game(int i) putchar('\n'); proutn(prompt); fgets(buf, sizeof(buf), stdin); - if (i != 0) { + if (announcement) { int j; for (j = 0; j < rows; j++) putchar('\n'); @@ -130,7 +130,7 @@ void skip(int i) while (i-- > 0) { if (game.options & OPTION_CURSES) { if (curwnd == message_window && linecount >= getmaxy(curwnd) - 3) { - pause_game(0); + pause_game(false); clrscr(); } else { proutn("\n"); @@ -140,7 +140,7 @@ void skip(int i) } else { linecount++; if (linecount >= rows) - pause_game(0); + pause_game(false); else putchar('\n'); } diff --git a/src/moving.c b/src/moving.c index 3ed56a4..c02f1c4 100644 --- a/src/moving.c +++ b/src/moving.c @@ -9,7 +9,8 @@ void imove(void) { double angle, deltax, deltay, bigger, x, y, finald, stopegy, probf; - int n, m, kink, kinks, iquad; + int n, m, kink, kinks; + feature iquad; coord w, final; bool trbeam = false; diff --git a/src/reports.c b/src/reports.c index 6120d02..90b9faf 100644 --- a/src/reports.c +++ b/src/reports.c @@ -337,7 +337,7 @@ static void status(int req) } } -int srscan(int l) +void srscan(scantype type) /* short-range scan */ { /* the "sy" request is undocumented */ @@ -346,7 +346,7 @@ int srscan(int l) int i, j, jj, req=0; int goodScan=true, leftside=true, rightside=true, title=false; - switch (l) { + switch (type) { case SCAN_FULL: // SRSCAN if (damaged(DSRSENS)) { /* Allow base's sensors if docked */ @@ -381,7 +381,7 @@ int srscan(int l) prout(_("UNRECOGNIZED REQUEST. Legal requests are:")); prout(_(" date, condition, position, lsupport, warpfactor,")); prout(_(" energy, torpedoes, shields, klingons, time, system, bases.")); - return false; + return; } // no break case SCAN_STATUS: // STATUS @@ -405,11 +405,10 @@ int srscan(int l) if (rightside) status(jj); if (i 0; j--) { @@ -339,7 +339,7 @@ void setup(bool needprompt) int klump = (1.0 - r*r)*klumper; if (klump > krem) klump = krem; krem -= klump; - do w = iran(GALSIZE); + do w = randplace(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; @@ -358,7 +358,7 @@ void setup(bool needprompt) } else #endif /* ODEBUG */ - w = iran(GALSIZE); + w = randplace(GALSIZE); } while ((!game.state.galaxy[w.x][w.y].klingons && Rand() < 0.75)|| game.state.galaxy[w.x][w.y].supernova|| @@ -372,7 +372,7 @@ void setup(bool needprompt) } // Locate planets in galaxy for (i = 0; i < game.inplan; i++) { - do w = iran(GALSIZE); + do w = randplace(GALSIZE); while (game.state.galaxy[w.x][w.y].planet != NOPLANET); game.state.plnets[i].w = w; if (i < NINHAB) { @@ -391,19 +391,19 @@ void setup(bool needprompt) } // Locate Romulans for (i = 1; i <= game.state.nromrem; i++) { - w = iran(GALSIZE); + w = randplace(GALSIZE); game.state.galaxy[w.x][w.y].romulans = 1; } // Locate the Super Commander if (game.state.nscrem > 0) { - do w = iran(GALSIZE); + do w = randplace(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) { - thing = iran(GALSIZE); + thing = randplace(GALSIZE); } else { thing.x = thing.y = 0; @@ -566,11 +566,11 @@ bool choose(bool needprompt) return false; } -coord dropin(int iquad) +coord dropin(feature iquad) /* drop a feature on a random dot in the current quadrant */ { coord w; - do w = iran(QUADSIZE); + do w = randplace(QUADSIZE); while (game.quad[w.x][w.y] != IHDOT); game.quad[w.x][w.y] = iquad; return w; @@ -707,7 +707,7 @@ void newqad(bool shutup) // Put in THING if needed if (same(thing, game.quadrant)) { w = dropin(IHQUEST); - thing = iran(GALSIZE); + thing = randplace(GALSIZE); game.nenhere++; iqhere=1; game.ks[game.nenhere] = w; diff --git a/src/sst.c b/src/sst.c index ba9bef7..f0ee5f9 100644 --- a/src/sst.c +++ b/src/sst.c @@ -460,7 +460,7 @@ static void makemoves(void) warp(false); break; case SHIELDS: // shields - doshield(1); + doshield(false); if (game.ididit) { hitme=true; game.shldchg = 0; @@ -675,7 +675,7 @@ int main(int argc, char **argv) } -void cramen(int i) +void cramen(feature i) /* print the name of an enemy */ { /* return an enemy */ @@ -709,7 +709,7 @@ char *cramlc(enum loctype key, coord w) return buf; } -void crmena(bool stars, int enemy, enum loctype key, coord w) +void crmena(bool stars, feature enemy, enum loctype key, coord w) /* print an enemy and his location */ { if (stars) proutn("***"); @@ -747,7 +747,7 @@ double Rand(void) return rand()/(1.0 + (double)RAND_MAX); } -coord iran(int size) +coord randplace(int size) /* choose a random location */ { coord w; @@ -845,7 +845,7 @@ void huh(void) prout("Beg your pardon, Captain?"); } -int isit(char *s) +bool isit(char *s) /* compares s to citem and returns true if it matches to the length of s */ { return strncasecmp(s, citem, max(1, strlen(citem))) == 0; diff --git a/src/sst.h b/src/sst.h index 18baff6..75e9be9 100644 --- a/src/sst.h +++ b/src/sst.h @@ -58,6 +58,27 @@ typedef struct { #define DESTROY(pl) memset(pl, '\0', sizeof(planet)) +typedef enum { + IHR = 'R', + IHK = 'K', + IHC = 'C', + IHS = 'S', + IHSTAR = '*', + IHP = 'P', + IHW = '@', + IHB = 'B', + IHBLANK = ' ', + IHDOT = '.', + IHQUEST = '?', + IHE = 'E', + IHF = 'F', + IHT = 'T', + IHWEB = '#', + IHMATER0 = '-', + IHMATER1 = 'o', + IHMATER2 = '0', +} feature; + typedef struct { int snap, // snapshot taken crew, // crew complement @@ -193,7 +214,7 @@ struct game { unsigned long options; snapshot state; snapshot snapsht; - char quad[QUADSIZE+1][QUADSIZE+1]; // contents of our quadrant + feature quad[QUADSIZE+1][QUADSIZE+1]; // contents of our quadrant double kpower[(QUADSIZE+1)*(QUADSIZE+1)]; // enemy energy levels double kdist[(QUADSIZE+1)*(QUADSIZE+1)]; // enemy distances double kavgd[(QUADSIZE+1)*(QUADSIZE+1)]; // average distances @@ -227,11 +248,11 @@ struct game { iseenit, // seen base attack report thawed; // thawed game enum { - green = 'G', - yellow = 'Y', - red = 'R', - docked = 'D', - dead = 'Z', + green, + yellow, + red, + docked, + dead, } condition; // condition (red/yellow/green/docked) int inkling, // initial number of klingons inbase, // initial number of bases @@ -302,31 +323,22 @@ extern FILE *logfp, *replayfp; extern coord thing; extern bool iqhere, iqengry; -typedef enum {FWON, FDEPLETE, FLIFESUP, FNRG, FBATTLE, - FNEG3, FNOVA, FSNOVAED, FABANDN, FDILITHIUM, - FMATERIALIZE, FPHASER, FLOST, FMINING, FDPLANET, - FPNOVA, FSSC, FSTRACTOR, FDRAY, FTRIBBLE, - FHOLE, FCREW} FINTYPE ; +typedef enum { + FWON, FDEPLETE, FLIFESUP, FNRG, FBATTLE, + FNEG3, FNOVA, FSNOVAED, FABANDN, FDILITHIUM, + FMATERIALIZE, FPHASER, FLOST, FMINING, FDPLANET, + FPNOVA, FSSC, FSTRACTOR, FDRAY, FTRIBBLE, + FHOLE, FCREW +} FINTYPE ; + enum loctype {neither, quadrant, sector}; -#define IHR 'R' -#define IHK 'K' -#define IHC 'C' -#define IHS 'S' -#define IHSTAR '*' -#define IHP 'P' -#define IHW '@' -#define IHB 'B' -#define IHBLANK ' ' -#define IHDOT '.' -#define IHQUEST '?' -#define IHE 'E' -#define IHF 'F' -#define IHT 'T' -#define IHWEB '#' -#define IHMATER0 '-' -#define IHMATER1 'o' -#define IHMATER2 '0' +typedef enum { + SCAN_FULL, + SCAN_REQUEST, + SCAN_STATUS, + SCAN_NO_LEFTSIDE, +} scantype; /* Function prototypes */ void prelim(void); @@ -335,12 +347,12 @@ bool choose(bool); void setup(bool); void score(void); void atover(bool); -int srscan(int); +void srscan(scantype); void lrscan(void); void phasers(void); void photon(void); void warp(bool); -void doshield(int); +void doshield(bool); void dock(bool); void dreprt(void); void chart(bool); @@ -371,30 +383,30 @@ void proutn(char *, ...); void stars(void); void newqad(bool); bool ja(void); -void cramen(int); +void cramen(feature); void crmshp(void); char *cramlc(enum loctype, coord w); double expran(double); double Rand(void); -coord iran(int); -coord dropin(int); +coord randplace(int); +coord dropin(feature); void newcnd(void); void sortkl(void); void imove(void); -void ram(bool, int, coord); -void crmena(bool, int, enum loctype, coord w); -void deadkl(coord, int, coord); +void ram(bool, feature, coord); +void crmena(bool, feature, enum loctype, coord w); +void deadkl(coord, feature, coord); void timwrp(void); void movcom(void); void torpedo(double, double, coord, double *, int, int); void huh(void); -void pause_game(int); +void pause_game(bool); void nova(coord); void snova(bool, coord *); void scom(bool *); void hittem(double *); void prouts(char *, ...); -int isit(char *); +bool isit(char *); void preport(void); void orbit(void); void sensor(void); @@ -422,12 +434,6 @@ void enqueue(char *); char *systemname(int); coord newkling(int); -/* mode arguments for srscan() */ -#define SCAN_FULL 1 -#define SCAN_REQUEST 2 -#define SCAN_STATUS 3 -#define SCAN_NO_LEFTSIDE 4 - extern WINDOW *curwnd; extern WINDOW *fullscreen_window; extern WINDOW *srscan_window; @@ -438,7 +444,7 @@ extern WINDOW *prompt_window; extern void clreol(void); extern void clrscr(void); -extern void textcolor(int color); +extern void textcolor(int); extern void highvideo(void); enum COLORS { -- 2.31.1