Kill the Items module

develop
Quietust 2012-01-16 22:12:58 -06:00
parent e75291ee91
commit fd653a0227
8 changed files with 44 additions and 228 deletions

@ -1094,7 +1094,6 @@ MODULE_GETTER(Maps);
MODULE_GETTER(Gui); MODULE_GETTER(Gui);
MODULE_GETTER(World); MODULE_GETTER(World);
MODULE_GETTER(Materials); MODULE_GETTER(Materials);
MODULE_GETTER(Items);
MODULE_GETTER(Translation); MODULE_GETTER(Translation);
MODULE_GETTER(Vegetation); MODULE_GETTER(Vegetation);
MODULE_GETTER(Constructions); MODULE_GETTER(Constructions);

@ -58,7 +58,6 @@ namespace DFHack
class Gui; class Gui;
class World; class World;
class Materials; class Materials;
class Items;
class Translation; class Translation;
class Vegetation; class Vegetation;
class Constructions; class Constructions;
@ -111,8 +110,6 @@ namespace DFHack
World * getWorld(); World * getWorld();
/// get the materials module /// get the materials module
Materials * getMaterials(); Materials * getMaterials();
/// get the items module
Items * getItems();
/// get the translation module /// get the translation module
Translation * getTranslation(); Translation * getTranslation();
/// get the vegetation module /// get the vegetation module
@ -173,7 +170,6 @@ namespace DFHack
Gui * pGui; Gui * pGui;
World * pWorld; World * pWorld;
Materials * pMaterials; Materials * pMaterials;
Items * pItems;
Translation * pTranslation; Translation * pTranslation;
Vegetation * pVegetation; Vegetation * pVegetation;
Constructions * pConstructions; Constructions * pConstructions;

@ -35,7 +35,6 @@ namespace DFHack
Module* createGui(); Module* createGui();
Module* createWorld(); Module* createWorld();
Module* createMaterials(); Module* createMaterials();
Module* createItems();
Module* createTranslation(); Module* createTranslation();
Module* createVegetation(); Module* createVegetation();
Module* createBuildings(); Module* createBuildings();

@ -116,46 +116,32 @@ struct dfh_item
* \ingroup grp_modules * \ingroup grp_modules
* \ingroup grp_items * \ingroup grp_items
*/ */
class DFHACK_EXPORT Items : public Module namespace Simple
{
namespace Items
{ {
public:
/**
* All the known item types as an enum
* From http://df.magmawiki.com/index.php/DF2010:Item_token
*/
public:
Items();
~Items();
bool Start();
bool Finish();
/// Read the item vector from DF into a supplied vector
bool readItemVector(std::vector<df::item *> &items);
/// Look for a particular item by ID /// Look for a particular item by ID
df::item * findItemByID(int32_t id); DFHACK_EXPORT df::item * findItemByID(int32_t id);
/// Make a partial copy of a DF item /// Make a partial copy of a DF item
bool copyItem(df::item * source, dfh_item & target); DFHACK_EXPORT bool copyItem(df::item * source, dfh_item & target);
/// write copied item back to its origin /// write copied item back to its origin
bool writeItem(const dfh_item & item); DFHACK_EXPORT bool writeItem(const dfh_item & item);
/// get the class name of an item /// get the class name of an item
std::string getItemClass(const df::item * item); DFHACK_EXPORT std::string getItemClass(const df::item * item);
/// who owns this item we already read? /// who owns this item we already read?
int32_t getItemOwnerID(const df::item * item); DFHACK_EXPORT int32_t getItemOwnerID(const df::item * item);
/// which item is it contained in? /// which item is it contained in?
int32_t getItemContainerID(const df::item * item); DFHACK_EXPORT int32_t getItemContainerID(const df::item * item);
/// which items does it contain? /// which items does it contain?
bool getContainedItems(const df::item * item, /*output*/ std::vector<int32_t> &items); DFHACK_EXPORT bool getContainedItems(const df::item * item, /*output*/ std::vector<int32_t> &items);
/// wipe out the owner records /// wipe out the owner records
bool removeItemOwner(df::item * item, Units *creatures); DFHACK_EXPORT 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 df::general_ref_type type, DFHACK_EXPORT 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 }
bool unknownRefs(const df::item * item, /*output*/ std::vector<std::pair<std::string, int32_t> >& refs); }
private:
class Private;
Private* d;
};
} }

