Source:NetHack 3.6.6/src/timeout.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to timeout.c from the source code of NetHack 3.6.6. To link to a particular line, write [[Source:NetHack 3.6.6/src/timeout.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 3.6	timeout.c	$NHDT-Date: 1573290422 2019/11/09 09:07:02 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.93 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2018. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. #include "lev.h" /* for checking save modes */
8. 
9. STATIC_DCL void NDECL(stoned_dialogue);
10. STATIC_DCL void NDECL(vomiting_dialogue);
11. STATIC_DCL void NDECL(choke_dialogue);
12. STATIC_DCL void NDECL(levitation_dialogue);
13. STATIC_DCL void NDECL(slime_dialogue);
14. STATIC_DCL void FDECL(slimed_to_death, (struct kinfo *));
15. STATIC_DCL void NDECL(phaze_dialogue);
16. STATIC_DCL void FDECL(done_timeout, (int, int));
17. STATIC_DCL void NDECL(slip_or_trip);
18. STATIC_DCL void FDECL(see_lamp_flicker, (struct obj *, const char *));
19. STATIC_DCL void FDECL(lantern_message, (struct obj *));
20. STATIC_DCL void FDECL(cleanup_burn, (ANY_P *, long));
21. 
22. /* used by wizard mode #timeout and #wizintrinsic; order by 'interest'
23.    for timeout countdown, where most won't occur in normal play */
24. const struct propname {
25.     int prop_num;
26.     const char *prop_name;
27. } propertynames[] = {
28.     { INVULNERABLE, "invulnerable" },
29.     { STONED, "petrifying" },
30.     { SLIMED, "becoming slime" },
31.     { STRANGLED, "strangling" },
32.     { SICK, "fatally sick" },
33.     { STUNNED, "stunned" },
34.     { CONFUSION, "confused" },
35.     { HALLUC, "hallucinating" },
36.     { BLINDED, "blinded" },
37.     { DEAF, "deafness" },
38.     { VOMITING, "vomiting" },
39.     { GLIB, "slippery fingers" },
40.     { WOUNDED_LEGS, "wounded legs" },
41.     { SLEEPY, "sleepy" },
42.     { TELEPORT, "teleporting" },
43.     { POLYMORPH, "polymorphing" },
44.     { LEVITATION, "levitating" },
45.     { FAST, "very fast" }, /* timed 'FAST' is very fast */
46.     { CLAIRVOYANT, "clairvoyant" },
47.     { DETECT_MONSTERS, "monster detection" },
48.     { SEE_INVIS, "see invisible" },
49.     { INVIS, "invisible" },
50.     /* properties beyond here don't have timed values during normal play,
51.        so there's not much point in trying to order them sensibly;
52.        they're either on or off based on equipment, role, actions, &c */
53.     { FIRE_RES, "fire resistance" },
54.     { COLD_RES, "cold resistance" },
55.     { SLEEP_RES, "sleep resistance" },
56.     { DISINT_RES, "disintegration resistance" },
57.     { SHOCK_RES, "shock resistance" },
58.     { POISON_RES, "poison resistance" },
59.     { ACID_RES, "acid resistance" },
60.     { STONE_RES, "stoning resistance" },
61.     { DRAIN_RES, "drain resistance" },
62.     { SICK_RES, "sickness resistance" },
63.     { ANTIMAGIC, "magic resistance" },
64.     { HALLUC_RES, "hallucination resistance" },
65.     { FUMBLING, "fumbling" },
66.     { HUNGER, "voracious hunger" },
67.     { TELEPAT, "telepathic" },
68.     { WARNING, "warning" },
69.     { WARN_OF_MON, "warn: monster type or class" },
70.     { WARN_UNDEAD, "warn: undead" },
71.     { SEARCHING, "searching" },
72.     { INFRAVISION, "infravision" },
73.     { ADORNED, "adorned (+/- Cha)" },
74.     { DISPLACED, "displaced" },
75.     { STEALTH, "stealthy" },
76.     { AGGRAVATE_MONSTER, "monster aggravation" },
77.     { CONFLICT, "conflict" },
78.     { JUMPING, "jumping" },
79.     { TELEPORT_CONTROL, "teleport control" },
80.     { FLYING, "flying" },
81.     { WWALKING, "water walking" },
82.     { SWIMMING, "swimming" },
83.     { MAGICAL_BREATHING, "magical breathing" },
84.     { PASSES_WALLS, "pass thru walls" },
85.     { SLOW_DIGESTION, "slow digestion" },
86.     { HALF_SPDAM, "half spell damage" },
87.     { HALF_PHDAM, "half physical damage" },
88.     { REGENERATION, "HP regeneration" },
89.     { ENERGY_REGENERATION, "energy regeneration" },
90.     { PROTECTION, "extra protection" },
91.     { PROT_FROM_SHAPE_CHANGERS, "protection from shape changers" },
92.     { POLYMORPH_CONTROL, "polymorph control" },
93.     { UNCHANGING, "unchanging" },
94.     { REFLECTING, "reflecting" },
95.     { FREE_ACTION, "free action" },
96.     { FIXED_ABIL, "fixed abilities" },
97.     { LIFESAVED, "life will be saved" },
98.     {  0, 0 },
99. };
100. 
101. /* He is being petrified - dialogue by inmet!tower */
102. static NEARDATA const char *const stoned_texts[] = {
103.     "You are slowing down.",            /* 5 */
104.     "Your limbs are stiffening.",       /* 4 */
105.     "Your limbs have turned to stone.", /* 3 */
106.     "You have turned to stone.",        /* 2 */
107.     "You are a statue."                 /* 1 */
108. };
109. 
110. STATIC_OVL void
111. stoned_dialogue()
112. {
113.     register long i = (Stoned & TIMEOUT);
114. 
115.     if (i > 0L && i <= SIZE(stoned_texts)) {
116.         char buf[BUFSZ];
117. 
118.         Strcpy(buf, stoned_texts[SIZE(stoned_texts) - i]);
119.         if (nolimbs(youmonst.data) && strstri(buf, "limbs"))
120.             (void) strsubst(buf, "limbs", "extremities");
121.         pline1(buf);
122.     }
123.     switch ((int) i) {
124.     case 5: /* slowing down */
125.         HFast = 0L;
126.         if (multi > 0)
127.             nomul(0);
128.         break;
129.     case 4: /* limbs stiffening */
130.         /* just one move left to save oneself so quit fiddling around;
131.            don't stop attempt to eat tin--might be lizard or acidic */
132.         if (!Popeye(STONED))
133.             stop_occupation();
134.         if (multi > 0)
135.             nomul(0);
136.         break;
137.     case 3: /* limbs turned to stone */
138.         stop_occupation();
139.         nomul(-3); /* can't move anymore */
140.         multi_reason = "getting stoned";
141.         nomovemsg = You_can_move_again; /* not unconscious */
142.         /* "your limbs have turned to stone" so terminate wounded legs */
143.         if (Wounded_legs && !u.usteed)
144.             heal_legs(2);
145.         break;
146.     case 2: /* turned to stone */
147.         if ((HDeaf & TIMEOUT) > 0L && (HDeaf & TIMEOUT) < 5L)
148.             set_itimeout(&HDeaf, 5L); /* avoid Hear_again at tail end */
149.         /* if also vomiting or turning into slime, stop those (no messages) */
150.         if (Vomiting)
151.             make_vomiting(0L, FALSE);
152.         if (Slimed)
153.             make_slimed(0L, (char *) 0);
154.         break;
155.     default:
156.         break;
157.     }
158.     exercise(A_DEX, FALSE);
159. }
160. 
161. /* hero is getting sicker and sicker prior to vomiting */
162. static NEARDATA const char *const vomiting_texts[] = {
163.     "are feeling mildly nauseated.", /* 14 */
164.     "feel slightly confused.",       /* 11 */
165.     "can't seem to think straight.", /* 8 */
166.     "feel incredibly sick.",         /* 5 */
167.     "are about to vomit."            /* 2 */
168. };
169. 
170. STATIC_OVL void
171. vomiting_dialogue()
172. {
173.     const char *txt = 0;
174.     long v = (Vomiting & TIMEOUT);
175. 
176.     /* note: nhtimeout() hasn't decremented timed properties for the
177.        current turn yet, so we use Vomiting-1 here */
178.     switch ((int) (v - 1L)) {
179.     case 14:
180.         txt = vomiting_texts[0];
181.         break;
182.     case 11:
183.         txt = vomiting_texts[1];
184.         break;
185.     case 6:
186.         make_stunned((HStun & TIMEOUT) + (long) d(2, 4), FALSE);
187.         if (!Popeye(VOMITING))
188.             stop_occupation();
189.     /*FALLTHRU*/
190.     case 9:
191.         make_confused((HConfusion & TIMEOUT) + (long) d(2, 4), FALSE);
192.         if (multi > 0)
193.             nomul(0);
194.         break;
195.     case 8:
196.         txt = vomiting_texts[2];
197.         break;
198.     case 5:
199.         txt = vomiting_texts[3];
200.         break;
201.     case 2:
202.         txt = vomiting_texts[4];
203.         if (cantvomit(youmonst.data))
204.             txt = "gag uncontrollably.";
205.         else if (Hallucination)
206.             /* "hurl" is short for "hurl chunks" which is slang for
207.                relatively violent vomiting... */
208.             txt = "are about to hurl!";
209.         break;
210.     case 0:
211.         stop_occupation();
212.         if (!cantvomit(youmonst.data)) {
213.             morehungry(20);
214.             /* case 2 used to be "You suddenly vomit!" but it wasn't sudden
215.                since you've just been through the earlier messages of the
216.                countdown, and it was still possible to move around between
217.                that message and "You can move again." (from vomit()'s
218.                nomul(-2)) with no intervening message; give one here to
219.                have more specific point at which hero became unable to move
220.                [vomit() issues its own message for the cantvomit() case
221.                and for the FAINTING-or-worse case where stomach is empty] */
222.             if (u.uhs < FAINTING)
223.                 You("%s!", !Hallucination ? "vomit" : "hurl chunks");
224.         }
225.         vomit();
226.         break;
227.     default:
228.         break;
229.     }
230.     if (txt)
231.         You1(txt);
232.     exercise(A_CON, FALSE);
233. }
234. 
235. static NEARDATA const char *const choke_texts[] = {
236.     "You find it hard to breathe.",
237.     "You're gasping for air.",
238.     "You can no longer breathe.",
239.     "You're turning %s.",
240.     "You suffocate."
241. };
242. 
243. static NEARDATA const char *const choke_texts2[] = {
244.     "Your %s is becoming constricted.",
245.     "Your blood is having trouble reaching your brain.",
246.     "The pressure on your %s increases.",
247.     "Your consciousness is fading.",
248.     "You suffocate."
249. };
250. 
251. STATIC_OVL void
252. choke_dialogue()
253. {
254.     register long i = (Strangled & TIMEOUT);
255. 
256.     if (i > 0 && i <= SIZE(choke_texts)) {
257.         if (Breathless || !rn2(50))
258.             pline(choke_texts2[SIZE(choke_texts2) - i], body_part(NECK));
259.         else {
260.             const char *str = choke_texts[SIZE(choke_texts) - i];
261. 
262.             if (index(str, '%'))
263.                 pline(str, hcolor(NH_BLUE));
264.             else
265.                 pline1(str);
266.         }
267.     }
268.     exercise(A_STR, FALSE);
269. }
270. 
271. static NEARDATA const char *const levi_texts[] = {
272.     "You float slightly lower.",
273.     "You wobble unsteadily %s the %s."
274. };
275. 
276. STATIC_OVL void
277. levitation_dialogue()
278. {
279.     /* -1 because the last message comes via float_down() */
280.     long i = (((HLevitation & TIMEOUT) - 1L) / 2L);
281. 
282.     if (ELevitation)
283.         return;
284. 
285.     if (!ACCESSIBLE(levl[u.ux][u.uy].typ)
286.         && !is_pool_or_lava(u.ux,u.uy))
287.         return;
288. 
289.     if (((HLevitation & TIMEOUT) % 2L) && i > 0L && i <= SIZE(levi_texts)) {
290.         const char *s = levi_texts[SIZE(levi_texts) - i];
291. 
292.         if (index(s, '%')) {
293.             boolean danger = (is_pool_or_lava(u.ux, u.uy)
294.                               && !Is_waterlevel(&u.uz));
295. 
296.             pline(s, danger ? "over" : "in",
297.                   danger ? surface(u.ux, u.uy) : "air");
298.         } else
299.             pline1(s);
300.     }
301. }
302. 
303. static NEARDATA const char *const slime_texts[] = {
304.     "You are turning a little %s.",   /* 5 */
305.     "Your limbs are getting oozy.",   /* 4 */
306.     "Your skin begins to peel away.", /* 3 */
307.     "You are turning into %s.",       /* 2 */
308.     "You have become %s."             /* 1 */
309. };
310. 
311. STATIC_OVL void
312. slime_dialogue()
313. {
314.     register long i = (Slimed & TIMEOUT) / 2L;
315. 
316.     if (i == 1L) {
317.         /* display as green slime during "You have become green slime."
318.            but don't worry about not being able to see self; if already
319.            mimicking something else at the time, implicitly be revealed */
320.         youmonst.m_ap_type = M_AP_MONSTER;
321.         youmonst.mappearance = PM_GREEN_SLIME;
322.     }
323.     if (((Slimed & TIMEOUT) % 2L) && i >= 0L && i < SIZE(slime_texts)) {
324.         char buf[BUFSZ];
325. 
326.         Strcpy(buf, slime_texts[SIZE(slime_texts) - i - 1L]);
327.         if (nolimbs(youmonst.data) && strstri(buf, "limbs"))
328.             (void) strsubst(buf, "limbs", "extremities");
329. 
330.         if (index(buf, '%')) {
331.             if (i == 4L) {  /* "you are turning green" */
332.                 if (!Blind) /* [what if you're already green?] */
333.                     pline(buf, hcolor(NH_GREEN));
334.             } else
335.                 pline(buf,
336.                       an(Hallucination ? rndmonnam(NULL) : "green slime"));
337.         } else
338.             pline1(buf);
339.     }
340. 
341.     switch (i) {
342.     case 3L:  /* limbs becoming oozy */
343.         HFast = 0L; /* lose intrinsic speed */
344.         if (!Popeye(SLIMED))
345.             stop_occupation();
346.         if (multi > 0)
347.             nomul(0);
348.         break;
349.     case 2L: /* skin begins to peel */
350.         if ((HDeaf & TIMEOUT) > 0L && (HDeaf & TIMEOUT) < 5L)
351.             set_itimeout(&HDeaf, 5L); /* avoid Hear_again at tail end */
352.         break;
353.     case 1L: /* turning into slime */
354.         /* if also turning to stone, stop doing that (no message) */
355.         if (Stoned)
356.             make_stoned(0L, (char *) 0, KILLED_BY_AN, (char *) 0);
357.         break;
358.     }
359.     exercise(A_DEX, FALSE);
360. }
361. 
362. void
363. burn_away_slime()
364. {
365.     if (Slimed) {
366.         make_slimed(0L, "The slime that covers you is burned away!");
367.     }
368. }
369. 
370. /* countdown timer for turning into green slime has run out; kill our hero */
371. STATIC_OVL void
372. slimed_to_death(kptr)
373. struct kinfo *kptr;
374. {
375.     uchar save_mvflags;
376. 
377.     /* redundant: polymon() cures sliming when polying into green slime */
378.     if (Upolyd && youmonst.data == &mons[PM_GREEN_SLIME]) {
379.         dealloc_killer(kptr);
380.         return;
381.     }
382.     /* more sure killer reason is set up */
383.     if (kptr && kptr->name[0]) {
384.         killer.format = kptr->format;
385.         Strcpy(killer.name, kptr->name);
386.     } else {
387.         killer.format = NO_KILLER_PREFIX;
388.         Strcpy(killer.name, "turned into green slime");
389.     }
390.     dealloc_killer(kptr);
391. 
392.     /*
393.      * Polymorph into a green slime, which might destroy some worn armor
394.      * (potentially affecting bones) and dismount from steed.
395.      * Can't be Unchanging; wouldn't have turned into slime if we were.
396.      * Despite lack of Unchanging, neither done() nor savelife() calls
397.      * rehumanize() if hero dies while polymorphed.
398.      * polymon() undoes the slime countdown's mimick-green-slime hack
399.      * but does not perform polyself()'s light source bookkeeping.
400.      * No longer need to manually increment uconduct.polyselfs to reflect
401.      * [formerly implicit] change of form; polymon() takes care of that.
402.      * Temporarily ungenocide if necessary.
403.      */
404.     if (emits_light(youmonst.data))
405.         del_light_source(LS_MONSTER, monst_to_any(&youmonst));
406.     save_mvflags = mvitals[PM_GREEN_SLIME].mvflags;
407.     mvitals[PM_GREEN_SLIME].mvflags = save_mvflags & ~G_GENOD;
408.     /* become a green slime; also resets youmonst.m_ap_type+.mappearance */
409.     (void) polymon(PM_GREEN_SLIME);
410.     mvitals[PM_GREEN_SLIME].mvflags = save_mvflags;
411.     done_timeout(TURNED_SLIME, SLIMED);
412. 
413.     /* life-saved; even so, hero still has turned into green slime;
414.        player may have genocided green slimes after being infected */
415.     if ((mvitals[PM_GREEN_SLIME].mvflags & G_GENOD) != 0) {
416.         char slimebuf[BUFSZ];
417. 
418.         killer.format = KILLED_BY;
419.         Strcpy(killer.name, "slimicide");
420.         /* vary the message depending upon whether life-save was due to
421.            amulet or due to declining to die in explore or wizard mode */
422.         Strcpy(slimebuf, "green slime has been genocided...");
423.         if (iflags.last_msg == PLNMSG_OK_DONT_DIE)
424.             /* follows "OK, so you don't die." and arg is second sentence */
425.             pline("Yes, you do.  %s", upstart(slimebuf));
426.         else
427.             /* follows "The medallion crumbles to dust." */
428.             pline("Unfortunately, %s", slimebuf);
429.         /* die again; no possibility of amulet this time */
430.         done(GENOCIDED); /* [should it be done_timeout(GENOCIDED, SLIMED)?] */
431.         /* could be life-saved again (only in explore or wizard mode)
432.            but green slimes are gone; just stay in current form */
433.     }
434.     return;
435. }
436. 
437. /* Intrinsic Passes_walls is temporary when your god is trying to fix
438.    all troubles and then TROUBLE_STUCK_IN_WALL calls safe_teleds() but
439.    it can't find anywhere to place you.  If that happens you get a small
440.    value for (HPasses_walls & TIMEOUT) to move somewhere yourself.
441.    Message given is "you feel much slimmer" as a joke hint that you can
442.    move between things which are closely packed--like the substance of
443.    solid rock! */
444. static NEARDATA const char *const phaze_texts[] = {
445.     "You start to feel bloated.",
446.     "You are feeling rather flabby.",
447. };
448. 
449. STATIC_OVL void
450. phaze_dialogue()
451. {
452.     long i = ((HPasses_walls & TIMEOUT) / 2L);
453. 
454.     if (EPasses_walls || (HPasses_walls & ~TIMEOUT))
455.         return;
456. 
457.     if (((HPasses_walls & TIMEOUT) % 2L) && i > 0L && i <= SIZE(phaze_texts))
458.         pline1(phaze_texts[SIZE(phaze_texts) - i]);
459. }
460. 
461. /* when a status timeout is fatal, keep the status line indicator shown
462.    during end of game rundown (and potential dumplog);
463.    timeout has already counted down to 0 by the time we get here */
464. STATIC_OVL void
465. done_timeout(how, which)
466. int how, which;
467. {
468.     long *intrinsic_p = &u.uprops[which].intrinsic;
469. 
470.     *intrinsic_p |= I_SPECIAL; /* affects final disclosure */
471.     done(how);
472. 
473.     /* life-saved */
474.     *intrinsic_p &= ~I_SPECIAL;
475.     context.botl = TRUE;
476. }
477. 
478. void
479. nh_timeout()
480. {
481.     register struct prop *upp;
482.     struct kinfo *kptr;
483.     boolean was_flying;
484.     int sleeptime;
485.     int m_idx;
486.     int baseluck = (flags.moonphase == FULL_MOON) ? 1 : 0;
487. 
488.     if (flags.friday13)
489.         baseluck -= 1;
490. 
491.     if (u.uluck != baseluck
492.         && moves % ((u.uhave.amulet || u.ugangr) ? 300 : 600) == 0) {
493.         /* Cursed luckstones stop bad luck from timing out; blessed luckstones
494.          * stop good luck from timing out; normal luckstones stop both;
495.          * neither is stopped if you don't have a luckstone.
496.          * Luck is based at 0 usually, +1 if a full moon and -1 on Friday 13th
497.          */
498.         register int time_luck = stone_luck(FALSE);
499.         boolean nostone = !carrying(LUCKSTONE) && !stone_luck(TRUE);
500. 
501.         if (u.uluck > baseluck && (nostone || time_luck < 0))
502.             u.uluck--;
503.         else if (u.uluck < baseluck && (nostone || time_luck > 0))
504.             u.uluck++;
505.     }
506.     if (u.uinvulnerable)
507.         return; /* things past this point could kill you */
508.     if (Stoned)
509.         stoned_dialogue();
510.     if (Slimed)
511.         slime_dialogue();
512.     if (Vomiting)
513.         vomiting_dialogue();
514.     if (Strangled)
515.         choke_dialogue();
516.     if (HLevitation & TIMEOUT)
517.         levitation_dialogue();
518.     if (HPasses_walls & TIMEOUT)
519.         phaze_dialogue();
520.     if (u.mtimedone && !--u.mtimedone) {
521.         if (Unchanging)
522.             u.mtimedone = rnd(100 * youmonst.data->mlevel + 1);
523.         else if (is_were(youmonst.data))
524.             you_unwere(FALSE); /* if polycontrl, asks whether to rehumanize */
525.         else
526.             rehumanize();
527.     }
528.     if (u.ucreamed)
529.         u.ucreamed--;
530. 
531.     /* Dissipate spell-based protection. */
532.     if (u.usptime) {
533.         if (--u.usptime == 0 && u.uspellprot) {
534.             u.usptime = u.uspmtime;
535.             u.uspellprot--;
536.             find_ac();
537.             if (!Blind)
538.                 Norep("The %s haze around you %s.", hcolor(NH_GOLDEN),
539.                       u.uspellprot ? "becomes less dense" : "disappears");
540.         }
541.     }
542. 
543.     if (u.ugallop) {
544.         if (--u.ugallop == 0L && u.usteed)
545.             pline("%s stops galloping.", Monnam(u.usteed));
546.     }
547. 
548.     was_flying = Flying;
549.     for (upp = u.uprops; upp < u.uprops + SIZE(u.uprops); upp++)
550.         if ((upp->intrinsic & TIMEOUT) && !(--upp->intrinsic & TIMEOUT)) {
551.             kptr = find_delayed_killer((int) (upp - u.uprops));
552.             switch (upp - u.uprops) {
553.             case STONED:
554.                 if (kptr && kptr->name[0]) {
555.                     killer.format = kptr->format;
556.                     Strcpy(killer.name, kptr->name);
557.                 } else {
558.                     killer.format = NO_KILLER_PREFIX;
559.                     Strcpy(killer.name, "killed by petrification");
560.                 }
561.                 dealloc_killer(kptr);
562.                 /* (unlike sliming, you aren't changing form here) */
563.                 done_timeout(STONING, STONED);
564.                 break;
565.             case SLIMED:
566.                 slimed_to_death(kptr); /* done_timeout(TURNED_SLIME,SLIMED) */
567.                 break;
568.             case VOMITING:
569.                 make_vomiting(0L, TRUE);
570.                 break;
571.             case SICK:
572.                 You("die from your illness.");
573.                 if (kptr && kptr->name[0]) {
574.                     killer.format = kptr->format;
575.                     Strcpy(killer.name, kptr->name);
576.                 } else {
577.                     killer.format = KILLED_BY_AN;
578.                     killer.name[0] = 0; /* take the default */
579.                 }
580.                 dealloc_killer(kptr);
581. 
582.                 if ((m_idx = name_to_mon(killer.name)) >= LOW_PM) {
583.                     if (type_is_pname(&mons[m_idx])) {
584.                         killer.format = KILLED_BY;
585.                     } else if (mons[m_idx].geno & G_UNIQ) {
586.                         Strcpy(killer.name, the(killer.name));
587.                         killer.format = KILLED_BY;
588.                     }
589.                 }
590.                 done_timeout(POISONING, SICK);
591.                 u.usick_type = 0;
592.                 break;
593.             case FAST:
594.                 if (!Very_fast)
595.                     You_feel("yourself slowing down%s.",
596.                              Fast ? " a bit" : "");
597.                 break;
598.             case CONFUSION:
599.                 /* So make_confused works properly */
600.                 set_itimeout(&HConfusion, 1L);
601.                 make_confused(0L, TRUE);
602.                 if (!Confusion)
603.                     stop_occupation();
604.                 break;
605.             case STUNNED:
606.                 set_itimeout(&HStun, 1L);
607.                 make_stunned(0L, TRUE);
608.                 if (!Stunned)
609.                     stop_occupation();
610.                 break;
611.             case BLINDED:
612.                 set_itimeout(&Blinded, 1L);
613.                 make_blinded(0L, TRUE);
614.                 if (!Blind)
615.                     stop_occupation();
616.                 break;
617.             case DEAF:
618.                 set_itimeout(&HDeaf, 1L);
619.                 make_deaf(0L, TRUE);
620.                 context.botl = TRUE;
621.                 if (!Deaf)
622.                     stop_occupation();
623.                 break;
624.             case INVIS:
625.                 newsym(u.ux, u.uy);
626.                 if (!Invis && !BInvis && !Blind) {
627.                     You(!See_invisible
628.                             ? "are no longer invisible."
629.                             : "can no longer see through yourself.");
630.                     stop_occupation();
631.                 }
632.                 break;
633.             case SEE_INVIS:
634.                 set_mimic_blocking(); /* do special mimic handling */
635.                 see_monsters();       /* make invis mons appear */
636.                 newsym(u.ux, u.uy);   /* make self appear */
637.                 stop_occupation();
638.                 break;
639.             case WOUNDED_LEGS:
640.                 heal_legs(0);
641.                 stop_occupation();
642.                 break;
643.             case HALLUC:
644.                 set_itimeout(&HHallucination, 1L);
645.                 (void) make_hallucinated(0L, TRUE, 0L);
646.                 if (!Hallucination)
647.                     stop_occupation();
648.                 break;
649.             case SLEEPY:
650.                 if (unconscious() || Sleep_resistance) {
651.                     incr_itimeout(&HSleepy, rnd(100));
652.                 } else if (Sleepy) {
653.                     You("fall asleep.");
654.                     sleeptime = rnd(20);
655.                     fall_asleep(-sleeptime, TRUE);
656.                     incr_itimeout(&HSleepy, sleeptime + rnd(100));
657.                 }
658.                 break;
659.             case LEVITATION:
660.                 (void) float_down(I_SPECIAL | TIMEOUT, 0L);
661.                 break;
662.             case FLYING:
663.                 /* timed Flying is via #wizintrinsic only */
664.                 if (was_flying && !Flying) {
665.                     context.botl = 1;
666.                     You("land.");
667.                     spoteffects(TRUE);
668.                 }
669.                 break;
670.             case WARN_OF_MON:
671.                 /* timed Warn_of_mon is via #wizintrinsic only */
672.                 if (!Warn_of_mon) {
673.                     context.warntype.speciesidx = NON_PM;
674.                     if (context.warntype.species) {
675.                         You("are no longer warned about %s.",
676.                             makeplural(context.warntype.species->mname));
677.                         context.warntype.species = (struct permonst *) 0;
678.                     }
679.                 }
680.                 break;
681.             case PASSES_WALLS:
682.                 if (!Passes_walls) {
683.                     if (stuck_in_wall())
684.                         You_feel("hemmed in again.");
685.                     else
686.                         pline("You're back to your %s self again.",
687.                               !Upolyd ? "normal" : "unusual");
688.                 }
689.                 break;
690.             case STRANGLED:
691.                 killer.format = KILLED_BY;
692.                 Strcpy(killer.name,
693.                        (u.uburied) ? "suffocation" : "strangulation");
694.                 done_timeout(DIED, STRANGLED);
695.                 /* must be declining to die in explore|wizard mode;
696.                    treat like being cured of strangulation by prayer */
697.                 if (uamul && uamul->otyp == AMULET_OF_STRANGULATION) {
698.                     Your("amulet vanishes!");
699.                     useup(uamul);
700.                 }
701.                 break;
702.             case FUMBLING:
703.                 /* call this only when a move took place.  */
704.                 /* otherwise handle fumbling msgs locally. */
705.                 if (u.umoved && !Levitation) {
706.                     slip_or_trip();
707.                     nomul(-2);
708.                     multi_reason = "fumbling";
709.                     nomovemsg = "";
710.                     /* The more you are carrying the more likely you
711.                      * are to make noise when you fumble.  Adjustments
712.                      * to this number must be thoroughly play tested.
713.                      */
714.                     if ((inv_weight() > -500)) {
715.                         You("make a lot of noise!");
716.                         wake_nearby();
717.                     }
718.                 }
719.                 /* from outside means slippery ice; don't reset
720.                    counter if that's the only fumble reason */
721.                 HFumbling &= ~FROMOUTSIDE;
722.                 if (Fumbling)
723.                     incr_itimeout(&HFumbling, rnd(20));
724.                 break;
725.             case DETECT_MONSTERS:
726.                 see_monsters();
727.                 break;
728.             case GLIB:
729.                 make_glib(0); /* might update persistent inventory */
730.                 break;
731.             }
732.         }
733. 
734.     run_timers();
735. }
736. 
737. void
738. fall_asleep(how_long, wakeup_msg)
739. int how_long;
740. boolean wakeup_msg;
741. {
742.     stop_occupation();
743.     nomul(how_long);
744.     multi_reason = "sleeping";
745.     /* generally don't notice sounds while sleeping */
746.     if (wakeup_msg && multi == how_long) {
747.         /* caller can follow with a direct call to Hear_again() if
748.            there's a need to override this when wakeup_msg is true */
749.         incr_itimeout(&HDeaf, how_long);
750.         context.botl = TRUE;
751.         afternmv = Hear_again; /* this won't give any messages */
752.     }
753.     /* early wakeup from combat won't be possible until next monster turn */
754.     u.usleep = monstermoves;
755.     nomovemsg = wakeup_msg ? "You wake up." : You_can_move_again;
756. }
757. 
758. /* Attach an egg hatch timeout to the given egg.
759.  *      when = Time to hatch, usually only passed if re-creating an
760.  *             existing hatch timer. Pass 0L for random hatch time.
761.  */
762. void
763. attach_egg_hatch_timeout(egg, when)
764. struct obj *egg;
765. long when;
766. {
767.     int i;
768. 
769.     /* stop previous timer, if any */
770.     (void) stop_timer(HATCH_EGG, obj_to_any(egg));
771. 
772.     /*
773.      * Decide if and when to hatch the egg.  The old hatch_it() code tried
774.      * once a turn from age 151 to 200 (inclusive), hatching if it rolled
775.      * a number x, 1<=x<=age, where x>150.  This yields a chance of
776.      * hatching > 99.9993%.  Mimic that here.
777.      */
778.     if (!when) {
779.         for (i = (MAX_EGG_HATCH_TIME - 50) + 1; i <= MAX_EGG_HATCH_TIME; i++)
780.             if (rnd(i) > 150) {
781.                 /* egg will hatch */
782.                 when = (long) i;
783.                 break;
784.             }
785.     }
786.     if (when) {
787.         (void) start_timer(when, TIMER_OBJECT, HATCH_EGG, obj_to_any(egg));
788.     }
789. }
790. 
791. /* prevent an egg from ever hatching */
792. void
793. kill_egg(egg)
794. struct obj *egg;
795. {
796.     /* stop previous timer, if any */
797.     (void) stop_timer(HATCH_EGG, obj_to_any(egg));
798. }
799. 
800. /* timer callback routine: hatch the given egg */
801. void
802. hatch_egg(arg, timeout)
803. anything *arg;
804. long timeout;
805. {
806.     struct obj *egg;
807.     struct monst *mon, *mon2;
808.     coord cc;
809.     xchar x, y;
810.     boolean yours, silent, knows_egg = FALSE;
811.     boolean cansee_hatchspot = FALSE;
812.     int i, mnum, hatchcount = 0;
813. 
814.     egg = arg->a_obj;
815.     /* sterilized while waiting */
816.     if (egg->corpsenm == NON_PM)
817.         return;
818. 
819.     mon = mon2 = (struct monst *) 0;
820.     mnum = big_to_little(egg->corpsenm);
821.     /* The identity of one's father is learned, not innate */
822.     yours = (egg->spe || (!flags.female && carried(egg) && !rn2(2)));
823.     silent = (timeout != monstermoves); /* hatched while away */
824. 
825.     /* only can hatch when in INVENT, FLOOR, MINVENT */
826.     if (get_obj_location(egg, &x, &y, 0)) {
827.         hatchcount = rnd((int) egg->quan);
828.         cansee_hatchspot = cansee(x, y) && !silent;
829.         if (!(mons[mnum].geno & G_UNIQ)
830.             && !(mvitals[mnum].mvflags & (G_GENOD | G_EXTINCT))) {
831.             for (i = hatchcount; i > 0; i--) {
832.                 if (!enexto(&cc, x, y, &mons[mnum])
833.                     || !(mon = makemon(&mons[mnum], cc.x, cc.y, NO_MINVENT)))
834.                     break;
835.                 /* tame if your own egg hatches while you're on the
836.                    same dungeon level, or any dragon egg which hatches
837.                    while it's in your inventory */
838.                 if ((yours && !silent)
839.                     || (carried(egg) && mon->data->mlet == S_DRAGON)) {
840.                     if (tamedog(mon, (struct obj *) 0)) {
841.                         if (carried(egg) && mon->data->mlet != S_DRAGON)
842.                             mon->mtame = 20;
843.                     }
844.                 }
845.                 if (mvitals[mnum].mvflags & G_EXTINCT)
846.                     break;  /* just made last one */
847.                 mon2 = mon; /* in case makemon() fails on 2nd egg */
848.             }
849.             if (!mon)
850.                 mon = mon2;
851.             hatchcount -= i;
852.             egg->quan -= (long) hatchcount;
853.         }
854. #if 0
855.     /*
856.      * We could possibly hatch while migrating, but the code isn't
857.      * set up for it...
858.      */
859.     } else if (obj->where == OBJ_MIGRATING) {
860.         /*
861.          * We can do several things.  The first ones that come to
862.          * mind are:
863.          * + Create the hatched monster then place it on the migrating
864.          *   mons list.  This is tough because all makemon() is made
865.          *   to place the monster as well.  Makemon() also doesn't lend
866.          *   itself well to splitting off a "not yet placed" subroutine.
867.          * + Mark the egg as hatched, then place the monster when we
868.          *   place the migrating objects.
869.          * + Or just kill any egg which gets sent to another level.
870.          *   Falling is the usual reason such transportation occurs.
871.          */
872.         cansee_hatchspot = FALSE;
873.         mon = ???;
874. #endif
875.     }
876. 
877.     if (mon) {
878.         char monnambuf[BUFSZ], carriedby[BUFSZ];
879.         boolean siblings = (hatchcount > 1), redraw = FALSE;
880. 
881.         if (cansee_hatchspot) {
882.             /* [bug?  m_monnam() yields accurate monster type
883.                regardless of hallucination] */
884.             Sprintf(monnambuf, "%s%s", siblings ? "some " : "",
885.                     siblings ? makeplural(m_monnam(mon)) : an(m_monnam(mon)));
886.             /* we don't learn the egg type here because learning
887.                an egg type requires either seeing the egg hatch
888.                or being familiar with the egg already,
889.                as well as being able to see the resulting
890.                monster, checked below
891.             */
892.         }
893.         switch (egg->where) {
894.         case OBJ_INVENT:
895.             knows_egg = TRUE; /* true even if you are blind */
896.             if (!cansee_hatchspot)
897.                 You_feel("%s %s from your pack!", something,
898.                          locomotion(mon->data, "drop"));
899.             else
900.                 You_see("%s %s out of your pack!", monnambuf,
901.                         locomotion(mon->data, "drop"));
902.             if (yours) {
903.                 pline("%s cries sound like \"%s%s\"",
904.                       siblings ? "Their" : "Its",
905.                       flags.female ? "mommy" : "daddy", egg->spe ? "." : "?");
906.             } else if (mon->data->mlet == S_DRAGON && !Deaf) {
907.                 verbalize("Gleep!"); /* Mything eggs :-) */
908.             }
909.             break;
910. 
911.         case OBJ_FLOOR:
912.             if (cansee_hatchspot) {
913.                 knows_egg = TRUE;
914.                 You_see("%s hatch.", monnambuf);
915.                 redraw = TRUE; /* update egg's map location */
916.             }
917.             break;
918. 
919.         case OBJ_MINVENT:
920.             if (cansee_hatchspot) {
921.                 /* egg carrying monster might be invisible */
922.                 mon2 = egg->ocarry;
923.                 if (canseemon(mon2)
924.                     && (!mon2->wormno || cansee(mon2->mx, mon2->my))) {
925.                     Sprintf(carriedby, "%s pack",
926.                             s_suffix(a_monnam(mon2)));
927.                     knows_egg = TRUE;
928.                 } else if (is_pool(mon->mx, mon->my)) {
929.                     Strcpy(carriedby, "empty water");
930.                 } else {
931.                     Strcpy(carriedby, "thin air");
932.                 }
933.                 You_see("%s %s out of %s!", monnambuf,
934.                         locomotion(mon->data, "drop"), carriedby);
935.             }
936.             break;
937. #if 0
938.         case OBJ_MIGRATING:
939.             break;
940. #endif
941.         default:
942.             impossible("egg hatched where? (%d)", (int) egg->where);
943.             break;
944.         }
945. 
946.         if (cansee_hatchspot && knows_egg)
947.             learn_egg_type(mnum);
948. 
949.         if (egg->quan > 0) {
950.             /* still some eggs left */
951.             /* Instead of ordinary egg timeout use a short one */
952.             attach_egg_hatch_timeout(egg, (long) rnd(12));
953.         } else if (carried(egg)) {
954.             useup(egg);
955.         } else {
956.             /* free egg here because we use it above */
957.             obj_extract_self(egg);
958.             obfree(egg, (struct obj *) 0);
959.         }
960.         if (redraw)
961.             newsym(x, y);
962.     }
963. }
964. 
965. /* Learn to recognize eggs of the given type. */
966. void
967. learn_egg_type(mnum)
968. int mnum;
969. {
970.     /* baby monsters hatch from grown-up eggs */
971.     mnum = little_to_big(mnum);
972.     mvitals[mnum].mvflags |= MV_KNOWS_EGG;
973.     /* we might have just learned about other eggs being carried */
974.     update_inventory();
975. }
976. 
977. /* Attach a fig_transform timeout to the given figurine. */
978. void
979. attach_fig_transform_timeout(figurine)
980. struct obj *figurine;
981. {
982.     int i;
983. 
984.     /* stop previous timer, if any */
985.     (void) stop_timer(FIG_TRANSFORM, obj_to_any(figurine));
986. 
987.     /*
988.      * Decide when to transform the figurine.
989.      */
990.     i = rnd(9000) + 200;
991.     /* figurine will transform */
992.     (void) start_timer((long) i, TIMER_OBJECT, FIG_TRANSFORM,
993.                        obj_to_any(figurine));
994. }
995. 
996. /* give a fumble message */
997. STATIC_OVL void
998. slip_or_trip()
999. {
1000.     struct obj *otmp = vobj_at(u.ux, u.uy), *otmp2;
1001.     const char *what;
1002.     char buf[BUFSZ];
1003.     boolean on_foot = TRUE;
1004.     if (u.usteed)
1005.         on_foot = FALSE;
1006. 
1007.     if (otmp && on_foot && !u.uinwater && is_pool(u.ux, u.uy))
1008.         otmp = 0;
1009. 
1010.     if (otmp && on_foot) { /* trip over something in particular */
1011.         /*
1012.           If there is only one item, it will have just been named
1013.           during the move, so refer to by via pronoun; otherwise,
1014.           if the top item has been or can be seen, refer to it by
1015.           name; if not, look for rocks to trip over; trip over
1016.           anonymous "something" if there aren't any rocks.
1017.         */
1018.         what = (iflags.last_msg == PLNMSG_ONE_ITEM_HERE)
1019.                 ? ((otmp->quan == 1L) ? "it"
1020.                       : Hallucination ? "they" : "them")
1021.                 : (otmp->dknown || !Blind)
1022.                       ? doname(otmp)
1023.                       : ((otmp2 = sobj_at(ROCK, u.ux, u.uy)) == 0
1024.                              ? something
1025.                              : (otmp2->quan == 1L ? "a rock" : "some rocks"));
1026.         if (Hallucination) {
1027.             what = strcpy(buf, what);
1028.             buf[0] = highc(buf[0]);
1029.             pline("Egads!  %s bite%s your %s!", what,
1030.                   (!otmp || otmp->quan == 1L) ? "s" : "", body_part(FOOT));
1031.         } else {
1032.             You("trip over %s.", what);
1033.         }
1034.         if (!uarmf && otmp->otyp == CORPSE
1035.             && touch_petrifies(&mons[otmp->corpsenm]) && !Stone_resistance) {
1036.             Sprintf(killer.name, "tripping over %s corpse",
1037.                     an(mons[otmp->corpsenm].mname));
1038.             instapetrify(killer.name);
1039.         }
1040.     } else if (rn2(3) && is_ice(u.ux, u.uy)) {
1041.         pline("%s %s%s on the ice.",
1042.               u.usteed ? upstart(x_monnam(u.usteed,
1043.                                           (has_mname(u.usteed)) ? ARTICLE_NONE
1044.                                                                 : ARTICLE_THE,
1045.                                           (char *) 0, SUPPRESS_SADDLE, FALSE))
1046.                        : "You",
1047.               rn2(2) ? "slip" : "slide", on_foot ? "" : "s");
1048.     } else {
1049.         if (on_foot) {
1050.             switch (rn2(4)) {
1051.             case 1:
1052.                 You("trip over your own %s.",
1053.                     Hallucination ? "elbow" : makeplural(body_part(FOOT)));
1054.                 break;
1055.             case 2:
1056.                 You("slip %s.",
1057.                     Hallucination ? "on a banana peel" : "and nearly fall");
1058.                 break;
1059.             case 3:
1060.                 You("flounder.");
1061.                 break;
1062.             default:
1063.                 You("stumble.");
1064.                 break;
1065.             }
1066.         } else {
1067.             switch (rn2(4)) {
1068.             case 1:
1069.                 Your("%s slip out of the stirrups.",
1070.                      makeplural(body_part(FOOT)));
1071.                 break;
1072.             case 2:
1073.                 You("let go of the reins.");
1074.                 break;
1075.             case 3:
1076.                 You("bang into the saddle-horn.");
1077.                 break;
1078.             default:
1079.                 You("slide to one side of the saddle.");
1080.                 break;
1081.             }
1082.             dismount_steed(DISMOUNT_FELL);
1083.         }
1084.     }
1085. }
1086. 
1087. /* Print a lamp flicker message with tailer. */
1088. STATIC_OVL void
1089. see_lamp_flicker(obj, tailer)
1090. struct obj *obj;
1091. const char *tailer;
1092. {
1093.     switch (obj->where) {
1094.     case OBJ_INVENT:
1095.     case OBJ_MINVENT:
1096.         pline("%s flickers%s.", Yname2(obj), tailer);
1097.         break;
1098.     case OBJ_FLOOR:
1099.         You_see("%s flicker%s.", an(xname(obj)), tailer);
1100.         break;
1101.     }
1102. }
1103. 
1104. /* Print a dimming message for brass lanterns. */
1105. STATIC_OVL void
1106. lantern_message(obj)
1107. struct obj *obj;
1108. {
1109.     /* from adventure */
1110.     switch (obj->where) {
1111.     case OBJ_INVENT:
1112.         Your("lantern is getting dim.");
1113.         if (Hallucination)
1114.             pline("Batteries have not been invented yet.");
1115.         break;
1116.     case OBJ_FLOOR:
1117.         You_see("a lantern getting dim.");
1118.         break;
1119.     case OBJ_MINVENT:
1120.         pline("%s lantern is getting dim.", s_suffix(Monnam(obj->ocarry)));
1121.         break;
1122.     }
1123. }
1124. 
1125. /*
1126.  * Timeout callback for for objects that are burning. E.g. lamps, candles.
1127.  * See begin_burn() for meanings of obj->age and obj->spe.
1128.  */
1129. void
1130. burn_object(arg, timeout)
1131. anything *arg;
1132. long timeout;
1133. {
1134.     struct obj *obj = arg->a_obj;
1135.     boolean canseeit, many, menorah, need_newsym, need_invupdate;
1136.     xchar x, y;
1137.     char whose[BUFSZ];
1138. 
1139.     menorah = obj->otyp == CANDELABRUM_OF_INVOCATION;
1140.     many = menorah ? obj->spe > 1 : obj->quan > 1L;
1141. 
1142.     /* timeout while away */
1143.     if (timeout != monstermoves) {
1144.         long how_long = monstermoves - timeout;
1145. 
1146.         if (how_long >= obj->age) {
1147.             obj->age = 0;
1148.             end_burn(obj, FALSE);
1149. 
1150.             if (menorah) {
1151.                 obj->spe = 0; /* no more candles */
1152.                 obj->owt = weight(obj);
1153.             } else if (Is_candle(obj) || obj->otyp == POT_OIL) {
1154.                 /* get rid of candles and burning oil potions;
1155.                    we know this object isn't carried by hero,
1156.                    nor is it migrating */
1157.                 obj_extract_self(obj);
1158.                 obfree(obj, (struct obj *) 0);
1159.                 obj = (struct obj *) 0;
1160.             }
1161. 
1162.         } else {
1163.             obj->age -= how_long;
1164.             begin_burn(obj, TRUE);
1165.         }
1166.         return;
1167.     }
1168. 
1169.     /* only interested in INVENT, FLOOR, and MINVENT */
1170.     if (get_obj_location(obj, &x, &y, 0)) {
1171.         canseeit = !Blind && cansee(x, y);
1172.         /* set `whose[]' to be "Your " or "Fred's " or "The goblin's " */
1173.         (void) Shk_Your(whose, obj);
1174.     } else {
1175.         canseeit = FALSE;
1176.     }
1177.     need_newsym = need_invupdate = FALSE;
1178. 
1179.     /* obj->age is the age remaining at this point.  */
1180.     switch (obj->otyp) {
1181.     case POT_OIL:
1182.         /* this should only be called when we run out */
1183.         if (canseeit) {
1184.             switch (obj->where) {
1185.             case OBJ_INVENT:
1186.                 need_invupdate = TRUE;
1187.                 /*FALLTHRU*/
1188.             case OBJ_MINVENT:
1189.                 pline("%spotion of oil has burnt away.", whose);
1190.                 break;
1191.             case OBJ_FLOOR:
1192.                 You_see("a burning potion of oil go out.");
1193.                 need_newsym = TRUE;
1194.                 break;
1195.             }
1196.         }
1197.         end_burn(obj, FALSE); /* turn off light source */
1198.         if (carried(obj)) {
1199.             useupall(obj);
1200.         } else {
1201.             /* clear migrating obj's destination code before obfree
1202.                to avoid false complaint of deleting worn item */
1203.             if (obj->where == OBJ_MIGRATING)
1204.                 obj->owornmask = 0L;
1205.             obj_extract_self(obj);
1206.             obfree(obj, (struct obj *) 0);
1207.         }
1208.         obj = (struct obj *) 0;
1209.         break;
1210. 
1211.     case BRASS_LANTERN:
1212.     case OIL_LAMP:
1213.         switch ((int) obj->age) {
1214.         case 150:
1215.         case 100:
1216.         case 50:
1217.             if (canseeit) {
1218.                 if (obj->otyp == BRASS_LANTERN)
1219.                     lantern_message(obj);
1220.                 else
1221.                     see_lamp_flicker(obj,
1222.                                      obj->age == 50L ? " considerably" : "");
1223.             }
1224.             break;
1225. 
1226.         case 25:
1227.             if (canseeit) {
1228.                 if (obj->otyp == BRASS_LANTERN)
1229.                     lantern_message(obj);
1230.                 else {
1231.                     switch (obj->where) {
1232.                     case OBJ_INVENT:
1233.                     case OBJ_MINVENT:
1234.                         pline("%s seems about to go out.", Yname2(obj));
1235.                         break;
1236.                     case OBJ_FLOOR:
1237.                         You_see("%s about to go out.", an(xname(obj)));
1238.                         break;
1239.                     }
1240.                 }
1241.             }
1242.             break;
1243. 
1244.         case 0:
1245.             /* even if blind you'll know if holding it */
1246.             if (canseeit || obj->where == OBJ_INVENT) {
1247.                 switch (obj->where) {
1248.                 case OBJ_INVENT:
1249.                     need_invupdate = TRUE;
1250.                     /*FALLTHRU*/
1251.                 case OBJ_MINVENT:
1252.                     if (obj->otyp == BRASS_LANTERN)
1253.                         pline("%slantern has run out of power.", whose);
1254.                     else
1255.                         pline("%s has gone out.", Yname2(obj));
1256.                     break;
1257.                 case OBJ_FLOOR:
1258.                     if (obj->otyp == BRASS_LANTERN)
1259.                         You_see("a lantern run out of power.");
1260.                     else
1261.                         You_see("%s go out.", an(xname(obj)));
1262.                     break;
1263.                 }
1264.             }
1265.             end_burn(obj, FALSE);
1266.             break;
1267. 
1268.         default:
1269.             /*
1270.              * Someone added fuel to the lamp while it was
1271.              * lit.  Just fall through and let begin burn
1272.              * handle the new age.
1273.              */
1274.             break;
1275.         }
1276. 
1277.         if (obj->age)
1278.             begin_burn(obj, TRUE);
1279. 
1280.         break;
1281. 
1282.     case CANDELABRUM_OF_INVOCATION:
1283.     case TALLOW_CANDLE:
1284.     case WAX_CANDLE:
1285.         switch (obj->age) {
1286.         case 75:
1287.             if (canseeit)
1288.                 switch (obj->where) {
1289.                 case OBJ_INVENT:
1290.                 case OBJ_MINVENT:
1291.                     pline("%s%scandle%s getting short.", whose,
1292.                           menorah ? "candelabrum's " : "",
1293.                           many ? "s are" : " is");
1294.                     break;
1295.                 case OBJ_FLOOR:
1296.                     You_see("%scandle%s getting short.",
1297.                             menorah ? "a candelabrum's " : many ? "some "
1298.                                                                 : "a ",
1299.                             many ? "s" : "");
1300.                     break;
1301.                 }
1302.             break;
1303. 
1304.         case 15:
1305.             if (canseeit)
1306.                 switch (obj->where) {
1307.                 case OBJ_INVENT:
1308.                 case OBJ_MINVENT:
1309.                     pline("%s%scandle%s flame%s flicker%s low!", whose,
1310.                           menorah ? "candelabrum's " : "", many ? "s'" : "'s",
1311.                           many ? "s" : "", many ? "" : "s");
1312.                     break;
1313.                 case OBJ_FLOOR:
1314.                     You_see("%scandle%s flame%s flicker low!",
1315.                             menorah ? "a candelabrum's " : many ? "some "
1316.                                                                 : "a ",
1317.                             many ? "s'" : "'s", many ? "s" : "");
1318.                     break;
1319.                 }
1320.             break;
1321. 
1322.         case 0:
1323.             /* we know even if blind and in our inventory */
1324.             if (canseeit || obj->where == OBJ_INVENT) {
1325.                 if (menorah) {
1326.                     switch (obj->where) {
1327.                     case OBJ_INVENT:
1328.                         need_invupdate = TRUE;
1329.                         /*FALLTHRU*/
1330.                     case OBJ_MINVENT:
1331.                         pline("%scandelabrum's flame%s.", whose,
1332.                               many ? "s die" : " dies");
1333.                         break;
1334.                     case OBJ_FLOOR:
1335.                         You_see("a candelabrum's flame%s die.",
1336.                                 many ? "s" : "");
1337.                         break;
1338.                     }
1339.                 } else {
1340.                     switch (obj->where) {
1341.                     case OBJ_INVENT:
1342.                         /* no need_invupdate for update_inventory() necessary;
1343.                            useupall() -> freeinv() handles it */
1344.                         /*FALLTHRU*/
1345.                     case OBJ_MINVENT:
1346.                         pline("%s %s consumed!", Yname2(obj),
1347.                               many ? "are" : "is");
1348.                         break;
1349.                     case OBJ_FLOOR:
1350.                         /*
1351.                           You see some wax candles consumed!
1352.                           You see a wax candle consumed!
1353.                          */
1354.                         You_see("%s%s consumed!", many ? "some " : "",
1355.                                 many ? xname(obj) : an(xname(obj)));
1356.                         need_newsym = TRUE;
1357.                         break;
1358.                     }
1359. 
1360.                     /* post message */
1361.                     pline(Hallucination
1362.                               ? (many ? "They shriek!" : "It shrieks!")
1363.                               : Blind ? "" : (many ? "Their flames die."
1364.                                                    : "Its flame dies."));
1365.                 }
1366.             }
1367.             end_burn(obj, FALSE);
1368. 
1369.             if (menorah) {
1370.                 obj->spe = 0;
1371.                 obj->owt = weight(obj);
1372.             } else {
1373.                 if (carried(obj)) {
1374.                     useupall(obj);
1375.                 } else {
1376.                     /* clear migrating obj's destination code
1377.                        so obfree won't think this item is worn */
1378.                     if (obj->where == OBJ_MIGRATING)
1379.                         obj->owornmask = 0L;
1380.                     obj_extract_self(obj);
1381.                     obfree(obj, (struct obj *) 0);
1382.                 }
1383.                 obj = (struct obj *) 0;
1384.             }
1385.             break; /* case [age ==] 0 */
1386. 
1387.         default:
1388.             /*
1389.              * Someone added fuel (candles) to the menorah while
1390.              * it was lit.  Just fall through and let begin burn
1391.              * handle the new age.
1392.              */
1393.             break;
1394.         }
1395. 
1396.         if (obj && obj->age)
1397.             begin_burn(obj, TRUE);
1398.         break; /* case [otyp ==] candelabrum|tallow_candle|wax_candle */
1399. 
1400.     default:
1401.         impossible("burn_object: unexpected obj %s", xname(obj));
1402.         break;
1403.     }
1404.     if (need_newsym)
1405.         newsym(x, y);
1406.     if (need_invupdate)
1407.         update_inventory();
1408. }
1409. 
1410. /*
1411.  * Start a burn timeout on the given object. If not "already lit" then
1412.  * create a light source for the vision system.  There had better not
1413.  * be a burn already running on the object.
1414.  *
1415.  * Magic lamps stay lit as long as there's a genie inside, so don't start
1416.  * a timer.
1417.  *
1418.  * Burn rules:
1419.  *      potions of oil, lamps & candles:
1420.  *              age = # of turns of fuel left
1421.  *              spe = <unused>
1422.  *      magic lamps:
1423.  *              age = <unused>
1424.  *              spe = 0 not lightable, 1 lightable forever
1425.  *      candelabrum:
1426.  *              age = # of turns of fuel left
1427.  *              spe = # of candles
1428.  *
1429.  * Once the burn begins, the age will be set to the amount of fuel
1430.  * remaining _once_the_burn_finishes_.  If the burn is terminated
1431.  * early then fuel is added back.
1432.  *
1433.  * This use of age differs from the use of age for corpses and eggs.
1434.  * For the latter items, age is when the object was created, so we
1435.  * know when it becomes "bad".
1436.  *
1437.  * This is a "silent" routine - it should not print anything out.
1438.  */
1439. void
1440. begin_burn(obj, already_lit)
1441. struct obj *obj;
1442. boolean already_lit;
1443. {
1444.     int radius = 3;
1445.     long turns = 0;
1446.     boolean do_timer = TRUE;
1447. 
1448.     if (obj->age == 0 && obj->otyp != MAGIC_LAMP && !artifact_light(obj))
1449.         return;
1450. 
1451.     switch (obj->otyp) {
1452.     case MAGIC_LAMP:
1453.         obj->lamplit = 1;
1454.         do_timer = FALSE;
1455.         break;
1456. 
1457.     case POT_OIL:
1458.         turns = obj->age;
1459.         if (obj->odiluted)
1460.             turns = (3L * turns + 2L) / 4L;
1461.         radius = 1; /* very dim light */
1462.         break;
1463. 
1464.     case BRASS_LANTERN:
1465.     case OIL_LAMP:
1466.         /* magic times are 150, 100, 50, 25, and 0 */
1467.         if (obj->age > 150L)
1468.             turns = obj->age - 150L;
1469.         else if (obj->age > 100L)
1470.             turns = obj->age - 100L;
1471.         else if (obj->age > 50L)
1472.             turns = obj->age - 50L;
1473.         else if (obj->age > 25L)
1474.             turns = obj->age - 25L;
1475.         else
1476.             turns = obj->age;
1477.         break;
1478. 
1479.     case CANDELABRUM_OF_INVOCATION:
1480.     case TALLOW_CANDLE:
1481.     case WAX_CANDLE:
1482.         /* magic times are 75, 15, and 0 */
1483.         if (obj->age > 75L)
1484.             turns = obj->age - 75L;
1485.         else if (obj->age > 15L)
1486.             turns = obj->age - 15L;
1487.         else
1488.             turns = obj->age;
1489.         radius = candle_light_range(obj);
1490.         break;
1491. 
1492.     default:
1493.         /* [ALI] Support artifact light sources */
1494.         if (artifact_light(obj)) {
1495.             obj->lamplit = 1;
1496.             do_timer = FALSE;
1497.             radius = arti_light_radius(obj);
1498.         } else {
1499.             impossible("begin burn: unexpected %s", xname(obj));
1500.             turns = obj->age;
1501.         }
1502.         break;
1503.     }
1504. 
1505.     if (do_timer) {
1506.         if (start_timer(turns, TIMER_OBJECT, BURN_OBJECT, obj_to_any(obj))) {
1507.             obj->lamplit = 1;
1508.             obj->age -= turns;
1509.             if (carried(obj) && !already_lit)
1510.                 update_inventory();
1511.         } else {
1512.             obj->lamplit = 0;
1513.         }
1514.     } else {
1515.         if (carried(obj) && !already_lit)
1516.             update_inventory();
1517.     }
1518. 
1519.     if (obj->lamplit && !already_lit) {
1520.         xchar x, y;
1521. 
1522.         if (get_obj_location(obj, &x, &y, CONTAINED_TOO | BURIED_TOO))
1523.             new_light_source(x, y, radius, LS_OBJECT, obj_to_any(obj));
1524.         else
1525.             impossible("begin_burn: can't get obj position");
1526.     }
1527. }
1528. 
1529. /*
1530.  * Stop a burn timeout on the given object if timer attached.  Darken
1531.  * light source.
1532.  */
1533. void
1534. end_burn(obj, timer_attached)
1535. struct obj *obj;
1536. boolean timer_attached;
1537. {
1538.     if (!obj->lamplit) {
1539.         impossible("end_burn: obj %s not lit", xname(obj));
1540.         return;
1541.     }
1542. 
1543.     if (obj->otyp == MAGIC_LAMP || artifact_light(obj))
1544.         timer_attached = FALSE;
1545. 
1546.     if (!timer_attached) {
1547.         /* [DS] Cleanup explicitly, since timer cleanup won't happen */
1548.         del_light_source(LS_OBJECT, obj_to_any(obj));
1549.         obj->lamplit = 0;
1550.         if (obj->where == OBJ_INVENT)
1551.             update_inventory();
1552.     } else if (!stop_timer(BURN_OBJECT, obj_to_any(obj)))
1553.         impossible("end_burn: obj %s not timed!", xname(obj));
1554. }
1555. 
1556. /*
1557.  * Cleanup a burning object if timer stopped.
1558.  */
1559. static void
1560. cleanup_burn(arg, expire_time)
1561. anything *arg;
1562. long expire_time;
1563. {
1564.     struct obj *obj = arg->a_obj;
1565.     if (!obj->lamplit) {
1566.         impossible("cleanup_burn: obj %s not lit", xname(obj));
1567.         return;
1568.     }
1569. 
1570.     del_light_source(LS_OBJECT, obj_to_any(obj));
1571. 
1572.     /* restore unused time */
1573.     obj->age += expire_time - monstermoves;
1574. 
1575.     obj->lamplit = 0;
1576. 
1577.     if (obj->where == OBJ_INVENT)
1578.         update_inventory();
1579. }
1580. 
1581. void
1582. do_storms()
1583. {
1584.     int nstrike;
1585.     register int x, y;
1586.     int dirx, diry;
1587.     int count;
1588. 
1589.     /* no lightning if not the air level or too often, even then */
1590.     if (!Is_airlevel(&u.uz) || rn2(8))
1591.         return;
1592. 
1593.     /* the number of strikes is 8-log2(nstrike) */
1594.     for (nstrike = rnd(64); nstrike <= 64; nstrike *= 2) {
1595.         count = 0;
1596.         do {
1597.             x = rnd(COLNO - 1);
1598.             y = rn2(ROWNO);
1599.         } while (++count < 100 && levl[x][y].typ != CLOUD);
1600. 
1601.         if (count < 100) {
1602.             dirx = rn2(3) - 1;
1603.             diry = rn2(3) - 1;
1604.             if (dirx != 0 || diry != 0)
1605.                 buzz(-15, /* "monster" LIGHTNING spell */
1606.                      8, x, y, dirx, diry);
1607.         }
1608.     }
1609. 
1610.     if (levl[u.ux][u.uy].typ == CLOUD) {
1611.         /* Inside a cloud during a thunder storm is deafening. */
1612.         /* Even if already deaf, we sense the thunder's vibrations. */
1613.         pline("Kaboom!!!  Boom!!  Boom!!");
1614.         incr_itimeout(&HDeaf, rn1(20, 30));
1615.         context.botl = TRUE;
1616.         if (!u.uinvulnerable) {
1617.             stop_occupation();
1618.             nomul(-3);
1619.             multi_reason = "hiding from thunderstorm";
1620.             nomovemsg = 0;
1621.         }
1622.     } else
1623.         You_hear("a rumbling noise.");
1624. }
1625. 
1626. /* -------------------------------------------------------------------------
1627.  */
1628. /*
1629.  * Generic Timeout Functions.
1630.  *
1631.  * Interface:
1632.  *
1633.  * General:
1634.  *  boolean start_timer(long timeout,short kind,short func_index,
1635.  *                      anything *arg)
1636.  *      Start a timer of kind 'kind' that will expire at time
1637.  *      monstermoves+'timeout'.  Call the function at 'func_index'
1638.  *      in the timeout table using argument 'arg'.  Return TRUE if
1639.  *      a timer was started.  This places the timer on a list ordered
1640.  *      "sooner" to "later".  If an object, increment the object's
1641.  *      timer count.
1642.  *
1643.  *  long stop_timer(short func_index, anything *arg)
1644.  *      Stop a timer specified by the (func_index, arg) pair.  This
1645.  *      assumes that such a pair is unique.  Return the time the
1646.  *      timer would have gone off.  If no timer is found, return 0.
1647.  *      If an object, decrement the object's timer count.
1648.  *
1649.  *  long peek_timer(short func_index, anything *arg)
1650.  *      Return time specified timer will go off (0 if no such timer).
1651.  *
1652.  *  void run_timers(void)
1653.  *      Call timers that have timed out.
1654.  *
1655.  * Save/Restore:
1656.  *  void save_timers(int fd, int mode, int range)
1657.  *      Save all timers of range 'range'.  Range is either global
1658.  *      or local.  Global timers follow game play, local timers
1659.  *      are saved with a level.  Object and monster timers are
1660.  *      saved using their respective id's instead of pointers.
1661.  *
1662.  *  void restore_timers(int fd, int range, boolean ghostly, long adjust)
1663.  *      Restore timers of range 'range'.  If from a ghost pile,
1664.  *      adjust the timeout by 'adjust'.  The object and monster
1665.  *      ids are not restored until later.
1666.  *
1667.  *  void relink_timers(boolean ghostly)
1668.  *      Relink all object and monster timers that had been saved
1669.  *      using their object's or monster's id number.
1670.  *
1671.  * Object Specific:
1672.  *  void obj_move_timers(struct obj *src, struct obj *dest)
1673.  *      Reassign all timers from src to dest.
1674.  *
1675.  *  void obj_split_timers(struct obj *src, struct obj *dest)
1676.  *      Duplicate all timers assigned to src and attach them to dest.
1677.  *
1678.  *  void obj_stop_timers(struct obj *obj)
1679.  *      Stop all timers attached to obj.
1680.  *
1681.  *  boolean obj_has_timer(struct obj *object, short timer_type)
1682.  *      Check whether object has a timer of type timer_type.
1683.  */
1684. 
1685. STATIC_DCL const char *FDECL(kind_name, (SHORT_P));
1686. STATIC_DCL void FDECL(print_queue, (winid, timer_element *));
1687. STATIC_DCL void FDECL(insert_timer, (timer_element *));
1688. STATIC_DCL timer_element *FDECL(remove_timer,
1689.                                 (timer_element **, SHORT_P, ANY_P *));
1690. STATIC_DCL void FDECL(write_timer, (int, timer_element *));
1691. STATIC_DCL boolean FDECL(mon_is_local, (struct monst *));
1692. STATIC_DCL boolean FDECL(timer_is_local, (timer_element *));
1693. STATIC_DCL int FDECL(maybe_write_timer, (int, int, BOOLEAN_P));
1694. 
1695. /* ordered timer list */
1696. static timer_element *timer_base; /* "active" */
1697. static unsigned long timer_id = 1;
1698. 
1699. /* If defined, then include names when printing out the timer queue */
1700. #define VERBOSE_TIMER
1701. 
1702. typedef struct {
1703.     timeout_proc f, cleanup;
1704. #ifdef VERBOSE_TIMER
1705.     const char *name;
1706. #define TTAB(a, b, c) \
1707.     {                 \
1708.         a, b, c       \
1709.     }
1710. #else
1711. #define TTAB(a, b, c) \
1712.     {                 \
1713.         a, b          \
1714.     }
1715. #endif
1716. } ttable;
1717. 
1718. /* table of timeout functions */
1719. static const ttable timeout_funcs[NUM_TIME_FUNCS] = {
1720.     TTAB(rot_organic, (timeout_proc) 0, "rot_organic"),
1721.     TTAB(rot_corpse, (timeout_proc) 0, "rot_corpse"),
1722.     TTAB(revive_mon, (timeout_proc) 0, "revive_mon"),
1723.     TTAB(burn_object, cleanup_burn, "burn_object"),
1724.     TTAB(hatch_egg, (timeout_proc) 0, "hatch_egg"),
1725.     TTAB(fig_transform, (timeout_proc) 0, "fig_transform"),
1726.     TTAB(melt_ice_away, (timeout_proc) 0, "melt_ice_away")
1727. };
1728. #undef TTAB
1729. 
1730. STATIC_OVL const char *
1731. kind_name(kind)
1732. short kind;
1733. {
1734.     switch (kind) {
1735.     case TIMER_LEVEL:
1736.         return "level";
1737.     case TIMER_GLOBAL:
1738.         return "global";
1739.     case TIMER_OBJECT:
1740.         return "object";
1741.     case TIMER_MONSTER:
1742.         return "monster";
1743.     }
1744.     return "unknown";
1745. }
1746. 
1747. STATIC_OVL void
1748. print_queue(win, base)
1749. winid win;
1750. timer_element *base;
1751. {
1752.     timer_element *curr;
1753.     char buf[BUFSZ];
1754. 
1755.     if (!base) {
1756.         putstr(win, 0, " <empty>");
1757.     } else {
1758.         putstr(win, 0, "timeout  id   kind   call");
1759.         for (curr = base; curr; curr = curr->next) {
1760. #ifdef VERBOSE_TIMER
1761.             Sprintf(buf, " %4ld   %4ld  %-6s %s(%s)", curr->timeout,
1762.                     curr->tid, kind_name(curr->kind),
1763.                     timeout_funcs[curr->func_index].name,
1764.                     fmt_ptr((genericptr_t) curr->arg.a_void));
1765. #else
1766.             Sprintf(buf, " %4ld   %4ld  %-6s #%d(%s)", curr->timeout,
1767.                     curr->tid, kind_name(curr->kind), curr->func_index,
1768.                     fmt_ptr((genericptr_t) curr->arg.a_void));
1769. #endif
1770.             putstr(win, 0, buf);
1771.         }
1772.     }
1773. }
1774. 
1775. int
1776. wiz_timeout_queue()
1777. {
1778.     winid win;
1779.     char buf[BUFSZ];
1780.     const char *propname;
1781.     long intrinsic;
1782.     int i, p, count, longestlen, ln, specindx = 0;
1783. 
1784.     win = create_nhwindow(NHW_MENU); /* corner text window */
1785.     if (win == WIN_ERR)
1786.         return 0;
1787. 
1788.     Sprintf(buf, "Current time = %ld.", monstermoves);
1789.     putstr(win, 0, buf);
1790.     putstr(win, 0, "");
1791.     putstr(win, 0, "Active timeout queue:");
1792.     putstr(win, 0, "");
1793.     print_queue(win, timer_base);
1794. 
1795.     /* Timed properies:
1796.      * check every one; the majority can't obtain temporary timeouts in
1797.      * normal play but those can be forced via the #wizintrinsic command.
1798.      */
1799.     count = longestlen = 0;
1800.     for (i = 0; (propname = propertynames[i].prop_name) != 0; ++i) {
1801.         p = propertynames[i].prop_num;
1802.         intrinsic = u.uprops[p].intrinsic;
1803.         if (intrinsic & TIMEOUT) {
1804.             ++count;
1805.             if ((ln = (int) strlen(propname)) > longestlen)
1806.                 longestlen = ln;
1807.         }
1808.         if (specindx == 0 && p == FIRE_RES)
1809.             specindx = i;
1810.     }
1811.     putstr(win, 0, "");
1812.     if (!count) {
1813.         putstr(win, 0, "No timed properties.");
1814.     } else {
1815.         putstr(win, 0, "Timed properties:");
1816.         putstr(win, 0, "");
1817.         for (i = 0; (propname = propertynames[i].prop_name) != 0; ++i) {
1818.             p = propertynames[i].prop_num;
1819.             intrinsic = u.uprops[p].intrinsic;
1820.             if (intrinsic & TIMEOUT) {
1821.                 if (specindx > 0 && i >= specindx) {
1822.                     putstr(win, 0, " -- settable via #wizinstrinc only --");
1823.                     specindx = 0;
1824.                 }
1825.                 /* timeout value can be up to 16777215 (0x00ffffff) but
1826.                    width of 4 digits should result in values lining up
1827.                    almost all the time (if/when they don't, it won't
1828.                    look nice but the information will still be accurate) */
1829.                 Sprintf(buf, " %*s %4ld", -longestlen, propname,
1830.                         (intrinsic & TIMEOUT));
1831.                 putstr(win, 0, buf);
1832.             }
1833.         }
1834.     }
1835.     display_nhwindow(win, FALSE);
1836.     destroy_nhwindow(win);
1837. 
1838.     return 0;
1839. }
1840. 
1841. void
1842. timer_sanity_check()
1843. {
1844.     timer_element *curr;
1845. 
1846.     /* this should be much more complete */
1847.     for (curr = timer_base; curr; curr = curr->next)
1848.         if (curr->kind == TIMER_OBJECT) {
1849.             struct obj *obj = curr->arg.a_obj;
1850. 
1851.             if (obj->timed == 0) {
1852.                 impossible("timer sanity: untimed obj %s, timer %ld",
1853.                       fmt_ptr((genericptr_t) obj), curr->tid);
1854.             }
1855.         }
1856. }
1857. 
1858. /*
1859.  * Pick off timeout elements from the global queue and call their functions.
1860.  * Do this until their time is less than or equal to the move count.
1861.  */
1862. void
1863. run_timers()
1864. {
1865.     timer_element *curr;
1866. 
1867.     /*
1868.      * Always use the first element.  Elements may be added or deleted at
1869.      * any time.  The list is ordered, we are done when the first element
1870.      * is in the future.
1871.      */
1872.     while (timer_base && timer_base->timeout <= monstermoves) {
1873.         curr = timer_base;
1874.         timer_base = curr->next;
1875. 
1876.         if (curr->kind == TIMER_OBJECT)
1877.             (curr->arg.a_obj)->timed--;
1878.         (*timeout_funcs[curr->func_index].f)(&curr->arg, curr->timeout);
1879.         free((genericptr_t) curr);
1880.     }
1881. }
1882. 
1883. /*
1884.  * Start a timer.  Return TRUE if successful.
1885.  */
1886. boolean
1887. start_timer(when, kind, func_index, arg)
1888. long when;
1889. short kind;
1890. short func_index;
1891. anything *arg;
1892. {
1893.     timer_element *gnu, *dup;
1894. 
1895.     if (kind < 0 || kind >= NUM_TIMER_KINDS
1896.         || func_index < 0 || func_index >= NUM_TIME_FUNCS)
1897.         panic("start_timer (%s: %d)", kind_name(kind), (int) func_index);
1898. 
1899.     /* fail if <arg> already has a <func_index> timer running */
1900.     for (dup = timer_base; dup; dup = dup->next)
1901.         if (dup->kind == kind
1902.             && dup->func_index == func_index
1903.             && dup->arg.a_void == arg->a_void)
1904.             break;
1905.     if (dup) {
1906.         char idbuf[QBUFSZ];
1907. 
1908. #ifdef VERBOSE_TIMER
1909.         Sprintf(idbuf, "%s timer", timeout_funcs[func_index].name);
1910. #else
1911.         Sprintf(idbuf, "%s timer (%d)", kind_name(kind), (int) func_index);
1912. #endif
1913.         impossible("Attempted to start duplicate %s, aborted.", idbuf);
1914.         return FALSE;
1915.     }
1916. 
1917.     gnu = (timer_element *) alloc(sizeof *gnu);
1918.     (void) memset((genericptr_t) gnu, 0, sizeof *gnu);
1919.     gnu->next = 0;
1920.     gnu->tid = timer_id++;
1921.     gnu->timeout = monstermoves + when;
1922.     gnu->kind = kind;
1923.     gnu->needs_fixup = 0;
1924.     gnu->func_index = func_index;
1925.     gnu->arg = *arg;
1926.     insert_timer(gnu);
1927. 
1928.     if (kind == TIMER_OBJECT) /* increment object's timed count */
1929.         (arg->a_obj)->timed++;
1930. 
1931.     return TRUE;
1932. }
1933. 
1934. /*
1935.  * Remove the timer from the current list and free it up.  Return the time
1936.  * remaining until it would have gone off, 0 if not found.
1937.  */
1938. long
1939. stop_timer(func_index, arg)
1940. short func_index;
1941. anything *arg;
1942. {
1943.     timer_element *doomed;
1944.     long timeout;
1945. 
1946.     doomed = remove_timer(&timer_base, func_index, arg);
1947. 
1948.     if (doomed) {
1949.         timeout = doomed->timeout;
1950.         if (doomed->kind == TIMER_OBJECT)
1951.             (arg->a_obj)->timed--;
1952.         if (timeout_funcs[doomed->func_index].cleanup)
1953.             (*timeout_funcs[doomed->func_index].cleanup)(arg, timeout);
1954.         free((genericptr_t) doomed);
1955.         return (timeout - monstermoves);
1956.     }
1957.     return 0L;
1958. }
1959. 
1960. /*
1961.  * Find the timeout of specified timer; return 0 if none.
1962.  */
1963. long
1964. peek_timer(type, arg)
1965. short type;
1966. anything *arg;
1967. {
1968.     timer_element *curr;
1969. 
1970.     for (curr = timer_base; curr; curr = curr->next) {
1971.         if (curr->func_index == type && curr->arg.a_void == arg->a_void)
1972.             return curr->timeout;
1973.     }
1974.     return 0L;
1975. }
1976. 
1977. /*
1978.  * Move all object timers from src to dest, leaving src untimed.
1979.  */
1980. void
1981. obj_move_timers(src, dest)
1982. struct obj *src, *dest;
1983. {
1984.     int count;
1985.     timer_element *curr;
1986. 
1987.     for (count = 0, curr = timer_base; curr; curr = curr->next)
1988.         if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == src) {
1989.             curr->arg.a_obj = dest;
1990.             dest->timed++;
1991.             count++;
1992.         }
1993.     if (count != src->timed)
1994.         panic("obj_move_timers");
1995.     src->timed = 0;
1996. }
1997. 
1998. /*
1999.  * Find all object timers and duplicate them for the new object "dest".
2000.  */
2001. void
2002. obj_split_timers(src, dest)
2003. struct obj *src, *dest;
2004. {
2005.     timer_element *curr, *next_timer = 0;
2006. 
2007.     for (curr = timer_base; curr; curr = next_timer) {
2008.         next_timer = curr->next; /* things may be inserted */
2009.         if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == src) {
2010.             (void) start_timer(curr->timeout - monstermoves, TIMER_OBJECT,
2011.                                curr->func_index, obj_to_any(dest));
2012.         }
2013.     }
2014. }
2015. 
2016. /*
2017.  * Stop all timers attached to this object.  We can get away with this because
2018.  * all object pointers are unique.
2019.  */
2020. void
2021. obj_stop_timers(obj)
2022. struct obj *obj;
2023. {
2024.     timer_element *curr, *prev, *next_timer = 0;
2025. 
2026.     for (prev = 0, curr = timer_base; curr; curr = next_timer) {
2027.         next_timer = curr->next;
2028.         if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == obj) {
2029.             if (prev)
2030.                 prev->next = curr->next;
2031.             else
2032.                 timer_base = curr->next;
2033.             if (timeout_funcs[curr->func_index].cleanup)
2034.                 (*timeout_funcs[curr->func_index].cleanup)(&curr->arg,
2035.                                                            curr->timeout);
2036.             free((genericptr_t) curr);
2037.         } else {
2038.             prev = curr;
2039.         }
2040.     }
2041.     obj->timed = 0;
2042. }
2043. 
2044. /*
2045.  * Check whether object has a timer of type timer_type.
2046.  */
2047. boolean
2048. obj_has_timer(object, timer_type)
2049. struct obj *object;
2050. short timer_type;
2051. {
2052.     long timeout = peek_timer(timer_type, obj_to_any(object));
2053. 
2054.     return (boolean) (timeout != 0L);
2055. }
2056. 
2057. /*
2058.  * Stop all timers of index func_index at this spot.
2059.  *
2060.  */
2061. void
2062. spot_stop_timers(x, y, func_index)
2063. xchar x, y;
2064. short func_index;
2065. {
2066.     timer_element *curr, *prev, *next_timer = 0;
2067.     long where = (((long) x << 16) | ((long) y));
2068. 
2069.     for (prev = 0, curr = timer_base; curr; curr = next_timer) {
2070.         next_timer = curr->next;
2071.         if (curr->kind == TIMER_LEVEL && curr->func_index == func_index
2072.             && curr->arg.a_long == where) {
2073.             if (prev)
2074.                 prev->next = curr->next;
2075.             else
2076.                 timer_base = curr->next;
2077.             if (timeout_funcs[curr->func_index].cleanup)
2078.                 (*timeout_funcs[curr->func_index].cleanup)(&curr->arg,
2079.                                                            curr->timeout);
2080.             free((genericptr_t) curr);
2081.         } else {
2082.             prev = curr;
2083.         }
2084.     }
2085. }
2086. 
2087. /*
2088.  * When is the spot timer of type func_index going to expire?
2089.  * Returns 0L if no such timer.
2090.  */
2091. long
2092. spot_time_expires(x, y, func_index)
2093. xchar x, y;
2094. short func_index;
2095. {
2096.     timer_element *curr;
2097.     long where = (((long) x << 16) | ((long) y));
2098. 
2099.     for (curr = timer_base; curr; curr = curr->next) {
2100.         if (curr->kind == TIMER_LEVEL && curr->func_index == func_index
2101.             && curr->arg.a_long == where)
2102.             return curr->timeout;
2103.     }
2104.     return 0L;
2105. }
2106. 
2107. long
2108. spot_time_left(x, y, func_index)
2109. xchar x, y;
2110. short func_index;
2111. {
2112.     long expires = spot_time_expires(x, y, func_index);
2113.     return (expires > 0L) ? expires - monstermoves : 0L;
2114. }
2115. 
2116. /* Insert timer into the global queue */
2117. STATIC_OVL void
2118. insert_timer(gnu)
2119. timer_element *gnu;
2120. {
2121.     timer_element *curr, *prev;
2122. 
2123.     for (prev = 0, curr = timer_base; curr; prev = curr, curr = curr->next)
2124.         if (curr->timeout >= gnu->timeout)
2125.             break;
2126. 
2127.     gnu->next = curr;
2128.     if (prev)
2129.         prev->next = gnu;
2130.     else
2131.         timer_base = gnu;
2132. }
2133. 
2134. STATIC_OVL timer_element *
2135. remove_timer(base, func_index, arg)
2136. timer_element **base;
2137. short func_index;
2138. anything *arg;
2139. {
2140.     timer_element *prev, *curr;
2141. 
2142.     for (prev = 0, curr = *base; curr; prev = curr, curr = curr->next)
2143.         if (curr->func_index == func_index && curr->arg.a_void == arg->a_void)
2144.             break;
2145. 
2146.     if (curr) {
2147.         if (prev)
2148.             prev->next = curr->next;
2149.         else
2150.             *base = curr->next;
2151.     }
2152. 
2153.     return curr;
2154. }
2155. 
2156. STATIC_OVL void
2157. write_timer(fd, timer)
2158. int fd;
2159. timer_element *timer;
2160. {
2161.     anything arg_save;
2162. 
2163.     arg_save = zeroany;
2164.     switch (timer->kind) {
2165.     case TIMER_GLOBAL:
2166.     case TIMER_LEVEL:
2167.         /* assume no pointers in arg */
2168.         bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
2169.         break;
2170. 
2171.     case TIMER_OBJECT:
2172.         if (timer->needs_fixup)
2173.             bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
2174.         else {
2175.             /* replace object pointer with id */
2176.             arg_save.a_obj = timer->arg.a_obj;
2177.             timer->arg = zeroany;
2178.             timer->arg.a_uint = (arg_save.a_obj)->o_id;
2179.             timer->needs_fixup = 1;
2180.             bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
2181.             timer->arg.a_obj = arg_save.a_obj;
2182.             timer->needs_fixup = 0;
2183.         }
2184.         break;
2185. 
2186.     case TIMER_MONSTER:
2187.         if (timer->needs_fixup)
2188.             bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
2189.         else {
2190.             /* replace monster pointer with id */
2191.             arg_save.a_monst = timer->arg.a_monst;
2192.             timer->arg = zeroany;
2193.             timer->arg.a_uint = (arg_save.a_monst)->m_id;
2194.             timer->needs_fixup = 1;
2195.             bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
2196.             timer->arg.a_monst = arg_save.a_monst;
2197.             timer->needs_fixup = 0;
2198.         }
2199.         break;
2200. 
2201.     default:
2202.         panic("write_timer");
2203.         break;
2204.     }
2205. }
2206. 
2207. /*
2208.  * Return TRUE if the object will stay on the level when the level is
2209.  * saved.
2210.  */
2211. boolean
2212. obj_is_local(obj)
2213. struct obj *obj;
2214. {
2215.     switch (obj->where) {
2216.     case OBJ_INVENT:
2217.     case OBJ_MIGRATING:
2218.         return FALSE;
2219.     case OBJ_FLOOR:
2220.     case OBJ_BURIED:
2221.         return TRUE;
2222.     case OBJ_CONTAINED:
2223.         return obj_is_local(obj->ocontainer);
2224.     case OBJ_MINVENT:
2225.         return mon_is_local(obj->ocarry);
2226.     }
2227.     panic("obj_is_local");
2228.     return FALSE;
2229. }
2230. 
2231. /*
2232.  * Return TRUE if the given monster will stay on the level when the
2233.  * level is saved.
2234.  */
2235. STATIC_OVL boolean
2236. mon_is_local(mon)
2237. struct monst *mon;
2238. {
2239.     struct monst *curr;
2240. 
2241.     for (curr = migrating_mons; curr; curr = curr->nmon)
2242.         if (curr == mon)
2243.             return FALSE;
2244.     /* `mydogs' is used during level changes, never saved and restored */
2245.     for (curr = mydogs; curr; curr = curr->nmon)
2246.         if (curr == mon)
2247.             return FALSE;
2248.     return TRUE;
2249. }
2250. 
2251. /*
2252.  * Return TRUE if the timer is attached to something that will stay on the
2253.  * level when the level is saved.
2254.  */
2255. STATIC_OVL boolean
2256. timer_is_local(timer)
2257. timer_element *timer;
2258. {
2259.     switch (timer->kind) {
2260.     case TIMER_LEVEL:
2261.         return TRUE;
2262.     case TIMER_GLOBAL:
2263.         return FALSE;
2264.     case TIMER_OBJECT:
2265.         return obj_is_local(timer->arg.a_obj);
2266.     case TIMER_MONSTER:
2267.         return mon_is_local(timer->arg.a_monst);
2268.     }
2269.     panic("timer_is_local");
2270.     return FALSE;
2271. }
2272. 
2273. /*
2274.  * Part of the save routine.  Count up the number of timers that would
2275.  * be written.  If write_it is true, actually write the timer.
2276.  */
2277. STATIC_OVL int
2278. maybe_write_timer(fd, range, write_it)
2279. int fd, range;
2280. boolean write_it;
2281. {
2282.     int count = 0;
2283.     timer_element *curr;
2284. 
2285.     for (curr = timer_base; curr; curr = curr->next) {
2286.         if (range == RANGE_GLOBAL) {
2287.             /* global timers */
2288. 
2289.             if (!timer_is_local(curr)) {
2290.                 count++;
2291.                 if (write_it)
2292.                     write_timer(fd, curr);
2293.             }
2294. 
2295.         } else {
2296.             /* local timers */
2297. 
2298.             if (timer_is_local(curr)) {
2299.                 count++;
2300.                 if (write_it)
2301.                     write_timer(fd, curr);
2302.             }
2303.         }
2304.     }
2305. 
2306.     return count;
2307. }
2308. 
2309. /*
2310.  * Save part of the timer list.  The parameter 'range' specifies either
2311.  * global or level timers to save.  The timer ID is saved with the global
2312.  * timers.
2313.  *
2314.  * Global range:
2315.  *      + timeouts that follow the hero (global)
2316.  *      + timeouts that follow obj & monst that are migrating
2317.  *
2318.  * Level range:
2319.  *      + timeouts that are level specific (e.g. storms)
2320.  *      + timeouts that stay with the level (obj & monst)
2321.  */
2322. void
2323. save_timers(fd, mode, range)
2324. int fd, mode, range;
2325. {
2326.     timer_element *curr, *prev, *next_timer = 0;
2327.     int count;
2328. 
2329.     if (perform_bwrite(mode)) {
2330.         if (range == RANGE_GLOBAL)
2331.             bwrite(fd, (genericptr_t) &timer_id, sizeof(timer_id));
2332. 
2333.         count = maybe_write_timer(fd, range, FALSE);
2334.         bwrite(fd, (genericptr_t) &count, sizeof count);
2335.         (void) maybe_write_timer(fd, range, TRUE);
2336.     }
2337. 
2338.     if (release_data(mode)) {
2339.         for (prev = 0, curr = timer_base; curr; curr = next_timer) {
2340.             next_timer = curr->next; /* in case curr is removed */
2341. 
2342.             if (!(!!(range == RANGE_LEVEL) ^ !!timer_is_local(curr))) {
2343.                 if (prev)
2344.                     prev->next = curr->next;
2345.                 else
2346.                     timer_base = curr->next;
2347.                 free((genericptr_t) curr);
2348.                 /* prev stays the same */
2349.             } else {
2350.                 prev = curr;
2351.             }
2352.         }
2353.     }
2354. }
2355. 
2356. /*
2357.  * Pull in the structures from disk, but don't recalculate the object and
2358.  * monster pointers.
2359.  */
2360. void
2361. restore_timers(fd, range, ghostly, adjust)
2362. int fd, range;
2363. boolean ghostly; /* restoring from a ghost level */
2364. long adjust;     /* how much to adjust timeout */
2365. {
2366.     int count;
2367.     timer_element *curr;
2368. 
2369.     if (range == RANGE_GLOBAL)
2370.         mread(fd, (genericptr_t) &timer_id, sizeof timer_id);
2371. 
2372.     /* restore elements */
2373.     mread(fd, (genericptr_t) &count, sizeof count);
2374.     while (count-- > 0) {
2375.         curr = (timer_element *) alloc(sizeof(timer_element));
2376.         mread(fd, (genericptr_t) curr, sizeof(timer_element));
2377.         if (ghostly)
2378.             curr->timeout += adjust;
2379.         insert_timer(curr);
2380.     }
2381. }
2382. 
2383. /* to support '#stats' wizard-mode command */
2384. void
2385. timer_stats(hdrfmt, hdrbuf, count, size)
2386. const char *hdrfmt;
2387. char *hdrbuf;
2388. long *count, *size;
2389. {
2390.     timer_element *te;
2391. 
2392.     Sprintf(hdrbuf, hdrfmt, (long) sizeof (timer_element));
2393.     *count = *size = 0L;
2394.     for (te = timer_base; te; te = te->next) {
2395.         ++*count;
2396.         *size += (long) sizeof *te;
2397.     }
2398. }
2399. 
2400. /* reset all timers that are marked for reseting */
2401. void
2402. relink_timers(ghostly)
2403. boolean ghostly;
2404. {
2405.     timer_element *curr;
2406.     unsigned nid;
2407. 
2408.     for (curr = timer_base; curr; curr = curr->next) {
2409.         if (curr->needs_fixup) {
2410.             if (curr->kind == TIMER_OBJECT) {
2411.                 if (ghostly) {
2412.                     if (!lookup_id_mapping(curr->arg.a_uint, &nid))
2413.                         panic("relink_timers 1");
2414.                 } else
2415.                     nid = curr->arg.a_uint;
2416.                 curr->arg.a_obj = find_oid(nid);
2417.                 if (!curr->arg.a_obj)
2418.                     panic("cant find o_id %d", nid);
2419.                 curr->needs_fixup = 0;
2420.             } else if (curr->kind == TIMER_MONSTER) {
2421.                 panic("relink_timers: no monster timer implemented");
2422.             } else
2423.                 panic("relink_timers 2");
2424.         }
2425.     }
2426. }
2427. 
2428. /*timeout.c*/