]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - adventure/io.c
Some assorted minor cleanups, from larn 12.2.
[bsdgames-darwin.git] / adventure / io.c
1 /* $NetBSD: io.c,v 1.20 2006/05/18 18:42:59 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
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.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";
41 #else
42 __RCSID("$NetBSD: io.c,v 1.20 2006/05/18 18:42:59 mrg Exp $");
43 #endif
44 #endif /* not lint */
45
46 /* Re-coding of advent in C: file i/o and user i/o */
47
48 #include <err.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include "hdr.h"
53 #include "extern.h"
54
55
56 /* get command from user */
57 /* no prompt, usually */
58 void
59 getin(char **wrd1, char **wrd2)
60 {
61 char *s;
62 static char wd1buf[MAXSTR], wd2buf[MAXSTR];
63 int first, numch, c;
64
65 *wrd1 = wd1buf; /* return ptr to internal str */
66 *wrd2 = wd2buf;
67 wd2buf[0] = 0; /* in case it isn't set here */
68 for (s = wd1buf, first = 1, numch = 0;;) {
69 c = getchar();
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 */
74 case '\n':
75 *s = 0;
76 return;
77 case ' ':
78 if (s == wd1buf || s == wd2buf) /* initial blank */
79 continue;
80 *s = 0;
81 if (first) { /* finished 1st wd; start 2nd */
82 first = numch = 0;
83 s = wd2buf;
84 break;
85 } else { /* finished 2nd word */
86 FLUSHLINE;
87 *s = 0;
88 return;
89 }
90 case EOF:
91 printf("user closed input stream, quitting...\n");
92 exit(0);
93 default:
94 if (++numch >= MAXSTR) { /* string too long */
95 printf("Give me a break!!\n");
96 wd1buf[0] = wd2buf[0] = 0;
97 FLUSHLINE;
98 return;
99 }
100 s++;
101 }
102 }
103 }
104
105 /* confirm with rspeak */
106 int
107 yes(int x, int y, int z)
108 {
109 int result = TRUE; /* pacify gcc */
110 int ch;
111 for (;;) {
112 rspeak(x); /* tell him what we want */
113 if ((ch = getchar()) == 'y')
114 result = TRUE;
115 else if (ch == 'n')
116 result = FALSE;
117 else if (ch == EOF) {
118 printf("user closed input stream, quitting...\n");
119 exit(0);
120 }
121 FLUSHLINE;
122 if (ch == 'y' || ch == 'n')
123 break;
124 printf("Please answer the question.\n");
125 }
126 if (result == TRUE)
127 rspeak(y);
128 if (result == FALSE)
129 rspeak(z);
130 return (result);
131 }
132
133 /* confirm with mspeak */
134 int
135 yesm(int x, int y, int z)
136 {
137 int result = TRUE; /* pacify gcc */
138 int ch;
139 for (;;) {
140 mspeak(x); /* tell him what we want */
141 if ((ch = getchar()) == 'y')
142 result = TRUE;
143 else if (ch == 'n')
144 result = FALSE;
145 else if (ch == EOF) {
146 printf("user closed input stream, quitting...\n");
147 exit(0);
148 }
149 FLUSHLINE;
150 if (ch == 'y' || ch == 'n')
151 break;
152 printf("Please answer the question.\n");
153 }
154 if (result == TRUE)
155 mspeak(y);
156 if (result == FALSE)
157 mspeak(z);
158 return (result);
159 }
160 /* FILE *inbuf,*outbuf; */
161
162 char *inptr; /* Pointer into virtual disk */
163
164 int outsw = 0; /* putting stuff to data file? */
165
166 const char iotape[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";
167 const char *tape = iotape; /* pointer to encryption tape */
168
169 /* next virtual char, bump adr */
170 int
171 next(void)
172 {
173 int ch;
174
175 ch = (*inptr ^ random()) & 0xFF; /* Decrypt input data */
176 if (outsw) { /* putting data in tmp file */
177 if (*tape == 0)
178 tape = iotape; /* rewind encryption tape */
179 *inptr = ch ^ *tape++; /* re-encrypt and replace value */
180 }
181 inptr++;
182 return (ch);
183 }
184
185 char breakch; /* tell which char ended rnum */
186
187 /* "read" data from virtual file */
188 void
189 rdata(void)
190 {
191 int sect;
192 char ch;
193
194 inptr = data_file; /* Pointer to virtual data file */
195 srandom(SEED); /* which is lightly encrypted. */
196
197 clsses = 1;
198 for (;;) { /* read data sections */
199 sect = next() - '0'; /* 1st digit of section number */
200 #ifdef VERBOSE
201 printf("Section %c", sect + '0');
202 #endif
203 if ((ch = next()) != LF) { /* is there a second digit? */
204 FLUSHLF;
205 #ifdef VERBOSE
206 putchar(ch);
207 #endif
208 sect = 10 * sect + ch - '0';
209 }
210 #ifdef VERBOSE
211 putchar('\n');
212 #endif
213 switch (sect) {
214 case 0: /* finished reading database */
215 return;
216 case 1: /* long form descriptions */
217 rdesc(1);
218 break;
219 case 2: /* short form descriptions */
220 rdesc(2);
221 break;
222 case 3: /* travel table */
223 rtrav();
224 break;
225 case 4: /* vocabulary */
226 rvoc();
227 break;
228 case 5: /* object descriptions */
229 rdesc(5);
230 break;
231 case 6: /* arbitrary messages */
232 rdesc(6);
233 break;
234 case 7: /* object locations */
235 rlocs();
236 break;
237 case 8: /* action defaults */
238 rdflt();
239 break;
240 case 9: /* liquid assets */
241 rliq();
242 break;
243 case 10: /* class messages */
244 rdesc(10);
245 break;
246 case 11: /* hints */
247 rhints();
248 break;
249 case 12: /* magic messages */
250 rdesc(12);
251 break;
252 default:
253 printf("Invalid data section number: %d\n", sect);
254 for (;;)
255 putchar(next());
256 }
257 if (breakch != LF) /* routines return after "-1" */
258 FLUSHLF;
259 }
260 }
261
262 char nbf[12];
263
264 /* read initial location num */
265 int
266 rnum(void)
267 {
268 char *s;
269 tape = iotape; /* restart encryption tape */
270 for (s = nbf, *s = 0;; s++)
271 if ((*s = next()) == TAB || *s == '\n' || *s == LF)
272 break;
273 breakch = *s; /* save char for rtrav() */
274 *s = 0; /* got the number as ascii */
275 if (nbf[0] == '-')
276 return (-1); /* end of data */
277 return (atoi(nbf)); /* convert it to integer */
278 }
279
280 char *seekhere;
281
282 /* read description-format msgs */
283 void
284 rdesc(int sect)
285 {
286 int locc;
287 char *seekstart, *maystart;
288
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 */
294 /* unless sect 5 */
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;
300 break;
301 case 2:/* short descriptions */
302 stext[oldloc].seekadr = seekhere;
303 stext[oldloc].txtlen = maystart - seekstart;
304 break;
305 case 5:/* object descriptions */
306 ptext[oldloc].seekadr = seekhere;
307 ptext[oldloc].txtlen = maystart - seekstart;
308 break;
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;
314 break;
315 case 10: /* class messages */
316 ctext[clsses].seekadr = seekhere;
317 ctext[clsses].txtlen = maystart - seekstart;
318 cval[clsses++] = oldloc;
319 break;
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;
325 break;
326 default:
327 errx(1,"rdesc called with bad section");
328 }
329 seekhere += maystart - seekstart;
330 }
331 if (locc < 0) {
332 outsw = 0; /* turn off output */
333 seekhere += 3; /* -1<delimiter> */
334 return;
335 }
336 if (sect != 5 || (locc > 0 && locc < 100)) {
337 if (oldloc != locc) /* starting a new message */
338 seekstart = maystart;
339 oldloc = locc;
340 }
341 FLUSHLF; /* scan the line */
342 }
343 }
344
345 /* read travel table */
346 void
347 rtrav(void)
348 {
349 int locc;
350 struct travlist *t = NULL;
351 char *s;
352 char buf[12];
353 int len, m, n, entries = 0;
354
355 for (oldloc = -1;;) { /* get another line */
356 /* end of entry */
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); */
361 }
362 if (locc == -1)
363 return;
364 if (locc != oldloc) { /* getting a new entry */
365 t = travel[locc] = calloc(1, sizeof(*t));
366 if (t == NULL)
367 err(1, NULL);
368 /* printf("New travel list for %d\n",locc); */
369 entries = 0;
370 oldloc = locc;
371 }
372 for (s = buf;; s++) /* get the newloc number /ASCII */
373 if ((*s = next()) == TAB || *s == LF)
374 break;
375 *s = 0;
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 */
379 m = 0;
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 */
384 m = atoi(buf);
385 }
386 while (breakch != LF) { /* only do one line at a time */
387 if (t == NULL)
388 abort();
389 if (entries++) {
390 t->next = calloc(1, sizeof(*t));
391 if (t->next == NULL)
392 err(1, NULL);
393 t = t->next;
394 }
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); */
399 }
400 }
401 }
402 #ifdef DEBUG
403
404 /* travel options from this loc */
405 void
406 twrite(int loq)
407 {
408 struct travlist *t;
409 printf("If");
410 speak(&ltext[loq]);
411 printf("then\n");
412 for (t = travel[loq]; t != 0; t = t->next) {
413 printf("verb %d takes you to ", t->tverb);
414 if (t->tloc <= 300)
415 speak(&ltext[t->tloc]);
416 else
417 if (t->tloc <= 500)
418 printf("special code %d\n", t->tloc - 300);
419 else
420 rspeak(t->tloc - 500);
421 printf("under conditions %d\n", t->conditions);
422 }
423 }
424 #endif /* DEBUG */
425
426 /* read the vocabulary */
427 void
428 rvoc(void)
429 {
430 char *s;
431 int idx;
432 char buf[6];
433 for (;;) {
434 idx = rnum();
435 if (idx < 0)
436 break;
437 for (s = buf, *s = 0;; s++) /* get the word */
438 if ((*s = next()) == TAB || *s == '\n' || *s == LF
439 || *s == ' ')
440 break;
441 /* terminate word with newline, LF, tab, blank */
442 if (*s != '\n' && *s != LF)
443 FLUSHLF;/* can be comments */
444 *s = 0;
445 /* printf("\"%s\"=%d\n",buf,idx); */
446 vocab(buf, -2, idx);
447 }
448 /* prht(); */
449 }
450
451 /* initial object locations */
452 void
453 rlocs(void)
454 {
455 for (;;) {
456 if ((obj = rnum()) < 0)
457 break;
458 plac[obj] = rnum(); /* initial loc for this obj */
459 if (breakch == TAB) /* there's another entry */
460 fixd[obj] = rnum();
461 else
462 fixd[obj] = 0;
463 }
464 }
465
466 /* default verb messages */
467 void
468 rdflt(void)
469 {
470 for (;;) {
471 if ((verb = rnum()) < 0)
472 break;
473 actspk[verb] = rnum();
474 }
475 }
476
477 /* liquid assets &c: cond bits */
478 void
479 rliq(void)
480 {
481 int bitnum;
482 for (;;) { /* read new bit list */
483 if ((bitnum = rnum()) < 0)
484 break;
485 for (;;) { /* read locs for bits */
486 int n = rnum();
487 if (n < 0)
488 break;
489 cond[n] |= setbit[bitnum];
490 if (breakch == LF)
491 break;
492 }
493 }
494 }
495
496 void
497 rhints(void)
498 {
499 int hintnum, i;
500 hntmax = 0;
501 for (;;) {
502 if ((hintnum = rnum()) < 0)
503 break;
504 for (i = 1; i < 5; i++)
505 hints[hintnum][i] = rnum();
506 if (hintnum > hntmax)
507 hntmax = hintnum;
508 }
509 }
510
511
512 void
513 rspeak(int msg)
514 {
515 if (msg != 0)
516 speak(&rtext[msg]);
517 }
518
519
520 void
521 mspeak(int msg)
522 {
523 if (msg != 0)
524 speak(&mtext[msg]);
525 }
526
527
528 /* read, decrypt, and print a message (not ptext) */
529 /* msg is a pointer to seek address and length of mess */
530 void
531 speak(const struct text *msg)
532 {
533 char *s, nonfirst;
534
535 s = msg->seekadr;
536 nonfirst = 0;
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)) == '<')
545 break;
546 if (blklin && !nonfirst++)
547 putchar('\n');
548 do {
549 if (*tape == 0)
550 tape = iotape; /* rewind decryp tape */
551 putchar(*s ^ *tape);
552 } while ((*s++ ^ *tape++) != LF); /* better end with LF */
553 }
554 }
555
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 */
559 void
560 pspeak(int m, int skip)
561 {
562 char *s, nonfirst;
563 char *numst;
564 struct text *msg;
565 char *tbuf;
566
567 msg = &ptext[m];
568 if ((tbuf = (char *) malloc(msg->txtlen + 1)) == NULL)
569 err(1, NULL);
570 memcpy(tbuf, msg->seekadr, msg->txtlen + 1); /* Room to null */
571 s = tbuf;
572
573 nonfirst = 0;
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 */
577
578 /* Temporarily trash the string (cringe) */
579 *s++ = 0; /* decrypting number within the string */
580
581 if (atoi(numst) != 100 * skip && skip >= 0) {
582 while ((*s++ ^ *tape++) != LF) /* flush the line */
583 if (*tape == 0)
584 tape = iotape;
585 continue;
586 }
587 if ((*s ^ *tape) == '>' && (*(s + 1) ^ *(tape + 1)) == '$' &&
588 (*(s + 2) ^ *(tape + 2)) == '<')
589 break;
590 if (blklin && !nonfirst++)
591 putchar('\n');
592 do {
593 if (*tape == 0)
594 tape = iotape;
595 putchar(*s ^ *tape);
596 } while ((*s++ ^ *tape++) != LF); /* better end with LF */
597 if (skip < 0)
598 break;
599 }
600 free(tbuf);
601 }