2011-04-10 13:25:41 -06:00
|
|
|
/*
|
|
|
|
* Confiscates and dumps garbage owned by dwarfs.
|
|
|
|
*/
|
|
|
|
|
2011-04-11 14:13:06 -06:00
|
|
|
#include <cstdio>
|
2011-04-10 13:25:41 -06:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <climits>
|
|
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <DFHack.h>
|
|
|
|
#include <dfhack/DFVector.h>
|
|
|
|
#include <dfhack/DFTypes.h>
|
2011-05-17 00:36:38 -06:00
|
|
|
#include <dfhack/extra/termutil.h>
|
2011-04-10 13:25:41 -06:00
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
{
|
2011-05-14 22:10:47 -06:00
|
|
|
bool temporary_terminal = TemporaryTerminal();
|
2011-04-10 13:25:41 -06:00
|
|
|
bool dump_scattered = false;
|
2011-04-11 04:33:30 -06:00
|
|
|
bool confiscate_all = false;
|
|
|
|
bool dry_run = false;
|
2011-04-10 13:25:41 -06:00
|
|
|
int wear_dump_level = 65536;
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
for(int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
char *arg = argv[i];
|
|
|
|
if (arg[0] != '-')
|
|
|
|
continue;
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
for (; *arg; arg++) {
|
|
|
|
switch (arg[0]) {
|
2011-04-11 04:33:30 -06:00
|
|
|
case 'd':
|
|
|
|
dry_run = true;
|
|
|
|
break;
|
|
|
|
case 'l':
|
2011-04-10 13:25:41 -06:00
|
|
|
dump_scattered = true;
|
|
|
|
break;
|
2011-04-11 04:33:30 -06:00
|
|
|
case 'a':
|
|
|
|
confiscate_all = true;
|
|
|
|
break;
|
2011-04-10 13:25:41 -06:00
|
|
|
case 'x':
|
|
|
|
wear_dump_level = 1;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
wear_dump_level = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
DFHack::Process * p;
|
|
|
|
unsigned int i,j;
|
|
|
|
DFHack::ContextManager DFMgr("Memory.xml");
|
|
|
|
DFHack::Context * DF;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
DF = DFMgr.getSingleContext();
|
|
|
|
DF->Attach();
|
|
|
|
}
|
|
|
|
catch (exception& e)
|
|
|
|
{
|
|
|
|
cerr << e.what() << endl;
|
2011-05-14 22:10:47 -06:00
|
|
|
if(temporary_terminal)
|
|
|
|
cin.ignore();
|
2011-04-10 13:25:41 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFHack::VersionInfo * mem = DF->getMemoryInfo();
|
2011-04-11 04:33:30 -06:00
|
|
|
DFHack::Materials *Materials = DF->getMaterials();
|
|
|
|
DFHack::Items *Items = DF->getItems();
|
|
|
|
DFHack::Creatures *Creatures = DF->getCreatures();
|
|
|
|
DFHack::Translation *Tran = DF->getTranslation();
|
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
Materials->ReadAllMaterials();
|
2011-04-11 04:33:30 -06:00
|
|
|
uint32_t num_creatures;
|
|
|
|
Creatures->Start(num_creatures);
|
|
|
|
Tran->Start();
|
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
p = DF->getProcess();
|
|
|
|
DFHack::OffsetGroup* itemGroup = mem->getGroup("Items");
|
|
|
|
unsigned vector_addr = itemGroup->getAddress("items_vector");
|
|
|
|
DFHack::DfVector <uint32_t> p_items (p, vector_addr);
|
|
|
|
uint32_t size = p_items.size();
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
printf("Found total %d items.\n", size);
|
|
|
|
|
|
|
|
for (i=0;i<size;i++)
|
|
|
|
{
|
|
|
|
uint32_t curItem = p_items[i];
|
2011-04-11 14:13:06 -06:00
|
|
|
DFHack::dfh_item itm;
|
|
|
|
Items->readItem(curItem, itm);
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
bool confiscate = false;
|
|
|
|
bool dump = false;
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-05-12 11:09:49 -06:00
|
|
|
if (!itm.base.flags.owned) {
|
|
|
|
int32_t owner = Items->getItemOwnerID(itm);
|
|
|
|
if (owner >= 0) {
|
|
|
|
printf("Fixing a misflagged item: ");
|
|
|
|
confiscate = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name = Items->getItemClass(itm.matdesc.itemType);
|
|
|
|
|
2011-04-11 14:13:06 -06:00
|
|
|
if (itm.base.flags.rotten)
|
2011-04-10 13:25:41 -06:00
|
|
|
{
|
2011-04-11 04:33:30 -06:00
|
|
|
printf("Confiscating a rotten item: \t");
|
2011-04-10 13:25:41 -06:00
|
|
|
confiscate = true;
|
|
|
|
}
|
2011-04-14 10:06:36 -06:00
|
|
|
else if (itm.base.flags.on_ground &&
|
|
|
|
(name == "food" || name == "meat" || name == "plant"))
|
|
|
|
{
|
|
|
|
printf("Confiscating a dropped foodstuff: \t");
|
|
|
|
confiscate = true;
|
|
|
|
}
|
2011-04-10 13:25:41 -06:00
|
|
|
else if (itm.wear_level >= wear_dump_level)
|
|
|
|
{
|
2011-04-11 04:33:30 -06:00
|
|
|
printf("Confiscating and dumping a worn item: \t");
|
2011-04-10 13:25:41 -06:00
|
|
|
confiscate = true;
|
|
|
|
dump = true;
|
|
|
|
}
|
2011-04-11 14:13:06 -06:00
|
|
|
else if (dump_scattered && itm.base.flags.on_ground)
|
2011-04-10 13:25:41 -06:00
|
|
|
{
|
2011-04-11 04:33:30 -06:00
|
|
|
printf("Confiscating and dumping litter: \t");
|
2011-04-10 13:25:41 -06:00
|
|
|
confiscate = true;
|
|
|
|
dump = true;
|
|
|
|
}
|
2011-04-11 04:33:30 -06:00
|
|
|
else if (confiscate_all)
|
|
|
|
{
|
|
|
|
printf("Confiscating: \t");
|
|
|
|
confiscate = true;
|
|
|
|
}
|
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
if (confiscate)
|
|
|
|
{
|
2011-05-12 11:09:49 -06:00
|
|
|
if (!dry_run) {
|
|
|
|
if (!Items->removeItemOwner(itm, Creatures))
|
|
|
|
printf("(unsuccessfully) ");
|
|
|
|
if (dump)
|
|
|
|
itm.base.flags.dump = 1;
|
2011-04-10 13:25:41 -06:00
|
|
|
|
2011-04-11 14:13:06 -06:00
|
|
|
Items->writeItem(itm);
|
2011-05-12 11:09:49 -06:00
|
|
|
}
|
2011-04-10 13:25:41 -06:00
|
|
|
|
|
|
|
printf(
|
2011-04-11 04:33:30 -06:00
|
|
|
"%s (wear %d)",
|
2011-04-11 14:13:06 -06:00
|
|
|
Items->getItemDescription(itm, Materials).c_str(),
|
2011-04-11 04:33:30 -06:00
|
|
|
itm.wear_level
|
|
|
|
);
|
|
|
|
|
2011-04-11 14:13:06 -06:00
|
|
|
int32_t owner = Items->getItemOwnerID(itm);
|
2011-04-11 04:33:30 -06:00
|
|
|
int32_t owner_index = Creatures->FindIndexById(owner);
|
|
|
|
std::string info;
|
|
|
|
|
|
|
|
if (owner_index >= 0)
|
|
|
|
{
|
|
|
|
DFHack::t_creature temp;
|
|
|
|
Creatures->ReadCreature(owner_index,temp);
|
|
|
|
temp.name.first_name[0] = toupper(temp.name.first_name[0]);
|
|
|
|
info = temp.name.first_name;
|
|
|
|
if (temp.name.nickname[0])
|
|
|
|
info += std::string(" '") + temp.name.nickname + "'";
|
|
|
|
info += " ";
|
|
|
|
info += Tran->TranslateName(temp.name,false);
|
|
|
|
printf(", owner %s", info.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
2011-04-11 14:13:06 -06:00
|
|
|
/*
|
|
|
|
printf(
|
2011-04-11 04:33:30 -06:00
|
|
|
"%5d: %08x %08x (%d,%d,%d) #%08x [%d] %s - %s %s\n",
|
2011-04-11 14:13:06 -06:00
|
|
|
i, itm.origin, itm.base.flags.whole,
|
|
|
|
itm.base.x, itm.base.y, itm.base.z,
|
|
|
|
itm.base.vtable,
|
2011-04-10 13:25:41 -06:00
|
|
|
itm.wear_level,
|
|
|
|
Items->getItemClass(itm.matdesc.itemType).c_str(),
|
2011-04-11 14:13:06 -06:00
|
|
|
Items->getItemDescription(itm, Materials).c_str(),
|
2011-04-11 04:33:30 -06:00
|
|
|
info.c_str()
|
2011-04-11 14:13:06 -06:00
|
|
|
);
|
|
|
|
*/
|
2011-04-10 13:25:41 -06:00
|
|
|
}
|
2011-04-11 04:33:30 -06:00
|
|
|
}
|
2011-05-14 22:10:47 -06:00
|
|
|
if(temporary_terminal)
|
|
|
|
{
|
|
|
|
cout << "Done. Press any key to continue" << endl;
|
|
|
|
cin.ignore();
|
|
|
|
}
|
2011-04-10 13:25:41 -06:00
|
|
|
return 0;
|
|
|
|
}
|