Don't build with two -std options.
[open-adventure.git] / misc.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include "main.h"
7 #include "share.h"      /* for SETUP */
8 #include "misc.h"
9 #include "funcs.h"
10 #include "database.h"
11
12 /* hack to ignore GCC Unused Result */
13 #define IGNORE(r) do{if(r){}}while(0)
14
15 /*  I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
16
17 #undef SPEAK
18 void fSPEAK(long N) {
19 long BLANK, CASE, I, K, L, NEG, NPARMS, PARM, PRMTYP, STATE;
20
21 /*  Print the message which starts at LINES(N).  Precede it with a blank line
22  *  unless BLKLIN is false. */
23
24
25         if(N == 0)return;
26         BLANK=BLKLIN;
27         K=N;
28         NPARMS=1;
29 L10:    L=IABS(LINES[K])-1;
30         K=K+1;
31         LNLENG=0;
32         LNPOSN=1;
33         STATE=0;
34         for (I=K; I<=L; I++) {
35         PUTTXT(LINES[I],STATE,2);
36         } /* end loop */
37         LNPOSN=0;
38 L30:    LNPOSN=LNPOSN+1;
39 L32:    if(LNPOSN > LNLENG) goto L40;
40         if(INLINE[LNPOSN] != 63) goto L30;
41         {long x = LNPOSN+1; PRMTYP=INLINE[x];}
42 /*  63 is a "%"; the next character determine the type of parameter:  1 (!) =
43  *  suppress message completely, 29 (S) = NULL If PARM=1, else 'S' (optional
44  *  plural ending), 33 (W) = word (two 30-bit values) with trailing spaces
45  *  suppressed, 22 (L) or 31 (U) = word but map to lower/upper case, 13 (C) =
46  *  word in lower case with first letter capitalised, 30 (T) = text ending
47  *  with a word of -1, 65-73 (1-9) = number using that many characters,
48  *  12 (B) = variable number of blanks. */
49         if(PRMTYP == 1)return;
50         if(PRMTYP == 29) goto L320;
51         if(PRMTYP == 30) goto L340;
52         if(PRMTYP == 12) goto L360;
53         if(PRMTYP == 33 || PRMTYP == 22 || PRMTYP == 31 || PRMTYP == 13) goto
54                 L380;
55         PRMTYP=PRMTYP-64;
56         if(PRMTYP < 1 || PRMTYP > 9) goto L30;
57         SHFTXT(LNPOSN+2,PRMTYP-2);
58         LNPOSN=LNPOSN+PRMTYP;
59         PARM=IABS(PARMS[NPARMS]);
60         NEG=0;
61         if(PARMS[NPARMS] < 0)NEG=9;
62         /* 390 */ for (I=1; I<=PRMTYP; I++) {
63         LNPOSN=LNPOSN-1;
64         INLINE[LNPOSN]=MOD(PARM,10)+64;
65         if(I == 1 || PARM != 0) goto L390;
66         INLINE[LNPOSN]=NEG;
67         NEG=0;
68 L390:   PARM=PARM/10;
69         } /* end loop */
70         LNPOSN=LNPOSN+PRMTYP;
71 L395:   NPARMS=NPARMS+1;
72          goto L32;
73
74 L320:   SHFTXT(LNPOSN+2,-1);
75         INLINE[LNPOSN]=55;
76         if(PARMS[NPARMS] == 1)SHFTXT(LNPOSN+1,-1);
77          goto L395;
78
79 L340:   SHFTXT(LNPOSN+2,-2);
80         STATE=0;
81         CASE=2;
82 L345:   if(PARMS[NPARMS] < 0) goto L395;
83         {long x = NPARMS+1; if(PARMS[x] < 0)CASE=0;}
84         PUTTXT(PARMS[NPARMS],STATE,CASE);
85         NPARMS=NPARMS+1;
86          goto L345;
87
88 L360:   PRMTYP=PARMS[NPARMS];
89         SHFTXT(LNPOSN+2,PRMTYP-2);
90         if(PRMTYP == 0) goto L395;
91         for (I=1; I<=PRMTYP; I++) {
92         INLINE[LNPOSN]=0;
93         LNPOSN=LNPOSN+1;
94         } /* end loop */
95          goto L395;
96
97 L380:   SHFTXT(LNPOSN+2,-2);
98         STATE=0;
99         CASE= -1;
100         if(PRMTYP == 31)CASE=1;
101         if(PRMTYP == 33)CASE=0;
102         I=LNPOSN;
103         PUTTXT(PARMS[NPARMS],STATE,CASE);
104         {long x = NPARMS+1; PUTTXT(PARMS[x],STATE,CASE);}
105         if(PRMTYP == 13 && INLINE[I] >= 37 && INLINE[I] <=
106                 62)INLINE[I]=INLINE[I]-26;
107         NPARMS=NPARMS+2;
108          goto L32;
109
110 L40:    if(BLANK)TYPE0();
111         BLANK=false;
112         TYPE();
113         K=L+1;
114         if(LINES[K] >= 0) goto L10;
115         return;
116 }
117
118
119
120 #define SPEAK(N) fSPEAK(N)
121 #undef PSPEAK
122 void fPSPEAK(long MSG,long SKIP) {
123 long I, M;
124
125 /*  Find the skip+1st message from msg and print it.  MSG should be the index of
126  *  the inventory message for object.  (INVEN+N+1 message is PROP=N message). */
127
128
129         M=PTEXT[MSG];
130         if(SKIP < 0) goto L9;
131         for (I=0; I<=SKIP; I++) {
132 L1:     M=IABS(LINES[M]);
133         if(LINES[M] >= 0) goto L1;
134         /*etc*/ ;
135         } /* end loop */
136 L9:     SPEAK(M);
137         return;
138 }
139
140
141
142 #define PSPEAK(MSG,SKIP) fPSPEAK(MSG,SKIP)
143 #undef RSPEAK
144 void fRSPEAK(long I) {
145 ;
146
147 /*  Print the I-TH "random" message (section 6 of database). */
148
149
150         if(I != 0)SPEAK(RTEXT[I]);
151         return;
152 }
153
154
155
156 #define RSPEAK(I) fRSPEAK(I)
157 #undef SETPRM
158 void fSETPRM(long FIRST, long P1, long P2) {
159 ;
160
161 /*  Stores parameters into the PRMCOM parms array for use by speak.  P1 and P2
162  *  are stored into PARMS(FIRST) and PARMS(FIRST+1). */
163
164
165         if(FIRST >= 25)BUG(29);
166         PARMS[FIRST]=P1;
167         {long x = FIRST+1; PARMS[x]=P2;}
168         return;
169 }
170
171
172
173 #define SETPRM(FIRST,P1,P2) fSETPRM(FIRST,P1,P2)
174 #undef GETIN
175 #define WORD1 (*wORD1)
176 #define WORD1X (*wORD1X)
177 #define WORD2 (*wORD2)
178 #define WORD2X (*wORD2X)
179 bool fGETIN(FILE *input, long *wORD1, long *wORD1X, long *wORD2, long *wORD2X) {
180 long JUNK;
181
182 /*  Get a command from the adventurer.  Snarf out the first word, pad it with
183  *  blanks, and return it in WORD1.  Chars 6 thru 10 are returned in WORD1X, in
184  *  case we need to print out the whole word in an error message.  Any number of
185  *  blanks may follow the word.  If a second word appears, it is returned in
186  *  WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
187
188
189 L10:    if(BLKLIN)TYPE0();
190         MAPLIN(input);
191         if (feof(input))
192             return false;
193         WORD1=GETTXT(true,true,true);
194         if(BLKLIN && WORD1 < 0) goto L10;
195         WORD1X=GETTXT(false,true,true);
196 L12:    JUNK=GETTXT(false,true,true);
197         if(JUNK > 0) goto L12;
198         WORD2=GETTXT(true,true,true);
199         WORD2X=GETTXT(false,true,true);
200 L22:    JUNK=GETTXT(false,true,true);
201         if(JUNK > 0) goto L22;
202         if(GETTXT(true,true,true) <= 0)return true;
203         RSPEAK(53);
204          goto L10;
205 }
206
207
208
209 #undef WORD1
210 #undef WORD1X
211 #undef WORD2
212 #undef WORD2X
213 #define GETIN(SRC,WORD1,WORD1X,WORD2,WORD2X) fGETIN(SRC,&WORD1,&WORD1X,&WORD2,&WORD2X)
214 #undef YES
215 long fYES(FILE *input, long X, long Y, long Z) {
216
217 long YES, REPLY, JUNK1, JUNK2, JUNK3;
218
219 /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
220  *  if no, print Z and return false. */
221
222 L1:     RSPEAK(X);
223         GETIN(input, REPLY,JUNK1,JUNK2,JUNK3);
224         if(REPLY == MAKEWD(250519) || REPLY == MAKEWD(25)) goto L10;
225         if(REPLY == MAKEWD(1415) || REPLY == MAKEWD(14)) goto L20;
226         RSPEAK(185);
227          goto L1;
228 L10:    YES=true;
229         RSPEAK(Y);
230         return(YES);
231 L20:    YES=false;
232         RSPEAK(Z);
233         return(YES);
234 }
235
236
237
238
239
240 /*  Line-parsing routines (GETNUM, GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0)
241                 */
242
243 /*  The routines on this page handle all the stuff that would normally be
244  *  taken care of by format statements.  We do it this way instead so that
245  *  we can handle textual data in a machine independent fashion.  All the
246  *  machine dependent i/o stuff is on the following page.  See that page
247  *  for a description of MAPCOM's inline array. */
248
249 #define YES(X,Y,Z) fYES(X,Y,Z)
250 #undef GETNUM
251 long fGETNUM(FILE *source) {
252 long DIGIT, GETNUM, SIGN;
253
254 /*  Obtain the next integer from an input line.  If K>0, we first read a
255  *  new input line from a file; if K<0, we read a line from the keyboard;
256  *  if K=0 we use a line that has already been read (and perhaps partially
257  *  scanned).  If we're at the end of the line or encounter an illegal
258  *  character (not a digit, hyphen, or blank), we return 0. */
259
260
261         if(source != NULL)MAPLIN(source);
262         GETNUM=0;
263 L10:    if(LNPOSN > LNLENG)return(GETNUM);
264         if(INLINE[LNPOSN] != 0) goto L20;
265         LNPOSN=LNPOSN+1;
266          goto L10;
267
268 L20:    SIGN=1;
269         if(INLINE[LNPOSN] != 9) goto L32;
270         SIGN= -1;
271 L30:    LNPOSN=LNPOSN+1;
272 L32:    if(LNPOSN > LNLENG || INLINE[LNPOSN] == 0) goto L42;
273         DIGIT=INLINE[LNPOSN]-64;
274         if(DIGIT < 0 || DIGIT > 9) goto L40;
275         GETNUM=GETNUM*10+DIGIT;
276          goto L30;
277
278 L40:    GETNUM=0;
279 L42:    GETNUM=GETNUM*SIGN;
280         LNPOSN=LNPOSN+1;
281         return(GETNUM);
282 }
283
284
285
286 #define GETNUM(K) fGETNUM(K)
287 #undef GETTXT
288 long fGETTXT(long SKIP,long ONEWRD, long UPPER) {
289 long CHAR, GETTXT, I; static long SPLITTING = -1;
290
291 /*  Take characters from an input line and pack them into 30-bit words.
292  *  Skip says to skip leading blanks.  ONEWRD says stop if we come to a
293  *  blank.  UPPER says to map all letters to uppercase.  If we reach the
294  *  end of the line, the word is filled up with blanks (which encode as 0's).
295  *  If we're already at end of line when GETTXT is called, we return -1. */
296
297         if(LNPOSN != SPLITTING)SPLITTING = -1;
298         GETTXT= -1;
299 L10:    if(LNPOSN > LNLENG)return(GETTXT);
300         if((!SKIP) || INLINE[LNPOSN] != 0) goto L11;
301         LNPOSN=LNPOSN+1;
302          goto L10;
303
304 L11:    GETTXT=0;
305         /* 15 */ for (I=1; I<=5; I++) {
306         GETTXT=GETTXT*64;
307         if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0)) goto L15;
308         CHAR=INLINE[LNPOSN];
309         if(CHAR >= 63) goto L12;
310         SPLITTING = -1;
311         if(UPPER && CHAR >= 37)CHAR=CHAR-26;
312         GETTXT=GETTXT+CHAR;
313          goto L14;
314
315 L12:    if(SPLITTING == LNPOSN) goto L13;
316         GETTXT=GETTXT+63;
317         SPLITTING = LNPOSN;
318          goto L15;
319
320 L13:    GETTXT=GETTXT+CHAR-63;
321         SPLITTING = -1;
322 L14:    LNPOSN=LNPOSN+1;
323 L15:    /*etc*/ ;
324         } /* end loop */
325
326         return(GETTXT);
327 }
328
329
330
331 #define GETTXT(SKIP,ONEWRD,UPPER) fGETTXT(SKIP,ONEWRD,UPPER)
332 #undef MAKEWD
333 long fMAKEWD(long LETTRS) {
334 long I, L, MAKEWD;
335
336 /*  Combine five uppercase letters (represented by pairs of decimal digits
337  *  in lettrs) to form a 30-bit value matching the one that GETTXT would
338  *  return given those characters plus trailing blanks.  Caution:
339  *  lettrs will overflow 31 bits if 5-letter word starts with V-Z.  As a
340  *  kludgey workaround, you can increment a letter by 5 by adding 50 to
341  *  the next pair of digits. */
342
343
344         MAKEWD=0;
345         I=1;
346         L=LETTRS;
347 L10:    MAKEWD=MAKEWD+I*(MOD(L,50)+10);
348         I=I*64;
349         if(MOD(L,100) > 50)MAKEWD=MAKEWD+I*5;
350         L=L/100;
351         if(L != 0) goto L10;
352         I=64L*64L*64L*64L*64L/I;
353         MAKEWD=MAKEWD*I;
354         return(MAKEWD);
355 }
356
357
358
359 #define MAKEWD(LETTRS) fMAKEWD(LETTRS)
360 #undef PUTTXT
361 #define STATE (*sTATE)
362 void fPUTTXT(long WORD, long *sTATE, long CASE) {
363 long ALPH1, ALPH2, BYTE, DIV, I, W;
364
365 /*  Unpack the 30-bit value in word to obtain up to 5 integer-encoded chars,
366  *  and store them in inline starting at LNPOSN.  If LNLENG>=LNPOSN, shift
367  *  existing characters to the right to make room.  STATE will be zero when
368  *  puttxt is called with the first of a sequence of words, but is thereafter
369  *  unchanged by the caller, so PUTTXT can use it to maintain state across
370  *  calls.  LNPOSN and LNLENG are incremented by the number of chars stored.
371  *  If CASE=1, all letters are made uppercase; if -1, lowercase; if 0, as is.
372  *  any other value for case is the same as 0 but also causes trailing blanks
373  *  to be included (in anticipation of subsequent additional text). */
374
375
376         ALPH1=13*CASE+24;
377         ALPH2=26*IABS(CASE)+ALPH1;
378         if(IABS(CASE) > 1)ALPH1=ALPH2;
379 /*  ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */
380         DIV=64L*64L*64L*64L;
381         W=WORD;
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) fPUTTXT(WORD,&STATE,CASE)
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 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         /* 1 */ for (I=1; I<=TABSIZ; I++) {
557         if(KTAB[I] == -1) goto L2;
558         if(INIT >= 0 && KTAB[I]/1000 != INIT) goto L1;
559         if(ATAB[I] == ID) 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         for (I=1; I<=5; I++) {
707         if(DLOC[I] == WHERE) goto L2;
708         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, set_seed, get_next_lcg_value,
724  *  randrange, RNDVOC, BUG) */
725
726 #undef SETBIT
727 long fSETBIT(long BIT) {
728 long I, SETBIT;
729
730 /*  Returns 2**bit for use in constructing bit-masks. */
731
732
733         SETBIT=1;
734         if(BIT <= 0)return(SETBIT);
735         for (I=1; I<=BIT; I++) {
736         SETBIT=SETBIT+SETBIT;
737         } /* end loop */
738         return(SETBIT);
739 }
740
741
742
743 #define SETBIT(BIT) fSETBIT(BIT)
744 #undef TSTBIT
745 long fTSTBIT(long MASK, long BIT) {
746 long TSTBIT;
747
748 /*  Returns true if the specified bit is set in the mask. */
749
750
751         TSTBIT=MOD(MASK/SETBIT(BIT),2) != 0;
752         return(TSTBIT);
753 }
754
755
756
757 #define TSTBIT(MASK,BIT) fTSTBIT(MASK,BIT)
758
759 void set_seed(long seedval)
760 {
761   lcgstate.x = (unsigned long) seedval % lcgstate.m;
762 }
763
764 unsigned long get_next_lcg_value(void)
765 {
766   /* Return the LCG's current value, and then iterate it. */
767   unsigned long old_x = lcgstate.x;
768   lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
769   return(old_x);
770 }
771
772 long randrange(long range)
773 {
774   /* Return a random integer from [0, range). */
775   long result = range * get_next_lcg_value() / lcgstate.m;
776   return(result);
777 }
778
779 #undef RNDVOC
780 long fRNDVOC(long CHAR, long FORCE) {
781 long DIV, I, J, RNDVOC;
782
783 /*  Searches the vocabulary for a word whose second character is char, and
784  *  changes that word such that each of the other four characters is a
785  *  random letter.  If force is non-zero, it is used as the new word.
786  *  Returns the new word. */
787
788
789         RNDVOC=FORCE;
790         if(RNDVOC != 0) goto L3;
791         for (I=1; I<=5; I++) {
792         J=11+randrange(26);
793         if(I == 2)J=CHAR;
794         RNDVOC=RNDVOC*64+J;
795         } /* end loop */
796 L3:     J=10000;
797         DIV=64L*64L*64L;
798         for (I=1; I<=TABSIZ; I++) {
799         J=J+7;
800         if(MOD((ATAB[I]-J*J)/DIV,64L) == CHAR) goto L8;
801         /*etc*/ ;
802         } /* end loop */
803         BUG(5);
804
805 L8:     ATAB[I]=RNDVOC+J*J;
806         return(RNDVOC);
807 }
808
809
810
811 #define RNDVOC(CHAR,FORCE) fRNDVOC(CHAR,FORCE)
812 #undef BUG
813 void fBUG(long NUM) {
814
815 /*  The following conditions are currently considered fatal bugs.  Numbers < 20
816  *  are detected while reading the database; the others occur at "run time".
817  *      0       Message line > 70 characters
818  *      1       Null line in message
819  *      2       Too many words of messages
820  *      3       Too many travel options
821  *      4       Too many vocabulary words
822  *      5       Required vocabulary word not found
823  *      6       Too many RTEXT messages
824  *      7       Too many hints
825  *      8       Location has cond bit being set twice
826  *      9       Invalid section number in database
827  *      10      Too many locations
828  *      11      Too many class or turn messages
829  *      20      Special travel (500>L>300) exceeds goto list
830  *      21      Ran off end of vocabulary table
831  *      22      Vocabulary type (N/1000) not between 0 and 3
832  *      23      Intransitive action verb exceeds goto list
833  *      24      Transitive action verb exceeds goto list
834  *      25      Conditional travel entry with no alternative
835  *      26      Location has no travel entries
836  *      27      Hint number exceeds goto list
837  *      28      Invalid month returned by date function
838  *      29      Too many parameters given to SETPRM */
839
840         printf("Fatal error %ld.  See source code for interpretation.\n",
841            NUM);
842         exit(0);
843 }
844
845
846
847
848
849 /*  Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
850
851 #define BUG(NUM) fBUG(NUM)
852 #undef MAPLIN
853 void fMAPLIN(FILE *OPENED) {
854 long I, VAL;
855
856 /*  Read a line of input, from the specified input source,
857  *  translate the chars to integers in the range 0-126 and store
858  *  them in the common array "INLINE".  Integer values are as follows:
859  *     0   = space [ASCII CODE 40 octal, 32 decimal]
860  *    1-2  = !" [ASCII 41-42 octal, 33-34 decimal]
861  *    3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
862  *   11-36 = upper-case letters
863  *   37-62 = lower-case letters
864  *    63   = percent (%) [ASCII 45 octal, 37 decimal]
865  *   64-73 = digits, 0 through 9
866  *  Remaining characters can be translated any way that is convenient;
867  *  The "TYPE" routine below is used to map them back to characters when
868  *  necessary.  The above mappings are required so that certain special
869  *  characters are known to fit in 6 bits and/or can be easily spotted.
870  *  Array elements beyond the end of the line should be filled with 0,
871  *  and LNLENG should be set to the index of the last character.
872  *
873  *  If the data file uses a character other than space (e.g., tab) to
874  *  separate numbers, that character should also translate to 0.
875  *
876  *  This procedure may use the map1,map2 arrays to maintain static data for
877  *  the mapping.  MAP2(1) is set to 0 when the program starts
878  *  and is not changed thereafter unless the routines on this page choose
879  *  to do so. */
880
881         if(MAP2[1] == 0)MPINIT();
882
883         if (!oldstyle && SETUP && OPENED == stdin)
884                 fputs("> ", stdout);
885         do {
886                 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,OPENED));
887         } while
888                 (!feof(OPENED) && rawbuf[0] == '#');
889         if (feof(OPENED)) {
890                 if (logfp && OPENED == stdin)
891                         fclose(logfp);
892         } else {
893                 if (logfp && OPENED == stdin)
894                         IGNORE(fputs(rawbuf, logfp));
895                 else if (!isatty(0))
896                         IGNORE(fputs(rawbuf, stdout));
897                 strcpy(INLINE+1, rawbuf);
898                 LNLENG=0;
899                 for (I=1; I<=sizeof(INLINE) && INLINE[I]!=0; I++) {
900                 VAL=INLINE[I]+1;
901                 INLINE[I]=MAP1[VAL];
902                 if(INLINE[I] != 0)LNLENG=I;
903                 } /* end loop */
904                 LNPOSN=1;
905         }
906 }
907 #define MAPLIN(FIL) fMAPLIN(FIL)
908
909 #undef TYPE
910 void fTYPE(void) {
911 long I, VAL;
912
913 /*  Type the first "LNLENG" characters stored in inline, mapping them
914  *  from integers to text per the rules described above.  INLINE(I),
915  *  I=1,LNLENG may be changed by this routine. */
916
917
918         if(LNLENG != 0) goto L10;
919         printf("\n");
920         return;
921
922 L10:    if(MAP2[1] == 0)MPINIT();
923         for (I=1; I<=LNLENG; I++) {
924         VAL=INLINE[I];
925         {long x = VAL+1; INLINE[I]=MAP2[x];}
926         } /* end loop */
927         {long x = LNLENG+1; INLINE[x]=0;}
928         printf("%s\n",INLINE+1);
929         return;
930 }
931
932
933
934 #define TYPE() fTYPE()
935 #undef MPINIT
936 void fMPINIT(void) {
937 long FIRST, I, J, LAST, VAL;
938 static long RUNS[7][2] = {32,34, 39,46, 65,90, 97,122, 37,37, 48,57, 0,126};
939
940
941         for (I=1; I<=128; I++) {
942         MAP1[I]= -1;
943         } /* end loop */
944         VAL=0;
945         for (I=0; I<7; I++) {
946         FIRST=RUNS[I][0];
947         LAST=RUNS[I][1];
948         /* 22 */ for (J=FIRST; J<=LAST; J++) {
949         J++; if(MAP1[J] >= 0) goto L22;
950         MAP1[J]=VAL;
951         VAL=VAL+1;
952 L22:    J--;
953         } /* end loop */
954         /*etc*/ ;
955         } /* end loop */
956         MAP1[128]=MAP1[10];
957 /*  For this version, tab (9) maps to space (32), so del (127) uses tab's value */
958         MAP1[10]=MAP1[33];
959         MAP1[11]=MAP1[33];
960
961         for (I=0; I<=126; I++) {
962         I++; VAL=MAP1[I]+1; I--;
963         MAP2[VAL]=I*('B'-'A');
964         if(I >= 64)MAP2[VAL]=(I-64)*('B'-'A')+'@';
965         } /* end loop */
966
967         return;
968 }
969
970
971
972 #define MPINIT() fMPINIT()
973 #undef SAVEIO
974 void fSAVEIO(long OP, long IN, long ARR[]) {
975 static FILE *F; char NAME[50];
976
977 /*  If OP=0, ask for a file name and open a file.  (If IN=true, the file is for
978  *  input, else output.)  If OP>0, read/write ARR from/into the previously-opened
979  *  file.  (ARR is a 250-integer array.)  If OP<0, finish reading/writing the
980  *  file.  (Finishing writing can be a no-op if a "stop" statement does it
981  *  automatically.  Finishing reading can be a no-op as long as a subsequent
982  *  SAVEIO(0,false,X) will still work.)  If you can catch errors (e.g., no such
983  *  file) and try again, great.  DEC F40 can't. */
984
985
986         {long ifvar; ifvar=(OP); switch (ifvar<0? -1 : ifvar>0? 1 : 0) { case -1:
987                 goto L10; case 0: goto L20; case 1: goto L30; }}
988
989 L10:    fclose(F);
990         return;
991
992 L20:    printf("\nFile name: ");
993         IGNORE(fgets(NAME, sizeof(NAME), stdin));
994         F=fopen(NAME,(IN ? READ_MODE : WRITE_MODE));
995         if(F == NULL) {printf("Can't open file, try again.\n"); goto L20;}
996         return;
997
998 L30:    if(IN)IGNORE(fread(ARR,sizeof(long),250,F));
999         if(!IN)fwrite(ARR,sizeof(long),250,F);
1000         return;
1001
1002 }
1003
1004
1005
1006 void DATIME(long* D, long* T) {
1007   struct timeval tv;
1008   gettimeofday(&tv, NULL);
1009   *D = (long) tv.tv_sec;
1010   *T = (long) tv.tv_usec;
1011 }
1012 long fIABS(N)long N; {return(N<0? -N : N);}
1013 long fMOD(N,M)long N, M; {return(N%M);}