2013-02-27 10:09:06 -07:00
|
|
|
// Create arbitrary items
|
|
|
|
|
|
|
|
#include "Core.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "MiscUtils.h"
|
|
|
|
|
|
|
|
#include "modules/Maps.h"
|
2013-12-27 11:53:33 -07:00
|
|
|
#include "modules/MapCache.h"
|
2013-02-27 10:09:06 -07:00
|
|
|
#include "modules/Gui.h"
|
|
|
|
#include "modules/Items.h"
|
|
|
|
#include "modules/Materials.h"
|
2015-03-09 18:17:09 -06:00
|
|
|
#include "modules/World.h"
|
2013-02-27 10:09:06 -07:00
|
|
|
|
|
|
|
#include "DataDefs.h"
|
2013-02-28 09:00:00 -07:00
|
|
|
#include "df/game_type.h"
|
2013-02-27 10:09:06 -07:00
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/ui.h"
|
|
|
|
#include "df/unit.h"
|
|
|
|
#include "df/historical_entity.h"
|
|
|
|
#include "df/world_site.h"
|
|
|
|
#include "df/item.h"
|
|
|
|
#include "df/creature_raw.h"
|
|
|
|
#include "df/caste_raw.h"
|
|
|
|
#include "df/reaction_reagent.h"
|
|
|
|
#include "df/reaction_product_itemst.h"
|
2014-01-03 15:42:24 -07:00
|
|
|
#include "df/tool_uses.h"
|
2013-02-27 10:09:06 -07:00
|
|
|
|
2014-05-22 07:18:01 -06:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2013-02-27 10:09:06 -07:00
|
|
|
using namespace DFHack;
|
2014-05-22 07:18:01 -06:00
|
|
|
using namespace df::enums;
|
2013-02-27 10:09:06 -07:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("createitem");
|
2016-04-05 09:49:38 -06:00
|
|
|
REQUIRE_GLOBAL(cursor);
|
2014-12-02 19:44:20 -07:00
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
REQUIRE_GLOBAL(ui);
|
|
|
|
REQUIRE_GLOBAL(gametype);
|
2013-02-27 10:09:06 -07:00
|
|
|
|
2013-12-27 11:53:33 -07:00
|
|
|
int dest_container = -1, dest_building = -1;
|
|
|
|
|
2013-02-27 10:09:06 -07:00
|
|
|
command_result df_createitem (color_ostream &out, vector <string> & parameters);
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init (color_ostream &out, std::vector<PluginCommand> &commands)
|
|
|
|
{
|
2013-12-27 11:53:33 -07:00
|
|
|
commands.push_back(PluginCommand("createitem", "Create arbitrary items.", df_createitem, false,
|
2013-10-30 14:58:14 -06:00
|
|
|
"Syntax: createitem <item> <material> [count]\n"
|
|
|
|
" <item> - Item token for what you wish to create, as specified in custom\n"
|
|
|
|
" reactions. If the item has no subtype, omit the :NONE.\n"
|
|
|
|
" <material> - The material you want the item to be made of, as specified\n"
|
|
|
|
" in custom reactions. For REMAINS, FISH, FISH_RAW, VERMIN,\n"
|
|
|
|
" PET, and EGG, replace this with a creature ID and caste.\n"
|
2013-12-27 11:53:33 -07:00
|
|
|
" [count] - How many of the item you wish to create.\n"
|
|
|
|
"\n"
|
2013-12-27 12:01:34 -07:00
|
|
|
"To use this command, you must select which unit will create the items.\n"
|
|
|
|
"By default, items created will be placed at that unit's feet.\n"
|
|
|
|
"To change this, type 'createitem <destination>'.\n"
|
|
|
|
"Valid destinations:\n"
|
|
|
|
"* floor - Place items on floor beneath maker's feet.\n"
|
|
|
|
"* item - Place items inside selected container.\n"
|
|
|
|
"* building - Place items inside selected building.\n"
|
2013-12-27 11:53:33 -07:00
|
|
|
));
|
2013-02-27 10:09:06 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-05 09:49:38 -06:00
|
|
|
bool makeItem (df::reaction_product_itemst *prod, df::unit *unit, bool second_item = false, bool move_to_cursor = false)
|
2013-02-27 10:09:06 -07:00
|
|
|
{
|
2015-12-22 07:54:00 -07:00
|
|
|
vector<df::reaction_product*> out_products;
|
2013-02-27 10:09:06 -07:00
|
|
|
vector<df::item *> out_items;
|
|
|
|
vector<df::reaction_reagent *> in_reag;
|
|
|
|
vector<df::item *> in_items;
|
2014-05-22 07:18:01 -06:00
|
|
|
bool is_gloves = (prod->item_type == item_type::GLOVES);
|
|
|
|
bool is_shoes = (prod->item_type == item_type::SHOES);
|
2013-02-27 10:09:06 -07:00
|
|
|
|
2013-12-27 11:53:33 -07:00
|
|
|
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);
|
|
|
|
|
2015-12-22 07:54:00 -07:00
|
|
|
prod->produce(unit, &out_products, &out_items, &in_reag, &in_items, 1, job_skill::NONE,
|
2016-06-13 14:57:45 -06:00
|
|
|
df::historical_entity::find(unit->civ_id), 0,
|
|
|
|
(World::isFortressMode()) ? df::world_site::find(ui->site_id) : NULL, 0);
|
2013-02-27 10:09:06 -07:00
|
|
|
if (!out_items.size())
|
|
|
|
return false;
|
2013-04-08 07:29:50 -06:00
|
|
|
// if we asked to make shoes and we got twice as many as we asked, then we're okay
|
|
|
|
// otherwise, make a second set because shoes are normally made in pairs
|
|
|
|
if (is_shoes && out_items.size() == prod->count * 2)
|
|
|
|
is_shoes = false;
|
2013-12-27 11:53:33 -07:00
|
|
|
|
|
|
|
MapExtras::MapCache mc;
|
|
|
|
|
2013-02-27 10:09:06 -07:00
|
|
|
for (size_t i = 0; i < out_items.size(); i++)
|
|
|
|
{
|
2013-12-27 11:53:33 -07:00
|
|
|
bool on_ground = true;
|
|
|
|
if (container)
|
|
|
|
{
|
|
|
|
on_ground = false;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (building)
|
|
|
|
{
|
|
|
|
on_ground = false;
|
|
|
|
out_items[i]->flags.bits.removed = 1;
|
|
|
|
if (!Items::moveToBuilding(mc, out_items[i], (df::building_actual *)building, 0))
|
|
|
|
out_items[i]->moveToGround(building->centerx, building->centery, building->z);
|
|
|
|
}
|
2016-04-05 09:49:38 -06:00
|
|
|
if (move_to_cursor)
|
|
|
|
out_items[i]->moveToGround(cursor->x, cursor->y, cursor->z);
|
2017-12-16 14:04:32 -07:00
|
|
|
else if (on_ground)
|
|
|
|
out_items[i]->moveToGround(unit->pos.x, unit->pos.y, unit->pos.z);
|
2013-02-28 08:23:48 -07:00
|
|
|
if (is_gloves)
|
|
|
|
{
|
|
|
|
// if the reaction creates gloves without handedness, then create 2 sets (left and right)
|
|
|
|
if (out_items[i]->getGloveHandedness() > 0)
|
|
|
|
is_gloves = false;
|
|
|
|
else
|
2013-04-08 07:29:50 -06:00
|
|
|
out_items[i]->setGloveHandedness(second_item ? 2 : 1);
|
2013-02-28 08:23:48 -07:00
|
|
|
}
|
2013-02-27 10:09:06 -07:00
|
|
|
}
|
2013-04-08 07:29:50 -06:00
|
|
|
if ((is_gloves || is_shoes) && !second_item)
|
2016-05-11 15:54:37 -06:00
|
|
|
return makeItem(prod, unit, true, move_to_cursor);
|
2013-02-28 08:23:48 -07:00
|
|
|
|
2013-02-27 10:09:06 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result df_createitem (color_ostream &out, vector <string> & parameters)
|
|
|
|
{
|
|
|
|
string item_str, material_str;
|
2014-05-22 07:18:01 -06:00
|
|
|
df::item_type item_type = item_type::NONE;
|
2013-02-28 08:23:48 -07:00
|
|
|
int16_t item_subtype = -1;
|
|
|
|
int16_t mat_type = -1;
|
|
|
|
int32_t mat_index = -1;
|
|
|
|
int count = 1;
|
2016-04-05 09:49:38 -06:00
|
|
|
bool move_to_cursor = false;
|
2013-02-27 10:09:06 -07:00
|
|
|
|
2013-12-27 11:53:33 -07:00
|
|
|
if (parameters.size() == 1)
|
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2014-05-22 07:18:01 -06:00
|
|
|
case item_type::FLASK:
|
|
|
|
case item_type::BARREL:
|
|
|
|
case item_type::BUCKET:
|
|
|
|
case item_type::ANIMALTRAP:
|
|
|
|
case item_type::BOX:
|
|
|
|
case item_type::BIN:
|
|
|
|
case item_type::BACKPACK:
|
|
|
|
case item_type::QUIVER:
|
2013-12-27 11:53:33 -07:00
|
|
|
break;
|
2014-05-22 07:18:01 -06:00
|
|
|
case item_type::TOOL:
|
|
|
|
if (item->hasToolUse(tool_uses::LIQUID_CONTAINER))
|
2013-12-27 11:53:33 -07:00
|
|
|
break;
|
2014-05-22 07:18:01 -06:00
|
|
|
if (item->hasToolUse(tool_uses::FOOD_STORAGE))
|
2013-12-27 11:53:33 -07:00
|
|
|
break;
|
2014-05-22 07:18:01 -06:00
|
|
|
if (item->hasToolUse(tool_uses::SMALL_OBJECT_STORAGE))
|
2013-12-27 11:53:33 -07:00
|
|
|
break;
|
2014-05-22 07:18:01 -06:00
|
|
|
if (item->hasToolUse(tool_uses::TRACK_CART))
|
2013-12-27 11:53:33 -07:00
|
|
|
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())
|
|
|
|
{
|
2014-05-22 07:18:01 -06:00
|
|
|
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:
|
2013-12-27 11:53:33 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-28 08:23:48 -07:00
|
|
|
if ((parameters.size() < 2) || (parameters.size() > 3))
|
2013-02-27 10:09:06 -07:00
|
|
|
return CR_WRONG_USAGE;
|
2013-12-27 11:53:33 -07:00
|
|
|
|
2013-02-27 10:09:06 -07:00
|
|
|
item_str = parameters[0];
|
|
|
|
material_str = parameters[1];
|
|
|
|
|
2013-02-28 08:23:48 -07:00
|
|
|
if (parameters.size() == 3)
|
|
|
|
{
|
|
|
|
stringstream ss(parameters[2]);
|
|
|
|
ss >> count;
|
|
|
|
if (count < 1)
|
|
|
|
{
|
|
|
|
out.printerr("You cannot produce less than one item!\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-27 10:09:06 -07:00
|
|
|
ItemTypeInfo item;
|
|
|
|
MaterialInfo material;
|
|
|
|
vector<string> tokens;
|
|
|
|
|
2014-05-22 07:18:01 -06:00
|
|
|
if (item.find(item_str))
|
2013-02-27 10:09:06 -07:00
|
|
|
{
|
2014-05-22 07:18:01 -06:00
|
|
|
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");
|
2013-02-27 10:09:06 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
switch (item.type)
|
|
|
|
{
|
2014-05-22 07:18:01 -06:00
|
|
|
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:
|
2013-02-27 10:09:06 -07:00
|
|
|
if (item_subtype == -1)
|
|
|
|
{
|
2013-02-28 08:23:48 -07:00
|
|
|
out.printerr("You must specify a subtype!\n");
|
2013-02-27 10:09:06 -07:00
|
|
|
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;
|
|
|
|
|
2014-05-22 07:18:01 -06:00
|
|
|
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:
|
2013-02-27 10:09:06 -07:00
|
|
|
split_string(&tokens, material_str, ":");
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
if (creature->caste[j]->caste_id == tokens[1])
|
|
|
|
{
|
|
|
|
mat_type = i;
|
|
|
|
mat_index = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mat_type == -1)
|
|
|
|
{
|
|
|
|
out.printerr("The creature you specified has no such caste!\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
}
|
2013-02-27 10:09:06 -07:00
|
|
|
}
|
|
|
|
if (mat_type == -1)
|
|
|
|
{
|
|
|
|
out.printerr("Unrecognized creature ID!\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-05-22 07:18:01 -06:00
|
|
|
case item_type::CORPSE:
|
|
|
|
case item_type::CORPSEPIECE:
|
|
|
|
case item_type::FOOD:
|
2013-02-27 10:09:06 -07:00
|
|
|
out.printerr("Cannot create that type of item!\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CoreSuspender suspend;
|
|
|
|
|
|
|
|
df::unit *unit = Gui::getSelectedUnit(out, true);
|
|
|
|
if (!unit)
|
|
|
|
{
|
2015-03-09 18:17:09 -06:00
|
|
|
if (*gametype == game_type::ADVENTURE_ARENA || World::isAdventureMode())
|
2014-03-24 09:35:34 -06:00
|
|
|
{
|
|
|
|
// Use the adventurer unit
|
|
|
|
unit = world->units.active[0];
|
|
|
|
}
|
2016-04-05 09:49:38 -06:00
|
|
|
else if (!world->units.active.empty() && cursor->x >= 0)
|
|
|
|
{
|
|
|
|
// Use the first possible unit and the cursor position
|
|
|
|
unit = world->units.active[0];
|
|
|
|
move_to_cursor = true;
|
|
|
|
}
|
2014-03-24 09:35:34 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
out.printerr("No unit selected!\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2013-02-27 10:09:06 -07:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
df::reaction_product_itemst *prod = df::allocate<df::reaction_product_itemst>();
|
|
|
|
prod->item_type = item_type;
|
|
|
|
prod->item_subtype = item_subtype;
|
|
|
|
prod->mat_type = mat_type;
|
|
|
|
prod->mat_index = mat_index;
|
|
|
|
prod->probability = 100;
|
2013-02-28 08:23:48 -07:00
|
|
|
prod->count = count;
|
2013-02-27 10:09:06 -07:00
|
|
|
switch (item_type)
|
|
|
|
{
|
2014-05-22 07:18:01 -06:00
|
|
|
case item_type::BAR:
|
|
|
|
case item_type::POWDER_MISC:
|
|
|
|
case item_type::LIQUID_MISC:
|
|
|
|
case item_type::DRINK:
|
2013-02-27 10:09:06 -07:00
|
|
|
prod->product_dimension = 150;
|
|
|
|
break;
|
2014-05-22 07:18:01 -06:00
|
|
|
case item_type::THREAD:
|
2013-02-27 10:09:06 -07:00
|
|
|
prod->product_dimension = 15000;
|
|
|
|
break;
|
2014-05-22 07:18:01 -06:00
|
|
|
case item_type::CLOTH:
|
2013-02-27 10:09:06 -07:00
|
|
|
prod->product_dimension = 10000;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
prod->product_dimension = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-12-27 11:53:33 -07:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-04-05 09:49:38 -06:00
|
|
|
bool result = makeItem(prod, unit, false, move_to_cursor);
|
2013-03-14 11:00:16 -06:00
|
|
|
delete prod;
|
|
|
|
if (!result)
|
2013-02-27 10:09:06 -07:00
|
|
|
{
|
|
|
|
out.printerr("Failed to create item!\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|