cleanup stockpiles plugin and don't bork on empty type vectors
parent
7c169e8585
commit
c56e248034
@ -1,154 +1,126 @@
|
||||
#include "OrganicMatLookup.h"
|
||||
|
||||
#include "StockpileUtils.h"
|
||||
|
||||
#include "modules/Materials.h"
|
||||
#include "MiscUtils.h"
|
||||
|
||||
#include "df/world.h"
|
||||
#include "df/world_data.h"
|
||||
#include "Debug.h"
|
||||
|
||||
#include "df/creature_raw.h"
|
||||
#include "df/caste_raw.h"
|
||||
#include "df/material.h"
|
||||
#include "df/world.h"
|
||||
|
||||
using namespace DFHack;
|
||||
using namespace df::enums;
|
||||
using df::global::world;
|
||||
|
||||
using std::endl;
|
||||
namespace DFHack {
|
||||
DBG_EXTERN(stockpiles, log);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class for mapping the various organic mats between their material indices
|
||||
* and their index in the stockpile_settings structures.
|
||||
*/
|
||||
|
||||
void OrganicMatLookup::food_mat_by_idx ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector<int16_t>::size_type food_idx, FoodMat & food_mat )
|
||||
{
|
||||
out << "food_lookup: food_idx(" << food_idx << ") ";
|
||||
df::world_raws &raws = world->raws;
|
||||
void OrganicMatLookup::food_mat_by_idx(organic_mat_category::organic_mat_category mat_category, std::vector<int16_t>::size_type food_idx, FoodMat& food_mat) {
|
||||
DEBUG(log).print("food_lookup: food_idx(%zd) ", 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 )
|
||||
{
|
||||
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 <<")" <<endl;
|
||||
DEBUG(log).print("special creature type(%d) caste(%d)", type, main_idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
food_mat.material.decode ( type, main_idx );
|
||||
out << " type(" << type << ") index("<< main_idx <<") token(" << food_mat.material.getToken() << ")" << endl;
|
||||
else {
|
||||
food_mat.material.decode(type, main_idx);
|
||||
DEBUG(log).print("type(%d) index(%d) token(%s)", type, main_idx, food_mat.material.getToken().c_str());
|
||||
}
|
||||
}
|
||||
std::string OrganicMatLookup::food_token_by_idx ( std::ostream &out, organic_mat_category::organic_mat_category mat_category, std::vector<int16_t>::size_type idx )
|
||||
{
|
||||
std::string OrganicMatLookup::food_token_by_idx(organic_mat_category::organic_mat_category mat_category, std::vector<int16_t>::size_type idx) {
|
||||
FoodMat food_mat;
|
||||
food_mat_by_idx ( out, mat_category, idx, food_mat );
|
||||
if ( food_mat.material.isValid() )
|
||||
{
|
||||
food_mat_by_idx(mat_category, idx, food_mat);
|
||||
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();
|
||||
}
|
||||
|
||||
size_t OrganicMatLookup::food_max_size ( organic_mat_category::organic_mat_category mat_category )
|
||||
{
|
||||
size_t OrganicMatLookup::food_max_size(organic_mat_category::organic_mat_category mat_category) {
|
||||
return world->raws.mat_table.organic_types[mat_category].size();
|
||||
}
|
||||
|
||||
void OrganicMatLookup::food_build_map ( std::ostream &out )
|
||||
{
|
||||
if ( index_built )
|
||||
void OrganicMatLookup::food_build_map() {
|
||||
if (index_built)
|
||||
return;
|
||||
df::world_raws &raws = world->raws;
|
||||
df::world_raws& raws = world->raws;
|
||||
df::special_mat_table table = raws.mat_table;
|
||||
using df::enums::organic_mat_category::organic_mat_category;
|
||||
using traits = df::enum_traits<organic_mat_category>;
|
||||
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++
|
||||
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++
|
||||
}
|
||||
}
|
||||
index_built = true;
|
||||
}
|
||||
|
||||
int16_t OrganicMatLookup::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;
|
||||
int16_t OrganicMatLookup::food_idx_by_token(organic_mat_category::organic_mat_category mat_category, const std::string& token) {
|
||||
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 )
|
||||
{
|
||||
DEBUG(log).print("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<std::string> tokens;
|
||||
split_string ( &tokens, token, ":" );
|
||||
if ( tokens.size() != 2 )
|
||||
{
|
||||
out << "creature " << "invalid CREATURE:CASTE token: " << token << endl;
|
||||
split_string(&tokens, token, ":");
|
||||
if (tokens.size() != 2) {
|
||||
WARN(log).print("creature invalid CREATURE:CASTE token: %s\n", token.c_str());
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int16_t creature_idx = find_creature ( tokens[0] );
|
||||
if ( creature_idx < 0 )
|
||||
{
|
||||
out << " creature invalid token " << tokens[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
food_idx = linear_index ( table.organic_types[mat_category], creature_idx );
|
||||
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;
|
||||
}
|
||||
}
|
||||
int16_t creature_idx = find_creature(tokens[0]);
|
||||
if (creature_idx < 0) {
|
||||
WARN(log).print("creature invalid token %s\n", tokens[0].c_str());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
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;
|
||||
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;
|
||||
food_idx = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx not found :(" << endl;
|
||||
int16_t food_idx = linear_index(table.organic_types[mat_category], creature_idx);
|
||||
if (tokens[1] == "MALE")
|
||||
food_idx += 1;
|
||||
if (table.organic_types[mat_category][food_idx] == creature_idx) {
|
||||
DEBUG(log).print("creature %s caste %s creature_idx(%d) food_idx(%d)\n", token.c_str(), tokens[1].c_str(), creature_idx, food_idx);
|
||||
return food_idx;
|
||||
}
|
||||
WARN(log).print("creature caste not found: %s caste %s creature_idx(%d) food_idx(%d)\n", token.c_str(), tokens[1].c_str(), creature_idx, food_idx);
|
||||
return -1;
|
||||
}
|
||||
return food_idx;
|
||||
|
||||
if (!index_built)
|
||||
food_build_map();
|
||||
MaterialInfo mat_info = food_mat_by_token(token);
|
||||
int16_t type = mat_info.type;
|
||||
int32_t index = mat_info.index;
|
||||
auto it = food_index[mat_category].find(std::make_pair(type, index));
|
||||
if (it != food_index[mat_category].end()) {
|
||||
DEBUG(log).print("matinfo: %s type(%d) idx(%d) food_idx(%zd)\n", token.c_str(), mat_info.type, mat_info.index, it->second);
|
||||
return it->second;
|
||||
|
||||
}
|
||||
|
||||
WARN(log).print("matinfo: %s type(%d) idx(%d) food_idx not found :(\n", token.c_str(), mat_info.type, mat_info.index);
|
||||
return -1;
|
||||
}
|
||||
|
||||
MaterialInfo OrganicMatLookup::food_mat_by_token ( std::ostream &out, const std::string & token )
|
||||
{
|
||||
MaterialInfo OrganicMatLookup::food_mat_by_token(const std::string& token) {
|
||||
MaterialInfo mat_info;
|
||||
mat_info.find ( token );
|
||||
mat_info.find(token);
|
||||
return mat_info;
|
||||
}
|
||||
|
||||
bool OrganicMatLookup::index_built = false;
|
||||
std::vector<OrganicMatLookup::FoodMatMap> OrganicMatLookup::food_index = std::vector<OrganicMatLookup::FoodMatMap> ( df::enum_traits< df::organic_mat_category >::last_item_value + 1 );
|
||||
std::vector<OrganicMatLookup::FoodMatMap> OrganicMatLookup::food_index = std::vector<OrganicMatLookup::FoodMatMap>(df::enum_traits< df::organic_mat_category >::last_item_value + 1);
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,349 +1,283 @@
|
||||
#pragma once
|
||||
|
||||
// stockpiles plugin
|
||||
#include "proto/stockpiles.pb.h"
|
||||
|
||||
// dfhack
|
||||
#include "modules/Materials.h"
|
||||
#include "modules/Items.h"
|
||||
|
||||
// df
|
||||
#include "df/world.h"
|
||||
#include "df/world_data.h"
|
||||
#include "df/itemdef.h"
|
||||
#include "df/organic_mat_category.h"
|
||||
#include "df/furniture_type.h"
|
||||
#include "df/item_quality.h"
|
||||
#include "df/item_type.h"
|
||||
|
||||
// stl
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <ostream>
|
||||
#include <istream>
|
||||
|
||||
namespace df {
|
||||
struct building_stockpilest;
|
||||
}
|
||||
|
||||
#include "proto/stockpiles.pb.h"
|
||||
|
||||
/**
|
||||
* Null buffer that acts like /dev/null for when debug is disabled
|
||||
*/
|
||||
class NullBuffer : public std::streambuf
|
||||
{
|
||||
public:
|
||||
int overflow ( int c );
|
||||
};
|
||||
#include <functional>
|
||||
|
||||
class NullStream : public std::ostream
|
||||
namespace df
|
||||
{
|
||||
public:
|
||||
NullStream();
|
||||
private:
|
||||
NullBuffer m_sb;
|
||||
};
|
||||
|
||||
struct building_stockpilest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for serializing the stockpile_settings structure into a Google protobuf
|
||||
*/
|
||||
class StockpileSerializer
|
||||
{
|
||||
class StockpileSerializer {
|
||||
public:
|
||||
/**
|
||||
* @param out for debugging
|
||||
* @param stockpile stockpile to read or write settings to
|
||||
*/
|
||||
|
||||
StockpileSerializer ( df::building_stockpilest * stockpile );
|
||||
|
||||
~StockpileSerializer();
|
||||
/**
|
||||
* @param out for debugging
|
||||
* @param stockpile stockpile to read or write settings to
|
||||
*/
|
||||
|
||||
void enable_debug ( std::ostream &out );
|
||||
StockpileSerializer(df::building_stockpilest* stockpile);
|
||||
|
||||
/**
|
||||
* Since we depend on protobuf-lite, not the full lib, we copy this function from
|
||||
* protobuf message.cc
|
||||
*/
|
||||
bool serialize_to_ostream(std::ostream* output);
|
||||
~StockpileSerializer();
|
||||
|
||||
/**
|
||||
* Will serialize stockpile settings to a file (overwrites existing files)
|
||||
* @return success/failure
|
||||
*/
|
||||
bool serialize_to_file ( const std::string & file );
|
||||
/**
|
||||
* Since we depend on protobuf-lite, not the full lib, we copy this function from
|
||||
* protobuf message.cc
|
||||
*/
|
||||
bool serialize_to_ostream(std::ostream* output);
|
||||
|
||||
/**
|
||||
* Again, copied from message.cc
|
||||
*/
|
||||
bool parse_from_istream(std::istream* input);
|
||||
/**
|
||||
* Will serialize stockpile settings to a file (overwrites existing files)
|
||||
* @return success/failure
|
||||
*/
|
||||
bool serialize_to_file(const std::string& file);
|
||||
|
||||
/**
|
||||
* Again, copied from message.cc
|
||||
*/
|
||||
bool parse_from_istream(std::istream* input);
|
||||
|
||||
/**
|
||||
* Read stockpile settings from file
|
||||
*/
|
||||
bool unserialize_from_file ( const std::string & file );
|
||||
/**
|
||||
* Read stockpile settings from file
|
||||
*/
|
||||
bool unserialize_from_file(const std::string& file);
|
||||
|
||||
private:
|
||||
df::building_stockpilest* mPile;
|
||||
dfstockpiles::StockpileSettings mBuffer;
|
||||
std::map<int, std::string> mOtherMatsFurniture;
|
||||
std::map<int, std::string> mOtherMatsFinishedGoods;
|
||||
std::map<int, std::string> mOtherMatsBars;
|
||||
std::map<int, std::string> mOtherMatsBlocks;
|
||||
std::map<int, std::string> mOtherMatsWeaponsArmor;
|
||||
|
||||
/**
|
||||
read memory structures and serialize to protobuf
|
||||
*/
|
||||
void write();
|
||||
|
||||
// parse serialized data into ui indices
|
||||
void read();
|
||||
|
||||
/**
|
||||
* 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, -1 if not found
|
||||
*/
|
||||
template <typename E>
|
||||
static typename df::enum_traits<E>::base_type linear_index(df::enum_traits<E> 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) {
|
||||
if (token.compare(traits.key_table[j]) == 0)
|
||||
return j;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// read the token from the serailized list during import
|
||||
typedef std::function<std::string(const size_t&)> FuncReadImport;
|
||||
// add the token to the serialized list during export
|
||||
typedef std::function<void(const std::string&)> FuncWriteExport;
|
||||
// are item's of item_type allowed?
|
||||
typedef std::function<bool(df::enums::item_type::item_type)> FuncItemAllowed;
|
||||
// is this material allowed?
|
||||
typedef std::function<bool(const DFHack::MaterialInfo&)> FuncMaterialAllowed;
|
||||
|
||||
// convenient struct for parsing food stockpile items
|
||||
struct food_pair {
|
||||
// exporting
|
||||
FuncWriteExport set_value;
|
||||
std::vector<char>* stockpile_values;
|
||||
// importing
|
||||
FuncReadImport get_value;
|
||||
size_t serialized_count;
|
||||
bool valid;
|
||||
|
||||
food_pair(FuncWriteExport s, std::vector<char>* sp_v, FuncReadImport g, size_t count)
|
||||
: set_value(s), stockpile_values(sp_v), get_value(g), serialized_count(count), valid(true) { }
|
||||
food_pair(): valid(false) { }
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 std::vector<char>* list, df::enums::organic_mat_category::organic_mat_category cat);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_organic_mat(FuncReadImport get_value, size_t list_size, std::vector<char>* pile_list, df::enums::organic_mat_category::organic_mat_category cat);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_item_type(FuncItemAllowed is_allowed, FuncWriteExport add_value, const std::vector<char>& list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_item_type(FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector<char>* pile_list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_material(FuncMaterialAllowed is_allowed, FuncWriteExport add_value, const std::vector<char>& list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_material(FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector<char>* pile_list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_quality(FuncWriteExport add_value, const bool(&quality_list)[7]);
|
||||
|
||||
/**
|
||||
* Set all values in a bool[7] to false
|
||||
*/
|
||||
void quality_clear(bool(&pile_list)[7]);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_quality(FuncReadImport read_value, int32_t list_size, bool(&pile_list)[7]);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_other_mats(const std::map<int, std::string> other_mats, FuncWriteExport add_value, std::vector<char> list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_other_mats(const std::map<int, std::string> other_mats, FuncReadImport read_value, int32_t list_size, std::vector<char>* pile_list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_itemdef(FuncWriteExport add_value, std::vector<char> list, std::vector<df::itemdef*> items, df::enums::item_type::item_type type);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_itemdef(FuncReadImport read_value, int32_t list_size, std::vector<char>* pile_list, df::enums::item_type::item_type type);
|
||||
|
||||
/**
|
||||
* 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<int, std::string> other_mats, int idx);
|
||||
|
||||
/**
|
||||
* 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<int, std::string> other_mats, const std::string& token);
|
||||
|
||||
void write_general();
|
||||
void read_general();
|
||||
|
||||
void write_animals();
|
||||
void read_animals();
|
||||
|
||||
bool mDebug;
|
||||
std::ostream * mOut;
|
||||
NullStream mNull;
|
||||
df::building_stockpilest * mPile;
|
||||
dfstockpiles::StockpileSettings mBuffer;
|
||||
std::map<int, std::string> mOtherMatsFurniture;
|
||||
std::map<int, std::string> mOtherMatsFinishedGoods;
|
||||
std::map<int, std::string> mOtherMatsBars;
|
||||
std::map<int, std::string> mOtherMatsBlocks;
|
||||
std::map<int, std::string> mOtherMatsWeaponsArmor;
|
||||
|
||||
|
||||
std::ostream & debug();
|
||||
|
||||
/**
|
||||
read memory structures and serialize to protobuf
|
||||
*/
|
||||
void write();
|
||||
|
||||
// parse serialized data into ui indices
|
||||
void read ();
|
||||
|
||||
/**
|
||||
* 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, -1 if not found
|
||||
*/
|
||||
template<typename E>
|
||||
static typename df::enum_traits<E>::base_type linear_index ( std::ostream & out, df::enum_traits<E> 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["<<j<<"/"<<limit<<"]: " <<traits.key_table[j] << endl;
|
||||
if ( token.compare ( traits.key_table[j] ) == 0 )
|
||||
return j;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// read the token from the serailized list during import
|
||||
typedef std::function<std::string ( const size_t& ) > FuncReadImport;
|
||||
// add the token to the serialized list during export
|
||||
typedef std::function<void ( const std::string & ) > FuncWriteExport;
|
||||
// are item's of item_type allowed?
|
||||
typedef std::function<bool ( df::enums::item_type::item_type ) > FuncItemAllowed;
|
||||
// is this material allowed?
|
||||
typedef std::function<bool ( const DFHack::MaterialInfo & ) > FuncMaterialAllowed;
|
||||
|
||||
// convenient struct for parsing food stockpile items
|
||||
struct food_pair
|
||||
{
|
||||
// exporting
|
||||
FuncWriteExport set_value;
|
||||
std::vector<char> * stockpile_values;
|
||||
// importing
|
||||
FuncReadImport get_value;
|
||||
size_t serialized_count;
|
||||
bool valid;
|
||||
|
||||
food_pair ( FuncWriteExport s, std::vector<char>* sp_v, FuncReadImport g, size_t count )
|
||||
: set_value ( s )
|
||||
, stockpile_values ( sp_v )
|
||||
, get_value ( g )
|
||||
, serialized_count ( count )
|
||||
, valid ( true )
|
||||
{}
|
||||
food_pair(): valid( false ) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 std::vector<char> * list, df::enums::organic_mat_category::organic_mat_category cat );
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_organic_mat ( FuncReadImport get_value, size_t list_size, std::vector<char> *pile_list, df::enums::organic_mat_category::organic_mat_category cat );
|
||||
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_item_type ( FuncItemAllowed is_allowed, FuncWriteExport add_value, const std::vector<char> &list );
|
||||
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_item_type ( FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list );
|
||||
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_material ( FuncMaterialAllowed is_allowed, FuncWriteExport add_value, const std::vector<char> &list );
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_material ( FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list );
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_quality ( FuncWriteExport add_value, const bool ( &quality_list ) [7] );
|
||||
|
||||
/**
|
||||
* Set all values in a bool[7] to false
|
||||
*/
|
||||
void quality_clear ( bool ( &pile_list ) [7] );
|
||||
|
||||
food_pair food_map(df::enums::organic_mat_category::organic_mat_category cat);
|
||||
|
||||
void write_food();
|
||||
void read_food();
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_quality ( FuncReadImport read_value, int32_t list_size, bool ( &pile_list ) [7] );
|
||||
void furniture_setup_other_mats();
|
||||
void write_furniture();
|
||||
bool furniture_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
void read_furniture();
|
||||
|
||||
bool refuse_creature_is_allowed(const df::creature_raw* raw);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_other_mats ( const std::map<int, std::string> other_mats, FuncWriteExport add_value, std::vector<char> list );
|
||||
void refuse_write_helper(std::function<void(const std::string&)> add_value, const std::vector<char>& list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_other_mats ( const std::map<int, std::string> other_mats, FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list );
|
||||
bool refuse_type_is_allowed(df::enums::item_type::item_type type);
|
||||
|
||||
void write_refuse();
|
||||
void refuse_read_helper(std::function<std::string(const size_t&)> get_value, size_t list_size, std::vector<char>* pile_list);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void serialize_list_itemdef ( FuncWriteExport add_value, std::vector<char> list, std::vector<df::itemdef *> items, df::enums::item_type::item_type type );
|
||||
void read_refuse();
|
||||
|
||||
bool stone_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
|
||||
/**
|
||||
* @see serialize_list_organic_mat
|
||||
*/
|
||||
void unserialize_list_itemdef ( FuncReadImport read_value, int32_t list_size, std::vector<char> *pile_list, df::enums::item_type::item_type type );
|
||||
void write_stone();
|
||||
|
||||
void read_stone();
|
||||
|
||||
/**
|
||||
* 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<int, std::string> other_mats, int idx );
|
||||
bool ammo_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
|
||||
/**
|
||||
* 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<int, std::string> other_mats, const std::string & token );
|
||||
void write_ammo();
|
||||
void read_ammo();
|
||||
bool coins_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
|
||||
void write_general();
|
||||
void read_general();
|
||||
void write_coins();
|
||||
|
||||
void read_coins();
|
||||
|
||||
void write_animals();
|
||||
void read_animals();
|
||||
void bars_blocks_setup_other_mats();
|
||||
|
||||
bool bars_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
|
||||
food_pair food_map ( df::enums::organic_mat_category::organic_mat_category cat );
|
||||
bool blocks_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
|
||||
void write_bars_blocks();
|
||||
void read_bars_blocks();
|
||||
bool gem_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
bool gem_cut_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
bool gem_other_mat_is_allowed(DFHack::MaterialInfo& mi);
|
||||
|
||||
void write_food();
|
||||
void read_food();
|
||||
void write_gems();
|
||||
|
||||
void furniture_setup_other_mats();
|
||||
void write_furniture();
|
||||
bool furniture_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
void read_furniture();
|
||||
void read_gems();
|
||||
|
||||
bool refuse_creature_is_allowed ( const df::creature_raw *raw );
|
||||
|
||||
|
||||
void refuse_write_helper ( std::function<void ( const std::string & ) > add_value, const std::vector<char> & list );
|
||||
|
||||
|
||||
bool refuse_type_is_allowed ( df::enums::item_type::item_type type );
|
||||
|
||||
|
||||
void write_refuse();
|
||||
void refuse_read_helper ( std::function<std::string ( const size_t& ) > get_value, size_t list_size, std::vector<char>* pile_list );
|
||||
|
||||
void read_refuse();
|
||||
|
||||
bool stone_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
|
||||
void write_stone();
|
||||
|
||||
void read_stone();
|
||||
|
||||
bool ammo_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
|
||||
void write_ammo();
|
||||
void read_ammo();
|
||||
bool coins_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
|
||||
void write_coins();
|
||||
|
||||
void read_coins();
|
||||
|
||||
void bars_blocks_setup_other_mats();
|
||||
|
||||
bool bars_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
|
||||
bool blocks_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
|
||||
void write_bars_blocks();
|
||||
void read_bars_blocks();
|
||||
bool gem_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
bool gem_cut_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
bool gem_other_mat_is_allowed(DFHack::MaterialInfo &mi );
|
||||
|
||||
void write_gems();
|
||||
|
||||
void read_gems();
|
||||
|
||||
bool finished_goods_type_is_allowed ( df::enums::item_type::item_type type );
|
||||
void finished_goods_setup_other_mats();
|
||||
bool finished_goods_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
void write_finished_goods();
|
||||
void read_finished_goods();
|
||||
void write_leather();
|
||||
void read_leather();
|
||||
void write_cloth();
|
||||
bool finished_goods_type_is_allowed(df::enums::item_type::item_type type);
|
||||
void finished_goods_setup_other_mats();
|
||||
bool finished_goods_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
void write_finished_goods();
|
||||
void read_finished_goods();
|
||||
void write_leather();
|
||||
void read_leather();
|
||||
void write_cloth();
|
||||
void read_cloth();
|
||||
bool wood_mat_is_allowed ( const df::plant_raw * plant );
|
||||
bool wood_mat_is_allowed(const df::plant_raw* plant);
|
||||
void write_wood();
|
||||
void read_wood();
|
||||
bool weapons_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
bool weapons_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
void write_weapons();
|
||||
void read_weapons();
|
||||
void weapons_armor_setup_other_mats();
|
||||
bool armor_mat_is_allowed ( const DFHack::MaterialInfo &mi );
|
||||
bool armor_mat_is_allowed(const DFHack::MaterialInfo& mi);
|
||||
void write_armor();
|
||||
void read_armor();
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue