Source:NetHack 3.4.3/src/timeout.c

From NetHackWiki
(Redirected from Source:Ref/print queue)
Jump to: navigation, search

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

Top of file[edit]

  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. */

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. #include "hack.h"
  2. #include "lev.h"	/* for checking save modes */
  3.  
  4. STATIC_DCL void NDECL(stoned_dialogue);
  5. STATIC_DCL void NDECL(vomiting_dialogue);
  6. STATIC_DCL void NDECL(choke_dialogue);
  7. STATIC_DCL void NDECL(slime_dialogue);
  8. STATIC_DCL void NDECL(slip_or_trip);
  9. STATIC_DCL void FDECL(see_lamp_flicker, (struct obj *, const char *));
  10. STATIC_DCL void FDECL(lantern_message, (struct obj *));
  11. STATIC_DCL void FDECL(cleanup_burn, (genericptr_t,long));

stoned_dialogue[edit]

  1. #ifdef OVLB
  2.  
  3. /* He is being petrified - dialogue by inmet!tower */
  4. static NEARDATA const char * const stoned_texts[] = {
  5. 	"You are slowing down.",		/* 5 */
  6. 	"Your limbs are stiffening.",		/* 4 */
  7. 	"Your limbs have turned to stone.",	/* 3 */
  8. 	"You have turned to stone.",		/* 2 */
  9. 	"You are a statue."			/* 1 */
  10. };
  11.  
  12. STATIC_OVL void
  13. stoned_dialogue()
  14. {
  15. 	register long i = (Stoned & TIMEOUT);
  16.  
  17. 	if (i > 0L && i <= SIZE(stoned_texts))
  18. 		pline(stoned_texts[SIZE(stoned_texts) - i]);
  19. 	if (i == 5L)
  20. 		HFast = 0L;
  21. 	if (i == 3L)
  22. 		nomul(-3);
  23. 	exercise(A_DEX, FALSE);
  24. }

vomiting_dialogue[edit]

  1. /* He is getting sicker and sicker prior to vomiting */
  2. static NEARDATA const char * const vomiting_texts[] = {
  3. 	"are feeling mildly nauseated.",	/* 14 */
  4. 	"feel slightly confused.",		/* 11 */
  5. 	"can't seem to think straight.",	/* 8 */
  6. 	"feel incredibly sick.",		/* 5 */
  7. 	"suddenly vomit!"			/* 2 */
  8. };
  9.  
  10. STATIC_OVL void
  11. vomiting_dialogue()
  12. {
  13. 	register long i = (Vomiting & TIMEOUT) / 3L;
  14.  
  15. 	if ((((Vomiting & TIMEOUT) % 3L) == 2) && (i >= 0)
  16. 	    && (i < SIZE(vomiting_texts)))
  17. 		You(vomiting_texts[SIZE(vomiting_texts) - i - 1]);
  18.  
  19. 	switch ((int) i) {
  20. 	case 0:
  21. 		vomit();
  22. 		morehungry(20);
  23. 		break;
  24. 	case 2:
  25. 		make_stunned(HStun + d(2,4), FALSE);
  26. 		/* fall through */
  27. 	case 3:
  28. 		make_confused(HConfusion + d(2,4), FALSE);
  29. 		break;
  30. 	}
  31. 	exercise(A_CON, FALSE);
  32. }

choke_dialogue[edit]

  1. static NEARDATA const char * const choke_texts[] = {
  2. 	"You find it hard to breathe.",
  3. 	"You're gasping for air.",
  4. 	"You can no longer breathe.",
  5. 	"You're turning %s.",
  6. 	"You suffocate."
  7. };
  8.  
  9. static NEARDATA const char * const choke_texts2[] = {
  10. 	"Your %s is becoming constricted.",
  11. 	"Your blood is having trouble reaching your brain.",
  12. 	"The pressure on your %s increases.",
  13. 	"Your consciousness is fading.",
  14. 	"You suffocate."
  15. };
  16.  
  17. STATIC_OVL void
  18. choke_dialogue()
  19. {
  20. 	register long i = (Strangled & TIMEOUT);
  21.  
  22. 	if(i > 0 && i <= SIZE(choke_texts)) {
  23. 	    if (Breathless || !rn2(50))
  24. 		pline(choke_texts2[SIZE(choke_texts2) - i], body_part(NECK));
  25. 	    else {
  26. 		const char *str = choke_texts[SIZE(choke_texts)-i];
  27.  
  28. 		if (index(str, '%'))
  29. 		    pline(str, hcolor(NH_BLUE));
  30. 		else
  31. 		    pline(str);
  32. 	    }
  33. 	}
  34. 	exercise(A_STR, FALSE);
  35. }

slime_dialogue[edit]

  1. static NEARDATA const char * const slime_texts[] = {
  2. 	"You are turning a little %s.",           /* 5 */
  3. 	"Your limbs are getting oozy.",              /* 4 */
  4. 	"Your skin begins to peel away.",            /* 3 */
  5. 	"You are turning into %s.",       /* 2 */
  6. 	"You have become %s."             /* 1 */
  7. };
  8.  
  9. STATIC_OVL void
  10. slime_dialogue()
  11. {
  12. 	register long i = (Slimed & TIMEOUT) / 2L;
  13.  
  14. 	if (((Slimed & TIMEOUT) % 2L) && i >= 0L
  15. 		&& i < SIZE(slime_texts)) {
  16. 	    const char *str = slime_texts[SIZE(slime_texts) - i - 1L];
  17.  
  18. 	    if (index(str, '%')) {
  19. 		if (i == 4L) {	/* "you are turning green" */
  20. 		    if (!Blind)	/* [what if you're already green?] */
  21. 			pline(str, hcolor(NH_GREEN));
  22. 		} else
  23. 		    pline(str, an(Hallucination ? rndmonnam() : "green slime"));
  24. 	    } else
  25. 		pline(str);
  26. 	}
  27. 	if (i == 3L) {	/* limbs becoming oozy */
  28. 	    HFast = 0L;	/* lose intrinsic speed */
  29. 	    stop_occupation();
  30. 	    if (multi > 0) nomul(0);
  31. 	}
  32. 	exercise(A_DEX, FALSE);
  33. }

burn_away_slime[edit]

  1. void
  2. burn_away_slime()
  3. {
  4. 	if (Slimed) {
  5. 	    pline_The("slime that covers you is burned away!");
  6. 	    Slimed = 0L;
  7. 	    flags.botl = 1;
  8. 	}
  9. 	return;
  10. }
  11.  
  12.  
  13. #endif /* OVLB */

nh_timeout[edit]

  1. #ifdef OVL0
  2.  
  3. void
  4. nh_timeout()
  5. {
  6. 	register struct prop *upp;
  7. 	int sleeptime;
  8. 	int m_idx;
  9. 	int baseluck = (flags.moonphase == FULL_MOON) ? 1 : 0;
  10.  
  11. 	if (flags.friday13) baseluck -= 1;
  12.  
  13. 	if (u.uluck != baseluck &&
  14. 		moves % (u.uhave.amulet || u.ugangr ? 300 : 600) == 0) {
  15. 	/* Cursed luckstones stop bad luck from timing out; blessed luckstones
  16. 	 * stop good luck from timing out; normal luckstones stop both;
  17. 	 * neither is stopped if you don't have a luckstone.
  18. 	 * Luck is based at 0 usually, +1 if a full moon and -1 on Friday 13th
  19. 	 */
  20. 	    register int time_luck = stone_luck(FALSE);
  21. 	    boolean nostone = !carrying(LUCKSTONE) && !stone_luck(TRUE);
  22.  
  23. 	    if(u.uluck > baseluck && (nostone || time_luck < 0))
  24. 		u.uluck--;
  25. 	    else if(u.uluck < baseluck && (nostone || time_luck > 0))
  26. 		u.uluck++;
  27. 	}
  28. 	if(u.uinvulnerable) return; /* things past this point could kill you */
  29. 	if(Stoned) stoned_dialogue();
  30. 	if(Slimed) slime_dialogue();
  31. 	if(Vomiting) vomiting_dialogue();
  32. 	if(Strangled) choke_dialogue();
  33. 	if(u.mtimedone && !--u.mtimedone) {
  34. 		if (Unchanging)
  35. 			u.mtimedone = rnd(100*youmonst.data->mlevel + 1);
  36. 		else
  37. 			rehumanize();
  38. 	}
  39. 	if(u.ucreamed) u.ucreamed--;
  40.  
  41. 	/* Dissipate spell-based protection. */
  42. 	if (u.usptime) {
  43. 	    if (--u.usptime == 0 && u.uspellprot) {
  44. 		u.usptime = u.uspmtime;
  45. 		u.uspellprot--;
  46. 		find_ac();
  47. 		if (!Blind)
  48. 		    Norep("The %s haze around you %s.", hcolor(NH_GOLDEN),
  49. 			  u.uspellprot ? "becomes less dense" : "disappears");
  50. 	    }
  51. 	}
  52.  
  53. #ifdef STEED
  54. 	if (u.ugallop) {
  55. 	    if (--u.ugallop == 0L && u.usteed)
  56. 	    	pline("%s stops galloping.", Monnam(u.usteed));
  57. 	}
  58. #endif
  59.  
  60. 	for(upp = u.uprops; upp < u.uprops+SIZE(u.uprops); upp++)
  61. 	    if((upp->intrinsic & TIMEOUT) && !(--upp->intrinsic & TIMEOUT)) {
  62. 		switch(upp - u.uprops){
  63. 		case STONED:
  64. 			if (delayed_killer && !killer) {
  65. 				killer = delayed_killer;
  66. 				delayed_killer = 0;
  67. 			}
  68. 			if (!killer) {
  69. 				/* leaving killer_format would make it
  70. 				   "petrified by petrification" */
  71. 				killer_format = NO_KILLER_PREFIX;
  72. 				killer = "killed by petrification";
  73. 			}
  74. 			done(STONING);
  75. 			break;
  76. 		case SLIMED:
  77. 			if (delayed_killer && !killer) {
  78. 				killer = delayed_killer;
  79. 				delayed_killer = 0;
  80. 			}
  81. 			if (!killer) {
  82. 				killer_format = NO_KILLER_PREFIX;
  83. 				killer = "turned into green slime";
  84. 			}
  85. 			done(TURNED_SLIME);
  86. 			break;
  87. 		case VOMITING:
  88. 			make_vomiting(0L, TRUE);
  89. 			break;
  90. 		case SICK:
  91. 			You("die from your illness.");
  92. 			killer_format = KILLED_BY_AN;
  93. 			killer = u.usick_cause;
  94. 			if ((m_idx = name_to_mon(killer)) >= LOW_PM) {
  95. 			    if (type_is_pname(&mons[m_idx])) {
  96. 				killer_format = KILLED_BY;
  97. 			    } else if (mons[m_idx].geno & G_UNIQ) {
  98. 				killer = the(killer);
  99. 				Strcpy(u.usick_cause, killer);
  100. 				killer_format = KILLED_BY;
  101. 			    }
  102. 			}
  103. 			u.usick_type = 0;
  104. 			done(POISONING);
  105. 			break;
  106. 		case FAST:
  107. 			if (!Very_fast)
  108. 				You_feel("yourself slowing down%s.",
  109. 							Fast ? " a bit" : "");
  110. 			break;
  111. 		case CONFUSION:
  112. 			HConfusion = 1; /* So make_confused works properly */
  113. 			make_confused(0L, TRUE);
  114. 			stop_occupation();
  115. 			break;
  116. 		case STUNNED:
  117. 			HStun = 1;
  118. 			make_stunned(0L, TRUE);
  119. 			stop_occupation();
  120. 			break;
  121. 		case BLINDED:
  122. 			Blinded = 1;
  123. 			make_blinded(0L, TRUE);
  124. 			stop_occupation();
  125. 			break;
  126. 		case INVIS:
  127. 			newsym(u.ux,u.uy);
  128. 			if (!Invis && !BInvis && !Blind) {
  129. 			    You(!See_invisible ?
  130. 				    "are no longer invisible." :
  131. 				    "can no longer see through yourself.");
  132. 			    stop_occupation();
  133. 			}
  134. 			break;
  135. 		case SEE_INVIS:
  136. 			set_mimic_blocking(); /* do special mimic handling */
  137. 			see_monsters();		/* make invis mons appear */
  138. 			newsym(u.ux,u.uy);	/* make self appear */
  139. 			stop_occupation();
  140. 			break;
  141. 		case WOUNDED_LEGS:
  142. 			heal_legs();
  143. 			stop_occupation();
  144. 			break;
  145. 		case HALLUC:
  146. 			HHallucination = 1;
  147. 			(void) make_hallucinated(0L, TRUE, 0L);
  148. 			stop_occupation();
  149. 			break;
  150. 		case SLEEPING:
  151. 			if (unconscious() || Sleep_resistance)
  152. 				HSleeping += rnd(100);
  153. 			else if (Sleeping) {
  154. 				You("fall asleep.");
  155. 				sleeptime = rnd(20);
  156. 				fall_asleep(-sleeptime, TRUE);
  157. 				HSleeping += sleeptime + rnd(100);
  158. 			}
  159. 			break;
  160. 		case LEVITATION:
  161. 			(void) float_down(I_SPECIAL|TIMEOUT, 0L);
  162. 			break;
  163. 		case STRANGLED:
  164. 			killer_format = KILLED_BY;
  165. 			killer = (u.uburied) ? "suffocation" : "strangulation";
  166. 			done(DIED);
  167. 			break;
  168. 		case FUMBLING:
  169. 			/* call this only when a move took place.  */
  170. 			/* otherwise handle fumbling msgs locally. */
  171. 			if (u.umoved && !Levitation) {
  172. 			    slip_or_trip();
  173. 			    nomul(-2);
  174. 			    nomovemsg = "";
  175. 			    /* The more you are carrying the more likely you
  176. 			     * are to make noise when you fumble.  Adjustments
  177. 			     * to this number must be thoroughly play tested.
  178. 			     */
  179. 			    if ((inv_weight() > -500)) {
  180. 				You("make a lot of noise!");
  181. 				wake_nearby();
  182. 			    }
  183. 			}
  184. 			/* from outside means slippery ice; don't reset
  185. 			   counter if that's the only fumble reason */
  186. 			HFumbling &= ~FROMOUTSIDE;
  187. 			if (Fumbling)
  188. 			    HFumbling += rnd(20);
  189. 			break;
  190. 		case DETECT_MONSTERS:
  191. 			see_monsters();
  192. 			break;
  193. 		}
  194. 	}
  195.  
  196. 	run_timers();
  197. }
  198.  
  199. #endif /* OVL0 */

fall_asleep[edit]

  1. #ifdef OVL1
  2.  
  3. void
  4. fall_asleep(how_long, wakeup_msg)
  5. int how_long;
  6. boolean wakeup_msg;
  7. {
  8. 	stop_occupation();
  9. 	nomul(how_long);
  10. 	/* generally don't notice sounds while sleeping */
  11. 	if (wakeup_msg && multi == how_long) {
  12. 	    /* caller can follow with a direct call to Hear_again() if
  13. 	       there's a need to override this when wakeup_msg is true */
  14. 	    flags.soundok = 0;
  15. 	    afternmv = Hear_again;	/* this won't give any messages */
  16. 	}
  17. 	/* early wakeup from combat won't be possible until next monster turn */
  18. 	u.usleep = monstermoves;
  19. 	nomovemsg = wakeup_msg ? "You wake up." : You_can_move_again;
  20. }

attach_egg_hatch_timeout[edit]

  1. /* Attach an egg hatch timeout to the given egg. */
  2. void
  3. attach_egg_hatch_timeout(egg)
  4. struct obj *egg;
  5. {
  6. 	int i;
  7.  
  8. 	/* stop previous timer, if any */
  9. 	(void) stop_timer(HATCH_EGG, (genericptr_t) egg);
  10.  
  11. 	/*
  12. 	 * Decide if and when to hatch the egg.  The old hatch_it() code tried
  13. 	 * once a turn from age 151 to 200 (inclusive), hatching if it rolled
  14. 	 * a number x, 1<=x<=age, where x>150.  This yields a chance of
  15. 	 * hatching > 99.9993%.  Mimic that here.
  16. 	 */
  17. 	for (i = (MAX_EGG_HATCH_TIME-50)+1; i <= MAX_EGG_HATCH_TIME; i++)
  18. 	    if (rnd(i) > 150) {
  19. 		/* egg will hatch */
  20. 		(void) start_timer((long)i, TIMER_OBJECT,
  21. 						HATCH_EGG, (genericptr_t)egg);
  22. 		break;
  23. 	    }
  24. }

kill_egg[edit]

  1. /* prevent an egg from ever hatching */
  2. void
  3. kill_egg(egg)
  4. struct obj *egg;
  5. {
  6. 	/* stop previous timer, if any */
  7. 	(void) stop_timer(HATCH_EGG, (genericptr_t) egg);
  8. }

hatch_egg[edit]

  1. /* timer callback routine: hatch the given egg */
  2. void
  3. hatch_egg(arg, timeout)
  4. genericptr_t arg;
  5. long timeout;
  6. {
  7. 	struct obj *egg;
  8. 	struct monst *mon, *mon2;
  9. 	coord cc;
  10. 	xchar x, y;
  11. 	boolean yours, silent, knows_egg = FALSE;
  12. 	boolean cansee_hatchspot = FALSE;
  13. 	int i, mnum, hatchcount = 0;
  14.  
  15. 	egg = (struct obj *) arg;
  16. 	/* sterilized while waiting */
  17. 	if (egg->corpsenm == NON_PM) return;
  18.  
  19. 	mon = mon2 = (struct monst *)0;
  20. 	mnum = big_to_little(egg->corpsenm);
  21. 	/* The identity of one's father is learned, not innate */
  22. 	yours = (egg->spe || (!flags.female && carried(egg) && !rn2(2)));
  23. 	silent = (timeout != monstermoves);	/* hatched while away */
  24.  
  25. 	/* only can hatch when in INVENT, FLOOR, MINVENT */
  26. 	if (get_obj_location(egg, &x, &y, 0)) {
  27. 	    hatchcount = rnd((int)egg->quan);
  28. 	    cansee_hatchspot = cansee(x, y) && !silent;
  29. 	    if (!(mons[mnum].geno & G_UNIQ) &&
  30. 		   !(mvitals[mnum].mvflags & (G_GENOD | G_EXTINCT))) {
  31. 		for (i = hatchcount; i > 0; i--) {
  32. 		    if (!enexto(&cc, x, y, &mons[mnum]) ||
  33. 			 !(mon = makemon(&mons[mnum], cc.x, cc.y, NO_MINVENT)))
  34. 			break;
  35. 		    /* tame if your own egg hatches while you're on the
  36. 		       same dungeon level, or any dragon egg which hatches
  37. 		       while it's in your inventory */
  38. 		    if ((yours && !silent) ||
  39. 			(carried(egg) && mon->data->mlet == S_DRAGON)) {
  40. 			if ((mon2 = tamedog(mon, (struct obj *)0)) != 0) {
  41. 			    mon = mon2;
  42. 			    if (carried(egg) && mon->data->mlet != S_DRAGON)
  43. 				mon->mtame = 20;
  44. 			}
  45. 		    }
  46. 		    if (mvitals[mnum].mvflags & G_EXTINCT)
  47. 			break;	/* just made last one */
  48. 		    mon2 = mon;	/* in case makemon() fails on 2nd egg */
  49. 		}
  50. 		if (!mon) mon = mon2;
  51. 		hatchcount -= i;
  52. 		egg->quan -= (long)hatchcount;
  53. 	    }
  54. 	}
  55. #if 0
  56. 	/*
  57. 	 * We could possibly hatch while migrating, but the code isn't
  58. 	 * set up for it...
  59. 	 */
  60. 	else if (obj->where == OBJ_MIGRATING) {
  61. 	    /*
  62. 	    We can do several things.  The first ones that come to
  63. 	    mind are:
  64.  
  65. 	    + Create the hatched monster then place it on the migrating
  66. 	      mons list.  This is tough because all makemon() is made
  67. 	      to place the monster as well.    Makemon() also doesn't
  68. 	      lend itself well to splitting off a "not yet placed"
  69. 	      subroutine.
  70.  
  71. 	    + Mark the egg as hatched, then place the monster when we
  72. 	      place the migrating objects.
  73.  
  74. 	    + Or just kill any egg which gets sent to another level.
  75. 	      Falling is the usual reason such transportation occurs.
  76. 	    */
  77. 	    cansee_hatchspot = FALSE;
  78. 	    mon = ???
  79. 	    }
  80. #endif
  81.  
  82. 	if (mon) {
  83. 	    char monnambuf[BUFSZ], carriedby[BUFSZ];
  84. 	    boolean siblings = (hatchcount > 1), redraw = FALSE;
  85.  
  86. 	    if (cansee_hatchspot) {
  87. 		Sprintf(monnambuf, "%s%s",
  88. 			siblings ? "some " : "",
  89. 			siblings ?
  90. 			makeplural(m_monnam(mon)) : an(m_monnam(mon)));
  91. 		/* we don't learn the egg type here because learning
  92. 		   an egg type requires either seeing the egg hatch
  93. 		   or being familiar with the egg already,
  94. 		   as well as being able to see the resulting
  95. 		   monster, checked below
  96. 		*/
  97. 	    }
  98. 	    switch (egg->where) {
  99. 		case OBJ_INVENT:
  100. 		    knows_egg = TRUE; /* true even if you are blind */
  101. 		    if (!cansee_hatchspot)
  102. 			You_feel("%s %s from your pack!", something,
  103. 			    locomotion(mon->data, "drop"));
  104. 		    else
  105. 			You("see %s %s out of your pack!",
  106. 			    monnambuf, locomotion(mon->data, "drop"));
  107. 		    if (yours) {
  108. 			pline("%s cries sound like \"%s%s\"",
  109. 			    siblings ? "Their" : "Its",
  110. 			    flags.female ? "mommy" : "daddy",
  111. 			    egg->spe ? "." : "?");
  112. 		    } else if (mon->data->mlet == S_DRAGON) {
  113. 			verbalize("Gleep!");		/* Mything eggs :-) */
  114. 		    }
  115. 		    break;
  116.  
  117. 		case OBJ_FLOOR:
  118. 		    if (cansee_hatchspot) {
  119. 			knows_egg = TRUE;
  120. 			You("see %s hatch.", monnambuf);
  121. 			redraw = TRUE;	/* update egg's map location */
  122. 		    }
  123. 		    break;
  124.  
  125. 		case OBJ_MINVENT:
  126. 		    if (cansee_hatchspot) {
  127. 			/* egg carring monster might be invisible */
  128. 			if (canseemon(egg->ocarry)) {
  129. 			    Sprintf(carriedby, "%s pack",
  130. 				     s_suffix(a_monnam(egg->ocarry)));
  131. 			    knows_egg = TRUE;
  132. 			}
  133. 			else if (is_pool(mon->mx, mon->my))
  134. 			    Strcpy(carriedby, "empty water");
  135. 			else
  136. 			    Strcpy(carriedby, "thin air");
  137. 			You("see %s %s out of %s!", monnambuf,
  138. 			    locomotion(mon->data, "drop"), carriedby);
  139. 		    }
  140. 		    break;
  141. #if 0
  142. 		case OBJ_MIGRATING:
  143. 		    break;
  144. #endif
  145. 		default:
  146. 		    impossible("egg hatched where? (%d)", (int)egg->where);
  147. 		    break;
  148. 	    }
  149.  
  150. 	    if (cansee_hatchspot && knows_egg)
  151. 		learn_egg_type(mnum);
  152.  
  153. 	    if (egg->quan > 0) {
  154. 		/* still some eggs left */
  155. 		attach_egg_hatch_timeout(egg);
  156. 		if (egg->timed) {
  157. 		    /* replace ordinary egg timeout with a short one */
  158. 		    (void) stop_timer(HATCH_EGG, (genericptr_t)egg);
  159. 		    (void) start_timer((long)rnd(12), TIMER_OBJECT,
  160. 					HATCH_EGG, (genericptr_t)egg);
  161. 		}
  162. 	    } else if (carried(egg)) {
  163. 		useup(egg);
  164. 	    } else {
  165. 		/* free egg here because we use it above */
  166. 		obj_extract_self(egg);
  167. 		obfree(egg, (struct obj *)0);
  168. 	    }
  169. 	    if (redraw) newsym(x, y);
  170. 	}
  171. }

learn_egg_type[edit]

  1. /* Learn to recognize eggs of the given type. */
  2. void
  3. learn_egg_type(mnum)
  4. int mnum;
  5. {
  6. 	/* baby monsters hatch from grown-up eggs */
  7. 	mnum = little_to_big(mnum);
  8. 	mvitals[mnum].mvflags |= MV_KNOWS_EGG;
  9. 	/* we might have just learned about other eggs being carried */
  10. 	update_inventory();
  11. }

attach_fig_transform_timeout[edit]

  1. /* Attach a fig_transform timeout to the given figurine. */
  2. void
  3. attach_fig_transform_timeout(figurine)
  4. struct obj *figurine;
  5. {
  6. 	int i;
  7.  
  8. 	/* stop previous timer, if any */
  9. 	(void) stop_timer(FIG_TRANSFORM, (genericptr_t) figurine);
  10.  
  11. 	/*
  12. 	 * Decide when to transform the figurine.
  13. 	 */
  14. 	i = rnd(9000) + 200;
  15. 	/* figurine will transform */
  16. 	(void) start_timer((long)i, TIMER_OBJECT,
  17. 				FIG_TRANSFORM, (genericptr_t)figurine);
  18. }

slip_or_trip[edit]

  1. /* give a fumble message */
  2. STATIC_OVL void
  3. slip_or_trip()
  4. {
  5. 	struct obj *otmp = vobj_at(u.ux, u.uy);
  6. 	const char *what, *pronoun;
  7. 	char buf[BUFSZ];
  8. 	boolean on_foot = TRUE;
  9. #ifdef STEED
  10. 	if (u.usteed) on_foot = FALSE;
  11. #endif
  12.  
  13. 	if (otmp && on_foot && !u.uinwater && is_pool(u.ux, u.uy)) otmp = 0;
  14.  
  15. 	if (otmp && on_foot) {		/* trip over something in particular */
  16. 	    /*
  17. 		If there is only one item, it will have just been named
  18. 		during the move, so refer to by via pronoun; otherwise,
  19. 		if the top item has been or can be seen, refer to it by
  20. 		name; if not, look for rocks to trip over; trip over
  21. 		anonymous "something" if there aren't any rocks.
  22. 	     */
  23. 	    pronoun = otmp->quan == 1L ? "it" : Hallucination ? "they" : "them";
  24. 	    what = !otmp->nexthere ? pronoun :
  25. 		  (otmp->dknown || !Blind) ? doname(otmp) :
  26. 		  ((otmp = sobj_at(ROCK, u.ux, u.uy)) == 0 ? something :
  27. 		  (otmp->quan == 1L ? "a rock" : "some rocks"));
  28. 	    if (Hallucination) {
  29. 		what = strcpy(buf, what);
  30. 		buf[0] = highc(buf[0]);
  31. 		pline("Egads!  %s bite%s your %s!",
  32. 			what, (!otmp || otmp->quan == 1L) ? "s" : "",
  33. 			body_part(FOOT));
  34. 	    } else {
  35. 		You("trip over %s.", what);
  36. 	    }
  37. 	} else if (rn2(3) && is_ice(u.ux, u.uy)) {
  38. 	    pline("%s %s%s on the ice.",
  39. #ifdef STEED
  40. 		u.usteed ? upstart(x_monnam(u.usteed,
  41. 				u.usteed->mnamelth ? ARTICLE_NONE : ARTICLE_THE,
  42. 				(char *)0, SUPPRESS_SADDLE, FALSE)) :
  43. #endif
  44. 		"You", rn2(2) ? "slip" : "slide", on_foot ? "" : "s");
  45. 	} else {
  46. 	    if (on_foot) {
  47. 		switch (rn2(4)) {
  48. 		  case 1:
  49. 			You("trip over your own %s.", Hallucination ?
  50. 				"elbow" : makeplural(body_part(FOOT)));
  51. 			break;
  52. 		  case 2:
  53. 			You("slip %s.", Hallucination ?
  54. 				"on a banana peel" : "and nearly fall");
  55. 			break;
  56. 		  case 3:
  57. 			You("flounder.");
  58. 			break;
  59. 		  default:
  60. 			You("stumble.");
  61. 			break;
  62. 		}
  63. 	    }
  64. #ifdef STEED
  65. 	    else {
  66. 		switch (rn2(4)) {
  67. 		  case 1:
  68. 			Your("%s slip out of the stirrups.", makeplural(body_part(FOOT)));
  69. 			break;
  70. 		  case 2:
  71. 			You("let go of the reins.");
  72. 			break;
  73. 		  case 3:
  74. 			You("bang into the saddle-horn.");
  75. 			break;
  76. 		  default:
  77. 			You("slide to one side of the saddle.");
  78. 			break;
  79. 		}
  80. 		dismount_steed(DISMOUNT_FELL);
  81. 	    }
  82. #endif
  83. 	}
  84. }

see_lamp_flicker[edit]

  1. /* Print a lamp flicker message with tailer. */
  2. STATIC_OVL void
  3. see_lamp_flicker(obj, tailer)
  4. struct obj *obj;
  5. const char *tailer;
  6. {
  7. 	switch (obj->where) {
  8. 	    case OBJ_INVENT:
  9. 	    case OBJ_MINVENT:
  10. 		pline("%s flickers%s.", Yname2(obj), tailer);
  11. 		break;
  12. 	    case OBJ_FLOOR:
  13. 		You("see %s flicker%s.", an(xname(obj)), tailer);
  14. 		break;
  15. 	}
  16. }

lantern_message[edit]

  1. /* Print a dimming message for brass lanterns. */
  2. STATIC_OVL void
  3. lantern_message(obj)
  4. struct obj *obj;
  5. {
  6. 	/* from adventure */
  7. 	switch (obj->where) {
  8. 	    case OBJ_INVENT:
  9. 		Your("lantern is getting dim.");
  10. 		if (Hallucination)
  11. 		    pline("Batteries have not been invented yet.");
  12. 		break;
  13. 	    case OBJ_FLOOR:
  14. 		You("see a lantern getting dim.");
  15. 		break;
  16. 	    case OBJ_MINVENT:
  17. 		pline("%s lantern is getting dim.",
  18. 		    s_suffix(Monnam(obj->ocarry)));
  19. 		break;
  20. 	}
  21. }