@ -807,8 +807,6 @@ namespace DFHack
void CopyNameTo(df_unit *creature, df_name * target); void CopyNameTo(df_unit *creature, df_name * target);
protected:
friend class Items;
bool RemoveOwnedItemByIdx(const uint32_t index, int32_t id); bool RemoveOwnedItemByIdx(const uint32_t index, int32_t id);
bool RemoveOwnedItemByPtr(df_unit * unit, int32_t id); bool RemoveOwnedItemByPtr(df_unit * unit, int32_t id);

@ -66,7 +66,9 @@ using namespace std;
#include "df/general_ref.h" #include "df/general_ref.h"
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Simple;
using namespace df::enums; using namespace df::enums;
using df::global::world;
#define ITEMDEF_VECTORS \ #define ITEMDEF_VECTORS \
ITEM(WEAPON, weapons, itemdef_weaponst) \ ITEM(WEAPON, weapons, itemdef_weaponst) \
@ -394,146 +396,11 @@ bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat)
bits_match(item.flags3.whole, item_ok3.whole, item_mask3.whole); bits_match(item.flags3.whole, item_ok3.whole, item_mask3.whole);
} }
Module* DFHack::createItems()
{
return new Items();
}
class Items::Private
{
public:
Process * owner;
std::map<int32_t, df::item *> idLookupTable;
uint32_t refVectorOffset;
uint32_t idFieldOffset;
void * itemVectorAddress;
ClassNameCheck isOwnerRefClass;
ClassNameCheck isContainerRefClass;
ClassNameCheck isContainsRefClass;
// Similar to isOwnerRefClass. Value is unique to each creature, but
// different than the creature's id.
ClassNameCheck isUnitHolderRefClass;
// One of these is present for each creature contained in a cage.
// The value is similar to that for isUnitHolderRefClass, different
// than the creature's ID but unique for each creature.
ClassNameCheck isCagedUnitRefClass;
// ID of bulding containing/holding the item.
ClassNameCheck isBuildingHolderRefClass;
// Building ID of lever/etc which triggers bridge/etc holding
// this mechanism.
ClassNameCheck isTriggeredByRefClass;
// Building ID of bridge/etc which is triggered by lever/etc holding
// this mechanism.
ClassNameCheck isTriggerTargetRefClass;
// Civilization ID of owner of item, for items not owned by the
// fortress.
ClassNameCheck isEntityOwnerRefClass;
// Item has been offered to the caravan. The value is the
// civilization ID of
ClassNameCheck isOfferedRefClass;
// Item is in a depot for trade. Purpose of value is unknown, but is
// different for each item, even in the same depot at the same time.
ClassNameCheck isTradingRefClass;
// Item is flying or falling through the air. The value seems to
// be the ID for a "projectile information" object.
ClassNameCheck isProjectileRefClass;
std::set<std::string> knownItemRefTypes;
};
Items::Items()
{
Core & c = Core::getInstance();
d = new Private;
d->owner = c.p;
DFHack::OffsetGroup* itemGroup = c.vinfo->getGroup("Items");
d->itemVectorAddress = itemGroup->getAddress("items_vector");
d->idFieldOffset = itemGroup->getOffset("id");
d->refVectorOffset = itemGroup->getOffset("item_ref_vector");
d->isOwnerRefClass = ClassNameCheck("general_ref_unit_itemownerst");
d->isContainerRefClass = ClassNameCheck("general_ref_contained_in_itemst");
d->isContainsRefClass = ClassNameCheck("general_ref_contains_itemst");
d->isUnitHolderRefClass = ClassNameCheck("general_ref_unit_holderst");
d->isCagedUnitRefClass = ClassNameCheck("general_ref_contains_unitst");
d->isBuildingHolderRefClass
= ClassNameCheck("general_ref_building_holderst");
d->isTriggeredByRefClass = ClassNameCheck("general_ref_building_triggerst");
d->isTriggerTargetRefClass
= ClassNameCheck("general_ref_building_triggertargetst");
d->isEntityOwnerRefClass = ClassNameCheck("general_ref_entity_itemownerst");
d->isOfferedRefClass = ClassNameCheck("general_ref_entity_offeredst");
d->isTradingRefClass = ClassNameCheck("general_ref_unit_tradebringerst");
d->isProjectileRefClass = ClassNameCheck("general_ref_projectilest");
std::vector<std::string> known_names;
ClassNameCheck::getKnownClassNames(known_names);
for (size_t i = 0; i < known_names.size(); i++)
{
if (known_names[i].find("general_ref_") == 0)
d->knownItemRefTypes.insert(known_names[i]);
}
}
bool Items::Start()
{
d->idLookupTable.clear();
return true;
}
bool Items::Finish()
{
return true;
}
bool Items::readItemVector(std::vector<df::item *> &items)
{
std::vector <df::item *> *p_items = (std::vector <df::item *> *) d->itemVectorAddress;
d->idLookupTable.clear();
items.resize(p_items->size());
for (unsigned i = 0; i < p_items->size(); i++)
{
df::item * ptr = p_items->at(i);
items[i] = ptr;
d->idLookupTable[ptr->id] = ptr;
}
return true;
}
df::item * Items::findItemByID(int32_t id) df::item * Items::findItemByID(int32_t id)
{ {
if (id < 0) if (id < 0)
return 0; return 0;
return df::item::find(id);
if (d->idLookupTable.empty())
{
std::vector<df::item *> tmp;
readItemVector(tmp);
}
return d->idLookupTable[id];
}
Items::~Items()
{
Finish();
delete d;
} }
bool Items::copyItem(df::item * itembase, DFHack::dfh_item &item) bool Items::copyItem(df::item * itembase, DFHack::dfh_item &item)
@ -599,23 +466,6 @@ bool Items::readItemRefs(const df::item * item, df::general_ref_type type, std::
return !values.empty(); return !values.empty();
} }
bool Items::unknownRefs(const df::item * item, std::vector<std::pair<std::string, int32_t> >& refs)
{
refs.clear();
for (uint32_t i = 0; i < item->itemrefs.size(); i++)
{
std::string name = ((t_virtual *)(item->itemrefs[i]))->getClassName();
if (d->knownItemRefTypes.find(name) == d->knownItemRefTypes.end())
{
refs.push_back(pair<string, int32_t>(name, item->itemrefs[i]->getID()));
}
}
return (refs.size() > 0);
}
bool Items::removeItemOwner(df::item * item, Units *creatures) bool Items::removeItemOwner(df::item * item, Units *creatures)
{ {
for (uint32_t i = 0; i < item->itemrefs.size(); i++) for (uint32_t i = 0; i < item->itemrefs.size(); i++)

@ -28,6 +28,7 @@ using namespace std;
using namespace DFHack; using namespace DFHack;
using MapExtras::Block; using MapExtras::Block;
using MapExtras::MapCache; using MapExtras::MapCache;
using df::global::world;
DFhackCExport command_result df_autodump(Core * c, vector <string> & parameters); DFhackCExport command_result df_autodump(Core * c, vector <string> & parameters);
DFhackCExport command_result df_autodump_destroy_here(Core * c, vector <string> & parameters); DFhackCExport command_result df_autodump_destroy_here(Core * c, vector <string> & parameters);
@ -95,16 +96,9 @@ static command_result autodump_main(Core * c, vector <string> & parameters)
DFHack::VersionInfo *mem = c->vinfo; DFHack::VersionInfo *mem = c->vinfo;
DFHack::Gui * Gui = c->getGui(); DFHack::Gui * Gui = c->getGui();
DFHack::Items * Items = c->getItems();
DFHack::Maps *Maps = c->getMaps(); DFHack::Maps *Maps = c->getMaps();
vector <df::item*> p_items; std::size_t numItems = world->items.all.size();
if(!Items->readItemVector(p_items))
{
c->con.printerr("Can't access the item vector.\n");
return CR_FAILURE;
}
std::size_t numItems = p_items.size();
// init the map // init the map
if(!Maps->Start()) if(!Maps->Start())
@ -148,7 +142,7 @@ static command_result autodump_main(Core * c, vector <string> & parameters)
// proceed with the dumpification operation // proceed with the dumpification operation
for(std::size_t i=0; i< numItems; i++) for(std::size_t i=0; i< numItems; i++)
{ {
df::item * itm = p_items[i]; df::item * itm = world->items.all[i];
DFCoord pos_item(itm->x, itm->y, itm->z); DFCoord pos_item(itm->x, itm->y, itm->z);
// keep track how many items are at places. all items. // keep track how many items are at places. all items.

@ -20,6 +20,11 @@ using namespace std;
#include "modules/Materials.h" #include "modules/Materials.h"
#include "modules/Translation.h" #include "modules/Translation.h"
using namespace DFHack; using namespace DFHack;
using namespace DFHack::Simple;
#include "DataDefs.h"
#include "df/world.h"
using df::global::world;
DFhackCExport command_result df_cleanowned (Core * c, vector <string> & parameters); DFhackCExport command_result df_cleanowned (Core * c, vector <string> & parameters);
@ -89,7 +94,6 @@ DFhackCExport command_result df_cleanowned (Core * c, vector <string> & paramete
} }
c->Suspend(); c->Suspend();
DFHack::Materials *Materials = c->getMaterials(); DFHack::Materials *Materials = c->getMaterials();
DFHack::Items *Items = c->getItems();
DFHack::Units *Creatures = c->getUnits(); DFHack::Units *Creatures = c->getUnits();
DFHack::Translation *Tran = c->getTranslation(); DFHack::Translation *Tran = c->getTranslation();
@ -99,25 +103,17 @@ DFhackCExport command_result df_cleanowned (Core * c, vector <string> & paramete
ok &= Creatures->Start(num_creatures); ok &= Creatures->Start(num_creatures);
ok &= Tran->Start(); ok &= Tran->Start();
vector<df::item *> p_items; c->con.print("Found total %d items.\n", world->items.all.size());
ok &= Items->readItemVector(p_items);
if(!ok)
{
c->con.printerr("Can't continue due to offset errors.\n");
c->Resume();
return CR_FAILURE;
}
c->con.print("Found total %d items.\n", p_items.size());
for (std::size_t i=0; i < p_items.size(); i++) for (std::size_t i=0; i < world->items.all.size(); i++)
{ {
df::item * item = p_items[i]; df::item * item = world->items.all[i];
bool confiscate = false; bool confiscate = false;
bool dump = false; bool dump = false;
if (!item->flags.bits.owned) if (!item->flags.bits.owned)
{ {
int32_t owner = Items->getItemOwnerID(item); int32_t owner = Items::getItemOwnerID(item);
if (owner >= 0) if (owner >= 0)
{ {
c->con.print("Fixing a misflagged item: \t"); c->con.print("Fixing a misflagged item: \t");
@ -129,8 +125,6 @@ DFhackCExport command_result df_cleanowned (Core * c, vector <string> & paramete
} }
} }
std::string name = Items->getItemClass(item);
if (item->flags.bits.rotten) if (item->flags.bits.rotten)
{ {
c->con.print("Confiscating a rotten item: \t"); c->con.print("Confiscating a rotten item: \t");
@ -189,7 +183,7 @@ DFhackCExport command_result df_cleanowned (Core * c, vector <string> & paramete
item->getWear() item->getWear()
); );
int32_t owner = Items->getItemOwnerID(item); int32_t owner = Items::getItemOwnerID(item);
int32_t owner_index = Creatures->FindIndexById(owner); int32_t owner_index = Creatures->FindIndexById(owner);
std::string info; std::string info;
@ -206,7 +200,7 @@ DFhackCExport command_result df_cleanowned (Core * c, vector <string> & paramete
if (!dry_run) if (!dry_run)
{ {
if (!Items->removeItemOwner(item, Creatures)) if (!Items::removeItemOwner(item, Creatures))
c->con.print("(unsuccessfully) "); c->con.print("(unsuccessfully) ");
if (dump) if (dump)
item->flags.bits.dump = 1; item->flags.bits.dump = 1;