]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/io.c
1 /* $NetBSD: io.c,v 1.14 2003/09/19 10:01:21 itojun Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * The game adventure was originally written in Fortran by Will Crowther
8 * and Don Woods. It was later translated to C and enhanced by Jim
9 * Gillogly. This code is derived from software contributed to Berkeley
10 * by Jim Gillogly at The Rand Corporation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: io.c,v 1.14 2003/09/19 10:01:21 itojun Exp $");
46 /* Re-coding of advent in C: file i/o and user i/o */
57 getin(wrd1
, wrd2
) /* get command from user */
58 char **wrd1
, **wrd2
; /* no prompt, usually */
61 static char wd1buf
[MAXSTR
], wd2buf
[MAXSTR
];
64 *wrd1
= wd1buf
; /* return ptr to internal str */
66 wd2buf
[0] = 0; /* in case it isn't set here */
67 for (s
= wd1buf
, first
= 1, numch
= 0;;) {
68 if ((*s
= getchar()) >= 'A' && *s
<= 'Z')
69 *s
= *s
- ('A' - 'a');
70 /* convert to upper case */
71 switch (*s
) { /* start reading from user */
76 if (s
== wd1buf
|| s
== wd2buf
) /* initial blank */
79 if (first
) { /* finished 1st wd; start 2nd */
83 } else { /* finished 2nd word */
89 printf("user closed input stream, quitting...\n");
92 if (++numch
>= MAXSTR
) { /* string too long */
93 printf("Give me a break!!\n");
94 wd1buf
[0] = wd2buf
[0] = 0;
104 yes(x
, y
, z
) /* confirm with rspeak */
107 int result
= TRUE
; /* pacify gcc */
110 rspeak(x
); /* tell him what we want */
111 if ((ch
= getchar()) == 'y')
115 else if (ch
== EOF
) {
116 printf("user closed input stream, quitting...\n");
120 if (ch
== 'y' || ch
== 'n')
122 printf("Please answer the question.\n");
132 yesm(x
, y
, z
) /* confirm with mspeak */
135 int result
= TRUE
; /* pacify gcc */
138 mspeak(x
); /* tell him what we want */
139 if ((ch
= getchar()) == 'y')
143 else if (ch
== EOF
) {
144 printf("user closed input stream, quitting...\n");
148 if (ch
== 'y' || ch
== 'n')
150 printf("Please answer the question.\n");
158 /* FILE *inbuf,*outbuf; */
160 char *inptr
; /* Pointer into virtual disk */
162 int outsw
= 0; /* putting stuff to data file? */
164 const char iotape
[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
165 const char *tape
= iotape
; /* pointer to encryption tape */
169 { /* next virtual char, bump adr */
172 ch
= (*inptr
^ random()) & 0xFF; /* Decrypt input data */
173 if (outsw
) { /* putting data in tmp file */
175 tape
= iotape
; /* rewind encryption tape */
176 *inptr
= ch
^ *tape
++; /* re-encrypt and replace value */
182 char breakch
; /* tell which char ended rnum */
186 { /* "read" data from virtual file */
190 inptr
= data_file
; /* Pointer to virtual data file */
191 srandom(SEED
); /* which is lightly encrypted. */
194 for (;;) { /* read data sections */
195 sect
= next() - '0'; /* 1st digit of section number */
197 printf("Section %c", sect
+ '0');
199 if ((ch
= next()) != LF
) { /* is there a second digit? */
204 sect
= 10 * sect
+ ch
- '0';
210 case 0: /* finished reading database */
212 case 1: /* long form descriptions */
215 case 2: /* short form descriptions */
218 case 3: /* travel table */
221 case 4: /* vocabulary */
224 case 5: /* object descriptions */
227 case 6: /* arbitrary messages */
230 case 7: /* object locations */
233 case 8: /* action defaults */
236 case 9: /* liquid assets */
239 case 10: /* class messages */
245 case 12: /* magic messages */
249 printf("Invalid data section number: %d\n", sect
);
253 if (breakch
!= LF
) /* routines return after "-1" */
263 { /* read initial location num */
265 tape
= iotape
; /* restart encryption tape */
266 for (s
= nbf
, *s
= 0;; s
++)
267 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
)
269 breakch
= *s
; /* save char for rtrav() */
270 *s
= 0; /* got the number as ascii */
272 return (-1); /* end of data */
273 return (atoi(nbf
)); /* convert it to integer */
279 rdesc(sect
) /* read description-format msgs */
283 char *seekstart
, *maystart
;
285 seekhere
= inptr
; /* Where are we in virtual file? */
286 outsw
= 1; /* these msgs go into tmp file */
287 for (oldloc
= -1, seekstart
= seekhere
;;) {
288 maystart
= inptr
; /* maybe starting new entry */
289 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 /* finished msg */
290 && !(sect
== 5 && (locc
== 0 || locc
>= 100))) { /* unless sect 5 */
291 switch (sect
) { /* now put it into right table */
292 case 1:/* long descriptions */
293 ltext
[oldloc
].seekadr
= seekhere
;
294 ltext
[oldloc
].txtlen
= maystart
- seekstart
;
296 case 2:/* short descriptions */
297 stext
[oldloc
].seekadr
= seekhere
;
298 stext
[oldloc
].txtlen
= maystart
- seekstart
;
300 case 5:/* object descriptions */
301 ptext
[oldloc
].seekadr
= seekhere
;
302 ptext
[oldloc
].txtlen
= maystart
- seekstart
;
304 case 6:/* random messages */
306 errx(1,"Too many random msgs");
307 rtext
[oldloc
].seekadr
= seekhere
;
308 rtext
[oldloc
].txtlen
= maystart
- seekstart
;
310 case 10: /* class messages */
311 ctext
[clsses
].seekadr
= seekhere
;
312 ctext
[clsses
].txtlen
= maystart
- seekstart
;
313 cval
[clsses
++] = oldloc
;
315 case 12: /* magic messages */
316 if (oldloc
>= MAGSIZ
)
317 errx(1,"Too many magic msgs");
318 mtext
[oldloc
].seekadr
= seekhere
;
319 mtext
[oldloc
].txtlen
= maystart
- seekstart
;
322 errx(1,"rdesc called with bad section");
324 seekhere
+= maystart
- seekstart
;
327 outsw
= 0; /* turn off output */
328 seekhere
+= 3; /* -1<delimiter> */
331 if (sect
!= 5 || (locc
> 0 && locc
< 100)) {
332 if (oldloc
!= locc
) /* starting a new message */
333 seekstart
= maystart
;
336 FLUSHLF
; /* scan the line */
342 { /* read travel table */
344 struct travlist
*t
= NULL
;
347 int len
, m
, n
, entries
= 0;
349 for (oldloc
= -1;;) { /* get another line */
350 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0) { /* end of entry */
351 t
->next
= 0; /* terminate the old entry */
352 /* printf("%d:%d entries\n",oldloc,entries); */
353 /* twrite(oldloc); */
357 if (locc
!= oldloc
) { /* getting a new entry */
358 t
= travel
[locc
] = (struct travlist
*) malloc(sizeof(struct travlist
));
361 /* printf("New travel list for %d\n",locc); */
365 for (s
= buf
;; s
++) /* get the newloc number /ASCII */
366 if ((*s
= next()) == TAB
|| *s
== LF
)
369 len
= length(buf
) - 1; /* quad long number handling */
370 /* printf("Newloc: %s (%d chars)\n",buf,len); */
371 if (len
< 4) { /* no "m" conditions */
373 n
= atoi(buf
); /* newloc mod 1000 = newloc */
374 } else { /* a long integer */
375 n
= atoi(buf
+ len
- 3);
376 buf
[len
- 3] = 0; /* terminate newloc/1000 */
379 while (breakch
!= LF
) { /* only do one line at a time */
381 t
= t
->next
= (struct travlist
*) malloc(sizeof(struct travlist
));
385 t
->tverb
= rnum(); /* get verb from the file */
386 t
->tloc
= n
; /* table entry mod 1000 */
387 t
->conditions
= m
; /* table entry / 1000 */
388 /* printf("entry %d for %d\n",entries,locc); */
395 twrite(loq
) /* travel options from this loc */
402 for (t
= travel
[loq
]; t
!= 0; t
= t
->next
) {
403 printf("verb %d takes you to ", t
->tverb
);
405 speak(<ext
[t
->tloc
]);
408 printf("special code %d\n", t
->tloc
- 300);
410 rspeak(t
->tloc
- 500);
411 printf("under conditions %d\n", t
->conditions
);
419 char *s
; /* read the vocabulary */
426 for (s
= buf
, *s
= 0;; s
++) /* get the word */
427 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
430 /* terminate word with newline, LF, tab, blank */
431 if (*s
!= '\n' && *s
!= LF
)
432 FLUSHLF
;/* can be comments */
434 /* printf("\"%s\"=%d\n",buf,index); */
435 vocab(buf
, -2, index
);
443 { /* initial object locations */
445 if ((obj
= rnum()) < 0)
447 plac
[obj
] = rnum(); /* initial loc for this obj */
448 if (breakch
== TAB
) /* there's another entry */
457 { /* default verb messages */
459 if ((verb
= rnum()) < 0)
461 actspk
[verb
] = rnum();
467 { /* liquid assets &c: cond bits */
469 for (;;) { /* read new bit list */
470 if ((bitnum
= rnum()) < 0)
472 for (;;) { /* read locs for bits */
473 cond
[rnum()] |= setbit
[bitnum
];
486 if ((hintnum
= rnum()) < 0)
488 for (i
= 1; i
< 5; i
++)
489 hints
[hintnum
][i
] = rnum();
490 if (hintnum
> hntmax
)
515 speak(msg
) /* read, decrypt, and print a message (not
517 const struct text
*msg
; /* msg is a pointer to seek address and length
524 while (s
- msg
->seekadr
< msg
->txtlen
) { /* read a line at a time */
525 tape
= iotape
; /* restart decryption tape */
526 while ((*s
++ ^ *tape
++) != TAB
); /* read past loc num */
527 /* assume tape is longer than location number */
528 /* plus the lookahead put together */
529 if ((*s
^ *tape
) == '>' &&
530 (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
531 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
533 if (blklin
&& !nonfirst
++)
537 tape
= iotape
; /* rewind decryp tape */
539 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */
545 pspeak(m
, skip
) /* read, decrypt an print a ptext message */
546 int m
; /* msg is the number of all the p msgs for
548 int skip
; /* assumes object 1 doesn't have prop 1, obj 2
557 if ((tbuf
= (char *) malloc(msg
->txtlen
+ 1)) == NULL
)
559 memcpy(tbuf
, msg
->seekadr
, msg
->txtlen
+ 1); /* Room to null */
563 while (s
- tbuf
< msg
->txtlen
) { /* read line at a time */
564 tape
= iotape
; /* restart decryption tape */
565 for (numst
= s
; (*s
^= *tape
++) != TAB
; s
++); /* get number */
567 save
= *s
; /* Temporarily trash the string (cringe) */
568 *s
++ = 0; /* decrypting number within the string */
570 if (atoi(numst
) != 100 * skip
&& skip
>= 0) {
571 while ((*s
++ ^ *tape
++) != LF
) /* flush the line */
576 if ((*s
^ *tape
) == '>' && (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
577 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
579 if (blklin
&& !nonfirst
++)
585 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */