2012-01-21 16:54:57 -07:00
|
|
|
// (un)designate matching plants for gathering/cutting
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "TileTypes.h"
|
2014-05-04 05:43:52 -06:00
|
|
|
|
2012-01-07 22:31:23 -07:00
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/map_block.h"
|
|
|
|
#include "df/tile_dig_designation.h"
|
|
|
|
#include "df/plant_raw.h"
|
2012-12-06 15:46:59 -07:00
|
|
|
#include "df/plant.h"
|
2014-05-04 05:43:52 -06:00
|
|
|
|
|
|
|
#include "modules/Maps.h"
|
2012-01-07 22:31:23 -07:00
|
|
|
#include <set>
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2012-01-07 22:31:23 -07:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::set;
|
|
|
|
using namespace DFHack;
|
2012-01-21 16:54:57 -07:00
|
|
|
using namespace df::enums;
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
DFHACK_PLUGIN("getplants");
|
|
|
|
REQUIRE_GLOBAL(world);
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result df_getplants (color_ostream &out, vector <string> & parameters)
|
2011-10-06 19:53:58 -06:00
|
|
|
{
|
2012-01-21 16:54:57 -07:00
|
|
|
string plantMatStr = "";
|
|
|
|
set<int> plantIDs;
|
|
|
|
set<string> plantNames;
|
2012-02-22 14:31:34 -07:00
|
|
|
bool deselect = false, exclude = false, treesonly = false, shrubsonly = false, all = false;
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2012-01-21 16:54:57 -07:00
|
|
|
int count = 0;
|
|
|
|
for (size_t i = 0; i < parameters.size(); i++)
|
|
|
|
{
|
|
|
|
if(parameters[i] == "help" || parameters[i] == "?")
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
else if(parameters[i] == "-t")
|
|
|
|
treesonly = true;
|
|
|
|
else if(parameters[i] == "-s")
|
|
|
|
shrubsonly = true;
|
|
|
|
else if(parameters[i] == "-c")
|
|
|
|
deselect = true;
|
|
|
|
else if(parameters[i] == "-x")
|
|
|
|
exclude = true;
|
2012-02-22 14:31:34 -07:00
|
|
|
else if(parameters[i] == "-a")
|
|
|
|
all = true;
|
2012-01-21 16:54:57 -07:00
|
|
|
else
|
|
|
|
plantNames.insert(parameters[i]);
|
|
|
|
}
|
|
|
|
if (treesonly && shrubsonly)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Cannot specify both -t and -s at the same time!\n");
|
2012-01-21 16:54:57 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2012-02-22 14:31:34 -07:00
|
|
|
if (all && exclude)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Cannot specify both -a and -x at the same time!\n");
|
2012-02-22 14:31:34 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
if (all && plantNames.size())
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Cannot specify -a along with plant IDs!\n");
|
2012-02-22 14:31:34 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
CoreSuspender suspend;
|
2012-01-07 22:31:23 -07:00
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < world->raws.plants.all.size(); i++)
|
2012-01-21 16:54:57 -07:00
|
|
|
{
|
|
|
|
df::plant_raw *plant = world->raws.plants.all[i];
|
2012-02-22 14:31:34 -07:00
|
|
|
if (all)
|
|
|
|
plantIDs.insert(i);
|
|
|
|
else if (plantNames.find(plant->id) != plantNames.end())
|
2012-01-21 16:54:57 -07:00
|
|
|
{
|
|
|
|
plantNames.erase(plant->id);
|
|
|
|
plantIDs.insert(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (plantNames.size() > 0)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Invalid plant ID(s):");
|
2012-01-21 16:54:57 -07:00
|
|
|
for (set<string>::const_iterator it = plantNames.begin(); it != plantNames.end(); it++)
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr(" %s", it->c_str());
|
|
|
|
out.printerr("\n");
|
2012-01-21 16:54:57 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2012-01-21 16:54:57 -07:00
|
|
|
if (plantIDs.size() == 0)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("Valid plant IDs:\n");
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < world->raws.plants.all.size(); i++)
|
2012-01-21 16:54:57 -07:00
|
|
|
{
|
|
|
|
df::plant_raw *plant = world->raws.plants.all[i];
|
|
|
|
if (plant->flags.is_set(plant_raw_flags::GRASS))
|
|
|
|
continue;
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("* (%s) %s - %s\n", plant->flags.is_set(plant_raw_flags::TREE) ? "tree" : "shrub", plant->id.c_str(), plant->name.c_str());
|
2012-01-21 16:54:57 -07:00
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2011-10-06 19:53:58 -06:00
|
|
|
|
2012-01-21 16:54:57 -07:00
|
|
|
count = 0;
|
2014-08-06 08:31:42 -06:00
|
|
|
for (size_t i = 0; i < world->plants.all.size(); i++)
|
2012-01-21 16:54:57 -07:00
|
|
|
{
|
2014-08-06 08:31:42 -06:00
|
|
|
const df::plant *plant = world->plants.all[i];
|
|
|
|
df::map_block *cur = Maps::getTileBlock(plant->pos);
|
2012-01-21 16:54:57 -07:00
|
|
|
bool dirty = false;
|
2014-08-06 08:31:42 -06:00
|
|
|
|
|
|
|
int x = plant->pos.x % 16;
|
|
|
|
int y = plant->pos.y % 16;
|
|
|
|
if (plantIDs.find(plant->material) != plantIDs.end())
|
2012-01-21 16:54:57 -07:00
|
|
|
{
|
2014-08-06 08:31:42 -06:00
|
|
|
if (exclude)
|
2012-01-21 16:54:57 -07:00
|
|
|
continue;
|
2014-08-06 08:31:42 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!exclude)
|
2012-01-21 16:54:57 -07:00
|
|
|
continue;
|
2014-08-06 08:31:42 -06:00
|
|
|
}
|
|
|
|
df::tiletype_shape shape = tileShape(cur->tiletype[x][y]);
|
|
|
|
df::tiletype_material material = tileMaterial(cur->tiletype[x][y]);
|
|
|
|
df::tiletype_special special = tileSpecial(cur->tiletype[x][y]);
|
|
|
|
if (plant->flags.bits.is_shrub && (treesonly || !(shape == tiletype_shape::SHRUB && special != tiletype_special::DEAD)))
|
|
|
|
continue;
|
|
|
|
if (!plant->flags.bits.is_shrub && (shrubsonly || !(material == tiletype_material::TREE)))
|
|
|
|
continue;
|
|
|
|
if (cur->designation[x][y].bits.hidden)
|
|
|
|
continue;
|
|
|
|
if (deselect && cur->designation[x][y].bits.dig == tile_dig_designation::Default)
|
|
|
|
{
|
|
|
|
cur->designation[x][y].bits.dig = tile_dig_designation::No;
|
|
|
|
dirty = true;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
if (!deselect && cur->designation[x][y].bits.dig == tile_dig_designation::No)
|
|
|
|
{
|
|
|
|
cur->designation[x][y].bits.dig = tile_dig_designation::Default;
|
|
|
|
dirty = true;
|
|
|
|
++count;
|
2012-01-21 16:54:57 -07:00
|
|
|
}
|
|
|
|
if (dirty)
|
2012-02-15 13:35:44 -07:00
|
|
|
cur->flags.bits.designated = true;
|
2012-01-21 16:54:57 -07:00
|
|
|
}
|
|
|
|
if (count)
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("Updated %d plant designations.\n", count);
|
2012-01-21 16:54:57 -07:00
|
|
|
return CR_OK;
|
2011-10-06 19:53:58 -06:00
|
|
|
}
|
2012-01-07 22:31:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCommand> &commands)
|
2012-01-07 22:31:23 -07:00
|
|
|
{
|
2012-01-28 05:03:56 -07:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"getplants", "Cut down all of the specified trees or gather specified shrubs",
|
|
|
|
df_getplants, false,
|
|
|
|
" Specify the types of trees to cut down and/or shrubs to gather by their\n"
|
|
|
|
" plant IDs, separated by spaces.\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -t - Select trees only (exclude shrubs)\n"
|
|
|
|
" -s - Select shrubs only (exclude trees)\n"
|
|
|
|
" -c - Clear designations instead of setting them\n"
|
|
|
|
" -x - Apply selected action to all plants except those specified\n"
|
2012-02-22 14:31:34 -07:00
|
|
|
" -a - Select every type of plant (obeys -t/-s)\n"
|
2012-01-28 05:03:56 -07:00
|
|
|
"Specifying both -t and -s will have no effect.\n"
|
2014-08-06 08:31:42 -06:00
|
|
|
"If no plant IDs are specified, all valid plant IDs will be listed.\n"
|
2012-01-28 05:03:56 -07:00
|
|
|
));
|
2012-01-21 16:54:57 -07:00
|
|
|
return CR_OK;
|
2012-01-07 22:31:23 -07:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
2012-01-07 22:31:23 -07:00
|
|
|
{
|
2012-01-21 16:54:57 -07:00
|
|
|
return CR_OK;
|
2014-08-06 08:31:42 -06:00
|
|
|
}
|