MCPcopy Index your code
hub / github.com/NetHack/NetHack / write_ls

Function write_ls

src/light.c:633–702  ·  view source on GitHub ↗

Write a light source structure to disk. */

Source from the content-addressed store, hash-verified

631
632/* Write a light source structure to disk. */
633staticfn void
634write_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) {

Callers 1

maybe_write_lsFunction · 0.85

Calls 4

find_oidFunction · 0.85
whereis_monFunction · 0.85
find_midFunction · 0.85
impossibleFunction · 0.70

Tested by

no test coverage detected