forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixveins.cpp
115 lines (101 loc) · 4.37 KB
/
fixveins.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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"
#include "df/block_square_event.h"
#include "df/block_square_event_mineralst.h"
#include "df/map_block.h"
#include "df/world.h"
using std::vector;
using std::string;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("fixveins");
REQUIRE_GLOBAL(world);
bool setTileMaterial(df::tiletype &tile, const df::tiletype_material mat)
{
df::tiletype newTile = findTileType(tileShape(tile), mat, tileVariant(tile), tileSpecial(tile), tileDirection(tile));
if (newTile == tiletype::Void)
return false;
if (newTile != tile)
{
tile = newTile;
return true;
}
return false;
}
command_result df_fixveins (color_ostream &out, vector <string> & parameters)
{
if (parameters.size())
return CR_WRONG_USAGE;
CoreSuspender suspend;
if (!Maps::IsValid())
{
out.printerr("Map is not available!\n");
return CR_FAILURE;
}
int mineral_removed = 0, feature_removed = 0;
int mineral_added = 0, feature_added = 0;
int 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 (size_t 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];
}
t_feature local, global;
Maps::ReadFeatures(block, &local, &global);
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
bool has_feature = ((block->designation[x][y].bits.feature_global) && (global.main_material != -1) && (global.sub_material != -1)) ||
((block->designation[x][y].bits.feature_local) && (local.main_material != -1) && (local.sub_material != -1));
bool has_vein = has_mineral[y] & (1 << x);
if (has_feature)
has_vein = false;
df::tiletype oldT = block->tiletype[x][y];
df::tiletype_material mat = tileMaterial(oldT);
if ((mat == tiletype_material::MINERAL) && !has_vein)
mineral_removed += setTileMaterial(block->tiletype[x][y], tiletype_material::STONE);
if ((mat == tiletype_material::STONE) && has_vein)
mineral_added += setTileMaterial(block->tiletype[x][y], tiletype_material::MINERAL);
if ((mat == tiletype_material::FEATURE) && !has_feature)
feature_removed += setTileMaterial(block->tiletype[x][y], tiletype_material::STONE);
if ((mat == tiletype_material::STONE) && has_feature)
feature_added += setTileMaterial(block->tiletype[x][y], tiletype_material::FEATURE);
}
}
}
if (mineral_removed || feature_removed)
out.print("Removed invalid references from %i mineral inclusion and %i map feature tiles.\n", mineral_removed, feature_removed);
if (mineral_added || feature_added)
out.print("Restored missing references to %i mineral inclusion and %i map feature tiles.\n", mineral_added, feature_added);
return CR_OK;
}
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
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 ( color_ostream &out )
{
return CR_OK;
}