burn_object[edit]

  1. /*
  2. * Timeout callback for for objects that are burning. E.g. lamps, candles.
  3. * See begin_burn() for meanings of obj->age and obj->spe.
  4. */
  5. void
  6. burn_object(arg, timeout)
  7. genericptr_t arg;
  8. long timeout;
  9. {
  10. 	struct obj *obj = (struct obj *) arg;
  11. 	boolean canseeit, many, menorah, need_newsym;
  12. 	xchar x, y;
  13. 	char whose[BUFSZ];
  14.  
  15. 	menorah = obj->otyp == CANDELABRUM_OF_INVOCATION;
  16. 	many = menorah ? obj->spe > 1 : obj->quan > 1L;
  17.  
  18. 	/* timeout while away */
  19. 	if (timeout != monstermoves) {
  20. 	    long how_long = monstermoves - timeout;
  21.  
  22. 	    if (how_long >= obj->age) {
  23. 		obj->age = 0;
  24. 		end_burn(obj, FALSE);
  25.  
  26. 		if (menorah) {
  27. 		    obj->spe = 0;	/* no more candles */
  28. 		} else if (Is_candle(obj) || obj->otyp == POT_OIL) {
  29. 		    /* get rid of candles and burning oil potions */
  30. 		    obj_extract_self(obj);
  31. 		    obfree(obj, (struct obj *)0);
  32. 		    obj = (struct obj *) 0;
  33. 		}
  34.  
  35. 	    } else {
  36. 		obj->age -= how_long;
  37. 		begin_burn(obj, TRUE);
  38. 	    }
  39. 	    return;
  40. 	}
  41.  
  42. 	/* only interested in INVENT, FLOOR, and MINVENT */
  43. 	if (get_obj_location(obj, &x, &y, 0)) {
  44. 	    canseeit = !Blind && cansee(x, y);
  45. 	    /* set up `whose[]' to be "Your" or "Fred's" or "The goblin's" */
  46. 	    (void) Shk_Your(whose, obj);
  47. 	} else {
  48. 	    canseeit = FALSE;
  49. 	}
  50. 	need_newsym = FALSE;
  51.  
  52. 	/* obj->age is the age remaining at this point.  */
  53. 	switch (obj->otyp) {
  54. 	    case POT_OIL:
  55. 		    /* this should only be called when we run out */
  56. 		    if (canseeit) {
  57. 			switch (obj->where) {
  58. 			    case OBJ_INVENT:
  59. 			    case OBJ_MINVENT:
  60. 				pline("%s potion of oil has burnt away.",
  61. 				    whose);
  62. 				break;
  63. 			    case OBJ_FLOOR:
  64. 				You("see a burning potion of oil go out.");
  65. 				need_newsym = TRUE;
  66. 				break;
  67. 			}
  68. 		    }
  69. 		    end_burn(obj, FALSE);	/* turn off light source */
  70. 		    obj_extract_self(obj);
  71. 		    obfree(obj, (struct obj *)0);
  72. 		    obj = (struct obj *) 0;
  73. 		    break;
  74.  
  75. 	    case BRASS_LANTERN:
  76. 	    case OIL_LAMP:
  77. 		switch((int)obj->age) {
  78. 		    case 150:
  79. 		    case 100:
  80. 		    case 50:
  81. 			if (canseeit) {
  82. 			    if (obj->otyp == BRASS_LANTERN)
  83. 				lantern_message(obj);
  84. 			    else
  85. 				see_lamp_flicker(obj,
  86. 				    obj->age == 50L ? " considerably" : "");
  87. 			}
  88. 			break;
  89.  
  90. 		    case 25:
  91. 			if (canseeit) {
  92. 			    if (obj->otyp == BRASS_LANTERN)
  93. 				lantern_message(obj);
  94. 			    else {
  95. 				switch (obj->where) {
  96. 				    case OBJ_INVENT:
  97. 				    case OBJ_MINVENT:
  98. 					pline("%s %s seems about to go out.",
  99. 					    whose, xname(obj));
  100. 					break;
  101. 				    case OBJ_FLOOR:
  102. 					You("see %s about to go out.",
  103. 					    an(xname(obj)));
  104. 					break;
  105. 				}
  106. 			    }
  107. 			}
  108. 			break;
  109.  
  110. 		    case 0:
  111. 			/* even if blind you'll know if holding it */
  112. 			if (canseeit || obj->where == OBJ_INVENT) {
  113. 			    switch (obj->where) {
  114. 				case OBJ_INVENT:
  115. 				case OBJ_MINVENT:
  116. 				    if (obj->otyp == BRASS_LANTERN)
  117. 					pline("%s lantern has run out of power.",
  118. 					    whose);
  119. 				    else
  120. 					pline("%s %s has gone out.",
  121. 					    whose, xname(obj));
  122. 				    break;
  123. 				case OBJ_FLOOR:
  124. 				    if (obj->otyp == BRASS_LANTERN)
  125. 					You("see a lantern run out of power.");
  126. 				    else
  127. 					You("see %s go out.",
  128. 					    an(xname(obj)));
  129. 				    break;
  130. 			    }
  131. 			}
  132. 			end_burn(obj, FALSE);
  133. 			break;
  134.  
  135. 		    default:
  136. 			/*
  137. 			 * Someone added fuel to the lamp while it was
  138. 			 * lit.  Just fall through and let begin burn
  139. 			 * handle the new age.
  140. 			 */
  141. 			break;
  142. 		}
  143.  
  144. 		if (obj->age)
  145. 		    begin_burn(obj, TRUE);
  146.  
  147. 		break;
  148.  
  149. 	    case CANDELABRUM_OF_INVOCATION:
  150. 	    case TALLOW_CANDLE:
  151. 	    case WAX_CANDLE:
  152. 		switch (obj->age) {
  153. 		    case 75:
  154. 			if (canseeit)
  155. 			    switch (obj->where) {
  156. 				case OBJ_INVENT:
  157. 				case OBJ_MINVENT:
  158. 				    pline("%s %scandle%s getting short.",
  159. 					whose,
  160. 					menorah ? "candelabrum's " : "",
  161. 					many ? "s are" : " is");
  162. 				    break;
  163. 				case OBJ_FLOOR:
  164. 				    You("see %scandle%s getting short.",
  165. 					    menorah ? "a candelabrum's " :
  166. 						many ? "some " : "a ",
  167. 					    many ? "s" : "");
  168. 				    break;
  169. 			    }
  170. 			break;
  171.  
  172. 		    case 15:
  173. 			if (canseeit)
  174. 			    switch (obj->where) {
  175. 				case OBJ_INVENT:
  176. 				case OBJ_MINVENT:
  177. 				    pline(
  178. 					"%s %scandle%s flame%s flicker%s low!",
  179. 					    whose,
  180. 					    menorah ? "candelabrum's " : "",
  181. 					    many ? "s'" : "'s",
  182. 					    many ? "s" : "",
  183. 					    many ? "" : "s");
  184. 				    break;
  185. 				case OBJ_FLOOR:
  186. 				    You("see %scandle%s flame%s flicker low!",
  187. 					    menorah ? "a candelabrum's " :
  188. 						many ? "some " : "a ",
  189. 					    many ? "s'" : "'s",
  190. 					    many ? "s" : "");
  191. 				    break;
  192. 			    }
  193. 			break;
  194.  
  195. 		    case 0:
  196. 			/* we know even if blind and in our inventory */
  197. 			if (canseeit || obj->where == OBJ_INVENT) {
  198. 			    if (menorah) {
  199. 				switch (obj->where) {
  200. 				    case OBJ_INVENT:
  201. 				    case OBJ_MINVENT:
  202. 					pline("%s candelabrum's flame%s.",
  203. 					    whose,
  204. 					    many ? "s die" : " dies");
  205. 					break;
  206. 				    case OBJ_FLOOR:
  207. 					You("see a candelabrum's flame%s die.",
  208. 						many ? "s" : "");
  209. 					break;
  210. 				}
  211. 			    } else {
  212. 				switch (obj->where) {
  213. 				    case OBJ_INVENT:
  214. 				    case OBJ_MINVENT:
  215. 					pline("%s %s %s consumed!",
  216. 					    whose,
  217. 					    xname(obj),
  218. 					    many ? "are" : "is");
  219. 					break;
  220. 				    case OBJ_FLOOR:
  221. 					/*
  222. 					You see some wax candles consumed!
  223. 					You see a wax candle consumed!
  224. 					*/
  225. 					You("see %s%s consumed!",
  226. 					    many ? "some " : "",
  227. 					    many ? xname(obj):an(xname(obj)));
  228. 					need_newsym = TRUE;
  229. 					break;
  230. 				}
  231.  
  232. 				/* post message */
  233. 				pline(Hallucination ?
  234. 					(many ? "They shriek!" :
  235. 						"It shrieks!") :
  236. 					Blind ? "" :
  237. 					    (many ? "Their flames die." :
  238. 						    "Its flame dies."));
  239. 			    }
  240. 			}
  241. 			end_burn(obj, FALSE);
  242.  
  243. 			if (menorah) {
  244. 			    obj->spe = 0;
  245. 			} else {
  246. 			    obj_extract_self(obj);
  247. 			    obfree(obj, (struct obj *)0);
  248. 			    obj = (struct obj *) 0;
  249. 			}
  250. 			break;
  251.  
  252. 		    default:
  253. 			/*
  254. 			 * Someone added fuel (candles) to the menorah while
  255. 			 * it was lit.  Just fall through and let begin burn
  256. 			 * handle the new age.
  257. 			 */
  258. 			break;
  259. 		}
  260.  
  261. 		if (obj && obj->age)
  262. 		    begin_burn(obj, TRUE);
  263.  
  264. 		break;
  265.  
  266. 	    default:
  267. 		impossible("burn_object: unexpeced obj %s", xname(obj));
  268. 		break;
  269. 	}
  270. 	if (need_newsym) newsym(x, y);
  271. }

begin_burn[edit]

  1. /*
  2. * Start a burn timeout on the given object. If not "already lit" then
  3. * create a light source for the vision system.  There had better not
  4. * be a burn already running on the object.
  5. *
  6. * Magic lamps stay lit as long as there's a genie inside, so don't start
  7. * a timer.
  8. *
  9. * Burn rules:
  10. *	potions of oil, lamps & candles:
  11. *		age = # of turns of fuel left
  12. *		spe = <unused>
  13. *
  14. *	magic lamps:
  15. *		age = <unused>
  16. *		spe = 0 not lightable, 1 lightable forever
  17. *
  18. *	candelabrum:
  19. *		age = # of turns of fuel left
  20. *		spe = # of candles
  21. *
  22. * Once the burn begins, the age will be set to the amount of fuel
  23. * remaining _once_the_burn_finishes_.  If the burn is terminated
  24. * early then fuel is added back.
  25. *
  26. * This use of age differs from the use of age for corpses and eggs.
  27. * For the latter items, age is when the object was created, so we
  28. * know when it becomes "bad".
  29. *
  30. * This is a "silent" routine - it should not print anything out.
  31. */
  32. void
  33. begin_burn(obj, already_lit)
  34. 	struct obj *obj;
  35. 	boolean already_lit;
  36. {
  37. 	int radius = 3;
  38. 	long turns = 0;
  39. 	boolean do_timer = TRUE;
  40.  
  41. 	if (obj->age == 0 && obj->otyp != MAGIC_LAMP && !artifact_light(obj))
  42. 	    return;
  43.  
  44. 	switch (obj->otyp) {
  45. 	    case MAGIC_LAMP:
  46. 		obj->lamplit = 1;
  47. 		do_timer = FALSE;
  48. 		break;
  49.  
  50. 	    case POT_OIL:
  51. 		turns = obj->age;
  52. 		radius = 1;	/* very dim light */
  53. 		break;
  54.  
  55. 	    case BRASS_LANTERN:
  56. 	    case OIL_LAMP:
  57. 		/* magic times are 150, 100, 50, 25, and 0 */
  58. 		if (obj->age > 150L)
  59. 		    turns = obj->age - 150L;
  60. 		else if (obj->age > 100L)
  61. 		    turns = obj->age - 100L;
  62. 		else if (obj->age > 50L)
  63. 		    turns = obj->age - 50L;
  64. 		else if (obj->age > 25L)
  65. 		    turns = obj->age - 25L;
  66. 		else
  67. 		    turns = obj->age;
  68. 		break;
  69.  
  70. 	    case CANDELABRUM_OF_INVOCATION:
  71. 	    case TALLOW_CANDLE:
  72. 	    case WAX_CANDLE:
  73. 		/* magic times are 75, 15, and 0 */
  74. 		if (obj->age > 75L)
  75. 		    turns = obj->age - 75L;
  76. 		else if (obj->age > 15L)
  77. 		    turns = obj->age - 15L;
  78. 		else
  79. 		    turns = obj->age;
  80. 		radius = candle_light_range(obj);
  81. 		break;
  82.  
  83. 	    default:
  84. /* [ALI] Support artifact light sources */
  85. if (artifact_light(obj)) {
  86. 		    obj->lamplit = 1;
  87. 		    do_timer = FALSE;
  88. 		    radius = 2;
  89. 		} else {
  90. 		    impossible("begin burn: unexpected %s", xname(obj));
  91. 		    turns = obj->age;
  92. 		}
  93. 		break;
  94. 	}
  95.  
  96. 	if (do_timer) {
  97. 	    if (start_timer(turns, TIMER_OBJECT,
  98. 					BURN_OBJECT, (genericptr_t)obj)) {
  99. 		obj->lamplit = 1;
  100. 		obj->age -= turns;
  101. 		if (carried(obj) && !already_lit)
  102. 		    update_inventory();
  103. 	    } else {
  104. 		obj->lamplit = 0;
  105. 	    }
  106. 	} else {
  107. 	    if (carried(obj) && !already_lit)
  108. 		update_inventory();
  109. 	}
  110.  
  111. 	if (obj->lamplit && !already_lit) {
  112. 	    xchar x, y;
  113.  
  114. 	    if (get_obj_location(obj, &x, &y, CONTAINED_TOO|BURIED_TOO))
  115. 		new_light_source(x, y, radius, LS_OBJECT, (genericptr_t) obj);
  116. 	    else
  117. 		impossible("begin_burn: can't get obj position");
  118. 	}
  119. }

end_burn[edit]

  1. /*
  2. * Stop a burn timeout on the given object if timer attached.  Darken
  3. * light source.
  4. */
  5. void
  6. end_burn(obj, timer_attached)
  7. 	struct obj *obj;
  8. 	boolean timer_attached;
  9. {
  10. 	if (!obj->lamplit) {
  11. 	    impossible("end_burn: obj %s not lit", xname(obj));
  12. 	    return;
  13. 	}
  14.  
  15. 	if (obj->otyp == MAGIC_LAMP || artifact_light(obj))
  16. 	    timer_attached = FALSE;
  17.  
  18. 	if (!timer_attached) {
  19. 	    /* [DS] Cleanup explicitly, since timer cleanup won't happen */
  20. 	    del_light_source(LS_OBJECT, (genericptr_t)obj);
  21. 	    obj->lamplit = 0;
  22. 	    if (obj->where == OBJ_INVENT)
  23. 		update_inventory();
  24. 	} else if (!stop_timer(BURN_OBJECT, (genericptr_t) obj))
  25. 	    impossible("end_burn: obj %s not timed!", xname(obj));
  26. }
  27.  
  28. #endif /* OVL1 */

cleanup_burn[edit]

  1. #ifdef OVL0
  2.  
  3. /*
  4. * Cleanup a burning object if timer stopped.
  5. */
  6. static void
  7. cleanup_burn(arg, expire_time)
  8. genericptr_t arg;
  9. long expire_time;
  10. {
  11. struct obj *obj = (struct obj *)arg;
  12. if (!obj->lamplit) {
  13. 	impossible("cleanup_burn: obj %s not lit", xname(obj));
  14. 	return;
  15. }
  16.  
  17. del_light_source(LS_OBJECT, arg);
  18.  
  19. /* restore unused time */
  20. obj->age += expire_time - monstermoves;
  21.  
  22. obj->lamplit = 0;
  23.  
  24. if (obj->where == OBJ_INVENT)
  25. 	update_inventory();
  26. }
  27.  
  28. #endif /* OVL0 */

do_storms[edit]

  1. #ifdef OVL1
  2.  
  3. void
  4. do_storms()
  5. {
  6. int nstrike;
  7. register int x, y;
  8. int dirx, diry;
  9. int count;
  10.  
  11. /* no lightning if not the air level or too often, even then */
  12. if(!Is_airlevel(&u.uz) || rn2(8))
  13. 	return;
  14.  
  15. /* the number of strikes is 8-log2(nstrike) */
  16. for(nstrike = rnd(64); nstrike <= 64; nstrike *= 2) {
  17. 	count = 0;
  18. 	do {
  19. 	    x = rnd(COLNO-1);
  20. 	    y = rn2(ROWNO);
  21. 	} while (++count < 100 && levl[x][y].typ != CLOUD);
  22.  
  23. 	if(count < 100) {
  24. 	    dirx = rn2(3) - 1;
  25. 	    diry = rn2(3) - 1;
  26. 	    if(dirx != 0 || diry != 0)
  27. 		buzz(-15, /* "monster" LIGHTNING spell */
  28. 		     8, x, y, dirx, diry);
  29. 	}
  30. }
  31.  
  32. if(levl[u.ux][u.uy].typ == CLOUD) {
  33. 	/* inside a cloud during a thunder storm is deafening */
  34. 	pline("Kaboom!!!  Boom!!  Boom!!");
  35. 	if(!u.uinvulnerable) {
  36. 	    stop_occupation();
  37. 	    nomul(-3);
  38. 	}
  39. } else
  40. 	You_hear("a rumbling noise.");
  41. }
  42. #endif /* OVL1 */

Generic Timeout Functions[edit]

  1. #ifdef OVL0
  2. /* ------------------------------------------------------------------------- */
  3. /*
  4. * Generic Timeout Functions.
  5. *
  6. * Interface:
  7. *
  8. * General:
  9. *	boolean start_timer(long timeout,short kind,short func_index,
  10. *							genericptr_t arg)
  11. *		Start a timer of kind 'kind' that will expire at time
  12. *		monstermoves+'timeout'.  Call the function at 'func_index'
  13. *		in the timeout table using argument 'arg'.  Return TRUE if
  14. *		a timer was started.  This places the timer on a list ordered
  15. *		"sooner" to "later".  If an object, increment the object's
  16. *		timer count.
  17. *
  18. *	long stop_timer(short func_index, genericptr_t arg)
  19. *		Stop a timer specified by the (func_index, arg) pair.  This
  20. *		assumes that such a pair is unique.  Return the time the
  21. *		timer would have gone off.  If no timer is found, return 0.
  22. *		If an object, decrement the object's timer count.
  23. *
  24. *	void run_timers(void)
  25. *		Call timers that have timed out.
  26. *
  27. *
  28. * Save/Restore:
  29. *	void save_timers(int fd, int mode, int range)
  30. *		Save all timers of range 'range'.  Range is either global
  31. *		or local.  Global timers follow game play, local timers
  32. *		are saved with a level.  Object and monster timers are
  33. *		saved using their respective id's instead of pointers.
  34. *
  35. *	void restore_timers(int fd, int range, boolean ghostly, long adjust)
  36. *		Restore timers of range 'range'.  If from a ghost pile,
  37. *		adjust the timeout by 'adjust'.  The object and monster
  38. *		ids are not restored until later.
  39. *
  40. *	void relink_timers(boolean ghostly)
  41. *		Relink all object and monster timers that had been saved
  42. *		using their object's or monster's id number.
  43. *
  44. * Object Specific:
  45. *	void obj_move_timers(struct obj *src, struct obj *dest)
  46. *		Reassign all timers from src to dest.
  47. *
  48. *	void obj_split_timers(struct obj *src, struct obj *dest)
  49. *		Duplicate all timers assigned to src and attach them to dest.
  50. *
  51. *	void obj_stop_timers(struct obj *obj)
  52. *		Stop all timers attached to obj.
  53. */
  54.  
  55. #ifdef WIZARD
  56. STATIC_DCL const char *FDECL(kind_name, (SHORT_P));
  57. STATIC_DCL void FDECL(print_queue, (winid, timer_element *));
  58. #endif
  59. STATIC_DCL void FDECL(insert_timer, (timer_element *));
  60. STATIC_DCL timer_element *FDECL(remove_timer, (timer_element **, SHORT_P,
  61. 								genericptr_t));
  62. STATIC_DCL void FDECL(write_timer, (int, timer_element *));
  63. STATIC_DCL boolean FDECL(mon_is_local, (struct monst *));
  64. STATIC_DCL boolean FDECL(timer_is_local, (timer_element *));
  65. STATIC_DCL int FDECL(maybe_write_timer, (int, int, BOOLEAN_P));
  66.  
  67. /* ordered timer list */
  68. static timer_element *timer_base;		/* "active" */
  69. static unsigned long timer_id = 1;
  70.  
  71. /* If defined, then include names when printing out the timer queue */
  72. #define VERBOSE_TIMER
  73.  
  74. typedef struct {
  75. timeout_proc f, cleanup;
  76. #ifdef VERBOSE_TIMER
  77. const char *name;
  78. # define TTAB(a, b, c) {a,b,c}
  79. #else
  80. # define TTAB(a, b, c) {a,b}
  81. #endif
  82. } ttable;
  83.  
  84. /* table of timeout functions */
  85. static const ttable timeout_funcs[NUM_TIME_FUNCS] = {
  86. TTAB(rot_organic,	(timeout_proc)0,	"rot_organic"),
  87. TTAB(rot_corpse,	(timeout_proc)0,	"rot_corpse"),
  88. TTAB(revive_mon,	(timeout_proc)0,	"revive_mon"),
  89. TTAB(burn_object,	cleanup_burn,		"burn_object"),
  90. TTAB(hatch_egg,	(timeout_proc)0,	"hatch_egg"),
  91. TTAB(fig_transform,	(timeout_proc)0,	"fig_transform")
  92. };
  93. #undef TTAB

kind_name[edit]

  1. #if defined(WIZARD)
  2.  
  3. STATIC_OVL const char *
  4. kind_name(kind)
  5. short kind;
  6. {
  7. switch (kind) {
  8. 	case TIMER_LEVEL: return "level";
  9. 	case TIMER_GLOBAL: return "global";
  10. 	case TIMER_OBJECT: return "object";
  11. 	case TIMER_MONSTER: return "monster";
  12. }
  13. return "unknown";
  14. }

print_queue[edit]

  1. STATIC_OVL void
  2. print_queue(win, base)
  3. winid win;
  4. timer_element *base;
  5. {
  6. timer_element *curr;
  7. char buf[BUFSZ], arg_address[20];
  8.  
  9. if (!base) {
  10. 	putstr(win, 0, "<empty>");
  11. } else {
  12. 	putstr(win, 0, "timeout  id   kind   call");
  13. 	for (curr = base; curr; curr = curr->next) {
  14. #ifdef VERBOSE_TIMER
  15. 	    Sprintf(buf, " %4ld   %4ld  %-6s %s(%s)",
  16. 		curr->timeout, curr->tid, kind_name(curr->kind),
  17. 		timeout_funcs[curr->func_index].name,
  18. 		fmt_ptr((genericptr_t)curr->arg, arg_address));
  19. #else
  20. 	    Sprintf(buf, " %4ld   %4ld  %-6s #%d(%s)",
  21. 		curr->timeout, curr->tid, kind_name(curr->kind),
  22. 		curr->func_index,
  23. 		fmt_ptr((genericptr_t)curr->arg, arg_address));
  24. #endif
  25. 	    putstr(win, 0, buf);
  26. 	}
  27. }
  28. }

wiz_timeout_queue[edit]

  1. int
  2. wiz_timeout_queue()
  3. {
  4. winid win;
  5. char buf[BUFSZ];
  6.  
  7. win = create_nhwindow(NHW_MENU);	/* corner text window */
  8. if (win == WIN_ERR) return 0;
  9.  
  10. Sprintf(buf, "Current time = %ld.", monstermoves);
  11. putstr(win, 0, buf);
  12. putstr(win, 0, "");
  13. putstr(win, 0, "Active timeout queue:");
  14. putstr(win, 0, "");
  15. print_queue(win, timer_base);
  16.  
  17. display_nhwindow(win, FALSE);
  18. destroy_nhwindow(win);
  19.  
  20. return 0;
  21. }

timer_sanity_check[edit]

  1. void
  2. timer_sanity_check()
  3. {
  4. timer_element *curr;
  5. char obj_address[20];
  6.  
  7. /* this should be much more complete */
  8. for (curr = timer_base; curr; curr = curr->next)
  9. 	if (curr->kind == TIMER_OBJECT) {
  10. 	    struct obj *obj = (struct obj *) curr->arg;
  11. 	    if (obj->timed == 0) {
  12. 		pline("timer sanity: untimed obj %s, timer %ld",
  13. 		      fmt_ptr((genericptr_t)obj, obj_address), curr->tid);
  14. 	    }
  15. 	}
  16. }
  17.  
  18. #endif /* WIZARD */

run_timers[edit]

  1. /*
  2. * Pick off timeout elements from the global queue and call their functions.
  3. * Do this until their time is less than or equal to the move count.
  4. */
  5. void
  6. run_timers()
  7. {
  8. timer_element *curr;
  9.  
  10. /*
  11. * Always use the first element.  Elements may be added or deleted at
  12. * any time.  The list is ordered, we are done when the first element
  13. * is in the future.
  14. */
  15. while (timer_base && timer_base->timeout <= monstermoves) {
  16. 	curr = timer_base;
  17. 	timer_base = curr->next;
  18.  
  19. 	if (curr->kind == TIMER_OBJECT) ((struct obj *)(curr->arg))->timed--;
  20. 	(*timeout_funcs[curr->func_index].f)(curr->arg, curr->timeout);
  21. 	free((genericptr_t) curr);
  22. }
  23. }

start_timer[edit]

  1. /*
  2. * Start a timer.  Return TRUE if successful.
  3. */
  4. boolean
  5. start_timer(when, kind, func_index, arg)
  6. long when;
  7. short kind;
  8. short func_index;
  9. genericptr_t arg;
  10. {
  11. timer_element *gnu;
  12.  
  13. if (func_index < 0 || func_index >= NUM_TIME_FUNCS)
  14. 	panic("start_timer");
  15.  
  16. gnu = (timer_element *) alloc(sizeof(timer_element));
  17. gnu->next = 0;
  18. gnu->tid = timer_id++;
  19. gnu->timeout = monstermoves + when;
  20. gnu->kind = kind;
  21. gnu->needs_fixup = 0;
  22. gnu->func_index = func_index;
  23. gnu->arg = arg;
  24. insert_timer(gnu);
  25.  
  26. if (kind == TIMER_OBJECT)	/* increment object's timed count */
  27. 	((struct obj *)arg)->timed++;
  28.  
  29. /* should check for duplicates and fail if any */
  30. return TRUE;
  31. }

stop_timer[edit]

  1. /*
  2. * Remove the timer from the current list and free it up.  Return the time
  3. * it would have gone off, 0 if not found.
  4. */
  5. long
  6. stop_timer(func_index, arg)
  7. short func_index;
  8. genericptr_t arg;
  9. {
  10. timer_element *doomed;
  11. long timeout;
  12.  
  13. doomed = remove_timer(&timer_base, func_index, arg);
  14.  
  15. if (doomed) {
  16. 	timeout = doomed->timeout;
  17. 	if (doomed->kind == TIMER_OBJECT)
  18. 	    ((struct obj *)arg)->timed--;
  19. 	if (timeout_funcs[doomed->func_index].cleanup)
  20. 	    (*timeout_funcs[doomed->func_index].cleanup)(arg, timeout);
  21. 	free((genericptr_t) doomed);
  22. 	return timeout;
  23. }
  24. return 0;
  25. }

obj_move_timers[edit]

  1. /*
  2. * Move all object timers from src to dest, leaving src untimed.
  3. */
  4. void
  5. obj_move_timers(src, dest)
  6. struct obj *src, *dest;
  7. {
  8. int count;
  9. timer_element *curr;
  10.  
  11. for (count = 0, curr = timer_base; curr; curr = curr->next)
  12. 	if (curr->kind == TIMER_OBJECT && curr->arg == (genericptr_t)src) {
  13. 	    curr->arg = (genericptr_t) dest;
  14. 	    dest->timed++;
  15. 	    count++;
  16. 	}
  17. if (count != src->timed)
  18. 	panic("obj_move_timers");
  19. src->timed = 0;
  20. }

obj_split_timers[edit]

  1. /*
  2. * Find all object timers and duplicate them for the new object "dest".
  3. */
  4. void
  5. obj_split_timers(src, dest)
  6. struct obj *src, *dest;
  7. {
  8. timer_element *curr, *next_timer=0;
  9.  
  10. for (curr = timer_base; curr; curr = next_timer) {
  11. 	next_timer = curr->next;	/* things may be inserted */
  12. 	if (curr->kind == TIMER_OBJECT && curr->arg == (genericptr_t)src) {
  13. 	    (void) start_timer(curr->timeout-monstermoves, TIMER_OBJECT,
  14. 					curr->func_index, (genericptr_t)dest);
  15. 	}
  16. }
  17. }

obj_stop_timers[edit]

  1. /*
  2. * Stop all timers attached to this object.  We can get away with this because
  3. * all object pointers are unique.
  4. */
  5. void
  6. obj_stop_timers(obj)
  7. struct obj *obj;
  8. {
  9. timer_element *curr, *prev, *next_timer=0;
  10.  
  11. for (prev = 0, curr = timer_base; curr; curr = next_timer) {
  12. 	next_timer = curr->next;
  13. 	if (curr->kind == TIMER_OBJECT && curr->arg == (genericptr_t)obj) {
  14. 	    if (prev)
  15. 		prev->next = curr->next;
  16. 	    else
  17. 		timer_base = curr->next;
  18. 	    if (timeout_funcs[curr->func_index].cleanup)
  19. 		(*timeout_funcs[curr->func_index].cleanup)(curr->arg,
  20. 			curr->timeout);
  21. 	    free((genericptr_t) curr);
  22. 	} else {
  23. 	    prev = curr;
  24. 	}
  25. }
  26. obj->timed = 0;
  27. }

insert_timer[edit]

  1. /* Insert timer into the global queue */
  2. STATIC_OVL void
  3. insert_timer(gnu)
  4. timer_element *gnu;
  5. {
  6. timer_element *curr, *prev;
  7.  
  8. for (prev = 0, curr = timer_base; curr; prev = curr, curr = curr->next)
  9. 	if (curr->timeout >= gnu->timeout) break;
  10.  
  11. gnu->next = curr;
  12. if (prev)
  13. 	prev->next = gnu;
  14. else
  15. 	timer_base = gnu;
  16. }

remove_timer[edit]

  1. STATIC_OVL timer_element *
  2. remove_timer(base, func_index, arg)
  3. timer_element **base;
  4. short func_index;
  5. genericptr_t arg;
  6. {
  7. timer_element *prev, *curr;
  8.  
  9. for (prev = 0, curr = *base; curr; prev = curr, curr = curr->next)
  10. 	if (curr->func_index == func_index && curr->arg == arg) break;
  11.  
  12. if (curr) {
  13. 	if (prev)
  14. 	    prev->next = curr->next;
  15. 	else
  16. 	    *base = curr->next;
  17. }
  18.  
  19. return curr;
  20. }

