]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/io.c
1 /* $NetBSD: io.c,v 1.20 2006/05/18 18:42:59 mrg 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.20 2006/05/18 18:42:59 mrg Exp $");
46 /* Re-coding of advent in C: file i/o and user i/o */
56 /* get command from user */
57 /* no prompt, usually */
59 getin(char **wrd1
, char **wrd2
)
62 static char wd1buf
[MAXSTR
], wd2buf
[MAXSTR
];
65 *wrd1
= wd1buf
; /* return ptr to internal str */
67 wd2buf
[0] = 0; /* in case it isn't set here */
68 for (s
= wd1buf
, first
= 1, numch
= 0;;) {
70 if ((*s
= (char)c
) >= 'A' && *s
<= 'Z')
71 *s
= *s
- ('A' - 'a');
72 /* convert to upper case */
73 switch (c
) { /* 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
)
109 int result
= TRUE
; /* pacify gcc */
112 rspeak(x
); /* tell him what we want */
113 if ((ch
= getchar()) == 'y')
117 else if (ch
== EOF
) {
118 printf("user closed input stream, quitting...\n");
122 if (ch
== 'y' || ch
== 'n')
124 printf("Please answer the question.\n");
133 /* confirm with mspeak */
135 yesm(int x
, int y
, int z
)
137 int result
= TRUE
; /* pacify gcc */
140 mspeak(x
); /* tell him what we want */
141 if ((ch
= getchar()) == 'y')
145 else if (ch
== EOF
) {
146 printf("user closed input stream, quitting...\n");
150 if (ch
== 'y' || ch
== 'n')
152 printf("Please answer the question.\n");
160 /* FILE *inbuf,*outbuf; */
162 char *inptr
; /* Pointer into virtual disk */
164 int outsw
= 0; /* putting stuff to data file? */
166 const char iotape
[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
167 const char *tape
= iotape
; /* pointer to encryption tape */
169 /* next virtual char, bump adr */
175 ch
= (*inptr
^ random()) & 0xFF; /* Decrypt input data */
176 if (outsw
) { /* putting data in tmp file */
178 tape
= iotape
; /* rewind encryption tape */
179 *inptr
= ch
^ *tape
++; /* re-encrypt and replace value */
185 char breakch
; /* tell which char ended rnum */
187 /* "read" data from virtual file */
194 inptr
= data_file
; /* Pointer to virtual data file */
195 srandom(SEED
); /* which is lightly encrypted. */
198 for (;;) { /* read data sections */
199 sect
= next() - '0'; /* 1st digit of section number */
201 printf("Section %c", sect
+ '0');
203 if ((ch
= next()) != LF
) { /* is there a second digit? */
208 sect
= 10 * sect
+ ch
- '0';
214 case 0: /* finished reading database */
216 case 1: /* long form descriptions */
219 case 2: /* short form descriptions */
222 case 3: /* travel table */
225 case 4: /* vocabulary */
228 case 5: /* object descriptions */
231 case 6: /* arbitrary messages */
234 case 7: /* object locations */
237 case 8: /* action defaults */
240 case 9: /* liquid assets */
243 case 10: /* class messages */
249 case 12: /* magic messages */
253 printf("Invalid data section number: %d\n", sect
);
257 if (breakch
!= LF
) /* routines return after "-1" */
264 /* read initial location num */
269 tape
= iotape
; /* restart encryption tape */
270 for (s
= nbf
, *s
= 0;; s
++)
271 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
)
273 breakch
= *s
; /* save char for rtrav() */
274 *s
= 0; /* got the number as ascii */
276 return (-1); /* end of data */
277 return (atoi(nbf
)); /* convert it to integer */
282 /* read description-format msgs */
287 char *seekstart
, *maystart
;
289 seekhere
= inptr
; /* Where are we in virtual file? */
290 outsw
= 1; /* these msgs go into tmp file */
291 for (oldloc
= -1, seekstart
= seekhere
;;) {
292 maystart
= inptr
; /* maybe starting new entry */
293 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 /* finished msg */
295 && !(sect
== 5 && (locc
== 0 || locc
>= 100))) {
296 switch (sect
) { /* now put it into right table */
297 case 1:/* long descriptions */
298 ltext
[oldloc
].seekadr
= seekhere
;
299 ltext
[oldloc
].txtlen
= maystart
- seekstart
;
301 case 2:/* short descriptions */
302 stext
[oldloc
].seekadr
= seekhere
;
303 stext
[oldloc
].txtlen
= maystart
- seekstart
;
305 case 5:/* object descriptions */
306 ptext
[oldloc
].seekadr
= seekhere
;
307 ptext
[oldloc
].txtlen
= maystart
- seekstart
;
309 case 6:/* random messages */
310 if (oldloc
>= RTXSIZ
)
311 errx(1,"Too many random msgs");
312 rtext
[oldloc
].seekadr
= seekhere
;
313 rtext
[oldloc
].txtlen
= maystart
- seekstart
;
315 case 10: /* class messages */
316 ctext
[clsses
].seekadr
= seekhere
;
317 ctext
[clsses
].txtlen
= maystart
- seekstart
;
318 cval
[clsses
++] = oldloc
;
320 case 12: /* magic messages */
321 if (oldloc
>= MAGSIZ
)
322 errx(1,"Too many magic msgs");
323 mtext
[oldloc
].seekadr
= seekhere
;
324 mtext
[oldloc
].txtlen
= maystart
- seekstart
;
327 errx(1,"rdesc called with bad section");
329 seekhere
+= maystart
- seekstart
;
332 outsw
= 0; /* turn off output */
333 seekhere
+= 3; /* -1<delimiter> */
336 if (sect
!= 5 || (locc
> 0 && locc
< 100)) {
337 if (oldloc
!= locc
) /* starting a new message */
338 seekstart
= maystart
;
341 FLUSHLF
; /* scan the line */
345 /* read travel table */
350 struct travlist
*t
= NULL
;
353 int len
, m
, n
, entries
= 0;
355 for (oldloc
= -1;;) { /* get another line */
357 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 && t
) {
358 t
->next
= 0; /* terminate the old entry */
359 /* printf("%d:%d entries\n",oldloc,entries); */
360 /* twrite(oldloc); */
364 if (locc
!= oldloc
) { /* getting a new entry */
365 t
= travel
[locc
] = calloc(1, sizeof(*t
));
368 /* printf("New travel list for %d\n",locc); */
372 for (s
= buf
;; s
++) /* get the newloc number /ASCII */
373 if ((*s
= next()) == TAB
|| *s
== LF
)
376 len
= length(buf
) - 1; /* quad long number handling */
377 /* printf("Newloc: %s (%d chars)\n",buf,len); */
378 if (len
< 4) { /* no "m" conditions */
380 n
= atoi(buf
); /* newloc mod 1000 = newloc */
381 } else { /* a long integer */
382 n
= atoi(buf
+ len
- 3);
383 buf
[len
- 3] = 0; /* terminate newloc/1000 */
386 while (breakch
!= LF
) { /* only do one line at a time */
390 t
->next
= calloc(1, sizeof(*t
));
395 t
->tverb
= rnum(); /* get verb from the file */
396 t
->tloc
= n
; /* table entry mod 1000 */
397 t
->conditions
= m
; /* table entry / 1000 */
398 /* printf("entry %d for %d\n",entries,locc); */
404 /* travel options from this loc */
412 for (t
= travel
[loq
]; t
!= 0; t
= t
->next
) {
413 printf("verb %d takes you to ", t
->tverb
);
415 speak(<ext
[t
->tloc
]);
418 printf("special code %d\n", t
->tloc
- 300);
420 rspeak(t
->tloc
- 500);
421 printf("under conditions %d\n", t
->conditions
);
426 /* read the vocabulary */
437 for (s
= buf
, *s
= 0;; s
++) /* get the word */
438 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
441 /* terminate word with newline, LF, tab, blank */
442 if (*s
!= '\n' && *s
!= LF
)
443 FLUSHLF
;/* can be comments */
445 /* printf("\"%s\"=%d\n",buf,idx); */
451 /* initial object locations */
456 if ((obj
= rnum()) < 0)
458 plac
[obj
] = rnum(); /* initial loc for this obj */
459 if (breakch
== TAB
) /* there's another entry */
466 /* default verb messages */
471 if ((verb
= rnum()) < 0)
473 actspk
[verb
] = rnum();
477 /* liquid assets &c: cond bits */
482 for (;;) { /* read new bit list */
483 if ((bitnum
= rnum()) < 0)
485 for (;;) { /* read locs for bits */
489 cond
[n
] |= setbit
[bitnum
];
502 if ((hintnum
= rnum()) < 0)
504 for (i
= 1; i
< 5; i
++)
505 hints
[hintnum
][i
] = rnum();
506 if (hintnum
> hntmax
)
528 /* read, decrypt, and print a message (not ptext) */
529 /* msg is a pointer to seek address and length of mess */
531 speak(const struct text
*msg
)
537 while (s
- msg
->seekadr
< msg
->txtlen
) { /* read a line at a time */
538 tape
= iotape
; /* restart decryption tape */
539 while ((*s
++ ^ *tape
++) != TAB
); /* read past loc num */
540 /* assume tape is longer than location number */
541 /* plus the lookahead put together */
542 if ((*s
^ *tape
) == '>' &&
543 (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
544 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
546 if (blklin
&& !nonfirst
++)
550 tape
= iotape
; /* rewind decryp tape */
552 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */
556 /* read, decrypt and print a ptext message */
557 /* msg is the number of all the p msgs for this place */
558 /* assumes object 1 doesn't have prop 1, obj 2 no prop 2 &c */
560 pspeak(int m
, int skip
)
568 if ((tbuf
= (char *) malloc(msg
->txtlen
+ 1)) == NULL
)
570 memcpy(tbuf
, msg
->seekadr
, msg
->txtlen
+ 1); /* Room to null */
574 while (s
- tbuf
< msg
->txtlen
) { /* read line at a time */
575 tape
= iotape
; /* restart decryption tape */
576 for (numst
= s
; (*s
^= *tape
++) != TAB
; s
++); /* get number */
578 /* Temporarily trash the string (cringe) */
579 *s
++ = 0; /* decrypting number within the string */
581 if (atoi(numst
) != 100 * skip
&& skip
>= 0) {
582 while ((*s
++ ^ *tape
++) != LF
) /* flush the line */
587 if ((*s
^ *tape
) == '>' && (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
588 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
590 if (blklin
&& !nonfirst
++)
596 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */