]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/io.c
1 /* $NetBSD: io.c,v 1.6 1997/10/11 01:53:29 lukem 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/cdefs.h>
44 static char sccsid
[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
46 __RCSID("$NetBSD: io.c,v 1.6 1997/10/11 01:53:29 lukem Exp $");
50 /* Re-coding of advent in C: file i/o and user i/o */
60 getin(wrd1
, wrd2
) /* get command from user */
61 char **wrd1
, **wrd2
; /* no prompt, usually */
64 static char wd1buf
[MAXSTR
], wd2buf
[MAXSTR
];
67 *wrd1
= wd1buf
; /* return ptr to internal str */
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');
73 /* 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 if (++numch
>= MAXSTR
) { /* string too long */
93 printf("Give me a break!!\n");
94 wd1buf
[0] = wd2buf
[0] = 0;
104 confirm(mesg
) /* confirm irreversible action */
108 printf("%s", mesg
); /* tell him what he did */
109 if (getchar() == 'y') /* was his first letter a 'y'? */
118 yes(x
, y
, z
) /* confirm with rspeak */
121 int result
= TRUE
; /* pacify gcc */
124 rspeak(x
); /* tell him what we want */
125 if ((ch
= getchar()) == 'y')
131 if (ch
== 'y' || ch
== 'n')
133 printf("Please answer the question.\n");
143 yesm(x
, y
, z
) /* confirm with mspeak */
146 int result
= TRUE
; /* pacify gcc */
149 mspeak(x
); /* tell him what we want */
150 if ((ch
= getchar()) == 'y')
156 if (ch
== 'y' || ch
== 'n')
158 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 char iotape
[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
173 char *tape
= iotape
; /* pointer to encryption tape */
177 { /* 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 */
194 { /* "read" data from virtual file */
198 inptr
= data_file
; /* Pointer to virtual data file */
199 srandom(SEED
); /* which is lightly encrypted. */
202 for (;;) { /* read data sections */
203 sect
= next() - '0'; /* 1st digit of section number */
205 printf("Section %c", sect
+ '0');
207 if ((ch
= next()) != LF
) { /* is there a second digit? */
212 sect
= 10 * sect
+ ch
- '0';
218 case 0: /* finished reading database */
220 case 1: /* long form descriptions */
223 case 2: /* short form descriptions */
226 case 3: /* travel table */
229 case 4: /* vocabulary */
232 case 5: /* object descriptions */
235 case 6: /* arbitrary messages */
238 case 7: /* object locations */
241 case 8: /* action defaults */
244 case 9: /* liquid assets */
247 case 10: /* class messages */
253 case 12: /* magic messages */
257 printf("Invalid data section number: %d\n", sect
);
261 if (breakch
!= LF
) /* routines return after "-1" */
271 { /* read initial location num */
273 tape
= iotape
; /* restart encryption tape */
274 for (s
= nbf
, *s
= 0;; s
++)
275 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
)
277 breakch
= *s
; /* save char for rtrav() */
278 *s
= 0; /* got the number as ascii */
280 return (-1); /* end of data */
281 return (atoi(nbf
)); /* convert it to integer */
287 rdesc(sect
) /* read description-format msgs */
291 char *seekstart
, *maystart
;
293 seekhere
= inptr
; /* Where are we in virtual file? */
294 outsw
= 1; /* these msgs go into tmp file */
295 for (oldloc
= -1, seekstart
= seekhere
;;) {
296 maystart
= inptr
; /* maybe starting new entry */
297 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 /* finished msg */
298 && !(sect
== 5 && (locc
== 0 || locc
>= 100))) { /* unless sect 5 */
299 switch (sect
) { /* now put it into right table */
300 case 1:/* long descriptions */
301 ltext
[oldloc
].seekadr
= seekhere
;
302 ltext
[oldloc
].txtlen
= maystart
- seekstart
;
304 case 2:/* short descriptions */
305 stext
[oldloc
].seekadr
= seekhere
;
306 stext
[oldloc
].txtlen
= maystart
- seekstart
;
308 case 5:/* object descriptions */
309 ptext
[oldloc
].seekadr
= seekhere
;
310 ptext
[oldloc
].txtlen
= maystart
- seekstart
;
312 case 6:/* random messages */
313 if (oldloc
> RTXSIZ
) {
314 printf("Too many random msgs\n");
317 rtext
[oldloc
].seekadr
= seekhere
;
318 rtext
[oldloc
].txtlen
= maystart
- seekstart
;
320 case 10: /* class messages */
321 ctext
[clsses
].seekadr
= seekhere
;
322 ctext
[clsses
].txtlen
= maystart
- seekstart
;
323 cval
[clsses
++] = oldloc
;
325 case 12: /* magic messages */
326 if (oldloc
> MAGSIZ
) {
327 printf("Too many magic msgs\n");
330 mtext
[oldloc
].seekadr
= seekhere
;
331 mtext
[oldloc
].txtlen
= maystart
- seekstart
;
334 printf("rdesc called with bad section\n");
337 seekhere
+= maystart
- seekstart
;
340 outsw
= 0; /* turn off output */
341 seekhere
+= 3; /* -1<delimiter> */
344 if (sect
!= 5 || (locc
> 0 && locc
< 100)) {
345 if (oldloc
!= locc
) /* starting a new message */
346 seekstart
= maystart
;
349 FLUSHLF
; /* scan the line */
355 { /* read travel table */
357 struct travlist
*t
= NULL
;
360 int len
, m
, n
, entries
= 0;
362 for (oldloc
= -1;;) { /* get another line */
363 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0) { /* end of entry */
364 t
->next
= 0; /* terminate the old entry */
365 /* printf("%d:%d entries\n",oldloc,entries); */
366 /* twrite(oldloc); */
370 if (locc
!= oldloc
) { /* getting a new entry */
371 t
= travel
[locc
] = (struct travlist
*) malloc(sizeof(struct travlist
));
372 /* printf("New travel list for %d\n",locc); */
376 for (s
= buf
;; s
++) /* get the newloc number /ASCII */
377 if ((*s
= next()) == TAB
|| *s
== LF
)
380 len
= length(buf
) - 1; /* quad long number handling */
381 /* printf("Newloc: %s (%d chars)\n",buf,len); */
382 if (len
< 4) { /* no "m" conditions */
384 n
= atoi(buf
); /* newloc mod 1000 = newloc */
385 } else { /* a long integer */
386 n
= atoi(buf
+ len
- 3);
387 buf
[len
- 3] = 0; /* terminate newloc/1000 */
390 while (breakch
!= LF
) { /* only do one line at a time */
392 t
= t
->next
= (struct travlist
*) malloc(sizeof(struct travlist
));
393 t
->tverb
= rnum(); /* get verb from the file */
394 t
->tloc
= n
; /* table entry mod 1000 */
395 t
->conditions
= m
; /* table entry / 1000 */
396 /* printf("entry %d for %d\n",entries,locc); */
403 twrite(loq
) /* travel options from this loc */
410 for (t
= travel
[loq
]; t
!= 0; t
= t
->next
) {
411 printf("verb %d takes you to ", t
->tverb
);
413 speak(<ext
[t
->tloc
]);
416 printf("special code %d\n", t
->tloc
- 300);
418 rspeak(t
->tloc
- 500);
419 printf("under conditions %d\n", t
->conditions
);
427 char *s
; /* read the vocabulary */
434 for (s
= buf
, *s
= 0;; s
++) /* get the word */
435 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
438 /* terminate word with newline, LF, tab, blank */
439 if (*s
!= '\n' && *s
!= LF
)
440 FLUSHLF
;/* can be comments */
442 /* printf("\"%s\"=%d\n",buf,index); */
443 vocab(buf
, -2, index
);
451 { /* initial object locations */
453 if ((obj
= rnum()) < 0)
455 plac
[obj
] = rnum(); /* initial loc for this obj */
456 if (breakch
== TAB
) /* there's another entry */
465 { /* default verb messages */
467 if ((verb
= rnum()) < 0)
469 actspk
[verb
] = rnum();
475 { /* liquid assets &c: cond bits */
477 for (;;) { /* read new bit list */
478 if ((bitnum
= rnum()) < 0)
480 for (;;) { /* read locs for bits */
481 cond
[rnum()] |= setbit
[bitnum
];
494 if ((hintnum
= rnum()) < 0)
496 for (i
= 1; i
< 5; i
++)
497 hints
[hintnum
][i
] = rnum();
498 if (hintnum
> hntmax
)
523 speak(msg
) /* read, decrypt, and print a message (not
525 struct text
*msg
; /* msg is a pointer to seek address and length
532 while (s
- msg
->seekadr
< msg
->txtlen
) { /* read a line at a time */
533 tape
= iotape
; /* restart decryption tape */
534 while ((*s
++ ^ *tape
++) != TAB
); /* read past loc num */
535 /* assume tape is longer than location number */
536 /* plus the lookahead put together */
537 if ((*s
^ *tape
) == '>' &&
538 (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
539 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
541 if (blklin
&& !nonfirst
++)
545 tape
= iotape
; /* rewind decryp tape */
547 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */
553 pspeak(m
, skip
) /* read, decrypt an print a ptext message */
554 int m
; /* msg is the number of all the p msgs for
556 int skip
; /* assumes object 1 doesn't have prop 1, obj 2
565 if ((tbuf
= (char *) malloc(msg
->txtlen
+ 1)) == 0)
567 memcpy(tbuf
, msg
->seekadr
, msg
->txtlen
+ 1); /* Room to null */
571 while (s
- tbuf
< msg
->txtlen
) { /* read line at a time */
572 tape
= iotape
; /* restart decryption tape */
573 for (numst
= s
; (*s
^= *tape
++) != TAB
; s
++); /* get number */
575 save
= *s
; /* Temporarily trash the string (cringe) */
576 *s
++ = 0; /* decrypting number within the string */
578 if (atoi(numst
) != 100 * skip
&& skip
>= 0) {
579 while ((*s
++ ^ *tape
++) != LF
) /* flush the line */
584 if ((*s
^ *tape
) == '>' && (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
585 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
587 if (blklin
&& !nonfirst
++)
593 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */