2011-04-27 17:36:31 -06:00
|
|
|
// Quick Dumper : Moves items marked as "dump" to cursor
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <climits>
|
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <DFHack.h>
|
|
|
|
#include <dfhack/DFVector.h>
|
|
|
|
#include <dfhack/extra/MapExtras.h> // map cache for the win.
|
|
|
|
using namespace DFHack;
|
|
|
|
using MapExtras::Block;
|
|
|
|
using MapExtras::MapCache;
|
|
|
|
|
|
|
|
typedef std::map <DFCoord, uint32_t> coordmap;
|
|
|
|
|
2011-04-29 20:48:28 -06:00
|
|
|
int main (int argc, char * argv[])
|
2011-04-27 17:36:31 -06:00
|
|
|
{
|
2011-04-29 20:48:28 -06:00
|
|
|
// Command line options
|
|
|
|
bool destroy = false;
|
|
|
|
if(argc > 1 && strcmp(argv[1],"-d") == 0)
|
|
|
|
destroy = true;
|
2011-04-27 17:36:31 -06:00
|
|
|
|
|
|
|
DFHack::ContextManager CM ("Memory.xml");
|
|
|
|
DFHack::Context * DF;
|
|
|
|
DFHack::VersionInfo *mem;
|
|
|
|
DFHack::Gui * Gui;
|
|
|
|
DFHack::Items * Items;
|
|
|
|
DFHack::Maps *Maps;
|
|
|
|
DFHack::occupancies40d * occupancies;
|
|
|
|
|
|
|
|
cout << "This utility lets you quickly move all items designated to be dumped." << endl
|
|
|
|
<< "Items are instantly moved to the cursor position, the dump flag is unset," << endl
|
|
|
|
<< "and the forbid flag is set, as if it had been dumped normally." << endl
|
|
|
|
<< "Be aware that any active dump item tasks still point at the item." << endl << endl;
|
2011-05-07 19:42:14 -06:00
|
|
|
|
|
|
|
uint32_t vector_addr = 0;
|
|
|
|
|
2011-04-27 17:36:31 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
DF = CM.getSingleContext();
|
|
|
|
DF->Attach();
|
|
|
|
mem = DF->getMemoryInfo();
|
|
|
|
Gui = DF->getGui();
|
|
|
|
Items = DF->getItems();
|
|
|
|
Maps = DF->getMaps();
|
2011-05-07 19:42:14 -06:00
|
|
|
DFHack::OffsetGroup* itemGroup = mem->getGroup("Items");
|
|
|
|
vector_addr = itemGroup->getAddress("items_vector");
|
2011-04-27 17:36:31 -06:00
|
|
|
}
|
|
|
|
catch (exception& e)
|
|
|
|
{
|
|
|
|
cerr << e.what() << endl;
|
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
cin.ignore();
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFHack::Process * p = DF->getProcess();
|
|
|
|
DFHack::DfVector <uint32_t> p_items (p, vector_addr);
|
|
|
|
uint32_t numItems = p_items.size();
|
|
|
|
|
|
|
|
// init the map
|
|
|
|
if(!Maps->Start())
|
|
|
|
{
|
|
|
|
cerr << "Can't initialize map." << endl;
|
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
cin.ignore();
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
MapCache MC (Maps);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
int dumped_total = 0;
|
|
|
|
|
|
|
|
int cx, cy, cz;
|
2011-04-29 20:48:28 -06:00
|
|
|
DFCoord pos_cursor;
|
|
|
|
if(!destroy)
|
2011-04-27 17:36:31 -06:00
|
|
|
{
|
2011-04-29 20:48:28 -06:00
|
|
|
if (!Gui->getCursorCoords(cx,cy,cz))
|
2011-04-27 17:36:31 -06:00
|
|
|
{
|
2011-04-29 20:48:28 -06:00
|
|
|
cerr << "Cursor position not found. Please enabled the cursor." << endl;
|
2011-04-27 17:36:31 -06:00
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
cin.ignore();
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-29 20:48:28 -06:00
|
|
|
pos_cursor = DFCoord(cx,cy,cz);
|
|
|
|
{
|
|
|
|
Block * b = MC.BlockAt(pos_cursor / 16);
|
|
|
|
if(!b)
|
|
|
|
{
|
|
|
|
cerr << "Cursor is in an invalid area. Place it over something save first." << endl;
|
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
cin.ignore();
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
2011-05-07 19:42:14 -06:00
|
|
|
uint16_t ttype = MC.tiletypeAt(pos_cursor);
|
|
|
|
if(!DFHack::isFloorTerrain(ttype))
|
|
|
|
{
|
|
|
|
cerr << "Cursor should be placed over a floor." << endl;
|
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
cin.ignore();
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
2011-04-29 20:48:28 -06:00
|
|
|
}
|
2011-04-27 17:36:31 -06:00
|
|
|
}
|
|
|
|
coordmap counts;
|
|
|
|
// proceed with the dumpification operation
|
|
|
|
for(uint32_t i=0; i< numItems; i++)
|
|
|
|
{
|
|
|
|
DFHack::dfh_item temp;
|
|
|
|
Items->readItem(p_items[i],temp);
|
|
|
|
DFCoord pos_item(temp.base.x, temp.base.y, temp.base.z);
|
|
|
|
|
|
|
|
// keep track how many items are at places. all items.
|
|
|
|
coordmap::iterator it = counts.find(pos_item);
|
|
|
|
if(it == counts.end())
|
|
|
|
{
|
|
|
|
std::pair< coordmap::iterator, bool > inserted = counts.insert(std::make_pair(pos_item,1));
|
|
|
|
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
|
2011-05-07 19:36:46 -06:00
|
|
|
if ( !temp.base.flags.dump
|
|
|
|
|| !temp.base.flags.on_ground
|
|
|
|
|| temp.base.flags.construction
|
|
|
|
|| temp.base.flags.hidden
|
|
|
|
|| temp.base.flags.in_building
|
|
|
|
|| temp.base.flags.in_chest
|
|
|
|
|| temp.base.flags.in_inventory
|
|
|
|
|| temp.base.flags.construction
|
|
|
|
)
|
2011-04-27 17:36:31 -06:00
|
|
|
continue;
|
|
|
|
|
2011-04-29 20:48:28 -06:00
|
|
|
if(!destroy) // move to cursor
|
|
|
|
{
|
|
|
|
// Change flags to indicate the dump was completed, as if by super-dwarfs
|
|
|
|
temp.base.flags.dump = false;
|
|
|
|
temp.base.flags.forbid = true;
|
|
|
|
|
|
|
|
// Don't move items if they're already at the cursor
|
|
|
|
if (pos_cursor == pos_item)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Move the item
|
|
|
|
temp.base.x = pos_cursor.x;
|
|
|
|
temp.base.y = pos_cursor.y;
|
|
|
|
temp.base.z = pos_cursor.z;
|
|
|
|
}
|
|
|
|
else // destroy
|
|
|
|
{
|
|
|
|
temp.base.flags.garbage_colect = true;
|
|
|
|
}
|
2011-04-27 17:36:31 -06:00
|
|
|
Items->writeItem(temp);
|
|
|
|
// keeping track of item pile sizes ;)
|
|
|
|
it->second --;
|
|
|
|
dumped_total++;
|
|
|
|
}
|
2011-04-29 20:48:28 -06:00
|
|
|
if(!destroy) // TODO: do we have to do any of this when destroying items?
|
2011-04-27 17:36:31 -06:00
|
|
|
{
|
2011-04-29 20:48:28 -06:00
|
|
|
// 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)
|
2011-04-27 17:36:31 -06:00
|
|
|
{
|
2011-04-29 20:48:28 -06:00
|
|
|
if(it->second == 0)
|
|
|
|
{
|
|
|
|
t_occupancy occ = MC.occupancyAt(it->first);
|
|
|
|
occ.bits.item = false;
|
|
|
|
MC.setOccupancyAt(it->first, occ);
|
|
|
|
}
|
|
|
|
it++;
|
2011-04-27 17:36:31 -06:00
|
|
|
}
|
2011-04-29 20:48:28 -06:00
|
|
|
// Set "item here" flag on target tile, if we moved any items to the target tile.
|
|
|
|
if (dumped_total > 0)
|
2011-04-27 17:36:31 -06:00
|
|
|
{
|
2011-04-29 20:48:28 -06:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
t_occupancy occ = MC.occupancyAt(pos_cursor);
|
|
|
|
occ.bits.item = 1;
|
|
|
|
MC.setOccupancyAt(pos_cursor,occ);
|
|
|
|
}
|
2011-04-27 17:36:31 -06:00
|
|
|
}
|
2011-04-29 20:48:28 -06:00
|
|
|
// write map changes back to DF.
|
|
|
|
MC.WriteAll();
|
|
|
|
// Is this necessary? Is "forbid" a dirtyable attribute like "dig" is?
|
|
|
|
Maps->WriteDirtyBit(cx/16, cy/16, cz, true);
|
2011-04-27 17:36:31 -06:00
|
|
|
}
|
|
|
|
DF->Detach();
|
2011-05-07 19:45:58 -06:00
|
|
|
if(!destroy)
|
|
|
|
{
|
|
|
|
cout << "Done. " << dumped_total << " items quickdumped." << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << "Done. " << dumped_total << " items marked for destruction." << endl;
|
|
|
|
}
|
2011-04-27 17:36:31 -06:00
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
cout << "Press any key to continue" << endl;
|
|
|
|
cin.ignore();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|