Fandom

Wikihack

Source:NetHack 3.4.0/timeout.c

2,034pages on
this wiki
Add New Page
Talk0

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Below is the full text to timeout.c from the source code of NetHack 3.4.0. To link to a particular line, write [[NetHack 3.4.0/timeout.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.
1.    /*	SCCS Id: @(#)timeout.c	3.4	2000/09/28	*/
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(slip_or_trip);
13.   STATIC_DCL void FDECL(see_lamp_flicker, (struct obj *, const char *));
14.   STATIC_DCL void FDECL(lantern_message, (struct obj *));
15.   STATIC_DCL void FDECL(cleanup_burn, (genericptr_t,long));
16.   
17.   #ifdef OVLB
18.   
19.   /* He is being petrified - dialogue by inmet!tower */
20.   static NEARDATA const char *stoned_texts[] = {
21.   	"You are slowing down.",		/* 5 */
22.   	"Your limbs are stiffening.",		/* 4 */
23.   	"Your limbs have turned to stone.",	/* 3 */
24.   	"You have turned to stone.",		/* 2 */
25.   	"You are a statue."			/* 1 */
26.   };
27.   
28.   STATIC_OVL void
29.   stoned_dialogue()
30.   {
31.   	register long i = (Stoned & TIMEOUT);
32.   
33.   	if (i > 0L && i <= SIZE(stoned_texts))
34.   		pline(stoned_texts[SIZE(stoned_texts) - i]);
35.   	if (i == 5L)
36.   		HFast = 0L;
37.   	if (i == 3L)
38.   		nomul(-3);
39.   	exercise(A_DEX, FALSE);
40.   }
41.   
42.   /* He is getting sicker and sicker prior to vomiting */
43.   static NEARDATA const char *vomiting_texts[] = {
44.   	"are feeling mildly nauseous.",		/* 14 */
45.   	"feel slightly confused.",		/* 11 */
46.   	"can't seem to think straight.",	/* 8 */
47.   	"feel incredibly sick.",		/* 5 */
48.   	"suddenly vomit!"			/* 2 */
49.   };
50.   
51.   STATIC_OVL void
52.   vomiting_dialogue()
53.   {
54.   	register long i = (Vomiting & TIMEOUT) / 3L;
55.   
56.   	if ((((Vomiting & TIMEOUT) % 3L) == 2) && (i >= 0)
57.   	    && (i < SIZE(vomiting_texts)))
58.   		You(vomiting_texts[SIZE(vomiting_texts) - i - 1]);
59.   
60.   	switch ((int) i) {
61.   	case 0:
62.   		vomit();
63.   		morehungry(20);
64.   		break;
65.   	case 2:
66.   		make_stunned(HStun + d(2,4), FALSE);
67.   		/* fall through */
68.   	case 3:
69.   		make_confused(HConfusion + d(2,4), FALSE);
70.   		break;
71.   	}
72.   	exercise(A_CON, FALSE);
73.   }
74.   
75.   static NEARDATA const char *choke_texts[] = {
76.   	"You find it hard to breathe.",
77.   	"You're gasping for air.",
78.   	"You can no longer breathe.",
79.   	"You're turning %s.",
80.   	"You suffocate."
81.   };
82.   
83.   static NEARDATA const char *choke_texts2[] = {
84.   	"Your %s is becoming constricted.",
85.   	"Your blood is having trouble reaching your brain.",
86.   	"The pressure on your %s increases.",
87.   	"Your consciousness is fading.",
88.   	"You suffocate."
89.   };
90.   
91.   STATIC_OVL void
92.   choke_dialogue()
93.   {
94.   	register long i = (Strangled & TIMEOUT);
95.   
96.   	if(i > 0 && i <= SIZE(choke_texts)) {
97.   	    if (Breathless || !rn2(50))
98.   		pline(choke_texts2[SIZE(choke_texts2) - i], body_part(NECK));
99.   	    else {
100.  		const char *str = choke_texts[SIZE(choke_texts)-i];
101.  
102.  		if (index(str, '%'))
103.  		    pline(str, hcolor(blue));
104.  		else
105.  		    pline(str);
106.  	    }
107.  	}
108.  	exercise(A_STR, FALSE);
109.  }
110.  
111.  static NEARDATA const char *slime_texts[] = {
112.  	"You are turning a little %s.",           /* 5 */
113.  	"Your limbs are getting oozy.",              /* 4 */
114.  	"Your skin begins to peel away.",            /* 3 */
115.  	"You are turning into %s.",       /* 2 */
116.  	"You have become %s."             /* 1 */
117.  };
118.  
119.  STATIC_OVL void
120.  slime_dialogue()
121.  {
122.  	register long i = (Slimed & TIMEOUT) / 2L;
123.  
124.  	if (((Slimed & TIMEOUT) % 2L) && i >= 0L
125.  		&& i < SIZE(slime_texts)) {
126.  	    const char *str = slime_texts[SIZE(slime_texts) - i - 1L];
127.  
128.  	    if (index(str, '%')) {
129.  		if (i == 4L) {	/* "you are turning green" */
130.  		    if (!Blind)	/* [what if you're already green?] */
131.  			pline(str, hcolor(green));
132.  		} else
133.  		    pline(str, an(Hallucination ? rndmonnam() : "green slime"));
134.  	    } else
135.  		pline(str);
136.  	}
137.  	if (i == 3L) {	/* limbs becoming oozy */
138.  	    HFast = 0L;	/* lose intrinsic speed */
139.  	    stop_occupation();
140.  	    if (multi > 0) nomul(0);
141.  	}
142.  	exercise(A_DEX, FALSE);
143.  }
144.  
145.  void
146.  burn_away_slime()
147.  {
148.  	if (Slimed) {
149.  	    pline_The("slime that covers you is burned away!");
150.  	    Slimed = 0L;
151.  	    flags.botl = 1;
152.  	}
153.  	return;
154.  }
155.  
156.  
157.  #endif /* OVLB */
158.  #ifdef OVL0
159.  
160.  void
161.  nh_timeout()
162.  {
163.  	register struct prop *upp;
164.  	int sleeptime;
165.  	int m_idx;
166.  	int baseluck = (flags.moonphase == FULL_MOON) ? 1 : 0;
167.  
168.  	if (flags.friday13) baseluck -= 1;
169.  
170.  	if (u.uluck != baseluck &&
171.  		moves % (u.uhave.amulet || u.ugangr ? 300 : 600) == 0) {
172.  	/* Cursed luckstones stop bad luck from timing out; blessed luckstones
173.  	 * stop good luck from timing out; normal luckstones stop both;
174.  	 * neither is stopped if you don't have a luckstone.
175.  	 * Luck is based at 0 usually, +1 if a full moon and -1 on Friday 13th
176.  	 */
177.  	    register int time_luck = stone_luck(FALSE);
178.  	    boolean nostone = !carrying(LUCKSTONE) && !stone_luck(TRUE);
179.  
180.  	    if(u.uluck > baseluck && (nostone || time_luck < 0))
181.  		u.uluck--;
182.  	    else if(u.uluck < baseluck && (nostone || time_luck > 0))
183.  		u.uluck++;
184.  	}
185.  	if(u.uinvulnerable) return; /* things past this point could kill you */
186.  	if(Stoned) stoned_dialogue();
187.  	if(Slimed) slime_dialogue();
188.  	if(Vomiting) vomiting_dialogue();
189.  	if(Strangled) choke_dialogue();
190.  	if(u.mtimedone && !--u.mtimedone) {
191.  		if (Unchanging)
192.  			u.mtimedone = rnd(100*youmonst.data->mlevel + 1);
193.  		else
194.  			rehumanize();
195.  	}
196.  	if(u.ucreamed) u.ucreamed--;
197.  
198.  	/* Dissipate spell-based protection. */
199.  	if (u.usptime) {
200.  	    if (--u.usptime == 0 && u.uspellprot) {
201.  		u.usptime = u.uspmtime;
202.  		u.uspellprot--;
203.  		find_ac();
204.  		if (!Blind)
205.  		    Norep("The %s haze around you %s.", hcolor(golden),
206.  			  u.uspellprot ? "becomes less dense" : "disappears");
207.  	    }
208.  	}
209.  
210.  #ifdef STEED
211.  	if (u.ugallop) {
212.  	    if (--u.ugallop == 0L && u.usteed)
213.  	    	pline("%s stops galloping.", Monnam(u.usteed));
214.  	}
215.  #endif
216.  
217.  	for(upp = u.uprops; upp < u.uprops+SIZE(u.uprops); upp++)
218.  	    if((upp->intrinsic & TIMEOUT) && !(--upp->intrinsic & TIMEOUT)) {
219.  		switch(upp - u.uprops){
220.  		case STONED:
221.  			if (delayed_killer && !killer) {
222.  				killer = delayed_killer;
223.  				delayed_killer = 0;
224.  			}
225.  			if (!killer) {
226.  				/* leaving killer_format would make it
227.  				   "petrified by petrification" */
228.  				killer_format = NO_KILLER_PREFIX;
229.  				killer = "killed by petrification";
230.  			}
231.  			done(STONING);
232.  			break;
233.  		case SLIMED:
234.  			if (delayed_killer && !killer) {
235.  				killer = delayed_killer;
236.  				delayed_killer = 0;
237.  			}
238.  			if (!killer) {
239.  				killer_format = NO_KILLER_PREFIX;
240.  				killer = "turned into green slime";
241.  			}
242.  			done(TURNED_SLIME);
243.  			break;
244.  		case VOMITING:
245.  			make_vomiting(0L, TRUE);
246.  			break;
247.  		case SICK:
248.  			You("die from your illness.");
249.  			killer_format = KILLED_BY_AN;
250.  			killer = u.usick_cause;
251.  			if ((m_idx = name_to_mon(killer)) >= LOW_PM) {
252.  			    if (type_is_pname(&mons[m_idx])) {
253.  				killer_format = KILLED_BY;
254.  			    }
255.  #if 0				/* at present, there aren't any monster
256.  				   poisoners with titles rather than names */
257.  			    else if (mons[m_idx].geno & G_UNIQ) {
258.  				char buf[BUFSZ];
259.  				Sprintf(buf, "the %s", killer);
260.  				Strcpy(u.usick_cause, buf);
261.  				killer_format = KILLED_BY;
262.  			    }
263.  #endif
264.  			}
265.  			u.usick_type = 0;
266.  			done(POISONING);
267.  			break;
268.  		case FAST:
269.  			if (!Very_fast)
270.  				You_feel("yourself slowing down%s.",
271.  							Fast ? " a bit" : "");
272.  			break;
273.  		case CONFUSION:
274.  			HConfusion = 1; /* So make_confused works properly */
275.  			make_confused(0L, TRUE);
276.  			stop_occupation();
277.  			break;
278.  		case STUNNED:
279.  			HStun = 1;
280.  			make_stunned(0L, TRUE);
281.  			stop_occupation();
282.  			break;
283.  		case BLINDED:
284.  			Blinded = 1;
285.  			make_blinded(0L, TRUE);
286.  			stop_occupation();
287.  			break;
288.  		case INVIS:
289.  			newsym(u.ux,u.uy);
290.  			if (!Invis && !BInvis &&
291.  			    !See_invisible && !Blind) {
292.  				You("are no longer invisible.");
293.  				stop_occupation();
294.  			}
295.  			break;
296.  		case SEE_INVIS:
297.  			set_mimic_blocking(); /* do special mimic handling */
298.  			see_monsters();		/* make invis mons appear */
299.  			newsym(u.ux,u.uy);	/* make self appear */
300.  			stop_occupation();
301.  			break;
302.  		case WOUNDED_LEGS:
303.  			heal_legs();
304.  			stop_occupation();
305.  			break;
306.  		case HALLUC:
307.  			HHallucination = 1;
308.  			make_hallucinated(0L, TRUE, 0L);
309.  			stop_occupation();
310.  			break;
311.  		case SLEEPING:
312.  			if (unconscious() || Sleep_resistance)
313.  				HSleeping += rnd(100);
314.  			else if (Sleeping) {
315.  				You("fall asleep.");
316.  				sleeptime = rnd(20);
317.  				fall_asleep(-sleeptime, TRUE);
318.  				HSleeping += sleeptime + rnd(100);
319.  			}
320.  			break;
321.  		case LEVITATION:
322.  			(void) float_down(I_SPECIAL|TIMEOUT, 0L);
323.  			break;
324.  		case STRANGLED:
325.  			killer_format = KILLED_BY;
326.  			killer = (u.uburied) ? "suffocation" : "strangulation";
327.  			done(DIED);
328.  			break;
329.  		case FUMBLING:
330.  			/* call this only when a move took place.  */
331.  			/* otherwise handle fumbling msgs locally. */
332.  			if (u.umoved && !Levitation) {
333.  			    slip_or_trip();
334.  			    nomul(-2);
335.  			    nomovemsg = "";
336.  			    /* The more you are carrying the more likely you
337.  			     * are to make noise when you fumble.  Adjustments
338.  			     * to this number must be thoroughly play tested.
339.  			     */
340.  			    if ((inv_weight() > -500)) {
341.  				You("make a lot of noise!");
342.  				wake_nearby();
343.  			    }
344.  			}
345.  			/* from outside means slippery ice; don't reset
346.  			   counter if that's the only fumble reason */
347.  			HFumbling &= ~FROMOUTSIDE;
348.  			if (Fumbling)
349.  			    HFumbling += rnd(20);
350.  			break;
351.  		case DETECT_MONSTERS:
352.  			see_monsters();
353.  			break;
354.  		}
355.  	}
356.  
357.  	run_timers();
358.  }
359.  
360.  #endif /* OVL0 */
361.  #ifdef OVL1
362.  
363.  void
364.  fall_asleep(how_long, wakeup_msg)
365.  int how_long;
366.  boolean wakeup_msg;
367.  {
368.  	stop_occupation();
369.  	nomul(how_long);
370.  	/* early wakeup from combat won't be possible until next monster turn */
371.  	u.usleep = monstermoves;
372.  	nomovemsg = wakeup_msg ? "You wake up." : You_can_move_again;
373.  }
374.  
375.  /* Attach an egg hatch timeout to the given egg. */
376.  void
377.  attach_egg_hatch_timeout(egg)
378.  struct obj *egg;
379.  {
380.  	int i;
381.  
382.  	/* stop previous timer, if any */
383.  	(void) stop_timer(HATCH_EGG, (genericptr_t) egg);
384.  
385.  	/*
386.  	 * Decide if and when to hatch the egg.  The old hatch_it() code tried
387.  	 * once a turn from age 151 to 200 (inclusive), hatching if it rolled
388.  	 * a number x, 1<=x<=age, where x>150.  This yields a chance of
389.  	 * hatching > 99.9993%.  Mimic that here.
390.  	 */
391.  	for (i = (MAX_EGG_HATCH_TIME-50)+1; i <= MAX_EGG_HATCH_TIME; i++)
392.  	    if (rnd(i) > 150) {
393.  		/* egg will hatch */
394.  		(void) start_timer((long)i, TIMER_OBJECT,
395.  						HATCH_EGG, (genericptr_t)egg);
396.  		break;
397.  	    }
398.  }
399.  
400.  /* prevent an egg from ever hatching */
401.  void
402.  kill_egg(egg)
403.  struct obj *egg;
404.  {
405.  	/* stop previous timer, if any */
406.  	(void) stop_timer(HATCH_EGG, (genericptr_t) egg);
407.  }
408.  
409.  /* timer callback routine: hatch the given egg */
410.  void
411.  hatch_egg(arg, timeout)
412.  genericptr_t arg;
413.  long timeout;
414.  {
415.  	struct obj *egg;
416.  	struct monst *mon, *mon2;
417.  	coord cc;
418.  	xchar x, y;
419.  	boolean yours, silent, knows_egg = FALSE;
420.  	boolean cansee_hatchspot = FALSE;
421.  	int i, mnum, hatchcount = 0;
422.  
423.  	egg = (struct obj *) arg;
424.  	/* sterilized while waiting */
425.  	if (egg->corpsenm == NON_PM) return;
426.  
427.  	mon = mon2 = (struct monst *)0;
428.  	mnum = big_to_little(egg->corpsenm);
429.  	/* The identity of one's father is learned, not innate */
430.  	yours = (egg->spe || (!flags.female && carried(egg) && !rn2(2)));
431.  	silent = (timeout != monstermoves);	/* hatched while away */
432.  
433.  	/* only can hatch when in INVENT, FLOOR, MINVENT */
434.  	if (get_obj_location(egg, &x, &y, 0)) {
435.  	    hatchcount = rnd((int)egg->quan);
436.  	    cansee_hatchspot = cansee(x, y) && !silent;
437.  	    if (!(mons[mnum].geno & G_UNIQ) &&
438.  		   !(mvitals[mnum].mvflags & (G_GENOD | G_EXTINCT))) {
439.  		for (i = hatchcount; i > 0; i--) {
440.  		    if (!enexto(&cc, x, y, &mons[mnum]) ||
441.  			 !(mon = makemon(&mons[mnum], cc.x, cc.y, NO_MINVENT)))
442.  			break;
443.  		    /* tame if your own egg hatches while you're on the
444.  		       same dungeon level, or any dragon egg which hatches
445.  		       while it's in your inventory */
446.  		    if ((yours && !silent) ||
447.  			(carried(egg) && mon->data->mlet == S_DRAGON)) {
448.  			if ((mon2 = tamedog(mon, (struct obj *)0)) != 0) {
449.  			    mon = mon2;
450.  			    if (carried(egg) && mon->data->mlet != S_DRAGON)
451.  				mon->mtame = 20;
452.  			}
453.  		    }
454.  		    if (mvitals[mnum].mvflags & G_EXTINCT)
455.  			break;	/* just made last one */
456.  		    mon2 = mon;	/* in case makemon() fails on 2nd egg */
457.  		}
458.  		if (!mon) mon = mon2;
459.  		hatchcount -= i;
460.  		egg->quan -= (long)hatchcount;
461.  	    }
462.  	}
463.  #if 0
464.  	/*
465.  	 * We could possibly hatch while migrating, but the code isn't
466.  	 * set up for it...
467.  	 */
468.  	else if (obj->where == OBJ_MIGRATING) {
469.  	    /*
470.  	    We can do several things.  The first ones that come to
471.  	    mind are:
472.  
473.  	    + Create the hatched monster then place it on the migrating
474.  	      mons list.  This is tough because all makemon() is made
475.  	      to place the monster as well.    Makemon() also doesn't
476.  	      lend itself well to splitting off a "not yet placed"
477.  	      subroutine.
478.  
479.  	    + Mark the egg as hatched, then place the monster when we
480.  	      place the migrating objects.
481.  
482.  	    + Or just kill any egg which gets sent to another level.
483.  	      Falling is the usual reason such transportation occurs.
484.  	    */
485.  	    cansee_hatchspot = FALSE;
486.  	    mon = ???
487.  	    }
488.  #endif
489.  
490.  	if (mon) {
491.  	    char monnambuf[BUFSZ], carriedby[BUFSZ];
492.  	    boolean siblings = (hatchcount > 1), redraw = FALSE;
493.  
494.  	    if (cansee_hatchspot) {
495.  		Sprintf(monnambuf, "%s%s",
496.  			siblings ? "some " : "",
497.  			siblings ?
498.  			makeplural(m_monnam(mon)) : an(m_monnam(mon)));
499.  		/* we don't learn the egg type here because learning
500.  		   an egg type requires either seeing the egg hatch
501.  		   or being familiar with the egg already,
502.  		   as well as being able to see the resulting
503.  		   monster, checked below
504.  		*/
505.  	    }
506.  	    switch (egg->where) {
507.  		case OBJ_INVENT:
508.  		    knows_egg = TRUE; /* true even if you are blind */
509.  		    if (!cansee_hatchspot)
510.  			You_feel("%s %s from your pack!", something,
511.  			    locomotion(mon->data, "drop"));
512.  		    else
513.  			You("see %s %s out of your pack!",
514.  			    monnambuf, locomotion(mon->data, "drop"));
515.  		    if (yours) {
516.  			pline("%s cries sound like \"%s%s\"",
517.  			    siblings ? "Their" : "Its",
518.  			    flags.female ? "mommy" : "daddy",
519.  			    egg->spe ? "." : "?");
520.  		    } else if (mon->data->mlet == S_DRAGON) {
521.  			verbalize("Gleep!");		/* Mything eggs :-) */
522.  		    }
523.  		    break;
524.  
525.  		case OBJ_FLOOR:
526.  		    if (cansee_hatchspot) {
527.  			knows_egg = TRUE;
528.  			You("see %s hatch.", monnambuf);
529.  			redraw = TRUE;	/* update egg's map location */
530.  		    }
531.  		    break;
532.  
533.  		case OBJ_MINVENT:
534.  		    if (cansee_hatchspot) {
535.  			/* egg carring monster might be invisible */
536.  			if (canseemon(egg->ocarry)) {
537.  			    Sprintf(carriedby, "%s pack",
538.  				     s_suffix(a_monnam(egg->ocarry)));
539.  			    knows_egg = TRUE;
540.  			}
541.  			else if (is_pool(mon->mx, mon->my))
542.  			    Strcpy(carriedby, "empty water");
543.  			else
544.  			    Strcpy(carriedby, "thin air");
545.  			You("see %s %s out of %s!", monnambuf,
546.  			    locomotion(mon->data, "drop"), carriedby);
547.  		    }
548.  		    break;
549.  #if 0
550.  		case OBJ_MIGRATING:
551.  		    break;
552.  #endif
553.  		default:
554.  		    impossible("egg hatched where? (%d)", (int)egg->where);
555.  		    break;
556.  	    }
557.  
558.  	    if (cansee_hatchspot && knows_egg)
559.  		learn_egg_type(mnum);
560.  
561.  	    if (egg->quan > 0) {
562.  		/* still some eggs left */
563.  		attach_egg_hatch_timeout(egg);
564.  		if (egg->timed) {
565.  		    /* replace ordinary egg timeout with a short one */
566.  		    (void) stop_timer(HATCH_EGG, (genericptr_t)egg);
567.  		    (void) start_timer((long)rnd(12), TIMER_OBJECT,
568.  					HATCH_EGG, (genericptr_t)egg);
569.  		}
570.  	    } else if (carried(egg)) {
571.  		useup(egg);
572.  	    } else {
573.  		/* free egg here because we use it above */
574.  		obj_extract_self(egg);
575.  		obfree(egg, (struct obj *)0);
576.  	    }
577.  	    if (redraw) newsym(x, y);
578.  	}
579.  }
580.  
581.  /* Learn to recognize eggs of the given type. */
582.  void
583.  learn_egg_type(mnum)
584.  int mnum;
585.  {
586.  	/* baby monsters hatch from grown-up eggs */
587.  	mnum = little_to_big(mnum);
588.  	mvitals[mnum].mvflags |= MV_KNOWS_EGG;
589.  	/* we might have just learned about other eggs being carried */
590.  	update_inventory();
591.  }
592.  
593.  /* Attach a fig_transform timeout to the given figurine. */
594.  void
595.  attach_fig_transform_timeout(figurine)
596.  struct obj *figurine;
597.  {
598.  	int i;
599.  
600.  	/* stop previous timer, if any */
601.  	(void) stop_timer(FIG_TRANSFORM, (genericptr_t) figurine);
602.  
603.  	/*
604.  	 * Decide when to transform the figurine.
605.  	 */
606.  	i = rnd(9000) + 200;
607.  	/* figurine will transform */
608.  	(void) start_timer((long)i, TIMER_OBJECT,
609.  				FIG_TRANSFORM, (genericptr_t)figurine);
610.  }
611.  
612.  /* give a fumble message */
613.  STATIC_OVL void
614.  slip_or_trip()
615.  {
616.  	struct obj *otmp = vobj_at(u.ux, u.uy);
617.  	const char *what, *pronoun;
618.  	char buf[BUFSZ];
619.  	boolean on_foot = TRUE;
620.  #ifdef STEED
621.  	if (u.usteed) on_foot = FALSE;
622.  #endif
623.  
624.  	if (otmp && on_foot) {		/* trip over something in particular */
625.  	    /*
626.  		If there is only one item, it will have just been named
627.  		during the move, so refer to by via pronoun; otherwise,
628.  		if the top item has been or can be seen, refer to it by
629.  		name; if not, look for rocks to trip over; trip over
630.  		anonymous "something" if there aren't any rocks.
631.  	     */
632.  	    pronoun = otmp->quan == 1L ? "it" : Hallucination ? "they" : "them";
633.  	    what = !otmp->nexthere ? pronoun :
634.  		  (otmp->dknown || !Blind) ? doname(otmp) :
635.  		  ((otmp = sobj_at(ROCK, u.ux, u.uy)) == 0 ? something :
636.  		  (otmp->quan == 1L ? "a rock" : "some rocks"));
637.  	    if (Hallucination) {
638.  		what = strcpy(buf, what);
639.  		buf[0] = highc(buf[0]);
640.  		pline("Egads!  %s bite%s your %s!",
641.  			what, (!otmp || otmp->quan == 1L) ? "s" : "",
642.  			body_part(FOOT));
643.  	    } else {
644.  		You("trip over %s.", what);
645.  	    }
646.  	} else if (rn2(3) && is_ice(u.ux, u.uy)) {
647.  	    pline("%s %s%s on the ice.",
648.  #ifdef STEED
649.  		u.usteed ? upstart(x_monnam(u.usteed,
650.  				u.usteed->mnamelth ? ARTICLE_NONE : ARTICLE_THE,
651.  				(char *)0, SUPPRESS_SADDLE, FALSE)) :
652.  #endif
653.  		"You", rn2(2) ? "slip" : "slide", on_foot ? "" : "s");
654.  	} else {
655.  	    if (on_foot) {
656.  		switch (rn2(4)) {
657.  		  case 1:
658.  			You("trip over your own %s.", Hallucination ?
659.  				"elbow" : makeplural(body_part(FOOT)));
660.  			break;
661.  		  case 2:
662.  			You("slip %s.", Hallucination ?
663.  				"on a banana peel" : "and nearly fall");
664.  			break;
665.  		  case 3:
666.  			You("flounder.");
667.  			break;
668.  		  default:
669.  			You("stumble.");
670.  			break;
671.  		}
672.  	    }
673.  #ifdef STEED
674.  	    else {
675.  		switch (rn2(4)) {
676.  		  case 1:
677.  			Your("%s slip out of the stirrups.", makeplural(body_part(FOOT)));
678.  			break;
679.  		  case 2:
680.  			You("let go of the reins.");
681.  			break;
682.  		  case 3:
683.  			You("bang into the saddle-horn.");
684.  			break;
685.  		  default:
686.  			You("slide to one side of the saddle.");
687.  			break;
688.  		}
689.  		dismount_steed(DISMOUNT_FELL);
690.  	    }
691.  #endif
692.  	}
693.  }
694.  
695.  /* Print a lamp flicker message with tailer. */
696.  STATIC_OVL void
697.  see_lamp_flicker(obj, tailer)
698.  struct obj *obj;
699.  const char *tailer;
700.  {
701.  	switch (obj->where) {
702.  	    case OBJ_INVENT:
703.  	    case OBJ_MINVENT:
704.  		pline("%s flickers%s.", Yname2(obj), tailer);
705.  		break;
706.  	    case OBJ_FLOOR:
707.  		You("see %s flicker%s.", an(xname(obj)), tailer);
708.  		break;
709.  	}
710.  }
711.  
712.  /* Print a dimming message for brass lanterns. */
713.  STATIC_OVL void
714.  lantern_message(obj)
715.  struct obj *obj;
716.  {
717.  	/* from adventure */
718.  	switch (obj->where) {
719.  	    case OBJ_INVENT:
720.  		Your("lantern is getting dim.");
721.  		if (Hallucination)
722.  		    pline("Batteries have not been invented yet.");
723.  		break;
724.  	    case OBJ_FLOOR:
725.  		You("see a lantern getting dim.");
726.  		break;
727.  	    case OBJ_MINVENT:
728.  		pline("%s lantern is getting dim.",
729.  		    s_suffix(Monnam(obj->ocarry)));
730.  		break;
731.  	}
732.  }
733.  
734.  /*
735.   * Timeout callback for for objects that are burning. E.g. lamps, candles.
736.   * See begin_burn() for meanings of obj->age and obj->spe.
737.   */
738.  void
739.  burn_object(arg, timeout)
740.  genericptr_t arg;
741.  long timeout;
742.  {
743.  	struct obj *obj = (struct obj *) arg;
744.  	boolean canseeit, many, menorah, need_newsym;
745.  	xchar x, y;
746.  	char whose[BUFSZ];
747.  
748.  	menorah = obj->otyp == CANDELABRUM_OF_INVOCATION;
749.  	many = menorah ? obj->spe > 1 : obj->quan > 1L;
750.  
751.  	/* timeout while away */
752.  	if (timeout != monstermoves) {
753.  	    long how_long = monstermoves - timeout;
754.  
755.  	    if (how_long >= obj->age) {
756.  		obj->age = 0;
757.  		end_burn(obj, FALSE);
758.  
759.  		if (menorah) {
760.  		    obj->spe = 0;	/* no more candles */
761.  		} else if (Is_candle(obj) || obj->otyp == POT_OIL) {
762.  		    /* get rid of candles and burning oil potions */
763.  		    obj_extract_self(obj);
764.  		    obfree(obj, (struct obj *)0);
765.  		    obj = (struct obj *) 0;
766.  		}
767.  
768.  	    } else {
769.  		obj->age -= how_long;
770.  		begin_burn(obj, TRUE);
771.  	    }
772.  	    return;
773.  	}
774.  
775.  	/* only interested in INVENT, FLOOR, and MINVENT */
776.  	if (get_obj_location(obj, &x, &y, 0)) {
777.  	    canseeit = !Blind && cansee(x, y);
778.  	    /* set up `whose[]' to be "Your" or "Fred's" or "The goblin's" */
779.  	    (void) Shk_Your(whose, obj);
780.  	} else {
781.  	    canseeit = FALSE;
782.  	}
783.  	need_newsym = FALSE;
784.  
785.  	/* obj->age is the age remaining at this point.  */
786.  	switch (obj->otyp) {
787.  	    case POT_OIL:
788.  		    /* this should only be called when we run out */
789.  		    if (canseeit) {
790.  			switch (obj->where) {
791.  			    case OBJ_INVENT:
792.  			    case OBJ_MINVENT:
793.  				pline("%s potion of oil has burnt away.",
794.  				    whose);
795.  				break;
796.  			    case OBJ_FLOOR:
797.  				You("see a burning potion of oil go out.");
798.  				need_newsym = TRUE;
799.  				break;
800.  			}
801.  		    }
802.  		    end_burn(obj, FALSE);	/* turn off light source */
803.  		    obj_extract_self(obj);
804.  		    obfree(obj, (struct obj *)0);
805.  		    obj = (struct obj *) 0;
806.  		    break;
807.  
808.  	    case BRASS_LANTERN:
809.  	    case OIL_LAMP:
810.  		switch((int)obj->age) {
811.  		    case 150:
812.  		    case 100:
813.  		    case 50:
814.  			if (canseeit) {
815.  			    if (obj->otyp == BRASS_LANTERN)
816.  				lantern_message(obj);
817.  			    else
818.  				see_lamp_flicker(obj,
819.  				    obj->age == 50L ? " considerably" : "");
820.  			}
821.  			break;
822.  
823.  		    case 25:
824.  			if (canseeit) {
825.  			    if (obj->otyp == BRASS_LANTERN)
826.  				lantern_message(obj);
827.  			    else {
828.  				switch (obj->where) {
829.  				    case OBJ_INVENT:
830.  				    case OBJ_MINVENT:
831.  					pline("%s %s seems about to go out.",
832.  					    whose, xname(obj));
833.  					break;
834.  				    case OBJ_FLOOR:
835.  					You("see %s about to go out.",
836.  					    an(xname(obj)));
837.  					break;
838.  				}
839.  			    }
840.  			}
841.  			break;
842.  
843.  		    case 0:
844.  			/* even if blind you'll know if holding it */
845.  			if (canseeit || obj->where == OBJ_INVENT) {
846.  			    switch (obj->where) {
847.  				case OBJ_INVENT:
848.  				case OBJ_MINVENT:
849.  				    if (obj->otyp == BRASS_LANTERN)
850.  					pline("%s lantern has run out of power.",
851.  					    whose);
852.  				    else
853.  					pline("%s %s has gone out.",
854.  					    whose, xname(obj));
855.  				    break;
856.  				case OBJ_FLOOR:
857.  				    if (obj->otyp == BRASS_LANTERN)
858.  					You("see a lantern run out of power.");
859.  				    else
860.  					You("see %s go out.",
861.  					    an(xname(obj)));
862.  				    break;
863.  			    }
864.  			}
865.  			end_burn(obj, FALSE);
866.  			break;
867.  
868.  		    default:
869.  			/*
870.  			 * Someone added fuel to the lamp while it was
871.  			 * lit.  Just fall through and let begin burn
872.  			 * handle the new age.
873.  			 */
874.  			break;
875.  		}
876.  
877.  		if (obj->age)
878.  		    begin_burn(obj, TRUE);
879.  
880.  		break;
881.  
882.  	    case CANDELABRUM_OF_INVOCATION:
883.  	    case TALLOW_CANDLE:
884.  	    case WAX_CANDLE:
885.  		switch (obj->age) {
886.  		    case 75:
887.  			if (canseeit)
888.  			    switch (obj->where) {
889.  				case OBJ_INVENT:
890.  				case OBJ_MINVENT:
891.  				    pline("%s %scandle%s getting short.",
892.  					whose,
893.  					menorah ? "candelabrum's " : "",
894.  					many ? "s are" : " is");
895.  				    break;
896.  				case OBJ_FLOOR:
897.  				    You("see %scandle%s getting short.",
898.  					    menorah ? "a candelabrum's " :
899.  						many ? "some " : "a ",
900.  					    many ? "s" : "");
901.  				    break;
902.  			    }
903.  			break;
904.  
905.  		    case 15:
906.  			if (canseeit)
907.  			    switch (obj->where) {
908.  				case OBJ_INVENT:
909.  				case OBJ_MINVENT:
910.  				    pline(
911.  					"%s %scandle%s flame%s flicker%s low!",
912.  					    whose,
913.  					    menorah ? "candelabrum's " : "",
914.  					    many ? "s'" : "'s",
915.  					    many ? "s" : "",
916.  					    many ? "" : "s");
917.  				    break;
918.  				case OBJ_FLOOR:
919.  				    You("see %scandle%s flame%s flicker low!",
920.  					    menorah ? "a candelabrum's " :
921.  						many ? "some " : "a ",
922.  					    many ? "s'" : "'s",
923.  					    many ? "s" : "");
924.  				    break;
925.  			    }
926.  			break;
927.  
928.  		    case 0:
929.  			/* we know even if blind and in our inventory */
930.  			if (canseeit || obj->where == OBJ_INVENT) {
931.  			    if (menorah) {
932.  				switch (obj->where) {
933.  				    case OBJ_INVENT:
934.  				    case OBJ_MINVENT:
935.  					pline("%s candelabrum's flame%s.",
936.  					    whose,
937.  					    many ? "s die" : " dies");
938.  					break;
939.  				    case OBJ_FLOOR:
940.  					You("see a candelabrum's flame%s die.",
941.  						many ? "s" : "");
942.  					break;
943.  				}
944.  			    } else {
945.  				switch (obj->where) {
946.  				    case OBJ_INVENT:
947.  				    case OBJ_MINVENT:
948.  					pline("%s %s %s consumed!",
949.  					    whose,
950.  					    xname(obj),
951.  					    many ? "are" : "is");
952.  					break;
953.  				    case OBJ_FLOOR:
954.  					/*
955.  					You see some wax candles consumed!
956.  					You see a wax candle consumed!
957.  					*/
958.  					You("see %s%s consumed!",
959.  					    many ? "some " : "",
960.  					    many ? xname(obj):an(xname(obj)));
961.  					need_newsym = TRUE;
962.  					break;
963.  				}
964.  
965.  				/* post message */
966.  				pline(Hallucination ?
967.  					(many ? "They shriek!" :
968.  						"It shrieks!") :
969.  					Blind ? "" :
970.  					    (many ? "Their flames die." :
971.  						    "Its flame dies."));
972.  			    }
973.  			}
974.  			end_burn(obj, FALSE);
975.  
976.  			if (menorah) {
977.  			    obj->spe = 0;
978.  			} else {
979.  			    obj_extract_self(obj);
980.  			    obfree(obj, (struct obj *)0);
981.  			    obj = (struct obj *) 0;
982.  			}
983.  			break;
984.  
985.  		    default:
986.  			/*
987.  			 * Someone added fuel (candles) to the menorah while
988.  			 * it was lit.  Just fall through and let begin burn
989.  			 * handle the new age.
990.  			 */
991.  			break;
992.  		}
993.  
994.  		if (obj && obj->age)
995.  		    begin_burn(obj, TRUE);
996.  
997.  		break;
998.  
999.  	    default:
1000. 		impossible("burn_object: unexpeced obj %s", xname(obj));
1001. 		break;
1002. 	}
1003. 	if (need_newsym) newsym(x, y);
1004. }
1005. 
1006. /*
1007.  * Start a burn timeout on the given object. If not "already lit" then
1008.  * create a light source for the vision system.  There had better not
1009.  * be a burn already running on the object.
1010.  *
1011.  * Magic lamps stay lit as long as there's a genie inside, so don't start
1012.  * a timer.
1013.  *
1014.  * Burn rules:
1015.  *	potions of oil, lamps & candles:
1016.  *		age = # of turns of fuel left
1017.  *		spe = <unused>
1018.  *
1019.  *	magic lamps:
1020.  *		age = <unused>
1021.  *		spe = 0 not lightable, 1 lightable forever
1022.  *
1023.  *	candelabrum:
1024.  *		age = # of turns of fuel left
1025.  *		spe = # of candles
1026.  *
1027.  * Once the burn begins, the age will be set to the amount of fuel
1028.  * remaining _once_the_burn_finishes_.  If the burn is terminated
1029.  * early then fuel is added back.
1030.  *
1031.  * This use of age differs from the use of age for corpses and eggs.
1032.  * For the latter items, age is when the object was created, so we
1033.  * know when it becomes "bad".
1034.  *
1035.  * This is a "silent" routine - it should not print anything out.
1036.  */
1037. void
1038. begin_burn(obj, already_lit)
1039. 	struct obj *obj;
1040. 	boolean already_lit;
1041. {
1042. 	int radius = 3;
1043. 	long turns = 0;
1044. 	boolean do_timer = TRUE;
1045. 
1046. 	if (obj->age == 0 && obj->otyp != MAGIC_LAMP && !artifact_light(obj))
1047. 	    return;
1048. 
1049. 	switch (obj->otyp) {
1050. 	    case MAGIC_LAMP:
1051. 		obj->lamplit = 1;
1052. 		do_timer = FALSE;
1053. 		break;
1054. 
1055. 	    case POT_OIL:
1056. 		turns = obj->age;
1057. 		radius = 1;	/* very dim light */
1058. 		break;
1059. 
1060. 	    case BRASS_LANTERN:
1061. 	    case OIL_LAMP:
1062. 		/* magic times are 150, 100, 50, 25, and 0 */
1063. 		if (obj->age > 150L)
1064. 		    turns = obj->age - 150L;
1065. 		else if (obj->age > 100L)
1066. 		    turns = obj->age - 100L;
1067. 		else if (obj->age > 50L)
1068. 		    turns = obj->age - 50L;
1069. 		else if (obj->age > 25L)
1070. 		    turns = obj->age - 25L;
1071. 		else
1072. 		    turns = obj->age;
1073. 		break;
1074. 
1075. 	    case CANDELABRUM_OF_INVOCATION:
1076. 	    case TALLOW_CANDLE:
1077. 	    case WAX_CANDLE:
1078. 		/* magic times are 75, 15, and 0 */
1079. 		if (obj->age > 75L)
1080. 		    turns = obj->age - 75L;
1081. 		else if (obj->age > 15L)
1082. 		    turns = obj->age - 15L;
1083. 		else
1084. 		    turns = obj->age;
1085. 		radius = candle_light_range(obj);
1086. 		break;
1087. 
1088. 	    default:
1089.                 /* [ALI] Support artifact light sources */
1090.                 if (artifact_light(obj)) {
1091. 		    obj->lamplit = 1;
1092. 		    do_timer = FALSE;
1093. 		    radius = 2;
1094. 		} else {
1095. 		    impossible("begin burn: unexpected %s", xname(obj));
1096. 		    turns = obj->age;
1097. 		}
1098. 		break;
1099. 	}
1100. 
1101. 	if (do_timer) {
1102. 	    if (start_timer(turns, TIMER_OBJECT,
1103. 					BURN_OBJECT, (genericptr_t)obj)) {
1104. 		obj->lamplit = 1;
1105. 		obj->age -= turns;
1106. 		if (carried(obj) && !already_lit)
1107. 		    update_inventory();
1108. 	    } else {
1109. 		obj->lamplit = 0;
1110. 	    }
1111. 	} else {
1112. 	    if (carried(obj) && !already_lit)
1113. 		update_inventory();
1114. 	}
1115. 
1116. 	if (obj->lamplit && !already_lit) {
1117. 	    xchar x, y;
1118. 
1119. 	    if (get_obj_location(obj, &x, &y, CONTAINED_TOO|BURIED_TOO))
1120. 		new_light_source(x, y, radius, LS_OBJECT, (genericptr_t) obj);
1121. 	    else
1122. 		impossible("begin_burn: can't get obj position");
1123. 	}
1124. }
1125. 
1126. /*
1127.  * Stop a burn timeout on the given object if timer attached.  Darken
1128.  * light source.
1129.  */
1130. void
1131. end_burn(obj, timer_attached)
1132. 	struct obj *obj;
1133. 	boolean timer_attached;
1134. {
1135. 	if (!obj->lamplit) {
1136. 	    impossible("end_burn: obj %s not lit", xname(obj));
1137. 	    return;
1138. 	}
1139. 
1140. 	if (obj->otyp == MAGIC_LAMP || artifact_light(obj))
1141. 	    timer_attached = FALSE;
1142. 
1143. 	if (!timer_attached) {
1144. 	    /* [DS] Cleanup explicitly, since timer cleanup won't happen */
1145. 	    del_light_source(LS_OBJECT, (genericptr_t)obj);
1146. 	    obj->lamplit = 0;
1147. 	    if (obj->where == OBJ_INVENT)
1148. 		update_inventory();
1149. 	} else if (!stop_timer(BURN_OBJECT, (genericptr_t) obj))
1150. 	    impossible("end_burn: obj %s not timed!", xname(obj));
1151. }
1152. 
1153. #endif /* OVL1 */
1154. #ifdef OVL0
1155. 
1156. /*
1157.  * Cleanup a burning object if timer stopped.
1158.  */
1159. static void
1160. cleanup_burn(arg, expire_time)
1161.     genericptr_t arg;
1162.     long expire_time;
1163. {
1164.     struct obj *obj = (struct obj *)arg;
1165.     if (!obj->lamplit) {
1166. 	impossible("cleanup_burn: obj %s not lit", xname(obj));
1167. 	return;
1168.     }
1169. 
1170.     del_light_source(LS_OBJECT, arg);
1171. 
1172.     /* restore unused time */
1173.     obj->age += expire_time - monstermoves;
1174. 
1175.     obj->lamplit = 0;
1176. 
1177.     if (obj->where == OBJ_INVENT)
1178. 	update_inventory();
1179. }
1180. 
1181. #endif /* OVL0 */
1182. #ifdef OVL1
1183. 
1184. void
1185. do_storms()
1186. {
1187.     int nstrike;
1188.     register int x, y;
1189.     int dirx, diry;
1190.     int count;
1191. 
1192.     /* no lightning if not the air level or too often, even then */
1193.     if(!Is_airlevel(&u.uz) || rn2(8))
1194. 	return;
1195. 
1196.     /* the number of strikes is 8-log2(nstrike) */
1197.     for(nstrike = rnd(64); nstrike <= 64; nstrike *= 2) {
1198. 	count = 0;
1199. 	do {
1200. 	    x = rnd(COLNO-1);
1201. 	    y = rn2(ROWNO);
1202. 	} while (++count < 100 && levl[x][y].typ != CLOUD);
1203. 
1204. 	if(count < 100) {
1205. 	    dirx = rn2(3) - 1;
1206. 	    diry = rn2(3) - 1;
1207. 	    if(dirx != 0 || diry != 0)
1208. 		buzz(-15, /* "monster" LIGHTNING spell */
1209. 		     8, x, y, dirx, diry);
1210. 	}
1211.     }
1212. 
1213.     if(levl[u.ux][u.uy].typ == CLOUD) {
1214. 	/* inside a cloud during a thunder storm is deafening */
1215. 	pline("Kaboom!!!  Boom!!  Boom!!");
1216. 	if(!u.uinvulnerable) {
1217. 	    stop_occupation();
1218. 	    nomul(-3);
1219. 	}
1220.     } else
1221. 	You_hear("a rumbling noise.");
1222. }
1223. #endif /* OVL1 */
1224. 
1225. 
1226. #ifdef OVL0
1227. /* ------------------------------------------------------------------------- */
1228. /*
1229.  * Generic Timeout Functions.
1230.  *
1231.  * Interface:
1232.  *
1233.  * General:
1234.  *	boolean start_timer(long timeout,short kind,short func_index,
1235.  *							genericptr_t arg)
1236.  *		Start a timer of kind 'kind' that will expire at time
1237.  *		monstermoves+'timeout'.  Call the function at 'func_index'
1238.  *		in the timeout table using argument 'arg'.  Return TRUE if
1239.  *		a timer was started.  This places the timer on a list ordered
1240.  *		"sooner" to "later".  If an object, increment the object's
1241.  *		timer count.
1242.  *
1243.  *	long stop_timer(short func_index, genericptr_t arg)
1244.  *		Stop a timer specified by the (func_index, arg) pair.  This
1245.  *		assumes that such a pair is unique.  Return the time the
1246.  *		timer would have gone off.  If no timer is found, return 0.
1247.  *		If an object, decrement the object's timer count.
1248.  *
1249.  *	void run_timers(void)
1250.  *		Call timers that have timed out.
1251.  *
1252.  *
1253.  * Save/Restore:
1254.  *	void save_timers(int fd, int mode, int range)
1255.  *		Save all timers of range 'range'.  Range is either global
1256.  *		or local.  Global timers follow game play, local timers
1257.  *		are saved with a level.  Object and monster timers are
1258.  *		saved using their respective id's instead of pointers.
1259.  *
1260.  *	void restore_timers(int fd, int range, boolean ghostly, long adjust)
1261.  *		Restore timers of range 'range'.  If from a ghost pile,
1262.  *		adjust the timeout by 'adjust'.  The object and monster
1263.  *		ids are not restored until later.
1264.  *
1265.  *	void relink_timers(boolean ghostly)
1266.  *		Relink all object and monster timers that had been saved
1267.  *		using their object's or monster's id number.
1268.  *
1269.  * Object Specific:
1270.  *	void obj_move_timers(struct obj *src, struct obj *dest)
1271.  *		Reassign all timers from src to dest.
1272.  *
1273.  *	void obj_split_timers(struct obj *src, struct obj *dest)
1274.  *		Duplicate all timers assigned to src and attach them to dest.
1275.  *
1276.  *	void obj_stop_timers(struct obj *obj)
1277.  *		Stop all timers attached to obj.
1278.  */
1279. 
1280. #ifdef WIZARD
1281. STATIC_DCL const char *FDECL(kind_name, (SHORT_P));
1282. STATIC_DCL void FDECL(print_queue, (winid, timer_element *));
1283. #endif
1284. STATIC_DCL void FDECL(insert_timer, (timer_element *));
1285. STATIC_DCL timer_element *FDECL(remove_timer, (timer_element **, SHORT_P,
1286. 								genericptr_t));
1287. STATIC_DCL void FDECL(write_timer, (int, timer_element *));
1288. STATIC_DCL boolean FDECL(mon_is_local, (struct monst *));
1289. STATIC_DCL boolean FDECL(timer_is_local, (timer_element *));
1290. STATIC_DCL int FDECL(maybe_write_timer, (int, int, BOOLEAN_P));
1291. 
1292. /* ordered timer list */
1293. static timer_element *timer_base;		/* "active" */
1294. static unsigned long timer_id = 1;
1295. 
1296. /* If defined, then include names when printing out the timer queue */
1297. #define VERBOSE_TIMER
1298. 
1299. typedef struct {
1300.     timeout_proc f, cleanup;
1301. #ifdef VERBOSE_TIMER
1302.     const char *name;
1303. # define TTAB(a, b, c) {a,b,c}
1304. #else
1305. # define TTAB(a, b, c) {a,b}
1306. #endif
1307. } ttable;
1308. 
1309. /* table of timeout functions */
1310. static ttable timeout_funcs[NUM_TIME_FUNCS] = {
1311.     TTAB(rot_organic,	(timeout_proc)0,	"rot_organic"),
1312.     TTAB(rot_corpse,	(timeout_proc)0,	"rot_corpse"),
1313.     TTAB(revive_mon,	(timeout_proc)0,	"revive_mon"),
1314.     TTAB(burn_object,	cleanup_burn,		"burn_object"),
1315.     TTAB(hatch_egg,	(timeout_proc)0,	"hatch_egg"),
1316.     TTAB(fig_transform,	(timeout_proc)0,	"fig_transform")
1317. };
1318. #undef TTAB
1319. 
1320. 
1321. #if defined(WIZARD)
1322. 
1323. STATIC_OVL const char *
1324. kind_name(kind)
1325.     short kind;
1326. {
1327.     switch (kind) {
1328. 	case TIMER_LEVEL: return "level";
1329. 	case TIMER_GLOBAL: return "global";
1330. 	case TIMER_OBJECT: return "object";
1331. 	case TIMER_MONSTER: return "monster";
1332.     }
1333.     return "unknown";
1334. }
1335. 
1336. STATIC_OVL void
1337. print_queue(win, base)
1338.     winid win;
1339.     timer_element *base;
1340. {
1341.     timer_element *curr;
1342.     char buf[BUFSZ], arg_address[20];
1343. 
1344.     if (!base) {
1345. 	putstr(win, 0, "<empty>");
1346.     } else {
1347. 	putstr(win, 0, "timeout  id   kind   call");
1348. 	for (curr = base; curr; curr = curr->next) {
1349. #ifdef VERBOSE_TIMER
1350. 	    Sprintf(buf, " %4ld   %4ld  %-6s %s(%s)",
1351. 		curr->timeout, curr->tid, kind_name(curr->kind),
1352. 		timeout_funcs[curr->func_index].name,
1353. 		fmt_ptr((genericptr_t)curr->arg, arg_address));
1354. #else
1355. 	    Sprintf(buf, " %4ld   %4ld  %-6s #%d(%s)",
1356. 		curr->timeout, curr->tid, kind_name(curr->kind),
1357. 		curr->func_index,
1358. 		fmt_ptr((genericptr_t)curr->arg, arg_address));
1359. #endif
1360. 	    putstr(win, 0, buf);
1361. 	}
1362.     }
1363. }
1364. 
1365. int
1366. wiz_timeout_queue()
1367. {
1368.     winid win;
1369.     char buf[BUFSZ];
1370. 
1371.     win = create_nhwindow(NHW_MENU);	/* corner text window */
1372.     if (win == WIN_ERR) return 0;
1373. 
1374.     Sprintf(buf, "Current time = %ld.", monstermoves);
1375.     putstr(win, 0, buf);
1376.     putstr(win, 0, "");
1377.     putstr(win, 0, "Active timeout queue:");
1378.     putstr(win, 0, "");
1379.     print_queue(win, timer_base);
1380. 
1381.     display_nhwindow(win, FALSE);
1382.     destroy_nhwindow(win);
1383. 
1384.     return 0;
1385. }
1386. 
1387. void
1388. timer_sanity_check()
1389. {
1390.     timer_element *curr;
1391.     char obj_address[20];
1392. 
1393.     /* this should be much more complete */
1394.     for (curr = timer_base; curr; curr = curr->next)
1395. 	if (curr->kind == TIMER_OBJECT) {
1396. 	    struct obj *obj = (struct obj *) curr->arg;
1397. 	    if (obj->timed == 0) {
1398. 		pline("timer sanity: untimed obj %s, timer %ld",
1399. 		      fmt_ptr((genericptr_t)obj, obj_address), curr->tid);
1400. 	    }
1401. 	}
1402. }
1403. 
1404. #endif /* WIZARD */
1405. 
1406. 
1407. /*
1408.  * Pick off timeout elements from the global queue and call their functions.
1409.  * Do this until their time is less than or equal to the move count.
1410.  */
1411. void
1412. run_timers()
1413. {
1414.     timer_element *curr;
1415. 
1416.     /*
1417.      * Always use the first element.  Elements may be added or deleted at
1418.      * any time.  The list is ordered, we are done when the first element
1419.      * is in the future.
1420.      */
1421.     while (timer_base && timer_base->timeout <= monstermoves) {
1422. 	curr = timer_base;
1423. 	timer_base = curr->next;
1424. 
1425. 	if (curr->kind == TIMER_OBJECT) ((struct obj *)(curr->arg))->timed--;
1426. 	(*timeout_funcs[curr->func_index].f)(curr->arg, curr->timeout);
1427. 	free((genericptr_t) curr);
1428.     }
1429. }
1430. 
1431. 
1432. /*
1433.  * Start a timer.  Return TRUE if successful.
1434.  */
1435. boolean
1436. start_timer(when, kind, func_index, arg)
1437. long when;
1438. short kind;
1439. short func_index;
1440. genericptr_t arg;
1441. {
1442.     timer_element *gnu;
1443. 
1444.     if (func_index < 0 || func_index >= NUM_TIME_FUNCS)
1445. 	panic("start_timer");
1446. 
1447.     gnu = (timer_element *) alloc(sizeof(timer_element));
1448.     gnu->next = 0;
1449.     gnu->tid = timer_id++;
1450.     gnu->timeout = monstermoves + when;
1451.     gnu->kind = kind;
1452.     gnu->needs_fixup = 0;
1453.     gnu->func_index = func_index;
1454.     gnu->arg = arg;
1455.     insert_timer(gnu);
1456. 
1457.     if (kind == TIMER_OBJECT)	/* increment object's timed count */
1458. 	((struct obj *)arg)->timed++;
1459. 
1460.     /* should check for duplicates and fail if any */
1461.     return TRUE;
1462. }
1463. 
1464. 
1465. /*
1466.  * Remove the timer from the current list and free it up.  Return the time
1467.  * it would have gone off, 0 if not found.
1468.  */
1469. long
1470. stop_timer(func_index, arg)
1471. short func_index;
1472. genericptr_t arg;
1473. {
1474.     timer_element *doomed;
1475.     long timeout;
1476. 
1477.     doomed = remove_timer(&timer_base, func_index, arg);
1478. 
1479.     if (doomed) {
1480. 	timeout = doomed->timeout;
1481. 	if (doomed->kind == TIMER_OBJECT)
1482. 	    ((struct obj *)arg)->timed--;
1483. 	if (timeout_funcs[doomed->func_index].cleanup)
1484. 	    (*timeout_funcs[doomed->func_index].cleanup)(arg, timeout);
1485. 	free((genericptr_t) doomed);
1486. 	return timeout;
1487.     }
1488.     return 0;
1489. }
1490. 
1491. 
1492. /*
1493.  * Move all object timers from src to dest, leaving src untimed.
1494.  */
1495. void
1496. obj_move_timers(src, dest)
1497.     struct obj *src, *dest;
1498. {
1499.     int count;
1500.     timer_element *curr;
1501. 
1502.     for (count = 0, curr = timer_base; curr; curr = curr->next)
1503. 	if (curr->kind == TIMER_OBJECT && curr->arg == (genericptr_t)src) {
1504. 	    curr->arg = (genericptr_t) dest;
1505. 	    dest->timed++;
1506. 	    count++;
1507. 	}
1508.     if (count != src->timed)
1509. 	panic("obj_move_timers");
1510.     src->timed = 0;
1511. }
1512. 
1513. 
1514. /*
1515.  * Find all object timers and duplicate them for the new object "dest".
1516.  */
1517. void
1518. obj_split_timers(src, dest)
1519.     struct obj *src, *dest;
1520. {
1521.     timer_element *curr, *next_timer=0;
1522. 
1523.     for (curr = timer_base; curr; curr = next_timer) {
1524. 	next_timer = curr->next;	/* things may be inserted */
1525. 	if (curr->kind == TIMER_OBJECT && curr->arg == (genericptr_t)src) {
1526. 	    (void) start_timer(curr->timeout-monstermoves, TIMER_OBJECT,
1527. 					curr->func_index, (genericptr_t)dest);
1528. 	}
1529.     }
1530. }
1531. 
1532. 
1533. /*
1534.  * Stop all timers attached to this object.  We can get away with this because
1535.  * all object pointers are unique.
1536.  */
1537. void
1538. obj_stop_timers(obj)
1539.     struct obj *obj;
1540. {
1541.     timer_element *curr, *prev, *next_timer=0;
1542. 
1543.     for (prev = 0, curr = timer_base; curr; curr = next_timer) {
1544. 	next_timer = curr->next;
1545. 	if (curr->kind == TIMER_OBJECT && curr->arg == (genericptr_t)obj) {
1546. 	    if (prev)
1547. 		prev->next = curr->next;
1548. 	    else
1549. 		timer_base = curr->next;
1550. 	    if (timeout_funcs[curr->func_index].cleanup)
1551. 		(*timeout_funcs[curr->func_index].cleanup)(curr->arg,
1552. 			curr->timeout);
1553. 	    free((genericptr_t) curr);
1554. 	} else {
1555. 	    prev = curr;
1556. 	}
1557.     }
1558.     obj->timed = 0;
1559. }
1560. 
1561. 
1562. /* Insert timer into the global queue */
1563. STATIC_OVL void
1564. insert_timer(gnu)
1565.     timer_element *gnu;
1566. {
1567.     timer_element *curr, *prev;
1568. 
1569.     for (prev = 0, curr = timer_base; curr; prev = curr, curr = curr->next)
1570. 	if (curr->timeout >= gnu->timeout) break;
1571. 
1572.     gnu->next = curr;
1573.     if (prev)
1574. 	prev->next = gnu;
1575.     else
1576. 	timer_base = gnu;
1577. }
1578. 
1579. 
1580. STATIC_OVL timer_element *
1581. remove_timer(base, func_index, arg)
1582. timer_element **base;
1583. short func_index;
1584. genericptr_t arg;
1585. {
1586.     timer_element *prev, *curr;
1587. 
1588.     for (prev = 0, curr = *base; curr; prev = curr, curr = curr->next)
1589. 	if (curr->func_index == func_index && curr->arg == arg) break;
1590. 
1591.     if (curr) {
1592. 	if (prev)
1593. 	    prev->next = curr->next;
1594. 	else
1595. 	    *base = curr->next;
1596.     }
1597. 
1598.     return curr;
1599. }
1600. 
1601. 
1602. STATIC_OVL void
1603. write_timer(fd, timer)
1604.     int fd;
1605.     timer_element *timer;
1606. {
1607.     genericptr_t arg_save;
1608. 
1609.     switch (timer->kind) {
1610. 	case TIMER_GLOBAL:
1611. 	case TIMER_LEVEL:
1612. 	    /* assume no pointers in arg */
1613. 	    bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
1614. 	    break;
1615. 
1616. 	case TIMER_OBJECT:
1617. 	    if (timer->needs_fixup)
1618. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
1619. 	    else {
1620. 		/* replace object pointer with id */
1621. 		arg_save = timer->arg;
1622. 		timer->arg = (genericptr_t)((struct obj *)timer->arg)->o_id;
1623. 		timer->needs_fixup = 1;
1624. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
1625. 		timer->arg = arg_save;
1626. 		timer->needs_fixup = 0;
1627. 	    }
1628. 	    break;
1629. 
1630. 	case TIMER_MONSTER:
1631. 	    if (timer->needs_fixup)
1632. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
1633. 	    else {
1634. 		/* replace monster pointer with id */
1635. 		arg_save = timer->arg;
1636. 		timer->arg = (genericptr_t)((struct monst *)timer->arg)->m_id;
1637. 		timer->needs_fixup = 1;
1638. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
1639. 		timer->arg = arg_save;
1640. 		timer->needs_fixup = 0;
1641. 	    }
1642. 	    break;
1643. 
1644. 	default:
1645. 	    panic("write_timer");
1646. 	    break;
1647.     }
1648. }
1649. 
1650. 
1651. /*
1652.  * Return TRUE if the object will stay on the level when the level is
1653.  * saved.
1654.  */
1655. boolean
1656. obj_is_local(obj)
1657.     struct obj *obj;
1658. {
1659.     switch (obj->where) {
1660. 	case OBJ_INVENT:
1661. 	case OBJ_MIGRATING:	return FALSE;
1662. 	case OBJ_FLOOR:
1663. 	case OBJ_BURIED:	return TRUE;
1664. 	case OBJ_CONTAINED:	return obj_is_local(obj->ocontainer);
1665. 	case OBJ_MINVENT:	return mon_is_local(obj->ocarry);
1666.     }
1667.     panic("obj_is_local");
1668.     return FALSE;
1669. }
1670. 
1671. 
1672. /*
1673.  * Return TRUE if the given monster will stay on the level when the
1674.  * level is saved.
1675.  */
1676. STATIC_OVL boolean
1677. mon_is_local(mon)
1678. struct monst *mon;
1679. {
1680.     struct monst *curr;
1681. 
1682.     for (curr = migrating_mons; curr; curr = curr->nmon)
1683. 	if (curr == mon) return FALSE;
1684.     /* `mydogs' is used during level changes, never saved and restored */
1685.     for (curr = mydogs; curr; curr = curr->nmon)
1686. 	if (curr == mon) return FALSE;
1687.     return TRUE;
1688. }
1689. 
1690. 
1691. /*
1692.  * Return TRUE if the timer is attached to something that will stay on the
1693.  * level when the level is saved.
1694.  */
1695. STATIC_OVL boolean
1696. timer_is_local(timer)
1697.     timer_element *timer;
1698. {
1699.     switch (timer->kind) {
1700. 	case TIMER_LEVEL:	return TRUE;
1701. 	case TIMER_GLOBAL:	return FALSE;
1702. 	case TIMER_OBJECT:	return obj_is_local((struct obj *)timer->arg);
1703. 	case TIMER_MONSTER:	return mon_is_local((struct monst *)timer->arg);
1704.     }
1705.     panic("timer_is_local");
1706.     return FALSE;
1707. }
1708. 
1709. 
1710. /*
1711.  * Part of the save routine.  Count up the number of timers that would
1712.  * be written.  If write_it is true, actually write the timer.
1713.  */
1714. STATIC_OVL int
1715. maybe_write_timer(fd, range, write_it)
1716.     int fd, range;
1717.     boolean write_it;
1718. {
1719.     int count = 0;
1720.     timer_element *curr;
1721. 
1722.     for (curr = timer_base; curr; curr = curr->next) {
1723. 	if (range == RANGE_GLOBAL) {
1724. 	    /* global timers */
1725. 
1726. 	    if (!timer_is_local(curr)) {
1727. 		count++;
1728. 		if (write_it) write_timer(fd, curr);
1729. 	    }
1730. 
1731. 	} else {
1732. 	    /* local timers */
1733. 
1734. 	    if (timer_is_local(curr)) {
1735. 		count++;
1736. 		if (write_it) write_timer(fd, curr);
1737. 	    }
1738. 
1739. 	}
1740.     }
1741. 
1742.     return count;
1743. }
1744. 
1745. 
1746. /*
1747.  * Save part of the timer list.  The parameter 'range' specifies either
1748.  * global or level timers to save.  The timer ID is saved with the global
1749.  * timers.
1750.  *
1751.  * Global range:
1752.  *		+ timeouts that follow the hero (global)
1753.  *		+ timeouts that follow obj & monst that are migrating
1754.  *
1755.  * Level range:
1756.  *		+ timeouts that are level specific (e.g. storms)
1757.  *		+ timeouts that stay with the level (obj & monst)
1758.  */
1759. void
1760. save_timers(fd, mode, range)
1761.     int fd, mode, range;
1762. {
1763.     timer_element *curr, *prev, *next_timer=0;
1764.     int count;
1765. 
1766.     if (perform_bwrite(mode)) {
1767. 	if (range == RANGE_GLOBAL)
1768. 	    bwrite(fd, (genericptr_t) &timer_id, sizeof(timer_id));
1769. 
1770. 	count = maybe_write_timer(fd, range, FALSE);
1771. 	bwrite(fd, (genericptr_t) &count, sizeof count);
1772. 	(void) maybe_write_timer(fd, range, TRUE);
1773.     }
1774. 
1775.     if (release_data(mode)) {
1776. 	for (prev = 0, curr = timer_base; curr; curr = next_timer) {
1777. 	    next_timer = curr->next;	/* in case curr is removed */
1778. 
1779. 	    if ( !(!!(range == RANGE_LEVEL) ^ !!timer_is_local(curr)) ) {
1780. 		if (prev)
1781. 		    prev->next = curr->next;
1782. 		else
1783. 		    timer_base = curr->next;
1784. 		free((genericptr_t) curr);
1785. 		/* prev stays the same */
1786. 	    } else {
1787. 		prev = curr;
1788. 	    }
1789. 	}
1790.     }
1791. }
1792. 
1793. 
1794. /*
1795.  * Pull in the structures from disk, but don't recalculate the object and
1796.  * monster pointers.
1797.  */
1798. void
1799. restore_timers(fd, range, ghostly, adjust)
1800.     int fd, range;
1801.     boolean ghostly;	/* restoring from a ghost level */
1802.     long adjust;	/* how much to adjust timeout */
1803. {
1804.     int count;
1805.     timer_element *curr;
1806. 
1807.     if (range == RANGE_GLOBAL)
1808. 	mread(fd, (genericptr_t) &timer_id, sizeof timer_id);
1809. 
1810.     /* restore elements */
1811.     mread(fd, (genericptr_t) &count, sizeof count);
1812.     while (count-- > 0) {
1813. 	curr = (timer_element *) alloc(sizeof(timer_element));
1814. 	mread(fd, (genericptr_t) curr, sizeof(timer_element));
1815. 	if (ghostly)
1816. 	    curr->timeout += adjust;
1817. 	insert_timer(curr);
1818.     }
1819. }
1820. 
1821. 
1822. /* reset all timers that are marked for reseting */
1823. void
1824. relink_timers(ghostly)
1825.     boolean ghostly;
1826. {
1827.     timer_element *curr;
1828.     unsigned nid;
1829. 
1830.     for (curr = timer_base; curr; curr = curr->next) {
1831. 	if (curr->needs_fixup) {
1832. 	    if (curr->kind == TIMER_OBJECT) {
1833. 		if (ghostly) {
1834. 		    if (!lookup_id_mapping((unsigned)curr->arg, &nid))
1835. 			panic("relink_timers 1");
1836. 		} else
1837. 		    nid = (unsigned) curr->arg;
1838. 		curr->arg = (genericptr_t) find_oid(nid);
1839. 		if (!curr->arg) panic("cant find o_id %d", nid);
1840. 		curr->needs_fixup = 0;
1841. 	    } else if (curr->kind == TIMER_MONSTER) {
1842. 		panic("relink_timers: no monster timer implemented");
1843. 	    } else
1844. 		panic("relink_timers 2");
1845. 	}
1846.     }
1847. }
1848. 
1849. #endif /* OVL0 */
1850. 
1851. /*timeout.c*/

Also on Fandom

Random Wiki