2011-08-03 20:04:46 -06:00
|
|
|
// Quick Dumper : Moves items marked as "dump" to cursor
|
|
|
|
// FIXME: local item cache in map blocks needs to be fixed after teleporting items
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <climits>
|
|
|
|
#include <vector>
|
2012-01-15 13:54:14 -07:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
2011-08-03 20:04:46 -06:00
|
|
|
#include <set>
|
|
|
|
using namespace std;
|
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "modules/Maps.h"
|
|
|
|
#include "modules/Gui.h"
|
|
|
|
#include "modules/Items.h"
|
|
|
|
#include "modules/Materials.h"
|
|
|
|
#include "modules/MapCache.h"
|
2011-08-03 20:04:46 -06:00
|
|
|
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "df/item.h"
|
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/general_ref.h"
|
2012-01-14 10:13:30 -07:00
|
|
|
|
2011-08-03 20:04:46 -06:00
|
|
|
using namespace DFHack;
|
2012-01-21 17:31:15 -07:00
|
|
|
using namespace df::enums;
|
|
|
|
|
2011-08-03 20:04:46 -06:00
|
|
|
using MapExtras::Block;
|
|
|
|
using MapExtras::MapCache;
|
2012-01-16 21:12:58 -07:00
|
|
|
using df::global::world;
|
2011-08-03 20:04:46 -06:00
|
|
|
|
2012-02-13 21:54:08 -07:00
|
|
|
command_result df_autodump(Core * c, vector <string> & parameters);
|
|
|
|
command_result df_autodump_destroy_here(Core * c, vector <string> & parameters);
|
|
|
|
command_result df_autodump_destroy_item(Core * c, vector <string> & parameters);
|
2011-08-03 20:04:46 -06:00
|
|
|
|
|
|
|
DFhackCExport const char * plugin_name ( void )
|
|
|
|
{
|
|
|
|
return "autodump";
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
DFhackCExport command_result plugin_init ( Core * c, vector <PluginCommand> &commands)
|
2011-08-03 20:04:46 -06:00
|
|
|
{
|
|
|
|
commands.clear();
|
2012-01-28 05:03:56 -07:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"autodump", "Teleport items marked for dumping to the cursor.",
|
|
|
|
df_autodump, false,
|
|
|
|
" This utility lets you quickly move all items designated to be dumped.\n"
|
|
|
|
" Items are instantly moved to the cursor position, the dump flag is unset,\n"
|
|
|
|
" and the forbid flag is set, as if it had been dumped normally.\n"
|
|
|
|
" Be aware that any active dump item tasks still point at the item.\n"
|
|
|
|
"Options:\n"
|
|
|
|
" destroy - instead of dumping, destroy the items instantly.\n"
|
|
|
|
" destroy-here - only affect the tile under cursor.\n"
|
2012-01-28 06:40:09 -07:00
|
|
|
" visible - only process items that are not hidden.\n"
|
|
|
|
" hidden - only process hidden items.\n"
|
|
|
|
" forbidden - only process forbidden items (default: only unforbidden).\n"
|
2012-01-28 05:03:56 -07:00
|
|
|
));
|
2012-01-14 10:13:30 -07:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"autodump-destroy-here", "Destroy items marked for dumping under cursor.",
|
|
|
|
df_autodump_destroy_here, cursor_hotkey,
|
|
|
|
" Identical to autodump destroy-here, but intended for use as keybinding.\n"
|
|
|
|
));
|
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"autodump-destroy-item", "Destroy the selected item.",
|
|
|
|
df_autodump_destroy_item, any_item_hotkey,
|
|
|
|
" Destroy the selected item. The item may be selected\n"
|
|
|
|
" in the 'k' list, or inside a container. If called\n"
|
|
|
|
" again before the game is resumed, cancels destroy.\n"
|
|
|
|
));
|
2011-08-03 20:04:46 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( Core * c )
|
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
typedef map <DFCoord, uint32_t> coordmap;
|
2011-08-03 20:04:46 -06:00
|
|
|
|
2012-01-14 10:13:30 -07:00
|
|
|
static command_result autodump_main(Core * c, vector <string> & parameters)
|
2011-08-03 20:04:46 -06:00
|
|
|
{
|
|
|
|
// Command line options
|
|
|
|
bool destroy = false;
|
2012-01-14 10:13:30 -07:00
|
|
|
bool here = false;
|
2012-01-28 06:40:09 -07:00
|
|
|
bool need_visible = false;
|
|
|
|
bool need_hidden = false;
|
|
|
|
bool need_forbidden = false;
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < parameters.size(); i++)
|
2011-08-03 20:04:46 -06:00
|
|
|
{
|
2012-01-29 07:17:06 -07:00
|
|
|
string & p = parameters[i];
|
2011-08-03 20:04:46 -06:00
|
|
|
if(p == "destroy")
|
|
|
|
destroy = true;
|
2012-01-14 10:13:30 -07:00
|
|
|
else if (p == "destroy-here")
|
|
|
|
destroy = here = true;
|
2012-01-28 06:40:09 -07:00
|
|
|
else if (p == "visible")
|
|
|
|
need_visible = true;
|
|
|
|
else if (p == "hidden")
|
|
|
|
need_hidden = true;
|
|
|
|
else if (p == "forbidden")
|
|
|
|
need_forbidden = true;
|
2012-01-28 05:03:56 -07:00
|
|
|
else
|
|
|
|
return CR_WRONG_USAGE;
|
2011-08-03 20:04:46 -06:00
|
|
|
}
|
2012-01-15 13:59:47 -07:00
|
|
|
|
2012-01-28 06:40:09 -07:00
|
|
|
if (need_visible && need_hidden)
|
|
|
|
{
|
|
|
|
c->con.printerr("An item can't be both hidden and visible.\n");
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
2011-08-03 20:04:46 -06:00
|
|
|
DFHack::VersionInfo *mem = c->vinfo;
|
|
|
|
DFHack::Gui * Gui = c->getGui();
|
2012-01-19 20:44:17 -07:00
|
|
|
if (!Maps::IsValid())
|
2011-08-03 20:04:46 -06:00
|
|
|
{
|
2012-01-19 20:44:17 -07:00
|
|
|
c->con.printerr("Map is not available!\n");
|
2011-08-03 20:04:46 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2012-01-31 09:55:38 -07:00
|
|
|
size_t numItems = world->items.all.size();
|
2012-01-19 20:44:17 -07:00
|
|
|
|
|
|
|
MapCache MC;
|
2011-08-03 20:04:46 -06:00
|
|
|
int i = 0;
|
|
|
|
int dumped_total = 0;
|
|
|
|
|
|
|
|
int cx, cy, cz;
|
|
|
|
DFCoord pos_cursor;
|
2012-01-14 10:13:30 -07:00
|
|
|
if(!destroy || here)
|
2011-08-03 20:04:46 -06:00
|
|
|
{
|
|
|
|
if (!Gui->getCursorCoords(cx,cy,cz))
|
|
|
|
{
|
|
|
|
c->con.printerr("Cursor position not found. Please enabled the cursor.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
pos_cursor = DFCoord(cx,cy,cz);
|
2012-01-14 10:13:30 -07:00
|
|
|
}
|
|
|
|
if (!destroy)
|
|
|
|
{
|
2011-08-03 20:04:46 -06:00
|
|
|
{
|
|
|
|
Block * b = MC.BlockAt(pos_cursor / 16);
|
|
|
|
if(!b)
|
|
|
|
{
|
|
|
|
c->con.printerr("Cursor is in an invalid/uninitialized area. Place it over a floor.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2012-02-13 15:56:33 -07:00
|
|
|
df::tiletype ttype = MC.tiletypeAt(pos_cursor);
|
2011-08-03 20:04:46 -06:00
|
|
|
if(!DFHack::isFloorTerrain(ttype))
|
|
|
|
{
|
|
|
|
c->con.printerr("Cursor should be placed over a floor.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
coordmap counts;
|
|
|
|
// proceed with the dumpification operation
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t i=0; i< numItems; i++)
|
2011-08-03 20:04:46 -06:00
|
|
|
{
|
2012-01-16 21:12:58 -07:00
|
|
|
df::item * itm = world->items.all[i];
|
2012-01-19 03:30:22 -07:00
|
|
|
DFCoord pos_item(itm->pos.x, itm->pos.y, itm->pos.z);
|
2011-08-03 20:04:46 -06:00
|
|
|
|
|
|
|
// keep track how many items are at places. all items.
|
|
|
|
coordmap::iterator it = counts.find(pos_item);
|
|
|
|
if(it == counts.end())
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
pair< coordmap::iterator, bool > inserted = counts.insert(make_pair(pos_item,1));
|
2011-08-03 20:04:46 -06:00
|
|
|
it = inserted.first;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->second ++;
|
|
|
|
}
|
|
|
|
// iterator is valid here, we use it later to decrement the pile counter if the item is moved
|
|
|
|
|
|
|
|
// only dump the stuff marked for dumping and laying on the ground
|
2012-01-16 19:16:16 -07:00
|
|
|
if ( !itm->flags.bits.dump
|
|
|
|
|| !itm->flags.bits.on_ground
|
|
|
|
|| itm->flags.bits.construction
|
|
|
|
|| itm->flags.bits.in_building
|
|
|
|
|| itm->flags.bits.in_chest
|
|
|
|
|| itm->flags.bits.in_inventory
|
|
|
|
|| itm->flags.bits.artifact1
|
2011-08-03 20:04:46 -06:00
|
|
|
)
|
|
|
|
continue;
|
|
|
|
|
2012-01-28 06:40:09 -07:00
|
|
|
if (need_visible && itm->flags.bits.hidden)
|
|
|
|
continue;
|
|
|
|
if (need_hidden && !itm->flags.bits.hidden)
|
|
|
|
continue;
|
|
|
|
if (need_forbidden && !itm->flags.bits.forbid)
|
|
|
|
continue;
|
|
|
|
if (!need_forbidden && itm->flags.bits.forbid)
|
|
|
|
continue;
|
|
|
|
|
2011-08-03 20:04:46 -06:00
|
|
|
if(!destroy) // move to cursor
|
|
|
|
{
|
|
|
|
// Change flags to indicate the dump was completed, as if by super-dwarfs
|
2012-01-16 19:16:16 -07:00
|
|
|
itm->flags.bits.dump = false;
|
|
|
|
itm->flags.bits.forbid = true;
|
2011-08-03 20:04:46 -06:00
|
|
|
|
|
|
|
// Don't move items if they're already at the cursor
|
|
|
|
if (pos_cursor == pos_item)
|
|
|
|
continue;
|
|
|
|
|
2011-08-21 15:02:05 -06:00
|
|
|
// Do we need to fix block-local item ID vector?
|
|
|
|
if(pos_item/16 != pos_cursor/16)
|
|
|
|
{
|
|
|
|
// yes...
|
|
|
|
cerr << "Moving from block to block!" << endl;
|
2012-01-19 20:44:17 -07:00
|
|
|
df::map_block * bl_src = Maps::getBlockAbs(itm->pos.x, itm->pos.y, itm->pos.z);
|
|
|
|
df::map_block * bl_tgt = Maps::getBlockAbs(cx, cy, cz);
|
2011-08-21 15:02:05 -06:00
|
|
|
if(bl_src)
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
remove(bl_src->items.begin(), bl_src->items.end(),itm->id);
|
2011-08-21 15:02:05 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "No source block" << endl;
|
|
|
|
}
|
|
|
|
if(bl_tgt)
|
|
|
|
{
|
|
|
|
bl_tgt->items.push_back(itm->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "No target block" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-03 20:04:46 -06:00
|
|
|
// Move the item
|
2012-01-19 03:30:22 -07:00
|
|
|
itm->pos.x = pos_cursor.x;
|
|
|
|
itm->pos.y = pos_cursor.y;
|
|
|
|
itm->pos.z = pos_cursor.z;
|
2011-08-03 20:04:46 -06:00
|
|
|
}
|
|
|
|
else // destroy
|
|
|
|
{
|
2012-01-14 10:13:30 -07:00
|
|
|
if (here && pos_item != pos_cursor)
|
|
|
|
continue;
|
|
|
|
|
2012-01-16 19:16:16 -07:00
|
|
|
itm->flags.bits.garbage_colect = true;
|
2012-01-14 10:13:30 -07:00
|
|
|
|
|
|
|
// Cosmetic changes: make them disappear from view instantly
|
2012-01-16 19:16:16 -07:00
|
|
|
itm->flags.bits.forbid = true;
|
|
|
|
itm->flags.bits.hidden = true;
|
2011-08-03 20:04:46 -06:00
|
|
|
}
|
|
|
|
// keeping track of item pile sizes ;)
|
|
|
|
it->second --;
|
|
|
|
dumped_total++;
|
|
|
|
}
|
|
|
|
if(!destroy) // TODO: do we have to do any of this when destroying items?
|
|
|
|
{
|
|
|
|
// for each item pile, see if it reached zero. if so, unset item flag on the tile it's on
|
|
|
|
coordmap::iterator it = counts.begin();
|
|
|
|
coordmap::iterator end = counts.end();
|
|
|
|
while(it != end)
|
|
|
|
{
|
|
|
|
if(it->second == 0)
|
|
|
|
{
|
2012-01-19 13:11:52 -07:00
|
|
|
df::tile_occupancy occ = MC.occupancyAt(it->first);
|
2011-08-03 20:04:46 -06:00
|
|
|
occ.bits.item = false;
|
|
|
|
MC.setOccupancyAt(it->first, occ);
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
// Set "item here" flag on target tile, if we moved any items to the target tile.
|
|
|
|
if (dumped_total > 0)
|
|
|
|
{
|
|
|
|
// assume there is a possibility the cursor points at some weird location with missing block data
|
|
|
|
Block * b = MC.BlockAt(pos_cursor / 16);
|
|
|
|
if(b)
|
|
|
|
{
|
2012-01-19 13:11:52 -07:00
|
|
|
df::tile_occupancy occ = MC.occupancyAt(pos_cursor);
|
2011-08-03 20:04:46 -06:00
|
|
|
occ.bits.item = 1;
|
|
|
|
MC.setOccupancyAt(pos_cursor,occ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// write map changes back to DF.
|
|
|
|
MC.WriteAll();
|
|
|
|
// Is this necessary? Is "forbid" a dirtyable attribute like "dig" is?
|
2012-01-19 20:44:17 -07:00
|
|
|
Maps::WriteDirtyBit(cx/16, cy/16, cz, true);
|
2011-08-03 20:04:46 -06:00
|
|
|
}
|
2012-01-15 14:01:40 -07:00
|
|
|
c->con.print("Done. %d items %s.\n", dumped_total, destroy ? "marked for destruction" : "quickdumped");
|
2011-08-03 20:04:46 -06:00
|
|
|
return CR_OK;
|
2012-01-14 10:13:30 -07:00
|
|
|
}
|
|
|
|
|
2012-02-13 21:54:08 -07:00
|
|
|
command_result df_autodump(Core * c, vector <string> & parameters)
|
2012-01-14 10:13:30 -07:00
|
|
|
{
|
|
|
|
CoreSuspender suspend(c);
|
|
|
|
|
|
|
|
return autodump_main(c, parameters);
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:54:08 -07:00
|
|
|
command_result df_autodump_destroy_here(Core * c, vector <string> & parameters)
|
2012-01-14 10:13:30 -07:00
|
|
|
{
|
|
|
|
// HOTKEY COMMAND; CORE ALREADY SUSPENDED
|
|
|
|
if (!parameters.empty())
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
vector<string> args;
|
|
|
|
args.push_back("destroy-here");
|
|
|
|
|
|
|
|
return autodump_main(c, args);
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
static map<int, df::item_flags> pending_destroy;
|
2012-01-14 10:13:30 -07:00
|
|
|
static int last_frame = 0;
|
|
|
|
|
2012-02-13 21:54:08 -07:00
|
|
|
command_result df_autodump_destroy_item(Core * c, vector <string> & parameters)
|
2012-01-14 10:13:30 -07:00
|
|
|
{
|
|
|
|
// HOTKEY COMMAND; CORE ALREADY SUSPENDED
|
|
|
|
if (!parameters.empty())
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
df::item *item = getSelectedItem(c);
|
|
|
|
if (!item)
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
|
|
|
// Allow undoing the destroy
|
2012-01-21 17:31:15 -07:00
|
|
|
if (world->frame_counter != last_frame)
|
2012-01-14 10:13:30 -07:00
|
|
|
{
|
2012-01-21 17:31:15 -07:00
|
|
|
last_frame = world->frame_counter;
|
2012-01-14 10:13:30 -07:00
|
|
|
pending_destroy.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pending_destroy.count(item->id))
|
|
|
|
{
|
|
|
|
df::item_flags old_flags = pending_destroy[item->id];
|
|
|
|
pending_destroy.erase(item->id);
|
|
|
|
|
|
|
|
item->flags.bits.garbage_colect = false;
|
|
|
|
item->flags.bits.hidden = old_flags.bits.hidden;
|
|
|
|
item->flags.bits.dump = old_flags.bits.dump;
|
|
|
|
item->flags.bits.forbid = old_flags.bits.forbid;
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the item is good to destroy
|
|
|
|
if (item->flags.bits.garbage_colect)
|
|
|
|
{
|
|
|
|
c->con.printerr("Item is already marked for destroy.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->flags.bits.construction ||
|
|
|
|
item->flags.bits.in_building ||
|
|
|
|
item->flags.bits.artifact1)
|
|
|
|
{
|
|
|
|
c->con.printerr("Choosing not to destroy buildings, constructions and artifacts.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < item->itemrefs.size(); i++)
|
2012-01-14 10:13:30 -07:00
|
|
|
{
|
|
|
|
df::general_ref *ref = item->itemrefs[i];
|
2012-01-21 17:31:15 -07:00
|
|
|
if (ref->getType() == general_ref_type::UNIT_HOLDER)
|
2012-01-14 10:13:30 -07:00
|
|
|
{
|
|
|
|
c->con.printerr("Choosing not to destroy items in unit inventory.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the flags
|
|
|
|
pending_destroy[item->id] = item->flags;
|
|
|
|
|
|
|
|
item->flags.bits.garbage_colect = true;
|
|
|
|
item->flags.bits.hidden = true;
|
|
|
|
item->flags.bits.dump = true;
|
|
|
|
item->flags.bits.forbid = true;
|
|
|
|
return CR_OK;
|
|
|
|
}
|