Source:NetHack 3.3.0/timeout.c

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