From ebb1345332b4a34a6556b8e96b1b5bfe788ed0af Mon Sep 17 00:00:00 2001 From: Casey Link Date: Tue, 18 Nov 2014 14:20:54 +0100 Subject: [PATCH 01/36] stockpiles: add settings protobuf message format --- plugins/CMakeLists.txt | 2 +- plugins/proto/stockpiles.proto | 158 +++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 plugins/proto/stockpiles.proto diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 9c52d2fa6..3cec373e7 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -152,7 +152,7 @@ if (BUILD_SUPPORTED) DFHACK_PLUGIN(sort sort.cpp LINK_LIBRARIES lua) DFHACK_PLUGIN(steam-engine steam-engine.cpp) DFHACK_PLUGIN(stockflow stockflow.cpp LINK_LIBRARIES lua) - DFHACK_PLUGIN(stockpiles stockpiles.cpp) + DFHACK_PLUGIN(stockpiles stockpiles.cpp PROTOBUFS stockpiles) DFHACK_PLUGIN(stocks stocks.cpp) DFHACK_PLUGIN(strangemood strangemood.cpp) DFHACK_PLUGIN(tiletypes tiletypes.cpp Brushes.h) diff --git a/plugins/proto/stockpiles.proto b/plugins/proto/stockpiles.proto new file mode 100644 index 000000000..28ff68907 --- /dev/null +++ b/plugins/proto/stockpiles.proto @@ -0,0 +1,158 @@ +package dfstockpiles; + +option optimize_for = CODE_SIZE; + +message StockpileSettings { + + message AnimalsSet { + optional bool empty_cages = 1; + optional bool empty_traps = 2; + repeated string enabled = 3; + } + + message FoodSet { + repeated string meat = 1; + repeated string fish = 2; + repeated string unprepared_fish = 20; + repeated string egg = 3 ; + repeated string plants = 4 ; + repeated string drink_plant = 5 ; + repeated string drink_animal = 6 ; + repeated string cheese_plant = 7 ; + repeated string cheese_animal = 8 ; + repeated string seeds = 9 ; + repeated string leaves = 10 ; + repeated string powder_plant = 11 ; + repeated string powder_creature = 12 ; + repeated string glob = 13; + repeated string glob_paste = 14 ; + repeated string glob_pressed = 15 ; + repeated string liquid_plant = 16 ; + repeated string liquid_animal = 17; + repeated string liquid_misc = 18; + optional bool prepared_meals = 19; + } + + message FurnitureSet { + repeated string type = 1; + repeated string other_mats = 2; + repeated string mats = 3; + repeated string quality_core = 4; + repeated string quality_total = 5; + optional bool sand_bags = 6; + } + message RefuseSet { + repeated string type = 1; + repeated string corpses = 2; + repeated string body_parts = 3; + repeated string skulls = 4; + repeated string bones = 5; + repeated string hair = 6; + repeated string shells = 7; + repeated string teeth= 8; + repeated string horns = 9; + optional bool fresh_raw_hide = 10; + optional bool rotten_raw_hide = 11; + } + message StoneSet { + repeated string mats = 1; + } + message OreSet { + repeated string mats = 1; + } + message AmmoSet { + repeated string type = 1; + repeated string other_mats = 2; + repeated string mats = 3; + repeated bool quality_core = 4; + repeated bool quality_total = 5; + } + message CoinSet { + repeated string mats = 1; + } + message BarsBlocksSet { + repeated string bars_other_mats = 1; + repeated string blocks_other_mats = 2; + repeated string bars_mats = 3; + repeated string blocks_mats = 4; + } + message GemsSet { + repeated string rough_other_mats = 1; + repeated string cut_other_mats = 2; + repeated string rough_mats = 3; + repeated string cut_mats = 4; + } + message FinishedGoodsSet { + repeated string type = 1; + repeated string other_mats = 2; + repeated string mats = 3; + optional bool quality_core = 4; + optional bool quality_total = 5; + } + message LeatherSet { + repeated string mats = 1; + } + message ClothSet { + repeated string thread_silk = 1; + repeated string thread_plant = 2; + repeated string thread_yarn = 3; + repeated string thread_metal = 4; + repeated string cloth_silk = 5; + repeated string cloth_plant = 6; + repeated string cloth_yarn = 7; + repeated string cloth_metal = 8; + } + message WoodSet { + repeated string mats = 1; + } + message WeaponsSet { + repeated string weapon_type = 1; + repeated string trapcomp_type = 2; + repeated string other_mats = 3; + repeated string mats = 4; + repeated bool quality_core = 5; + repeated bool quality_total = 6; + optional bool usable = 7; + optional bool unusable = 8; + } + + message ArmorSet { + repeated string body = 1; + repeated string head = 2; + repeated string feet = 3; + repeated string hands = 4; + repeated string legs = 5; + repeated string shield = 6; + repeated string other_mats = 7; + repeated string mats = 8; + repeated bool quality_core = 9; + repeated bool quality_total = 10; + optional bool usable =11; + optional bool unusable = 12; + } + + optional AnimalsSet animals = 1; + optional FoodSet food = 2; + optional FurnitureSet furniture = 3; + optional int32 unknown1 = 4; + optional RefuseSet refuse = 5; + optional StoneSet stone = 6; + optional OreSet ore = 7; + optional AmmoSet ammp = 8; + optional CoinSet coin = 9; + optional BarsBlocksSet barsblocks = 10; + optional GemsSet gems = 11; + optional FinishedGoodsSet finished_goods= 12; + optional LeatherSet leather = 13; + optional ClothSet cloth = 14; + optional WoodSet wood = 15; + optional WeaponsSet weapons = 16; + optional ArmorSet armor = 17; + optional bool allow_organic = 18; + optional bool allow_inorganic = 19; + // extras + optional int32 max_barrels = 20; + optional int32 max_bins = 21; + optional int32 max_wheelbarrows = 22; + optional bool use_links_only = 23; +} \ No newline at end of file From 443a9676e110349fbecaac8014d8605f8dd5c603 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Tue, 18 Nov 2014 14:22:04 +0100 Subject: [PATCH 02/36] install the full protobuf library required to access text_format (needed by stockpiles plugin) --- depends/protobuf/CMakeLists.txt | 5 ++++- plugins/Plugins.cmake | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/depends/protobuf/CMakeLists.txt b/depends/protobuf/CMakeLists.txt index 5034f00f4..0544f5d35 100644 --- a/depends/protobuf/CMakeLists.txt +++ b/depends/protobuf/CMakeLists.txt @@ -222,7 +222,10 @@ SET_TARGET_PROPERTIES(protobuf-lite PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_E TARGET_LINK_LIBRARIES(protobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) TARGET_LINK_LIBRARIES(protobuf-lite ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) -install(TARGETS protobuf-lite +#install(TARGETS protobuf-lite + #LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION} + #RUNTIME DESTINATION ${DFHACK_LIBRARY_DESTINATION}) +install(TARGETS protobuf LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION} RUNTIME DESTINATION ${DFHACK_LIBRARY_DESTINATION}) diff --git a/plugins/Plugins.cmake b/plugins/Plugins.cmake index e5d0c3bae..9465bfeaf 100644 --- a/plugins/Plugins.cmake +++ b/plugins/Plugins.cmake @@ -83,7 +83,7 @@ MACRO(DFHACK_PLUGIN) LIST(LENGTH PLUGIN_PROTOBUFS NUM_PROTO) IF(NUM_PROTO) - TARGET_LINK_LIBRARIES(${PLUGIN_NAME} dfhack protobuf-lite ${PLUGIN_LINK_LIBRARIES}) + TARGET_LINK_LIBRARIES(${PLUGIN_NAME} dfhack protobuf ${PLUGIN_LINK_LIBRARIES}) IF(UNIX) SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES COMPILE_FLAGS "-include Export.h") ELSE() @@ -104,4 +104,4 @@ MACRO(DFHACK_PLUGIN) install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION ${DFHACK_PLUGIN_DESTINATION} RUNTIME DESTINATION ${DFHACK_PLUGIN_DESTINATION}) -ENDMACRO(DFHACK_PLUGIN) \ No newline at end of file +ENDMACRO(DFHACK_PLUGIN) From d167b3f3850e79e227106db51fa55459c44ee35f Mon Sep 17 00:00:00 2001 From: Casey Link Date: Wed, 19 Nov 2014 09:09:44 +0100 Subject: [PATCH 03/36] stockpiles: basic serialization for animals - stone --- plugins/proto/stockpiles.proto | 4 +- plugins/stockpiles.cpp | 971 ++++++++++++++++++++++++++++++++- 2 files changed, 954 insertions(+), 21 deletions(-) diff --git a/plugins/proto/stockpiles.proto b/plugins/proto/stockpiles.proto index 28ff68907..392da093b 100644 --- a/plugins/proto/stockpiles.proto +++ b/plugins/proto/stockpiles.proto @@ -138,11 +138,11 @@ message StockpileSettings { optional RefuseSet refuse = 5; optional StoneSet stone = 6; optional OreSet ore = 7; - optional AmmoSet ammp = 8; + optional AmmoSet ammo = 8; optional CoinSet coin = 9; optional BarsBlocksSet barsblocks = 10; optional GemsSet gems = 11; - optional FinishedGoodsSet finished_goods= 12; + optional FinishedGoodsSet finished_goods = 12; optional LeatherSet leather = 13; optional ClothSet cloth = 14; optional WoodSet wood = 15; diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 0cd5d861d..b1f943fc1 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -3,18 +3,46 @@ #include "Export.h" #include "PluginManager.h" +#include "MiscUtils.h" + +#include "modules/Materials.h" +#include "modules/Items.h" + #include "DataDefs.h" #include "df/world.h" +#include "df/world_data.h" +#include "df/inorganic_raw.h" +#include "df/organic_mat_category.h" +#include "df/furniture_type.h" +#include "df/item_quality.h" +#include "df/item_type.h" +#include "df/creature_raw.h" +#include "df/caste_raw.h" +#include "df/material.h" +#include "df/inorganic_raw.h" +#include "df/plant_raw.h" +#include "df/stockpile_group_set.h" + #include "df/ui.h" #include "df/building_stockpilest.h" #include "df/global_objects.h" #include "df/viewscreen_dwarfmodest.h" +#include + + +#include "stockpiles.pb.h" + +#include + +#include using std::vector; using std::string; using std::endl; using namespace DFHack; using namespace df::enums; +using namespace google::protobuf; +using namespace dfstockpiles; using df::global::world; using df::global::ui; @@ -22,16 +50,19 @@ using df::global::selection_rect; using df::building_stockpilest; -static command_result copystock(color_ostream &out, vector & parameters); -static bool copystock_guard(df::viewscreen *top); +static command_result copystock ( color_ostream &out, vector & parameters ); +static bool copystock_guard ( df::viewscreen *top ); + +static command_result savestock ( color_ostream &out, vector & parameters ); +static bool savestock_guard ( df::viewscreen *top ); -DFHACK_PLUGIN("stockpiles"); +DFHACK_PLUGIN ( "stockpiles" ); -DFhackCExport command_result plugin_init (color_ostream &out, std::vector &commands) +DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands ) { - if (world && ui) { - commands.push_back( - PluginCommand( + if ( world && ui ) { + commands.push_back ( + PluginCommand ( "copystock", "Copy stockpile under cursor.", copystock, copystock_guard, " - In 'q' or 't' mode: select a stockpile and invoke in order\n" @@ -40,9 +71,19 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector main.mode) { + switch ( ui->main.mode ) { case Stockpiles: return true; case BuildingItems: case QueryBuilding: - return !!virtual_cast(world->selected_building); + return !!virtual_cast ( world->selected_building ); default: return false; } } -static command_result copystock(color_ostream &out, vector & parameters) +static command_result copystock ( color_ostream &out, vector & parameters ) { // HOTKEY COMMAND: CORE ALREADY SUSPENDED // For convenience: when used in the stockpiles mode, switch to 'q' - if (ui->main.mode == ui_sidebar_mode::Stockpiles) { + if ( ui->main.mode == ui_sidebar_mode::Stockpiles ) { world->selected_building = NULL; // just in case it contains some kind of garbage ui->main.mode = ui_sidebar_mode::QueryBuilding; selection_rect->start_x = -30000; @@ -83,10 +124,9 @@ static command_result copystock(color_ostream &out, vector & parameters return CR_OK; } - building_stockpilest *sp = virtual_cast(world->selected_building); - if (!sp) - { - out.printerr("Selected building isn't a stockpile.\n"); + building_stockpilest *sp = virtual_cast ( world->selected_building ); + if ( !sp ) { + out.printerr ( "Selected building isn't a stockpile.\n" ); return CR_WRONG_USAGE; } @@ -97,3 +137,896 @@ static command_result copystock(color_ostream &out, vector & parameters out << "Stockpile options copied." << endl; return CR_OK; } + + +static bool savestock_guard ( df::viewscreen *top ) +{ + using namespace ui_sidebar_mode; + + if ( !Gui::dwarfmode_hotkey ( top ) ) + return false; + + switch ( ui->main.mode ) { + case Stockpiles: + return true; + case BuildingItems: + case QueryBuilding: + return !!virtual_cast ( world->selected_building ); + default: + return false; + } +} + + + +/** + * Retrieve creature raw from index + */ +static df::creature_raw* find_creature ( int32_t idx ) +{ + return world->raws.creatures.all[idx]; +} + +/** + * Retrieve creature index from id string + */ +static int16_t find_creature ( const std::string &creature_id ) +{ + return linear_index ( world->raws.creatures.all, &df::creature_raw::creature_id, creature_id ); +} + +typedef std::pair FoodMatPair; +typedef std::map FoodMatMap; + +/** + * Helper class for performing organic_index/organic_types <---> material array index lookups + */ +class FoodLookup +{ +public: + struct FoodMat { + MaterialInfo material; + df::creature_raw *creature; + df::caste_raw * caste; + FoodMat() : material ( -1 ), creature ( 0 ), caste ( 0 ) {} + }; + static void food_mat_by_idx ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector::size_type food_idx, FoodMat & food_mat ) { + out << "food_lookup: food_idx(" << food_idx << ") "; + df::world_raws &raws = world->raws; + df::special_mat_table table = raws.mat_table; + int32_t main_idx = table.organic_indexes[mat_category][food_idx]; + int16_t type = table.organic_types[mat_category][food_idx]; + if ( mat_category == organic_mat_category::Fish || + mat_category == organic_mat_category::UnpreparedFish || + mat_category == organic_mat_category::Eggs ) { + food_mat.creature = raws.creatures.all[type]; + food_mat.caste = food_mat.creature->caste[main_idx]; + out << " special creature type(" << type << ") caste("<< main_idx <<")" <::size_type idx ) { + FoodMat food_mat; + food_mat_by_idx ( out, mat_category, idx, food_mat ); + if ( food_mat.material.isValid() ) { + return food_mat.material.getToken(); + } else if ( food_mat.creature ) { + return food_mat.creature->creature_id + ":" + food_mat.caste->caste_id; + } + return std::string(); + } + + static void food_build_map ( color_ostream &out ) { + if ( index_built ) + return; + df::world_raws &raws = world->raws; + 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 raws; + df::special_mat_table table = raws.mat_table; + out << "food_idx_by_token: "; + if ( mat_category == organic_mat_category::Fish || + mat_category == organic_mat_category::UnpreparedFish || + mat_category == organic_mat_category::Eggs ) { + std::vector tokens; + split_string ( &tokens, token, ":" ); + if ( tokens.size() != 2 ) { + out << "creature " << "invalid CREATURE:CASTE token: " << token << endl; + } else { + int16_t creature_idx = find_creature ( tokens[0] ); + if ( creature_idx >= 0 ) { + food_idx = linear_index ( table.organic_types[mat_category], creature_idx ); + out << "creature " << token << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl; + } else out << " creature invalid token " << tokens[0]; + } + } else { + if ( !index_built ) { + food_build_map ( out ); + } + MaterialInfo mat_info = food_mat_by_token ( out, mat_category, token ); + int16_t type = mat_info.type; + int32_t index = mat_info.index; + int16_t food_idx2 = -1; + auto it = food_index[mat_category].find ( std::make_pair ( type, index ) ); + if ( it != food_index[mat_category].end() ) { + out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx(" << it->second << ")" << endl; + } else { + out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx not found :("; + } + } + return food_idx; + } + + static MaterialInfo food_mat_by_token ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) { + MaterialInfo mat_info; + mat_info.find ( token ); + return mat_info; + } + + static bool index_built; + static std::vector food_index; +private: + FoodLookup() {} +}; + +bool FoodLookup::index_built = false; +std::vector FoodLookup::food_index = std::vector ( 37 ); + +class StockpileSerializer +{ +public: + StockpileSerializer ( color_ostream &out, building_stockpilest const * stockpile ) + : mOut ( &out ) + , mPile ( stockpile ) { + furniture_setup_other_mats(); + } + StockpileSettings write() { +// *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; + write_general(); + if ( mPile->settings.flags.bits.animals ) + write_animals(); + if ( mPile->settings.flags.bits.food ) + write_food(); + if ( mPile->settings.flags.bits.furniture ) + write_furniture(); + if ( mPile->settings.flags.bits.refuse ) + write_refuse(); + if ( mPile->settings.flags.bits.stone ) + write_stone(); + if ( mPile->settings.flags.bits.ammo ) + write_ammo(); + std::string str; + TextFormat::PrintToString ( mBuffer, &str ); + *mOut << "serialized: " << str << endl; + return mBuffer; + } + + void read ( const StockpileSettings & settings ) { + *mOut << endl << "==READ==" << endl; + mBuffer = settings; + read_general(); + read_animals(); + read_food(); + read_furniture(); + read_refuse(); + read_stone(); + read_ammo(); + } + + ~StockpileSerializer() {} +private: + color_ostream * mOut; + building_stockpilest const * mPile; + StockpileSettings mBuffer; + std::map mOtherMats;; + /** + * Find an enum's value based off the string label. + * @param traits the enum's trait struct + * @param token the string value in key_table + * @return the enum's value + */ + template + static typename df::enum_traits::base_type linear_index ( color_ostream& out, df::enum_traits traits, const std::string &token ) { + auto j = traits.first_item_value; + auto limit = traits.last_item_value; + // sometimes enums start at -1, which is bad news for array indexing + if ( j < 0 ) { + j += abs ( traits.first_item_value ); + limit += abs ( traits.first_item_value ); + } + for ( ; j <= limit; ++j ) { +// out << " linear_index("<< token <<") = table["<max_barrels ); + mBuffer.set_max_wheelbarrows ( mPile->max_wheelbarrows ); + mBuffer.set_use_links_only ( mPile->use_links_only ); + mBuffer.set_unknown1 ( mPile->settings.unk1 ); + *mOut << "unknown is " << mPile->settings.unk1 << endl; + } + void read_general() { + int bins = mBuffer.max_bins(); + *mOut << "Max bins: " << bins <settings.animals.empty_cages ); + animals.set_empty_traps ( mPile->settings.animals.empty_traps ); + for ( size_t i = 0; i < mPile->settings.animals.enabled.size(); ++i ) { + if ( mPile->settings.animals.enabled.at ( i ) == 1 ) { + df::creature_raw* r = find_creature ( i ); + *mOut << "creature "<< r->creature_id << " " << i << endl; + mBuffer.mutable_animals()->add_enabled ( r->creature_id ); + } + } + } + + void read_animals() { + if ( mBuffer.has_animals() ) { +// mPile->settings.flags.bits.animals = true; + *mOut << "animals:" < 0 ) { + for ( auto i = 0; i < mBuffer.animals().enabled().size(); ++i ) { + std::string id = mBuffer.animals().enabled ( i ); + int idx = find_creature ( id ); + *mOut << id << " " << idx << endl; + } + } + } + } + + typedef std::function FoodImportFunc; + typedef std::function FoodExportFunc; + struct food_pair { + // exporting + FoodExportFunc set_value; + vector stockpile_values; + // importing + FoodImportFunc get_value; + size_t serialized_count; + + food_pair ( FoodExportFunc s, const vector& sp_v, FoodImportFunc g, size_t count ) + : set_value ( s ) + , stockpile_values ( sp_v ) + , get_value ( g ) + ,serialized_count ( count ) + {} + food_pair() {} + }; + food_pair food_map ( organic_mat_category::organic_mat_category cat ) { + using df::enums::organic_mat_category::organic_mat_category; + using namespace std::placeholders; + switch ( cat ) { + case organic_mat_category::Meat: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_meat ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().meat ( idx ); }; + return food_pair ( setter, mPile->settings.food.meat, getter, mBuffer.food().meat_size() ); + } + case organic_mat_category::Fish: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_fish ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().fish ( idx ); }; + return food_pair ( setter, mPile->settings.food.fish, getter, mBuffer.food().fish_size() ); + } + case organic_mat_category::UnpreparedFish: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_unprepared_fish ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().unprepared_fish ( idx ); }; + return food_pair ( setter, mPile->settings.food.unprepared_fish, getter, mBuffer.food().unprepared_fish_size() ); + } + case organic_mat_category::Eggs: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_egg ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().egg ( idx ); }; + return food_pair ( setter, mPile->settings.food.egg, getter, mBuffer.food().egg_size() ); + } + case organic_mat_category::Plants: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_plants ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().plants ( idx ); }; + return food_pair ( setter, mPile->settings.food.plants, getter, mBuffer.food().plants_size() ); + } + case organic_mat_category::PlantDrink: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_drink_plant ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_plant ( idx ); }; + return food_pair ( setter, mPile->settings.food.drink_plant, getter, mBuffer.food().drink_plant_size() ); + } + case organic_mat_category::CreatureDrink: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_drink_animal ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_animal ( idx ); }; + return food_pair ( setter, mPile->settings.food.drink_animal, getter, mBuffer.food().drink_animal_size() ); + } + case organic_mat_category::PlantCheese: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_cheese_plant ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_plant ( idx ); }; + return food_pair ( setter, mPile->settings.food.cheese_plant, getter, mBuffer.food().cheese_plant_size() ); + } + case organic_mat_category::CreatureCheese: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_cheese_animal ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_animal ( idx ); }; + return food_pair ( setter, mPile->settings.food.cheese_animal, getter, mBuffer.food().cheese_animal_size() ); + } + case organic_mat_category::Seed: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_seeds ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().seeds ( idx ); }; + return food_pair ( setter, mPile->settings.food.seeds, getter, mBuffer.food().seeds_size() ); + } + case organic_mat_category::Leaf: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_leaves ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().leaves ( idx ); }; + return food_pair ( setter, mPile->settings.food.leaves, getter, mBuffer.food().leaves_size() ); + } + case organic_mat_category::PlantPowder: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_powder_plant ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_plant ( idx ); }; + return food_pair ( setter, mPile->settings.food.powder_plant, getter, mBuffer.food().powder_plant_size() ); + } + case organic_mat_category::CreaturePowder: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_powder_creature ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_creature ( idx ); }; + return food_pair ( setter, mPile->settings.food.powder_creature, getter, mBuffer.food().powder_creature_size() ); + } + case organic_mat_category::Glob: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_glob ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob ( idx ); }; + return food_pair ( setter, mPile->settings.food.glob, getter, mBuffer.food().glob_size() ); + } + case organic_mat_category::PlantLiquid: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_liquid_plant ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_plant ( idx ); }; + return food_pair ( setter, mPile->settings.food.liquid_plant, getter, mBuffer.food().liquid_plant_size() ); + } + case organic_mat_category::CreatureLiquid: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_liquid_animal ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_animal ( idx ); }; + return food_pair ( setter, mPile->settings.food.liquid_animal, getter, mBuffer.food().liquid_animal_size() ); + } + case organic_mat_category::MiscLiquid: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_liquid_misc ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_misc ( idx ); }; + return food_pair ( setter, mPile->settings.food.liquid_misc, getter, mBuffer.food().liquid_misc_size() ); + } + + case organic_mat_category::Paste: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_glob_paste ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_paste ( idx ); }; + return food_pair ( setter, mPile->settings.food.glob_paste, getter, mBuffer.food().glob_paste_size() ); + } + case organic_mat_category::Pressed: { + auto setter = [=] ( const std::string &id ) { + mBuffer.mutable_food()->add_glob_pressed ( id ); + }; + auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_pressed ( idx ); }; + return food_pair ( setter, mPile->settings.food.glob_pressed, getter, mBuffer.food().glob_pressed_size() ); + } + case organic_mat_category::Leather: + case organic_mat_category::Silk: + case organic_mat_category::PlantFiber: + case organic_mat_category::Bone: + case organic_mat_category::Shell: + case organic_mat_category::Wood: + case organic_mat_category::Horn: + case organic_mat_category::Pearl: + case organic_mat_category::Tooth: + case organic_mat_category::EdibleCheese: + case organic_mat_category::AnyDrink: + case organic_mat_category::EdiblePlant: + case organic_mat_category::CookableLiquid: + case organic_mat_category::CookablePowder: + case organic_mat_category::CookableSeed: + case organic_mat_category::CookableLeaf: + case organic_mat_category::Yarn: + case organic_mat_category::MetalThread: + // not used in stockpile food menu + break; + } + 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(); + food->set_prepared_meals ( mPile->settings.food.prepared_meals ); + food_pair p = food_map ( organic_mat_category::Meat ); + food_write_helper ( p.set_value, p.stockpile_values, organic_mat_category::Meat ); + + using df::enums::organic_mat_category::organic_mat_category; + df::enum_traits traits; + for ( int32_t mat_category = traits.first_item_value; mat_category 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 = 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(); + *mOut << "food:" < traits; + for ( int32_t mat_category = traits.first_item_value; mat_category second; + } + + int furn_other_mats ( const std::string & token ) { + for ( auto it = mOtherMats.begin(); it != mOtherMats.end(); ++it ) { + if ( it->second == token ) + return it->first; + } + return -1; + } + + void write_furniture() { + StockpileSettings::FurnitureSet *furniture= mBuffer.mutable_furniture(); + furniture->set_sand_bags ( mPile->settings.furniture.sand_bags ); + + // FURNITURE type + using df::enums::furniture_type::furniture_type; + df::enum_traits type_traits; + for ( size_t i = 0; i < mPile->settings.furniture.type.size(); ++i ) { + if ( mPile->settings.furniture.type.at ( i ) ) { + std::string f_type ( type_traits.key_table[i] ); + furniture->add_type ( f_type ); + *mOut << "furniture_type " << i << " is " << f_type <settings.furniture.mats.size(); ++i ) { + if ( mPile->settings.furniture.mats.at ( i ) ) { + mi.decode ( 0, i ); + if ( !furniture_mat_is_allowed ( mi ) ) continue; + *mOut << "furniture mat: " << mi.getToken() << endl; + furniture->add_mats ( mi.getToken() ); + } + } + // other mats + for ( size_t i = 0; i < mPile->settings.furniture.other_mats.size(); ++i ) { + if ( mPile->settings.furniture.other_mats.at ( i ) ) { + const std::string token = furn_other_mats ( i ); + if ( token.empty() ) { + *mOut << " invalid other material with index " << i << endl; + continue; + } + furniture->add_other_mats ( token ); + *mOut << " other mats " << i << " is " << token << endl; + } + } + // quality core + using df::enums::item_quality::item_quality; + df::enum_traits quality_traits; + size_t core_size = std::extent< decltype ( mPile->settings.furniture.quality_core ) >::value; + for ( size_t i = 0; i < core_size; ++i ) { + if ( mPile->settings.furniture.quality_core[i] ) { + const std::string f_type ( quality_traits.key_table[i] ); + furniture->add_quality_core ( f_type ); + *mOut << "quality_core " << i << " is " << f_type <settings.furniture.quality_total ) >::value; + for ( size_t i = 0; i < total_size; ++i ) { + if ( mPile->settings.furniture.quality_total[i] ) { + const std::string f_type ( quality_traits.key_table[i] ); + furniture->add_quality_total ( f_type ); + *mOut << "quality_total " << i << " is " << f_type <:flags.is_set ( material_flags::IS_GEM ) && mi.material->flags.is_set ( material_flags::ITEMS_HARD ); + } + + void read_furniture() { + if ( mBuffer.has_furniture() ) { + const StockpileSettings::FurnitureSet furniture = mBuffer.furniture(); + *mOut << "furniture:" < type_traits; + for ( int i = 0; i < furniture.type_size(); ++i ) { + const std::string type = furniture.type ( i ); + df::enum_traits::base_type idx = linear_index ( *mOut, type_traits, type ); + *mOut << " type " << idx << " is " << type << endl; + } + // metal, stone/clay materials + for ( int i = 0; i < furniture.mats_size(); ++i ) { + const std::string token = furniture.mats ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !furniture_mat_is_allowed ( mi ) ) continue; + *mOut << " mats " << mi.index << " is " << token << endl; + } + // other materials + for ( int i = 0; i < furniture.other_mats_size(); ++i ) { + const std::string token = furniture.other_mats ( i ); + int16_t idx = furn_other_mats ( token ); + if ( idx < 0 ) { + *mOut << "invalid other mat with token " << token; + continue; + } + *mOut << " other_mats" << idx << " is " << token << endl; + } + // core quality + using df::enums::item_quality::item_quality; + df::enum_traits quality_traits; + for ( int i = 0; i < furniture.quality_core_size(); ++i ) { + const std::string quality = furniture.quality_core ( i ); + df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); + if ( idx < 0 ) { + *mOut << " invalid quality core token " << quality << endl; + continue; + } + *mOut << " quality_core" << idx << " is " << quality << endl; + } + // total quality + for ( int i = 0; i < furniture.quality_total_size(); ++i ) { + const std::string quality = furniture.quality_total ( i ); + df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); + if ( idx < 0 ) { + *mOut << " invalid quality total token " << quality << endl; + continue; + } + *mOut << " quality_total" << idx << " is " << quality << endl; + } + } + } + + bool refuse_creature_is_allowed ( const df::creature_raw *raw ) { + if ( !raw ) return false; + // wagon and generated creatures not allowed, except angels + const bool is_wagon = raw->creature_id == "EQUIPMENT_WAGON"; + const bool is_generated = raw->flags.is_set ( creature_raw_flags::GENERATED ); + const bool is_angel = is_generated && raw->creature_id.find ( "DIVINE_" ) != std::string::npos; + return !is_wagon && ! ( is_generated && !is_angel ); + } + + void refuse_write_helper ( std::function add_value, const vector & list ) { + for ( size_t i = 0; i < list.size(); ++i ) { + if ( list.at ( i ) == 1 ) { + df::creature_raw* r = find_creature ( i ); + // skip forgotten beasts, titans, demons, and night creatures + if ( !refuse_creature_is_allowed ( r ) ) continue; + *mOut << "creature "<< r->creature_id << " " << i << endl; + add_value ( r->creature_id ); + } + } + } + + bool refuse_type_is_allowed ( item_type::item_type type ) { + if ( type == item_type::NONE + || type == item_type::BAR + || type == item_type::SMALLGEM + || type == item_type::BLOCKS + || type == item_type::ROUGH + || type == item_type::BOULDER + || type == item_type::CORPSE + || type == item_type::CORPSEPIECE + || type == item_type::ROCK + || type == item_type::ORTHOPEDIC_CAST + ) return false; + return true; + } + + void write_refuse() { + StockpileSettings::RefuseSet *refuse = mBuffer.mutable_refuse(); + refuse->set_fresh_raw_hide ( mPile->settings.refuse.fresh_raw_hide ); + refuse->set_rotten_raw_hide ( mPile->settings.refuse.rotten_raw_hide ); + + // type + using df::enums::item_type::item_type; + df::enum_traits type_traits; + *mOut << "refuse type size = " << mPile->settings.refuse.type.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; + for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) { + if ( mPile->settings.refuse.type.at ( i ) ) { + const item_type type = ( item_type ) ( ( df::enum_traits::base_type ) i ); + std::string r_type ( type_traits.key_table[i+1] ); + if ( !refuse_type_is_allowed ( type ) ) continue; + refuse->add_type ( r_type ); + *mOut << "refuse type key_table[" << i+1 << "] type[" << ( int16_t ) type << "] is " << r_type <add_corpses ( id ); + }, mPile->settings.refuse.corpses ); + // body_parts + refuse_write_helper ( [=] ( const std::string &id ) { + refuse->add_body_parts ( id ); + }, mPile->settings.refuse.body_parts ); + // skulls + refuse_write_helper ( [=] ( const std::string &id ) { + refuse->add_skulls ( id ); + }, mPile->settings.refuse.skulls ); + // bones + refuse_write_helper ( [=] ( const std::string &id ) { + refuse->add_bones ( id ); + }, mPile->settings.refuse.bones ); + // hair + refuse_write_helper ( [=] ( const std::string &id ) { + refuse->add_hair ( id ); + }, mPile->settings.refuse.hair ); + // shells + refuse_write_helper ( [=] ( const std::string &id ) { + refuse->add_shells ( id ); + }, mPile->settings.refuse.shells ); + // teeth + refuse_write_helper ( [=] ( const std::string &id ) { + refuse->add_teeth ( id ); + }, mPile->settings.refuse.teeth ); + // horns + refuse_write_helper ( [=] ( const std::string &id ) { + refuse->add_horns ( id ); + }, mPile->settings.refuse.horns ); + } + + void refuse_read_helper ( std::function get_value, size_t list_size ) { + if ( list_size > 0 ) { + for ( size_t i = 0; i < list_size; ++i ) { + const std::string creature_id = get_value ( i ); + const int idx = find_creature ( creature_id ); + const df::creature_raw* creature = find_creature ( idx ); + if ( idx < 0 || !refuse_creature_is_allowed ( creature ) ) { + *mOut << "invalid refuse creature: " << creature_id << endl; + continue; + } + *mOut << " creature " << idx << " is " << creature_id << endl; + } + } + } + + void read_refuse() { + if ( mBuffer.has_refuse() ) { + const StockpileSettings::RefuseSet refuse = mBuffer.refuse(); + *mOut << "refuse: " < type_traits; + for ( int i = 0; i < refuse.type_size(); ++i ) { + const std::string token = refuse.type ( i ); + // subtract one because item_type starts at -1 + const df::enum_traits::base_type idx = linear_index ( *mOut, type_traits, token ) - 1; + const item_type type = (item_type) idx; + if ( !refuse_type_is_allowed(type) ) continue; + *mOut << " type " << idx << " is " << token << endl; + } + + // corpses + *mOut << " corpses" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.corpses ( idx ); + }, refuse.corpses_size() ); + // body_parts + *mOut << " body_parts" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.body_parts ( idx ); + }, refuse.body_parts_size() ); + // skulls + *mOut << " skulls" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.skulls ( idx ); + }, refuse.skulls_size() ); + // bones + *mOut << " bones" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.bones ( idx ); + }, refuse.bones_size() ); + // hair + *mOut << " hair" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.hair ( idx ); + }, refuse.hair_size() ); + // shells + *mOut << " shells" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.shells ( idx ); + }, refuse.shells_size() ); + // teeth + *mOut << " teeth" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.teeth ( idx ); + }, refuse.teeth_size() ); + // horns + *mOut << " horns" << endl; + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + return refuse.horns ( idx ); + }, refuse.horns_size() ); + } + } + + bool stone_is_allowed ( const MaterialInfo &mi ) { + if ( !mi.isValid() ) return false; + const bool is_allowed_soil = mi.inorganic->flags.is_set ( inorganic_flags::SOIL ) && !mi.inorganic->flags.is_set ( inorganic_flags::AQUIFER ); + const bool is_allowed_stone = mi.material->flags.is_set ( material_flags::IS_STONE ) && !mi.material->flags.is_set ( material_flags::NO_STONE_STOCKPILE ); + return is_allowed_soil || is_allowed_stone; + } + + void write_stone() { + StockpileSettings::StoneSet *stone= mBuffer.mutable_stone(); + MaterialInfo mi; + for ( size_t i = 0; i < mPile->settings.stone.mats.size(); ++i ) { + if ( mPile->settings.stone.mats.at ( i ) ) { + mi.decode ( 0, i ); + if ( !stone_is_allowed ( mi ) ) continue; + *mOut << "stone mat: " << i << " is " << mi.getToken() << endl; + stone->add_mats ( mi.getToken() ); + } + } + } + + void read_stone() { + if ( mBuffer.has_stone() ) { + const StockpileSettings::StoneSet stone = mBuffer.stone(); + *mOut << "stone: " <settings.ammo.type.size(); ++i ) { + if ( mPile->settings.ammo.type.at ( i ) ) { + const df::itemdef_ammost *a = world->raws.itemdefs.ammo.at ( i ); + // skip procedurally generated ammo + if ( a->base_flags.is_set ( 0 ) ) continue; + ItemTypeInfo ii; +// ii.decode(item_type::AMMO, a->subtype); + ii.decode ( item_type::AMMO, i ); + if ( !ii.isValid() ) continue; + ammo->add_type ( ii.getToken() ); + *mOut << " " << i << " is " << ii.getToken() << endl; + } + } + } + + void read_ammo() { + if ( mBuffer.has_ammo() ) { + const StockpileSettings::AmmoSet ammo = mBuffer.ammo(); + *mOut << "ammo: " < & parameters ) +{ + // HOTKEY COMMAND: CORE ALREADY SUSPENDED + building_stockpilest *sp = virtual_cast ( world->selected_building ); + if ( !sp ) { + out.printerr ( "Selected building isn't a stockpile.\n" ); + return CR_WRONG_USAGE; + } + + // for testing + StockpileSerializer cereal ( out, sp ); + StockpileSettings s = cereal.write(); + StockpileSerializer cereal2 ( out, sp ); + cereal2.read ( s ); + + return CR_OK; +} + From dc5f0fed1a1c162452bc8e594f75e50c199805b3 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Wed, 19 Nov 2014 10:20:54 +0100 Subject: [PATCH 04/36] stockpiles: serialize ammo and coins --- plugins/proto/stockpiles.proto | 22 +++--- plugins/stockpiles.cpp | 131 ++++++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 15 deletions(-) diff --git a/plugins/proto/stockpiles.proto b/plugins/proto/stockpiles.proto index 392da093b..7be0edb13 100644 --- a/plugins/proto/stockpiles.proto +++ b/plugins/proto/stockpiles.proto @@ -32,7 +32,7 @@ message StockpileSettings { repeated string liquid_misc = 18; optional bool prepared_meals = 19; } - + message FurnitureSet { repeated string type = 1; repeated string other_mats = 2; @@ -64,8 +64,8 @@ message StockpileSettings { repeated string type = 1; repeated string other_mats = 2; repeated string mats = 3; - repeated bool quality_core = 4; - repeated bool quality_total = 5; + repeated string quality_core = 4; + repeated string quality_total = 5; } message CoinSet { repeated string mats = 1; @@ -86,8 +86,8 @@ message StockpileSettings { repeated string type = 1; repeated string other_mats = 2; repeated string mats = 3; - optional bool quality_core = 4; - optional bool quality_total = 5; + optional string quality_core = 4; + optional string quality_total = 5; } message LeatherSet { repeated string mats = 1; @@ -110,12 +110,12 @@ message StockpileSettings { repeated string trapcomp_type = 2; repeated string other_mats = 3; repeated string mats = 4; - repeated bool quality_core = 5; - repeated bool quality_total = 6; + repeated string quality_core = 5; + repeated string quality_total = 6; optional bool usable = 7; optional bool unusable = 8; } - + message ArmorSet { repeated string body = 1; repeated string head = 2; @@ -125,12 +125,12 @@ message StockpileSettings { repeated string shield = 6; repeated string other_mats = 7; repeated string mats = 8; - repeated bool quality_core = 9; - repeated bool quality_total = 10; + repeated string quality_core = 9; + repeated string quality_total = 10; optional bool usable =11; optional bool unusable = 12; } - + optional AnimalsSet animals = 1; optional FoodSet food = 2; optional FurnitureSet furniture = 3; diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index b1f943fc1..ee438a8eb 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -310,6 +310,8 @@ public: write_stone(); if ( mPile->settings.flags.bits.ammo ) write_ammo(); + if ( mPile->settings.flags.bits.coins ) + write_coins(); std::string str; TextFormat::PrintToString ( mBuffer, &str ); *mOut << "serialized: " << str << endl; @@ -326,6 +328,7 @@ public: read_refuse(); read_stone(); read_ammo(); + read_coins(); } ~StockpileSerializer() {} @@ -899,8 +902,8 @@ private: const std::string token = refuse.type ( i ); // subtract one because item_type starts at -1 const df::enum_traits::base_type idx = linear_index ( *mOut, type_traits, token ) - 1; - const item_type type = (item_type) idx; - if ( !refuse_type_is_allowed(type) ) continue; + const item_type type = ( item_type ) idx; + if ( !refuse_type_is_allowed ( type ) ) continue; *mOut << " type " << idx << " is " << token << endl; } @@ -981,21 +984,72 @@ private: } } + bool ammo_mat_is_allowed ( const MaterialInfo &mi ) { + return mi.isValid() && mi.material && mi.material->flags.is_set ( material_flags::IS_METAL ); + } + void write_ammo() { - StockpileSettings::AmmoSet *ammo= mBuffer.mutable_ammo(); + StockpileSettings::AmmoSet *ammo = mBuffer.mutable_ammo(); + + // type for ( size_t i = 0; i < mPile->settings.ammo.type.size(); ++i ) { if ( mPile->settings.ammo.type.at ( i ) ) { const df::itemdef_ammost *a = world->raws.itemdefs.ammo.at ( i ); // skip procedurally generated ammo if ( a->base_flags.is_set ( 0 ) ) continue; ItemTypeInfo ii; -// ii.decode(item_type::AMMO, a->subtype); ii.decode ( item_type::AMMO, i ); if ( !ii.isValid() ) continue; ammo->add_type ( ii.getToken() ); *mOut << " " << i << " is " << ii.getToken() << endl; } } + // metal + MaterialInfo mi; + for ( size_t i = 0; i < mPile->settings.ammo.mats.size(); ++i ) { + if ( mPile->settings.ammo.mats.at ( i ) ) { + mi.decode ( 0, i ); + if ( !ammo_mat_is_allowed ( mi ) ) continue; + ammo->add_mats ( mi.getToken() ); + } + } + *mOut << "after metal" << endl; + // other mats + if ( mPile->settings.ammo.other_mats.size() > 2 ) { + *mOut << "WARNING: ammo other materials > 2! " << mPile->settings.ammo.other_mats.size() << endl; + } + *mOut << "after other check" << endl; + for ( size_t i = 0; i < std::min ( size_t ( 2 ), mPile->settings.ammo.other_mats.size() ); ++i ) { + if ( !mPile->settings.ammo.other_mats.at ( i ) ) + continue; + const std::string token = i == 0 ? "WOOD" : "BONE"; + ammo->add_other_mats ( token ); + *mOut << " other mats " << i << " is " << token << endl; + } + *mOut << "after other" << endl; + // quality core + using df::enums::item_quality::item_quality; + df::enum_traits quality_traits; + size_t core_size = std::extent< decltype ( mPile->settings.ammo.quality_core ) >::value; + for ( size_t i = 0; i < core_size; ++i ) { + if ( mPile->settings.ammo.quality_core[i] ) { + const std::string f_type ( quality_traits.key_table[i] ); + ammo->add_quality_core ( f_type ); + *mOut << "quality_core " << i << " is " << f_type <settings.ammo.quality_total ) >::value; + for ( size_t i = 0; i < total_size; ++i ) { + if ( mPile->settings.ammo.quality_total[i] ) { + const std::string f_type ( quality_traits.key_table[i] ); + ammo->add_quality_total ( f_type ); + *mOut << "quality_total " << i << " is " << f_type < quality_traits; + for ( int i = 0; i < ammo.quality_core_size(); ++i ) { + const std::string quality = ammo.quality_core ( i ); + df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); + if ( idx < 0 ) { + *mOut << " invalid quality core token " << quality << endl; + continue; + } + *mOut << " quality_core" << idx << " is " << quality << endl; + } + // total quality + for ( int i = 0; i < ammo.quality_total_size(); ++i ) { + const std::string quality = ammo.quality_total ( i ); + df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); + if ( idx < 0 ) { + *mOut << " invalid quality total token " << quality << endl; + continue; + } + *mOut << " quality_total" << idx << " is " << quality << endl; + } + } + } + + bool coins_mat_is_allowed ( const MaterialInfo &mi ) { + return mi.isValid(); + } + + void write_coins() { + StockpileSettings::CoinSet *coins = mBuffer.mutable_coin(); + MaterialInfo mi; + for ( size_t i = 0; i < mPile->settings.coins.mats.size(); ++i ) { + if ( mPile->settings.coins.mats.at ( i ) ) { + mi.decode ( 0, i ); + if ( !coins_mat_is_allowed ( mi ) ) continue; + *mOut << " coin mat" << i << " is " << mi.getToken() << endl; + coins->add_mats ( mi.getToken() ); + } + } + + } + + void read_coins() { + if ( mBuffer.has_coin() ) { + const StockpileSettings::CoinSet coins = mBuffer.coin(); + *mOut << "coins: " < Date: Wed, 19 Nov 2014 14:14:46 +0100 Subject: [PATCH 05/36] stockpiles: serialize through finished goods * add stubs for remaining categories * refactor w/ generics to reuse code --- plugins/stockpiles.cpp | 622 ++++++++++++++++++++++++++++++++++------- 1 file changed, 524 insertions(+), 98 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index ee438a8eb..583d4048a 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -49,6 +49,7 @@ using df::global::ui; using df::global::selection_rect; using df::building_stockpilest; +using std::placeholders::_1; static command_result copystock ( color_ostream &out, vector & parameters ); static bool copystock_guard ( df::viewscreen *top ); @@ -294,6 +295,7 @@ public: : mOut ( &out ) , mPile ( stockpile ) { furniture_setup_other_mats(); + bars_blocks_setup_other_mats(); } StockpileSettings write() { // *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; @@ -312,6 +314,23 @@ public: write_ammo(); if ( mPile->settings.flags.bits.coins ) write_coins(); + if ( mPile->settings.flags.bits.bars_blocks ) + write_bars_blocks(); + if ( mPile->settings.flags.bits.gems ) + write_gems(); + if ( mPile->settings.flags.bits.finished_goods ) + write_finished_goods(); + if ( mPile->settings.flags.bits.leather ) + write_leather(); + if ( mPile->settings.flags.bits.cloth ) + write_cloth(); + if ( mPile->settings.flags.bits.wood ) + write_wood(); + if ( mPile->settings.flags.bits.weapons ) + write_weapons(); + if ( mPile->settings.flags.bits.armor ) + write_armor(); + std::string str; TextFormat::PrintToString ( mBuffer, &str ); *mOut << "serialized: " << str << endl; @@ -329,6 +348,14 @@ public: read_stone(); read_ammo(); read_coins(); + read_bars_blocks(); + read_gems(); + read_finished_goods(); + read_leather(); + read_cloth(); + read_wood(); + read_weapons(); + read_armor(); } ~StockpileSerializer() {} @@ -336,7 +363,9 @@ private: color_ostream * mOut; building_stockpilest const * mPile; StockpileSettings mBuffer; - std::map mOtherMats;; + std::map mOtherMatsFurniture;; + std::map mOtherMatsBars; + std::map mOtherMatsBlocks; /** * Find an enum's value based off the string label. * @param traits the enum's trait struct @@ -359,6 +388,81 @@ private: } return -1; } + + + // read the token from the serailized list during import + typedef std::function FuncReadImport; + // add the token to the serialized list during export + typedef std::function FuncWriteExport; + // are item's of item_type allowed? + typedef std::function FuncItemAllowed; + typedef std::function FuncMaterialAllowed; + struct food_pair { + // exporting + FuncWriteExport set_value; + vector stockpile_values; + // importing + FuncReadImport get_value; + size_t serialized_count; + + food_pair ( FuncWriteExport s, const vector& sp_v, FuncReadImport g, size_t count ) + : set_value ( s ) + , stockpile_values ( sp_v ) + , get_value ( g ) + ,serialized_count ( count ) + {} + food_pair() {} + }; + + 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; + *mOut << "item_type size = " << list.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; + for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) { + if ( list.at ( i ) ) { + const item_type type = ( item_type ) ( ( df::enum_traits::base_type ) i ); + std::string r_type ( type_traits.key_table[i+1] ); + if ( !is_allowed ( type ) ) continue; + add_value ( r_type ); + *mOut << "item_type key_table[" << i+1 << "] type[" << ( int16_t ) type << "] is " << r_type < type_traits; + for ( int32_t i = 0; i < list_size; ++i ) { + const std::string token = read_value ( i ); + // subtract one because item_type starts at -1 + const df::enum_traits::base_type idx = linear_index ( *mOut, type_traits, token ) - 1; + const item_type type = ( item_type ) idx; + if ( !is_allowed ( type ) ) continue; + *mOut << " item_type " << idx << " is " << token << endl; + } + } + void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, std::vector list ) { + MaterialInfo mi; + for ( size_t i = 0; i < list.size(); ++i ) { + if ( list.at ( i ) ) { + mi.decode ( 0, i ); + if ( !is_allowed( mi ) ) continue; + *mOut << " material" << i << " is " << mi.getToken() << endl; + add_value(mi.getToken()); + } + } + } + + + void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) { + for ( int i = 0; i < list_size; ++i ) { + const std::string token = read_value ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !is_allowed ( mi ) ) continue; + *mOut << " material " << mi.index << " is " << token << endl; + } + } + void write_general() { mBuffer.set_max_bins ( mPile->max_barrels ); mBuffer.set_max_wheelbarrows ( mPile->max_wheelbarrows ); @@ -399,160 +503,142 @@ private: } } - typedef std::function FoodImportFunc; - typedef std::function FoodExportFunc; - struct food_pair { - // exporting - FoodExportFunc set_value; - vector stockpile_values; - // importing - FoodImportFunc get_value; - size_t serialized_count; - - food_pair ( FoodExportFunc s, const vector& sp_v, FoodImportFunc g, size_t count ) - : set_value ( s ) - , stockpile_values ( sp_v ) - , get_value ( g ) - ,serialized_count ( count ) - {} - food_pair() {} - }; food_pair food_map ( organic_mat_category::organic_mat_category cat ) { using df::enums::organic_mat_category::organic_mat_category; using namespace std::placeholders; switch ( cat ) { case organic_mat_category::Meat: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_meat ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().meat ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().meat ( idx ); }; return food_pair ( setter, mPile->settings.food.meat, getter, mBuffer.food().meat_size() ); } case organic_mat_category::Fish: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_fish ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().fish ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().fish ( idx ); }; return food_pair ( setter, mPile->settings.food.fish, getter, mBuffer.food().fish_size() ); } case organic_mat_category::UnpreparedFish: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_unprepared_fish ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().unprepared_fish ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().unprepared_fish ( idx ); }; return food_pair ( setter, mPile->settings.food.unprepared_fish, getter, mBuffer.food().unprepared_fish_size() ); } case organic_mat_category::Eggs: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_egg ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().egg ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().egg ( idx ); }; return food_pair ( setter, mPile->settings.food.egg, getter, mBuffer.food().egg_size() ); } case organic_mat_category::Plants: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_plants ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().plants ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().plants ( idx ); }; return food_pair ( setter, mPile->settings.food.plants, getter, mBuffer.food().plants_size() ); } case organic_mat_category::PlantDrink: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_drink_plant ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_plant ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.drink_plant, getter, mBuffer.food().drink_plant_size() ); } case organic_mat_category::CreatureDrink: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_drink_animal ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_animal ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_animal ( idx ); }; return food_pair ( setter, mPile->settings.food.drink_animal, getter, mBuffer.food().drink_animal_size() ); } case organic_mat_category::PlantCheese: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_cheese_plant ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_plant ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.cheese_plant, getter, mBuffer.food().cheese_plant_size() ); } case organic_mat_category::CreatureCheese: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_cheese_animal ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_animal ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_animal ( idx ); }; return food_pair ( setter, mPile->settings.food.cheese_animal, getter, mBuffer.food().cheese_animal_size() ); } case organic_mat_category::Seed: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_seeds ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().seeds ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().seeds ( idx ); }; return food_pair ( setter, mPile->settings.food.seeds, getter, mBuffer.food().seeds_size() ); } case organic_mat_category::Leaf: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_leaves ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().leaves ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().leaves ( idx ); }; return food_pair ( setter, mPile->settings.food.leaves, getter, mBuffer.food().leaves_size() ); } case organic_mat_category::PlantPowder: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_powder_plant ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_plant ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.powder_plant, getter, mBuffer.food().powder_plant_size() ); } case organic_mat_category::CreaturePowder: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_powder_creature ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_creature ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_creature ( idx ); }; return food_pair ( setter, mPile->settings.food.powder_creature, getter, mBuffer.food().powder_creature_size() ); } case organic_mat_category::Glob: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_glob ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob ( idx ); }; return food_pair ( setter, mPile->settings.food.glob, getter, mBuffer.food().glob_size() ); } case organic_mat_category::PlantLiquid: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_liquid_plant ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_plant ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.liquid_plant, getter, mBuffer.food().liquid_plant_size() ); } case organic_mat_category::CreatureLiquid: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_liquid_animal ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_animal ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_animal ( idx ); }; return food_pair ( setter, mPile->settings.food.liquid_animal, getter, mBuffer.food().liquid_animal_size() ); } case organic_mat_category::MiscLiquid: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_liquid_misc ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_misc ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_misc ( idx ); }; return food_pair ( setter, mPile->settings.food.liquid_misc, getter, mBuffer.food().liquid_misc_size() ); } case organic_mat_category::Paste: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_glob_paste ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_paste ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_paste ( idx ); }; return food_pair ( setter, mPile->settings.food.glob_paste, getter, mBuffer.food().glob_paste_size() ); } case organic_mat_category::Pressed: { - auto setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) { mBuffer.mutable_food()->add_glob_pressed ( id ); }; - auto getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_pressed ( idx ); }; + FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_pressed ( idx ); }; return food_pair ( setter, mPile->settings.food.glob_pressed, getter, mBuffer.food().glob_pressed_size() ); } case organic_mat_category::Leather: @@ -606,7 +692,7 @@ private: } - void food_read_helper ( std::function get_value, size_t list_size, organic_mat_category::organic_mat_category cat ) { + void food_read_helper ( 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 ); @@ -630,32 +716,32 @@ private: } void furniture_setup_other_mats() { - mOtherMats.insert ( std::make_pair ( 0,"WOOD" ) ); - mOtherMats.insert ( std::make_pair ( 1,"PLANT_CLOTH" ) ); - mOtherMats.insert ( std::make_pair ( 2,"BONE" ) ); - mOtherMats.insert ( std::make_pair ( 3,"TOOTH" ) ); - mOtherMats.insert ( std::make_pair ( 4,"HORN" ) ); - mOtherMats.insert ( std::make_pair ( 5,"PEARL" ) ); - mOtherMats.insert ( std::make_pair ( 6,"SHELL" ) ); - mOtherMats.insert ( std::make_pair ( 7,"LEATHER" ) ); - mOtherMats.insert ( std::make_pair ( 8,"SILK" ) ); - mOtherMats.insert ( std::make_pair ( 9,"AMBER" ) ); - mOtherMats.insert ( std::make_pair ( 10,"CORAL" ) ); - mOtherMats.insert ( std::make_pair ( 11,"GREEN_GLASS" ) ); - mOtherMats.insert ( std::make_pair ( 12,"CLEAR_GLASS" ) ); - mOtherMats.insert ( std::make_pair ( 13,"CRYSTAL_GLASS" ) ); - mOtherMats.insert ( std::make_pair ( 14,"YARN" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 0,"WOOD" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 1,"PLANT_CLOTH" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 2,"BONE" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 3,"TOOTH" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 4,"HORN" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 5,"PEARL" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 6,"SHELL" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 7,"LEATHER" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 8,"SILK" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 9,"AMBER" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 10,"CORAL" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 11,"GREEN_GLASS" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 12,"CLEAR_GLASS" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 13,"CRYSTAL_GLASS" ) ); + mOtherMatsFurniture.insert ( std::make_pair ( 14,"YARN" ) ); } std::string furn_other_mats ( int idx ) { - auto it = mOtherMats.find ( idx ); - if ( it == mOtherMats.end() ) + auto it = mOtherMatsFurniture.find ( idx ); + if ( it == mOtherMatsFurniture.end() ) return std::string(); return it->second; } int furn_other_mats ( const std::string & token ) { - for ( auto it = mOtherMats.begin(); it != mOtherMats.end(); ++it ) { + for ( auto it = mOtherMatsFurniture.begin(); it != mOtherMatsFurniture.end(); ++it ) { if ( it->second == token ) return it->first; } @@ -821,24 +907,30 @@ private: return true; } + void write_refuse() { StockpileSettings::RefuseSet *refuse = mBuffer.mutable_refuse(); refuse->set_fresh_raw_hide ( mPile->settings.refuse.fresh_raw_hide ); refuse->set_rotten_raw_hide ( mPile->settings.refuse.rotten_raw_hide ); // type - using df::enums::item_type::item_type; - df::enum_traits type_traits; - *mOut << "refuse type size = " << mPile->settings.refuse.type.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; - for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) { - if ( mPile->settings.refuse.type.at ( i ) ) { - const item_type type = ( item_type ) ( ( df::enum_traits::base_type ) i ); - std::string r_type ( type_traits.key_table[i+1] ); - if ( !refuse_type_is_allowed ( type ) ) continue; - refuse->add_type ( r_type ); - *mOut << "refuse type key_table[" << i+1 << "] type[" << ( int16_t ) type << "] is " << r_type <add_type ( token ); + }, mPile->settings.refuse.type ); + +// using df::enums::item_type::item_type; +// df::enum_traits type_traits; +// *mOut << "refuse type size = " << mPile->settings.refuse.type.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; +// for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) { +// if ( mPile->settings.refuse.type.at ( i ) ) { +// const item_type type = ( item_type ) ( ( df::enum_traits::base_type ) i ); +// std::string r_type ( type_traits.key_table[i+1] ); +// if ( !refuse_type_is_allowed ( type ) ) continue; +// refuse->add_type ( r_type ); +// *mOut << "refuse type key_table[" << i+1 << "] type[" << ( int16_t ) type << "] is " << r_type <add_corpses ( id ); @@ -888,6 +980,8 @@ private: } } + + void read_refuse() { if ( mBuffer.has_refuse() ) { const StockpileSettings::RefuseSet refuse = mBuffer.refuse(); @@ -896,16 +990,10 @@ private: *mOut << " rotten hide " << refuse.rotten_raw_hide() << endl; // type - using df::enums::item_type::item_type; - df::enum_traits type_traits; - for ( int i = 0; i < refuse.type_size(); ++i ) { - const std::string token = refuse.type ( i ); - // subtract one because item_type starts at -1 - const df::enum_traits::base_type idx = linear_index ( *mOut, type_traits, token ) - 1; - const item_type type = ( item_type ) idx; - if ( !refuse_type_is_allowed ( type ) ) continue; - *mOut << " type " << idx << " is " << token << endl; - } + FuncItemAllowed filter = std::bind ( &StockpileSerializer::refuse_type_is_allowed, this, _1 ); + unserialize_list_item_type ( filter, [=] ( const size_t & idx ) -> const std::string& { + return refuse.type ( idx ); + }, refuse.type_size() ); // corpses *mOut << " corpses" << endl; @@ -1133,6 +1221,344 @@ private: } } } + + void bars_blocks_setup_other_mats() { + mOtherMatsBars.insert ( std::make_pair ( 0,"COAL" ) ); + mOtherMatsBars.insert ( std::make_pair ( 1,"POTASH" ) ); + mOtherMatsBars.insert ( std::make_pair ( 2,"ASH" ) ); + mOtherMatsBars.insert ( std::make_pair ( 3,"PEARLASH" ) ); + mOtherMatsBars.insert ( std::make_pair ( 4,"SOAP" ) ); + + mOtherMatsBlocks.insert ( std::make_pair ( 0,"GREEN_GLASS" ) ); + mOtherMatsBlocks.insert ( std::make_pair ( 1,"CLEAR_GLASS" ) ); + mOtherMatsBlocks.insert ( std::make_pair ( 2,"CRYSTAL_GLASS" ) ); + mOtherMatsBlocks.insert ( std::make_pair ( 3,"WOOD" ) ); + } + + std::string bars_other_mats ( int idx ) { + auto it = mOtherMatsBars.find ( idx ); + if ( it == mOtherMatsBars.end() ) + return std::string(); + return it->second; + } + + int bars_other_mats ( const std::string & token ) { + for ( auto it = mOtherMatsBars.begin(); it != mOtherMatsBars.end(); ++it ) { + if ( it->second == token ) + return it->first; + } + return -1; + } + + std::string blocks_other_mats ( int idx ) { + auto it = mOtherMatsBlocks.find ( idx ); + if ( it == mOtherMatsBlocks.end() ) + return std::string(); + return it->second; + } + + int blocks_other_mats ( const std::string & token ) { + for ( auto it = mOtherMatsBlocks.begin(); it != mOtherMatsBlocks.end(); ++it ) { + if ( it->second == token ) + return it->first; + } + return -1; + } + + bool bars_mat_is_allowed ( const MaterialInfo &mi ) { + return mi.isValid() && mi.material && mi.material->flags.is_set ( material_flags::IS_METAL ); + } + + bool blocks_mat_is_allowed ( const MaterialInfo &mi ) { + return mi.isValid() && mi.material && !mi.material->flags.is_set ( material_flags::IS_GEM ) && mi.material->flags.is_set ( material_flags::ITEMS_HARD ); + } + + + void write_bars_blocks() { + StockpileSettings::BarsBlocksSet *bars_blocks = mBuffer.mutable_barsblocks(); + MaterialInfo mi; + // bars mats +// for ( size_t i = 0; i < mPile->settings.bars_blocks.bars_mats.size(); ++i ) { +// if ( mPile->settings.bars_blocks.bars_mats.at ( i ) ) { +// mi.decode ( 0, i ); +// if ( !bars_mat_is_allowed ( mi ) ) continue; +// *mOut << " bars mat" << i << " is " << mi.getToken() << endl; +// } +// } + + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::bars_mat_is_allowed, this, _1 ); + serialize_list_material( filter, [=] ( const std::string &token ) { + bars_blocks->add_bars_mats ( token ); + }, mPile->settings.bars_blocks.bars_mats); + + // blocks mats + filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); + serialize_list_material ( filter, [=] ( const std::string &token ) { + bars_blocks->add_blocks_mats ( token ); + }, mPile->settings.bars_blocks.blocks_mats); +// for ( size_t i = 0; i < mPile->settings.bars_blocks.blocks_mats.size(); ++i ) { +// if ( mPile->settings.bars_blocks.blocks_mats.at ( i ) ) { +// mi.decode ( 0, i ); +// if ( !blocks_mat_is_allowed ( mi ) ) continue; +// *mOut << " blocks mat" << i << " is " << mi.getToken() << endl; +// bars_blocks->add_blocks_mats ( mi.getToken() ); +// } +// } + + // bars other mats + for ( size_t i = 0; i < mPile->settings.bars_blocks.bars_other_mats.size(); ++i ) { + if ( mPile->settings.bars_blocks.bars_other_mats.at ( i ) ) { + const std::string token = bars_other_mats ( i ); + if ( token.empty() ) { + *mOut << " invalid other bar material with index " << i << endl; + continue; + } + bars_blocks->add_bars_other_mats ( token ); + *mOut << " bars other mats " << i << " is " << token << endl; + } + } + // blocks other mats + for ( size_t i = 0; i < mPile->settings.bars_blocks.blocks_other_mats.size(); ++i ) { + if ( mPile->settings.bars_blocks.blocks_other_mats.at ( i ) ) { + const std::string token = blocks_other_mats ( i ); + if ( token.empty() ) { + *mOut << " invalid other block material with index " << i << endl; + continue; + } + bars_blocks->add_blocks_other_mats ( token ); + *mOut << " blocks other mats " << i << " is " << token << endl; + } + } + + } + + void read_bars_blocks() { + if ( mBuffer.has_barsblocks() ) { + const StockpileSettings::BarsBlocksSet bars_blocks = mBuffer.barsblocks(); + *mOut << "bars_blocks: " < const std::string& { + return bars_blocks.bars_mats( idx ); + }, bars_blocks.bars_mats_size()); +// for ( int i = 0; i < bars_blocks.bars_mats_size(); ++i ) { +// const std::string token = bars_blocks.bars_mats ( i ); +// MaterialInfo mi; +// mi.find ( token ); +// if ( !bars_mat_is_allowed ( mi ) ) continue; +// *mOut << " bars mats " << mi.index << " is " << token << endl; +// } + // blocks + filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { + return bars_blocks.blocks_mats( idx ); + }, bars_blocks.blocks_mats_size()); +// for ( int i = 0; i < bars_blocks.blocks_mats_size(); ++i ) { +// const std::string token = bars_blocks.blocks_mats ( i ); +// MaterialInfo mi; +// mi.find ( token ); +// if ( !blocks_mat_is_allowed ( mi ) ) continue; +// *mOut << " blocks mats " << mi.index << " is " << token << endl; +// } + // bars other mats + for ( int i = 0; i < bars_blocks.bars_other_mats_size(); ++i ) { + const std::string token = bars_blocks.bars_other_mats ( i ); + int16_t idx = bars_other_mats ( token ); + if ( idx < 0 ) { + *mOut << "invalid other bars mat with token " << token; + continue; + } + *mOut << " bars other mats " << idx << " is " << token << endl; + } + // blocks other mats + for ( int i = 0; i < bars_blocks.blocks_other_mats_size(); ++i ) { + const std::string token = bars_blocks.blocks_other_mats ( i ); + int16_t idx = blocks_other_mats ( token ); + if ( idx < 0 ) { + *mOut << "invalid other blocks mat with token " << token; + continue; + } + *mOut << " blocks other mats " << idx << " is " << token << endl; + } + } + } + + bool gem_mat_is_allowed ( const MaterialInfo &mi ) { + return mi.isValid() && mi.material && mi.material->flags.is_set ( material_flags::IS_GEM ); + } + bool gem_cut_mat_is_allowed ( const MaterialInfo &mi ) { + return mi.isValid() && mi.material && ( mi.material->flags.is_set ( material_flags::IS_GEM ) || mi.material->flags.is_set ( material_flags::IS_STONE ) ) ; + } + + void write_gems() { + StockpileSettings::GemsSet *gems = mBuffer.mutable_gems(); + MaterialInfo mi; + // rough mats + for ( size_t i = 0; i < mPile->settings.gems.rough_mats.size(); ++i ) { + if ( mPile->settings.gems.rough_mats.at ( i ) ) { + mi.decode ( 0, i ); + if ( !gem_mat_is_allowed ( mi ) ) continue; + *mOut << " gem rough mat" << i << " is " << mi.getToken() << endl; + gems->add_rough_mats ( mi.getToken() ); + } + } + // cut mats + for ( size_t i = 0; i < mPile->settings.gems.cut_mats.size(); ++i ) { + if ( mPile->settings.gems.cut_mats.at ( i ) ) { + mi.decode ( 0, i ); + if ( !gem_cut_mat_is_allowed ( mi ) ) continue; + *mOut << " gem cut mat" << i << " is " << mi.getToken() << endl; + gems->add_cut_mats ( mi.getToken() ); + } + } + // rough other + for ( size_t i = 0; i < mPile->settings.gems.rough_other_mats.size(); ++i ) { + if ( mPile->settings.gems.rough_other_mats.at ( i ) ) { + mi.decode ( i, -1 ); + if ( !mi.isValid() ) continue; + *mOut << " gem rough_other mat" << i << " is " << mi.getToken() << endl; + gems->add_rough_other_mats ( mi.getToken() ); + } + } + // cut other + for ( size_t i = 0; i < mPile->settings.gems.cut_other_mats.size(); ++i ) { + if ( mPile->settings.gems.cut_other_mats.at ( i ) ) { + mi.decode ( i, -1 ); + if ( !mi.isValid() ) mi.decode ( 0, i ); + if ( !mi.isValid() ) continue; + *mOut << " gem cut_other mat" << i << " is " << mi.getToken() << endl; + gems->add_cut_other_mats ( mi.getToken() ); + } + } + } + void read_gems() { + if ( mBuffer.has_gems() ) { + const StockpileSettings::GemsSet gems = mBuffer.gems(); + *mOut << "gems: " <add_type ( token ); + }, mPile->settings.finished_goods.type ); + + // materials + FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); + serialize_list_material ( mat_filter, [=] ( const std::string &token ) { + finished_goods->add_mats ( token ); + }, mPile->settings.finished_goods.mats ); + + } + + void read_finished_goods() { + if ( mBuffer.has_finished_goods() ) { + const StockpileSettings::FinishedGoodsSet finished_goods = mBuffer.finished_goods(); + *mOut << "finished_goods: " < const std::string& { + return finished_goods.type ( idx ); + }, finished_goods.type_size() ); + + // materials + FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); + unserialize_list_material( mat_filter, [=] ( const size_t & idx ) -> const std::string& { + return finished_goods.mats( idx ); + }, finished_goods.mats_size() ); + + } + } + + void write_leather() {} + void read_leather() {} + + void write_cloth() {} + void read_cloth() {} + + void write_wood() {} + void read_wood() {} + + void write_weapons() {} + void read_weapons() {} + + void write_armor() {} + void read_armor() {} }; static command_result savestock ( color_ostream &out, vector & parameters ) From d74ac1b3a65f6196a1f86221c246c5636106a38c Mon Sep 17 00:00:00 2001 From: Casey Link Date: Wed, 19 Nov 2014 17:46:52 +0100 Subject: [PATCH 06/36] stockpiles: serialize through leather * more code reuse! --- plugins/proto/stockpiles.proto | 4 +- plugins/stockpiles.cpp | 533 +++++++++++++++++---------------- 2 files changed, 283 insertions(+), 254 deletions(-) diff --git a/plugins/proto/stockpiles.proto b/plugins/proto/stockpiles.proto index 7be0edb13..e99608e38 100644 --- a/plugins/proto/stockpiles.proto +++ b/plugins/proto/stockpiles.proto @@ -86,8 +86,8 @@ message StockpileSettings { repeated string type = 1; repeated string other_mats = 2; repeated string mats = 3; - optional string quality_core = 4; - optional string quality_total = 5; + repeated string quality_core = 4; + repeated string quality_total = 5; } message LeatherSet { repeated string mats = 1; diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 583d4048a..9f2f313b9 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -294,8 +294,10 @@ public: StockpileSerializer ( color_ostream &out, building_stockpilest const * stockpile ) : mOut ( &out ) , mPile ( stockpile ) { + // build other mats indices furniture_setup_other_mats(); bars_blocks_setup_other_mats(); + finished_goods_setup_other_mats(); } StockpileSettings write() { // *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; @@ -363,7 +365,8 @@ private: color_ostream * mOut; building_stockpilest const * mPile; StockpileSettings mBuffer; - std::map mOtherMatsFurniture;; + std::map mOtherMatsFurniture; + std::map mOtherMatsFinishedGoods; std::map mOtherMatsBars; std::map mOtherMatsBlocks; /** @@ -389,14 +392,15 @@ private: return -1; } - // read the token from the serailized list during import typedef std::function FuncReadImport; // add the token to the serialized list during export typedef std::function FuncWriteExport; // are item's of item_type allowed? typedef std::function FuncItemAllowed; + // is this material allowed? typedef std::function FuncMaterialAllowed; + struct food_pair { // exporting FuncWriteExport set_value; @@ -463,6 +467,71 @@ private: } } + + void serialize_list_quality ( FuncWriteExport add_value, const bool (&quality_list)[7] ) { + using df::enums::item_quality::item_quality; + df::enum_traits quality_traits; + for ( size_t i = 0; i < 7; ++i ) { + if ( quality_list[i] ) { + const std::string f_type ( quality_traits.key_table[i] ); + add_value( f_type ); + *mOut << " quality: " << i << " is " << f_type < quality_traits; + for ( int i = 0; i < list_size; ++i ) { + const std::string quality = read_value( i ); + df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); + if ( idx < 0 ) { + *mOut << " invalid quality token " << quality << endl; + continue; + } + *mOut << " quality: " << idx << " is " << quality << endl; + } + } + + void serialize_list_other_mats(const std::map other_mats, FuncWriteExport add_value, std::vector list) { + for ( size_t i = 0; i < list.size(); ++i ) { + if ( list.at ( i ) ) { + const std::string token = other_mats_index(other_mats, i); + if ( token.empty() ) { + *mOut << " invalid other material with index " << i << endl; + continue; + } + add_value(token); + *mOut << " other mats " << i << " is " << token << endl; + } + } + } + void unserialize_list_other_mats(const std::map other_mats, FuncReadImport read_value, int32_t list_size ) { + for ( int i = 0; i < list_size; ++i ) { + const std::string token = read_value( i ); + size_t idx = other_mats_token( other_mats, token ); + if ( idx < 0 ) { + *mOut << "invalid other mat with token " << token; + continue; + } + *mOut << " other_mats " << idx << " is " << token << endl; + } + } + + std::string other_mats_index( const std::map other_mats, int idx ) { + auto it = other_mats.find ( idx ); + if ( it == other_mats.end() ) + return std::string(); + return it->second; + } + int other_mats_token( const std::map other_mats, const std::string & token) { + for ( auto it = other_mats.begin(); it != other_mats.end(); ++it ) { + if ( it->second == token ) + return it->first; + } + return -1; + } + void write_general() { mBuffer.set_max_bins ( mPile->max_barrels ); mBuffer.set_max_wheelbarrows ( mPile->max_wheelbarrows ); @@ -680,8 +749,6 @@ private: void write_food() { StockpileSettings::FoodSet *food = mBuffer.mutable_food(); food->set_prepared_meals ( mPile->settings.food.prepared_meals ); - food_pair p = food_map ( organic_mat_category::Meat ); - food_write_helper ( p.set_value, p.stockpile_values, organic_mat_category::Meat ); using df::enums::organic_mat_category::organic_mat_category; df::enum_traits traits; @@ -733,7 +800,7 @@ private: mOtherMatsFurniture.insert ( std::make_pair ( 14,"YARN" ) ); } - std::string furn_other_mats ( int idx ) { + std::string furn_other_mats_index ( int idx ) { auto it = mOtherMatsFurniture.find ( idx ); if ( it == mOtherMatsFurniture.end() ) return std::string(); @@ -773,38 +840,27 @@ private: } } // other mats - for ( size_t i = 0; i < mPile->settings.furniture.other_mats.size(); ++i ) { - if ( mPile->settings.furniture.other_mats.at ( i ) ) { - const std::string token = furn_other_mats ( i ); - if ( token.empty() ) { - *mOut << " invalid other material with index " << i << endl; - continue; - } - furniture->add_other_mats ( token ); - *mOut << " other mats " << i << " is " << token << endl; - } - } - // quality core - using df::enums::item_quality::item_quality; - df::enum_traits quality_traits; - size_t core_size = std::extent< decltype ( mPile->settings.furniture.quality_core ) >::value; - for ( size_t i = 0; i < core_size; ++i ) { - if ( mPile->settings.furniture.quality_core[i] ) { - const std::string f_type ( quality_traits.key_table[i] ); - furniture->add_quality_core ( f_type ); - *mOut << "quality_core " << i << " is " << f_type <settings.furniture.quality_total ) >::value; - for ( size_t i = 0; i < total_size; ++i ) { - if ( mPile->settings.furniture.quality_total[i] ) { - const std::string f_type ( quality_traits.key_table[i] ); - furniture->add_quality_total ( f_type ); - *mOut << "quality_total " << i << " is " << f_type <add_other_mats( token ); + }, mPile->settings.furniture.other_mats ); + +// for ( size_t i = 0; i < mPile->settings.furniture.other_mats.size(); ++i ) { +// if ( mPile->settings.furniture.other_mats.at ( i ) ) { +// const std::string token = furn_other_mats ( i ); +// if ( token.empty() ) { +// *mOut << " invalid other material with index " << i << endl; +// continue; +// } +// furniture->add_other_mats ( token ); +// *mOut << " other mats " << i << " is " << token << endl; +// } +// } + serialize_list_quality([=] ( const std::string &token ) { + furniture->add_quality_core ( token ); + }, mPile->settings.furniture.quality_core); + serialize_list_quality([=] ( const std::string &token ) { + furniture->add_quality_total( token ); + }, mPile->settings.furniture.quality_total); } /* skip gems and non hard things @@ -837,37 +893,19 @@ private: *mOut << " mats " << mi.index << " is " << token << endl; } // other materials - for ( int i = 0; i < furniture.other_mats_size(); ++i ) { - const std::string token = furniture.other_mats ( i ); - int16_t idx = furn_other_mats ( token ); - if ( idx < 0 ) { - *mOut << "invalid other mat with token " << token; - continue; - } - *mOut << " other_mats" << idx << " is " << token << endl; - } + unserialize_list_other_mats( mOtherMatsFurniture, [=] ( const size_t & idx ) -> const std::string& { + return furniture.other_mats( idx ); + }, furniture.other_mats_size() ); + // core quality - using df::enums::item_quality::item_quality; - df::enum_traits quality_traits; - for ( int i = 0; i < furniture.quality_core_size(); ++i ) { - const std::string quality = furniture.quality_core ( i ); - df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); - if ( idx < 0 ) { - *mOut << " invalid quality core token " << quality << endl; - continue; - } - *mOut << " quality_core" << idx << " is " << quality << endl; - } + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return furniture.quality_core ( idx ); + }, furniture.quality_core_size() ); + // total quality - for ( int i = 0; i < furniture.quality_total_size(); ++i ) { - const std::string quality = furniture.quality_total ( i ); - df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); - if ( idx < 0 ) { - *mOut << " invalid quality total token " << quality << endl; - continue; - } - *mOut << " quality_total" << idx << " is " << quality << endl; - } + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return furniture.quality_total ( idx ); + }, furniture.quality_total_size() ); } } @@ -1116,28 +1154,13 @@ private: } *mOut << "after other" << endl; // quality core - using df::enums::item_quality::item_quality; - df::enum_traits quality_traits; - size_t core_size = std::extent< decltype ( mPile->settings.ammo.quality_core ) >::value; - for ( size_t i = 0; i < core_size; ++i ) { - if ( mPile->settings.ammo.quality_core[i] ) { - const std::string f_type ( quality_traits.key_table[i] ); - ammo->add_quality_core ( f_type ); - *mOut << "quality_core " << i << " is " << f_type <add_quality_core ( token ); + }, mPile->settings.ammo.quality_core ); // quality total - size_t total_size = std::extent< decltype ( mPile->settings.ammo.quality_total ) >::value; - for ( size_t i = 0; i < total_size; ++i ) { - if ( mPile->settings.ammo.quality_total[i] ) { - const std::string f_type ( quality_traits.key_table[i] ); - ammo->add_quality_total ( f_type ); - *mOut << "quality_total " << i << " is " << f_type <add_quality_total( token ); + }, mPile->settings.ammo.quality_total); } void read_ammo() { @@ -1166,27 +1189,13 @@ private: *mOut << " other mats " << idx << " is " << token << endl; } // core quality - using df::enums::item_quality::item_quality; - df::enum_traits quality_traits; - for ( int i = 0; i < ammo.quality_core_size(); ++i ) { - const std::string quality = ammo.quality_core ( i ); - df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); - if ( idx < 0 ) { - *mOut << " invalid quality core token " << quality << endl; - continue; - } - *mOut << " quality_core" << idx << " is " << quality << endl; - } + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return ammo.quality_core ( idx ); + }, ammo.quality_core_size() ); // total quality - for ( int i = 0; i < ammo.quality_total_size(); ++i ) { - const std::string quality = ammo.quality_total ( i ); - df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); - if ( idx < 0 ) { - *mOut << " invalid quality total token " << quality << endl; - continue; - } - *mOut << " quality_total" << idx << " is " << quality << endl; - } + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return ammo.quality_total ( idx ); + }, ammo.quality_total_size() ); } } @@ -1235,36 +1244,6 @@ private: mOtherMatsBlocks.insert ( std::make_pair ( 3,"WOOD" ) ); } - std::string bars_other_mats ( int idx ) { - auto it = mOtherMatsBars.find ( idx ); - if ( it == mOtherMatsBars.end() ) - return std::string(); - return it->second; - } - - int bars_other_mats ( const std::string & token ) { - for ( auto it = mOtherMatsBars.begin(); it != mOtherMatsBars.end(); ++it ) { - if ( it->second == token ) - return it->first; - } - return -1; - } - - std::string blocks_other_mats ( int idx ) { - auto it = mOtherMatsBlocks.find ( idx ); - if ( it == mOtherMatsBlocks.end() ) - return std::string(); - return it->second; - } - - int blocks_other_mats ( const std::string & token ) { - for ( auto it = mOtherMatsBlocks.begin(); it != mOtherMatsBlocks.end(); ++it ) { - if ( it->second == token ) - return it->first; - } - return -1; - } - bool bars_mat_is_allowed ( const MaterialInfo &mi ) { return mi.isValid() && mi.material && mi.material->flags.is_set ( material_flags::IS_METAL ); } @@ -1277,59 +1256,26 @@ private: void write_bars_blocks() { StockpileSettings::BarsBlocksSet *bars_blocks = mBuffer.mutable_barsblocks(); MaterialInfo mi; - // bars mats -// for ( size_t i = 0; i < mPile->settings.bars_blocks.bars_mats.size(); ++i ) { -// if ( mPile->settings.bars_blocks.bars_mats.at ( i ) ) { -// mi.decode ( 0, i ); -// if ( !bars_mat_is_allowed ( mi ) ) continue; -// *mOut << " bars mat" << i << " is " << mi.getToken() << endl; -// } -// } - - FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::bars_mat_is_allowed, this, _1 ); + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::bars_mat_is_allowed, this, _1 ); serialize_list_material( filter, [=] ( const std::string &token ) { - bars_blocks->add_bars_mats ( token ); + bars_blocks->add_bars_mats ( token ); }, mPile->settings.bars_blocks.bars_mats); // blocks mats - filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); + filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); serialize_list_material ( filter, [=] ( const std::string &token ) { bars_blocks->add_blocks_mats ( token ); }, mPile->settings.bars_blocks.blocks_mats); -// for ( size_t i = 0; i < mPile->settings.bars_blocks.blocks_mats.size(); ++i ) { -// if ( mPile->settings.bars_blocks.blocks_mats.at ( i ) ) { -// mi.decode ( 0, i ); -// if ( !blocks_mat_is_allowed ( mi ) ) continue; -// *mOut << " blocks mat" << i << " is " << mi.getToken() << endl; -// bars_blocks->add_blocks_mats ( mi.getToken() ); -// } -// } // bars other mats - for ( size_t i = 0; i < mPile->settings.bars_blocks.bars_other_mats.size(); ++i ) { - if ( mPile->settings.bars_blocks.bars_other_mats.at ( i ) ) { - const std::string token = bars_other_mats ( i ); - if ( token.empty() ) { - *mOut << " invalid other bar material with index " << i << endl; - continue; - } - bars_blocks->add_bars_other_mats ( token ); - *mOut << " bars other mats " << i << " is " << token << endl; - } - } - // blocks other mats - for ( size_t i = 0; i < mPile->settings.bars_blocks.blocks_other_mats.size(); ++i ) { - if ( mPile->settings.bars_blocks.blocks_other_mats.at ( i ) ) { - const std::string token = blocks_other_mats ( i ); - if ( token.empty() ) { - *mOut << " invalid other block material with index " << i << endl; - continue; - } - bars_blocks->add_blocks_other_mats ( token ); - *mOut << " blocks other mats " << i << " is " << token << endl; - } - } + serialize_list_other_mats(mOtherMatsBars, [=] ( const std::string &token ) { + bars_blocks->add_bars_other_mats( token ); + }, mPile->settings.bars_blocks.bars_other_mats ); + // blocks other mats + serialize_list_other_mats(mOtherMatsBlocks, [=] ( const std::string &token ) { + bars_blocks->add_blocks_other_mats( token ); + }, mPile->settings.bars_blocks.blocks_other_mats ); } void read_bars_blocks() { @@ -1341,45 +1287,23 @@ private: unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { return bars_blocks.bars_mats( idx ); }, bars_blocks.bars_mats_size()); -// for ( int i = 0; i < bars_blocks.bars_mats_size(); ++i ) { -// const std::string token = bars_blocks.bars_mats ( i ); -// MaterialInfo mi; -// mi.find ( token ); -// if ( !bars_mat_is_allowed ( mi ) ) continue; -// *mOut << " bars mats " << mi.index << " is " << token << endl; -// } + // blocks filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { return bars_blocks.blocks_mats( idx ); }, bars_blocks.blocks_mats_size()); -// for ( int i = 0; i < bars_blocks.blocks_mats_size(); ++i ) { -// const std::string token = bars_blocks.blocks_mats ( i ); -// MaterialInfo mi; -// mi.find ( token ); -// if ( !blocks_mat_is_allowed ( mi ) ) continue; -// *mOut << " blocks mats " << mi.index << " is " << token << endl; -// } // bars other mats - for ( int i = 0; i < bars_blocks.bars_other_mats_size(); ++i ) { - const std::string token = bars_blocks.bars_other_mats ( i ); - int16_t idx = bars_other_mats ( token ); - if ( idx < 0 ) { - *mOut << "invalid other bars mat with token " << token; - continue; - } - *mOut << " bars other mats " << idx << " is " << token << endl; - } + unserialize_list_other_mats( mOtherMatsBars, [=] ( const size_t & idx ) -> const std::string& { + return bars_blocks.bars_other_mats ( idx ); + }, bars_blocks.bars_other_mats_size() ); + + // blocks other mats - for ( int i = 0; i < bars_blocks.blocks_other_mats_size(); ++i ) { - const std::string token = bars_blocks.blocks_other_mats ( i ); - int16_t idx = blocks_other_mats ( token ); - if ( idx < 0 ) { - *mOut << "invalid other blocks mat with token " << token; - continue; - } - *mOut << " blocks other mats " << idx << " is " << token << endl; - } + unserialize_list_other_mats( mOtherMatsBlocks, [=] ( const size_t & idx ) -> const std::string& { + return bars_blocks.blocks_other_mats ( idx ); + }, bars_blocks.blocks_other_mats_size() ); + } } @@ -1394,23 +1318,15 @@ private: StockpileSettings::GemsSet *gems = mBuffer.mutable_gems(); MaterialInfo mi; // rough mats - for ( size_t i = 0; i < mPile->settings.gems.rough_mats.size(); ++i ) { - if ( mPile->settings.gems.rough_mats.at ( i ) ) { - mi.decode ( 0, i ); - if ( !gem_mat_is_allowed ( mi ) ) continue; - *mOut << " gem rough mat" << i << " is " << mi.getToken() << endl; - gems->add_rough_mats ( mi.getToken() ); - } - } + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::gem_mat_is_allowed, this, _1 ); + serialize_list_material ( filter, [=] ( const std::string &token ) { + gems->add_rough_mats( token ); + }, mPile->settings.gems.rough_mats ); // cut mats - for ( size_t i = 0; i < mPile->settings.gems.cut_mats.size(); ++i ) { - if ( mPile->settings.gems.cut_mats.at ( i ) ) { - mi.decode ( 0, i ); - if ( !gem_cut_mat_is_allowed ( mi ) ) continue; - *mOut << " gem cut mat" << i << " is " << mi.getToken() << endl; - gems->add_cut_mats ( mi.getToken() ); - } - } + filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); + serialize_list_material ( filter, [=] ( const std::string &token ) { + gems->add_cut_mats( token ); + }, mPile->settings.gems.cut_mats); // rough other for ( size_t i = 0; i < mPile->settings.gems.rough_other_mats.size(); ++i ) { if ( mPile->settings.gems.rough_other_mats.at ( i ) ) { @@ -1436,21 +1352,17 @@ private: const StockpileSettings::GemsSet gems = mBuffer.gems(); *mOut << "gems: " < const std::string& { + return gems.rough_mats ( idx ); + }, gems.rough_mats_size() ); + // cut - for ( int i = 0; i < gems.cut_mats_size(); ++i ) { - const std::string token = gems.cut_mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !gem_cut_mat_is_allowed ( mi ) ) continue; - *mOut << " cut mats " << mi.index << " is " << token << endl; - } + filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { + return gems.cut_mats( idx ); + }, gems.cut_mats_size() ); + // rough other for ( int i = 0; i < gems.rough_other_mats_size(); ++i ) { const std::string token = gems.rough_other_mats ( i ); @@ -1504,8 +1416,32 @@ private: } - bool finished_goods_mat_is_allowed(const MaterialInfo &mat) { - return mat.isValid(); + void finished_goods_setup_other_mats() { + mOtherMatsFinishedGoods.insert ( std::make_pair ( 0,"WOOD" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 1,"PLANT_CLOTH" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 2,"BONE" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 3,"TOOTH" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 4,"HORN" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 5,"PEARL" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 6,"SHELL" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 7,"LEATHER" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 8,"SILK" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 9,"AMBER" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 10,"CORAL" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 11,"GREEN_GLASS" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 12,"CLEAR_GLASS" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 13,"CRYSTAL_GLASS" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 14,"YARN" ) ); + mOtherMatsFinishedGoods.insert ( std::make_pair ( 15,"WAX" ) ); + } + + + bool finished_goods_mat_is_allowed(const MaterialInfo &mi) { + return mi.isValid() + && mi.material + && ( mi.material->flags.is_set ( material_flags::IS_GEM ) + || mi.material->flags.is_set ( material_flags::IS_METAL ) + || mi.material->flags.is_set ( material_flags::IS_STONE ) ) ; } void write_finished_goods() { @@ -1518,11 +1454,24 @@ private: }, mPile->settings.finished_goods.type ); // materials - FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); + FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); serialize_list_material ( mat_filter, [=] ( const std::string &token ) { finished_goods->add_mats ( token ); }, mPile->settings.finished_goods.mats ); + // other mats + serialize_list_other_mats(mOtherMatsFinishedGoods, [=] ( const std::string &token ) { + finished_goods->add_other_mats( token ); + }, mPile->settings.finished_goods.other_mats ); + + // quality core + serialize_list_quality ( [=] ( const std::string &token ) { + finished_goods->add_quality_core ( token ); + }, mPile->settings.finished_goods.quality_core ); + // quality total + serialize_list_quality ( [=] ( const std::string &token ) { + finished_goods->add_quality_total ( token ); + }, mPile->settings.finished_goods.quality_total ); } void read_finished_goods() { @@ -1537,16 +1486,47 @@ private: }, finished_goods.type_size() ); // materials - FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); - unserialize_list_material( mat_filter, [=] ( const size_t & idx ) -> const std::string& { - return finished_goods.mats( idx ); + FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); + unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { + return finished_goods.mats ( idx ); }, finished_goods.mats_size() ); + // other mats + unserialize_list_other_mats( mOtherMatsFinishedGoods, [=] ( const size_t & idx ) -> const std::string& { + return finished_goods.other_mats( idx ); + }, finished_goods.other_mats_size() ); + + // core quality + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return finished_goods.quality_core ( idx ); + }, finished_goods.quality_core_size() ); + + // total quality + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return finished_goods.quality_total ( idx ); + }, finished_goods.quality_total_size() ); + } } - void write_leather() {} - void read_leather() {} + void write_leather() { + StockpileSettings::LeatherSet *leather = mBuffer.mutable_leather(); + FuncWriteExport setter = [=] ( const std::string &id ) { + leather->add_mats( id ); + }; + + food_write_helper ( setter, mPile->settings.leather.mats, organic_mat_category::Leather ); + } + void read_leather() { + if ( mBuffer.has_leather() ) { + const StockpileSettings::LeatherSet leather = mBuffer.leather(); + *mOut << "leather: " < std::string { + return leather.mats( idx ); + }; + food_read_helper ( getter, leather.mats_size(), organic_mat_category::Leather ); + } + } void write_cloth() {} void read_cloth() {} @@ -1554,11 +1534,60 @@ private: void write_wood() {} void read_wood() {} + bool weapons_mat_is_allowed(const MaterialInfo &mi) { + return mi.isValid(); + } + void write_weapons() {} - void read_weapons() {} + void read_weapons() { + if ( mBuffer.has_weapons() ) { + const StockpileSettings::WeaponsSet weapons = mBuffer.weapons(); + *mOut << "weapons: " < const std::string& { + return weapons.mats ( idx ); + }, weapons.mats_size() ); + + // core quality + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return weapons.quality_core ( idx ); + }, weapons.quality_core_size() ); + // total quality + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return weapons.quality_total ( idx ); + }, weapons.quality_total_size() ); + } + } + + bool armor_mat_is_allowed(const MaterialInfo &mi) { + return mi.isValid(); + } void write_armor() {} - void read_armor() {} + void read_armor() { + if ( mBuffer.has_armor() ) { + const StockpileSettings::ArmorSet armor = mBuffer.armor(); + *mOut << "armor: " < const std::string& { + return armor.mats ( idx ); + }, armor.mats_size() ); + + // core quality + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return armor.quality_core ( idx ); + }, armor.quality_core_size() ); + // total quality + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + return armor.quality_total ( idx ); + }, armor.quality_total_size() ); + } + + } }; static command_result savestock ( color_ostream &out, vector & parameters ) From 25f6c440d806bf53c8b209ac3f871d8deab40964 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Wed, 19 Nov 2014 18:38:03 +0100 Subject: [PATCH 07/36] 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() {} From 224a19295d06e3f1bee94f49f8b0f2a01412e2e0 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Wed, 19 Nov 2014 19:53:53 +0100 Subject: [PATCH 08/36] stockpiles: serialize through weapons+trap components * generalize itemdefs --- plugins/stockpiles.cpp | 378 ++++++++++++++++++++++++++++++----------- 1 file changed, 281 insertions(+), 97 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 7ff54f23a..4f6bddc9f 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -28,6 +28,15 @@ #include "df/global_objects.h" #include "df/viewscreen_dwarfmodest.h" #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "stockpiles.pb.h" @@ -176,6 +185,21 @@ static int16_t find_creature ( const std::string &creature_id ) return linear_index ( world->raws.creatures.all, &df::creature_raw::creature_id, creature_id ); } +/** + * Retrieve plant raw from index + */ +static df::plant_raw* find_plant( size_t idx ) { + return world->raws.plants.all[idx]; +} + +/** + * Retrieve plant index from id string + */ +static size_t find_plant( const std::string &plant_id) +{ + return linear_index ( world->raws.plants.all, &df::plant_raw::id, plant_id); +} + typedef std::pair FoodMatPair; typedef std::map FoodMatMap; @@ -299,7 +323,9 @@ public: furniture_setup_other_mats(); bars_blocks_setup_other_mats(); finished_goods_setup_other_mats(); + weapons_armor_setup_other_mats(); } + StockpileSettings write() { // *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; write_general(); @@ -370,6 +396,7 @@ private: std::map mOtherMatsFinishedGoods; std::map mOtherMatsBars; std::map mOtherMatsBlocks; + std::map mOtherMatsWeaponsArmor; /** * Find an enum's value based off the string label. * @param traits the enum's trait struct @@ -543,6 +570,30 @@ private: } } + void serialize_list_itemdef ( FuncWriteExport add_value, std::vector list, std::vector items, item_type::item_type type ) { + for ( size_t i = 0; i < list.size(); ++i ) { + if ( list.at ( i ) ) { + const df::itemdef *a = items.at ( i ); + // skip procedurally generated items + if ( a->base_flags.is_set ( 0 ) ) continue; + ItemTypeInfo ii; + ii.decode ( type, i ); + if ( !ii.isValid() ) continue; + add_value ( ii.getToken() ); + *mOut << " itemdef type" << i << " is " << ii.getToken() << endl; + } + } + } + + void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size ) { + for ( int i = 0; i < list_size; ++i ) { + std::string token = read_value( i ); + ItemTypeInfo ii; + if ( !ii.find ( token ) ) continue; + *mOut << " itemdef " << ii.subtype << " is " << token << endl; + } + } + std::string other_mats_index( const std::map other_mats, int idx ) { auto it = other_mats.find ( idx ); if ( it == other_mats.end() ) @@ -1122,19 +1173,13 @@ private: void write_ammo() { StockpileSettings::AmmoSet *ammo = mBuffer.mutable_ammo(); - // type - for ( size_t i = 0; i < mPile->settings.ammo.type.size(); ++i ) { - if ( mPile->settings.ammo.type.at ( i ) ) { - const df::itemdef_ammost *a = world->raws.itemdefs.ammo.at ( i ); - // skip procedurally generated ammo - if ( a->base_flags.is_set ( 0 ) ) continue; - ItemTypeInfo ii; - ii.decode ( item_type::AMMO, i ); - if ( !ii.isValid() ) continue; - ammo->add_type ( ii.getToken() ); - *mOut << " " << i << " is " << ii.getToken() << endl; - } - } + // ammo type + serialize_list_itemdef ( [=] ( const std::string &token ) { + ammo->add_type ( token ); + }, mPile->settings.ammo.type, + std::vector ( world->raws.itemdefs.ammo.begin(),world->raws.itemdefs.ammo.end() ), + item_type::AMMO ); + // metal MaterialInfo mi; for ( size_t i = 0; i < mPile->settings.ammo.mats.size(); ++i ) { @@ -1172,12 +1217,12 @@ private: if ( mBuffer.has_ammo() ) { const StockpileSettings::AmmoSet ammo = mBuffer.ammo(); *mOut << "ammo: " < const std::string& { + return ammo.type ( idx ); + }, ammo.type_size() ); + // metals for ( int i = 0; i < ammo.mats_size(); ++i ) { const std::string token = ammo.mats ( i ); @@ -1194,7 +1239,7 @@ private: *mOut << " other mats " << idx << " is " << token << endl; } // core quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return ammo.quality_core ( idx ); }, ammo.quality_core_size() ); // total quality @@ -1262,24 +1307,24 @@ private: StockpileSettings::BarsBlocksSet *bars_blocks = mBuffer.mutable_barsblocks(); MaterialInfo mi; FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::bars_mat_is_allowed, this, _1 ); - serialize_list_material( filter, [=] ( const std::string &token ) { + serialize_list_material ( filter, [=] ( const std::string &token ) { bars_blocks->add_bars_mats ( token ); - }, mPile->settings.bars_blocks.bars_mats); + }, mPile->settings.bars_blocks.bars_mats ); // blocks mats filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); serialize_list_material ( filter, [=] ( const std::string &token ) { bars_blocks->add_blocks_mats ( token ); - }, mPile->settings.bars_blocks.blocks_mats); + }, mPile->settings.bars_blocks.blocks_mats ); // bars other mats - serialize_list_other_mats(mOtherMatsBars, [=] ( const std::string &token ) { - bars_blocks->add_bars_other_mats( token ); + serialize_list_other_mats ( mOtherMatsBars, [=] ( const std::string &token ) { + bars_blocks->add_bars_other_mats ( token ); }, mPile->settings.bars_blocks.bars_other_mats ); // blocks other mats - serialize_list_other_mats(mOtherMatsBlocks, [=] ( const std::string &token ) { - bars_blocks->add_blocks_other_mats( token ); + serialize_list_other_mats ( mOtherMatsBlocks, [=] ( const std::string &token ) { + bars_blocks->add_blocks_other_mats ( token ); }, mPile->settings.bars_blocks.blocks_other_mats ); } @@ -1290,22 +1335,22 @@ private: // bars FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::bars_mat_is_allowed, this, _1 ); unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { - return bars_blocks.bars_mats( idx ); - }, bars_blocks.bars_mats_size()); + return bars_blocks.bars_mats ( idx ); + }, bars_blocks.bars_mats_size() ); // blocks filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { - return bars_blocks.blocks_mats( idx ); - }, bars_blocks.blocks_mats_size()); + return bars_blocks.blocks_mats ( idx ); + }, bars_blocks.blocks_mats_size() ); // bars other mats - unserialize_list_other_mats( mOtherMatsBars, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsBars, [=] ( const size_t & idx ) -> const std::string& { return bars_blocks.bars_other_mats ( idx ); }, bars_blocks.bars_other_mats_size() ); // blocks other mats - unserialize_list_other_mats( mOtherMatsBlocks, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsBlocks, [=] ( const size_t & idx ) -> const std::string& { return bars_blocks.blocks_other_mats ( idx ); }, bars_blocks.blocks_other_mats_size() ); @@ -1323,15 +1368,15 @@ private: StockpileSettings::GemsSet *gems = mBuffer.mutable_gems(); MaterialInfo mi; // rough mats - FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::gem_mat_is_allowed, this, _1 ); + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::gem_mat_is_allowed, this, _1 ); serialize_list_material ( filter, [=] ( const std::string &token ) { - gems->add_rough_mats( token ); + gems->add_rough_mats ( token ); }, mPile->settings.gems.rough_mats ); // cut mats - filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); + filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); serialize_list_material ( filter, [=] ( const std::string &token ) { - gems->add_cut_mats( token ); - }, mPile->settings.gems.cut_mats); + gems->add_cut_mats ( token ); + }, mPile->settings.gems.cut_mats ); // rough other for ( size_t i = 0; i < mPile->settings.gems.rough_other_mats.size(); ++i ) { if ( mPile->settings.gems.rough_other_mats.at ( i ) ) { @@ -1365,7 +1410,7 @@ private: // cut filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { - return gems.cut_mats( idx ); + return gems.cut_mats ( idx ); }, gems.cut_mats_size() ); // rough other @@ -1440,8 +1485,7 @@ private: mOtherMatsFinishedGoods.insert ( std::make_pair ( 15,"WAX" ) ); } - - bool finished_goods_mat_is_allowed(const MaterialInfo &mi) { + bool finished_goods_mat_is_allowed ( const MaterialInfo &mi ) { return mi.isValid() && mi.material && ( mi.material->flags.is_set ( material_flags::IS_GEM ) @@ -1465,8 +1509,8 @@ private: }, mPile->settings.finished_goods.mats ); // other mats - serialize_list_other_mats(mOtherMatsFinishedGoods, [=] ( const std::string &token ) { - finished_goods->add_other_mats( token ); + serialize_list_other_mats ( mOtherMatsFinishedGoods, [=] ( const std::string &token ) { + finished_goods->add_other_mats ( token ); }, mPile->settings.finished_goods.other_mats ); // quality core @@ -1497,8 +1541,8 @@ private: }, finished_goods.mats_size() ); // other mats - unserialize_list_other_mats( mOtherMatsFinishedGoods, [=] ( const size_t & idx ) -> const std::string& { - return finished_goods.other_mats( idx ); + unserialize_list_other_mats ( mOtherMatsFinishedGoods, [=] ( const size_t & idx ) -> const std::string& { + return finished_goods.other_mats ( idx ); }, finished_goods.other_mats_size() ); // core quality @@ -1518,7 +1562,7 @@ private: StockpileSettings::LeatherSet *leather = mBuffer.mutable_leather(); FuncWriteExport setter = [=] ( const std::string &id ) { - leather->add_mats( id ); + leather->add_mats ( id ); }; serialize_list_organic_mat ( setter, mPile->settings.leather.mats, organic_mat_category::Leather ); } @@ -1527,7 +1571,7 @@ private: const StockpileSettings::LeatherSet leather = mBuffer.leather(); *mOut << "leather: " < std::string { - return leather.mats( idx ); + return leather.mats ( idx ); }; unserialize_list_organic_mat ( getter, leather.mats_size(), organic_mat_category::Leather ); } @@ -1537,36 +1581,36 @@ private: StockpileSettings::ClothSet * cloth = mBuffer.mutable_cloth(); serialize_list_organic_mat ( [=] ( const std::string &token ) { - cloth->add_thread_silk( 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); + 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); + 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); + 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 ); + 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); + 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); + 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); + cloth->add_cloth_metal ( token ); + }, mPile->settings.cloth.cloth_metal, organic_mat_category::MetalThread ); } void read_cloth() { @@ -1574,58 +1618,148 @@ private: 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_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_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_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.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_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_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_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); + 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() {} + bool wood_mat_is_allowed ( const df::plant_raw * plant ) { + return plant && plant->flags.is_set ( plant_raw_flags::TREE ); + } + + void write_wood() { + StockpileSettings::WoodSet * wood = mBuffer.mutable_wood(); + for ( size_t i = 0; i < mPile->settings.wood.mats.size(); ++i ) { + if ( mPile->settings.wood.mats.at ( i ) ) { + const df::plant_raw * plant = find_plant ( i ); + if ( !wood_mat_is_allowed ( plant ) ) continue; + wood->add_mats ( plant->id ); + *mOut << " plant " << i << " is " << plant->id << endl; + } + } + } + void read_wood() { + if ( mBuffer.has_wood() ) { + const StockpileSettings::WoodSet wood = mBuffer.wood(); + *mOut << "wood: " <flags.is_set ( material_flags::IS_METAL ) || + mi.material->flags.is_set ( material_flags::IS_STONE ) ); - bool weapons_mat_is_allowed(const MaterialInfo &mi) { - return mi.isValid(); } - void write_weapons() {} + void write_weapons() { + StockpileSettings::WeaponsSet * weapons = mBuffer.mutable_weapons(); + + weapons->set_unusable ( mPile->settings.weapons.unusable ); + weapons->set_usable ( mPile->settings.weapons.usable ); + + // weapon type + serialize_list_itemdef ( [=] ( const std::string &token ) { + weapons->add_weapon_type ( token ); + }, mPile->settings.weapons.weapon_type, + std::vector ( world->raws.itemdefs.weapons.begin(),world->raws.itemdefs.weapons.end() ), + item_type::WEAPON ); + + // trapcomp type + serialize_list_itemdef ( [=] ( const std::string &token ) { + weapons->add_trapcomp_type ( token ); + }, mPile->settings.weapons.trapcomp_type, + std::vector ( world->raws.itemdefs.trapcomps.begin(),world->raws.itemdefs.trapcomps.end() ), + item_type::TRAPCOMP ); + + // materials + FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::weapons_mat_is_allowed, this, _1 ); + serialize_list_material ( mat_filter, [=] ( const std::string &token ) { + weapons->add_mats ( token ); + }, mPile->settings.weapons.mats ); + + // other mats + serialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const std::string &token ) { + weapons->add_other_mats ( token ); + }, mPile->settings.weapons.other_mats ); + + // quality core + serialize_list_quality ( [=] ( const std::string &token ) { + weapons->add_quality_core ( token ); + }, mPile->settings.weapons.quality_core ); + + // quality total + serialize_list_quality ( [=] ( const std::string &token ) { + weapons->add_quality_total ( token ); + }, mPile->settings.weapons.quality_total ); + } + void read_weapons() { if ( mBuffer.has_weapons() ) { const StockpileSettings::WeaponsSet weapons = mBuffer.weapons(); *mOut << "weapons: " < const std::string& { + return weapons.weapon_type ( idx ); + }, weapons.weapon_type_size() ); + + // trapcomp type + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + return weapons.trapcomp_type ( idx ); + }, weapons.trapcomp_type_size() ); + // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::weapons_mat_is_allowed, this, _1 ); unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { return weapons.mats ( idx ); - }, weapons.mats_size() ); + }, weapons.mats_size() ); + + // other mats + unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& { + return weapons.other_mats ( idx ); + }, weapons.other_mats_size() ); // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { @@ -1639,20 +1773,71 @@ private: } - bool armor_mat_is_allowed(const MaterialInfo &mi) { - return mi.isValid(); - } - void write_armor() {} + void weapons_armor_setup_other_mats() { + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 0,"WOOD" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 1,"PLANT_CLOTH" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 2,"BONE" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 3,"SHELL" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 4,"LEATHER" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 5,"SILK" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 6,"GREEN_GLASS" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 7,"CLEAR_GLASS" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 8,"CRYSTAL_GLASS" ) ); + mOtherMatsWeaponsArmor.insert ( std::make_pair ( 9,"YARN" ) ); + } + + bool armor_mat_is_allowed ( const MaterialInfo &mi ) { + return weapons_mat_is_allowed ( mi ); + } + + void write_armor() { + StockpileSettings::ArmorSet * armor = mBuffer.mutable_armor(); + + armor->set_unusable ( mPile->settings.armor.unusable ); + armor->set_usable ( mPile->settings.armor.usable ); + + // materials + FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::armor_mat_is_allowed, this, _1 ); + serialize_list_material ( mat_filter, [=] ( const std::string &token ) { + armor->add_mats ( token ); + }, mPile->settings.armor.mats ); + + // other mats + serialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const std::string &token ) { + armor->add_other_mats ( token ); + }, mPile->settings.armor.other_mats ); + + // quality core + serialize_list_quality ( [=] ( const std::string &token ) { + armor->add_quality_core ( token ); + }, mPile->settings.armor.quality_core ); + + // quality total + serialize_list_quality ( [=] ( const std::string &token ) { + armor->add_quality_total ( token ); + }, mPile->settings.armor.quality_total ); + } + void read_armor() { if ( mBuffer.has_armor() ) { const StockpileSettings::ArmorSet armor = mBuffer.armor(); *mOut << "armor: " < const std::string& { return armor.mats ( idx ); - }, armor.mats_size() ); + }, armor.mats_size() ); + + // other mats + unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& { + return armor.other_mats ( idx ); + }, armor.other_mats_size() ); // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { @@ -1663,7 +1848,6 @@ private: return armor.quality_total ( idx ); }, armor.quality_total_size() ); } - } }; From f7ed821ef9a20a7fb17993ea37bebc4790a4f559 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 20 Nov 2014 10:33:09 +0100 Subject: [PATCH 09/36] stockpiles: serialization complete * working file import/export --- plugins/stockpiles.cpp | 298 ++++++++++++++++++++++++++++++++++------- 1 file changed, 246 insertions(+), 52 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 4f6bddc9f..2743740f4 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -43,6 +43,8 @@ #include +#include + #include using std::vector; @@ -66,6 +68,9 @@ static bool copystock_guard ( df::viewscreen *top ); static command_result savestock ( color_ostream &out, vector & parameters ); static bool savestock_guard ( df::viewscreen *top ); +static command_result loadstock( color_ostream &out, vector & parameters ); +static bool loadstock_guard ( df::viewscreen *top ); + DFHACK_PLUGIN ( "stockpiles" ); DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands ) @@ -91,6 +96,16 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector main.mode ) { + case Stockpiles: + return true; + case BuildingItems: + case QueryBuilding: + return !!virtual_cast ( world->selected_building ); + default: + return false; + } +} /** @@ -319,14 +351,58 @@ public: StockpileSerializer ( color_ostream &out, building_stockpilest const * stockpile ) : mOut ( &out ) , mPile ( stockpile ) { - // build other mats indices + + // build other_mats indices furniture_setup_other_mats(); bars_blocks_setup_other_mats(); finished_goods_setup_other_mats(); weapons_armor_setup_other_mats(); } - StockpileSettings write() { + ~StockpileSerializer() {} + + /** + * Will serialize stockpile settings to a file (overwrites existing files) + * @return success/failure + */ + bool serialize_to_file ( const std::string & file ) { + mBuffer.Clear(); + write(); + std::fstream output ( file, std::ios::out | std::ios::binary ); + return mBuffer.SerializeToOstream ( &output ); + } + + /** + * Serializes the stockpile settings to a string. + * @return empty string on error + */ + std::string serialize_to_string() { + mBuffer.Clear(); + write(); + std::string str; + if ( !TextFormat::PrintToString ( mBuffer, &str ) ) + return std::string(); + return str; + } + + bool unserialize_from_file(const std::string & file ) { + mBuffer.Clear(); + std::fstream input(file, std::ios::in | std::ios::binary); + const bool res = mBuffer.ParseFromIstream(&input); + read(); + return res; + } + + bool unserialize_from_string(const std::string & data) { + mBuffer.Clear(); + return TextFormat::ParseFromString(data, &mBuffer); + } + +private: + /** + read memory structures and serialize to protobuf + */ + void write() { // *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; write_general(); if ( mPile->settings.flags.bits.animals ) @@ -359,16 +435,10 @@ public: write_weapons(); if ( mPile->settings.flags.bits.armor ) write_armor(); - - std::string str; - TextFormat::PrintToString ( mBuffer, &str ); - *mOut << "serialized: " << str << endl; - return mBuffer; } - void read ( const StockpileSettings & settings ) { + void read () { *mOut << endl << "==READ==" << endl; - mBuffer = settings; read_general(); read_animals(); read_food(); @@ -387,8 +457,6 @@ public: read_armor(); } - ~StockpileSerializer() {} -private: color_ostream * mOut; building_stockpilest const * mPile; StockpileSettings mBuffer; @@ -427,7 +495,7 @@ private: // are item's of item_type allowed? typedef std::function FuncItemAllowed; // is this material allowed? - typedef std::function FuncMaterialAllowed; + typedef std::function FuncMaterialAllowed; struct food_pair { // exporting @@ -497,13 +565,13 @@ private: } } void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, std::vector list ) { - MaterialInfo mi; + MaterialInfo mi; for ( size_t i = 0; i < list.size(); ++i ) { if ( list.at ( i ) ) { mi.decode ( 0, i ); - if ( !is_allowed( mi ) ) continue; + if ( !is_allowed ( mi ) ) continue; *mOut << " material" << i << " is " << mi.getToken() << endl; - add_value(mi.getToken()); + add_value ( mi.getToken() ); } } } @@ -520,22 +588,22 @@ private: } - void serialize_list_quality ( FuncWriteExport add_value, const bool (&quality_list)[7] ) { + void serialize_list_quality ( FuncWriteExport add_value, const bool ( &quality_list ) [7] ) { using df::enums::item_quality::item_quality; df::enum_traits quality_traits; for ( size_t i = 0; i < 7; ++i ) { if ( quality_list[i] ) { const std::string f_type ( quality_traits.key_table[i] ); - add_value( f_type ); + add_value ( f_type ); *mOut << " quality: " << i << " is " << f_type < quality_traits; for ( int i = 0; i < list_size; ++i ) { - const std::string quality = read_value( i ); + const std::string quality = read_value ( i ); df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); if ( idx < 0 ) { *mOut << " invalid quality token " << quality << endl; @@ -545,29 +613,29 @@ private: } } - void serialize_list_other_mats(const std::map other_mats, FuncWriteExport add_value, std::vector list) { + void serialize_list_other_mats ( const std::map other_mats, FuncWriteExport add_value, std::vector list ) { for ( size_t i = 0; i < list.size(); ++i ) { if ( list.at ( i ) ) { - const std::string token = other_mats_index(other_mats, i); + const std::string token = other_mats_index ( other_mats, i ); if ( token.empty() ) { *mOut << " invalid other material with index " << i << endl; continue; } - add_value(token); + add_value ( token ); *mOut << " other mats " << i << " is " << token << endl; } } } - void unserialize_list_other_mats(const std::map other_mats, FuncReadImport read_value, int32_t list_size ) { + void unserialize_list_other_mats ( const std::map other_mats, FuncReadImport read_value, int32_t list_size ) { for ( int i = 0; i < list_size; ++i ) { - const std::string token = read_value( i ); - size_t idx = other_mats_token( other_mats, token ); + const std::string token = read_value ( i ); + size_t idx = other_mats_token ( other_mats, token ); if ( idx < 0 ) { *mOut << "invalid other mat with token " << token; continue; - } + } *mOut << " other_mats " << idx << " is " << token << endl; - } + } } void serialize_list_itemdef ( FuncWriteExport add_value, std::vector list, std::vector items, item_type::item_type type ) { @@ -587,20 +655,20 @@ private: void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size ) { for ( int i = 0; i < list_size; ++i ) { - std::string token = read_value( i ); + std::string token = read_value ( i ); ItemTypeInfo ii; if ( !ii.find ( token ) ) continue; *mOut << " itemdef " << ii.subtype << " is " << token << endl; } } - std::string other_mats_index( const std::map other_mats, int idx ) { + std::string other_mats_index ( const std::map other_mats, int idx ) { auto it = other_mats.find ( idx ); if ( it == other_mats.end() ) return std::string(); return it->second; } - int other_mats_token( const std::map other_mats, const std::string & token) { + int other_mats_token ( const std::map other_mats, const std::string & token ) { for ( auto it = other_mats.begin(); it != other_mats.end(); ++it ) { if ( it->second == token ) return it->first; @@ -613,12 +681,20 @@ private: mBuffer.set_max_wheelbarrows ( mPile->max_wheelbarrows ); mBuffer.set_use_links_only ( mPile->use_links_only ); mBuffer.set_unknown1 ( mPile->settings.unk1 ); - *mOut << "unknown is " << mPile->settings.unk1 << endl; + mBuffer.set_allow_inorganic ( mPile->settings.allow_inorganic ); + mBuffer.set_allow_organic ( mPile->settings.allow_organic ); } + void read_general() { - int bins = mBuffer.max_bins(); - *mOut << "Max bins: " << bins <settings.animals.empty_cages ); @@ -896,9 +972,9 @@ private: } } // other mats - serialize_list_other_mats(mOtherMatsFurniture, [=] ( const std::string &token ) { - furniture->add_other_mats( token ); - }, mPile->settings.furniture.other_mats ); + serialize_list_other_mats ( mOtherMatsFurniture, [=] ( const std::string &token ) { + furniture->add_other_mats ( token ); + }, mPile->settings.furniture.other_mats ); // for ( size_t i = 0; i < mPile->settings.furniture.other_mats.size(); ++i ) { // if ( mPile->settings.furniture.other_mats.at ( i ) ) { @@ -911,12 +987,12 @@ private: // *mOut << " other mats " << i << " is " << token << endl; // } // } - serialize_list_quality([=] ( const std::string &token ) { - furniture->add_quality_core ( token ); - }, mPile->settings.furniture.quality_core); - serialize_list_quality([=] ( const std::string &token ) { - furniture->add_quality_total( token ); - }, mPile->settings.furniture.quality_total); + serialize_list_quality ( [=] ( const std::string &token ) { + furniture->add_quality_core ( token ); + }, mPile->settings.furniture.quality_core ); + serialize_list_quality ( [=] ( const std::string &token ) { + furniture->add_quality_total ( token ); + }, mPile->settings.furniture.quality_total ); } /* skip gems and non hard things @@ -949,8 +1025,8 @@ private: *mOut << " mats " << mi.index << " is " << token << endl; } // other materials - unserialize_list_other_mats( mOtherMatsFurniture, [=] ( const size_t & idx ) -> const std::string& { - return furniture.other_mats( idx ); + unserialize_list_other_mats ( mOtherMatsFurniture, [=] ( const size_t & idx ) -> const std::string& { + return furniture.other_mats ( idx ); }, furniture.other_mats_size() ); // core quality @@ -1209,8 +1285,8 @@ private: }, mPile->settings.ammo.quality_core ); // quality total serialize_list_quality ( [=] ( const std::string &token ) { - ammo->add_quality_total( token ); - }, mPile->settings.ammo.quality_total); + ammo->add_quality_total ( token ); + }, mPile->settings.ammo.quality_total ); } void read_ammo() { @@ -1787,7 +1863,7 @@ private: } bool armor_mat_is_allowed ( const MaterialInfo &mi ) { - return weapons_mat_is_allowed ( mi ); + return mi.isValid() && mi.material && mi.material->flags.is_set ( material_flags::IS_METAL ); } void write_armor() { @@ -1796,6 +1872,48 @@ private: armor->set_unusable ( mPile->settings.armor.unusable ); armor->set_usable ( mPile->settings.armor.usable ); + // armor type + serialize_list_itemdef ( [=] ( const std::string &token ) { + armor->add_body ( token ); + }, mPile->settings.armor.body, + std::vector ( world->raws.itemdefs.armor.begin(),world->raws.itemdefs.armor.end() ), + item_type::ARMOR ); + + // helm type + serialize_list_itemdef ( [=] ( const std::string &token ) { + armor->add_head ( token ); + }, mPile->settings.armor.head, + std::vector ( world->raws.itemdefs.helms.begin(),world->raws.itemdefs.helms.end() ), + item_type::HELM ); + + // shoes type + serialize_list_itemdef ( [=] ( const std::string &token ) { + armor->add_feet ( token ); + }, mPile->settings.armor.feet, + std::vector ( world->raws.itemdefs.shoes.begin(),world->raws.itemdefs.shoes.end() ), + item_type::SHOES ); + + // gloves type + serialize_list_itemdef ( [=] ( const std::string &token ) { + armor->add_hands ( token ); + }, mPile->settings.armor.hands, + std::vector ( world->raws.itemdefs.gloves.begin(),world->raws.itemdefs.gloves.end() ), + item_type::GLOVES ); + + // pant type + serialize_list_itemdef ( [=] ( const std::string &token ) { + armor->add_legs ( token ); + }, mPile->settings.armor.legs, + std::vector ( world->raws.itemdefs.pants.begin(),world->raws.itemdefs.pants.end() ), + item_type::PANTS ); + + // shield type + serialize_list_itemdef ( [=] ( const std::string &token ) { + armor->add_shield ( token ); + }, mPile->settings.armor.shield, + std::vector ( world->raws.itemdefs.shields.begin(),world->raws.itemdefs.shields.end() ), + item_type::SHIELD ); + // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::armor_mat_is_allowed, this, _1 ); serialize_list_material ( mat_filter, [=] ( const std::string &token ) { @@ -1828,6 +1946,36 @@ private: *mOut << "unusable " << unusable << endl; *mOut << "usable " << usable << endl; + // body type + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + return armor.body ( idx ); + }, armor.body_size() ); + + // head type + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + return armor.head ( idx ); + }, armor.head_size() ); + + // feet type + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + return armor.feet ( idx ); + }, armor.feet_size() ); + + // hands type + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + return armor.hands ( idx ); + }, armor.hands_size() ); + + // legs type + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + return armor.legs ( idx ); + }, armor.legs_size() ); + + // shield type + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + return armor.shield ( idx ); + }, armor.shield_size() ); + // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::armor_mat_is_allowed, this, _1 ); unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { @@ -1851,20 +1999,66 @@ private: } }; +static bool file_exists(const std::string& filename ) +{ + struct stat buf; + if ( stat ( filename.c_str(), &buf ) != -1 ) { + return true; + } + return false; +} + +static bool is_dfstockfile(const std::string& filename) { + return filename.rfind(".dfstock") != std::string::npos; +} + +// exporting static command_result savestock ( color_ostream &out, vector & parameters ) { - // HOTKEY COMMAND: CORE ALREADY SUSPENDED building_stockpilest *sp = virtual_cast ( world->selected_building ); if ( !sp ) { out.printerr ( "Selected building isn't a stockpile.\n" ); return CR_WRONG_USAGE; } - // for testing StockpileSerializer cereal ( out, sp ); - StockpileSettings s = cereal.write(); - StockpileSerializer cereal2 ( out, sp ); - cereal2.read ( s ); + if ( parameters.size() < 1 ) { + std::string data = cereal.serialize_to_string(); + out << data << endl; + } else { + std::string file = parameters.at ( 0 ); + if ( !is_dfstockfile ( file ) ) file += ".dfstock"; + if ( !cereal.serialize_to_file ( file ) ) { + out << "serialize failed" << endl; + return CR_FAILURE; + } + } + return CR_OK; +} + + +// importing +static command_result loadstock ( color_ostream &out, vector & parameters ) +{ + building_stockpilest *sp = virtual_cast ( world->selected_building ); + if ( !sp ) { + out.printerr ( "Selected building isn't a stockpile.\n" ); + return CR_WRONG_USAGE; + } + + if ( parameters.size() != 1 || + !file_exists ( parameters.at ( 0 ) ) || + !is_dfstockfile ( parameters.at ( 0 ) ) ) { + out << parameters.size() << "\t" << file_exists(parameters.at(0)) << "\t" << is_dfstockfile(parameters.at(0)) << endl; + out.printerr ( "loadstock: first parameter must be a .dfstock file\n" ); + return CR_WRONG_USAGE; + } + + StockpileSerializer cereal ( out, sp ); + if ( !cereal.unserialize_from_file ( parameters.at ( 0 ) ) ) { + out << "unserialize failed" << endl; + return CR_FAILURE; + } return CR_OK; } From a4d19070e897878f20d824988ac2dda9bd87db9a Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 20 Nov 2014 11:14:27 +0100 Subject: [PATCH 10/36] stockpiles: write some documentation --- plugins/stockpiles.cpp | 130 +++++++++++++++++++++++++++++++---------- 1 file changed, 100 insertions(+), 30 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 2743740f4..8c2f72f9a 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -200,6 +200,9 @@ static bool loadstock_guard ( df::viewscreen *top ) } } +// Utility Functions {{{ +// A set of convenience functions for doing common lookups + /** * Retrieve creature raw from index @@ -232,14 +235,23 @@ static size_t find_plant( const std::string &plant_id) return linear_index ( world->raws.plants.all, &df::plant_raw::id, plant_id); } -typedef std::pair FoodMatPair; -typedef std::map FoodMatMap; +// }}} utility Functions + + + /** - * Helper class for performing organic_index/organic_types <---> material array index lookups + * Helper class for mapping the various organic mats between their material indices + * and their index in the stockpile_settings structures. */ class OrganicMatLookup { + +// pair of material type and index +typedef std::pair FoodMatPair; +// map for using type,index pairs to find the food index +typedef std::map FoodMatMap; + public: struct FoodMat { MaterialInfo material; @@ -343,11 +355,18 @@ private: }; bool OrganicMatLookup::index_built = false; -std::vector OrganicMatLookup::food_index = std::vector ( 37 ); +std::vector OrganicMatLookup::food_index = std::vector ( 37 ); +/** + * Class for serializing the stockpile_settings structure into a Google protobuf + */ class StockpileSerializer { public: + /** + * @param out for debugging + * @param stockpile stockpile to read or write settings to + */ StockpileSerializer ( color_ostream &out, building_stockpilest const * stockpile ) : mOut ( &out ) , mPile ( stockpile ) { @@ -385,6 +404,9 @@ public: return str; } + /** + * Read stockpile settings from file + */ bool unserialize_from_file(const std::string & file ) { mBuffer.Clear(); std::fstream input(file, std::ios::in | std::ios::binary); @@ -393,12 +415,20 @@ public: return res; } - bool unserialize_from_string(const std::string & data) { + /** + * Read text_format stockpile settings from string + */ + bool unserialize_from_string ( const std::string & data ) { mBuffer.Clear(); - return TextFormat::ParseFromString(data, &mBuffer); + return TextFormat::ParseFromString ( data, &mBuffer ); } private: + +// color_ostream & debug() { +// return *mOut; +// } + /** read memory structures and serialize to protobuf */ @@ -437,6 +467,7 @@ private: write_armor(); } + // parse serialized data into ui indices void read () { *mOut << endl << "==READ==" << endl; read_general(); @@ -465,6 +496,8 @@ private: std::map mOtherMatsBars; std::map mOtherMatsBlocks; std::map mOtherMatsWeaponsArmor; + + /** * Find an enum's value based off the string label. * @param traits the enum's trait struct @@ -497,6 +530,7 @@ private: // is this material allowed? typedef std::function FuncMaterialAllowed; + // convenient struct for parsing food stockpile items struct food_pair { // exporting FuncWriteExport set_value; @@ -514,6 +548,19 @@ private: food_pair() {} }; + /** + * There are many repeated (un)serialization cases throughout the stockpile_settings structure, + * so the most common cases have been generalized into generic functions using lambdas. + * + * The basic process to serialize a stockpile_settings structure is: + * 1. loop through the list + * 2. for every element that is TRUE: + * 3. map the specific stockpile_settings index into a general material, creature, etc index + * 4. verify that type is allowed in the list (e.g., no stone in gems stockpiles) + * 5. add it to the protobuf using FuncWriteExport + * + * The unserialization process is the same in reverse. + */ 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 ) ) { @@ -527,6 +574,9 @@ private: } } + /** + * @see serialize_list_organic_mat + */ 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 ) { @@ -538,6 +588,9 @@ private: } } + /** + * @see serialize_list_organic_mat + */ 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; @@ -552,6 +605,10 @@ private: } } } + + /** + * @see serialize_list_organic_mat + */ void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) { using df::enums::item_type::item_type; df::enum_traits type_traits; @@ -564,6 +621,10 @@ private: *mOut << " item_type " << idx << " is " << token << endl; } } + + /** + * @see serialize_list_organic_mat + */ void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, std::vector list ) { MaterialInfo mi; for ( size_t i = 0; i < list.size(); ++i ) { @@ -576,7 +637,9 @@ private: } } - + /** + * @see serialize_list_organic_mat + */ void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) { for ( int i = 0; i < list_size; ++i ) { const std::string token = read_value ( i ); @@ -588,6 +651,9 @@ private: } + /** + * @see serialize_list_organic_mat + */ void serialize_list_quality ( FuncWriteExport add_value, const bool ( &quality_list ) [7] ) { using df::enums::item_quality::item_quality; df::enum_traits quality_traits; @@ -599,6 +665,10 @@ private: } } } + + /** + * @see serialize_list_organic_mat + */ void unserialize_list_quality ( FuncReadImport read_value, int32_t list_size ) { using df::enums::item_quality::item_quality; df::enum_traits quality_traits; @@ -613,6 +683,9 @@ private: } } + /** + * @see serialize_list_organic_mat + */ void serialize_list_other_mats ( const std::map other_mats, FuncWriteExport add_value, std::vector list ) { for ( size_t i = 0; i < list.size(); ++i ) { if ( list.at ( i ) ) { @@ -626,6 +699,10 @@ private: } } } + + /** + * @see serialize_list_organic_mat + */ void unserialize_list_other_mats ( const std::map other_mats, FuncReadImport read_value, int32_t list_size ) { for ( int i = 0; i < list_size; ++i ) { const std::string token = read_value ( i ); @@ -638,6 +715,9 @@ private: } } + /** + * @see serialize_list_organic_mat + */ void serialize_list_itemdef ( FuncWriteExport add_value, std::vector list, std::vector items, item_type::item_type type ) { for ( size_t i = 0; i < list.size(); ++i ) { if ( list.at ( i ) ) { @@ -653,6 +733,9 @@ private: } } + /** + * @see serialize_list_organic_mat + */ void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size ) { for ( int i = 0; i < list_size; ++i ) { std::string token = read_value ( i ); @@ -662,12 +745,22 @@ private: } } + /** + * Given a list of other_materials and an index, return its corresponding token + * @return empty string if not found + * @see other_mats_token + */ std::string other_mats_index ( const std::map other_mats, int idx ) { auto it = other_mats.find ( idx ); if ( it == other_mats.end() ) return std::string(); return it->second; } + /** + * Given a list of other_materials and a token, return its corresponding index + * @return -1 if not found + * @see other_mats_index + */ int other_mats_token ( const std::map other_mats, const std::string & token ) { for ( auto it = other_mats.begin(); it != other_mats.end(); ++it ) { if ( it->second == token ) @@ -976,17 +1069,6 @@ private: furniture->add_other_mats ( token ); }, mPile->settings.furniture.other_mats ); -// for ( size_t i = 0; i < mPile->settings.furniture.other_mats.size(); ++i ) { -// if ( mPile->settings.furniture.other_mats.at ( i ) ) { -// const std::string token = furn_other_mats ( i ); -// if ( token.empty() ) { -// *mOut << " invalid other material with index " << i << endl; -// continue; -// } -// furniture->add_other_mats ( token ); -// *mOut << " other mats " << i << " is " << token << endl; -// } -// } serialize_list_quality ( [=] ( const std::string &token ) { furniture->add_quality_core ( token ); }, mPile->settings.furniture.quality_core ); @@ -1089,18 +1171,6 @@ private: refuse->add_type ( token ); }, mPile->settings.refuse.type ); -// using df::enums::item_type::item_type; -// df::enum_traits type_traits; -// *mOut << "refuse type size = " << mPile->settings.refuse.type.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; -// for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) { -// if ( mPile->settings.refuse.type.at ( i ) ) { -// const item_type type = ( item_type ) ( ( df::enum_traits::base_type ) i ); -// std::string r_type ( type_traits.key_table[i+1] ); -// if ( !refuse_type_is_allowed ( type ) ) continue; -// refuse->add_type ( r_type ); -// *mOut << "refuse type key_table[" << i+1 << "] type[" << ( int16_t ) type << "] is " << r_type <add_corpses ( id ); From 86ba81a3a92e237af0d19b7e357cb5a62ab9d687 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 20 Nov 2014 11:18:01 +0100 Subject: [PATCH 11/36] stockpiles: format with ANSI style and 4 spaces --- plugins/stockpiles.cpp | 1062 ++++++++++++++++++++++++++-------------- 1 file changed, 704 insertions(+), 358 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 8c2f72f9a..8388b34f4 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -68,14 +68,15 @@ static bool copystock_guard ( df::viewscreen *top ); static command_result savestock ( color_ostream &out, vector & parameters ); static bool savestock_guard ( df::viewscreen *top ); -static command_result loadstock( color_ostream &out, vector & parameters ); +static command_result loadstock ( color_ostream &out, vector & parameters ); static bool loadstock_guard ( df::viewscreen *top ); DFHACK_PLUGIN ( "stockpiles" ); DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands ) { - if ( world && ui ) { + if ( world && ui ) + { commands.push_back ( PluginCommand ( "copystock", "Copy stockpile under cursor.", @@ -104,8 +105,8 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector main.mode ) { + switch ( ui->main.mode ) + { case Stockpiles: return true; case BuildingItems: @@ -140,7 +142,8 @@ static command_result copystock ( color_ostream &out, vector & paramete // HOTKEY COMMAND: CORE ALREADY SUSPENDED // For convenience: when used in the stockpiles mode, switch to 'q' - if ( ui->main.mode == ui_sidebar_mode::Stockpiles ) { + if ( ui->main.mode == ui_sidebar_mode::Stockpiles ) + { world->selected_building = NULL; // just in case it contains some kind of garbage ui->main.mode = ui_sidebar_mode::QueryBuilding; selection_rect->start_x = -30000; @@ -150,7 +153,8 @@ static command_result copystock ( color_ostream &out, vector & paramete } building_stockpilest *sp = virtual_cast ( world->selected_building ); - if ( !sp ) { + if ( !sp ) + { out.printerr ( "Selected building isn't a stockpile.\n" ); return CR_WRONG_USAGE; } @@ -171,7 +175,8 @@ static bool savestock_guard ( df::viewscreen *top ) if ( !Gui::dwarfmode_hotkey ( top ) ) return false; - switch ( ui->main.mode ) { + switch ( ui->main.mode ) + { case Stockpiles: return true; case BuildingItems: @@ -187,17 +192,18 @@ static bool loadstock_guard ( df::viewscreen *top ) using namespace ui_sidebar_mode; if ( !Gui::dwarfmode_hotkey ( top ) ) - return false; + return false; - switch ( ui->main.mode ) { - case Stockpiles: + switch ( ui->main.mode ) + { + case Stockpiles: return true; - case BuildingItems: - case QueryBuilding: + case BuildingItems: + case QueryBuilding: return !!virtual_cast ( world->selected_building ); - default: + default: return false; - } + } } // Utility Functions {{{ @@ -223,16 +229,17 @@ static int16_t find_creature ( const std::string &creature_id ) /** * Retrieve plant raw from index */ -static df::plant_raw* find_plant( size_t idx ) { - return world->raws.plants.all[idx]; +static df::plant_raw* find_plant ( size_t idx ) +{ + return world->raws.plants.all[idx]; } /** * Retrieve plant index from id string */ -static size_t find_plant( const std::string &plant_id) +static size_t find_plant ( const std::string &plant_id ) { - return linear_index ( world->raws.plants.all, &df::plant_raw::id, plant_id); + return linear_index ( world->raws.plants.all, &df::plant_raw::id, plant_id ); } // }}} utility Functions @@ -248,18 +255,20 @@ class OrganicMatLookup { // pair of material type and index -typedef std::pair FoodMatPair; + typedef std::pair FoodMatPair; // map for using type,index pairs to find the food index -typedef std::map FoodMatMap; + typedef std::map FoodMatMap; public: - struct FoodMat { + struct FoodMat + { MaterialInfo material; df::creature_raw *creature; df::caste_raw * caste; FoodMat() : material ( -1 ), creature ( 0 ), caste ( 0 ) {} }; - static void food_mat_by_idx ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector::size_type food_idx, FoodMat & food_mat ) { + static void food_mat_by_idx ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector::size_type food_idx, FoodMat & food_mat ) + { out << "food_lookup: food_idx(" << food_idx << ") "; df::world_raws &raws = world->raws; df::special_mat_table table = raws.mat_table; @@ -267,35 +276,45 @@ public: int16_t type = table.organic_types[mat_category][food_idx]; if ( mat_category == organic_mat_category::Fish || mat_category == organic_mat_category::UnpreparedFish || - mat_category == organic_mat_category::Eggs ) { + mat_category == organic_mat_category::Eggs ) + { food_mat.creature = raws.creatures.all[type]; food_mat.caste = food_mat.creature->caste[main_idx]; out << " special creature type(" << type << ") caste("<< main_idx <<")" <::size_type idx ) { + static std::string food_token_by_idx ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector::size_type idx ) + { FoodMat food_mat; food_mat_by_idx ( out, mat_category, idx, food_mat ); - if ( food_mat.material.isValid() ) { + if ( food_mat.material.isValid() ) + { return food_mat.material.getToken(); - } else if ( food_mat.creature ) { + } + else if ( food_mat.creature ) + { return food_mat.creature->creature_id + ":" + food_mat.caste->caste_id; } return std::string(); } - static void food_build_map ( color_ostream &out ) { + static void food_build_map ( color_ostream &out ) + { if ( index_built ) return; df::world_raws &raws = world->raws; 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 <= traits.last_item_value; ++mat_category ) { - for ( size_t i = 0; i < table.organic_indexes[mat_category].size(); ++i ) { + for ( int32_t mat_category = traits.first_item_value; mat_category <= traits.last_item_value; ++mat_category ) + { + for ( size_t i = 0; i < table.organic_indexes[mat_category].size(); ++i ) + { int16_t type = table.organic_types[mat_category].at ( i ); int32_t index = table.organic_indexes[mat_category].at ( i ); food_index[mat_category].insert ( std::make_pair ( std::make_pair ( type,index ), i ) ); // wtf.. only in c++ @@ -304,27 +323,37 @@ public: index_built = true; } - static int food_idx_by_token ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) { + static int food_idx_by_token ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) + { int16_t food_idx = -1; df::world_raws &raws = world->raws; df::special_mat_table table = raws.mat_table; out << "food_idx_by_token: "; if ( mat_category == organic_mat_category::Fish || mat_category == organic_mat_category::UnpreparedFish || - mat_category == organic_mat_category::Eggs ) { + mat_category == organic_mat_category::Eggs ) + { std::vector tokens; split_string ( &tokens, token, ":" ); - if ( tokens.size() != 2 ) { + if ( tokens.size() != 2 ) + { out << "creature " << "invalid CREATURE:CASTE token: " << token << endl; - } else { + } + else + { int16_t creature_idx = find_creature ( tokens[0] ); - if ( creature_idx >= 0 ) { + if ( creature_idx >= 0 ) + { food_idx = linear_index ( table.organic_types[mat_category], creature_idx ); out << "creature " << token << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl; - } else out << " creature invalid token " << tokens[0]; + } + else out << " creature invalid token " << tokens[0]; } - } else { - if ( !index_built ) { + } + else + { + if ( !index_built ) + { food_build_map ( out ); } MaterialInfo mat_info = food_mat_by_token ( out, token ); @@ -332,17 +361,21 @@ public: int32_t index = mat_info.index; int16_t food_idx2 = -1; auto it = food_index[mat_category].find ( std::make_pair ( type, index ) ); - if ( it != food_index[mat_category].end() ) { + if ( it != food_index[mat_category].end() ) + { out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx(" << it->second << ")" << endl; food_idx = it->second; - } else { + } + else + { 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, 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; @@ -369,7 +402,8 @@ public: */ StockpileSerializer ( color_ostream &out, building_stockpilest const * stockpile ) : mOut ( &out ) - , mPile ( stockpile ) { + , mPile ( stockpile ) + { // build other_mats indices furniture_setup_other_mats(); @@ -384,7 +418,8 @@ public: * Will serialize stockpile settings to a file (overwrites existing files) * @return success/failure */ - bool serialize_to_file ( const std::string & file ) { + bool serialize_to_file ( const std::string & file ) + { mBuffer.Clear(); write(); std::fstream output ( file, std::ios::out | std::ios::binary ); @@ -395,7 +430,8 @@ public: * Serializes the stockpile settings to a string. * @return empty string on error */ - std::string serialize_to_string() { + std::string serialize_to_string() + { mBuffer.Clear(); write(); std::string str; @@ -407,10 +443,11 @@ public: /** * Read stockpile settings from file */ - bool unserialize_from_file(const std::string & file ) { + bool unserialize_from_file ( const std::string & file ) + { mBuffer.Clear(); - std::fstream input(file, std::ios::in | std::ios::binary); - const bool res = mBuffer.ParseFromIstream(&input); + std::fstream input ( file, std::ios::in | std::ios::binary ); + const bool res = mBuffer.ParseFromIstream ( &input ); read(); return res; } @@ -418,7 +455,8 @@ public: /** * Read text_format stockpile settings from string */ - bool unserialize_from_string ( const std::string & data ) { + bool unserialize_from_string ( const std::string & data ) + { mBuffer.Clear(); return TextFormat::ParseFromString ( data, &mBuffer ); } @@ -432,7 +470,8 @@ private: /** read memory structures and serialize to protobuf */ - void write() { + void write() + { // *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; write_general(); if ( mPile->settings.flags.bits.animals ) @@ -468,7 +507,8 @@ private: } // parse serialized data into ui indices - void read () { + void read () + { *mOut << endl << "==READ==" << endl; read_general(); read_animals(); @@ -505,15 +545,18 @@ private: * @return the enum's value */ template - static typename df::enum_traits::base_type linear_index ( color_ostream& out, df::enum_traits traits, const std::string &token ) { + static typename df::enum_traits::base_type linear_index ( color_ostream& out, df::enum_traits traits, const std::string &token ) + { auto j = traits.first_item_value; auto limit = traits.last_item_value; // sometimes enums start at -1, which is bad news for array indexing - if ( j < 0 ) { + if ( j < 0 ) + { j += abs ( traits.first_item_value ); limit += abs ( traits.first_item_value ); } - for ( ; j <= limit; ++j ) { + for ( ; j <= limit; ++j ) + { // out << " linear_index("<< token <<") = table["< FuncMaterialAllowed; // convenient struct for parsing food stockpile items - struct food_pair { + struct food_pair + { // exporting FuncWriteExport set_value; vector stockpile_values; @@ -561,13 +605,19 @@ private: * * The unserialization process is the same in reverse. */ - 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 ) ) { + 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() ) { + if ( !token.empty() ) + { add_value ( token ); - } else { + } + else + { *mOut << "food mat invalid :(" << endl; } } @@ -577,9 +627,12 @@ private: /** * @see serialize_list_organic_mat */ - 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 ) { + 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; @@ -591,12 +644,15 @@ private: /** * @see serialize_list_organic_mat */ - void serialize_list_item_type ( FuncItemAllowed is_allowed, FuncWriteExport add_value, std::vector list ) { + 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; *mOut << "item_type size = " << list.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; - for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) { - if ( list.at ( i ) ) { + for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) + { + if ( list.at ( i ) ) + { const item_type type = ( item_type ) ( ( df::enum_traits::base_type ) i ); std::string r_type ( type_traits.key_table[i+1] ); if ( !is_allowed ( type ) ) continue; @@ -609,10 +665,12 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) { + void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) + { using df::enums::item_type::item_type; df::enum_traits type_traits; - for ( int32_t i = 0; i < list_size; ++i ) { + for ( int32_t i = 0; i < list_size; ++i ) + { const std::string token = read_value ( i ); // subtract one because item_type starts at -1 const df::enum_traits::base_type idx = linear_index ( *mOut, type_traits, token ) - 1; @@ -625,10 +683,13 @@ private: /** * @see serialize_list_organic_mat */ - void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, std::vector list ) { + void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, std::vector list ) + { MaterialInfo mi; - for ( size_t i = 0; i < list.size(); ++i ) { - if ( list.at ( i ) ) { + for ( size_t i = 0; i < list.size(); ++i ) + { + if ( list.at ( i ) ) + { mi.decode ( 0, i ); if ( !is_allowed ( mi ) ) continue; *mOut << " material" << i << " is " << mi.getToken() << endl; @@ -640,8 +701,10 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) { - for ( int i = 0; i < list_size; ++i ) { + void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) + { + for ( int i = 0; i < list_size; ++i ) + { const std::string token = read_value ( i ); MaterialInfo mi; mi.find ( token ); @@ -654,11 +717,14 @@ private: /** * @see serialize_list_organic_mat */ - void serialize_list_quality ( FuncWriteExport add_value, const bool ( &quality_list ) [7] ) { + void serialize_list_quality ( FuncWriteExport add_value, const bool ( &quality_list ) [7] ) + { using df::enums::item_quality::item_quality; df::enum_traits quality_traits; - for ( size_t i = 0; i < 7; ++i ) { - if ( quality_list[i] ) { + for ( size_t i = 0; i < 7; ++i ) + { + if ( quality_list[i] ) + { const std::string f_type ( quality_traits.key_table[i] ); add_value ( f_type ); *mOut << " quality: " << i << " is " << f_type < quality_traits; - for ( int i = 0; i < list_size; ++i ) { + for ( int i = 0; i < list_size; ++i ) + { const std::string quality = read_value ( i ); df::enum_traits::base_type idx = linear_index ( *mOut, quality_traits, quality ); - if ( idx < 0 ) { + if ( idx < 0 ) + { *mOut << " invalid quality token " << quality << endl; continue; } @@ -686,11 +755,15 @@ private: /** * @see serialize_list_organic_mat */ - void serialize_list_other_mats ( const std::map other_mats, FuncWriteExport add_value, std::vector list ) { - for ( size_t i = 0; i < list.size(); ++i ) { - if ( list.at ( i ) ) { + void serialize_list_other_mats ( const std::map other_mats, FuncWriteExport add_value, std::vector list ) + { + for ( size_t i = 0; i < list.size(); ++i ) + { + if ( list.at ( i ) ) + { const std::string token = other_mats_index ( other_mats, i ); - if ( token.empty() ) { + if ( token.empty() ) + { *mOut << " invalid other material with index " << i << endl; continue; } @@ -703,11 +776,14 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_other_mats ( const std::map other_mats, FuncReadImport read_value, int32_t list_size ) { - for ( int i = 0; i < list_size; ++i ) { + void unserialize_list_other_mats ( const std::map other_mats, FuncReadImport read_value, int32_t list_size ) + { + for ( int i = 0; i < list_size; ++i ) + { const std::string token = read_value ( i ); size_t idx = other_mats_token ( other_mats, token ); - if ( idx < 0 ) { + if ( idx < 0 ) + { *mOut << "invalid other mat with token " << token; continue; } @@ -718,9 +794,12 @@ private: /** * @see serialize_list_organic_mat */ - void serialize_list_itemdef ( FuncWriteExport add_value, std::vector list, std::vector items, item_type::item_type type ) { - for ( size_t i = 0; i < list.size(); ++i ) { - if ( list.at ( i ) ) { + void serialize_list_itemdef ( FuncWriteExport add_value, std::vector list, std::vector items, item_type::item_type type ) + { + for ( size_t i = 0; i < list.size(); ++i ) + { + if ( list.at ( i ) ) + { const df::itemdef *a = items.at ( i ); // skip procedurally generated items if ( a->base_flags.is_set ( 0 ) ) continue; @@ -736,8 +815,10 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size ) { - for ( int i = 0; i < list_size; ++i ) { + void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size ) + { + for ( int i = 0; i < list_size; ++i ) + { std::string token = read_value ( i ); ItemTypeInfo ii; if ( !ii.find ( token ) ) continue; @@ -750,7 +831,8 @@ private: * @return empty string if not found * @see other_mats_token */ - std::string other_mats_index ( const std::map other_mats, int idx ) { + std::string other_mats_index ( const std::map other_mats, int idx ) + { auto it = other_mats.find ( idx ); if ( it == other_mats.end() ) return std::string(); @@ -761,15 +843,18 @@ private: * @return -1 if not found * @see other_mats_index */ - int other_mats_token ( const std::map other_mats, const std::string & token ) { - for ( auto it = other_mats.begin(); it != other_mats.end(); ++it ) { + int other_mats_token ( const std::map other_mats, const std::string & token ) + { + for ( auto it = other_mats.begin(); it != other_mats.end(); ++it ) + { if ( it->second == token ) return it->first; } return -1; } - void write_general() { + void write_general() + { mBuffer.set_max_bins ( mPile->max_barrels ); mBuffer.set_max_wheelbarrows ( mPile->max_wheelbarrows ); mBuffer.set_use_links_only ( mPile->use_links_only ); @@ -778,7 +863,8 @@ private: mBuffer.set_allow_organic ( mPile->settings.allow_organic ); } - void read_general() { + void read_general() + { const int bins = mBuffer.max_bins(); const int wheelbarrows = mBuffer.max_wheelbarrows(); const bool use_links_only = mBuffer.use_links_only(); @@ -788,12 +874,15 @@ private: } - void write_animals() { + void write_animals() + { dfstockpiles::StockpileSettings::AnimalsSet animals; animals.set_empty_cages ( mPile->settings.animals.empty_cages ); animals.set_empty_traps ( mPile->settings.animals.empty_traps ); - for ( size_t i = 0; i < mPile->settings.animals.enabled.size(); ++i ) { - if ( mPile->settings.animals.enabled.at ( i ) == 1 ) { + for ( size_t i = 0; i < mPile->settings.animals.enabled.size(); ++i ) + { + if ( mPile->settings.animals.enabled.at ( i ) == 1 ) + { df::creature_raw* r = find_creature ( i ); *mOut << "creature "<< r->creature_id << " " << i << endl; mBuffer.mutable_animals()->add_enabled ( r->creature_id ); @@ -801,14 +890,18 @@ private: } } - void read_animals() { - if ( mBuffer.has_animals() ) { + void read_animals() + { + if ( mBuffer.has_animals() ) + { // mPile->settings.flags.bits.animals = true; *mOut << "animals:" < 0 ) { - for ( auto i = 0; i < mBuffer.animals().enabled().size(); ++i ) { + if ( mBuffer.animals().enabled_size() > 0 ) + { + for ( auto i = 0; i < mBuffer.animals().enabled().size(); ++i ) + { std::string id = mBuffer.animals().enabled ( i ); int idx = find_creature ( id ); *mOut << id << " " << idx << endl; @@ -817,139 +910,179 @@ private: } } - food_pair food_map ( organic_mat_category::organic_mat_category cat ) { + food_pair food_map ( organic_mat_category::organic_mat_category cat ) + { using df::enums::organic_mat_category::organic_mat_category; using namespace std::placeholders; - switch ( cat ) { - case organic_mat_category::Meat: { - FuncWriteExport setter = [=] ( const std::string &id ) { + switch ( cat ) + { + case organic_mat_category::Meat: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_meat ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().meat ( idx ); }; return food_pair ( setter, mPile->settings.food.meat, getter, mBuffer.food().meat_size() ); } - case organic_mat_category::Fish: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Fish: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_fish ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().fish ( idx ); }; return food_pair ( setter, mPile->settings.food.fish, getter, mBuffer.food().fish_size() ); } - case organic_mat_category::UnpreparedFish: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::UnpreparedFish: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_unprepared_fish ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().unprepared_fish ( idx ); }; return food_pair ( setter, mPile->settings.food.unprepared_fish, getter, mBuffer.food().unprepared_fish_size() ); } - case organic_mat_category::Eggs: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Eggs: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_egg ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().egg ( idx ); }; return food_pair ( setter, mPile->settings.food.egg, getter, mBuffer.food().egg_size() ); } - case organic_mat_category::Plants: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Plants: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_plants ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().plants ( idx ); }; return food_pair ( setter, mPile->settings.food.plants, getter, mBuffer.food().plants_size() ); } - case organic_mat_category::PlantDrink: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::PlantDrink: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_drink_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.drink_plant, getter, mBuffer.food().drink_plant_size() ); } - case organic_mat_category::CreatureDrink: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::CreatureDrink: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_drink_animal ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_animal ( idx ); }; return food_pair ( setter, mPile->settings.food.drink_animal, getter, mBuffer.food().drink_animal_size() ); } - case organic_mat_category::PlantCheese: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::PlantCheese: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_cheese_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.cheese_plant, getter, mBuffer.food().cheese_plant_size() ); } - case organic_mat_category::CreatureCheese: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::CreatureCheese: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_cheese_animal ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_animal ( idx ); }; return food_pair ( setter, mPile->settings.food.cheese_animal, getter, mBuffer.food().cheese_animal_size() ); } - case organic_mat_category::Seed: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Seed: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_seeds ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().seeds ( idx ); }; return food_pair ( setter, mPile->settings.food.seeds, getter, mBuffer.food().seeds_size() ); } - case organic_mat_category::Leaf: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Leaf: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_leaves ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().leaves ( idx ); }; return food_pair ( setter, mPile->settings.food.leaves, getter, mBuffer.food().leaves_size() ); } - case organic_mat_category::PlantPowder: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::PlantPowder: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_powder_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.powder_plant, getter, mBuffer.food().powder_plant_size() ); } - case organic_mat_category::CreaturePowder: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::CreaturePowder: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_powder_creature ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_creature ( idx ); }; return food_pair ( setter, mPile->settings.food.powder_creature, getter, mBuffer.food().powder_creature_size() ); } - case organic_mat_category::Glob: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Glob: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_glob ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob ( idx ); }; return food_pair ( setter, mPile->settings.food.glob, getter, mBuffer.food().glob_size() ); } - case organic_mat_category::PlantLiquid: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::PlantLiquid: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_liquid_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_plant ( idx ); }; return food_pair ( setter, mPile->settings.food.liquid_plant, getter, mBuffer.food().liquid_plant_size() ); } - case organic_mat_category::CreatureLiquid: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::CreatureLiquid: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_liquid_animal ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_animal ( idx ); }; return food_pair ( setter, mPile->settings.food.liquid_animal, getter, mBuffer.food().liquid_animal_size() ); } - case organic_mat_category::MiscLiquid: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::MiscLiquid: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_liquid_misc ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_misc ( idx ); }; return food_pair ( setter, mPile->settings.food.liquid_misc, getter, mBuffer.food().liquid_misc_size() ); } - case organic_mat_category::Paste: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Paste: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_glob_paste ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_paste ( idx ); }; return food_pair ( setter, mPile->settings.food.glob_paste, getter, mBuffer.food().glob_paste_size() ); } - case organic_mat_category::Pressed: { - FuncWriteExport setter = [=] ( const std::string &id ) { + case organic_mat_category::Pressed: + { + FuncWriteExport setter = [=] ( const std::string &id ) + { mBuffer.mutable_food()->add_glob_pressed ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_pressed ( idx ); }; @@ -980,34 +1113,40 @@ private: } - void write_food() { + void write_food() + { StockpileSettings::FoodSet *food = mBuffer.mutable_food(); food->set_prepared_meals ( mPile->settings.food.prepared_meals ); using df::enums::organic_mat_category::organic_mat_category; df::enum_traits traits; - for ( int32_t mat_category = traits.first_item_value; mat_category traits; - for ( int32_t mat_category = traits.first_item_value; mat_category second; } - int furn_other_mats ( const std::string & token ) { - for ( auto it = mOtherMatsFurniture.begin(); it != mOtherMatsFurniture.end(); ++it ) { + int furn_other_mats ( const std::string & token ) + { + for ( auto it = mOtherMatsFurniture.begin(); it != mOtherMatsFurniture.end(); ++it ) + { if ( it->second == token ) return it->first; } return -1; } - void write_furniture() { + void write_furniture() + { StockpileSettings::FurnitureSet *furniture= mBuffer.mutable_furniture(); furniture->set_sand_bags ( mPile->settings.furniture.sand_bags ); // FURNITURE type using df::enums::furniture_type::furniture_type; df::enum_traits type_traits; - for ( size_t i = 0; i < mPile->settings.furniture.type.size(); ++i ) { - if ( mPile->settings.furniture.type.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.furniture.type.size(); ++i ) + { + if ( mPile->settings.furniture.type.at ( i ) ) + { std::string f_type ( type_traits.key_table[i] ); furniture->add_type ( f_type ); *mOut << "furniture_type " << i << " is " << f_type <settings.furniture.mats.size(); ++i ) { - if ( mPile->settings.furniture.mats.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.furniture.mats.size(); ++i ) + { + if ( mPile->settings.furniture.mats.at ( i ) ) + { mi.decode ( 0, i ); if ( !furniture_mat_is_allowed ( mi ) ) continue; *mOut << "furniture mat: " << mi.getToken() << endl; @@ -1065,14 +1212,17 @@ private: } } // other mats - serialize_list_other_mats ( mOtherMatsFurniture, [=] ( const std::string &token ) { + serialize_list_other_mats ( mOtherMatsFurniture, [=] ( const std::string &token ) + { furniture->add_other_mats ( token ); }, mPile->settings.furniture.other_mats ); - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { furniture->add_quality_core ( token ); }, mPile->settings.furniture.quality_core ); - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { furniture->add_quality_total ( token ); }, mPile->settings.furniture.quality_total ); } @@ -1080,26 +1230,31 @@ private: /* skip gems and non hard things should be roughly (:flags.is_set ( material_flags::IS_GEM ) && mi.material->flags.is_set ( material_flags::ITEMS_HARD ); } - void read_furniture() { - if ( mBuffer.has_furniture() ) { + void read_furniture() + { + if ( mBuffer.has_furniture() ) + { const StockpileSettings::FurnitureSet furniture = mBuffer.furniture(); *mOut << "furniture:" < type_traits; - for ( int i = 0; i < furniture.type_size(); ++i ) { + for ( int i = 0; i < furniture.type_size(); ++i ) + { const std::string type = furniture.type ( i ); df::enum_traits::base_type idx = linear_index ( *mOut, type_traits, type ); *mOut << " type " << idx << " is " << type << endl; } // metal, stone/clay materials - for ( int i = 0; i < furniture.mats_size(); ++i ) { + for ( int i = 0; i < furniture.mats_size(); ++i ) + { const std::string token = furniture.mats ( i ); MaterialInfo mi; mi.find ( token ); @@ -1107,23 +1262,27 @@ private: *mOut << " mats " << mi.index << " is " << token << endl; } // other materials - unserialize_list_other_mats ( mOtherMatsFurniture, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsFurniture, [=] ( const size_t & idx ) -> const std::string& + { return furniture.other_mats ( idx ); }, furniture.other_mats_size() ); // core quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return furniture.quality_core ( idx ); }, furniture.quality_core_size() ); // total quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return furniture.quality_total ( idx ); }, furniture.quality_total_size() ); } } - bool refuse_creature_is_allowed ( const df::creature_raw *raw ) { + bool refuse_creature_is_allowed ( const df::creature_raw *raw ) + { if ( !raw ) return false; // wagon and generated creatures not allowed, except angels const bool is_wagon = raw->creature_id == "EQUIPMENT_WAGON"; @@ -1132,9 +1291,12 @@ private: return !is_wagon && ! ( is_generated && !is_angel ); } - void refuse_write_helper ( std::function add_value, const vector & list ) { - for ( size_t i = 0; i < list.size(); ++i ) { - if ( list.at ( i ) == 1 ) { + void refuse_write_helper ( std::function add_value, const vector & list ) + { + for ( size_t i = 0; i < list.size(); ++i ) + { + if ( list.at ( i ) == 1 ) + { df::creature_raw* r = find_creature ( i ); // skip forgotten beasts, titans, demons, and night creatures if ( !refuse_creature_is_allowed ( r ) ) continue; @@ -1144,7 +1306,8 @@ private: } } - bool refuse_type_is_allowed ( item_type::item_type type ) { + bool refuse_type_is_allowed ( item_type::item_type type ) + { if ( type == item_type::NONE || type == item_type::BAR || type == item_type::SMALLGEM @@ -1160,58 +1323,72 @@ private: } - void write_refuse() { + void write_refuse() + { StockpileSettings::RefuseSet *refuse = mBuffer.mutable_refuse(); refuse->set_fresh_raw_hide ( mPile->settings.refuse.fresh_raw_hide ); refuse->set_rotten_raw_hide ( mPile->settings.refuse.rotten_raw_hide ); // type FuncItemAllowed filter = std::bind ( &StockpileSerializer::refuse_type_is_allowed, this, _1 ); - serialize_list_item_type ( filter, [=] ( const std::string &token ) { + serialize_list_item_type ( filter, [=] ( const std::string &token ) + { refuse->add_type ( token ); }, mPile->settings.refuse.type ); // corpses - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_corpses ( id ); }, mPile->settings.refuse.corpses ); // body_parts - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_body_parts ( id ); }, mPile->settings.refuse.body_parts ); // skulls - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_skulls ( id ); }, mPile->settings.refuse.skulls ); // bones - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_bones ( id ); }, mPile->settings.refuse.bones ); // hair - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_hair ( id ); }, mPile->settings.refuse.hair ); // shells - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_shells ( id ); }, mPile->settings.refuse.shells ); // teeth - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_teeth ( id ); }, mPile->settings.refuse.teeth ); // horns - refuse_write_helper ( [=] ( const std::string &id ) { + refuse_write_helper ( [=] ( const std::string &id ) + { refuse->add_horns ( id ); }, mPile->settings.refuse.horns ); } - void refuse_read_helper ( std::function get_value, size_t list_size ) { - if ( list_size > 0 ) { - for ( size_t i = 0; i < list_size; ++i ) { + void refuse_read_helper ( std::function get_value, size_t list_size ) + { + if ( list_size > 0 ) + { + for ( size_t i = 0; i < list_size; ++i ) + { const std::string creature_id = get_value ( i ); const int idx = find_creature ( creature_id ); const df::creature_raw* creature = find_creature ( idx ); - if ( idx < 0 || !refuse_creature_is_allowed ( creature ) ) { + if ( idx < 0 || !refuse_creature_is_allowed ( creature ) ) + { *mOut << "invalid refuse creature: " << creature_id << endl; continue; } @@ -1222,8 +1399,10 @@ private: - void read_refuse() { - if ( mBuffer.has_refuse() ) { + void read_refuse() + { + if ( mBuffer.has_refuse() ) + { const StockpileSettings::RefuseSet refuse = mBuffer.refuse(); *mOut << "refuse: " < const std::string& { + unserialize_list_item_type ( filter, [=] ( const size_t & idx ) -> const std::string& + { return refuse.type ( idx ); }, refuse.type_size() ); // corpses *mOut << " corpses" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.corpses ( idx ); }, refuse.corpses_size() ); // body_parts *mOut << " body_parts" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.body_parts ( idx ); }, refuse.body_parts_size() ); // skulls *mOut << " skulls" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.skulls ( idx ); }, refuse.skulls_size() ); // bones *mOut << " bones" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.bones ( idx ); }, refuse.bones_size() ); // hair *mOut << " hair" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.hair ( idx ); }, refuse.hair_size() ); // shells *mOut << " shells" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.shells ( idx ); }, refuse.shells_size() ); // teeth *mOut << " teeth" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.teeth ( idx ); }, refuse.teeth_size() ); // horns *mOut << " horns" << endl; - refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { + refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& + { return refuse.horns ( idx ); }, refuse.horns_size() ); } } - bool stone_is_allowed ( const MaterialInfo &mi ) { + bool stone_is_allowed ( const MaterialInfo &mi ) + { if ( !mi.isValid() ) return false; const bool is_allowed_soil = mi.inorganic->flags.is_set ( inorganic_flags::SOIL ) && !mi.inorganic->flags.is_set ( inorganic_flags::AQUIFER ); const bool is_allowed_stone = mi.material->flags.is_set ( material_flags::IS_STONE ) && !mi.material->flags.is_set ( material_flags::NO_STONE_STOCKPILE ); return is_allowed_soil || is_allowed_stone; } - void write_stone() { + void write_stone() + { StockpileSettings::StoneSet *stone= mBuffer.mutable_stone(); MaterialInfo mi; - for ( size_t i = 0; i < mPile->settings.stone.mats.size(); ++i ) { - if ( mPile->settings.stone.mats.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.stone.mats.size(); ++i ) + { + if ( mPile->settings.stone.mats.at ( i ) ) + { mi.decode ( 0, i ); if ( !stone_is_allowed ( mi ) ) continue; *mOut << "stone mat: " << i << " is " << mi.getToken() << endl; @@ -1298,11 +1490,14 @@ private: } } - void read_stone() { - if ( mBuffer.has_stone() ) { + void read_stone() + { + if ( mBuffer.has_stone() ) + { const StockpileSettings::StoneSet stone = mBuffer.stone(); *mOut << "stone: " <flags.is_set ( material_flags::IS_METAL ); } - void write_ammo() { + void write_ammo() + { StockpileSettings::AmmoSet *ammo = mBuffer.mutable_ammo(); // ammo type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { ammo->add_type ( token ); }, mPile->settings.ammo.type, std::vector ( world->raws.itemdefs.ammo.begin(),world->raws.itemdefs.ammo.end() ), @@ -1328,8 +1526,10 @@ private: // metal MaterialInfo mi; - for ( size_t i = 0; i < mPile->settings.ammo.mats.size(); ++i ) { - if ( mPile->settings.ammo.mats.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.ammo.mats.size(); ++i ) + { + if ( mPile->settings.ammo.mats.at ( i ) ) + { mi.decode ( 0, i ); if ( !ammo_mat_is_allowed ( mi ) ) continue; ammo->add_mats ( mi.getToken() ); @@ -1337,11 +1537,13 @@ private: } *mOut << "after metal" << endl; // other mats - if ( mPile->settings.ammo.other_mats.size() > 2 ) { + if ( mPile->settings.ammo.other_mats.size() > 2 ) + { *mOut << "WARNING: ammo other materials > 2! " << mPile->settings.ammo.other_mats.size() << endl; } *mOut << "after other check" << endl; - for ( size_t i = 0; i < std::min ( size_t ( 2 ), mPile->settings.ammo.other_mats.size() ); ++i ) { + for ( size_t i = 0; i < std::min ( size_t ( 2 ), mPile->settings.ammo.other_mats.size() ); ++i ) + { if ( !mPile->settings.ammo.other_mats.at ( i ) ) continue; const std::string token = i == 0 ? "WOOD" : "BONE"; @@ -1350,27 +1552,33 @@ private: } *mOut << "after other" << endl; // quality core - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { ammo->add_quality_core ( token ); }, mPile->settings.ammo.quality_core ); // quality total - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { ammo->add_quality_total ( token ); }, mPile->settings.ammo.quality_total ); } - void read_ammo() { - if ( mBuffer.has_ammo() ) { + void read_ammo() + { + if ( mBuffer.has_ammo() ) + { const StockpileSettings::AmmoSet ammo = mBuffer.ammo(); *mOut << "ammo: " < const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return ammo.type ( idx ); }, ammo.type_size() ); // metals - for ( int i = 0; i < ammo.mats_size(); ++i ) { + for ( int i = 0; i < ammo.mats_size(); ++i ) + { const std::string token = ammo.mats ( i ); MaterialInfo mi; mi.find ( token ); @@ -1379,31 +1587,38 @@ private: } // others - for ( int i = 0; i < ammo.other_mats_size(); ++i ) { + for ( int i = 0; i < ammo.other_mats_size(); ++i ) + { const std::string token = ammo.other_mats ( i ); const int32_t idx = token == "WOOD" ? 0 : token == "BONE" ? 1 : -1; *mOut << " other mats " << idx << " is " << token << endl; } // core quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return ammo.quality_core ( idx ); }, ammo.quality_core_size() ); // total quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return ammo.quality_total ( idx ); }, ammo.quality_total_size() ); } } - bool coins_mat_is_allowed ( const MaterialInfo &mi ) { + bool coins_mat_is_allowed ( const MaterialInfo &mi ) + { return mi.isValid(); } - void write_coins() { + void write_coins() + { StockpileSettings::CoinSet *coins = mBuffer.mutable_coin(); MaterialInfo mi; - for ( size_t i = 0; i < mPile->settings.coins.mats.size(); ++i ) { - if ( mPile->settings.coins.mats.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.coins.mats.size(); ++i ) + { + if ( mPile->settings.coins.mats.at ( i ) ) + { mi.decode ( 0, i ); if ( !coins_mat_is_allowed ( mi ) ) continue; *mOut << " coin mat" << i << " is " << mi.getToken() << endl; @@ -1413,11 +1628,14 @@ private: } - void read_coins() { - if ( mBuffer.has_coin() ) { + void read_coins() + { + if ( mBuffer.has_coin() ) + { const StockpileSettings::CoinSet coins = mBuffer.coin(); *mOut << "coins: " <flags.is_set ( material_flags::IS_METAL ); } - bool blocks_mat_is_allowed ( const MaterialInfo &mi ) { + bool blocks_mat_is_allowed ( const MaterialInfo &mi ) + { return mi.isValid() && mi.material && !mi.material->flags.is_set ( material_flags::IS_GEM ) && mi.material->flags.is_set ( material_flags::ITEMS_HARD ); } - void write_bars_blocks() { + void write_bars_blocks() + { StockpileSettings::BarsBlocksSet *bars_blocks = mBuffer.mutable_barsblocks(); MaterialInfo mi; FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::bars_mat_is_allowed, this, _1 ); - serialize_list_material ( filter, [=] ( const std::string &token ) { + serialize_list_material ( filter, [=] ( const std::string &token ) + { bars_blocks->add_bars_mats ( token ); }, mPile->settings.bars_blocks.bars_mats ); // blocks mats filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); - serialize_list_material ( filter, [=] ( const std::string &token ) { + serialize_list_material ( filter, [=] ( const std::string &token ) + { bars_blocks->add_blocks_mats ( token ); }, mPile->settings.bars_blocks.blocks_mats ); // bars other mats - serialize_list_other_mats ( mOtherMatsBars, [=] ( const std::string &token ) { + serialize_list_other_mats ( mOtherMatsBars, [=] ( const std::string &token ) + { bars_blocks->add_bars_other_mats ( token ); }, mPile->settings.bars_blocks.bars_other_mats ); // blocks other mats - serialize_list_other_mats ( mOtherMatsBlocks, [=] ( const std::string &token ) { + serialize_list_other_mats ( mOtherMatsBlocks, [=] ( const std::string &token ) + { bars_blocks->add_blocks_other_mats ( token ); }, mPile->settings.bars_blocks.blocks_other_mats ); } - void read_bars_blocks() { - if ( mBuffer.has_barsblocks() ) { + void read_bars_blocks() + { + if ( mBuffer.has_barsblocks() ) + { const StockpileSettings::BarsBlocksSet bars_blocks = mBuffer.barsblocks(); *mOut << "bars_blocks: " < const std::string& { + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& + { return bars_blocks.bars_mats ( idx ); }, bars_blocks.bars_mats_size() ); // blocks filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); - unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& + { return bars_blocks.blocks_mats ( idx ); }, bars_blocks.blocks_mats_size() ); // bars other mats - unserialize_list_other_mats ( mOtherMatsBars, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsBars, [=] ( const size_t & idx ) -> const std::string& + { return bars_blocks.bars_other_mats ( idx ); }, bars_blocks.bars_other_mats_size() ); // blocks other mats - unserialize_list_other_mats ( mOtherMatsBlocks, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsBlocks, [=] ( const size_t & idx ) -> const std::string& + { return bars_blocks.blocks_other_mats ( idx ); }, bars_blocks.blocks_other_mats_size() ); } } - bool gem_mat_is_allowed ( const MaterialInfo &mi ) { + bool gem_mat_is_allowed ( const MaterialInfo &mi ) + { return mi.isValid() && mi.material && mi.material->flags.is_set ( material_flags::IS_GEM ); } - bool gem_cut_mat_is_allowed ( const MaterialInfo &mi ) { + bool gem_cut_mat_is_allowed ( const MaterialInfo &mi ) + { return mi.isValid() && mi.material && ( mi.material->flags.is_set ( material_flags::IS_GEM ) || mi.material->flags.is_set ( material_flags::IS_STONE ) ) ; } - void write_gems() { + void write_gems() + { StockpileSettings::GemsSet *gems = mBuffer.mutable_gems(); MaterialInfo mi; // rough mats FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::gem_mat_is_allowed, this, _1 ); - serialize_list_material ( filter, [=] ( const std::string &token ) { + serialize_list_material ( filter, [=] ( const std::string &token ) + { gems->add_rough_mats ( token ); }, mPile->settings.gems.rough_mats ); // cut mats filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); - serialize_list_material ( filter, [=] ( const std::string &token ) { + serialize_list_material ( filter, [=] ( const std::string &token ) + { gems->add_cut_mats ( token ); }, mPile->settings.gems.cut_mats ); // rough other - for ( size_t i = 0; i < mPile->settings.gems.rough_other_mats.size(); ++i ) { - if ( mPile->settings.gems.rough_other_mats.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.gems.rough_other_mats.size(); ++i ) + { + if ( mPile->settings.gems.rough_other_mats.at ( i ) ) + { mi.decode ( i, -1 ); if ( !mi.isValid() ) continue; *mOut << " gem rough_other mat" << i << " is " << mi.getToken() << endl; @@ -1533,8 +1772,10 @@ private: } } // cut other - for ( size_t i = 0; i < mPile->settings.gems.cut_other_mats.size(); ++i ) { - if ( mPile->settings.gems.cut_other_mats.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.gems.cut_other_mats.size(); ++i ) + { + if ( mPile->settings.gems.cut_other_mats.at ( i ) ) + { mi.decode ( i, -1 ); if ( !mi.isValid() ) mi.decode ( 0, i ); if ( !mi.isValid() ) continue; @@ -1543,24 +1784,29 @@ private: } } } - void read_gems() { - if ( mBuffer.has_gems() ) { + void read_gems() + { + if ( mBuffer.has_gems() ) + { const StockpileSettings::GemsSet gems = mBuffer.gems(); *mOut << "gems: " < const std::string& { + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& + { return gems.rough_mats ( idx ); }, gems.rough_mats_size() ); // cut filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); - unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& + { return gems.cut_mats ( idx ); }, gems.cut_mats_size() ); // rough other - for ( int i = 0; i < gems.rough_other_mats_size(); ++i ) { + for ( int i = 0; i < gems.rough_other_mats_size(); ++i ) + { const std::string token = gems.rough_other_mats ( i ); MaterialInfo mi; mi.find ( token ); @@ -1568,7 +1814,8 @@ private: *mOut << " rough_other mats " << mi.type << " is " << token << endl; } // cut other - for ( int i = 0; i < gems.cut_other_mats_size(); ++i ) { + for ( int i = 0; i < gems.cut_other_mats_size(); ++i ) + { const std::string token = gems.cut_other_mats ( i ); MaterialInfo mi; mi.find ( token ); @@ -1578,8 +1825,10 @@ private: } } - bool finished_goods_type_is_allowed ( item_type::item_type type ) { - switch ( type ) { + bool finished_goods_type_is_allowed ( item_type::item_type type ) + { + switch ( type ) + { case item_type::CHAIN: case item_type::FLASK: case item_type::GOBLET: @@ -1612,7 +1861,8 @@ private: } - void finished_goods_setup_other_mats() { + void finished_goods_setup_other_mats() + { mOtherMatsFinishedGoods.insert ( std::make_pair ( 0,"WOOD" ) ); mOtherMatsFinishedGoods.insert ( std::make_pair ( 1,"PLANT_CLOTH" ) ); mOtherMatsFinishedGoods.insert ( std::make_pair ( 2,"BONE" ) ); @@ -1631,7 +1881,8 @@ private: mOtherMatsFinishedGoods.insert ( std::make_pair ( 15,"WAX" ) ); } - bool finished_goods_mat_is_allowed ( const MaterialInfo &mi ) { + bool finished_goods_mat_is_allowed ( const MaterialInfo &mi ) + { return mi.isValid() && mi.material && ( mi.material->flags.is_set ( material_flags::IS_GEM ) @@ -1639,173 +1890,214 @@ private: || mi.material->flags.is_set ( material_flags::IS_STONE ) ) ; } - void write_finished_goods() { + void write_finished_goods() + { StockpileSettings::FinishedGoodsSet *finished_goods = mBuffer.mutable_finished_goods(); // type FuncItemAllowed filter = std::bind ( &StockpileSerializer::finished_goods_type_is_allowed, this, _1 ); - serialize_list_item_type ( filter, [=] ( const std::string &token ) { + serialize_list_item_type ( filter, [=] ( const std::string &token ) + { finished_goods->add_type ( token ); }, mPile->settings.finished_goods.type ); // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); - serialize_list_material ( mat_filter, [=] ( const std::string &token ) { + serialize_list_material ( mat_filter, [=] ( const std::string &token ) + { finished_goods->add_mats ( token ); }, mPile->settings.finished_goods.mats ); // other mats - serialize_list_other_mats ( mOtherMatsFinishedGoods, [=] ( const std::string &token ) { + serialize_list_other_mats ( mOtherMatsFinishedGoods, [=] ( const std::string &token ) + { finished_goods->add_other_mats ( token ); }, mPile->settings.finished_goods.other_mats ); // quality core - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { finished_goods->add_quality_core ( token ); }, mPile->settings.finished_goods.quality_core ); // quality total - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { finished_goods->add_quality_total ( token ); }, mPile->settings.finished_goods.quality_total ); } - void read_finished_goods() { - if ( mBuffer.has_finished_goods() ) { + void read_finished_goods() + { + if ( mBuffer.has_finished_goods() ) + { const StockpileSettings::FinishedGoodsSet finished_goods = mBuffer.finished_goods(); *mOut << "finished_goods: " < const std::string& { + unserialize_list_item_type ( filter, [=] ( const size_t & idx ) -> const std::string& + { return finished_goods.type ( idx ); }, finished_goods.type_size() ); // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); - unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& + { return finished_goods.mats ( idx ); }, finished_goods.mats_size() ); // other mats - unserialize_list_other_mats ( mOtherMatsFinishedGoods, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsFinishedGoods, [=] ( const size_t & idx ) -> const std::string& + { return finished_goods.other_mats ( idx ); }, finished_goods.other_mats_size() ); // core quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return finished_goods.quality_core ( idx ); }, finished_goods.quality_core_size() ); // total quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return finished_goods.quality_total ( idx ); }, finished_goods.quality_total_size() ); } } - void write_leather() { + void write_leather() + { StockpileSettings::LeatherSet *leather = mBuffer.mutable_leather(); - FuncWriteExport setter = [=] ( const std::string &id ) { + FuncWriteExport setter = [=] ( const std::string &id ) + { leather->add_mats ( id ); }; serialize_list_organic_mat ( setter, mPile->settings.leather.mats, organic_mat_category::Leather ); } - void read_leather() { - if ( mBuffer.has_leather() ) { + void read_leather() + { + if ( mBuffer.has_leather() ) + { const StockpileSettings::LeatherSet leather = mBuffer.leather(); *mOut << "leather: " < std::string { + FuncReadImport getter = [=] ( size_t idx ) -> std::string + { return leather.mats ( idx ); }; unserialize_list_organic_mat ( getter, leather.mats_size(), organic_mat_category::Leather ); } } - void write_cloth() { + void write_cloth() + { StockpileSettings::ClothSet * cloth = mBuffer.mutable_cloth(); - serialize_list_organic_mat ( [=] ( const std::string &token ) { + 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 ) { + 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 ) { + 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 ) { + 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 ) { + 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 ) { + 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 ) { + 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 ) { + 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() ) { + void read_cloth() + { + if ( mBuffer.has_cloth() ) + { const StockpileSettings::ClothSet cloth = mBuffer.cloth(); *mOut << "cloth: " < std::string { + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string + { return cloth.thread_silk ( idx ); }, cloth.thread_silk_size(), organic_mat_category::Silk ); - unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { + 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 { + 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 { + 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 { + 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 { + 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 { + 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 { + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string + { return cloth.cloth_metal ( idx ); }, cloth.cloth_metal_size(), organic_mat_category::MetalThread ); } } - bool wood_mat_is_allowed ( const df::plant_raw * plant ) { + bool wood_mat_is_allowed ( const df::plant_raw * plant ) + { return plant && plant->flags.is_set ( plant_raw_flags::TREE ); } - void write_wood() { + void write_wood() + { StockpileSettings::WoodSet * wood = mBuffer.mutable_wood(); - for ( size_t i = 0; i < mPile->settings.wood.mats.size(); ++i ) { - if ( mPile->settings.wood.mats.at ( i ) ) { + for ( size_t i = 0; i < mPile->settings.wood.mats.size(); ++i ) + { + if ( mPile->settings.wood.mats.at ( i ) ) + { const df::plant_raw * plant = find_plant ( i ); if ( !wood_mat_is_allowed ( plant ) ) continue; wood->add_mats ( plant->id ); @@ -1813,12 +2105,15 @@ private: } } } - void read_wood() { - if ( mBuffer.has_wood() ) { + void read_wood() + { + if ( mBuffer.has_wood() ) + { const StockpileSettings::WoodSet wood = mBuffer.wood(); *mOut << "wood: " <flags.is_set ( material_flags::IS_METAL ) || mi.material->flags.is_set ( material_flags::IS_STONE ) ); } - void write_weapons() { + void write_weapons() + { StockpileSettings::WeaponsSet * weapons = mBuffer.mutable_weapons(); weapons->set_unusable ( mPile->settings.weapons.unusable ); weapons->set_usable ( mPile->settings.weapons.usable ); // weapon type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { weapons->add_weapon_type ( token ); }, mPile->settings.weapons.weapon_type, std::vector ( world->raws.itemdefs.weapons.begin(),world->raws.itemdefs.weapons.end() ), item_type::WEAPON ); // trapcomp type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { weapons->add_trapcomp_type ( token ); }, mPile->settings.weapons.trapcomp_type, std::vector ( world->raws.itemdefs.trapcomps.begin(),world->raws.itemdefs.trapcomps.end() ), @@ -1856,28 +2155,34 @@ private: // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::weapons_mat_is_allowed, this, _1 ); - serialize_list_material ( mat_filter, [=] ( const std::string &token ) { + serialize_list_material ( mat_filter, [=] ( const std::string &token ) + { weapons->add_mats ( token ); }, mPile->settings.weapons.mats ); // other mats - serialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const std::string &token ) { + serialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const std::string &token ) + { weapons->add_other_mats ( token ); }, mPile->settings.weapons.other_mats ); // quality core - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { weapons->add_quality_core ( token ); }, mPile->settings.weapons.quality_core ); // quality total - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { weapons->add_quality_total ( token ); }, mPile->settings.weapons.quality_total ); } - void read_weapons() { - if ( mBuffer.has_weapons() ) { + void read_weapons() + { + if ( mBuffer.has_weapons() ) + { const StockpileSettings::WeaponsSet weapons = mBuffer.weapons(); *mOut << "weapons: " < const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return weapons.weapon_type ( idx ); }, weapons.weapon_type_size() ); // trapcomp type - unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return weapons.trapcomp_type ( idx ); }, weapons.trapcomp_type_size() ); // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::weapons_mat_is_allowed, this, _1 ); - unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& + { return weapons.mats ( idx ); }, weapons.mats_size() ); // other mats - unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& + { return weapons.other_mats ( idx ); }, weapons.other_mats_size() ); // core quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return weapons.quality_core ( idx ); }, weapons.quality_core_size() ); // total quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return weapons.quality_total ( idx ); }, weapons.quality_total_size() ); } } - void weapons_armor_setup_other_mats() { + void weapons_armor_setup_other_mats() + { mOtherMatsWeaponsArmor.insert ( std::make_pair ( 0,"WOOD" ) ); mOtherMatsWeaponsArmor.insert ( std::make_pair ( 1,"PLANT_CLOTH" ) ); mOtherMatsWeaponsArmor.insert ( std::make_pair ( 2,"BONE" ) ); @@ -1932,53 +2244,61 @@ private: mOtherMatsWeaponsArmor.insert ( std::make_pair ( 9,"YARN" ) ); } - bool armor_mat_is_allowed ( const MaterialInfo &mi ) { + bool armor_mat_is_allowed ( const MaterialInfo &mi ) + { return mi.isValid() && mi.material && mi.material->flags.is_set ( material_flags::IS_METAL ); } - void write_armor() { + void write_armor() + { StockpileSettings::ArmorSet * armor = mBuffer.mutable_armor(); armor->set_unusable ( mPile->settings.armor.unusable ); armor->set_usable ( mPile->settings.armor.usable ); // armor type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { armor->add_body ( token ); }, mPile->settings.armor.body, std::vector ( world->raws.itemdefs.armor.begin(),world->raws.itemdefs.armor.end() ), item_type::ARMOR ); // helm type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { armor->add_head ( token ); }, mPile->settings.armor.head, std::vector ( world->raws.itemdefs.helms.begin(),world->raws.itemdefs.helms.end() ), item_type::HELM ); // shoes type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { armor->add_feet ( token ); }, mPile->settings.armor.feet, std::vector ( world->raws.itemdefs.shoes.begin(),world->raws.itemdefs.shoes.end() ), item_type::SHOES ); // gloves type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { armor->add_hands ( token ); }, mPile->settings.armor.hands, std::vector ( world->raws.itemdefs.gloves.begin(),world->raws.itemdefs.gloves.end() ), item_type::GLOVES ); // pant type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { armor->add_legs ( token ); }, mPile->settings.armor.legs, std::vector ( world->raws.itemdefs.pants.begin(),world->raws.itemdefs.pants.end() ), item_type::PANTS ); // shield type - serialize_list_itemdef ( [=] ( const std::string &token ) { + serialize_list_itemdef ( [=] ( const std::string &token ) + { armor->add_shield ( token ); }, mPile->settings.armor.shield, std::vector ( world->raws.itemdefs.shields.begin(),world->raws.itemdefs.shields.end() ), @@ -1986,28 +2306,34 @@ private: // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::armor_mat_is_allowed, this, _1 ); - serialize_list_material ( mat_filter, [=] ( const std::string &token ) { + serialize_list_material ( mat_filter, [=] ( const std::string &token ) + { armor->add_mats ( token ); }, mPile->settings.armor.mats ); // other mats - serialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const std::string &token ) { + serialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const std::string &token ) + { armor->add_other_mats ( token ); }, mPile->settings.armor.other_mats ); // quality core - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { armor->add_quality_core ( token ); }, mPile->settings.armor.quality_core ); // quality total - serialize_list_quality ( [=] ( const std::string &token ) { + serialize_list_quality ( [=] ( const std::string &token ) + { armor->add_quality_total ( token ); }, mPile->settings.armor.quality_total ); } - void read_armor() { - if ( mBuffer.has_armor() ) { + void read_armor() + { + if ( mBuffer.has_armor() ) + { const StockpileSettings::ArmorSet armor = mBuffer.armor(); *mOut << "armor: " < const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return armor.body ( idx ); }, armor.body_size() ); // head type - unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return armor.head ( idx ); }, armor.head_size() ); // feet type - unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return armor.feet ( idx ); }, armor.feet_size() ); // hands type - unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return armor.hands ( idx ); }, armor.hands_size() ); // legs type - unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return armor.legs ( idx ); }, armor.legs_size() ); // shield type - unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& + { return armor.shield ( idx ); }, armor.shield_size() ); // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::armor_mat_is_allowed, this, _1 ); - unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& + { return armor.mats ( idx ); }, armor.mats_size() ); // other mats - unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& + { return armor.other_mats ( idx ); }, armor.other_mats_size() ); // core quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return armor.quality_core ( idx ); }, armor.quality_core_size() ); // total quality - unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { + unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& + { return armor.quality_total ( idx ); }, armor.quality_total_size() ); } } }; -static bool file_exists(const std::string& filename ) +static bool file_exists ( const std::string& filename ) { struct stat buf; - if ( stat ( filename.c_str(), &buf ) != -1 ) { + if ( stat ( filename.c_str(), &buf ) != -1 ) + { return true; } return false; } -static bool is_dfstockfile(const std::string& filename) { - return filename.rfind(".dfstock") != std::string::npos; +static bool is_dfstockfile ( const std::string& filename ) +{ + return filename.rfind ( ".dfstock" ) != std::string::npos; } // exporting static command_result savestock ( color_ostream &out, vector & parameters ) { building_stockpilest *sp = virtual_cast ( world->selected_building ); - if ( !sp ) { + if ( !sp ) + { out.printerr ( "Selected building isn't a stockpile.\n" ); return CR_WRONG_USAGE; } StockpileSerializer cereal ( out, sp ); - if ( parameters.size() < 1 ) { + if ( parameters.size() < 1 ) + { std::string data = cereal.serialize_to_string(); out << data << endl; - } else { + } + else + { std::string file = parameters.at ( 0 ); if ( !is_dfstockfile ( file ) ) file += ".dfstock"; - if ( !cereal.serialize_to_file ( file ) ) { + if ( !cereal.serialize_to_file ( file ) ) + { out << "serialize failed" << endl; return CR_FAILURE; } @@ -2111,21 +2454,24 @@ static command_result savestock ( color_ostream &out, vector & paramete static command_result loadstock ( color_ostream &out, vector & parameters ) { building_stockpilest *sp = virtual_cast ( world->selected_building ); - if ( !sp ) { + if ( !sp ) + { out.printerr ( "Selected building isn't a stockpile.\n" ); return CR_WRONG_USAGE; } if ( parameters.size() != 1 || !file_exists ( parameters.at ( 0 ) ) || - !is_dfstockfile ( parameters.at ( 0 ) ) ) { - out << parameters.size() << "\t" << file_exists(parameters.at(0)) << "\t" << is_dfstockfile(parameters.at(0)) << endl; + !is_dfstockfile ( parameters.at ( 0 ) ) ) + { + out << parameters.size() << "\t" << file_exists ( parameters.at ( 0 ) ) << "\t" << is_dfstockfile ( parameters.at ( 0 ) ) << endl; out.printerr ( "loadstock: first parameter must be a .dfstock file\n" ); return CR_WRONG_USAGE; } StockpileSerializer cereal ( out, sp ); - if ( !cereal.unserialize_from_file ( parameters.at ( 0 ) ) ) { + if ( !cereal.unserialize_from_file ( parameters.at ( 0 ) ) ) + { out << "unserialize failed" << endl; return CR_FAILURE; } From c251aeabe3e4f7e17ff01d6c41c5317c282df1d9 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 20 Nov 2014 11:53:33 +0100 Subject: [PATCH 12/36] stockpiles: add optional debug output --- plugins/stockpiles.cpp | 299 +++++++++++++++++++++++++---------------- 1 file changed, 183 insertions(+), 116 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 8388b34f4..ae7e9d421 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -91,20 +91,16 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector : filename to save stockpile settings to (will overwrite!)\n" ) ); commands.push_back ( PluginCommand ( "loadstock", "Import stockpile settings and aplply them to the stockpile under cursor.", loadstock, loadstock_guard, - " - In 'q' or 't' mode: select a stockpile and invoke in order\n" - " to switch to the 'p' stockpile creation mode, and initialize\n" - " the custom settings from the selected stockpile.\n" - " - In 'p': invoke in order to switch back to 'q'.\n" + " -d, --debug: enable debug output\n" + " : filename to load stockpile settings from\n" ) ); } @@ -267,7 +263,7 @@ public: df::caste_raw * caste; FoodMat() : material ( -1 ), creature ( 0 ), caste ( 0 ) {} }; - static void food_mat_by_idx ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector::size_type food_idx, FoodMat & food_mat ) + static void food_mat_by_idx ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector::size_type food_idx, FoodMat & food_mat ) { out << "food_lookup: food_idx(" << food_idx << ") "; df::world_raws &raws = world->raws; @@ -288,7 +284,7 @@ public: out << " type(" << type << ") index("<< main_idx <<")" <::size_type idx ) + static std::string food_token_by_idx ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector::size_type idx ) { FoodMat food_mat; food_mat_by_idx ( out, mat_category, idx, food_mat ); @@ -303,7 +299,7 @@ public: return std::string(); } - static void food_build_map ( color_ostream &out ) + static void food_build_map ( std::ostream &out ) { if ( index_built ) return; @@ -323,7 +319,7 @@ public: index_built = true; } - static int food_idx_by_token ( color_ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) + static int food_idx_by_token ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) { int16_t food_idx = -1; df::world_raws &raws = world->raws; @@ -374,7 +370,7 @@ public: return food_idx; } - static MaterialInfo food_mat_by_token ( color_ostream &out, const std::string & token ) + static MaterialInfo food_mat_by_token ( std::ostream &out, const std::string & token ) { MaterialInfo mat_info; mat_info.find ( token ); @@ -390,6 +386,27 @@ private: bool OrganicMatLookup::index_built = false; std::vector OrganicMatLookup::food_index = std::vector ( 37 ); + +/** + * Null buffer that acts like /dev/null for when debug is disabled + */ +class NullBuffer : public std::streambuf +{ +public: + int overflow ( int c ) + { + return c; + } +}; + +class NullStream : public std::ostream +{ +public: + NullStream() : std::ostream ( &m_sb ) {} +private: + NullBuffer m_sb; +}; + /** * Class for serializing the stockpile_settings structure into a Google protobuf */ @@ -400,8 +417,10 @@ public: * @param out for debugging * @param stockpile stockpile to read or write settings to */ - StockpileSerializer ( color_ostream &out, building_stockpilest const * stockpile ) - : mOut ( &out ) + StockpileSerializer ( building_stockpilest const * stockpile ) + : mDebug ( false ) + , mOut ( 0 ) + , mNull() , mPile ( stockpile ) { @@ -414,6 +433,12 @@ public: ~StockpileSerializer() {} + void enable_debug ( color_ostream&out ) + { + mDebug = true; + mOut = &out; + } + /** * Will serialize stockpile settings to a file (overwrites existing files) * @return success/failure @@ -463,16 +488,31 @@ public: private: -// color_ostream & debug() { -// return *mOut; -// } + bool mDebug; + std::ostream * mOut; + NullStream mNull; + building_stockpilest const * mPile; + StockpileSettings mBuffer; + std::map mOtherMatsFurniture; + std::map mOtherMatsFinishedGoods; + std::map mOtherMatsBars; + std::map mOtherMatsBlocks; + std::map mOtherMatsWeaponsArmor; + + + std::ostream & debug() + { + if ( mDebug ) + return *mOut; + return mNull; + } /** read memory structures and serialize to protobuf */ void write() { -// *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; +// debug() << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; write_general(); if ( mPile->settings.flags.bits.animals ) write_animals(); @@ -509,7 +549,7 @@ private: // parse serialized data into ui indices void read () { - *mOut << endl << "==READ==" << endl; + debug() << endl << "==READ==" << endl; read_general(); read_animals(); read_food(); @@ -528,16 +568,6 @@ private: read_armor(); } - color_ostream * mOut; - building_stockpilest const * mPile; - StockpileSettings mBuffer; - std::map mOtherMatsFurniture; - std::map mOtherMatsFinishedGoods; - std::map mOtherMatsBars; - std::map mOtherMatsBlocks; - std::map mOtherMatsWeaponsArmor; - - /** * Find an enum's value based off the string label. * @param traits the enum's trait struct @@ -545,7 +575,7 @@ private: * @return the enum's value */ template - static typename df::enum_traits::base_type linear_index ( color_ostream& out, df::enum_traits traits, const std::string &token ) + static typename df::enum_traits::base_type linear_index ( std::ostream & out, df::enum_traits traits, const std::string &token ) { auto j = traits.first_item_value; auto limit = traits.last_item_value; @@ -611,14 +641,14 @@ private: { if ( list.at ( i ) ) { - std::string token = OrganicMatLookup::food_token_by_idx ( *mOut, cat, i ); + std::string token = OrganicMatLookup::food_token_by_idx ( debug(), cat, i ); if ( !token.empty() ) { add_value ( token ); } else { - *mOut << "food mat invalid :(" << endl; + debug() << "food mat invalid :(" << endl; } } } @@ -634,8 +664,8 @@ private: 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; + int idx = OrganicMatLookup::food_idx_by_token ( debug(), cat, token ); + debug() << " organic_material " << idx << " is " << token << endl; //mPile->settings.food.meat.at(idx) = (char) 1; } } @@ -648,7 +678,7 @@ private: { using df::enums::item_type::item_type; df::enum_traits type_traits; - *mOut << "item_type size = " << list.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; + debug() << "item_type size = " << list.size() << " size limit = " << type_traits.last_item_value << " typecasted: " << ( size_t ) type_traits.last_item_value << endl; for ( size_t i = 0; i <= ( size_t ) type_traits.last_item_value; ++i ) { if ( list.at ( i ) ) @@ -657,7 +687,7 @@ private: std::string r_type ( type_traits.key_table[i+1] ); if ( !is_allowed ( type ) ) continue; add_value ( r_type ); - *mOut << "item_type key_table[" << i+1 << "] type[" << ( int16_t ) type << "] is " << r_type <::base_type idx = linear_index ( *mOut, type_traits, token ) - 1; + const df::enum_traits::base_type idx = linear_index ( debug(), type_traits, token ) - 1; const item_type type = ( item_type ) idx; if ( !is_allowed ( type ) ) continue; - *mOut << " item_type " << idx << " is " << token << endl; + debug() << " item_type " << idx << " is " << token << endl; } } @@ -692,7 +722,7 @@ private: { mi.decode ( 0, i ); if ( !is_allowed ( mi ) ) continue; - *mOut << " material" << i << " is " << mi.getToken() << endl; + debug() << " material" << i << " is " << mi.getToken() << endl; add_value ( mi.getToken() ); } } @@ -709,7 +739,7 @@ private: MaterialInfo mi; mi.find ( token ); if ( !is_allowed ( mi ) ) continue; - *mOut << " material " << mi.index << " is " << token << endl; + debug() << " material " << mi.index << " is " << token << endl; } } @@ -727,7 +757,7 @@ private: { const std::string f_type ( quality_traits.key_table[i] ); add_value ( f_type ); - *mOut << " quality: " << i << " is " << f_type <::base_type idx = linear_index ( *mOut, quality_traits, quality ); + df::enum_traits::base_type idx = linear_index ( debug(), quality_traits, quality ); if ( idx < 0 ) { - *mOut << " invalid quality token " << quality << endl; + debug() << " invalid quality token " << quality << endl; continue; } - *mOut << " quality: " << idx << " is " << quality << endl; + debug() << " quality: " << idx << " is " << quality << endl; } } @@ -764,11 +794,11 @@ private: const std::string token = other_mats_index ( other_mats, i ); if ( token.empty() ) { - *mOut << " invalid other material with index " << i << endl; + debug() << " invalid other material with index " << i << endl; continue; } add_value ( token ); - *mOut << " other mats " << i << " is " << token << endl; + debug() << " other mats " << i << " is " << token << endl; } } } @@ -784,10 +814,10 @@ private: size_t idx = other_mats_token ( other_mats, token ); if ( idx < 0 ) { - *mOut << "invalid other mat with token " << token; + debug() << "invalid other mat with token " << token; continue; } - *mOut << " other_mats " << idx << " is " << token << endl; + debug() << " other_mats " << idx << " is " << token << endl; } } @@ -807,7 +837,7 @@ private: ii.decode ( type, i ); if ( !ii.isValid() ) continue; add_value ( ii.getToken() ); - *mOut << " itemdef type" << i << " is " << ii.getToken() << endl; + debug() << " itemdef type" << i << " is " << ii.getToken() << endl; } } } @@ -822,7 +852,7 @@ private: std::string token = read_value ( i ); ItemTypeInfo ii; if ( !ii.find ( token ) ) continue; - *mOut << " itemdef " << ii.subtype << " is " << token << endl; + debug() << " itemdef " << ii.subtype << " is " << token << endl; } } @@ -884,7 +914,7 @@ private: if ( mPile->settings.animals.enabled.at ( i ) == 1 ) { df::creature_raw* r = find_creature ( i ); - *mOut << "creature "<< r->creature_id << " " << i << endl; + debug() << "creature "<< r->creature_id << " " << i << endl; mBuffer.mutable_animals()->add_enabled ( r->creature_id ); } } @@ -895,7 +925,7 @@ private: if ( mBuffer.has_animals() ) { // mPile->settings.flags.bits.animals = true; - *mOut << "animals:" < 0 ) @@ -904,7 +934,7 @@ private: { std::string id = mBuffer.animals().enabled ( i ); int idx = find_creature ( id ); - *mOut << id << " " << idx << endl; + debug() << id << " " << idx << endl; } } } @@ -1133,7 +1163,7 @@ private: if ( mBuffer.has_food() ) { const StockpileSettings::FoodSet food = mBuffer.food(); - *mOut << "food:" < traits; @@ -1196,7 +1226,7 @@ private: { std::string f_type ( type_traits.key_table[i] ); furniture->add_type ( f_type ); - *mOut << "furniture_type " << i << " is " << f_type <add_mats ( mi.getToken() ); } } @@ -1241,7 +1271,7 @@ private: if ( mBuffer.has_furniture() ) { const StockpileSettings::FurnitureSet furniture = mBuffer.furniture(); - *mOut << "furniture:" <::base_type idx = linear_index ( *mOut, type_traits, type ); - *mOut << " type " << idx << " is " << type << endl; + df::enum_traits::base_type idx = linear_index ( debug(), type_traits, type ); + debug() << " type " << idx << " is " << type << endl; } // metal, stone/clay materials for ( int i = 0; i < furniture.mats_size(); ++i ) @@ -1259,7 +1289,7 @@ private: MaterialInfo mi; mi.find ( token ); if ( !furniture_mat_is_allowed ( mi ) ) continue; - *mOut << " mats " << mi.index << " is " << token << endl; + debug() << " mats " << mi.index << " is " << token << endl; } // other materials unserialize_list_other_mats ( mOtherMatsFurniture, [=] ( const size_t & idx ) -> const std::string& @@ -1300,7 +1330,7 @@ private: df::creature_raw* r = find_creature ( i ); // skip forgotten beasts, titans, demons, and night creatures if ( !refuse_creature_is_allowed ( r ) ) continue; - *mOut << "creature "<< r->creature_id << " " << i << endl; + debug() << "creature "<< r->creature_id << " " << i << endl; add_value ( r->creature_id ); } } @@ -1389,10 +1419,10 @@ private: const df::creature_raw* creature = find_creature ( idx ); if ( idx < 0 || !refuse_creature_is_allowed ( creature ) ) { - *mOut << "invalid refuse creature: " << creature_id << endl; + debug() << "invalid refuse creature: " << creature_id << endl; continue; } - *mOut << " creature " << idx << " is " << creature_id << endl; + debug() << " creature " << idx << " is " << creature_id << endl; } } } @@ -1404,9 +1434,9 @@ private: if ( mBuffer.has_refuse() ) { const StockpileSettings::RefuseSet refuse = mBuffer.refuse(); - *mOut << "refuse: " < const std::string& { return refuse.corpses ( idx ); }, refuse.corpses_size() ); // body_parts - *mOut << " body_parts" << endl; + debug() << " body_parts" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.body_parts ( idx ); }, refuse.body_parts_size() ); // skulls - *mOut << " skulls" << endl; + debug() << " skulls" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.skulls ( idx ); }, refuse.skulls_size() ); // bones - *mOut << " bones" << endl; + debug() << " bones" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.bones ( idx ); }, refuse.bones_size() ); // hair - *mOut << " hair" << endl; + debug() << " hair" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.hair ( idx ); }, refuse.hair_size() ); // shells - *mOut << " shells" << endl; + debug() << " shells" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.shells ( idx ); }, refuse.shells_size() ); // teeth - *mOut << " teeth" << endl; + debug() << " teeth" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.teeth ( idx ); }, refuse.teeth_size() ); // horns - *mOut << " horns" << endl; + debug() << " horns" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.horns ( idx ); @@ -1484,7 +1514,7 @@ private: { mi.decode ( 0, i ); if ( !stone_is_allowed ( mi ) ) continue; - *mOut << "stone mat: " << i << " is " << mi.getToken() << endl; + debug() << "stone mat: " << i << " is " << mi.getToken() << endl; stone->add_mats ( mi.getToken() ); } } @@ -1495,14 +1525,14 @@ private: if ( mBuffer.has_stone() ) { const StockpileSettings::StoneSet stone = mBuffer.stone(); - *mOut << "stone: " <add_mats ( mi.getToken() ); } } - *mOut << "after metal" << endl; + debug() << "after metal" << endl; // other mats if ( mPile->settings.ammo.other_mats.size() > 2 ) { - *mOut << "WARNING: ammo other materials > 2! " << mPile->settings.ammo.other_mats.size() << endl; + debug() << "WARNING: ammo other materials > 2! " << mPile->settings.ammo.other_mats.size() << endl; } - *mOut << "after other check" << endl; + debug() << "after other check" << endl; for ( size_t i = 0; i < std::min ( size_t ( 2 ), mPile->settings.ammo.other_mats.size() ); ++i ) { if ( !mPile->settings.ammo.other_mats.at ( i ) ) continue; const std::string token = i == 0 ? "WOOD" : "BONE"; ammo->add_other_mats ( token ); - *mOut << " other mats " << i << " is " << token << endl; + debug() << " other mats " << i << " is " << token << endl; } - *mOut << "after other" << endl; + debug() << "after other" << endl; // quality core serialize_list_quality ( [=] ( const std::string &token ) { @@ -1568,7 +1598,7 @@ private: if ( mBuffer.has_ammo() ) { const StockpileSettings::AmmoSet ammo = mBuffer.ammo(); - *mOut << "ammo: " < const std::string& @@ -1583,7 +1613,7 @@ private: MaterialInfo mi; mi.find ( token ); if ( !ammo_mat_is_allowed ( mi ) ) continue; - *mOut << " mats " << mi.index << " is " << token << endl; + debug() << " mats " << mi.index << " is " << token << endl; } // others @@ -1591,7 +1621,7 @@ private: { const std::string token = ammo.other_mats ( i ); const int32_t idx = token == "WOOD" ? 0 : token == "BONE" ? 1 : -1; - *mOut << " other mats " << idx << " is " << token << endl; + debug() << " other mats " << idx << " is " << token << endl; } // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& @@ -1621,7 +1651,7 @@ private: { mi.decode ( 0, i ); if ( !coins_mat_is_allowed ( mi ) ) continue; - *mOut << " coin mat" << i << " is " << mi.getToken() << endl; + debug() << " coin mat" << i << " is " << mi.getToken() << endl; coins->add_mats ( mi.getToken() ); } } @@ -1633,14 +1663,14 @@ private: if ( mBuffer.has_coin() ) { const StockpileSettings::CoinSet coins = mBuffer.coin(); - *mOut << "coins: " < const std::string& @@ -1767,7 +1797,7 @@ private: { mi.decode ( i, -1 ); if ( !mi.isValid() ) continue; - *mOut << " gem rough_other mat" << i << " is " << mi.getToken() << endl; + debug() << " gem rough_other mat" << i << " is " << mi.getToken() << endl; gems->add_rough_other_mats ( mi.getToken() ); } } @@ -1779,7 +1809,7 @@ private: mi.decode ( i, -1 ); if ( !mi.isValid() ) mi.decode ( 0, i ); if ( !mi.isValid() ) continue; - *mOut << " gem cut_other mat" << i << " is " << mi.getToken() << endl; + debug() << " gem cut_other mat" << i << " is " << mi.getToken() << endl; gems->add_cut_other_mats ( mi.getToken() ); } } @@ -1789,7 +1819,7 @@ private: if ( mBuffer.has_gems() ) { const StockpileSettings::GemsSet gems = mBuffer.gems(); - *mOut << "gems: " < const std::string& @@ -1811,7 +1841,7 @@ private: MaterialInfo mi; mi.find ( token ); if ( !mi.isValid() ) continue; - *mOut << " rough_other mats " << mi.type << " is " << token << endl; + debug() << " rough_other mats " << mi.type << " is " << token << endl; } // cut other for ( int i = 0; i < gems.cut_other_mats_size(); ++i ) @@ -1820,7 +1850,7 @@ private: MaterialInfo mi; mi.find ( token ); if ( !mi.isValid() ) continue; - *mOut << " cut_other mats " << mi.type << " is " << token << endl; + debug() << " cut_other mats " << mi.type << " is " << token << endl; } } } @@ -1931,7 +1961,7 @@ private: if ( mBuffer.has_finished_goods() ) { const StockpileSettings::FinishedGoodsSet finished_goods = mBuffer.finished_goods(); - *mOut << "finished_goods: " < std::string { return leather.mats ( idx ); @@ -2042,7 +2072,7 @@ private: if ( mBuffer.has_cloth() ) { const StockpileSettings::ClothSet cloth = mBuffer.cloth(); - *mOut << "cloth: " < std::string { @@ -2101,7 +2131,7 @@ private: const df::plant_raw * plant = find_plant ( i ); if ( !wood_mat_is_allowed ( plant ) ) continue; wood->add_mats ( plant->id ); - *mOut << " plant " << i << " is " << plant->id << endl; + debug() << " plant " << i << " is " << plant->id << endl; } } } @@ -2110,14 +2140,14 @@ private: if ( mBuffer.has_wood() ) { const StockpileSettings::WoodSet wood = mBuffer.wood(); - *mOut << "wood: " < const std::string& @@ -2335,12 +2365,12 @@ private: if ( mBuffer.has_armor() ) { const StockpileSettings::ArmorSet armor = mBuffer.armor(); - *mOut << "armor: " < const std::string& @@ -2430,15 +2460,34 @@ static command_result savestock ( color_ostream &out, vector & paramete return CR_WRONG_USAGE; } - StockpileSerializer cereal ( out, sp ); - if ( parameters.size() < 1 ) + if ( parameters.size() > 2 ) + { + out.printerr ( "Invalid parameters\n" ); + return CR_WRONG_USAGE; + } + + bool debug = false; + std::string file; + for ( std::string o : parameters ) + { + if ( o == "--debug" || o == "-d" ) + debug = true; + else if ( !o.empty() && o[0] != '-' ) + { + file = o; + } + } + + StockpileSerializer cereal ( sp ); + if ( debug ) + cereal.enable_debug ( out ); + if ( file.empty() ) { std::string data = cereal.serialize_to_string(); out << data << endl; } else { - std::string file = parameters.at ( 0 ); if ( !is_dfstockfile ( file ) ) file += ".dfstock"; if ( !cereal.serialize_to_file ( file ) ) { @@ -2460,16 +2509,34 @@ static command_result loadstock ( color_ostream &out, vector & paramete return CR_WRONG_USAGE; } - if ( parameters.size() != 1 || - !file_exists ( parameters.at ( 0 ) ) || - !is_dfstockfile ( parameters.at ( 0 ) ) ) + if ( parameters.size() < 1 || parameters.size() > 2 ) + { + out.printerr ( "Invalid parameters\n" ); + return CR_WRONG_USAGE; + } + + bool debug = false; + std::string file; + for ( std::string o : parameters ) + { + if ( o == "--debug" || o == "-d" ) + debug = true; + else if ( !o.empty() && o[0] != '-' ) + { + file = o; + } + } + + if ( file.empty() || !file_exists ( file ) || + !is_dfstockfile ( file ) ) { - out << parameters.size() << "\t" << file_exists ( parameters.at ( 0 ) ) << "\t" << is_dfstockfile ( parameters.at ( 0 ) ) << endl; - out.printerr ( "loadstock: first parameter must be a .dfstock file\n" ); + out.printerr ( "loadstock: a .dfstock file is required to import\n" ); return CR_WRONG_USAGE; } - StockpileSerializer cereal ( out, sp ); + StockpileSerializer cereal ( sp ); + if ( debug ) + cereal.enable_debug ( out ); if ( !cereal.unserialize_from_file ( parameters.at ( 0 ) ) ) { out << "unserialize failed" << endl; From 543a88d03ddf375f96a7a595228e52e1904f814a Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 20 Nov 2014 13:37:14 +0100 Subject: [PATCH 13/36] stockpiles: pass correct filename --- plugins/stockpiles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index ae7e9d421..cb4406264 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -2537,7 +2537,7 @@ static command_result loadstock ( color_ostream &out, vector & paramete StockpileSerializer cereal ( sp ); if ( debug ) cereal.enable_debug ( out ); - if ( !cereal.unserialize_from_file ( parameters.at ( 0 ) ) ) + if ( !cereal.unserialize_from_file ( file ) ) { out << "unserialize failed" << endl; return CR_FAILURE; From 44af084e8e2c2ad8d430b7f72ddd1307b7de3459 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 14:37:37 +0100 Subject: [PATCH 14/36] stockpiles: import from disk and write to memory --- plugins/stockpiles.cpp | 859 +++++++++++++++++++++++++++-------------- 1 file changed, 578 insertions(+), 281 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index cb4406264..9bb507df4 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -25,6 +25,7 @@ #include "df/ui.h" #include "df/building_stockpilest.h" +#include "df/stockpile_settings.h" #include "df/global_objects.h" #include "df/viewscreen_dwarfmodest.h" #include @@ -38,14 +39,16 @@ #include #include - -#include "stockpiles.pb.h" - +// protobuf +#include "proto/stockpiles.pb.h" #include +// os #include +// stl #include +#include using std::vector; using std::string; @@ -216,6 +219,7 @@ static df::creature_raw* find_creature ( int32_t idx ) /** * Retrieve creature index from id string + * @return -1 if not found */ static int16_t find_creature ( const std::string &creature_id ) { @@ -232,6 +236,7 @@ static df::plant_raw* find_plant ( size_t idx ) /** * Retrieve plant index from id string + * @return -1 if not found */ static size_t find_plant ( const std::string &plant_id ) { @@ -281,7 +286,7 @@ public: else { food_mat.material.decode ( type, main_idx ); - out << " type(" << type << ") index("<< main_idx <<")" <::size_type idx ) @@ -299,6 +304,11 @@ public: return std::string(); } + static size_t food_max_size ( organic_mat_category::organic_mat_category mat_category ) + { + return world->raws.mat_table.organic_types[mat_category].size(); + } + static void food_build_map ( std::ostream &out ) { if ( index_built ) @@ -319,7 +329,7 @@ public: index_built = true; } - static int food_idx_by_token ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) + static int16_t food_idx_by_token ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, const std::string & token ) { int16_t food_idx = -1; df::world_raws &raws = world->raws; @@ -417,7 +427,7 @@ public: * @param out for debugging * @param stockpile stockpile to read or write settings to */ - StockpileSerializer ( building_stockpilest const * stockpile ) + StockpileSerializer ( building_stockpilest * stockpile ) : mDebug ( false ) , mOut ( 0 ) , mNull() @@ -491,7 +501,7 @@ private: bool mDebug; std::ostream * mOut; NullStream mNull; - building_stockpilest const * mPile; + building_stockpilest * mPile; StockpileSettings mBuffer; std::map mOtherMatsFurniture; std::map mOtherMatsFinishedGoods; @@ -572,7 +582,7 @@ private: * Find an enum's value based off the string label. * @param traits the enum's trait struct * @param token the string value in key_table - * @return the enum's value + * @return the enum's value, -1 if not found */ template static typename df::enum_traits::base_type linear_index ( std::ostream & out, df::enum_traits traits, const std::string &token ) @@ -608,16 +618,18 @@ private: { // exporting FuncWriteExport set_value; - vector stockpile_values; + std::vector * stockpile_values; // importing FuncReadImport get_value; size_t serialized_count; + bool valid = false; - food_pair ( FuncWriteExport s, const vector& sp_v, FuncReadImport g, size_t count ) + food_pair ( FuncWriteExport s, std::vector* sp_v, FuncReadImport g, size_t count ) : set_value ( s ) , stockpile_values ( sp_v ) , get_value ( g ) - ,serialized_count ( count ) + , serialized_count ( count ) + , valid ( true ) {} food_pair() {} }; @@ -635,16 +647,21 @@ private: * * The unserialization process is the same in reverse. */ - void serialize_list_organic_mat ( FuncWriteExport add_value, const vector & list, organic_mat_category::organic_mat_category cat ) + void serialize_list_organic_mat ( FuncWriteExport add_value, const std::vector * list, organic_mat_category::organic_mat_category cat ) { - for ( size_t i = 0; i < list.size(); ++i ) + if ( !list ) { - if ( list.at ( i ) ) + debug() << "serialize_list_organic_mat: list null" << endl; + } + for ( size_t i = 0; i < list->size(); ++i ) + { + if ( list->at ( i ) ) { std::string token = OrganicMatLookup::food_token_by_idx ( debug(), cat, i ); if ( !token.empty() ) { add_value ( token ); + debug() << " organic_material " << i << " is " << token << endl; } else { @@ -657,24 +674,34 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_organic_mat ( FuncReadImport get_value, size_t list_size, organic_mat_category::organic_mat_category cat ) + void unserialize_list_organic_mat ( FuncReadImport get_value, size_t list_size, std::vector *pile_list, organic_mat_category::organic_mat_category cat ) { if ( list_size > 0 ) { + pile_list->resize ( OrganicMatLookup::food_max_size ( cat ), '\0' ); for ( size_t i = 0; i < list_size; ++i ) { std::string token = get_value ( i ); - int idx = OrganicMatLookup::food_idx_by_token ( debug(), cat, token ); + int16_t idx = OrganicMatLookup::food_idx_by_token ( debug(), cat, token ); debug() << " organic_material " << idx << " is " << token << endl; - //mPile->settings.food.meat.at(idx) = (char) 1; + if ( idx >= pile_list->size() ) + { + debug() << "error organic mat index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + pile_list->at ( idx ) = 1; } } + else + { + pile_list->clear(); + } } /** * @see serialize_list_organic_mat */ - void serialize_list_item_type ( FuncItemAllowed is_allowed, FuncWriteExport add_value, std::vector list ) + void serialize_list_item_type ( FuncItemAllowed is_allowed, FuncWriteExport add_value, const std::vector &list ) { using df::enums::item_type::item_type; df::enum_traits type_traits; @@ -695,25 +722,39 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) + void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector *pile_list ) { - using df::enums::item_type::item_type; - df::enum_traits type_traits; - for ( int32_t i = 0; i < list_size; ++i ) + if ( list_size > 0 ) { - const std::string token = read_value ( i ); - // subtract one because item_type starts at -1 - const df::enum_traits::base_type idx = linear_index ( debug(), type_traits, token ) - 1; - const item_type type = ( item_type ) idx; - if ( !is_allowed ( type ) ) continue; - debug() << " item_type " << idx << " is " << token << endl; + using df::enums::item_type::item_type; + df::enum_traits type_traits; + pile_list->resize ( 112, '\0' ); // TODO remove hardcoded list size value + for ( int32_t i = 0; i < list_size; ++i ) + { + const std::string token = read_value ( i ); + // subtract one because item_type starts at -1 + const df::enum_traits::base_type idx = linear_index ( debug(), type_traits, token ) - 1; + const item_type type = ( item_type ) idx; + if ( !is_allowed ( type ) ) continue; + debug() << " item_type " << idx << " is " << token << endl; + if ( idx >= pile_list->size() ) + { + debug() << "error item_type index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + pile_list->at ( idx ) = 1; + } + } + else + { + pile_list->clear(); } } /** * @see serialize_list_organic_mat */ - void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, std::vector list ) + void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, const std::vector &list ) { MaterialInfo mi; for ( size_t i = 0; i < list.size(); ++i ) @@ -731,19 +772,33 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size ) + void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector *pile_list ) { - for ( int i = 0; i < list_size; ++i ) + if ( list_size > 0 ) { - const std::string token = read_value ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !is_allowed ( mi ) ) continue; - debug() << " material " << mi.index << " is " << token << endl; + std::set idx_set; + pile_list->resize ( world->raws.inorganics.size(), '\0' ); + for ( int i = 0; i < list_size; ++i ) + { + const std::string token = read_value ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !is_allowed ( mi ) ) continue; + debug() << " material " << mi.index << " is " << token << endl; + if ( mi.index >= pile_list->size() ) + { + debug() << "error material index too large! idx[" << mi.index << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + pile_list->at ( mi.index ) = 1; + } + } + else + { + pile_list->clear(); } } - /** * @see serialize_list_organic_mat */ @@ -762,23 +817,39 @@ private: } } + /** + * Set all values in a bool[7] to false + */ + void quality_clear ( bool ( &pile_list ) [7] ) + { + std::fill ( pile_list, pile_list+7, false ); + } + /** * @see serialize_list_organic_mat */ - void unserialize_list_quality ( FuncReadImport read_value, int32_t list_size ) + void unserialize_list_quality ( FuncReadImport read_value, int32_t list_size, bool ( &pile_list ) [7] ) { - using df::enums::item_quality::item_quality; - df::enum_traits quality_traits; - for ( int i = 0; i < list_size; ++i ) + if ( list_size > 0 && list_size < 7 ) { - const std::string quality = read_value ( i ); - df::enum_traits::base_type idx = linear_index ( debug(), quality_traits, quality ); - if ( idx < 0 ) + using df::enums::item_quality::item_quality; + df::enum_traits quality_traits; + for ( int i = 0; i < list_size; ++i ) { - debug() << " invalid quality token " << quality << endl; - continue; + const std::string quality = read_value ( i ); + df::enum_traits::base_type idx = linear_index ( debug(), quality_traits, quality ); + if ( idx < 0 ) + { + debug() << " invalid quality token " << quality << endl; + continue; + } + debug() << " quality: " << idx << " is " << quality << endl; + pile_list[idx] = true; } - debug() << " quality: " << idx << " is " << quality << endl; + } + else + { + quality_clear ( pile_list ); } } @@ -806,18 +877,33 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_other_mats ( const std::map other_mats, FuncReadImport read_value, int32_t list_size ) + void unserialize_list_other_mats ( const std::map other_mats, FuncReadImport read_value, int32_t list_size, std::vector *pile_list ) { - for ( int i = 0; i < list_size; ++i ) + if ( list_size > 0 ) { - const std::string token = read_value ( i ); - size_t idx = other_mats_token ( other_mats, token ); - if ( idx < 0 ) + pile_list->resize ( other_mats.size(), '\0' ); + for ( int i = 0; i < list_size; ++i ) { - debug() << "invalid other mat with token " << token; - continue; + const std::string token = read_value ( i ); + size_t idx = other_mats_token ( other_mats, token ); + if ( idx < 0 ) + { + debug() << "invalid other mat with token " << token; + continue; + } + debug() << " other_mats " << idx << " is " << token << endl; + if ( idx >= pile_list->size() ) + { + debug() << "error other_mats index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + + pile_list->at ( idx ) = 1; } - debug() << " other_mats " << idx << " is " << token << endl; + } + else + { + pile_list->clear(); } } @@ -834,8 +920,7 @@ private: // skip procedurally generated items if ( a->base_flags.is_set ( 0 ) ) continue; ItemTypeInfo ii; - ii.decode ( type, i ); - if ( !ii.isValid() ) continue; + if ( !ii.decode ( type, i ) ) continue; add_value ( ii.getToken() ); debug() << " itemdef type" << i << " is " << ii.getToken() << endl; } @@ -845,14 +930,28 @@ private: /** * @see serialize_list_organic_mat */ - void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size ) + void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size, std::vector *pile_list, item_type::item_type type ) { - for ( int i = 0; i < list_size; ++i ) + if ( list_size > 0 ) { - std::string token = read_value ( i ); - ItemTypeInfo ii; - if ( !ii.find ( token ) ) continue; - debug() << " itemdef " << ii.subtype << " is " << token << endl; + pile_list->resize ( Items::getSubtypeCount ( type ), '\0' ); + for ( int i = 0; i < list_size; ++i ) + { + std::string token = read_value ( i ); + ItemTypeInfo ii; + if ( !ii.find ( token ) ) continue; + debug() << " itemdef " << ii.subtype << " is " << token << endl; + if ( ii.subtype >= pile_list->size() ) + { + debug() << "error itemdef index too large! idx[" << ii.subtype << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + pile_list->at ( ii.subtype ) = 1; + } + } + else + { + pile_list->clear(); } } @@ -891,17 +990,27 @@ private: mBuffer.set_unknown1 ( mPile->settings.unk1 ); mBuffer.set_allow_inorganic ( mPile->settings.allow_inorganic ); mBuffer.set_allow_organic ( mPile->settings.allow_organic ); + mBuffer.set_corpses ( mPile->settings.flags.bits.corpses ); } void read_general() { - const int bins = mBuffer.max_bins(); - const int wheelbarrows = mBuffer.max_wheelbarrows(); - const bool use_links_only = mBuffer.use_links_only(); - const bool unknown1 = mBuffer.unknown1(); - const bool allow_inorganic = mBuffer.allow_inorganic(); - const bool allow_organic = mBuffer.allow_organic(); - + if ( mBuffer.has_max_bins() ) + mPile->max_bins = mBuffer.max_bins(); + if ( mBuffer.has_max_wheelbarrows() ) + mPile->max_wheelbarrows = mBuffer.max_wheelbarrows(); + if ( mBuffer.has_max_barrels() ) + mPile->max_barrels = mBuffer.max_barrels(); + if ( mBuffer.has_use_links_only() ) + mPile->use_links_only = mBuffer.use_links_only(); + if ( mBuffer.has_unknown1() ) + mPile->settings.unk1 = mBuffer.unknown1(); + if ( mBuffer.has_allow_inorganic() ) + mPile->settings.allow_inorganic = mBuffer.allow_inorganic(); + if ( mBuffer.has_allow_organic() ) + mPile->settings.allow_organic = mBuffer.allow_organic(); + if ( mBuffer.has_corpses() ) + mPile->settings.flags.bits.corpses = mBuffer.corpses(); } void write_animals() @@ -924,19 +1033,37 @@ private: { if ( mBuffer.has_animals() ) { -// mPile->settings.flags.bits.animals = true; - debug() << "animals:" <settings.flags.bits.animals = 1; + debug() << "animals:" << endl; + mPile->settings.animals.empty_cages = mBuffer.animals().empty_cages(); + mPile->settings.animals.empty_traps = mBuffer.animals().empty_traps(); if ( mBuffer.animals().enabled_size() > 0 ) { - for ( auto i = 0; i < mBuffer.animals().enabled().size(); ++i ) + mPile->settings.animals.enabled.resize ( world->raws.creatures.all.size(), '\0' ); + debug() << " pile has " << mPile->settings.animals.enabled.size() << endl; + for ( auto i = 0; i < mBuffer.animals().enabled_size(); ++i ) { std::string id = mBuffer.animals().enabled ( i ); int idx = find_creature ( id ); debug() << id << " " << idx << endl; + if ( idx < 0 || idx >= mPile->settings.animals.enabled.size() ) + { + debug() << "WARNING: animal index invalid: " << idx << endl; + continue; + } + mPile->settings.animals.enabled.at ( idx ) = ( char ) 1; } } + else + { + mPile->settings.animals.enabled.clear(); + } + + } + else + { + mPile->settings.animals.enabled.clear(); + mPile->settings.flags.bits.animals = 0; } } @@ -953,7 +1080,7 @@ private: mBuffer.mutable_food()->add_meat ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().meat ( idx ); }; - return food_pair ( setter, mPile->settings.food.meat, getter, mBuffer.food().meat_size() ); + return food_pair ( setter, &mPile->settings.food.meat, getter, mBuffer.food().meat_size() ); } case organic_mat_category::Fish: { @@ -962,7 +1089,7 @@ private: mBuffer.mutable_food()->add_fish ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().fish ( idx ); }; - return food_pair ( setter, mPile->settings.food.fish, getter, mBuffer.food().fish_size() ); + return food_pair ( setter, &mPile->settings.food.fish, getter, mBuffer.food().fish_size() ); } case organic_mat_category::UnpreparedFish: { @@ -971,7 +1098,7 @@ private: mBuffer.mutable_food()->add_unprepared_fish ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().unprepared_fish ( idx ); }; - return food_pair ( setter, mPile->settings.food.unprepared_fish, getter, mBuffer.food().unprepared_fish_size() ); + return food_pair ( setter, &mPile->settings.food.unprepared_fish, getter, mBuffer.food().unprepared_fish_size() ); } case organic_mat_category::Eggs: { @@ -980,7 +1107,7 @@ private: mBuffer.mutable_food()->add_egg ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().egg ( idx ); }; - return food_pair ( setter, mPile->settings.food.egg, getter, mBuffer.food().egg_size() ); + return food_pair ( setter, &mPile->settings.food.egg, getter, mBuffer.food().egg_size() ); } case organic_mat_category::Plants: { @@ -989,7 +1116,7 @@ private: mBuffer.mutable_food()->add_plants ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().plants ( idx ); }; - return food_pair ( setter, mPile->settings.food.plants, getter, mBuffer.food().plants_size() ); + return food_pair ( setter, &mPile->settings.food.plants, getter, mBuffer.food().plants_size() ); } case organic_mat_category::PlantDrink: { @@ -998,7 +1125,7 @@ private: mBuffer.mutable_food()->add_drink_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_plant ( idx ); }; - return food_pair ( setter, mPile->settings.food.drink_plant, getter, mBuffer.food().drink_plant_size() ); + return food_pair ( setter, &mPile->settings.food.drink_plant, getter, mBuffer.food().drink_plant_size() ); } case organic_mat_category::CreatureDrink: { @@ -1007,7 +1134,7 @@ private: mBuffer.mutable_food()->add_drink_animal ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().drink_animal ( idx ); }; - return food_pair ( setter, mPile->settings.food.drink_animal, getter, mBuffer.food().drink_animal_size() ); + return food_pair ( setter, &mPile->settings.food.drink_animal, getter, mBuffer.food().drink_animal_size() ); } case organic_mat_category::PlantCheese: { @@ -1016,7 +1143,7 @@ private: mBuffer.mutable_food()->add_cheese_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_plant ( idx ); }; - return food_pair ( setter, mPile->settings.food.cheese_plant, getter, mBuffer.food().cheese_plant_size() ); + return food_pair ( setter, &mPile->settings.food.cheese_plant, getter, mBuffer.food().cheese_plant_size() ); } case organic_mat_category::CreatureCheese: { @@ -1025,7 +1152,7 @@ private: mBuffer.mutable_food()->add_cheese_animal ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().cheese_animal ( idx ); }; - return food_pair ( setter, mPile->settings.food.cheese_animal, getter, mBuffer.food().cheese_animal_size() ); + return food_pair ( setter, &mPile->settings.food.cheese_animal, getter, mBuffer.food().cheese_animal_size() ); } case organic_mat_category::Seed: { @@ -1034,7 +1161,7 @@ private: mBuffer.mutable_food()->add_seeds ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().seeds ( idx ); }; - return food_pair ( setter, mPile->settings.food.seeds, getter, mBuffer.food().seeds_size() ); + return food_pair ( setter, &mPile->settings.food.seeds, getter, mBuffer.food().seeds_size() ); } case organic_mat_category::Leaf: { @@ -1043,7 +1170,7 @@ private: mBuffer.mutable_food()->add_leaves ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().leaves ( idx ); }; - return food_pair ( setter, mPile->settings.food.leaves, getter, mBuffer.food().leaves_size() ); + return food_pair ( setter, &mPile->settings.food.leaves, getter, mBuffer.food().leaves_size() ); } case organic_mat_category::PlantPowder: { @@ -1052,7 +1179,7 @@ private: mBuffer.mutable_food()->add_powder_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_plant ( idx ); }; - return food_pair ( setter, mPile->settings.food.powder_plant, getter, mBuffer.food().powder_plant_size() ); + return food_pair ( setter, &mPile->settings.food.powder_plant, getter, mBuffer.food().powder_plant_size() ); } case organic_mat_category::CreaturePowder: { @@ -1061,7 +1188,7 @@ private: mBuffer.mutable_food()->add_powder_creature ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().powder_creature ( idx ); }; - return food_pair ( setter, mPile->settings.food.powder_creature, getter, mBuffer.food().powder_creature_size() ); + return food_pair ( setter, &mPile->settings.food.powder_creature, getter, mBuffer.food().powder_creature_size() ); } case organic_mat_category::Glob: { @@ -1070,7 +1197,7 @@ private: mBuffer.mutable_food()->add_glob ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob ( idx ); }; - return food_pair ( setter, mPile->settings.food.glob, getter, mBuffer.food().glob_size() ); + return food_pair ( setter, &mPile->settings.food.glob, getter, mBuffer.food().glob_size() ); } case organic_mat_category::PlantLiquid: { @@ -1079,7 +1206,7 @@ private: mBuffer.mutable_food()->add_liquid_plant ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_plant ( idx ); }; - return food_pair ( setter, mPile->settings.food.liquid_plant, getter, mBuffer.food().liquid_plant_size() ); + return food_pair ( setter, &mPile->settings.food.liquid_plant, getter, mBuffer.food().liquid_plant_size() ); } case organic_mat_category::CreatureLiquid: { @@ -1088,7 +1215,7 @@ private: mBuffer.mutable_food()->add_liquid_animal ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_animal ( idx ); }; - return food_pair ( setter, mPile->settings.food.liquid_animal, getter, mBuffer.food().liquid_animal_size() ); + return food_pair ( setter, &mPile->settings.food.liquid_animal, getter, mBuffer.food().liquid_animal_size() ); } case organic_mat_category::MiscLiquid: { @@ -1097,7 +1224,7 @@ private: mBuffer.mutable_food()->add_liquid_misc ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().liquid_misc ( idx ); }; - return food_pair ( setter, mPile->settings.food.liquid_misc, getter, mBuffer.food().liquid_misc_size() ); + return food_pair ( setter, &mPile->settings.food.liquid_misc, getter, mBuffer.food().liquid_misc_size() ); } case organic_mat_category::Paste: @@ -1107,7 +1234,7 @@ private: mBuffer.mutable_food()->add_glob_paste ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_paste ( idx ); }; - return food_pair ( setter, mPile->settings.food.glob_paste, getter, mBuffer.food().glob_paste_size() ); + return food_pair ( setter, &mPile->settings.food.glob_paste, getter, mBuffer.food().glob_paste_size() ); } case organic_mat_category::Pressed: { @@ -1116,7 +1243,7 @@ private: mBuffer.mutable_food()->add_glob_pressed ( id ); }; FuncReadImport getter = [=] ( size_t idx ) -> std::string { return mBuffer.food().glob_pressed ( idx ); }; - return food_pair ( setter, mPile->settings.food.glob_pressed, getter, mBuffer.food().glob_pressed_size() ); + return food_pair ( setter, &mPile->settings.food.glob_pressed, getter, mBuffer.food().glob_pressed_size() ); } case organic_mat_category::Leather: case organic_mat_category::Silk: @@ -1136,6 +1263,7 @@ private: case organic_mat_category::CookableLeaf: case organic_mat_category::Yarn: case organic_mat_category::MetalThread: + default: // not used in stockpile food menu break; } @@ -1146,6 +1274,7 @@ private: void write_food() { StockpileSettings::FoodSet *food = mBuffer.mutable_food(); + debug() << " food: " << endl; food->set_prepared_meals ( mPile->settings.food.prepared_meals ); using df::enums::organic_mat_category::organic_mat_category; @@ -1153,25 +1282,45 @@ private: for ( int32_t mat_category = traits.first_item_value; mat_category traits; if ( mBuffer.has_food() ) { + mPile->settings.flags.bits.food = 1; const StockpileSettings::FoodSet food = mBuffer.food(); debug() << "food:" < traits; + if ( food.has_prepared_meals() ) + mPile->settings.food.prepared_meals = food.prepared_meals(); + else + mPile->settings.food.prepared_meals = true; + + for ( int32_t mat_category = traits.first_item_value; mat_category clear(); } + mPile->settings.flags.bits.food = 0; } } @@ -1194,24 +1343,6 @@ private: mOtherMatsFurniture.insert ( std::make_pair ( 14,"YARN" ) ); } - std::string furn_other_mats_index ( int idx ) - { - auto it = mOtherMatsFurniture.find ( idx ); - if ( it == mOtherMatsFurniture.end() ) - return std::string(); - return it->second; - } - - int furn_other_mats ( const std::string & token ) - { - for ( auto it = mOtherMatsFurniture.begin(); it != mOtherMatsFurniture.end(); ++it ) - { - if ( it->second == token ) - return it->first; - } - return -1; - } - void write_furniture() { StockpileSettings::FurnitureSet *furniture= mBuffer.mutable_furniture(); @@ -1230,17 +1361,12 @@ private: } } // metal, stone/clay materials - MaterialInfo mi; - for ( size_t i = 0; i < mPile->settings.furniture.mats.size(); ++i ) + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::furniture_mat_is_allowed, this, _1 ); + serialize_list_material ( filter, [=] ( const std::string &token ) { - if ( mPile->settings.furniture.mats.at ( i ) ) - { - mi.decode ( 0, i ); - if ( !furniture_mat_is_allowed ( mi ) ) continue; - debug() << "furniture mat: " << mi.getToken() << endl; - furniture->add_mats ( mi.getToken() ); - } - } + furniture->add_mats ( token ); + }, mPile->settings.furniture.mats ); + // other mats serialize_list_other_mats ( mOtherMatsFurniture, [=] ( const std::string &token ) { @@ -1270,44 +1396,65 @@ private: { if ( mBuffer.has_furniture() ) { + mPile->settings.flags.bits.furniture = 1; const StockpileSettings::FurnitureSet furniture = mBuffer.furniture(); debug() << "furniture:" < type_traits; - for ( int i = 0; i < furniture.type_size(); ++i ) + if ( furniture.type_size() > 0 ) { - const std::string type = furniture.type ( i ); - df::enum_traits::base_type idx = linear_index ( debug(), type_traits, type ); - debug() << " type " << idx << " is " << type << endl; + using df::enums::furniture_type::furniture_type; + df::enum_traits type_traits; + mPile->settings.furniture.type.resize ( type_traits.last_item_value+1, '\0' ); + for ( int i = 0; i < furniture.type_size(); ++i ) + { + const std::string type = furniture.type ( i ); + df::enum_traits::base_type idx = linear_index ( debug(), type_traits, type ); + debug() << " type " << idx << " is " << type << endl; + if ( idx < 0 || idx >= mPile->settings.furniture.type.size() ) + { + debug() << "WARNING: furniture type index invalid " << type << ", idx=" << idx << endl; + continue; + } + mPile->settings.furniture.type.at ( idx ) = 1; + } } - // metal, stone/clay materials - for ( int i = 0; i < furniture.mats_size(); ++i ) + else + mPile->settings.furniture.type.clear(); + + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::furniture_mat_is_allowed, this, _1 ); + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { - const std::string token = furniture.mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !furniture_mat_is_allowed ( mi ) ) continue; - debug() << " mats " << mi.index << " is " << token << endl; - } + return furniture.mats ( idx ); + }, furniture.mats_size(), &mPile->settings.furniture.mats ); + // other materials unserialize_list_other_mats ( mOtherMatsFurniture, [=] ( const size_t & idx ) -> const std::string& { return furniture.other_mats ( idx ); - }, furniture.other_mats_size() ); + }, furniture.other_mats_size(), &mPile->settings.furniture.other_mats ); // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return furniture.quality_core ( idx ); - }, furniture.quality_core_size() ); + }, furniture.quality_core_size(), mPile->settings.furniture.quality_core ); // total quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return furniture.quality_total ( idx ); - }, furniture.quality_total_size() ); + }, furniture.quality_total_size(), mPile->settings.furniture.quality_total ); + + } + else + { + mPile->settings.flags.bits.furniture = 0; + mPile->settings.furniture.type.clear(); + mPile->settings.furniture.other_mats.clear(); + mPile->settings.furniture.mats.clear(); + quality_clear ( mPile->settings.furniture.quality_core ); + quality_clear ( mPile->settings.furniture.quality_total ); } } @@ -1408,23 +1555,29 @@ private: }, mPile->settings.refuse.horns ); } - void refuse_read_helper ( std::function get_value, size_t list_size ) + void refuse_read_helper ( std::function get_value, size_t list_size, std::vector* pile_list ) { if ( list_size > 0 ) { + pile_list->resize ( world->raws.creatures.all.size(), '\0' ); for ( size_t i = 0; i < list_size; ++i ) { const std::string creature_id = get_value ( i ); const int idx = find_creature ( creature_id ); const df::creature_raw* creature = find_creature ( idx ); - if ( idx < 0 || !refuse_creature_is_allowed ( creature ) ) + if ( idx < 0 || !refuse_creature_is_allowed ( creature ) || idx >= pile_list->size() ) { - debug() << "invalid refuse creature: " << creature_id << endl; + debug() << "WARNING invalid refuse creature " << creature_id << ", idx=" << idx << endl; continue; } debug() << " creature " << idx << " is " << creature_id << endl; + pile_list->at ( idx ) = 1; } } + else + { + pile_list->clear(); + } } @@ -1433,66 +1586,84 @@ private: { if ( mBuffer.has_refuse() ) { + mPile->settings.flags.bits.refuse = 1; const StockpileSettings::RefuseSet refuse = mBuffer.refuse(); debug() << "refuse: " <settings.refuse.fresh_raw_hide = refuse.fresh_raw_hide(); + mPile->settings.refuse.rotten_raw_hide = refuse.rotten_raw_hide(); // type FuncItemAllowed filter = std::bind ( &StockpileSerializer::refuse_type_is_allowed, this, _1 ); unserialize_list_item_type ( filter, [=] ( const size_t & idx ) -> const std::string& { return refuse.type ( idx ); - }, refuse.type_size() ); + }, refuse.type_size(), &mPile->settings.refuse.type ); // corpses debug() << " corpses" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.corpses ( idx ); - }, refuse.corpses_size() ); + }, refuse.corpses_size(), &mPile->settings.refuse.corpses ); // body_parts debug() << " body_parts" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.body_parts ( idx ); - }, refuse.body_parts_size() ); + }, refuse.body_parts_size(), &mPile->settings.refuse.body_parts ); // skulls debug() << " skulls" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.skulls ( idx ); - }, refuse.skulls_size() ); + }, refuse.skulls_size(), &mPile->settings.refuse.skulls ); // bones debug() << " bones" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.bones ( idx ); - }, refuse.bones_size() ); + }, refuse.bones_size(), &mPile->settings.refuse.bones ); // hair debug() << " hair" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.hair ( idx ); - }, refuse.hair_size() ); + }, refuse.hair_size(), &mPile->settings.refuse.hair ); // shells debug() << " shells" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.shells ( idx ); - }, refuse.shells_size() ); + }, refuse.shells_size(), &mPile->settings.refuse.shells ); // teeth debug() << " teeth" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.teeth ( idx ); - }, refuse.teeth_size() ); + }, refuse.teeth_size(), &mPile->settings.refuse.teeth ); // horns debug() << " horns" << endl; refuse_read_helper ( [=] ( const size_t & idx ) -> const std::string& { return refuse.horns ( idx ); - }, refuse.horns_size() ); + }, refuse.horns_size(), &mPile->settings.refuse.horns ); + } + else + { + mPile->settings.flags.bits.refuse = 0; + mPile->settings.refuse.type.clear(); + mPile->settings.refuse.corpses.clear(); + mPile->settings.refuse.body_parts.clear(); + mPile->settings.refuse.skulls.clear(); + mPile->settings.refuse.bones.clear(); + mPile->settings.refuse.hair.clear(); + mPile->settings.refuse.shells.clear(); + mPile->settings.refuse.teeth.clear(); + mPile->settings.refuse.horns.clear(); + mPile->settings.refuse.fresh_raw_hide = false; + mPile->settings.refuse.rotten_raw_hide = false; } } @@ -1507,33 +1678,32 @@ private: void write_stone() { StockpileSettings::StoneSet *stone= mBuffer.mutable_stone(); - MaterialInfo mi; - for ( size_t i = 0; i < mPile->settings.stone.mats.size(); ++i ) + + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::stone_is_allowed, this, _1 ); + serialize_list_material ( filter, [=] ( const std::string &token ) { - if ( mPile->settings.stone.mats.at ( i ) ) - { - mi.decode ( 0, i ); - if ( !stone_is_allowed ( mi ) ) continue; - debug() << "stone mat: " << i << " is " << mi.getToken() << endl; - stone->add_mats ( mi.getToken() ); - } - } + stone->add_mats ( token ); + }, mPile->settings.stone.mats ); } void read_stone() { if ( mBuffer.has_stone() ) { + mPile->settings.flags.bits.stone = 1; const StockpileSettings::StoneSet stone = mBuffer.stone(); debug() << "stone: " < const std::string& { - const std::string token = stone.mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !stone_is_allowed ( mi ) ) continue; - debug() << " mats " << mi.index << " is " << token << endl; - } + return stone.mats ( idx ); + }, stone.mats_size(), &mPile->settings.stone.mats ); + } + else + { + mPile->settings.flags.bits.stone = 0; + mPile->settings.stone.mats.clear(); } } @@ -1555,23 +1725,18 @@ private: item_type::AMMO ); // metal - MaterialInfo mi; - for ( size_t i = 0; i < mPile->settings.ammo.mats.size(); ++i ) + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::ammo_mat_is_allowed, this, _1 ); + serialize_list_material ( filter, [=] ( const std::string &token ) { - if ( mPile->settings.ammo.mats.at ( i ) ) - { - mi.decode ( 0, i ); - if ( !ammo_mat_is_allowed ( mi ) ) continue; - ammo->add_mats ( mi.getToken() ); - } - } - debug() << "after metal" << endl; - // other mats + ammo->add_mats ( token ); + }, mPile->settings.ammo.mats ); + + // other mats - only wood and bone if ( mPile->settings.ammo.other_mats.size() > 2 ) { debug() << "WARNING: ammo other materials > 2! " << mPile->settings.ammo.other_mats.size() << endl; } - debug() << "after other check" << endl; + for ( size_t i = 0; i < std::min ( size_t ( 2 ), mPile->settings.ammo.other_mats.size() ); ++i ) { if ( !mPile->settings.ammo.other_mats.at ( i ) ) @@ -1580,12 +1745,13 @@ private: ammo->add_other_mats ( token ); debug() << " other mats " << i << " is " << token << endl; } - debug() << "after other" << endl; + // quality core serialize_list_quality ( [=] ( const std::string &token ) { ammo->add_quality_core ( token ); }, mPile->settings.ammo.quality_core ); + // quality total serialize_list_quality ( [=] ( const std::string &token ) { @@ -1597,6 +1763,7 @@ private: { if ( mBuffer.has_ammo() ) { + mPile->settings.flags.bits.ammo = 1; const StockpileSettings::AmmoSet ammo = mBuffer.ammo(); debug() << "ammo: " < const std::string& { return ammo.type ( idx ); - }, ammo.type_size() ); + }, ammo.type_size(), &mPile->settings.ammo.type, item_type::AMMO ); - // metals - for ( int i = 0; i < ammo.mats_size(); ++i ) + // materials metals + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::ammo_mat_is_allowed, this, _1 ); + unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { - const std::string token = ammo.mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !ammo_mat_is_allowed ( mi ) ) continue; - debug() << " mats " << mi.index << " is " << token << endl; - } + return ammo.mats ( idx ); + }, ammo.mats_size(), &mPile->settings.ammo.mats ); // others - for ( int i = 0; i < ammo.other_mats_size(); ++i ) + if ( ammo.other_mats_size() > 0 ) { - const std::string token = ammo.other_mats ( i ); - const int32_t idx = token == "WOOD" ? 0 : token == "BONE" ? 1 : -1; - debug() << " other mats " << idx << " is " << token << endl; + // TODO remove hardcoded value + mPile->settings.ammo.other_mats.resize ( 2, '\0' ); + for ( int i = 0; i < ammo.other_mats_size(); ++i ) + { + const std::string token = ammo.other_mats ( i ); + const int32_t idx = token == "WOOD" ? 0 : token == "BONE" ? 1 : -1; + debug() << " other mats " << idx << " is " << token << endl; + if ( idx != -1 ) + mPile->settings.ammo.other_mats.at ( idx ) = 1; + } } + else + mPile->settings.ammo.other_mats.clear(); + // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return ammo.quality_core ( idx ); - }, ammo.quality_core_size() ); + }, ammo.quality_core_size(), mPile->settings.ammo.quality_core ); + // total quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return ammo.quality_total ( idx ); - }, ammo.quality_total_size() ); + }, ammo.quality_total_size(), mPile->settings.ammo.quality_total ); + } + else + { + mPile->settings.flags.bits.ammo = 0; + mPile->settings.ammo.type.clear(); + mPile->settings.ammo.mats.clear(); + mPile->settings.ammo.other_mats.clear(); + quality_clear ( mPile->settings.ammo.quality_core ); + quality_clear ( mPile->settings.ammo.quality_total ); } } @@ -1644,34 +1828,31 @@ private: void write_coins() { StockpileSettings::CoinSet *coins = mBuffer.mutable_coin(); - MaterialInfo mi; - for ( size_t i = 0; i < mPile->settings.coins.mats.size(); ++i ) + FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::coins_mat_is_allowed, this, _1 ); + serialize_list_material ( filter, [=] ( const std::string &token ) { - if ( mPile->settings.coins.mats.at ( i ) ) - { - mi.decode ( 0, i ); - if ( !coins_mat_is_allowed ( mi ) ) continue; - debug() << " coin mat" << i << " is " << mi.getToken() << endl; - coins->add_mats ( mi.getToken() ); - } - } - + coins->add_mats ( token ); + }, mPile->settings.coins.mats ); } void read_coins() { if ( mBuffer.has_coin() ) { + mPile->settings.flags.bits.coins = 1; const StockpileSettings::CoinSet coins = mBuffer.coin(); debug() << "coins: " < const std::string& { - const std::string token = coins.mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !coins_mat_is_allowed ( mi ) ) continue; - debug() << " mats " << mi.index << " is " << token << endl; - } + return coins.mats ( idx ); + }, coins.mats_size(), &mPile->settings.coins.mats ); + } + else + { + mPile->settings.flags.bits.coins = 0; + mPile->settings.coins.mats.clear(); } } @@ -1734,6 +1915,7 @@ private: { if ( mBuffer.has_barsblocks() ) { + mPile->settings.flags.bits.bars_blocks = 1; const StockpileSettings::BarsBlocksSet bars_blocks = mBuffer.barsblocks(); debug() << "bars_blocks: " < const std::string& { return bars_blocks.bars_mats ( idx ); - }, bars_blocks.bars_mats_size() ); + }, bars_blocks.bars_mats_size(), &mPile->settings.bars_blocks.bars_mats ); // blocks filter = std::bind ( &StockpileSerializer::blocks_mat_is_allowed, this, _1 ); unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { return bars_blocks.blocks_mats ( idx ); - }, bars_blocks.blocks_mats_size() ); + }, bars_blocks.blocks_mats_size(), &mPile->settings.bars_blocks.blocks_mats ); // bars other mats unserialize_list_other_mats ( mOtherMatsBars, [=] ( const size_t & idx ) -> const std::string& { return bars_blocks.bars_other_mats ( idx ); - }, bars_blocks.bars_other_mats_size() ); + }, bars_blocks.bars_other_mats_size(), &mPile->settings.bars_blocks.bars_other_mats ); // blocks other mats unserialize_list_other_mats ( mOtherMatsBlocks, [=] ( const size_t & idx ) -> const std::string& { return bars_blocks.blocks_other_mats ( idx ); - }, bars_blocks.blocks_other_mats_size() ); + }, bars_blocks.blocks_other_mats_size(), &mPile->settings.bars_blocks.blocks_other_mats ); } + else + { + mPile->settings.flags.bits.bars_blocks = 0; + mPile->settings.bars_blocks.bars_other_mats.clear(); + mPile->settings.bars_blocks.bars_mats.clear(); + mPile->settings.bars_blocks.blocks_other_mats.clear(); + mPile->settings.bars_blocks.blocks_mats.clear(); + } } bool gem_mat_is_allowed ( const MaterialInfo &mi ) @@ -1814,10 +2004,12 @@ private: } } } + void read_gems() { if ( mBuffer.has_gems() ) { + mPile->settings.flags.bits.gems = 1; const StockpileSettings::GemsSet gems = mBuffer.gems(); debug() << "gems: " < const std::string& { return gems.rough_mats ( idx ); - }, gems.rough_mats_size() ); + }, gems.rough_mats_size(), &mPile->settings.gems.rough_mats ); // cut filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& { return gems.cut_mats ( idx ); - }, gems.cut_mats_size() ); + }, gems.cut_mats_size(), &mPile->settings.gems.rough_mats ); + const size_t builtin_size = std::extentraws.mat_table.builtin ) >::value; // rough other - for ( int i = 0; i < gems.rough_other_mats_size(); ++i ) + if ( gems.rough_other_mats_size() > 0 ) { - const std::string token = gems.rough_other_mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !mi.isValid() ) continue; - debug() << " rough_other mats " << mi.type << " is " << token << endl; + mPile->settings.gems.rough_other_mats.resize ( builtin_size, '\0' ); + for ( int i = 0; i < gems.rough_other_mats_size(); ++i ) + { + const std::string token = gems.rough_other_mats ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !mi.isValid() || mi.type >= builtin_size ) + { + debug() << "WARNING: invalid gem mat " << token << ". idx=" << mi.type << endl; + continue; + } + debug() << " rough_other mats " << mi.type << " is " << token << endl; + mPile->settings.gems.rough_other_mats.at ( mi.type ) = 1; + } } + else + mPile->settings.gems.rough_other_mats.clear(); + // cut other - for ( int i = 0; i < gems.cut_other_mats_size(); ++i ) + if ( gems.cut_other_mats_size() > 0 ) { - const std::string token = gems.cut_other_mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !mi.isValid() ) continue; - debug() << " cut_other mats " << mi.type << " is " << token << endl; + mPile->settings.gems.cut_other_mats.resize ( builtin_size, '\0' ); + for ( int i = 0; i < gems.cut_other_mats_size(); ++i ) + { + const std::string token = gems.cut_other_mats ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !mi.isValid() || mi.type >= builtin_size ) + { + debug() << "WARNING: invalid gem mat " << token << ". idx=" << mi.type << endl; + continue; + } + debug() << " cut_other mats " << mi.type << " is " << token << endl; + mPile->settings.gems.cut_other_mats.at ( mi.type ) = 1; + } } + else + mPile->settings.gems.cut_other_mats.clear(); + } + else + { + mPile->settings.flags.bits.gems = 0; + mPile->settings.gems.cut_other_mats.clear(); + mPile->settings.gems.cut_mats.clear(); + mPile->settings.gems.rough_other_mats.clear(); + mPile->settings.gems.rough_mats.clear(); } } @@ -1960,6 +2184,7 @@ private: { if ( mBuffer.has_finished_goods() ) { + mPile->settings.flags.bits.finished_goods = 1; const StockpileSettings::FinishedGoodsSet finished_goods = mBuffer.finished_goods(); debug() << "finished_goods: " < const std::string& { return finished_goods.type ( idx ); - }, finished_goods.type_size() ); + }, finished_goods.type_size(), &mPile->settings.finished_goods.type ); // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::finished_goods_mat_is_allowed, this, _1 ); unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { return finished_goods.mats ( idx ); - }, finished_goods.mats_size() ); + }, finished_goods.mats_size(), &mPile->settings.finished_goods.mats ); // other mats unserialize_list_other_mats ( mOtherMatsFinishedGoods, [=] ( const size_t & idx ) -> const std::string& { return finished_goods.other_mats ( idx ); - }, finished_goods.other_mats_size() ); + }, finished_goods.other_mats_size(), &mPile->settings.finished_goods.other_mats ); // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return finished_goods.quality_core ( idx ); - }, finished_goods.quality_core_size() ); + }, finished_goods.quality_core_size(), mPile->settings.finished_goods.quality_total ); // total quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return finished_goods.quality_total ( idx ); - }, finished_goods.quality_total_size() ); + }, finished_goods.quality_total_size(), mPile->settings.finished_goods.quality_total ); } + else + { + mPile->settings.flags.bits.finished_goods = 0; + mPile->settings.finished_goods.type.clear(); + mPile->settings.finished_goods.other_mats.clear(); + mPile->settings.finished_goods.mats.clear(); + quality_clear ( mPile->settings.finished_goods.quality_core ); + quality_clear ( mPile->settings.finished_goods.quality_total ); + } } void write_leather() @@ -2006,19 +2240,25 @@ private: { leather->add_mats ( id ); }; - serialize_list_organic_mat ( 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() ) { + mPile->settings.flags.bits.leather = 1; const StockpileSettings::LeatherSet leather = mBuffer.leather(); debug() << "leather: " < std::string + + unserialize_list_organic_mat ( [=] ( size_t idx ) -> std::string { return leather.mats ( idx ); - }; - unserialize_list_organic_mat ( getter, leather.mats_size(), organic_mat_category::Leather ); + }, leather.mats_size(), &mPile->settings.leather.mats, organic_mat_category::Leather ); + } + else + { + mPile->settings.flags.bits.leather = 0; + mPile->settings.leather.mats.clear(); } } @@ -2029,90 +2269,103 @@ private: serialize_list_organic_mat ( [=] ( const std::string &token ) { cloth->add_thread_silk ( token ); - }, mPile->settings.cloth.thread_silk, organic_mat_category::Silk ); + }, &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 ); + }, &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 ); + }, &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 ); + }, &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 ); + }, &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 ); + }, &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 ); + }, &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 ); + }, &mPile->settings.cloth.cloth_metal, organic_mat_category::MetalThread ); } void read_cloth() { if ( mBuffer.has_cloth() ) { + mPile->settings.flags.bits.cloth = 1; const StockpileSettings::ClothSet cloth = mBuffer.cloth(); debug() << "cloth: " < std::string { return cloth.thread_silk ( idx ); - }, cloth.thread_silk_size(), organic_mat_category::Silk ); + }, cloth.thread_silk_size(), &mPile->settings.cloth.thread_silk, 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 ); + }, cloth.thread_plant_size(), &mPile->settings.cloth.thread_plant, 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 ); + }, cloth.thread_yarn_size(), &mPile->settings.cloth.thread_yarn, 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 ); + }, cloth.thread_metal_size(), &mPile->settings.cloth.thread_metal, 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 ); + }, cloth.cloth_silk_size(), &mPile->settings.cloth.cloth_silk, 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 ); + }, cloth.cloth_plant_size(), &mPile->settings.cloth.cloth_plant, 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 ); + }, cloth.cloth_yarn_size(), &mPile->settings.cloth.cloth_yarn, 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 ); + }, cloth.cloth_metal_size(), &mPile->settings.cloth.cloth_metal, organic_mat_category::MetalThread ); + } + else + { + mPile->settings.cloth.thread_metal.clear(); + mPile->settings.cloth.thread_plant.clear(); + mPile->settings.cloth.thread_silk.clear(); + mPile->settings.cloth.thread_yarn.clear(); + mPile->settings.cloth.cloth_metal.clear(); + mPile->settings.cloth.cloth_plant.clear(); + mPile->settings.cloth.cloth_silk.clear(); + mPile->settings.cloth.cloth_yarn.clear(); + mPile->settings.flags.bits.cloth = 0; } } @@ -2139,16 +2392,33 @@ private: { if ( mBuffer.has_wood() ) { + mPile->settings.flags.bits.wood = 1; const StockpileSettings::WoodSet wood = mBuffer.wood(); debug() << "wood: " < 0 ) { - const std::string token = wood.mats ( i ); - const size_t idx = find_plant ( token ); - if ( idx < 0 ) continue; - debug() << " plant " << idx << " is " << token << endl; + mPile->settings.wood.mats.resize ( world->raws.plants.all.size(), '\0' ); + for ( int i = 0; i < wood.mats_size(); ++i ) + { + const std::string token = wood.mats ( i ); + const size_t idx = find_plant ( token ); + if ( idx < 0 || idx >= mPile->settings.wood.mats.size() ) + { + debug() << "WARNING wood mat index invalid " << token << ", idx=" << idx << endl; + continue; + } + debug() << " plant " << idx << " is " << token << endl; + mPile->settings.wood.mats.at ( idx ) = 1; + } } + else + mPile->settings.wood.mats.clear(); + } + else + { + mPile->settings.flags.bits.wood = 0; + mPile->settings.wood.mats.clear(); } } @@ -2213,6 +2483,7 @@ private: { if ( mBuffer.has_weapons() ) { + mPile->settings.flags.bits.weapons = 1; const StockpileSettings::WeaponsSet weapons = mBuffer.weapons(); debug() << "weapons: " < const std::string& { return weapons.weapon_type ( idx ); - }, weapons.weapon_type_size() ); + }, weapons.weapon_type_size(), &mPile->settings.weapons.weapon_type, item_type::WEAPON ); // trapcomp type unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { return weapons.trapcomp_type ( idx ); - }, weapons.trapcomp_type_size() ); + }, weapons.trapcomp_type_size(), &mPile->settings.weapons.trapcomp_type, item_type::TRAPCOMP ); // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::weapons_mat_is_allowed, this, _1 ); unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { return weapons.mats ( idx ); - }, weapons.mats_size() ); + }, weapons.mats_size(), &mPile->settings.weapons.mats ); // other mats unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& { return weapons.other_mats ( idx ); - }, weapons.other_mats_size() ); + }, weapons.other_mats_size(), &mPile->settings.weapons.other_mats ); + // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return weapons.quality_core ( idx ); - }, weapons.quality_core_size() ); + }, weapons.quality_core_size(), mPile->settings.weapons.quality_core ); // total quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return weapons.quality_total ( idx ); - }, weapons.quality_total_size() ); + }, weapons.quality_total_size(), mPile->settings.weapons.quality_total ); + } + else + { + mPile->settings.flags.bits.weapons = 0; + mPile->settings.weapons.weapon_type.clear(); + mPile->settings.weapons.trapcomp_type.clear(); + mPile->settings.weapons.other_mats.clear(); + mPile->settings.weapons.mats.clear(); + quality_clear ( mPile->settings.weapons.quality_core ); + quality_clear ( mPile->settings.weapons.quality_total ); } } @@ -2364,6 +2646,7 @@ private: { if ( mBuffer.has_armor() ) { + mPile->settings.flags.bits.armor = 1; const StockpileSettings::ArmorSet armor = mBuffer.armor(); debug() << "armor: " < const std::string& { return armor.body ( idx ); - }, armor.body_size() ); + }, armor.body_size(), &mPile->settings.armor.body, item_type::ARMOR ); // head type unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { return armor.head ( idx ); - }, armor.head_size() ); + }, armor.head_size(), &mPile->settings.armor.head, item_type::HELM ); // feet type unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { return armor.feet ( idx ); - }, armor.feet_size() ); + }, armor.feet_size(), &mPile->settings.armor.feet, item_type::SHOES ); // hands type unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { return armor.hands ( idx ); - }, armor.hands_size() ); + }, armor.hands_size(), &mPile->settings.armor.hands, item_type::GLOVES ); // legs type unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { return armor.legs ( idx ); - }, armor.legs_size() ); + }, armor.legs_size(), &mPile->settings.armor.legs, item_type::PANTS ); // shield type unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& { return armor.shield ( idx ); - }, armor.shield_size() ); + }, armor.shield_size(), &mPile->settings.armor.shield, item_type::SHIELD ); // materials FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::armor_mat_is_allowed, this, _1 ); unserialize_list_material ( mat_filter, [=] ( const size_t & idx ) -> const std::string& { return armor.mats ( idx ); - }, armor.mats_size() ); + }, armor.mats_size(), &mPile->settings.armor.mats ); // other mats unserialize_list_other_mats ( mOtherMatsWeaponsArmor, [=] ( const size_t & idx ) -> const std::string& { return armor.other_mats ( idx ); - }, armor.other_mats_size() ); + }, armor.other_mats_size(), &mPile->settings.armor.other_mats ); // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return armor.quality_core ( idx ); - }, armor.quality_core_size() ); + }, armor.quality_core_size(), mPile->settings.armor.quality_core ); // total quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return armor.quality_total ( idx ); - }, armor.quality_total_size() ); + }, armor.quality_total_size(), mPile->settings.armor.quality_total ); + } + else + { + mPile->settings.flags.bits.armor = 0; + mPile->settings.armor.body.clear(); + mPile->settings.armor.head.clear(); + mPile->settings.armor.feet.clear(); + mPile->settings.armor.hands.clear(); + mPile->settings.armor.legs.clear(); + mPile->settings.armor.shield.clear(); + mPile->settings.armor.other_mats.clear(); + mPile->settings.armor.mats.clear(); + quality_clear ( mPile->settings.armor.quality_core ); + quality_clear ( mPile->settings.armor.quality_total ); } } }; From 79c65e0fcfc73e8c0de71d1bc535c712ca7ce4ea Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 16:07:08 +0100 Subject: [PATCH 15/36] stockpiles: fix male/female creature mat lookups --- plugins/stockpiles.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 9bb507df4..ed0b253b0 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -228,7 +228,7 @@ static int16_t find_creature ( const std::string &creature_id ) /** * Retrieve plant raw from index - */ +*/ static df::plant_raw* find_plant ( size_t idx ) { return world->raws.plants.all[idx]; @@ -348,20 +348,28 @@ public: else { int16_t creature_idx = find_creature ( tokens[0] ); - if ( creature_idx >= 0 ) + if ( creature_idx < 0 ) + { + out << " creature invalid token " << tokens[0]; + } + else { food_idx = linear_index ( table.organic_types[mat_category], creature_idx ); - out << "creature " << token << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl; + if ( tokens[1] == "MALE" ) + food_idx += 1; + if (table.organic_types[mat_category][food_idx] == creature_idx ) + out << "creature " << token << " caste " << tokens[1] << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl; + else { + out << "ERROR creature caste not found: " << token << " caste " << tokens[1] << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl; + food_idx = -1; + } } - else out << " creature invalid token " << tokens[0]; } } else { if ( !index_built ) - { food_build_map ( out ); - } MaterialInfo mat_info = food_mat_by_token ( out, token ); int16_t type = mat_info.type; int32_t index = mat_info.index; @@ -2843,3 +2851,4 @@ static command_result loadstock ( color_ostream &out, vector & paramete return CR_OK; } + From e66732f731f5f6c3f0369e375d0165cd23e4c800 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 16:33:33 +0100 Subject: [PATCH 16/36] stockpiles: add missing protobuf field --- plugins/proto/stockpiles.proto | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/proto/stockpiles.proto b/plugins/proto/stockpiles.proto index e99608e38..9ed43d9d3 100644 --- a/plugins/proto/stockpiles.proto +++ b/plugins/proto/stockpiles.proto @@ -150,6 +150,7 @@ message StockpileSettings { optional ArmorSet armor = 17; optional bool allow_organic = 18; optional bool allow_inorganic = 19; + optional bool corpses = 24; // extras optional int32 max_barrels = 20; optional int32 max_bins = 21; From cc1e4d16a29a77ece65ea1e99e9b999ce64fccb5 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 16:51:45 +0100 Subject: [PATCH 17/36] stockpiles: allow loading without file extension --- plugins/stockpiles.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index ed0b253b0..ffc77bdf7 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -2832,8 +2832,8 @@ static command_result loadstock ( color_ostream &out, vector & paramete } } - if ( file.empty() || !file_exists ( file ) || - !is_dfstockfile ( file ) ) + if ( !is_dfstockfile ( file ) ) file += ".dfstock"; + if ( file.empty() || !file_exists ( file ) ) { out.printerr ( "loadstock: a .dfstock file is required to import\n" ); return CR_WRONG_USAGE; From 98a127812885d5c37602ee7a87138d490a4d5273 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 17:29:15 +0100 Subject: [PATCH 18/36] stockpiles: fix quality unserialization --- plugins/stockpiles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index ffc77bdf7..93ac4431e 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -838,7 +838,7 @@ private: */ void unserialize_list_quality ( FuncReadImport read_value, int32_t list_size, bool ( &pile_list ) [7] ) { - if ( list_size > 0 && list_size < 7 ) + if ( list_size > 0 && list_size <= 7 ) { using df::enums::item_quality::item_quality; df::enum_traits quality_traits; From 12b27e09bbe0f11ff8775db86b93878dd6da6185 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 17:42:38 +0100 Subject: [PATCH 19/36] stockpiles: handle furniture sand_bags field --- plugins/stockpiles.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 93ac4431e..2cdfba64d 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -1408,6 +1408,11 @@ private: const StockpileSettings::FurnitureSet furniture = mBuffer.furniture(); debug() << "furniture:" <settings.furniture.sand_bags = mBuffer.furniture().sand_bags(); + else + mPile->settings.furniture.sand_bags = false; + // type if ( furniture.type_size() > 0 ) { From eb3a71c739bb88f82c42ff535c41a7af69bc78c1 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 17:43:21 +0100 Subject: [PATCH 20/36] stockpiles: fix finished goods quality --- plugins/stockpiles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 2cdfba64d..4ec7fd1f4 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -2225,7 +2225,7 @@ private: unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& { return finished_goods.quality_core ( idx ); - }, finished_goods.quality_core_size(), mPile->settings.finished_goods.quality_total ); + }, finished_goods.quality_core_size(), mPile->settings.finished_goods.quality_core ); // total quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& From e525b47815b2bc7a7901f79fb87ac6fba3508012 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 17:57:18 +0100 Subject: [PATCH 21/36] stockpiles: make gem other mats more robust --- plugins/stockpiles.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 4ec7fd1f4..017152f42 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -1976,20 +1976,23 @@ private: { return mi.isValid() && mi.material && ( mi.material->flags.is_set ( material_flags::IS_GEM ) || mi.material->flags.is_set ( material_flags::IS_STONE ) ) ; } + bool gem_other_mat_is_allowed(MaterialInfo &mi ) { + return mi.isValid() && (mi.getToken() == "GLASS_GREEN" || mi.getToken() == "GLASS_CLEAR" || mi.getToken() == "GLASS_CRYSTAL"); + } void write_gems() { StockpileSettings::GemsSet *gems = mBuffer.mutable_gems(); MaterialInfo mi; // rough mats - FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::gem_mat_is_allowed, this, _1 ); - serialize_list_material ( filter, [=] ( const std::string &token ) + FuncMaterialAllowed filter_rough = std::bind ( &StockpileSerializer::gem_mat_is_allowed, this, _1 ); + serialize_list_material ( filter_rough, [=] ( const std::string &token ) { gems->add_rough_mats ( token ); }, mPile->settings.gems.rough_mats ); // cut mats - filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); - serialize_list_material ( filter, [=] ( const std::string &token ) + FuncMaterialAllowed filter_cut = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); + serialize_list_material ( filter_cut, [=] ( const std::string &token ) { gems->add_cut_mats ( token ); }, mPile->settings.gems.cut_mats ); @@ -1999,7 +2002,7 @@ private: if ( mPile->settings.gems.rough_other_mats.at ( i ) ) { mi.decode ( i, -1 ); - if ( !mi.isValid() ) continue; + if ( !gem_other_mat_is_allowed(mi) ) continue; debug() << " gem rough_other mat" << i << " is " << mi.getToken() << endl; gems->add_rough_other_mats ( mi.getToken() ); } @@ -2011,7 +2014,7 @@ private: { mi.decode ( i, -1 ); if ( !mi.isValid() ) mi.decode ( 0, i ); - if ( !mi.isValid() ) continue; + if ( !gem_other_mat_is_allowed(mi) ) continue; debug() << " gem cut_other mat" << i << " is " << mi.getToken() << endl; gems->add_cut_other_mats ( mi.getToken() ); } @@ -2026,18 +2029,18 @@ private: const StockpileSettings::GemsSet gems = mBuffer.gems(); debug() << "gems: " < const std::string& + FuncMaterialAllowed filter_rough = std::bind ( &StockpileSerializer::gem_mat_is_allowed, this, _1 ); + unserialize_list_material ( filter_rough, [=] ( const size_t & idx ) -> const std::string& { return gems.rough_mats ( idx ); }, gems.rough_mats_size(), &mPile->settings.gems.rough_mats ); // cut - filter = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); - unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& + FuncMaterialAllowed filter_cut = std::bind ( &StockpileSerializer::gem_cut_mat_is_allowed, this, _1 ); + unserialize_list_material ( filter_cut, [=] ( const size_t & idx ) -> const std::string& { return gems.cut_mats ( idx ); - }, gems.cut_mats_size(), &mPile->settings.gems.rough_mats ); + }, gems.cut_mats_size(), &mPile->settings.gems.cut_mats ); const size_t builtin_size = std::extentraws.mat_table.builtin ) >::value; // rough other @@ -2186,6 +2189,7 @@ private: { finished_goods->add_quality_core ( token ); }, mPile->settings.finished_goods.quality_core ); + // quality total serialize_list_quality ( [=] ( const std::string &token ) { From 4f2851199e857c357de74e49e66218e5e581d4c4 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 18:09:07 +0100 Subject: [PATCH 22/36] stockpiles: handle animal cages and prepared meals --- plugins/stockpiles.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 017152f42..457248c70 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -1023,16 +1023,16 @@ private: void write_animals() { - dfstockpiles::StockpileSettings::AnimalsSet animals; - animals.set_empty_cages ( mPile->settings.animals.empty_cages ); - animals.set_empty_traps ( mPile->settings.animals.empty_traps ); + StockpileSettings::AnimalsSet *animals= mBuffer.mutable_animals(); + animals->set_empty_cages ( mPile->settings.animals.empty_cages ); + animals->set_empty_traps ( mPile->settings.animals.empty_traps ); for ( size_t i = 0; i < mPile->settings.animals.enabled.size(); ++i ) { if ( mPile->settings.animals.enabled.at ( i ) == 1 ) { df::creature_raw* r = find_creature ( i ); debug() << "creature "<< r->creature_id << " " << i << endl; - mBuffer.mutable_animals()->add_enabled ( r->creature_id ); + animals->add_enabled ( r->creature_id ); } } } @@ -1072,6 +1072,8 @@ private: { mPile->settings.animals.enabled.clear(); mPile->settings.flags.bits.animals = 0; + mPile->settings.animals.empty_cages = false; + mPile->settings.animals.empty_traps = false; } } @@ -1313,6 +1315,8 @@ private: else mPile->settings.food.prepared_meals = true; + debug() << " prepared_meals: " << mPile->settings.food.prepared_meals << endl; + for ( int32_t mat_category = traits.first_item_value; mat_category clear(); } mPile->settings.flags.bits.food = 0; + mPile->settings.food.prepared_meals = false; } } From f1ce575be3058e8520b6de6cbc49f7c9c1da1082 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 18:14:12 +0100 Subject: [PATCH 23/36] stockpiles: animals list still shown even when disabled --- plugins/stockpiles.cpp | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 457248c70..0885b9fbf 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -1045,28 +1045,22 @@ private: debug() << "animals:" << endl; mPile->settings.animals.empty_cages = mBuffer.animals().empty_cages(); mPile->settings.animals.empty_traps = mBuffer.animals().empty_traps(); - if ( mBuffer.animals().enabled_size() > 0 ) - { - mPile->settings.animals.enabled.resize ( world->raws.creatures.all.size(), '\0' ); - debug() << " pile has " << mPile->settings.animals.enabled.size() << endl; - for ( auto i = 0; i < mBuffer.animals().enabled_size(); ++i ) + + mPile->settings.animals.enabled.clear(); + mPile->settings.animals.enabled.resize ( world->raws.creatures.all.size(), '\0' ); + debug() << " pile has " << mPile->settings.animals.enabled.size() << endl; + for ( auto i = 0; i < mBuffer.animals().enabled_size(); ++i ) + { + std::string id = mBuffer.animals().enabled ( i ); + int idx = find_creature ( id ); + debug() << id << " " << idx << endl; + if ( idx < 0 || idx >= mPile->settings.animals.enabled.size() ) { - std::string id = mBuffer.animals().enabled ( i ); - int idx = find_creature ( id ); - debug() << id << " " << idx << endl; - if ( idx < 0 || idx >= mPile->settings.animals.enabled.size() ) - { - debug() << "WARNING: animal index invalid: " << idx << endl; - continue; - } - mPile->settings.animals.enabled.at ( idx ) = ( char ) 1; + debug() << "WARNING: animal index invalid: " << idx << endl; + continue; } + mPile->settings.animals.enabled.at ( idx ) = ( char ) 1; } - else - { - mPile->settings.animals.enabled.clear(); - } - } else { From 0e8832f05dbb541696bebcfcb307b27005f9b93a Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 19:17:33 +0100 Subject: [PATCH 24/36] stockpiles: overwrite existing files --- plugins/stockpiles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 0885b9fbf..8e0243958 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -465,7 +465,7 @@ public: { mBuffer.Clear(); write(); - std::fstream output ( file, std::ios::out | std::ios::binary ); + std::fstream output ( file, std::ios::out | std::ios::binary | std::ios::trunc ); return mBuffer.SerializeToOstream ( &output ); } From 869a92dfc56ef8112c7dd906628b69673a0866a9 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 21 Nov 2014 19:19:07 +0100 Subject: [PATCH 25/36] stockpiles: handle all metals and stones --- plugins/stockpiles.cpp | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 8e0243958..ab0763c1b 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -100,7 +100,7 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector : filename to load stockpile settings from\n" @@ -771,7 +771,7 @@ private: { mi.decode ( 0, i ); if ( !is_allowed ( mi ) ) continue; - debug() << " material" << i << " is " << mi.getToken() << endl; + debug() << " material " << i << " is " << mi.getToken() << endl; add_value ( mi.getToken() ); } } @@ -784,8 +784,17 @@ private: { if ( list_size > 0 ) { + // we initialize all possible (allowed) values to 0, + // then all other not-allowed values to 1 + // why? because that's how the memory is in DF before + // we muck with it. std::set idx_set; - pile_list->resize ( world->raws.inorganics.size(), '\0' ); + pile_list->clear(); + pile_list->resize ( world->raws.inorganics.size(), 0 ); + for ( int i = 0; i < pile_list->size(); ++i ) { + MaterialInfo mi(0, i); + pile_list->at(i) = is_allowed ( mi ) ? 0 : 1; + } for ( int i = 0; i < list_size; ++i ) { const std::string token = read_value ( i ); @@ -1289,7 +1298,6 @@ private: if ( !p.valid ) continue; debug() << " food: " << traits.key_table[mat_category] << endl; serialize_list_organic_mat ( p.set_value, p.stockpile_values, ( organic_mat_category ) mat_category ); - debug() << " finished food : " << traits.key_table[mat_category] << endl; } } @@ -1390,13 +1398,11 @@ private: }, mPile->settings.furniture.quality_total ); } - /* skip gems and non hard things - should be roughly (:flags.is_set ( material_flags::IS_GEM ) && mi.material->flags.is_set ( material_flags::ITEMS_HARD ); + return mi.isValid() && mi.material + && ( mi.material->flags.is_set ( material_flags::IS_METAL) + || mi.material->flags.is_set ( material_flags::IS_STONE )); } void read_furniture() @@ -1889,7 +1895,9 @@ private: bool blocks_mat_is_allowed ( const MaterialInfo &mi ) { - return mi.isValid() && mi.material && !mi.material->flags.is_set ( material_flags::IS_GEM ) && mi.material->flags.is_set ( material_flags::ITEMS_HARD ); + return mi.isValid() && mi.material + && ( mi.material->flags.is_set ( material_flags::IS_METAL) + || mi.material->flags.is_set ( material_flags::IS_STONE )); } @@ -2808,6 +2816,7 @@ static command_result savestock ( color_ostream &out, vector & paramete return CR_FAILURE; } } + out << "save complete." << endl; return CR_OK; } @@ -2855,8 +2864,11 @@ static command_result loadstock ( color_ostream &out, vector & paramete out << "unserialize failed" << endl; return CR_FAILURE; } - + out << "load complete." << endl; return CR_OK; } + + + From f2d4bf53bda0289079f2bb910ab503b4412211df Mon Sep 17 00:00:00 2001 From: Casey Link Date: Sat, 22 Nov 2014 18:25:00 +0100 Subject: [PATCH 26/36] stockpiles: fix empty list crash I was clear()ing all empty stockpile index vectors , but DF expects them to be initialized with '\0' despite the fact they aren't used. To keep DF happy and prevent segfaults, we now initialize all lists to their appropriate sizes with a sane default value. --- plugins/stockpiles.cpp | 280 +++++++++++++++++------------------------ 1 file changed, 118 insertions(+), 162 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index ab0763c1b..a98773234 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -684,25 +684,19 @@ private: */ void unserialize_list_organic_mat ( FuncReadImport get_value, size_t list_size, std::vector *pile_list, organic_mat_category::organic_mat_category cat ) { - if ( list_size > 0 ) + pile_list->clear(); + pile_list->resize ( OrganicMatLookup::food_max_size ( cat ), '\0' ); + for ( size_t i = 0; i < list_size; ++i ) { - pile_list->resize ( OrganicMatLookup::food_max_size ( cat ), '\0' ); - for ( size_t i = 0; i < list_size; ++i ) + std::string token = get_value ( i ); + int16_t idx = OrganicMatLookup::food_idx_by_token ( debug(), cat, token ); + debug() << " organic_material " << idx << " is " << token << endl; + if ( idx >= pile_list->size() ) { - std::string token = get_value ( i ); - int16_t idx = OrganicMatLookup::food_idx_by_token ( debug(), cat, token ); - debug() << " organic_material " << idx << " is " << token << endl; - if ( idx >= pile_list->size() ) - { - debug() << "error organic mat index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; - continue; - } - pile_list->at ( idx ) = 1; + debug() << "error organic mat index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; + continue; } - } - else - { - pile_list->clear(); + pile_list->at ( idx ) = 1; } } @@ -732,30 +726,28 @@ private: */ void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector *pile_list ) { - if ( list_size > 0 ) + pile_list->clear(); + pile_list->resize ( 112, '\0' ); // TODO remove hardcoded list size value + for ( int i = 0; i < pile_list->size(); ++i ) { - using df::enums::item_type::item_type; - df::enum_traits type_traits; - pile_list->resize ( 112, '\0' ); // TODO remove hardcoded list size value - for ( int32_t i = 0; i < list_size; ++i ) - { - const std::string token = read_value ( i ); - // subtract one because item_type starts at -1 - const df::enum_traits::base_type idx = linear_index ( debug(), type_traits, token ) - 1; - const item_type type = ( item_type ) idx; - if ( !is_allowed ( type ) ) continue; - debug() << " item_type " << idx << " is " << token << endl; - if ( idx >= pile_list->size() ) - { - debug() << "error item_type index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; - continue; - } - pile_list->at ( idx ) = 1; - } + pile_list->at ( i ) = is_allowed ( ( item_type::item_type ) i ) ? 0 : 1; } - else + using df::enums::item_type::item_type; + df::enum_traits type_traits; + for ( int32_t i = 0; i < list_size; ++i ) { - pile_list->clear(); + const std::string token = read_value ( i ); + // subtract one because item_type starts at -1 + const df::enum_traits::base_type idx = linear_index ( debug(), type_traits, token ) - 1; + const item_type type = ( item_type ) idx; + if ( !is_allowed ( type ) ) continue; + debug() << " item_type " << idx << " is " << token << endl; + if ( idx >= pile_list->size() ) + { + debug() << "error item_type index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + pile_list->at ( idx ) = 1; } } @@ -782,37 +774,31 @@ private: */ void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector *pile_list ) { - if ( list_size > 0 ) + // we initialize all possible (allowed) values to 0, + // then all other not-allowed values to 1 + // why? because that's how the memory is in DF before + // we muck with it. + std::set idx_set; + pile_list->clear(); + pile_list->resize ( world->raws.inorganics.size(), 0 ); + for ( int i = 0; i < pile_list->size(); ++i ) { - // we initialize all possible (allowed) values to 0, - // then all other not-allowed values to 1 - // why? because that's how the memory is in DF before - // we muck with it. - std::set idx_set; - pile_list->clear(); - pile_list->resize ( world->raws.inorganics.size(), 0 ); - for ( int i = 0; i < pile_list->size(); ++i ) { - MaterialInfo mi(0, i); - pile_list->at(i) = is_allowed ( mi ) ? 0 : 1; - } - for ( int i = 0; i < list_size; ++i ) - { - const std::string token = read_value ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !is_allowed ( mi ) ) continue; - debug() << " material " << mi.index << " is " << token << endl; - if ( mi.index >= pile_list->size() ) - { - debug() << "error material index too large! idx[" << mi.index << "] max_size[" << pile_list->size() << "]" << endl; - continue; - } - pile_list->at ( mi.index ) = 1; - } + MaterialInfo mi ( 0, i ); + pile_list->at ( i ) = is_allowed ( mi ) ? 0 : 1; } - else + for ( int i = 0; i < list_size; ++i ) { - pile_list->clear(); + const std::string token = read_value ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !is_allowed ( mi ) ) continue; + debug() << " material " << mi.index << " is " << token << endl; + if ( mi.index >= pile_list->size() ) + { + debug() << "error material index too large! idx[" << mi.index << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + pile_list->at ( mi.index ) = 1; } } @@ -896,34 +882,28 @@ private: */ void unserialize_list_other_mats ( const std::map other_mats, FuncReadImport read_value, int32_t list_size, std::vector *pile_list ) { - if ( list_size > 0 ) + pile_list->clear(); + pile_list->resize ( other_mats.size(), '\0' ); + for ( int i = 0; i < list_size; ++i ) { - pile_list->resize ( other_mats.size(), '\0' ); - for ( int i = 0; i < list_size; ++i ) + const std::string token = read_value ( i ); + size_t idx = other_mats_token ( other_mats, token ); + if ( idx < 0 ) { - const std::string token = read_value ( i ); - size_t idx = other_mats_token ( other_mats, token ); - if ( idx < 0 ) - { - debug() << "invalid other mat with token " << token; - continue; - } - debug() << " other_mats " << idx << " is " << token << endl; - if ( idx >= pile_list->size() ) - { - debug() << "error other_mats index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; - continue; - } - - pile_list->at ( idx ) = 1; + debug() << "invalid other mat with token " << token; + continue; } - } - else - { - pile_list->clear(); + debug() << " other_mats " << idx << " is " << token << endl; + if ( idx >= pile_list->size() ) + { + debug() << "error other_mats index too large! idx[" << idx << "] max_size[" << pile_list->size() << "]" << endl; + continue; + } + pile_list->at ( idx ) = 1; } } + /** * @see serialize_list_organic_mat */ @@ -949,26 +929,20 @@ private: */ void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size, std::vector *pile_list, item_type::item_type type ) { - if ( list_size > 0 ) + pile_list->clear(); + pile_list->resize ( Items::getSubtypeCount ( type ), '\0' ); + for ( int i = 0; i < list_size; ++i ) { - pile_list->resize ( Items::getSubtypeCount ( type ), '\0' ); - for ( int i = 0; i < list_size; ++i ) + std::string token = read_value ( i ); + ItemTypeInfo ii; + if ( !ii.find ( token ) ) continue; + debug() << " itemdef " << ii.subtype << " is " << token << endl; + if ( ii.subtype >= pile_list->size() ) { - std::string token = read_value ( i ); - ItemTypeInfo ii; - if ( !ii.find ( token ) ) continue; - debug() << " itemdef " << ii.subtype << " is " << token << endl; - if ( ii.subtype >= pile_list->size() ) - { - debug() << "error itemdef index too large! idx[" << ii.subtype << "] max_size[" << pile_list->size() << "]" << endl; - continue; - } - pile_list->at ( ii.subtype ) = 1; + debug() << "error itemdef index too large! idx[" << ii.subtype << "] max_size[" << pile_list->size() << "]" << endl; + continue; } - } - else - { - pile_list->clear(); + pile_list->at ( ii.subtype ) = 1; } } @@ -1419,11 +1393,12 @@ private: mPile->settings.furniture.sand_bags = false; // type + using df::enums::furniture_type::furniture_type; + df::enum_traits type_traits; + mPile->settings.furniture.type.clear(); + mPile->settings.furniture.type.resize ( type_traits.last_item_value+1, '\0' ); if ( furniture.type_size() > 0 ) { - using df::enums::furniture_type::furniture_type; - df::enum_traits type_traits; - mPile->settings.furniture.type.resize ( type_traits.last_item_value+1, '\0' ); for ( int i = 0; i < furniture.type_size(); ++i ) { const std::string type = furniture.type ( i ); @@ -1437,8 +1412,6 @@ private: mPile->settings.furniture.type.at ( idx ) = 1; } } - else - mPile->settings.furniture.type.clear(); FuncMaterialAllowed filter = std::bind ( &StockpileSerializer::furniture_mat_is_allowed, this, _1 ); unserialize_list_material ( filter, [=] ( const size_t & idx ) -> const std::string& @@ -1575,9 +1548,10 @@ private: void refuse_read_helper ( std::function get_value, size_t list_size, std::vector* pile_list ) { + pile_list->clear(); + pile_list->resize ( world->raws.creatures.all.size(), '\0' ); if ( list_size > 0 ) { - pile_list->resize ( world->raws.creatures.all.size(), '\0' ); for ( size_t i = 0; i < list_size; ++i ) { const std::string creature_id = get_value ( i ); @@ -1592,10 +1566,6 @@ private: pile_list->at ( idx ) = 1; } } - else - { - pile_list->clear(); - } } @@ -1799,10 +1769,11 @@ private: }, ammo.mats_size(), &mPile->settings.ammo.mats ); // others + mPile->settings.ammo.other_mats.clear(); + mPile->settings.ammo.other_mats.resize ( 2, '\0' ); if ( ammo.other_mats_size() > 0 ) { // TODO remove hardcoded value - mPile->settings.ammo.other_mats.resize ( 2, '\0' ); for ( int i = 0; i < ammo.other_mats_size(); ++i ) { const std::string token = ammo.other_mats ( i ); @@ -1812,8 +1783,6 @@ private: mPile->settings.ammo.other_mats.at ( idx ) = 1; } } - else - mPile->settings.ammo.other_mats.clear(); // core quality unserialize_list_quality ( [=] ( const size_t & idx ) -> const std::string& @@ -2051,46 +2020,37 @@ private: const size_t builtin_size = std::extentraws.mat_table.builtin ) >::value; // rough other - if ( gems.rough_other_mats_size() > 0 ) + mPile->settings.gems.rough_other_mats.clear(); + mPile->settings.gems.rough_other_mats.resize ( builtin_size, '\0' ); + for ( int i = 0; i < gems.rough_other_mats_size(); ++i ) { - mPile->settings.gems.rough_other_mats.resize ( builtin_size, '\0' ); - for ( int i = 0; i < gems.rough_other_mats_size(); ++i ) + const std::string token = gems.rough_other_mats ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !mi.isValid() || mi.type >= builtin_size ) { - const std::string token = gems.rough_other_mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !mi.isValid() || mi.type >= builtin_size ) - { - debug() << "WARNING: invalid gem mat " << token << ". idx=" << mi.type << endl; - continue; - } - debug() << " rough_other mats " << mi.type << " is " << token << endl; - mPile->settings.gems.rough_other_mats.at ( mi.type ) = 1; + debug() << "WARNING: invalid gem mat " << token << ". idx=" << mi.type << endl; + continue; } + debug() << " rough_other mats " << mi.type << " is " << token << endl; + mPile->settings.gems.rough_other_mats.at ( mi.type ) = 1; } - else - mPile->settings.gems.rough_other_mats.clear(); // cut other - if ( gems.cut_other_mats_size() > 0 ) + mPile->settings.gems.cut_other_mats.clear(); + mPile->settings.gems.cut_other_mats.resize ( builtin_size, '\0' ); + for ( int i = 0; i < gems.cut_other_mats_size(); ++i ) { - mPile->settings.gems.cut_other_mats.resize ( builtin_size, '\0' ); - for ( int i = 0; i < gems.cut_other_mats_size(); ++i ) + const std::string token = gems.cut_other_mats ( i ); + MaterialInfo mi; + mi.find ( token ); + if ( !mi.isValid() || mi.type >= builtin_size ) { - const std::string token = gems.cut_other_mats ( i ); - MaterialInfo mi; - mi.find ( token ); - if ( !mi.isValid() || mi.type >= builtin_size ) - { - debug() << "WARNING: invalid gem mat " << token << ". idx=" << mi.type << endl; - continue; - } - debug() << " cut_other mats " << mi.type << " is " << token << endl; - mPile->settings.gems.cut_other_mats.at ( mi.type ) = 1; + debug() << "WARNING: invalid gem mat " << token << ". idx=" << mi.type << endl; + continue; } - } - else - mPile->settings.gems.cut_other_mats.clear(); + debug() << " cut_other mats " << mi.type << " is " << token << endl; + mPile->settings.gems.cut_other_mats.at ( mi.type ) = 1; } } else { @@ -2420,24 +2380,20 @@ private: const StockpileSettings::WoodSet wood = mBuffer.wood(); debug() << "wood: " < 0 ) + mPile->settings.wood.mats.clear(); + mPile->settings.wood.mats.resize ( world->raws.plants.all.size(), '\0' ); + for ( int i = 0; i < wood.mats_size(); ++i ) { - mPile->settings.wood.mats.resize ( world->raws.plants.all.size(), '\0' ); - for ( int i = 0; i < wood.mats_size(); ++i ) + const std::string token = wood.mats ( i ); + const size_t idx = find_plant ( token ); + if ( idx < 0 || idx >= mPile->settings.wood.mats.size() ) { - const std::string token = wood.mats ( i ); - const size_t idx = find_plant ( token ); - if ( idx < 0 || idx >= mPile->settings.wood.mats.size() ) - { - debug() << "WARNING wood mat index invalid " << token << ", idx=" << idx << endl; - continue; - } - debug() << " plant " << idx << " is " << token << endl; - mPile->settings.wood.mats.at ( idx ) = 1; + debug() << "WARNING wood mat index invalid " << token << ", idx=" << idx << endl; + continue; } + debug() << " plant " << idx << " is " << token << endl; + mPile->settings.wood.mats.at ( idx ) = 1; } - else - mPile->settings.wood.mats.clear(); } else { From e599f4b1e1a361d5be99d78b4882b109df89a3b2 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Sat, 22 Nov 2014 18:32:19 +0100 Subject: [PATCH 27/36] stockpiles: more user documentation --- plugins/stockpiles.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index a98773234..1a3cc1fbf 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -92,18 +92,22 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector : filename to save stockpile settings to (will overwrite!)\n" + " : filename to save stockpile settings to (will be overwriten!)\n" ) ); commands.push_back ( PluginCommand ( - "loadstock", "Import stockpile settings and apply them to the stockpile under cursor.", + "loadstock", "Load settings from a file and apply them to the active stockpile.", loadstock, loadstock_guard, + "Must be in 'q' mode and have a stockpile selected.\n" + "example: 'loadstock food.dfstock' will load the settings from 'food.dfstock'\nin your stockpile folder and apply them to the selected stockpile.\n\n" " -d, --debug: enable debug output\n" - " : filename to load stockpile settings from\n" + " : filename to load stockpile settings from\n" ) ); } From 00c1676744e041b51471e520abb5027ebb2557c7 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Sat, 22 Nov 2014 18:54:22 +0100 Subject: [PATCH 28/36] stockpiles: correct typo --- plugins/stockpiles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 1a3cc1fbf..3bdfb3351 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -97,7 +97,7 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector : filename to save stockpile settings to (will be overwriten!)\n" + " : filename to save stockpile settings to (will be overwritten!)\n" ) ); commands.push_back ( From 063b669f6b6c0983d0db95048f440051394c07a3 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Sat, 22 Nov 2014 18:57:56 +0100 Subject: [PATCH 29/36] stockpiles: cleanup output and add docs --- plugins/stockpiles.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 3bdfb3351..0e395b0d7 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -95,7 +95,9 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector : filename to save stockpile settings to (will be overwritten!)\n" ) @@ -105,7 +107,8 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector : filename to load stockpile settings from\n" ) @@ -2772,11 +2775,10 @@ static command_result savestock ( color_ostream &out, vector & paramete if ( !is_dfstockfile ( file ) ) file += ".dfstock"; if ( !cereal.serialize_to_file ( file ) ) { - out << "serialize failed" << endl; + out.printerr ( "serialize failed\n" ); return CR_FAILURE; } } - out << "save complete." << endl; return CR_OK; } @@ -2821,10 +2823,9 @@ static command_result loadstock ( color_ostream &out, vector & paramete cereal.enable_debug ( out ); if ( !cereal.unserialize_from_file ( file ) ) { - out << "unserialize failed" << endl; + out.printerr ( "unserialization failed\n" ); return CR_FAILURE; } - out << "load complete." << endl; return CR_OK; } From f100d30f56585c87cd0d7515a7f68f7327efb1b5 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Tue, 25 Nov 2014 10:48:51 +0100 Subject: [PATCH 30/36] use full protobuf library project wide since plugins are using full protobuf, there is no reason for everything not to. this removes the need to ship both the full protobuf lib and the protobuf-lite lib. --- depends/protobuf/CMakeLists.txt | 8 ++++---- library/CMakeLists.txt | 4 ++-- plugins/mapexport/CMakeLists.txt | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/depends/protobuf/CMakeLists.txt b/depends/protobuf/CMakeLists.txt index 0544f5d35..f14a444ba 100644 --- a/depends/protobuf/CMakeLists.txt +++ b/depends/protobuf/CMakeLists.txt @@ -213,14 +213,14 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) ADD_LIBRARY(protobuf SHARED ${LIBPROTOBUF_FULL_SRCS} ${LIBPROTOBUF_FULL_HDRS}) IDE_FOLDER(protobuf "Depends") -ADD_LIBRARY(protobuf-lite SHARED ${LIBPROTOBUF_LITE_SRCS} ${LIBPROTOBUF_LITE_HDRS}) -IDE_FOLDER(protobuf-lite "Depends") +#ADD_LIBRARY(protobuf-lite SHARED ${LIBPROTOBUF_LITE_SRCS} ${LIBPROTOBUF_LITE_HDRS}) +#IDE_FOLDER(protobuf-lite "Depends") SET_TARGET_PROPERTIES(protobuf PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS) -SET_TARGET_PROPERTIES(protobuf-lite PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS) +#SET_TARGET_PROPERTIES(protobuf-lite PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS) TARGET_LINK_LIBRARIES(protobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) -TARGET_LINK_LIBRARIES(protobuf-lite ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) +#TARGET_LINK_LIBRARIES(protobuf-lite ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) #install(TARGETS protobuf-lite #LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION} diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index e0ad3606b..ac8c10814 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -290,10 +290,10 @@ IF(APPLE) SET_TARGET_PROPERTIES(dfhack PROPERTIES SOVERSION 1.0.0) ENDIF() -TARGET_LINK_LIBRARIES(dfhack protobuf-lite clsocket lua ${PROJECT_LIBS}) +TARGET_LINK_LIBRARIES(dfhack protobuf clsocket lua ${PROJECT_LIBS}) SET_TARGET_PROPERTIES(dfhack PROPERTIES LINK_INTERFACE_LIBRARIES "") -TARGET_LINK_LIBRARIES(dfhack-client protobuf-lite clsocket) +TARGET_LINK_LIBRARIES(dfhack-client protobuf clsocket) TARGET_LINK_LIBRARIES(dfhack-run dfhack-client) if(APPLE) diff --git a/plugins/mapexport/CMakeLists.txt b/plugins/mapexport/CMakeLists.txt index 429507a77..370c32c48 100644 --- a/plugins/mapexport/CMakeLists.txt +++ b/plugins/mapexport/CMakeLists.txt @@ -37,7 +37,7 @@ DEPENDS protoc-bin ${PROJECT_PROTOS} ) IF(WIN32) - DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf-lite) + DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf) ELSE() - DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf-lite) + DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf) ENDIF() From b6f5bcb7dc817d305c9e2997ac4b66b662284697 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 27 Nov 2014 13:28:17 +0100 Subject: [PATCH 31/36] stockpiles: fix compilation on windows VS2010 only supports a subset of C++11 --- plugins/stockpiles.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 0e395b0d7..f9f54a717 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -637,7 +637,7 @@ private: // importing FuncReadImport get_value; size_t serialized_count; - bool valid = false; + bool valid; food_pair ( FuncWriteExport s, std::vector* sp_v, FuncReadImport g, size_t count ) : set_value ( s ) @@ -646,7 +646,7 @@ private: , serialized_count ( count ) , valid ( true ) {} - food_pair() {} + food_pair(): valid( false ) {} }; /** @@ -2752,8 +2752,9 @@ static command_result savestock ( color_ostream &out, vector & paramete bool debug = false; std::string file; - for ( std::string o : parameters ) - { + for( size_t i = 0; i < parameters.size(); ++i ) + { + const std::string o = parameters.at(i); if ( o == "--debug" || o == "-d" ) debug = true; else if ( !o.empty() && o[0] != '-' ) @@ -2801,8 +2802,9 @@ static command_result loadstock ( color_ostream &out, vector & paramete bool debug = false; std::string file; - for ( std::string o : parameters ) - { + for( size_t i = 0; i < parameters.size(); ++i ) + { + const std::string o = parameters.at(i); if ( o == "--debug" || o == "-d" ) debug = true; else if ( !o.empty() && o[0] != '-' ) From 7cbb1ed0434cc2eded2209e68b7ab3f451fd6497 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 27 Nov 2014 13:39:30 +0100 Subject: [PATCH 32/36] Revert "use full protobuf library project wide" This reverts commit f100d30f56585c87cd0d7515a7f68f7327efb1b5. --- depends/protobuf/CMakeLists.txt | 8 ++++---- library/CMakeLists.txt | 4 ++-- plugins/mapexport/CMakeLists.txt | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/depends/protobuf/CMakeLists.txt b/depends/protobuf/CMakeLists.txt index f14a444ba..0544f5d35 100644 --- a/depends/protobuf/CMakeLists.txt +++ b/depends/protobuf/CMakeLists.txt @@ -213,14 +213,14 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) ADD_LIBRARY(protobuf SHARED ${LIBPROTOBUF_FULL_SRCS} ${LIBPROTOBUF_FULL_HDRS}) IDE_FOLDER(protobuf "Depends") -#ADD_LIBRARY(protobuf-lite SHARED ${LIBPROTOBUF_LITE_SRCS} ${LIBPROTOBUF_LITE_HDRS}) -#IDE_FOLDER(protobuf-lite "Depends") +ADD_LIBRARY(protobuf-lite SHARED ${LIBPROTOBUF_LITE_SRCS} ${LIBPROTOBUF_LITE_HDRS}) +IDE_FOLDER(protobuf-lite "Depends") SET_TARGET_PROPERTIES(protobuf PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS) -#SET_TARGET_PROPERTIES(protobuf-lite PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS) +SET_TARGET_PROPERTIES(protobuf-lite PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS) TARGET_LINK_LIBRARIES(protobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) -#TARGET_LINK_LIBRARIES(protobuf-lite ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) +TARGET_LINK_LIBRARIES(protobuf-lite ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) #install(TARGETS protobuf-lite #LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION} diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index ac8c10814..e0ad3606b 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -290,10 +290,10 @@ IF(APPLE) SET_TARGET_PROPERTIES(dfhack PROPERTIES SOVERSION 1.0.0) ENDIF() -TARGET_LINK_LIBRARIES(dfhack protobuf clsocket lua ${PROJECT_LIBS}) +TARGET_LINK_LIBRARIES(dfhack protobuf-lite clsocket lua ${PROJECT_LIBS}) SET_TARGET_PROPERTIES(dfhack PROPERTIES LINK_INTERFACE_LIBRARIES "") -TARGET_LINK_LIBRARIES(dfhack-client protobuf clsocket) +TARGET_LINK_LIBRARIES(dfhack-client protobuf-lite clsocket) TARGET_LINK_LIBRARIES(dfhack-run dfhack-client) if(APPLE) diff --git a/plugins/mapexport/CMakeLists.txt b/plugins/mapexport/CMakeLists.txt index 370c32c48..429507a77 100644 --- a/plugins/mapexport/CMakeLists.txt +++ b/plugins/mapexport/CMakeLists.txt @@ -37,7 +37,7 @@ DEPENDS protoc-bin ${PROJECT_PROTOS} ) IF(WIN32) - DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf) + DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf-lite) ELSE() - DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf) + DFHACK_PLUGIN(mapexport ${PROJECT_SRCS} ${PROJECT_HDRS} LINK_LIBRARIES protobuf-lite) ENDIF() From de919050b1119f531d5509bcd66218849e41c229 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 27 Nov 2014 13:40:11 +0100 Subject: [PATCH 33/36] Revert "install the full protobuf library" This reverts commit 443a9676e110349fbecaac8014d8605f8dd5c603. --- depends/protobuf/CMakeLists.txt | 5 +---- plugins/Plugins.cmake | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/depends/protobuf/CMakeLists.txt b/depends/protobuf/CMakeLists.txt index 0544f5d35..5034f00f4 100644 --- a/depends/protobuf/CMakeLists.txt +++ b/depends/protobuf/CMakeLists.txt @@ -222,10 +222,7 @@ SET_TARGET_PROPERTIES(protobuf-lite PROPERTIES COMPILE_DEFINITIONS LIBPROTOBUF_E TARGET_LINK_LIBRARIES(protobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) TARGET_LINK_LIBRARIES(protobuf-lite ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) -#install(TARGETS protobuf-lite - #LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION} - #RUNTIME DESTINATION ${DFHACK_LIBRARY_DESTINATION}) -install(TARGETS protobuf +install(TARGETS protobuf-lite LIBRARY DESTINATION ${DFHACK_LIBRARY_DESTINATION} RUNTIME DESTINATION ${DFHACK_LIBRARY_DESTINATION}) diff --git a/plugins/Plugins.cmake b/plugins/Plugins.cmake index 9465bfeaf..e5d0c3bae 100644 --- a/plugins/Plugins.cmake +++ b/plugins/Plugins.cmake @@ -83,7 +83,7 @@ MACRO(DFHACK_PLUGIN) LIST(LENGTH PLUGIN_PROTOBUFS NUM_PROTO) IF(NUM_PROTO) - TARGET_LINK_LIBRARIES(${PLUGIN_NAME} dfhack protobuf ${PLUGIN_LINK_LIBRARIES}) + TARGET_LINK_LIBRARIES(${PLUGIN_NAME} dfhack protobuf-lite ${PLUGIN_LINK_LIBRARIES}) IF(UNIX) SET_TARGET_PROPERTIES(${PLUGIN_NAME} PROPERTIES COMPILE_FLAGS "-include Export.h") ELSE() @@ -104,4 +104,4 @@ MACRO(DFHACK_PLUGIN) install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION ${DFHACK_PLUGIN_DESTINATION} RUNTIME DESTINATION ${DFHACK_PLUGIN_DESTINATION}) -ENDMACRO(DFHACK_PLUGIN) +ENDMACRO(DFHACK_PLUGIN) \ No newline at end of file From d352d0f42db60084f3ab0e230277621ca6d50790 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 27 Nov 2014 14:23:54 +0100 Subject: [PATCH 34/36] stockpiles: remove dependency on full protobuf --- plugins/stockpiles.cpp | 52 +++++++++++------------------------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index f9f54a717..9996d5d1c 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -41,7 +41,6 @@ // protobuf #include "proto/stockpiles.pb.h" -#include // os #include @@ -476,20 +475,6 @@ public: return mBuffer.SerializeToOstream ( &output ); } - /** - * Serializes the stockpile settings to a string. - * @return empty string on error - */ - std::string serialize_to_string() - { - mBuffer.Clear(); - write(); - std::string str; - if ( !TextFormat::PrintToString ( mBuffer, &str ) ) - return std::string(); - return str; - } - /** * Read stockpile settings from file */ @@ -502,15 +487,6 @@ public: return res; } - /** - * Read text_format stockpile settings from string - */ - bool unserialize_from_string ( const std::string & data ) - { - mBuffer.Clear(); - return TextFormat::ParseFromString ( data, &mBuffer ); - } - private: bool mDebug; @@ -2762,23 +2738,21 @@ static command_result savestock ( color_ostream &out, vector & paramete file = o; } } + if ( file.empty() ) + { + out.printerr("You must supply a valid filename.\n"); + return CR_WRONG_USAGE; + } StockpileSerializer cereal ( sp ); if ( debug ) cereal.enable_debug ( out ); - if ( file.empty() ) - { - std::string data = cereal.serialize_to_string(); - out << data << endl; - } - else + + if ( !is_dfstockfile ( file ) ) file += ".dfstock"; + if ( !cereal.serialize_to_file ( file ) ) { - if ( !is_dfstockfile ( file ) ) file += ".dfstock"; - if ( !cereal.serialize_to_file ( file ) ) - { - out.printerr ( "serialize failed\n" ); - return CR_FAILURE; - } + out.printerr ( "serialize failed\n" ); + return CR_FAILURE; } return CR_OK; } @@ -2802,9 +2776,9 @@ static command_result loadstock ( color_ostream &out, vector & paramete bool debug = false; std::string file; - for( size_t i = 0; i < parameters.size(); ++i ) - { - const std::string o = parameters.at(i); + for( size_t i = 0; i < parameters.size(); ++i ) + { + const std::string o = parameters.at(i); if ( o == "--debug" || o == "-d" ) debug = true; else if ( !o.empty() && o[0] != '-' ) From 1c496fb25587ce749974593982996707c363a7c0 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 27 Nov 2014 14:49:16 +0100 Subject: [PATCH 35/36] add savestock and loadstock to NEWS and Readme.rst --- NEWS | 3 +++ Readme.rst | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 5bd968839..656951320 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,9 @@ DFHack Future Internals: Fixes: New plugins: + - savestock and loadstock: two commands for saving and loading + stockpile settings to a file. They can be used to migrate stockpile + settings between worlds and saves. New scripts: Misc improvements: diff --git a/Readme.rst b/Readme.rst index cd0b677b1..a4965d54b 100644 --- a/Readme.rst +++ b/Readme.rst @@ -308,12 +308,54 @@ twaterlvl --------- Toggle between displaying/not displaying liquid depth as numbers. +stockpile settings management +----------------------------- + copystock ----------- +~~~~~~~~~ + Copies the parameters of the currently highlighted stockpile to the custom stockpile settings and switches to custom stockpile placement mode, effectively allowing you to copy/paste stockpiles easily. +savestock +~~~~~~~~~ + +Saves the currently highlighted stockpile's settings to a file in your Dwarf +Fortress folder. This file can be used to copy settings between game saves or +players. + +example: + +``` +savestock food_settings.dfstock +``` + +loadstock +~~~~~~~~~ + +Loads a saved stockpile settings file and applies it to the currently selected +stockpile. + +example: + +``` +loadstock food_settings.dfstock +``` + +To use savestock and loadstock, use the 'q' command to highlight a stockpile. +Then run savestock giving it a descriptive filename. Then, in a different (or +same!) gameworld, you can highlight any stockpile with 'q' then execute the +'loadstock' command passing it the name of that file. The settings will be +applied to that stockpile. + +**Notes:** It saves and loads files relative to the DF folder, so put your files +there or in a subfolder for easy access. Filenames should not have spaces. + +**Limitations:** Generated materials, divine metals, etc are not saved as they +are different in every world. + + rename ------ Allows renaming various things. From cb6d4da2f29a46de9715f5bc7b409d750bb643fa Mon Sep 17 00:00:00 2001 From: Casey Link Date: Thu, 27 Nov 2014 16:36:42 +0100 Subject: [PATCH 36/36] stockpules: fix protobuf-lite support Looks like we lost more than just TextFormat when we lost full protobuf support. Copied these two serialization functions over. --- plugins/proto/stockpiles.proto | 4 ++-- plugins/stockpiles.cpp | 38 +++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/plugins/proto/stockpiles.proto b/plugins/proto/stockpiles.proto index 9ed43d9d3..59e905031 100644 --- a/plugins/proto/stockpiles.proto +++ b/plugins/proto/stockpiles.proto @@ -1,6 +1,6 @@ package dfstockpiles; -option optimize_for = CODE_SIZE; +option optimize_for = LITE_RUNTIME; message StockpileSettings { @@ -156,4 +156,4 @@ message StockpileSettings { optional int32 max_bins = 21; optional int32 max_wheelbarrows = 22; optional bool use_links_only = 23; -} \ No newline at end of file +} diff --git a/plugins/stockpiles.cpp b/plugins/stockpiles.cpp index 9996d5d1c..ebf4b0d40 100644 --- a/plugins/stockpiles.cpp +++ b/plugins/stockpiles.cpp @@ -41,6 +41,7 @@ // protobuf #include "proto/stockpiles.pb.h" +#include // os #include @@ -463,28 +464,51 @@ public: mOut = &out; } + /** + * Since we depend on protobuf-lite, not the full lib, we copy this function from + * protobuf message.cc + */ + bool serialize_to_ostream(ostream* output) + { + mBuffer.Clear(); + write(); + { + io::OstreamOutputStream zero_copy_output(output); + if (!mBuffer.SerializeToZeroCopyStream(&zero_copy_output)) return false; + } + return output->good(); + } + /** * Will serialize stockpile settings to a file (overwrites existing files) * @return success/failure */ bool serialize_to_file ( const std::string & file ) { - mBuffer.Clear(); - write(); std::fstream output ( file, std::ios::out | std::ios::binary | std::ios::trunc ); - return mBuffer.SerializeToOstream ( &output ); + return serialize_to_ostream(&output); } + /** + * Again, copied from message.cc + */ + bool parse_from_istream(istream* input) + { + mBuffer.Clear(); + io::IstreamInputStream zero_copy_input(input); + const bool res = mBuffer.ParseFromZeroCopyStream(&zero_copy_input) && input->eof(); + if( res ) read(); + return res; + } + + /** * Read stockpile settings from file */ bool unserialize_from_file ( const std::string & file ) { - mBuffer.Clear(); std::fstream input ( file, std::ios::in | std::ios::binary ); - const bool res = mBuffer.ParseFromIstream ( &input ); - read(); - return res; + return parse_from_istream(&input); } private: