diff --git a/Source/g_game.c b/Source/g_game.c index 663587df..5e362b5e 100644 --- a/Source/g_game.c +++ b/Source/g_game.c @@ -1053,6 +1053,90 @@ static void G_PlayerFinishLevel(int player) p->bonuscount = 0; } +// [crispy] format time for level statistics +#define TIMESTRSIZE 16 +static void G_FormatLevelStatTime(char *str, int tics) +{ + int exitHours, exitMinutes; + float exitTime, exitSeconds; + + exitTime = (float) tics / 35; + exitHours = exitTime / 3600; + exitTime -= exitHours * 3600; + exitMinutes = exitTime / 60; + exitTime -= exitMinutes * 60; + exitSeconds = exitTime; + + if (exitHours) + { + M_snprintf(str, TIMESTRSIZE, "%d:%02d:%05.2f", + exitHours, exitMinutes, exitSeconds); + } + else + { + M_snprintf(str, TIMESTRSIZE, "%01d:%05.2f", exitMinutes, exitSeconds); + } +} + +// [crispy] Write level statistics upon exit +static void G_WriteLevelStat(void) +{ + static FILE *fstream = NULL; + + int i, playerKills = 0, playerItems = 0, playerSecrets = 0; + + char levelString[8]; + char levelTimeString[TIMESTRSIZE]; + char totalTimeString[TIMESTRSIZE]; + char *decimal; + + if (fstream == NULL) + { + fstream = fopen("levelstat.txt", "w"); + + if (fstream == NULL) + { + fprintf(stderr, "G_WriteLevelStat: Unable to open levelstat.txt for writing!\n"); + return; + } + } + + if (gamemode == commercial) + { + M_snprintf(levelString, sizeof(levelString), "MAP%02d", gamemap); + } + else + { + M_snprintf(levelString, sizeof(levelString), "E%dM%d", + gameepisode, gamemap); + } + + G_FormatLevelStatTime(levelTimeString, leveltime); + G_FormatLevelStatTime(totalTimeString, totalleveltimes + leveltime); + + // Total time ignores centiseconds + decimal = strchr(totalTimeString, '.'); + if (decimal != NULL) + { + *decimal = '\0'; + } + + for (i = 0; i < MAXPLAYERS; i++) + { + if (playeringame[i]) + { + playerKills += players[i].killcount; + playerItems += players[i].itemcount; + playerSecrets += players[i].secretcount; + } + } + + fprintf(fstream, "%s%s - %s (%s) K: %d/%d I: %d/%d S: %d/%d\n", + levelString, (secretexit ? "s" : ""), + levelTimeString, totalTimeString, playerKills, totalkills, + playerItems, totalitems, playerSecrets, totalsecret); +} + // // G_DoCompleted // @@ -1061,6 +1145,12 @@ static void G_DoCompleted(void) { int i; + // [crispy] Write level statistics upon exit + if (M_CheckParm("-levelstat")) + { + G_WriteLevelStat(); + } + gameaction = ga_nothing; for (i=0; i