Skip to content

Commit 10616a3

Browse files
committed
cleaner mask-based implementation
1 parent 6b14a92 commit 10616a3

7 files changed

Lines changed: 25 additions & 20 deletions

File tree

library/include/modules/Items.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ namespace DFHack
8888
bool find(const std::string &token);
8989

9090
bool matches(df::job_item_vector_id vec_id);
91-
bool matches(const df::job_item &item, MaterialInfo *mat = NULL, bool skip_vector = false);
91+
bool matches(const df::job_item &item, MaterialInfo *mat = NULL,
92+
bool skip_vector = false,
93+
df::item_type itype = df::item_type::NONE);
9294
};
9395

9496
inline bool operator== (const ItemTypeInfo &a, const ItemTypeInfo &b) {

library/include/modules/Job.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ namespace DFHack
102102
int filter_idx = -1, int insert_idx = -1);
103103

104104
DFHACK_EXPORT bool isSuitableItem(df::job_item *item, df::item_type itype, int isubtype);
105-
DFHACK_EXPORT bool isSuitableMaterial(df::job_item *item, int mat_type, int mat_index, df::item_type itype = df::item_type::NONE);
105+
DFHACK_EXPORT bool isSuitableMaterial(df::job_item *item, int mat_type,
106+
int mat_index,
107+
df::item_type itype);
106108
DFHACK_EXPORT std::string getName(df::job *job);
107109
}
108110

library/include/modules/Materials.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ namespace DFHack
7878

7979
int16_t type;
8080
int32_t index;
81-
df::item_type itype;
8281

8382
df::material *material;
8483

@@ -99,7 +98,7 @@ namespace DFHack
9998
df::historical_figure *figure;
10099

101100
public:
102-
MaterialInfo(int16_t type = -1, int32_t index = -1, df::item_type itype = df::item_type::NONE) { decode(type, index, itype); }
101+
MaterialInfo(int16_t type = -1, int32_t index = -1) { decode(type, index); }
103102
MaterialInfo(const t_matpair &mp) { decode(mp.mat_type, mp.mat_index); }
104103
template<class T> MaterialInfo(T *ptr) { decode(ptr); }
105104

@@ -114,7 +113,7 @@ namespace DFHack
114113
bool isAnyInorganic() const { return type == 0; }
115114
bool isInorganicWildcard() const { return isAnyInorganic() && isBuiltin(); }
116115

117-
bool decode(int16_t type, int32_t index = -1, df::item_type itype = df::item_type::NONE);
116+
bool decode(int16_t type, int32_t index = -1);
118117
bool decode(df::item *item);
119118
bool decode(const df::material_vec_ref &vr, int idx);
120119
bool decode(const t_matpair &mp) { return decode(mp.mat_type, mp.mat_index); }
@@ -157,7 +156,8 @@ namespace DFHack
157156

158157
bool matches(const df::job_material_category &cat);
159158
bool matches(const df::dfhack_material_category &cat);
160-
bool matches(const df::job_item &item);
159+
bool matches(const df::job_item &item,
160+
df::item_type itype = df::item_type::NONE);
161161
};
162162

163163
DFHACK_EXPORT bool parseJobMaterialCategory(df::job_material_category *cat, const std::string &token);

library/modules/Items.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,13 @@ bool ItemTypeInfo::matches(df::job_item_vector_id vec_id)
279279
return true;
280280
}
281281

282-
bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat, bool skip_vector)
282+
bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat,
283+
bool skip_vector, df::item_type itype)
283284
{
284285
using namespace df::enums::item_type;
285286

286287
if (!isValid())
287-
return mat ? mat->matches(item) : false;
288+
return mat ? mat->matches(item, itype) : false;
288289

289290
if (Items::isCasteMaterial(type) && mat && !mat->isNone())
290291
return false;

library/modules/Job.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ bool Job::isSuitableItem(df::job_item *item, df::item_type itype, int isubtype)
605605
ItemTypeInfo iinfo(itype, isubtype);
606606
MaterialInfo minfo(item);
607607

608-
return iinfo.isValid() && iinfo.matches(*item, &minfo);
608+
return iinfo.isValid() && iinfo.matches(*item, &minfo, true, itype);
609609
}
610610

611611
bool Job::isSuitableMaterial(
@@ -617,9 +617,9 @@ bool Job::isSuitableMaterial(
617617
return true;
618618

619619
ItemTypeInfo iinfo(item);
620-
MaterialInfo minfo(mat_type, mat_index, itype);
620+
MaterialInfo minfo(mat_type, mat_index);
621621

622-
return minfo.isValid() && iinfo.matches(*item, &minfo);
622+
return minfo.isValid() && iinfo.matches(*item, &minfo, true, itype);
623623
}
624624

625625
std::string Job::getName(df::job *job)

library/modules/Materials.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ bool MaterialInfo::decode(df::item *item)
7676
return decode(-1);
7777
else
7878
return decode(item->getActualMaterial(),
79-
item->getActualMaterialIndex(),
80-
item->getType());
79+
item->getActualMaterialIndex());
8180
}
8281

8382
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)
8887
return decode(vr.mat_type[idx], vr.mat_index[idx]);
8988
}
9089

91-
bool MaterialInfo::decode(int16_t type, int32_t index, df::item_type itype)
90+
bool MaterialInfo::decode(int16_t type, int32_t index)
9291
{
9392
this->type = type;
9493
this->index = index;
95-
this->itype = itype;
9694

9795
material = NULL;
9896
mode = Builtin; subtype = 0;
@@ -447,19 +445,22 @@ bool MaterialInfo::matches(const df::dfhack_material_category &cat)
447445

448446
#undef TEST
449447

450-
bool MaterialInfo::matches(const df::job_item &item)
448+
bool MaterialInfo::matches(const df::job_item &item, df::item_type itype)
451449
{
452450
if (!isValid()) return false;
453451

454452
df::job_item_flags1 ok1, mask1;
455453
getMatchBits(ok1, mask1);
456454

457-
df::job_item_flags2 ok2, mask2;
455+
df::job_item_flags2 ok2, mask2, xmask2;
458456
getMatchBits(ok2, mask2);
459457

460458
df::job_item_flags3 ok3, mask3;
461459
getMatchBits(ok3, mask3);
462460

461+
xmask2.bits.non_economic = itype != df::item_type::BOULDER;
462+
mask2.whole &= ~xmask2.whole;
463+
463464
return bits_match(item.flags1.whole, ok1.whole, mask1.whole) &&
464465
bits_match(item.flags2.whole, ok2.whole, mask2.whole) &&
465466
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
516517
TEST(fire_safe, material->heat.melting_point > 11000);
517518
TEST(magma_safe, material->heat.melting_point > 12000);
518519
TEST(deep_material, FLAG(inorganic, inorganic_flags::SPECIAL));
519-
TEST(non_economic, !inorganic || !(ui && vector_get(ui->economic_stone, index)) || itype == df::item_type::BAR);
520-
520+
TEST(non_economic, !inorganic || !(ui && vector_get(ui->economic_stone, index)));
521521
TEST(plant, plant);
522522
TEST(silk, MAT_FLAG(SILK));
523523
TEST(leather, MAT_FLAG(LEATHER));

plugins/buildingplan-planner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ bool ItemFilter::matches(df::item *item) const
224224

225225
auto imattype = item->getActualMaterial();
226226
auto imatindex = item->getActualMaterialIndex();
227-
auto item_mat = DFHack::MaterialInfo(imattype, imatindex, item->getType());
227+
auto item_mat = DFHack::MaterialInfo(imattype, imatindex);
228228

229229
return (materials.size() == 0) ? matchesMask(item_mat) : matches(item_mat);
230230
}

0 commit comments

Comments
 (0)