-
-
- TSTBIT=MOD(MASK/SETBIT(BIT),2) != 0;
- return(TSTBIT);
-}
-
-
-
-#define TSTBIT(MASK,BIT) fTSTBIT(MASK,BIT)
-#undef RAN
-long fRAN(long RANGE) {
-static long D, R = 0, RAN, T;
-
-/* Since the ran function in LIB40 seems to be a real lose, we'll use one of
- * our own. It's been run through many of the tests in Knuth vol. 2 and
- * seems to be quite reliable. RAN returns a value uniformly selected
- * between 0 and range-1. */
-
-
- D=1;
- if(R != 0 && RANGE >= 0) goto L1;
- DATIME(D,T);
- R=MOD(T+5,1048576L);
- D=1000+MOD(D,1000);
-L1: for (T=1; T<=D; T++) {
- R=MOD(R*1093L+221587L,1048576L);
- } /* end loop */
- RAN=(RANGE*R)/1048576;
- return(RAN);
-}
-
-
-
-#define RAN(RANGE) fRAN(RANGE)
-#undef RNDVOC
-long fRNDVOC(long CHAR, long FORCE) {
-long DIV, I, J, RNDVOC;
-
-/* Searches the vocabulary for a word whose second character is char, and
- * changes that word such that each of the other four characters is a
- * random letter. If force is non-zero, it is used as the new word.
- * Returns the new word. */
-
-
- RNDVOC=FORCE;
- if(RNDVOC != 0) goto L3;
- for (I=1; I<=5; I++) {
- J=11+RAN(26);
- if(I == 2)J=CHAR;
- RNDVOC=RNDVOC*64+J;
- } /* end loop */
-L3: J=10000;
- DIV=64L*64L*64L;
- for (I=1; I<=TABSIZ; I++) {
- J=J+7;
- if(MOD((ATAB[I]-J*J)/DIV,64L) == CHAR) goto L8;
- /*etc*/ ;
- } /* end loop */
- BUG(5);
-
-L8: ATAB[I]=RNDVOC+J*J;
- return(RNDVOC);
-}
-
-
-
-#define RNDVOC(CHAR,FORCE) fRNDVOC(CHAR,FORCE)
-#undef BUG
-void fBUG(long NUM) {
-
-/* The following conditions are currently considered fatal bugs. Numbers < 20
- * are detected while reading the database; the others occur at "run time".
- * 0 Message line > 70 characters
- * 1 Null line in message
- * 2 Too many words of messages
- * 3 Too many travel options
- * 4 Too many vocabulary words
- * 5 Required vocabulary word not found
- * 6 Too many RTEXT messages
- * 7 Too many hints
- * 8 Location has cond bit being set twice
- * 9 Invalid section number in database
- * 10 Too many locations
- * 11 Too many class or turn messages
- * 20 Special travel (500>L>300) exceeds goto list
- * 21 Ran off end of vocabulary table
- * 22 Vocabulary type (N/1000) not between 0 and 3
- * 23 Intransitive action verb exceeds goto list
- * 24 Transitive action verb exceeds goto list
- * 25 Conditional travel entry with no alternative
- * 26 Location has no travel entries
- * 27 Hint number exceeds goto list
- * 28 Invalid month returned by date function
- * 29 Too many parameters given to SETPRM */
-
- printf("Fatal error %ld. See source code for interpretation.\n",
- NUM);
- exit(0);
-}
-
-
-
-
-
-/* Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
-
-#define BUG(NUM) fBUG(NUM)
-#undef MAPLIN
-void fMAPLIN(FILE *OPENED) {
-long I, VAL;
-
-/* 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]
- * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
- * 11-36 = upper-case letters
- * 37-62 = lower-case letters
- * 63 = percent (%) [ASCII 45 octal, 37 decimal]
- * 64-73 = digits, 0 through 9
- * Remaining characters can be translated any way that is convenient;
- * The "TYPE" routine below is used to map them back to characters when
- * necessary. The above mappings are required so that certain special
- * characters are known to fit in 6 bits and/or can be easily spotted.
- * Array elements beyond the end of the line should be filled with 0,
- * and LNLENG should be set to the index of the last character.
- *
- * If the data file uses a character other than space (e.g., tab) to
- * separate numbers, that character should also translate to 0.
- *
- * 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. */
-
- if(MAP2[1] == 0)MPINIT();
-
- if (!oldstyle && isatty(0) && !SETUP)
- fputs("> ", stdout);
- IGNORE(fgets(INLINE+1,sizeof(INLINE)-1,OPENED));
- if (feof(OPENED)) {
- if (logfp)
- fclose(logfp);
- } else {
- if (logfp)
- IGNORE(fputs(INLINE+1, logfp));
- else if (!isatty(0))
- IGNORE(fputs(INLINE+1, stdout));
- 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;
-
-/* Type the first "LNLENG" characters stored in inline, mapping them
- * from integers to text per the rules described above. INLINE(I),
- * I=1,LNLENG may be changed by this routine. */
-
-
- if(LNLENG != 0) goto L10;
- printf("\n");
- return;
-
-L10: if(MAP2[1] == 0)MPINIT();
- for (I=1; I<=LNLENG; I++) {
- VAL=INLINE[I];
- {long x = VAL+1; INLINE[I]=MAP2[x];}
- } /* end loop */
- {long x = LNLENG+1; INLINE[x]=0;}
- printf("%s\n",INLINE+1);
- return;
-}
-
-
-
-#define TYPE() fTYPE()
-#undef MPINIT
-void fMPINIT(void) {
-long FIRST, I, J, LAST, VAL;
-static long RUNS[7][2] = {32,34, 39,46, 65,90, 97,122, 37,37, 48,57, 0,126};
-
-
- for (I=1; I<=128; I++) {
- MAP1[I]= -1;
- } /* end loop */
- VAL=0;
- for (I=0; I<7; I++) {
- FIRST=RUNS[I][0];
- LAST=RUNS[I][1];
- /* 22 */ for (J=FIRST; J<=LAST; J++) {
- J++; if(MAP1[J] >= 0) goto L22;
- MAP1[J]=VAL;
- VAL=VAL+1;
-L22: J--;
- } /* end loop */
- /*etc*/ ;
- } /* end loop */
- MAP1[128]=MAP1[10];
-/* For this version, tab (9) maps to space (32), so del (127) uses tab's value */
- MAP1[10]=MAP1[33];
- MAP1[11]=MAP1[33];
-
- for (I=0; I<=126; I++) {
- I++; VAL=MAP1[I]+1; I--;
- MAP2[VAL]=I*('B'-'A');
- if(I >= 64)MAP2[VAL]=(I-64)*('B'-'A')+'@';
- } /* end loop */
-
- return;
-}
-
-
-
-#define MPINIT() fMPINIT()
-#undef SAVEIO
-void fSAVEIO(long OP, long IN, long ARR[]) {
-static FILE *F; char NAME[50];
-
-/* If OP=0, ask for a file name and open a file. (If IN=true, the file is for
- * input, else output.) If OP>0, read/write ARR from/into the previously-opened
- * file. (ARR is a 250-integer array.) If OP<0, finish reading/writing the
- * file. (Finishing writing can be a no-op if a "stop" statement does it
- * automatically. Finishing reading can be a no-op as long as a subsequent
- * SAVEIO(0,false,X) will still work.) If you can catch errors (e.g., no such
- * file) and try again, great. DEC F40 can't. */
-
-
- {long ifvar; ifvar=(OP); switch (ifvar<0? -1 : ifvar>0? 1 : 0) { case -1:
- goto L10; case 0: goto L20; case 1: goto L30; }}
-
-L10: fclose(F);
- return;
-
-L20: printf("\nFile name: ");
- IGNORE(fgets(NAME, sizeof(NAME), stdin));
- F=fopen(NAME,(IN ? READ_MODE : WRITE_MODE));
- if(F == NULL) {printf("Can't open file, try again.\n"); goto L20;}
- return;
-
-L30: if(IN)IGNORE(fread(ARR,sizeof(long),250,F));
- if(!IN)fwrite(ARR,sizeof(long),250,F);
- return;
-
-}
-
-
-
-long fIABS(N)long N; {return(N<0? -N : N);}
-long fMOD(N,M)long N, M; {return(N%M);}
+{
+ return (mask & (1 << bit)) != 0;
+}
+
+void set_seed(long seedval)
+/* Set the LCG seed */
+{
+ game.lcg_x = (unsigned long) seedval % game.lcg_m;
+}
+
+unsigned long get_next_lcg_value(void)
+/* Return the LCG's current value, and then iterate it. */
+{
+ unsigned long old_x = game.lcg_x;
+ game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
+ return old_x;
+}
+
+long randrange(long range)
+/* Return a random integer from [0, range). */
+{
+ return range * get_next_lcg_value() / game.lcg_m;
+}
+
+long RNDVOC(long second, long force)
+/* Searches the vocabulary ATAB for a word whose second character is
+ * char, and changes that word such that each of the other four
+ * characters is a random letter. If force is non-zero, it is used
+ * as the new word. Returns the new word. */
+{
+ long rnd = force;
+
+ if (rnd == 0) {
+ for (int i = 1; i <= 5; i++) {
+ long j = 11 + randrange(26);
+ if (i == 2)
+ j = second;
+ rnd = rnd * 64 + j;
+ }
+ }
+
+ long div = 64L * 64L * 64L;
+ for (int i = 1; i <= TABSIZ; i++) {
+ if (MOD(ATAB[i] / div, 64L) == second) {
+ ATAB[i] = rnd;
+ break;
+ }
+ }
+
+ return rnd;
+}
+
+
+/* Machine dependent routines (MAPLIN, SAVEIO) */
+
+bool MAPLIN(FILE *fp)
+{
+ bool eof;
+
+ /* Read a line of input, from the specified input source.
+ * This logic is complicated partly because it has to serve
+ * several cases with different requirements and partly because
+ * of a quirk in linenoise().
+ *
+ * The quirk shows up when you paste a test log from the clipboard
+ * to the program's command prompt. While fgets (as expected)
+ * consumes it a line at a time, linenoise() returns the first
+ * line and discards the rest. Thus, there needs to be an
+ * editline (-s) option to fall back to fgets while still
+ * prompting. Note that linenoise does behave properly when
+ * fed redirected stdin.
+ *
+ * The logging is a bit of a mess because there are two distinct cases
+ * in which you want to echo commands. One is when shipping them to
+ * a log under the -l option, in which case you want to suppress
+ * prompt generation (so test logs are unadorned command sequences).
+ * On the other hand, if you redirected stdin and are feeding the program
+ * a logfile, you *do* want prompt generation - it makes checkfiles
+ * easier to read when the commands are marked by a preceding prompt.
+ */
+ do {
+ if (!editline) {
+ if (prompt)
+ fputs("> ", stdout);
+ IGNORE(fgets(rawbuf, sizeof(rawbuf) - 1, fp));
+ eof = (feof(fp));
+ } else {
+ char *cp = linenoise("> ");
+ eof = (cp == NULL);
+ if (!eof) {
+ strncpy(rawbuf, cp, sizeof(rawbuf) - 1);
+ linenoiseHistoryAdd(rawbuf);
+ strncat(rawbuf, "\n", sizeof(rawbuf) - strlen(rawbuf) - 1);
+ linenoiseFree(cp);
+ }
+ }
+ } while
+ (!eof && rawbuf[0] == '#');
+ if (eof) {
+ if (logfp && fp == stdin)
+ fclose(logfp);
+ return false;
+ } else {
+ FILE *efp = NULL;
+ if (logfp && fp == stdin)
+ efp = logfp;
+ else if (!isatty(0))
+ efp = stdout;
+ if (efp != NULL) {
+ if (prompt && efp == stdout)
+ fputs("> ", efp);
+ IGNORE(fputs(rawbuf, efp));
+ }
+ strcpy(INLINE + 1, rawbuf);
+ /* 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]
+ * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
+ * 11-36 = upper-case letters
+ * 37-62 = lower-case letters
+ * 63 = percent (%) [ASCII 45 octal, 37 decimal]
+ * 64-73 = digits, 0 through 9
+ * Remaining characters can be translated any way that is convenient;
+ * The above mappings are required so that certain special
+ * characters are known to fit in 6 bits and/or can be easily spotted.
+ * Array elements beyond the end of the line should be filled with 0,
+ * and LNLENG should be set to the index of the last character.
+ *
+ * If the data file uses a character other than space (e.g., tab) to
+ * separate numbers, that character should also translate to 0.
+ *
+ * This procedure may use the map1,map2 arrays to maintain
+ * static data for he mapping. MAP2(1) is set to 0 when the
+ * program starts and is not changed thereafter unless the
+ * routines in this module choose to do so. */
+ LNLENG = 0;
+ for (long i = 1; i <= (long)sizeof(INLINE) && INLINE[i] != 0; i++) {
+ long val = INLINE[i];
+ INLINE[i] = ascii_to_advent[val];
+ if (INLINE[i] != 0)
+ LNLENG = i;
+ }
+ LNPOSN = 1;
+ return true;
+ }
+}
+
+void DATIME(long* d, long* t)
+{
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ *d = (long) tv.tv_sec;
+ *t = (long) tv.tv_usec;
+}
+
+void bug(enum bugtype num, const char *error_string)
+{
+ fprintf(stderr, "Fatal error %d, %s.\n", num, error_string);
+ exit(EXIT_FAILURE);
+}
+
+/* end */