Resume from file on startup

This commit is contained in:
NHOrus 2017-06-16 19:44:23 +03:00
parent 51a88bec4d
commit 300d246179
4 changed files with 27 additions and 6 deletions

View file

@ -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

23
main.c
View file

@ -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);

View file

@ -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.

View file

@ -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 */