main.c revision 1.11 1 /* $NetBSD: main.c,v 1.11 1998/09/13 15:21:37 hubertf 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. 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.
27 *
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
38 * SUCH DAMAGE.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
44 The Regents of the University of California. All rights reserved.\n");
45 #endif /* not lint */
46
47 #ifndef lint
48 #if 0
49 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/2/93";
50 #else
51 __RCSID("$NetBSD: main.c,v 1.11 1998/09/13 15:21:37 hubertf Exp $");
52 #endif
53 #endif /* not lint */
54
55 /* Re-coding of advent in C: main program */
56
57 #include <sys/file.h>
58 #include <err.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <unistd.h>
62 #include "hdr.h"
63 #include "extern.h"
64
65 int main __P((int, char **));
66
67 int
68 main(argc, argv)
69 int argc;
70 char **argv;
71 {
72 int i;
73 int rval, ll;
74 struct text *kk;
75
76 /* adventure doesn't need setuid-ness, so, just get rid of it */
77 if (setuid(getuid()) < 0)
78 warn("setuid");
79
80 init(); /* Initialize everything */
81 signal(SIGINT, trapdel);
82
83 if (argc > 1) { /* Restore file specified */
84 /* Restart is label 8305 (Fortran) */
85 i = restore(argv[1]); /* See what we've got */
86 switch (i) {
87 case 0: /* The restore worked fine */
88 yea = Start();
89 k = null;
90 unlink(argv[1]); /* Don't re-use the save */
91 goto l8; /* Get where we're going */
92 case 1: /* Couldn't open it */
93 exit(0); /* So give up */
94 case 2: /* Oops -- file was altered */
95 rspeak(202); /* You dissolve */
96 exit(0); /* File could be non-adventure */
97 } /* So don't unlink it. */
98 }
99 startup(); /* prepare for a user */
100
101 for (;;) { /* main command loop (label 2) */
102 if (newloc < 9 && newloc != 0 && closng) {
103 rspeak(130); /* if closing leave only by */
104 newloc = loc; /* main office */
105 if (!panic)
106 clock2 = 15;
107 panic = TRUE;
108 }
109 rval = fdwarf(); /* dwarf stuff */
110 if (rval == 99)
111 die(99);
112
113 l2000: if (loc == 0)
114 die(99); /* label 2000 */
115 kk = &stext[loc];
116 if ((abb[loc] % abbnum) == 0 || kk->seekadr == 0)
117 kk = <ext[loc];
118 if (!forced(loc) && dark()) {
119 if (wzdark && pct(35)) {
120 die(90);
121 goto l2000;
122 }
123 kk = &rtext[16];
124 }
125 #if 0
126 l2001:
127 #endif
128 if (toting(bear))
129 rspeak(141); /* 2001 */
130 speak(kk);
131 k = 1;
132 if (forced(loc))
133 goto l8;
134 if (loc == 33 && pct(25) && !closng)
135 rspeak(8);
136 if (!dark()) {
137 abb[loc]++;
138 for (i = atloc[loc]; i != 0; i = links[i]) { /* 2004 */
139 obj = i;
140 if (obj > 100)
141 obj -= 100;
142 if (obj == steps && toting(nugget))
143 continue;
144 if (prop[obj] < 0) {
145 if (closed)
146 continue;
147 prop[obj] = 0;
148 if (obj == rug || obj == chain)
149 prop[obj] = 1;
150 tally--;
151 if (tally == tally2 && tally != 0)
152 if (limit > 35)
153 limit = 35;
154 }
155 ll = prop[obj]; /* 2006 */
156 if (obj == steps && loc == fixed[steps])
157 ll = 1;
158 pspeak(obj, ll);
159 } /* 2008 */
160 goto l2012;
161 l2009: k = 54; /* 2009 */
162 l2010: spk = k;
163 l2011: rspeak(spk);
164 }
165 l2012: verb = 0; /* 2012 */
166 obj = 0;
167 l2600: checkhints(); /* to 2600-2602 */
168 if (closed) {
169 if (prop[oyster] < 0 && toting(oyster))
170 pspeak(oyster, 1);
171 for (i = 1; i < 100; i++)
172 if (toting(i) && prop[i] < 0) /* 2604 */
173 prop[i] = -1 - prop[i];
174 }
175 wzdark = dark(); /* 2605 */
176 if (knfloc > 0 && knfloc != loc)
177 knfloc = 1;
178 getin(&wd1, &wd2);
179 if (delhit) { /* user typed a DEL */
180 delhit = 0; /* reset counter */
181 copystr("quit", wd1); /* pretend he's quitting */
182 *wd2 = 0;
183 }
184 l2608: if ((foobar = -foobar) > 0)
185 foobar = 0; /* 2608 */
186 /* should check here for "magic mode" */
187 turns++;
188 if (demo && turns >= SHORT)
189 done(1); /* to 13000 */
190
191 if (verb == say && *wd2 != 0)
192 verb = 0;
193 if (verb == say)
194 goto l4090;
195 if (tally == 0 && loc >= 15 && loc != 33)
196 clock1--;
197 if (clock1 == 0) {
198 closing(); /* to 10000 */
199 goto l19999;
200 }
201 if (clock1 < 0)
202 clock2--;
203 if (clock2 == 0) {
204 caveclose(); /* to 11000 */
205 continue; /* back to 2 */
206 }
207 if (prop[lamp] == 1)
208 limit--;
209 if (limit <= 30 && here(batter) && prop[batter] == 0
210 && here(lamp)) {
211 rspeak(188); /* 12000 */
212 prop[batter] = 1;
213 if (toting(batter))
214 drop(batter, loc);
215 limit = limit + 2500;
216 lmwarn = FALSE;
217 goto l19999;
218 }
219 if (limit == 0) {
220 limit = -1; /* 12400 */
221 prop[lamp] = 0;
222 rspeak(184);
223 goto l19999;
224 }
225 if (limit < 0 && loc <= 8) {
226 rspeak(185); /* 12600 */
227 gaveup = TRUE;
228 done(2); /* to 20000 */
229 }
230 if (limit <= 30) {
231 if (lmwarn || !here(lamp))
232 goto l19999; /* 12200 */
233 lmwarn = TRUE;
234 spk = 187;
235 if (place[batter] == 0)
236 spk = 183;
237 if (prop[batter] == 1)
238 spk = 189;
239 rspeak(spk);
240 }
241 l19999: k = 43;
242 if (liqloc(loc) == water)
243 k = 70;
244 if (weq(wd1, "enter") &&
245 (weq(wd2, "strea") || weq(wd2, "water")))
246 goto l2010;
247 if (weq(wd1, "enter") && *wd2 != 0)
248 goto l2800;
249 if ((!weq(wd1, "water") && !weq(wd1, "oil"))
250 || (!weq(wd2, "plant") && !weq(wd2, "door")))
251 goto l2610;
252 if (at(vocab(wd2, 1, 0)))
253 copystr("pour", wd2);
254
255 l2610: if (weq(wd1, "west"))
256 if (++iwest == 10)
257 rspeak(17);
258 l2630: i = vocab(wd1, -1, 0);
259 if (i == -1) {
260 spk = 60; /* 3000 */
261 if (pct(20))
262 spk = 61;
263 if (pct(20))
264 spk = 13;
265 rspeak(spk);
266 goto l2600;
267 }
268 k = i % 1000;
269 kq = i / 1000 + 1;
270 switch (kq) {
271 case 1:
272 goto l8;
273 case 2:
274 goto l5000;
275 case 3:
276 goto l4000;
277 case 4:
278 goto l2010;
279 default:
280 printf("Error 22\n");
281 exit(0);
282 }
283
284 l8:
285 switch (march()) {
286 case 2:
287 continue; /* i.e. goto l2 */
288 case 99:
289 die(99);
290 goto l2000;
291 default:
292 bug(110);
293 }
294
295 l2800: copystr(wd2, wd1);
296 *wd2 = 0;
297 goto l2610;
298
299 l4000: verb = k;
300 spk = actspk[verb];
301 if (*wd2 != 0 && verb != say)
302 goto l2800;
303 if (verb == say)
304 obj = *wd2;
305 if (obj != 0)
306 goto l4090;
307 #if 0
308 l4080:
309 #endif
310 switch (verb) {
311 case 1: /* take = 8010 */
312 if (atloc[loc] == 0 || links[atloc[loc]] != 0)
313 goto l8000;
314 for (i = 1; i <= 5; i++)
315 if (dloc[i] == loc && dflag >= 2)
316 goto l8000;
317 obj = atloc[loc];
318 goto l9010;
319 case 2:
320 case 3:
321 case 9: /* 8000 : drop,say,wave */
322 case 10:
323 case 16:
324 case 17: /* calm,rub,toss */
325 case 19:
326 case 21:
327 case 28: /* find,feed,break */
328 case 29: /* wake */
329 l8000: printf("%s what?\n", wd1);
330 obj = 0;
331 goto l2600;
332 case 4:
333 case 6: /* 8040 open,lock */
334 spk = 28;
335 if (here(clam))
336 obj = clam;
337 if (here(oyster))
338 obj = oyster;
339 if (at(door))
340 obj = door;
341 if (at(grate))
342 obj = grate;
343 if (obj != 0 && here(chain))
344 goto l8000;
345 if (here(chain))
346 obj = chain;
347 if (obj == 0)
348 goto l2011;
349 goto l9040;
350 case 5:
351 goto l2009; /* nothing */
352 case 7:
353 goto l9070; /* on */
354 case 8:
355 goto l9080; /* off */
356 case 11:
357 goto l8000; /* walk */
358 case 12:
359 goto l9120; /* kill */
360 case 13:
361 goto l9130; /* pour */
362 case 14: /* eat: 8140 */
363 if (!here(food))
364 goto l8000;
365 l8142: dstroy(food);
366 spk = 72;
367 goto l2011;
368 case 15:
369 goto l9150; /* drink */
370 case 18: /* quit: 8180 */
371 gaveup = yes(22, 54, 54);
372 if (gaveup)
373 done(2); /* 8185 */
374 goto l2012;
375 case 20: /* invent=8200 */
376 spk = 98;
377 for (i = 1; i <= 100; i++) {
378 if (i != bear && toting(i)) {
379 if (spk == 98)
380 rspeak(99);
381 blklin = FALSE;
382 pspeak(i, -1);
383 blklin = TRUE;
384 spk = 0;
385 }
386 }
387 if (toting(bear))
388 spk = 141;
389 goto l2011;
390 case 22:
391 goto l9220; /* fill */
392 case 23:
393 goto l9230; /* blast */
394 case 24: /* score: 8240 */
395 scorng = TRUE;
396 printf("If you were to quit now, you would score");
397 printf(" %d out of a possible ", score());
398 printf("%d.", mxscor);
399 scorng = FALSE;
400 gaveup = yes(143, 54, 54);
401 if (gaveup)
402 done(2);
403 goto l2012;
404 case 25: /* foo: 8250 */
405 k = vocab(wd1, 3, 0);
406 spk = 42;
407 if (foobar == 1 - k)
408 goto l8252;
409 if (foobar != 0)
410 spk = 151;
411 goto l2011;
412 l8252: foobar = k;
413 if (k != 4)
414 goto l2009;
415 foobar = 0;
416 if (place[eggs] == plac[eggs]
417 || (toting(eggs) && loc == plac[eggs]))
418 goto l2011;
419 if (place[eggs] == 0 && place[troll] == 0 && prop[troll] == 0)
420 prop[troll] = 1;
421 k = 2;
422 if (here(eggs))
423 k = 1;
424 if (loc == plac[eggs])
425 k = 0;
426 move(eggs, plac[eggs]);
427 pspeak(eggs, k);
428 goto l2012;
429 case 26: /* brief=8260 */
430 spk = 156;
431 abbnum = 10000;
432 detail = 3;
433 goto l2011;
434 case 27: /* read=8270 */
435 if (here(magzin))
436 obj = magzin;
437 if (here(tablet))
438 obj = obj * 100 + tablet;
439 if (here(messag))
440 obj = obj * 100 + messag;
441 if (closed && toting(oyster))
442 obj = oyster;
443 if (obj > 100 || obj == 0 || dark())
444 goto l8000;
445 goto l9270;
446 case 30: /* suspend=8300 */
447 spk = 201;
448 if (demo)
449 goto l2011;
450 printf("I can suspend your adventure for you so");
451 printf(" you can resume later, but\n");
452 printf("you will have to wait at least");
453 printf(" %d minutes before continuing.", latncy);
454 if (!yes(200, 54, 54))
455 goto l2012;
456 datime(&saved, &savet);
457 ciao(); /* Do we quit? */
458 continue; /* Maybe not */
459 case 31: /* hours=8310 */
460 printf("Colossal cave is closed 9am-5pm Mon ");
461 printf("through Fri except holidays.\n");
462 goto l2012;
463 default:
464 bug(23);
465 }
466
467 l4090:
468 switch (verb) {
469 case 1: /* take = 9010 */
470 l9010: switch (trtake()) {
471 case 2011:
472 goto l2011;
473 case 9220:
474 goto l9220;
475 case 2009:
476 goto l2009;
477 case 2012:
478 goto l2012;
479 default:
480 bug(102);
481 }
482 l9020: case 2: /* drop = 9020 */
483 switch (trdrop()) {
484 case 2011:
485 goto l2011;
486 case 19000:
487 done(3);
488 case 2012:
489 goto l2012;
490 default:
491 bug(105);
492 }
493 #if 0
494 l9030:
495 #endif
496 case 3:
497 switch (trsay()) {
498 case 2012:
499 goto l2012;
500 case 2630:
501 goto l2630;
502 default:
503 bug(107);
504 }
505 l9040: case 4:
506 case 6: /* open, close */
507 switch (tropen()) {
508 case 2011:
509 goto l2011;
510 case 2010:
511 goto l2010;
512 default:
513 bug(106);
514 }
515 case 5:
516 goto l2009; /* nothing */
517 case 7: /* on 9070 */
518 l9070: if (!here(lamp))
519 goto l2011;
520 spk = 184;
521 if (limit < 0)
522 goto l2011;
523 prop[lamp] = 1;
524 rspeak(39);
525 if (wzdark)
526 goto l2000;
527 goto l2012;
528
529 case 8: /* off */
530 l9080: if (!here(lamp))
531 goto l2011;
532 prop[lamp] = 0;
533 rspeak(40);
534 if (dark())
535 rspeak(16);
536 goto l2012;
537
538 case 9: /* wave */
539 if ((!toting(obj)) && (obj != rod || !toting(rod2)))
540 spk = 29;
541 if (obj != rod || !at(fissur) || !toting(obj) || closng)
542 goto l2011;
543 prop[fissur] = 1 - prop[fissur];
544 pspeak(fissur, 2 - prop[fissur]);
545 goto l2012;
546 case 10:
547 case 11:
548 case 18: /* calm, walk, quit */
549 case 24:
550 case 25:
551 case 26: /* score, foo, brief */
552 case 30:
553 case 31: /* suspend, hours */
554 goto l2011;
555 l9120: case 12:/* kill */
556 switch (trkill()) {
557 case 8000:
558 goto l8000;
559 case 8:
560 goto l8;
561 case 2011:
562 goto l2011;
563 case 2608:
564 goto l2608;
565 case 19000:
566 done(3);
567 default:
568 bug(112);
569 }
570 l9130: case 13:/* pour */
571 if (obj == bottle || obj == 0)
572 obj = liq();
573 if (obj == 0)
574 goto l8000;
575 if (!toting(obj))
576 goto l2011;
577 spk = 78;
578 if (obj != oil && obj != water)
579 goto l2011;
580 prop[bottle] = 1;
581 place[obj] = 0;
582 spk = 77;
583 if (!(at(plant) || at(door)))
584 goto l2011;
585 if (at(door)) {
586 prop[door] = 0; /* 9132 */
587 if (obj == oil)
588 prop[door] = 1;
589 spk = 113 + prop[door];
590 goto l2011;
591 }
592 spk = 112;
593 if (obj != water)
594 goto l2011;
595 pspeak(plant, prop[plant] + 1);
596 prop[plant] = (prop[plant] + 2) % 6;
597 prop[plant2] = prop[plant] / 2;
598 k = null;
599 goto l8;
600 case 14: /* 9140 - eat */
601 if (obj == food)
602 goto l8142;
603 if (obj == bird || obj == snake || obj == clam || obj == oyster
604 || obj == dwarf || obj == dragon || obj == troll
605 || obj == bear)
606 spk = 71;
607 goto l2011;
608 l9150: case 15:/* 9150 - drink */
609 if (obj == 0 && liqloc(loc) != water && (liq() != water
610 || !here(bottle)))
611 goto l8000;
612 if (obj != 0 && obj != water)
613 spk = 110;
614 if (spk == 110 || liq() != water || !here(bottle))
615 goto l2011;
616 prop[bottle] = 1;
617 place[water] = 0;
618 spk = 74;
619 goto l2011;
620 case 16: /* 9160: rub */
621 if (obj != lamp)
622 spk = 76;
623 goto l2011;
624 case 17: /* 9170: throw */
625 switch (trtoss()) {
626 case 2011:
627 goto l2011;
628 case 9020:
629 goto l9020;
630 case 9120:
631 goto l9120;
632 case 8:
633 goto l8;
634 case 9210:
635 goto l9210;
636 default:
637 bug(113);
638 }
639 case 19:
640 case 20: /* 9190: find, invent */
641 if (at(obj) || (liq() == obj && at(bottle))
642 || k == liqloc(loc))
643 spk = 94;
644 for (i = 1; i <= 5; i++)
645 if (dloc[i] == loc && dflag >= 2 && obj == dwarf)
646 spk = 94;
647 if (closed)
648 spk = 138;
649 if (toting(obj))
650 spk = 24;
651 goto l2011;
652 l9210: case 21:/* feed */
653 switch (trfeed()) {
654 case 2011:
655 goto l2011;
656 default:
657 bug(114);
658 }
659 l9220: case 22:/* fill */
660 switch (trfill()) {
661 case 2011:
662 goto l2011;
663 case 8000:
664 goto l8000;
665 case 9020:
666 goto l9020;
667 default:
668 bug(115);
669 }
670 l9230: case 23:/* blast */
671 if (prop[rod2] < 0 || !closed)
672 goto l2011;
673 bonus = 133;
674 if (loc == 115)
675 bonus = 134;
676 if (here(rod2))
677 bonus = 135;
678 rspeak(bonus);
679 done(2);
680 l9270: case 27:/* read */
681 if (dark())
682 goto l5190;
683 if (obj == magzin)
684 spk = 190;
685 if (obj == tablet)
686 spk = 196;
687 if (obj == messag)
688 spk = 191;
689 if (obj == oyster && hinted[2] && toting(oyster))
690 spk = 194;
691 if (obj != oyster || hinted[2] || !toting(oyster)
692 || !closed)
693 goto l2011;
694 hinted[2] = yes(192, 193, 54);
695 goto l2012;
696 #if 0
697 l9280:
698 #endif
699 case 28: /* break */
700 if (obj == mirror)
701 spk = 148;
702 if (obj == vase && prop[vase] == 0) {
703 spk = 198;
704 if (toting(vase))
705 drop(vase, loc);
706 prop[vase] = 2;
707 fixed[vase] = -1;
708 goto l2011;
709 }
710 if (obj != mirror || !closed)
711 goto l2011;
712 rspeak(197);
713 done(3);
714 #if 0
715 l9290:
716 #endif
717 case 29: /* wake */
718 if (obj != dwarf || !closed)
719 goto l2011;
720 rspeak(199);
721 done(3);
722
723 default:
724 bug(24);
725 }
726
727 l5000:
728 obj = k;
729 if (fixed[k] != loc && !here(k))
730 goto l5100;
731 l5010: if (*wd2 != 0)
732 goto l2800;
733 if (verb != 0)
734 goto l4090;
735 printf("What do you want to do with the %s?\n", wd1);
736 goto l2600;
737 l5100: if (k != grate)
738 goto l5110;
739 if (loc == 1 || loc == 4 || loc == 7)
740 k = dprssn;
741 if (loc > 9 && loc < 15)
742 k = entrnc;
743 if (k != grate)
744 goto l8;
745 l5110: if (k != dwarf)
746 goto l5120;
747 for (i = 1; i <= 5; i++)
748 if (dloc[i] == loc && dflag >= 2)
749 goto l5010;
750 l5120: if ((liq() == k && here(bottle)) || k == liqloc(loc))
751 goto l5010;
752 if (obj != plant || !at(plant2) || prop[plant2] == 0)
753 goto l5130;
754 obj = plant2;
755 goto l5010;
756 l5130: if (obj != knife || knfloc != loc)
757 goto l5140;
758 knfloc = -1;
759 spk = 116;
760 goto l2011;
761 l5140: if (obj != rod || !here(rod2))
762 goto l5190;
763 obj = rod2;
764 goto l5010;
765 l5190: if ((verb == find || verb == invent) && *wd2 == 0)
766 goto l5010;
767 printf("I see no %s here\n", wd1);
768 goto l2012;
769 }
770 }
771