From 300d2461799808a5acb8f6f4a866325c0e50d996 Mon Sep 17 00:00:00 2001 From: NHOrus Date: Fri, 16 Jun 2017 19:44:23 +0300 Subject: [PATCH] Resume from file on startup --- advent.h | 1 + main.c | 23 ++++++++++++++++++----- notes.adoc | 3 +++ saveresume.c | 6 +++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/advent.h b/advent.h index ffde43c..2044cc2 100644 --- a/advent.h +++ b/advent.h @@ -116,6 +116,7 @@ extern long randrange(long); extern void score(enum termination); extern int suspend(FILE *); extern int resume(FILE *); +extern int restore(FILE *); /* * MOD(N,M) = Arithmetic modulus diff --git a/main.c b/main.c index d7ecd77..a6fea59 100644 --- a/main.c +++ b/main.c @@ -43,7 +43,7 @@ long AMBER, AXE, BACK, BATTER, BEAR, BIRD, BLOOD, URN, VASE, VEND, VOLCAN, WATER; long WD1, WD1X, WD2, WD2X; -FILE *logfp; +FILE *logfp = NULL, *rfp = NULL; bool oldstyle = false; bool editline = true; bool prompt = true; @@ -53,9 +53,10 @@ extern int action(FILE *, long, long, long); void sig_handler(int signo) { - if (signo == SIGINT) + if (signo == SIGINT){ if (logfp != NULL) fflush(logfp); + } exit(0); } @@ -79,7 +80,7 @@ int main(int argc, char *argv[]) /* Options. */ - while ((ch = getopt(argc, argv, "l:os")) != EOF) { + while ((ch = getopt(argc, argv, "l:or:s")) != EOF) { switch (ch) { case 'l': logfp = fopen(optarg, "w"); @@ -93,6 +94,14 @@ int main(int argc, char *argv[]) oldstyle = true; editline = prompt = false; break; + case 'r': + rfp = fopen(optarg, "r"); + if (rfp == NULL) + fprintf(stderr, + "advent: can't open save file %s for read\n", + optarg); + signal(SIGINT, sig_handler); + break; case 's': editline = false; break; @@ -125,11 +134,15 @@ int main(int argc, char *argv[]) /* Start-up, dwarf stuff */ game.zzword=RNDVOC(3,0); - game.novice=YES(stdin, WELCOME_YOU,CAVE_NEARBY,NO_MESSAGE); game.newloc = LOC_START; game.loc = LOC_START; game.limit=330; - if (game.novice)game.limit=1000; + if (!rfp){ + game.novice=YES(stdin, WELCOME_YOU,CAVE_NEARBY,NO_MESSAGE); + if (game.novice)game.limit=1000; + } else { + restore(rfp); + } if (logfp) fprintf(logfp, "seed %ld\n", seedval); diff --git a/notes.adoc b/notes.adoc index fd3504a..f1716ba 100644 --- a/notes.adoc +++ b/notes.adoc @@ -73,6 +73,9 @@ necessarily pretty ugly by modern standards. Encryption and checksumming have been discarded - it's pointless to try tamper-proofing saves when everyone has the source code. +A -r command-line option has been added. It is functionally equivalent +to RESTORE command, but faster. + == Translation == The 2.5 code was a mechanical C translation of a FORTRAN original. diff --git a/saveresume.c b/saveresume.c index e6961d2..676fa24 100644 --- a/saveresume.c +++ b/saveresume.c @@ -86,6 +86,10 @@ int resume(FILE *input) linenoiseFree(name); } + return restore(fp); +} + +int restore(FILE* fp){ IGNORE(fread(&save, sizeof(struct save_t), 1, fp)); fclose(fp); if (save.version != VRSION) { @@ -101,4 +105,4 @@ int resume(FILE *input) return GO_TOP; } -/* end */ +/* end */ \ No newline at end of file -- 2.31.1