summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorennewalker <ennewalker@c06c8d41-db1a-0410-9941-cceddc491573>2009-04-18 15:08:40 +0000
committerennewalker <ennewalker@c06c8d41-db1a-0410-9941-cceddc491573>2009-04-18 15:08:40 +0000
commit4f56baa35df811304ef3e45c83471fcfd43bce2c (patch)
tree32443c8d02e82c16a5e8f1153e3ce194e9b8f02b
parent92a70a7bda3d5a52fd6cc016b748e6bf59fb81e4 (diff)
downloadcrawl-ref-4f56baa35df811304ef3e45c83471fcfd43bce2c.tar.gz
crawl-ref-4f56baa35df811304ef3e45c83471fcfd43bce2c.zip
[2758242] Fixing segfault during debug_mons_scan due to an mprf not matching its format string with its var args. Also, fixing clone function from chaos brand that was causing this where cloned items in monster inventories weren't setting their link to be part of the new monster's inventory. Refactoring monster holding functions into item_def, where they probably should be.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@9618 c06c8d41-db1a-0410-9941-cceddc491573
-rw-r--r--crawl-ref/source/debug.cc13
-rw-r--r--crawl-ref/source/dungeon.cc2
-rw-r--r--crawl-ref/source/externs.h10
-rw-r--r--crawl-ref/source/items.cc27
-rw-r--r--crawl-ref/source/makeitem.cc2
-rw-r--r--crawl-ref/source/misc.cc6
-rw-r--r--crawl-ref/source/mon-util.cc22
-rw-r--r--crawl-ref/source/mon-util.h3
-rw-r--r--crawl-ref/source/mstuff2.cc1
9 files changed, 50 insertions, 36 deletions
diff --git a/crawl-ref/source/debug.cc b/crawl-ref/source/debug.cc
index 3ff7d97624..6e9924cd0b 100644
--- a/crawl-ref/source/debug.cc
+++ b/crawl-ref/source/debug.cc
@@ -2489,7 +2489,7 @@ void wizard_list_items()
for (int i = 0; i < MAX_ITEMS; i++)
{
item_def &item(mitm[i]);
- if (!is_valid_item(item) || held_by_monster(item))
+ if (!is_valid_item(item) || item.held_by_monster())
continue;
if (item.link != NON_ITEM)
@@ -2728,7 +2728,7 @@ void debug_item_scan( void )
strcpy(name, mitm[i].name(DESC_PLAIN).c_str());
- const monsters* mon = holding_monster(mitm[i]);
+ const monsters* mon = mitm[i].holding_monster();
// Don't check (-1, -1) player items or (-2, -2) monster items
// (except to make sure that the monster is alive).
@@ -3007,7 +3007,7 @@ void debug_mons_scan()
continue;
}
- const monsters* holder = holding_monster(item);
+ const monsters* holder = item.holding_monster();
if (holder == NULL)
{
@@ -3031,6 +3031,7 @@ void debug_mons_scan()
"monster %s (%d, %d) [midx = %d]",
m->full_name(DESC_PLAIN, true).c_str(),
m->pos().x, m->pos().y, i,
+ item.name(DESC_PLAIN).c_str(),
holder->full_name(DESC_PLAIN, true).c_str(),
holder->pos().x, holder->pos().y, holder->mindex());
@@ -6680,12 +6681,12 @@ void debug_dump_mon(const monsters* mon, bool recurse)
fprintf(stderr, "%s", item.name(DESC_PLAIN, false, true).c_str());
- if (!held_by_monster(item))
+ if (!item.held_by_monster())
fprintf(stderr, " [not held by monster, pos = %s]",
debug_coord_str(item.pos).c_str());
- else if (holding_monster(item) != mon)
+ else if (item.holding_monster() != mon)
fprintf(stderr, " [held by other monster: %s]",
- debug_mon_str(holding_monster(item)).c_str());
+ debug_mon_str(item.holding_monster()).c_str());
fprintf(stderr, EOL);
}
diff --git a/crawl-ref/source/dungeon.cc b/crawl-ref/source/dungeon.cc
index e5b51ee0cf..3930a08826 100644
--- a/crawl-ref/source/dungeon.cc
+++ b/crawl-ref/source/dungeon.cc
@@ -1089,7 +1089,7 @@ static void _fixup_misplaced_items()
{
item_def& item(mitm[i]);
if (!is_valid_item(item) || (item.pos.x == 0)
- || held_by_monster(item))
+ || item.held_by_monster())
{
continue;
}
diff --git a/crawl-ref/source/externs.h b/crawl-ref/source/externs.h
index 0e773c455f..e94cc0c68c 100644
--- a/crawl-ref/source/externs.h
+++ b/crawl-ref/source/externs.h
@@ -620,6 +620,16 @@ public:
{
*this = item_def();
}
+
+ // Sets this item as being held by a given monster.
+ void set_holding_monster(int midx);
+
+ // Returns monster holding this item. NULL if none.
+ monsters* holding_monster() const;
+
+ // Returns true if a monster is holding this item.
+ bool held_by_monster() const;
+
private:
std::string name_aux(description_level_type desc,
bool terse, bool ident,
diff --git a/crawl-ref/source/items.cc b/crawl-ref/source/items.cc
index 24d63b7dd9..d13e974967 100644
--- a/crawl-ref/source/items.cc
+++ b/crawl-ref/source/items.cc
@@ -114,7 +114,7 @@ void link_items(void)
{
// Don't mess with monster held items, since the index of the holding
// monster is stored in the link field.
- if (held_by_monster(mitm[i]))
+ if (mitm[i].held_by_monster())
continue;
if (!is_valid_item(mitm[i]))
@@ -368,7 +368,7 @@ void unlink_item( int dest )
if (dest == NON_ITEM || !is_valid_item( mitm[dest] ))
return;
- monsters* monster = holding_monster(mitm[dest]);
+ monsters* monster = mitm[dest].holding_monster();
if (monster != NULL)
{
@@ -2688,3 +2688,26 @@ int item_def::armour_rating() const
return (property(*this, PARM_AC) + plus);
}
+
+monsters* item_def::holding_monster() const
+{
+ if (!pos.equals(-2, -2))
+ return (NULL);
+ const int midx = link - NON_ITEM - 1;
+ if (invalid_monster_index(midx))
+ return (NULL);
+
+ return (&menv[midx]);
+}
+
+void item_def::set_holding_monster(int midx)
+{
+ ASSERT(midx != NON_MONSTER);
+ pos.set(-2, -2);
+ link = NON_ITEM + 1 + midx;
+}
+
+bool item_def::held_by_monster() const
+{
+ return (pos.equals(-2, -2) && !invalid_monster_index(link - NON_ITEM - 1));
+}
diff --git a/crawl-ref/source/makeitem.cc b/crawl-ref/source/makeitem.cc
index fa81f0de80..703def7229 100644
--- a/crawl-ref/source/makeitem.cc
+++ b/crawl-ref/source/makeitem.cc
@@ -3029,7 +3029,7 @@ static void _give_monster_item(monsters *mon, int thing,
return;
}
ASSERT(is_valid_item(mthing));
- ASSERT(holding_monster(mthing) == mon);
+ ASSERT(mthing.holding_monster() == mon);
if (!force_item || mthing.colour == BLACK)
item_colour(mthing);
diff --git a/crawl-ref/source/misc.cc b/crawl-ref/source/misc.cc
index 488babbb43..72edbf9402 100644
--- a/crawl-ref/source/misc.cc
+++ b/crawl-ref/source/misc.cc
@@ -302,7 +302,7 @@ void maybe_coagulate_blood_potions_floor(int obj)
// Coagulated blood cannot coagulate any further...
ASSERT(blood.sub_type == POT_BLOOD);
- if (!held_by_monster(blood))
+ if (!blood.held_by_monster())
{
// Now that coagulating is necessary, check square for
// !coagulated blood.
@@ -392,8 +392,8 @@ void maybe_coagulate_blood_potions_floor(int obj)
ASSERT(timer_new.size() == coag_count);
props_new.assert_validity();
- if (held_by_monster(blood))
- move_item_to_grid(&o, holding_monster(blood)->pos());
+ if (blood.held_by_monster())
+ move_item_to_grid(&o, blood.holding_monster()->pos());
else
move_item_to_grid(&o, blood.pos);
diff --git a/crawl-ref/source/mon-util.cc b/crawl-ref/source/mon-util.cc
index 1f4763e561..6e5f00f6ac 100644
--- a/crawl-ref/source/mon-util.cc
+++ b/crawl-ref/source/mon-util.cc
@@ -4329,28 +4329,11 @@ void monsters::pickup_message(const item_def &item, int near)
}
}
-bool held_by_monster(const item_def &item)
-{
- return (item.pos.equals(-2, -2)
- && !invalid_monster_index(item.link - NON_ITEM - 1));
-}
-
-monsters* holding_monster(const item_def &item)
-{
- if (!item.pos.equals(-2, -2))
- return (NULL);
- const int midx = item.link - NON_ITEM - 1;
- if (invalid_monster_index(midx))
- return (NULL);
-
- return (&menv[midx]);
-}
-
bool monsters::pickup(item_def &item, int slot, int near, bool force_merge)
{
ASSERT(is_valid_item(item));
- const monsters *other_mon = holding_monster(item);
+ const monsters *other_mon = item.holding_monster();
if (other_mon != NULL)
{
@@ -4442,8 +4425,7 @@ bool monsters::pickup(item_def &item, int slot, int near, bool force_merge)
inv[slot] = item_index;
- item.pos.set(-2, -2);
- item.link = NON_ITEM + 1 + mindex();
+ item.set_holding_monster(mindex());
pickup_message(item, near);
equip(item, slot, near);
diff --git a/crawl-ref/source/mon-util.h b/crawl-ref/source/mon-util.h
index 77e64f15df..9bd0ce27d3 100644
--- a/crawl-ref/source/mon-util.h
+++ b/crawl-ref/source/mon-util.h
@@ -868,7 +868,4 @@ bool mons_can_pass(const monsters *mon, dungeon_feature_type grid);
mon_inv_type equip_slot_to_mslot(equipment_type eq);
mon_inv_type item_to_mslot(const item_def &item);
-
-bool held_by_monster(const item_def &item);
-monsters* holding_monster(const item_def &item);
#endif
diff --git a/crawl-ref/source/mstuff2.cc b/crawl-ref/source/mstuff2.cc
index dc7b2d9e75..0908dcea75 100644
--- a/crawl-ref/source/mstuff2.cc
+++ b/crawl-ref/source/mstuff2.cc
@@ -2565,6 +2565,7 @@ int clone_mons(const monsters* orig, bool quiet, bool* obvious,
mon.inv[i] = new_index;
mitm[new_index] = mitm[old_index];
+ mitm[new_index].set_holding_monster(midx);
}
bool _obvious;