cleaner mask-based implementation

develop
myk002 2020-11-12 22:44:38 -08:00
parent 6b14a92385
commit 10616a387f
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
7 changed files with 25 additions and 20 deletions

@ -88,7 +88,9 @@ namespace DFHack
bool find(const std::string &token);
bool matches(df::job_item_vector_id vec_id);
bool matches(const df::job_item &item, MaterialInfo *mat = NULL, bool skip_vector = false);
bool matches(const df::job_item &item, MaterialInfo *mat = NULL,
bool skip_vector = false,
df::item_type itype = df::item_type::NONE);
};
inline bool operator== (const ItemTypeInfo &a, const ItemTypeInfo &b) {

@ -102,7 +102,9 @@ namespace DFHack
int filter_idx = -1, int insert_idx = -1);
DFHACK_EXPORT bool isSuitableItem(df::job_item *item, df::item_type itype, int isubtype);
DFHACK_EXPORT bool isSuitableMaterial(df::job_item *item, int mat_type, int mat_index, df::item_type itype = df::item_type::NONE);
DFHACK_EXPORT bool isSuitableMaterial(df::job_item *item, int mat_type,
int mat_index,
df::item_type itype);
DFHACK_EXPORT std::string getName(df::job *job);
}

@ -78,7 +78,6 @@ namespace DFHack
int16_t type;
int32_t index;
df::item_type itype;
df::material *material;
@ -99,7 +98,7 @@ namespace DFHack
df::historical_figure *figure;
public:
MaterialInfo(int16_t type = -1, int32_t index = -1, df::item_type itype = df::item_type::NONE) { decode(type, index, itype); }
MaterialInfo(int16_t type = -1, int32_t index = -1) { decode(type, index); }
MaterialInfo(const t_matpair &mp) { decode(mp.mat_type, mp.mat_index); }
template<class T> MaterialInfo(T *ptr) { decode(ptr); }
@ -114,7 +113,7 @@ namespace DFHack
bool isAnyInorganic() const { return type == 0; }
bool isInorganicWildcard() const { return isAnyInorganic() && isBuiltin(); }
bool decode(int16_t type, int32_t index = -1, df::item_type itype = df::item_type::NONE);
bool decode(int16_t type, int32_t index = -1);
bool decode(df::item *item);
bool decode(const df::material_vec_ref &vr, int idx);
bool decode(const t_matpair &mp) { return decode(mp.mat_type, mp.mat_index); }
@ -157,7 +156,8 @@ namespace DFHack
bool matches(const df::job_material_category &cat);
bool matches(const df::dfhack_material_category &cat);
bool matches(const df::job_item &item);
bool matches(const df::job_item &item,
df::item_type itype = df::item_type::NONE);
};
DFHACK_EXPORT bool parseJobMaterialCategory(df::job_material_category *cat, const std::string &token);

@ -279,12 +279,13 @@ bool ItemTypeInfo::matches(df::job_item_vector_id vec_id)
return true;
}
bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat, bool skip_vector)
bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat,
bool skip_vector, df::item_type itype)
{
using namespace df::enums::item_type;
if (!isValid())
return mat ? mat->matches(item) : false;
return mat ? mat->matches(item, itype) : false;
if (Items::isCasteMaterial(type) && mat && !mat->isNone())
return false;

@ -605,7 +605,7 @@ bool Job::isSuitableItem(df::job_item *item, df::item_type itype, int isubtype)
ItemTypeInfo iinfo(itype, isubtype);
MaterialInfo minfo(item);
return iinfo.isValid() && iinfo.matches(*item, &minfo);
return iinfo.isValid() && iinfo.matches(*item, &minfo, true, itype);
}
bool Job::isSuitableMaterial(
@ -617,9 +617,9 @@ bool Job::isSuitableMaterial(
return true;
ItemTypeInfo iinfo(item);
MaterialInfo minfo(mat_type, mat_index, itype);
MaterialInfo minfo(mat_type, mat_index);
return minfo.isValid() && iinfo.matches(*item, &minfo);
return minfo.isValid() && iinfo.matches(*item, &minfo, true, itype);
}
std::string Job::getName(df::job *job)

@ -76,8 +76,7 @@ bool MaterialInfo::decode(df::item *item)
return decode(-1);
else
return decode(item->getActualMaterial(),
item->getActualMaterialIndex(),
item->getType());
item->getActualMaterialIndex());
}
bool MaterialInfo::decode(const df::material_vec_ref &vr, int idx)
@ -88,11 +87,10 @@ bool MaterialInfo::decode(const df::material_vec_ref &vr, int idx)
return decode(vr.mat_type[idx], vr.mat_index[idx]);
}
bool MaterialInfo::decode(int16_t type, int32_t index, df::item_type itype)
bool MaterialInfo::decode(int16_t type, int32_t index)
{
this->type = type;
this->index = index;
this->itype = itype;
material = NULL;
mode = Builtin; subtype = 0;
@ -447,19 +445,22 @@ bool MaterialInfo::matches(const df::dfhack_material_category &cat)
#undef TEST
bool MaterialInfo::matches(const df::job_item &item)
bool MaterialInfo::matches(const df::job_item &item, df::item_type itype)
{
if (!isValid()) return false;
df::job_item_flags1 ok1, mask1;
getMatchBits(ok1, mask1);
df::job_item_flags2 ok2, mask2;
df::job_item_flags2 ok2, mask2, xmask2;
getMatchBits(ok2, mask2);
df::job_item_flags3 ok3, mask3;
getMatchBits(ok3, mask3);
xmask2.bits.non_economic = itype != df::item_type::BOULDER;
mask2.whole &= ~xmask2.whole;
return bits_match(item.flags1.whole, ok1.whole, mask1.whole) &&
bits_match(item.flags2.whole, ok2.whole, mask2.whole) &&
bits_match(item.flags3.whole, ok3.whole, mask3.whole);
@ -516,8 +517,7 @@ void MaterialInfo::getMatchBits(df::job_item_flags2 &ok, df::job_item_flags2 &ma
TEST(fire_safe, material->heat.melting_point > 11000);
TEST(magma_safe, material->heat.melting_point > 12000);
TEST(deep_material, FLAG(inorganic, inorganic_flags::SPECIAL));
TEST(non_economic, !inorganic || !(ui && vector_get(ui->economic_stone, index)) || itype == df::item_type::BAR);
TEST(non_economic, !inorganic || !(ui && vector_get(ui->economic_stone, index)));
TEST(plant, plant);
TEST(silk, MAT_FLAG(SILK));
TEST(leather, MAT_FLAG(LEATHER));

@ -224,7 +224,7 @@ bool ItemFilter::matches(df::item *item) const
auto imattype = item->getActualMaterial();
auto imatindex = item->getActualMaterialIndex();
auto item_mat = DFHack::MaterialInfo(imattype, imatindex, item->getType());
auto item_mat = DFHack::MaterialInfo(imattype, imatindex);
return (materials.size() == 0) ? matchesMask(item_mat) : matches(item_mat);
}