Mercurial > hg > early-roguelike
diff rogue3/rip.c @ 16:a731f515575e
rogue3: add the option of logging all games to a text file
author | edwarj4 |
---|---|
date | Tue, 10 Nov 2009 22:38:46 +0000 |
parents | 78df7025783b |
children | d67cac79f0f1 |
line wrap: on
line diff
--- a/rogue3/rip.c Sat Oct 31 15:18:51 2009 +0000 +++ b/rogue3/rip.c Tue Nov 10 22:38:46 2009 +0000 @@ -77,6 +77,7 @@ mvaddstr(18, 26, prbuf); move(LINES-1, 0); draw(stdscr); + log(purse, 0, monst); score(purse, 0, monst); /* Make sure all the output gets through ssh and anything else that might be in the way. */ @@ -118,6 +119,31 @@ #endif } +#if 0 /* not necessary */ +/* Same thing, but for the log file. Maybe combine them eventually. */ +/* FIXME you don't know what this does */ +void open_log(void) +{ +#ifdef LOGFILE + if (logf != NULL) { + rewind(logf); + return; + } + + logf = fopen(LOGFILE, "a"); + + if (logf == NULL) + { + fprintf(stderr, "Could not open %s for appending: %s\n", LOGFILE, strerror(errno)); + fflush(stderr); + } +#else + logf == NULL; +#endif + return; +} +#endif + /* VARARGS2 */ void score(int amount, int flags, int monst) @@ -288,6 +314,59 @@ fclose(outf); } +void log(int amount, int flags, int monst) +{ + char logmessage[160], ltemp[80]; + char *killer; + + if (waswizard) + return; +#ifdef LOGFILE + sprintf(logmessage, "%d %d %.20s ", time(NULL), amount, whoami); + if (flags == 0) /* died */ + { + strcat(logmessage, "killed by a"); + killer = killname(monst); + if (*killer == 'a' || *killer == 'e' || *killer == 'i' || + *killer == 'o' || *killer == 'u') + strcat(logmessage, "n "); + else + strcat(logmessage, " "); + strcat(logmessage, killer); + if (max_level > level) + sprintf(ltemp, " on level %d [max %d]\n", level, max_level); + else + sprintf(ltemp, " on level %d\n", level); + strcat(logmessage, ltemp); + } + else if (flags == 1) /* quit */ + { + if (max_level > level) + sprintf(ltemp, "quit on level %d [max %d]\n", level, max_level); + else + sprintf(ltemp, "quit on level %d\n", level); + strcat(logmessage, ltemp); + } + else if (flags == 2) /* won */ + { + sprintf(ltemp, "escaped with the Amulet found on level %d\n", max_level); + strcat(logmessage, ltemp); + } + else + return; + + logf = fopen(LOGFILE, "a"); /* permissions? */ + if (logf == NULL) + return; + /* and write it */ + md_lockfile(logf); + fprintf(logf, "%s", logmessage); + md_unlockfile(logf); + fclose(logf); +#endif + return; +} + void total_winner() { @@ -394,6 +473,7 @@ } mvprintw(c - 'a' + 1, 0," %5d Gold Peices ", oldpurse); refresh(); + log(purse, 2, 0); score(purse, 2, 0); exit(0); }