2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * The game adventure was originally written in Fortran by Will Crowther
6 * and Don Woods. It was later translated to C and enhanced by Jim
7 * Gillogly. This code is derived from software contributed to Berkeley
8 * by Jim Gillogly at The Rand Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)io.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/games/adventure/io.c,v 1.8.2.1 2001/03/05 11:43:11 kris Exp $
38 /* Re-coding of advent in C: file i/o and user i/o */
46 static int next(void);
47 static void rdesc(int);
48 static void rdflt(void);
49 static void rhints(void);
50 static void rliq(void);
51 static void rlocs(void);
52 static int rnum(void);
53 static void rtrav(void);
54 static void rvoc(void);
56 static void twrite(int);
59 /* Get command from user. No prompt, usually. */
61 getin(char **wrd1
, char **wrd2
)
64 static char wd1buf
[MAXSTR
], wd2buf
[MAXSTR
];
67 *wrd1
= wd1buf
; /* return ptr to internal string */
69 wd2buf
[0] = 0; /* in case it isn't set here */
70 for (s
= wd1buf
, first
= 1, numch
= 0;;) {
71 if ((*s
= getchar()) >= 'A' && *s
<= 'Z')
72 *s
= *s
- ('A' - 'a'); /* convert to upper case */
73 switch (*s
) { /* start reading from user */
78 if (s
== wd1buf
|| s
== wd2buf
) /* initial blank */
81 if (first
) { /* finished 1st wd; start 2nd */
85 } else { /* finished 2nd word */
91 printf("user closed input stream, quitting...\n");
94 if (++numch
>= MAXSTR
) { /* string too long */
95 printf("Give me a break!!\n");
96 wd1buf
[0] = wd2buf
[0] = 0;
105 /* confirm with rspeak */
107 yes(int x
, int y
, int z
)
114 rspeak(x
); /* tell him what we want */
115 if ((ch
= getchar()) == 'y')
119 else if (ch
== EOF
) {
120 printf("user closed input stream, quitting...\n");
124 if (ch
== 'y' || ch
== 'n')
126 printf("Please answer the question.\n");
135 /* confirm with mspeak */
137 yesm(int x
, int y
, int z
)
144 mspeak(x
); /* tell him what we want */
145 if ((ch
= getchar()) == 'y')
149 else if (ch
== EOF
) {
150 printf("user closed input stream, quitting...\n");
154 if (ch
== 'y' || ch
== 'n')
156 printf("Please answer the question.\n");
165 /* FILE *inbuf, *outbuf; */
167 char *inptr
; /* Pointer into virtual disk */
169 int outsw
= 0; /* putting stuff to data file? */
171 const char iotape
[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
172 const char *tape
= iotape
; /* pointer to encryption tape */
174 /* next virtual char, bump adr */
180 ch
= (*inptr
^ random()) & 0xFF; /* Decrypt input data */
181 if (outsw
) { /* putting data in tmp file */
183 tape
= iotape
; /* rewind encryption tape */
184 *inptr
= ch
^ *tape
++; /* re-encrypt and replace value */
190 char breakch
; /* tell which char ended rnum */
192 /* "read" data from virtual file */
199 inptr
= data_file
; /* Pointer to virtual data file */
200 srandom(SEED
); /* which is lightly encrypted. */
203 for (;;) { /* read data sections */
204 sect
= next() - '0'; /* 1st digit of section number */
206 printf("Section %c", sect
+ '0');
208 if ((ch
= next()) != LF
) { /* is there a second digit? */
213 sect
= 10 * sect
+ ch
- '0';
219 case 0: /* finished reading database */
221 case 1: /* long form descriptions */
224 case 2: /* short form descriptions */
227 case 3: /* travel table */
230 case 4: /* vocabulary */
233 case 5: /* object descriptions */
236 case 6: /* arbitrary messages */
239 case 7: /* object locations */
242 case 8: /* action defaults */
245 case 9: /* liquid assets */
248 case 10: /* class messages */
254 case 12: /* magic messages */
258 printf("Invalid data section number: %d\n", sect
);
262 if (breakch
!= LF
) /* routines return after "-1" */
269 /* read initial location num */
275 tape
= iotape
; /* restart encryption tape */
276 for (s
= nbf
, *s
= 0;; s
++)
277 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
)
279 breakch
= *s
; /* save char for rtrav() */
280 *s
= 0; /* got the number as ascii */
281 if (nbf
[0] == '-') /* end of data */
283 return (atoi(nbf
)); /* convert it to integer */
288 /* read description-format msgs */
293 char *seekstart
, *maystart
;
295 seekhere
= inptr
; /* Where are we in virtual file? */
296 outsw
= 1; /* these msgs go into tmp file */
297 for (oldloc
= -1, seekstart
= seekhere
;;) {
298 maystart
= inptr
; /* maybe starting new entry */
299 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 /* finished msg */
301 && !(sect
== 5 && (locc
== 0 || locc
>= 100))) {
302 switch (sect
) { /* now put it into right table */
303 case 1: /* long descriptions */
304 ltext
[oldloc
].seekadr
= seekhere
;
305 ltext
[oldloc
].txtlen
= maystart
- seekstart
;
307 case 2: /* short descriptions */
308 stext
[oldloc
].seekadr
= seekhere
;
309 stext
[oldloc
].txtlen
= maystart
- seekstart
;
311 case 5: /* object descriptions */
312 ptext
[oldloc
].seekadr
= seekhere
;
313 ptext
[oldloc
].txtlen
= maystart
- seekstart
;
315 case 6: /* random messages */
317 errx(1, "Too many random msgs");
318 rtext
[oldloc
].seekadr
= seekhere
;
319 rtext
[oldloc
].txtlen
= maystart
- seekstart
;
321 case 10: /* class messages */
322 ctext
[clsses
].seekadr
= seekhere
;
323 ctext
[clsses
].txtlen
= maystart
- seekstart
;
324 cval
[clsses
++] = oldloc
;
326 case 12: /* magic messages */
328 errx(1, "Too many magic msgs");
329 mtext
[oldloc
].seekadr
= seekhere
;
330 mtext
[oldloc
].txtlen
= maystart
- seekstart
;
333 errx(1, "rdesc called with bad section");
335 seekhere
+= maystart
- seekstart
;
338 outsw
= 0; /* turn off output */
339 seekhere
+= 3; /* -1<delimiter> */
342 if (sect
!= 5 || (locc
> 0 && locc
< 100)) {
343 if (oldloc
!= locc
) /* starting a new message */
344 seekstart
= maystart
;
347 FLUSHLF
; /* scan the line */
351 /* read travel table */
359 int len
, m
, n
, entries
;
363 for (oldloc
= -1;;) { /* get another line */
365 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0) {
366 t
->next
= 0; /* terminate the old entry */
368 printf("%d:%d entries\n", oldloc
, entries
);
374 if (locc
!= oldloc
) { /* getting a new entry */
375 t
= travel
[locc
] = malloc(sizeof(*t
));
377 errx(1, "Out of memory!");
378 /* printf("New travel list for %d\n", locc); */
382 for (s
= buf
;; s
++) /* get the newloc number /ASCII */
383 if ((*s
= next()) == TAB
|| *s
== LF
)
386 len
= strlen(buf
); /* quad long number handling */
387 /* printf("Newloc: %s (%d chars)\n", buf, len); */
388 if (len
< 4) { /* no "m" conditions */
390 n
= atoi(buf
); /* newloc mod 1000 = newloc */
391 } else { /* a long integer */
392 n
= atoi(buf
+ len
- 3);
393 buf
[len
- 3] = 0; /* terminate newloc/1000 */
396 while (breakch
!= LF
) { /* only do one line at a time */
398 t
= t
->next
= malloc(sizeof(*t
));
400 errx(1, "Out of memory!");
402 t
->tverb
= rnum(); /* get verb from the file */
403 t
->tloc
= n
; /* table entry mod 1000 */
404 t
->conditions
= m
; /* table entry / 1000 */
405 /* printf("entry %d for %d\n", entries, locc); */
412 /* travel options from this loc */
421 for (t
= travel
[loq
]; t
!= NULL
; t
= t
->next
) {
422 printf("verb %d takes you to ", t
->tverb
);
424 speak(<ext
[t
->tloc
]);
425 else if (t
->tloc
<= 500)
426 printf("special code %d\n", t
->tloc
- 300);
428 rspeak(t
->tloc
- 500);
429 printf("under conditions %d\n", t
->conditions
);
434 /* read the vocabulary */
446 for (s
= buf
, *s
= 0;; s
++) /* get the word */
447 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
450 /* terminate word with newline, LF, tab, blank */
451 if (*s
!= '\n' && *s
!= LF
)
452 FLUSHLF
; /* can be comments */
454 /* printf("\"%s\"=%d\n", buf, rv_index); */
455 vocab(buf
, -2, rv_index
);
459 /* initial object locations */
464 if ((obj
= rnum()) < 0)
466 plac
[obj
] = rnum(); /* initial loc for this obj */
467 if (breakch
== TAB
) /* there's another entry */
474 /* default verb messages */
479 if ((verb
= rnum()) < 0)
481 actspk
[verb
] = rnum();
485 /* liquid assets &c: cond bits */
491 for (;;) { /* read new bit list */
492 if ((bitnum
= rnum()) < 0)
494 for (;;) { /* read locs for bits */
495 cond
[rnum()] |= setbit
[bitnum
];
509 if ((hintnum
= rnum()) < 0)
511 for (i
= 1; i
< 5; i
++)
512 hints
[hintnum
][i
] = rnum();
513 if (hintnum
> hntmax
)
532 /* read, decrypt, and print a message (not ptext) */
533 /* msg is a pointer to seek address and length of mess */
535 speak(const struct text
*msg
)
541 while (s
- msg
->seekadr
< msg
->txtlen
) { /* read a line at a time */
542 tape
= iotape
; /* restart decryption tape */
543 while ((*s
++ ^ *tape
++) != TAB
); /* read past loc num */
544 /* assume tape is longer than location number */
545 /* plus the lookahead put together */
546 if ((*s
^ *tape
) == '>' &&
547 (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
548 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
550 if (blklin
&& !nonfirst
++)
554 tape
= iotape
; /* rewind decryp tape */
556 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */
560 /* read, decrypt and print a ptext message */
561 /* msg is the number of all the p msgs for this place */
562 /* assumes object 1 doesn't have prop 1, obj 2 no prop 2, etc. */
564 pspeak(int m
, int skip
)
572 if ((tbuf
= malloc(msg
->txtlen
+ 1)) == NULL
)
573 errx(1, "Out of memory!");
574 memcpy(tbuf
, msg
->seekadr
, (unsigned)msg
->txtlen
+ 1); /* Room to null */
578 while (s
- tbuf
< msg
->txtlen
) { /* read line at a time */
579 tape
= iotape
; /* restart decryption tape */
580 for (numst
= s
; (*s
^= *tape
++) != TAB
; s
++) /* get number */
583 /* Temporarily trash the string (cringe) */
584 *s
++ = 0; /* decrypting number within the string */
586 if (atoi(numst
) != 100 * skip
&& skip
>= 0) {
587 while ((*s
++ ^ *tape
++) != LF
) /* flush the line */
592 if ((*s
^ *tape
) == '>' && (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
593 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
595 if (blklin
&& !nonfirst
++)
601 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */