From 64b55acf37519450f6feca5cda7d8fba2ee1e491 Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 20:16:16 -0600 Subject: [PATCH 1/8] df_item -> df::item, t_itemflags -> df::item_flags --- library/include/modules/Items.h | 489 +------------------------------- library/include/modules/Units.h | 6 +- library/modules/Items.cpp | 38 +-- library/modules/Units.cpp | 4 +- library/modules/kitchen.cpp | 11 +- plugins/autodump.cpp | 30 +- plugins/cleanowned.cpp | 26 +- plugins/seedwatch.cpp | 31 +- 8 files changed, 87 insertions(+), 548 deletions(-) diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index d0424a6f0..566ba3447 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -34,6 +34,7 @@ distribution. #include "MemAccess.h" #include "DataDefs.h" +#include "df/item.h" #include "df/item_type.h" namespace df @@ -90,58 +91,6 @@ class Context; class DFContextShared; class Units; -/** - * Item flags. A bit fuzzy. - * Mostly from http://dwarffortresswiki.net/index.php/User:Rick/Memory_research - * \ingroup grp_items - */ -union t_itemflags -{ - uint32_t whole; ///< the whole struct. all 32 bits of it, as an integer - struct - { - unsigned int on_ground : 1; ///< 0000 0001 Item on ground - unsigned int in_job : 1; ///< 0000 0002 Item currently being used in a job - unsigned int hostile : 1; ///< 0000 0004 Item owned by hostile - unsigned int in_inventory : 1; ///< 0000 0008 Item in a creature or workshop inventory - - unsigned int unk1 : 1; ///< 0000 0010 unknown, lost (artifact)?, unusable, unseen - unsigned int in_building : 1; ///< 0000 0020 Part of a building (including mechanisms, bodies in coffins) - unsigned int unk2 : 1; ///< 0000 0040 unknown, unseen - unsigned int dead_dwarf : 1; ///< 0000 0080 Dwarf's dead body or body part - - unsigned int rotten : 1; ///< 0000 0100 Rotten food - unsigned int spider_web : 1; ///< 0000 0200 Thread in spider web - unsigned int construction : 1; ///< 0000 0400 Material used in construction - unsigned int unk3 : 1; ///< 0000 0800 unknown, unseen, unusable - - unsigned int unk4 : 1; ///< 0000 1000 unknown, unseen - unsigned int murder : 1; ///< 0000 2000 Implies murder - used in fell moods - unsigned int foreign : 1; ///< 0000 4000 Item is imported - unsigned int trader : 1; ///< 0000 8000 Item ownwed by trader - - unsigned int owned : 1; ///< 0001 0000 Item is owned by a dwarf - unsigned int garbage_colect : 1; ///< 0002 0000 Marked for deallocation by DF it seems - unsigned int artifact1 : 1; ///< 0004 0000 Artifact ? - unsigned int forbid : 1; ///< 0008 0000 Forbidden item - - unsigned int unk6 : 1; ///< 0010 0000 unknown, unseen - unsigned int dump : 1; ///< 0020 0000 Designated for dumping - unsigned int on_fire: 1; ///< 0040 0000 Indicates if item is on fire, Will Set Item On Fire if Set! - unsigned int melt : 1; ///< 0080 0000 Designated for melting, if applicable - - unsigned int hidden : 1; ///< 0100 0000 Hidden item - unsigned int in_chest : 1; ///< 0200 0000 Stored in chest/part of well? - unsigned int unk7 : 1; ///< 0400 0000 unknown, unseen - unsigned int artifact2 : 1; ///< 0800 0000 Artifact ? - - unsigned int unk8 : 1; ///< 1000 0000 unknown, unseen, common - unsigned int unk9 : 1; ///< 2000 0000 unknown, set when viewing details - unsigned int unk10 : 1; ///< 4000 0000 unknown, unseen - unsigned int unk11 : 1; ///< 8000 0000 unknown, unseen - }; -}; - /** * Describes relationship of an item with other objects * \ingroup grp_items @@ -161,337 +110,17 @@ struct df_contaminant int32_t size; ///< 1-24=spatter, 25-49=smear, 50-* = coating }; -/** - * A partial mirror of a DF base type for items - * \ingroup grp_items - */ -class df_item -{ -public: - // 4 - int16_t x; - int16_t y; - // 8 - int16_t z; - // C - t_itemflags flags; - // 10 - uint32_t age; - // 14 - uint32_t id; - // 18 - std::vector unk1; - // 24 L, 28 W - std::vector itemrefs; - // 30 L, 38 W - these were mostly unset (0xCC with malloc patch) - int16_t mystery_meat[12]; - // 48 L, 50 W - int32_t mystery1; - // 4C L, 54 W - int32_t mystery2; - // 50 L, 58 W - int32_t mystery3; - // 54 L, 5C W - int32_t mystery4; - // 58 L, 60 W - int32_t mystery5; - // 5C L, 64 W - pointer to vector of contaminants - std::vector * contaminants; - // 60 L, 6C W - temperature in Urists - int16_t temperature; - // 62 L, 6E W - temperature fraction (maybe, just a guess) - int16_t temperature_fraction; -public: - // 0x0 - virtual t_itemType getType(); - virtual t_itemSubtype getSubtype(); - virtual t_materialType getMaterial(); - virtual t_materialIndex getMaterialIndex(); - // 0x10 - virtual void setSubType(t_itemSubtype); - virtual void setMaterial(t_materialType mat); - virtual void setMaterialIndex (t_materialIndex submat); - virtual t_materialType getMaterial2(); // weird - // 0x20 - virtual t_materialIndex getMaterialIndex2(); // weird - virtual void fn9(void); - virtual void fn10(void); - virtual void fn11(void); - // 0x30 - virtual void fn12(void); - virtual void fn13(void); - virtual void fn14(void); - virtual void fn15(void); - // 0x40 - virtual void fn16(void); - virtual void fn17(void); - virtual void fn18(void); - virtual void fn19(void); - // 0x50 - virtual void fn20(void); - virtual void fn21(void); - virtual void fn22(void); - virtual void fn23(void); - // 0x60 - virtual void fn24(void); - virtual void fn25(void); - virtual void fn26(void); - virtual void fn27(void); - // 0x70 - virtual void fn28(void); - virtual void fn29(void); - virtual void fn30(void); - virtual void fn31(void); - // 0x80 - virtual void fn32(void); - virtual void fn33(void); - virtual void fn34(void); - virtual void fn35(void); - // 0x90 - virtual void fn36(void); - virtual void fn37(void); - virtual void fn38(void); - virtual void fn39(void); - // 0xA0 - virtual void fn40(void); - virtual void fn41(void); - virtual void fn42(void); - virtual void fn43(void); - // 0xB0 - virtual void fn44(void); - virtual void fn45(void); - virtual void fn46(void); - virtual void fn47(void); - // 0xC0 - virtual void fn48(void); - virtual void fn49(void); - virtual void fn50(void); - virtual int16_t getWear(void); // 0 = normal, 1 = x, 2 = X, 3 = XX - // 0xD0 - virtual void setWear(int16_t wear); // also zeroes wear timer? - virtual void fn53(void); - virtual void fn54(void); - virtual void fn55(void); - // 0xE0 - virtual void fn56(void); - virtual void fn57(void); - virtual void fn58(void); - virtual void fn59(void); - // 0xF0 - virtual void fn60(void); - virtual void fn61(void); - virtual void fn62(void); - virtual void fn63(void); - // 0x100 - virtual void fn64(void); - virtual void fn65(void); - virtual void fn66(void); - virtual void fn67(void); - // 0x110 - virtual void fn68(void); - virtual void fn69(void); - virtual void fn70(void); - virtual void fn71(void); - // 0x120 - virtual void fn72(void); - virtual void fn73(void); - virtual void fn74(void); - virtual void fn75(void); - // 0x130 - virtual void fn76(void); - virtual void fn77(void); - virtual void fn78(void); - virtual void fn79(void); - // 0x140 - virtual void fn80(void); - virtual void fn81(void); - virtual void fn82(void); - virtual void fn83(void); - // 0x150 - virtual void fn84(void); - virtual void fn85(void); - virtual void fn86(void); - virtual void fn87(void); - // 0x160 - virtual void fn88(void); - virtual void fn89(void); - virtual void fn90(void); - virtual void fn91(void); - // 0x170 - virtual void fn92(void); - virtual void fn93(void); - virtual void fn94(void); - virtual void fn95(void); - // 0x180 - virtual void fn96(void); - virtual void fn97(void); - virtual void fn98(void); - virtual void fn99(void); - // 0x190 - virtual void fn100(void); - virtual void fn101(void); - virtual void fn102(void); - virtual void fn103(void); - // 0x1A0 - virtual void fn104(void); - virtual void fn105(void); - virtual void fn106(void); - virtual void fn107(void); - // 0x1B0 - virtual void fn108(void); - virtual void fn109(void); - virtual void fn110(void); - virtual void fn111(void); - // 0x1C0 - virtual void fn112(void); - virtual void fn113(void); - virtual void fn114(void); - virtual void fn115(void); - // 0x1D0 - virtual void fn116(void); - virtual void fn117(void); - virtual void fn118(void); - virtual void fn119(void); - // 0x1E0 - virtual void fn120(void); - virtual void fn121(void); - virtual void fn122(void); - virtual void fn123(void); - // 0x1F0 - virtual void fn124(void); - virtual void fn125(void); - virtual void fn126(void); - virtual void fn127(void); - // 0x200 - virtual void fn128(void); - virtual void fn129(void); - virtual void fn130(void); - virtual void fn131(void); - // 0x210 - virtual void fn132(void); - virtual int32_t getStackSize( void ); - virtual void fn134(void); - virtual void fn135(void); - // 0x220 - virtual void fn136(void); - virtual void fn137(void); - virtual void fn138(void); - virtual void fn139(void); - // 0x230 - virtual void fn140(void); - virtual void fn141(void); - virtual void fn142(void); - virtual void fn143(void); - // 0x240 - virtual void fn144(void); - virtual void fn145(void); - virtual void fn146(void); - virtual void fn147(void); - // 0x250 - virtual void fn148(void); - virtual void fn149(void); - virtual void fn150(void); - virtual int16_t getQuality( void ); - // 0x260 - virtual void fn152(void); - virtual void fn153(void); - virtual void fn154(void); - virtual void fn155(void); - // 0x270 - virtual void fn156(void); - virtual void fn157(void); - virtual void fn158(void); - virtual void fn159(void); - // 0x280 - virtual void fn160(void); - virtual void fn161(void); - virtual void fn162(void); - virtual void fn163(void); - // 0x290 - virtual void fn164(void); - virtual void fn165(void); - virtual void fn166(void); - virtual void fn167(void); - // 0x2A0 - virtual void fn168(void); // value returned (int) = first param to descriprion construction function - virtual void fn169(void); - virtual void fn170(void); - virtual void fn171(void); - // 0x2B0 - virtual void fn172(void); - virtual void fn173(void); - virtual void fn174(void); - virtual void fn175(void); - // 0x2C0 - virtual void fn176(void); - virtual void fn177(void); - //virtual std::string *getItemDescription ( std::string * str, int sizes = 0); // 0 = stacked, 1 = singular, 2 = plural - virtual std::string *getItemDescription ( std::string * str, int sizes = 0); // 0 = stacked, 1 = singular, 2 = plural - virtual void fn179(void); - // 0x2D0 - virtual void fn180(void); - virtual void fn181(void); - virtual void fn182(void); - virtual void fn183(void); - // 0x2E0 - virtual void fn184(void); - virtual void fn185(void); - virtual void fn186(void); - virtual void fn187(void); - // 0x2F0 - virtual void fn188(void); - virtual void fn189(void); - virtual void fn190(void); - virtual void fn191(void); - // 0x300 - virtual void fn192(void); - virtual void fn193(void); - virtual void fn194(void); - virtual void fn195(void); - // 0x310 - virtual void fn196(void); - virtual void fn197(void); - virtual void fn198(void); - virtual void fn199(void); - // 0x320 - virtual void fn200(void); - virtual void fn201(void); - virtual void fn202(void); - virtual void fn203(void); - // 0x330 - virtual void fn204(void); - virtual void fn205(void); - virtual void fn206(void); - virtual void fn207(void); - // 0x340 - virtual void fn208(void); - virtual void fn209(void); - virtual void fn210(void); - virtual void fn211(void); - // 0x350 - virtual void fn212(void); - virtual void fn213(void); - virtual void fn214(void); - virtual void fn215(void); - // 0x360 - virtual void fn216(void); - virtual void fn217(void); - - // and this should be all 218 of them - // fn numbers start with 0 -}; - /** * Type for holding an item read from DF * \ingroup grp_items */ struct dfh_item { - df_item *origin; // where this was read from + df::item *origin; // where this was read from int16_t x; int16_t y; int16_t z; - t_itemflags flags; + df::item_flags flags; uint32_t age; uint32_t id; t_material matdesc; @@ -523,98 +152,6 @@ public: * All the known item types as an enum * From http://df.magmawiki.com/index.php/DF2010:Item_token */ - enum item_types - { - BAR, ///< Bars, such as metal, fuel, or soap. - SMALLGEM, ///< Cut gemstones usable in jeweler's workshop - BLOCKS, ///< Blocks of any kind. - ROUGH, ///< Rough gemstones. - BOULDER, ///< or IT_STONE - raw mined stone. - STONE = BOULDER, - WOOD, ///< Wooden logs. - DOOR, ///< Doors. - FLOODGATE, ///< Floodgates. - BED, ///< Beds. - CHAIR, ///< Chairs and thrones. - CHAIN, ///< Restraints. - FLASK, ///< Flasks. - GOBLET, ///< Goblets. - INSTRUMENT, ///< Musical instruments. Subtypes come from item_instrument.txt - TOY, ///< Toys. Subtypes come from item_toy.txt - WINDOW, ///< Glass windows. - CAGE, ///< Cages. - BARREL, ///< Barrels. - BUCKET, ///< Buckets. - ANIMALTRAP, ///< Animal traps. - TABLE, ///< Tables. - COFFIN, ///< Coffins. - STATUE, ///< Statues. - CORPSE, ///< Corpses. - WEAPON, ///< Weapons. Subtypes come from item_weapon.txt - ARMOR, ///< Armor and clothing worn on the upper body. Subtypes come from item_armor.txt - SHOES, ///< Armor and clothing worn on the feet. Subtypes come from item_shoes.txt - SHIELD, ///< Shields and bucklers. Subtypes come from item_shield.txt - HELM, ///< Armor and clothing worn on the head. Subtypes come from item_helm.txt - GLOVES, ///< Armor and clothing worn on the hands. Subtypes come from item_gloves.txt - BOX, ///< Chests (wood), coffers (stone), boxes (glass), and bags (cloth or leather). - BIN, ///< Bins. - ARMORSTAND, ///< Armor stands. - WEAPONRACK, ///< Weapon racks. - CABINET, ///< Cabinets. - FIGURINE, ///< Figurines. - AMULET, ///< Amulets. - SCEPTER, ///< Scepters. - AMMO, ///< Ammunition for hand-held weapons. Subtypes come from item_ammo.txt - CROWN, ///< Crowns. - RING, ///< Rings. - EARRING, ///< Earrings. - BRACELET, ///< Bracelets. - GEM, ///< Large gems. - ANVIL, ///< Anvils. - CORPSEPIECE,///< Body parts. - REMAINS, ///< Dead vermin bodies. - MEAT, ///< Butchered meat. - FISH, ///< Prepared fish. - FISH_RAW, ///< Unprepared fish. - VERMIN, ///< Live vermin. - PET, ///< Tame vermin. - SEEDS, ///< Seeds from plants. - PLANT, ///< Plants. - SKIN_TANNED,///< Tanned skins. - LEAVES, ///< Leaves, usually from quarry bushes. - THREAD, ///< Thread gathered from webs or made at the farmer's workshop. - CLOTH, ///< Cloth made at the loom. - TOTEM, ///< Skull totems. - PANTS, ///< Armor and clothing worn on the legs. Subtypes come from item_pants.txt - BACKPACK, ///< Backpacks. - QUIVER, ///< Quivers. - CATAPULTPARTS, ///< Catapult parts. - BALLISTAPARTS, ///< Ballista parts. - SIEGEAMMO, ///< Siege engine ammunition. Subtypes come from item_siegeammo.txt - BALLISTAARROWHEAD, ///< Ballista arrow heads. - TRAPPARTS, ///< Mechanisms. - TRAPCOMP, ///< Trap components. Subtypes come from item_trapcomp.txt - DRINK, ///< Alcoholic drinks. - POWDER_MISC,///< Powders such as flour, gypsum plaster, dye, or sand. - CHEESE, ///< Pieces of cheese. - FOOD, ///< Prepared meals. Subtypes come from item_food.txt - LIQUID_MISC,///< Liquids such as water, lye, and extracts. - COIN, ///< Coins. - GLOB, ///< Fat, tallow, pastes/pressed objects, and small bits of molten rock/metal. - ROCK, ///< Small rocks (usually sharpened and/or thrown in adventurer mode) - PIPE_SECTION, ///< Pipe sections. - HATCH_COVER, ///< Hatch covers. - GRATE, ///< Grates. - QUERN, ///< Querns. - MILLSTONE, ///< Millstones. - SPLINT, ///< Splints. - CRUTCH, ///< Crutches. - TRACTION_BENCH, ///< Traction benches. - ORTHOPEDIC_CAST, ///< Casts. - TOOL, ///< Tools. Subtypes come from item_tool.txt - SLAB, ///< Slabs. - EGG, ///< Eggs. - }; public: Items(); @@ -622,30 +159,30 @@ public: bool Start(); bool Finish(); /// Read the item vector from DF into a supplied vector - bool readItemVector(std::vector &items); + bool readItemVector(std::vector &items); /// Look for a particular item by ID - df_item * findItemByID(int32_t id); + df::item * findItemByID(int32_t id); /// Make a partial copy of a DF item - bool copyItem(df_item * source, dfh_item & target); + 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); + std::string getItemClass(const df::item * item); /// who owns this item we already read? - int32_t getItemOwnerID(const df_item * item); + int32_t getItemOwnerID(const df::item * item); /// which item is it contained in? - int32_t getItemContainerID(const df_item * item); + int32_t getItemContainerID(const df::item * item); /// which items does it contain? - bool getContainedItems(const df_item * item, /*output*/ std::vector &items); + bool getContainedItems(const df::item * item, /*output*/ std::vector &items); /// 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 - bool readItemRefs(const df_item * item, const ClassNameCheck &classname, + bool readItemRefs(const df::item * item, const ClassNameCheck &classname, /*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); + bool unknownRefs(const df::item * item, /*output*/ std::vector >& refs); private: class Private; Private* d; diff --git a/library/include/modules/Units.h b/library/include/modules/Units.h index d373b0a16..c7dfe202d 100644 --- a/library/include/modules/Units.h +++ b/library/include/modules/Units.h @@ -557,7 +557,7 @@ namespace DFHack int16_t unk_280; int32_t unk_284; - std::vector inventory; // 288 - vector of item pointers + std::vector inventory; // 288 - vector of item pointers std::vector owned_items; // 298 - vector of item IDs std::vector unk_2a8; std::vector unk_2b8; @@ -777,8 +777,8 @@ namespace DFHack bool ReadJob(const df_unit * unit, std::vector & mat); - bool ReadInventoryByIdx(const uint32_t index, std::vector & item); - bool ReadInventoryByPtr(const df_unit * unit, std::vector & item); + bool ReadInventoryByIdx(const uint32_t index, std::vector & item); + bool ReadInventoryByPtr(const df_unit * unit, std::vector & item); bool ReadOwnedItemsByIdx(const uint32_t index, std::vector & item); bool ReadOwnedItemsByPtr(const df_unit * unit, std::vector & item); diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 9fffcc03d..4c695607d 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -403,7 +403,7 @@ class Items::Private { public: Process * owner; - std::map idLookupTable; + std::map idLookupTable; uint32_t refVectorOffset; uint32_t idFieldOffset; void * itemVectorAddress; @@ -498,16 +498,16 @@ bool Items::Finish() return true; } -bool Items::readItemVector(std::vector &items) +bool Items::readItemVector(std::vector &items) { - std::vector *p_items = (std::vector *) d->itemVectorAddress; + 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); + df::item * ptr = p_items->at(i); items[i] = ptr; d->idLookupTable[ptr->id] = ptr; } @@ -515,14 +515,14 @@ bool Items::readItemVector(std::vector &items) return true; } -df_item * Items::findItemByID(int32_t id) +df::item * Items::findItemByID(int32_t id) { if (id < 0) return 0; if (d->idLookupTable.empty()) { - std::vector tmp; + std::vector tmp; readItemVector(tmp); } @@ -535,11 +535,11 @@ Items::~Items() delete d; } -bool Items::copyItem(df_item * itembase, DFHack::dfh_item &item) +bool Items::copyItem(df::item * itembase, DFHack::dfh_item &item) { if(!itembase) return false; - df_item * itreal = (df_item *) itembase; + df::item * itreal = (df::item *) itembase; item.origin = itembase; item.x = itreal->x; item.y = itreal->y; @@ -557,7 +557,7 @@ bool Items::copyItem(df_item * itembase, DFHack::dfh_item &item) return true; } -int32_t Items::getItemOwnerID(const DFHack::df_item * item) +int32_t Items::getItemOwnerID(const df::item * item) { std::vector vals; if (readItemRefs(item, d->isOwnerRefClass, vals)) @@ -566,7 +566,7 @@ int32_t Items::getItemOwnerID(const DFHack::df_item * item) return -1; } -int32_t Items::getItemContainerID(const DFHack::df_item * item) +int32_t Items::getItemContainerID(const df::item * item) { std::vector vals; if (readItemRefs(item, d->isContainerRefClass, vals)) @@ -575,14 +575,14 @@ int32_t Items::getItemContainerID(const DFHack::df_item * item) return -1; } -bool Items::getContainedItems(const DFHack::df_item * item, std::vector &items) +bool Items::getContainedItems(const df::item * item, std::vector &items) { return readItemRefs(item, d->isContainsRefClass, items); } -bool Items::readItemRefs(const df_item * item, const ClassNameCheck &classname, std::vector &values) +bool Items::readItemRefs(const df::item * item, const ClassNameCheck &classname, std::vector &values) { - const std::vector &p_refs = item->itemrefs; + const std::vector &p_refs = (const std::vector &)item->itemrefs; values.clear(); for (uint32_t i=0; i >& refs) +bool Items::unknownRefs(const df::item * item, std::vector >& refs) { refs.clear(); - const std::vector &p_refs = item->itemrefs; + const std::vector &p_refs = (const std::vector &)item->itemrefs; for (uint32_t i=0; i 0); } -bool Items::removeItemOwner(df_item * item, Units *creatures) +bool Items::removeItemOwner(df::item * item, Units *creatures) { - std::vector &p_refs = item->itemrefs; + std::vector &p_refs = (std::vector &)item->itemrefs; for (uint32_t i=0; iisOwnerRefClass(d->owner, p_refs[i]->vptr)) @@ -632,12 +632,12 @@ bool Items::removeItemOwner(df_item * item, Units *creatures) p_refs.erase(p_refs.begin() + i--); } - item->flags.owned = 0; + item->flags.bits.owned = 0; return true; } -std::string Items::getItemClass(const df_item * item) +std::string Items::getItemClass(const df::item * item) { const t_virtual * virt = (t_virtual *) item; return virt->getClassName(); diff --git a/library/modules/Units.cpp b/library/modules/Units.cpp index f969e3f90..f69d74c69 100644 --- a/library/modules/Units.cpp +++ b/library/modules/Units.cpp @@ -549,7 +549,7 @@ bool Creatures::ReadJob(const t_creature * furball, vector & mat) return true; } */ -bool Units::ReadInventoryByIdx(const uint32_t index, std::vector & item) +bool Units::ReadInventoryByIdx(const uint32_t index, std::vector & item) { if(!d->Started) return false; if(index >= creatures->size()) return false; @@ -557,7 +557,7 @@ bool Units::ReadInventoryByIdx(const uint32_t index, std::vector & it return this->ReadInventoryByPtr(temp, item); } -bool Units::ReadInventoryByPtr(const df_unit * temp, std::vector & items) +bool Units::ReadInventoryByPtr(const df_unit * temp, std::vector & items) { if(!d->Started) return false; items = temp->inventory; diff --git a/library/modules/kitchen.cpp b/library/modules/kitchen.cpp index fbf11a079..cd67a9e23 100644 --- a/library/modules/kitchen.cpp +++ b/library/modules/kitchen.cpp @@ -19,6 +19,7 @@ using namespace std; #include "ModuleFactory.h" #include "Core.h" #include "Virtual.h" +#include "df/item_type.h" namespace DFHack { @@ -89,7 +90,7 @@ namespace Kitchen for(std::size_t i = 0; i < size(); ++i) { if(d->materialIndices[i] == materialIndex - && (d->itemTypes[i] == DFHack::Items::SEEDS || d->itemTypes[i] == DFHack::Items::PLANT) + && (d->itemTypes[i] == df::item_type::SEEDS || d->itemTypes[i] == df::item_type::PLANT) && d->exclusionTypes[i] == cookingExclusion ) { @@ -119,15 +120,15 @@ namespace Kitchen if(d->materialIndices[i] == materialIndex && d->exclusionTypes[i] == cookingExclusion) { - if(d->itemTypes[i] == DFHack::Items::SEEDS) + if(d->itemTypes[i] == df::item_type::SEEDS) SeedAlreadyIn = true; - else if (d->itemTypes[i] == DFHack::Items::PLANT) + else if (d->itemTypes[i] == df::item_type::PLANT) PlantAlreadyIn = true; } } if(!SeedAlreadyIn) { - d->itemTypes.push_back(DFHack::Items::SEEDS); + d->itemTypes.push_back(df::item_type::SEEDS); d->itemSubtypes.push_back(organicSubtype); d->materialTypes.push_back(type->material_type_seed); d->materialIndices.push_back(materialIndex); @@ -135,7 +136,7 @@ namespace Kitchen } if(!PlantAlreadyIn) { - d->itemTypes.push_back(DFHack::Items::PLANT); + d->itemTypes.push_back(df::item_type::PLANT); d->itemSubtypes.push_back(organicSubtype); d->materialTypes.push_back(type->material_type_basic_mat); d->materialIndices.push_back(materialIndex); diff --git a/plugins/autodump.cpp b/plugins/autodump.cpp index 1ad392ed7..8c1820f2b 100644 --- a/plugins/autodump.cpp +++ b/plugins/autodump.cpp @@ -98,7 +98,7 @@ static command_result autodump_main(Core * c, vector & parameters) DFHack::Items * Items = c->getItems(); DFHack::Maps *Maps = c->getMaps(); - vector p_items; + vector p_items; if(!Items->readItemVector(p_items)) { c->con.printerr("Can't access the item vector.\n"); @@ -148,7 +148,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 = p_items[i]; DFCoord pos_item(itm->x, itm->y, itm->z); // keep track how many items are at places. all items. @@ -165,22 +165,22 @@ static command_result autodump_main(Core * c, vector & parameters) // iterator is valid here, we use it later to decrement the pile counter if the item is moved // only dump the stuff marked for dumping and laying on the ground - if ( !itm->flags.dump - || !itm->flags.on_ground - || itm->flags.construction - || itm->flags.hidden - || itm->flags.in_building - || itm->flags.in_chest - || itm->flags.in_inventory - || itm->flags.artifact1 + if ( !itm->flags.bits.dump + || !itm->flags.bits.on_ground + || itm->flags.bits.construction + || itm->flags.bits.hidden + || itm->flags.bits.in_building + || itm->flags.bits.in_chest + || itm->flags.bits.in_inventory + || itm->flags.bits.artifact1 ) continue; if(!destroy) // move to cursor { // Change flags to indicate the dump was completed, as if by super-dwarfs - itm->flags.dump = false; - itm->flags.forbid = true; + itm->flags.bits.dump = false; + itm->flags.bits.forbid = true; // Don't move items if they're already at the cursor if (pos_cursor == pos_item) @@ -221,11 +221,11 @@ static command_result autodump_main(Core * c, vector & parameters) if (here && pos_item != pos_cursor) continue; - itm->flags.garbage_colect = true; + itm->flags.bits.garbage_colect = true; // Cosmetic changes: make them disappear from view instantly - itm->flags.forbid = true; - itm->flags.hidden = true; + itm->flags.bits.forbid = true; + itm->flags.bits.hidden = true; } // keeping track of item pile sizes ;) it->second --; diff --git a/plugins/cleanowned.cpp b/plugins/cleanowned.cpp index 966d8e335..0935c2eaa 100644 --- a/plugins/cleanowned.cpp +++ b/plugins/cleanowned.cpp @@ -99,7 +99,7 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete ok &= Creatures->Start(num_creatures); ok &= Tran->Start(); - vector p_items; + vector p_items; ok &= Items->readItemVector(p_items); if(!ok) { @@ -111,11 +111,11 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete for (std::size_t i=0; i < p_items.size(); i++) { - df_item * item = p_items[i]; + df::item * item = p_items[i]; bool confiscate = false; bool dump = false; - if (!item->flags.owned) + if (!item->flags.bits.owned) { int32_t owner = Items->getItemOwnerID(item); if (owner >= 0) @@ -131,21 +131,21 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete std::string name = Items->getItemClass(item); - if (item->flags.rotten) + if (item->flags.bits.rotten) { c->con.print("Confiscating a rotten item: \t"); confiscate = true; } - else if (item->flags.on_ground) + else if (item->flags.bits.on_ground) { int32_t type = item->getType(); - if(type == Items::MEAT || - type == Items::FISH || - type == Items::VERMIN || - type == Items::PET || - type == Items::PLANT || - type == Items::CHEESE || - type == Items::FOOD + if(type == df::item_type::MEAT || + type == df::item_type::FISH || + type == df::item_type::VERMIN || + type == df::item_type::PET || + type == df::item_type::PLANT || + type == df::item_type::CHEESE || + type == df::item_type::FOOD ) { confiscate = true; @@ -209,7 +209,7 @@ DFhackCExport command_result df_cleanowned (Core * c, vector & paramete if (!Items->removeItemOwner(item, Creatures)) c->con.print("(unsuccessfully) "); if (dump) - item->flags.dump = 1; + item->flags.bits.dump = 1; } c->con.print("\n"); } diff --git a/plugins/seedwatch.cpp b/plugins/seedwatch.cpp index 2d1a23b51..91da6813c 100755 --- a/plugins/seedwatch.cpp +++ b/plugins/seedwatch.cpp @@ -13,6 +13,7 @@ #include "modules/World.h" #include "modules/kitchen.h" #include "VersionInfo.h" +#include "df/item_flags.h" using DFHack::t_materialType; using DFHack::t_materialIndex; @@ -23,19 +24,19 @@ bool running = false; // whether seedwatch is counting the seeds or not // abbreviations for the standard plants std::map abbreviations; -bool ignoreSeeds(DFHack::t_itemflags& f) // seeds with the following flags should not be counted +bool ignoreSeeds(df::item_flags& f) // seeds with the following flags should not be counted { return - f.dump || - f.forbid || - f.garbage_colect || - f.hidden || - f.hostile || - f.on_fire || - f.rotten || - f.trader || - f.in_building || - f.in_job; + f.bits.dump || + f.bits.forbid || + f.bits.garbage_colect || + f.bits.hidden || + f.bits.hostile || + f.bits.on_fire || + f.bits.rotten || + f.bits.trader || + f.bits.in_building || + f.bits.in_job; }; void printHelp(DFHack::Core& core) // prints help @@ -329,9 +330,9 @@ DFhackCExport DFHack::command_result plugin_onupdate(DFHack::Core* pCore) std::map seedCount; // the number of seeds DFHack::Items& itemsModule = *core.getItems(); itemsModule.Start(); - std::vector items; + std::vector items; itemsModule.readItemVector(items); - DFHack::df_item * item; + df::item * item; // count all seeds and plants by RAW material for(std::size_t i = 0; i < items.size(); ++i) { @@ -339,10 +340,10 @@ DFhackCExport DFHack::command_result plugin_onupdate(DFHack::Core* pCore) t_materialIndex materialIndex = item->getMaterialIndex(); switch(item->getType()) { - case DFHack::Items::SEEDS: + case df::item_type::SEEDS: if(!ignoreSeeds(item->flags)) ++seedCount[materialIndex]; break; - case DFHack::Items::PLANT: + case df::item_type::PLANT: break; } } From c18619520d47ca07d64be22d046917ffcb820dcd Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 20:18:40 -0600 Subject: [PATCH 2/8] df_contaminant -> df::contaminant --- library/include/modules/Items.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 566ba3447..eeea8eb63 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -100,16 +100,6 @@ struct t_itemref : public t_virtual int32_t value; }; -struct df_contaminant -{ - int16_t material; - int32_t mat_index; - int16_t mat_state; // FIXME: enum or document in text - int16_t temperature; - int16_t temperature_fraction; // maybe... - int32_t size; ///< 1-24=spatter, 25-49=smear, 50-* = coating -}; - /** * Type for holding an item read from DF * \ingroup grp_items From 7c5835d318282ee30bfeae0d490ebf73a08b6791 Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 20:40:29 -0600 Subject: [PATCH 3/8] 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; From a9eb555b99e7bd83b48ac9c0e057fe35f6cc3ab8 Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 20:41:09 -0600 Subject: [PATCH 4/8] Get rid of t_itemimprovement (wasn't even used) --- library/include/modules/Items.h | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 1f0a0a5d0..828fb07cf 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -111,17 +111,6 @@ struct dfh_item int16_t wear_level; }; - -/** - * Type for holding item improvements. broken/unused. - * \ingroup grp_items - */ -struct t_improvement -{ - t_material matdesc; - int32_t quality; -}; - /** * The Items module * \ingroup grp_modules From 9d8c67b71038e7d0cacb13f5b026bbdedf21c4ed Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 21:22:42 -0600 Subject: [PATCH 5/8] Kill the Kitchen module (which was never really a proper module to begin with) --- library/include/modules/kitchen.h | 56 +++-- library/modules/kitchen.cpp | 333 +++++++++++++----------------- plugins/seedwatch.cpp | 90 ++++---- 3 files changed, 213 insertions(+), 266 deletions(-) diff --git a/library/include/modules/kitchen.h b/library/include/modules/kitchen.h index b169dfb3f..a18011390 100644 --- a/library/include/modules/kitchen.h +++ b/library/include/modules/kitchen.h @@ -30,9 +30,9 @@ distribution. #include "Module.h" #include "Types.h" #include "VersionInfo.h" -#include "modules/Materials.h" -#include "modules/Items.h" #include "Core.h" +#include "modules/Items.h" + /** * \defgroup grp_kitchen Kitchen settings * @ingroup grp_modules @@ -40,6 +40,8 @@ distribution. namespace DFHack { +namespace Simple +{ namespace Kitchen { typedef uint8_t t_exclusionType; @@ -52,45 +54,33 @@ const t_itemSubtype limitSubtype = 0; // used to store limit as an entry in the const t_exclusionType limitExclusion = 4; // used to store limit as an entry in the exclusion list /** - * Kitchen exclusions manipulator class. Currently geared towards plants and seeds. - * @ingroup grp_kitchen + * Kitchen exclusions manipulator. Currently geared towards plants and seeds. + * \ingroup grp_modules + * \ingroup grp_kitchen */ -class DFHACK_EXPORT Exclusions -{ -public: - /// ctor - Exclusions(DFHack::Core& core_); - /// dtor - ~Exclusions(); - - /// print the exclusion list, with the material index also translated into its token (for organics) - for debug really - void debug_print() const; - /// remove this material from the exclusion list if it is in it - void allowPlantSeedCookery(t_materialIndex materialIndex); +// print the exclusion list, with the material index also translated into its token (for organics) - for debug really +DFHACK_EXPORT void debug_print(Core &); - /// add this material to the exclusion list, if it is not already in it - void denyPlantSeedCookery(t_materialIndex materialIndex); +// remove this material from the exclusion list if it is in it +DFHACK_EXPORT void allowPlantSeedCookery(t_materialIndex materialIndex); - /// fills a map with info from the limit info storage entries in the exclusion list - void fillWatchMap(std::map& watchMap) const; +// add this material to the exclusion list, if it is not already in it +DFHACK_EXPORT void denyPlantSeedCookery(t_materialIndex materialIndex); - /// removes a limit info storage entry from the exclusion list if it's present - void removeLimit(t_materialIndex materialIndex); +// fills a map with info from the limit info storage entries in the exclusion list +DFHACK_EXPORT void fillWatchMap(std::map& watchMap); - /// add a limit info storage item to the exclusion list, or alters an existing one - void setLimit(t_materialIndex materialIndex, unsigned int limit); +// removes a limit info storage entry from the exclusion list if it's present +DFHACK_EXPORT void removeLimit(t_materialIndex materialIndex); - /// clears all limit info storage items from the exclusion list - void clearLimits(); +// add a limit info storage item to the exclusion list, or alters an existing one +DFHACK_EXPORT void setLimit(t_materialIndex materialIndex, unsigned int limit); - /// the size of the exclusions vectors (they are all the same size - if not, there is a problem!) - std::size_t size() const; -private: - class Private; - Private* d; - -}; +// clears all limit info storage items from the exclusion list +DFHACK_EXPORT void clearLimits(); +DFHACK_EXPORT std::size_t size(); +} } } diff --git a/library/modules/kitchen.cpp b/library/modules/kitchen.cpp index cd67a9e23..3a85b3926 100644 --- a/library/modules/kitchen.cpp +++ b/library/modules/kitchen.cpp @@ -9,226 +9,191 @@ #include using namespace std; -#include "Types.h" #include "VersionInfo.h" #include "MemAccess.h" -#include "modules/Materials.h" -#include "modules/Items.h" -#include "modules/Units.h" +#include "Types.h" +#include "Error.h" #include "modules/kitchen.h" #include "ModuleFactory.h" #include "Core.h" -#include "Virtual.h" +using namespace DFHack; +using namespace DFHack::Simple; + +#include "DataDefs.h" +#include "df/world.h" +#include "df/ui.h" #include "df/item_type.h" +#include "df/plant_raw.h" + +using namespace df::enums; +using df::global::world; +using df::global::ui; -namespace DFHack +void Kitchen::debug_print(Core &core) { -namespace Kitchen + core.con.print("Kitchen Exclusions\n"); + for(std::size_t i = 0; i < size(); ++i) + { + core.con.print("%2u: IT:%2i IS:%i MT:%3i MI:%2i ET:%i %s\n", + i, + ui->kitchen.item_types[i], + ui->kitchen.item_subtypes[i], + ui->kitchen.mat_types[i], + ui->kitchen.mat_indices[i], + ui->kitchen.exc_types[i], + (ui->kitchen.mat_types[i] >= 419 && ui->kitchen.mat_types[i] <= 618) ? world->raws.plants.all[ui->kitchen.mat_indices[i]]->id.c_str() : "n/a" + ); + } + core.con.print("\n"); +} + +void Kitchen::allowPlantSeedCookery(t_materialIndex materialIndex) { - class Exclusions::Private + bool match = false; + do { - public: - Private (DFHack::Core& core_) - : core(core_) - , itemTypes (*((std::vector*)(addr(core_,0)))) - , itemSubtypes (*((std::vector*)(addr(core_,1)))) - , materialTypes (*((std::vector*)(addr(core_,2)))) - , materialIndices (*((std::vector*)(addr(core_,3)))) - , exclusionTypes (*((std::vector*)(addr(core_,4)))) + match = false; + std::size_t matchIndex = 0; + for(std::size_t i = 0; i < size(); ++i) { - }; - DFHack::Core& core; - std::vector& itemTypes; // the item types vector of the kitchen exclusion list - std::vector& itemSubtypes; // the item subtype vector of the kitchen exclusion list - std::vector& materialTypes; // the material subindex vector of the kitchen exclusion list - std::vector& materialIndices; // the material index vector of the kitchen exclusion list - std::vector& exclusionTypes; // the exclusion type vector of the kitchen excluions list - - static void * addr(const DFHack::Core& core, int index) + if(ui->kitchen.mat_indices[i] == materialIndex + && (ui->kitchen.item_types[i] == df::item_type::SEEDS || ui->kitchen.item_types[i] == df::item_type::PLANT) + && ui->kitchen.exc_types[i] == cookingExclusion + ) + { + match = true; + matchIndex = i; + } + } + if(match) { - static char * start = core.vinfo->getAddress("kitchen_limits"); - return start + sizeof(std::vector) * index; - }; - }; - - Exclusions::Exclusions(Core & c) - { - d = new Private(c); - }; - - Exclusions::~Exclusions() - { - delete d; - }; + ui->kitchen.item_types.erase(ui->kitchen.item_types.begin() + matchIndex); + ui->kitchen.item_subtypes.erase(ui->kitchen.item_subtypes.begin() + matchIndex); + ui->kitchen.mat_indices.erase(ui->kitchen.mat_indices.begin() + matchIndex); + ui->kitchen.mat_types.erase(ui->kitchen.mat_types.begin() + matchIndex); + ui->kitchen.exc_types.erase(ui->kitchen.exc_types.begin() + matchIndex); + } + } while(match); +}; - void Exclusions::debug_print() const +void Kitchen::denyPlantSeedCookery(t_materialIndex materialIndex) +{ + df::plant_raw *type = world->raws.plants.all[materialIndex]; + bool SeedAlreadyIn = false; + bool PlantAlreadyIn = false; + for(std::size_t i = 0; i < size(); ++i) { - d->core.con.print("Kitchen Exclusions\n"); - Materials& materialsModule= *d->core.getMaterials(); - for(std::size_t i = 0; i < size(); ++i) + if(ui->kitchen.mat_indices[i] == materialIndex + && ui->kitchen.exc_types[i] == cookingExclusion) { - d->core.con.print("%2u: IT:%2i IS:%i MT:%3i MI:%2i ET:%i %s\n", - i, - d->itemTypes[i], - d->itemSubtypes[i], - d->materialTypes[i], - d->materialIndices[i], - d->exclusionTypes[i], - materialsModule.df_organic->at(d->materialIndices[i])->ID.c_str() - ); + if(ui->kitchen.item_types[i] == df::item_type::SEEDS) + SeedAlreadyIn = true; + else if (ui->kitchen.item_types[i] == df::item_type::PLANT) + PlantAlreadyIn = true; } - d->core.con.print("\n"); } + if(!SeedAlreadyIn) + { + ui->kitchen.item_types.push_back(df::item_type::SEEDS); + ui->kitchen.item_subtypes.push_back(organicSubtype); + ui->kitchen.mat_types.push_back(type->material_defs.type_seed); + ui->kitchen.mat_indices.push_back(materialIndex); + ui->kitchen.exc_types.push_back(cookingExclusion); + } + if(!PlantAlreadyIn) + { + ui->kitchen.item_types.push_back(df::item_type::PLANT); + ui->kitchen.item_subtypes.push_back(organicSubtype); + ui->kitchen.mat_types.push_back(type->material_defs.type_basic_mat); + ui->kitchen.mat_indices.push_back(materialIndex); + ui->kitchen.exc_types.push_back(cookingExclusion); + } +}; - void Exclusions::allowPlantSeedCookery(t_materialIndex materialIndex) +void Kitchen::fillWatchMap(std::map& watchMap) +{ + watchMap.clear(); + for(std::size_t i = 0; i < size(); ++i) { - bool match = false; - do + if(ui->kitchen.item_subtypes[i] == limitType && ui->kitchen.item_subtypes[i] == limitSubtype && ui->kitchen.exc_types[i] == limitExclusion) { - match = false; - std::size_t matchIndex = 0; - for(std::size_t i = 0; i < size(); ++i) - { - if(d->materialIndices[i] == materialIndex - && (d->itemTypes[i] == df::item_type::SEEDS || d->itemTypes[i] == df::item_type::PLANT) - && d->exclusionTypes[i] == cookingExclusion - ) - { - match = true; - matchIndex = i; - } - } - if(match) - { - d->itemTypes.erase(d->itemTypes.begin() + matchIndex); - d->itemSubtypes.erase(d->itemSubtypes.begin() + matchIndex); - d->materialIndices.erase(d->materialIndices.begin() + matchIndex); - d->materialTypes.erase(d->materialTypes.begin() + matchIndex); - d->exclusionTypes.erase(d->exclusionTypes.begin() + matchIndex); - } - } while(match); - }; + watchMap[ui->kitchen.mat_indices[i]] = (unsigned int) ui->kitchen.mat_types[i]; + } + } +}; - void Exclusions::denyPlantSeedCookery(t_materialIndex materialIndex) +void Kitchen::removeLimit(t_materialIndex materialIndex) +{ + bool match = false; + do { - Materials *mats = d->core.getMaterials(); - df_plant_type *type = mats->df_organic->at(materialIndex); - bool SeedAlreadyIn = false; - bool PlantAlreadyIn = false; + match = false; + std::size_t matchIndex = 0; for(std::size_t i = 0; i < size(); ++i) { - if(d->materialIndices[i] == materialIndex - && d->exclusionTypes[i] == cookingExclusion) + if(ui->kitchen.item_types[i] == limitType + && ui->kitchen.item_subtypes[i] == limitSubtype + && ui->kitchen.mat_indices[i] == materialIndex + && ui->kitchen.exc_types[i] == limitExclusion) { - if(d->itemTypes[i] == df::item_type::SEEDS) - SeedAlreadyIn = true; - else if (d->itemTypes[i] == df::item_type::PLANT) - PlantAlreadyIn = true; + match = true; + matchIndex = i; } } - if(!SeedAlreadyIn) + if(match) { - d->itemTypes.push_back(df::item_type::SEEDS); - d->itemSubtypes.push_back(organicSubtype); - d->materialTypes.push_back(type->material_type_seed); - d->materialIndices.push_back(materialIndex); - d->exclusionTypes.push_back(cookingExclusion); + ui->kitchen.item_types.erase(ui->kitchen.item_types.begin() + matchIndex); + ui->kitchen.item_subtypes.erase(ui->kitchen.item_subtypes.begin() + matchIndex); + ui->kitchen.mat_types.erase(ui->kitchen.mat_types.begin() + matchIndex); + ui->kitchen.mat_indices.erase(ui->kitchen.mat_indices.begin() + matchIndex); + ui->kitchen.exc_types.erase(ui->kitchen.exc_types.begin() + matchIndex); } - if(!PlantAlreadyIn) - { - d->itemTypes.push_back(df::item_type::PLANT); - d->itemSubtypes.push_back(organicSubtype); - d->materialTypes.push_back(type->material_type_basic_mat); - d->materialIndices.push_back(materialIndex); - d->exclusionTypes.push_back(cookingExclusion); - } - }; + } while(match); +}; - void Exclusions::fillWatchMap(std::map& watchMap) const +void Kitchen::setLimit(t_materialIndex materialIndex, unsigned int limit) +{ + removeLimit(materialIndex); + if(limit > seedLimit) { - watchMap.clear(); - for(std::size_t i = 0; i < size(); ++i) - { - if(d->itemTypes[i] == limitType && d->itemSubtypes[i] == limitSubtype && d->exclusionTypes[i] == limitExclusion) - { - watchMap[d->materialIndices[i]] = (unsigned int) d->materialTypes[i]; - } - } - }; + limit = seedLimit; + } + ui->kitchen.item_types.push_back(limitType); + ui->kitchen.item_subtypes.push_back(limitSubtype); + ui->kitchen.mat_indices.push_back(materialIndex); + ui->kitchen.mat_types.push_back((t_materialType) (limit < seedLimit) ? limit : seedLimit); + ui->kitchen.exc_types.push_back(limitExclusion); +}; - void Exclusions::removeLimit(t_materialIndex materialIndex) +void Kitchen::clearLimits() +{ + bool match = false; + do { - bool match = false; - do + match = false; + std::size_t matchIndex; + for(std::size_t i = 0; i < size(); ++i) { - match = false; - std::size_t matchIndex = 0; - for(std::size_t i = 0; i < size(); ++i) + if(ui->kitchen.item_types[i] == limitType + && ui->kitchen.item_subtypes[i] == limitSubtype + && ui->kitchen.exc_types[i] == limitExclusion) { - if(d->itemTypes[i] == limitType - && d->itemSubtypes[i] == limitSubtype - && d->materialIndices[i] == materialIndex - && d->exclusionTypes[i] == limitExclusion) - { - match = true; - matchIndex = i; - } + match = true; + matchIndex = i; } - if(match) - { - d->itemTypes.erase(d->itemTypes.begin() + matchIndex); - d->itemSubtypes.erase(d->itemSubtypes.begin() + matchIndex); - d->materialTypes.erase(d->materialTypes.begin() + matchIndex); - d->materialIndices.erase(d->materialIndices.begin() + matchIndex); - d->exclusionTypes.erase(d->exclusionTypes.begin() + matchIndex); - } - } while(match); - }; - - void Exclusions::setLimit(t_materialIndex materialIndex, unsigned int limit) - { - removeLimit(materialIndex); - if(limit > seedLimit) + } + if(match) { - limit = seedLimit; + ui->kitchen.item_types.erase(ui->kitchen.item_types.begin() + matchIndex); + ui->kitchen.item_subtypes.erase(ui->kitchen.item_subtypes.begin() + matchIndex); + ui->kitchen.mat_indices.erase(ui->kitchen.mat_indices.begin() + matchIndex); + ui->kitchen.mat_types.erase(ui->kitchen.mat_types.begin() + matchIndex); + ui->kitchen.exc_types.erase(ui->kitchen.exc_types.begin() + matchIndex); } - d->itemTypes.push_back(limitType); - d->itemSubtypes.push_back(limitSubtype); - d->materialIndices.push_back(materialIndex); - d->materialTypes.push_back((t_materialType) (limit < seedLimit) ? limit : seedLimit); - d->exclusionTypes.push_back(limitExclusion); - }; + } while(match); +}; - void Exclusions::clearLimits() - { - bool match = false; - do - { - match = false; - std::size_t matchIndex; - for(std::size_t i = 0; i < size(); ++i) - { - if(d->itemTypes[i] == limitType - && d->itemSubtypes[i] == limitSubtype - && d->exclusionTypes[i] == limitExclusion) - { - match = true; - matchIndex = i; - } - } - if(match) - { - d->itemTypes.erase(d->itemTypes.begin() + matchIndex); - d->itemSubtypes.erase(d->itemSubtypes.begin() + matchIndex); - d->materialIndices.erase(d->materialIndices.begin() + matchIndex); - d->materialTypes.erase(d->materialTypes.begin() + matchIndex); - d->exclusionTypes.erase(d->exclusionTypes.begin() + matchIndex); - } - } while(match); - }; - size_t Exclusions::size() const - { - return d->itemTypes.size(); - }; -} -}; \ No newline at end of file +size_t Kitchen::size() +{ + return ui->kitchen.item_types.size(); +}; diff --git a/plugins/seedwatch.cpp b/plugins/seedwatch.cpp index 91da6813c..20d59be36 100755 --- a/plugins/seedwatch.cpp +++ b/plugins/seedwatch.cpp @@ -8,15 +8,14 @@ #include "Core.h" #include "Export.h" #include "PluginManager.h" -#include "modules/Materials.h" #include "modules/Items.h" #include "modules/World.h" #include "modules/kitchen.h" #include "VersionInfo.h" #include "df/item_flags.h" -using DFHack::t_materialType; -using DFHack::t_materialIndex; +using namespace DFHack; +using namespace DFHack::Simple; const int buffer = 20; // seed number buffer - 20 is reasonable bool running = false; // whether seedwatch is counting the seeds or not @@ -39,7 +38,7 @@ bool ignoreSeeds(df::item_flags& f) // seeds with the following flags should not f.bits.in_job; }; -void printHelp(DFHack::Core& core) // prints help +void printHelp(Core& core) // prints help { core.con.print( "Watches the numbers of seeds available and enables/disables seed and plant cooking.\n" @@ -93,17 +92,17 @@ std::string searchAbbreviations(std::string in) } }; -DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vector& parameters) +DFhackCExport command_result df_seedwatch(Core* pCore, std::vector& parameters) { - DFHack::Core& core = *pCore; + Core& core = *pCore; if(!core.isValid()) { - return DFHack::CR_FAILURE; + return CR_FAILURE; } core.Suspend(); - DFHack::Materials& materialsModule = *core.getMaterials(); - std::vector organics; + Materials& materialsModule = *core.getMaterials(); + std::vector organics; materialsModule.CopyOrganicMaterials(organics); std::map materialsReverser; @@ -112,17 +111,17 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect materialsReverser[organics[i].id] = i; } - DFHack::World *w = core.getWorld(); - DFHack::t_gamemodes gm; + World *w = core.getWorld(); + t_gamemodes gm; w->ReadGameMode(gm);// FIXME: check return value // if game mode isn't fortress mode - if(gm.g_mode != DFHack::GAMEMODE_DWARF || gm.g_type != DFHack::GAMETYPE_DWARF_MAIN) + if(gm.g_mode != GAMEMODE_DWARF || gm.g_type != GAMETYPE_DWARF_MAIN) { // just print the help printHelp(core); core.Resume(); - return DFHack::CR_OK; + return CR_OK; } std::string par; @@ -148,8 +147,7 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect } else if(par == "clear") { - DFHack::Kitchen::Exclusions kitchenExclusions(core); - kitchenExclusions.clearLimits(); + Kitchen::clearLimits(); core.con.print("seedwatch watchlist cleared\n"); } else if(par == "info") @@ -163,9 +161,8 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect { core.con.print("seedwatch is not supervising. Use 'seedwatch start' to start supervision.\n"); } - DFHack::Kitchen::Exclusions kitchenExclusions(core); std::map watchMap; - kitchenExclusions.fillWatchMap(watchMap); + Kitchen::fillWatchMap(watchMap); if(watchMap.empty()) { core.con.print("The watch list is empty.\n"); @@ -181,10 +178,9 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect } else if(par == "debug") { - DFHack::Kitchen::Exclusions kitchenExclusions(core); std::map watchMap; - kitchenExclusions.fillWatchMap(watchMap); - kitchenExclusions.debug_print(); + Kitchen::fillWatchMap(watchMap); + Kitchen::debug_print(core); } /* else if(par == "dumpmaps") @@ -209,8 +205,7 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect std::string token = searchAbbreviations(par); if(materialsReverser.count(token) > 0) { - DFHack::Kitchen::Exclusions kitchenExclusions(core); - kitchenExclusions.removeLimit(materialsReverser[token]); + Kitchen::removeLimit(materialsReverser[token]); core.con.print("%s is not being watched\n", token.c_str()); } else @@ -226,8 +221,7 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect { for(std::map::const_iterator i = abbreviations.begin(); i != abbreviations.end(); ++i) { - DFHack::Kitchen::Exclusions kitchenExclusions(core); - if(materialsReverser.count(i->second) > 0) kitchenExclusions.setLimit(materialsReverser[i->second], limit); + if(materialsReverser.count(i->second) > 0) Kitchen::setLimit(materialsReverser[i->second], limit); } } else @@ -235,8 +229,7 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect std::string token = searchAbbreviations(parameters[0]); if(materialsReverser.count(token) > 0) { - DFHack::Kitchen::Exclusions kitchenExclusions(core); - kitchenExclusions.setLimit(materialsReverser[token], limit); + Kitchen::setLimit(materialsReverser[token], limit); core.con.print("%s is being watched.\n", token.c_str()); } else @@ -251,7 +244,7 @@ DFhackCExport DFHack::command_result df_seedwatch(DFHack::Core* pCore, std::vect } core.Resume(); - return DFHack::CR_OK; + return CR_OK; } DFhackCExport const char* plugin_name(void) @@ -259,10 +252,10 @@ DFhackCExport const char* plugin_name(void) return "seedwatch"; } -DFhackCExport DFHack::command_result plugin_init(DFHack::Core* pCore, std::vector& commands) +DFhackCExport command_result plugin_init(Core* pCore, std::vector& commands) { commands.clear(); - commands.push_back(DFHack::PluginCommand("seedwatch", "Switches cookery based on quantity of seeds, to keep reserves", df_seedwatch)); + commands.push_back(PluginCommand("seedwatch", "Switches cookery based on quantity of seeds, to keep reserves", df_seedwatch)); // fill in the abbreviations map, with abbreviations for the standard plants abbreviations["bs"] = "SLIVER_BARB"; abbreviations["bt"] = "TUBER_BLOATED"; @@ -285,14 +278,14 @@ DFhackCExport DFHack::command_result plugin_init(DFHack::Core* pCore, std::vecto abbreviations["vh"] = "HERB_VALLEY"; abbreviations["ws"] = "BERRIES_STRAW_WILD"; abbreviations["wv"] = "VINE_WHIP"; - return DFHack::CR_OK; + return CR_OK; } -DFhackCExport DFHack::command_result plugin_onstatechange(DFHack::Core* pCore, DFHack::state_change_event event) +DFhackCExport command_result plugin_onstatechange(Core* pCore, state_change_event event) { switch (event) { - case DFHack::SC_GAME_LOADED: - case DFHack::SC_GAME_UNLOADED: + case SC_GAME_LOADED: + case SC_GAME_UNLOADED: if (running) pCore->con.printerr("seedwatch deactivated due to game load/unload\n"); running = false; @@ -301,34 +294,34 @@ DFhackCExport DFHack::command_result plugin_onstatechange(DFHack::Core* pCore, D break; } - return DFHack::CR_OK; + return CR_OK; } -DFhackCExport DFHack::command_result plugin_onupdate(DFHack::Core* pCore) +DFhackCExport command_result plugin_onupdate(Core* pCore) { if (running) { // reduce processing rate static int counter = 0; if (++counter < 500) - return DFHack::CR_OK; + return CR_OK; counter = 0; - DFHack::Core& core = *pCore; - DFHack::World *w = core.getWorld(); - DFHack::t_gamemodes gm; + Core& core = *pCore; + World *w = core.getWorld(); + t_gamemodes gm; w->ReadGameMode(gm);// FIXME: check return value // if game mode isn't fortress mode - if(gm.g_mode != DFHack::GAMEMODE_DWARF || gm.g_type != DFHack::GAMETYPE_DWARF_MAIN) + if(gm.g_mode != GAMEMODE_DWARF || gm.g_type != GAMETYPE_DWARF_MAIN) { // stop running. running = false; core.con.printerr("seedwatch deactivated due to game mode switch\n"); - return DFHack::CR_OK; + return CR_OK; } // this is dwarf mode, continue std::map seedCount; // the number of seeds - DFHack::Items& itemsModule = *core.getItems(); + Items& itemsModule = *core.getItems(); itemsModule.Start(); std::vector items; itemsModule.readItemVector(items); @@ -349,25 +342,24 @@ DFhackCExport DFHack::command_result plugin_onupdate(DFHack::Core* pCore) } itemsModule.Finish(); - DFHack::Kitchen::Exclusions kitchenExclusions(core); std::map watchMap; - kitchenExclusions.fillWatchMap(watchMap); + Kitchen::fillWatchMap(watchMap); for(auto i = watchMap.begin(); i != watchMap.end(); ++i) { if(seedCount[i->first] <= i->second) { - kitchenExclusions.denyPlantSeedCookery(i->first); + Kitchen::denyPlantSeedCookery(i->first); } else if(i->second + buffer < seedCount[i->first]) { - kitchenExclusions.allowPlantSeedCookery(i->first); + Kitchen::allowPlantSeedCookery(i->first); } } } - return DFHack::CR_OK; + return CR_OK; } -DFhackCExport DFHack::command_result plugin_shutdown(DFHack::Core* pCore) +DFhackCExport command_result plugin_shutdown(Core* pCore) { - return DFHack::CR_OK; + return CR_OK; } From e75291ee91adb0c76619a7d61814b85ea22402a1 Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 21:29:09 -0600 Subject: [PATCH 6/8] Unlink seedwatch plugin from Materials module --- plugins/seedwatch.cpp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/plugins/seedwatch.cpp b/plugins/seedwatch.cpp index 20d59be36..43de41490 100755 --- a/plugins/seedwatch.cpp +++ b/plugins/seedwatch.cpp @@ -8,15 +8,18 @@ #include "Core.h" #include "Export.h" #include "PluginManager.h" -#include "modules/Items.h" #include "modules/World.h" #include "modules/kitchen.h" #include "VersionInfo.h" +#include "df/world.h" +#include "df/plant_raw.h" #include "df/item_flags.h" using namespace DFHack; using namespace DFHack::Simple; +using df::global::world; + const int buffer = 20; // seed number buffer - 20 is reasonable bool running = false; // whether seedwatch is counting the seeds or not @@ -101,14 +104,10 @@ DFhackCExport command_result df_seedwatch(Core* pCore, std::vector& } core.Suspend(); - Materials& materialsModule = *core.getMaterials(); - std::vector organics; - materialsModule.CopyOrganicMaterials(organics); - std::map materialsReverser; - for(std::size_t i = 0; i < organics.size(); ++i) + for(std::size_t i = 0; i < world->raws.plants.all.size(); ++i) { - materialsReverser[organics[i].id] = i; + materialsReverser[world->raws.plants.all[i]->id] = i; } World *w = core.getWorld(); @@ -172,7 +171,7 @@ DFhackCExport command_result df_seedwatch(Core* pCore, std::vector& core.con.print("The watch list is:\n"); for(std::map::const_iterator i = watchMap.begin(); i != watchMap.end(); ++i) { - core.con.print("%s : %u\n", organics[i->first].id.c_str(), i->second); + core.con.print("%s : %u\n", world->raws.plants.all[i->first]->id.c_str(), i->second); } } } @@ -321,15 +320,11 @@ DFhackCExport command_result plugin_onupdate(Core* pCore) } // this is dwarf mode, continue std::map seedCount; // the number of seeds - Items& itemsModule = *core.getItems(); - itemsModule.Start(); - std::vector items; - itemsModule.readItemVector(items); - df::item * item; + // count all seeds and plants by RAW material - for(std::size_t i = 0; i < items.size(); ++i) + for(std::size_t i = 0; i < world->items.all.size(); ++i) { - item = items[i]; + df::item * item = world->items.all[i]; t_materialIndex materialIndex = item->getMaterialIndex(); switch(item->getType()) { @@ -340,7 +335,6 @@ DFhackCExport command_result plugin_onupdate(Core* pCore) break; } } - itemsModule.Finish(); std::map watchMap; Kitchen::fillWatchMap(watchMap); From fd653a0227f3bca86d8fd4b9e7f515b6c4015524 Mon Sep 17 00:00:00 2001 From: Quietust Date: Mon, 16 Jan 2012 22:12:58 -0600 Subject: [PATCH 7/8] Kill the Items module --- library/Core.cpp | 1 - library/include/Core.h | 4 - library/include/ModuleFactory.h | 1 - library/include/modules/Items.h | 68 ++++++-------- library/include/modules/Units.h | 2 - library/modules/Items.cpp | 156 +------------------------------- plugins/autodump.cpp | 12 +-- plugins/cleanowned.cpp | 28 +++--- 8 files changed, 44 insertions(+), 228 deletions(-) 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; From 35ad84c58a0a35732d14cc6e31e457e28546e721 Mon Sep 17 00:00:00 2001 From: Quietust Date: Wed, 18 Jan 2012 08:26:08 -0600 Subject: [PATCH 8/8] angavrilov changed general_ref_type to be all uppercase --- library/modules/Items.cpp | 10 +++++----- plugins/autodump.cpp | 2 +- plugins/workflow.cpp | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index d54a56f13..3b731c0d0 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -1,4 +1,4 @@ -/* +/* https://github.com/peterix/dfhack Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com) @@ -430,7 +430,7 @@ int32_t Items::getItemOwnerID(const df::item * item) 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) + if (ref->getType() == df::general_ref_type::UNIT_ITEMOWNER) return ref->getID(); } return -1; @@ -441,7 +441,7 @@ int32_t Items::getItemContainerID(const df::item * item) 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) + if (ref->getType() == df::general_ref_type::CONTAINED_IN_ITEM) return ref->getID(); } return -1; @@ -449,7 +449,7 @@ int32_t Items::getItemContainerID(const df::item * item) bool Items::getContainedItems(const df::item * item, std::vector &items) { - return readItemRefs(item, df::general_ref_type::contains_item, items); + return readItemRefs(item, df::general_ref_type::CONTAINS_ITEM, items); } bool Items::readItemRefs(const df::item * item, df::general_ref_type type, std::vector &values) @@ -471,7 +471,7 @@ bool Items::removeItemOwner(df::item * item, Units *creatures) 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) + if (ref->getType() != df::general_ref_type::UNIT_ITEMOWNER) continue; df_unit *unit = (df_unit *)ref->getUnit(); diff --git a/plugins/autodump.cpp b/plugins/autodump.cpp index 8c7f9a16b..d36854bbf 100644 --- a/plugins/autodump.cpp +++ b/plugins/autodump.cpp @@ -330,7 +330,7 @@ DFhackCExport command_result df_autodump_destroy_item(Core * c, vector for (unsigned i = 0; i < item->itemrefs.size(); i++) { df::general_ref *ref = item->itemrefs[i]; - if (ref->getType() == df::general_ref_type::unit_holder) + if (ref->getType() == df::general_ref_type::UNIT_HOLDER) { c->con.printerr("Choosing not to destroy items in unit inventory.\n"); return CR_FAILURE; diff --git a/plugins/workflow.cpp b/plugins/workflow.cpp index 4a3564d65..7556ca944 100644 --- a/plugins/workflow.cpp +++ b/plugins/workflow.cpp @@ -977,7 +977,7 @@ static void dryBucket(df::item *item) for (unsigned i = 0; i < item->itemrefs.size(); i++) { df::general_ref *ref = item->itemrefs[i]; - if (ref->getType() == df::general_ref_type::contains_item) + if (ref->getType() == df::general_ref_type::CONTAINS_ITEM) { df::item *obj = ref->getItem(); @@ -999,20 +999,20 @@ static bool itemBusy(df::item *item) for (unsigned i = 0; i < item->itemrefs.size(); i++) { df::general_ref *ref = item->itemrefs[i]; - if (ref->getType() == df::general_ref_type::contains_item) + if (ref->getType() == df::general_ref_type::CONTAINS_ITEM) { df::item *obj = ref->getItem(); if (obj && !obj->flags.bits.garbage_colect) return true; } - else if (ref->getType() == df::general_ref_type::contains_unit) + else if (ref->getType() == df::general_ref_type::CONTAINS_UNIT) return true; - else if (ref->getType() == df::general_ref_type::unit_holder) + else if (ref->getType() == df::general_ref_type::UNIT_HOLDER) { if (!item->flags.bits.in_job) return true; } - else if (ref->getType() == df::general_ref_type::contained_in_item) + else if (ref->getType() == df::general_ref_type::CONTAINED_IN_ITEM) { df::item *obj = ref->getItem(); if (!obj)