De-gotoifying main.c

This commit is contained in:
Eric S. Raymond 2017-06-08 05:01:51 -04:00
parent 8bf31a9c19
commit 30ac7894e7

111
main.c
View file

@ -47,21 +47,22 @@ void sig_handler(int signo)
/* /*
* MAIN PROGRAM * MAIN PROGRAM
*
* Adventure (rev 2: 20 treasures)
*
* History: Original idea & 5-treasure version (adventures) by Willie Crowther
* 15-treasure version (adventure) by Don Woods, April-June 1977
* 20-treasure version (rev 2) by Don Woods, August 1978
* Errata fixed: 78/12/25
* Revived 2017 as Open Advebture.
*/ */
static bool do_command(FILE *); static bool do_command(FILE *);
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
int ch; int ch;
/* Adventure (rev 2: 20 treasures) */
/* History: Original idea & 5-treasure version (adventures) by Willie Crowther
* 15-treasure version (adventure) by Don Woods, April-June 1977
* 20-treasure version (rev 2) by Don Woods, August 1978
* Errata fixed: 78/12/25 */
/* Options. */ /* Options. */
while ((ch = getopt(argc, argv, "l:o")) != EOF) { while ((ch = getopt(argc, argv, "l:o")) != EOF) {
@ -80,7 +81,7 @@ int main(int argc, char *argv[]) {
} }
} }
/* Logical variables: /* Logical variables:
* *
* game.closed says whether we're all the way closed * game.closed says whether we're all the way closed
* game.closng says whether it's closing time yet * game.closng says whether it's closing time yet
@ -90,8 +91,8 @@ int main(int argc, char *argv[]) {
* game.panic says whether he's found out he's trapped in the cave * game.panic says whether he's found out he's trapped in the cave
* game.wzdark says whether the loc he's leaving was dark */ * game.wzdark says whether the loc he's leaving was dark */
/* Initialize our LCG PRNG with parameters tested against Knuth vol. 2. by the original authors */ /* Initialize our LCG PRNG with parameters tested against
* Knuth vol. 2. by the original authors */
lcgstate.a = 1093; lcgstate.a = 1093;
lcgstate.c = 221587; lcgstate.c = 221587;
lcgstate.m = 1048576; lcgstate.m = 1048576;
@ -99,24 +100,22 @@ int main(int argc, char *argv[]) {
long seedval = (long)rand(); long seedval = (long)rand();
set_seed(seedval); set_seed(seedval);
/* Read the database if we have not yet done so */ /* Initialize game variables */
MAP2[1] = 0; MAP2[1] = 0;
if (!game.setup)initialise(); if (!game.setup)
if(game.setup > 0) goto L1; initialise();
/* Unlike earlier versions, adventure is no longer restartable. (This /* Unlike earlier versions, adventure is no longer restartable. (This
* lets us get away with modifying things such as OBJSND(BIRD) without * lets us get away with modifying things such as OBJSND(BIRD) without
* having to be able to undo the changes later.) If a "used" copy is * having to be able to undo the changes later.) If a "used" copy is
* rerun, we come here and tell the player to run a fresh copy. */ * rerun, we come here and tell the player to run a fresh copy. */
if(game.setup <= 0) {
RSPEAK(201); RSPEAK(201);
exit(0); exit(0);
}
/* Start-up, dwarf stuff */ /* Start-up, dwarf stuff */
game.setup= -1;
L1: game.setup= -1;
I=0;
game.zzword=RNDVOC(3,0); game.zzword=RNDVOC(3,0);
game.novice=YES(stdin, 65,1,0); game.novice=YES(stdin, 65,1,0);
game.newloc=1; game.newloc=1;
@ -127,10 +126,12 @@ L1: game.setup= -1;
if (logfp) if (logfp)
fprintf(logfp, "seed %ld\n", seedval); fprintf(logfp, "seed %ld\n", seedval);
/* interpret commands ubtil EOF or interrupt */
for (;;) { for (;;) {
if (!do_command(stdin)) if (!do_command(stdin))
break; break;
} }
/* show score and exit */
score(1); score(1);
} }
@ -152,49 +153,57 @@ static bool fallback_handler(char *buf)
static bool do_command(FILE *cmdin) { static bool do_command(FILE *cmdin) {
long LL, KQ, VERB, KK, K2, V1, V2; long LL, KQ, VERB, KK, K2, V1, V2;
long obj; long obj, i;
long TK[21]; long TK[21];
static long IGO = 0; static long IGO = 0;
/* Can't leave cave once it's closing (except by main office). */ /* Can't leave cave once it's closing (except by main office). */
if(OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
if(!OUTSID(game.newloc) || game.newloc == 0 || !game.closng) goto L71;
RSPEAK(130); RSPEAK(130);
game.newloc=game.loc; game.newloc=game.loc;
if(!game.panic)game.clock2=15; if(!game.panic)game.clock2=15;
game.panic=true; game.panic=true;
}
/* See if a dwarf has seen him and has come from where he wants to go. If so, /* See if a dwarf has seen him and has come from where he
* the dwarf's blocking his way. If coming from place forbidden to pirate * wants to go. If so, the dwarf's blocking his way. If
* (dwarves rooted in place) let him get out (and attacked). */ * coming from place forbidden to pirate (dwarves rooted in
* place) let him get out (and attacked). */
L71: if(game.newloc == game.loc || FORCED(game.loc) || CNDBIT(game.loc,3)) goto L74; if(game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc,3)) {
/* 73 */ for (I=1; I<=NDWARVES-1; I++) { for (i=1; i<=NDWARVES-1; i++) {
if(game.odloc[I] != game.newloc || !game.dseen[I]) goto L73; if(game.odloc[i] == game.newloc && game.dseen[i]) {
game.newloc=game.loc; game.newloc=game.loc;
RSPEAK(2); RSPEAK(2);
goto L74; break;
L73: /*etc*/ ; }
} /* end loop */ }
L74: game.loc=game.newloc; }
game.loc=game.newloc;
/* Dwarf stuff. See earlier comments for description of variables. Remember /* Dwarf stuff. See earlier comments for description of
* sixth dwarf is pirate and is thus very different except for motion rules. */ * variables. Remember sixth dwarf is pirate and is thus
* very different except for motion rules. */
/* First off, don't let the dwarves follow him into a pit or a wall. Activate /* First off, don't let the dwarves follow him into a pit or
* the whole mess the first time he gets as far as the hall of mists (loc 15). * a wall. Activate the whole mess the first time he gets as
* If game.newloc is forbidden to pirate (in particular, if it's beyond the troll * far as the hall of mists (loc 15). If game.newloc is
* bridge), bypass dwarf stuff. That way pirate can't steal return toll, and * forbidden to pirate (in particular, if it's beyond the
* dwarves can't meet the bear. Also means dwarves won't follow him into dead * troll bridge), bypass dwarf stuff. That way pirate can't
* end in maze, but c'est la vie. They'll wait for him outside the dead end. */ * steal return toll, and dwarves can't meet the bear. Also
* means dwarves won't follow him into dead end in maze, but
if(game.loc == 0 || FORCED(game.loc) || CNDBIT(game.newloc,3)) goto L2000; * c'est la vie. They'll wait for him outside the dead
if(game.dflag != 0) goto L6000; * end. */
if(INDEEP(game.loc))game.dflag=1; if(game.loc == 0 || FORCED(game.loc) || CNDBIT(game.newloc,3))
goto L2000;
if(game.dflag != 0)
goto L6000;
if(INDEEP(game.loc))
game.dflag=1;
goto L2000; goto L2000;
/* When we encounter the first dwarf, we kill 0, 1, or 2 of the 5 dwarves. If /* When we encounter the first dwarf, we kill 0, 1, or 2 of
* any of the survivors is at loc, replace him with the alternate. */ * the 5 dwarves. If any of the survivors is at loc,
* replace him with the alternate. */
L6000: if(game.dflag != 1) goto L6010; L6000: if(game.dflag != 1) goto L6010;
if(!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc,4) || PCT(85)))) goto L2000; if(!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc,4) || PCT(85)))) goto L2000;