SPK is no longer global.
[open-adventure.git] / actions.c
1 #include <stdlib.h>
2 #include <stdbool.h>
3 #include "advent.h"
4 #include "database.h"
5
6 /* Limit visibility of ugly globals.  Eventually these should go away. */
7 extern long K, WD1, WD1X, WD2, WD2X;
8
9 static long SPK;        /* This should go away too */
10
11 /*
12  * Action handlers.  Eventually we'll do lookup through a method table
13  * that calls these.  Absolutely nothing like the original FORTRAN.
14  */
15
16 static int fill(long);
17
18 static int attack(FILE *input, long verb, long obj)
19 /*  Attack.  Assume target if unambiguous.  "Throw" also links here.
20  *  Attackable objects fall into two categories: enemies (snake,
21  *  dwarf, etc.)  and others (bird, clam, machine).  Ambiguous if 2
22  *  enemies, or no enemies but 2 others. */
23 {
24     int i =ATDWRF(game.loc);
25     if (obj == 0) {
26         if (i > 0)
27             obj=DWARF;
28         if (HERE(SNAKE))obj=obj*NOBJECTS+SNAKE;
29         if (AT(DRAGON) && game.prop[DRAGON] == 0)obj=obj*NOBJECTS+DRAGON;
30         if (AT(TROLL))obj=obj*NOBJECTS+TROLL;
31         if (AT(OGRE))obj=obj*NOBJECTS+OGRE;
32         if (HERE(BEAR) && game.prop[BEAR] == 0)obj=obj*NOBJECTS+BEAR;
33         if (obj > NOBJECTS) return(8000);
34         if (obj == 0) {
35             /* Can't attack bird or machine by throwing axe. */
36             if (HERE(BIRD) && verb != THROW)obj=BIRD;
37             if (HERE(VEND) && verb != THROW)obj=obj*NOBJECTS+VEND;
38             /* Clam and oyster both treated as clam for intransitive case;
39              * no harm done. */
40             if (HERE(CLAM) || HERE(OYSTER))obj=NOBJECTS*obj+CLAM;
41             if (obj > NOBJECTS) return(8000);
42         }
43     }
44     if (obj == BIRD) {
45         SPK=137;
46         if (game.closed)
47         {
48             RSPEAK(SPK);
49             return 2012;
50         }
51         DSTROY(BIRD);
52         game.prop[BIRD]=0;
53         SPK=45;
54     }
55     if (obj == VEND) {
56         PSPEAK(VEND,game.prop[VEND]+2);
57         game.prop[VEND]=3-game.prop[VEND];
58         return(2012);
59     }
60
61     if (obj == 0)SPK=44;
62     if (obj == CLAM || obj == OYSTER)SPK=150;
63     if (obj == SNAKE)SPK=46;
64     if (obj == DWARF)SPK=49;
65     if (obj == DWARF && game.closed) return(19000);
66     if (obj == DRAGON)SPK=167;
67     if (obj == TROLL)SPK=157;
68     if (obj == OGRE)SPK=203;
69     if (obj == OGRE && i > 0) {
70         RSPEAK(SPK);
71         RSPEAK(6);
72         DSTROY(OGRE);
73         int k=0;
74         for (i=1; i < PIRATE; i++) {
75             if (game.dloc[i] == game.loc) {
76                 ++k;
77                 game.dloc[i]=61;
78                 game.dseen[i]=false;
79             }
80         }
81         SPK=SPK+1+1/k;
82         RSPEAK(SPK);
83         return 2012;
84     }
85     if (obj == BEAR)SPK=165+(game.prop[BEAR]+1)/2;
86     if (obj != DRAGON || game.prop[DRAGON] != 0) {RSPEAK(SPK); return 2012;}
87     /*  Fun stuff for dragon.  If he insists on attacking it, win!
88      *  Set game.prop to dead, move dragon to central loc (still
89      *  fixed), move rug there (not fixed), and move him there,
90      *  too.  Then do a null motion to get new description. */
91     RSPEAK(49);
92     GETIN(input,&WD1,&WD1X,&WD2,&WD2X);
93     if (WD1 != MAKEWD(25) && WD1 != MAKEWD(250519))
94         return(2607);
95     PSPEAK(DRAGON,3);
96     game.prop[DRAGON]=1;
97     game.prop[RUG]=0;
98     int k=(PLAC[DRAGON]+FIXD[DRAGON])/2;
99     MOVE(DRAGON+NOBJECTS,-1);
100     MOVE(RUG+NOBJECTS,0);
101     MOVE(DRAGON,k);
102     MOVE(RUG,k);
103     DROP(BLOOD,k);
104     for (obj=1; obj<=NOBJECTS; obj++) {
105         if (game.place[obj] == PLAC[DRAGON] || game.place[obj] == FIXD[DRAGON])
106             MOVE(obj,k);
107     }
108     game.loc=k;
109     K=NUL;      /* FIXME: error if removed */
110     return(8);
111 }
112
113 static int bigwords(long foo)
114 /*  FEE FIE FOE FOO (AND FUM).  Advance to next state if given in proper order.
115  *  Look up WD1 in section 3 of vocab to determine which word we've got.  Last
116  *  word zips the eggs back to the giant room (unless already there). */
117 {
118     int k=VOCAB(foo,3);
119     SPK=42;
120     if (game.foobar != 1-k) {
121         if (game.foobar != 0)SPK=151;
122         RSPEAK(SPK);
123         return 2012;
124     } else {
125         game.foobar=k;
126         if (k != 4) {
127             RSPEAK(54);
128             return 2012;
129         }
130         game.foobar=0;
131         if (game.place[EGGS]==PLAC[EGGS] || (TOTING(EGGS) && game.loc==PLAC[EGGS])) {
132             RSPEAK(SPK);
133             return 2012;
134         }
135         /*  Bring back troll if we steal the eggs back from him before
136          *  crossing. */
137         if (game.place[EGGS]==0 && game.place[TROLL]==0 && game.prop[TROLL]==0)
138             game.prop[TROLL]=1;
139         k=2;
140         if (HERE(EGGS))k=1;
141         if (game.loc == PLAC[EGGS])k=0;
142         MOVE(EGGS,PLAC[EGGS]);
143         PSPEAK(EGGS,k);
144         return(2012);
145     }
146 }
147
148 static int bivalve(token_t verb, token_t obj)
149 /* Clam/oyster actions */
150 {
151     int spk, k=0;
152     if (obj == OYSTER)k=1;
153     spk=124+k;
154     if (TOTING(obj))spk=120+k;
155     if (!TOTING(TRIDNT))spk=122+k;
156     if (verb == LOCK)spk=61;
157     if (spk == 124) {
158         DSTROY(CLAM);
159         DROP(OYSTER,game.loc);
160         DROP(PEARL,105);
161     }
162     RSPEAK(spk);
163     return 2012;
164 }
165
166 static int blast(void)
167 /*  Blast.  No effect unless you've got dynamite, which is a neat trick! */
168 {
169     if (game.prop[ROD2] < 0 || !game.closed)
170     {
171         RSPEAK(67);
172         return 2012;
173     }
174     game.bonus=133;
175     if (game.loc == 115)
176         game.bonus=134;
177     if (HERE(ROD2))
178         game.bonus=135;
179     RSPEAK(game.bonus);
180     score(0);
181 }
182
183 static int vbreak(token_t obj)
184 /*  Break.  Only works for mirror in repository and, of course, the vase. */
185 {
186     /* FIXME: defaults from ACTSPK */
187     if (obj == MIRROR)SPK=148;
188     if (obj == VASE && game.prop[VASE] == 0) {
189         SPK=198;
190         if (TOTING(VASE))DROP(VASE,game.loc);
191         game.prop[VASE]=2;
192         game.fixed[VASE]= -1;
193     } else {
194         if (obj == MIRROR && game.closed) {
195             RSPEAK(197);
196             return(190000);
197         }
198     }
199     RSPEAK(SPK);
200     return 2012;
201 }
202
203 static int brief(void)
204 /*  Brief.  Intransitive only.  Suppress long descriptions after first time. */
205 {
206     game.abbnum=10000;
207     game.detail=3;
208     RSPEAK(156);
209     return 2012;
210 }
211
212 static int carry(long obj)
213 /*  Carry an object.  Special cases for bird and cage (if bird in cage, can't
214  *  take one without the other).  Liquids also special, since they depend on
215  *  status of bottle.  Also various side effects, etc. */
216 {
217     int spk;
218     if (obj == INTRANSITIVE) {
219         /*  Carry, no object given yet.  OK if only one object present. */
220         if(game.atloc[game.loc] == 0 ||
221            game.link[game.atloc[game.loc]] != 0 ||
222            ATDWRF(game.loc) > 0)
223             return(8000);
224         obj=game.atloc[game.loc];
225     }
226
227     if (TOTING(obj)) {RSPEAK(24); return 2012;}
228     spk=25;
229     if (obj == PLANT && game.prop[PLANT] <= 0)spk=115;
230     if (obj == BEAR && game.prop[BEAR] == 1)spk=169;
231     if (obj == CHAIN && game.prop[BEAR] != 0)spk=170;
232     if (obj == URN)spk=215;
233     if (obj == CAVITY)spk=217;
234     if (obj == BLOOD)spk=239;
235     if (obj == RUG && game.prop[RUG] == 2)spk=222;
236     if (obj == SIGN)spk=196;
237     if (obj == MESSAG) {
238         spk=190;
239         return 2012;
240         DSTROY(MESSAG);
241     }
242     if (game.fixed[obj] != 0) {
243         RSPEAK(spk);
244         return 2012;
245     }
246     if (obj == WATER || obj == OIL) {
247         if (!HERE(BOTTLE) || LIQUID() != obj) {
248             if (TOTING(BOTTLE) && game.prop[BOTTLE] == 1)
249                 return(fill(BOTTLE));
250             if (game.prop[BOTTLE] != 1)spk=105;
251             if (!TOTING(BOTTLE))spk=104;
252             RSPEAK(spk);
253             return 2012;
254         }
255         obj = BOTTLE;
256     }
257
258     spk=92;
259     if (game.holdng >= INVLIMIT) {
260         RSPEAK(spk);
261         return 2012;
262     }
263     else if (obj == BIRD && game.prop[BIRD] != 1 && -1-game.prop[BIRD] != 1) {
264         if (game.prop[BIRD] == 2) {
265             DSTROY(BIRD);
266             RSPEAK(238);
267             return 2012;
268         }
269         if (!TOTING(CAGE))spk=27;
270         if (TOTING(ROD))spk=26;
271         if (spk/2 == 13) {
272             RSPEAK(spk);
273             return 2012;
274         }
275         game.prop[BIRD]=1;
276     }
277     if ((obj==BIRD || obj==CAGE) && (game.prop[BIRD]==1 || -1-game.prop[BIRD]==1))
278         CARRY(BIRD+CAGE-obj,game.loc);
279     CARRY(obj,game.loc);
280     if (obj == BOTTLE && LIQUID() != 0)
281         game.place[LIQUID()] = -1;
282     if (GSTONE(obj) && game.prop[obj] != 0) {
283         game.prop[obj]=0;
284         game.prop[CAVITY]=1;
285     }
286     RSPEAK(54);
287     return(2012);
288 }
289
290 static int chain(token_t verb)
291 /* Do something to the bear's chain */
292 {
293     int spk;
294     if (verb != LOCK) {
295         spk=171;
296         if (game.prop[BEAR] == 0)spk=41;
297         if (game.prop[CHAIN] == 0)spk=37;
298         if (spk != 171) {RSPEAK(spk); return 2012;}
299         game.prop[CHAIN]=0;
300         game.fixed[CHAIN]=0;
301         if (game.prop[BEAR] != 3)game.prop[BEAR]=2;
302         game.fixed[BEAR]=2-game.prop[BEAR];
303     } else {
304         spk=172;
305         if (game.prop[CHAIN] != 0)spk=34;
306         if (game.loc != PLAC[CHAIN])spk=173;
307         if (spk != 172) {RSPEAK(spk); return 2012;}
308         game.prop[CHAIN]=2;
309         if (TOTING(CHAIN))DROP(CHAIN,game.loc);
310         game.fixed[CHAIN]= -1;
311     }
312     RSPEAK(spk);
313     return 2012;
314 }
315
316 static int discard(long obj, bool just_do_it)
317 /*  Discard object.  "Throw" also comes here for most objects.  Special cases for
318  *  bird (might attack snake or dragon) and cage (might contain bird) and vase.
319  *  Drop coins at vending machine for extra batteries. */
320 {
321     if (!just_do_it) {
322         if (TOTING(ROD2) && obj == ROD && !TOTING(ROD))obj=ROD2;
323         if (!TOTING(obj)) {RSPEAK(SPK); return 2012;}
324         if (obj == BIRD && HERE(SNAKE)) {
325             RSPEAK(30);
326             if (game.closed) return(19000);
327             DSTROY(SNAKE);
328             /* Set game.prop for use by travel options */
329             game.prop[SNAKE]=1;
330
331         } else if ((GSTONE(obj) && AT(CAVITY) && game.prop[CAVITY] != 0)) {
332             RSPEAK(218);
333             game.prop[obj]=1;
334             game.prop[CAVITY]=0;
335             if (HERE(RUG) && ((obj == EMRALD && game.prop[RUG] != 2) || (obj == RUBY &&
336                                                                          game.prop[RUG] == 2))) {
337                 SPK=219;
338                 if (TOTING(RUG))SPK=220;
339                 if (obj == RUBY)SPK=221;
340                 RSPEAK(SPK);
341                 if (SPK != 220) {
342                     K=2-game.prop[RUG];
343                     game.prop[RUG]=K;
344                     if (K == 2)K=PLAC[SAPPH];
345                     MOVE(RUG+NOBJECTS,K);
346                 }
347             }
348         } else if (obj == COINS && HERE(VEND)) {
349             DSTROY(COINS);
350             DROP(BATTER,game.loc);
351             PSPEAK(BATTER,0);
352             return(2012);
353         } else if (obj == BIRD && AT(DRAGON) && game.prop[DRAGON] == 0) {
354             RSPEAK(154);
355             DSTROY(BIRD);
356             game.prop[BIRD]=0;
357             return(2012);
358         } else if (obj == BEAR && AT(TROLL)) {
359             RSPEAK(163);
360             MOVE(TROLL,0);
361             MOVE(TROLL+NOBJECTS,0);
362             MOVE(TROLL2,PLAC[TROLL]);
363             MOVE(TROLL2+NOBJECTS,FIXD[TROLL]);
364             JUGGLE(CHASM);
365             game.prop[TROLL]=2;
366         } else if (obj != VASE || game.loc == PLAC[PILLOW]) {
367             RSPEAK(54);
368         } else {
369             game.prop[VASE]=2;
370             if (AT(PILLOW))game.prop[VASE]=0;
371             PSPEAK(VASE,game.prop[VASE]+1);
372             if (game.prop[VASE] != 0)game.fixed[VASE]= -1;
373         }
374     }
375     K=LIQUID();
376     if (K == obj)obj=BOTTLE;
377     if (obj == BOTTLE && K != 0)game.place[K]=0;
378     if (obj == CAGE && game.prop[BIRD] == 1)DROP(BIRD,game.loc);
379     DROP(obj,game.loc);
380     if (obj != BIRD) return(2012);
381     game.prop[BIRD]=0;
382     if (FOREST(game.loc))game.prop[BIRD]=2;
383     return(2012);
384 }
385
386 static int drink(token_t obj)
387 /*  Drink.  If no object, assume water and look for it here.  If water is in
388  *  the bottle, drink that, else must be at a water loc, so drink stream. */
389 {
390     if (obj == 0 && LIQLOC(game.loc) != WATER && (LIQUID() != WATER || !HERE(BOTTLE)))
391         return(8000);
392     if (obj != BLOOD) {
393         if (obj != 0 && obj != WATER)SPK=110;
394         if (SPK == 110 || LIQUID() != WATER || !HERE(BOTTLE)) {RSPEAK(SPK); return 2012;}
395         game.prop[BOTTLE]=1;
396         game.place[WATER]=0;
397         SPK=74;
398         RSPEAK(SPK);
399         return 2012;
400     } else {
401         DSTROY(BLOOD);
402         game.prop[DRAGON]=2;
403         OBJSND[BIRD]=OBJSND[BIRD]+3;
404         SPK=240;
405         RSPEAK(SPK);
406         return 2012;
407     }
408 }
409
410 static int eat(token_t obj)
411 /*  Eat.  Intransitive: assume food if present, else ask what.  Transitive: food
412  *  ok, some things lose appetite, rest are ridiculous. */
413 {
414     if (obj == INTRANSITIVE) {
415         if (!HERE(FOOD))
416             return(8000);
417         DSTROY(FOOD);
418         SPK=72;
419     } else {
420         if (obj == FOOD) {
421             DSTROY(FOOD);
422             SPK=72;
423         }
424         if (obj == BIRD || obj == SNAKE || obj == CLAM || obj == OYSTER || obj ==
425            DWARF || obj == DRAGON || obj == TROLL || obj == BEAR || obj ==
426            OGRE)SPK=71;
427     }
428     RSPEAK(SPK);
429     return 2012;
430 }
431
432 static int extinguish(int obj)
433 /* Extinguish.  Lamp, urn, dragon/volcano (nice try). */
434 {
435     if (obj == INTRANSITIVE) {
436         if (HERE(LAMP) && game.prop[LAMP] == 1)obj=LAMP;
437         if (HERE(URN) && game.prop[URN] == 2)obj=obj*NOBJECTS+URN;
438         if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS) return(8000);
439     }
440
441     if (obj == URN) {
442         game.prop[URN]=game.prop[URN]/2;
443         SPK=210;
444         RSPEAK(SPK);
445         return 2012;
446     }
447     else if (obj == LAMP) {
448         game.prop[LAMP]=0;
449         RSPEAK(40);
450         if (DARK(game.loc))
451             RSPEAK(16);
452         return(2012);
453     }
454     else if (obj == DRAGON || obj == VOLCAN)
455         SPK=146;
456     RSPEAK(SPK);
457     return 2012;
458 }
459
460 static int feed(long obj)
461 /*  Feed.  If bird, no seed.  Snake, dragon, troll: quip.  If dwarf, make him
462  *  mad.  Bear, special. */
463 {
464     if (obj == BIRD) {
465         RSPEAK(100);
466         return 2012;
467     }
468
469     if (!(obj != SNAKE && obj != DRAGON && obj != TROLL)) {
470         int spk=102;
471         if (obj == DRAGON && game.prop[DRAGON] != 0)spk=110;
472         if (obj == TROLL)spk=182;
473         if (obj != SNAKE || game.closed || !HERE(BIRD))
474         {
475             RSPEAK(spk);
476             return 2012;
477         }
478         DSTROY(BIRD);
479         game.prop[BIRD]=0;
480         RSPEAK(101);
481         return 2012;
482     }
483
484     if (obj == DWARF) {
485         if (!HERE(FOOD))
486         {
487             RSPEAK(SPK);        /* FIXME: Defaults from ACTSPK */
488             return 2012;
489         }
490         game.dflag=game.dflag+2;
491         RSPEAK(103);
492         return 2012;
493     }
494
495     if (obj == BEAR) {
496         if (game.prop[BEAR] == 0)SPK=102;
497         if (game.prop[BEAR] == 3)SPK=110;
498         if (!HERE(FOOD)) {
499             RSPEAK(SPK);
500             return 2012;
501         }
502         DSTROY(FOOD);
503         game.prop[BEAR]=1;
504         game.fixed[AXE]=0;
505         game.prop[AXE]=0;
506         SPK=168;
507         RSPEAK(SPK);
508         return 2012;
509     }
510
511     if (obj == OGRE) {
512         if (HERE(FOOD))
513             SPK=202;
514         RSPEAK(SPK);
515         return 2012;
516     }
517
518     SPK=14;
519     RSPEAK(SPK);
520     return 2012;
521 }
522
523 int fill(long obj)
524 /*  Fill.  Bottle or urn must be empty, and liquid available.  (Vase
525  *  is nasty.) */
526 {
527     int k;
528     if (obj == VASE) {
529         SPK=29;
530         if (LIQLOC(game.loc) == 0)SPK=144;
531         if (LIQLOC(game.loc) == 0 || !TOTING(VASE)) {
532             RSPEAK(SPK);
533             return 2012;
534         }
535         RSPEAK(145);
536         game.prop[VASE]=2;
537         game.fixed[VASE]= -1;
538         return(discard(obj, true));
539     }
540
541     if (obj == URN){
542         SPK=213;
543         if (game.prop[URN] != 0) {RSPEAK(SPK); return 2012;}
544         SPK=144;
545         k=LIQUID();
546         if (k == 0 || !HERE(BOTTLE)) {RSPEAK(SPK); return 2012;}
547         game.place[k]=0;
548         game.prop[BOTTLE]=1;
549         if (k == OIL)game.prop[URN]=1;
550         SPK=211+game.prop[URN];
551         RSPEAK(SPK);
552         return 2012;
553     }
554
555     if (obj != 0 && obj != BOTTLE) {
556         RSPEAK(SPK);
557         return 2012;
558     }
559     if (obj == 0 && !HERE(BOTTLE))
560         return(8000);
561     SPK=107;
562     if (LIQLOC(game.loc) == 0)
563         SPK=106;
564     if (HERE(URN) && game.prop[URN] != 0)
565         SPK=214;
566     if (LIQUID() != 0)
567         SPK=105;
568     if (SPK != 107)
569         {RSPEAK(SPK); return 2012;}
570     game.prop[BOTTLE]=MOD(COND[game.loc],4)/2*2;
571     k=LIQUID();
572     if (TOTING(BOTTLE))
573         game.place[k]= -1;
574     if (k == OIL)
575         SPK=108;
576     RSPEAK(SPK);
577     return 2012;
578 }
579
580 static int find(token_t obj)
581 /* Find.  Might be carrying it, or it might be here.  Else give caveat. */
582 {
583     if (AT(obj) ||
584        (LIQUID() == obj && AT(BOTTLE)) ||
585        obj == LIQLOC(game.loc) ||
586        (obj == DWARF && ATDWRF(game.loc) > 0))
587         SPK=94;
588     if (game.closed)SPK=138;
589     if (TOTING(obj))SPK=24;
590     RSPEAK(SPK);
591     return 2012;
592 }
593
594 static int fly(token_t obj)
595 /* Fly.  Snide remarks unless hovering rug is here. */
596 {
597     if (obj == INTRANSITIVE) {
598         if (game.prop[RUG] != 2)SPK=224;
599         if (!HERE(RUG))SPK=225;
600         if (SPK/2 == 112) {
601             RSPEAK(SPK);
602             return 2012;
603         }
604         obj=RUG;
605     }
606
607     if (obj != RUG) {
608         RSPEAK(SPK);
609         return 2012;
610     }
611     SPK=223;
612     if (game.prop[RUG] != 2) {RSPEAK(SPK); return 2012;}
613     game.oldlc2=game.oldloc;
614     game.oldloc=game.loc;
615     game.newloc=game.place[RUG]+game.fixed[RUG]-game.loc;
616     SPK=226;
617     if (game.prop[SAPPH] >= 0)SPK=227;
618     RSPEAK(SPK);
619     return(2);
620 }
621     
622 static int inven(token_t obj)
623 /* Inventory. If object, treat same as find.  Else report on current burden. */
624 {
625     int i;
626     SPK=98;
627     for (i=1; i<=NOBJECTS; i++) {
628         if (i == BEAR || !TOTING(i))
629             continue;
630         if (SPK == 98)
631             RSPEAK(99);
632         game.blklin=false;
633         PSPEAK(i,-1);
634         game.blklin=true;
635         SPK=0;
636     }
637     if (TOTING(BEAR))
638         SPK=141;
639     RSPEAK(SPK);
640     return 2012;
641 }
642
643 int light(token_t obj)
644 /*  Light.  Applicable only to lamp and urn. */
645 {
646     int spk;
647     if (obj == INTRANSITIVE) {
648         if (HERE(LAMP) && game.prop[LAMP] == 0 && game.limit >= 0)obj=LAMP;
649         if (HERE(URN) && game.prop[URN] == 1)obj=obj*NOBJECTS+URN;
650         if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS) return(8000);
651     }
652
653     if (obj == URN) {
654         spk=38;
655         if (game.prop[URN] == 0)
656             {RSPEAK(spk); return 2012;}
657         spk=209;
658         game.prop[URN]=2;
659         RSPEAK(spk);
660         return 2012;
661     } else {
662         if (obj != LAMP)
663         {
664             RSPEAK(spk);
665             return 2012;
666         }
667         spk=184;
668         if (game.limit < 0) {
669             RSPEAK(spk);
670             return 2012;
671         }
672         game.prop[LAMP]=1;
673         RSPEAK(39);
674         if (game.wzdark)
675             return(2000);
676         return(2012);
677     }    
678 }
679
680 static int listen(void)
681 /*  Listen.  Intransitive only.  Print stuff based on objsnd/locsnd. */
682 {
683     int i, k;
684     int spk=228;
685     k=LOCSND[game.loc];
686     if (k != 0) {
687         RSPEAK(labs(k));
688         if (k < 0) return(2012);
689         spk=0;
690     }
691     SETPRM(1,game.zzword,0);
692     for (i=1; i<=NOBJECTS; i++) {
693         if (!HERE(i) || OBJSND[i] == 0 || game.prop[i] < 0)
694             continue;
695         PSPEAK(i,OBJSND[i]+game.prop[i]);
696         spk=0;
697         if (i == BIRD && OBJSND[i]+game.prop[i] == 8)
698             DSTROY(BIRD);
699     }
700     RSPEAK(spk);
701     return 2012;
702 }
703
704 static int lock(token_t verb, token_t obj)
705 /* Lock, unlock, no object given.  Assume various things if present. */
706 {
707     int k;
708     if (obj == INTRANSITIVE) {
709         SPK=28;
710         if (HERE(CLAM))obj=CLAM;
711         if (HERE(OYSTER))obj=OYSTER;
712         if (AT(DOOR))obj=DOOR;
713         if (AT(GRATE))obj=GRATE;
714         if (obj != 0 && HERE(CHAIN)) return(8000);
715         if (HERE(CHAIN))obj=CHAIN;
716         if (obj == 0) {RSPEAK(SPK); return 2012;}
717     }
718         
719     /*  Lock, unlock object.  Special stuff for opening clam/oyster
720      *  and for chain. */
721     if (obj == CLAM || obj == OYSTER)
722         return bivalve(verb, obj);
723     if (obj == DOOR)SPK=111;
724     if (obj == DOOR && game.prop[DOOR] == 1)SPK=54;
725     if (obj == CAGE)SPK=32;
726     if (obj == KEYS)SPK=55;
727     if (obj == GRATE || obj == CHAIN)SPK=31;
728     if (SPK != 31 || !HERE(KEYS)) {
729         RSPEAK(SPK);
730         return 2012;
731     }
732     if (obj == CHAIN)
733         return chain(verb);
734     if (game.closng) {
735         SPK=130;
736         if (!game.panic)game.clock2=15;
737         game.panic=true;
738         RSPEAK(SPK);
739         return 2012;
740     }
741     SPK=34+game.prop[GRATE];
742     game.prop[GRATE]=1;
743     if (verb == LOCK)game.prop[GRATE]=0;
744     SPK=SPK+2*game.prop[GRATE];
745     RSPEAK(SPK);
746     return 2012;
747 }
748
749 static int pour(token_t obj)
750 /*  Pour.  If no object, or object is bottle, assume contents of bottle.
751  *  special tests for pouring water or oil on plant or rusty door. */
752 {
753     if (obj == BOTTLE || obj == 0)obj=LIQUID();
754     if (obj == 0) return(8000);
755     if (!TOTING(obj)) {RSPEAK(SPK); return 2012;}
756     SPK=78;
757     if (obj != OIL && obj != WATER) {RSPEAK(SPK); return 2012;}
758     if (HERE(URN) && game.prop[URN] == 0)
759         return fill(URN);
760     game.prop[BOTTLE]=1;
761     game.place[obj]=0;
762     SPK=77;
763     if (!(AT(PLANT) || AT(DOOR)))
764         {RSPEAK(SPK); return 2012;}
765     if (!AT(DOOR)) {
766         SPK=112;
767         if (obj != WATER) {RSPEAK(SPK); return 2012;}
768         PSPEAK(PLANT,game.prop[PLANT]+3);
769         game.prop[PLANT]=MOD(game.prop[PLANT]+1,3);
770         game.prop[PLANT2]=game.prop[PLANT];
771         K=NUL;
772         return(8);
773     } else {
774         game.prop[DOOR]=0;
775         if (obj == OIL)game.prop[DOOR]=1;
776         SPK=113+game.prop[DOOR];
777         RSPEAK(SPK);
778         return 2012;
779     }
780 }
781
782 static int quit(FILE *input)
783 /*  Quit.  Intransitive only.  Verify intent and exit if that's what he wants. */
784 {
785     if (YES(input,22,54,54))
786         score(1);
787     return(2012);
788 }
789
790 static int read(FILE *input, token_t obj)
791 /*  Read.  Print stuff based on objtxt.  Oyster (?) is special case. */
792 {
793     int i;
794     if (obj == INTRANSITIVE) {
795         obj = 0;
796         for (i=1; i<=NOBJECTS; i++) {
797             if (HERE(i) && OBJTXT[i] != 0 && game.prop[i] >= 0)
798                 obj = obj * NOBJECTS + i;
799         }
800         if (obj > NOBJECTS || obj == 0 || DARK(game.loc)) return(8000);
801     }
802         
803     if (DARK(game.loc)) {
804         SETPRM(1,WD1,WD1X);
805         RSPEAK(256);
806         return(2012);
807     }
808     if (OBJTXT[obj] == 0 || game.prop[obj] < 0)
809         {RSPEAK(SPK); return 2012;}
810     if (obj == OYSTER && !game.clshnt) {
811         game.clshnt=YES(input,192,193,54);
812         return(2012);
813     }
814     PSPEAK(obj,OBJTXT[obj]+game.prop[obj]);
815     return(2012);
816 }
817
818 static int reservoir(void)
819 /*  Z'ZZZ (word gets recomputed at startup; different each game). */
820 {
821     if (!AT(RESER) && game.loc != game.fixed[RESER]-1) {RSPEAK(SPK); return 2012;}
822     PSPEAK(RESER,game.prop[RESER]+1);
823     game.prop[RESER]=1-game.prop[RESER];
824     if (AT(RESER)) return(2012);
825     game.oldlc2=game.loc;
826     game.newloc=0;
827     RSPEAK(241);
828     return(2);
829 }
830
831 static int rub(token_t obj)
832 /* Rub.  Yields various snide remarks except for lit urn. */
833 {
834     if (obj != LAMP)SPK=76;
835     if (obj != URN || game.prop[URN] != 2) {RSPEAK(SPK); return 2012;}
836     DSTROY(URN);
837     DROP(AMBER,game.loc);
838     game.prop[AMBER]=1;
839     --game.tally;
840     DROP(CAVITY,game.loc);
841     SPK=216;
842     RSPEAK(SPK);
843     return 2012;
844 }
845
846 static int say(void)
847 /* SAY.  Echo WD2 (or WD1 if no WD2 (SAY WHAT?, etc.).)  Magic words override. */
848 {
849     /* FIXME: ugly use of globals */
850     SETPRM(1,WD2,WD2X); if (WD2 <= 0)SETPRM(1,WD1,WD1X);
851     if (WD2 > 0)WD1=WD2;
852     int wd=VOCAB(WD1,-1);
853     if (wd == 62 || wd == 65 || wd == 71 || wd == 2025 || wd == 2034) {
854         WD2=0;
855         return(2630);
856     }
857     RSPEAK(258);
858     return 2012;
859
860 }
861
862 static int throw_support(long spk)
863 {
864     RSPEAK(spk);
865     DROP(AXE,game.loc);
866     K=NUL;
867     return(8);
868 }
869
870 static int throw(FILE *cmdin, long verb, long obj)
871 /*  Throw.  Same as discard unless axe.  Then same as attack except
872  *  ignore bird, and if dwarf is present then one might be killed.
873  *  (Only way to do so!)  Axe also special for dragon, bear, and
874  *  troll.  Treasures special for troll. */
875 {
876     if (TOTING(ROD2) && obj == ROD && !TOTING(ROD))obj=ROD2;
877     if (!TOTING(obj)) {
878         RSPEAK(SPK);    /* FIXME: Defaults from ACTSPK */
879         return 2012;
880     }
881     if (obj >= 50 && obj <= MAXTRS && AT(TROLL)) {
882         SPK=159;
883         /*  Snarf a treasure for the troll. */
884         DROP(obj,0);
885         MOVE(TROLL,0);
886         MOVE(TROLL+NOBJECTS,0);
887         DROP(TROLL2,PLAC[TROLL]);
888         DROP(TROLL2+NOBJECTS,FIXD[TROLL]);
889         JUGGLE(CHASM);
890         RSPEAK(SPK);
891         return 2012;
892     }
893     if (obj == FOOD && HERE(BEAR)) {
894     /* But throwing food is another story. */
895         obj=BEAR;
896         return(feed(obj));
897     }
898     if (obj != AXE)
899         return(discard(obj, false));
900     int i=ATDWRF(game.loc);
901     if (i <= 0) {
902         if (AT(DRAGON) && game.prop[DRAGON] == 0) {
903             SPK=152;
904             return throw_support(SPK);
905         }
906         if (AT(TROLL)) {
907             SPK=158;
908             return throw_support(SPK);
909         }
910         if (AT(OGRE)) {
911             SPK=203;
912             return throw_support(SPK);
913         }
914         if (HERE(BEAR) && game.prop[BEAR] == 0) {
915             /* This'll teach him to throw the axe at the bear! */
916             SPK=164;
917             DROP(AXE,game.loc);
918             game.fixed[AXE]= -1;
919             game.prop[AXE]=1;
920             JUGGLE(BEAR);
921             {RSPEAK(SPK); return 2012;}
922         }
923         return(attack(cmdin, verb, 0));
924     }
925
926     if (randrange(NDWARVES+1) < game.dflag) {
927         SPK=48;
928         return throw_support(SPK);
929     }
930     game.dseen[i]=false;
931     game.dloc[i]=0;
932     SPK=47;
933     ++game.dkill;
934     if (game.dkill == 1)SPK=149;
935
936     return throw_support(SPK);
937 }
938
939 static int vscore(void)
940 /* Score.  Call scoring routine but tell it to return. */
941 {
942     score(-1);
943     return 2012;
944 }
945
946 static int wake(token_t obj)
947 /* Wake.  Only use is to disturb the dwarves. */
948 {
949     if (obj != DWARF || !game.closed) {RSPEAK(SPK); return 2012;}
950     RSPEAK(199);
951     return(19000);
952 }
953
954 static int wave(token_t obj)
955 /* Wave.  No effect unless waving rod at fissure or at bird. */
956 {
957     if ((!TOTING(obj)) && (obj != ROD || !TOTING(ROD2)))SPK=29;
958     if (obj != ROD ||
959        !TOTING(obj) ||
960        (!HERE(BIRD) && (game.closng || !AT(FISSUR)))) {
961         RSPEAK(SPK);
962         return 2012;
963     }
964     if (HERE(BIRD))SPK=206+MOD(game.prop[BIRD],2);
965     if (SPK == 206 && game.loc == game.place[STEPS] && game.prop[JADE] < 0) {
966         DROP(JADE,game.loc);
967         game.prop[JADE]=0;
968         --game.tally;
969         SPK=208;
970         RSPEAK(SPK);
971         return 2012;
972     } else {
973         if (game.closed) {
974             RSPEAK(SPK);        /* FIXME: How is SPK set here? */
975             return(19000);
976         }
977         if (game.closng || !AT(FISSUR)) {RSPEAK(SPK); return 2012;}
978         if (HERE(BIRD))RSPEAK(SPK);
979         game.prop[FISSUR]=1-game.prop[FISSUR];
980         PSPEAK(FISSUR,2-game.prop[FISSUR]);
981         return 2012;
982     }
983 }
984
985 int action(FILE *input, enum speechpart part, long verb, long obj)
986 /*  Analyse a verb.  Remember what it was, go back for object if second word
987  *  unless verb is "say", which snarfs arbitrary second word.
988  */
989 {
990     int kk;
991
992     if (part == unknown)
993     {
994         /*  Analyse an object word.  See if the thing is here, whether
995          *  we've got a verb yet, and so on.  Object must be here
996          *  unless verb is "find" or "invent(ory)" (and no new verb
997          *  yet to be analysed).  Water and oil are also funny, since
998          *  they are never actually dropped at any location, but might
999          *  be here inside the bottle or urn or as a feature of the
1000          *  location. */
1001         if (HERE(obj))
1002             /* FALL THROUGH */;
1003         else if (obj == GRATE) {
1004             if (game.loc == 1 || game.loc == 4 || game.loc == 7)
1005                 obj=DPRSSN;
1006             if (game.loc > 9 && game.loc < 15)
1007                 obj=ENTRNC;
1008             if (obj != GRATE)
1009                 return(8);
1010         }
1011         else if (obj == DWARF && ATDWRF(game.loc) > 0)
1012             /* FALL THROUGH */;
1013         else if ((LIQUID() == obj && HERE(BOTTLE)) || obj == LIQLOC(game.loc))
1014             /* FALL THROUGH */;
1015         else if (obj == OIL && HERE(URN) && game.prop[URN] != 0) {
1016             obj=URN;
1017             /* FALL THROUGH */;
1018         }
1019         else if (obj == PLANT && AT(PLANT2) && game.prop[PLANT2] != 0) {
1020             obj=PLANT2;
1021             /* FALL THROUGH */;
1022         }
1023         else if (obj == KNIFE && game.knfloc == game.loc) {
1024             game.knfloc= -1;
1025             SPK=116;
1026             {RSPEAK(SPK); return 2012;}
1027         }
1028         else if (obj == ROD && HERE(ROD2)) {
1029             obj=ROD2;
1030             /* FALL THROUGH */;
1031         }
1032         else if ((verb == FIND || verb == INVENT) && WD2 <= 0)
1033             /* FALL THROUGH */;
1034         else {
1035             SETPRM(1,WD1,WD1X);
1036             RSPEAK(256);
1037             return(2012);
1038         }
1039
1040         if (WD2 > 0)
1041             return(2800);
1042         if (verb != 0)
1043             part = transitive;
1044     }
1045
1046     switch(part)
1047     {
1048         case intransitive:
1049             SPK=ACTSPK[verb];
1050             if (WD2 > 0 && verb != SAY) return(2800);
1051             if (verb == SAY)obj=WD2;
1052             if (obj == 0) {
1053                 /*  Analyse an intransitive verb (ie, no object given yet). */
1054                 switch (verb-1) {
1055                     case  0: /* CARRY */ return carry(INTRANSITIVE);
1056                     case  1: /* DROP  */ return(8000); 
1057                     case  2: /* SAY   */ return(8000); 
1058                     case  3: /* UNLOC */ return lock(verb, INTRANSITIVE);    
1059                     case  4: /* NOTHI */ {RSPEAK(54); return(20012);}
1060                     case  5: /* LOCK  */ return lock(verb, INTRANSITIVE);    
1061                     case  6: /* LIGHT */ return light(INTRANSITIVE);    
1062                     case  7: /* EXTIN */ return extinguish(INTRANSITIVE);    
1063                     case  8: /* WAVE  */ return(8000); 
1064                     case  9: /* CALM  */ return(8000); 
1065                     case 10: /* WALK  */ {RSPEAK(SPK); return 2012;} 
1066                     case 11: /* ATTAC */ return attack(input, verb, obj);   
1067                     case 12: /* POUR  */ return pour(obj);   
1068                     case 13: /* EAT   */ return eat(INTRANSITIVE);   
1069                     case 14: /* DRINK */ return drink(obj);   
1070                     case 15: /* RUB   */ return(8000); 
1071                     case 16: /* TOSS  */ return(8000); 
1072                     case 17: /* QUIT  */ return quit(input);   
1073                     case 18: /* FIND  */ return(8000); 
1074                     case 19: /* INVEN */ return inven(obj);   
1075                     case 20: /* FEED  */ return(8000); 
1076                     case 21: /* FILL  */ return fill(obj);   
1077                     case 22: /* BLAST */ return blast();   
1078                     case 23: /* SCOR  */ return vscore();   
1079                     case 24: /* FOO   */ return bigwords(WD1);   
1080                     case 25: /* BRIEF */ return brief();   
1081                     case 26: /* READ  */ return read(input, INTRANSITIVE);   
1082                     case 27: /* BREAK */ return(8000); 
1083                     case 28: /* WAKE  */ return(8000); 
1084                     case 29: /* SUSP  */ return saveresume(input, false);   
1085                     case 30: /* RESU  */ return saveresume(input, true);   
1086                     case 31: /* FLY   */ return fly(INTRANSITIVE);   
1087                     case 32: /* LISTE */ return listen();   
1088                     case 33: /* ZZZZ  */ return reservoir();   
1089                 }
1090                 BUG(23);
1091             }
1092             /* FALLTHRU */
1093         case transitive:
1094             /*  Analyse a transitive verb. */
1095             switch (verb-1) {
1096                 case  0: /* CARRY */ return carry(obj);    
1097                 case  1: /* DROP  */ return discard(obj, false);    
1098                 case  2: /* SAY   */ return say();    
1099                 case  3: /* UNLOC */ return lock(verb, obj);    
1100                 case  4: /* NOTHI */ {RSPEAK(54); return(20012);}
1101                 case  5: /* LOCK  */ return lock(verb, obj);    
1102                 case  6: /* LIGHT */ return light(obj);    
1103                 case  7: /* EXTI  */ return extinguish(obj);    
1104                 case  8: /* WAVE  */ return wave(obj);    
1105                 case  9: /* CALM  */ {RSPEAK(SPK); return 2012;} 
1106                 case 10: /* WALK  */ {RSPEAK(SPK); return 2012;} 
1107                 case 11: /* ATTAC */ return attack(input, verb, obj);   
1108                 case 12: /* POUR  */ return pour(obj);   
1109                 case 13: /* EAT   */ return eat(obj);   
1110                 case 14: /* DRINK */ return drink(obj);   
1111                 case 15: /* RUB   */ return rub(obj);   
1112                 case 16: /* TOSS  */ return throw(input, verb, obj);   
1113                 case 17: /* QUIT  */ {RSPEAK(SPK); return 2012;} 
1114                 case 18: /* FIND  */ return find(obj);   
1115                 case 19: /* INVEN */ return find(obj);   
1116                 case 20: /* FEED  */ return feed(obj);   
1117                 case 21: /* FILL  */ return fill(obj);   
1118                 case 22: /* BLAST */ return blast();   
1119                 case 23: /* SCOR  */ {RSPEAK(SPK); return 2012;} 
1120                 case 24: /* FOO   */ {RSPEAK(SPK); return 2012;} 
1121                 case 25: /* BRIEF */ {RSPEAK(SPK); return 2012;} 
1122                 case 26: /* READ  */ return read(input, obj);   
1123                 case 27: /* BREAK */ return vbreak(obj);   
1124                 case 28: /* WAKE  */ return wake(obj);   
1125                 case 29: /* SUSP  */ {RSPEAK(SPK); return 2012;} 
1126                 case 30: /* RESU  */ {RSPEAK(SPK); return 2012;} 
1127                 case 31: /* FLY   */ return fly(obj);   
1128                 case 32: /* LISTE */ {RSPEAK(SPK); return 2012;} 
1129                 case 33: /* ZZZZ  */ return reservoir();   
1130             }
1131             BUG(24);
1132         case unknown:
1133             /* Unknown verb, couldn't deduce object - might need hint */
1134             SETPRM(1,WD1,WD1X);
1135             RSPEAK(255);
1136             return(2600);
1137     default:
1138             BUG(99);
1139     }
1140 }