From 25f6c440d806bf53c8b209ac3f871d8deab40964 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Wed, 19 Nov 2014 18:38:03 +0100 Subject: [PATCH] stockpiles: serialize through cloth --- plugins/stockpiles.cpp | 149 +++++++++++++++++++++++++++++++---------- 1 file changed, 113 insertions(+), 36 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 9f2f313b9..7ff54f23a 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -182,7 +182,7 @@ typedef std::map FoodMatMap; /** * Helper class for performing organic_index/organic_types <---> material array index lookups */ -class FoodLookup +class OrganicMatLookup { public: struct FoodMat { @@ -226,7 +226,7 @@ public: df::special_mat_table table = raws.mat_table; using df::enums::organic_mat_category::organic_mat_category; df::enum_traits traits; - for ( int32_t mat_category = traits.first_item_value; mat_category second << ")" << endl; + food_idx = it->second; } else { - out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx not found :("; + out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx not found :(" << endl; } } return food_idx; } - static MaterialInfo food_mat_by_token ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) { + static MaterialInfo food_mat_by_token ( color_ostream &out, const std::string & token ) { MaterialInfo mat_info; mat_info.find ( token ); return mat_info; @@ -282,11 +283,11 @@ public: static bool index_built; static std::vector food_index; private: - FoodLookup() {} + OrganicMatLookup() {} }; -bool FoodLookup::index_built = false; -std::vector FoodLookup::food_index = std::vector ( 37 ); +bool OrganicMatLookup::index_built = false; +std::vector OrganicMatLookup::food_index = std::vector ( 37 ); class StockpileSerializer { @@ -418,6 +419,30 @@ private: food_pair() {} }; + void serialize_list_organic_mat ( FuncWriteExport add_value, const vector & list, organic_mat_category::organic_mat_category cat ) { + for ( size_t i = 0; i < list.size(); ++i ) { + if ( list.at ( i ) ) { + std::string token = OrganicMatLookup::food_token_by_idx ( *mOut, cat, i ); + if ( !token.empty() ) { + add_value ( token ); + } else { + *mOut << "food mat invalid :(" << endl; + } + } + } + } + + void unserialize_list_organic_mat ( FuncReadImport get_value, size_t list_size, organic_mat_category::organic_mat_category cat ) { + if ( list_size > 0 ) { + for ( size_t i = 0; i < list_size; ++i ) { + std::string token = get_value ( i ); + int idx = OrganicMatLookup::food_idx_by_token ( *mOut, cat, token ); + *mOut << " organic_material " << idx << " is " << token << endl; + //mPile->settings.food.meat.at(idx) = (char) 1; + } + } + } + void serialize_list_item_type ( FuncItemAllowed is_allowed, FuncWriteExport add_value, std::vector list ) { using df::enums::item_type::item_type; df::enum_traits type_traits; @@ -733,18 +758,7 @@ private: } return food_pair(); } - void food_write_helper ( std::function add_value, const vector & list, organic_mat_category::organic_mat_category cat ) { - for ( size_t i = 0; i < list.size(); ++i ) { - if ( list.at ( i ) ) { - std::string token = FoodLookup::food_token_by_idx ( *mOut, cat, i ); - if ( !token.empty() ) { - add_value ( token ); - } else { - *mOut << "food mat invalid :(" << endl; - } - } - } - } + void write_food() { StockpileSettings::FoodSet *food = mBuffer.mutable_food(); @@ -754,20 +768,11 @@ private: df::enum_traits traits; for ( int32_t mat_category = traits.first_item_value; mat_category 0 ) { - for ( size_t i = 0; i < list_size; ++i ) { - std::string token = get_value ( i ); - int idx = FoodLookup::food_idx_by_token ( *mOut, cat, token ); - //mPile->settings.food.meat.at(idx) = (char) 1; - } - } - } void read_food() { if ( mBuffer.has_food() ) { const StockpileSettings::FoodSet food = mBuffer.food(); @@ -777,7 +782,7 @@ private: df::enum_traits traits; for ( int32_t mat_category = traits.first_item_value; mat_category add_mats( id ); }; - - food_write_helper ( setter, mPile->settings.leather.mats, organic_mat_category::Leather ); + serialize_list_organic_mat ( setter, mPile->settings.leather.mats, organic_mat_category::Leather ); } void read_leather() { if ( mBuffer.has_leather() ) { @@ -1524,12 +1529,84 @@ private: FuncReadImport getter = [=] ( size_t idx ) -> std::string { return leather.mats( idx ); }; - food_read_helper ( getter, leather.mats_size(), organic_mat_category::Leather ); + unserialize_list_organic_mat ( getter, leather.mats_size(), organic_mat_category::Leather ); } } - void write_cloth() {} - void read_cloth() {} + void write_cloth() { + StockpileSettings::ClothSet * cloth = mBuffer.mutable_cloth(); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_thread_silk( token ); + }, mPile->settings.cloth.thread_silk, organic_mat_category::Silk ); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_thread_plant( token ); + }, mPile->settings.cloth.thread_plant, organic_mat_category::PlantFiber); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_thread_yarn( token ); + }, mPile->settings.cloth.thread_yarn, organic_mat_category::Yarn); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_thread_metal( token ); + }, mPile->settings.cloth.thread_metal, organic_mat_category::MetalThread); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_cloth_silk( token ); + }, mPile->settings.cloth.cloth_silk, organic_mat_category::Silk ); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_cloth_plant( token ); + }, mPile->settings.cloth.cloth_plant, organic_mat_category::PlantFiber); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_cloth_yarn( token ); + }, mPile->settings.cloth.cloth_yarn, organic_mat_category::Yarn); + + serialize_list_organic_mat ( [=] ( const std::string &token ) { + cloth->add_cloth_metal( token ); + }, mPile->settings.cloth.cloth_metal, organic_mat_category::MetalThread); + + } + void read_cloth() { + if ( mBuffer.has_cloth() ) { + const StockpileSettings::ClothSet cloth = mBuffer.cloth(); + *mOut << "cloth: " < std::string { + return cloth.thread_silk( idx ); + }, cloth.thread_silk_size(), organic_mat_category::Silk); + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + return cloth.thread_plant( idx ); + }, cloth.thread_plant_size(), organic_mat_category::PlantFiber); + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + return cloth.thread_yarn( idx ); + }, cloth.thread_yarn_size(), organic_mat_category::Yarn); + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + return cloth.thread_metal( idx ); + }, cloth.thread_metal_size(), organic_mat_category::MetalThread); + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + return cloth.cloth_silk( idx ); + }, cloth.cloth_silk_size(), organic_mat_category::Silk); + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + return cloth.cloth_plant( idx ); + }, cloth.cloth_plant_size(), organic_mat_category::PlantFiber); + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + return cloth.cloth_yarn( idx ); + }, cloth.cloth_yarn_size(), organic_mat_category::Yarn); + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + return cloth.cloth_metal( idx ); + }, cloth.cloth_metal_size(), organic_mat_category::MetalThread); + } + } void write_wood() {} void read_wood() {}