From 459d6afcbe13be588b29c8bd1fbffb39b13fdee1 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 18 Sep 2006 08:18:50 +0000 Subject: [PATCH] New debug-mode support. Get rid of DEBUG, the instrumentation is cheap and we want to cut down on the possibilities for unanticipated logic changes due to configuration vagaries. Take the debugging bit out of the game structure. Start on the logger. --- src/ai.c | 52 +++++++++++++++++++-------------------------------- src/battle.c | 4 +--- src/events.c | 17 +++++++---------- src/finish.c | 12 ++++-------- src/io.c | 4 ++++ src/moving.c | 6 ++---- src/planets.c | 14 ++++++-------- src/reports.c | 2 +- src/setup.c | 35 +++++++++++++++------------------- src/sst.c | 43 +++++++++++++++++++++--------------------- src/sst.h | 9 ++++----- 11 files changed, 85 insertions(+), 113 deletions(-) diff --git a/src/ai.c b/src/ai.c index dbf6573..685fe69 100644 --- a/src/ai.c +++ b/src/ai.c @@ -154,12 +154,8 @@ static void movebaddy(coord com, int loccom, int ienm) if (game.condit==IHDOCKED && (game.options & OPTION_BASE)) /* protected by base -- back off ! */ motion -= game.skill*(2.0-square(Rand())); } -#ifdef DEBUG - if (game.idebug) { - proutn("MOTION = %1.2f", motion); - proutn(" FORCES = %1.2f", forces); - } -#endif + if (idebug) + proutn("=== MOTION = %1.2f, FORCES = %1.2f, ", motion, forces); /* don't move if no motion */ if (motion==0) return; /* Limit motion according to skill */ @@ -170,11 +166,9 @@ static void movebaddy(coord com, int loccom, int ienm) if (motion > 0 && nsteps > mdist) nsteps = mdist; /* don't overshoot */ if (nsteps > QUADSIZE) nsteps = QUADSIZE; /* This shouldn't be necessary */ if (nsteps < 1) nsteps = 1; /* This shouldn't be necessary */ -#ifdef DEBUG - if (game.idebug) { - prout("NSTEPS = %d", nsteps); + if (idebug) { + proutn("NSTEPS = %d:", nsteps); } -#endif /* Compute preferred values of delta X and Y */ mx = game.sector.x - com.x; my = game.sector.y - com.y; @@ -185,11 +179,8 @@ static void movebaddy(coord com, int loccom, int ienm) next = com; /* main move loop */ for (ll = 0; ll < nsteps; ll++) { -#ifdef DEBUG - if (game.idebug) { - prout("%d", ll+1); - } -#endif + if (idebug) + proutn(" %d", ll+1); /* Check if preferred position available */ lookx = next.x + mx; looky = next.y + my; @@ -234,14 +225,14 @@ static void movebaddy(coord com, int loccom, int ienm) if (success) { next.x = lookx; next.y = looky; -#ifdef DEBUG - if (game.idebug) { - prout(cramlc(neither, next)); - } -#endif + if (idebug) + proutn(cramlc(neither, next)); } else break; /* done early */ + } + if (idebug) + prout(""); /* Put commander in place within same quadrant */ game.quad[com.x][com.y] = IHDOT; game.quad[next.x][next.y] = ienm; @@ -267,9 +258,7 @@ void movcom(void) coord w; int i; -#ifdef DEBUG - if (game.idebug) prout("MOVCOM"); -#endif + if (idebug) prout("== MOVCOM"); /* Figure out which Klingon is the commander (or Supercommander) and do move */ @@ -302,7 +291,7 @@ void movcom(void) sortkl(); } -static int movescom(coord iq, int flag, int *ipage) +static bool movescom(coord iq, int flag, int *ipage) { int i; @@ -315,7 +304,7 @@ static int movescom(coord iq, int flag, int *ipage) for_starbases(i) if (game.state.baseq[i].x==iq.x && game.state.baseq[i].y==iq.y) return 1; } - if (game.justin && !game.iscate) return 1; + if (game.justin && !game.iscate) return true; /* do the move */ game.state.galaxy[game.state.kscmdr.x][game.state.kscmdr.y].klingons--; game.state.kscmdr = iq; @@ -358,7 +347,7 @@ static int movescom(coord iq, int flag, int *ipage) break; } } - return 0; /* looks good! */ + return false; /* looks good! */ } void scom(int *ipage) @@ -368,9 +357,8 @@ void scom(int *ipage) int basetbl[BASEMAX+1]; double bdist[BASEMAX+1]; int flag; -#ifdef DEBUG - if (game.idebug) prout("SCOM"); -#endif + + if (idebug) prout("== SCOM"); /* Decide on being active or passive */ flag = ((NKILLC+NKILLK)/(game.state.date+0.01-game.indate) < 0.1*game.skill*(game.skill+1.0) || @@ -497,7 +485,7 @@ void scom(int *ipage) /* attack the base */ if (flag) return; /* no, don't attack base! */ game.iseenit = 0; - game.isatb=1; + game.isatb = 1; schedule(FSCDBAS, 1.0 +2.0*Rand()); if (is_scheduled(FCDBAS)) postpone(FSCDBAS, scheduled(FCDBAS)-game.state.date); @@ -522,9 +510,7 @@ void scom(int *ipage) } /* Check for intelligence report */ if ( -#ifdef DEBUG - game.idebug==0 && -#endif + !idebug && (Rand() > 0.2 || (game.damage[DRADIO] > 0.0 && game.condit != IHDOCKED) || !game.state.galaxy[game.state.kscmdr.x][game.state.kscmdr.y].charted)) diff --git a/src/battle.c b/src/battle.c index aa5f04a..c0dac65 100644 --- a/src/battle.c +++ b/src/battle.c @@ -469,9 +469,7 @@ void attack(int torps_ok) game.iattak = 1; if (game.alldone) return; -#ifdef DEBUG - if (game.idebug) prout("ATTACK!"); -#endif + if (idebug) prout("=== ATTACK!"); if (game.ithere) movetho(); diff --git a/src/events.c b/src/events.c index cd921c5..55de8a5 100644 --- a/src/events.c +++ b/src/events.c @@ -38,9 +38,7 @@ void events(void) int radio_was_broken; struct quadrant *pdest; -#ifdef DEBUG - if (game.idebug) prout("EVENTS"); -#endif + if (idebug) prout("=== EVENTS"); radio_was_broken = (game.damage[DRADIO] != 0.0); @@ -298,7 +296,8 @@ void events(void) proutn(_(" the starbase in ")); proutn(cramlc(quadrant, game.battle)); prout(_(" has been destroyed by")); - if (game.isatb==2) prout(_("the Klingon Super-Commander")); + if (game.isatb == 2) + prout(_("the Klingon Super-Commander")); else prout(_("a Klingon Commander")); game.state.chart[game.battle.x][game.battle.y].starbase = false; } @@ -323,8 +322,8 @@ void events(void) case FSCMOVE: /* Supercommander moves */ schedule(FSCMOVE, 0.2777); if (game.ientesc+istract==0 && - game.isatb!=1 && - (game.iscate!=1 || game.justin==1)) scom(&ipage); + game.isatb != 1 && + (game.iscate != 1 || !game.justin)) scom(&ipage); break; case FDSPROB: /* Move deep space probe */ schedule(FDSPROB, 0.01); @@ -775,15 +774,13 @@ void snova(int insx, int insy) } if (num <=0) break; } -#ifdef DEBUG - if (game.idebug) { - proutn("Super nova here?"); + if (idebug) { + proutn("=== Super nova here?"); if (ja()==1) { nq.x = game.quadrant.x; nq.y = game.quadrant.y; } } -#endif } if (nq.x != game.quadrant.y || nq.y != game.quadrant.y || game.justin != 0) { diff --git a/src/finish.c b/src/finish.c index e191451..d23723b 100644 --- a/src/finish.c +++ b/src/finish.c @@ -71,8 +71,8 @@ void kaboom(void) void finish(FINTYPE ifin) { - int igotit = 0; - game.alldone = 1; + bool igotit = false; + game.alldone = true; skip(3); prout(_("It is stardate %.1f."), game.state.date); skip(1); @@ -84,7 +84,7 @@ void finish(FINTYPE ifin) prout(_("You have smashed the Klingon invasion fleet and saved")); prout(_("the Federation.")); - game.gamewon=1; + game.gamewon = true; if (game.alive) { double badpt; badpt = 5.0*game.state.starkl + game.casual + 10.0*game.state.nplankl + @@ -134,11 +134,7 @@ void finish(FINTYPE ifin) break; } if (game.skill >= SKILL_EXPERT) { - if (game.thawed -#ifdef DEBUG - && !game.idebug -#endif - ) + if (game.thawed && !idebug) prout(_("You cannot get a citation, so...")); else { proutn(_("Do you want your Commodore Emeritus Citation printed? ")); diff --git a/src/io.c b/src/io.c index 48f2e38..7b4855f 100644 --- a/src/io.c +++ b/src/io.c @@ -27,6 +27,8 @@ static void outro(void) (void)endwin(); putchar('\n'); } + if (logfp) + fclose(logfp); } void iostart(void) @@ -203,6 +205,8 @@ void cgetline(char *line, int max) } else { fgets(line, max, stdin); } + if (logfp) + fputs(line, logfp); line[strlen(line)-1] = '\0'; } diff --git a/src/moving.c b/src/moving.c index 42df8c1..5c9f67d 100644 --- a/src/moving.c +++ b/src/moving.c @@ -565,13 +565,11 @@ void warp(int i) } /* Decide if time warp will occur */ if (0.5*game.dist*pow(7.0,game.warpfac-10.0) > Rand()) twarp=1; -#ifdef DEBUG - if (game.idebug &&game.warpfac==10 && twarp==0) { + if (idebug && game.warpfac==10 && twarp==0) { blooey=0; - proutn("Force time warp? "); + proutn("=== Force time warp? "); if (ja()==1) twarp=1; } -#endif if (blooey || twarp) { /* If time warp or engine damage, check path */ /* If it is obstructed, don't do warp or damage */ diff --git a/src/planets.c b/src/planets.c index c1ead06..c1fdb16 100644 --- a/src/planets.c +++ b/src/planets.c @@ -27,7 +27,8 @@ static int consumeTime(void) void preport(void) { - int iknow = 0, i; + bool iknow = false; + int i; skip(1); chew(); prout("Spock- \"Planet report follows, Captain.\""); @@ -35,14 +36,11 @@ void preport(void) for (i = 0; i < game.inplan; i++) { if ((game.state.plnets[i].known != unknown && game.state.plnets[i].crystals != 0) -#ifdef DEBUG - || ( game.idebug && game.state.plnets[i].x !=0) -#endif + || (idebug && game.state.plnets[i].w.x !=0) ) { - iknow = 1; -#ifdef DEBUG - if (game.idebug && game.state.plnets[i].known==unknown) proutn("(Unknown) "); -#endif + iknow = true; + if (idebug && game.state.plnets[i].known==unknown) + proutn("(Unknown) "); proutn(cramlc(quadrant, game.state.plnets[i].w)); proutn(" class "); proutn(classes[game.state.plnets[i].pclass]); diff --git a/src/reports.c b/src/reports.c index 8e4f070..22a7cee 100644 --- a/src/reports.c +++ b/src/reports.c @@ -21,7 +21,7 @@ void attakreport(int curt) } else { if (is_scheduled(FCDBAS)) proutn("Base in %i - %i attacked by C. Alive until %.1f", game.battle.x, game.battle.y, scheduled(FCDBAS)); - if (game.isatb == 1) + if (game.isatb) proutn("Base in %i - %i attacked by S. Alive until %.1f", game.state.kscmdr.x, game.state.kscmdr.y, scheduled(FSCDBAS)); } clreol(); diff --git a/src/setup.c b/src/setup.c index 5c67f23..7b17fbc 100644 --- a/src/setup.c +++ b/src/setup.c @@ -195,9 +195,6 @@ void setup(int needprompt) { int i,j, krem, klumper; int ix, iy; -#ifdef DEBUG - game.idebug = 0; -#endif // Decide how many of everything if (choose(needprompt)) return; // frozen game // Prepare the Enterprise @@ -279,16 +276,14 @@ void setup(int needprompt) double distq = square(ix-game.state.baseq[j].x) + square(iy-game.state.baseq[j].y); if (distq < 6.0*(BASEMAX+1-game.inbase) && Rand() < 0.75) { contflag = true; -#ifdef DEBUG - prout("DEBUG: Abandoning base #%d at %d-%d", i, ix, iy); -#endif + if (idebug) + prout("=== Abandoning base #%d at %d-%d", i, ix, iy); break; } -#ifdef DEBUG else if (distq < 6.0 * (BASEMAX+1-game.inbase)) { - prout("DEBUG: saving base #%d, close to #%d", i, j); + if (idebug) + prout("=== Saving base #%d, close to #%d", i, j); } -#endif } } while (contflag); @@ -300,7 +295,8 @@ void setup(int needprompt) // Position ordinary Klingon Battle Cruisers krem = game.inkling; klumper = 0.25*game.skill*(9.0-game.length)+1.0; - if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant + if (klumper > 9) + klumper = 9; // Can't have more than 9 in quadrant do { double r = Rand(); int klump = (1.0 - r*r)*klumper; @@ -312,20 +308,20 @@ void setup(int needprompt) game.state.galaxy[ix][iy].klingons += klump; } while (krem > 0); // Position Klingon Commander Ships -#ifdef DEBUG +#ifdef ODEBUG klumper = 1; -#endif +#endif /* ODEBUG */ for (i = 1; i <= game.incom; i++) { do { do { /* IF debugging, put commanders by bases, always! */ -#ifdef DEBUG +#ifdef ODEBUG if (game.idebug && klumper <= game.inbase) { ix = game.state.baseq[klumper].x; iy = game.state.baseq[klumper].y; klumper++; } else -#endif +#endif /* ODEBUG */ iran(GALSIZE, &ix, &iy); } while ((!game.state.galaxy[ix][iy].klingons && Rand() < 0.75)|| @@ -508,9 +504,11 @@ bool choose(bool needprompt) prout("\"?"); } setpassword(); -#ifdef DEBUG - if (strcmp(game.passwd, "debug")==0) game.idebug = 1; -#endif + if (strcmp(game.passwd, "debug")==0) { + idebug = true; + logfp = fopen("sst-input.log", "w"); + fputs("=== Debug mode enabled\n", stdout); + } // Use parameters to generate initial values of things game.damfac = 0.5 * game.skill; @@ -687,9 +685,6 @@ void newqad(int shutup) if ((game.skill < SKILL_GOOD && Rand() <= 0.02) || /* Lighten up if skill is low */ (game.skill == SKILL_GOOD && Rand() <= 0.05) || (game.skill > SKILL_GOOD && Rand() <= 0.08) - #ifdef DEBUG - || strcmp(game.passwd, "tholianx")==0 - #endif ) { do { game.tholian.x = Rand() > 0.5 ? QUADSIZE : 1; diff --git a/src/sst.c b/src/sst.c index 080a9a0..0679cbb 100644 --- a/src/sst.c +++ b/src/sst.c @@ -166,10 +166,14 @@ static char line[128], *linep = line; struct game game; coord thing; int iqhere, iqengry; -int iscore, iskill; // Common PLAQ +int iscore, iskill; // Common PLAQ double aaitem; double perdate; char citem[10]; +int seed; // the random-number seed +bool idebug; // debug mode +bool randready; // Has the random-number generator initialized? +FILE *logfp; char *device[NDEVICES] = { "S. R. Sensors", @@ -529,9 +533,7 @@ static void makemoves(void) if (game.ididit) hitme = true; break; case DEBUGCMD: // What do we want for debug??? -#ifdef DEBUG debugme(); -#endif break; case MAYDAY: // Call for help mayday(); @@ -539,9 +541,6 @@ static void makemoves(void) break; case QUIT: game.alldone = 1; // quit the game -#ifdef DEBUG - if (game.idebug) score(); -#endif break; case HELP: helpme(); // get help @@ -550,9 +549,6 @@ static void makemoves(void) commandhook(commands[i].name, false); for (;;) { if (game.alldone) break; // Game has ended -#ifdef DEBUG - if (game.idebug) prout("2500"); -#endif if (game.optime != 0.0) { events(); if (game.alldone) break; // Events did us in @@ -574,6 +570,7 @@ static void makemoves(void) } if (game.alldone) break; } + if (idebug) prout("=== Ending"); } @@ -609,9 +606,6 @@ int main(int argc, char **argv) } for(;;) { /* Play a game */ setwnd(fullscreen_window); -#ifdef DEBUG - prout("INITIAL OPTIONS: %0lx", game.options); -#endif /* DEBUG */ clrscr(); prelim(); setup(line[0] == '\0'); @@ -702,8 +696,17 @@ double expran(double avrage) return -avrage*log(1e-7 + Rand()); } -double Rand(void) { - return rand()/(1.0 + (double)RAND_MAX); +double Rand(void) +{ + if (!randready) { + if (seed == 0) + seed = (unsigned)time(NULL); + if (idebug) + fprintf(logfp, "seed %d\n", seed); + srand(seed); + randready = true; + } + return rand()/(1.0 + (double)RAND_MAX); } void iran(int size, int *i, int *j) @@ -808,14 +811,13 @@ int isit(char *s) } -#ifdef DEBUG void debugme(void) { proutn("Reset levels? "); if (ja() != 0) { - if (energy < game.inenrg) energy = game.inenrg; - shield = game.inshld; - torps = game.intorps; + if (game.energy < game.inenrg) game.energy = game.inenrg; + game.shield = game.inshld; + game.torps = game.intorps; game.lsupres = game.inlsr; } proutn("Reset damage? "); @@ -827,8 +829,8 @@ void debugme(void) } proutn("Toggle game.idebug? "); if (ja() != 0) { - game.idebug = !game.idebug; - if (game.idebug) prout("Debug output ON"); + idebug = !idebug; + if (idebug) prout("Debug output ON"); else prout("Debug output OFF"); } proutn("Cause selective damage? "); @@ -876,4 +878,3 @@ void debugme(void) atover(1); } } -#endif diff --git a/src/sst.h b/src/sst.h index eece90c..e584993 100644 --- a/src/sst.h +++ b/src/sst.h @@ -20,8 +20,6 @@ #define min(x, y) ((x)<(y)?(x):(y)) #define max(x, y) ((x)>(y)?(x):(y)) -// #define DEBUG - #define PHASEFAC (2.0) #define GALSIZE (8) #define NINHAB (GALSIZE * GALSIZE / 2) @@ -241,9 +239,6 @@ struct game { iscraft, // =1 if craft on ship, -1 if removed from game isatb, // =1 if super commander is attacking base iscate, // super commander is here -#ifdef DEBUG - idebug, // debug mode -#endif iattak, // attack recursion elimination (was cracks[4]) icrystl, // dilithium crystals aboard tourn, // tournament number @@ -288,6 +283,10 @@ extern int iscore, iskill; // Common PLAQ extern double perdate; extern double aaitem; extern char citem[10]; +extern int seed; +extern bool randready; +extern bool idebug; +extern FILE *logfp; /* the Space Thingy's global state should *not* be saved! */ extern coord thing; -- 2.31.1