xref: /openbsd/games/adventure/main.c (revision 07ea8d15)
1 /*	$NetBSD: main.c,v 1.5 1996/05/21 21:53:09 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. 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 #ifndef lint
42 static char copyright[] =
43 "@(#) 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 static char rcsid[] = "$NetBSD: main.c,v 1.5 1996/05/21 21:53:09 mrg 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 <signal.h>
59 #include <stdio.h>
60 #include "hdr.h"
61 
62 main(argc,argv)
63 int argc;
64 char **argv;
65 {
66 	register int i;
67 	int rval,ll;
68 	struct text *kk;
69 	extern trapdel();
70 
71 	egid = getegid();
72 	setegid(getgid());
73 
74 	init();         /* Initialize everything */
75 	signal(SIGINT,trapdel);
76 
77 	if (argc > 1)   /* Restore file specified */
78 	{               /* Restart is label 8305 (Fortran) */
79 		i = restore(argv[1]);       /* See what we've got */
80 		switch(i)
81 		{
82 		    case 0:     /* The restore worked fine */
83 			yea=Start(0);
84 			k=null;
85 			unlink(argv[1]);/* Don't re-use the save */
86 			goto l8;        /* Get where we're going */
87 		    case 1:             /* Couldn't open it */
88 			exit(0);        /* So give up */
89 		    case 2:             /* Oops -- file was altered */
90 			rspeak(202);    /* You dissolve */
91 			exit(0);        /* File could be non-adventure */
92 		}                       /* So don't unlink it. */
93 	}
94 
95 	startup();              /* prepare for a user           */
96 
97 	for (;;)                        /* main command loop (label 2)  */
98 	{       if (newloc<9 && newloc!=0 && closng)
99 		{       rspeak(130);    /* if closing leave only by     */
100 			newloc=loc;     /*      main office             */
101 			if (!panic) clock2=15;
102 			panic=TRUE;
103 		}
104 
105 		rval=fdwarf();          /* dwarf stuff                  */
106 		if (rval==99) die(99);
107 
108 	l2000:  if (loc==0) die(99);    /* label 2000                   */
109 		kk = &stext[loc];
110 		if ((abb[loc]%abbnum)==0 || kk->seekadr==0)
111 			kk = &ltext[loc];
112 		if (!forced(loc) && dark(0))
113 		{       if (wzdark && pct(35))
114 			{       die(90);
115 				goto l2000;
116 			}
117 			kk = &rtext[16];
118 		}
119 	l2001:  if (toting(bear)) rspeak(141);  /* 2001                 */
120 		speak(kk);
121 		k=1;
122 		if (forced(loc))
123 			goto l8;
124 		if (loc==33 && pct(25)&&!closng) rspeak(8);
125 		if (!dark(0))
126 		{       abb[loc]++;
127 			for (i=atloc[loc]; i!=0; i=linkx[i])     /*2004  */
128 			{       obj=i;
129 				if (obj>100) obj -= 100;
130 				if (obj==steps && toting(nugget)) continue;
131 				if (prop[obj]<0)
132 				{       if (closed) continue;
133 					prop[obj]=0;
134 					if (obj==rug||obj==chain)
135 						prop[obj]=1;
136 					tally--;
137 					if (tally==tally2 && tally != 0)
138 						if (limit>35) limit=35;
139 				}
140 				ll =  prop[obj];   /* 2006         */
141 				if (obj==steps && loc==fixed[steps])
142 					ll = 1;
143 				pspeak(obj, ll);
144 			}                                       /* 2008 */
145 			goto l2012;
146 	l2009:          k=54;                   /* 2009                 */
147 	l2010:          spk=k;
148 	l2011:          rspeak(spk);
149 		}
150 	l2012:  verb=0;                         /* 2012                 */
151 		obj=0;
152 	l2600:	checkhints();                   /* to 2600-2602         */
153 		if (closed)
154 		{       if (prop[oyster]<0 && toting(oyster))
155 				pspeak(oyster,1);
156 			for (i=1; i<100; i++)
157 				if (toting(i)&&prop[i]<0)       /*2604  */
158 					prop[i] = -1-prop[i];
159 		}
160 		wzdark=dark(0);                 /* 2605                 */
161 		if (knfloc>0 && knfloc!=loc) knfloc=1;
162 		getin(&wd1,&wd2);
163 		if (delhit)                     /* user typed a DEL     */
164 		{       delhit=0;               /* reset counter        */
165 			copystr("quit",wd1);    /* pretend he's quitting*/
166 			*wd2=0;
167 		}
168 	l2608:  if ((foobar = -foobar)>0) foobar=0;     /* 2608         */
169 		/* should check here for "magic mode"                   */
170 		turns++;
171 		if (demo && turns>=SHORT) done(1);      /* to 13000     */
172 
173 		if (verb==say && *wd2!=0) verb=0;
174 		if (verb==say)
175 			goto l4090;
176 		if (tally==0 && loc>=15 && loc!=33) clock1--;
177 		if (clock1==0)
178 		{       closing();                      /* to 10000     */
179 			goto l19999;
180 		}
181 		if (clock1<0) clock2--;
182 		if (clock2==0)
183 		{       caveclose();            /* to 11000             */
184 			continue;               /* back to 2            */
185 		}
186 		if (prop[lamp]==1) limit--;
187 		if (limit<=30 && here(batter) && prop[batter]==0
188 			&& here(lamp))
189 		{       rspeak(188);            /* 12000                */
190 			prop[batter]=1;
191 			if (toting(batter)) drop(batter,loc);
192 			limit=limit+2500;
193 			lmwarn=FALSE;
194 			goto l19999;
195 		}
196 		if (limit==0)
197 		{       limit = -1;             /* 12400                */
198 			prop[lamp]=0;
199 			rspeak(184);
200 			goto l19999;
201 		}
202 		if (limit<0&&loc<=8)
203 		{       rspeak(185);            /* 12600                */
204 			gaveup=TRUE;
205 			done(2);                /* to 20000             */
206 		}
207 		if (limit<=30)
208 		{       if (lmwarn|| !here(lamp)) goto l19999;  /*12200*/
209 			lmwarn=TRUE;
210 			spk=187;
211 			if (place[batter]==0) spk=183;
212 			if (prop[batter]==1) spk=189;
213 			rspeak(spk);
214 		}
215 	l19999: k=43;
216 		if (liqloc(loc)==water) k=70;
217 		if (weq(wd1,"enter") &&
218 		    (weq(wd2,"strea")||weq(wd2,"water")))
219 			goto l2010;
220 		if (weq(wd1,"enter") && *wd2!=0) goto l2800;
221 		if ((!weq(wd1,"water")&&!weq(wd1,"oil"))
222 		    || (!weq(wd2,"plant")&&!weq(wd2,"door")))
223 			goto l2610;
224 		if (at(vocab(wd2,1))) copystr("pour",wd2);
225 
226 	l2610:  if (weq(wd1,"west"))
227 			if (++iwest==10) rspeak(17);
228 	l2630:  i=vocab(wd1,-1);
229 		if (i== -1)
230 		{       spk=60;                 /* 3000         */
231 			if (pct(20)) spk=61;
232 			if (pct(20)) spk=13;
233 			rspeak(spk);
234 			goto l2600;
235 		}
236 		k=i%1000;
237 		kq=i/1000+1;
238 		switch(kq)
239 		{   case 1: goto l8;
240 		    case 2: goto l5000;
241 		    case 3: goto l4000;
242 		    case 4: goto l2010;
243 		    default:
244 			printf("Error 22\n");
245 			exit(0);
246 		}
247 
248 	l8:
249 		switch(march())
250 		{   case 2: continue;           /* i.e. goto l2         */
251 		    case 99:
252 			switch(die(99))
253 			{   case 2000: goto l2000;
254 			    default: bug(111);
255 			}
256 		    default: bug(110);
257 		}
258 
259 	l2800:  copystr(wd2,wd1);
260 		*wd2=0;
261 		goto l2610;
262 
263 	l4000:  verb=k;
264 		spk=actspk[verb];
265 		if (*wd2!=0 && verb!=say) goto l2800;
266 		if (verb==say) obj= *wd2;
267 		if (obj!=0) goto l4090;
268 	l4080:
269 		switch(verb)
270 		{   case 1:                     /* take = 8010          */
271 			if (atloc[loc]==0||linkx[atloc[loc]]!=0) goto l8000;
272 			for (i=1; i<=5; i++)
273 				if (dloc[i]==loc&&dflag>=2) goto l8000;
274 			obj=atloc[loc];
275 			goto l9010;
276 		    case 2: case 3: case 9:     /* 8000 : drop,say,wave */
277 		    case 10: case 16: case 17:  /* calm,rub,toss        */
278 		    case 19: case 21: case 28:  /* find,feed,break      */
279 		    case 29:                    /* wake                 */
280 		l8000:  printf("%s what?\n",wd1);
281 			obj=0;
282 			goto l2600;
283 		    case 4: case 6:             /* 8040 open,lock       */
284 			spk=28;
285 			if (here(clam)) obj=clam;
286 			if (here(oyster)) obj=oyster;
287 			if (at(door)) obj=door;
288 			if (at(grate)) obj=grate;
289 			if (obj!=0 && here(chain)) goto l8000;
290 			if (here(chain)) obj=chain;
291 			if (obj==0) goto l2011;
292 			goto l9040;
293 		    case 5: goto l2009;         /* nothing              */
294 		    case 7: goto l9070;         /* on                   */
295 		    case 8: goto l9080;         /* off                  */
296 		    case 11: goto l8000;        /* walk                 */
297 		    case 12: goto l9120;        /* kill                 */
298 		    case 13: goto l9130;        /* pour                 */
299 		    case 14:                    /* eat: 8140            */
300 			if (!here(food)) goto l8000;
301 		l8142:  dstroy(food);
302 			spk=72;
303 			goto l2011;
304 		    case 15: goto l9150;        /* drink                */
305 		    case 18:                    /* quit: 8180           */
306 			gaveup=yes(22,54,54);
307 			if (gaveup) done(2);    /* 8185                 */
308 			goto l2012;
309 		    case 20:                    /* invent=8200          */
310 			spk=98;
311 			for (i=1; i<=100; i++)
312 			{       if (i!=bear && toting(i))
313 				{       if (spk==98) rspeak(99);
314 					blklin=FALSE;
315 					pspeak(i,-1);
316 					blklin=TRUE;
317 					spk=0;
318 				}
319 			}
320 			if (toting(bear)) spk=141;
321 			goto l2011;
322 		    case 22: goto l9220;        /* fill                 */
323 		    case 23: goto l9230;        /* blast                */
324 		    case 24:                    /* score: 8240          */
325 			scorng=TRUE;
326 			printf("If you were to quit now, you would score");
327 			printf(" %d out of a possible ",score());
328 			printf("%d.",mxscor);
329 			scorng=FALSE;
330 			gaveup=yes(143,54,54);
331 			if (gaveup) done(2);
332 			goto l2012;
333 		    case 25:                    /* foo: 8250            */
334 			k=vocab(wd1,3);
335 			spk=42;
336 			if (foobar==1-k) goto l8252;
337 			if (foobar!=0) spk=151;
338 			goto l2011;
339 		l8252:  foobar=k;
340 			if (k!=4) goto l2009;
341 			foobar=0;
342 			if (place[eggs]==plac[eggs]
343 				||(toting(eggs)&&loc==plac[eggs])) goto l2011;
344 			if (place[eggs]==0&&place[troll]==0&&prop[troll]==0)
345 				prop[troll]=1;
346 			k=2;
347 			if (here(eggs)) k=1;
348 			if (loc==plac[eggs]) k=0;
349 			move(eggs,plac[eggs]);
350 			pspeak(eggs,k);
351 			goto l2012;
352 		    case 26:                    /* brief=8260           */
353 			spk=156;
354 			abbnum=10000;
355 			detail=3;
356 			goto l2011;
357 		    case 27:                    /* read=8270            */
358 			if (here(magzin)) obj=magzin;
359 			if (here(tablet)) obj=obj*100+tablet;
360 			if (here(messag)) obj=obj*100+messag;
361 			if (closed&&toting(oyster)) obj=oyster;
362 			if (obj>100||obj==0||dark(0)) goto l8000;
363 			goto l9270;
364 		    case 30:                    /* suspend=8300         */
365 			spk=201;
366 			if (demo) goto l2011;
367 			printf("I can suspend your adventure for you so");
368 			printf(" you can resume later, but\n");
369 			printf("you will have to wait at least");
370 			printf(" %d minutes before continuing.",latncy);
371 			if (!yes(200,54,54)) goto l2012;
372 			datime(&saved,&savet);
373 			ciao(argv[0]);          /* Do we quit? */
374 			continue;               /* Maybe not */
375 		    case 31:                    /* hours=8310           */
376 			printf("Colossal cave is closed 9am-5pm Mon ");
377 			printf("through Fri except holidays.\n");
378 			goto l2012;
379 		    default: bug(23);
380 		}
381 
382 	l4090:
383 		switch(verb)
384 		{   case 1:                     /* take = 9010          */
385 	l9010:          switch(trtake())
386 			{   case 2011: goto l2011;
387 			    case 9220: goto l9220;
388 			    case 2009: goto l2009;
389 			    case 2012: goto l2012;
390 			    default: bug(102);
391 			}
392 	l9020:      case 2:                     /* drop = 9020          */
393 			switch(trdrop())
394 			{   case 2011: goto l2011;
395 			    case 19000: done(3);
396 			    case 2012: goto l2012;
397 			    default: bug(105);
398 			}
399 	l9030:      case 3:
400 			switch(trsay())
401 			{   case 2012: goto l2012;
402 			    case 2630: goto l2630;
403 			    default: bug(107);
404 			}
405 	l9040:      case 4:  case 6:            /* open, close          */
406 			switch(tropen())
407 			{   case 2011: goto l2011;
408 			    case 2010: goto l2010;
409 			    default: bug(106);
410 			}
411 		    case 5: goto l2009;         /* nothing              */
412 		    case 7:                     /* on   9070            */
413 	l9070:          if (!here(lamp))  goto l2011;
414 			spk=184;
415 			if (limit<0) goto l2011;
416 			prop[lamp]=1;
417 			rspeak(39);
418 			if (wzdark) goto l2000;
419 			goto l2012;
420 
421 		    case 8:                     /* off                  */
422 	l9080:          if (!here(lamp)) goto l2011;
423 			prop[lamp]=0;
424 			rspeak(40);
425 			if (dark(0)) rspeak(16);
426 			goto l2012;
427 
428 		    case 9:                     /* wave                 */
429 			if ((!toting(obj))&&(obj!=rod||!toting(rod2)))
430 				spk=29;
431 			if (obj!=rod||!at(fissur)||!toting(obj)||closng)
432 				goto l2011;
433 			prop[fissur]=1-prop[fissur];
434 			pspeak(fissur,2-prop[fissur]);
435 			goto l2012;
436 		    case 10: case 11: case 18:  /* calm, walk, quit     */
437 		    case 24: case 25: case 26:  /* score, foo, brief    */
438 		    case 30: case 31:           /* suspend, hours       */
439 			     goto l2011;
440 	l9120:      case 12:                    /* kill                 */
441 			switch(trkill())
442 			{   case 8000: goto l8000;
443 			    case 8: goto l8;
444 			    case 2011: goto l2011;
445 			    case 2608: goto l2608;
446 			    case 19000: done(3);
447 			    default: bug(112);
448 			}
449 	l9130:      case 13:                    /* pour                 */
450 			if (obj==bottle||obj==0) obj=liq(0);
451 			if (obj==0) goto l8000;
452 			if (!toting(obj)) goto l2011;
453 			spk=78;
454 			if (obj!=oil&&obj!=water) goto l2011;
455 			prop[bottle]=1;
456 			place[obj]=0;
457 			spk=77;
458 			if (!(at(plant)||at(door))) goto l2011;
459 			if (at(door))
460 			{       prop[door]=0;   /* 9132                 */
461 				if (obj==oil) prop[door]=1;
462 				spk=113+prop[door];
463 				goto l2011;
464 			}
465 			spk=112;
466 			if (obj!=water) goto l2011;
467 			pspeak(plant,prop[plant]+1);
468 			prop[plant]=(prop[plant]+2)% 6;
469 			prop[plant2]=prop[plant]/2;
470 			k=null;
471 			goto l8;
472 		    case 14:                    /* 9140 - eat           */
473 			if (obj==food) goto l8142;
474 			if (obj==bird||obj==snake||obj==clam||obj==oyster
475 			    ||obj==dwarf||obj==dragon||obj==troll
476 			    ||obj==bear) spk=71;
477 			goto l2011;
478 	l9150:      case 15:                    /* 9150 - drink         */
479 			if (obj==0&&liqloc(loc)!=water&&(liq(0)!=water
480 				||!here(bottle))) goto l8000;
481 			if (obj!=0&&obj!=water) spk=110;
482 			if (spk==110||liq(0)!=water||!here(bottle))
483 				goto l2011;
484 			prop[bottle]=1;
485 			place[water]=0;
486 			spk=74;
487 			goto l2011;
488 		    case 16:                    /* 9160: rub            */
489 			if (obj!=lamp) spk=76;
490 			goto l2011;
491 		    case 17:                    /* 9170: throw          */
492 			switch(trtoss())
493 			{   case 2011: goto l2011;
494 			    case 9020: goto l9020;
495 			    case 9120: goto l9120;
496 			    case 8: goto l8;
497 			    case 9210: goto l9210;
498 			    default: bug(113);
499 			}
500 		    case 19: case 20:           /* 9190: find, invent   */
501 			if (at(obj)||(liq(0)==obj&&at(bottle))
502 				||k==liqloc(loc)) spk=94;
503 			for (i=1; i<=5; i++)
504 				if (dloc[i]==loc&&dflag>=2&&obj==dwarf)
505 					spk=94;
506 			if (closed) spk=138;
507 			if (toting(obj)) spk=24;
508 			goto l2011;
509 	l9210:      case 21:                    /* feed                 */
510 			switch(trfeed())
511 			{   case 2011: goto l2011;
512 			    default: bug(114);
513 			}
514 	l9220:      case 22:                    /* fill                 */
515 			switch(trfill())
516 			{   case 2011: goto l2011;
517 			    case 8000: goto l8000;
518 			    case 9020: goto l9020;
519 			    default: bug(115);
520 			}
521 	l9230:      case 23:                    /* blast                */
522 			if (prop[rod2]<0||!closed) goto l2011;
523 			bonus=133;
524 			if (loc==115) bonus=134;
525 			if (here(rod2)) bonus=135;
526 			rspeak(bonus);
527 			done(2);
528 	l9270:      case 27:                    /* read                 */
529 			if (dark(0)) goto l5190;
530 			if (obj==magzin) spk=190;
531 			if (obj==tablet) spk=196;
532 			if (obj==messag) spk=191;
533 			if (obj==oyster&&hinted[2]&&toting(oyster)) spk=194;
534 			if (obj!=oyster||hinted[2]||!toting(oyster)
535 				||!closed) goto l2011;
536 			hinted[2]=yes(192,193,54);
537 			goto l2012;
538 	l9280:      case 28:                    /* break                */
539 			if (obj==mirror) spk=148;
540 			if (obj==vase&&prop[vase]==0)
541 			{       spk=198;
542 				if (toting(vase)) drop(vase,loc);
543 				prop[vase]=2;
544 				fixed[vase]= -1;
545 				goto l2011;
546 			}
547 			if (obj!=mirror||!closed) goto l2011;
548 			rspeak(197);
549 			done(3);
550 
551 	l9290:      case 29:                    /* wake                 */
552 			if (obj!=dwarf||!closed) goto l2011;
553 			rspeak(199);
554 			done(3);
555 
556 		    default: bug(24);
557 		}
558 
559 	l5000:
560 		obj=k;
561 		if (fixed[k]!=loc && !here(k)) goto l5100;
562 	l5010:  if (*wd2!=0) goto l2800;
563 		if (verb!=0) goto l4090;
564 		printf("What do you want to do with the %s?\n",wd1);
565 		goto l2600;
566 	l5100:  if (k!=grate) goto l5110;
567 		if (loc==1||loc==4||loc==7) k=dprssn;
568 		if (loc>9&&loc<15) k=entrnc;
569 		if (k!=grate) goto l8;
570 	l5110:  if (k!=dwarf) goto l5120;
571 		for (i=1; i<=5; i++)
572 			if (dloc[i]==loc&&dflag>=2) goto l5010;
573 	l5120:  if ((liq(0)==k&&here(bottle))||k==liqloc(loc)) goto l5010;
574 		if (obj!=plant||!at(plant2)||prop[plant2]==0) goto l5130;
575 		obj=plant2;
576 		goto l5010;
577 	l5130:  if (obj!=knife||knfloc!=loc) goto l5140;
578 		knfloc = -1;
579 		spk=116;
580 		goto l2011;
581 	l5140:  if (obj!=rod||!here(rod2)) goto l5190;
582 		obj=rod2;
583 		goto l5010;
584 	l5190:  if ((verb==find||verb==invent)&&*wd2==0) goto l5010;
585 		printf("I see no %s here\n",wd1);
586 		goto l2012;
587 	}
588 }
589