From 942f187b68be75931f4bd35015d74d284ef802c4 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 1 Nov 2004 03:48:44 +0000 Subject: [PATCH] printfs have been banished. All outputnow goes through prout/proutn/prouts. These are now prinf-like variadic functions. The point of this maneuver was to make the output capturable by a different front end, for example a curses-based front end. --- ai.c | 7 ++--- battle.c | 46 ++++++++------------------- finish.c | 39 +++++++++++------------ moving.c | 12 +++---- planets.c | 2 +- reports.c | 93 +++++++++++++++++++++++++++---------------------------- setup.c | 40 ++++++++++++++---------- sst.c | 44 ++++++++++++++------------ sst.h | 7 ++--- 9 files changed, 138 insertions(+), 152 deletions(-) diff --git a/ai.c b/ai.c index d2223ca..37f266c 100644 --- a/ai.c +++ b/ai.c @@ -173,9 +173,7 @@ static void movebaddy(int comx, int comy, int loccom, int ienm) { if (nsteps < 1) nsteps = 1; /* This shouldn't be necessary */ #ifdef DEBUG if (idebug) { - proutn("NSTEPS = "); - crami(nsteps, 1); - skip(1); + prout("NSTEPS = %d", nsteps); } #endif /* Compute preferred values of delta X and Y */ @@ -192,8 +190,7 @@ static void movebaddy(int comx, int comy, int loccom, int ienm) { for (ll = 1; ll <= nsteps; ll++) { #ifdef DEBUG if (idebug) { - crami(ll,2); - skip(1); + prout("%d", ll); } #endif /* Check if preferred position available */ diff --git a/battle.c b/battle.c index 590d08c..d476711 100644 --- a/battle.c +++ b/battle.c @@ -152,9 +152,7 @@ void ram(int ibumpd, int ienm, int ix, int iy) { crmshp(); prout(" heavily damaged."); icas = 10.0+20.0*Rand(); - proutn("***Sickbay reports "); - crami(icas, 1); - prout(" casualties."); + prout("***Sickbay reports %d casualties", icas); casual += icas; for (l=1; l <= NDEVICES; l++) { if (l == DDRAY) continue; // Don't damage deathray @@ -532,7 +530,7 @@ void attack(int k) { if (hit > hitmax) hitmax = hit; hittot += hit; fry(hit); - printf("Hit %g energy %g\n", hit, energy); + prout("Hit %g energy %g", hit, energy); energy -= hit; } if (energy <= 0) { @@ -551,25 +549,18 @@ void attack(int k) { else { /* Print message if starship suffered hit(s) */ skip(1); - proutn("Energy left "); - cramf(energy, 0, 2); - proutn(" shields "); - if (shldup) proutn("up, "); - else if (game.damage[DSHIELD] == 0) proutn("down, "); + proutn("Energy left %2d shields ", (int)energy); + if (shldup) proutn("up "); + else if (game.damage[DSHIELD] == 0) proutn("down "); else proutn("damaged, "); } - crami(percent, 1); - proutn("% torpedoes left "); - crami(torps, 1); - skip(1); + prout("%d%%, torpedoes left %d", percent, torps); /* Check if anyone was hurt */ if (hitmax >= 200 || hittot >= 500) { int icas= hittot*Rand()*0.015; if (icas >= 2) { skip(1); - proutn("Mc Coy- \"Sickbay to bridge. We suffered "); - crami(icas, 1); - prout(" casualties"); + prout("Mc Coy- \"Sickbay to bridge. We suffered %d casualties", icas); prout(" in that last attack.\""); casual += icas; } @@ -705,8 +696,7 @@ void photon(void) { return; } else if (key == IHEOL) { - crami(torps,1); - prout(" torpedoes left."); + prout("%d torpedoes left.", torps); proutn("Number of torpedoes to fire- "); key = scan(); } @@ -758,9 +748,7 @@ void photon(void) { if (i == 1 && key == IHEOL) { /* prompt for each one */ for (i = 1; i <= n; i++) { - proutn("Target sector for torpedo number"); - crami(i, 2); - proutn("- "); + proutn("Target sector for torpedo number %d- ", i); key = scan(); if (key != IHREAL) { huh(); @@ -787,9 +775,7 @@ void photon(void) { /* misfire! */ r = (Rand()+1.2) * r; if (n>1) { - prouts("***TORPEDO NUMBER"); - crami(i, 2); - prouts(" MISFIRES."); + prouts("***TORPEDO NUMBER %d MISFIRES", i); } else prouts("***TORPEDO MISFIRES."); skip(1); @@ -805,9 +791,7 @@ void photon(void) { if (shldup != 0 || condit == IHDOCKED) r *= 1.0 + 0.0001*shield; if (n != 1) { skip(1); - proutn("Track for torpedo number"); - crami(i, 2); - proutn("- "); + proutn("Track for torpedo number %d- ", i); } else { skip(1); @@ -862,9 +846,7 @@ static int checkshctrl(double rpow) { if (icas) { skip(1); prout("McCoy to bridge- \"Severe radiation burns, Jim."); - proutn(" "); - crami(icas, 1); - prout(" casualties so far.\""); + prout(" %d casualties so far.\"", icas); casual -= icas; } skip(1); @@ -1083,9 +1065,7 @@ void phasers(void) { int irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))* (1.01+0.05*Rand()) + 1.0; kz = k; - proutn("("); - crami(irec, 1); - proutn(") "); + proutn("(%d)", irec); } proutn("units to fire at "); crmena(0, ienm, 2, ii, jj); diff --git a/finish.c b/finish.c index e0fb221..657bc51 100644 --- a/finish.c +++ b/finish.c @@ -68,11 +68,12 @@ void finish(FINTYPE ifin) { int igotit = 0; alldone = 1; skip(3); - printf("It is stardate %.1f .\n\n", game.state.date); + prout("It is stardate %.1f.", game.state.date); + prout(""); switch (ifin) { case FWON: // Game has been won if (game.state.nromrem != 0) - printf("The remaining %d Romulans surrender to Starfleet Command.\n", + prout("The remaining %d Romulans surrender to Starfleet Command.", game.state.nromrem); prout("You have smashed the Klingon invasion fleet and saved"); @@ -325,40 +326,40 @@ void score(void) { skip(2); prout("Your score --"); if (game.state.nromkl) - printf("%6d Romulans destroyed %5d\n", + prout("%6d Romulans destroyed %5d", game.state.nromkl,20*game.state.nromkl); if (game.state.nromrem) - printf("%6d Romulans captured %5d\n", + prout("%6d Romulans captured %5d", game.state.nromrem, game.state.nromrem); if (game.state.killk) - printf("%6d ordinary Klingons destroyed %5d\n", + prout("%6d ordinary Klingons destroyed %5d", game.state.killk, 10*game.state.killk); if (game.state.killc) - printf("%6d Klingon commanders destroyed %5d\n", + prout("%6d Klingon commanders destroyed %5d", game.state.killc, 50*game.state.killc); if (game.state.nsckill) - printf("%6d Super-Commander destroyed %5d\n", + prout("%6d Super-Commander destroyed %5d", game.state.nsckill, 200*game.state.nsckill); if (ithperd) - printf("%6.2f Klingons per stardate %5d\n", + prout("%6.2f Klingons per stardate %5d", perdate, ithperd); if (game.state.starkl) - printf("%6d stars destroyed by your action %5d\n", + prout("%6d stars destroyed by your action %5d", game.state.starkl, -5*game.state.starkl); if (game.state.nplankl) - printf("%6d planets destroyed by your action %5d\n", + prout("%6d planets destroyed by your action %5d", game.state.nplankl, -10*game.state.nplankl); if (game.state.basekl) - printf("%6d bases destroyed by your action %5d\n", + prout("%6d bases destroyed by your action %5d", game.state.basekl, -100*game.state.basekl); if (nhelp) - printf("%6d calls for help from starbase %5d\n", + prout("%6d calls for help from starbase %5d", nhelp, -45*nhelp); if (casual) - printf("%6d casualties incurred %5d\n", + prout("%6d casualties incurred %5d", casual, -casual); if (klship) - printf("%6d ship(s) lost or destroyed %5d\n", + prout("%6d ship(s) lost or destroyed %5d", klship, -100*klship); if (alive==0) prout("Penalty for getting yourself killed -200"); @@ -372,10 +373,10 @@ void score(void) { case 4: proutn("Expert game "); break; case 5: proutn("Emeritus game"); break; } - printf(" %5d\n", iwon); + prout(" %5d", iwon); } skip(2); - printf("TOTAL SCORE %5d\n", iscore); + prout("TOTAL SCORE %5d", iscore); } void plaque(void) { @@ -388,16 +389,16 @@ void plaque(void) { skip(2); while (fp == NULL) { - printf("File or device name for your plaque:"); + proutn("File or device name for your plaque:"); fgets(winner, 128, stdin); winner[strlen(winner)-1] = '\0'; fp = fopen(winner, "w"); if (fp==NULL) { - printf("Invalid name.\n"); + prout("Invalid name."); } } - printf("Enter name to go on plaque (up to 30 characters):"); + proutn("Enter name to go on plaque (up to 30 characters):"); fgets(winner, 128, stdin); winner[strlen(winner)-1] = '\0'; winner[30] = '\0'; diff --git a/moving.c b/moving.c index 0b9ecbe..d1f5baf 100644 --- a/moving.c +++ b/moving.c @@ -500,10 +500,11 @@ void warp(int i) { prout("We can't do it, Captain. We haven't the energy."); } else { - proutn("We haven't the energy, but we could do it at warp "); - crami(iwarp, 1); - if (shldup) - prout(",\nif you'll lower the shields."); + proutn("We haven't the energy, but we could do it at warp %d", iwarp); + if (shldup) { + prout(","); + prout("if you'll lower the shields."); + } else prout("."); } @@ -837,8 +838,7 @@ void probe(void) { if (key == IHEOL) { /* slow mode, so let Kirk know how many probes there are left */ - crami(nprobes,1); - prout(nprobes==1 ? " probe left." : " probes left."); + prout(nprobes==1 ? "%d probe left." : "%d probes left.", nprobes); proutn("Are you sure you want to fire a probe? "); if (ja()==0) return; } diff --git a/planets.c b/planets.c index 3f9bfc3..51a1747 100644 --- a/planets.c +++ b/planets.c @@ -446,7 +446,7 @@ void deathray(void) { prout(" is still operational.\""); } else { - prout(" has been rendered disfunctional.\""); + prout(" has been rendered nonfunctional.\""); game.damage[DDRAY] = 39.95; } return; diff --git a/reports.c b/reports.c index 436900e..ccb13cd 100644 --- a/reports.c +++ b/reports.c @@ -42,44 +42,41 @@ void report(int f) { case 5: s3="emeritus"; break; default: s3="skilled"; break; } - printf("\nYou %s playing a %s%s %s game.\n", + prout(""); + prout("You %s playing a %s%s %s game.", alldone? "were": "are now", s1, s2, s3); if (skill>3 && thawed && !alldone) prout("No plaque is allowed."); - if (tourn) printf("This is tournament game %d.\n", tourn); - if (f) printf("Your secret password is \"%s\"\n",game.passwd); - printf("%d of %d Klingons have been killed", + if (tourn) prout("This is tournament game %d.", tourn); + if (f) prout("Your secret password is \"%s\"",game.passwd); + proutn("%d of %d Klingons have been killed", game.state.killk+game.state.killc+game.state.nsckill, inkling); - if (game.state.killc) printf(", including %d Commander%s.\n", game.state.killc, game.state.killc==1?"":"s"); + if (game.state.killc) prout(", including %d Commander%s.", game.state.killc, game.state.killc==1?"":"s"); else if (game.state.killk+game.state.nsckill > 0) prout(", but no Commanders."); else prout("."); - if (skill > 2) printf("The Super Commander has %sbeen destroyed.\n", + if (skill > 2) prout("The Super Commander has %sbeen destroyed.", game.state.nscrem?"not ":""); if (game.state.rembase != inbase) { proutn("There "); if (inbase-game.state.rembase==1) proutn("has been 1 base"); else { - proutn("have been "); - crami(inbase-game.state.rembase, 1); - proutn(" bases"); + proutn("have been %d bases", inbase-game.state.rembase); } - proutn(" destroyed, "); - crami(game.state.rembase, 1); - prout(" remaining."); + prout(" destroyed, %d remaining.", game.state.rembase); } - else printf("There are %d bases.\n", inbase); + else prout("There are %d bases.", inbase); if (game.damage[DRADIO] == 0.0 || condit == IHDOCKED || iseenit) { /* Don't report this if not seen and either the radio is dead or not at base! */ attakreport(); iseenit = 1; } - if (casual) printf("%d casualt%s suffered so far.\n", + if (casual) prout("%d casualt%s suffered so far.", casual, casual==1? "y" : "ies"); - if (nhelp) printf("There were %d call%s for help.\n", + if (nhelp) prout("There were %d call%s for help.", nhelp, nhelp==1 ? "" : "s"); if (ship == IHE) { proutn("You have "); - if (nprobes) crami(nprobes,1); + if (nprobes) proutn("%d", nprobes); else proutn("no"); proutn(" deep space probe"); if (nprobes!=1) proutn("s"); @@ -104,7 +101,7 @@ void report(int f) { ai *= 2.0; i++; } - printf("Dilithium crystals have been used %d time%s.\n", + prout("Dilithium crystals have been used %d time%s.", i, i==1? "" : "s"); } } @@ -132,9 +129,9 @@ void lrscan(void) { for (x = quadx-1; x <= quadx+1; x++) { for (y = quady-1; y <= quady+1; y++) { if (x == 0 || x > 8 || y == 0 || y > 8) - printf(" -1"); + proutn(" -1"); else { - printf("%5d", game.state.galaxy[x][y]); + proutn("%5d", game.state.galaxy[x][y]); game.starch[x][y] = game.damage[DRADIO] > 0 ? game.state.galaxy[x][y]+1000 :1; } } @@ -155,7 +152,7 @@ void dreprt(void) { prout(" IN FLIGHT DOCKED"); jdam = TRUE; } - printf(" %16s ", device[i]); + proutn(" %16s ", device[i]); cramf(game.damage[i]+0.05, 8, 2); proutn(" "); cramf(docfac*game.damage[i]+0.005, 8, 2); @@ -194,18 +191,18 @@ void chart(int nn) { prout(" 1 2 3 4 5 6 7 8"); prout(" ----------------------------------------"); - if (nn==0) prout(" -"); + /* if (nn==0) prout(" -"); */ for (i = 1; i <= 8; i++) { - printf("%d -", i); + proutn("%d -", i); for (j = 1; j <= 8; j++) { if (game.starch[i][j] < 0) - printf(" .1."); + proutn(" .1."); else if (game.starch[i][j] == 0) - printf(" ..."); + proutn(" ..."); else if (game.starch[i][j] > 999) - printf("%5d", game.starch[i][j]-1000); + proutn("%5d", game.starch[i][j]-1000); else - printf("%5d", game.state.galaxy[i][j]); + proutn("%5d", game.state.galaxy[i][j]); } prout(" -"); } @@ -247,7 +244,7 @@ void srscan(int l) { break; case 2: // REQUEST while (scan() == IHEOL) - printf("Information desired? "); + proutn("Information desired? "); chew(); for (k = 1; k <= 10; k++) if (strncmp(citem,requests[k],min(2,strlen(citem)))==0) @@ -267,18 +264,18 @@ void srscan(int l) { for (i = 1; i <= 10; i++) { int jj = (k!=0 ? k : i); if (leftside) { - printf("%2d ", i); + proutn("%2d ", i); for (j = 1; j <= 10; j++) { if (goodScan || (abs(i-sectx)<= 1 && abs(j-secty) <= 1)) - printf("%c ",game.quad[i][j]); + proutn("%c ",game.quad[i][j]); else - printf("- "); + proutn("- "); } } if (rightside) { switch (jj) { case 1: - printf(" Stardate %.1f", game.state.date); + proutn(" Stardate %.1f", game.state.date); break; case 2: if (condit != IHDOCKED) newcnd(); @@ -288,50 +285,50 @@ void srscan(int l) { case IHYELLOW: cp = "YELLOW"; break; case IHDOCKED: cp = "DOCKED"; break; } - printf(" Condition %s", cp); + proutn(" Condition %s", cp); break; case 3: - printf(" Position "); + proutn(" Position "); cramlc(0, quadx, quady); - putchar(','); + proutn(","); cramlc(0, sectx, secty); break; case 4: - printf(" Life Support "); + proutn(" Life Support "); if (game.damage[DLIFSUP] != 0.0) { if (condit == IHDOCKED) - printf("DAMAGED, supported by starbase"); + proutn("DAMAGED, supported by starbase"); else - printf("DAMAGED, reserves=%4.2f", lsupres); + proutn("DAMAGED, reserves=%4.2f", lsupres); } else - printf("ACTIVE"); + proutn("ACTIVE"); break; case 5: - printf(" Warp Factor %.1f", warpfac); + proutn(" Warp Factor %.1f", warpfac); break; case 6: - printf(" Energy %.2f", energy); + proutn(" Energy %.2f", energy); break; case 7: - printf(" Torpedoes %d", torps); + proutn(" Torpedoes %d", torps); break; case 8: - printf(" Shields "); + proutn(" Shields "); if (game.damage[DSHIELD] != 0) - printf("DAMAGED,"); + proutn("DAMAGED,"); else if (shldup) - printf("UP,"); + proutn("UP,"); else - printf("DOWN,"); - printf(" %d%% %.1f units", + proutn("DOWN,"); + proutn(" %d%% %.1f units", (int)((100.0*shield)/inshld + 0.5), shield); break; case 9: - printf(" Klingons Left %d", game.state.remkl); + proutn(" Klingons Left %d", game.state.remkl); break; case 10: - printf(" Time Left %.2f", game.state.remtime); + proutn(" Time Left %.2f", game.state.remtime); break; } diff --git a/setup.c b/setup.c index 24c5f6f..747618c 100644 --- a/setup.c +++ b/setup.c @@ -36,6 +36,7 @@ void freeze(int boss) { skip(1); return; } + strcpy(game.magic, SSTMAGIC); fwrite(&game, sizeof(game), 1, fp); fclose(fp); @@ -69,6 +70,11 @@ void thaw(void) { return; } fread(&game, sizeof(game), 1, fp); + if (strcmp(game.magic, SSTMAGIC)) { + prout("Game file format is bad, should begin with " SSTMAGIC); + skip(1); + return; + } fclose(fp); @@ -239,13 +245,13 @@ void setup(void) { if (distq < 6.0*(6-inbase) && Rand() < 0.75) { contflag = TRUE; #ifdef DEBUG - printf("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy); + proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy); #endif break; } #ifdef DEBUG else if (distq < 6.0 * (6-inbase)) { - printf("DEBUG: saving base #%d, close to #%d\n", i, j); + proutn("DEBUG: saving base #%d, close to #%d\n", i, j); } #endif } @@ -334,24 +340,26 @@ void setup(void) { game.state.snap = 0; if (skill == 1) { - printf("It is stardate %d. The Federation is being attacked by\n", + prout("It is stardate %d. The Federation is being attacked by", (int)game.state.date); - printf("a deadly Klingon invasion force. As captain of the United\n" - "Starship U.S.S. Enterprise, it is your mission to seek out\n" - "and destroy this invasion force of %d battle cruisers.\n", + prout("a deadly Klingon invasion force. As captain of the United"); + prout("Starship U.S.S. Enterprise, it is your mission to seek out"); + prout("and destroy this invasion force of %d battle cruisers.", inkling); - printf("You have an initial allotment of %d stardates to complete\n" - "your mission. As you proceed you may be given more time.\n\n" - "You will have %d supporting starbases.\n" - "Starbase locations- ", - (int)intime, inbase); + prout("You have an initial allotment of %d stardates to complete", (int)intime); + prout("your mission. As you proceed you may be given more time."); + prout(""); + prout("You will have %d supporting starbases.", inbase); + proutn("Starbase locations- "); } else { - printf("Stardate %d.\n\n" - "%d Klingons.\nAn unknown number of Romulans\n", - (int)game.state.date, inkling); - if (game.state.nscrem) printf("and one (GULP) Super-Commander.\n"); - printf("%d stardates\n%d starbases in ",(int)intime, inbase); + prout("Stardate %d.", (int)game.state.date); + prout(""); + prout("%d Klingons.", inkling); + prout("An unknown number of Romulans."); + if (game.state.nscrem) prout("and one (GULP) Super-Commander."); + prout("%d stardates.",(int)intime); + proutn("%d starbases in ", inbase); } for (i = 1; i <= inbase; i++) { cramlc(0, game.state.baseqx[i], game.state.baseqy[i]); diff --git a/sst.c b/sst.c index f6db887..af932a0 100644 --- a/sst.c +++ b/sst.c @@ -1,6 +1,7 @@ #define INCLUDED // Define externs here #include "sst.h" #include +#include #ifdef MSDOS #include #endif @@ -373,13 +374,13 @@ int main(int argc, char **argv) { skip(1); if (tourn && alldone) { - printf("Do you want your score recorded?"); + proutn("Do you want your score recorded?"); if (ja()) { chew2(); freeze(FALSE); } } - printf("Do you want to play again?"); + proutn("Do you want to play again?"); if (!ja()) break; } skip(1); @@ -410,10 +411,7 @@ void cramen(int i) { void cramlc(int key, int x, int y) { if (key == 1) proutn(" Quadrant"); else if (key == 2) proutn(" Sector"); - proutn(" "); - crami(x, 1); - proutn(" - "); - crami(y, 1); + proutn(" %d - %d", x, y); } void crmena(int i, int enemy, int key, int x, int y) { @@ -536,12 +534,6 @@ void cramf(double x, int w, int d) { proutn(buf); } -void crami(int i, int w) { - char buf[16]; - sprintf(buf, "%*d", w, i); - proutn(buf); -} - double square(double i) { return i*i; } static void clearscreen(void) { @@ -591,22 +583,34 @@ void skip(int i) { } -void proutn(char *s) { - fputs(s, stdout); +void proutn(char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); } -void prout(char *s) { - proutn(s); - skip(1); +void prout(char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + skip(1); } -void prouts(char *s) { +void prouts(char *fmt, ...) { clock_t endTime; + char *s, buf[BUFSIZ]; /* print slowly! */ - while (*s) { + va_list ap; + va_start(ap, fmt); + vsprintf(buf, fmt, ap); + va_end(ap); + skip(1); + for (s = buf; *s; s++) { endTime = clock() + CLOCKS_PER_SEC*0.05; while (clock() < endTime) ; - putchar(*s++); + putchar(*s); fflush(stdout); } } diff --git a/sst.h b/sst.h index a2c4c92..255e5cc 100644 --- a/sst.h +++ b/sst.h @@ -373,8 +373,8 @@ int scan(void); void chew(void); void chew2(void); void skip(int); -void prout(char *s); -void proutn(char *s); +void prout(char *, ...); +void proutn(char *, ...); void stars(void); void newqad(int); int ja(void); @@ -397,14 +397,13 @@ void timwrp(void); void movcom(void); void torpedo(double, double, int, int, double *); void cramf(double, int, int); -void crami(int, int); void huh(void); void pause(int); void nova(int, int); void snova(int, int); void scom(int *); void hittem(double *); -void prouts(char *); +void prouts(char *, ...); int isit(char *); void preport(void); void orbit(void); -- 2.31.1