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
*
* 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 *);
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
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. */
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.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.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.c = 221587;
lcgstate.m = 1048576;
@ -99,24 +100,22 @@ int main(int argc, char *argv[]) {
long seedval = (long)rand();
set_seed(seedval);
/* Read the database if we have not yet done so */
/* Initialize game variables */
MAP2[1] = 0;
if (!game.setup)initialise();
if(game.setup > 0) goto L1;
if (!game.setup)
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
* 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. */
if(game.setup <= 0) {
RSPEAK(201);
exit(0);
}
/* Start-up, dwarf stuff */
L1: game.setup= -1;
I=0;
/* Start-up, dwarf stuff */
game.setup= -1;
game.zzword=RNDVOC(3,0);
game.novice=YES(stdin, 65,1,0);
game.newloc=1;
@ -127,10 +126,12 @@ L1: game.setup= -1;
if (logfp)
fprintf(logfp, "seed %ld\n", seedval);
/* interpret commands ubtil EOF or interrupt */
for (;;) {
if (!do_command(stdin))
break;
}
/* show score and exit */
score(1);
}
@ -152,49 +153,57 @@ static bool fallback_handler(char *buf)
static bool do_command(FILE *cmdin) {
long LL, KQ, VERB, KK, K2, V1, V2;
long obj;
long obj, i;
long TK[21];
static long IGO = 0;
/* Can't leave cave once it's closing (except by main office). */
if(!OUTSID(game.newloc) || game.newloc == 0 || !game.closng) goto L71;
/* Can't leave cave once it's closing (except by main office). */
if(OUTSID(game.newloc) && game.newloc != 0 && game.closng) {
RSPEAK(130);
game.newloc=game.loc;
if(!game.panic)game.clock2=15;
game.panic=true;
}
/* See if a dwarf has seen him and has come from where he wants to go. If so,
* the dwarf's blocking his way. If 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;
/* 73 */ for (I=1; I<=NDWARVES-1; I++) {
if(game.odloc[I] != game.newloc || !game.dseen[I]) goto L73;
/* See if a dwarf has seen him and has come from where he
* wants to go. If so, the dwarf's blocking his way. If
* coming from place forbidden to pirate (dwarves rooted in
* place) let him get out (and attacked). */
if(game.newloc != game.loc && !FORCED(game.loc) && !CNDBIT(game.loc,3)) {
for (i=1; i<=NDWARVES-1; i++) {
if(game.odloc[i] == game.newloc && game.dseen[i]) {
game.newloc=game.loc;
RSPEAK(2);
goto L74;
L73: /*etc*/ ;
} /* end loop */
L74: game.loc=game.newloc;
break;
}
}
}
game.loc=game.newloc;
/* Dwarf stuff. See earlier comments for description of variables. Remember
* sixth dwarf is pirate and is thus very different except for motion rules. */
/* Dwarf stuff. See earlier comments for description of
* 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
* the whole mess the first time he gets as far as the hall of mists (loc 15).
* If game.newloc is forbidden to pirate (in particular, if it's beyond the troll
* bridge), bypass dwarf stuff. That way pirate can't steal return toll, and
* dwarves can't meet the bear. Also means dwarves won't follow him into dead
* end in maze, but c'est la vie. They'll wait for him outside the dead end. */
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;
/* First off, don't let the dwarves follow him into a pit or
* a wall. Activate the whole mess the first time he gets as
* far as the hall of mists (loc 15). If game.newloc is
* forbidden to pirate (in particular, if it's beyond the
* troll bridge), bypass dwarf stuff. That way pirate can't
* steal return toll, and dwarves can't meet the bear. Also
* means dwarves won't follow him into dead end in maze, but
* c'est la vie. They'll wait for him outside the dead
* end. */
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;
/* When we encounter the first dwarf, we kill 0, 1, or 2 of the 5 dwarves. If
* any of the survivors is at loc, replace him with the alternate. */
/* When we encounter the first dwarf, we kill 0, 1, or 2 of
* the 5 dwarves. If any of the survivors is at loc,
* replace him with the alternate. */
L6000: if(game.dflag != 1) goto L6010;
if(!INDEEP(game.loc) || (PCT(95) && (!CNDBIT(game.loc,4) || PCT(85)))) goto L2000;