Petr Mrázek 2011-12-30 03:08:20 +01:00
commit 5c4ba8c1df
1 changed files with 95 additions and 21 deletions

@ -2,6 +2,7 @@
// Flood-fill from cursor or fill entire map. // Flood-fill from cursor or fill entire map.
#include <ctype.h> //For toupper(). #include <ctype.h> //For toupper().
#include <algorithm> //for min().
#include <map> #include <map>
#include <vector> #include <vector>
#include <dfhack/Core.h> #include <dfhack/Core.h>
@ -15,8 +16,22 @@ using std::stack;
using MapExtras::MapCache; using MapExtras::MapCache;
using namespace DFHack; using namespace DFHack;
//Function pointer type for whole-map tile checks.
typedef void (*checkTile)(DFHack::DFCoord, MapExtras::MapCache *);
//Forward Declarations for Commands
DFhackCExport command_result filltraffic(DFHack::Core * c, std::vector<std::string> & params); DFhackCExport command_result filltraffic(DFHack::Core * c, std::vector<std::string> & params);
DFhackCExport command_result tiletraffic(DFHack::Core * c, std::vector<std::string> & params); DFhackCExport command_result alltraffic(DFHack::Core * c, std::vector<std::string> & params);
//Forward Declarations for Utility Functions
DFhackCExport command_result setAllMatching(DFHack::Core * c, checkTile checkProc,
DFHack::DFCoord minCoord = DFHack::DFCoord(0, 0, 0),
DFHack::DFCoord maxCoord = DFHack::DFCoord(0xFFFF, 0xFFFF, 0xFFFFFFFF));
void allHigh(DFHack::DFCoord coord, MapExtras::MapCache * map);
void allNormal(DFHack::DFCoord coord, MapExtras::MapCache * map);
void allLow(DFHack::DFCoord coord, MapExtras::MapCache * map);
void allRestricted(DFHack::DFCoord coord, MapExtras::MapCache * map);
DFhackCExport const char * plugin_name ( void ) DFhackCExport const char * plugin_name ( void )
{ {
@ -27,7 +42,8 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
{ {
commands.clear(); commands.clear();
commands.push_back(PluginCommand("filltraffic","Flood-fill with selected traffic designation from cursor",filltraffic)); commands.push_back(PluginCommand("filltraffic","Flood-fill with selected traffic designation from cursor",filltraffic));
commands.push_back(PluginCommand("tiletraffic","Set traffic for all tiles based on given criteria",tiletraffic)); commands.push_back(PluginCommand("alltraffic","Set traffic for the entire map",alltraffic));
return CR_OK; return CR_OK;
} }
@ -60,7 +76,7 @@ DFhackCExport command_result filltraffic(DFHack::Core * c, std::vector<std::stri
"\tL: Low Traffic\n" "\tL: Low Traffic\n"
"\tR: Restricted Traffic\n" "\tR: Restricted Traffic\n"
"Other Options:\n" "Other Options:\n"
"\tX: Fill accross z-levels.\n" "\tX: Fill across z-levels.\n"
"\tB: Include buildings and stockpiles.\n" "\tB: Include buildings and stockpiles.\n"
"\tP: Include empty space.\n" "\tP: Include empty space.\n"
"Example:\n" "Example:\n"
@ -227,11 +243,9 @@ DFhackCExport command_result filltraffic(DFHack::Core * c, std::vector<std::stri
enum e_checktype {no_check, check_equal, check_nequal}; enum e_checktype {no_check, check_equal, check_nequal};
DFhackCExport command_result tiletraffic(DFHack::Core * c, std::vector<std::string> & params) DFhackCExport command_result alltraffic(DFHack::Core * c, std::vector<std::string> & params)
{ {
//Target traffic types. void (*proc)(DFHack::DFCoord, MapExtras::MapCache *) = allNormal;
e_traffic target = traffic_normal;
//!!! Options Later !!!
//Loop through parameters //Loop through parameters
for(int i = 0; i < params.size();i++) for(int i = 0; i < params.size();i++)
@ -248,19 +262,30 @@ DFhackCExport command_result tiletraffic(DFHack::Core * c, std::vector<std::stri
return CR_OK; return CR_OK;
} }
//Pick traffic type. Possibly set bounding rectangle later.
switch (toupper(params[i][0])) switch (toupper(params[i][0]))
{ {
case 'H': case 'H':
target = traffic_high; break; proc = allHigh; break;
case 'N': case 'N':
target = traffic_normal; break; proc = allNormal; break;
case 'L': case 'L':
target = traffic_low; break; proc = allLow; break;
case 'R': case 'R':
target = traffic_restricted; break; proc = allRestricted; break;
} }
} }
return setAllMatching(c, proc);
}
//Helper function for writing new functions that check every tile on the map.
//newTraffic is the traffic designation to set.
//check takes a coordinate and the map cache as arguments, and returns true if the criteria is met.
//minCoord and maxCoord can be used to specify a bounding cube.
DFhackCExport command_result setAllMatching(DFHack::Core * c, checkTile checkProc,
DFHack::DFCoord minCoord, DFHack::DFCoord maxCoord)
{
//Initialization. //Initialization.
c->Suspend(); c->Suspend();
@ -280,27 +305,76 @@ DFhackCExport command_result tiletraffic(DFHack::Core * c, std::vector<std::stri
uint32_t tx_max = x_max * 16; uint32_t tx_max = x_max * 16;
uint32_t ty_max = y_max * 16; uint32_t ty_max = y_max * 16;
//Ensure maximum coordinate is within map. Truncate to map edge.
maxCoord.x = std::min((uint32_t) maxCoord.x, tx_max);
maxCoord.y = std::min((uint32_t) maxCoord.y, ty_max);
maxCoord.z = std::min(maxCoord.z, z_max);
//Check minimum co-ordinates against maximum map size
if (minCoord.x > maxCoord.x)
{
c->con.printerr("Minimum x coordinate is greater than maximum x coordinate.\n");
c->Resume();
return CR_FAILURE;
}
if (minCoord.y > maxCoord.y)
{
c->con.printerr("Minimum y coordinate is greater than maximum y coordinate.\n");
c->Resume();
return CR_FAILURE;
}
if (minCoord.z > maxCoord.y)
{
c->con.printerr("Minimum z coordinate is greater than maximum z coordinate.\n");
c->Resume();
return CR_FAILURE;
}
MapExtras::MapCache * MCache = new MapExtras::MapCache(Maps); MapExtras::MapCache * MCache = new MapExtras::MapCache(Maps);
c->con.print("Entire map ... FILLING!\n"); c->con.print("Setting traffic...\n");
//Loop through every single tile //Loop through every single tile
for(uint32_t x = 0; x <= tx_max; x++) for(uint32_t x = minCoord.x; x <= maxCoord.x; x++)
{ {
for(uint32_t y = 0; y <= ty_max; y++) for(uint32_t y = minCoord.y; y <= maxCoord.y; y++)
{ {
for(uint32_t z = 0; z <= z_max; z++) for(uint32_t z = minCoord.z; z <= maxCoord.z; z++)
{ {
DFHack::DFCoord tile = DFHack::DFCoord(x, y, z); DFHack::DFCoord tile = DFHack::DFCoord(x, y, z);
DFHack::t_designation des = MCache->designationAt(tile); checkProc(tile, MCache);
des.bits.traffic = target;
MCache->setDesignationAt(tile, des);
} }
} }
} }
MCache->WriteAll(); MCache->WriteAll();
c->con.print("Complete!\n");
c->Resume(); c->Resume();
return CR_OK; return CR_OK;
} }
//Unconditionally set map to target traffic type
void allHigh(DFHack::DFCoord coord, MapExtras::MapCache * map)
{
DFHack::t_designation des = map->designationAt(coord);
des.bits.traffic = traffic_high;
map->setDesignationAt(coord, des);
}
void allNormal(DFHack::DFCoord coord, MapExtras::MapCache * map)
{
DFHack::t_designation des = map->designationAt(coord);
des.bits.traffic = traffic_normal;
map->setDesignationAt(coord, des);
}
void allLow(DFHack::DFCoord coord, MapExtras::MapCache * map)
{
DFHack::t_designation des = map->designationAt(coord);
des.bits.traffic = traffic_low;
map->setDesignationAt(coord, des);
}
void allRestricted(DFHack::DFCoord coord, MapExtras::MapCache * map)
{
DFHack::t_designation des = map->designationAt(coord);
des.bits.traffic = traffic_restricted;
map->setDesignationAt(coord, des);
}