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