Source:SLASH'EM 0.0.7E7F2/timeout.c

From NetHackWiki
Revision as of 19:49, 7 March 2008 by Kernigh bot (talk | contribs) (SLASH'EM 0.0.7E7F2/timeout.c moved to Source:SLASH'EM 0.0.7E7F2/timeout.c: Robot: moved page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Below is the full text to timeout.c from the source code of SLASH'EM 0.0.7E7F2. To link to a particular line, write [[SLASH'EM 0.0.7E7F2/timeout.c#line123]], for example.

The latest source code for vanilla NetHack is at Source code.


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