-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathadd-spatter.cpp
More file actions
410 lines (327 loc) · 10.8 KB
/
add-spatter.cpp
File metadata and controls
410 lines (327 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "PluginManager.h"
#include "VTableInterpose.h"
#include "modules/Items.h"
#include "modules/Units.h"
#include "df/item_constructed.h"
#include "df/job.h"
#include "df/job_item.h"
#include "df/job_item_ref.h"
#include "df/reaction.h"
#include "df/reaction_reagent_itemst.h"
#include "df/reaction_product_item_improvementst.h"
#include "df/spatter.h"
using std::vector;
using std::string;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("add-spatter");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
typedef df::reaction_product_item_improvementst improvement_product;
struct ReagentSource {
int idx;
df::reaction_reagent *reagent;
ReagentSource() : idx(-1), reagent(NULL) {}
};
struct MaterialSource : ReagentSource {
bool product;
std::string product_name;
int mat_type, mat_index;
MaterialSource() : product(false), mat_type(-1), mat_index(-1) {}
};
struct ProductInfo {
df::reaction *react;
improvement_product *product;
ReagentSource object;
MaterialSource material;
bool isValid() {
return object.reagent && (material.mat_type >= 0 || material.reagent);
}
};
struct ReactionInfo {
df::reaction *react;
std::vector<ProductInfo> products;
};
static std::map<std::string, ReactionInfo> reactions;
static std::map<df::reaction_product*, ProductInfo*> products;
static ReactionInfo *find_reaction(const std::string &name)
{
auto it = reactions.find(name);
return (it != reactions.end()) ? &it->second : NULL;
}
static bool is_add_spatter(const std::string &name)
{
return name.size() > 12 && memcmp(name.data(), "SPATTER_ADD_", 12) == 0;
}
static void find_material(int *type, int *index, df::item *input, MaterialSource &mat)
{
if (input && mat.reagent)
{
MaterialInfo info(input);
if (mat.product)
{
if (!info.findProduct(info, mat.product_name))
{
color_ostream_proxy out(Core::getInstance().getConsole());
out.printerr("Cannot find product '{}'\n", mat.product_name);
}
}
*type = info.type;
*index = info.index;
}
else
{
*type = mat.mat_type;
*index = mat.mat_index;
}
}
static int has_contaminant(df::item_actual *item, int type, int index)
{
auto cont = item->contaminants;
if (!cont)
return 0;
int size = 0;
for (size_t i = 0; i < cont->size(); i++)
{
auto cur = (*cont)[i];
if (cur->base.mat_type == type && cur->base.mat_index == index)
size += cur->base.size;
}
return size;
}
/*
* Hooks
*/
typedef std::map<int, std::vector<df::item*> > item_table;
static void index_items(item_table &table, df::job *job, ReactionInfo *info)
{
for (int i = job->items.size()-1; i >= 0; i--)
{
auto iref = job->items[i];
if (iref->job_item_idx < 0) continue;
auto iitem = job->job_items.elements[iref->job_item_idx];
if (iitem->contains.empty())
{
table[iitem->reagent_index].push_back(iref->item);
}
else
{
std::vector<df::item*> contents;
Items::getContainedItems(iref->item, &contents);
for (int j = contents.size()-1; j >= 0; j--)
{
for (int k = iitem->contains.size()-1; k >= 0; k--)
{
int ridx = iitem->contains[k];
auto reag = info->react->reagents[ridx];
if (reag->matchesChild(contents[j], info->react, iitem->reaction_id))
table[ridx].push_back(contents[j]);
}
}
}
}
}
df::item* find_item(ReagentSource &info, item_table &table)
{
if (!info.reagent)
return NULL;
if (table[info.idx].empty())
return NULL;
return table[info.idx].back();
}
struct item_hook : df::item_constructed {
typedef df::item_constructed interpose_base;
DEFINE_VMETHOD_INTERPOSE(bool, isImprovable, (df::job *job, int16_t mat_type, int32_t mat_index))
{
ReactionInfo *info;
if (job && job->job_type == job_type::CustomReaction &&
(info = find_reaction(job->reaction_name)) != NULL)
{
if (!contaminants || contaminants->empty())
return true;
item_table table;
index_items(table, job, info);
for (size_t i = 0; i < info->products.size(); i++)
{
auto &product = info->products[i];
int mattype, matindex;
auto material = find_item(info->products[i].material, table);
find_material(&mattype, &matindex, material, product.material);
if (mattype < 0 || has_contaminant(this, mattype, matindex) >= 50)
return false;
}
return true;
}
return INTERPOSE_NEXT(isImprovable)(job, mat_type, mat_index);
}
};
IMPLEMENT_VMETHOD_INTERPOSE(item_hook, isImprovable);
df::item* find_item(
ReagentSource &info,
std::vector<df::reaction_reagent*> *in_reag,
std::vector<df::item*> *in_items
) {
if (!info.reagent)
return NULL;
for (int i = in_items->size(); i >= 0; i--)
if ((*in_reag)[i] == info.reagent)
return (*in_items)[i];
return NULL;
}
struct product_hook : improvement_product {
typedef improvement_product interpose_base;
DEFINE_VMETHOD_INTERPOSE(
void, produce,
(df::unit *unit,
std::vector<df::reaction_product*> *out_products,
std::vector<df::item*> *out_items,
std::vector<df::reaction_reagent*> *in_reag,
std::vector<df::item*> *in_items,
int32_t quantity, df::job_skill skill,
int32_t quality, df::historical_entity *entity, df::world_site *site, std::vector<df::item *> *improv_items)
) {
if (auto product = products[this])
{
auto object = find_item(product->object, in_reag, in_items);
auto material = find_item(product->material, in_reag, in_items);
if (object && (material || !product->material.reagent))
{
using namespace df::enums::improvement_type;
int mattype, matindex;
find_material(&mattype, &matindex, material, product->material);
df::matter_state state = matter_state::Liquid;
switch (improvement_type)
{
case COVERED:
if (flags.is_set(reaction_product_improvement_flags::GLAZED))
state = matter_state::Solid;
break;
case BANDS:
state = matter_state::Paste;
break;
case SPIKES:
state = matter_state::Powder;
break;
default:
break;
}
int rating = unit ? Units::getEffectiveSkill(unit, skill) : 0;
int size = int(probability*(1.0f + 0.06f*rating)); // +90% at legendary
object->addContaminant(
mattype, matindex, state,
object->getTemperature(),
size, -1,
0x8000 // not washed by water, and 'clean items' safe.
);
}
return;
}
INTERPOSE_NEXT(produce)(unit, out_products, out_items, in_reag, in_items, quantity, skill, quality, entity, site, improv_items);
}
};
IMPLEMENT_VMETHOD_INTERPOSE(product_hook, produce);
/*
* Scan raws for matching reactions.
*/
static void find_reagent(
color_ostream &out, ReagentSource &info, df::reaction *react, std::string name
) {
for (size_t i = 0; i < react->reagents.size(); i++)
{
if (react->reagents[i]->code != name)
continue;
info.idx = i;
info.reagent = react->reagents[i];
return;
}
out.printerr("Invalid reagent name '{}' in '{}'\n", name, react->code);
}
static void parse_product(
color_ostream &out, ProductInfo &info, df::reaction *react, improvement_product *prod
) {
using namespace df::enums::reaction_product_improvement_flags;
info.react = react;
info.product = prod;
find_reagent(out, info.object, react, prod->target_reagent);
auto ritem = strict_virtual_cast<df::reaction_reagent_itemst>(info.object.reagent);
if (ritem)
ritem->flags1.bits.improvable = true;
info.material.mat_type = prod->mat_type;
info.material.mat_index = prod->mat_index;
if (prod->flags.is_set(GET_MATERIAL_PRODUCT))
{
find_reagent(out, info.material, react, prod->get_material.reagent_code);
info.material.product = true;
info.material.product_name = prod->get_material.product_code;
}
else if (prod->flags.is_set(GET_MATERIAL_SAME))
{
find_reagent(out, info.material, react, prod->get_material.reagent_code);
}
}
static bool find_reactions(color_ostream &out)
{
reactions.clear();
products.clear();
auto &rlist = df::reaction::get_vector();
for (size_t i = 0; i < rlist.size(); i++)
{
if (!is_add_spatter(rlist[i]->code))
continue;
reactions[rlist[i]->code].react = rlist[i];
}
for (auto it = reactions.begin(); it != reactions.end(); ++it)
{
auto &prod = it->second.react->products;
auto &out_prod = it->second.products;
for (size_t i = 0; i < prod.size(); i++)
{
auto itprod = strict_virtual_cast<improvement_product>(prod[i]);
if (!itprod) continue;
out_prod.push_back(ProductInfo());
parse_product(out, out_prod.back(), it->second.react, itprod);
}
for (size_t i = 0; i < out_prod.size(); i++)
{
if (out_prod[i].isValid())
products[out_prod[i].product] = &out_prod[i];
}
}
return !products.empty();
}
static void enable_hooks(bool enable)
{
is_enabled = enable;
INTERPOSE_HOOK(item_hook, isImprovable).apply(enable);
INTERPOSE_HOOK(product_hook, produce).apply(enable);
}
DFhackCExport command_result plugin_load_world_data (color_ostream &out) {
if (find_reactions(out)) {
out.print("Detected spatter add reactions - enabling plugin.\n");
enable_hooks(true);
}
else
enable_hooks(false);
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
switch (event) {
case SC_WORLD_UNLOADED:
enable_hooks(false);
reactions.clear();
products.clear();
break;
default:
break;
}
return CR_OK;
}
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
enable_hooks(false);
return CR_OK;
}