Skip to content

Commit 3561c9f

Browse files
authored
Merge pull request #5602 from NicksWorld/feat/stockpiles_dye
Add stockpiles plugin support for dyes
2 parents df1bdc3 + 2a332eb commit 3561c9f

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Template for new versions:
6161
## Fixes
6262

6363
## Misc Improvements
64+
- `stockpiles`: add support for managing the dyed, undyed, and color filter settings.
6465

6566
## Documentation
6667

plugins/stockpiles/StockpileSerializer.cpp

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "df/building_stockpilest.h"
1313
#include "df/creature_raw.h"
1414
#include "df/caste_raw.h"
15+
#include "df/descriptor_color.h"
1516
#include "df/inorganic_raw.h"
1617
#include "df/item_quality.h"
1718
#include <df/itemdef_ammost.h>
@@ -497,6 +498,57 @@ static void unserialize_list_organic_mat(color_ostream& out, const char* subcat,
497498
}
498499
}
499500

501+
static bool serialize_list_color(color_ostream& out, FuncWriteExport add_value, const vector<char>* colors) {
502+
bool all = world->raws.descriptors.colors.size() == colors->size();
503+
if (!colors) {
504+
DEBUG(log, out).print("serialize_list_color: list null\n");
505+
return all;
506+
}
507+
for (size_t i = 0; i < colors->size(); ++i) {
508+
if (!colors->at(i)) {
509+
all = false;
510+
continue;
511+
}
512+
add_value(world->raws.descriptors.colors[i]->id);
513+
}
514+
return all;
515+
}
516+
517+
// Find the index of a named color, returning SIZE_MAX on unknown.
518+
static size_t find_color_by_token(const string& token) {
519+
auto& colors = world->raws.descriptors.colors;
520+
size_t num_colors = colors.size();
521+
for (size_t idx = 0; idx < num_colors; ++ idx) {
522+
if (colors[idx]->id == token) {
523+
return idx;
524+
}
525+
}
526+
return SIZE_MAX;
527+
}
528+
529+
static void unserialize_list_color(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
530+
FuncReadImport read_value, int32_t list_size, vector<char>& pile_list) {
531+
size_t num_elems = world->raws.descriptors.colors.size();
532+
pile_list.resize(num_elems, '\0');
533+
if (all) {
534+
for (size_t idx = 0; idx < num_elems; ++idx) {
535+
set_filter_elem(out, subcat, filters, val, world->raws.descriptors.colors[idx]->id, idx, pile_list[idx]);
536+
}
537+
return;
538+
}
539+
540+
for (int32_t idx = 0; idx < list_size; ++idx) {
541+
const string& value = read_value(idx);
542+
543+
size_t color = find_color_by_token(value);
544+
if (color == SIZE_MAX) {
545+
WARN(log, out).print("unknown color %s", value.c_str());
546+
continue;
547+
}
548+
set_filter_elem(out, subcat, filters, val, value, color, pile_list[color]);
549+
}
550+
}
551+
500552
static bool serialize_list_item_type(color_ostream& out, FuncItemAllowed is_allowed,
501553
FuncWriteExport add_value, const vector<char>& list) {
502554
using df::enums::item_type::item_type;
@@ -1059,10 +1111,12 @@ static bool armor_mat_is_allowed(const MaterialInfo& mi) {
10591111
bool StockpileSettingsSerializer::write_armor(color_ostream& out, StockpileSettings::ArmorSet* armor) {
10601112

10611113
auto & parmor = mSettings->armor;
1062-
bool all = parmor.unusable && parmor.usable;
1114+
bool all = parmor.unusable && parmor.usable && parmor.dyed && parmor.undyed;
10631115

10641116
armor->set_unusable(parmor.unusable);
10651117
armor->set_usable(parmor.usable);
1118+
armor->set_dyed(parmor.dyed);
1119+
armor->set_undyed(parmor.undyed);
10661120

10671121
// armor type
10681122
all = serialize_list_itemdef(out,
@@ -1125,6 +1179,9 @@ bool StockpileSettingsSerializer::write_armor(color_ostream& out, StockpileSetti
11251179
all = serialize_list_quality(out, [&](const string& token) { armor->add_quality_total(token); },
11261180
parmor.quality_total) && all;
11271181

1182+
all = serialize_list_color(out, [&](const string& token) { armor->add_color(token); },
1183+
&parmor.color) && all;
1184+
11281185
return all;
11291186
}
11301187

@@ -1148,6 +1205,9 @@ void StockpileSettingsSerializer::read_armor(color_ostream& out, DeserializeMode
11481205
parmor.mats.clear();
11491206
quality_clear(parmor.quality_core);
11501207
quality_clear(parmor.quality_total);
1208+
parmor.dyed = false;
1209+
parmor.undyed = false;
1210+
parmor.color.clear();
11511211
},
11521212
[&](bool all, char val) {
11531213
auto & barmor = mBuffer.armor();
@@ -1195,6 +1255,13 @@ void StockpileSettingsSerializer::read_armor(color_ostream& out, DeserializeMode
11951255
unserialize_list_quality(out, "total", all, val, filters,
11961256
[&](const size_t& idx) -> const string& { return barmor.quality_total(idx); },
11971257
barmor.quality_total_size(), parmor.quality_total);
1258+
1259+
unserialize_list_color(out, "color", all, val, filters,
1260+
[&](const size_t& idx) -> const string& { return barmor.color(idx); },
1261+
barmor.color_size(), parmor.color);
1262+
1263+
set_flag(out, "dyed", filters, all, val, barmor.dyed(), parmor.dyed);
1264+
set_flag(out, "undyed", filters, all, val, barmor.undyed(), parmor.undyed);
11981265
});
11991266
}
12001267

@@ -1266,7 +1333,9 @@ void StockpileSettingsSerializer::read_bars_blocks(color_ostream& out, Deseriali
12661333
}
12671334

12681335
bool StockpileSettingsSerializer::write_cloth(color_ostream& out, StockpileSettings::ClothSet* cloth) {
1269-
bool all = true;
1336+
bool all = mSettings->cloth.dyed && mSettings->cloth.undyed;
1337+
cloth->set_dyed(mSettings->cloth.dyed);
1338+
cloth->set_undyed(mSettings->cloth.undyed);
12701339

12711340
all = serialize_list_organic_mat(out,
12721341
[&](const string& token) { cloth->add_thread_silk(token); },
@@ -1300,6 +1369,10 @@ bool StockpileSettingsSerializer::write_cloth(color_ostream& out, StockpileSetti
13001369
[&](const string& token) { cloth->add_cloth_metal(token); },
13011370
&mSettings->cloth.cloth_metal, organic_mat_category::MetalThread) && all;
13021371

1372+
all = serialize_list_color(out,
1373+
[&](const string& token) { cloth->add_color(token); },
1374+
&mSettings->cloth.color) && all;
1375+
13031376
return all;
13041377
}
13051378

@@ -1319,6 +1392,9 @@ void StockpileSettingsSerializer::read_cloth(color_ostream& out, DeserializeMode
13191392
pcloth.cloth_plant.clear();
13201393
pcloth.cloth_yarn.clear();
13211394
pcloth.cloth_metal.clear();
1395+
pcloth.dyed = false;
1396+
pcloth.undyed = false;
1397+
pcloth.color.clear();
13221398
},
13231399
[&](bool all, char val) {
13241400
auto & bcloth = mBuffer.cloth();
@@ -1354,6 +1430,13 @@ void StockpileSettingsSerializer::read_cloth(color_ostream& out, DeserializeMode
13541430
unserialize_list_organic_mat(out, "cloth/metal", all, val, filters,
13551431
[&](size_t idx) -> string { return bcloth.cloth_metal(idx); },
13561432
bcloth.cloth_metal_size(), pcloth.cloth_metal, organic_mat_category::MetalThread);
1433+
1434+
unserialize_list_color(out, "cloth/color", all, val, filters,
1435+
[&](size_t idx) -> string { return bcloth.color(idx); },
1436+
bcloth.color_size(), pcloth.color);
1437+
1438+
set_flag(out, "dyed", filters, all, val, bcloth.dyed(), pcloth.dyed);
1439+
set_flag(out, "undyed", filters, all, val, bcloth.undyed(), pcloth.undyed);
13571440
});
13581441
}
13591442

@@ -1425,10 +1508,14 @@ static bool finished_goods_mat_is_allowed(const MaterialInfo& mi) {
14251508
}
14261509

14271510
bool StockpileSettingsSerializer::write_finished_goods(color_ostream& out, StockpileSettings::FinishedGoodsSet* finished_goods) {
1428-
bool all = serialize_list_item_type(out,
1511+
bool all = mSettings->finished_goods.dyed && mSettings->finished_goods.undyed;
1512+
finished_goods->set_dyed(mSettings->finished_goods.dyed);
1513+
finished_goods->set_undyed(mSettings->finished_goods.undyed);
1514+
1515+
all = serialize_list_item_type(out,
14291516
finished_goods_type_is_allowed,
14301517
[&](const string& token) { finished_goods->add_type(token); },
1431-
mSettings->finished_goods.type);
1518+
mSettings->finished_goods.type) && all;
14321519

14331520
all = serialize_list_material(out,
14341521
finished_goods_mat_is_allowed,
@@ -1447,6 +1534,10 @@ bool StockpileSettingsSerializer::write_finished_goods(color_ostream& out, Stock
14471534
[&](const string& token) { finished_goods->add_quality_total(token); },
14481535
mSettings->finished_goods.quality_total) && all;
14491536

1537+
all = serialize_list_color(out,
1538+
[&](const string& token) { finished_goods->add_color(token); },
1539+
&mSettings->finished_goods.color) && all;
1540+
14501541
return all;
14511542
}
14521543

@@ -1463,6 +1554,9 @@ void StockpileSettingsSerializer::read_finished_goods(color_ostream& out, Deseri
14631554
pfinished_goods.mats.clear();
14641555
quality_clear(pfinished_goods.quality_core);
14651556
quality_clear(pfinished_goods.quality_total);
1557+
pfinished_goods.dyed = false;
1558+
pfinished_goods.undyed = false;
1559+
pfinished_goods.color.clear();
14661560
},
14671561
[&](bool all, char val) {
14681562
auto & bfinished_goods = mBuffer.finished_goods();
@@ -1486,6 +1580,13 @@ void StockpileSettingsSerializer::read_finished_goods(color_ostream& out, Deseri
14861580
unserialize_list_quality(out, "total", all, val, filters,
14871581
[&](const size_t& idx) -> const string& { return bfinished_goods.quality_total(idx); },
14881582
bfinished_goods.quality_total_size(), pfinished_goods.quality_total);
1583+
1584+
unserialize_list_color(out, "color", all, val, filters,
1585+
[&](const size_t& idx) -> const string& { return bfinished_goods.color(idx); },
1586+
bfinished_goods.color_size(), pfinished_goods.color);
1587+
1588+
set_flag(out, "dyed", filters, all, val, bfinished_goods.dyed(), pfinished_goods.dyed);
1589+
set_flag(out, "undyed", filters, all, val, bfinished_goods.undyed(), pfinished_goods.undyed);
14891590
});
14901591
}
14911592

@@ -1934,9 +2035,17 @@ void StockpileSettingsSerializer::read_gems(color_ostream& out, DeserializeMode
19342035
}
19352036

19362037
bool StockpileSettingsSerializer::write_leather(color_ostream& out, StockpileSettings::LeatherSet* leather) {
1937-
return serialize_list_organic_mat(out,
2038+
bool all = mSettings->leather.dyed && mSettings->leather.undyed;
2039+
leather->set_dyed(mSettings->leather.dyed);
2040+
leather->set_undyed(mSettings->leather.undyed);
2041+
2042+
all = serialize_list_organic_mat(out,
19382043
[&](const string& id) { leather->add_mats(id); },
1939-
&mSettings->leather.mats, organic_mat_category::Leather);
2044+
&mSettings->leather.mats, organic_mat_category::Leather) && all;
2045+
all = serialize_list_color(out,
2046+
[&](const string& id) { leather->add_color(id); },
2047+
&mSettings->leather.color) && all;
2048+
return all;
19402049
}
19412050

19422051
void StockpileSettingsSerializer::read_leather(color_ostream& out, DeserializeMode mode, const vector<string>& filters) {
@@ -1948,13 +2057,22 @@ void StockpileSettingsSerializer::read_leather(color_ostream& out, DeserializeMo
19482057
mSettings->flags.mask_leather,
19492058
[&]() {
19502059
pleather.mats.clear();
2060+
pleather.color.clear();
2061+
pleather.dyed = false;
2062+
pleather.undyed = false;
19512063
},
19522064
[&](bool all, char val) {
19532065
auto & bleather = mBuffer.leather();
19542066

19552067
unserialize_list_organic_mat(out, "", all, val, filters,
19562068
[&](size_t idx) -> string { return bleather.mats(idx); },
19572069
bleather.mats_size(), pleather.mats, organic_mat_category::Leather);
2070+
unserialize_list_color(out, "", all, val, filters,
2071+
[&](size_t idx) -> string { return bleather.color(idx); },
2072+
bleather.color_size(), pleather.color);
2073+
2074+
set_flag(out, "dyed", filters, all, val, bleather.dyed(), pleather.dyed);
2075+
set_flag(out, "undyed", filters, all, val, bleather.undyed(), pleather.undyed);
19582076
});
19592077
}
19602078

plugins/stockpiles/proto/stockpiles.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,16 @@ message StockpileSettings {
100100
repeated string mats = 3;
101101
repeated string quality_core = 4;
102102
repeated string quality_total = 5;
103+
optional bool dyed = 7;
104+
optional bool undyed = 8;
105+
repeated string color = 9;
103106
}
104107
message LeatherSet {
105108
optional bool all = 2;
106109
repeated string mats = 1;
110+
optional bool dyed = 3;
111+
optional bool undyed = 4;
112+
repeated string color = 5;
107113
}
108114
message ClothSet {
109115
optional bool all = 9;
@@ -115,6 +121,9 @@ message StockpileSettings {
115121
repeated string cloth_plant = 6;
116122
repeated string cloth_yarn = 7;
117123
repeated string cloth_metal = 8;
124+
optional bool dyed = 10;
125+
optional bool undyed = 11;
126+
repeated string color = 12;
118127
}
119128
message WoodSet {
120129
optional bool all = 2;
@@ -145,6 +154,9 @@ message StockpileSettings {
145154
repeated string quality_total = 10;
146155
optional bool usable = 11;
147156
optional bool unusable = 12;
157+
optional bool dyed = 14;
158+
optional bool undyed = 15;
159+
repeated string color = 16;
148160
}
149161
message CorpsesSet {
150162
optional bool all = 1;

0 commit comments

Comments
 (0)