1 /* $NetBSD: subr.c,v 1.12 2009/08/12 04:28:27 dholland Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)subr.c 8.1 (Berkeley) 5/31/93";
42 __RCSID("$NetBSD: subr.c,v 1.12 2009/08/12 04:28:27 dholland Exp $");
46 /* Re-coding of advent in C: subroutines from main */
53 static void badmove(void);
54 static int bitset(int, int);
55 static int dropper(void);
57 static int mback(void);
58 static int specials(void);
59 static int trbridge(void);
61 /* Statement functions */
65 if (place
[objj
] == -1)
74 if (place
[objj
] == loc
|| toting(objj
))
83 if (place
[objj
] == loc
|| fixed
[objj
] == loc
)
92 return ((1 - pbotl
) * water
+ (pbotl
/ 2) * (water
+ oil
));
103 return (liq2(-1 - i
));
106 /* may want to clean this one up a bit */
112 j
= ((i
* 2) % 8) - 5;
115 return (liq2(j
* l
+ 1));
121 if (cond
[l
] & setbit
[n
])
137 if ((cond
[loc
] % 2) == 0 && (prop
[lamp
] == 0 || !here(lamp
)))
157 if (newloc
!= loc
&& !forced(loc
) && !bitset(loc
, 3)) {
158 for (i
= 1; i
<= 5; i
++) {
159 if (odloc
[i
] != newloc
|| !dseen
[i
])
166 loc
= newloc
; /* 74 */
167 if (loc
== 0 || forced(loc
) || bitset(newloc
, 3))
174 if (dflag
== 1) { /* 6000 */
175 if (loc
< 15 || pct(95))
178 for (i
= 1; i
<= 2; i
++) {
180 if (pct(50) && saved
== -1)
181 dloc
[j
] = 0; /* 6001 */
183 for (i
= 1; i
<= 5; i
++) {
186 odloc
[i
] = dloc
[i
]; /* 6002 */
192 dtotal
= attack
= stick
= 0; /* 6010 */
193 for (i
= 1; i
<= 6; i
++) { /* loop to 6030 */
197 for (kk
= travel
[dloc
[i
]]; kk
!= 0; kk
= kk
->next
) {
199 if (newloc
> 300 || newloc
< 15 || newloc
== odloc
[i
]
200 || (j
> 1 && newloc
== tk
[j
- 1]) || j
>= 20
201 || newloc
== dloc
[i
] || forced(newloc
)
202 || (i
== 6 && bitset(newloc
, 3))
203 || kk
->conditions
== 100)
207 tk
[j
] = odloc
[i
]; /* 6016 */
213 dseen
[i
] = (dseen
[i
] && loc
>= 15) ||
214 (dloc
[i
] == loc
|| odloc
[i
] == loc
);
216 continue; /* i.e. goto 6030 */
218 if (i
== 6) { /* pirate's spotted him */
219 if (loc
== chloc
|| prop
[chest
] >= 0)
222 for (j
= 50; j
<= maxtrs
; j
++) { /* loop to 6020 */
223 if (j
== pyramid
&& (loc
== plac
[pyramid
]
224 || loc
== plac
[emerald
]))
231 if (tally
== tally2
+ 1 && k
== 0 && place
[chest
] == 0
232 && here(lamp
) && prop
[lamp
] == 1)
234 if (odloc
[6] != dloc
[6] && pct(20))
236 continue; /* to 6030 */
238 if (place
[message
] == 0)
240 move(message
, chloc2
);
241 for (j
= 50; j
<= maxtrs
; j
++) { /* loop to 6023 */
242 if (j
== pyramid
&& (loc
== plac
[pyramid
]
243 || loc
== plac
[emerald
]))
245 if (at(j
) && fixed
[j
] == 0)
250 l6024
: dloc
[6] = odloc
[6] = chloc
;
255 move(message
, chloc2
);
259 if (odloc
[i
] != dloc
[i
])
264 if (ran(1000) < 95 * (dflag
- 2))
270 printf("There are %d threatening little dwarves ", dtotal
);
271 printf("in the room with you.\n");
281 printf("%d of them throw knives at you!\n", attack
);
283 l82
: if (stick
<= 1) { /* 82 */
288 printf("%d of them get you!\n", stick
); /* 83 */
304 if ((tkk
= travel
[newloc
= loc
]) == 0)
308 if (k
== cave
) { /* 40 */
315 if (k
== look
) { /* 30 */
322 if (k
== back
) { /* 20 */
335 for (; tkk
!= 0; tkk
= tkk
->next
)
336 if (tkk
->tverb
== 1 || tkk
->tverb
== k
)
342 l11
: ll1
= tkk
->conditions
; /* 11 */
344 newloc
= ll1
; /* newloc=conditions */
345 k
= newloc
% 100; /* k used for prob */
347 if (newloc
<= 100) { /* 13 */
348 if (newloc
!= 0 && !pct(newloc
))
350 l16
: newloc
= ll2
; /* newloc=location */
354 switch (specials()) { /* to 30000 */
364 rspeak(newloc
- 500);
368 if (toting(k
) || (newloc
> 200 && at(k
)))
372 if (prop
[k
] != (newloc
/ 100) - 3)
373 goto l16
; /* newloc still conditions */
374 l12
: /* alternative to probability move */
375 for (; tkk
!= 0; tkk
= tkk
->next
)
376 if (tkk
->tloc
!= ll2
|| tkk
->conditions
!= ll1
)
387 struct travlist
*tk2
, *j
;
389 if (forced(k
= oldloc
))
390 k
= oldloc2
; /* k=location */
398 for (; tkk
!= 0; tkk
= tkk
->next
) { /* 21 */
401 k
= tkk
->tverb
; /* k back to verb */
407 if (forced(ll
) && k
== j
->tloc
)
425 switch (newloc
-= 300) {
427 newloc
= 99 + 100 - loc
;
428 if (holding
== 0 || (holding
== 1 && toting(emerald
)))
436 case 3: /* to 30300 */
447 if (prop
[troll
] == 1) {
451 move(troll2
+ 100, 0);
452 move(troll
, plac
[troll
]);
453 move(troll
+ 100, fixd
[troll
]);
458 newloc
= plac
[troll
] + fixd
[troll
] - loc
; /* 30310 */
459 if (prop
[troll
] == 0)
469 if (prop
[spices
] < 0)
480 if (k
>= 43 && k
<= 50)
482 if (k
== 29 || k
== 30)
484 if (k
== 7 || k
== 36 || k
== 37)
486 if (k
== 11 || k
== 19)
488 if (verb
== find
|| verb
== invent
)
490 if (k
== 62 || k
== 65)
500 printf("Please tell jim@rand.org that fatal bug %d happened.\n", n
);
509 for (hint
= 4; hint
<= hintmax
; hint
++) {
512 if (!bitset(loc
, hint
))
515 if (hintlc
[hint
] < hints
[hint
][1])
519 if (prop
[grate
] == 0 && !here(keys
))
523 if (here(bird
) && toting(rod
) && obj
== bird
)
525 continue; /* i.e. goto l40030 */
527 if (here(snake
) && !here(bird
))
531 if (atloc
[loc
] == 0 && atloc
[oldloc
] == 0
532 && atloc
[oldloc2
] == 0 && holding
> 1)
536 if (prop
[emerald
] != -1 && prop
[pyramid
] == -1)
540 goto l40010
; /* 40900 */
544 l40010
: hintlc
[hint
] = 0;
545 if (!yes(hints
[hint
][3], 0, 54))
547 printf("I am prepared to give you a hint, but it will ");
548 printf("cost you %d points.\n", hints
[hint
][2]);
549 hinted
[hint
] = yes(175, hints
[hint
][4], 54);
550 l40020
: hintlc
[hint
] = 0;
561 i
= vocab(wd1
, -1, 0);
562 if (i
== 62 || i
== 65 || i
== 71 || i
== 2025) {
567 printf("\nOkay, \"%s\".\n", wd2
);
576 return (2011); /* 9010 */
578 if (obj
== plant
&& prop
[plant
] <= 0)
580 if (obj
== bear
&& prop
[bear
] == 1)
582 if (obj
== chain
&& prop
[bear
] != 0)
586 if (obj
== water
|| obj
== oil
) {
587 if (here(bottle
) && liq() == obj
) {
592 if (toting(bottle
) && prop
[bottle
] == 1)
594 if (prop
[bottle
] != 1)
600 l9017
: if (holding
>= 7) {
611 if (!toting(cage
)) { /* 9013 */
615 prop
[bird
] = 1; /* 9015 */
617 l9014
: if ((obj
== bird
|| obj
== cage
) && prop
[bird
] != 0)
618 carry(bird
+ cage
- obj
, loc
);
621 if (obj
== bottle
&& k
!= 0)
633 if (obj
== bottle
&& k
!= 0)
635 if (obj
== cage
&& prop
[bird
] != 0)
647 if (toting(rod2
) && obj
== rod
&& !toting(rod
))
651 if (obj
== bird
&& here(snake
)) {
659 if (obj
== coins
&& here(vend
)) { /* 9024 */
665 if (obj
== bird
&& at(dragon
) && prop
[dragon
] == 0) { /* 9025 */
669 if (place
[snake
] == plac
[snake
])
673 if (obj
== bear
&& at(troll
)) { /* 9026 */
676 move(troll
+ 100, 0);
677 move(troll2
, plac
[troll
]);
678 move(troll2
+ 100, fixd
[troll
]);
683 if (obj
!= vase
|| loc
== plac
[pillow
]) { /* 9027 */
687 prop
[vase
] = 2; /* 9028 */
690 pspeak(vase
, prop
[vase
] + 1);
700 if (obj
== clam
|| obj
== oyster
) {
707 if (!toting(trident
))
720 if (obj
== door
&& prop
[door
] == 1)
726 if (obj
== grate
|| obj
== chain
)
728 if (spk
!= 31 || !here(keys
))
732 spk
= 172; /* 9049: lock */
733 if (prop
[chain
] != 0)
735 if (loc
!= plac
[chain
])
748 if (prop
[chain
] == 0)
756 fixed
[bear
] = 2 - prop
[bear
];
766 k
= 34 + prop
[grate
]; /* 9043 */
770 k
= k
+ 2 * prop
[grate
];
779 for (i
= 1; i
<= 5; i
++)
780 if (dloc
[i
] == loc
&& dflag
>= 2)
784 if (obj
== 0) { /* 9122 */
788 obj
= obj
* 100 + snake
;
789 if (at(dragon
) && prop
[dragon
] == 0)
790 obj
= obj
* 100 + dragon
;
792 obj
= obj
* 100 + troll
;
793 if (here(bear
) && prop
[bear
] == 0)
794 obj
= obj
* 100 + bear
;
798 if (here(bird
) && verb
!= throw)
800 if (here(clam
) || here(oyster
))
801 obj
= 100 * obj
+ clam
;
806 if (obj
== bird
) { /* 9124 */
812 if (place
[snake
] == plac
[snake
])
818 if (obj
== clam
|| obj
== oyster
)
824 if (obj
== dwarf
&& closed
)
831 spk
= 165 + (prop
[bear
] + 1) / 2;
832 if (obj
!= dragon
|| prop
[dragon
] != 0)
838 if (!weq(wd1
, "y") && !weq(wd1
, "yes"))
843 k
= (plac
[dragon
] + fixd
[dragon
]) / 2;
844 move(dragon
+ 100, -1);
848 for (obj
= 1; obj
<= 100; obj
++)
849 if (place
[obj
] == plac
[dragon
] || place
[obj
] == fixd
[dragon
])
861 if (toting(rod2
) && obj
== rod
&& !toting(rod
))
865 if (obj
>= 50 && obj
<= maxtrs
&& at(troll
)) {
866 spk
= 159; /* 9178 */
869 move(troll
+ 100, 0);
870 drop(troll2
, plac
[troll
]);
871 drop(troll2
+ 100, fixd
[troll
]);
875 if (obj
== food
&& here(bear
)) {
876 obj
= bear
; /* 9177 */
881 for (i
= 1; i
<= 5; i
++) {
882 if (dloc
[i
] == loc
) {
884 if (ran(3) == 0 || saved
!= -1)
901 if (at(dragon
) && prop
[dragon
] == 0)
906 if (here(bear
) && prop
[bear
] == 0) {
926 if (obj
== snake
|| obj
== dragon
|| obj
== troll
) {
928 if (obj
== dragon
&& prop
[dragon
] != 0)
932 if (obj
!= snake
|| closed
|| !here(bird
))
971 if (liqloc(loc
) == 0)
973 if (liqloc(loc
) == 0 || !toting(vase
))
978 return (9020); /* advent/10 goes to 9024 */
980 if (obj
!= 0 && obj
!= bottle
)
982 if (obj
== 0 && !here(bottle
))
985 if (liqloc(loc
) == 0)
991 prop
[bottle
] = ((cond
[loc
] % 4) / 2) * 2;
1006 prop
[grate
] = prop
[fissure
] = 0;
1007 for (i
= 1; i
<= 6; i
++) {
1012 move(troll
+ 100, 0);
1013 move(troll2
, plac
[troll
]);
1014 move(troll2
+ 100, fixd
[troll
]);
1016 if (prop
[bear
] != 3)
1032 prop
[bottle
] = put(bottle
, 115, 1);
1033 prop
[plant
] = put(plant
, 115, 0);
1034 prop
[oyster
] = put(oyster
, 115, 0);
1035 prop
[lamp
] = put(lamp
, 115, 0);
1036 prop
[rod
] = put(rod
, 115, 0);
1037 prop
[dwarf
] = put(dwarf
, 115, 0);
1043 prop
[snake
] = put(snake
, 116, 1);
1044 prop
[bird
] = put(bird
, 116, 1);
1045 prop
[cage
] = put(cage
, 116, 0);
1046 prop
[rod2
] = put(rod2
, 116, 0);
1047 prop
[pillow
] = put(pillow
, 116, 0);
1049 prop
[mirror
] = put(mirror
, 115, 0);
1050 fixed
[mirror
] = 116;
1052 for (i
= 1; i
<= 100; i
++)