Source:NetHack 3.6.2/src/timeout.c
Jump to navigation
Jump to search
Below is the full text to timeout.c from the source code of NetHack 3.6.2. To link to a particular line, write [[Source:NetHack 3.6.2/src/timeout.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 3.6 timeout.c $NHDT-Date: 1545182148 2018/12/19 01:15:48 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.89 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2018. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. #include "lev.h" /* for checking save modes */ 8. 9. STATIC_DCL void NDECL(stoned_dialogue); 10. STATIC_DCL void NDECL(vomiting_dialogue); 11. STATIC_DCL void NDECL(choke_dialogue); 12. STATIC_DCL void NDECL(levitation_dialogue); 13. STATIC_DCL void NDECL(slime_dialogue); 14. STATIC_DCL void FDECL(slimed_to_death, (struct kinfo *)); 15. STATIC_DCL void NDECL(slip_or_trip); 16. STATIC_DCL void FDECL(see_lamp_flicker, (struct obj *, const char *)); 17. STATIC_DCL void FDECL(lantern_message, (struct obj *)); 18. STATIC_DCL void FDECL(cleanup_burn, (ANY_P *, long)); 19. 20. /* used by wizard mode #timeout and #wizintrinsic; order by 'interest' 21. for timeout countdown, where most won't occur in normal play */ 22. const struct propname { 23. int prop_num; 24. const char *prop_name; 25. } propertynames[] = { 26. { INVULNERABLE, "invulnerable" }, 27. { STONED, "petrifying" }, 28. { SLIMED, "becoming slime" }, 29. { STRANGLED, "strangling" }, 30. { SICK, "fatally sick" }, 31. { STUNNED, "stunned" }, 32. { CONFUSION, "confused" }, 33. { HALLUC, "hallucinating" }, 34. { BLINDED, "blinded" }, 35. { DEAF, "deafness" }, 36. { VOMITING, "vomiting" }, 37. { GLIB, "slippery fingers" }, 38. { WOUNDED_LEGS, "wounded legs" }, 39. { SLEEPY, "sleepy" }, 40. { TELEPORT, "teleporting" }, 41. { POLYMORPH, "polymorphing" }, 42. { LEVITATION, "levitating" }, 43. { FAST, "very fast" }, /* timed 'FAST' is very fast */ 44. { CLAIRVOYANT, "clairvoyant" }, 45. { DETECT_MONSTERS, "monster detection" }, 46. { SEE_INVIS, "see invisible" }, 47. { INVIS, "invisible" }, 48. /* properties beyond here don't have timed values during normal play, 49. so there's not much point in trying to order them sensibly; 50. they're either on or off based on equipment, role, actions, &c */ 51. { FIRE_RES, "fire resistance" }, 52. { COLD_RES, "cold resistance" }, 53. { SLEEP_RES, "sleep resistance" }, 54. { DISINT_RES, "disintegration resistance" }, 55. { SHOCK_RES, "shock resistance" }, 56. { POISON_RES, "poison resistance" }, 57. { ACID_RES, "acid resistance" }, 58. { STONE_RES, "stoning resistance" }, 59. { DRAIN_RES, "drain resistance" }, 60. { SICK_RES, "sickness resistance" }, 61. { ANTIMAGIC, "magic resistance" }, 62. { HALLUC_RES, "hallucination resistance" }, 63. { FUMBLING, "fumbling" }, 64. { HUNGER, "voracious hunger" }, 65. { TELEPAT, "telepathic" }, 66. { WARNING, "warning" }, 67. { WARN_OF_MON, "warn: monster type or class" }, 68. { WARN_UNDEAD, "warn: undead" }, 69. { SEARCHING, "searching" }, 70. { INFRAVISION, "infravision" }, 71. { ADORNED, "adorned (+/- Cha)" }, 72. { DISPLACED, "displaced" }, 73. { STEALTH, "stealthy" }, 74. { AGGRAVATE_MONSTER, "monster aggravation" }, 75. { CONFLICT, "conflict" }, 76. { JUMPING, "jumping" }, 77. { TELEPORT_CONTROL, "teleport control" }, 78. { FLYING, "flying" }, 79. { WWALKING, "water walking" }, 80. { SWIMMING, "swimming" }, 81. { MAGICAL_BREATHING, "magical breathing" }, 82. { PASSES_WALLS, "pass thru walls" }, 83. { SLOW_DIGESTION, "slow digestion" }, 84. { HALF_SPDAM, "half spell damage" }, 85. { HALF_PHDAM, "half physical damage" }, 86. { REGENERATION, "HP regeneration" }, 87. { ENERGY_REGENERATION, "energy regeneration" }, 88. { PROTECTION, "extra protection" }, 89. { PROT_FROM_SHAPE_CHANGERS, "protection from shape changers" }, 90. { POLYMORPH_CONTROL, "polymorph control" }, 91. { UNCHANGING, "unchanging" }, 92. { REFLECTING, "reflecting" }, 93. { FREE_ACTION, "free action" }, 94. { FIXED_ABIL, "fixed abilites" }, 95. { LIFESAVED, "life will be saved" }, 96. { 0, 0 }, 97. }; 98. 99. /* He is being petrified - dialogue by inmet!tower */ 100. static NEARDATA const char *const stoned_texts[] = { 101. "You are slowing down.", /* 5 */ 102. "Your limbs are stiffening.", /* 4 */ 103. "Your limbs have turned to stone.", /* 3 */ 104. "You have turned to stone.", /* 2 */ 105. "You are a statue." /* 1 */ 106. }; 107. 108. STATIC_OVL void 109. stoned_dialogue() 110. { 111. register long i = (Stoned & TIMEOUT); 112. 113. if (i > 0L && i <= SIZE(stoned_texts)) { 114. char buf[BUFSZ]; 115. 116. Strcpy(buf, stoned_texts[SIZE(stoned_texts) - i]); 117. if (nolimbs(youmonst.data) && strstri(buf, "limbs")) 118. (void) strsubst(buf, "limbs", "extremities"); 119. pline1(buf); 120. } 121. switch ((int) i) { 122. case 5: /* slowing down */ 123. HFast = 0L; 124. if (multi > 0) 125. nomul(0); 126. break; 127. case 4: /* limbs stiffening */ 128. /* just one move left to save oneself so quit fiddling around; 129. don't stop attempt to eat tin--might be lizard or acidic */ 130. if (!Popeye(STONED)) 131. stop_occupation(); 132. if (multi > 0) 133. nomul(0); 134. break; 135. case 3: /* limbs turned to stone */ 136. stop_occupation(); 137. nomul(-3); /* can't move anymore */ 138. multi_reason = "getting stoned"; 139. nomovemsg = You_can_move_again; /* not unconscious */ 140. /* "your limbs have turned to stone" so terminate wounded legs */ 141. if (Wounded_legs && !u.usteed) 142. heal_legs(2); 143. break; 144. case 2: /* turned to stone */ 145. if ((HDeaf & TIMEOUT) > 0L && (HDeaf & TIMEOUT) < 5L) 146. set_itimeout(&HDeaf, 5L); /* avoid Hear_again at tail end */ 147. /* if also vomiting or turning into slime, stop those (no messages) */ 148. if (Vomiting) 149. make_vomiting(0L, FALSE); 150. if (Slimed) 151. make_slimed(0L, (char *) 0); 152. break; 153. default: 154. break; 155. } 156. exercise(A_DEX, FALSE); 157. } 158. 159. /* hero is getting sicker and sicker prior to vomiting */ 160. static NEARDATA const char *const vomiting_texts[] = { 161. "are feeling mildly nauseated.", /* 14 */ 162. "feel slightly confused.", /* 11 */ 163. "can't seem to think straight.", /* 8 */ 164. "feel incredibly sick.", /* 5 */ 165. "are about to vomit." /* 2 */ 166. }; 167. 168. STATIC_OVL void 169. vomiting_dialogue() 170. { 171. const char *txt = 0; 172. long v = (Vomiting & TIMEOUT); 173. 174. /* note: nhtimeout() hasn't decremented timed properties for the 175. current turn yet, so we use Vomiting-1 here */ 176. switch ((int) (v - 1L)) { 177. case 14: 178. txt = vomiting_texts[0]; 179. break; 180. case 11: 181. txt = vomiting_texts[1]; 182. break; 183. case 6: 184. make_stunned((HStun & TIMEOUT) + (long) d(2, 4), FALSE); 185. if (!Popeye(VOMITING)) 186. stop_occupation(); 187. /*FALLTHRU*/ 188. case 9: 189. make_confused((HConfusion & TIMEOUT) + (long) d(2, 4), FALSE); 190. if (multi > 0) 191. nomul(0); 192. break; 193. case 8: 194. txt = vomiting_texts[2]; 195. break; 196. case 5: 197. txt = vomiting_texts[3]; 198. break; 199. case 2: 200. txt = vomiting_texts[4]; 201. if (cantvomit(youmonst.data)) 202. txt = "gag uncontrollably."; 203. else if (Hallucination) 204. /* "hurl" is short for "hurl chunks" which is slang for 205. relatively violent vomiting... */ 206. txt = "are about to hurl!"; 207. break; 208. case 0: 209. stop_occupation(); 210. if (!cantvomit(youmonst.data)) { 211. morehungry(20); 212. /* case 2 used to be "You suddenly vomit!" but it wasn't sudden 213. since you've just been through the earlier messages of the 214. countdown, and it was still possible to move around between 215. that message and "You can move again." (from vomit()'s 216. nomul(-2)) with no intervening message; give one here to 217. have more specific point at which hero became unable to move 218. [vomit() issues its own message for the cantvomit() case 219. and for the FAINTING-or-worse case where stomach is empty] */ 220. if (u.uhs < FAINTING) 221. You("%s!", !Hallucination ? "vomit" : "hurl chunks"); 222. } 223. vomit(); 224. break; 225. default: 226. break; 227. } 228. if (txt) 229. You1(txt); 230. exercise(A_CON, FALSE); 231. } 232. 233. static NEARDATA const char *const choke_texts[] = { 234. "You find it hard to breathe.", 235. "You're gasping for air.", 236. "You can no longer breathe.", 237. "You're turning %s.", 238. "You suffocate." 239. }; 240. 241. static NEARDATA const char *const choke_texts2[] = { 242. "Your %s is becoming constricted.", 243. "Your blood is having trouble reaching your brain.", 244. "The pressure on your %s increases.", 245. "Your consciousness is fading.", 246. "You suffocate." 247. }; 248. 249. STATIC_OVL void 250. choke_dialogue() 251. { 252. register long i = (Strangled & TIMEOUT); 253. 254. if (i > 0 && i <= SIZE(choke_texts)) { 255. if (Breathless || !rn2(50)) 256. pline(choke_texts2[SIZE(choke_texts2) - i], body_part(NECK)); 257. else { 258. const char *str = choke_texts[SIZE(choke_texts) - i]; 259. 260. if (index(str, '%')) 261. pline(str, hcolor(NH_BLUE)); 262. else 263. pline1(str); 264. } 265. } 266. exercise(A_STR, FALSE); 267. } 268. 269. static NEARDATA const char *const levi_texts[] = { 270. "You float slightly lower.", 271. "You wobble unsteadily %s the %s." 272. }; 273. 274. STATIC_OVL void 275. levitation_dialogue() 276. { 277. /* -1 because the last message comes via float_down() */ 278. long i = (((HLevitation & TIMEOUT) - 1L) / 2L); 279. 280. if (ELevitation) 281. return; 282. 283. if (!ACCESSIBLE(levl[u.ux][u.uy].typ) 284. && !is_pool_or_lava(u.ux,u.uy)) 285. return; 286. 287. if (((HLevitation & TIMEOUT) % 2L) && i > 0L && i <= SIZE(levi_texts)) { 288. const char *s = levi_texts[SIZE(levi_texts) - i]; 289. 290. if (index(s, '%')) { 291. boolean danger = (is_pool_or_lava(u.ux, u.uy) 292. && !Is_waterlevel(&u.uz)); 293. 294. pline(s, danger ? "over" : "in", 295. danger ? surface(u.ux, u.uy) : "air"); 296. } else 297. pline1(s); 298. } 299. } 300. 301. static NEARDATA const char *const slime_texts[] = { 302. "You are turning a little %s.", /* 5 */ 303. "Your limbs are getting oozy.", /* 4 */ 304. "Your skin begins to peel away.", /* 3 */ 305. "You are turning into %s.", /* 2 */ 306. "You have become %s." /* 1 */ 307. }; 308. 309. STATIC_OVL void 310. slime_dialogue() 311. { 312. register long i = (Slimed & TIMEOUT) / 2L; 313. 314. if (i == 1L) { 315. /* display as green slime during "You have become green slime." 316. but don't worry about not being able to see self; if already 317. mimicking something else at the time, implicitly be revealed */ 318. youmonst.m_ap_type = M_AP_MONSTER; 319. youmonst.mappearance = PM_GREEN_SLIME; 320. } 321. if (((Slimed & TIMEOUT) % 2L) && i >= 0L && i < SIZE(slime_texts)) { 322. char buf[BUFSZ]; 323. 324. Strcpy(buf, slime_texts[SIZE(slime_texts) - i - 1L]); 325. if (nolimbs(youmonst.data) && strstri(buf, "limbs")) 326. (void) strsubst(buf, "limbs", "extremities"); 327. 328. if (index(buf, '%')) { 329. if (i == 4L) { /* "you are turning green" */ 330. if (!Blind) /* [what if you're already green?] */ 331. pline(buf, hcolor(NH_GREEN)); 332. } else 333. pline(buf, 334. an(Hallucination ? rndmonnam(NULL) : "green slime")); 335. } else 336. pline1(buf); 337. } 338. 339. switch (i) { 340. case 3L: /* limbs becoming oozy */ 341. HFast = 0L; /* lose intrinsic speed */ 342. if (!Popeye(SLIMED)) 343. stop_occupation(); 344. if (multi > 0) 345. nomul(0); 346. break; 347. case 2L: /* skin begins to peel */ 348. if ((HDeaf & TIMEOUT) > 0L && (HDeaf & TIMEOUT) < 5L) 349. set_itimeout(&HDeaf, 5L); /* avoid Hear_again at tail end */ 350. break; 351. case 1L: /* turning into slime */ 352. /* if also turning to stone, stop doing that (no message) */ 353. if (Stoned) 354. make_stoned(0L, (char *) 0, KILLED_BY_AN, (char *) 0); 355. break; 356. } 357. exercise(A_DEX, FALSE); 358. } 359. 360. void 361. burn_away_slime() 362. { 363. if (Slimed) { 364. make_slimed(0L, "The slime that covers you is burned away!"); 365. } 366. } 367. 368. /* countdown timer for turning into green slime has run out; kill our hero */ 369. STATIC_OVL void 370. slimed_to_death(kptr) 371. struct kinfo *kptr; 372. { 373. uchar save_mvflags; 374. 375. /* redundant: polymon() cures sliming when polying into green slime */ 376. if (Upolyd && youmonst.data == &mons[PM_GREEN_SLIME]) { 377. dealloc_killer(kptr); 378. return; 379. } 380. /* more sure killer reason is set up */ 381. if (kptr && kptr->name[0]) { 382. killer.format = kptr->format; 383. Strcpy(killer.name, kptr->name); 384. } else { 385. killer.format = NO_KILLER_PREFIX; 386. Strcpy(killer.name, "turned into green slime"); 387. } 388. dealloc_killer(kptr); 389. 390. /* 391. * Polymorph into a green slime, which might destroy some worn armor 392. * (potentially affecting bones) and dismount from steed. 393. * Can't be Unchanging; wouldn't have turned into slime if we were. 394. * Despite lack of Unchanging, neither done() nor savelife() calls 395. * rehumanize() if hero dies while polymorphed. 396. * polymon() undoes the slime countdown's mimick-green-slime hack 397. * but does not perform polyself()'s light source bookkeeping. 398. * No longer need to manually increment uconduct.polyselfs to reflect 399. * [formerly implicit] change of form; polymon() takes care of that. 400. * Temporarily ungenocide if necessary. 401. */ 402. if (emits_light(youmonst.data)) 403. del_light_source(LS_MONSTER, monst_to_any(&youmonst)); 404. save_mvflags = mvitals[PM_GREEN_SLIME].mvflags; 405. mvitals[PM_GREEN_SLIME].mvflags = save_mvflags & ~G_GENOD; 406. (void) polymon(PM_GREEN_SLIME); 407. mvitals[PM_GREEN_SLIME].mvflags = save_mvflags; 408. done(TURNED_SLIME); 409. 410. /* life-saved; even so, hero still has turned into green slime; 411. player may have genocided green slimes after being infected */ 412. if ((mvitals[PM_GREEN_SLIME].mvflags & G_GENOD) != 0) { 413. killer.format = KILLED_BY; 414. Strcpy(killer.name, "slimicide"); 415. /* immediately follows "OK, so you don't die." */ 416. pline("Yes, you do. Green slime has been genocided..."); 417. done(GENOCIDED); 418. /* could be life-saved again (only in explore or wizard mode) 419. but green slimes are gone; just stay in current form */ 420. } 421. return; 422. } 423. 424. /* Intrinsic Passes_walls is temporary when your god is trying to fix 425. all troubles and then TROUBLE_STUCK_IN_WALL calls safe_teleds() but 426. it can't find anywhere to place you. If that happens you get a small 427. value for (HPasses_walls & TIMEOUT) to move somewhere yourself. 428. Message given is "you feel much slimmer" as a joke hint that you can 429. move between things which are closely packed--like the substance of 430. solid rock! */ 431. static NEARDATA const char *const phaze_texts[] = { 432. "You start to feel bloated.", 433. "You are feeling rather flabby.", 434. }; 435. 436. STATIC_OVL void 437. phaze_dialogue() 438. { 439. long i = ((HPasses_walls & TIMEOUT) / 2L); 440. 441. if (EPasses_walls || (HPasses_walls & ~TIMEOUT)) 442. return; 443. 444. if (((HPasses_walls & TIMEOUT) % 2L) && i > 0L && i <= SIZE(phaze_texts)) 445. pline1(phaze_texts[SIZE(phaze_texts) - i]); 446. } 447. 448. void 449. nh_timeout() 450. { 451. register struct prop *upp; 452. struct kinfo *kptr; 453. boolean was_flying; 454. int sleeptime; 455. int m_idx; 456. int baseluck = (flags.moonphase == FULL_MOON) ? 1 : 0; 457. 458. if (flags.friday13) 459. baseluck -= 1; 460. 461. if (u.uluck != baseluck 462. && moves % ((u.uhave.amulet || u.ugangr) ? 300 : 600) == 0) { 463. /* Cursed luckstones stop bad luck from timing out; blessed luckstones 464. * stop good luck from timing out; normal luckstones stop both; 465. * neither is stopped if you don't have a luckstone. 466. * Luck is based at 0 usually, +1 if a full moon and -1 on Friday 13th 467. */ 468. register int time_luck = stone_luck(FALSE); 469. boolean nostone = !carrying(LUCKSTONE) && !stone_luck(TRUE); 470. 471. if (u.uluck > baseluck && (nostone || time_luck < 0)) 472. u.uluck--; 473. else if (u.uluck < baseluck && (nostone || time_luck > 0)) 474. u.uluck++; 475. } 476. if (u.uinvulnerable) 477. return; /* things past this point could kill you */ 478. if (Stoned) 479. stoned_dialogue(); 480. if (Slimed) 481. slime_dialogue(); 482. if (Vomiting) 483. vomiting_dialogue(); 484. if (Strangled) 485. choke_dialogue(); 486. if (HLevitation & TIMEOUT) 487. levitation_dialogue(); 488. if (HPasses_walls & TIMEOUT) 489. phaze_dialogue(); 490. if (u.mtimedone && !--u.mtimedone) { 491. if (Unchanging) 492. u.mtimedone = rnd(100 * youmonst.data->mlevel + 1); 493. else if (is_were(youmonst.data)) 494. you_unwere(FALSE); /* if polycontrl, asks whether to rehumanize */ 495. else 496. rehumanize(); 497. } 498. if (u.ucreamed) 499. u.ucreamed--; 500. 501. /* Dissipate spell-based protection. */ 502. if (u.usptime) { 503. if (--u.usptime == 0 && u.uspellprot) { 504. u.usptime = u.uspmtime; 505. u.uspellprot--; 506. find_ac(); 507. if (!Blind) 508. Norep("The %s haze around you %s.", hcolor(NH_GOLDEN), 509. u.uspellprot ? "becomes less dense" : "disappears"); 510. } 511. } 512. 513. if (u.ugallop) { 514. if (--u.ugallop == 0L && u.usteed) 515. pline("%s stops galloping.", Monnam(u.usteed)); 516. } 517. 518. was_flying = Flying; 519. for (upp = u.uprops; upp < u.uprops + SIZE(u.uprops); upp++) 520. if ((upp->intrinsic & TIMEOUT) && !(--upp->intrinsic & TIMEOUT)) { 521. kptr = find_delayed_killer((int) (upp - u.uprops)); 522. switch (upp - u.uprops) { 523. case STONED: 524. if (kptr && kptr->name[0]) { 525. killer.format = kptr->format; 526. Strcpy(killer.name, kptr->name); 527. } else { 528. killer.format = NO_KILLER_PREFIX; 529. Strcpy(killer.name, "killed by petrification"); 530. } 531. dealloc_killer(kptr); 532. /* (unlike sliming, you aren't changing form here) */ 533. done(STONING); 534. break; 535. case SLIMED: 536. slimed_to_death(kptr); /* done(TURNED_SLIME) */ 537. break; 538. case VOMITING: 539. make_vomiting(0L, TRUE); 540. break; 541. case SICK: 542. You("die from your illness."); 543. if (kptr && kptr->name[0]) { 544. killer.format = kptr->format; 545. Strcpy(killer.name, kptr->name); 546. } else { 547. killer.format = KILLED_BY_AN; 548. killer.name[0] = 0; /* take the default */ 549. } 550. dealloc_killer(kptr); 551. 552. if ((m_idx = name_to_mon(killer.name)) >= LOW_PM) { 553. if (type_is_pname(&mons[m_idx])) { 554. killer.format = KILLED_BY; 555. } else if (mons[m_idx].geno & G_UNIQ) { 556. Strcpy(killer.name, the(killer.name)); 557. killer.format = KILLED_BY; 558. } 559. } 560. u.usick_type = 0; 561. done(POISONING); 562. break; 563. case FAST: 564. if (!Very_fast) 565. You_feel("yourself slowing down%s.", 566. Fast ? " a bit" : ""); 567. break; 568. case CONFUSION: 569. /* So make_confused works properly */ 570. set_itimeout(&HConfusion, 1L); 571. make_confused(0L, TRUE); 572. if (!Confusion) 573. stop_occupation(); 574. break; 575. case STUNNED: 576. set_itimeout(&HStun, 1L); 577. make_stunned(0L, TRUE); 578. if (!Stunned) 579. stop_occupation(); 580. break; 581. case BLINDED: 582. set_itimeout(&Blinded, 1L); 583. make_blinded(0L, TRUE); 584. if (!Blind) 585. stop_occupation(); 586. break; 587. case DEAF: 588. set_itimeout(&HDeaf, 1L); 589. make_deaf(0L, TRUE); 590. context.botl = TRUE; 591. if (!Deaf) 592. stop_occupation(); 593. break; 594. case INVIS: 595. newsym(u.ux, u.uy); 596. if (!Invis && !BInvis && !Blind) { 597. You(!See_invisible 598. ? "are no longer invisible." 599. : "can no longer see through yourself."); 600. stop_occupation(); 601. } 602. break; 603. case SEE_INVIS: 604. set_mimic_blocking(); /* do special mimic handling */ 605. see_monsters(); /* make invis mons appear */ 606. newsym(u.ux, u.uy); /* make self appear */ 607. stop_occupation(); 608. break; 609. case WOUNDED_LEGS: 610. heal_legs(0); 611. stop_occupation(); 612. break; 613. case HALLUC: 614. set_itimeout(&HHallucination, 1L); 615. (void) make_hallucinated(0L, TRUE, 0L); 616. if (!Hallucination) 617. stop_occupation(); 618. break; 619. case SLEEPY: 620. if (unconscious() || Sleep_resistance) { 621. incr_itimeout(&HSleepy, rnd(100)); 622. } else if (Sleepy) { 623. You("fall asleep."); 624. sleeptime = rnd(20); 625. fall_asleep(-sleeptime, TRUE); 626. incr_itimeout(&HSleepy, sleeptime + rnd(100)); 627. } 628. break; 629. case LEVITATION: 630. (void) float_down(I_SPECIAL | TIMEOUT, 0L); 631. break; 632. case FLYING: 633. /* timed Flying is via #wizintrinsic only */ 634. if (was_flying && !Flying) { 635. context.botl = 1; 636. You("land."); 637. spoteffects(TRUE); 638. } 639. break; 640. case WARN_OF_MON: 641. /* timed Warn_of_mon is via #wizintrinsic only */ 642. if (!Warn_of_mon) { 643. context.warntype.speciesidx = NON_PM; 644. if (context.warntype.species) { 645. You("are no longer warned about %s.", 646. makeplural(context.warntype.species->mname)); 647. context.warntype.species = (struct permonst *) 0; 648. } 649. } 650. break; 651. case PASSES_WALLS: 652. if (!Passes_walls) { 653. if (stuck_in_wall()) 654. You_feel("hemmed in again."); 655. else 656. pline("You're back to your %s self again.", 657. !Upolyd ? "normal" : "unusual"); 658. } 659. break; 660. case STRANGLED: 661. killer.format = KILLED_BY; 662. Strcpy(killer.name, 663. (u.uburied) ? "suffocation" : "strangulation"); 664. done(DIED); 665. /* must be declining to die in explore|wizard mode; 666. treat like being cured of strangulation by prayer */ 667. if (uamul && uamul->otyp == AMULET_OF_STRANGULATION) { 668. Your("amulet vanishes!"); 669. useup(uamul); 670. } 671. break; 672. case FUMBLING: 673. /* call this only when a move took place. */ 674. /* otherwise handle fumbling msgs locally. */ 675. if (u.umoved && !Levitation) { 676. slip_or_trip(); 677. nomul(-2); 678. multi_reason = "fumbling"; 679. nomovemsg = ""; 680. /* The more you are carrying the more likely you 681. * are to make noise when you fumble. Adjustments 682. * to this number must be thoroughly play tested. 683. */ 684. if ((inv_weight() > -500)) { 685. You("make a lot of noise!"); 686. wake_nearby(); 687. } 688. } 689. /* from outside means slippery ice; don't reset 690. counter if that's the only fumble reason */ 691. HFumbling &= ~FROMOUTSIDE; 692. if (Fumbling) 693. incr_itimeout(&HFumbling, rnd(20)); 694. break; 695. case DETECT_MONSTERS: 696. see_monsters(); 697. break; 698. } 699. } 700. 701. run_timers(); 702. } 703. 704. void 705. fall_asleep(how_long, wakeup_msg) 706. int how_long; 707. boolean wakeup_msg; 708. { 709. stop_occupation(); 710. nomul(how_long); 711. multi_reason = "sleeping"; 712. /* generally don't notice sounds while sleeping */ 713. if (wakeup_msg && multi == how_long) { 714. /* caller can follow with a direct call to Hear_again() if 715. there's a need to override this when wakeup_msg is true */ 716. incr_itimeout(&HDeaf, how_long); 717. context.botl = TRUE; 718. afternmv = Hear_again; /* this won't give any messages */ 719. } 720. /* early wakeup from combat won't be possible until next monster turn */ 721. u.usleep = monstermoves; 722. nomovemsg = wakeup_msg ? "You wake up." : You_can_move_again; 723. } 724. 725. /* Attach an egg hatch timeout to the given egg. 726. * when = Time to hatch, usually only passed if re-creating an 727. * existing hatch timer. Pass 0L for random hatch time. 728. */ 729. void 730. attach_egg_hatch_timeout(egg, when) 731. struct obj *egg; 732. long when; 733. { 734. int i; 735. 736. /* stop previous timer, if any */ 737. (void) stop_timer(HATCH_EGG, obj_to_any(egg)); 738. 739. /* 740. * Decide if and when to hatch the egg. The old hatch_it() code tried 741. * once a turn from age 151 to 200 (inclusive), hatching if it rolled 742. * a number x, 1<=x<=age, where x>150. This yields a chance of 743. * hatching > 99.9993%. Mimic that here. 744. */ 745. if (!when) { 746. for (i = (MAX_EGG_HATCH_TIME - 50) + 1; i <= MAX_EGG_HATCH_TIME; i++) 747. if (rnd(i) > 150) { 748. /* egg will hatch */ 749. when = (long) i; 750. break; 751. } 752. } 753. if (when) { 754. (void) start_timer(when, TIMER_OBJECT, HATCH_EGG, obj_to_any(egg)); 755. } 756. } 757. 758. /* prevent an egg from ever hatching */ 759. void 760. kill_egg(egg) 761. struct obj *egg; 762. { 763. /* stop previous timer, if any */ 764. (void) stop_timer(HATCH_EGG, obj_to_any(egg)); 765. } 766. 767. /* timer callback routine: hatch the given egg */ 768. void 769. hatch_egg(arg, timeout) 770. anything *arg; 771. long timeout; 772. { 773. struct obj *egg; 774. struct monst *mon, *mon2; 775. coord cc; 776. xchar x, y; 777. boolean yours, silent, knows_egg = FALSE; 778. boolean cansee_hatchspot = FALSE; 779. int i, mnum, hatchcount = 0; 780. 781. egg = arg->a_obj; 782. /* sterilized while waiting */ 783. if (egg->corpsenm == NON_PM) 784. return; 785. 786. mon = mon2 = (struct monst *) 0; 787. mnum = big_to_little(egg->corpsenm); 788. /* The identity of one's father is learned, not innate */ 789. yours = (egg->spe || (!flags.female && carried(egg) && !rn2(2))); 790. silent = (timeout != monstermoves); /* hatched while away */ 791. 792. /* only can hatch when in INVENT, FLOOR, MINVENT */ 793. if (get_obj_location(egg, &x, &y, 0)) { 794. hatchcount = rnd((int) egg->quan); 795. cansee_hatchspot = cansee(x, y) && !silent; 796. if (!(mons[mnum].geno & G_UNIQ) 797. && !(mvitals[mnum].mvflags & (G_GENOD | G_EXTINCT))) { 798. for (i = hatchcount; i > 0; i--) { 799. if (!enexto(&cc, x, y, &mons[mnum]) 800. || !(mon = makemon(&mons[mnum], cc.x, cc.y, NO_MINVENT))) 801. break; 802. /* tame if your own egg hatches while you're on the 803. same dungeon level, or any dragon egg which hatches 804. while it's in your inventory */ 805. if ((yours && !silent) 806. || (carried(egg) && mon->data->mlet == S_DRAGON)) { 807. if (tamedog(mon, (struct obj *) 0)) { 808. if (carried(egg) && mon->data->mlet != S_DRAGON) 809. mon->mtame = 20; 810. } 811. } 812. if (mvitals[mnum].mvflags & G_EXTINCT) 813. break; /* just made last one */ 814. mon2 = mon; /* in case makemon() fails on 2nd egg */ 815. } 816. if (!mon) 817. mon = mon2; 818. hatchcount -= i; 819. egg->quan -= (long) hatchcount; 820. } 821. #if 0 822. /* 823. * We could possibly hatch while migrating, but the code isn't 824. * set up for it... 825. */ 826. } else if (obj->where == OBJ_MIGRATING) { 827. /* 828. * We can do several things. The first ones that come to 829. * mind are: 830. * + Create the hatched monster then place it on the migrating 831. * mons list. This is tough because all makemon() is made 832. * to place the monster as well. Makemon() also doesn't lend 833. * itself well to splitting off a "not yet placed" subroutine. 834. * + Mark the egg as hatched, then place the monster when we 835. * place the migrating objects. 836. * + Or just kill any egg which gets sent to another level. 837. * Falling is the usual reason such transportation occurs. 838. */ 839. cansee_hatchspot = FALSE; 840. mon = ???; 841. #endif 842. } 843. 844. if (mon) { 845. char monnambuf[BUFSZ], carriedby[BUFSZ]; 846. boolean siblings = (hatchcount > 1), redraw = FALSE; 847. 848. if (cansee_hatchspot) { 849. /* [bug? m_monnam() yields accurate monster type 850. regardless of hallucination] */ 851. Sprintf(monnambuf, "%s%s", siblings ? "some " : "", 852. siblings ? makeplural(m_monnam(mon)) : an(m_monnam(mon))); 853. /* we don't learn the egg type here because learning 854. an egg type requires either seeing the egg hatch 855. or being familiar with the egg already, 856. as well as being able to see the resulting 857. monster, checked below 858. */ 859. } 860. switch (egg->where) { 861. case OBJ_INVENT: 862. knows_egg = TRUE; /* true even if you are blind */ 863. if (!cansee_hatchspot) 864. You_feel("%s %s from your pack!", something, 865. locomotion(mon->data, "drop")); 866. else 867. You_see("%s %s out of your pack!", monnambuf, 868. locomotion(mon->data, "drop")); 869. if (yours) { 870. pline("%s cries sound like \"%s%s\"", 871. siblings ? "Their" : "Its", 872. flags.female ? "mommy" : "daddy", egg->spe ? "." : "?"); 873. } else if (mon->data->mlet == S_DRAGON && !Deaf) { 874. verbalize("Gleep!"); /* Mything eggs :-) */ 875. } 876. break; 877. 878. case OBJ_FLOOR: 879. if (cansee_hatchspot) { 880. knows_egg = TRUE; 881. You_see("%s hatch.", monnambuf); 882. redraw = TRUE; /* update egg's map location */ 883. } 884. break; 885. 886. case OBJ_MINVENT: 887. if (cansee_hatchspot) { 888. /* egg carrying monster might be invisible */ 889. mon2 = egg->ocarry; 890. if (canseemon(mon2) 891. && (!mon2->wormno || cansee(mon2->mx, mon2->my))) { 892. Sprintf(carriedby, "%s pack", 893. s_suffix(a_monnam(mon2))); 894. knows_egg = TRUE; 895. } else if (is_pool(mon->mx, mon->my)) { 896. Strcpy(carriedby, "empty water"); 897. } else { 898. Strcpy(carriedby, "thin air"); 899. } 900. You_see("%s %s out of %s!", monnambuf, 901. locomotion(mon->data, "drop"), carriedby); 902. } 903. break; 904. #if 0 905. case OBJ_MIGRATING: 906. break; 907. #endif 908. default: 909. impossible("egg hatched where? (%d)", (int) egg->where); 910. break; 911. } 912. 913. if (cansee_hatchspot && knows_egg) 914. learn_egg_type(mnum); 915. 916. if (egg->quan > 0) { 917. /* still some eggs left */ 918. /* Instead of ordinary egg timeout use a short one */ 919. attach_egg_hatch_timeout(egg, (long) rnd(12)); 920. } else if (carried(egg)) { 921. useup(egg); 922. } else { 923. /* free egg here because we use it above */ 924. obj_extract_self(egg); 925. obfree(egg, (struct obj *) 0); 926. } 927. if (redraw) 928. newsym(x, y); 929. } 930. } 931. 932. /* Learn to recognize eggs of the given type. */ 933. void 934. learn_egg_type(mnum) 935. int mnum; 936. { 937. /* baby monsters hatch from grown-up eggs */ 938. mnum = little_to_big(mnum); 939. mvitals[mnum].mvflags |= MV_KNOWS_EGG; 940. /* we might have just learned about other eggs being carried */ 941. update_inventory(); 942. } 943. 944. /* Attach a fig_transform timeout to the given figurine. */ 945. void 946. attach_fig_transform_timeout(figurine) 947. struct obj *figurine; 948. { 949. int i; 950. 951. /* stop previous timer, if any */ 952. (void) stop_timer(FIG_TRANSFORM, obj_to_any(figurine)); 953. 954. /* 955. * Decide when to transform the figurine. 956. */ 957. i = rnd(9000) + 200; 958. /* figurine will transform */ 959. (void) start_timer((long) i, TIMER_OBJECT, FIG_TRANSFORM, 960. obj_to_any(figurine)); 961. } 962. 963. /* give a fumble message */ 964. STATIC_OVL void 965. slip_or_trip() 966. { 967. struct obj *otmp = vobj_at(u.ux, u.uy), *otmp2; 968. const char *what; 969. char buf[BUFSZ]; 970. boolean on_foot = TRUE; 971. if (u.usteed) 972. on_foot = FALSE; 973. 974. if (otmp && on_foot && !u.uinwater && is_pool(u.ux, u.uy)) 975. otmp = 0; 976. 977. if (otmp && on_foot) { /* trip over something in particular */ 978. /* 979. If there is only one item, it will have just been named 980. during the move, so refer to by via pronoun; otherwise, 981. if the top item has been or can be seen, refer to it by 982. name; if not, look for rocks to trip over; trip over 983. anonymous "something" if there aren't any rocks. 984. */ 985. what = (iflags.last_msg == PLNMSG_ONE_ITEM_HERE) 986. ? ((otmp->quan == 1L) ? "it" 987. : Hallucination ? "they" : "them") 988. : (otmp->dknown || !Blind) 989. ? doname(otmp) 990. : ((otmp2 = sobj_at(ROCK, u.ux, u.uy)) == 0 991. ? something 992. : (otmp2->quan == 1L ? "a rock" : "some rocks")); 993. if (Hallucination) { 994. what = strcpy(buf, what); 995. buf[0] = highc(buf[0]); 996. pline("Egads! %s bite%s your %s!", what, 997. (!otmp || otmp->quan == 1L) ? "s" : "", body_part(FOOT)); 998. } else { 999. You("trip over %s.", what); 1000. } 1001. if (!uarmf && otmp->otyp == CORPSE 1002. && touch_petrifies(&mons[otmp->corpsenm]) && !Stone_resistance) { 1003. Sprintf(killer.name, "tripping over %s corpse", 1004. an(mons[otmp->corpsenm].mname)); 1005. instapetrify(killer.name); 1006. } 1007. } else if (rn2(3) && is_ice(u.ux, u.uy)) { 1008. pline("%s %s%s on the ice.", 1009. u.usteed ? upstart(x_monnam(u.usteed, 1010. (has_mname(u.usteed)) ? ARTICLE_NONE 1011. : ARTICLE_THE, 1012. (char *) 0, SUPPRESS_SADDLE, FALSE)) 1013. : "You", 1014. rn2(2) ? "slip" : "slide", on_foot ? "" : "s"); 1015. } else { 1016. if (on_foot) { 1017. switch (rn2(4)) { 1018. case 1: 1019. You("trip over your own %s.", 1020. Hallucination ? "elbow" : makeplural(body_part(FOOT))); 1021. break; 1022. case 2: 1023. You("slip %s.", 1024. Hallucination ? "on a banana peel" : "and nearly fall"); 1025. break; 1026. case 3: 1027. You("flounder."); 1028. break; 1029. default: 1030. You("stumble."); 1031. break; 1032. } 1033. } else { 1034. switch (rn2(4)) { 1035. case 1: 1036. Your("%s slip out of the stirrups.", 1037. makeplural(body_part(FOOT))); 1038. break; 1039. case 2: 1040. You("let go of the reins."); 1041. break; 1042. case 3: 1043. You("bang into the saddle-horn."); 1044. break; 1045. default: 1046. You("slide to one side of the saddle."); 1047. break; 1048. } 1049. dismount_steed(DISMOUNT_FELL); 1050. } 1051. } 1052. } 1053. 1054. /* Print a lamp flicker message with tailer. */ 1055. STATIC_OVL void 1056. see_lamp_flicker(obj, tailer) 1057. struct obj *obj; 1058. const char *tailer; 1059. { 1060. switch (obj->where) { 1061. case OBJ_INVENT: 1062. case OBJ_MINVENT: 1063. pline("%s flickers%s.", Yname2(obj), tailer); 1064. break; 1065. case OBJ_FLOOR: 1066. You_see("%s flicker%s.", an(xname(obj)), tailer); 1067. break; 1068. } 1069. } 1070. 1071. /* Print a dimming message for brass lanterns. */ 1072. STATIC_OVL void 1073. lantern_message(obj) 1074. struct obj *obj; 1075. { 1076. /* from adventure */ 1077. switch (obj->where) { 1078. case OBJ_INVENT: 1079. Your("lantern is getting dim."); 1080. if (Hallucination) 1081. pline("Batteries have not been invented yet."); 1082. break; 1083. case OBJ_FLOOR: 1084. You_see("a lantern getting dim."); 1085. break; 1086. case OBJ_MINVENT: 1087. pline("%s lantern is getting dim.", s_suffix(Monnam(obj->ocarry))); 1088. break; 1089. } 1090. } 1091. 1092. /* 1093. * Timeout callback for for objects that are burning. E.g. lamps, candles. 1094. * See begin_burn() for meanings of obj->age and obj->spe. 1095. */ 1096. void 1097. burn_object(arg, timeout) 1098. anything *arg; 1099. long timeout; 1100. { 1101. struct obj *obj = arg->a_obj; 1102. boolean canseeit, many, menorah, need_newsym, need_invupdate; 1103. xchar x, y; 1104. char whose[BUFSZ]; 1105. 1106. menorah = obj->otyp == CANDELABRUM_OF_INVOCATION; 1107. many = menorah ? obj->spe > 1 : obj->quan > 1L; 1108. 1109. /* timeout while away */ 1110. if (timeout != monstermoves) { 1111. long how_long = monstermoves - timeout; 1112. 1113. if (how_long >= obj->age) { 1114. obj->age = 0; 1115. end_burn(obj, FALSE); 1116. 1117. if (menorah) { 1118. obj->spe = 0; /* no more candles */ 1119. obj->owt = weight(obj); 1120. } else if (Is_candle(obj) || obj->otyp == POT_OIL) { 1121. /* get rid of candles and burning oil potions; 1122. we know this object isn't carried by hero, 1123. nor is it migrating */ 1124. obj_extract_self(obj); 1125. obfree(obj, (struct obj *) 0); 1126. obj = (struct obj *) 0; 1127. } 1128. 1129. } else { 1130. obj->age -= how_long; 1131. begin_burn(obj, TRUE); 1132. } 1133. return; 1134. } 1135. 1136. /* only interested in INVENT, FLOOR, and MINVENT */ 1137. if (get_obj_location(obj, &x, &y, 0)) { 1138. canseeit = !Blind && cansee(x, y); 1139. /* set `whose[]' to be "Your " or "Fred's " or "The goblin's " */ 1140. (void) Shk_Your(whose, obj); 1141. } else { 1142. canseeit = FALSE; 1143. } 1144. need_newsym = need_invupdate = FALSE; 1145. 1146. /* obj->age is the age remaining at this point. */ 1147. switch (obj->otyp) { 1148. case POT_OIL: 1149. /* this should only be called when we run out */ 1150. if (canseeit) { 1151. switch (obj->where) { 1152. case OBJ_INVENT: 1153. need_invupdate = TRUE; 1154. /*FALLTHRU*/ 1155. case OBJ_MINVENT: 1156. pline("%spotion of oil has burnt away.", whose); 1157. break; 1158. case OBJ_FLOOR: 1159. You_see("a burning potion of oil go out."); 1160. need_newsym = TRUE; 1161. break; 1162. } 1163. } 1164. end_burn(obj, FALSE); /* turn off light source */ 1165. if (carried(obj)) { 1166. useupall(obj); 1167. } else { 1168. /* clear migrating obj's destination code before obfree 1169. to avoid false complaint of deleting worn item */ 1170. if (obj->where == OBJ_MIGRATING) 1171. obj->owornmask = 0L; 1172. obj_extract_self(obj); 1173. obfree(obj, (struct obj *) 0); 1174. } 1175. obj = (struct obj *) 0; 1176. break; 1177. 1178. case BRASS_LANTERN: 1179. case OIL_LAMP: 1180. switch ((int) obj->age) { 1181. case 150: 1182. case 100: 1183. case 50: 1184. if (canseeit) { 1185. if (obj->otyp == BRASS_LANTERN) 1186. lantern_message(obj); 1187. else 1188. see_lamp_flicker(obj, 1189. obj->age == 50L ? " considerably" : ""); 1190. } 1191. break; 1192. 1193. case 25: 1194. if (canseeit) { 1195. if (obj->otyp == BRASS_LANTERN) 1196. lantern_message(obj); 1197. else { 1198. switch (obj->where) { 1199. case OBJ_INVENT: 1200. case OBJ_MINVENT: 1201. pline("%s seems about to go out.", Yname2(obj)); 1202. break; 1203. case OBJ_FLOOR: 1204. You_see("%s about to go out.", an(xname(obj))); 1205. break; 1206. } 1207. } 1208. } 1209. break; 1210. 1211. case 0: 1212. /* even if blind you'll know if holding it */ 1213. if (canseeit || obj->where == OBJ_INVENT) { 1214. switch (obj->where) { 1215. case OBJ_INVENT: 1216. need_invupdate = TRUE; 1217. /*FALLTHRU*/ 1218. case OBJ_MINVENT: 1219. if (obj->otyp == BRASS_LANTERN) 1220. pline("%slantern has run out of power.", whose); 1221. else 1222. pline("%s has gone out.", Yname2(obj)); 1223. break; 1224. case OBJ_FLOOR: 1225. if (obj->otyp == BRASS_LANTERN) 1226. You_see("a lantern run out of power."); 1227. else 1228. You_see("%s go out.", an(xname(obj))); 1229. break; 1230. } 1231. } 1232. end_burn(obj, FALSE); 1233. break; 1234. 1235. default: 1236. /* 1237. * Someone added fuel to the lamp while it was 1238. * lit. Just fall through and let begin burn 1239. * handle the new age. 1240. */ 1241. break; 1242. } 1243. 1244. if (obj->age) 1245. begin_burn(obj, TRUE); 1246. 1247. break; 1248. 1249. case CANDELABRUM_OF_INVOCATION: 1250. case TALLOW_CANDLE: 1251. case WAX_CANDLE: 1252. switch (obj->age) { 1253. case 75: 1254. if (canseeit) 1255. switch (obj->where) { 1256. case OBJ_INVENT: 1257. case OBJ_MINVENT: 1258. pline("%s%scandle%s getting short.", whose, 1259. menorah ? "candelabrum's " : "", 1260. many ? "s are" : " is"); 1261. break; 1262. case OBJ_FLOOR: 1263. You_see("%scandle%s getting short.", 1264. menorah ? "a candelabrum's " : many ? "some " 1265. : "a ", 1266. many ? "s" : ""); 1267. break; 1268. } 1269. break; 1270. 1271. case 15: 1272. if (canseeit) 1273. switch (obj->where) { 1274. case OBJ_INVENT: 1275. case OBJ_MINVENT: 1276. pline("%s%scandle%s flame%s flicker%s low!", whose, 1277. menorah ? "candelabrum's " : "", many ? "s'" : "'s", 1278. many ? "s" : "", many ? "" : "s"); 1279. break; 1280. case OBJ_FLOOR: 1281. You_see("%scandle%s flame%s flicker low!", 1282. menorah ? "a candelabrum's " : many ? "some " 1283. : "a ", 1284. many ? "s'" : "'s", many ? "s" : ""); 1285. break; 1286. } 1287. break; 1288. 1289. case 0: 1290. /* we know even if blind and in our inventory */ 1291. if (canseeit || obj->where == OBJ_INVENT) { 1292. if (menorah) { 1293. switch (obj->where) { 1294. case OBJ_INVENT: 1295. need_invupdate = TRUE; 1296. /*FALLTHRU*/ 1297. case OBJ_MINVENT: 1298. pline("%scandelabrum's flame%s.", whose, 1299. many ? "s die" : " dies"); 1300. break; 1301. case OBJ_FLOOR: 1302. You_see("a candelabrum's flame%s die.", 1303. many ? "s" : ""); 1304. break; 1305. } 1306. } else { 1307. switch (obj->where) { 1308. case OBJ_INVENT: 1309. /* no need_invupdate for update_inventory() necessary; 1310. useupall() -> freeinv() handles it */ 1311. /*FALLTHRU*/ 1312. case OBJ_MINVENT: 1313. pline("%s %s consumed!", Yname2(obj), 1314. many ? "are" : "is"); 1315. break; 1316. case OBJ_FLOOR: 1317. /* 1318. You see some wax candles consumed! 1319. You see a wax candle consumed! 1320. */ 1321. You_see("%s%s consumed!", many ? "some " : "", 1322. many ? xname(obj) : an(xname(obj))); 1323. need_newsym = TRUE; 1324. break; 1325. } 1326. 1327. /* post message */ 1328. pline(Hallucination 1329. ? (many ? "They shriek!" : "It shrieks!") 1330. : Blind ? "" : (many ? "Their flames die." 1331. : "Its flame dies.")); 1332. } 1333. } 1334. end_burn(obj, FALSE); 1335. 1336. if (menorah) { 1337. obj->spe = 0; 1338. obj->owt = weight(obj); 1339. } else { 1340. if (carried(obj)) { 1341. useupall(obj); 1342. } else { 1343. /* clear migrating obj's destination code 1344. so obfree won't think this item is worn */ 1345. if (obj->where == OBJ_MIGRATING) 1346. obj->owornmask = 0L; 1347. obj_extract_self(obj); 1348. obfree(obj, (struct obj *) 0); 1349. } 1350. obj = (struct obj *) 0; 1351. } 1352. break; /* case [age ==] 0 */ 1353. 1354. default: 1355. /* 1356. * Someone added fuel (candles) to the menorah while 1357. * it was lit. Just fall through and let begin burn 1358. * handle the new age. 1359. */ 1360. break; 1361. } 1362. 1363. if (obj && obj->age) 1364. begin_burn(obj, TRUE); 1365. break; /* case [otyp ==] candelabrum|tallow_candle|wax_candle */ 1366. 1367. default: 1368. impossible("burn_object: unexpeced obj %s", xname(obj)); 1369. break; 1370. } 1371. if (need_newsym) 1372. newsym(x, y); 1373. if (need_invupdate) 1374. update_inventory(); 1375. } 1376. 1377. /* 1378. * Start a burn timeout on the given object. If not "already lit" then 1379. * create a light source for the vision system. There had better not 1380. * be a burn already running on the object. 1381. * 1382. * Magic lamps stay lit as long as there's a genie inside, so don't start 1383. * a timer. 1384. * 1385. * Burn rules: 1386. * potions of oil, lamps & candles: 1387. * age = # of turns of fuel left 1388. * spe = <unused> 1389. * magic lamps: 1390. * age = <unused> 1391. * spe = 0 not lightable, 1 lightable forever 1392. * candelabrum: 1393. * age = # of turns of fuel left 1394. * spe = # of candles 1395. * 1396. * Once the burn begins, the age will be set to the amount of fuel 1397. * remaining _once_the_burn_finishes_. If the burn is terminated 1398. * early then fuel is added back. 1399. * 1400. * This use of age differs from the use of age for corpses and eggs. 1401. * For the latter items, age is when the object was created, so we 1402. * know when it becomes "bad". 1403. * 1404. * This is a "silent" routine - it should not print anything out. 1405. */ 1406. void 1407. begin_burn(obj, already_lit) 1408. struct obj *obj; 1409. boolean already_lit; 1410. { 1411. int radius = 3; 1412. long turns = 0; 1413. boolean do_timer = TRUE; 1414. 1415. if (obj->age == 0 && obj->otyp != MAGIC_LAMP && !artifact_light(obj)) 1416. return; 1417. 1418. switch (obj->otyp) { 1419. case MAGIC_LAMP: 1420. obj->lamplit = 1; 1421. do_timer = FALSE; 1422. break; 1423. 1424. case POT_OIL: 1425. turns = obj->age; 1426. if (obj->odiluted) 1427. turns = (3L * turns + 2L) / 4L; 1428. radius = 1; /* very dim light */ 1429. break; 1430. 1431. case BRASS_LANTERN: 1432. case OIL_LAMP: 1433. /* magic times are 150, 100, 50, 25, and 0 */ 1434. if (obj->age > 150L) 1435. turns = obj->age - 150L; 1436. else if (obj->age > 100L) 1437. turns = obj->age - 100L; 1438. else if (obj->age > 50L) 1439. turns = obj->age - 50L; 1440. else if (obj->age > 25L) 1441. turns = obj->age - 25L; 1442. else 1443. turns = obj->age; 1444. break; 1445. 1446. case CANDELABRUM_OF_INVOCATION: 1447. case TALLOW_CANDLE: 1448. case WAX_CANDLE: 1449. /* magic times are 75, 15, and 0 */ 1450. if (obj->age > 75L) 1451. turns = obj->age - 75L; 1452. else if (obj->age > 15L) 1453. turns = obj->age - 15L; 1454. else 1455. turns = obj->age; 1456. radius = candle_light_range(obj); 1457. break; 1458. 1459. default: 1460. /* [ALI] Support artifact light sources */ 1461. if (artifact_light(obj)) { 1462. obj->lamplit = 1; 1463. do_timer = FALSE; 1464. radius = arti_light_radius(obj); 1465. } else { 1466. impossible("begin burn: unexpected %s", xname(obj)); 1467. turns = obj->age; 1468. } 1469. break; 1470. } 1471. 1472. if (do_timer) { 1473. if (start_timer(turns, TIMER_OBJECT, BURN_OBJECT, obj_to_any(obj))) { 1474. obj->lamplit = 1; 1475. obj->age -= turns; 1476. if (carried(obj) && !already_lit) 1477. update_inventory(); 1478. } else { 1479. obj->lamplit = 0; 1480. } 1481. } else { 1482. if (carried(obj) && !already_lit) 1483. update_inventory(); 1484. } 1485. 1486. if (obj->lamplit && !already_lit) { 1487. xchar x, y; 1488. 1489. if (get_obj_location(obj, &x, &y, CONTAINED_TOO | BURIED_TOO)) 1490. new_light_source(x, y, radius, LS_OBJECT, obj_to_any(obj)); 1491. else 1492. impossible("begin_burn: can't get obj position"); 1493. } 1494. } 1495. 1496. /* 1497. * Stop a burn timeout on the given object if timer attached. Darken 1498. * light source. 1499. */ 1500. void 1501. end_burn(obj, timer_attached) 1502. struct obj *obj; 1503. boolean timer_attached; 1504. { 1505. if (!obj->lamplit) { 1506. impossible("end_burn: obj %s not lit", xname(obj)); 1507. return; 1508. } 1509. 1510. if (obj->otyp == MAGIC_LAMP || artifact_light(obj)) 1511. timer_attached = FALSE; 1512. 1513. if (!timer_attached) { 1514. /* [DS] Cleanup explicitly, since timer cleanup won't happen */ 1515. del_light_source(LS_OBJECT, obj_to_any(obj)); 1516. obj->lamplit = 0; 1517. if (obj->where == OBJ_INVENT) 1518. update_inventory(); 1519. } else if (!stop_timer(BURN_OBJECT, obj_to_any(obj))) 1520. impossible("end_burn: obj %s not timed!", xname(obj)); 1521. } 1522. 1523. /* 1524. * Cleanup a burning object if timer stopped. 1525. */ 1526. static void 1527. cleanup_burn(arg, expire_time) 1528. anything *arg; 1529. long expire_time; 1530. { 1531. struct obj *obj = arg->a_obj; 1532. if (!obj->lamplit) { 1533. impossible("cleanup_burn: obj %s not lit", xname(obj)); 1534. return; 1535. } 1536. 1537. del_light_source(LS_OBJECT, obj_to_any(obj)); 1538. 1539. /* restore unused time */ 1540. obj->age += expire_time - monstermoves; 1541. 1542. obj->lamplit = 0; 1543. 1544. if (obj->where == OBJ_INVENT) 1545. update_inventory(); 1546. } 1547. 1548. void 1549. do_storms() 1550. { 1551. int nstrike; 1552. register int x, y; 1553. int dirx, diry; 1554. int count; 1555. 1556. /* no lightning if not the air level or too often, even then */ 1557. if (!Is_airlevel(&u.uz) || rn2(8)) 1558. return; 1559. 1560. /* the number of strikes is 8-log2(nstrike) */ 1561. for (nstrike = rnd(64); nstrike <= 64; nstrike *= 2) { 1562. count = 0; 1563. do { 1564. x = rnd(COLNO - 1); 1565. y = rn2(ROWNO); 1566. } while (++count < 100 && levl[x][y].typ != CLOUD); 1567. 1568. if (count < 100) { 1569. dirx = rn2(3) - 1; 1570. diry = rn2(3) - 1; 1571. if (dirx != 0 || diry != 0) 1572. buzz(-15, /* "monster" LIGHTNING spell */ 1573. 8, x, y, dirx, diry); 1574. } 1575. } 1576. 1577. if (levl[u.ux][u.uy].typ == CLOUD) { 1578. /* Inside a cloud during a thunder storm is deafening. */ 1579. /* Even if already deaf, we sense the thunder's vibrations. */ 1580. pline("Kaboom!!! Boom!! Boom!!"); 1581. incr_itimeout(&HDeaf, rn1(20, 30)); 1582. context.botl = TRUE; 1583. if (!u.uinvulnerable) { 1584. stop_occupation(); 1585. nomul(-3); 1586. multi_reason = "hiding from thunderstorm"; 1587. nomovemsg = 0; 1588. } 1589. } else 1590. You_hear("a rumbling noise."); 1591. } 1592. 1593. /* ------------------------------------------------------------------------- 1594. */ 1595. /* 1596. * Generic Timeout Functions. 1597. * 1598. * Interface: 1599. * 1600. * General: 1601. * boolean start_timer(long timeout,short kind,short func_index, 1602. * anything *arg) 1603. * Start a timer of kind 'kind' that will expire at time 1604. * monstermoves+'timeout'. Call the function at 'func_index' 1605. * in the timeout table using argument 'arg'. Return TRUE if 1606. * a timer was started. This places the timer on a list ordered 1607. * "sooner" to "later". If an object, increment the object's 1608. * timer count. 1609. * 1610. * long stop_timer(short func_index, anything *arg) 1611. * Stop a timer specified by the (func_index, arg) pair. This 1612. * assumes that such a pair is unique. Return the time the 1613. * timer would have gone off. If no timer is found, return 0. 1614. * If an object, decrement the object's timer count. 1615. * 1616. * long peek_timer(short func_index, anything *arg) 1617. * Return time specified timer will go off (0 if no such timer). 1618. * 1619. * void run_timers(void) 1620. * Call timers that have timed out. 1621. * 1622. * Save/Restore: 1623. * void save_timers(int fd, int mode, int range) 1624. * Save all timers of range 'range'. Range is either global 1625. * or local. Global timers follow game play, local timers 1626. * are saved with a level. Object and monster timers are 1627. * saved using their respective id's instead of pointers. 1628. * 1629. * void restore_timers(int fd, int range, boolean ghostly, long adjust) 1630. * Restore timers of range 'range'. If from a ghost pile, 1631. * adjust the timeout by 'adjust'. The object and monster 1632. * ids are not restored until later. 1633. * 1634. * void relink_timers(boolean ghostly) 1635. * Relink all object and monster timers that had been saved 1636. * using their object's or monster's id number. 1637. * 1638. * Object Specific: 1639. * void obj_move_timers(struct obj *src, struct obj *dest) 1640. * Reassign all timers from src to dest. 1641. * 1642. * void obj_split_timers(struct obj *src, struct obj *dest) 1643. * Duplicate all timers assigned to src and attach them to dest. 1644. * 1645. * void obj_stop_timers(struct obj *obj) 1646. * Stop all timers attached to obj. 1647. * 1648. * boolean obj_has_timer(struct obj *object, short timer_type) 1649. * Check whether object has a timer of type timer_type. 1650. */ 1651. 1652. STATIC_DCL const char *FDECL(kind_name, (SHORT_P)); 1653. STATIC_DCL void FDECL(print_queue, (winid, timer_element *)); 1654. STATIC_DCL void FDECL(insert_timer, (timer_element *)); 1655. STATIC_DCL timer_element *FDECL(remove_timer, 1656. (timer_element **, SHORT_P, ANY_P *)); 1657. STATIC_DCL void FDECL(write_timer, (int, timer_element *)); 1658. STATIC_DCL boolean FDECL(mon_is_local, (struct monst *)); 1659. STATIC_DCL boolean FDECL(timer_is_local, (timer_element *)); 1660. STATIC_DCL int FDECL(maybe_write_timer, (int, int, BOOLEAN_P)); 1661. 1662. /* ordered timer list */ 1663. static timer_element *timer_base; /* "active" */ 1664. static unsigned long timer_id = 1; 1665. 1666. /* If defined, then include names when printing out the timer queue */ 1667. #define VERBOSE_TIMER 1668. 1669. typedef struct { 1670. timeout_proc f, cleanup; 1671. #ifdef VERBOSE_TIMER 1672. const char *name; 1673. #define TTAB(a, b, c) \ 1674. { \ 1675. a, b, c \ 1676. } 1677. #else 1678. #define TTAB(a, b, c) \ 1679. { \ 1680. a, b \ 1681. } 1682. #endif 1683. } ttable; 1684. 1685. /* table of timeout functions */ 1686. static const ttable timeout_funcs[NUM_TIME_FUNCS] = { 1687. TTAB(rot_organic, (timeout_proc) 0, "rot_organic"), 1688. TTAB(rot_corpse, (timeout_proc) 0, "rot_corpse"), 1689. TTAB(revive_mon, (timeout_proc) 0, "revive_mon"), 1690. TTAB(burn_object, cleanup_burn, "burn_object"), 1691. TTAB(hatch_egg, (timeout_proc) 0, "hatch_egg"), 1692. TTAB(fig_transform, (timeout_proc) 0, "fig_transform"), 1693. TTAB(melt_ice_away, (timeout_proc) 0, "melt_ice_away") 1694. }; 1695. #undef TTAB 1696. 1697. STATIC_OVL const char * 1698. kind_name(kind) 1699. short kind; 1700. { 1701. switch (kind) { 1702. case TIMER_LEVEL: 1703. return "level"; 1704. case TIMER_GLOBAL: 1705. return "global"; 1706. case TIMER_OBJECT: 1707. return "object"; 1708. case TIMER_MONSTER: 1709. return "monster"; 1710. } 1711. return "unknown"; 1712. } 1713. 1714. STATIC_OVL void 1715. print_queue(win, base) 1716. winid win; 1717. timer_element *base; 1718. { 1719. timer_element *curr; 1720. char buf[BUFSZ]; 1721. 1722. if (!base) { 1723. putstr(win, 0, " <empty>"); 1724. } else { 1725. putstr(win, 0, "timeout id kind call"); 1726. for (curr = base; curr; curr = curr->next) { 1727. #ifdef VERBOSE_TIMER 1728. Sprintf(buf, " %4ld %4ld %-6s %s(%s)", curr->timeout, 1729. curr->tid, kind_name(curr->kind), 1730. timeout_funcs[curr->func_index].name, 1731. fmt_ptr((genericptr_t) curr->arg.a_void)); 1732. #else 1733. Sprintf(buf, " %4ld %4ld %-6s #%d(%s)", curr->timeout, 1734. curr->tid, kind_name(curr->kind), curr->func_index, 1735. fmt_ptr((genericptr_t) curr->arg.a_void)); 1736. #endif 1737. putstr(win, 0, buf); 1738. } 1739. } 1740. } 1741. 1742. int 1743. wiz_timeout_queue() 1744. { 1745. winid win; 1746. char buf[BUFSZ]; 1747. const char *propname; 1748. long intrinsic; 1749. int i, p, count, longestlen, ln, specindx = 0; 1750. 1751. win = create_nhwindow(NHW_MENU); /* corner text window */ 1752. if (win == WIN_ERR) 1753. return 0; 1754. 1755. Sprintf(buf, "Current time = %ld.", monstermoves); 1756. putstr(win, 0, buf); 1757. putstr(win, 0, ""); 1758. putstr(win, 0, "Active timeout queue:"); 1759. putstr(win, 0, ""); 1760. print_queue(win, timer_base); 1761. 1762. /* Timed properies: 1763. * check every one; the majority can't obtain temporary timeouts in 1764. * normal play but those can be forced via the #wizintrinsic command. 1765. */ 1766. count = longestlen = 0; 1767. for (i = 0; (propname = propertynames[i].prop_name) != 0; ++i) { 1768. p = propertynames[i].prop_num; 1769. intrinsic = u.uprops[p].intrinsic; 1770. if (intrinsic & TIMEOUT) { 1771. ++count; 1772. if ((ln = (int) strlen(propname)) > longestlen) 1773. longestlen = ln; 1774. } 1775. if (specindx == 0 && p == FIRE_RES) 1776. specindx = i; 1777. } 1778. putstr(win, 0, ""); 1779. if (!count) { 1780. putstr(win, 0, "No timed properties."); 1781. } else { 1782. putstr(win, 0, "Timed properties:"); 1783. putstr(win, 0, ""); 1784. for (i = 0; (propname = propertynames[i].prop_name) != 0; ++i) { 1785. p = propertynames[i].prop_num; 1786. intrinsic = u.uprops[p].intrinsic; 1787. if (intrinsic & TIMEOUT) { 1788. if (specindx > 0 && i >= specindx) { 1789. putstr(win, 0, " -- settable via #wizinstrinc only --"); 1790. specindx = 0; 1791. } 1792. /* timeout value can be up to 16777215 (0x00ffffff) but 1793. width of 4 digits should result in values lining up 1794. almost all the time (if/when they don't, it won't 1795. look nice but the information will still be accurate) */ 1796. Sprintf(buf, " %*s %4ld", -longestlen, propname, 1797. (intrinsic & TIMEOUT)); 1798. putstr(win, 0, buf); 1799. } 1800. } 1801. } 1802. display_nhwindow(win, FALSE); 1803. destroy_nhwindow(win); 1804. 1805. return 0; 1806. } 1807. 1808. void 1809. timer_sanity_check() 1810. { 1811. timer_element *curr; 1812. 1813. /* this should be much more complete */ 1814. for (curr = timer_base; curr; curr = curr->next) 1815. if (curr->kind == TIMER_OBJECT) { 1816. struct obj *obj = curr->arg.a_obj; 1817. 1818. if (obj->timed == 0) { 1819. impossible("timer sanity: untimed obj %s, timer %ld", 1820. fmt_ptr((genericptr_t) obj), curr->tid); 1821. } 1822. } 1823. } 1824. 1825. /* 1826. * Pick off timeout elements from the global queue and call their functions. 1827. * Do this until their time is less than or equal to the move count. 1828. */ 1829. void 1830. run_timers() 1831. { 1832. timer_element *curr; 1833. 1834. /* 1835. * Always use the first element. Elements may be added or deleted at 1836. * any time. The list is ordered, we are done when the first element 1837. * is in the future. 1838. */ 1839. while (timer_base && timer_base->timeout <= monstermoves) { 1840. curr = timer_base; 1841. timer_base = curr->next; 1842. 1843. if (curr->kind == TIMER_OBJECT) 1844. (curr->arg.a_obj)->timed--; 1845. (*timeout_funcs[curr->func_index].f)(&curr->arg, curr->timeout); 1846. free((genericptr_t) curr); 1847. } 1848. } 1849. 1850. /* 1851. * Start a timer. Return TRUE if successful. 1852. */ 1853. boolean 1854. start_timer(when, kind, func_index, arg) 1855. long when; 1856. short kind; 1857. short func_index; 1858. anything *arg; 1859. { 1860. timer_element *gnu; 1861. 1862. if (func_index < 0 || func_index >= NUM_TIME_FUNCS) 1863. panic("start_timer"); 1864. 1865. gnu = (timer_element *) alloc(sizeof(timer_element)); 1866. (void) memset((genericptr_t)gnu, 0, sizeof(timer_element)); 1867. gnu->next = 0; 1868. gnu->tid = timer_id++; 1869. gnu->timeout = monstermoves + when; 1870. gnu->kind = kind; 1871. gnu->needs_fixup = 0; 1872. gnu->func_index = func_index; 1873. gnu->arg = *arg; 1874. insert_timer(gnu); 1875. 1876. if (kind == TIMER_OBJECT) /* increment object's timed count */ 1877. (arg->a_obj)->timed++; 1878. 1879. /* should check for duplicates and fail if any */ 1880. return TRUE; 1881. } 1882. 1883. /* 1884. * Remove the timer from the current list and free it up. Return the time 1885. * remaining until it would have gone off, 0 if not found. 1886. */ 1887. long 1888. stop_timer(func_index, arg) 1889. short func_index; 1890. anything *arg; 1891. { 1892. timer_element *doomed; 1893. long timeout; 1894. 1895. doomed = remove_timer(&timer_base, func_index, arg); 1896. 1897. if (doomed) { 1898. timeout = doomed->timeout; 1899. if (doomed->kind == TIMER_OBJECT) 1900. (arg->a_obj)->timed--; 1901. if (timeout_funcs[doomed->func_index].cleanup) 1902. (*timeout_funcs[doomed->func_index].cleanup)(arg, timeout); 1903. free((genericptr_t) doomed); 1904. return (timeout - monstermoves); 1905. } 1906. return 0L; 1907. } 1908. 1909. /* 1910. * Find the timeout of specified timer; return 0 if none. 1911. */ 1912. long 1913. peek_timer(type, arg) 1914. short type; 1915. anything *arg; 1916. { 1917. timer_element *curr; 1918. 1919. for (curr = timer_base; curr; curr = curr->next) { 1920. if (curr->func_index == type && curr->arg.a_void == arg->a_void) 1921. return curr->timeout; 1922. } 1923. return 0L; 1924. } 1925. 1926. /* 1927. * Move all object timers from src to dest, leaving src untimed. 1928. */ 1929. void 1930. obj_move_timers(src, dest) 1931. struct obj *src, *dest; 1932. { 1933. int count; 1934. timer_element *curr; 1935. 1936. for (count = 0, curr = timer_base; curr; curr = curr->next) 1937. if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == src) { 1938. curr->arg.a_obj = dest; 1939. dest->timed++; 1940. count++; 1941. } 1942. if (count != src->timed) 1943. panic("obj_move_timers"); 1944. src->timed = 0; 1945. } 1946. 1947. /* 1948. * Find all object timers and duplicate them for the new object "dest". 1949. */ 1950. void 1951. obj_split_timers(src, dest) 1952. struct obj *src, *dest; 1953. { 1954. timer_element *curr, *next_timer = 0; 1955. 1956. for (curr = timer_base; curr; curr = next_timer) { 1957. next_timer = curr->next; /* things may be inserted */ 1958. if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == src) { 1959. (void) start_timer(curr->timeout - monstermoves, TIMER_OBJECT, 1960. curr->func_index, obj_to_any(dest)); 1961. } 1962. } 1963. } 1964. 1965. /* 1966. * Stop all timers attached to this object. We can get away with this because 1967. * all object pointers are unique. 1968. */ 1969. void 1970. obj_stop_timers(obj) 1971. struct obj *obj; 1972. { 1973. timer_element *curr, *prev, *next_timer = 0; 1974. 1975. for (prev = 0, curr = timer_base; curr; curr = next_timer) { 1976. next_timer = curr->next; 1977. if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == obj) { 1978. if (prev) 1979. prev->next = curr->next; 1980. else 1981. timer_base = curr->next; 1982. if (timeout_funcs[curr->func_index].cleanup) 1983. (*timeout_funcs[curr->func_index].cleanup)(&curr->arg, 1984. curr->timeout); 1985. free((genericptr_t) curr); 1986. } else { 1987. prev = curr; 1988. } 1989. } 1990. obj->timed = 0; 1991. } 1992. 1993. /* 1994. * Check whether object has a timer of type timer_type. 1995. */ 1996. boolean 1997. obj_has_timer(object, timer_type) 1998. struct obj *object; 1999. short timer_type; 2000. { 2001. long timeout = peek_timer(timer_type, obj_to_any(object)); 2002. 2003. return (boolean) (timeout != 0L); 2004. } 2005. 2006. /* 2007. * Stop all timers of index func_index at this spot. 2008. * 2009. */ 2010. void 2011. spot_stop_timers(x, y, func_index) 2012. xchar x, y; 2013. short func_index; 2014. { 2015. timer_element *curr, *prev, *next_timer = 0; 2016. long where = (((long) x << 16) | ((long) y)); 2017. 2018. for (prev = 0, curr = timer_base; curr; curr = next_timer) { 2019. next_timer = curr->next; 2020. if (curr->kind == TIMER_LEVEL && curr->func_index == func_index 2021. && curr->arg.a_long == where) { 2022. if (prev) 2023. prev->next = curr->next; 2024. else 2025. timer_base = curr->next; 2026. if (timeout_funcs[curr->func_index].cleanup) 2027. (*timeout_funcs[curr->func_index].cleanup)(&curr->arg, 2028. curr->timeout); 2029. free((genericptr_t) curr); 2030. } else { 2031. prev = curr; 2032. } 2033. } 2034. } 2035. 2036. /* 2037. * When is the spot timer of type func_index going to expire? 2038. * Returns 0L if no such timer. 2039. */ 2040. long 2041. spot_time_expires(x, y, func_index) 2042. xchar x, y; 2043. short func_index; 2044. { 2045. timer_element *curr; 2046. long where = (((long) x << 16) | ((long) y)); 2047. 2048. for (curr = timer_base; curr; curr = curr->next) { 2049. if (curr->kind == TIMER_LEVEL && curr->func_index == func_index 2050. && curr->arg.a_long == where) 2051. return curr->timeout; 2052. } 2053. return 0L; 2054. } 2055. 2056. long 2057. spot_time_left(x, y, func_index) 2058. xchar x, y; 2059. short func_index; 2060. { 2061. long expires = spot_time_expires(x, y, func_index); 2062. return (expires > 0L) ? expires - monstermoves : 0L; 2063. } 2064. 2065. /* Insert timer into the global queue */ 2066. STATIC_OVL void 2067. insert_timer(gnu) 2068. timer_element *gnu; 2069. { 2070. timer_element *curr, *prev; 2071. 2072. for (prev = 0, curr = timer_base; curr; prev = curr, curr = curr->next) 2073. if (curr->timeout >= gnu->timeout) 2074. break; 2075. 2076. gnu->next = curr; 2077. if (prev) 2078. prev->next = gnu; 2079. else 2080. timer_base = gnu; 2081. } 2082. 2083. STATIC_OVL timer_element * 2084. remove_timer(base, func_index, arg) 2085. timer_element **base; 2086. short func_index; 2087. anything *arg; 2088. { 2089. timer_element *prev, *curr; 2090. 2091. for (prev = 0, curr = *base; curr; prev = curr, curr = curr->next) 2092. if (curr->func_index == func_index && curr->arg.a_void == arg->a_void) 2093. break; 2094. 2095. if (curr) { 2096. if (prev) 2097. prev->next = curr->next; 2098. else 2099. *base = curr->next; 2100. } 2101. 2102. return curr; 2103. } 2104. 2105. STATIC_OVL void 2106. write_timer(fd, timer) 2107. int fd; 2108. timer_element *timer; 2109. { 2110. anything arg_save; 2111. 2112. arg_save = zeroany; 2113. switch (timer->kind) { 2114. case TIMER_GLOBAL: 2115. case TIMER_LEVEL: 2116. /* assume no pointers in arg */ 2117. bwrite(fd, (genericptr_t) timer, sizeof(timer_element)); 2118. break; 2119. 2120. case TIMER_OBJECT: 2121. if (timer->needs_fixup) 2122. bwrite(fd, (genericptr_t) timer, sizeof(timer_element)); 2123. else { 2124. /* replace object pointer with id */ 2125. arg_save.a_obj = timer->arg.a_obj; 2126. timer->arg = zeroany; 2127. timer->arg.a_uint = (arg_save.a_obj)->o_id; 2128. timer->needs_fixup = 1; 2129. bwrite(fd, (genericptr_t) timer, sizeof(timer_element)); 2130. timer->arg.a_obj = arg_save.a_obj; 2131. timer->needs_fixup = 0; 2132. } 2133. break; 2134. 2135. case TIMER_MONSTER: 2136. if (timer->needs_fixup) 2137. bwrite(fd, (genericptr_t) timer, sizeof(timer_element)); 2138. else { 2139. /* replace monster pointer with id */ 2140. arg_save.a_monst = timer->arg.a_monst; 2141. timer->arg = zeroany; 2142. timer->arg.a_uint = (arg_save.a_monst)->m_id; 2143. timer->needs_fixup = 1; 2144. bwrite(fd, (genericptr_t) timer, sizeof(timer_element)); 2145. timer->arg.a_monst = arg_save.a_monst; 2146. timer->needs_fixup = 0; 2147. } 2148. break; 2149. 2150. default: 2151. panic("write_timer"); 2152. break; 2153. } 2154. } 2155. 2156. /* 2157. * Return TRUE if the object will stay on the level when the level is 2158. * saved. 2159. */ 2160. boolean 2161. obj_is_local(obj) 2162. struct obj *obj; 2163. { 2164. switch (obj->where) { 2165. case OBJ_INVENT: 2166. case OBJ_MIGRATING: 2167. return FALSE; 2168. case OBJ_FLOOR: 2169. case OBJ_BURIED: 2170. return TRUE; 2171. case OBJ_CONTAINED: 2172. return obj_is_local(obj->ocontainer); 2173. case OBJ_MINVENT: 2174. return mon_is_local(obj->ocarry); 2175. } 2176. panic("obj_is_local"); 2177. return FALSE; 2178. } 2179. 2180. /* 2181. * Return TRUE if the given monster will stay on the level when the 2182. * level is saved. 2183. */ 2184. STATIC_OVL boolean 2185. mon_is_local(mon) 2186. struct monst *mon; 2187. { 2188. struct monst *curr; 2189. 2190. for (curr = migrating_mons; curr; curr = curr->nmon) 2191. if (curr == mon) 2192. return FALSE; 2193. /* `mydogs' is used during level changes, never saved and restored */ 2194. for (curr = mydogs; curr; curr = curr->nmon) 2195. if (curr == mon) 2196. return FALSE; 2197. return TRUE; 2198. } 2199. 2200. /* 2201. * Return TRUE if the timer is attached to something that will stay on the 2202. * level when the level is saved. 2203. */ 2204. STATIC_OVL boolean 2205. timer_is_local(timer) 2206. timer_element *timer; 2207. { 2208. switch (timer->kind) { 2209. case TIMER_LEVEL: 2210. return TRUE; 2211. case TIMER_GLOBAL: 2212. return FALSE; 2213. case TIMER_OBJECT: 2214. return obj_is_local(timer->arg.a_obj); 2215. case TIMER_MONSTER: 2216. return mon_is_local(timer->arg.a_monst); 2217. } 2218. panic("timer_is_local"); 2219. return FALSE; 2220. } 2221. 2222. /* 2223. * Part of the save routine. Count up the number of timers that would 2224. * be written. If write_it is true, actually write the timer. 2225. */ 2226. STATIC_OVL int 2227. maybe_write_timer(fd, range, write_it) 2228. int fd, range; 2229. boolean write_it; 2230. { 2231. int count = 0; 2232. timer_element *curr; 2233. 2234. for (curr = timer_base; curr; curr = curr->next) { 2235. if (range == RANGE_GLOBAL) { 2236. /* global timers */ 2237. 2238. if (!timer_is_local(curr)) { 2239. count++; 2240. if (write_it) 2241. write_timer(fd, curr); 2242. } 2243. 2244. } else { 2245. /* local timers */ 2246. 2247. if (timer_is_local(curr)) { 2248. count++; 2249. if (write_it) 2250. write_timer(fd, curr); 2251. } 2252. } 2253. } 2254. 2255. return count; 2256. } 2257. 2258. /* 2259. * Save part of the timer list. The parameter 'range' specifies either 2260. * global or level timers to save. The timer ID is saved with the global 2261. * timers. 2262. * 2263. * Global range: 2264. * + timeouts that follow the hero (global) 2265. * + timeouts that follow obj & monst that are migrating 2266. * 2267. * Level range: 2268. * + timeouts that are level specific (e.g. storms) 2269. * + timeouts that stay with the level (obj & monst) 2270. */ 2271. void 2272. save_timers(fd, mode, range) 2273. int fd, mode, range; 2274. { 2275. timer_element *curr, *prev, *next_timer = 0; 2276. int count; 2277. 2278. if (perform_bwrite(mode)) { 2279. if (range == RANGE_GLOBAL) 2280. bwrite(fd, (genericptr_t) &timer_id, sizeof(timer_id)); 2281. 2282. count = maybe_write_timer(fd, range, FALSE); 2283. bwrite(fd, (genericptr_t) &count, sizeof count); 2284. (void) maybe_write_timer(fd, range, TRUE); 2285. } 2286. 2287. if (release_data(mode)) { 2288. for (prev = 0, curr = timer_base; curr; curr = next_timer) { 2289. next_timer = curr->next; /* in case curr is removed */ 2290. 2291. if (!(!!(range == RANGE_LEVEL) ^ !!timer_is_local(curr))) { 2292. if (prev) 2293. prev->next = curr->next; 2294. else 2295. timer_base = curr->next; 2296. free((genericptr_t) curr); 2297. /* prev stays the same */ 2298. } else { 2299. prev = curr; 2300. } 2301. } 2302. } 2303. } 2304. 2305. /* 2306. * Pull in the structures from disk, but don't recalculate the object and 2307. * monster pointers. 2308. */ 2309. void 2310. restore_timers(fd, range, ghostly, adjust) 2311. int fd, range; 2312. boolean ghostly; /* restoring from a ghost level */ 2313. long adjust; /* how much to adjust timeout */ 2314. { 2315. int count; 2316. timer_element *curr; 2317. 2318. if (range == RANGE_GLOBAL) 2319. mread(fd, (genericptr_t) &timer_id, sizeof timer_id); 2320. 2321. /* restore elements */ 2322. mread(fd, (genericptr_t) &count, sizeof count); 2323. while (count-- > 0) { 2324. curr = (timer_element *) alloc(sizeof(timer_element)); 2325. mread(fd, (genericptr_t) curr, sizeof(timer_element)); 2326. if (ghostly) 2327. curr->timeout += adjust; 2328. insert_timer(curr); 2329. } 2330. } 2331. 2332. /* to support '#stats' wizard-mode command */ 2333. void 2334. timer_stats(hdrfmt, hdrbuf, count, size) 2335. const char *hdrfmt; 2336. char *hdrbuf; 2337. long *count, *size; 2338. { 2339. timer_element *te; 2340. 2341. Sprintf(hdrbuf, hdrfmt, (long) sizeof (timer_element)); 2342. *count = *size = 0L; 2343. for (te = timer_base; te; te = te->next) { 2344. ++*count; 2345. *size += (long) sizeof *te; 2346. } 2347. } 2348. 2349. /* reset all timers that are marked for reseting */ 2350. void 2351. relink_timers(ghostly) 2352. boolean ghostly; 2353. { 2354. timer_element *curr; 2355. unsigned nid; 2356. 2357. for (curr = timer_base; curr; curr = curr->next) { 2358. if (curr->needs_fixup) { 2359. if (curr->kind == TIMER_OBJECT) { 2360. if (ghostly) { 2361. if (!lookup_id_mapping(curr->arg.a_uint, &nid)) 2362. panic("relink_timers 1"); 2363. } else 2364. nid = curr->arg.a_uint; 2365. curr->arg.a_obj = find_oid(nid); 2366. if (!curr->arg.a_obj) 2367. panic("cant find o_id %d", nid); 2368. curr->needs_fixup = 0; 2369. } else if (curr->kind == TIMER_MONSTER) { 2370. panic("relink_timers: no monster timer implemented"); 2371. } else 2372. panic("relink_timers 2"); 2373. } 2374. } 2375. } 2376. 2377. /*timeout.c*/