From 040f19adde85fdd78628eb390a685907804f85ae Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 22 May 2017 21:26:14 -0400 Subject: [PATCH] Fix strange inside-out organization of the input routines. Seems to have nbben a result of FORTRAN not having anything like a stream object that can be passed around. A step towards logging and log replay. --- actions1.c | 6 ++-- actions2.c | 4 +-- init.c | 94 +++++++++++++++++++++++++++++------------------------- main.c | 20 ++++++------ misc.c | 58 ++++++++++++--------------------- misc.h | 5 +-- score.c | 10 +++--- 7 files changed, 93 insertions(+), 104 deletions(-) diff --git a/actions1.c b/actions1.c index 55413be..dd1e8e6 100644 --- a/actions1.c +++ b/actions1.c @@ -30,7 +30,7 @@ L4000: VERB=K; /* Analyse an intransitive verb (ie, no object given yet). */ -L4080: switch (VERB-1) { case 0: goto L8010; case 1: return(8000); case 2: + switch (VERB-1) { case 0: goto L8010; case 1: return(8000); case 2: return(8000); case 3: goto L8040; case 4: return(2009); case 5: goto L8040; case 6: goto L8070; case 7: goto L8080; case 8: return(8000); case 9: return(8000); case 10: return(2011); case 11: goto L9120; case 12: @@ -435,8 +435,8 @@ L8260: SPK=156; /* Read. Print stuff based on objtxt. Oyster (?) is special case. */ -L8270: /* 8275 */ for (I=1; I<=100; I++) { -L8275: if(HERE(I) && OBJTXT[I] != 0 && PROP[I] >= 0)OBJ=OBJ*100+I; +L8270: for (I=1; I<=100; I++) { + if(HERE(I) && OBJTXT[I] != 0 && PROP[I] >= 0)OBJ=OBJ*100+I; } /* end loop */ if(OBJ > 100 || OBJ == 0 || DARK(0)) return(8000); diff --git a/actions2.c b/actions2.c index ae2c2f4..fb25c11 100644 --- a/actions2.c +++ b/actions2.c @@ -187,9 +187,9 @@ L9126: if(OBJ == 0)SPK=44; MOVE(DRAGON,K); MOVE(RUG,K); DROP(BLOOD,K); - /* 9127 */ for (OBJ=1; OBJ<=100; OBJ++) { + for (OBJ=1; OBJ<=100; OBJ++) { if(PLACE[OBJ] == PLAC[DRAGON] || PLACE[OBJ] == FIXD[DRAGON])MOVE(OBJ,K); -L9127: /*etc*/ ; + /*etc*/ ; } /* end loop */ LOC=K; K=NUL; diff --git a/init.c b/init.c index 1e4ec5e..51ab313 100644 --- a/init.c +++ b/init.c @@ -1,9 +1,12 @@ +#include +#include +#include +#include + #include "misc.h" #include "main.h" #include "share.h" #include "funcs.h" -#include -#include /* * Initialisation @@ -178,6 +181,9 @@ void initialise(void) { static int raw_init(void) { printf("Couldn't find adventure.data, using adventure.text...\n"); + FILE *OPENED=fopen("adventure.text","r" /* NOT binary */); + if(!OPENED){printf("Can't read adventure.text!\n"); exit(0);} + /* Clear out the various text-pointer arrays. All text is stored in array * lines; each line is preceded by a word pointing to the next pointer (i.e. * the word following the end of the line). The pointer is negative if this is @@ -210,7 +216,7 @@ L1001: /*etc*/ ; /* Start new data section. sect is the section number. */ -L1002: SECT=GETNUM(1); +L1002: SECT=GETNUM(OPENED); OLDLOC= -1; switch (SECT) { case 0: return(0); case 1: goto L1004; case 2: goto L1004; case 3: goto L1030; case 4: goto L1040; case 5: goto L1004; @@ -225,7 +231,7 @@ L1002: SECT=GETNUM(1); L1004: KK=LINUSE; L1005: LINUSE=KK; - LOC=GETNUM(1); + LOC=GETNUM(OPENED); if(LNLENG >= LNPOSN+70)BUG(0); if(LOC == -1) goto L1002; if(LNLENG < LNPOSN)BUG(1); @@ -275,14 +281,14 @@ L1014: TRNVLS=TRNVLS+1; * this is the last entry for this location. KEY(N) is the index in travel * of the first option at location N. */ -L1030: LOC=GETNUM(1); +L1030: LOC=GETNUM(OPENED); if(LOC == -1) goto L1002; - NEWLOC=GETNUM(0); + NEWLOC=GETNUM(NULL); if(KEY[LOC] != 0) goto L1033; KEY[LOC]=TRVS; goto L1035; L1033: TRVS--; TRAVEL[TRVS]= -TRAVEL[TRVS]; TRVS++; -L1035: L=GETNUM(0); +L1035: L=GETNUM(NULL); if(L == 0) goto L1039; TRAVEL[TRVS]=NEWLOC*1000+L; TRVS=TRVS+1; @@ -299,11 +305,11 @@ L1039: TRVS--; TRAVEL[TRVS]= -TRAVEL[TRVS]; TRVS++; * would make it harder to detect particular input words.) */ L1040: J=10000; - /* 1042 */ for (TABNDX=1; TABNDX<=TABSIZ; TABNDX++) { -L1043: KTAB[TABNDX]=GETNUM(1); + for (TABNDX=1; TABNDX<=TABSIZ; TABNDX++) { + KTAB[TABNDX]=GETNUM(OPENED); if(KTAB[TABNDX] == -1) goto L1002; J=J+7; -L1042: ATAB[TABNDX]=GETTXT(true,true,true,0)+J*J; + ATAB[TABNDX]=GETTXT(true,true,true,0)+J*J; } /* end loop */ BUG(4); @@ -311,24 +317,24 @@ L1042: ATAB[TABNDX]=GETTXT(true,true,true,0)+J*J; * plac contains initial locations of objects. FIXD is -1 for immovable * objects (including the snake), or = second loc for two-placed objects. */ -L1050: OBJ=GETNUM(1); +L1050: OBJ=GETNUM(OPENED); if(OBJ == -1) goto L1002; - PLAC[OBJ]=GETNUM(0); - FIXD[OBJ]=GETNUM(0); + PLAC[OBJ]=GETNUM(NULL); + FIXD[OBJ]=GETNUM(NULL); goto L1050; /* Read default message numbers for action verbs, store in ACTSPK. */ -L1060: VERB=GETNUM(1); +L1060: VERB=GETNUM(OPENED); if(VERB == -1) goto L1002; - ACTSPK[VERB]=GETNUM(0); + ACTSPK[VERB]=GETNUM(NULL); goto L1060; /* Read info about available liquids and other conditions, store in COND. */ -L1070: K=GETNUM(1); +L1070: K=GETNUM(OPENED); if(K == -1) goto L1002; -L1071: LOC=GETNUM(0); +L1071: LOC=GETNUM(NULL); if(LOC == 0) goto L1070; if(CNDBIT(LOC,K)) BUG(8); COND[LOC]=COND[LOC]+SETBIT(K); @@ -337,21 +343,21 @@ L1071: LOC=GETNUM(0); /* Read data for hints. */ L1080: HNTMAX=0; -L1081: K=GETNUM(1); +L1081: K=GETNUM(OPENED); if(K == -1) goto L1002; if(K <= 0 || K > HNTSIZ)BUG(7); - /* 1083 */ for (I=1; I<=4; I++) { -L1083: HINTS[K][I] =GETNUM(0); + for (I=1; I<=4; I++) { + HINTS[K][I] =GETNUM(NULL); } /* end loop */ HNTMAX=(HNTMAX>K ? HNTMAX : K); goto L1081; /* Read the sound/text info, store in OBJSND, OBJTXT, LOCSND. */ -L1090: K=GETNUM(1); +L1090: K=GETNUM(OPENED); if(K == -1) goto L1002; - KK=GETNUM(0); - I=GETNUM(0); + KK=GETNUM(NULL); + I=GETNUM(NULL); if(I == 0) goto L1092; OBJSND[K]=(KK>0 ? KK : 0); OBJTXT[K]=(I>0 ? I : 0); @@ -372,11 +378,11 @@ L1092: LOCSND[K]=KK; * description is printed. Counts modulo 5 unless "LOOK" is used. */ static int finish_init(void) { - /* 1101 */ for (I=1; I<=100; I++) { + for (I=1; I<=100; I++) { PLACE[I]=0; PROP[I]=0; LINK[I]=0; -L1101: {long x = I+100; LINK[x]=0;} + {long x = I+100; LINK[x]=0;} } /* end loop */ /* 1102 */ for (I=1; I<=LOCSIZ; I++) { @@ -402,10 +408,10 @@ L1102: ATLOC[I]=0; L1106: /*etc*/ ; } /* end loop */ - /* 1107 */ for (I=1; I<=100; I++) { + for (I=1; I<=100; I++) { K=101-I; FIXED[K]=FIXD[K]; -L1107: if(PLAC[K] != 0 && FIXD[K] <= 0)DROP(K,PLAC[K]); + if(PLAC[K] != 0 && FIXD[K] <= 0)DROP(K,PLAC[K]); } /* end loop */ /* Treasures, as noted earlier, are objects 50 through MAXTRS (CURRENTLY 79). @@ -415,17 +421,17 @@ L1107: if(PLAC[K] != 0 && FIXD[K] <= 0)DROP(K,PLAC[K]); MAXTRS=79; TALLY=0; - /* 1200 */ for (I=50; I<=MAXTRS; I++) { + for (I=50; I<=MAXTRS; I++) { if(PTEXT[I] != 0)PROP[I]= -1; -L1200: TALLY=TALLY-PROP[I]; + TALLY=TALLY-PROP[I]; } /* end loop */ /* Clear the hint stuff. HINTLC(I) is how long he's been at LOC with cond bit * I. HINTED(I) is true iff hint I has been used. */ - /* 1300 */ for (I=1; I<=HNTMAX; I++) { + for (I=1; I<=HNTMAX; I++) { HINTED[I]=false; -L1300: HINTLC[I]=0; + HINTLC[I]=0; } /* end loop */ /* Define some handy mnemonics. these correspond to object numbers. */ @@ -524,8 +530,8 @@ L1300: HINTLC[I]=0; CHLOC=114; CHLOC2=140; - /* 1700 */ for (I=1; I<=6; I++) { -L1700: DSEEN[I]=false; + for (I=1; I<=6; I++) { + DSEEN[I]=false; } /* end loop */ DFLAG=0; DLOC[1]=19; @@ -569,8 +575,8 @@ L1700: DSEEN[I]=false; KNFLOC=0; DETAIL=0; ABBNUM=5; - /* 1800 */ for (I=0; I<=4; I++) { -L1800: {long x = 2*I+81; if(RTEXT[x] != 0)MAXDIE=I+1;} + for (I=0; I<=4; I++) { + {long x = 2*I+81; if(RTEXT[x] != 0)MAXDIE=I+1;} } /* end loop */ NUMDIE=0; HOLDNG=0; @@ -596,25 +602,25 @@ L1800: {long x = 2*I+81; if(RTEXT[x] != 0)MAXDIE=I+1;} /* Report on amount of arrays actually used, to permit reductions. */ static void report(void) { - /* 1998 */ for (K=1; K<=LOCSIZ; K++) { + for (K=1; K<=LOCSIZ; K++) { KK=LOCSIZ+1-K; if(LTEXT[KK] != 0) goto L1997; -L1998: /*etc*/ ; + /*etc*/ ; } /* end loop */ OBJ=0; -L1997: /* 1996 */ for (K=1; K<=100; K++) { -L1996: if(PTEXT[K] != 0)OBJ=OBJ+1; +L1997: for (K=1; K<=100; K++) { + if(PTEXT[K] != 0)OBJ=OBJ+1; } /* end loop */ - /* 1995 */ for (K=1; K<=TABNDX; K++) { -L1995: if(KTAB[K]/1000 == 2)VERB=KTAB[K]-2000; + for (K=1; K<=TABNDX; K++) { + if(KTAB[K]/1000 == 2)VERB=KTAB[K]-2000; } /* end loop */ - /* 1994 */ for (K=1; K<=RTXSIZ; K++) { + for (K=1; K<=RTXSIZ; K++) { J=RTXSIZ+1-K; if(RTEXT[J] != 0) goto L1993; -L1994: /*etc*/ ; + /*etc*/ ; } /* end loop */ L1993: SETPRM(1,LINUSE,LINSIZ); @@ -695,7 +701,7 @@ static void quick_io(void) { } static void quick_item(W)long *W; { - if(init_reading && fread(W,sizeof(long),1,f) != 1)return; + if(init_reading && fread(W,sizeof(long),1,f) != 1)return; init_cksum = MOD(init_cksum*13+(*W),60000000); if(!init_reading)fwrite(W,sizeof(long),1,f); } diff --git a/main.c b/main.c index 49acfa6..1cbf9e9 100644 --- a/main.c +++ b/main.c @@ -146,13 +146,13 @@ L74: LOC=NEWLOC; L6000: if(DFLAG != 1) goto L6010; if(!INDEEP(LOC) || (PCT(95) && (!CNDBIT(LOC,4) || PCT(85)))) goto L2000; DFLAG=2; - /* 6001 */ for (I=1; I<=2; I++) { + for (I=1; I<=2; I++) { J=1+RAN(5); -L6001: if(PCT(50))DLOC[J]=0; + if(PCT(50))DLOC[J]=0; } /* end loop */ - /* 6002 */ for (I=1; I<=5; I++) { + for (I=1; I<=5; I++) { if(DLOC[I] == LOC)DLOC[I]=DALTLC; -L6002: ODLOC[I]=DLOC[I]; + ODLOC[I]=DLOC[I]; } /* end loop */ RSPEAK(3); DROP(AXE,LOC); @@ -347,8 +347,8 @@ L2602: /*etc*/ ; L2603: if(!CLOSED) goto L2605; if(PROP[OYSTER] < 0 && TOTING(OYSTER))PSPEAK(OYSTER,1); - /* 2604 */ for (I=1; I<=100; I++) { -L2604: if(TOTING(I) && PROP[I] < 0)PROP[I]= -1-PROP[I]; + for (I=1; I<=100; I++) { + if(TOTING(I) && PROP[I] < 0)PROP[I]= -1-PROP[I]; } /* end loop */ L2605: WZDARK=DARK(0); if(KNFLOC > 0 && KNFLOC != LOC)KNFLOC=0; @@ -766,9 +766,9 @@ L41000: if(TALLY == 1 && PROP[JADE] < 0) goto L40010; L10000: PROP[GRATE]=0; PROP[FISSUR]=0; - /* 10010 */ for (I=1; I<=6; I++) { + for (I=1; I<=6; I++) { DSEEN[I]=false; -L10010: DLOC[I]=0; + DLOC[I]=0; } /* end loop */ MOVE(TROLL,0); MOVE(TROLL+100,0); @@ -822,8 +822,8 @@ L11000: PROP[BOTTLE]=PUT(BOTTLE,115,1); PROP[MIRROR]=PUT(MIRROR,115,0); FIXED[MIRROR]=116; - /* 11010 */ for (I=1; I<=100; I++) { -L11010: if(TOTING(I))DSTROY(I); + for (I=1; I<=100; I++) { + if(TOTING(I))DSTROY(I); } /* end loop */ RSPEAK(132); diff --git a/misc.c b/misc.c index baa0d56..8f0a2b2 100644 --- a/misc.c +++ b/misc.c @@ -182,7 +182,8 @@ long JUNK; L10: if(BLKLIN)TYPE0(); - MAPLIN(false); + MAPLIN(stdin); + if(feof(stdin)) score(1); WORD1=GETTXT(true,true,true,0); if(BLKLIN && WORD1 < 0) goto L10; WORD1X=GETTXT(false,true,true,0); @@ -241,7 +242,7 @@ L20: YES=false; #define YES(X,Y,Z) fYES(X,Y,Z) #undef GETNUM -long fGETNUM(long K) { +long fGETNUM(FILE *source) { long DIGIT, GETNUM, SIGN; /* Obtain the next integer from an input line. If K>0, we first read a @@ -251,7 +252,7 @@ long DIGIT, GETNUM, SIGN; * character (not a digit, hyphen, or blank), we return 0. */ - if(K != 0)MAPLIN(K > 0); + if(source != NULL)MAPLIN(source); GETNUM=0; L10: if(LNPOSN > LNLENG)return(GETNUM); if(INLINE[LNPOSN] != 0) goto L20; @@ -853,11 +854,11 @@ void fBUG(long NUM) { #define BUG(NUM) fBUG(NUM) #undef MAPLIN -void fMAPLIN(long FIL) { -long I, VAL; static FILE *OPENED = NULL; +void fMAPLIN(FILE *OPENED) { +long I, VAL; -/* Read a line of input, either from a file (if FIL=true) or from the - * keyboard, translate the chars to integers in the range 0-126 and store +/* Read a line of input, from the specified input source, + * translate the chars to integers in the range 0-126 and store * them in the common array "INLINE". Integer values are as follows: * 0 = space [ASCII CODE 40 octal, 32 decimal] * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal] @@ -879,42 +880,23 @@ long I, VAL; static FILE *OPENED = NULL; * This procedure may use the map1,map2 arrays to maintain static data for * the mapping. MAP2(1) is set to 0 when the program starts * and is not changed thereafter unless the routines on this page choose - * to do so. - * - * Note that MAPLIN is expected to open the file the first time it is - * asked to read a line from it. that is, there is no other place where - * the data file is opened. */ - + * to do so. */ if(MAP2[1] == 0)MPINIT(); - if(FIL) goto L15; - IGNORE(fgets(INLINE+1, sizeof(INLINE)-1, stdin)); - if(feof(stdin)) score(1); - goto L20; - -L15: if(!OPENED){ - OPENED=fopen("adventure.text","r" /* NOT binary */); - if(!OPENED){printf("Can't read adventure.text!\n"); exit(0);} - } - IGNORE(fgets(INLINE+1,sizeof(INLINE)-1,OPENED)); - -L20: LNLENG=0; - for (I=1; I<=sizeof(INLINE) && INLINE[I]!=0; I++) { - VAL=INLINE[I]+1; - INLINE[I]=MAP1[VAL]; - if(INLINE[I] != 0)LNLENG=I; - } /* end loop */ - LNPOSN=1; - if(FIL && LNLENG == 0) goto L15; -/* Above is to get around an F40 compiler bug wherein it reads a blank - * line whenever a crlf is broken across a record boundary. */ - return; + IGNORE(fgets(INLINE+1,sizeof(INLINE)-1,OPENED)); + if (!feof(OPENED)) { + LNLENG=0; + for (I=1; I<=sizeof(INLINE) && INLINE[I]!=0; I++) { + VAL=INLINE[I]+1; + INLINE[I]=MAP1[VAL]; + if(INLINE[I] != 0)LNLENG=I; + } /* end loop */ + LNPOSN=1; + } } - - - #define MAPLIN(FIL) fMAPLIN(FIL) + #undef TYPE void fTYPE(void) { long I, VAL; diff --git a/misc.h b/misc.h index 7d7958d..19612b0 100644 --- a/misc.h +++ b/misc.h @@ -1,4 +1,5 @@ #include +#include /* b is not needed for POSIX but harmless */ #define READ_MODE "rb" @@ -16,7 +17,7 @@ extern void fGETIN(long*,long*,long*,long*); #define GETIN(WORD1,WORD1X,WORD2,WORD2X) fGETIN(&WORD1,&WORD1X,&WORD2,&WORD2X) extern long fYES(long,long,long); #define YES(X,Y,Z) fYES(X,Y,Z) -extern long fGETNUM(long); +extern long fGETNUM(FILE *); #define GETNUM(K) fGETNUM(K) extern long fGETTXT(long,long,long,long); #define GETTXT(SKIP,ONEWRD,UPPER,HASH) fGETTXT(SKIP,ONEWRD,UPPER,HASH) @@ -60,7 +61,7 @@ extern long fRNDVOC(long,long); #define RNDVOC(CHAR,FORCE) fRNDVOC(CHAR,FORCE) extern void fBUG(long); #define BUG(NUM) fBUG(NUM) -extern void fMAPLIN(long); +extern void fMAPLIN(FILE *); #define MAPLIN(FIL) fMAPLIN(FIL) extern void fTYPE(); #define TYPE() fTYPE() diff --git a/score.c b/score.c index e0a5be1..103982e 100644 --- a/score.c +++ b/score.c @@ -29,7 +29,7 @@ void score(long MODE) { * Points can also be deducted for using hints or too many turns, or for * saving intermediate positions. */ -L20000: SCORE=0; + SCORE=0; MXSCOR=0; /* First tally up the treasures. must be in building and not broken. @@ -80,8 +80,8 @@ L20020: MXSCOR=MXSCOR+45; /* Deduct for hints/turns/saves. Hints < 4 are special; see database desc. */ - /* 20030 */ for (I=1; I<=HNTMAX; I++) { -L20030: if(HINTED[I])SCORE=SCORE-HINTS[I][2]; + for (I=1; I<=HNTMAX; I++) { + if(HINTED[I])SCORE=SCORE-HINTS[I][2]; } /* end loop */ if(NOVICE)SCORE=SCORE-5; if(CLSHNT)SCORE=SCORE-10; @@ -98,9 +98,9 @@ L20030: if(HINTED[I])SCORE=SCORE-HINTS[I][2]; SETPRM(1,SCORE,MXSCOR); SETPRM(3,TURNS,TURNS); RSPEAK(262); - /* 20200 */ for (I=1; I<=CLSSES; I++) { + for (I=1; I<=CLSSES; I++) { if(CVAL[I] >= SCORE) goto L20210; -L20200: /*etc*/ ; + /*etc*/ ; } /* end loop */ SPK=265; goto L25000; -- 2.31.1