Localization moves for K.
[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                     int 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     int k = LIQUID();
376     if (k == obj)obj=BOTTLE;
377     if (obj == BOTTLE && k != 0)
378         game.place[k]=0;
379     if (obj == CAGE && game.prop[BIRD] == 1)DROP(BIRD,game.loc);
380     DROP(obj,game.loc);
381     if (obj != BIRD) return(2012);
382     game.prop[BIRD]=0;
383     if (FOREST(game.loc))game.prop[BIRD]=2;
384     return(2012);
385 }
386
387 static int drink(token_t obj)
388 /*  Drink.  If no object, assume water and look for it here.  If water is in
389  *  the bottle, drink that, else must be at a water loc, so drink stream. */
390 {
391     if (obj == 0 && LIQLOC(game.loc) != WATER && (LIQUID() != WATER || !HERE(BOTTLE)))
392         return(8000);
393     if (obj != BLOOD) {
394         if (obj != 0 && obj != WATER)SPK=110;
395         if (SPK == 110 || LIQUID() != WATER || !HERE(BOTTLE)) {RSPEAK(SPK); return 2012;}
396         game.prop[BOTTLE]=1;
397         game.place[WATER]=0;
398         SPK=74;
399         RSPEAK(SPK);
400         return 2012;
401     } else {
402         DSTROY(BLOOD);
403         game.prop[DRAGON]=2;
404         OBJSND[BIRD]=OBJSND[BIRD]+3;
405         SPK=240;
406         RSPEAK(SPK);
407         return 2012;
408     }
409 }
410
411 static int eat(token_t obj)
412 /*  Eat.  Intransitive: assume food if present, else ask what.  Transitive: food
413  *  ok, some things lose appetite, rest are ridiculous. */
414 {
415     if (obj == INTRANSITIVE) {
416         if (!HERE(FOOD))
417             return(8000);
418         DSTROY(FOOD);
419         SPK=72;
420     } else {
421         if (obj == FOOD) {
422             DSTROY(FOOD);
423             SPK=72;
424         }
425         if (obj == BIRD || obj == SNAKE || obj == CLAM || obj == OYSTER || obj ==
426            DWARF || obj == DRAGON || obj == TROLL || obj == BEAR || obj ==
427            OGRE)SPK=71;
428     }
429     RSPEAK(SPK);
430     return 2012;
431 }
432
433 static int extinguish(int obj)
434 /* Extinguish.  Lamp, urn, dragon/volcano (nice try). */
435 {
436     if (obj == INTRANSITIVE) {
437         if (HERE(LAMP) && game.prop[LAMP] == 1)obj=LAMP;
438         if (HERE(URN) && game.prop[URN] == 2)obj=obj*NOBJECTS+URN;
439         if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS) return(8000);
440     }
441
442     if (obj == URN) {
443         game.prop[URN]=game.prop[URN]/2;
444         SPK=210;
445         RSPEAK(SPK);
446         return 2012;
447     }
448     else if (obj == LAMP) {
449         game.prop[LAMP]=0;
450         RSPEAK(40);
451         if (DARK(game.loc))
452             RSPEAK(16);
453         return(2012);
454     }
455     else if (obj == DRAGON || obj == VOLCAN)
456         SPK=146;
457     RSPEAK(SPK);
458     return 2012;
459 }
460
461 static int feed(long obj)
462 /*  Feed.  If bird, no seed.  Snake, dragon, troll: quip.  If dwarf, make him
463  *  mad.  Bear, special. */
464 {
465     if (obj == BIRD) {
466         RSPEAK(100);
467         return 2012;
468     }
469
470     if (!(obj != SNAKE && obj != DRAGON && obj != TROLL)) {
471         int spk=102;
472         if (obj == DRAGON && game.prop[DRAGON] != 0)spk=110;
473         if (obj == TROLL)spk=182;
474         if (obj != SNAKE || game.closed || !HERE(BIRD))
475         {
476             RSPEAK(spk);
477             return 2012;
478         }
479         DSTROY(BIRD);
480         game.prop[BIRD]=0;
481         RSPEAK(101);
482         return 2012;
483     }
484
485     if (obj == DWARF) {
486         if (!HERE(FOOD))
487         {
488             RSPEAK(SPK);        /* FIXME: Defaults from ACTSPK */
489             return 2012;
490         }
491         game.dflag=game.dflag+2;
492         RSPEAK(103);
493         return 2012;
494     }
495
496     if (obj == BEAR) {
497         if (game.prop[BEAR] == 0)SPK=102;
498         if (game.prop[BEAR] == 3)SPK=110;
499         if (!HERE(FOOD)) {
500             RSPEAK(SPK);
501             return 2012;
502         }
503         DSTROY(FOOD);
504         game.prop[BEAR]=1;
505         game.fixed[AXE]=0;
506         game.prop[AXE]=0;
507         SPK=168;
508         RSPEAK(SPK);
509         return 2012;
510     }
511
512     if (obj == OGRE) {
513         if (HERE(FOOD))
514             SPK=202;
515         RSPEAK(SPK);
516         return 2012;
517     }
518
519     SPK=14;
520     RSPEAK(SPK);
521     return 2012;
522 }
523
524 int fill(long obj)
525 /*  Fill.  Bottle or urn must be empty, and liquid available.  (Vase
526  *  is nasty.) */
527 {
528     int k;
529     if (obj == VASE) {
530         SPK=29;
531         if (LIQLOC(game.loc) == 0)SPK=144;
532         if (LIQLOC(game.loc) == 0 || !TOTING(VASE)) {
533             RSPEAK(SPK);
534             return 2012;
535         }
536         RSPEAK(145);
537         game.prop[VASE]=2;
538         game.fixed[VASE]= -1;
539         return(discard(obj, true));
540     }
541
542     if (obj == URN){
543         SPK=213;
544         if (game.prop[URN] != 0) {RSPEAK(SPK); return 2012;}
545         SPK=144;
546         k=LIQUID();
547         if (k == 0 || !HERE(BOTTLE)) {RSPEAK(SPK); return 2012;}
548         game.place[k]=0;
549         game.prop[BOTTLE]=1;
550         if (k == OIL)game.prop[URN]=1;
551         SPK=211+game.prop[URN];
552         RSPEAK(SPK);
553         return 2012;
554     }
555
556     if (obj != 0 && obj != BOTTLE) {
557         RSPEAK(SPK);
558         return 2012;
559     }
560     if (obj == 0 && !HERE(BOTTLE))
561         return(8000);
562     SPK=107;
563     if (LIQLOC(game.loc) == 0)
564         SPK=106;
565     if (HERE(URN) && game.prop[URN] != 0)
566         SPK=214;
567     if (LIQUID() != 0)
568         SPK=105;
569     if (SPK != 107)
570         {RSPEAK(SPK); return 2012;}
571     game.prop[BOTTLE]=MOD(COND[game.loc],4)/2*2;
572     k=LIQUID();
573     if (TOTING(BOTTLE))
574         game.place[k]= -1;
575     if (k == OIL)
576         SPK=108;
577     RSPEAK(SPK);
578     return 2012;
579 }
580
581 static int find(token_t obj)
582 /* Find.  Might be carrying it, or it might be here.  Else give caveat. */
583 {
584     if (AT(obj) ||
585        (LIQUID() == obj && AT(BOTTLE)) ||
586        obj == LIQLOC(game.loc) ||
587        (obj == DWARF && ATDWRF(game.loc) > 0))
588         SPK=94;
589     if (game.closed)SPK=138;
590     if (TOTING(obj))SPK=24;
591     RSPEAK(SPK);
592     return 2012;
593 }
594
595 static int fly(token_t obj)
596 /* Fly.  Snide remarks unless hovering rug is here. */
597 {
598     if (obj == INTRANSITIVE) {
599         if (game.prop[RUG] != 2)SPK=224;
600         if (!HERE(RUG))SPK=225;
601         if (SPK/2 == 112) {
602             RSPEAK(SPK);
603             return 2012;
604         }
605         obj=RUG;
606     }
607
608     if (obj != RUG) {
609         RSPEAK(SPK);
610         return 2012;
611     }
612     SPK=223;
613     if (game.prop[RUG] != 2) {RSPEAK(SPK); return 2012;}
614     game.oldlc2=game.oldloc;
615     game.oldloc=game.loc;
616     game.newloc=game.place[RUG]+game.fixed[RUG]-game.loc;
617     SPK=226;
618     if (game.prop[SAPPH] >= 0)SPK=227;
619     RSPEAK(SPK);
620     return(2);
621 }
622     
623 static int inven(token_t obj)
624 /* Inventory. If object, treat same as find.  Else report on current burden. */
625 {
626     int i;
627     SPK=98;
628     for (i=1; i<=NOBJECTS; i++) {
629         if (i == BEAR || !TOTING(i))
630             continue;
631         if (SPK == 98)
632             RSPEAK(99);
633         game.blklin=false;
634         PSPEAK(i,-1);
635         game.blklin=true;
636         SPK=0;
637     }
638     if (TOTING(BEAR))
639         SPK=141;
640     RSPEAK(SPK);
641     return 2012;
642 }
643
644 int light(token_t obj)
645 /*  Light.  Applicable only to lamp and urn. */
646 {
647     int spk;
648     if (obj == INTRANSITIVE) {
649         if (HERE(LAMP) && game.prop[LAMP] == 0 && game.limit >= 0)obj=LAMP;
650         if (HERE(URN) && game.prop[URN] == 1)obj=obj*NOBJECTS+URN;
651         if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS) return(8000);
652     }
653
654     if (obj == URN) {
655         spk=38;
656         if (game.prop[URN] == 0)
657             {RSPEAK(spk); return 2012;}
658         spk=209;
659         game.prop[URN]=2;
660         RSPEAK(spk);
661         return 2012;
662     } else {
663         if (obj != LAMP)
664         {
665             RSPEAK(spk);
666             return 2012;
667         }
668         spk=184;
669         if (game.limit < 0) {
670             RSPEAK(spk);
671             return 2012;
672         }
673         game.prop[LAMP]=1;
674         RSPEAK(39);
675         if (game.wzdark)
676             return(2000);
677         return(2012);
678     }    
679 }
680
681 static int listen(void)
682 /*  Listen.  Intransitive only.  Print stuff based on objsnd/locsnd. */
683 {
684     int i, k;
685     int spk=228;
686     k=LOCSND[game.loc];
687     if (k != 0) {
688         RSPEAK(labs(k));
689         if (k < 0) return(2012);
690         spk=0;
691     }
692     SETPRM(1,game.zzword,0);
693     for (i=1; i<=NOBJECTS; i++) {
694         if (!HERE(i) || OBJSND[i] == 0 || game.prop[i] < 0)
695             continue;
696         PSPEAK(i,OBJSND[i]+game.prop[i]);
697         spk=0;
698         if (i == BIRD && OBJSND[i]+game.prop[i] == 8)
699             DSTROY(BIRD);
700     }
701     RSPEAK(spk);
702     return 2012;
703 }
704
705 static int lock(token_t verb, token_t obj)
706 /* Lock, unlock, no object given.  Assume various things if present. */
707 {
708     int k;
709     if (obj == INTRANSITIVE) {
710         SPK=28;
711         if (HERE(CLAM))obj=CLAM;
712         if (HERE(OYSTER))obj=OYSTER;
713         if (AT(DOOR))obj=DOOR;
714         if (AT(GRATE))obj=GRATE;
715         if (obj != 0 && HERE(CHAIN)) return(8000);
716         if (HERE(CHAIN))obj=CHAIN;
717         if (obj == 0) {RSPEAK(SPK); return 2012;}
718     }
719         
720     /*  Lock, unlock object.  Special stuff for opening clam/oyster
721      *  and for chain. */
722     if (obj == CLAM || obj == OYSTER)
723         return bivalve(verb, obj);
724     if (obj == DOOR)SPK=111;
725     if (obj == DOOR && game.prop[DOOR] == 1)SPK=54;
726     if (obj == CAGE)SPK=32;
727     if (obj == KEYS)SPK=55;
728     if (obj == GRATE || obj == CHAIN)SPK=31;
729     if (SPK != 31 || !HERE(KEYS)) {
730         RSPEAK(SPK);
731         return 2012;
732     }
733     if (obj == CHAIN)
734         return chain(verb);
735     if (game.closng) {
736         SPK=130;
737         if (!game.panic)game.clock2=15;
738         game.panic=true;
739         RSPEAK(SPK);
740         return 2012;
741     }
742     SPK=34+game.prop[GRATE];
743     game.prop[GRATE]=1;
744     if (verb == LOCK)game.prop[GRATE]=0;
745     SPK=SPK+2*game.prop[GRATE];
746     RSPEAK(SPK);
747     return 2012;
748 }
749
750 static int pour(token_t obj)
751 /*  Pour.  If no object, or object is bottle, assume contents of bottle.
752  *  special tests for pouring water or oil on plant or rusty door. */
753 {
754     if (obj == BOTTLE || obj == 0)obj=LIQUID();
755     if (obj == 0) return(8000);
756     if (!TOTING(obj)) {RSPEAK(SPK); return 2012;}
757     SPK=78;
758     if (obj != OIL && obj != WATER) {RSPEAK(SPK); return 2012;}
759     if (HERE(URN) && game.prop[URN] == 0)
760         return fill(URN);
761     game.prop[BOTTLE]=1;
762     game.place[obj]=0;
763     SPK=77;
764     if (!(AT(PLANT) || AT(DOOR)))
765         {RSPEAK(SPK); return 2012;}
766     if (!AT(DOOR)) {
767         SPK=112;
768         if (obj != WATER) {RSPEAK(SPK); return 2012;}
769         PSPEAK(PLANT,game.prop[PLANT]+3);
770         game.prop[PLANT]=MOD(game.prop[PLANT]+1,3);
771         game.prop[PLANT2]=game.prop[PLANT];
772         K=NUL;
773         return(8);
774     } else {
775         game.prop[DOOR]=0;
776         if (obj == OIL)game.prop[DOOR]=1;
777         SPK=113+game.prop[DOOR];
778         RSPEAK(SPK);
779         return 2012;
780     }
781 }
782
783 static int quit(FILE *input)
784 /*  Quit.  Intransitive only.  Verify intent and exit if that's what he wants. */
785 {
786     if (YES(input,22,54,54))
787         score(1);
788     return(2012);
789 }
790
791 static int read(FILE *input, token_t obj)
792 /*  Read.  Print stuff based on objtxt.  Oyster (?) is special case. */
793 {
794     int i;
795     if (obj == INTRANSITIVE) {
796         obj = 0;
797         for (i=1; i<=NOBJECTS; i++) {
798             if (HERE(i) && OBJTXT[i] != 0 && game.prop[i] >= 0)
799                 obj = obj * NOBJECTS + i;
800         }
801         if (obj > NOBJECTS || obj == 0 || DARK(game.loc)) return(8000);
802     }
803         
804     if (DARK(game.loc)) {
805         SETPRM(1,WD1,WD1X);
806         RSPEAK(256);
807         return(2012);
808     }
809     if (OBJTXT[obj] == 0 || game.prop[obj] < 0)
810         {RSPEAK(SPK); return 2012;}
811     if (obj == OYSTER && !game.clshnt) {
812         game.clshnt=YES(input,192,193,54);
813         return(2012);
814     }
815     PSPEAK(obj,OBJTXT[obj]+game.prop[obj]);
816     return(2012);
817 }
818
819 static int reservoir(void)
820 /*  Z'ZZZ (word gets recomputed at startup; different each game). */
821 {
822     if (!AT(RESER) && game.loc != game.fixed[RESER]-1) {RSPEAK(SPK); return 2012;}
823     PSPEAK(RESER,game.prop[RESER]+1);
824     game.prop[RESER]=1-game.prop[RESER];
825     if (AT(RESER)) return(2012);
826     game.oldlc2=game.loc;
827     game.newloc=0;
828     RSPEAK(241);
829     return(2);
830 }
831
832 static int rub(token_t obj)
833 /* Rub.  Yields various snide remarks except for lit urn. */
834 {
835     if (obj != LAMP)SPK=76;
836     if (obj != URN || game.prop[URN] != 2) {RSPEAK(SPK); return 2012;}
837     DSTROY(URN);
838     DROP(AMBER,game.loc);
839     game.prop[AMBER]=1;
840     --game.tally;
841     DROP(CAVITY,game.loc);
842     SPK=216;
843     RSPEAK(SPK);
844     return 2012;
845 }
846
847 static int say(void)
848 /* SAY.  Echo WD2 (or WD1 if no WD2 (SAY WHAT?, etc.).)  Magic words override. */
849 {
850     /* FIXME: ugly use of globals */
851     SETPRM(1,WD2,WD2X); if (WD2 <= 0)SETPRM(1,WD1,WD1X);
852     if (WD2 > 0)WD1=WD2;
853     int wd=VOCAB(WD1,-1);
854     if (wd == 62 || wd == 65 || wd == 71 || wd == 2025 || wd == 2034) {
855         WD2=0;
856         return(2630);
857     }
858     RSPEAK(258);
859     return 2012;
860
861 }
862
863 static int throw_support(long spk)
864 {
865     RSPEAK(spk);
866     DROP(AXE,game.loc);
867     K=NUL;
868     return(8);
869 }
870
871 static int throw(FILE *cmdin, long verb, long obj)
872 /*  Throw.  Same as discard unless axe.  Then same as attack except
873  *  ignore bird, and if dwarf is present then one might be killed.
874  *  (Only way to do so!)  Axe also special for dragon, bear, and
875  *  troll.  Treasures special for troll. */
876 {
877     if (TOTING(ROD2) && obj == ROD && !TOTING(ROD))obj=ROD2;
878     if (!TOTING(obj)) {
879         RSPEAK(SPK);    /* FIXME: Defaults from ACTSPK */
880         return 2012;
881     }
882     if (obj >= 50 && obj <= MAXTRS && AT(TROLL)) {
883         SPK=159;
884         /*  Snarf a treasure for the troll. */
885         DROP(obj,0);
886         MOVE(TROLL,0);
887         MOVE(TROLL+NOBJECTS,0);
888         DROP(TROLL2,PLAC[TROLL]);
889         DROP(TROLL2+NOBJECTS,FIXD[TROLL]);
890         JUGGLE(CHASM);
891         RSPEAK(SPK);
892         return 2012;
893     }
894     if (obj == FOOD && HERE(BEAR)) {
895     /* But throwing food is another story. */
896         obj=BEAR;
897         return(feed(obj));
898     }
899     if (obj != AXE)
900         return(discard(obj, false));
901     int i=ATDWRF(game.loc);
902     if (i <= 0) {
903         if (AT(DRAGON) && game.prop[DRAGON] == 0) {
904             SPK=152;
905             return throw_support(SPK);
906         }
907         if (AT(TROLL)) {
908             SPK=158;
909             return throw_support(SPK);
910         }
911         if (AT(OGRE)) {
912             SPK=203;
913             return throw_support(SPK);
914         }
915         if (HERE(BEAR) && game.prop[BEAR] == 0) {
916             /* This'll teach him to throw the axe at the bear! */
917             SPK=164;
918             DROP(AXE,game.loc);
919             game.fixed[AXE]= -1;
920             game.prop[AXE]=1;
921             JUGGLE(BEAR);
922             {RSPEAK(SPK); return 2012;}
923         }
924         return(attack(cmdin, verb, 0));
925     }
926
927     if (randrange(NDWARVES+1) < game.dflag) {
928         SPK=48;
929         return throw_support(SPK);
930     }
931     game.dseen[i]=false;
932     game.dloc[i]=0;
933     SPK=47;
934     ++game.dkill;
935     if (game.dkill == 1)SPK=149;
936
937     return throw_support(SPK);
938 }
939
940 static int vscore(void)
941 /* Score.  Call scoring routine but tell it to return. */
942 {
943     score(-1);
944     return 2012;
945 }
946
947 static int wake(token_t obj)
948 /* Wake.  Only use is to disturb the dwarves. */
949 {
950     if (obj != DWARF || !game.closed) {RSPEAK(SPK); return 2012;}
951     RSPEAK(199);
952     return(19000);
953 }
954
955 static int wave(token_t obj)
956 /* Wave.  No effect unless waving rod at fissure or at bird. */
957 {
958     if ((!TOTING(obj)) && (obj != ROD || !TOTING(ROD2)))SPK=29;
959     if (obj != ROD ||
960        !TOTING(obj) ||
961        (!HERE(BIRD) && (game.closng || !AT(FISSUR)))) {
962         RSPEAK(SPK);
963         return 2012;
964     }
965     if (HERE(BIRD))SPK=206+MOD(game.prop[BIRD],2);
966     if (SPK == 206 && game.loc == game.place[STEPS] && game.prop[JADE] < 0) {
967         DROP(JADE,game.loc);
968         game.prop[JADE]=0;
969         --game.tally;
970         SPK=208;
971         RSPEAK(SPK);
972         return 2012;
973     } else {
974         if (game.closed) {
975             RSPEAK(SPK);        /* FIXME: How is SPK set here? */
976             return(19000);
977         }
978         if (game.closng || !AT(FISSUR)) {RSPEAK(SPK); return 2012;}
979         if (HERE(BIRD))RSPEAK(SPK);
980         game.prop[FISSUR]=1-game.prop[FISSUR];
981         PSPEAK(FISSUR,2-game.prop[FISSUR]);
982         return 2012;
983     }
984 }
985
986 int action(FILE *input, enum speechpart part, long verb, long obj)
987 /*  Analyse a verb.  Remember what it was, go back for object if second word
988  *  unless verb is "say", which snarfs arbitrary second word.
989  */
990 {
991     int kk;
992
993     if (part == unknown)
994     {
995         /*  Analyse an object word.  See if the thing is here, whether
996          *  we've got a verb yet, and so on.  Object must be here
997          *  unless verb is "find" or "invent(ory)" (and no new verb
998          *  yet to be analysed).  Water and oil are also funny, since
999          *  they are never actually dropped at any location, but might
1000          *  be here inside the bottle or urn or as a feature of the
1001          *  location. */
1002         if (HERE(obj))
1003             /* FALL THROUGH */;
1004         else if (obj == GRATE) {
1005             if (game.loc == 1 || game.loc == 4 || game.loc == 7)
1006                 obj=DPRSSN;
1007             if (game.loc > 9 && game.loc < 15)
1008                 obj=ENTRNC;
1009             if (obj != GRATE)
1010                 return(8);
1011         }
1012         else if (obj == DWARF && ATDWRF(game.loc) > 0)
1013             /* FALL THROUGH */;
1014         else if ((LIQUID() == obj && HERE(BOTTLE)) || obj == LIQLOC(game.loc))
1015             /* FALL THROUGH */;
1016         else if (obj == OIL && HERE(URN) && game.prop[URN] != 0) {
1017             obj=URN;
1018             /* FALL THROUGH */;
1019         }
1020         else if (obj == PLANT && AT(PLANT2) && game.prop[PLANT2] != 0) {
1021             obj=PLANT2;
1022             /* FALL THROUGH */;
1023         }
1024         else if (obj == KNIFE && game.knfloc == game.loc) {
1025             game.knfloc= -1;
1026             SPK=116;
1027             {RSPEAK(SPK); return 2012;}
1028         }
1029         else if (obj == ROD && HERE(ROD2)) {
1030             obj=ROD2;
1031             /* FALL THROUGH */;
1032         }
1033         else if ((verb == FIND || verb == INVENT) && WD2 <= 0)
1034             /* FALL THROUGH */;
1035         else {
1036             SETPRM(1,WD1,WD1X);
1037             RSPEAK(256);
1038             return(2012);
1039         }
1040
1041         if (WD2 > 0)
1042             return(2800);
1043         if (verb != 0)
1044             part = transitive;
1045     }
1046
1047     switch(part)
1048     {
1049         case intransitive:
1050             SPK=ACTSPK[verb];
1051             if (WD2 > 0 && verb != SAY) return(2800);
1052             if (verb == SAY)obj=WD2;
1053             if (obj == 0) {
1054                 /*  Analyse an intransitive verb (ie, no object given yet). */
1055                 switch (verb-1) {
1056                     case  0: /* CARRY */ return carry(INTRANSITIVE);
1057                     case  1: /* DROP  */ return(8000); 
1058                     case  2: /* SAY   */ return(8000); 
1059                     case  3: /* UNLOC */ return lock(verb, INTRANSITIVE);    
1060                     case  4: /* NOTHI */ {RSPEAK(54); return(20012);}
1061                     case  5: /* LOCK  */ return lock(verb, INTRANSITIVE);    
1062                     case  6: /* LIGHT */ return light(INTRANSITIVE);    
1063                     case  7: /* EXTIN */ return extinguish(INTRANSITIVE);    
1064                     case  8: /* WAVE  */ return(8000); 
1065                     case  9: /* CALM  */ return(8000); 
1066                     case 10: /* WALK  */ {RSPEAK(SPK); return 2012;} 
1067                     case 11: /* ATTAC */ return attack(input, verb, obj);   
1068                     case 12: /* POUR  */ return pour(obj);   
1069                     case 13: /* EAT   */ return eat(INTRANSITIVE);   
1070                     case 14: /* DRINK */ return drink(obj);   
1071                     case 15: /* RUB   */ return(8000); 
1072                     case 16: /* TOSS  */ return(8000); 
1073                     case 17: /* QUIT  */ return quit(input);   
1074                     case 18: /* FIND  */ return(8000); 
1075                     case 19: /* INVEN */ return inven(obj);   
1076                     case 20: /* FEED  */ return(8000); 
1077                     case 21: /* FILL  */ return fill(obj);   
1078                     case 22: /* BLAST */ return blast();   
1079                     case 23: /* SCOR  */ return vscore();   
1080                     case 24: /* FOO   */ return bigwords(WD1);   
1081                     case 25: /* BRIEF */ return brief();   
1082                     case 26: /* READ  */ return read(input, INTRANSITIVE);   
1083                     case 27: /* BREAK */ return(8000); 
1084                     case 28: /* WAKE  */ return(8000); 
1085                     case 29: /* SUSP  */ return saveresume(input, false);   
1086                     case 30: /* RESU  */ return saveresume(input, true);   
1087                     case 31: /* FLY   */ return fly(INTRANSITIVE);   
1088                     case 32: /* LISTE */ return listen();   
1089                     case 33: /* ZZZZ  */ return reservoir();   
1090                 }
1091                 BUG(23);
1092             }
1093             /* FALLTHRU */
1094         case transitive:
1095             /*  Analyse a transitive verb. */
1096             switch (verb-1) {
1097                 case  0: /* CARRY */ return carry(obj);    
1098                 case  1: /* DROP  */ return discard(obj, false);    
1099                 case  2: /* SAY   */ return say();    
1100                 case  3: /* UNLOC */ return lock(verb, obj);    
1101                 case  4: /* NOTHI */ {RSPEAK(54); return(20012);}
1102                 case  5: /* LOCK  */ return lock(verb, obj);    
1103                 case  6: /* LIGHT */ return light(obj);    
1104                 case  7: /* EXTI  */ return extinguish(obj);    
1105                 case  8: /* WAVE  */ return wave(obj);    
1106                 case  9: /* CALM  */ {RSPEAK(SPK); return 2012;} 
1107                 case 10: /* WALK  */ {RSPEAK(SPK); return 2012;} 
1108                 case 11: /* ATTAC */ return attack(input, verb, obj);   
1109                 case 12: /* POUR  */ return pour(obj);   
1110                 case 13: /* EAT   */ return eat(obj);   
1111                 case 14: /* DRINK */ return drink(obj);   
1112                 case 15: /* RUB   */ return rub(obj);   
1113                 case 16: /* TOSS  */ return throw(input, verb, obj);   
1114                 case 17: /* QUIT  */ {RSPEAK(SPK); return 2012;} 
1115                 case 18: /* FIND  */ return find(obj);   
1116                 case 19: /* INVEN */ return find(obj);   
1117                 case 20: /* FEED  */ return feed(obj);   
1118                 case 21: /* FILL  */ return fill(obj);   
1119                 case 22: /* BLAST */ return blast();   
1120                 case 23: /* SCOR  */ {RSPEAK(SPK); return 2012;} 
1121                 case 24: /* FOO   */ {RSPEAK(SPK); return 2012;} 
1122                 case 25: /* BRIEF */ {RSPEAK(SPK); return 2012;} 
1123                 case 26: /* READ  */ return read(input, obj);   
1124                 case 27: /* BREAK */ return vbreak(obj);   
1125                 case 28: /* WAKE  */ return wake(obj);   
1126                 case 29: /* SUSP  */ {RSPEAK(SPK); return 2012;} 
1127                 case 30: /* RESU  */ {RSPEAK(SPK); return 2012;} 
1128                 case 31: /* FLY   */ return fly(obj);   
1129                 case 32: /* LISTE */ {RSPEAK(SPK); return 2012;} 
1130                 case 33: /* ZZZZ  */ return reservoir();   
1131             }
1132             BUG(24);
1133         case unknown:
1134             /* Unknown verb, couldn't deduce object - might need hint */
1135             SETPRM(1,WD1,WD1X);
1136             RSPEAK(255);
1137             return(2600);
1138     default:
1139             BUG(99);
1140     }
1141 }