diff --git a/library/Core.cpp b/library/Core.cpp index 9404fdb5b..68af3e15a 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1094,7 +1094,6 @@ MODULE_GETTER(Maps); MODULE_GETTER(Gui); MODULE_GETTER(World); MODULE_GETTER(Materials); -MODULE_GETTER(Items); MODULE_GETTER(Translation); MODULE_GETTER(Vegetation); MODULE_GETTER(Constructions); diff --git a/library/include/Core.h b/library/include/Core.h index 3366b990b..988595062 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -58,7 +58,6 @@ namespace DFHack class Gui; class World; class Materials; - class Items; class Translation; class Vegetation; class Constructions; @@ -111,8 +110,6 @@ namespace DFHack World * getWorld(); /// get the materials module Materials * getMaterials(); - /// get the items module - Items * getItems(); /// get the translation module Translation * getTranslation(); /// get the vegetation module @@ -173,7 +170,6 @@ namespace DFHack Gui * pGui; World * pWorld; Materials * pMaterials; - Items * pItems; Translation * pTranslation; Vegetation * pVegetation; Constructions * pConstructions; diff --git a/library/include/ModuleFactory.h b/library/include/ModuleFactory.h index 0ee5b178f..78ff9db19 100644 --- a/library/include/ModuleFactory.h +++ b/library/include/ModuleFactory.h @@ -35,7 +35,6 @@ namespace DFHack Module* createGui(); Module* createWorld(); Module* createMaterials(); - Module* createItems(); Module* createTranslation(); Module* createVegetation(); Module* createBuildings(); diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 828fb07cf..4cd29766a 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -116,46 +116,32 @@ struct dfh_item * \ingroup grp_modules * \ingroup grp_items */ -class DFHACK_EXPORT Items : public Module +namespace Simple { -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 &items); - /// Look for a particular item by ID - df::item * findItemByID(int32_t id); - - /// Make a partial copy of a DF item - bool copyItem(df::item * source, dfh_item & target); - /// write copied item back to its origin - bool writeItem(const dfh_item & item); - - /// get the class name of an item - std::string getItemClass(const df::item * item); - /// who owns this item we already read? - int32_t getItemOwnerID(const df::item * item); - /// which item is it contained in? - int32_t getItemContainerID(const df::item * item); - /// which items does it contain? - bool getContainedItems(const df::item * item, /*output*/ std::vector &items); - /// 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 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); -private: - class Private; - Private* d; -}; +namespace Items +{ + +/// Look for a particular item by ID +DFHACK_EXPORT df::item * findItemByID(int32_t id); + +/// Make a partial copy of a DF item +DFHACK_EXPORT bool copyItem(df::item * source, dfh_item & target); +/// write copied item back to its origin +DFHACK_EXPORT bool writeItem(const dfh_item & item); + +/// get the class name of an item +DFHACK_EXPORT std::string getItemClass(const df::item * item); +/// who owns this item we already read? +DFHACK_EXPORT int32_t getItemOwnerID(const df::item * item); +/// which item is it contained in? +DFHACK_EXPORT int32_t getItemContainerID(const df::item * item); +/// which items does it contain? +DFHACK_EXPORT bool getContainedItems(const df::item * item, /*output*/ std::vector &items); +/// wipe out the owner records +DFHACK_EXPORT bool removeItemOwner(df::item * item, Units *creatures); +/// read item references, filtered by class +DFHACK_EXPORT bool readItemRefs(const df::item * item, const df::general_ref_type type, + /*output*/ std::vector &values); +} } +} \ No newline at end of file diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index c7dfe202d..779fef8cf 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -807,8 +807,6 @@ namespace DFHack void CopyNameTo(df_unit *creature, df_name * target); - protected: - friend class Items; bool RemoveOwnedItemByIdx(const uint32_t index, int32_t id); bool RemoveOwnedItemByPtr(df_unit * unit, int32_t id); diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 4588b4397..d54a56f13 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -66,7 +66,9 @@ using namespace std; #include "df/general_ref.h" using namespace DFHack; +using namespace DFHack::Simple; using namespace df::enums; +using df::global::world; #define ITEMDEF_VECTORS \ 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); } - -Module* DFHack::createItems() -{ - return new Items(); -} - -class Items::Private -{ - public: - Process * owner; - std::map 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 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 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 &items) -{ - std::vector *p_items = (std::vector *) 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) { if (id < 0) return 0; - - if (d->idLookupTable.empty()) - { - std::vector tmp; - readItemVector(tmp); - } - - return d->idLookupTable[id]; -} - -Items::~Items() -{ - Finish(); - delete d; + return df::item::find(id); } 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(); } -bool Items::unknownRefs(const df::item * item, std::vector >& 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(name, item->itemrefs[i]->getID())); - } - } - - return (refs.size() > 0); -} - bool Items::removeItemOwner(df::item * item, Units *creatures) { for (uint32_t i = 0; i < item->itemrefs.size(); i++) diff --git a/plugins/autodump.cpp b/plugins/autodump.cpp index 8c1820f2b..8c7f9a16b 100644 --- a/plugins/autodump.cpp +++ b/plugins/autodump.cpp @@ -28,6 +28,7 @@ using namespace std; using namespace DFHack; using MapExtras::Block; using MapExtras::MapCache; +using df::global::world; DFhackCExport command_result df_autodump(Core * c, vector & parameters); DFhackCExport command_result df_autodump_destroy_here(Core * c, vector & parameters); @@ -95,16 +96,9 @@ static command_result autodump_main(Core * c, vector & parameters) DFHack::VersionInfo *mem = c->vinfo; DFHack::Gui * Gui = c->getGui(); - DFHack::Items * Items = c->getItems(); DFHack::Maps *Maps = c->getMaps(); - vector p_items; - 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(); + std::size_t numItems = world->items.all.size(); // init the map if(!Maps->Start()) @@ -148,7 +142,7 @@ static command_result autodump_main(Core * c, vector & parameters) // proceed with the dumpification operation 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); // keep track how many items are at places. all items. diff --git a/plugins/cleanowned.cpp b/plugins/cleanowned.cpp index 0935c2eaa..4e8c254bc 100644 --- a/plugins/cleanowned.cpp +++ b/plugins/cleanowned.cpp @@ -20,6 +20,11 @@ using namespace std; #include "modules/Materials.h" #include "modules/Translation.h" 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 & parameters); @@ -89,7 +94,6 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete } c->Suspend(); DFHack::Materials *Materials = c->getMaterials(); - DFHack::Items *Items = c->getItems(); DFHack::Units *Creatures = c->getUnits(); DFHack::Translation *Tran = c->getTranslation(); @@ -99,25 +103,17 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete ok &= Creatures->Start(num_creatures); ok &= Tran->Start(); - vector p_items; - 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()); + c->con.print("Found total %d items.\n", world->items.all.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 dump = false; if (!item->flags.bits.owned) { - int32_t owner = Items->getItemOwnerID(item); + int32_t owner = Items::getItemOwnerID(item); if (owner >= 0) { c->con.print("Fixing a misflagged item: \t"); @@ -129,8 +125,6 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete } } - std::string name = Items->getItemClass(item); - if (item->flags.bits.rotten) { c->con.print("Confiscating a rotten item: \t"); @@ -189,7 +183,7 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete item->getWear() ); - int32_t owner = Items->getItemOwnerID(item); + int32_t owner = Items::getItemOwnerID(item); int32_t owner_index = Creatures->FindIndexById(owner); std::string info; @@ -206,7 +200,7 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete if (!dry_run) { - if (!Items->removeItemOwner(item, Creatures)) + if (!Items::removeItemOwner(item, Creatures)) c->con.print("(unsuccessfully) "); if (dump) item->flags.bits.dump = 1;