Resume from file on startup
This commit is contained in:
parent
51a88bec4d
commit
300d246179
4 changed files with 27 additions and 6 deletions
1
advent.h
1
advent.h
|
@ -116,6 +116,7 @@ extern long randrange(long);
|
||||||
extern void score(enum termination);
|
extern void score(enum termination);
|
||||||
extern int suspend(FILE *);
|
extern int suspend(FILE *);
|
||||||
extern int resume(FILE *);
|
extern int resume(FILE *);
|
||||||
|
extern int restore(FILE *);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* MOD(N,M) = Arithmetic modulus
|
* MOD(N,M) = Arithmetic modulus
|
||||||
|
|
23
main.c
23
main.c
|
@ -43,7 +43,7 @@ long AMBER, AXE, BACK, BATTER, BEAR, BIRD, BLOOD,
|
||||||
URN, VASE, VEND, VOLCAN, WATER;
|
URN, VASE, VEND, VOLCAN, WATER;
|
||||||
long WD1, WD1X, WD2, WD2X;
|
long WD1, WD1X, WD2, WD2X;
|
||||||
|
|
||||||
FILE *logfp;
|
FILE *logfp = NULL, *rfp = NULL;
|
||||||
bool oldstyle = false;
|
bool oldstyle = false;
|
||||||
bool editline = true;
|
bool editline = true;
|
||||||
bool prompt = true;
|
bool prompt = true;
|
||||||
|
@ -53,9 +53,10 @@ extern int action(FILE *, long, long, long);
|
||||||
|
|
||||||
void sig_handler(int signo)
|
void sig_handler(int signo)
|
||||||
{
|
{
|
||||||
if (signo == SIGINT)
|
if (signo == SIGINT){
|
||||||
if (logfp != NULL)
|
if (logfp != NULL)
|
||||||
fflush(logfp);
|
fflush(logfp);
|
||||||
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +80,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
/* Options. */
|
/* Options. */
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "l:os")) != EOF) {
|
while ((ch = getopt(argc, argv, "l:or:s")) != EOF) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'l':
|
case 'l':
|
||||||
logfp = fopen(optarg, "w");
|
logfp = fopen(optarg, "w");
|
||||||
|
@ -93,6 +94,14 @@ int main(int argc, char *argv[])
|
||||||
oldstyle = true;
|
oldstyle = true;
|
||||||
editline = prompt = false;
|
editline = prompt = false;
|
||||||
break;
|
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':
|
case 's':
|
||||||
editline = false;
|
editline = false;
|
||||||
break;
|
break;
|
||||||
|
@ -125,11 +134,15 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
/* Start-up, dwarf stuff */
|
/* Start-up, dwarf stuff */
|
||||||
game.zzword=RNDVOC(3,0);
|
game.zzword=RNDVOC(3,0);
|
||||||
game.novice=YES(stdin, WELCOME_YOU,CAVE_NEARBY,NO_MESSAGE);
|
|
||||||
game.newloc = LOC_START;
|
game.newloc = LOC_START;
|
||||||
game.loc = LOC_START;
|
game.loc = LOC_START;
|
||||||
game.limit=330;
|
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)
|
if (logfp)
|
||||||
fprintf(logfp, "seed %ld\n", seedval);
|
fprintf(logfp, "seed %ld\n", seedval);
|
||||||
|
|
|
@ -73,6 +73,9 @@ necessarily pretty ugly by modern standards. Encryption and
|
||||||
checksumming have been discarded - it's pointless to try
|
checksumming have been discarded - it's pointless to try
|
||||||
tamper-proofing saves when everyone has the source code.
|
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 ==
|
== Translation ==
|
||||||
|
|
||||||
The 2.5 code was a mechanical C translation of a FORTRAN original.
|
The 2.5 code was a mechanical C translation of a FORTRAN original.
|
||||||
|
|
|
@ -86,6 +86,10 @@ int resume(FILE *input)
|
||||||
linenoiseFree(name);
|
linenoiseFree(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return restore(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
int restore(FILE* fp){
|
||||||
IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
|
IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
if (save.version != VRSION) {
|
if (save.version != VRSION) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue