Merge pull request #3005 from myk002/myk_stockpiles_refuse
cleanup stockpiles plugin and don't bork on empty type vectorsdevelop
commit
f6718b7d5c
@ -1,154 +1,126 @@
|
|||||||
#include "OrganicMatLookup.h"
|
#include "OrganicMatLookup.h"
|
||||||
|
|
||||||
#include "StockpileUtils.h"
|
#include "StockpileUtils.h"
|
||||||
|
|
||||||
#include "modules/Materials.h"
|
#include "Debug.h"
|
||||||
#include "MiscUtils.h"
|
|
||||||
|
|
||||||
#include "df/world.h"
|
|
||||||
#include "df/world_data.h"
|
|
||||||
|
|
||||||
#include "df/creature_raw.h"
|
#include "df/creature_raw.h"
|
||||||
#include "df/caste_raw.h"
|
#include "df/caste_raw.h"
|
||||||
#include "df/material.h"
|
#include "df/world.h"
|
||||||
|
|
||||||
using namespace DFHack;
|
using namespace DFHack;
|
||||||
using namespace df::enums;
|
using namespace df::enums;
|
||||||
using df::global::world;
|
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
|
* Helper class for mapping the various organic mats between their material indices
|
||||||
* and their index in the stockpile_settings structures.
|
* 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 )
|
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);
|
||||||
out << "food_lookup: food_idx(" << food_idx << ") ";
|
df::world_raws& raws = world->raws;
|
||||||
df::world_raws &raws = world->raws;
|
|
||||||
df::special_mat_table table = raws.mat_table;
|
df::special_mat_table table = raws.mat_table;
|
||||||
int32_t main_idx = table.organic_indexes[mat_category][food_idx];
|
int32_t main_idx = table.organic_indexes[mat_category][food_idx];
|
||||||
int16_t type = table.organic_types[mat_category][food_idx];
|
int16_t type = table.organic_types[mat_category][food_idx];
|
||||||
if ( mat_category == organic_mat_category::Fish ||
|
if (mat_category == organic_mat_category::Fish ||
|
||||||
mat_category == organic_mat_category::UnpreparedFish ||
|
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.creature = raws.creatures.all[type];
|
||||||
food_mat.caste = food_mat.creature->caste[main_idx];
|
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
|
else {
|
||||||
{
|
food_mat.material.decode(type, main_idx);
|
||||||
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());
|
||||||
out << " type(" << type << ") index("<< main_idx <<") token(" << food_mat.material.getToken() << ")" << endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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;
|
FoodMat food_mat;
|
||||||
food_mat_by_idx ( out, mat_category, idx, food_mat );
|
food_mat_by_idx(mat_category, idx, food_mat);
|
||||||
if ( food_mat.material.isValid() )
|
if (food_mat.material.isValid()) {
|
||||||
{
|
|
||||||
return food_mat.material.getToken();
|
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 food_mat.creature->creature_id + ":" + food_mat.caste->caste_id;
|
||||||
}
|
}
|
||||||
return std::string();
|
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();
|
return world->raws.mat_table.organic_types[mat_category].size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OrganicMatLookup::food_build_map ( std::ostream &out )
|
void OrganicMatLookup::food_build_map() {
|
||||||
{
|
if (index_built)
|
||||||
if ( index_built )
|
|
||||||
return;
|
return;
|
||||||
df::world_raws &raws = world->raws;
|
df::world_raws& raws = world->raws;
|
||||||
df::special_mat_table table = raws.mat_table;
|
df::special_mat_table table = raws.mat_table;
|
||||||
using df::enums::organic_mat_category::organic_mat_category;
|
using df::enums::organic_mat_category::organic_mat_category;
|
||||||
using traits = df::enum_traits<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 (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 ( 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);
|
||||||
int16_t type = table.organic_types[mat_category].at ( i );
|
food_index[mat_category].insert(std::make_pair(std::make_pair(type, index), i)); // wtf.. only in c++
|
||||||
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;
|
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 OrganicMatLookup::food_idx_by_token(organic_mat_category::organic_mat_category mat_category, const std::string& token) {
|
||||||
{
|
df::world_raws& raws = world->raws;
|
||||||
int16_t food_idx = -1;
|
|
||||||
df::world_raws &raws = world->raws;
|
|
||||||
df::special_mat_table table = raws.mat_table;
|
df::special_mat_table table = raws.mat_table;
|
||||||
out << "food_idx_by_token: ";
|
DEBUG(log).print("food_idx_by_token: ");
|
||||||
if ( mat_category == organic_mat_category::Fish ||
|
if (mat_category == organic_mat_category::Fish ||
|
||||||
mat_category == organic_mat_category::UnpreparedFish ||
|
mat_category == organic_mat_category::UnpreparedFish ||
|
||||||
mat_category == organic_mat_category::Eggs )
|
mat_category == organic_mat_category::Eggs) {
|
||||||
{
|
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
split_string ( &tokens, token, ":" );
|
split_string(&tokens, token, ":");
|
||||||
if ( tokens.size() != 2 )
|
if (tokens.size() != 2) {
|
||||||
{
|
WARN(log).print("creature invalid CREATURE:CASTE token: %s\n", token.c_str());
|
||||||
out << "creature " << "invalid CREATURE:CASTE token: " << token << endl;
|
return -1;
|
||||||
}
|
}
|
||||||
else
|
int16_t creature_idx = find_creature(tokens[0]);
|
||||||
{
|
if (creature_idx < 0) {
|
||||||
int16_t creature_idx = find_creature ( tokens[0] );
|
WARN(log).print("creature invalid token %s\n", tokens[0].c_str());
|
||||||
if ( creature_idx < 0 )
|
return -1;
|
||||||
{
|
|
||||||
out << " creature invalid token " << tokens[0];
|
|
||||||
}
|
}
|
||||||
else
|
int16_t food_idx = linear_index(table.organic_types[mat_category], creature_idx);
|
||||||
{
|
if (tokens[1] == "MALE")
|
||||||
food_idx = linear_index ( table.organic_types[mat_category], creature_idx );
|
|
||||||
if ( tokens[1] == "MALE" )
|
|
||||||
food_idx += 1;
|
food_idx += 1;
|
||||||
if ( table.organic_types[mat_category][food_idx] == creature_idx )
|
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;
|
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);
|
||||||
else
|
return food_idx;
|
||||||
{
|
|
||||||
out << "ERROR creature caste not found: " << token << " caste " << tokens[1] << " creature_idx(" << creature_idx << ") food_idx("<< food_idx << ")" << endl;
|
|
||||||
food_idx = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
if (!index_built)
|
||||||
if ( !index_built )
|
food_build_map();
|
||||||
food_build_map ( out );
|
MaterialInfo mat_info = food_mat_by_token(token);
|
||||||
MaterialInfo mat_info = food_mat_by_token ( out, token );
|
|
||||||
int16_t type = mat_info.type;
|
int16_t type = mat_info.type;
|
||||||
int32_t index = mat_info.index;
|
int32_t index = mat_info.index;
|
||||||
auto it = food_index[mat_category].find ( std::make_pair ( type, index ) );
|
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()) {
|
||||||
{
|
DEBUG(log).print("matinfo: %s type(%d) idx(%d) food_idx(%zd)\n", token.c_str(), mat_info.type, mat_info.index, it->second);
|
||||||
out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx(" << it->second << ")" << endl;
|
return it->second;
|
||||||
food_idx = it->second;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
out << "matinfo: " << token << " type(" << mat_info.type << ") idx(" << mat_info.index << ") food_idx not found :(" << endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return food_idx;
|
|
||||||
|
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;
|
MaterialInfo mat_info;
|
||||||
mat_info.find ( token );
|
mat_info.find(token);
|
||||||
return mat_info;
|
return mat_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OrganicMatLookup::index_built = false;
|
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
Loading…
Reference in New Issue