Petr Mrázek 2012-01-08 16:17:28 +01:00
commit 3a8bc4dbb5
4 changed files with 242 additions and 331 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++)
{
for(uint32_t y = 0; y< y_max;y++)
{
for(uint32_t z = 0; z< z_max;z++)
// Invoked from clean(), already suspended
int num_blocks = 0, blocks_total = world->map.map_blocks.size();
for (int i = 0; i < blocks_total; i++)
{
df_block * block = Mapz->getBlock(x,y,z);
if(block)
{
blocks_total ++;
df::map_block *block = world->map.map_blocks[i];
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++)
for(int x = 0; x < 16; x++)
{
for(int y = 0; y < 16; y++)
{
block->occupancy[i][j].bits.arrow_color = 0;
block->occupancy[i][j].bits.broken_arrows_variant = 0;
block->occupancy[x][y].bits.arrow_color = 0;
block->occupancy[x][y].bits.arrow_variant = 0;
}
}
for(uint32_t i = 0; i < splatter.size(); i++)
for (int j = 0; j < block->block_events.size(); j++)
{
DFHack::t_spattervein * vein = splatter[i];
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
&& vein->mat1 == DFHack::Materials::WATER
&& vein->matter_state == DFHack::state_powder)
&& spatter->mat_type == DFHack::Materials::WATER
&& spatter->mat_state == df::matter_state::Powder)
continue;
// filter mud
if(!mud
&& vein->mat1 == DFHack::Materials::MUD
&& vein->matter_state == DFHack::state_solid)
&& spatter->mat_type == DFHack::Materials::MUD
&& spatter->mat_state == df::matter_state::Solid)
continue;
Mapz->RemoveBlockEvent(x,y,z,(t_virtual *) vein);
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,30 +72,19 @@ 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)
@ -135,27 +94,19 @@ command_result cleanitems (Core * c)
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)
@ -163,33 +114,40 @@ command_result cleanunits (Core * c)
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;
}
@ -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;
}

@ -1,76 +1,57 @@
// Dry Buckets : Remove all "water" objects from buckets scattered around the fortress
#include <iostream>
#include <iomanip>
#include <sstream>
#include <climits>
#include <vector>
#include <set>
using namespace std;
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <vector>
#include <string>
#include <algorithm>
#include <modules/Items.h>
#include <DataDefs.h>
#include "df/world.h"
#include "df/item.h"
#include "modules/Materials.h"
using std::string;
using std::vector;
using namespace DFHack;
DFhackCExport command_result df_drybuckets (Core * c, vector <string> & parameters);
using df::global::world;
DFhackCExport const char * plugin_name ( void )
DFhackCExport command_result df_drybuckets (Core * c, vector <string> & parameters)
{
return "drybuckets";
}
if (!parameters.empty())
return CR_WRONG_USAGE;
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("drybuckets", "Removes water from buckets.", df_drybuckets));
return CR_OK;
}
CoreSuspender suspend(c);
DFhackCExport command_result plugin_shutdown ( Core * c )
int dried_total = 0;
for (int i = 0; i < world->items.all.size(); i++)
{
df::item *item = world->items.all[i];
if ((item->getType() == df::item_type::LIQUID_MISC) && (item->getMaterial() == DFHack::Materials::WATER))
{
item->flags.bits.garbage_colect = 1;
dried_total++;
}
}
if (dried_total)
c->con.print("Done. %d buckets of water marked for emptying.\n", dried_total);
return CR_OK;
}
DFhackCExport command_result df_drybuckets (Core * c, vector <string> & parameters)
{
if(parameters.size() > 0)
{
string & p = parameters[0];
if(p == "?" || p == "help")
DFhackCExport const char * plugin_name ( void )
{
c->con.print("This utility removes all objects of type LIQUID_MISC:NONE and material WATER:NONE - that is, water stored in buckets.\n");
return CR_OK;
}
return "drybuckets";
}
c->Suspend();
DFHack::Items * Items = c->getItems();
vector <df_item *> p_items;
if(!Items->readItemVector(p_items))
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
{
c->con.printerr("Can't access the item vector.\n");
c->Resume();
return CR_FAILURE;
commands.clear();
commands.push_back(PluginCommand("drybuckets", "Removes water from buckets.", df_drybuckets));
return CR_OK;
}
std::size_t numItems = p_items.size();
int dried_total = 0;
for (std::size_t i = 0; i < numItems; i++)
{
df_item *item = p_items[i];
if ((item->getType() == Items::LIQUID_MISC) && (item->getMaterial() == Materials::WATER))
DFhackCExport command_result plugin_shutdown ( Core * c )
{
item->flags.garbage_colect = 1;
dried_total++;
}
}
c->Resume();
c->con.print("Done. %d buckets of water emptied.\n", dried_total);
return CR_OK;
}

