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