dfhack/plugins/cleanowned.cpp

171 lines
4.4 KiB
C++

/*
* Confiscates and dumps garbage owned by dwarfs.
*/
#include <sstream>
#include <climits>
#include <vector>
#include <set>
using namespace std;
2011-12-31 04:48:42 -07:00
#include "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include <vector>
#include <string>
#include "modules/Items.h"
#include "modules/Units.h"
#include "modules/Translation.h"
2012-01-16 21:12:58 -07:00
#include "DataDefs.h"
#include "df/world.h"
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("cleanowned");
REQUIRE_GLOBAL(world);
command_result df_cleanowned (color_ostream &out, vector <string> & parameters);
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
commands.push_back(PluginCommand(
2022-07-20 15:51:06 -06:00
"cleanowned",
"Confiscates and dumps garbage owned by dwarves.",
df_cleanowned));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
command_result df_cleanowned (color_ostream &out, vector <string> & parameters)
{
bool dump_scattered = false;
bool confiscate_all = false;
bool dry_run = false;
int wear_dump_level = 65536;
2011-08-04 21:02:36 -06:00
for(std::size_t i = 0; i < parameters.size(); i++)
{
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;
else
return CR_WRONG_USAGE;
}
CoreSuspender suspend;
if (!Translation::IsValid())
{
out.printerr("Translation data unavailable!\n");
return CR_FAILURE;
}
2012-01-16 21:12:58 -07:00
for (std::size_t i=0; i < world->items.all.size(); i++)
{
2012-01-16 21:12:58 -07:00
df::item * item = world->items.all[i];
bool confiscate = false;
bool dump = false;
if (!item->flags.bits.owned)
{
if (Items::getOwner(item))
{
out.print("Fixing a misflagged item: \t");
confiscate = true;
}
else
{
continue;
}
}
if (item->flags.bits.rotten)
{
out.print("Confiscating a rotten item: \t");
confiscate = true;
}
else if (item->flags.bits.on_ground)
{
df::item_type type = item->getType();
2012-11-16 14:33:36 -07:00
if(type == item_type::MEAT ||
type == item_type::FISH ||
type == item_type::VERMIN ||
type == item_type::PET ||
type == item_type::PLANT ||
type == item_type::CHEESE ||
type == item_type::FOOD
2011-10-26 15:12:20 -06:00
)
{
confiscate = true;
2011-11-02 19:40:37 -06:00
if(dump_scattered)
{
out.print("Dumping a dropped item: \t");
2011-11-02 19:40:37 -06:00
dump = true;
}
else
{
out.print("Confiscating a dropped item: \t");
2011-11-02 19:40:37 -06:00
}
}
else if(dump_scattered)
{
out.print("Confiscating and dumping litter: \t");
2011-11-02 19:40:37 -06:00
confiscate = true;
dump = true;
2011-10-26 15:12:20 -06:00
}
}
else if (item->getWear() >= wear_dump_level)
{
out.print("Confiscating and dumping a worn item: \t");
confiscate = true;
dump = true;
}
else if (confiscate_all)
{
out.print("Confiscating: \t");
confiscate = true;
}
if (confiscate)
{
2011-10-25 05:30:41 -06:00
std::string description;
item->getItemDescription(&description, 0);
out.print(
"[%d] %s (wear level %d)",
item->id,
2023-10-05 15:25:14 -06:00
DF2CONSOLE(description).c_str(),
item->getWear()
);
df::unit *owner = Items::getOwner(item);
if (owner)
2023-10-05 15:25:14 -06:00
out.print(", owner %s", DF2CONSOLE(Translation::TranslateName(&owner->name,false)).c_str());
2011-08-05 07:05:57 -06:00
if (!dry_run)
{
if (!Items::setOwner(item,NULL))
out.print("(unsuccessfully) ");
2011-08-05 07:05:57 -06:00
if (dump)
item->flags.bits.dump = 1;
2011-08-05 07:05:57 -06:00
}
out.print("\n");
}
}
return CR_OK;
}