@ -6,21 +6,17 @@
#include <PluginManager.h>
#include <DataDefs.h>
#include <df/world.h>
#include <df/historical_entity.h>
#include <df/entity_raw.h>
#include <df/creature_raw.h>
#include <df/caste_raw.h>
#include "df/world.h"
#include "df/historical_entity.h"
#include "df/entity_raw.h"
#include "df/creature_raw.h"
#include "df/caste_raw.h"
using std::string;
using std::vector;
using namespace DFHack;
using df::global::world;
using df::historical_entity;
using df::entity_raw_flags;
using df::creature_raw;
using df::creature_raw_flags;
command_result df_fixwagons (Core *c, vector<string> &parameters)
{
@ -29,11 +25,11 @@ command_result df_fixwagons (Core *c, vector<string> &parameters)
CoreSuspender suspend(c);
int32_t wagon_creature = -1, wagon_puller_creature = -1;
creature_raw *wagon, *wagon_puller;
df::creature_raw *wagon, *wagon_puller;
for (int i = 0; i < world->raws.creatures.all.size(); i++)
{
creature_raw *cr = world->raws.creatures.all[i];
if (cr->flags.is_set(creature_raw_flags::EQUIPMENT_WAGON) && (wagon_creature == -1))
df::creature_raw *cr = world->raws.creatures.all[i];
if (cr->flags.is_set(df::creature_raw_flags::EQUIPMENT_WAGON) && (wagon_creature == -1))
{
wagon = cr;
wagon_creature = i;
@ -58,8 +54,8 @@ command_result df_fixwagons (Core *c, vector<string> &parameters)
for (int i = 0; i < world->entities.all.size(); i++)
{
bool updated = false;
historical_entity *ent = world->entities.all[i];
if (!ent->entity_raw->flags.is_set(entity_raw_flags::COMMON_DOMESTIC_PULL))
df::historical_entity *ent = world->entities.all[i];
if (!ent->entity_raw->flags.is_set(df::entity_raw_flags::COMMON_DOMESTIC_PULL))
continue;
if (ent->resources.animals.wagon_races.size() == 0)
{

@ -1,56 +1,34 @@
// Designate all matching plants for gathering/cutting
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stddef.h>
#include <assert.h>
#include <string.h>
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <modules/Maps.h>
#include <modules/Materials.h>
#include <modules/Vegetation.h>
#include <TileTypes.h>
using namespace std;
using namespace DFHack;
DFhackCExport command_result df_getplants (Core * c, vector <string> & parameters);
#include <DataDefs.h>
#include <TileTypes.h>
#include "df/world.h"
#include "df/map_block.h"
#include "df/tile_dig_designation.h"
#include "df/plant_raw.h"
DFhackCExport const char * plugin_name ( void )
{
return "getplants";
}
#include "modules/Vegetation.h"
#include <set>
DFhackCExport command_result plugin_init ( Core * c, vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("getplants", "Cut down all of the specified trees or gather all of the specified shrubs", df_getplants));
return CR_OK;
}
using std::string;
using std::vector;
using std::set;
using namespace DFHack;
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}
using df::global::world;
DFhackCExport command_result df_getplants (Core * c, vector <string> & parameters)
{
uint32_t x_max,y_max,z_max;
designations40d designations;
tiletypes40d tiles;
t_blockflags blockflags;
string plantMatStr = "";
set<int> plantIDs;
set<string> plantNames;
bool deselect = false, exclude = false, treesonly = false, shrubsonly = false;
bool dirty = false;
int count = 0;
for (size_t i = 0; i < parameters.size(); i++)
{
@ -77,16 +55,16 @@ DFhackCExport command_result df_getplants (Core * c, vector <string> & parameter
exclude = true;
else plantNames.insert(parameters[i]);
}
c->Suspend();
Materials *mats = c->getMaterials();
for (vector<df_plant_type *>::const_iterator it = mats->df_organic->begin(); it != mats->df_organic->end(); it++)
CoreSuspender suspend(c);
for (int i = 0; i < world->raws.plants.all.size(); i++)
{
df_plant_type &plant = **it;
if (plantNames.find(plant.ID) != plantNames.end())
df::plant_raw *plant = world->raws.plants.all[i];
if (plantNames.find(plant->id) != plantNames.end())
{
plantNames.erase(plant.ID);
plantIDs.insert(it - mats->df_organic->begin());
plantNames.erase(plant->id);
plantIDs.insert(i);
}
}
if (plantNames.size() > 0)
@ -95,58 +73,33 @@ DFhackCExport command_result df_getplants (Core * c, vector <string> & parameter
for (set<string>::const_iterator it = plantNames.begin(); it != plantNames.end(); it++)
c->con.printerr(" %s", it->c_str());
c->con.printerr("\n");
c->Resume();
return CR_FAILURE;
}
if (plantIDs.size() == 0)
{
c->con.print("Valid plant IDs:\n");
for (vector<df_plant_type *>::const_iterator it = mats->df_organic->begin(); it != mats->df_organic->end(); it++)
for (int i = 0; i < world->raws.plants.all.size(); i++)
{
df_plant_type &plant = **it;
if (plant.flags.is_set(PLANT_GRASS))
df::plant_raw *plant = world->raws.plants.all[i];
if (plant->flags.is_set(df::plant_raw_flags::GRASS))
continue;
c->con.print("* (%s) %s - %s\n", plant.flags.is_set(PLANT_TREE) ? "tree" : "shrub", plant.ID.c_str(), plant.name.c_str());
c->con.print("* (%s) %s - %s\n", plant->flags.is_set(df::plant_raw_flags::TREE) ? "tree" : "shrub", plant->id.c_str(), plant->name.c_str());
}
c->Resume();
return CR_OK;
}
Maps *maps = c->getMaps();
// init the map
if (!maps->Start())
{
c->con.printerr("Can't init map.\n");
c->Resume();
return CR_FAILURE;
}
maps->getSize(x_max,y_max,z_max);
// walk the map
for (uint32_t x = 0; x < x_max; x++)
{
for (uint32_t y = 0; y < y_max; y++)
{
for (uint32_t z = 0; z < z_max; z++)
{
if (maps->getBlock(x,y,z))
{
dirty = false;
maps->ReadDesignations(x,y,z, &designations);
maps->ReadTileTypes(x,y,z, &tiles);
maps->ReadBlockFlags(x,y,z, blockflags);
vector<df_plant *> *plants;
if (maps->ReadVegetation(x, y, z, plants))
int count = 0;
for (int i = 0; i < world->map.map_blocks.size(); i++)
{
for (vector<df_plant *>::const_iterator it = plants->begin(); it != plants->end(); it++)
df::map_block *cur = world->map.map_blocks[i];
bool dirty = false;
for (int j = 0; j < cur->plants.size(); j++)
{
const df_plant &plant = **it;
uint32_t tx = plant.x % 16;
uint32_t ty = plant.y % 16;
if (plantIDs.find(plant.material) != plantIDs.end())
const df_plant *plant = (df_plant *)cur->plants[i];
int x = plant->x % 16;
int y = plant->y % 16;
if (plantIDs.find(plant->material) != plantIDs.end())
{
if (exclude)
continue;
@ -156,42 +109,47 @@ DFhackCExport command_result df_getplants (Core * c, vector <string> & parameter
if (!exclude)
continue;
}
TileShape shape = tileShape(tiles[tx][ty]);
if (plant.is_shrub && (treesonly || shape != SHRUB_OK))
TileShape shape = tileShape(cur->tiletype[x][y]);
if (plant->is_shrub && (treesonly || shape != SHRUB_OK))
continue;
if (!plant.is_shrub && (shrubsonly || (shape != TREE_OK && shape != TREE_DEAD)))
if (!plant->is_shrub && (shrubsonly || (shape != TREE_OK && shape != TREE_DEAD)))
continue;
if (designations[tx][ty].bits.hidden)
if (cur->designation[x][y].bits.hidden)
continue;
if (deselect && designations[tx][ty].bits.dig != designation_no)
if (deselect && cur->designation[x][y].bits.dig != df::tile_dig_designation::No)
{
designations[tx][ty].bits.dig = designation_no;
cur->designation[x][y].bits.dig = df::tile_dig_designation::No;
dirty = true;
++count;
}
if (!deselect && designations[tx][ty].bits.dig != designation_default)
if (!deselect && cur->designation[x][y].bits.dig != df::tile_dig_designation::Default)
{
designations[tx][ty].bits.dig = designation_default;
cur->designation[x][y].bits.dig = df::tile_dig_designation::Default;
dirty = true;
++count;
}
}
}
// If anything was changed, write it all.
if (dirty)
{
blockflags.bits.designated = 1;
maps->WriteDesignations(x,y,z, &designations);
maps->WriteBlockFlags(x,y,z, blockflags);
dirty = false;
}
cur->flags.set(df::block_flags::Designated);
}
if (count)
c->con.print("Updated %d plant designations.\n", count);
return CR_OK;
}
DFhackCExport const char * plugin_name ( void )
{
return "getplants";
}
DFhackCExport command_result plugin_init ( Core * c, vector <PluginCommand> &commands)
{
commands.clear();
commands.push_back(PluginCommand("getplants", "Cut down all of the specified trees or gather all of the specified shrubs", df_getplants));
return CR_OK;
}
c->Resume();
if (count)
c->con.print("Updated %d plant designations.\n", count);
DFhackCExport command_result plugin_shutdown ( Core * c )
{
return CR_OK;
}