summaryrefslogtreecommitdiff
path: root/plugins/fixveins.cpp
diff options
context:
space:
mode:
authorQuietust2012-01-26 10:06:03 -0600
committerQuietust2012-01-26 10:06:03 -0600
commitbe51221939dd0ab67f908fb4e990aa70e1b0c821 (patch)
treebf01f3d4ae1a689cf38bcab349bfe837b528331b /plugins/fixveins.cpp
parent40b82d4e6c13cf927bb9ee1ec62d27d315abec42 (diff)
downloaddfhack-be51221939dd0ab67f908fb4e990aa70e1b0c821.tar.gz
dfhack-be51221939dd0ab67f908fb4e990aa70e1b0c821.tar.bz2
dfhack-be51221939dd0ab67f908fb4e990aa70e1b0c821.tar.xz
Add plugin "fixveins", restores mineral floors that were erased by placing/removing constructions
Diffstat (limited to 'plugins/fixveins.cpp')
-rw-r--r--plugins/fixveins.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/plugins/fixveins.cpp b/plugins/fixveins.cpp
new file mode 100644
index 00000000..092630f3
--- /dev/null
+++ b/plugins/fixveins.cpp
@@ -0,0 +1,107 @@
+// Building and removing construction on a mineral floor will destroy the mineral, changing it to the layer stone
+// Farm plots or paved roads do the same thing periodically (once every 500 ticks or so)
+// This tool changes said tiles back into the mineral type they originally had
+
+// It also fixes tiles marked as "mineral inclusion" where no inclusion is present,
+// which generally happen as a result of improper use of the tiletypes plugin
+
+#include "Core.h"
+#include "Console.h"
+#include "Export.h"
+#include "PluginManager.h"
+
+#include "DataDefs.h"
+#include "modules/Maps.h"
+#include "TileTypes.h"
+
+using std::vector;
+using std::string;
+using namespace DFHack;
+using namespace DFHack::Simple;
+using namespace df::enums;
+
+using df::global::world;
+
+DFhackCExport command_result df_fixveins (Core * c, vector <string> & parameters)
+{
+ if (parameters.size())
+ return CR_WRONG_USAGE;
+
+ CoreSuspender suspend(c);
+
+ if (!Maps::IsValid())
+ {
+ c->con.printerr("Map is not available!\n");
+ return CR_FAILURE;
+ }
+
+ int removed = 0;
+ int added = 0;
+
+ int num_blocks = 0, blocks_total = world->map.map_blocks.size();
+ for (int i = 0; i < blocks_total; i++)
+ {
+ df::map_block *block = world->map.map_blocks[i];
+ uint16_t has_mineral[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ for (int j = 0; j < block->block_events.size(); j++)
+ {
+ df::block_square_event *evt = block->block_events[j];
+ if (evt->getType() != block_square_event_type::mineral)
+ continue;
+ df::block_square_event_mineralst *mineral = (df::block_square_event_mineralst *)evt;
+ for (int k = 0; k < 16; k++)
+ has_mineral[k] |= mineral->tile_bitmask[k];
+ }
+ for (int x = 0; x < 16; x++)
+ {
+ for (int y = 0; y < 16; y++)
+ {
+ int16_t oldT = block->tiletype[x][y];
+ int16_t newT = oldT;
+ TileMaterial mat = tileMaterial(oldT);
+ if ((mat == VEIN) && !(has_mineral[y] & (1 << x)))
+ {
+ newT = findTileType(tileShape(oldT), STONE, tileVariant(oldT), tileSpecial(oldT), tileDirection(oldT));
+ if ((newT != -1) && (newT != oldT))
+ {
+ block->tiletype[x][y] = newT;
+ removed++;
+ }
+ }
+ if ((mat == STONE) && (has_mineral[y] & (1 << x)))
+ {
+ newT = findTileType(tileShape(oldT), VEIN, tileVariant(oldT), tileSpecial(oldT), tileDirection(oldT));
+ if ((newT != -1) && (newT != oldT))
+ {
+ block->tiletype[x][y] = newT;
+ added++;
+ }
+ }
+ }
+ }
+ }
+ if (removed)
+ c->con.print("Removed %i invalid references to mineral inclusions.\n", removed);
+ if (added)
+ c->con.print("Restored %i missing references to mineral inclusions.\n", added);
+ return CR_OK;
+}
+
+DFhackCExport const char * plugin_name ( void )
+{
+ return "fixveins";
+}
+
+DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
+{
+ commands.clear();
+ commands.push_back(PluginCommand("fixveins",
+ "Remove invalid references to mineral inclusions and restore missing ones.",
+ df_fixveins));
+ return CR_OK;
+}
+
+DFhackCExport command_result plugin_shutdown ( Core * c )
+{
+ return CR_OK;
+}