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 <Export.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::string;
using namespace DFHack;
DFhackCExport command_result clean (Core * c, vector <string> & parameters);
DFhackCExport command_result spotclean (Core * c, vector <string> & parameters);
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;
}
using df::global::world;
using df::global::cursor;
command_result cleanmap (Core * c, bool snow, bool mud)
{
const uint32_t water_idx = 6;
const uint32_t mud_idx = 12;
vector<DFHack::t_spattervein *> splatter;
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++)
// Invoked from clean(), already suspended
int num_blocks = 0, blocks_total = world->map.map_blocks.size();
for (int i = 0; i < blocks_total; i++)
{
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);
if(block)
{
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;
}
block->occupancy[x][y].bits.arrow_color = 0;
block->occupancy[x][y].bits.arrow_variant = 0;
}
}
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)
c->con.print("Cleaned %d of %d map blocks.\n", num_blocks, blocks_total);
return CR_OK;
@ -102,94 +72,82 @@ command_result cleanmap (Core * c, bool snow, bool mud)
command_result cleanitems (Core * c)
{
DFHack::Items * Items = c->getItems();
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();
// Invoked from clean(), already suspended
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];
if(!itm->contaminants)
continue;
if (itm->contaminants->size())
// currently, all item classes extend item_actual, so this should be safe
df::item_actual *item = (df::item_actual *)world->items.all[i];
if (item->contaminants && item->contaminants->size())
{
for(int j = 0; j < itm->contaminants->size(); j++)
delete itm->contaminants->at(j);
for (int j = 0; j < item->contaminants->size(); j++)
delete item->contaminants->at(j);
cleaned_items++;
cleaned_total += itm->contaminants->size();
itm->contaminants->clear();
cleaned_total += item->contaminants->size();
item->contaminants->clear();
}
}
if(cleaned_total)
if (cleaned_total)
c->con.print("Removed %d contaminants from %d items.\n", cleaned_total, cleaned_items);
return CR_OK;
}
command_result cleanunits (Core * c)
{
DFHack::Units * Creatures = c->getUnits();
uint32_t num_creatures;
if (!Creatures->Start(num_creatures))
{
c->con.printerr("Can't read unit list!\n");
c->Resume();
return CR_FAILURE;
}
// Invoked from clean(), already suspended
int num_units = world->units.all.size();
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);
int num = unit->contaminants.size();
if (num)
df::unit *unit = world->units.all[i];
if (unit->body.spatters.size())
{
for(int j = 0; j < unit->contaminants.size(); j++)
delete unit->contaminants.at(j);
for (int j = 0; j < unit->body.spatters.size(); j++)
delete unit->body.spatters[j];
cleaned_units++;
cleaned_total += num;
unit->contaminants.clear();
cleaned_total += unit->body.spatters.size();
unit->body.spatters.clear();
}
}
if(cleaned_total)
if (cleaned_total)
c->con.print("Removed %d contaminants from %d creatures.\n", cleaned_total, cleaned_units);
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)
{
// HOTKEY COMMAND: CORE ALREADY SUSPENDED
vector<DFHack::t_spattervein *> splatter;
DFHack::Maps *Mapz = c->getMaps();
DFHack::Gui *Gui = c->getGui();
// init the map
if(!Mapz->Start())
if (cursor->x == -30000)
{
c->con.printerr("Can't init map.\n");
c->con.printerr("The cursor is not active.\n");
return CR_FAILURE;
}
int32_t cursorX, cursorY, cursorZ;
Gui->getCursorCoords(cursorX,cursorY,cursorZ);
if(cursorX == -30000)
df::map_block *block = getBlock(cursor->x, cursor->y, cursor->z);
if (block == NULL)
{
c->con.printerr("The cursor is not active.\n");
c->con.printerr("Invalid map block selected!\n");
return CR_FAILURE;
}
int32_t blockX = cursorX / 16, blockY = cursorY / 16;
int32_t tileX = cursorX % 16, tileY = cursorY % 16;
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++)
for (int i = 0; i < block->block_events.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;
}
@ -230,17 +188,17 @@ DFhackCExport command_result clean (Core * c, vector <string> & parameters)
if(help)
{
c->con.print("Removes contaminants from map tiles, items and creatures.\n"
"Options:\n"
"map - clean the map tiles\n"
"items - clean all items\n"
"units - clean all creatures\n"
"all - clean everything.\n"
"More options for 'map':\n"
"snow - also remove snow\n"
"mud - also remove mud\n"
"Example: clean all mud snow\n"
"This removes all spatter, including mud and snow from map tiles."
);
"Options:\n"
"map - clean the map tiles\n"
"items - clean all items\n"
"units - clean all creatures\n"
"all - clean everything.\n"
"More options for 'map':\n"
"snow - also remove snow\n"
"mud - also remove mud\n"
"Example: clean all mud snow\n"
"This removes all spatter, including mud and snow from map tiles."
);
return CR_OK;
}
c->Suspend();
@ -253,3 +211,21 @@ DFhackCExport command_result clean (Core * c, vector <string> & parameters)
c->Resume();
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;
}