From a29bb4578ff8355b52b0a496d7995cad354aa55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 30 Oct 2011 03:20:54 +0100 Subject: [PATCH] Move kitchen exclusion module methods to a source file. --- library/CMakeLists.txt | 2 + library/include/dfhack/modules/kitchen.h | 197 ++----------------- library/modules/kitchen.cpp | 234 +++++++++++++++++++++++ 3 files changed, 252 insertions(+), 181 deletions(-) create mode 100644 library/modules/kitchen.cpp diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 98d2dd027..70f216eb2 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -47,6 +47,7 @@ include/dfhack/modules/Creatures.h include/dfhack/modules/Engravings.h include/dfhack/modules/Gui.h include/dfhack/modules/Items.h +include/dfhack/modules/kitchen.h include/dfhack/modules/Maps.h include/dfhack/modules/Materials.h include/dfhack/modules/Notes.h @@ -80,6 +81,7 @@ modules/Creatures.cpp modules/Engravings.cpp modules/Gui.cpp modules/Items.cpp +modules/kitchen.cpp modules/Maps.cpp modules/Materials.cpp modules/Notes.cpp diff --git a/library/include/dfhack/modules/kitchen.h b/library/include/dfhack/modules/kitchen.h index c442ccf58..f33daf5c5 100644 --- a/library/include/dfhack/modules/kitchen.h +++ b/library/include/dfhack/modules/kitchen.h @@ -59,202 +59,37 @@ class Exclusions { public: /// ctor - Exclusions(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)))) - { - }; + 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() - { - core.con.print("Kitchen Exclusions\n"); - DFHack::Materials& materialsModule= *core.getMaterials(); - 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, - itemTypes[i], - itemSubtypes[i], - materialTypes[i], - materialIndices[i], - exclusionTypes[i], - materialsModule.df_organic->at(materialIndices[i])->ID.c_str() - ); - } - core.con.print("\n"); - }; + void debug_print() const; /// remove this material from the exclusion list if it is in it - void allowPlantSeedCookery(t_materialIndex materialIndex) - { - bool match = false; - do - { - match = false; - std::size_t matchIndex = 0; - for(std::size_t i = 0; i < size(); ++i) - { - if(materialIndices[i] == materialIndex - && (itemTypes[i] == DFHack::Items::SEEDS || itemTypes[i] == DFHack::Items::PLANT) - && exclusionTypes[i] == cookingExclusion - ) - { - match = true; - matchIndex = i; - } - } - if(match) - { - itemTypes.erase(itemTypes.begin() + matchIndex); - itemSubtypes.erase(itemSubtypes.begin() + matchIndex); - materialIndices.erase(materialIndices.begin() + matchIndex); - materialTypes.erase( materialTypes.begin() + matchIndex); - exclusionTypes.erase(exclusionTypes.begin() + matchIndex); - } - } while(match); - }; + void allowPlantSeedCookery(t_materialIndex materialIndex); /// add this material to the exclusion list, if it is not already in it - void denyPlantSeedCookery(t_materialIndex materialIndex) - { - Materials *mats = core.getMaterials(); - df_plant_type *type = mats->df_organic->at(materialIndex); - bool SeedAlreadyIn = false; - bool PlantAlreadyIn = false; - for(std::size_t i = 0; i < size(); ++i) - { - if(materialIndices[i] == materialIndex - && exclusionTypes[i] == cookingExclusion) - { - if(itemTypes[i] == DFHack::Items::SEEDS) - SeedAlreadyIn = true; - else if (itemTypes[i] == DFHack::Items::PLANT) - PlantAlreadyIn = true; - } - } - if(!SeedAlreadyIn) - { - itemTypes.push_back(DFHack::Items::SEEDS); - itemSubtypes.push_back(organicSubtype); - materialTypes.push_back(type->material_type_seed); - materialIndices.push_back(materialIndex); - exclusionTypes.push_back(cookingExclusion); - } - if(!PlantAlreadyIn) - { - itemTypes.push_back(DFHack::Items::PLANT); - itemSubtypes.push_back(organicSubtype); - materialTypes.push_back(type->material_type_basic_mat); - materialIndices.push_back(materialIndex); - exclusionTypes.push_back(cookingExclusion); - } - }; + void denyPlantSeedCookery(t_materialIndex materialIndex); /// fills a map with info from the limit info storage entries in the exclusion list - void fillWatchMap(std::map& watchMap) - { - watchMap.clear(); - for(std::size_t i = 0; i < size(); ++i) - { - if(itemTypes[i] == limitType && itemSubtypes[i] == limitSubtype && exclusionTypes[i] == limitExclusion) - { - watchMap[materialIndices[i]] = (unsigned int) materialTypes[i]; - } - } - }; + void fillWatchMap(std::map& watchMap) const; /// removes a limit info storage entry from the exclusion list if it's present - void removeLimit(t_materialIndex materialIndex) - { - bool match = false; - do - { - match = false; - std::size_t matchIndex = 0; - for(std::size_t i = 0; i < size(); ++i) - { - if(itemTypes[i] == limitType - && itemSubtypes[i] == limitSubtype - && materialIndices[i] == materialIndex - && exclusionTypes[i] == limitExclusion) - { - match = true; - matchIndex = i; - } - } - if(match) - { - itemTypes.erase(itemTypes.begin() + matchIndex); - itemSubtypes.erase(itemSubtypes.begin() + matchIndex); - materialTypes.erase( materialTypes.begin() + matchIndex); - materialIndices.erase(materialIndices.begin() + matchIndex); - exclusionTypes.erase(exclusionTypes.begin() + matchIndex); - } - } while(match); - }; + void removeLimit(t_materialIndex materialIndex); /// add a limit info storage item to the exclusion list, or alters an existing one - void setLimit(t_materialIndex materialIndex, unsigned int limit) - { - removeLimit(materialIndex); - if(limit > seedLimit) limit = seedLimit; - - itemTypes.push_back(limitType); - itemSubtypes.push_back(limitSubtype); - materialIndices.push_back(materialIndex); - materialTypes.push_back((t_materialType) (limit < seedLimit) ? limit : seedLimit); - exclusionTypes.push_back(limitExclusion); - }; + void setLimit(t_materialIndex materialIndex, unsigned int limit); /// clears all limit info storage items from the exclusion list - void clearLimits() - { - bool match = false; - do - { - match = false; - std::size_t matchIndex; - for(std::size_t i = 0; i < size(); ++i) - { - if(itemTypes[i] == limitType - && itemSubtypes[i] == limitSubtype - && exclusionTypes[i] == limitExclusion) - { - match = true; - matchIndex = i; - } - } - if(match) - { - itemTypes.erase(itemTypes.begin() + matchIndex); - itemSubtypes.erase(itemSubtypes.begin() + matchIndex); - materialIndices.erase(materialIndices.begin() + matchIndex); - materialTypes.erase( materialTypes.begin() + matchIndex); - exclusionTypes.erase(exclusionTypes.begin() + matchIndex); - } - } while(match); - }; + void clearLimits(); + + /// the size of the exclusions vectors (they are all the same size - if not, there is a problem!) + std::size_t size() const; private: - 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 - std::size_t size() // the size of the exclusions vectors (they are all the same size - if not, there is a problem!) - { - return itemTypes.size(); - }; - static uint32_t addr(const DFHack::Core& core, int index) - { - static uint32_t start = core.vinfo->getAddress("kitchen_limits"); - return start + sizeof(std::vector) * index; - }; + class Private; + Private* d; + }; } diff --git a/library/modules/kitchen.cpp b/library/modules/kitchen.cpp new file mode 100644 index 000000000..001e9dd40 --- /dev/null +++ b/library/modules/kitchen.cpp @@ -0,0 +1,234 @@ + +#include "Internal.h" + +#include +#include +#include +#include +#include +#include +using namespace std; + +#include "dfhack/Types.h" +#include "dfhack/VersionInfo.h" +#include "dfhack/Process.h" +#include "dfhack/Vector.h" +#include "dfhack/modules/Materials.h" +#include "dfhack/modules/Items.h" +#include "dfhack/modules/Creatures.h" +#include "dfhack/modules/kitchen.h" +#include "ModuleFactory.h" +#include +#include + +namespace DFHack +{ +namespace Kitchen +{ + class Exclusions::Private + { + 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)))) + { + }; + 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 uint32_t addr(const DFHack::Core& core, int index) + { + static uint32_t start = core.vinfo->getAddress("kitchen_limits"); + return start + sizeof(std::vector) * index; + }; + }; + + Exclusions::Exclusions(Core & c) + { + d = new Private(c); + }; + + Exclusions::~Exclusions() + { + delete d; + }; + + void Exclusions::debug_print() const + { + d->core.con.print("Kitchen Exclusions\n"); + Materials& materialsModule= *d->core.getMaterials(); + for(std::size_t i = 0; i < size(); ++i) + { + 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() + ); + } + d->core.con.print("\n"); + } + + void Exclusions::allowPlantSeedCookery(t_materialIndex materialIndex) + { + bool match = false; + do + { + match = false; + std::size_t matchIndex = 0; + 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->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); + }; + + void Exclusions::denyPlantSeedCookery(t_materialIndex materialIndex) + { + Materials *mats = d->core.getMaterials(); + df_plant_type *type = mats->df_organic->at(materialIndex); + bool SeedAlreadyIn = false; + bool PlantAlreadyIn = false; + for(std::size_t i = 0; i < size(); ++i) + { + if(d->materialIndices[i] == materialIndex + && d->exclusionTypes[i] == cookingExclusion) + { + if(d->itemTypes[i] == DFHack::Items::SEEDS) + SeedAlreadyIn = true; + else if (d->itemTypes[i] == DFHack::Items::PLANT) + PlantAlreadyIn = true; + } + } + if(!SeedAlreadyIn) + { + d->itemTypes.push_back(DFHack::Items::SEEDS); + d->itemSubtypes.push_back(organicSubtype); + d->materialTypes.push_back(type->material_type_seed); + d->materialIndices.push_back(materialIndex); + d->exclusionTypes.push_back(cookingExclusion); + } + if(!PlantAlreadyIn) + { + d->itemTypes.push_back(DFHack::Items::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); + } + }; + + void Exclusions::fillWatchMap(std::map& watchMap) const + { + 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]; + } + } + }; + + void Exclusions::removeLimit(t_materialIndex materialIndex) + { + bool match = false; + do + { + match = false; + std::size_t matchIndex = 0; + for(std::size_t i = 0; i < size(); ++i) + { + if(d->itemTypes[i] == limitType + && d->itemSubtypes[i] == limitSubtype + && d->materialIndices[i] == materialIndex + && 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->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) + { + limit = seedLimit; + } + 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); + }; + + 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