(linenum→info "unix/slp.c:2238")

bsd-games/2.17/adventure/subr.c

    1: /*      $NetBSD: subr.c,v 1.10 2003/08/07 09:36:51 agc 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[] = "@(#)subr.c      8.1 (Berkeley) 5/31/93";
   41: #else
   42: __RCSID("$NetBSD: subr.c,v 1.10 2003/08/07 09:36:51 agc Exp $");
   43: #endif
   44: #endif                          /* not lint */
   45: 
   46: /*      Re-coding of advent in C: subroutines from main                 */
   47: 
   48: #include <stdio.h>
   49: #include <stdlib.h>
   50: #include "hdr.h"
   51: #include "extern.h"
   52: 
   53: /*              Statement functions     */
   54: int
   55: toting(objj)
   56:         int     objj;
   57: {
   58:         if (place[objj] == -1)
   59:                 return (TRUE);
   60:         else
   61:                 return (FALSE);
   62: }
   63: 
   64: int
   65: here(objj)
   66:         int     objj;
   67: {
   68:         if (place[objj] == loc || toting(objj))
   69:                 return (TRUE);
   70:         else
   71:                 return (FALSE);
   72: }
   73: 
   74: int
   75: at(objj)
   76:         int     objj;
   77: {
   78:         if (place[objj] == loc || fixed[objj] == loc)
   79:                 return (TRUE);
   80:         else
   81:                 return (FALSE);
   82: }
   83: 
   84: int
   85: liq2(pbotl)
   86:         int     pbotl;
   87: {
   88:         return ((1 - pbotl) * water + (pbotl / 2) * (water + oil));
   89: }
   90: 
   91: int
   92: liq()
   93: {
   94:         int     i;
   95:         i = prop[bottle];
   96:         if (i > -1 - i)
   97:                 return (liq2(i));
   98:         else
   99:                 return (liq2(-1 - i));
  100: }
  101: 
  102: int
  103: liqloc(locc)                    /* may want to clean this one up a bit */
  104:         int     locc;
  105: {
  106:         int     i, j, l;
  107:         i = cond[locc] / 2;
  108:         j = ((i * 2) % 8) - 5;
  109:         l = cond[locc] / 4;
  110:         l = l % 2;
  111:         return (liq2(j * l + 1));
  112: }
  113: 
  114: int
  115: bitset(l, n)
  116:         int     l, n;
  117: {
  118:         if (cond[l] & setbit[n])
  119:                 return (TRUE);
  120:         return (FALSE);
  121: }
  122: 
  123: int
  124: forced(locc)
  125:         int     locc;
  126: {
  127:         if (cond[locc] == 2)
  128:                 return (TRUE);
  129:         return (FALSE);
  130: }
  131: 
  132: int
  133: dark()
  134: {
  135:         if ((cond[loc] % 2) == 0 && (prop[lamp] == 0 || !here(lamp)))
  136:                 return (TRUE);
  137:         return (FALSE);
  138: }
  139: 
  140: int
  141: pct(n)
  142:         int     n;
  143: {
  144:         if (ran(100) < n)
  145:                 return (TRUE);
  146:         return (FALSE);
  147: }
  148: 
  149: 
  150: int
  151: fdwarf()
  152: {                               /* 71 */
  153:         int     i, j;
  154:         struct travlist *kk;
  155: 
  156:         if (newloc != loc && !forced(loc) && !bitset(loc, 3)) {
  157:                 for (i = 1; i <= 5; i++) {
  158:                         if (odloc[i] != newloc || !dseen[i])
  159:                                 continue;
  160:                         newloc = loc;
  161:                         rspeak(2);
  162:                         break;
  163:                 }
  164:         }
  165:         loc = newloc;          /* 74 */
  166:         if (loc == 0 || forced(loc) || bitset(newloc, 3))
  167:                 return (2000);
  168:         if (dflag == 0) {
  169:                 if (loc >= 15)
  170:                         dflag = 1;
  171:                 return (2000);
  172:         }
  173:         if (dflag == 1) {      /* 6000 */
  174:                 if (loc < 15 || pct(95))
  175:                         return (2000);
  176:                 dflag = 2;
  177:                 for (i = 1; i <= 2; i++) {
  178:                         j = 1 + ran(5);
  179:                         if (pct(50) && saved == -1)
  180:                                 dloc[j] = 0;        /* 6001 */
  181:                 }
  182:                 for (i = 1; i <= 5; i++) {
  183:                         if (dloc[i] == loc)
  184:                                 dloc[i] = daltlc;
  185:                         odloc[i] = dloc[i];  /* 6002 */
  186:                 }
  187:                 rspeak(3);
  188:                 drop(axe, loc);
  189:                 return (2000);
  190:         }
  191:         dtotal = attack = stick = 0;   /* 6010 */
  192:         for (i = 1; i <= 6; i++) {     /* loop to 6030 */
  193:                 if (dloc[i] == 0)
  194:                         continue;
  195:                 j = 1;
  196:                 for (kk = travel[dloc[i]]; kk != 0; kk = kk->next) {
  197:                         newloc = kk->tloc;
  198:                         if (newloc > 300 || newloc < 15 || newloc == odloc[i]
  199:                             || (j > 1 && newloc == tk[j - 1]) || j >= 20
  200:                             || newloc == dloc[i] || forced(newloc)
  201:                             || (i == 6 && bitset(newloc, 3))
  202:                             || kk->conditions == 100)
  203:                                 continue;
  204:                         tk[j++] = newloc;
  205:                 }
  206:                 tk[j] = odloc[i];     /* 6016 */
  207:                 if (j >= 2)
  208:                         j--;
  209:                 j = 1 + ran(j);
  210:                 odloc[i] = dloc[i];
  211:                 dloc[i] = tk[j];
  212:                 dseen[i] = (dseen[i] && loc >= 15) || (dloc[i] == loc || odloc[i] == loc);
  213:                 if (!dseen[i])
  214:                         continue;    /* i.e. goto 6030 */
  215:                 dloc[i] = loc;
  216:                 if (i == 6) { /* pirate's spotted him */
  217:                         if (loc == chloc || prop[chest] >= 0)
  218:                                 continue;
  219:                         k = 0;
  220:                         for (j = 50; j <= maxtrs; j++) {     /* loop to 6020 */
  221:                                 if (j == pyram && (loc == plac[pyram]
  222:                                         || loc == plac[emrald]))
  223:                                         goto l6020;
  224:                                 if (toting(j))
  225:                                         goto l6022;
  226:                 l6020:                if (here(j))
  227:                                         k = 1;
  228:                         }    /* 6020 */
  229:                         if (tally == tally2 + 1 && k == 0 && place[chest] == 0
  230:                             && here(lamp) && prop[lamp] == 1)
  231:                                 goto l6025;
  232:                         if (odloc[6] != dloc[6] && pct(20))
  233:                                 rspeak(127);
  234:                         continue;    /* to 6030 */
  235:         l6022:         rspeak(128);
  236:                         if (place[messag] == 0)
  237:                                 move(chest, chloc);
  238:                         move(messag, chloc2);
  239:                         for (j = 50; j <= maxtrs; j++) {     /* loop to 6023 */
  240:                                 if (j == pyram && (loc == plac[pyram]
  241:                                         || loc == plac[emrald]))
  242:                                         continue;
  243:                                 if (at(j) && fixed[j] == 0)
  244:                                         carry(j, loc);
  245:                                 if (toting(j))
  246:                                         drop(j, chloc);
  247:                         }
  248:         l6024:         dloc[6] = odloc[6] = chloc;
  249:                         dseen[6] = FALSE;
  250:                         continue;
  251:         l6025:         rspeak(186);
  252:                         move(chest, chloc);
  253:                         move(messag, chloc2);
  254:                         goto l6024;
  255:                 }
  256:                 dtotal++;     /* 6027 */
  257:                 if (odloc[i] != dloc[i])
  258:                         continue;
  259:                 attack++;
  260:                 if (knfloc >= 0)
  261:                         knfloc = loc;
  262:                 if (ran(1000) < 95 * (dflag - 2))
  263:                         stick++;
  264:         }                      /* 6030 */
  265:         if (dtotal == 0)
  266:                 return (2000);
  267:         if (dtotal != 1) {
  268:                 printf("There are %d threatening little dwarves ", dtotal);
  269:                 printf("in the room with you.\n");
  270:         } else
  271:                 rspeak(4);
  272:         if (attack == 0)
  273:                 return (2000);
  274:         if (dflag == 2)
  275:                 dflag = 3;
  276:         if (saved != -1)
  277:                 dflag = 20;
  278:         if (attack != 1) {
  279:                 printf("%d of them throw knives at you!\n", attack);
  280:                 k = 6;
  281: l82:            if (stick <= 1) { /* 82 */
  282:                         rspeak(k + stick);
  283:                         if (stick == 0)
  284:                                 return (2000);
  285:                 } else
  286:                         printf("%d of them get you!\n", stick);      /* 83 */
  287:                 oldlc2 = loc;
  288:                 return (99);
  289:         }
  290:         rspeak(5);
  291:         k = 52;
  292:         goto l82;
  293: }
  294: 
  295: 
  296: int
  297: march()
  298: {                               /* label 8              */
  299:         int     ll1, ll2;
  300: 
  301:         if ((tkk = travel[newloc = loc]) == 0)
  302:                 bug(26);
  303:         if (k == null)
  304:                 return (2);
  305:         if (k == cave) {       /* 40                   */
  306:                 if (loc < 8)
  307:                         rspeak(57);
  308:                 if (loc >= 8)
  309:                         rspeak(58);
  310:                 return (2);
  311:         }
  312:         if (k == look) {       /* 30                   */
  313:                 if (detail++ < 3)
  314:                         rspeak(15);
  315:                 wzdark = FALSE;
  316:                 abb[loc] = 0;
  317:                 return (2);
  318:         }
  319:         if (k == back) {       /* 20                   */
  320:                 switch (mback()) {
  321:                 case 2:
  322:                         return (2);
  323:                 case 9:
  324:                         goto l9;
  325:                 default:
  326:                         bug(100);
  327:                 }
  328:         }
  329:         oldlc2 = oldloc;
  330:         oldloc = loc;
  331: l9:
  332:         for (; tkk != 0; tkk = tkk->next)
  333:                 if (tkk->tverb == 1 || tkk->tverb == k)
  334:                         break;
  335:         if (tkk == 0) {
  336:                 badmove();
  337:                 return (2);
  338:         }
  339: l11:    ll1 = tkk->conditions;     /* 11                   */
  340:         ll2 = tkk->tloc;
  341:         newloc = ll1;          /* newloc=conditions    */
  342:         k = newloc % 100;      /* k used for prob      */
  343:         if (newloc <= 300) {
  344:                 if (newloc <= 100) {  /* 13                   */
  345:                         if (newloc != 0 && !pct(newloc))
  346:                                 goto l12;   /* 14   */
  347:         l16:           newloc = ll2;    /* newloc=location      */
  348:                         if (newloc <= 300)
  349:                                 return (2);
  350:                         if (newloc <= 500)
  351:                                 switch (specials()) {       /* to 30000           */
  352:                                 case 2:
  353:                                         return (2);
  354:                                 case 12:
  355:                                         goto l12;
  356:                                 case 99:
  357:                                         return (99);
  358:                                 default:
  359:                                         bug(101);
  360:                                 }
  361:                         rspeak(newloc - 500);
  362:                         newloc = loc;
  363:                         return (2);
  364:                 }
  365:                 if (toting(k) || (newloc > 200 && at(k)))
  366:                         goto l16;
  367:                 goto l12;
  368:         }
  369:         if (prop[k] != (newloc / 100) - 3)
  370:                 goto l16;     /* newloc still conditions */
  371: l12:                            /* alternative to probability move      */
  372:         for (; tkk != 0; tkk = tkk->next)
  373:                 if (tkk->tloc != ll2 || tkk->conditions != ll1)
  374:                         break;
  375:         if (tkk == 0)
  376:                 bug(25);
  377:         goto l11;
  378: }
  379: 
  380: 
  381: 
  382: int
  383: mback()
  384: {                               /* 20                   */
  385:         struct travlist *tk2, *j;
  386:         int     ll;
  387:         if (forced(k = oldloc))
  388:                 k = oldlc2;   /* k=location           */
  389:         oldlc2 = oldloc;
  390:         oldloc = loc;
  391:         tk2 = 0;
  392:         if (k == loc) {
  393:                 rspeak(91);
  394:                 return (2);
  395:         }
  396:         for (; tkk != 0; tkk = tkk->next) {    /* 21                   */
  397:                 ll = tkk->tloc;
  398:                 if (ll == k) {
  399:                         k = tkk->tverb;      /* k back to verb       */
  400:                         tkk = travel[loc];
  401:                         return (9);
  402:                 }
  403:                 if (ll <= 300) {
  404:                         j = travel[loc];
  405:                         if (forced(ll) && k == j->tloc)
  406:                                 tk2 = tkk;
  407:                 }
  408:         }
  409:         tkk = tk2;             /* 23                   */
  410:         if (tkk != 0) {
  411:                 k = tkk->tverb;
  412:                 tkk = travel[loc];
  413:                 return (9);
  414:         }
  415:         rspeak(140);
  416:         return (2);
  417: }
  418: 
  419: 
  420: int
  421: specials()
  422: {                               /* 30000                */
  423:         switch (newloc -= 300) {
  424:                 case 1:               /* 30100                */
  425:                 newloc = 99 + 100 - loc;
  426:                 if (holdng == 0 || (holdng == 1 && toting(emrald)))
  427:                         return (2);
  428:                 newloc = loc;
  429:                 rspeak(117);
  430:                 return (2);
  431:         case 2:                /* 30200                */
  432:                 drop(emrald, loc);
  433:                 return (12);
  434:         case 3:                /* to 30300             */
  435:                 return (trbridge());
  436:         default:
  437:                 bug(29);
  438:         }
  439: }
  440: 
  441: 
  442: int
  443: trbridge()
  444: {                               /* 30300                */
  445:         if (prop[troll] == 1) {
  446:                 pspeak(troll, 1);
  447:                 prop[troll] = 0;
  448:                 move(troll2, 0);
  449:                 move(troll2 + 100, 0);
  450:                 move(troll, plac[troll]);
  451:                 move(troll + 100, fixd[troll]);
  452:                 juggle(chasm);
  453:                 newloc = loc;
  454:                 return (2);
  455:         }
  456:         newloc = plac[troll] + fixd[troll] - loc;      /* 30310                */
  457:         if (prop[troll] == 0)
  458:                 prop[troll] = 1;
  459:         if (!toting(bear))
  460:                 return (2);
  461:         rspeak(162);
  462:         prop[chasm] = 1;
  463:         prop[troll] = 2;
  464:         drop(bear, newloc);
  465:         fixed[bear] = -1;
  466:         prop[bear] = 3;
  467:         if (prop[spices] < 0)
  468:                 tally2++;
  469:         oldlc2 = newloc;
  470:         return (99);
  471: }
  472: 
  473: 
  474: void
  475: badmove()
  476: {                               /* 20                   */
  477:         spk = 12;
  478:         if (k >= 43 && k <= 50)
  479:                 spk = 9;
  480:         if (k == 29 || k == 30)
  481:                 spk = 9;
  482:         if (k == 7 || k == 36 || k == 37)
  483:                 spk = 10;
  484:         if (k == 11 || k == 19)
  485:                 spk = 11;
  486:         if (verb == find || verb == invent)
  487:                 spk = 59;
  488:         if (k == 62 || k == 65)
  489:                 spk = 42;
  490:         if (k == 17)
  491:                 spk = 80;
  492:         rspeak(spk);
  493: }
  494: 
  495: void
  496: bug(n)
  497:         int     n;
  498: {
  499:         printf("Please tell jim@rand.org that fatal bug %d happened.\n", n);
  500:         exit(1);
  501: }
  502: 
  503: 
  504: void
  505: checkhints()
  506: {                               /* 2600 &c              */
  507:         int     hint;
  508:         for (hint = 4; hint <= hntmax; hint++) {
  509:                 if (hinted[hint])
  510:                         continue;
  511:                 if (!bitset(loc, hint))
  512:                         hintlc[hint] = -1;
  513:                 hintlc[hint]++;
  514:                 if (hintlc[hint] < hints[hint][1])
  515:                         continue;
  516:                 switch (hint) {
  517:                 case 4:       /* 40400 */
  518:                         if (prop[grate] == 0 && !here(keys))
  519:                                 goto l40010;
  520:                         goto l40020;
  521:                 case 5:       /* 40500 */
  522:                         if (here(bird) && toting(rod) && obj == bird)
  523:                                 goto l40010;
  524:                         continue;    /* i.e. goto l40030 */
  525:                 case 6:       /* 40600 */
  526:                         if (here(snake) && !here(bird))
  527:                                 goto l40010;
  528:                         goto l40020;
  529:                 case 7:       /* 40700 */
  530:                         if (atloc[loc] == 0 && atloc[oldloc] == 0
  531:                             && atloc[oldlc2] == 0 && holdng > 1)
  532:                                 goto l40010;
  533:                         goto l40020;
  534:                 case 8:       /* 40800 */
  535:                         if (prop[emrald] != -1 && prop[pyram] == -1)
  536:                                 goto l40010;
  537:                         goto l40020;
  538:                 case 9:
  539:                         goto l40010; /* 40900 */
  540:                 default:
  541:                         bug(27);
  542:                 }
  543: l40010: hintlc[hint] = 0;
  544:                 if (!yes(hints[hint][3], 0, 54))
  545:                         continue;
  546:                 printf("I am prepared to give you a hint, but it will ");
  547: