2014-12-02 04:06:01 -07:00
|
|
|
#include "OrganicMatLookup.h"
|
|
|
|
#include "StockpileUtils.h"
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
#include "Debug.h"
|
2014-12-02 04:06:01 -07:00
|
|
|
|
|
|
|
#include "df/creature_raw.h"
|
|
|
|
#include "df/caste_raw.h"
|
2023-03-10 15:08:31 -07:00
|
|
|
#include "df/world.h"
|
2014-12-02 04:06:01 -07:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
using df::global::world;
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
namespace DFHack {
|
|
|
|
DBG_EXTERN(stockpiles, log);
|
|
|
|
}
|
2014-12-02 04:06:01 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class for mapping the various organic mats between their material indices
|
|
|
|
* and their index in the stockpile_settings structures.
|
|
|
|
*/
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
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) {
|
2023-03-20 17:05:07 -06:00
|
|
|
DEBUG(log).print("food_lookup: food_idx(%zd)\n", food_idx);
|
2023-03-10 15:08:31 -07:00
|
|
|
df::world_raws& raws = world->raws;
|
2014-12-02 04:06:01 -07:00
|
|
|
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];
|
2023-03-10 15:08:31 -07:00
|
|
|
if (mat_category == organic_mat_category::Fish ||
|
|
|
|
mat_category == organic_mat_category::UnpreparedFish ||
|
|
|
|
mat_category == organic_mat_category::Eggs) {
|
2014-12-02 04:06:01 -07:00
|
|
|
food_mat.creature = raws.creatures.all[type];
|
|
|
|
food_mat.caste = food_mat.creature->caste[main_idx];
|
2023-03-20 17:05:07 -06:00
|
|
|
DEBUG(log).print("special creature type(%d) caste(%d)\n", type, main_idx);
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
else {
|
|
|
|
food_mat.material.decode(type, main_idx);
|
2023-03-20 17:05:07 -06:00
|
|
|
DEBUG(log).print("type(%d) index(%d) token(%s)\n", type, main_idx, food_mat.material.getToken().c_str());
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
std::string OrganicMatLookup::food_token_by_idx(organic_mat_category::organic_mat_category mat_category, std::vector<int16_t>::size_type idx) {
|
2014-12-02 04:06:01 -07:00
|
|
|
FoodMat food_mat;
|
2023-03-10 15:08:31 -07:00
|
|
|
food_mat_by_idx(mat_category, idx, food_mat);
|
|
|
|
if (food_mat.material.isValid()) {
|
2014-12-02 04:06:01 -07:00
|
|
|
return food_mat.material.getToken();
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
else if (food_mat.creature) {
|
2014-12-02 04:06:01 -07:00
|
|
|
return food_mat.creature->creature_id + ":" + food_mat.caste->caste_id;
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
size_t OrganicMatLookup::food_max_size(organic_mat_category::organic_mat_category mat_category) {
|
2014-12-02 04:06:01 -07:00
|
|
|
return world->raws.mat_table.organic_types[mat_category].size();
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
void OrganicMatLookup::food_build_map() {
|
|
|
|
if (index_built)
|
2014-12-02 04:06:01 -07:00
|
|
|
return;
|
2023-03-10 15:08:31 -07:00
|
|
|
df::world_raws& raws = world->raws;
|
2014-12-02 04:06:01 -07:00
|
|
|
df::special_mat_table table = raws.mat_table;
|
|
|
|
using df::enums::organic_mat_category::organic_mat_category;
|
2018-04-06 20:22:48 -06:00
|
|
|
using traits = df::enum_traits<organic_mat_category>;
|
2023-03-10 15:08:31 -07:00
|
|
|
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++
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
index_built = true;
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
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;
|
2014-12-02 04:06:01 -07:00
|
|
|
df::special_mat_table table = raws.mat_table;
|
2023-03-20 17:05:07 -06:00
|
|
|
DEBUG(log).print("food_idx_by_token:\n");
|
2023-03-10 15:08:31 -07:00
|
|
|
if (mat_category == organic_mat_category::Fish ||
|
|
|
|
mat_category == organic_mat_category::UnpreparedFish ||
|
|
|
|
mat_category == organic_mat_category::Eggs) {
|
2014-12-02 04:06:01 -07:00
|
|
|
std::vector<std::string> tokens;
|
2023-03-10 15:08:31 -07:00
|
|
|
split_string(&tokens, token, ":");
|
|
|
|
if (tokens.size() != 2) {
|
|
|
|
WARN(log).print("creature invalid CREATURE:CASTE token: %s\n", token.c_str());
|
|
|
|
return -1;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
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;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
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;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
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;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
|
|
|
|
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;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
MaterialInfo OrganicMatLookup::food_mat_by_token(const std::string& token) {
|
2014-12-02 04:06:01 -07:00
|
|
|
MaterialInfo mat_info;
|
2023-03-10 15:08:31 -07:00
|
|
|
mat_info.find(token);
|
2014-12-02 04:06:01 -07:00
|
|
|
return mat_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OrganicMatLookup::index_built = false;
|
2023-03-10 15:08:31 -07:00
|
|
|
std::vector<OrganicMatLookup::FoodMatMap> OrganicMatLookup::food_index = std::vector<OrganicMatLookup::FoodMatMap>(df::enum_traits< df::organic_mat_category >::last_item_value + 1);
|