Localize K to main.c
[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 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     return(8);
110 }
111
112 static int bigwords(long foo)
113 /*  FEE FIE FOE FOO (AND FUM).  Advance to next state if given in proper order.
114  *  Look up WD1 in section 3 of vocab to determine which word we've got.  Last
115  *  word zips the eggs back to the giant room (unless already there). */
116 {
117     int k=VOCAB(foo,3);
118     SPK=42;
119     if (game.foobar != 1-k) {
120         if (game.foobar != 0)SPK=151;
121         RSPEAK(SPK);
122         return 2012;
123     } else {
124         game.foobar=k;
125         if (k != 4) {
126             RSPEAK(54);
127             return 2012;
128         }
129         game.foobar=0;
130         if (game.place[EGGS]==PLAC[EGGS] || (TOTING(EGGS) && game.loc==PLAC[EGGS])) {
131             RSPEAK(SPK);
132             return 2012;
133         }
134         /*  Bring back troll if we steal the eggs back from him before
135          *  crossing. */
136         if (game.place[EGGS]==0 && game.place[TROLL]==0 && game.prop[TROLL]==0)
137             game.prop[TROLL]=1;
138         k=2;
139         if (HERE(EGGS))k=1;
140         if (game.loc == PLAC[EGGS])k=0;
141         MOVE(EGGS,PLAC[EGGS]);
142         PSPEAK(EGGS,k);
143         return(2012);
144     }
145 }
146
147 static int bivalve(token_t verb, token_t obj)
148 /* Clam/oyster actions */
149 {
150     int spk, k=0;
151     if (obj == OYSTER)k=1;
152     spk=124+k;
153     if (TOTING(obj))spk=120+k;
154     if (!TOTING(TRIDNT))spk=122+k;
155     if (verb == LOCK)spk=61;
156     if (spk == 124) {
157         DSTROY(CLAM);
158         DROP(OYSTER,game.loc);
159         DROP(PEARL,105);
160     }
161     RSPEAK(spk);
162     return 2012;
163 }
164
165 static int blast(void)
166 /*  Blast.  No effect unless you've got dynamite, which is a neat trick! */
167 {
168     if (game.prop[ROD2] < 0 || !game.closed)
169     {
170         RSPEAK(67);
171         return 2012;
172     }
173     game.bonus=133;
174     if (game.loc == 115)
175         game.bonus=134;
176     if (HERE(ROD2))
177         game.bonus=135;
178     RSPEAK(game.bonus);
179     score(0);
180 }
181
182 static int vbreak(token_t obj)
183 /*  Break.  Only works for mirror in repository and, of course, the vase. */
184 {
185     /* FIXME: defaults from ACTSPK */
186     if (obj == MIRROR)SPK=148;
187     if (obj == VASE && game.prop[VASE] == 0) {
188         SPK=198;
189         if (TOTING(VASE))DROP(VASE,game.loc);
190         game.prop[VASE]=2;
191         game.fixed[VASE]= -1;
192     } else {
193         if (obj == MIRROR && game.closed) {
194             RSPEAK(197);
195             return(190000);
196         }
197     }
198     RSPEAK(SPK);
199     return 2012;
200 }
201
202 static int brief(void)
203 /*  Brief.  Intransitive only.  Suppress long descriptions after first time. */
204 {
205     game.abbnum=10000;
206     game.detail=3;
207     RSPEAK(156);
208     return 2012;
209 }
210
211 static int carry(long obj)
212 /*  Carry an object.  Special cases for bird and cage (if bird in cage, can't
213  *  take one without the other).  Liquids also special, since they depend on
214  *  status of bottle.  Also various side effects, etc. */
215 {
216     int spk;
217     if (obj == INTRANSITIVE) {
218         /*  Carry, no object given yet.  OK if only one object present. */
219         if(game.atloc[game.loc] == 0 ||
220            game.link[game.atloc[game.loc]] != 0 ||
221            ATDWRF(game.loc) > 0)
222             return(8000);
223         obj=game.atloc[game.loc];
224     }
225
226     if (TOTING(obj)) {RSPEAK(24); return 2012;}
227     spk=25;
228     if (obj == PLANT && game.prop[PLANT] <= 0)spk=115;
229     if (obj == BEAR && game.prop[BEAR] == 1)spk=169;
230     if (obj == CHAIN && game.prop[BEAR] != 0)spk=170;
231     if (obj == URN)spk=215;
232     if (obj == CAVITY)spk=217;
233     if (obj == BLOOD)spk=239;
234     if (obj == RUG && game.prop[RUG] == 2)spk=222;
235     if (obj == SIGN)spk=196;
236     if (obj == MESSAG) {
237         spk=190;
238         return 2012;
239         DSTROY(MESSAG);
240     }
241     if (game.fixed[obj] != 0) {
242         RSPEAK(spk);
243         return 2012;
244     }
245     if (obj == WATER || obj == OIL) {
246         if (!HERE(BOTTLE) || LIQUID() != obj) {
247             if (TOTING(BOTTLE) && game.prop[BOTTLE] == 1)
248                 return(fill(BOTTLE));
249             if (game.prop[BOTTLE] != 1)spk=105;
250             if (!TOTING(BOTTLE))spk=104;
251             RSPEAK(spk);
252             return 2012;
253         }
254         obj = BOTTLE;
255     }
256
257     spk=92;
258     if (game.holdng >= INVLIMIT) {
259         RSPEAK(spk);
260         return 2012;
261     }
262     else if (obj == BIRD && game.prop[BIRD] != 1 && -1-game.prop[BIRD] != 1) {
263         if (game.prop[BIRD] == 2) {
264             DSTROY(BIRD);
265             RSPEAK(238);
266             return 2012;
267         }
268         if (!TOTING(CAGE))spk=27;
269         if (TOTING(ROD))spk=26;
270         if (spk/2 == 13) {
271             RSPEAK(spk);
272             return 2012;
273         }
274         game.prop[BIRD]=1;
275     }
276     if ((obj==BIRD || obj==CAGE) && (game.prop[BIRD]==1 || -1-game.prop[BIRD]==1))
277         CARRY(BIRD+CAGE-obj,game.loc);
278     CARRY(obj,game.loc);
279     if (obj == BOTTLE && LIQUID() != 0)
280         game.place[LIQUID()] = -1;
281     if (GSTONE(obj) && game.prop[obj] != 0) {
282         game.prop[obj]=0;
283         game.prop[CAVITY]=1;
284     }
285     RSPEAK(54);
286     return(2012);
287 }
288
289 static int chain(token_t verb)
290 /* Do something to the bear's chain */
291 {
292     int spk;
293     if (verb != LOCK) {
294         spk=171;
295         if (game.prop[BEAR] == 0)spk=41;
296         if (game.prop[CHAIN] == 0)spk=37;
297         if (spk != 171) {RSPEAK(spk); return 2012;}
298         game.prop[CHAIN]=0;
299         game.fixed[CHAIN]=0;
300         if (game.prop[BEAR] != 3)game.prop[BEAR]=2;
301         game.fixed[BEAR]=2-game.prop[BEAR];
302     } else {
303         spk=172;
304         if (game.prop[CHAIN] != 0)spk=34;
305         if (game.loc != PLAC[CHAIN])spk=173;
306         if (spk != 172) {RSPEAK(spk); return 2012;}
307         game.prop[CHAIN]=2;
308         if (TOTING(CHAIN))DROP(CHAIN,game.loc);
309         game.fixed[CHAIN]= -1;
310     }
311     RSPEAK(spk);
312     return 2012;
313 }
314
315 static int discard(long obj, bool just_do_it)
316 /*  Discard object.  "Throw" also comes here for most objects.  Special cases for
317  *  bird (might attack snake or dragon) and cage (might contain bird) and vase.
318  *  Drop coins at vending machine for extra batteries. */
319 {
320     if (!just_do_it) {
321         if (TOTING(ROD2) && obj == ROD && !TOTING(ROD))obj=ROD2;
322         if (!TOTING(obj)) {RSPEAK(SPK); return 2012;}
323         if (obj == BIRD && HERE(SNAKE)) {
324             RSPEAK(30);
325             if (game.closed) return(19000);
326             DSTROY(SNAKE);
327             /* Set game.prop for use by travel options */
328             game.prop[SNAKE]=1;
329
330         } else if ((GSTONE(obj) && AT(CAVITY) && game.prop[CAVITY] != 0)) {
331             RSPEAK(218);
332             game.prop[obj]=1;
333             game.prop[CAVITY]=0;
334             if (HERE(RUG) && ((obj == EMRALD && game.prop[RUG] != 2) || (obj == RUBY &&
335                                                                          game.prop[RUG] == 2))) {
336                 SPK=219;
337                 if (TOTING(RUG))SPK=220;
338                 if (obj == RUBY)SPK=221;
339                 RSPEAK(SPK);
340                 if (SPK != 220) {
341                     int k = 2-game.prop[RUG];
342                     game.prop[RUG] = k;
343                     if (k == 2) k = PLAC[SAPPH];
344                     MOVE(RUG+NOBJECTS, k);
345                 }
346             }
347         } else if (obj == COINS && HERE(VEND)) {
348             DSTROY(COINS);
349             DROP(BATTER,game.loc);
350             PSPEAK(BATTER,0);
351             return(2012);
352         } else if (obj == BIRD && AT(DRAGON) && game.prop[DRAGON] == 0) {
353             RSPEAK(154);
354             DSTROY(BIRD);
355             game.prop[BIRD]=0;
356             return(2012);
357         } else if (obj == BEAR && AT(TROLL)) {
358             RSPEAK(163);
359             MOVE(TROLL,0);
360             MOVE(TROLL+NOBJECTS,0);
361             MOVE(TROLL2,PLAC[TROLL]);
362             MOVE(TROLL2+NOBJECTS,FIXD[TROLL]);
363             JUGGLE(CHASM);
364             game.prop[TROLL]=2;
365         } else if (obj != VASE || game.loc == PLAC[PILLOW]) {
366             RSPEAK(54);
367         } else {
368             game.prop[VASE]=2;
369             if (AT(PILLOW))game.prop[VASE]=0;
370             PSPEAK(VASE,game.prop[VASE]+1);
371             if (game.prop[VASE] != 0)game.fixed[VASE]= -1;
372         }
373     }
374     int k = LIQUID();
375     if (k == obj)obj=BOTTLE;
376     if (obj == BOTTLE && k != 0)
377         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         return(8);
772     } else {
773         game.prop[DOOR]=0;
774         if (obj == OIL)game.prop[DOOR]=1;
775         SPK=113+game.prop[DOOR];
776         RSPEAK(SPK);
777         return 2012;
778     }
779 }
780
781 static int quit(FILE *input)
782 /*  Quit.  Intransitive only.  Verify intent and exit if that's what he wants. */
783 {
784     if (YES(input,22,54,54))
785         score(1);
786     return(2012);
787 }
788
789 static int read(FILE *input, token_t obj)
790 /*  Read.  Print stuff based on objtxt.  Oyster (?) is special case. */
791 {
792     int i;
793     if (obj == INTRANSITIVE) {
794         obj = 0;
795         for (i=1; i<=NOBJECTS; i++) {
796             if (HERE(i) && OBJTXT[i] != 0 && game.prop[i] >= 0)
797                 obj = obj * NOBJECTS + i;
798         }
799         if (obj > NOBJECTS || obj == 0 || DARK(game.loc)) return(8000);
800     }
801         
802     if (DARK(game.loc)) {
803         SETPRM(1,WD1,WD1X);
804         RSPEAK(256);
805         return(2012);
806     }
807     if (OBJTXT[obj] == 0 || game.prop[obj] < 0)
808         {RSPEAK(SPK); return 2012;}
809     if (obj == OYSTER && !game.clshnt) {
810         game.clshnt=YES(input,192,193,54);
811         return(2012);
812     }
813     PSPEAK(obj,OBJTXT[obj]+game.prop[obj]);
814     return(2012);
815 }
816
817 static int reservoir(void)
818 /*  Z'ZZZ (word gets recomputed at startup; different each game). */
819 {
820     if (!AT(RESER) && game.loc != game.fixed[RESER]-1) {RSPEAK(SPK); return 2012;}
821     PSPEAK(RESER,game.prop[RESER]+1);
822     game.prop[RESER]=1-game.prop[RESER];
823     if (AT(RESER)) return(2012);
824     game.oldlc2=game.loc;
825     game.newloc=0;
826     RSPEAK(241);
827     return(2);
828 }
829
830 static int rub(token_t obj)
831 /* Rub.  Yields various snide remarks except for lit urn. */
832 {
833     if (obj != LAMP)SPK=76;
834     if (obj != URN || game.prop[URN] != 2) {RSPEAK(SPK); return 2012;}
835     DSTROY(URN);
836     DROP(AMBER,game.loc);
837     game.prop[AMBER]=1;
838     --game.tally;
839     DROP(CAVITY,game.loc);
840     SPK=216;
841     RSPEAK(SPK);
842     return 2012;
843 }
844
845 static int say(void)
846 /* SAY.  Echo WD2 (or WD1 if no WD2 (SAY WHAT?, etc.).)  Magic words override. */
847 {
848     /* FIXME: ugly use of globals */
849     SETPRM(1,WD2,WD2X); if (WD2 <= 0)SETPRM(1,WD1,WD1X);
850     if (WD2 > 0)WD1=WD2;
851     int wd=VOCAB(WD1,-1);
852     if (wd == 62 || wd == 65 || wd == 71 || wd == 2025 || wd == 2034) {
853         WD2=0;
854         return(2630);
855     }
856     RSPEAK(258);
857     return 2012;
858
859 }
860
861 static int throw_support(long spk)
862 {
863     RSPEAK(spk);
864     DROP(AXE,game.loc);
865     return(8);
866 }
867
868 static int throw(FILE *cmdin, long verb, long obj)
869 /*  Throw.  Same as discard unless axe.  Then same as attack except
870  *  ignore bird, and if dwarf is present then one might be killed.
871  *  (Only way to do so!)  Axe also special for dragon, bear, and
872  *  troll.  Treasures special for troll. */
873 {
874     if (TOTING(ROD2) && obj == ROD && !TOTING(ROD))obj=ROD2;
875     if (!TOTING(obj)) {
876         RSPEAK(SPK);    /* FIXME: Defaults from ACTSPK */
877         return 2012;
878     }
879     if (obj >= 50 && obj <= MAXTRS && AT(TROLL)) {
880         SPK=159;
881         /*  Snarf a treasure for the troll. */
882         DROP(obj,0);
883         MOVE(TROLL,0);
884         MOVE(TROLL+NOBJECTS,0);
885         DROP(TROLL2,PLAC[TROLL]);
886         DROP(TROLL2+NOBJECTS,FIXD[TROLL]);
887         JUGGLE(CHASM);
888         RSPEAK(SPK);
889         return 2012;
890     }
891     if (obj == FOOD && HERE(BEAR)) {
892     /* But throwing food is another story. */
893         obj=BEAR;
894         return(feed(obj));
895     }
896     if (obj != AXE)
897         return(discard(obj, false));
898     int i=ATDWRF(game.loc);
899     if (i <= 0) {
900         if (AT(DRAGON) && game.prop[DRAGON] == 0) {
901             SPK=152;
902             return throw_support(SPK);
903         }
904         if (AT(TROLL)) {
905             SPK=158;
906             return throw_support(SPK);
907         }
908         if (AT(OGRE)) {
909             SPK=203;
910             return throw_support(SPK);
911         }
912         if (HERE(BEAR) && game.prop[BEAR] == 0) {
913             /* This'll teach him to throw the axe at the bear! */
914             SPK=164;
915             DROP(AXE,game.loc);
916             game.fixed[AXE]= -1;
917             game.prop[AXE]=1;
918             JUGGLE(BEAR);
919             {RSPEAK(SPK); return 2012;}
920         }
921         return(attack(cmdin, verb, 0));
922     }
923
924     if (randrange(NDWARVES+1) < game.dflag) {
925         SPK=48;
926         return throw_support(SPK);
927     }
928     game.dseen[i]=false;
929     game.dloc[i]=0;
930     SPK=47;
931     ++game.dkill;
932     if (game.dkill == 1)SPK=149;
933
934     return throw_support(SPK);
935 }
936
937 static int vscore(void)
938 /* Score.  Call scoring routine but tell it to return. */
939 {
940     score(-1);
941     return 2012;
942 }
943
944 static int wake(token_t obj)
945 /* Wake.  Only use is to disturb the dwarves. */
946 {
947     if (obj != DWARF || !game.closed) {RSPEAK(SPK); return 2012;}
948     RSPEAK(199);
949     return(19000);
950 }
951
952 static int wave(token_t obj)
953 /* Wave.  No effect unless waving rod at fissure or at bird. */
954 {
955     if ((!TOTING(obj)) && (obj != ROD || !TOTING(ROD2)))SPK=29;
956     if (obj != ROD ||
957        !TOTING(obj) ||
958        (!HERE(BIRD) && (game.closng || !AT(FISSUR)))) {
959         RSPEAK(SPK);
960         return 2012;
961     }
962     if (HERE(BIRD))SPK=206+MOD(game.prop[BIRD],2);
963     if (SPK == 206 && game.loc == game.place[STEPS] && game.prop[JADE] < 0) {
964         DROP(JADE,game.loc);
965         game.prop[JADE]=0;
966         --game.tally;
967         SPK=208;
968         RSPEAK(SPK);
969         return 2012;
970     } else {
971         if (game.closed) {
972             RSPEAK(SPK);        /* FIXME: How is SPK set here? */
973             return(19000);
974         }
975         if (game.closng || !AT(FISSUR)) {RSPEAK(SPK); return 2012;}
976         if (HERE(BIRD))RSPEAK(SPK);
977         game.prop[FISSUR]=1-game.prop[FISSUR];
978         PSPEAK(FISSUR,2-game.prop[FISSUR]);
979         return 2012;
980     }
981 }
982
983 int action(FILE *input, enum speechpart part, long verb, long obj)
984 /*  Analyse a verb.  Remember what it was, go back for object if second word
985  *  unless verb is "say", which snarfs arbitrary second word.
986  */
987 {
988     int kk;
989
990     if (part == unknown)
991     {
992         /*  Analyse an object word.  See if the thing is here, whether
993          *  we've got a verb yet, and so on.  Object must be here
994          *  unless verb is "find" or "invent(ory)" (and no new verb
995          *  yet to be analysed).  Water and oil are also funny, since
996          *  they are never actually dropped at any location, but might
997          *  be here inside the bottle or urn or as a feature of the
998          *  location. */
999         if (HERE(obj))
1000             /* FALL THROUGH */;
1001         else if (obj == GRATE) {
1002             if (game.loc == 1 || game.loc == 4 || game.loc == 7)
1003                 obj=DPRSSN;
1004             if (game.loc > 9 && game.loc < 15)
1005                 obj=ENTRNC;
1006             if (obj != GRATE)
1007                 return(8);
1008         }
1009         else if (obj == DWARF && ATDWRF(game.loc) > 0)
1010             /* FALL THROUGH */;
1011         else if ((LIQUID() == obj && HERE(BOTTLE)) || obj == LIQLOC(game.loc))
1012             /* FALL THROUGH */;
1013         else if (obj == OIL && HERE(URN) && game.prop[URN] != 0) {
1014             obj=URN;
1015             /* FALL THROUGH */;
1016         }
1017         else if (obj == PLANT && AT(PLANT2) && game.prop[PLANT2] != 0) {
1018             obj=PLANT2;
1019             /* FALL THROUGH */;
1020         }
1021         else if (obj == KNIFE && game.knfloc == game.loc) {
1022             game.knfloc= -1;
1023             SPK=116;
1024             {RSPEAK(SPK); return 2012;}
1025         }
1026         else if (obj == ROD && HERE(ROD2)) {
1027             obj=ROD2;
1028             /* FALL THROUGH */;
1029         }
1030         else if ((verb == FIND || verb == INVENT) && WD2 <= 0)
1031             /* FALL THROUGH */;
1032         else {
1033             SETPRM(1,WD1,WD1X);
1034             RSPEAK(256);
1035             return(2012);
1036         }
1037
1038         if (WD2 > 0)
1039             return(2800);
1040         if (verb != 0)
1041             part = transitive;
1042     }
1043
1044     switch(part)
1045     {
1046         case intransitive:
1047             SPK=ACTSPK[verb];
1048             if (WD2 > 0 && verb != SAY) return(2800);
1049             if (verb == SAY)obj=WD2;
1050             if (obj == 0) {
1051                 /*  Analyse an intransitive verb (ie, no object given yet). */
1052                 switch (verb-1) {
1053                     case  0: /* CARRY */ return carry(INTRANSITIVE);
1054                     case  1: /* DROP  */ return(8000); 
1055                     case  2: /* SAY   */ return(8000); 
1056                     case  3: /* UNLOC */ return lock(verb, INTRANSITIVE);    
1057                     case  4: /* NOTHI */ {RSPEAK(54); return(20012);}
1058                     case  5: /* LOCK  */ return lock(verb, INTRANSITIVE);    
1059                     case  6: /* LIGHT */ return light(INTRANSITIVE);    
1060                     case  7: /* EXTIN */ return extinguish(INTRANSITIVE);    
1061                     case  8: /* WAVE  */ return(8000); 
1062                     case  9: /* CALM  */ return(8000); 
1063                     case 10: /* WALK  */ {RSPEAK(SPK); return 2012;} 
1064                     case 11: /* ATTAC */ return attack(input, verb, obj);   
1065                     case 12: /* POUR  */ return pour(obj);   
1066                     case 13: /* EAT   */ return eat(INTRANSITIVE);   
1067                     case 14: /* DRINK */ return drink(obj);   
1068                     case 15: /* RUB   */ return(8000); 
1069                     case 16: /* TOSS  */ return(8000); 
1070                     case 17: /* QUIT  */ return quit(input);   
1071                     case 18: /* FIND  */ return(8000); 
1072                     case 19: /* INVEN */ return inven(obj);   
1073                     case 20: /* FEED  */ return(8000); 
1074                     case 21: /* FILL  */ return fill(obj);   
1075                     case 22: /* BLAST */ return blast();   
1076                     case 23: /* SCOR  */ return vscore();   
1077                     case 24: /* FOO   */ return bigwords(WD1);   
1078                     case 25: /* BRIEF */ return brief();   
1079                     case 26: /* READ  */ return read(input, INTRANSITIVE);   
1080                     case 27: /* BREAK */ return(8000); 
1081                     case 28: /* WAKE  */ return(8000); 
1082                     case 29: /* SUSP  */ return saveresume(input, false);   
1083                     case 30: /* RESU  */ return saveresume(input, true);   
1084                     case 31: /* FLY   */ return fly(INTRANSITIVE);   
1085                     case 32: /* LISTE */ return listen();   
1086                     case 33: /* ZZZZ  */ return reservoir();   
1087                 }
1088                 BUG(23);
1089             }
1090             /* FALLTHRU */
1091         case transitive:
1092             /*  Analyse a transitive verb. */
1093             switch (verb-1) {
1094                 case  0: /* CARRY */ return carry(obj);    
1095                 case  1: /* DROP  */ return discard(obj, false);    
1096                 case  2: /* SAY   */ return say();    
1097                 case  3: /* UNLOC */ return lock(verb, obj);    
1098                 case  4: /* NOTHI */ {RSPEAK(54); return(20012);}
1099                 case  5: /* LOCK  */ return lock(verb, obj);    
1100                 case  6: /* LIGHT */ return light(obj);    
1101                 case  7: /* EXTI  */ return extinguish(obj);    
1102                 case  8: /* WAVE  */ return wave(obj);    
1103                 case  9: /* CALM  */ {RSPEAK(SPK); return 2012;} 
1104                 case 10: /* WALK  */ {RSPEAK(SPK); return 2012;} 
1105                 case 11: /* ATTAC */ return attack(input, verb, obj);   
1106                 case 12: /* POUR  */ return pour(obj);   
1107                 case 13: /* EAT   */ return eat(obj);   
1108                 case 14: /* DRINK */ return drink(obj);   
1109                 case 15: /* RUB   */ return rub(obj);   
1110                 case 16: /* TOSS  */ return throw(input, verb, obj);   
1111                 case 17: /* QUIT  */ {RSPEAK(SPK); return 2012;} 
1112                 case 18: /* FIND  */ return find(obj);   
1113                 case 19: /* INVEN */ return find(obj);   
1114                 case 20: /* FEED  */ return feed(obj);   
1115                 case 21: /* FILL  */ return fill(obj);   
1116                 case 22: /* BLAST */ return blast();   
1117                 case 23: /* SCOR  */ {RSPEAK(SPK); return 2012;} 
1118                 case 24: /* FOO   */ {RSPEAK(SPK); return 2012;} 
1119                 case 25: /* BRIEF */ {RSPEAK(SPK); return 2012;} 
1120                 case 26: /* READ  */ return read(input, obj);   
1121                 case 27: /* BREAK */ return vbreak(obj);   
1122                 case 28: /* WAKE  */ return wake(obj);   
1123                 case 29: /* SUSP  */ {RSPEAK(SPK); return 2012;} 
1124                 case 30: /* RESU  */ {RSPEAK(SPK); return 2012;} 
1125                 case 31: /* FLY   */ return fly(obj);   
1126                 case 32: /* LISTE */ {RSPEAK(SPK); return 2012;} 
1127                 case 33: /* ZZZZ  */ return reservoir();   
1128             }
1129             BUG(24);
1130         case unknown:
1131             /* Unknown verb, couldn't deduce object - might need hint */
1132             SETPRM(1,WD1,WD1X);
1133             RSPEAK(255);
1134             return(2600);
1135     default:
1136             BUG(99);
1137     }
1138 }