]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/io.c
1 /* $NetBSD: io.c,v 1.18 2006/03/18 23:33:38 christos 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.18 2006/03/18 23:33:38 christos 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;;) {
69 if ((*s
= getchar()) >= 'A' && *s
<= 'Z')
70 *s
= *s
- ('A' - 'a');
71 /* convert to upper case */
72 switch (*s
) { /* start reading from user */
77 if (s
== wd1buf
|| s
== wd2buf
) /* initial blank */
80 if (first
) { /* finished 1st wd; start 2nd */
84 } else { /* finished 2nd word */
90 printf("user closed input stream, quitting...\n");
93 if (++numch
>= MAXSTR
) { /* string too long */
94 printf("Give me a break!!\n");
95 wd1buf
[0] = wd2buf
[0] = 0;
104 /* confirm with rspeak */
106 yes(int x
, int y
, int z
)
108 int result
= TRUE
; /* pacify gcc */
111 rspeak(x
); /* tell him what we want */
112 if ((ch
= getchar()) == 'y')
116 else if (ch
== EOF
) {
117 printf("user closed input stream, quitting...\n");
121 if (ch
== 'y' || ch
== 'n')
123 printf("Please answer the question.\n");
132 /* confirm with mspeak */
134 yesm(int x
, int y
, int z
)
136 int result
= TRUE
; /* pacify gcc */
139 mspeak(x
); /* tell him what we want */
140 if ((ch
= getchar()) == 'y')
144 else if (ch
== EOF
) {
145 printf("user closed input stream, quitting...\n");
149 if (ch
== 'y' || ch
== 'n')
151 printf("Please answer the question.\n");
159 /* FILE *inbuf,*outbuf; */
161 char *inptr
; /* Pointer into virtual disk */
163 int outsw
= 0; /* putting stuff to data file? */
165 const char iotape
[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
166 const char *tape
= iotape
; /* pointer to encryption tape */
168 /* next virtual char, bump adr */
174 ch
= (*inptr
^ random()) & 0xFF; /* Decrypt input data */
175 if (outsw
) { /* putting data in tmp file */
177 tape
= iotape
; /* rewind encryption tape */
178 *inptr
= ch
^ *tape
++; /* re-encrypt and replace value */
184 char breakch
; /* tell which char ended rnum */
186 /* "read" data from virtual file */
193 inptr
= data_file
; /* Pointer to virtual data file */
194 srandom(SEED
); /* which is lightly encrypted. */
197 for (;;) { /* read data sections */
198 sect
= next() - '0'; /* 1st digit of section number */
200 printf("Section %c", sect
+ '0');
202 if ((ch
= next()) != LF
) { /* is there a second digit? */
207 sect
= 10 * sect
+ ch
- '0';
213 case 0: /* finished reading database */
215 case 1: /* long form descriptions */
218 case 2: /* short form descriptions */
221 case 3: /* travel table */
224 case 4: /* vocabulary */
227 case 5: /* object descriptions */
230 case 6: /* arbitrary messages */
233 case 7: /* object locations */
236 case 8: /* action defaults */
239 case 9: /* liquid assets */
242 case 10: /* class messages */
248 case 12: /* magic messages */
252 printf("Invalid data section number: %d\n", sect
);
256 if (breakch
!= LF
) /* routines return after "-1" */
263 /* read initial location num */
268 tape
= iotape
; /* restart encryption tape */
269 for (s
= nbf
, *s
= 0;; s
++)
270 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
)
272 breakch
= *s
; /* save char for rtrav() */
273 *s
= 0; /* got the number as ascii */
275 return (-1); /* end of data */
276 return (atoi(nbf
)); /* convert it to integer */
281 /* read description-format msgs */
286 char *seekstart
, *maystart
;
288 seekhere
= inptr
; /* Where are we in virtual file? */
289 outsw
= 1; /* these msgs go into tmp file */
290 for (oldloc
= -1, seekstart
= seekhere
;;) {
291 maystart
= inptr
; /* maybe starting new entry */
292 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 /* finished msg */
294 && !(sect
== 5 && (locc
== 0 || locc
>= 100))) {
295 switch (sect
) { /* now put it into right table */
296 case 1:/* long descriptions */
297 ltext
[oldloc
].seekadr
= seekhere
;
298 ltext
[oldloc
].txtlen
= maystart
- seekstart
;
300 case 2:/* short descriptions */
301 stext
[oldloc
].seekadr
= seekhere
;
302 stext
[oldloc
].txtlen
= maystart
- seekstart
;
304 case 5:/* object descriptions */
305 ptext
[oldloc
].seekadr
= seekhere
;
306 ptext
[oldloc
].txtlen
= maystart
- seekstart
;
308 case 6:/* random messages */
309 if (oldloc
>= RTXSIZ
)
310 errx(1,"Too many random msgs");
311 rtext
[oldloc
].seekadr
= seekhere
;
312 rtext
[oldloc
].txtlen
= maystart
- seekstart
;
314 case 10: /* class messages */
315 ctext
[clsses
].seekadr
= seekhere
;
316 ctext
[clsses
].txtlen
= maystart
- seekstart
;
317 cval
[clsses
++] = oldloc
;
319 case 12: /* magic messages */
320 if (oldloc
>= MAGSIZ
)
321 errx(1,"Too many magic msgs");
322 mtext
[oldloc
].seekadr
= seekhere
;
323 mtext
[oldloc
].txtlen
= maystart
- seekstart
;
326 errx(1,"rdesc called with bad section");
328 seekhere
+= maystart
- seekstart
;
331 outsw
= 0; /* turn off output */
332 seekhere
+= 3; /* -1<delimiter> */
335 if (sect
!= 5 || (locc
> 0 && locc
< 100)) {
336 if (oldloc
!= locc
) /* starting a new message */
337 seekstart
= maystart
;
340 FLUSHLF
; /* scan the line */
344 /* read travel table */
349 struct travlist
*t
= NULL
;
352 int len
, m
, n
, entries
= 0;
354 for (oldloc
= -1;;) { /* get another line */
356 if ((locc
= rnum()) != oldloc
&& oldloc
>= 0 && t
) {
357 t
->next
= 0; /* terminate the old entry */
358 /* printf("%d:%d entries\n",oldloc,entries); */
359 /* twrite(oldloc); */
363 if (locc
!= oldloc
) { /* getting a new entry */
364 t
= travel
[locc
] = (struct travlist
*)
365 malloc(sizeof(struct travlist
));
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 */
387 if (t
&& entries
++) {
388 t
->next
= 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); */
402 /* 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
);
424 /* read the vocabulary */
435 for (s
= buf
, *s
= 0;; s
++) /* get the word */
436 if ((*s
= next()) == TAB
|| *s
== '\n' || *s
== LF
439 /* terminate word with newline, LF, tab, blank */
440 if (*s
!= '\n' && *s
!= LF
)
441 FLUSHLF
;/* can be comments */
443 /* printf("\"%s\"=%d\n",buf,idx); */
449 /* initial object locations */
454 if ((obj
= rnum()) < 0)
456 plac
[obj
] = rnum(); /* initial loc for this obj */
457 if (breakch
== TAB
) /* there's another entry */
464 /* default verb messages */
469 if ((verb
= rnum()) < 0)
471 actspk
[verb
] = rnum();
475 /* liquid assets &c: cond bits */
480 for (;;) { /* read new bit list */
481 if ((bitnum
= rnum()) < 0)
483 for (;;) { /* read locs for bits */
487 cond
[n
] |= setbit
[bitnum
];
500 if ((hintnum
= rnum()) < 0)
502 for (i
= 1; i
< 5; i
++)
503 hints
[hintnum
][i
] = rnum();
504 if (hintnum
> hntmax
)
526 /* read, decrypt, and print a message (not ptext) */
527 /* msg is a pointer to seek address and length of mess */
529 speak(const struct text
*msg
)
535 while (s
- msg
->seekadr
< msg
->txtlen
) { /* read a line at a time */
536 tape
= iotape
; /* restart decryption tape */
537 while ((*s
++ ^ *tape
++) != TAB
); /* read past loc num */
538 /* assume tape is longer than location number */
539 /* plus the lookahead put together */
540 if ((*s
^ *tape
) == '>' &&
541 (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
542 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
544 if (blklin
&& !nonfirst
++)
548 tape
= iotape
; /* rewind decryp tape */
550 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */
554 /* read, decrypt and print a ptext message */
555 /* msg is the number of all the p msgs for this place */
556 /* assumes object 1 doesn't have prop 1, obj 2 no prop 2 &c */
558 pspeak(int m
, int skip
)
566 if ((tbuf
= (char *) malloc(msg
->txtlen
+ 1)) == NULL
)
568 memcpy(tbuf
, msg
->seekadr
, msg
->txtlen
+ 1); /* Room to null */
572 while (s
- tbuf
< msg
->txtlen
) { /* read line at a time */
573 tape
= iotape
; /* restart decryption tape */
574 for (numst
= s
; (*s
^= *tape
++) != TAB
; s
++); /* get number */
576 /* Temporarily trash the string (cringe) */
577 *s
++ = 0; /* decrypting number within the string */
579 if (atoi(numst
) != 100 * skip
&& skip
>= 0) {
580 while ((*s
++ ^ *tape
++) != LF
) /* flush the line */
585 if ((*s
^ *tape
) == '>' && (*(s
+ 1) ^ *(tape
+ 1)) == '$' &&
586 (*(s
+ 2) ^ *(tape
+ 2)) == '<')
588 if (blklin
&& !nonfirst
++)
594 } while ((*s
++ ^ *tape
++) != LF
); /* better end with LF */