write_timer[edit]

  1. STATIC_OVL void
  2. write_timer(fd, timer)
  3. int fd;
  4. timer_element *timer;
  5. {
  6. genericptr_t arg_save;
  7.  
  8. switch (timer->kind) {
  9. 	case TIMER_GLOBAL:
  10. 	case TIMER_LEVEL:
  11. 	    /* assume no pointers in arg */
  12. 	    bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
  13. 	    break;
  14.  
  15. 	case TIMER_OBJECT:
  16. 	    if (timer->needs_fixup)
  17. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
  18. 	    else {
  19. 		/* replace object pointer with id */
  20. 		arg_save = timer->arg;
  21. 		timer->arg = (genericptr_t)((struct obj *)timer->arg)->o_id;
  22. 		timer->needs_fixup = 1;
  23. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
  24. 		timer->arg = arg_save;
  25. 		timer->needs_fixup = 0;
  26. 	    }
  27. 	    break;
  28.  
  29. 	case TIMER_MONSTER:
  30. 	    if (timer->needs_fixup)
  31. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
  32. 	    else {
  33. 		/* replace monster pointer with id */
  34. 		arg_save = timer->arg;
  35. 		timer->arg = (genericptr_t)((struct monst *)timer->arg)->m_id;
  36. 		timer->needs_fixup = 1;
  37. 		bwrite(fd, (genericptr_t)timer, sizeof(timer_element));
  38. 		timer->arg = arg_save;
  39. 		timer->needs_fixup = 0;
  40. 	    }
  41. 	    break;
  42.  
  43. 	default:
  44. 	    panic("write_timer");
  45. 	    break;
  46. }
  47. }

obj_is_local[edit]

  1. /*
  2. * Return TRUE if the object will stay on the level when the level is
  3. * saved.
  4. */
  5. boolean
  6. obj_is_local(obj)
  7. struct obj *obj;
  8. {
  9. switch (obj->where) {
  10. 	case OBJ_INVENT:
  11. 	case OBJ_MIGRATING:	return FALSE;
  12. 	case OBJ_FLOOR:
  13. 	case OBJ_BURIED:	return TRUE;
  14. 	case OBJ_CONTAINED:	return obj_is_local(obj->ocontainer);
  15. 	case OBJ_MINVENT:	return mon_is_local(obj->ocarry);
  16. }
  17. panic("obj_is_local");
  18. return FALSE;
  19. }

mon_is_local[edit]

  1. /*
  2. * Return TRUE if the given monster will stay on the level when the
  3. * level is saved.
  4. */
  5. STATIC_OVL boolean
  6. mon_is_local(mon)
  7. struct monst *mon;
  8. {
  9. struct monst *curr;
  10.  
  11. for (curr = migrating_mons; curr; curr = curr->nmon)
  12. 	if (curr == mon) return FALSE;
  13. /* `mydogs' is used during level changes, never saved and restored */
  14. for (curr = mydogs; curr; curr = curr->nmon)
  15. 	if (curr == mon) return FALSE;
  16. return TRUE;
  17. }

timer_is_local[edit]

  1. /*
  2. * Return TRUE if the timer is attached to something that will stay on the
  3. * level when the level is saved.
  4. */
  5. STATIC_OVL boolean
  6. timer_is_local(timer)
  7. timer_element *timer;
  8. {
  9. switch (timer->kind) {
  10. 	case TIMER_LEVEL:	return TRUE;
  11. 	case TIMER_GLOBAL:	return FALSE;
  12. 	case TIMER_OBJECT:	return obj_is_local((struct obj *)timer->arg);
  13. 	case TIMER_MONSTER:	return mon_is_local((struct monst *)timer->arg);
  14. }
  15. panic("timer_is_local");
  16. return FALSE;
  17. }

maybe_write_timer[edit]

  1. /*
  2. * Part of the save routine.  Count up the number of timers that would
  3. * be written.  If write_it is true, actually write the timer.
  4. */
  5. STATIC_OVL int
  6. maybe_write_timer(fd, range, write_it)
  7. int fd, range;
  8. boolean write_it;
  9. {
  10. int count = 0;
  11. timer_element *curr;
  12.  
  13. for (curr = timer_base; curr; curr = curr->next) {
  14. 	if (range == RANGE_GLOBAL) {
  15. 	    /* global timers */
  16.  
  17. 	    if (!timer_is_local(curr)) {
  18. 		count++;
  19. 		if (write_it) write_timer(fd, curr);
  20. 	    }
  21.  
  22. 	} else {
  23. 	    /* local timers */
  24.  
  25. 	    if (timer_is_local(curr)) {
  26. 		count++;
  27. 		if (write_it) write_timer(fd, curr);
  28. 	    }
  29.  
  30. 	}
  31. }
  32.  
  33. return count;
  34. }

save_timers[edit]

  1. /*
  2. * Save part of the timer list.  The parameter 'range' specifies either
  3. * global or level timers to save.  The timer ID is saved with the global
  4. * timers.
  5. *
  6. * Global range:
  7. *		+ timeouts that follow the hero (global)
  8. *		+ timeouts that follow obj & monst that are migrating
  9. *
  10. * Level range:
  11. *		+ timeouts that are level specific (e.g. storms)
  12. *		+ timeouts that stay with the level (obj & monst)
  13. */
  14. void
  15. save_timers(fd, mode, range)
  16. int fd, mode, range;
  17. {
  18. timer_element *curr, *prev, *next_timer=0;
  19. int count;
  20.  
  21. if (perform_bwrite(mode)) {
  22. 	if (range == RANGE_GLOBAL)
  23. 	    bwrite(fd, (genericptr_t) &timer_id, sizeof(timer_id));
  24.  
  25. 	count = maybe_write_timer(fd, range, FALSE);
  26. 	bwrite(fd, (genericptr_t) &count, sizeof count);
  27. 	(void) maybe_write_timer(fd, range, TRUE);
  28. }
  29.  
  30. if (release_data(mode)) {
  31. 	for (prev = 0, curr = timer_base; curr; curr = next_timer) {
  32. 	    next_timer = curr->next;	/* in case curr is removed */
  33.  
  34. 	    if ( !(!!(range == RANGE_LEVEL) ^ !!timer_is_local(curr)) ) {
  35. 		if (prev)
  36. 		    prev->next = curr->next;
  37. 		else
  38. 		    timer_base = curr->next;
  39. 		free((genericptr_t) curr);
  40. 		/* prev stays the same */
  41. 	    } else {
  42. 		prev = curr;
  43. 	    }
  44. 	}
  45. }
  46. }

restore_timers[edit]

  1. /*
  2. * Pull in the structures from disk, but don't recalculate the object and
  3. * monster pointers.
  4. */
  5. void
  6. restore_timers(fd, range, ghostly, adjust)
  7. int fd, range;
  8. boolean ghostly;	/* restoring from a ghost level */
  9. long adjust;	/* how much to adjust timeout */
  10. {
  11. int count;
  12. timer_element *curr;
  13.  
  14. if (range == RANGE_GLOBAL)
  15. 	mread(fd, (genericptr_t) &timer_id, sizeof timer_id);
  16.  
  17. /* restore elements */
  18. mread(fd, (genericptr_t) &count, sizeof count);
  19. while (count-- > 0) {
  20. 	curr = (timer_element *) alloc(sizeof(timer_element));
  21. 	mread(fd, (genericptr_t) curr, sizeof(timer_element));
  22. 	if (ghostly)
  23. 	    curr->timeout += adjust;
  24. 	insert_timer(curr);
  25. }
  26. }

relink_timers[edit]

  1. /* reset all timers that are marked for reseting */
  2. void
  3. relink_timers(ghostly)
  4. boolean ghostly;
  5. {
  6. timer_element *curr;
  7. unsigned nid;
  8.  
  9. for (curr = timer_base; curr; curr = curr->next) {
  10. 	if (curr->needs_fixup) {
  11. 	    if (curr->kind == TIMER_OBJECT) {
  12. 		if (ghostly) {
  13. 		    if (!lookup_id_mapping((unsigned)curr->arg, &nid))
  14. 			panic("relink_timers 1");
  15. 		} else
  16. 		    nid = (unsigned) curr->arg;
  17. 		curr->arg = (genericptr_t) find_oid(nid);
  18. 		if (!curr->arg) panic("cant find o_id %d", nid);
  19. 		curr->needs_fixup = 0;
  20. 	    } else if (curr->kind == TIMER_MONSTER) {
  21. 		panic("relink_timers: no monster timer implemented");
  22. 	    } else
  23. 		panic("relink_timers 2");
  24. 	}
  25. }
  26. }
  27.  
  28. #endif /* OVL0 */
  29.  
  30. /*timeout.c*/