From 4f56baa35df811304ef3e45c83471fcfd43bce2c Mon Sep 17 00:00:00 2001 From: ennewalker Date: Sat, 18 Apr 2009 15:08:40 +0000 Subject: [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 --- crawl-ref/source/items.cc | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'crawl-ref/source/items.cc') 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)); +} -- cgit v1.2.3-54-g00ecf