8d5a296d61e2068ae19deafc228c35547da46d35
[open-adventure.git] / misc.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "main.h"
5 #include "misc.h"
6 #include "funcs.h"
7
8 /* hack to ignore GCC Unused Result */
9 #define IGNORE(r) do{if(r){}}while(0)
10
11 /*  I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
12
13 #undef SPEAK
14 void fSPEAK(long N) {
15 long BLANK, CASE, I, K, L, NEG, NPARMS, PARM, PRMTYP, STATE;
16
17 /*  Print the message which starts at LINES(N).  Precede it with a blank line
18  *  unless game.blklin is false. */
19
20
21         if(N == 0)return;
22         BLANK=game.blklin;
23         K=N;
24         NPARMS=1;
25 L10:    L=IABS(LINES[K])-1;
26         K=K+1;
27         LNLENG=0;
28         LNPOSN=1;
29         STATE=0;
30         for (I=K; I<=L; I++) {
31         PUTTXT(LINES[I],STATE,2,I);
32         } /* end loop */
33         LNPOSN=0;
34 L30:    LNPOSN=LNPOSN+1;
35 L32:    if(LNPOSN > LNLENG) goto L40;
36         if(INLINE[LNPOSN] != 63) goto L30;
37         {long x = LNPOSN+1; PRMTYP=INLINE[x];}
38 /*  63 is a "%"; the next character determine the type of parameter:  1 (!) =
39  *  suppress message completely, 29 (S) = NULL If PARM=1, else 'S' (optional
40  *  plural ending), 33 (W) = word (two 30-bit values) with trailing spaces
41  *  suppressed, 22 (L) or 31 (U) = word but map to lower/upper case, 13 (C) =
42  *  word in lower case with first letter capitalised, 30 (T) = text ending
43  *  with a word of -1, 65-73 (1-9) = number using that many characters,
44  *  12 (B) = variable number of blanks. */
45         if(PRMTYP == 1)return;
46         if(PRMTYP == 29) goto L320;
47         if(PRMTYP == 30) goto L340;
48         if(PRMTYP == 12) goto L360;
49         if(PRMTYP == 33 || PRMTYP == 22 || PRMTYP == 31 || PRMTYP == 13) goto
50                 L380;
51         PRMTYP=PRMTYP-64;
52         if(PRMTYP < 1 || PRMTYP > 9) goto L30;
53         SHFTXT(LNPOSN+2,PRMTYP-2);
54         LNPOSN=LNPOSN+PRMTYP;
55         PARM=IABS(PARMS[NPARMS]);
56         NEG=0;
57         if(PARMS[NPARMS] < 0)NEG=9;
58         /* 390 */ for (I=1; I<=PRMTYP; I++) {
59         LNPOSN=LNPOSN-1;
60         INLINE[LNPOSN]=MOD(PARM,10)+64;
61         if(I == 1 || PARM != 0) goto L390;
62         INLINE[LNPOSN]=NEG;
63         NEG=0;
64 L390:   PARM=PARM/10;
65         } /* end loop */
66         LNPOSN=LNPOSN+PRMTYP;
67 L395:   NPARMS=NPARMS+1;
68          goto L32;
69
70 L320:   SHFTXT(LNPOSN+2,-1);
71         INLINE[LNPOSN]=55;
72         if(PARMS[NPARMS] == 1)SHFTXT(LNPOSN+1,-1);
73          goto L395;
74
75 L340:   SHFTXT(LNPOSN+2,-2);
76         STATE=0;
77         CASE=2;
78 L345:   if(PARMS[NPARMS] < 0) goto L395;
79         {long x = NPARMS+1; if(PARMS[x] < 0)CASE=0;}
80         PUTTXT(PARMS[NPARMS],STATE,CASE,0);
81         NPARMS=NPARMS+1;
82          goto L345;
83
84 L360:   PRMTYP=PARMS[NPARMS];
85         SHFTXT(LNPOSN+2,PRMTYP-2);
86         if(PRMTYP == 0) goto L395;
87         for (I=1; I<=PRMTYP; I++) {
88         INLINE[LNPOSN]=0;
89         LNPOSN=LNPOSN+1;
90         } /* end loop */
91          goto L395;
92
93 L380:   SHFTXT(LNPOSN+2,-2);
94         STATE=0;
95         CASE= -1;
96         if(PRMTYP == 31)CASE=1;
97         if(PRMTYP == 33)CASE=0;
98         I=LNPOSN;
99         PUTTXT(PARMS[NPARMS],STATE,CASE,0);
100         {long x = NPARMS+1; PUTTXT(PARMS[x],STATE,CASE,0);}
101         if(PRMTYP == 13 && INLINE[I] >= 37 && INLINE[I] <=
102                 62)INLINE[I]=INLINE[I]-26;
103         NPARMS=NPARMS+2;
104          goto L32;
105
106 L40:    if(BLANK)TYPE0();
107         BLANK=false;
108         TYPE();
109         K=L+1;
110         if(LINES[K] >= 0) goto L10;
111         return;
112 }
113
114
115
116 #define SPEAK(N) fSPEAK(N)
117 #undef PSPEAK
118 void fPSPEAK(long MSG,long SKIP) {
119 long I, M;
120
121 /*  Find the skip+1st message from msg and print it.  MSG should be the index of
122  *  the inventory message for object.  (INVEN+N+1 message is PROP=N message). */
123
124
125         M=PTEXT[MSG];
126         if(SKIP < 0) goto L9;
127         for (I=0; I<=SKIP; I++) {
128 L1:     M=IABS(LINES[M]);
129         if(LINES[M] >= 0) goto L1;
130         /*etc*/ ;
131         } /* end loop */
132 L9:     SPEAK(M);
133         return;
134 }
135
136
137
138 #define PSPEAK(MSG,SKIP) fPSPEAK(MSG,SKIP)
139 #undef RSPEAK
140 void fRSPEAK(long I) {
141 ;
142
143 /*  Print the I-TH "random" message (section 6 of database). */
144
145
146         if(I != 0)SPEAK(RTEXT[I]);
147         return;
148 }
149
150
151
152 #define RSPEAK(I) fRSPEAK(I)
153 #undef SETPRM
154 void fSETPRM(long FIRST, long P1, long P2) {
155 ;
156
157 /*  Stores parameters into the PRMCOM parms array for use by speak.  P1 and P2
158  *  are stored into PARMS(FIRST) and PARMS(FIRST+1). */
159
160
161         if(FIRST >= 25)BUG(29);
162         PARMS[FIRST]=P1;
163         {long x = FIRST+1; PARMS[x]=P2;}
164         return;
165 }
166
167
168
169 #define SETPRM(FIRST,P1,P2) fSETPRM(FIRST,P1,P2)
170 #undef GETIN
171 #define WORD1 (*wORD1)
172 #define WORD1X (*wORD1X)
173 #define WORD2 (*wORD2)
174 #define WORD2X (*wORD2X)
175 void fGETIN(FILE *input, long *wORD1, long *wORD1X, long *wORD2, long *wORD2X) {
176 long JUNK;
177
178 /*  Get a command from the adventurer.  Snarf out the first word, pad it with
179  *  blanks, and return it in WORD1.  Chars 6 thru 10 are returned in WORD1X, in
180  *  case we need to print out the whole word in an error message.  Any number of
181  *  blanks may follow the word.  If a second word appears, it is returned in
182  *  WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
183
184
185 L10:    if(game.blklin)TYPE0();
186         MAPLIN(input);
187         if(input == stdin && feof(stdin)) score(1);
188         WORD1=GETTXT(true,true,true,0);
189         if(game.blklin && WORD1 < 0) goto L10;
190         WORD1X=GETTXT(false,true,true,0);
191 L12:    JUNK=GETTXT(false,true,true,0);
192         if(JUNK > 0) goto L12;
193         WORD2=GETTXT(true,true,true,0);
194         WORD2X=GETTXT(false,true,true,0);
195 L22:    JUNK=GETTXT(false,true,true,0);
196         if(JUNK > 0) goto L22;
197         if(GETTXT(true,true,true,0) <= 0)return;
198         RSPEAK(53);
199          goto L10;
200 }
201
202
203
204 #undef WORD1
205 #undef WORD1X
206 #undef WORD2
207 #undef WORD2X
208 #define GETIN(SRC,WORD1,WORD1X,WORD2,WORD2X) fGETIN(SRC,&WORD1,&WORD1X,&WORD2,&WORD2X)
209 #undef YES
210 long fYES(FILE *input, long X, long Y, long Z) {
211
212 long YES, REPLY, JUNK1, JUNK2, JUNK3;
213
214 /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
215  *  if no, print Z and return false. */
216
217 L1:     RSPEAK(X);
218         GETIN(input, REPLY,JUNK1,JUNK2,JUNK3);
219         if(REPLY == MAKEWD(250519) || REPLY == MAKEWD(25)) goto L10;
220         if(REPLY == MAKEWD(1415) || REPLY == MAKEWD(14)) goto L20;
221         RSPEAK(185);
222          goto L1;
223 L10:    YES=true;
224         RSPEAK(Y);
225         return(YES);
226 L20:    YES=false;
227         RSPEAK(Z);
228         return(YES);
229 }
230
231
232
233
234
235 /*  Line-parsing routines (GETNUM, GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0)
236                 */
237
238 /*  The routines on this page handle all the stuff that would normally be
239  *  taken care of by format statements.  We do it this way instead so that
240  *  we can handle textual data in a machine independent fashion.  All the
241  *  machine dependent i/o stuff is on the following page.  See that page
242  *  for a description of MAPCOM's inline array. */
243
244 #define YES(X,Y,Z) fYES(X,Y,Z)
245 #undef GETNUM
246 long fGETNUM(FILE *source) {
247 long DIGIT, GETNUM, SIGN;
248
249 /*  Obtain the next integer from an input line.  If K>0, we first read a
250  *  new input line from a file; if K<0, we read a line from the keyboard;
251  *  if K=0 we use a line that has already been read (and perhaps partially
252  *  scanned).  If we're at the end of the line or encounter an illegal
253  *  character (not a digit, hyphen, or blank), we return 0. */
254
255
256         if(source != NULL)MAPLIN(source);
257         GETNUM=0;
258 L10:    if(LNPOSN > LNLENG)return(GETNUM);
259         if(INLINE[LNPOSN] != 0) goto L20;
260         LNPOSN=LNPOSN+1;
261          goto L10;
262
263 L20:    SIGN=1;
264         if(INLINE[LNPOSN] != 9) goto L32;
265         SIGN= -1;
266 L30:    LNPOSN=LNPOSN+1;
267 L32:    if(LNPOSN > LNLENG || INLINE[LNPOSN] == 0) goto L42;
268         DIGIT=INLINE[LNPOSN]-64;
269         if(DIGIT < 0 || DIGIT > 9) goto L40;
270         GETNUM=GETNUM*10+DIGIT;
271          goto L30;
272
273 L40:    GETNUM=0;
274 L42:    GETNUM=GETNUM*SIGN;
275         LNPOSN=LNPOSN+1;
276         return(GETNUM);
277 }
278
279
280
281 #define GETNUM(K) fGETNUM(K)
282 #undef GETTXT
283 long fGETTXT(long SKIP,long ONEWRD, long UPPER, long HASH) {
284 long CHAR, GETTXT, I; static long SPLITTING = -1;
285
286 /*  Take characters from an input line and pack them into 30-bit words.
287  *  Skip says to skip leading blanks.  ONEWRD says stop if we come to a
288  *  blank.  UPPER says to map all letters to uppercase.  HASH may be used
289  *  as a parameter for encrypting the text if desired; however, a hash of 0
290  *  should result in unmodified bytes being packed.  If we reach the
291  *  end of the line, the word is filled up with blanks (which encode as 0's).
292  *  If we're already at end of line when GETTXT is called, we return -1. */
293
294         if(LNPOSN != SPLITTING)SPLITTING = -1;
295         GETTXT= -1;
296 L10:    if(LNPOSN > LNLENG)return(GETTXT);
297         if((!SKIP) || INLINE[LNPOSN] != 0) goto L11;
298         LNPOSN=LNPOSN+1;
299          goto L10;
300
301 L11:    GETTXT=0;
302         /* 15 */ for (I=1; I<=5; I++) {
303         GETTXT=GETTXT*64;
304         if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0)) goto L15;
305         CHAR=INLINE[LNPOSN];
306         if(CHAR >= 63) goto L12;
307         SPLITTING = -1;
308         if(UPPER && CHAR >= 37)CHAR=CHAR-26;
309         GETTXT=GETTXT+CHAR;
310          goto L14;
311
312 L12:    if(SPLITTING == LNPOSN) goto L13;
313         GETTXT=GETTXT+63;
314         SPLITTING = LNPOSN;
315          goto L15;
316
317 L13:    GETTXT=GETTXT+CHAR-63;
318         SPLITTING = -1;
319 L14:    LNPOSN=LNPOSN+1;
320 L15:    /*etc*/ ;
321         } /* end loop */
322
323         if(HASH)GETTXT=GETTXT+MOD(HASH*13579L+5432L,97531L)*12345L+HASH;
324         return(GETTXT);
325 }
326
327
328
329 #define GETTXT(SKIP,ONEWRD,UPPER,HASH) fGETTXT(SKIP,ONEWRD,UPPER,HASH)
330 #undef MAKEWD
331 long fMAKEWD(long LETTRS) {
332 long I, L, MAKEWD;
333
334 /*  Combine five uppercase letters (represented by pairs of decimal digits
335  *  in lettrs) to form a 30-bit value matching the one that GETTXT would
336  *  return given those characters plus trailing blanks and HASH=0.  Caution:
337  *  lettrs will overflow 31 bits if 5-letter word starts with V-Z.  As a
338  *  kludgey workaround, you can increment a letter by 5 by adding 50 to
339  *  the next pair of digits. */
340
341
342         MAKEWD=0;
343         I=1;
344         L=LETTRS;
345 L10:    MAKEWD=MAKEWD+I*(MOD(L,50)+10);
346         I=I*64;
347         if(MOD(L,100) > 50)MAKEWD=MAKEWD+I*5;
348         L=L/100;
349         if(L != 0) goto L10;
350         I=64L*64L*64L*64L*64L/I;
351         MAKEWD=MAKEWD*I;
352         return(MAKEWD);
353 }
354
355
356
357 #define MAKEWD(LETTRS) fMAKEWD(LETTRS)
358 #undef PUTTXT
359 #define STATE (*sTATE)
360 void fPUTTXT(long WORD, long *sTATE, long CASE, long HASH) {
361 long ALPH1, ALPH2, BYTE, DIV, I, W;
362
363 /*  Unpack the 30-bit value in word to obtain up to 5 integer-encoded chars,
364  *  and store them in inline starting at LNPOSN.  If LNLENG>=LNPOSN, shift
365  *  existing characters to the right to make room.  HASH must be the same
366  *  as it was when gettxt created the 30-bit word.  STATE will be zero when
367  *  puttxt is called with the first of a sequence of words, but is thereafter
368  *  unchanged by the caller, so PUTTXT can use it to maintain state across
369  *  calls.  LNPOSN and LNLENG are incremented by the number of chars stored.
370  *  If CASE=1, all letters are made uppercase; if -1, lowercase; if 0, as is.
371  *  any other value for case is the same as 0 but also causes trailing blanks
372  *  to be included (in anticipation of subsequent additional text). */
373
374
375         ALPH1=13*CASE+24;
376         ALPH2=26*IABS(CASE)+ALPH1;
377         if(IABS(CASE) > 1)ALPH1=ALPH2;
378 /*  ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */
379         DIV=64L*64L*64L*64L;
380         W=WORD;
381         if(HASH)W=W-MOD(HASH*13579L+5432L,97531L)*12345L-HASH;
382         /* 18 */ for (I=1; I<=5; I++) {
383         if(W <= 0 && STATE == 0 && IABS(CASE) <= 1)return;
384         BYTE=W/DIV;
385         if(STATE != 0 || BYTE != 63) goto L12;
386         STATE=63;
387          goto L18;
388
389 L12:    SHFTXT(LNPOSN,1);
390         STATE=STATE+BYTE;
391         if(STATE < ALPH2 && STATE >= ALPH1)STATE=STATE-26*CASE;
392         INLINE[LNPOSN]=STATE;
393         LNPOSN=LNPOSN+1;
394         STATE=0;
395 L18:    W=(W-BYTE*DIV)*64;
396         } /* end loop */
397         return;
398 }
399
400
401
402 #undef STATE
403 #define PUTTXT(WORD,STATE,CASE,HASH) fPUTTXT(WORD,&STATE,CASE,HASH)
404 #undef SHFTXT
405 void fSHFTXT(long FROM, long DELTA) {
406 long I, II, JJ;
407
408 /*  Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG.  Delta can be
409  *  negative.  LNLENG is updated; LNPOSN is not changed. */
410
411
412         if(LNLENG < FROM || DELTA == 0) goto L2;
413         for (I=FROM; I<=LNLENG; I++) {
414         II=I;
415         if(DELTA > 0)II=FROM+LNLENG-I;
416         JJ=II+DELTA;
417         INLINE[JJ]=INLINE[II];
418         } /* end loop */
419 L2:     LNLENG=LNLENG+DELTA;
420         return;
421 }
422
423
424
425 #define SHFTXT(FROM,DELTA) fSHFTXT(FROM,DELTA)
426 #undef TYPE0
427 void fTYPE0() {
428 long TEMP;
429
430 /*  Type a blank line.  This procedure is provided as a convenience for callers
431  *  who otherwise have no use for MAPCOM. */
432
433
434         TEMP=LNLENG;
435         LNLENG=0;
436         TYPE();
437         LNLENG=TEMP;
438         return;
439 }
440
441
442
443 #define TYPE0() fTYPE0()
444
445
446 /*  Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
447
448 #undef SAVWDS
449 void fSAVWDS(long *W1, long *W2, long *W3, long *W4, long *W5, long *W6, long *W7) {
450
451 /*  Write or read 7 variables.  See SAVWRD. */
452
453
454         SAVWRD(0,(*W1));
455         SAVWRD(0,(*W2));
456         SAVWRD(0,(*W3));
457         SAVWRD(0,(*W4));
458         SAVWRD(0,(*W5));
459         SAVWRD(0,(*W6));
460         SAVWRD(0,(*W7));
461         return;
462 }
463
464
465 #define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
466 #undef SAVARR
467 void fSAVARR(long ARR[], long N) {
468 long I;
469
470 /*  Write or read an array of N words.  See SAVWRD. */
471
472
473         for (I=1; I<=N; I++) {
474         SAVWRD(0,ARR[I]);
475         } /* end loop */
476         return;
477 }
478
479
480
481 #define SAVARR(ARR,N) fSAVARR(ARR,N)
482 #undef SAVWRD
483 #define WORD (*wORD)
484 void fSAVWRD(long OP, long *wORD) {
485 static long BUF[250], CKSUM = 0, H1, HASH = 0, N = 0, STATE = 0;
486
487 /*  If OP<0, start writing a file, using word to initialise encryption; save
488  *  word in the file.  If OP>0, start reading a file; read the file to find
489  *  the value with which to decrypt the rest.  In either case, if a file is
490  *  already open, finish writing/reading it and don't start a new one.  If OP=0,
491  *  read/write a single word.  Words are buffered in case that makes for more
492  *  efficient disk use.  We also compute a simple checksum to catch elementary
493  *  poking within the saved file.  When we finish reading/writing the file,
494  *  we store zero into WORD if there's no checksum error, else nonzero. */
495
496
497         if(OP != 0){long ifvar; ifvar=(STATE); switch (ifvar<0? -1 : ifvar>0? 1 :
498                 0) { case -1: goto L30; case 0: goto L10; case 1: goto L30; }}
499         if(STATE == 0)return;
500         if(N == 250)SAVEIO(1,STATE > 0,BUF);
501         N=MOD(N,250)+1;
502         H1=MOD(HASH*1093L+221573L,1048576L);
503         HASH=MOD(H1*1093L+221573L,1048576L);
504         H1=MOD(H1,1234)*765432+MOD(HASH,123);
505         N--;
506         if(STATE > 0)WORD=BUF[N]+H1;
507         BUF[N]=WORD-H1;
508         N++;
509         CKSUM=MOD(CKSUM*13+WORD,1000000000L);
510         return;
511
512 L10:    STATE=OP;
513         SAVEIO(0,STATE > 0,BUF);
514         N=1;
515         if(STATE > 0) goto L15;
516         HASH=MOD(WORD,1048576L);
517         BUF[0]=1234L*5678L-HASH;
518 L13:    CKSUM=BUF[0];
519         return;
520
521 L15:    SAVEIO(1,true,BUF);
522         HASH=MOD(1234L*5678L-BUF[0],1048576L);
523          goto L13;
524
525 L30:    if(N == 250)SAVEIO(1,STATE > 0,BUF);
526         N=MOD(N,250)+1;
527         if(STATE > 0) goto L32;
528         N--; BUF[N]=CKSUM; N++;
529         SAVEIO(1,false,BUF);
530 L32:    N--; WORD=BUF[N]-CKSUM; N++;
531         SAVEIO(-1,STATE > 0,BUF);
532         STATE=0;
533         return;
534 }
535
536
537
538
539
540 /*  Data struc. routines (VOCAB, DSTROY, JUGGLE, MOVE, PUT, CARRY, DROP, ATDWRF)
541                 */
542
543 #undef WORD
544 #define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
545 #undef VOCAB
546 long fVOCAB(long ID, long INIT) {
547 long HASH, I, VOCAB;
548
549 /*  Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
550  *  -1 if not found.  If INIT is positive, this is an initialisation call setting
551  *  up a keyword variable, and not finding it constitutes a bug.  It also means
552  *  that only KTAB values which taken over 1000 equal INIT may be considered.
553  *  (Thus "STEPS", which is a motion verb as well as an object, may be located
554  *  as an object.)  And it also means the KTAB value is taken modulo 1000. */
555
556         HASH=10000;
557         /* 1 */ for (I=1; I<=TABSIZ; I++) {
558         if(KTAB[I] == -1) goto L2;
559         HASH=HASH+7;
560         if(INIT >= 0 && KTAB[I]/1000 != INIT) goto L1;
561         if(ATAB[I] == ID+HASH*HASH) goto L3;
562 L1:     /*etc*/ ;
563         } /* end loop */
564         BUG(21);
565
566 L2:     VOCAB= -1;
567         if(INIT < 0)return(VOCAB);
568         BUG(5);
569
570 L3:     VOCAB=KTAB[I];
571         if(INIT >= 0)VOCAB=MOD(VOCAB,1000);
572         return(VOCAB);
573 }
574
575
576
577 #define VOCAB(ID,INIT) fVOCAB(ID,INIT)
578 #undef DSTROY
579 void fDSTROY(long OBJECT) {
580 ;
581
582 /*  Permanently eliminate "OBJECT" by moving to a non-existent location. */
583
584
585         MOVE(OBJECT,0);
586         return;
587 }
588
589
590
591 #define DSTROY(OBJECT) fDSTROY(OBJECT)
592 #undef JUGGLE
593 void fJUGGLE(long OBJECT) {
594 long I, J;
595
596 /*  Juggle an object by picking it up and putting it down again, the purpose
597  *  being to get the object to the front of the chain of things at its loc. */
598
599
600         I=PLACE[OBJECT];
601         J=FIXED[OBJECT];
602         MOVE(OBJECT,I);
603         MOVE(OBJECT+100,J);
604         return;
605 }
606
607
608
609 #define JUGGLE(OBJECT) fJUGGLE(OBJECT)
610 #undef MOVE
611 void fMOVE(long OBJECT, long WHERE) {
612 long FROM;
613
614 /*  Place any object anywhere by picking it up and dropping it.  May already be
615  *  toting, in which case the carry is a no-op.  Mustn't pick up objects which
616  *  are not at any loc, since carry wants to remove objects from ATLOC chains. */
617
618
619         if(OBJECT > 100) goto L1;
620         FROM=PLACE[OBJECT];
621          goto L2;
622 L1:     {long x = OBJECT-100; FROM=FIXED[x];}
623 L2:     if(FROM > 0 && FROM <= 300)CARRY(OBJECT,FROM);
624         DROP(OBJECT,WHERE);
625         return;
626 }
627
628
629
630 #define MOVE(OBJECT,WHERE) fMOVE(OBJECT,WHERE)
631 #undef PUT
632 long fPUT(long OBJECT, long WHERE, long PVAL) {
633 long PUT;
634
635 /*  PUT is the same as MOVE, except it returns a value used to set up the
636  *  negated PROP values for the repository objects. */
637
638
639         MOVE(OBJECT,WHERE);
640         PUT=(-1)-PVAL;
641         return(PUT);
642 }
643
644
645
646 #define PUT(OBJECT,WHERE,PVAL) fPUT(OBJECT,WHERE,PVAL)
647 #undef CARRY
648 void fCARRY(long OBJECT, long WHERE) {
649 long TEMP;
650
651 /*  Start toting an object, removing it from the list of things at its former
652  *  location.  Incr holdng unless it was already being toted.  If OBJECT>100
653  *  (moving "fixed" second loc), don't change PLACE or game.holdng. */
654
655
656         if(OBJECT > 100) goto L5;
657         if(PLACE[OBJECT] == -1)return;
658         PLACE[OBJECT]= -1;
659         game.holdng=game.holdng+1;
660 L5:     if(ATLOC[WHERE] != OBJECT) goto L6;
661         ATLOC[WHERE]=LINK[OBJECT];
662         return;
663 L6:     TEMP=ATLOC[WHERE];
664 L7:     if(LINK[TEMP] == OBJECT) goto L8;
665         TEMP=LINK[TEMP];
666          goto L7;
667 L8:     LINK[TEMP]=LINK[OBJECT];
668         return;
669 }
670
671
672
673 #define CARRY(OBJECT,WHERE) fCARRY(OBJECT,WHERE)
674 #undef DROP
675 void fDROP(long OBJECT, long WHERE) {
676 ;
677
678 /*  Place an object at a given loc, prefixing it onto the ATLOC list.  Decr
679  *  game.holdng if the object was being toted. */
680
681
682         if(OBJECT > 100) goto L1;
683         if(PLACE[OBJECT] == -1)game.holdng=game.holdng-1;
684         PLACE[OBJECT]=WHERE;
685          goto L2;
686 L1:     {long x = OBJECT-100; FIXED[x]=WHERE;}
687 L2:     if(WHERE <= 0)return;
688         LINK[OBJECT]=ATLOC[WHERE];
689         ATLOC[WHERE]=OBJECT;
690         return;
691 }
692
693
694
695 #define DROP(OBJECT,WHERE) fDROP(OBJECT,WHERE)
696 #undef ATDWRF
697 long fATDWRF(long WHERE) {
698 long ATDWRF, I;
699
700 /*  Return the index of first dwarf at the given location, zero if no dwarf is
701  *  there (or if dwarves not active yet), -1 if all dwarves are dead.  Ignore
702  *  the pirate (6th dwarf). */
703
704
705         ATDWRF=0;
706         if(game.dflag < 2)return(ATDWRF);
707         ATDWRF= -1;
708         for (I=1; I<=5; I++) {
709         if(DLOC[I] == WHERE) goto L2;
710         if(DLOC[I] != 0)ATDWRF=0;
711         } /* end loop */
712         return(ATDWRF);
713
714 L2:     ATDWRF=I;
715         return(ATDWRF);
716 }
717
718
719
720
721 #define ATDWRF(WHERE) fATDWRF(WHERE)
722
723
724
725 /*  Utility routines (SETBIT, TSTBIT, RAN, RNDVOC, BUG) */
726
727 #undef SETBIT
728 long fSETBIT(long BIT) {
729 long I, SETBIT;
730
731 /*  Returns 2**bit for use in constructing bit-masks. */
732
733
734         SETBIT=1;
735         if(BIT <= 0)return(SETBIT);
736         for (I=1; I<=BIT; I++) {
737         SETBIT=SETBIT+SETBIT;
738         } /* end loop */
739         return(SETBIT);
740 }
741
742
743
744 #define SETBIT(BIT) fSETBIT(BIT)
745 #undef TSTBIT
746 long fTSTBIT(long MASK, long BIT) {
747 long TSTBIT;
748
749 /*  Returns true if the specified bit is set in the mask. */
750
751
752         TSTBIT=MOD(MASK/SETBIT(BIT),2) != 0;
753         return(TSTBIT);
754 }
755
756
757
758 #define TSTBIT(MASK,BIT) fTSTBIT(MASK,BIT)
759 #undef RAN
760 long fRAN(long RANGE) {
761 static long D, R = 0, RAN, T;
762
763 /*  Since the ran function in LIB40 seems to be a real lose, we'll use one of
764  *  our own.  It's been run through many of the tests in Knuth vol. 2 and
765  *  seems to be quite reliable.  RAN returns a value uniformly selected
766  *  between 0 and range-1. */
767
768
769         D=1;
770         if(R != 0 && RANGE >= 0) goto L1;
771         DATIME(D,T);
772         R=MOD(T+5,1048576L);
773         D=1000+MOD(D,1000);
774 L1:     for (T=1; T<=D; T++) {
775         R=MOD(R*1093L+221587L,1048576L);
776         } /* end loop */
777         RAN=(RANGE*R)/1048576;
778         return(RAN);
779 }
780
781
782
783 #define RAN(RANGE) fRAN(RANGE)
784 #undef RNDVOC
785 long fRNDVOC(long CHAR, long FORCE) {
786 long DIV, I, J, RNDVOC;
787
788 /*  Searches the vocabulary for a word whose second character is char, and
789  *  changes that word such that each of the other four characters is a
790  *  random letter.  If force is non-zero, it is used as the new word.
791  *  Returns the new word. */
792
793
794         RNDVOC=FORCE;
795         if(RNDVOC != 0) goto L3;
796         for (I=1; I<=5; I++) {
797         J=11+RAN(26);
798         if(I == 2)J=CHAR;
799         RNDVOC=RNDVOC*64+J;
800         } /* end loop */
801 L3:     J=10000;
802         DIV=64L*64L*64L;
803         for (I=1; I<=TABSIZ; I++) {
804         J=J+7;
805         if(MOD((ATAB[I]-J*J)/DIV,64L) == CHAR) goto L8;
806         /*etc*/ ;
807         } /* end loop */
808         BUG(5);
809
810 L8:     ATAB[I]=RNDVOC+J*J;
811         return(RNDVOC);
812 }
813
814
815
816 #define RNDVOC(CHAR,FORCE) fRNDVOC(CHAR,FORCE)
817 #undef BUG
818 void fBUG(long NUM) {
819
820 /*  The following conditions are currently considered fatal bugs.  Numbers < 20
821  *  are detected while reading the database; the others occur at "run time".
822  *      0       Message line > 70 characters
823  *      1       Null line in message
824  *      2       Too many words of messages
825  *      3       Too many travel options
826  *      4       Too many vocabulary words
827  *      5       Required vocabulary word not found
828  *      6       Too many RTEXT messages
829  *      7       Too many hints
830  *      8       Location has cond bit being set twice
831  *      9       Invalid section number in database
832  *      10      Too many locations
833  *      11      Too many class or turn messages
834  *      20      Special travel (500>L>300) exceeds goto list
835  *      21      Ran off end of vocabulary table
836  *      22      Vocabulary type (N/1000) not between 0 and 3
837  *      23      Intransitive action verb exceeds goto list
838  *      24      Transitive action verb exceeds goto list
839  *      25      Conditional travel entry with no alternative
840  *      26      Location has no travel entries
841  *      27      Hint number exceeds goto list
842  *      28      Invalid month returned by date function
843  *      29      Too many parameters given to SETPRM */
844
845         printf("Fatal error %ld.  See source code for interpretation.\n",
846            NUM);
847         exit(0);
848 }
849
850
851
852
853
854 /*  Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
855
856 #define BUG(NUM) fBUG(NUM)
857 #undef MAPLIN
858 void fMAPLIN(FILE *OPENED) {
859 long I, VAL;
860
861 /*  Read a line of input, from the specified input source,
862  *  translate the chars to integers in the range 0-126 and store
863  *  them in the common array "INLINE".  Integer values are as follows:
864  *     0   = space [ASCII CODE 40 octal, 32 decimal]
865  *    1-2  = !" [ASCII 41-42 octal, 33-34 decimal]
866  *    3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
867  *   11-36 = upper-case letters
868  *   37-62 = lower-case letters
869  *    63   = percent (%) [ASCII 45 octal, 37 decimal]
870  *   64-73 = digits, 0 through 9
871  *  Remaining characters can be translated any way that is convenient;
872  *  The "TYPE" routine below is used to map them back to characters when
873  *  necessary.  The above mappings are required so that certain special
874  *  characters are known to fit in 6 bits and/or can be easily spotted.
875  *  Array elements beyond the end of the line should be filled with 0,
876  *  and LNLENG should be set to the index of the last character.
877  *
878  *  If the data file uses a character other than space (e.g., tab) to
879  *  separate numbers, that character should also translate to 0.
880  *
881  *  This procedure may use the map1,map2 arrays to maintain static data for
882  *  the mapping.  MAP2(1) is set to 0 when the program starts
883  *  and is not changed thereafter unless the routines on this page choose
884  *  to do so. */
885
886         if(MAP2[1] == 0)MPINIT();
887
888         if (!oldstyle && isatty(0))
889             fputs("> ", stdout);
890         IGNORE(fgets(INLINE+1,sizeof(INLINE)-1,OPENED));
891         if (feof(OPENED)) {
892                 if (logfp)
893                         fclose(logfp);
894         } else {
895                 if (logfp)
896                         IGNORE(fputs(INLINE+1, logfp));
897                 LNLENG=0;
898                 for (I=1; I<=sizeof(INLINE) && INLINE[I]!=0; I++) {
899                 VAL=INLINE[I]+1;
900                 INLINE[I]=MAP1[VAL];
901                 if(INLINE[I] != 0)LNLENG=I;
902                 } /* end loop */
903                 LNPOSN=1;
904         }
905 }
906 #define MAPLIN(FIL) fMAPLIN(FIL)
907
908 #undef TYPE
909 void fTYPE(void) {
910 long I, VAL;
911
912 /*  Type the first "LNLENG" characters stored in inline, mapping them
913  *  from integers to text per the rules described above.  INLINE(I),
914  *  I=1,LNLENG may be changed by this routine. */
915
916
917         if(LNLENG != 0) goto L10;
918         printf("\n");
919         return;
920
921 L10:    if(MAP2[1] == 0)MPINIT();
922         for (I=1; I<=LNLENG; I++) {
923         VAL=INLINE[I];
924         {long x = VAL+1; INLINE[I]=MAP2[x];}
925         } /* end loop */
926         {long x = LNLENG+1; INLINE[x]=0;}
927         printf("%s\n",INLINE+1);
928         return;
929 }
930
931
932
933 #define TYPE() fTYPE()
934 #undef MPINIT
935 void fMPINIT(void) {
936 long FIRST, I, J, LAST, VAL;
937 static long RUNS[7][2] = {32,34, 39,46, 65,90, 97,122, 37,37, 48,57, 0,126};
938
939
940         for (I=1; I<=128; I++) {
941         MAP1[I]= -1;
942         } /* end loop */
943         VAL=0;
944         for (I=0; I<7; I++) {
945         FIRST=RUNS[I][0];
946         LAST=RUNS[I][1];
947         /* 22 */ for (J=FIRST; J<=LAST; J++) {
948         J++; if(MAP1[J] >= 0) goto L22;
949         MAP1[J]=VAL;
950         VAL=VAL+1;
951 L22:    J--;
952         } /* end loop */
953         /*etc*/ ;
954         } /* end loop */
955         MAP1[128]=MAP1[10];
956 /*  For this version, tab (9) maps to space (32), so del (127) uses tab's value */
957         MAP1[10]=MAP1[33];
958         MAP1[11]=MAP1[33];
959
960         for (I=0; I<=126; I++) {
961         I++; VAL=MAP1[I]+1; I--;
962         MAP2[VAL]=I*('B'-'A');
963         if(I >= 64)MAP2[VAL]=(I-64)*('B'-'A')+'@';
964         } /* end loop */
965
966         return;
967 }
968
969
970
971 #define MPINIT() fMPINIT()
972 #undef SAVEIO
973 void fSAVEIO(long OP, long IN, long ARR[]) {
974 static FILE *F; char NAME[50];
975
976 /*  If OP=0, ask for a file name and open a file.  (If IN=true, the file is for
977  *  input, else output.)  If OP>0, read/write ARR from/into the previously-opened
978  *  file.  (ARR is a 250-integer array.)  If OP<0, finish reading/writing the
979  *  file.  (Finishing writing can be a no-op if a "stop" statement does it
980  *  automatically.  Finishing reading can be a no-op as long as a subsequent
981  *  SAVEIO(0,false,X) will still work.)  If you can catch errors (e.g., no such
982  *  file) and try again, great.  DEC F40 can't. */
983
984
985         {long ifvar; ifvar=(OP); switch (ifvar<0? -1 : ifvar>0? 1 : 0) { case -1:
986                 goto L10; case 0: goto L20; case 1: goto L30; }}
987
988 L10:    fclose(F);
989         return;
990
991 L20:    printf("\nFile name: ");
992         IGNORE(fgets(NAME, sizeof(NAME), stdin));
993         F=fopen(NAME,(IN ? READ_MODE : WRITE_MODE));
994         if(F == NULL) {printf("Can't open file, try again.\n"); goto L20;}
995         return;
996
997 L30:    if(IN)IGNORE(fread(ARR,sizeof(long),250,F));
998         if(!IN)fwrite(ARR,sizeof(long),250,F);
999         return;
1000
1001 }
1002
1003
1004
1005 long fIABS(N)long N; {return(N<0? -N : N);}
1006 long fMOD(N,M)long N, M; {return(N%M);}