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