From 7c5835d318282ee30bfeae0d490ebf73a08b6791 Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 20:40:29 -0600 Subject: [PATCH] t_itemref -> df::general_ref --- library/include/modules/Items.h | 12 ++----- library/modules/Items.cpp | 61 +++++++++++++++++---------------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index eeea8eb63..1f0a0a5d0 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -36,6 +36,7 @@ distribution. #include "DataDefs.h" #include "df/item.h" #include "df/item_type.h" +#include "df/general_ref.h" namespace df { @@ -91,15 +92,6 @@ class Context; class DFContextShared; class Units; -/** - * Describes relationship of an item with other objects - * \ingroup grp_items - */ -struct t_itemref : public t_virtual -{ - int32_t value; -}; - /** * Type for holding an item read from DF * \ingroup grp_items @@ -169,7 +161,7 @@ public: /// wipe out the owner records bool removeItemOwner(df::item * item, Units *creatures); /// read item references, filtered by class - bool readItemRefs(const df::item * item, const ClassNameCheck &classname, + bool readItemRefs(const df::item * item, const df::general_ref_type type, /*output*/ std::vector &values); /// get list of item references that are unknown along with their values bool unknownRefs(const df::item * item, /*output*/ std::vector >& refs); diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 4c695607d..4588b4397 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -63,6 +63,7 @@ using namespace std; #include "df/itemdef_foodst.h" #include "df/trapcomp_flags.h" #include "df/job_item.h" +#include "df/general_ref.h" using namespace DFHack; using namespace df::enums; @@ -559,36 +560,40 @@ bool Items::copyItem(df::item * itembase, DFHack::dfh_item &item) int32_t Items::getItemOwnerID(const df::item * item) { - std::vector vals; - if (readItemRefs(item, d->isOwnerRefClass, vals)) - return vals[0]; - else - return -1; + for (uint32_t i = 0; i < item->itemrefs.size(); i++) + { + df::general_ref *ref = item->itemrefs[i]; + if (ref->getType() == df::general_ref_type::unit_itemowner) + return ref->getID(); + } + return -1; } int32_t Items::getItemContainerID(const df::item * item) { - std::vector vals; - if (readItemRefs(item, d->isContainerRefClass, vals)) - return vals[0]; - else - return -1; + for (uint32_t i = 0; i < item->itemrefs.size(); i++) + { + df::general_ref *ref = item->itemrefs[i]; + if (ref->getType() == df::general_ref_type::contained_in_item) + return ref->getID(); + } + return -1; } bool Items::getContainedItems(const df::item * item, std::vector &items) { - return readItemRefs(item, d->isContainsRefClass, items); + return readItemRefs(item, df::general_ref_type::contains_item, items); } -bool Items::readItemRefs(const df::item * item, const ClassNameCheck &classname, std::vector &values) +bool Items::readItemRefs(const df::item * item, df::general_ref_type type, std::vector &values) { - const std::vector &p_refs = (const std::vector &)item->itemrefs; values.clear(); - for (uint32_t i=0; iitemrefs.size(); i++) { - if (classname(d->owner, p_refs[i]->vptr)) - values.push_back(int32_t(p_refs[i]->value)); + df::general_ref *ref = item->itemrefs[i]; + if (ref->getType() == type) + values.push_back(ref->getID()); } return !values.empty(); @@ -598,15 +603,13 @@ bool Items::unknownRefs(const df::item * item, std::vector &p_refs = (const std::vector &)item->itemrefs; - - for (uint32_t i=0; iitemrefs.size(); i++) { - std::string name = p_refs[i]->getClassName(); + std::string name = ((t_virtual *)(item->itemrefs[i]))->getClassName(); if (d->knownItemRefTypes.find(name) == d->knownItemRefTypes.end()) { - refs.push_back(pair(name, p_refs[i]->value)); + refs.push_back(pair(name, item->itemrefs[i]->getID())); } } @@ -615,21 +618,21 @@ bool Items::unknownRefs(const df::item * item, std::vector &p_refs = (std::vector &)item->itemrefs; - for (uint32_t i=0; iitemrefs.size(); i++) { - if (!d->isOwnerRefClass(d->owner, p_refs[i]->vptr)) + df::general_ref *ref = item->itemrefs[i]; + if (ref->getType() != df::general_ref_type::unit_itemowner) continue; - int32_t & oid = p_refs[i]->value; - int32_t ix = creatures->FindIndexById(oid); + df_unit *unit = (df_unit *)ref->getUnit(); - if (ix < 0 || !creatures->RemoveOwnedItemByIdx(ix, item->id)) + if (unit == NULL || !creatures->RemoveOwnedItemByPtr(unit, item->id)) { - cerr << "RemoveOwnedItemIdx: CREATURE " << ix << " ID " << item->id << " FAILED!" << endl; + cerr << "RemoveOwnedItemIdx: CREATURE " << ref->getID() << " ID " << item->id << " FAILED!" << endl; return false; } - p_refs.erase(p_refs.begin() + i--); + delete ref; + item->itemrefs.erase(item->itemrefs.begin() + i--); } item->flags.bits.owned = 0;