Cleanup "getplants" plugin, minimize reliance on modules

develop
Quietust 2012-01-07 23:31:23 -06:00
parent c484d7ceae
commit fd2563d0d5
1 changed files with 78 additions and 120 deletions

@ -1,56 +1,34 @@
// Designate all matching plants for gathering/cutting // 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 "Core.h"
#include <Console.h> #include <Console.h>
#include <Export.h> #include <Export.h>
#include <PluginManager.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 ) #include "modules/Vegetation.h"
{ #include <set>
return "getplants";
}
DFhackCExport command_result plugin_init ( Core * c, vector <PluginCommand> &commands) using std::string;
{ using std::vector;
commands.clear(); using std::set;
commands.push_back(PluginCommand("getplants", "Cut down all of the specified trees or gather all of the specified shrubs", df_getplants)); using namespace DFHack;
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c ) using df::global::world;
{
return CR_OK;
}
DFhackCExport command_result df_getplants (Core * c, vector <string> & parameters) 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 = ""; string plantMatStr = "";
set<int> plantIDs; set<int> plantIDs;
set<string> plantNames; set<string> plantNames;
bool deselect = false, exclude = false, treesonly = false, shrubsonly = false; bool deselect = false, exclude = false, treesonly = false, shrubsonly = false;
bool dirty = false;
int count = 0; int count = 0;
for (size_t i = 0; i < parameters.size(); i++) 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; exclude = true;
else plantNames.insert(parameters[i]); else plantNames.insert(parameters[i]);
} }
c->Suspend();
Materials *mats = c->getMaterials(); CoreSuspender suspend(c);
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; df::plant_raw *plant = world->raws.plants.all[i];
if (plantNames.find(plant.ID) != plantNames.end()) if (plantNames.find(plant->id) != plantNames.end())
{ {
plantNames.erase(plant.ID); plantNames.erase(plant->id);
plantIDs.insert(it - mats->df_organic->begin()); plantIDs.insert(i);
} }
} }
if (plantNames.size() > 0) 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++) for (set<string>::const_iterator it = plantNames.begin(); it != plantNames.end(); it++)
c->con.printerr(" %s", it->c_str()); c->con.printerr(" %s", it->c_str());
c->con.printerr("\n"); c->con.printerr("\n");
c->Resume();
return CR_FAILURE; return CR_FAILURE;
} }
if (plantIDs.size() == 0) if (plantIDs.size() == 0)
{ {
c->con.print("Valid plant IDs:\n"); 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; df::plant_raw *plant = world->raws.plants.all[i];
if (plant.flags.is_set(PLANT_GRASS)) if (plant->flags.is_set(df::plant_raw_flags::GRASS))
continue; 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; return CR_OK;
} }
Maps *maps = c->getMaps(); int count = 0;
for (int i = 0; i < world->map.map_blocks.size(); i++)
// 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))
{ {
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; const df_plant *plant = (df_plant *)cur->plants[i];
uint32_t tx = plant.x % 16; int x = plant->x % 16;
uint32_t ty = plant.y % 16; int y = plant->y % 16;
if (plantIDs.find(plant.material) != plantIDs.end()) if (plantIDs.find(plant->material) != plantIDs.end())
{ {
if (exclude) if (exclude)
continue; continue;
@ -156,42 +109,47 @@ DFhackCExport command_result df_getplants (Core * c, vector <string> & parameter
if (!exclude) if (!exclude)
continue; continue;
} }
TileShape shape = tileShape(cur->tiletype[x][y]);
TileShape shape = tileShape(tiles[tx][ty]); if (plant->is_shrub && (treesonly || shape != SHRUB_OK))
if (plant.is_shrub && (treesonly || shape != SHRUB_OK))
continue; continue;
if (!plant.is_shrub && (shrubsonly || (shape != TREE_OK && shape != TREE_DEAD))) if (!plant->is_shrub && (shrubsonly || (shape != TREE_OK && shape != TREE_DEAD)))
continue; continue;
if (designations[tx][ty].bits.hidden) if (cur->designation[x][y].bits.hidden)
continue; 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; dirty = true;
++count; ++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; dirty = true;
++count; ++count;
} }
} }
}
// If anything was changed, write it all.
if (dirty) if (dirty)
{ cur->flags.set(df::block_flags::Designated);
blockflags.bits.designated = 1;
maps->WriteDesignations(x,y,z, &designations);
maps->WriteBlockFlags(x,y,z, blockflags);
dirty = false;
}
} }
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) DFhackCExport command_result plugin_shutdown ( Core * c )
c->con.print("Updated %d plant designations.\n", count); {
return CR_OK; return CR_OK;
} }