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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Config.h"
#include <filesystem>
#include <fkYAML/node.hpp>
#include "PathCommon.h"
#include "TerrainBuilder.h"
namespace MMAP
{
float ComputeBaseUnitDim(int vertexPerMapEdge)
{
return GRID_SIZE / static_cast<float>(vertexPerMapEdge);
}
std::pair<uint32, uint32> MakeTileKey(uint32 x, uint32 y)
{
return {x, y};
}
bool isCurrentDirectory(const std::string& pathStr) {
try {
const std::filesystem::path givenPath = std::filesystem::canonical(std::filesystem::absolute(pathStr));
const std::filesystem::path currentPath = std::filesystem::canonical(std::filesystem::current_path());
return givenPath == currentPath;
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Filesystem error: " << e.what() << "\n";
return false;
}
}
MmapTileRecastConfig ResolvedMeshConfig::toMMAPTileRecastConfig() const {
MmapTileRecastConfig config;
config.walkableSlopeAngle = walkableSlopeAngle;
config.walkableHeight = walkableHeight;
config.walkableClimb = walkableClimb;
config.walkableRadius = walkableRadius;
config.maxSimplificationError = maxSimplificationError;
config.cellSizeHorizontal = cellSizeHorizontal;
config.cellSizeVertical = cellSizeVertical;
config.baseUnitDim = baseUnitDim;
config.vertexPerMapEdge = vertexPerMapEdge;
config.vertexPerTileEdge = vertexPerTileEdge;
config.tilesPerMapEdge = tilesPerMapEdge;
return config;
}
std::optional<Config> Config::FromFile(std::string_view configFile) {
Config config;
if (!config.LoadConfig(configFile))
return std::nullopt;
return config;
}
Config::Config()
{
}
ResolvedMeshConfig Config::GetConfigForTile(uint32 mapID, uint32 tileX, uint32 tileY) const
{
const MapOverride* mapOverride = nullptr;
const TileOverride* tileOverride = nullptr;
// Lookup map and tile overrides
if (auto mapIt = _maps.find(mapID); mapIt != _maps.end())
{
mapOverride = &mapIt->second;
auto tileIt = mapOverride->tileOverrides.find(MakeTileKey(tileY, tileX));
if (tileIt != mapOverride->tileOverrides.end())
tileOverride = &tileIt->second;
}
// Helper lambdas to resolve values in order: tile -> map -> global
auto resolveFloat = [&](auto TileField, auto MapField, float GlobalValue) -> float {
if (tileOverride && TileField(tileOverride)) return *TileField(tileOverride);
if (mapOverride && MapField(mapOverride)) return *MapField(mapOverride);
return GlobalValue;
};
auto resolveInt = [&](auto TileField, auto MapField, int GlobalValue) -> int {
if (tileOverride && TileField(tileOverride)) return *TileField(tileOverride);
if (mapOverride && MapField(mapOverride)) return *MapField(mapOverride);
return GlobalValue;
};
// Resolve vertex settings
int vertexPerMap = resolveInt(
[](const TileOverride*) { return std::optional<int>{}; },
[](const MapOverride* m) { return m->vertexPerMapEdge; },
_global.vertexPerMapEdge
);
int vertexPerTile = resolveInt(
[](const TileOverride*) { return std::optional<int>{}; },
[](const MapOverride* m) { return m->vertexPerTileEdge; },
_global.vertexPerTileEdge
);
ResolvedMeshConfig config;
config.walkableSlopeAngle = resolveFloat(
[](const TileOverride* t) { return t->walkableSlopeAngle; },
[](const MapOverride* m) { return m->walkableSlopeAngle; },
_global.walkableSlopeAngle
);
config.walkableRadius = resolveInt(
[](const TileOverride* t) { return t->walkableRadius; },
[](const MapOverride* m) { return m->walkableRadius; },
_global.walkableRadius
);
config.walkableHeight = resolveInt(
[](const TileOverride* t) { return t->walkableHeight; },
[](const MapOverride* m) { return m->walkableHeight; },
_global.walkableHeight
);
config.walkableClimb = resolveInt(
[](const TileOverride* t) { return t->walkableClimb; },
[](const MapOverride* m) { return m->walkableClimb; },
_global.walkableClimb
);
config.vertexPerMapEdge = vertexPerMap;
config.vertexPerTileEdge = vertexPerTile;
config.baseUnitDim = ComputeBaseUnitDim(vertexPerMap);
config.tilesPerMapEdge = vertexPerMap / vertexPerTile;
config.maxSimplificationError = _global.maxSimplificationError;
config.cellSizeHorizontal = config.baseUnitDim;
config.cellSizeVertical = config.baseUnitDim;
if (mapOverride && mapOverride->cellSizeHorizontal.has_value())
config.cellSizeHorizontal = *mapOverride->cellSizeHorizontal;
if (mapOverride && mapOverride->cellSizeVertical.has_value())
config.cellSizeVertical = *mapOverride->cellSizeVertical;
return config;
}
bool Config::LoadConfig(std::string_view configFile) {
FILE* f = std::fopen(configFile.data(), "r");
if (!f)
return false;
fkyaml::node root = fkyaml::node::deserialize(f);
std::fclose(f);
if (!root.contains("mmapsConfig"))
return false;
fkyaml::node mmapsNode = root["mmapsConfig"];
auto tryFloat = [](const fkyaml::node& n, const char* key, float& out)
{
if (n.contains(key)) out = n[key].get_value<float>();
};
auto tryInt = [](const fkyaml::node& n, const char* key, int& out)
{
if (n.contains(key)) out = n[key].get_value<int>();
};
auto tryBoolean = [](const fkyaml::node& n, const char* key, bool& out)
{
if (n.contains(key)) out = n[key].get_value<bool>();
};
auto tryString = [](const fkyaml::node& n, const char* key, std::string& out)
{
if (n.contains(key)) out = n[key].get_value<std::string>();
};
tryBoolean(mmapsNode, "skipLiquid", _skipLiquid);
tryBoolean(mmapsNode, "skipContinents", _skipContinents);
tryBoolean(mmapsNode, "skipJunkMaps", _skipJunkMaps);
tryBoolean(mmapsNode, "skipBattlegrounds", _skipBattlegrounds);
tryBoolean(mmapsNode, "debugOutput", _debugOutput);
std::string dataDirPath;
tryString(mmapsNode, "dataDir", dataDirPath);
_dataDir = dataDirPath;
mmapsNode = mmapsNode["meshSettings"];
// Global config
tryFloat(mmapsNode, "walkableSlopeAngle", _global.walkableSlopeAngle);
tryInt(mmapsNode, "walkableHeight", _global.walkableHeight);
tryInt(mmapsNode, "walkableClimb", _global.walkableClimb);
tryInt(mmapsNode, "walkableRadius", _global.walkableRadius);
tryInt(mmapsNode, "vertexPerMapEdge", _global.vertexPerMapEdge);
tryInt(mmapsNode, "vertexPerTileEdge", _global.vertexPerTileEdge);
tryFloat(mmapsNode, "maxSimplificationError", _global.maxSimplificationError);
// Map overrides
if (mmapsNode.contains("mapsOverrides"))
{
fkyaml::node maps = mmapsNode["mapsOverrides"];
for (auto const& mapEntry : maps.as_map())
{
uint32 mapId = std::stoi(mapEntry.first.as_str());
MapOverride override;
fkyaml::node mapNode = mapEntry.second;
if (mapNode.contains("walkableSlopeAngle"))
override.walkableSlopeAngle = mapNode["walkableSlopeAngle"].get_value<float>();
if (mapNode.contains("walkableRadius"))
override.walkableRadius = mapNode["walkableRadius"].get_value<int>();
if (mapNode.contains("walkableHeight"))
override.walkableHeight = mapNode["walkableHeight"].get_value<int>();
if (mapNode.contains("walkableClimb"))
override.walkableClimb = mapNode["walkableClimb"].get_value<int>();
if (mapNode.contains("vertexPerMapEdge"))
override.vertexPerMapEdge = mapNode["vertexPerMapEdge"].get_value<int>();
if (mapNode.contains("cellSizeHorizontal"))
override.cellSizeHorizontal = mapNode["cellSizeHorizontal"].get_value<float>();
if (mapNode.contains("cellSizeVertical"))
override.cellSizeVertical = mapNode["cellSizeVertical"].get_value<float>();
// Tile overrides
if (mapNode.contains("tilesOverrides"))
{
fkyaml::node tiles = mapNode["tilesOverrides"];
for (auto const& tileEntry : tiles.as_map())
{
std::string key = tileEntry.first.as_str();
fkyaml::node tileNode = tileEntry.second;
size_t comma = key.find(',');
if (comma == std::string::npos)
continue;
uint32 tileX = static_cast<uint32>(std::stoi(key.substr(0, comma)));
uint32 tileY = static_cast<uint32>(std::stoi(key.substr(comma + 1)));
TileOverride tileOverride;
if (tileNode.contains("walkableSlopeAngle"))
tileOverride.walkableSlopeAngle = tileNode["walkableSlopeAngle"].get_value<float>();
if (tileNode.contains("walkableRadius"))
tileOverride.walkableRadius = tileNode["walkableRadius"].get_value<int>();
if (tileNode.contains("walkableHeight"))
tileOverride.walkableHeight = tileNode["walkableHeight"].get_value<int>();
if (tileNode.contains("walkableClimb"))
tileOverride.walkableClimb = tileNode["walkableClimb"].get_value<int>();
override.tileOverrides[{tileX, tileY}] = std::move(tileOverride);
}
}
_maps[mapId] = std::move(override);
}
}
// Resolve data dir path. Maybe we need to use an executable path instead of the current dir.
if (isCurrentDirectory(_dataDir.string()) && !std::filesystem::exists(MapsPath()))
if (auto execPath = std::filesystem::path(executableDirectoryPath()); std::filesystem::exists(execPath/ "maps"))
_dataDir = execPath;
return true;
}
}
|