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 $
36 * $DragonFly: src/games/adventure/io.c,v 1.3 2007/04/18 18:32:12 swildner Exp $
39 /* Re-coding of advent in C: file i/o and user i/o */
47 static int next(void);
48 static void rdesc(int);
49 static void rdflt(void);
50 static void rhints(void);
51 static void rliq(void);
52 static void rlocs(void);
53 static int rnum(void);
54 static void rtrav(void);
55 static void rvoc(void);
57 static void twrite(int);
60 /* Get command from user. No prompt, usually. */
62 getin(char **wrd1
, char **wrd2
)
65 static char wd1buf
[MAXSTR
], wd2buf
[MAXSTR
];
68 *wrd1
= wd1buf
; /* return ptr to internal string */
70 wd2buf
[0] = 0; /* in case it isn't set here */
71 for (s
= wd1buf
, first
= 1, numch
= 0;;) {
72 if ((*s
= getchar()) >= 'A' && *s
<= 'Z')
73 *s
= *s
- ('A' - 'a'); /* convert to upper case */
74 switch (*s
) { /* start reading from user */
79 if (s
== wd1buf
|| s
== wd2buf
) /* initial blank */
82 if (first
) { /* finished 1st wd; start 2nd */
86 } else { /* finished 2nd word */
92 printf("user closed input stream, quitting...\n");
95 if (++numch
>= MAXSTR
) { /* string too long */
96 printf("Give me a break!!\n");
97 wd1buf
[0] = wd2buf
[0] = 0;
106 /* confirm with rspeak */
108 yes(int x
, int y
, int z
)
115 rspeak(x
); /* tell him what we want */
116 if ((ch
= getchar()) == 'y')
120 else if (ch
== EOF
) {
121 printf("user closed input stream, quitting...\n");
125 if (ch
== 'y' || ch
== 'n')
127 printf("Please answer the question.\n");
136 /* confirm with mspeak */
138 yesm(int x
, int y
, int z
)
145 mspeak(x
); /* tell him what we want */
146 if ((ch
= getchar()) == 'y')
150 else if (ch
== EOF
) {
151 printf("user closed input stream, quitting...\n");
155 if (ch
== 'y' || ch
== 'n')
157 printf("Please answer the question.\n");
166 /* FILE *inbuf, *outbuf; */
168 char *inptr
; /* Pointer into virtual disk */
170 int outsw
= 0; /* putting stuff to data file? */
172 const char iotape
[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
173 const char *tape
= iotape
; /* pointer to encryption tape */
175 /* next virtual char, bump adr */
181 ch
= (*inptr
^ random()) & 0xFF; /* Decrypt input data */
182 if (outsw
) { /* putting data in tmp file */
184 tape
= iotape
; /* rewind encryption tape */
185 *inptr
= ch
^ *tape
++; /* re-encrypt and replace value */
191 char breakch
; /* tell which char ended rnum */
193 /* "read" data from virtual file */
200 inptr
= data_file
; /* Pointer to virtual data file */
201 srandom(SEED
); /* which is lightly encrypted. */
204 for (;;) { /* read data sections */
205 sect
= next() - '0'; /* 1st digit of section number */
207 printf("Section %c", sect
+ '0');
209 if ((ch
= next()) != LF
) { /* is there a second digit? */
214 sect
= 10 * sect
+ ch
- '0';
220 case 0: /* finished reading database */
222 case 1: /* long form descriptions */
225 case 2: /* short form descriptions */
228 case 3: /* travel table */
231 case 4: /* vocabulary */
234 case 5: /* object descriptions */
237 case 6: /* arbitrary messages */
240 case 7: /* object locations */
243 case 8: /* action defaults */
246 case 9: /* liquid assets */
249 case 10: /* class messages */
255 case 12: /* magic messages */
259 printf("Invalid data section number: %d\n", sect
);
263 if (breakch
!= LF
) /* routines return after "-1" */
270 /* read initial location num */
276 tape
= iotape
; /* restart encryption tape */
277 for (s
= nbf
, *s
= 0;; s
++)
278 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
)
280 breakch
= *s
; /* save char for rtrav() */
281 *s
= 0; /* got the number as ascii */
282 if (nbf
[0] == '-') /* end of data */
284 return (atoi(nbf
)); /* convert it to integer */
289 /* read description-format msgs */
294 char *seekstart
, *maystart
;
296 seekhere
= inptr
; /* Where are we in virtual file? */
297 outsw
= 1; /* these msgs go into tmp file */
298 for (oldloc
= -1, seekstart
= seekhere
;;) {
299 maystart
= inptr
; /* maybe starting new entry */
300 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 /* finished msg */
302 && !(sect
== 5 && (locc
== 0 || locc
>= 100))) {
303 switch (sect
) { /* now put it into right table */
304 case 1: /* long descriptions */
305 ltext
[oldloc
].seekadr
= seekhere
;
306 ltext
[oldloc
].txtlen
= maystart
- seekstart
;
308 case 2: /* short descriptions */
309 stext
[oldloc
].seekadr
= seekhere
;
310 stext
[oldloc
].txtlen
= maystart
- seekstart
;
312 case 5: /* object descriptions */
313 ptext
[oldloc
].seekadr
= seekhere
;
314 ptext
[oldloc
].txtlen
= maystart
- seekstart
;
316 case 6: /* random messages */
318 errx(1, "Too many random msgs");
319 rtext
[oldloc
].seekadr
= seekhere
;
320 rtext
[oldloc
].txtlen
= maystart
- seekstart
;
322 case 10: /* class messages */
323 ctext
[clsses
].seekadr
= seekhere
;
324 ctext
[clsses
].txtlen
= maystart
- seekstart
;
325 cval
[clsses
++] = oldloc
;
327 case 12: /* magic messages */
329 errx(1, "Too many magic msgs");
330 mtext
[oldloc
].seekadr
= seekhere
;
331 mtext
[oldloc
].txtlen
= maystart
- seekstart
;
334 errx(1, "rdesc called with bad section");
336 seekhere
+= maystart
- seekstart
;
339 outsw
= 0; /* turn off output */
340 seekhere
+= 3; /* -1<delimiter> */
343 if (sect
!= 5 || (locc
> 0 && locc
< 100)) {
344 if (oldloc
!= locc
) /* starting a new message */
345 seekstart
= maystart
;
348 FLUSHLF
; /* scan the line */
352 /* read travel table */
360 int len
, m
, n
, entries
;
364 for (oldloc
= -1;;) { /* get another line */
366 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0) {
367 t
->next
= 0; /* terminate the old entry */
369 printf("%d:%d entries\n", oldloc
, entries
);
375 if (locc
!= oldloc
) { /* getting a new entry */
376 t
= travel
[locc
] = malloc(sizeof(*t
));
378 errx(1, "Out of memory!");
379 /* printf("New travel list for %d\n", locc); */
383 for (s
= buf
;; s
++) /* get the newloc number /ASCII */
384 if ((*s
= next()) == TAB
|| *s
== LF
)
387 len
= strlen(buf
); /* quad long number handling */
388 /* printf("Newloc: %s (%d chars)\n", buf, len); */
389 if (len
< 4) { /* no "m" conditions */
391 n
= atoi(buf
); /* newloc mod 1000 = newloc */
392 } else { /* a long integer */
393 n
= atoi(buf
+ len
- 3);
394 buf
[len
- 3] = 0; /* terminate newloc/1000 */
397 while (breakch
!= LF
) { /* only do one line at a time */
399 t
= t
->next
= malloc(sizeof(*t
));
401 errx(1, "Out of memory!");
403 t
->tverb
= rnum(); /* get verb from the file */
404 t
->tloc
= n
; /* table entry mod 1000 */
405 t
->conditions
= m
; /* table entry / 1000 */
406 /* printf("entry %d for %d\n", entries, locc); */
413 /* travel options from this loc */
422 for (t
= travel
[loq
]; t
!= 0; t
= t
->next
) {
423 printf("verb %d takes you to ", t
->tverb
);
425 speak(<ext
[t
->tloc
]);
426 else if (t
->tloc
<= 500)
427 printf("special code %d\n", t
->tloc
- 300);
429 rspeak(t
->tloc
- 500);
430 printf("under conditions %d\n", t
->conditions
);
435 /* read the vocabulary */
447 for (s
= buf
, *s
= 0;; s
++) /* get the word */
448 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
451 /* terminate word with newline, LF, tab, blank */
452 if (*s
!= '\n' && *s
!= LF
)
453 FLUSHLF
; /* can be comments */
455 /* printf("\"%s\"=%d\n", buf, rv_index); */
456 vocab(buf
, -2, rv_index
);
460 /* initial object locations */
465 if ((obj
= rnum()) < 0)
467 plac
[obj
] = rnum(); /* initial loc for this obj */
468 if (breakch
== TAB
) /* there's another entry */
475 /* default verb messages */
480 if ((verb
= rnum()) < 0)
482 actspk
[verb
] = rnum();
486 /* liquid assets &c: cond bits */
492 for (;;) { /* read new bit list */
493 if ((bitnum
= rnum()) < 0)
495 for (;;) { /* read locs for bits */
496 cond
[rnum()] |= setbit
[bitnum
];
510 if ((hintnum
= rnum()) < 0)
512 for (i
= 1; i
< 5; i
++)
513 hints
[hintnum
][i
] = rnum();
514 if (hintnum
> hntmax
)
533 /* read, decrypt, and print a message (not ptext) */
534 /* msg is a pointer to seek address and length of mess */
536 speak(const struct text
*msg
)
542 while (s
- msg
->seekadr
< msg
->txtlen
) { /* read a line at a time */
543 tape
= iotape
; /* restart decryption tape */
544 while ((*s
++ ^ *tape
++) != TAB
); /* read past loc num */
545 /* assume tape is longer than location number */
546 /* plus the lookahead put together */
547 if ((*s
^ *tape
) == '>' &&
548 (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
549 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
551 if (blklin
&& !nonfirst
++)
555 tape
= iotape
; /* rewind decryp tape */
557 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */
561 /* read, decrypt and print a ptext message */
562 /* msg is the number of all the p msgs for this place */
563 /* assumes object 1 doesn't have prop 1, obj 2 no prop 2, etc. */
565 pspeak(int m
, int skip
)
568 char *numst
, ps_save
;
573 if ((tbuf
= malloc(msg
->txtlen
+ 1)) == 0)
574 errx(1, "Out of memory!");
575 memcpy(tbuf
, msg
->seekadr
, (unsigned)msg
->txtlen
+ 1); /* Room to null */
579 while (s
- tbuf
< msg
->txtlen
) { /* read line at a time */
580 tape
= iotape
; /* restart decryption tape */
581 for (numst
= s
; (*s
^= *tape
++) != TAB
; s
++) /* get number */
584 ps_save
= *s
; /* Temporarily trash the string (cringe) */
585 *s
++ = 0; /* decrypting number within the string */
587 if (atoi(numst
) != 100 * skip
&& skip
>= 0) {
588 while ((*s
++ ^ *tape
++) != LF
) /* flush the line */
593 if ((*s
^ *tape
) == '>' && (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
594 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
596 if (blklin
&& !nonfirst
++)
602 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */