Skip to content
Navigation Menu
{{ message }}
forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateitem.cpp
More file actions
498 lines (460 loc) · 16.1 KB
/
Copy pathcreateitem.cpp
File metadata and controls
498 lines (460 loc) · 16.1 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Create arbitrary items
#include "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "MiscUtils.h"
#include "DataDefs.h"
#include "modules/Maps.h"
#include "modules/MapCache.h"
#include "modules/Gui.h"
#include "modules/Items.h"
#include "modules/Materials.h"
#include "modules/Units.h"
#include "modules/World.h"
#include "df/building.h"
#include "df/game_type.h"
#include "df/world.h"
#include "df/unit.h"
#include "df/item.h"
#include "df/creature_raw.h"
#include "df/caste_raw.h"
#include "df/tool_uses.h"
#include "df/plant_growth.h"
#include "df/plant_growth_print.h"
#include "df/plant_raw.h"
using std::string;
using std::vector;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("createitem");
REQUIRE_GLOBAL(cursor);
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(gametype);
REQUIRE_GLOBAL(cur_year_tick);
int dest_container = -1, dest_building = -1;
command_result df_createitem (color_ostream &out, vector <string> & parameters);
DFhackCExport command_result plugin_init (color_ostream &out, std::vector<PluginCommand> &commands)
{
commands.push_back(
PluginCommand("createitem",
"Create arbitrary items.",
df_createitem));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
bool makeItem (df::unit *unit, df::item_type type, int16_t subtype, int16_t mat_type, int32_t mat_index, int32_t growth_print = -1, bool move_to_cursor = false, bool second_item = false)
{
// Special logic for making Gloves and Shoes in pairs
bool is_gloves = (type == item_type::GLOVES);
bool is_shoes = (type == item_type::SHOES);
df::item *container = NULL;
df::building *building = NULL;
if (dest_container != -1)
container = df::item::find(dest_container);
if (dest_building != -1)
building = df::building::find(dest_building);
bool on_floor = (container == NULL) && (building == NULL) && !move_to_cursor;
vector<df::item *> out_items;
if (!Items::createItem(out_items, unit, type, subtype, mat_type, mat_index, growth_print, !on_floor))
return false;
MapExtras::MapCache mc;
for (size_t i = 0; i < out_items.size(); i++)
{
if (container)
{
out_items[i]->flags.bits.removed = 1;
if (!Items::moveToContainer(mc, out_items[i], container))
out_items[i]->moveToGround(container->pos.x, container->pos.y, container->pos.z);
}
else if (building)
{
out_items[i]->flags.bits.removed = 1;
if (!Items::moveToBuilding(mc, out_items[i], (df::building_actual *)building, df::building_item_role_type::TEMP))
out_items[i]->moveToGround(building->centerx, building->centery, building->z);
}
else if (move_to_cursor)
out_items[i]->moveToGround(cursor->x, cursor->y, cursor->z);
// else createItem() already put it on the floor at the unit's feet, so we're good
// Special logic for creating proper gloves in pairs
if (is_gloves)
{
// If the gloves have non-zero handedness, then disable special pair-making logic
// ("reaction-gloves" tweak is active, or Toady fixed glove-making reactions)
// Otherwise, set them to be either Left or Right-handed
if (out_items[i]->getGloveHandedness() > 0)
is_gloves = false;
else
out_items[i]->setGloveHandedness(second_item ? 2 : 1);
}
}
// If we asked to make a Shoe and we got more than one, then disable special pair-making logic
if (is_shoes && out_items.size() > 1)
is_shoes = false;
// If we asked for gloves/shoes and only got one (and we're making the first one), make another
if ((is_gloves || is_shoes) && !second_item)
return makeItem(unit, type, subtype, mat_type, mat_index, growth_print, move_to_cursor, true);
return true;
}
command_result df_createitem (color_ostream &out, vector <string> & parameters)
{
string item_str, material_str;
df::item_type item_type = item_type::NONE;
int16_t item_subtype = -1;
int16_t mat_type = -1;
int32_t mat_index = -1;
int32_t growth_print = -1;
int count = 1;
bool move_to_cursor = false;
if (parameters.size() == 1)
{
if (parameters[0] == "inspect")
{
CoreSuspender suspend;
df::item *item = Gui::getSelectedItem(out);
if (!item)
{
return CR_FAILURE;
}
ItemTypeInfo iinfo(item->getType(), item->getSubtype());
MaterialInfo minfo(item->getMaterial(), item->getMaterialIndex());
out.print("%s %s\n", iinfo.getToken().c_str(), minfo.getToken().c_str());
return CR_OK;
}
else if (parameters[0] == "floor")
{
dest_container = -1;
dest_building = -1;
out.print("Items created will be placed on the floor.\n");
return CR_OK;
}
else if (parameters[0] == "item")
{
dest_building = -1;
df::item *item = Gui::getSelectedItem(out);
if (!item)
{
out.printerr("You must select a container!\n");
return CR_FAILURE;
}
switch (item->getType())
{
case item_type::FLASK:
case item_type::BARREL:
case item_type::BUCKET:
case item_type::ANIMALTRAP:
case item_type::BOX:
case item_type::BAG:
case item_type::BIN:
case item_type::BACKPACK:
case item_type::QUIVER:
break;
case item_type::TOOL:
if (item->hasToolUse(tool_uses::LIQUID_CONTAINER))
break;
if (item->hasToolUse(tool_uses::FOOD_STORAGE))
break;
if (item->hasToolUse(tool_uses::SMALL_OBJECT_STORAGE))
break;
if (item->hasToolUse(tool_uses::TRACK_CART))
break;
default:
out.printerr("The selected item cannot be used for item storage!\n");
return CR_FAILURE;
}
dest_container = item->id;
string name;
item->getItemDescription(&name, 0);
out.print("Items created will be placed inside %s.\n", name.c_str());
return CR_OK;
}
else if (parameters[0] == "building")
{
dest_container = -1;
df::building *building = Gui::getSelectedBuilding(out);
if (!building)
{
out.printerr("You must select a building!\n");
return CR_FAILURE;
}
switch (building->getType())
{
case building_type::Coffin:
case building_type::Furnace:
case building_type::TradeDepot:
case building_type::Shop:
case building_type::Box:
case building_type::Weaponrack:
case building_type::Armorstand:
case building_type::Workshop:
case building_type::Cabinet:
case building_type::SiegeEngine:
case building_type::Trap:
case building_type::AnimalTrap:
case building_type::Cage:
case building_type::Wagon:
case building_type::NestBox:
case building_type::Hive:
break;
default:
out.printerr("The selected building cannot be used for item storage!\n");
return CR_FAILURE;
}
if (building->getBuildStage() != building->getMaxBuildStage())
{
out.printerr("The selected building has not yet been fully constructed!\n");
return CR_FAILURE;
}
dest_building = building->id;
string name;
building->getName(&name);
out.print("Items created will be placed inside %s.\n", name.c_str());
return CR_OK;
}
else
return CR_WRONG_USAGE;
}
if ((parameters.size() < 2) || (parameters.size() > 3))
return CR_WRONG_USAGE;
item_str = parameters[0];
material_str = parameters[1];
if (parameters.size() == 3)
{
std::stringstream ss(parameters[2]);
ss >> count;
if (count < 1)
{
out.printerr("You cannot produce less than one item!\n");
return CR_FAILURE;
}
}
ItemTypeInfo item;
MaterialInfo material;
vector<string> tokens;
if (item.find(item_str))
{
item_type = item.type;
item_subtype = item.subtype;
}
if (item_type == item_type::NONE)
{
out.printerr("You must specify a valid item type to create!\n");
return CR_FAILURE;
}
switch (item.type)
{
case item_type::INSTRUMENT:
case item_type::TOY:
case item_type::WEAPON:
case item_type::ARMOR:
case item_type::SHOES:
case item_type::SHIELD:
case item_type::HELM:
case item_type::GLOVES:
case item_type::AMMO:
case item_type::PANTS:
case item_type::SIEGEAMMO:
case item_type::TRAPCOMP:
case item_type::TOOL:
if (item_subtype == -1)
{
out.printerr("You must specify a subtype!\n");
return CR_FAILURE;
}
default:
if (!material.find(material_str))
{
out.printerr("Unrecognized material!\n");
return CR_FAILURE;
}
mat_type = material.type;
mat_index = material.index;
break;
case item_type::REMAINS:
case item_type::FISH:
case item_type::FISH_RAW:
case item_type::VERMIN:
case item_type::PET:
case item_type::EGG:
split_string(&tokens, material_str, ":");
if (tokens.size() == 1)
{
// default to empty caste to display a list of valid castes later
tokens.push_back("");
}
else if (tokens.size() != 2)
{
out.printerr("You must specify a creature ID and caste for this item type!\n");
return CR_FAILURE;
}
for (size_t i = 0; i < world->raws.creatures.all.size(); i++)
{
string castes = "";
df::creature_raw *creature = world->raws.creatures.all[i];
if (creature->creature_id == tokens[0])
{
for (size_t j = 0; j < creature->caste.size(); j++)
{
df::caste_raw *caste = creature->caste[j];
castes += " " + caste->caste_id;
if (caste->caste_id == tokens[1])
{
mat_type = i;
mat_index = j;
break;
}
}
if (mat_type == -1)
{
if (tokens[1].empty())
{
out.printerr("You must also specify a caste.\n");
}
else
{
out.printerr("The creature you specified has no such caste!\n");
}
out.printerr("Valid castes:%s\n", castes.c_str());
return CR_FAILURE;
}
}
}
if (mat_type == -1)
{
out.printerr("Unrecognized creature ID!\n");
return CR_FAILURE;
}
break;
case item_type::PLANT_GROWTH:
split_string(&tokens, material_str, ":");
if (tokens.size() == 1)
{
// default to empty to display a list of valid growths later
tokens.push_back("");
}
else if (tokens.size() == 3 && tokens[0] == "PLANT")
{
tokens.erase(tokens.begin());
}
else if (tokens.size() != 2)
{
out.printerr("You must specify a plant and growth ID for this item type!\n");
return CR_FAILURE;
}
for (size_t i = 0; i < world->raws.plants.all.size(); i++)
{
string growths = "";
df::plant_raw *plant = world->raws.plants.all[i];
if (plant->id != tokens[0])
continue;
for (size_t j = 0; j < plant->growths.size(); j++)
{
df::plant_growth *growth = plant->growths[j];
growths += " " + growth->id;
if (growth->id != tokens[1])
continue;
// Plant growths specify the actual item type/subtype
// so that certain growths can drop as SEEDS items
item_type = growth->item_type;
item_subtype = growth->item_subtype;
mat_type = growth->mat_type;
mat_index = growth->mat_index;
// Try and find a growth print matching the current time
// (in practice, only tree leaves use this for autumn color changes)
for (size_t k = 0; k < growth->prints.size(); k++)
{
df::plant_growth_print *print = growth->prints[k];
if (print->timing_start <= *cur_year_tick && *cur_year_tick <= print->timing_end)
{
growth_print = k;
break;
}
}
// If we didn't find one, then pick the first one (if it exists)
if (growth_print == -1 && growth->prints.size() > 0)
growth_print = 0;
break;
}
if (mat_type == -1)
{
if (tokens[1].empty())
out.printerr("You must also specify a growth ID.\n");
else
out.printerr("The plant you specified has no such growth!\n");
out.printerr("Valid growths:%s\n", growths.c_str());
return CR_FAILURE;
}
}
if (mat_type == -1)
{
out.printerr("Unrecognized plant ID!\n");
return CR_FAILURE;
}
break;
case item_type::CORPSE:
case item_type::CORPSEPIECE:
case item_type::FOOD:
out.printerr("Cannot create that type of item!\n");
return CR_FAILURE;
break;
}
CoreSuspender suspend;
df::unit *unit = Gui::getSelectedUnit(out, true);
if (!unit)
{
if (*gametype == game_type::ADVENTURE_ARENA || World::isAdventureMode())
{
// Use the adventurer unit
unit = World::getAdventurer();
}
else if (cursor->x >= 0)
{
// Use the first possible citizen if possible, otherwise the first unit
for (auto u : Units::citizensRange(world->units.active)) {
unit = u;
break;
}
if (!unit && world->units.active.size())
unit = world->units.active[0];
move_to_cursor = true;
}
if (!unit)
{
out.printerr("No unit selected!\n");
return CR_FAILURE;
}
}
if (!Maps::IsValid())
{
out.printerr("Map is not available.\n");
return CR_FAILURE;
}
df::map_block *block = Maps::getTileBlock(unit->pos.x, unit->pos.y, unit->pos.z);
if (block == NULL)
{
out.printerr("Unit does not reside in a valid map block, somehow?\n");
return CR_FAILURE;
}
if ((dest_container != -1) && !df::item::find(dest_container))
{
dest_container = -1;
out.printerr("Previously selected container no longer exists - item will be placed on the floor.\n");
}
if ((dest_building != -1) && !df::building::find(dest_building))
{
dest_building = -1;
out.printerr("Previously selected building no longer exists - item will be placed on the floor.\n");
}
for (int i = 0; i < count; i++)
{
if (!makeItem(unit, item_type, item_subtype, mat_type, mat_index, growth_print, move_to_cursor, false))
{
out.printerr("Failed to create item!\n");
return CR_FAILURE;
}
}
return CR_OK;
}
You can’t perform that action at this time.
