t_itemref -> df::general_ref

develop
Quietust 2012-01-16 20:40:29 -06:00
parent c18619520d
commit 7c5835d318
2 changed files with 34 additions and 39 deletions

@ -36,6 +36,7 @@ distribution.
#include "DataDefs.h" #include "DataDefs.h"
#include "df/item.h" #include "df/item.h"
#include "df/item_type.h" #include "df/item_type.h"
#include "df/general_ref.h"
namespace df namespace df
{ {
@ -91,15 +92,6 @@ class Context;
class DFContextShared; class DFContextShared;
class Units; 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 * Type for holding an item read from DF
* \ingroup grp_items * \ingroup grp_items
@ -169,7 +161,7 @@ public:
/// wipe out the owner records /// wipe out the owner records
bool removeItemOwner(df::item * item, Units *creatures); bool removeItemOwner(df::item * item, Units *creatures);
/// read item references, filtered by class /// 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<int32_t> &values); /*output*/ std::vector<int32_t> &values);
/// get list of item references that are unknown along with their values /// get list of item references that are unknown along with their values
bool unknownRefs(const df::item * item, /*output*/ std::vector<std::pair<std::string, int32_t> >& refs); bool unknownRefs(const df::item * item, /*output*/ std::vector<std::pair<std::string, int32_t> >& refs);

@ -63,6 +63,7 @@ using namespace std;
#include "df/itemdef_foodst.h" #include "df/itemdef_foodst.h"
#include "df/trapcomp_flags.h" #include "df/trapcomp_flags.h"
#include "df/job_item.h" #include "df/job_item.h"
#include "df/general_ref.h"
using namespace DFHack; using namespace DFHack;
using namespace df::enums; 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) int32_t Items::getItemOwnerID(const df::item * item)
{ {
std::vector<int32_t> vals; for (uint32_t i = 0; i < item->itemrefs.size(); i++)
if (readItemRefs(item, d->isOwnerRefClass, vals)) {
return vals[0]; df::general_ref *ref = item->itemrefs[i];
else if (ref->getType() == df::general_ref_type::unit_itemowner)
return ref->getID();
}
return -1; return -1;
} }
int32_t Items::getItemContainerID(const df::item * item) int32_t Items::getItemContainerID(const df::item * item)
{ {
std::vector<int32_t> vals; for (uint32_t i = 0; i < item->itemrefs.size(); i++)
if (readItemRefs(item, d->isContainerRefClass, vals)) {
return vals[0]; df::general_ref *ref = item->itemrefs[i];
else if (ref->getType() == df::general_ref_type::contained_in_item)
return ref->getID();
}
return -1; return -1;
} }
bool Items::getContainedItems(const df::item * item, std::vector<int32_t> &items) bool Items::getContainedItems(const df::item * item, std::vector<int32_t> &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<int32_t> &values) bool Items::readItemRefs(const df::item * item, df::general_ref_type type, std::vector<int32_t> &values)
{ {
const std::vector <t_itemref *> &p_refs = (const std::vector<t_itemref *> &)item->itemrefs;
values.clear(); values.clear();
for (uint32_t i=0; i<p_refs.size(); i++) for (uint32_t i = 0; i < item->itemrefs.size(); i++)
{ {
if (classname(d->owner, p_refs[i]->vptr)) df::general_ref *ref = item->itemrefs[i];
values.push_back(int32_t(p_refs[i]->value)); if (ref->getType() == type)
values.push_back(ref->getID());
} }
return !values.empty(); return !values.empty();
@ -598,15 +603,13 @@ bool Items::unknownRefs(const df::item * item, std::vector<std::pair<std::string
{ {
refs.clear(); refs.clear();
const std::vector <t_itemref *> &p_refs = (const std::vector<t_itemref *> &)item->itemrefs; for (uint32_t i = 0; i < item->itemrefs.size(); i++)
for (uint32_t i=0; i<p_refs.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()) if (d->knownItemRefTypes.find(name) == d->knownItemRefTypes.end())
{ {
refs.push_back(pair<string, int32_t>(name, p_refs[i]->value)); refs.push_back(pair<string, int32_t>(name, item->itemrefs[i]->getID()));
} }
} }
@ -615,21 +618,21 @@ bool Items::unknownRefs(const df::item * item, std::vector<std::pair<std::string
bool Items::removeItemOwner(df::item * item, Units *creatures) bool Items::removeItemOwner(df::item * item, Units *creatures)
{ {
std::vector <t_itemref *> &p_refs = (std::vector<t_itemref *> &)item->itemrefs; for (uint32_t i = 0; i < item->itemrefs.size(); i++)
for (uint32_t i=0; i<p_refs.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; continue;
int32_t & oid = p_refs[i]->value; df_unit *unit = (df_unit *)ref->getUnit();
int32_t ix = creatures->FindIndexById(oid);
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; return false;
} }
p_refs.erase(p_refs.begin() + i--); delete ref;
item->itemrefs.erase(item->itemrefs.begin() + i--);
} }
item->flags.bits.owned = 0; item->flags.bits.owned = 0;