Source:NetHack 3.2.0/timeout.c

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

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

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