stockpiles: serialize through weapons+trap components

* generalize itemdefs
develop
Casey Link 2014-11-19 19:53:53 +01:00
parent 25f6c440d8
commit 224a19295d
1 changed files with 281 additions and 97 deletions

@ -28,6 +28,15 @@
#include "df/global_objects.h" #include "df/global_objects.h"
#include "df/viewscreen_dwarfmodest.h" #include "df/viewscreen_dwarfmodest.h"
#include <df/itemdef_ammost.h> #include <df/itemdef_ammost.h>
#include <df/itemdef_weaponst.h>
#include <df/itemdef_ammost.h>
#include <df/itemdef_trapcompst.h>
#include <df/itemdef_armorst.h>
#include <df/itemdef_helmst.h>
#include <df/itemdef_shoesst.h>
#include <df/itemdef_glovesst.h>
#include <df/itemdef_pantsst.h>
#include <df/itemdef_shieldst.h>
#include "stockpiles.pb.h" #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 ); 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<int16_t, int32_t> FoodMatPair; typedef std::pair<int16_t, int32_t> FoodMatPair;
typedef std::map<FoodMatPair, size_t> FoodMatMap; typedef std::map<FoodMatPair, size_t> FoodMatMap;
@ -299,7 +323,9 @@ public:
furniture_setup_other_mats(); furniture_setup_other_mats();
bars_blocks_setup_other_mats(); bars_blocks_setup_other_mats();
finished_goods_setup_other_mats(); finished_goods_setup_other_mats();
weapons_armor_setup_other_mats();
} }
StockpileSettings write() { StockpileSettings write() {
// *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl; // *mOut << "GROUP SET " << bitfield_to_string(mPile->settings.flags) << endl;
write_general(); write_general();
@ -370,6 +396,7 @@ private:
std::map<int, std::string> mOtherMatsFinishedGoods; std::map<int, std::string> mOtherMatsFinishedGoods;
std::map<int, std::string> mOtherMatsBars; std::map<int, std::string> mOtherMatsBars;
std::map<int, std::string> mOtherMatsBlocks; std::map<int, std::string> mOtherMatsBlocks;
std::map<int, std::string> mOtherMatsWeaponsArmor;
/** /**
* Find an enum's value based off the string label. * Find an enum's value based off the string label.
* @param traits the enum's trait struct * @param traits the enum's trait struct
@ -543,6 +570,30 @@ private:
} }
} }
void serialize_list_itemdef ( FuncWriteExport add_value, std::vector<char> list, std::vector<df::itemdef *> 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<int, std::string> other_mats, int idx ) { std::string other_mats_index( const std::map<int, std::string> other_mats, int idx ) {
auto it = other_mats.find ( idx ); auto it = other_mats.find ( idx );
if ( it == other_mats.end() ) if ( it == other_mats.end() )
@ -1122,19 +1173,13 @@ private:
void write_ammo() { void write_ammo() {
StockpileSettings::AmmoSet *ammo = mBuffer.mutable_ammo(); StockpileSettings::AmmoSet *ammo = mBuffer.mutable_ammo();
// type // ammo type
for ( size_t i = 0; i < mPile->settings.ammo.type.size(); ++i ) { serialize_list_itemdef ( [=] ( const std::string &token ) {
if ( mPile->settings.ammo.type.at ( i ) ) { ammo->add_type ( token );
const df::itemdef_ammost *a = world->raws.itemdefs.ammo.at ( i ); }, mPile->settings.ammo.type,
// skip procedurally generated ammo std::vector<df::itemdef*> ( world->raws.itemdefs.ammo.begin(),world->raws.itemdefs.ammo.end() ),
if ( a->base_flags.is_set ( 0 ) ) continue; item_type::AMMO );
ItemTypeInfo ii;
ii.decode ( item_type::AMMO, i );
if ( !ii.isValid() ) continue;
ammo->add_type ( ii.getToken() );
*mOut << " " << i << " is " << ii.getToken() << endl;
}
}
// metal // metal
MaterialInfo mi; MaterialInfo mi;
for ( size_t i = 0; i < mPile->settings.ammo.mats.size(); ++i ) { for ( size_t i = 0; i < mPile->settings.ammo.mats.size(); ++i ) {
@ -1172,12 +1217,12 @@ private:
if ( mBuffer.has_ammo() ) { if ( mBuffer.has_ammo() ) {
const StockpileSettings::AmmoSet ammo = mBuffer.ammo(); const StockpileSettings::AmmoSet ammo = mBuffer.ammo();
*mOut << "ammo: " <<endl; *mOut << "ammo: " <<endl;
for ( int i = 0; i < ammo.type_size(); ++i ) {
std::string token = ammo.type ( i ); // ammo type
ItemTypeInfo ii; unserialize_list_itemdef ( [=] ( const size_t & idx ) -> const std::string& {
if ( !ii.find ( token ) ) continue; return ammo.type ( idx );
*mOut << " " << token << endl; }, ammo.type_size() );
}
// metals // 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 ); const std::string token = ammo.mats ( i );
@ -1440,7 +1485,6 @@ private:
mOtherMatsFinishedGoods.insert ( std::make_pair ( 15,"WAX" ) ); 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() return mi.isValid()
&& mi.material && mi.material
@ -1608,25 +1652,115 @@ private:
} }
} }
void write_wood() {} bool wood_mat_is_allowed ( const df::plant_raw * plant ) {
void read_wood() {} 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: " <<endl;
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 ) continue;
*mOut << " plant " << idx << " is " << token << endl;
}
}
}
bool weapons_mat_is_allowed ( const MaterialInfo &mi ) { bool weapons_mat_is_allowed ( const MaterialInfo &mi ) {
return mi.isValid(); return mi.isValid() && mi.material && (
mi.material->flags.is_set ( material_flags::IS_METAL ) ||
mi.material->flags.is_set ( material_flags::IS_STONE ) );
}
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<df::itemdef*> ( 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<df::itemdef*> ( 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 write_weapons() {}
void read_weapons() { void read_weapons() {
if ( mBuffer.has_weapons() ) { if ( mBuffer.has_weapons() ) {
const StockpileSettings::WeaponsSet weapons = mBuffer.weapons(); const StockpileSettings::WeaponsSet weapons = mBuffer.weapons();
*mOut << "weapons: " <<endl; *mOut << "weapons: " <<endl;
bool unusable = weapons.unusable();
bool usable = weapons.usable();
*mOut << "unusable " << unusable << endl;
*mOut << "usable " << usable << endl;
// weapon type
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& {
return weapons.trapcomp_type ( idx );
}, weapons.trapcomp_type_size() );
// materials // materials
FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::weapons_mat_is_allowed, this, _1 ); 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 ); 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 // 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 ); return weapons.quality_core ( idx );
@ -1639,21 +1773,72 @@ private:
} }
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 ) { bool armor_mat_is_allowed ( const MaterialInfo &mi ) {
return mi.isValid(); return weapons_mat_is_allowed ( mi );
} }
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 );
// 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() { void read_armor() {
if ( mBuffer.has_armor() ) { if ( mBuffer.has_armor() ) {
const StockpileSettings::ArmorSet armor = mBuffer.armor(); const StockpileSettings::ArmorSet armor = mBuffer.armor();
*mOut << "armor: " <<endl; *mOut << "armor: " <<endl;
bool unusable = armor.unusable();
bool usable = armor.usable();
*mOut << "unusable " << unusable << endl;
*mOut << "usable " << usable << endl;
// materials // materials
FuncMaterialAllowed mat_filter = std::bind ( &StockpileSerializer::armor_mat_is_allowed, this, _1 ); 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 ); 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 // 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 ); return armor.quality_core ( idx );
@ -1663,7 +1848,6 @@ private:
return armor.quality_total ( idx ); return armor.quality_total ( idx );
}, armor.quality_total_size() ); }, armor.quality_total_size() );
} }
} }
}; };