2011-04-10 13:25:41 -06:00
|
|
|
/*
|
|
|
|
* Confiscates and dumps garbage owned by dwarfs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <climits>
|
|
|
|
#include <vector>
|
2011-08-04 16:41:31 -06:00
|
|
|
#include <set>
|
2011-04-10 13:25:41 -06:00
|
|
|
using namespace std;
|
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
|
|
|
#include <Console.h>
|
|
|
|
#include <Export.h>
|
|
|
|
#include <PluginManager.h>
|
2011-08-04 16:41:31 -06:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2011-12-31 04:48:42 -07:00
|
|
|
#include <modules/Maps.h>
|
|
|
|
#include <modules/Items.h>
|
|
|
|
#include <modules/Units.h>
|
|
|
|
#include <modules/Materials.h>
|
|
|
|
#include <modules/Translation.h>
|
2011-08-04 16:41:31 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
DFhackCExport command_result df_cleanowned (Core * c, vector <string> & parameters);
|
|
|
|
|
|
|
|
DFhackCExport const char * plugin_name ( void )
|
|
|
|
{
|
|
|
|
return "cleanowned";
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
|
|
|
commands.clear();
|
2011-08-05 20:37:29 -06:00
|
|
|
commands.push_back(PluginCommand("cleanowned",
|
2011-08-04 16:41:31 -06:00
|
|
|
"Confiscates and dumps garbage owned by dwarfs.",
|
|
|
|
df_cleanowned));
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2011-04-10 13:25:41 -06:00
|
|
|
|
2011-08-04 16:41:31 -06:00
|
|
|
DFhackCExport command_result plugin_shutdown ( Core * c )
|
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result df_cleanowned (Core * c, vector <string> & parameters)
|
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-08-04 21:02:36 -06:00
|
|
|
for(std::size_t i = 0; i < parameters.size(); i++)
|
2011-04-10 13:25:41 -06:00
|
|
|
{
|
2011-08-04 16:41:31 -06:00
|
|
|
string & param = parameters[i];
|
|
|
|
if(param == "dryrun")
|
|
|
|
dry_run = true;
|
|
|
|
else if(param == "scattered")
|
|
|
|
dump_scattered = true;
|
|
|
|
else if(param == "all")
|
|
|
|
confiscate_all = true;
|
|
|
|
else if(param == "x")
|
|
|
|
wear_dump_level = 1;
|
|
|
|
else if(param == "X")
|
2011-08-04 21:02:36 -06:00
|
|
|
wear_dump_level = 2;
|
2011-08-04 16:41:31 -06:00
|
|
|
else if(param == "?" || param == "help")
|
|
|
|
{
|
2011-08-04 21:02:36 -06:00
|
|
|
c->con.print("This tool lets you confiscate and dump all the garbage\n"
|
|
|
|
"dwarves ultimately accumulate.\n"
|
|
|
|
"By default, only rotten and dropped food is confiscated.\n"
|
|
|
|
"Options:\n"
|
|
|
|
" dryrun - don't actually do anything, just print what would be done.\n"
|
|
|
|
" scattered - confiscate owned items on the ground\n"
|
|
|
|
" all - confiscate everything\n"
|
|
|
|
" x - confiscate & dump 'x' and worse damaged items\n"
|
|
|
|
" X - confiscate & dump 'X' and worse damaged items\n"
|
|
|
|
" ? - this help\n"
|
|
|
|
"Example:\n"
|
|
|
|
" confiscate scattered X\n"
|
|
|
|
" This will confiscate rotten and dropped food, garbage on the floors\n"
|
|
|
|
" and any worn items wit 'X' damage and above.\n"
|
|
|
|
);
|
2011-08-04 16:41:31 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->con.printerr("Parameter '%s' is not valid. See 'cleanowned help'.\n",param.c_str());
|
|
|
|
return CR_FAILURE;
|
2011-04-10 13:25:41 -06:00
|
|
|
}
|
|
|
|
}
|
2011-08-04 21:02:36 -06:00
|
|
|
c->Suspend();
|
2011-08-04 16:41:31 -06:00
|
|
|
DFHack::Materials *Materials = c->getMaterials();
|
|
|
|
DFHack::Items *Items = c->getItems();
|
2011-12-02 02:56:40 -07:00
|
|
|
DFHack::Units *Creatures = c->getUnits();
|
2011-08-04 16:41:31 -06:00
|
|
|
DFHack::Translation *Tran = c->getTranslation();
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-08-04 16:41:31 -06:00
|
|
|
uint32_t num_creatures;
|
|
|
|
bool ok = true;
|
|
|
|
ok &= Materials->ReadAllMaterials();
|
|
|
|
ok &= Creatures->Start(num_creatures);
|
|
|
|
ok &= Tran->Start();
|
|
|
|
|
2011-10-24 20:48:06 -06:00
|
|
|
vector<df_item *> p_items;
|
2011-08-04 16:41:31 -06:00
|
|
|
ok &= Items->readItemVector(p_items);
|
|
|
|
if(!ok)
|
2011-04-10 13:25:41 -06:00
|
|
|
{
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.printerr("Can't continue due to offset errors.\n");
|
|
|
|
c->Resume();
|
|
|
|
return CR_FAILURE;
|
2011-04-10 13:25:41 -06:00
|
|
|
}
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.print("Found total %d items.\n", p_items.size());
|
2011-04-10 13:25:41 -06:00
|
|
|
|
2011-08-04 16:41:31 -06:00
|
|
|
for (std::size_t i=0; i < p_items.size(); i++)
|
2011-04-10 13:25:41 -06:00
|
|
|
{
|
2011-10-26 14:18:13 -06:00
|
|
|
df_item * item = p_items[i];
|
2011-04-10 13:25:41 -06:00
|
|
|
bool confiscate = false;
|
|
|
|
bool dump = false;
|
2011-04-11 04:33:30 -06:00
|
|
|
|
2011-10-26 14:18:13 -06:00
|
|
|
if (!item->flags.owned)
|
2011-08-04 16:41:31 -06:00
|
|
|
{
|
2011-10-26 14:18:13 -06:00
|
|
|
int32_t owner = Items->getItemOwnerID(item);
|
2011-08-04 16:41:31 -06:00
|
|
|
if (owner >= 0)
|
|
|
|
{
|
2012-01-11 10:04:20 -07:00
|
|
|
c->con.print("Fixing a misflagged item: \t");
|
2011-05-12 11:09:49 -06:00
|
|
|
confiscate = true;
|
|
|
|
}
|
|
|
|
else
|
2011-08-04 16:41:31 -06:00
|
|
|
{
|
2011-05-12 11:09:49 -06:00
|
|
|
continue;
|
2011-08-04 16:41:31 -06:00
|
|
|
}
|
2011-05-12 11:09:49 -06:00
|
|
|
}
|
|
|
|
|
2011-10-26 14:18:13 -06:00
|
|
|
std::string name = Items->getItemClass(item);
|
2011-05-12 11:09:49 -06:00
|
|
|
|
2011-10-26 14:18:13 -06:00
|
|
|
if (item->flags.rotten)
|
2011-04-10 13:25:41 -06:00
|
|
|
{
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.print("Confiscating a rotten item: \t");
|
2011-04-10 13:25:41 -06:00
|
|
|
confiscate = true;
|
|
|
|
}
|
2011-10-26 15:12:20 -06:00
|
|
|
else if (item->flags.on_ground)
|
2011-04-14 10:06:36 -06:00
|
|
|
{
|
2011-10-26 15:12:20 -06:00
|
|
|
int32_t type = item->getType();
|
|
|
|
if(type == Items::MEAT ||
|
|
|
|
type == Items::FISH ||
|
|
|
|
type == Items::VERMIN ||
|
|
|
|
type == Items::PET ||
|
|
|
|
type == Items::PLANT ||
|
|
|
|
type == Items::CHEESE ||
|
|
|
|
type == Items::FOOD
|
|
|
|
)
|
|
|
|
{
|
|
|
|
confiscate = true;
|
2011-11-02 19:40:37 -06:00
|
|
|
if(dump_scattered)
|
|
|
|
{
|
|
|
|
c->con.print("Dumping a dropped item: \t");
|
|
|
|
dump = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->con.print("Confiscating a dropped item: \t");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(dump_scattered)
|
|
|
|
{
|
|
|
|
c->con.print("Confiscating and dumping litter: \t");
|
|
|
|
confiscate = true;
|
|
|
|
dump = true;
|
2011-10-26 15:12:20 -06:00
|
|
|
}
|
2011-04-14 10:06:36 -06:00
|
|
|
}
|
2011-10-26 14:18:13 -06:00
|
|
|
else if (item->getWear() >= wear_dump_level)
|
2011-04-10 13:25:41 -06:00
|
|
|
{
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.print("Confiscating and dumping a worn item: \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)
|
|
|
|
{
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.print("Confiscating: \t");
|
2011-04-11 04:33:30 -06:00
|
|
|
confiscate = true;
|
|
|
|
}
|
|
|
|
|
2011-04-10 13:25:41 -06:00
|
|
|
if (confiscate)
|
|
|
|
{
|
2011-10-25 05:30:41 -06:00
|
|
|
std::string description;
|
2011-10-26 14:18:13 -06:00
|
|
|
item->getItemDescription(&description, 0);
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.print(
|
2011-08-05 07:05:57 -06:00
|
|
|
"0x%x %s (wear %d)",
|
2011-10-26 14:18:13 -06:00
|
|
|
item,
|
2011-10-25 05:30:41 -06:00
|
|
|
description.c_str(),
|
2011-10-26 14:18:13 -06:00
|
|
|
item->getWear()
|
2011-04-11 04:33:30 -06:00
|
|
|
);
|
|
|
|
|
2011-10-26 14:18:13 -06:00
|
|
|
int32_t owner = Items->getItemOwnerID(item);
|
2011-04-11 04:33:30 -06:00
|
|
|
int32_t owner_index = Creatures->FindIndexById(owner);
|
|
|
|
std::string info;
|
|
|
|
|
|
|
|
if (owner_index >= 0)
|
|
|
|
{
|
2011-12-02 02:56:40 -07:00
|
|
|
DFHack::df_unit * temp = Creatures->GetCreature(owner_index);
|
2011-09-18 05:49:10 -06:00
|
|
|
info = temp->name.first_name;
|
|
|
|
if (!temp->name.nick_name.empty())
|
|
|
|
info += std::string(" '") + temp->name.nick_name + "'";
|
2011-04-11 04:33:30 -06:00
|
|
|
info += " ";
|
2011-09-18 05:49:10 -06:00
|
|
|
info += Tran->TranslateName(&temp->name,false);
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.print(", owner %s", info.c_str());
|
2011-04-11 04:33:30 -06:00
|
|
|
}
|
|
|
|
|
2011-08-05 07:05:57 -06:00
|
|
|
if (!dry_run)
|
|
|
|
{
|
2011-10-26 14:18:13 -06:00
|
|
|
if (!Items->removeItemOwner(item, Creatures))
|
2011-08-05 07:05:57 -06:00
|
|
|
c->con.print("(unsuccessfully) ");
|
|
|
|
if (dump)
|
2011-10-26 14:18:13 -06:00
|
|
|
item->flags.dump = 1;
|
2011-08-05 07:05:57 -06:00
|
|
|
}
|
2011-08-04 16:41:31 -06:00
|
|
|
c->con.print("\n");
|
2011-04-10 13:25:41 -06:00
|
|
|
}
|
2011-04-11 04:33:30 -06:00
|
|
|
}
|
2011-08-04 21:02:36 -06:00
|
|
|
c->Resume();
|
2011-08-04 16:41:31 -06:00
|
|
|
return CR_OK;
|
2011-04-10 13:25:41 -06:00
|
|
|
}
|