Cleanup "cleaners" plugin, minimize reliance on modules

develop
Quietust 2012-01-07 23:04:31 -06:00
parent 72016d9188
commit 2ccb15524b
1 changed files with 122 additions and 146 deletions

@ -2,99 +2,69 @@
#include <Console.h> #include <Console.h>
#include <Export.h> #include <Export.h>
#include <PluginManager.h> #include <PluginManager.h>
#include <modules/Maps.h>
#include <modules/Items.h>
#include <modules/Units.h>
#include <modules/Gui.h>
using namespace DFHack; #include "DataDefs.h"
#include "df/world.h"
#include "df/map_block.h"
#include "df/block_square_event.h"
#include "df/block_square_event_material_spatterst.h"
#include "df/item_actual.h"
#include "df/unit.h"
#include "df/matter_state.h"
#include "df/cursor.h"
#include "modules/Materials.h"
#include <vector>
#include <string>
#include <string.h>
using std::vector; using std::vector;
using std::string; using std::string;
using namespace DFHack;
DFhackCExport command_result clean (Core * c, vector <string> & parameters); using df::global::world;
DFhackCExport command_result spotclean (Core * c, vector <string> & parameters); using df::global::cursor;
DFhackCExport const char * plugin_name ( void )
{
return "cleaners";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("clean","Removes contaminants from map tiles, items and creatures.",clean));
commands.push_back(PluginCommand("spotclean","Cleans map tile under cursor.",spotclean,cursor_hotkey));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
command_result cleanmap (Core * c, bool snow, bool mud) command_result cleanmap (Core * c, bool snow, bool mud)
{ {
const uint32_t water_idx = 6; // Invoked from clean(), already suspended
const uint32_t mud_idx = 12; int num_blocks = 0, blocks_total = world->map.map_blocks.size();
vector<DFHack::t_spattervein *> splatter; for (int i = 0; i < blocks_total; i++)
DFHack::Maps *Mapz = c->getMaps();
// init the map
if(!Mapz->Start())
{
c->con << "Can't init map." << std::endl;
c->Resume();
return CR_FAILURE;
}
uint32_t x_max,y_max,z_max;
Mapz->getSize(x_max,y_max,z_max);
int num_blocks = 0;
int blocks_total = 0;
// walk the map
for(uint32_t x = 0; x< x_max;x++)
{ {
for(uint32_t y = 0; y< y_max;y++) df::map_block *block = world->map.map_blocks[i];
bool cleaned = false;
for(int x = 0; x < 16; x++)
{ {
for(uint32_t z = 0; z< z_max;z++) for(int y = 0; y < 16; y++)
{ {
df_block * block = Mapz->getBlock(x,y,z); block->occupancy[x][y].bits.arrow_color = 0;
if(block) block->occupancy[x][y].bits.arrow_variant = 0;
{
blocks_total ++;
bool cleaned = false;
Mapz->SortBlockEvents(x,y,z,0,0,&splatter);
for(int i = 0; i < 16; i++)
for(int j = 0; j < 16; j++)
{
block->occupancy[i][j].bits.arrow_color = 0;
block->occupancy[i][j].bits.broken_arrows_variant = 0;
}
for(uint32_t i = 0; i < splatter.size(); i++)
{
DFHack::t_spattervein * vein = splatter[i];
// filter snow
if(!snow
&& vein->mat1 == DFHack::Materials::WATER
&& vein->matter_state == DFHack::state_powder)
continue;
// filter mud
if(!mud
&& vein->mat1 == DFHack::Materials::MUD
&& vein->matter_state == DFHack::state_solid)
continue;
Mapz->RemoveBlockEvent(x,y,z,(t_virtual *) vein);
cleaned = true;
}
num_blocks += cleaned;
}
} }
} }
for (int j = 0; j < block->block_events.size(); j++)
{
df::block_square_event *evt = block->block_events[j];
if (evt->getType() != df::block_square_event_type::material_spatter)
continue;
// type verified - recast to subclass
df::block_square_event_material_spatterst *spatter = (df::block_square_event_material_spatterst *)evt;
// filter snow
if(!snow
&& spatter->mat_type == DFHack::Materials::WATER
&& spatter->mat_state == df::matter_state::Powder)
continue;
// filter mud
if(!mud
&& spatter->mat_type == DFHack::Materials::MUD
&& spatter->mat_state == df::matter_state::Solid)
continue;
delete evt;
block->block_events.erase(block->block_events.begin() + j);
j--;
cleaned = true;
}
num_blocks += cleaned;
} }
if(num_blocks) if(num_blocks)
c->con.print("Cleaned %d of %d map blocks.\n", num_blocks, blocks_total); c->con.print("Cleaned %d of %d map blocks.\n", num_blocks, blocks_total);
return CR_OK; return CR_OK;
@ -102,94 +72,82 @@ command_result cleanmap (Core * c, bool snow, bool mud)
command_result cleanitems (Core * c) command_result cleanitems (Core * c)
{ {
DFHack::Items * Items = c->getItems(); // Invoked from clean(), already suspended
vector <df_item*> p_items;
if(!Items->readItemVector(p_items))
{
c->con.printerr("Can't access the item vector.\n");
c->Resume();
return CR_FAILURE;
}
std::size_t numItems = p_items.size();
int cleaned_items = 0, cleaned_total = 0; int cleaned_items = 0, cleaned_total = 0;
for (std::size_t i = 0; i < numItems; i++) for (int i = 0; i < world->items.all.size(); i++)
{ {
df_item * itm = p_items[i]; // currently, all item classes extend item_actual, so this should be safe
if(!itm->contaminants) df::item_actual *item = (df::item_actual *)world->items.all[i];
continue; if (item->contaminants && item->contaminants->size())
if (itm->contaminants->size())
{ {
for(int j = 0; j < itm->contaminants->size(); j++) for (int j = 0; j < item->contaminants->size(); j++)
delete itm->contaminants->at(j); delete item->contaminants->at(j);
cleaned_items++; cleaned_items++;
cleaned_total += itm->contaminants->size(); cleaned_total += item->contaminants->size();
itm->contaminants->clear(); item->contaminants->clear();
} }
} }
if(cleaned_total) if (cleaned_total)
c->con.print("Removed %d contaminants from %d items.\n", cleaned_total, cleaned_items); c->con.print("Removed %d contaminants from %d items.\n", cleaned_total, cleaned_items);
return CR_OK; return CR_OK;
} }
command_result cleanunits (Core * c) command_result cleanunits (Core * c)
{ {
DFHack::Units * Creatures = c->getUnits(); // Invoked from clean(), already suspended
int num_units = world->units.all.size();
uint32_t num_creatures;
if (!Creatures->Start(num_creatures))
{
c->con.printerr("Can't read unit list!\n");
c->Resume();
return CR_FAILURE;
}
int cleaned_units = 0, cleaned_total = 0; int cleaned_units = 0, cleaned_total = 0;
for (std::size_t i = 0; i < num_creatures; i++) for (int i = 0; i < num_units; i++)
{ {
df_unit *unit = Creatures->creatures->at(i); df::unit *unit = world->units.all[i];
int num = unit->contaminants.size(); if (unit->body.spatters.size())
if (num)
{ {
for(int j = 0; j < unit->contaminants.size(); j++) for (int j = 0; j < unit->body.spatters.size(); j++)
delete unit->contaminants.at(j); delete unit->body.spatters[j];
cleaned_units++; cleaned_units++;
cleaned_total += num; cleaned_total += unit->body.spatters.size();
unit->contaminants.clear(); unit->body.spatters.clear();
} }
} }
if(cleaned_total) if (cleaned_total)
c->con.print("Removed %d contaminants from %d creatures.\n", cleaned_total, cleaned_units); c->con.print("Removed %d contaminants from %d creatures.\n", cleaned_total, cleaned_units);
return CR_OK; return CR_OK;
} }
// This is slightly different from what's in the Maps module - it takes tile coordinates rather than block coordinates
df::map_block *getBlock (int32_t x, int32_t y, int32_t z)
{
if ((x < 0) || (y < 0) || (z < 0))
return NULL;
if ((x >= world->map.x_count) || (y >= world->map.y_count) || (z >= world->map.z_count))
return NULL;
// block_index isn't declared correctly - needs one more level of indirection
return ((df::map_block ****)world->map.block_index)[x >> 4][y >> 4][z];
}
DFhackCExport command_result spotclean (Core * c, vector <string> & parameters) DFhackCExport command_result spotclean (Core * c, vector <string> & parameters)
{ {
// HOTKEY COMMAND: CORE ALREADY SUSPENDED // HOTKEY COMMAND: CORE ALREADY SUSPENDED
vector<DFHack::t_spattervein *> splatter; if (cursor->x == -30000)
DFHack::Maps *Mapz = c->getMaps();
DFHack::Gui *Gui = c->getGui();
// init the map
if(!Mapz->Start())
{ {
c->con.printerr("Can't init map.\n"); c->con.printerr("The cursor is not active.\n");
return CR_FAILURE; return CR_FAILURE;
} }
int32_t cursorX, cursorY, cursorZ; df::map_block *block = getBlock(cursor->x, cursor->y, cursor->z);
Gui->getCursorCoords(cursorX,cursorY,cursorZ); if (block == NULL)
if(cursorX == -30000)
{ {
c->con.printerr("The cursor is not active.\n"); c->con.printerr("Invalid map block selected!\n");
return CR_FAILURE; return CR_FAILURE;
} }
int32_t blockX = cursorX / 16, blockY = cursorY / 16;
int32_t tileX = cursorX % 16, tileY = cursorY % 16; for (int i = 0; i < block->block_events.size(); i++)
df_block *b = Mapz->getBlock(blockX,blockY,cursorZ);
vector <t_spattervein *> spatters;
Mapz->SortBlockEvents(blockX, blockY, cursorZ, 0,0, &spatters);
for(int i = 0; i < spatters.size(); i++)
{ {
spatters[i]->intensity[tileX][tileY] = 0; df::block_square_event *evt = block->block_events[i];
if (evt->getType() != df::block_square_event_type::material_spatter)
continue;
// type verified - recast to subclass
df::block_square_event_material_spatterst *spatter = (df::block_square_event_material_spatterst *)evt;
spatter->amount[cursor->x % 16][cursor->y % 16] = 0;
} }
return CR_OK; return CR_OK;
} }
@ -230,17 +188,17 @@ DFhackCExport command_result clean (Core * c, vector <string> & parameters)
if(help) if(help)
{ {
c->con.print("Removes contaminants from map tiles, items and creatures.\n" c->con.print("Removes contaminants from map tiles, items and creatures.\n"
"Options:\n" "Options:\n"
"map - clean the map tiles\n" "map - clean the map tiles\n"
"items - clean all items\n" "items - clean all items\n"
"units - clean all creatures\n" "units - clean all creatures\n"
"all - clean everything.\n" "all - clean everything.\n"
"More options for 'map':\n" "More options for 'map':\n"
"snow - also remove snow\n" "snow - also remove snow\n"
"mud - also remove mud\n" "mud - also remove mud\n"
"Example: clean all mud snow\n" "Example: clean all mud snow\n"
"This removes all spatter, including mud and snow from map tiles." "This removes all spatter, including mud and snow from map tiles."
); );
return CR_OK; return CR_OK;
} }
c->Suspend(); c->Suspend();
@ -253,3 +211,21 @@ DFhackCExport command_result clean (Core * c, vector <string> & parameters)
c->Resume(); c->Resume();
return CR_OK; return CR_OK;
} }
DFhackCExport const char * plugin_name ( void )
{
return "cleaners";
}
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("clean","Removes contaminants from map tiles, items and creatures.",clean));
commands.push_back(PluginCommand("spotclean","Cleans map tile under cursor.",spotclean,cursor_hotkey));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}