Source:NetHack 5.0.0/src/light.c
Jump to navigation
Jump to search
Below is the full text to light.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/light.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 5.0 light.c $NHDT-Date: 1773375430 2026/03/12 20:17:10 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.82 $ */ 2. /* Copyright (c) Dean Luick, 1994 */ 3. /* NetHack may be freely redistributed. See license for details. */ 4. 5. #include "hack.h" 6. 7. /* 8. * Mobile light sources. 9. * 10. * This implementation minimizes memory at the expense of extra 11. * recalculations. 12. * 13. * Light sources are "things" that have a physical position and range. 14. * They have a type, which gives us information about them. Currently 15. * they are only attached to objects and monsters. Note well: the 16. * polymorphed-player handling assumes that gy.youmonst.m_id will 17. * always remain 1 and gy.youmonst.mx will always remain 0. 18. * 19. * Light sources, like timers, either follow game play (RANGE_GLOBAL) or 20. * stay on a level (RANGE_LEVEL). Light sources are unique by their 21. * (type, id) pair. For light sources attached to objects, this id 22. * is a pointer to the object. 23. * 24. * The major working function is do_light_sources(). It is called 25. * when the vision system is recreating its "could see" array. Here 26. * we add a flag (TEMP_LIT) to the array for all locations that are lit 27. * via a light source. The bad part of this is that we have to 28. * re-calculate the LOS of each light source every time the vision 29. * system runs. Even if the light sources and any topology (vision blocking 30. * positions) have not changed. The good part is that no extra memory 31. * is used, plus we don't have to figure out how far the sources have moved, 32. * or if the topology has changed. 33. * 34. * The structure of the save/restore mechanism is amazingly similar to 35. * the timer save/restore. This is because they both have the same 36. * principals of having pointers into objects that must be recalculated 37. * across saves and restores. 38. */ 39. 40. /* flags */ 41. #define LSF_SHOW 0x1 /* display the light source */ 42. #define LSF_NEEDS_FIXUP 0x2 /* need oid fixup */ 43. #define LSF_IS_PROBLEMATIC 0x4 /* impossible situation encountered */ 44. 45. #ifndef SFCTOOL 46. staticfn light_source *new_light_core(coordxy, coordxy, 47. int, int, anything *) NONNULLPTRS; 48. staticfn void delete_ls(light_source *); 49. staticfn void discard_flashes(void); 50. staticfn void write_ls(NHFILE *, light_source *); 51. staticfn int maybe_write_ls(NHFILE *, int, boolean); 52. staticfn unsigned whereis_mon(struct monst *, unsigned); 53. 54. /* imported from vision.c, for small circles */ 55. extern const coordxy circle_data[]; 56. extern const coordxy circle_start[]; 57. 58. 59. /* Create a new light source. Caller (and extern.h) doesn't need to know 60. anything about type 'light_source'. */ 61. void 62. new_light_source(coordxy x, coordxy y, int range, int type, anything *id) 63. { 64. (void) new_light_core(x, y, range, type, id); 65. } 66. 67. /* Create a new light source and return it. Only used within this file. */ 68. staticfn light_source * 69. new_light_core(coordxy x, coordxy y, int range, int type, anything *id) 70. { 71. light_source *ls; 72. 73. if (range > MAX_RADIUS || range < 0 74. /* camera flash uses radius 0 and passes Null object */ 75. || (range == 0 && (type != LS_OBJECT || id->a_obj != 0))) { 76. impossible("new_light_source: illegal range %d", range); 77. return (light_source *) 0; 78. } 79. 80. ls = (light_source *) alloc(sizeof *ls); 81. 82. (void) memset((genericptr_t) ls, 0, sizeof (light_source)); 83. ls->next = gl.light_base; 84. ls->x = x; 85. ls->y = y; 86. ls->range = range; 87. ls->type = type; 88. ls->id = *id; 89. ls->flags = 0; 90. gl.light_base = ls; 91. 92. gv.vision_full_recalc = 1; /* make the source show up */ 93. return ls; 94. } 95. 96. /* Find and delete a light source. 97. Assumes at most one light source is attached to an object at a time. */ 98. void 99. del_light_source(int type, anything *id) 100. { 101. light_source *curr; 102. anything tmp_id; 103. 104. tmp_id = cg.zeroany; 105. /* need to be prepared for dealing a with light source which 106. has only been partially restored during a level change 107. (in particular: chameleon vs prot. from shape changers) */ 108. switch (type) { 109. case LS_NONE: 110. impossible("del_light_source:type=none"); 111. tmp_id.a_uint = 0; 112. break; 113. case LS_OBJECT: 114. tmp_id.a_uint = id->a_obj ? id->a_obj->o_id : 0; 115. break; 116. case LS_MONSTER: 117. tmp_id.a_uint = id->a_monst->m_id; 118. break; 119. default: 120. tmp_id.a_uint = 0; 121. break; 122. } 123. 124. /* find the light source from its id */ 125. for (curr = gl.light_base; curr; curr = curr->next) { 126. if (curr->type != type) 127. continue; 128. if (curr->id.a_obj 129. == ((curr->flags & LSF_NEEDS_FIXUP) ? tmp_id.a_obj : id->a_obj)) 130. break; 131. } 132. if (curr) { 133. delete_ls(curr); 134. } else { 135. impossible("del_light_source: not found type=%d, id=%s", type, 136. fmt_ptr((genericptr_t) id->a_obj)); 137. } 138. } 139. 140. /* remove a light source from the light_base list and free it */ 141. staticfn void 142. delete_ls(light_source *ls) 143. { 144. light_source *curr, *prev; 145. 146. for (prev = 0, curr = gl.light_base; curr; 147. prev = curr, curr = curr->next) { 148. if (curr == ls) { 149. if (prev) 150. prev->next = curr->next; 151. else 152. gl.light_base = curr->next; 153. break; 154. } 155. } 156. if (curr) { 157. assert(curr == ls); 158. (void) memset((genericptr_t) ls, 0, sizeof(light_source)); 159. free((genericptr_t) ls); 160. gv.vision_full_recalc = 1; 161. } else { 162. impossible("delete_ls not found, ls=%s", fmt_ptr((genericptr_t) ls)); 163. } 164. return; 165. } 166. 167. /* Mark locations that are temporarily lit via mobile light sources. */ 168. void 169. do_light_sources(seenV **cs_rows) 170. { 171. coordxy x, y, min_x, max_x, max_y; 172. int offset; 173. const coordxy *limits; 174. short at_hero_range = 0; 175. light_source *ls; 176. seenV *row; 177. 178. for (ls = gl.light_base; ls; ls = ls->next) { 179. ls->flags &= ~LSF_SHOW; 180. 181. /* 182. * Check for moved light sources. It may be possible to 183. * save some effort if an object has not moved, but not in 184. * the current setup -- we need to recalculate for every 185. * vision recalc. 186. */ 187. if (ls->type == LS_OBJECT) { 188. if (ls->range == 0 /* camera flash; caller has set ls->{x,y} */ 189. || get_obj_location(ls->id.a_obj, &ls->x, &ls->y, 0)) 190. ls->flags |= LSF_SHOW; 191. } else if (ls->type == LS_MONSTER) { 192. if (get_mon_location(ls->id.a_monst, &ls->x, &ls->y, 0)) 193. ls->flags |= LSF_SHOW; 194. } 195. 196. /* minor optimization: don't bother with duplicate light sources 197. at hero */ 198. if (u_at(ls->x, ls->y)) { 199. if (at_hero_range >= ls->range) 200. ls->flags &= ~LSF_SHOW; 201. else 202. at_hero_range = ls->range; 203. } 204. 205. if (ls->flags & LSF_SHOW) { 206. /* 207. * Walk the points in the circle and see if they are 208. * visible from the center. If so, mark'em. 209. * 210. * Kevin's tests indicated that doing this brute-force 211. * method is faster for radius <= 3 (or so). 212. */ 213. limits = circle_ptr(ls->range); 214. if ((max_y = (ls->y + ls->range)) >= ROWNO) 215. max_y = ROWNO - 1; 216. if ((y = (ls->y - ls->range)) < 0) 217. y = 0; 218. for (; y <= max_y; y++) { 219. row = cs_rows[y]; 220. offset = limits[abs(y - ls->y)]; 221. if ((min_x = (ls->x - offset)) < 1) 222. min_x = 1; 223. if ((max_x = (ls->x + offset)) >= COLNO) 224. max_x = COLNO - 1; 225. 226. if (u_at(ls->x, ls->y)) { 227. /* 228. * If the light source is located at the hero, then 229. * we can use the COULD_SEE bits already calculated 230. * by the vision system. More importantly than 231. * this optimization, is that it allows the vision 232. * system to correct problems with clear_path(). 233. * The function clear_path() is a simple LOS 234. * path checker that doesn't go out of its way to 235. * make things look "correct". The vision system 236. * does this. 237. */ 238. for (x = min_x; x <= max_x; x++) 239. if (row[x] & COULD_SEE) 240. row[x] |= TEMP_LIT; 241. } else { 242. for (x = min_x; x <= max_x; x++) 243. if ((ls->x == x && ls->y == y) 244. || clear_path((int) ls->x, (int) ls->y, x, y)) 245. row[x] |= TEMP_LIT; 246. } 247. } 248. } 249. } 250. } 251. 252. /* lit 'obj' has been thrown or kicked and is passing through x,y on the 253. way to its destination; show its light so that hero has a chance to 254. remember terrain, objects, and monsters being revealed; 255. if 'obj' is Null, <x,y> is being hit by a camera's light flash */ 256. void 257. show_transient_light(struct obj *obj, coordxy x, coordxy y) 258. { 259. light_source *ls = 0; 260. anything cameraflash; 261. struct monst *mon; 262. int radius_squared; 263. 264. /* Null object indicates camera flash */ 265. if (!obj) { 266. /* no need to temporarily light an already lit spot */ 267. if (levl[x][y].lit) 268. return; 269. 270. cameraflash = cg.zeroany; 271. /* radius 0 will just light <x,y>; cameraflash.a_obj is Null */ 272. ls = new_light_core(x, y, 0, LS_OBJECT, &cameraflash); 273. /* pacify static analysis; 'ls' is never Null for 274. new_light_core(,,0,LS_OBJECT,&zeroany) */ 275. assert(ls != NULL); 276. } else { 277. /* thrown or kicked object which is emitting light; validate its 278. light source to obtain its radius (for monster sightings) */ 279. for (ls = gl.light_base; ls; ls = ls->next) { 280. if (ls->type != LS_OBJECT) 281. continue; 282. if (ls->id.a_obj == obj) 283. break; 284. } 285. assert(obj != NULL); /* necessary condition to get into this 'else' */ 286. if (!ls || obj->where != OBJ_FREE) { 287. impossible("transient light %s %s %s not %s?", 288. obj->lamplit ? "lit" : "unlit", 289. simpleonames(obj), otense(obj, "are"), 290. !ls ? "a light source" : "free"); 291. return; 292. } 293. } 294. 295. if (obj) /* put lit candle or lamp temporarily on the map */ 296. place_object(obj, gb.bhitpos.x, gb.bhitpos.y); 297. else /* camera flash: no object; directly set light source's location */ 298. ls->x = x, ls->y = y; 299. 300. /* full recalc; runs do_light_sources() */ 301. vision_recalc(0); 302. flush_screen(0); 303. 304. radius_squared = ls->range * ls->range; 305. for (mon = fmon; mon; mon = mon->nmon) { 306. if (DEADMONSTER(mon) || (mon->isgd && !mon->mx)) 307. continue; 308. /* light range is the radius of a circle and we're limiting 309. canseemon() to a square enclosing that circle, but setting 310. mtemplit 'erroneously' for a seen monster is not a problem; 311. it just flags monsters for another canseemon() check when 312. 'obj' has reached its destination after missile traversal */ 313. if (dist2(mon->mx, mon->my, x, y) <= radius_squared) { 314. if (canseemon(mon)) 315. mon->mtemplit = 1; 316. } 317. /* [what about worm tails?] */ 318. } 319. 320. if (obj) { /* take thrown/kicked candle or lamp off the map */ 321. nh_delay_output(); 322. remove_object(obj); 323. } 324. } 325. 326. /* delete any camera flash light sources and draw "remembered, unseen 327. monster" glyph at locations where a monster was flagged for being 328. visible during transient light movement but can't be seen now */ 329. void 330. transient_light_cleanup(void) 331. { 332. struct monst *mon; 333. int mtempcount; 334. 335. /* in case we're cleaning up a camera flash, remove all object light 336. sources which aren't associated with a specific object */ 337. discard_flashes(); 338. if (gv.vision_full_recalc) /* set by del_light_source() */ 339. vision_recalc(0); 340. 341. /* for thrown/kicked candle or lamp or for camera flash, some 342. monsters may have been mapped in light which has now gone away 343. so need to be replaced by "remembered, unseen monster" glyph */ 344. mtempcount = 0; 345. for (mon = fmon; mon; mon = mon->nmon) { 346. if (DEADMONSTER(mon)) 347. continue; 348. if (mon->mtemplit) { 349. mon->mtemplit = 0; 350. ++mtempcount; 351. if (!canspotmon(mon)) 352. map_invisible(mon->mx, mon->my); 353. } 354. } 355. if (mtempcount) 356. flush_screen(0); 357. } 358. 359. /* camera flashes have Null object; caller wants to get rid of them now */ 360. staticfn void 361. discard_flashes(void) 362. { 363. light_source *ls, *nxt_ls; 364. 365. for (ls = gl.light_base; ls; ls = nxt_ls) { 366. nxt_ls = ls->next; 367. if (ls->type == LS_OBJECT && !ls->id.a_obj) 368. delete_ls(ls); 369. } 370. } 371. 372. /* (mon->mx == 0) implies migrating */ 373. #define mon_is_local(mon) ((mon)->mx > 0) 374. 375. struct monst * 376. find_mid(unsigned nid, unsigned fmflags) 377. { 378. struct monst *mtmp; 379. 380. if ((fmflags & FM_YOU) && nid == 1) 381. return &gy.youmonst; 382. if (fmflags & FM_FMON) 383. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) 384. if (!DEADMONSTER(mtmp) && mtmp->m_id == nid) 385. return mtmp; 386. if (fmflags & FM_MIGRATE) 387. for (mtmp = gm.migrating_mons; mtmp; mtmp = mtmp->nmon) 388. if (mtmp->m_id == nid) 389. return mtmp; 390. if (fmflags & FM_MYDOGS) 391. for (mtmp = gm.mydogs; mtmp; mtmp = mtmp->nmon) 392. if (mtmp->m_id == nid) 393. return mtmp; 394. return (struct monst *) 0; 395. } 396. 397. staticfn unsigned 398. whereis_mon(struct monst *mon, unsigned fmflags) 399. { 400. struct monst *mtmp; 401. 402. if ((fmflags & FM_YOU) && mon == &gy.youmonst) 403. return FM_YOU; 404. if (fmflags & FM_FMON) 405. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) 406. if (mtmp == mon) 407. return FM_FMON; 408. if (fmflags & FM_MIGRATE) 409. for (mtmp = gm.migrating_mons; mtmp; mtmp = mtmp->nmon) 410. if (mtmp == mon) 411. return FM_MIGRATE; 412. if (fmflags & FM_MYDOGS) 413. for (mtmp = gm.mydogs; mtmp; mtmp = mtmp->nmon) 414. if (mtmp == mon) 415. return FM_MYDOGS; 416. return 0; 417. } 418. 419. /* Save all light sources of the given range. */ 420. void 421. save_light_sources(NHFILE *nhfp, int range) 422. { 423. int count, actual, is_global; 424. light_source **prev, *curr; 425. 426. /* camera flash light sources have Null object and would trigger 427. impossible("no id!") below; they can only happen here if we're 428. in the midst of a panic save and they wouldn't be useful after 429. restore so just throw any that are present away */ 430. discard_flashes(); 431. gv.vision_full_recalc = 0; 432. 433. if (update_file(nhfp)) { 434. count = maybe_write_ls(nhfp, range, FALSE); 435. Sfo_int(nhfp, &count, "lightsource-count"); 436. actual = maybe_write_ls(nhfp, range, TRUE); 437. if (actual != count) 438. panic("counted %d light sources, wrote %d! [range=%d]", count, 439. actual, range); 440. } 441. 442. if (release_data(nhfp)) { 443. for (prev = &gl.light_base; (curr = *prev) != 0; ) { 444. if (!curr->id.a_monst) { 445. impossible("save_light_sources: no id! [range=%d]", range); 446. is_global = 0; 447. } else 448. switch (curr->type) { 449. case LS_OBJECT: 450. is_global = !obj_is_local(curr->id.a_obj); 451. break; 452. case LS_MONSTER: 453. is_global = !mon_is_local(curr->id.a_monst); 454. break; 455. default: 456. is_global = 0; 457. impossible("save_light_sources: bad type (%d) [range=%d]", 458. curr->type, range); 459. break; 460. } 461. /* if global and not doing local, or vice versa, remove it */ 462. if (is_global ^ (range == RANGE_LEVEL)) { 463. *prev = curr->next; 464. (void) memset((genericptr_t) curr, 0, sizeof(light_source)); 465. free((genericptr_t) curr); 466. } else { 467. prev = &(*prev)->next; 468. } 469. } 470. } 471. } 472. #endif /* !SFCTOOL */ 473. 474. /* 475. * Pull in the structures from disk, but don't recalculate the object 476. * pointers. 477. */ 478. void 479. restore_light_sources(NHFILE *nhfp) 480. { 481. int count = 0; 482. light_source *ls; 483. 484. /* restore elements */ 485. Sfi_int(nhfp, &count, "lightsource-count"); 486. 487. while (count-- > 0) { 488. ls = (light_source *) alloc(sizeof(light_source)); 489. Sfi_ls_t(nhfp, ls, "lightsource"); 490. ls->next = gl.light_base; 491. gl.light_base = ls; 492. } 493. } 494. 495. #ifndef SFCTOOL 496. 497. DISABLE_WARNING_FORMAT_NONLITERAL 498. 499. /* to support '#stats' wizard-mode command */ 500. void 501. light_stats(const char *hdrfmt, char *hdrbuf, long *count, long *size) 502. { 503. light_source *ls; 504. 505. Sprintf(hdrbuf, hdrfmt, (long) sizeof (light_source)); 506. *count = *size = 0L; 507. for (ls = gl.light_base; ls; ls = ls->next) { 508. ++*count; 509. *size += (long) sizeof *ls; 510. } 511. } 512. 513. RESTORE_WARNING_FORMAT_NONLITERAL 514. 515. /* Relink all lights that are so marked. */ 516. void 517. relink_light_sources(boolean ghostly) 518. { 519. char which; 520. unsigned nid; 521. light_source *ls; 522. 523. /* 524. * Caveat: 525. * There has been at least one instance during to-be-5.0 development 526. * where the light_base linked list ended up with a circular link. 527. * If that happens, then once all the traversed elements have their 528. * LSF_NEEDS_FIXUP flag cleared, the traversal attempt will run wild. 529. * 530. * The circular list instance was blamed on attempting to restore 531. * a save file which should have been invalidated by version/patch/ 532. * editlevel verification, but wasn't rejected because EDITLEVEL 533. * didn't get incremented when it should have been. Valid data should 534. * never produce the problem and it isn't possible in general to guard 535. * against code updates that neglect to set the verification info up 536. * to date. 537. */ 538. 539. for (ls = gl.light_base; ls; ls = ls->next) { 540. if (ls->flags & LSF_NEEDS_FIXUP) { 541. if (ls->type == LS_OBJECT || ls->type == LS_MONSTER) { 542. nid = ls->id.a_uint; 543. if (ghostly && !lookup_id_mapping(nid, &nid)) 544. panic("relink_light_sources: no id mapping"); 545. 546. which = '\0'; 547. if (ls->type == LS_OBJECT) { 548. if ((ls->id.a_obj = find_oid(nid)) == 0) 549. which = 'o'; 550. } else { 551. if ((ls->id.a_monst = find_mid(nid, FM_EVERYWHERE)) == 0) 552. which = 'm'; 553. } 554. if (which != '\0') 555. panic("relink_light_sources: can't find %c_id %u", 556. which, nid); 557. } else { 558. panic("relink_light_sources: bad type (%d)", ls->type); 559. } 560. ls->flags &= ~LSF_NEEDS_FIXUP; 561. } 562. } 563. } 564. 565. /* 566. * Part of the light source save routine. Count up the number of light 567. * sources that would be written. If write_it is true, actually write 568. * the light source out. 569. */ 570. staticfn int 571. maybe_write_ls(NHFILE *nhfp, int range, boolean write_it) 572. { 573. int count = 0, is_global; 574. light_source *ls; 575. 576. for (ls = gl.light_base; ls; ls = ls->next) { 577. if (!ls->id.a_monst) { 578. impossible("maybe_write_ls: no id! [range=%d]", range); 579. continue; 580. } 581. switch (ls->type) { 582. case LS_OBJECT: 583. is_global = !obj_is_local(ls->id.a_obj); 584. break; 585. case LS_MONSTER: 586. is_global = !mon_is_local(ls->id.a_monst); 587. break; 588. default: 589. is_global = 0; 590. impossible("maybe_write_ls: bad type (%d) [range=%d]", ls->type, 591. range); 592. break; 593. } 594. /* if global and not doing local, or vice versa, count it */ 595. if (is_global ^ (range == RANGE_LEVEL)) { 596. count++; 597. if (write_it) 598. write_ls(nhfp, ls); 599. } 600. } 601. 602. return count; 603. } 604. 605. void 606. light_sources_sanity_check(void) 607. { 608. light_source *ls; 609. struct monst *mtmp; 610. struct obj *otmp; 611. unsigned int auint; 612. 613. for (ls = gl.light_base; ls; ls = ls->next) { 614. if (!ls->id.a_monst) 615. panic("insane light source: no id!"); 616. if (ls->type == LS_OBJECT) { 617. otmp = ls->id.a_obj; 618. auint = otmp->o_id; 619. if (find_oid(auint) != otmp) 620. panic("insane light source: can't find obj #%u!", auint); 621. } else if (ls->type == LS_MONSTER) { 622. mtmp = (struct monst *) ls->id.a_monst; 623. auint = mtmp->m_id; 624. if (find_mid(auint, FM_EVERYWHERE) != mtmp) 625. panic("insane light source: can't find mon #%u!", auint); 626. } else { 627. panic("insane light source: bad ls type %d", ls->type); 628. } 629. } 630. } 631. 632. /* Write a light source structure to disk. */ 633. staticfn void 634. write_ls(NHFILE *nhfp, light_source *ls) 635. { 636. anything arg_save; 637. struct obj *otmp; 638. struct monst *mtmp; 639. 640. if (ls->type == LS_OBJECT || ls->type == LS_MONSTER) { 641. if (ls->flags & LSF_NEEDS_FIXUP) { 642. Sfo_ls_t(nhfp, ls, "lightsource"); 643. } else { 644. /* replace object pointer with id for write, then put back */ 645. arg_save = ls->id; 646. if (ls->type == LS_OBJECT) { 647. otmp = ls->id.a_obj; 648. ls->id = cg.zeroany; 649. ls->id.a_uint = otmp->o_id; 650. if (find_oid((unsigned) ls->id.a_uint) != otmp) { 651. impossible("write_ls: can't find obj #%u!", 652. ls->id.a_uint); 653. ls->flags |= LSF_IS_PROBLEMATIC; 654. } 655. } else { /* ls->type == LS_MONSTER */ 656. unsigned monloc = 0; 657. 658. mtmp = (struct monst *) ls->id.a_monst; 659. 660. /* The monster pointer has been stashed in the light source 661. * for a while and while there is code meant to clean-up the 662. * light source aspects if a monster goes away, there have 663. * been some reports of light source issues, such as when 664. * going to the planes. 665. * 666. * Verify that the stashed monst pointer is still present 667. * in one of the monster chains before pulling subfield 668. * values such as m_id from it, to avoid any attempt to 669. * pull random m_id value from (now) freed memory. 670. * 671. * find_mid() disregards a DEADMONSTER, but whereis_mon() 672. * does not. */ 673. 674. if ((monloc = whereis_mon(mtmp, FM_EVERYWHERE)) != 0) { 675. ls->id = cg.zeroany; 676. ls->id.a_uint = mtmp->m_id; 677. if (find_mid((unsigned) ls->id.a_uint, monloc) != mtmp) { 678. impossible("write_ls: can't find mon%s #%u!", 679. DEADMONSTER(mtmp) ? " because it's dead" 680. : "", 681. ls->id.a_uint); 682. ls->flags |= LSF_IS_PROBLEMATIC; 683. } 684. } else { 685. impossible( 686. "write_ls: stashed monst ptr not in any chain"); 687. ls->flags |= LSF_IS_PROBLEMATIC; 688. } 689. } 690. if (ls->flags & LSF_IS_PROBLEMATIC) { 691. /* TODO: cleanup this ls, or skip writing it */ 692. } 693. ls->flags |= LSF_NEEDS_FIXUP; 694. Sfo_ls_t(nhfp, ls, "lightsource"); 695. ls->id = arg_save; 696. ls->flags &= ~LSF_NEEDS_FIXUP; 697. ls->flags &= ~LSF_IS_PROBLEMATIC; 698. } 699. } else { 700. impossible("write_ls: bad type (%d)", ls->type); 701. } 702. } 703. 704. /* Change light source's ID from src to dest. */ 705. void 706. obj_move_light_source(struct obj *src, struct obj *dest) 707. { 708. light_source *ls; 709. 710. for (ls = gl.light_base; ls; ls = ls->next) 711. if (ls->type == LS_OBJECT && ls->id.a_obj == src) 712. ls->id.a_obj = dest; 713. src->lamplit = 0; 714. dest->lamplit = 1; 715. } 716. 717. /* return true if there exist any light sources */ 718. boolean 719. any_light_source(void) 720. { 721. return (boolean) (gl.light_base != (light_source *) 0); 722. } 723. 724. /* 725. * Snuff an object light source if at (x,y). This currently works 726. * only for burning light sources. 727. */ 728. void 729. snuff_light_source(coordxy x, coordxy y) 730. { 731. light_source *ls; 732. struct obj *obj; 733. 734. for (ls = gl.light_base; ls; ls = ls->next) 735. /* 736. * Is this position check valid??? Can I assume that the positions 737. * will always be correct because the objects would have been 738. * updated with the last vision update? [Is that recent enough???] 739. */ 740. if (ls->type == LS_OBJECT && ls->x == x && ls->y == y) { 741. obj = ls->id.a_obj; 742. if (obj_is_burning(obj)) { 743. /* The only way to snuff Sunsword is to unwield it. Darkness 744. * scrolls won't affect it. (If we got here because it was 745. * dropped or thrown inside a monster, this won't matter 746. * anyway because it will go out when dropped.) 747. */ 748. if (artifact_light(obj)) 749. continue; 750. end_burn(obj, obj->otyp != MAGIC_LAMP); 751. /* 752. * The current ls element has just been removed (and 753. * ls->next is now invalid). Return assuming that there 754. * is only one light source attached to each object. 755. */ 756. return; 757. } 758. } 759. } 760. 761. /* Return TRUE if object sheds any light at all. */ 762. boolean 763. obj_sheds_light(struct obj *obj) 764. { 765. /* so far, only burning objects shed light */ 766. return obj_is_burning(obj); 767. } 768. 769. /* Return TRUE if sheds light AND will be snuffed by end_burn(). */ 770. boolean 771. obj_is_burning(struct obj *obj) 772. { 773. return (boolean) (obj->lamplit && (ignitable(obj) 774. || artifact_light(obj))); 775. } 776. 777. /* copy the light source(s) attached to src, and attach it/them to dest */ 778. void 779. obj_split_light_source(struct obj *src, struct obj *dest) 780. { 781. light_source *ls, *new_ls; 782. 783. for (ls = gl.light_base; ls; ls = ls->next) 784. if (ls->type == LS_OBJECT && ls->id.a_obj == src) { 785. /* 786. * Insert the new source at beginning of list. This will 787. * never interfere us walking down the list - we are already 788. * past the insertion point. 789. */ 790. new_ls = (light_source *) alloc(sizeof(light_source)); 791. *new_ls = *ls; 792. if (Is_candle(src)) { 793. /* split candles may emit less light than original group */ 794. ls->range = candle_light_range(src); 795. new_ls->range = candle_light_range(dest); 796. gv.vision_full_recalc = 1; /* in case range changed */ 797. } 798. new_ls->id.a_obj = dest; 799. new_ls->next = gl.light_base; 800. gl.light_base = new_ls; 801. dest->lamplit = 1; /* now an active light source */ 802. } 803. } 804. 805. /* light source `src' has been folded into light source `dest'; 806. used for merging lit candles and adding candle(s) to lit candelabrum */ 807. void 808. obj_merge_light_sources(struct obj *src, struct obj *dest) 809. { 810. light_source *ls; 811. 812. /* src == dest implies adding to candelabrum */ 813. if (src != dest) 814. end_burn(src, TRUE); /* extinguish candles */ 815. 816. for (ls = gl.light_base; ls; ls = ls->next) 817. if (ls->type == LS_OBJECT && ls->id.a_obj == dest) { 818. ls->range = candle_light_range(dest); 819. gv.vision_full_recalc = 1; /* in case range changed */ 820. break; 821. } 822. } 823. 824. /* light source `obj' is being made brighter or dimmer */ 825. void 826. obj_adjust_light_radius(struct obj *obj, int new_radius) 827. { 828. light_source *ls; 829. 830. for (ls = gl.light_base; ls; ls = ls->next) 831. if (ls->type == LS_OBJECT && ls->id.a_obj == obj) { 832. if (new_radius != ls->range) 833. gv.vision_full_recalc = 1; 834. ls->range = new_radius; 835. return; 836. } 837. impossible("obj_adjust_light_radius: can't find %s", xname(obj)); 838. } 839. 840. /* Candlelight is proportional to the number of candles; 841. minimum range is 2 rather than 1 for playability. */ 842. int 843. candle_light_range(struct obj *obj) 844. { 845. int radius; 846. 847. if (obj->otyp == CANDELABRUM_OF_INVOCATION) { 848. /* 849. * The special candelabrum emits more light than the 850. * corresponding number of candles would. 851. * 1..3 candles, range 2 (minimum range); 852. * 4..6 candles, range 3 (normal lamp range); 853. * 7 candles, range 4 (bright). 854. */ 855. radius = (obj->spe < 4) ? 2 : (obj->spe < 7) ? 3 : 4; 856. } else if (Is_candle(obj)) { 857. /* 858. * Range is incremented quadratically. You can get the same 859. * amount of light as from a lamp with 4 candles, and 860. * even better light with 9 candles, and so on. 861. * 1..3 candles, range 2; 862. * 4..8 candles, range 3; 863. * 9..15 candles, range 4; &c. 864. */ 865. long n = obj->quan; 866. 867. radius = 1; /* always incremented at least once */ 868. while(radius*radius <= n && radius < MAX_RADIUS) { 869. radius++; 870. } 871. } else { 872. /* we're only called for lit candelabrum or candles */ 873. /* impossible("candlelight for %d?", obj->otyp); */ 874. radius = 3; /* lamp's value */ 875. } 876. return radius; 877. } 878. 879. /* light emitting artifact's range depends upon its curse/bless state */ 880. int 881. arti_light_radius(struct obj *obj) 882. { 883. int res; 884. 885. /* 886. * Used by begin_burn() when setting up a new light source 887. * (obj->lamplit will already be set by this point) and 888. * also by bless()/unbless()/uncurse()/curse() to decide 889. * whether to call obj_adjust_light_radius(). 890. */ 891. 892. /* sanity check [simplifies usage by bless()/curse()/&c] */ 893. if (!obj->lamplit || !artifact_light(obj)) 894. return 0; 895. 896. /* cursed radius of 1 is not noticeable for an item that's 897. carried by the hero but is if it's carried by a monster 898. or left lit on the floor (not applicable for Sunsword) */ 899. res = (obj->blessed ? 3 : !obj->cursed ? 2 : 1); 900. 901. /* if poly'd into gold dragon with embedded scales, make the scales 902. have minimum radiance (hero as light source will use light radius 903. based on monster form); otherwise, worn gold DSM gives off more 904. light than other light sources */ 905. if (obj == uskin) 906. res = 1; 907. else if (obj->otyp == GOLD_DRAGON_SCALE_MAIL) /* DSM but not scales */ 908. ++res; 909. 910. return res; 911. } 912. 913. /* adverb describing lit artifact's light; radius varies depending upon 914. curse/bless state; also used for gold dragon scales/scale mail */ 915. const char * 916. arti_light_description(struct obj *obj) 917. { 918. switch (arti_light_radius(obj)) { 919. case 4: 920. return "radiantly"; /* blessed gold dragon scale mail */ 921. case 3: 922. return "brilliantly"; /* blessed artifact, uncursed gold DSM */ 923. case 2: 924. return "brightly"; /* uncursed artifact, cursed gold DSM */ 925. case 1: 926. return "dimly"; /* cursed artifact, embedded scales */ 927. default: 928. break; 929. } 930. return "strangely"; 931. } 932. 933. /* the #lightsources command */ 934. int 935. wiz_light_sources(void) 936. { 937. winid win; 938. char buf[BUFSZ]; 939. light_source *ls; 940. 941. win = create_nhwindow(NHW_MENU); /* corner text window */ 942. if (win == WIN_ERR) 943. return ECMD_OK; 944. 945. Sprintf(buf, "Mobile light sources: hero @ (%2d,%2d)", u.ux, u.uy); 946. putstr(win, 0, buf); 947. putstr(win, 0, ""); 948. 949. if (gl.light_base) { 950. putstr(win, 0, "location range flags type id"); 951. putstr(win, 0, "-------- ----- ------ ---- -------"); 952. for (ls = gl.light_base; ls; ls = ls->next) { 953. Sprintf(buf, " %2d,%2d %2d 0x%04x %s %s", ls->x, ls->y, 954. ls->range, ls->flags, 955. (ls->type == LS_OBJECT 956. ? "obj" 957. : ls->type == LS_MONSTER 958. ? (mon_is_local(ls->id.a_monst) 959. ? "mon" 960. : (ls->id.a_monst == &gy.youmonst) 961. ? "you" 962. /* migrating monster */ 963. : "<m>") 964. : "???"), 965. fmt_ptr(ls->id.a_void)); 966. putstr(win, 0, buf); 967. } 968. } else 969. putstr(win, 0, "<none>"); 970. 971. display_nhwindow(win, FALSE); 972. destroy_nhwindow(win); 973. 974. return ECMD_OK; 975. } 976. #endif /* !SFCTOOL */ 977. 978. /* for 'onefile' processing where end of this file isn't necessarily the 979. end of the source code seen by the compiler */ 980. #undef LSF_SHOW 981. #undef LSF_NEEDS_FIXUP 982. #undef LSF_IS_PROBLEMATIC 983. #undef mon_is_local 984. 985. /*light.c*/