feat(navgen): bake road materials and directional cover
Decode LMp2 terrain materials and decals into navigable road areas. Preserve usable cover bearings and verify left and right corner approaches independently on the standard tank mesh. Verified with Rust and MSVC builds, TypeScript checks, and offline baking on Berlin, Poland, and Hurtgen for 2.61.0.58.
This commit is contained in:
1 parent
842ee23fa4
commit
4191d59168
9 files changed
+662
-241
No files matched your search
+275
-211
@@ -29,6 +29,7 @@
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <tuple>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
@@ -47,7 +48,15 @@ struct PoiPt {
|
||||
float x, y, z, radius;
|
||||
};
|
||||
|
||||
struct CoverBake {
|
||||
float minCoverHeightM;
|
||||
float peekMinM;
|
||||
float peekMaxM;
|
||||
float retreatMinM;
|
||||
};
|
||||
|
||||
struct PoisData {
|
||||
CoverBake cover{};
|
||||
std::vector<PoiPt> t1Spawns;
|
||||
std::vector<PoiPt> t2Spawns;
|
||||
std::vector<PoiPt> caps;
|
||||
@@ -74,15 +83,29 @@ static constexpr float kTileWorldSize = kTileSize * kCellSize;
|
||||
static constexpr float kShallowWaterMaxM = 0.60f;
|
||||
static constexpr float kShallowWaterMinM = 0.05f;
|
||||
static constexpr float kRoughSlopeDeg = 18.0f;
|
||||
static constexpr float kCoverScanM = 5.2f;
|
||||
static constexpr float kPeekSightM = 14.0f;
|
||||
static constexpr float kHullDownSightM = 22.0f;
|
||||
static constexpr float kCoverHeightToleranceM = 1.2f;
|
||||
|
||||
struct GeoData {
|
||||
WtoGeoHeader hdr{};
|
||||
std::vector<float> heights;
|
||||
WtoGeoRoadMask roads{};
|
||||
std::vector<uint8_t> roadCells;
|
||||
std::vector<Vec3> obsVerts;
|
||||
std::vector<ObsTri> obsTris;
|
||||
float minY = 1e9f;
|
||||
float maxY = -1e9f;
|
||||
|
||||
bool isRoad(float wx, float wz) const {
|
||||
if (roadCells.empty()) return false;
|
||||
float x = (wx - roads.origin[0]) / roads.cellSize;
|
||||
float z = (wz - roads.origin[1]) / roads.cellSize;
|
||||
if (x < 0.0f || z < 0.0f || x >= roads.size || z >= roads.size) return false;
|
||||
return roadCells[(size_t)z * roads.size + (size_t)x] != 0;
|
||||
}
|
||||
|
||||
float sampleHeight(float wx, float wz) const {
|
||||
float fx = (wx - hdr.hmOrigin[0]) / hdr.hmCellSize;
|
||||
float fz = (wz - hdr.hmOrigin[1]) / hdr.hmCellSize;
|
||||
@@ -121,6 +144,15 @@ static bool loadGeo(const std::string &path, GeoData &out) {
|
||||
in.read(reinterpret_cast<char *>(&cls), 1);
|
||||
out.obsTris[i] = { idx[0], idx[1], idx[2], cls };
|
||||
}
|
||||
if (out.hdr.flags & WTO_GEO_FLAG_ROAD_MASK) {
|
||||
in.read(reinterpret_cast<char *>(&out.roads), sizeof(WtoGeoRoadMask));
|
||||
// Same allocation limit as the extractor.
|
||||
if (!in || out.roads.size == 0 || out.roads.size > 8192 ||
|
||||
!(out.roads.cellSize > 0.0f) || !std::isfinite(out.roads.cellSize) ||
|
||||
!std::isfinite(out.roads.origin[0]) || !std::isfinite(out.roads.origin[1])) return false;
|
||||
out.roadCells.resize((size_t)out.roads.size * out.roads.size);
|
||||
in.read(reinterpret_cast<char *>(out.roadCells.data()), out.roadCells.size());
|
||||
}
|
||||
if (!in) return false;
|
||||
|
||||
for (float h : out.heights) {
|
||||
@@ -142,6 +174,17 @@ static bool loadGeo(const std::string &path, GeoData &out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static float readJsonNumber(const std::string &json, const char *key, float fallback) {
|
||||
size_t pos = json.find(std::string("\"") + key + "\"");
|
||||
if (pos == std::string::npos) return fallback;
|
||||
pos = json.find(':', pos);
|
||||
if (pos == std::string::npos) return fallback;
|
||||
const char *start = json.c_str() + pos + 1;
|
||||
char *end = nullptr;
|
||||
float value = std::strtof(start, &end);
|
||||
return end != start && std::isfinite(value) ? value : fallback;
|
||||
}
|
||||
|
||||
/* Minimal JSON array extractor for `.pois.json` (team1Spawns / team2Spawns / captureZones). */
|
||||
static void parsePoiArray(const std::string &json, const std::string &key, std::vector<PoiPt> &out) {
|
||||
std::string needle = "\"" + key + "\"";
|
||||
@@ -156,14 +199,6 @@ static void parsePoiArray(const std::string &json, const std::string &key, std::
|
||||
else if (json[i] == ']') { depth--; if (depth == 0) break; }
|
||||
}
|
||||
std::string slice = json.substr(lb, i - lb + 1);
|
||||
auto readNum = [](const std::string &obj, const char *field, float def) -> float {
|
||||
std::string fn = std::string("\"") + field + "\"";
|
||||
size_t p = obj.find(fn);
|
||||
if (p == std::string::npos) return def;
|
||||
p = obj.find(':', p);
|
||||
if (p == std::string::npos) return def;
|
||||
return std::strtof(obj.c_str() + p + 1, nullptr);
|
||||
};
|
||||
auto readStr = [](const std::string &obj, const char *field) -> std::string {
|
||||
std::string fn = std::string("\"") + field + "\"";
|
||||
size_t p = obj.find(fn);
|
||||
@@ -182,10 +217,10 @@ static void parsePoiArray(const std::string &json, const std::string &key, std::
|
||||
std::string obj = slice.substr(ob, cb - ob + 1);
|
||||
PoiPt pt;
|
||||
pt.name = readStr(obj, "name");
|
||||
pt.x = readNum(obj, "x", 0.0f);
|
||||
pt.y = readNum(obj, "y", 0.0f);
|
||||
pt.z = readNum(obj, "z", 0.0f);
|
||||
pt.radius = readNum(obj, "radius", 15.0f);
|
||||
pt.x = readJsonNumber(obj, "x", 0.0f);
|
||||
pt.y = readJsonNumber(obj, "y", 0.0f);
|
||||
pt.z = readJsonNumber(obj, "z", 0.0f);
|
||||
pt.radius = readJsonNumber(obj, "radius", 15.0f);
|
||||
out.push_back(pt);
|
||||
cur = cb + 1;
|
||||
}
|
||||
@@ -195,6 +230,18 @@ static bool loadPois(const std::string &path, PoisData &out) {
|
||||
std::ifstream in(path);
|
||||
if (!in) return false;
|
||||
std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
|
||||
out.cover = {
|
||||
readJsonNumber(content, "minCoverHeightM", -1.0f),
|
||||
readJsonNumber(content, "peekMinM", -1.0f),
|
||||
readJsonNumber(content, "peekMaxM", -1.0f),
|
||||
readJsonNumber(content, "retreatMinM", -1.0f)
|
||||
};
|
||||
if (out.cover.minCoverHeightM <= 0.0f || out.cover.peekMinM <= 0.0f ||
|
||||
out.cover.peekMaxM < out.cover.peekMinM || out.cover.peekMaxM >= kTileWorldSize ||
|
||||
out.cover.retreatMinM <= 0.0f || out.cover.retreatMinM >= kTileWorldSize) {
|
||||
std::fprintf(stderr, "Invalid cover baking settings in %s. Regenerate it with mission-bounds.mts.\n", path.c_str());
|
||||
return false;
|
||||
}
|
||||
parsePoiArray(content, "team1Spawns", out.t1Spawns);
|
||||
parsePoiArray(content, "team2Spawns", out.t2Spawns);
|
||||
parsePoiArray(content, "captureZones", out.caps);
|
||||
@@ -254,6 +301,129 @@ static bool hfTraceBand(const rcHeightfield &hardHf, const GeoData &geo,
|
||||
return false;
|
||||
}
|
||||
|
||||
static int coverBorderCells(const CoverBake &tuning) {
|
||||
float reach = std::max(kHullDownSightM, std::hypot(tuning.peekMaxM, kPeekSightM));
|
||||
return (int)std::ceil(reach / kCellSize) + 1;
|
||||
}
|
||||
|
||||
static bool isCoverArea(unsigned char area) {
|
||||
return area != RC_NULL_AREA && area != WTO_AREA_DEEP_WATER && area != WTO_AREA_SHALLOW_WATER;
|
||||
}
|
||||
|
||||
static bool isCoverBoundary(const rcCompactHeightfield &field, int x, int z, const rcCompactSpan &span) {
|
||||
for (int dir = 0; dir < 4; ++dir) {
|
||||
int connection = rcGetCon(span, dir);
|
||||
if (connection == RC_NOT_CONNECTED) return true;
|
||||
int nx = x + rcGetDirOffsetX(dir);
|
||||
int nz = z + rcGetDirOffsetY(dir);
|
||||
if (field.areas[field.cells[nx + nz * field.width].index + connection] == RC_NULL_AREA) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool findCornerPeek(
|
||||
const rcCompactHeightfield &field, const rcHeightfield &hard, const GeoData &geo,
|
||||
const CoverBake &tuning, const WtoCoverPoint &cover, float side, float (&point)[3]
|
||||
) {
|
||||
const float dx = cover.coverDir[0], dz = cover.coverDir[1];
|
||||
for (float distance = tuning.peekMinM; distance <= tuning.peekMaxM; distance += field.cs) {
|
||||
const float x = cover.pos[0] - dz * side * distance;
|
||||
const float z = cover.pos[2] + dx * side * distance;
|
||||
const int gx = (int)std::floor((x - field.bmin[0]) / field.cs);
|
||||
const int gz = (int)std::floor((z - field.bmin[2]) / field.cs);
|
||||
if (gx < 0 || gz < 0 || gx >= field.width || gz >= field.height) break;
|
||||
|
||||
const rcCompactCell &cell = field.cells[gx + gz * field.width];
|
||||
for (unsigned int i = cell.index; i < cell.index + cell.count; ++i) {
|
||||
if (!isCoverArea(field.areas[i])) continue;
|
||||
float y = field.bmin[1] + field.spans[i].y * field.ch;
|
||||
if (std::fabs(y - cover.pos[1]) > kCoverHeightToleranceM) continue;
|
||||
if (hfTraceBand(hard, geo, x, z, dx, dz, kPeekSightM, y + 1.8f, y + 2.6f)) continue;
|
||||
point[0] = x;
|
||||
point[1] = y;
|
||||
point[2] = z;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void bakeTileCovers(
|
||||
const rcCompactHeightfield &field, const rcHeightfield &hard, const GeoData &geo,
|
||||
const rcConfig &cfg, const CoverBake &tuning, std::vector<WtoCoverPoint> &covers
|
||||
) {
|
||||
static const float directions[8][2] = {
|
||||
{ 1.0f, 0.0f }, { 0.70710678f, 0.70710678f }, { 0.0f, 1.0f }, { -0.70710678f, 0.70710678f },
|
||||
{ -1.0f, 0.0f }, { -0.70710678f, -0.70710678f }, { 0.0f, -1.0f }, { 0.70710678f, -0.70710678f }
|
||||
};
|
||||
const int innerEnd = cfg.borderSize + cfg.tileSize;
|
||||
for (int z = cfg.borderSize; z < innerEnd; ++z) {
|
||||
for (int x = cfg.borderSize; x < innerEnd; ++x) {
|
||||
const rcCompactCell &cell = field.cells[x + z * field.width];
|
||||
for (unsigned int i = cell.index; i < cell.index + cell.count; ++i) {
|
||||
const rcCompactSpan &span = field.spans[i];
|
||||
if (!isCoverArea(field.areas[i]) || !isCoverBoundary(field, x, z, span)) continue;
|
||||
Vec3 pos{ cfg.bmin[0] + (x + 0.5f) * cfg.cs, cfg.bmin[1] + span.y * cfg.ch,
|
||||
cfg.bmin[2] + (z + 0.5f) * cfg.cs };
|
||||
|
||||
// Every usable bearing goes through the same checks, including opposite faces of one object.
|
||||
for (const auto &dir : directions) {
|
||||
float dx = dir[0], dz = dir[1];
|
||||
int hullHits = 0, turretHits = 0;
|
||||
float totalHeight = 0.0f;
|
||||
for (int column = -2; column <= 2; ++column) {
|
||||
float offset = column * 0.9f; // Five samples across a 3.6 m hull.
|
||||
float sx = pos.x - dz * offset, sz = pos.z + dx * offset;
|
||||
float top = pos.y;
|
||||
if (!hfTraceBand(hard, geo, sx, sz, dx, dz, kCoverScanM, pos.y + 0.65f, pos.y + 1.80f, &top)) continue;
|
||||
++hullHits;
|
||||
totalHeight += std::max(0.0f, top - pos.y);
|
||||
turretHits += hfTraceBand(hard, geo, sx, sz, dx, dz, kCoverScanM, pos.y + 2.05f, pos.y + 2.85f);
|
||||
}
|
||||
if (hullHits < 4 || totalHeight < tuning.minCoverHeightM * hullHits) continue;
|
||||
|
||||
float width = 3.6f;
|
||||
for (float off : { -2.6f, 2.6f }) {
|
||||
if (hfTraceBand(hard, geo, pos.x - dz * off, pos.z + dx * off, dx, dz,
|
||||
kCoverScanM, pos.y + 0.65f, pos.y + 1.80f)) width += 1.0f;
|
||||
}
|
||||
WtoCoverPoint cover{};
|
||||
cover.pos[0] = pos.x; cover.pos[1] = pos.y; cover.pos[2] = pos.z;
|
||||
cover.coverDir[0] = dx; cover.coverDir[1] = dz;
|
||||
cover.halfAngleDeg = width >= 5.0f ? 42.0f : 32.0f;
|
||||
cover.heightM = totalHeight / hullHits;
|
||||
cover.widthM = width;
|
||||
|
||||
if (turretHits >= 3) {
|
||||
cover.kind = WTO_COVER_FULL;
|
||||
cover.hasPeekLeft = findCornerPeek(field, hard, geo, tuning, cover, -1.0f, cover.peekLeft);
|
||||
cover.hasPeekRight = findCornerPeek(field, hard, geo, tuning, cover, 1.0f, cover.peekRight);
|
||||
} else {
|
||||
if (hfTraceBand(hard, geo, pos.x, pos.z, dx, dz, kHullDownSightM, pos.y + 2.15f, pos.y + 2.75f)) continue;
|
||||
cover.kind = WTO_COVER_HULLDOWN;
|
||||
}
|
||||
covers.push_back(cover);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void markRoadAreas(rcCompactHeightfield &field, const GeoData &geo) {
|
||||
if (geo.roadCells.empty()) return;
|
||||
for (int z = 0; z < field.height; ++z) {
|
||||
const float wz = field.bmin[2] + (z + 0.5f) * field.cs;
|
||||
for (int x = 0; x < field.width; ++x) {
|
||||
const float wx = field.bmin[0] + (x + 0.5f) * field.cs;
|
||||
if (!geo.isRoad(wx, wz)) continue;
|
||||
const rcCompactCell &cell = field.cells[x + z * field.width];
|
||||
for (unsigned int i = cell.index; i < cell.index + cell.count; ++i) {
|
||||
if (field.areas[i] == WTO_AREA_GROUND) field.areas[i] = WTO_AREA_ROAD;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct BuiltTile {
|
||||
int tx = 0;
|
||||
int tz = 0;
|
||||
@@ -272,7 +442,7 @@ static TileBakeResult buildSingleTile(
|
||||
const TierSpec &tier,
|
||||
int tx, int tz,
|
||||
float mapMinX, float mapMinZ,
|
||||
bool bakeCovers
|
||||
const CoverBake &coverTuning, bool bakeCovers
|
||||
) {
|
||||
TileBakeResult res;
|
||||
res.tile.tx = tx;
|
||||
@@ -293,6 +463,8 @@ static TileBakeResult buildSingleTile(
|
||||
cfg.maxVertsPerPoly = 6;
|
||||
cfg.tileSize = kTileSize;
|
||||
cfg.borderSize = cfg.walkableRadius + 3;
|
||||
// Cover and corner rays must see hard collision on the other side of tile boundaries.
|
||||
if (bakeCovers) cfg.borderSize = std::max(cfg.borderSize, coverBorderCells(coverTuning));
|
||||
cfg.width = cfg.tileSize + cfg.borderSize * 2;
|
||||
cfg.height = cfg.tileSize + cfg.borderSize * 2;
|
||||
cfg.detailSampleDist = 12.0f * cfg.cs;
|
||||
@@ -442,139 +614,14 @@ static TileBakeResult buildSingleTile(
|
||||
}
|
||||
}
|
||||
|
||||
/* 3. Bake tank-qualified covers directly from eroded Tier-1 walkable boundary + hardHf */
|
||||
if (bakeCovers && hardHf) {
|
||||
const int b = cfg.borderSize;
|
||||
const int innerEnd = b + cfg.tileSize;
|
||||
static const float kDirs[8][2] = {
|
||||
{ 1.0f, 0.0f }, { 0.7071f, 0.7071f }, { 0.0f, 1.0f }, { -0.7071f, 0.7071f },
|
||||
{ -1.0f, 0.0f }, { -0.7071f, -0.7071f }, { 0.0f, -1.0f }, { 0.7071f, -0.7071f }
|
||||
};
|
||||
for (int z = b; z < innerEnd; z += 4) {
|
||||
for (int x = b; x < innerEnd; x += 4) {
|
||||
const rcCompactCell &c = chf->cells[x + z * chf->width];
|
||||
for (int si = (int)c.index, ni = (int)(c.index + c.count); si < ni; ++si) {
|
||||
unsigned char area = chf->areas[si];
|
||||
if (area == RC_NULL_AREA || area == WTO_AREA_DEEP_WATER || area == WTO_AREA_SHALLOW_WATER) continue;
|
||||
/* Check if any 4-neighbor (at distance 1..2 cells) was eroded to RC_NULL_AREA or disconnected */
|
||||
const rcCompactSpan &s = chf->spans[si];
|
||||
bool isBoundary = false;
|
||||
for (int d = 0; d < 4; ++d) {
|
||||
int con = rcGetCon(s, d);
|
||||
if (con == RC_NOT_CONNECTED) { isBoundary = true; break; }
|
||||
int nx = x + rcGetDirOffsetX(d);
|
||||
int nz = z + rcGetDirOffsetY(d);
|
||||
int nsi = (int)chf->cells[nx + nz * chf->width].index + con;
|
||||
if (chf->areas[nsi] == RC_NULL_AREA) { isBoundary = true; break; }
|
||||
}
|
||||
if (!isBoundary) continue;
|
||||
|
||||
float wx = cfg.bmin[0] + (x + 0.5f) * cfg.cs;
|
||||
float wy = cfg.bmin[1] + s.y * cfg.ch;
|
||||
float wz = cfg.bmin[2] + (z + 0.5f) * cfg.cs;
|
||||
|
||||
/* Find strongest hard-obstacle direction within 2.0..5.2 m */
|
||||
int bestDir = -1;
|
||||
float bestWidth = 0.0f;
|
||||
float bestHeight = 0.0f;
|
||||
bool bestTurretBlocked = false;
|
||||
|
||||
for (int di = 0; di < 8; ++di) {
|
||||
float dx = kDirs[di][0], dz = kDirs[di][1];
|
||||
float px = -dz, pz = dx; /* tangent */
|
||||
int hullHits = 0;
|
||||
int turretHits = 0;
|
||||
float hSum = 0.0f;
|
||||
/* 5 lateral samples across 3.6 m hull width (-1.8, -0.9, 0, +0.9, +1.8) */
|
||||
for (int li = -2; li <= 2; ++li) {
|
||||
float off = li * 0.9f;
|
||||
float sx = wx + px * off;
|
||||
float sz = wz + pz * off;
|
||||
float topY = wy;
|
||||
if (hfTraceBand(*hardHf, geo, sx, sz, dx, dz, 5.2f, wy + 0.65f, wy + 1.80f, &topY)) {
|
||||
hullHits++;
|
||||
hSum += std::max(0.0f, topY - wy);
|
||||
if (hfTraceBand(*hardHf, geo, sx, sz, dx, dz, 5.2f, wy + 2.05f, wy + 2.85f)) {
|
||||
turretHits++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hullHits < 4) continue; /* Require >= 80% hull width blocked (eliminates thin posts & narrow rocks) */
|
||||
float avgH = hSum / (float)hullHits;
|
||||
if (avgH < 1.55f) continue; /* Eliminate low rocks (< 1.55 m) */
|
||||
|
||||
/* Check extra lateral span for continuous obstacle width */
|
||||
float wMeters = 3.6f;
|
||||
if (hfTraceBand(*hardHf, geo, wx + px * 2.6f, wz + pz * 2.6f, dx, dz, 5.2f, wy + 0.65f, wy + 1.80f)) wMeters += 1.0f;
|
||||
if (hfTraceBand(*hardHf, geo, wx - px * 2.6f, wz - pz * 2.6f, dx, dz, 5.2f, wy + 0.65f, wy + 1.80f)) wMeters += 1.0f;
|
||||
|
||||
if (wMeters > bestWidth || (wMeters == bestWidth && avgH > bestHeight)) {
|
||||
bestDir = di;
|
||||
bestWidth = wMeters;
|
||||
bestHeight = avgH;
|
||||
bestTurretBlocked = (turretHits >= 3);
|
||||
}
|
||||
}
|
||||
|
||||
if (bestDir < 0) continue;
|
||||
float dx = kDirs[bestDir][0], dz = kDirs[bestDir][1];
|
||||
float px = -dz, pz = dx;
|
||||
|
||||
WtoCoverPoint cp{};
|
||||
cp.pos[0] = wx; cp.pos[1] = wy; cp.pos[2] = wz;
|
||||
cp.coverDir[0] = dx; cp.coverDir[1] = dz;
|
||||
cp.retreatDir[0] = -dx; cp.retreatDir[1] = -dz;
|
||||
cp.halfAngleDeg = bestWidth >= 5.0f ? 42.0f : 32.0f;
|
||||
cp.heightM = bestHeight;
|
||||
cp.widthM = bestWidth;
|
||||
|
||||
if (bestTurretBlocked) {
|
||||
cp.kind = WTO_COVER_FULL;
|
||||
/* Probe corner peek left/right at 4.2 m */
|
||||
for (int side = -1; side <= 1; side += 2) {
|
||||
float kx = wx + px * (side * 4.2f);
|
||||
float kz = wz + pz * (side * 4.2f);
|
||||
int kgx = (int)std::floor((kx - cfg.bmin[0]) / cfg.cs);
|
||||
int kgz = (int)std::floor((kz - cfg.bmin[2]) / cfg.cs);
|
||||
if (kgx >= 0 && kgz >= 0 && kgx < chf->width && kgz < chf->height) {
|
||||
const rcCompactCell &kc = chf->cells[kgx + kgz * chf->width];
|
||||
bool walkablePeek = false;
|
||||
float ky = wy;
|
||||
for (int ksi = (int)kc.index, kni = (int)(kc.index + kc.count); ksi < kni; ++ksi) {
|
||||
if (chf->areas[ksi] != RC_NULL_AREA && chf->areas[ksi] != WTO_AREA_DEEP_WATER) {
|
||||
float sy = cfg.bmin[1] + chf->spans[ksi].y * cfg.ch;
|
||||
if (std::fabs(sy - wy) <= 1.2f) { walkablePeek = true; ky = sy; break; }
|
||||
}
|
||||
}
|
||||
if (walkablePeek && !hfTraceBand(*hardHf, geo, kx, kz, dx, dz, 14.0f, ky + 1.8f, ky + 2.6f)) {
|
||||
if (side < 0) {
|
||||
cp.hasPeekLeft = 1;
|
||||
cp.peekLeft[0] = kx; cp.peekLeft[1] = ky; cp.peekLeft[2] = kz;
|
||||
} else {
|
||||
cp.hasPeekRight = 1;
|
||||
cp.peekRight[0] = kx; cp.peekRight[1] = ky; cp.peekRight[2] = kz;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
res.covers.push_back(cp);
|
||||
} else {
|
||||
/* Hull-down candidate: verify forward 22 m clear at turret height */
|
||||
if (!hfTraceBand(*hardHf, geo, wx, wz, dx, dz, 22.0f, wy + 2.15f, wy + 2.75f)) {
|
||||
cp.kind = WTO_COVER_HULLDOWN;
|
||||
res.covers.push_back(cp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hardHf) bakeTileCovers(*chf, *hardHf, geo, cfg, coverTuning, res.covers);
|
||||
rcFreeHeightField(hardHf);
|
||||
hardHf = nullptr;
|
||||
|
||||
/* Smooth isolated single-voxel slope/area noise slivers before region building */
|
||||
rcMedianFilterWalkableArea(&ctx, *chf);
|
||||
rcMedianFilterWalkableArea(&ctx, *chf);
|
||||
markRoadAreas(*chf, geo);
|
||||
|
||||
if (!rcBuildDistanceField(&ctx, *chf) ||
|
||||
!rcBuildRegions(&ctx, *chf, cfg.borderSize, cfg.minRegionArea, cfg.mergeRegionArea)) {
|
||||
@@ -771,74 +818,89 @@ static void markChokepoints(dtNavMesh *mesh, dtNavMeshQuery *query, float chokeD
|
||||
}
|
||||
}
|
||||
|
||||
static dtPolyRef verifyCoverPosition(dtNavMeshQuery &query, const dtQueryFilter &filter, float (&point)[3]) {
|
||||
const float extent[3] = { kCellSize, kCoverHeightToleranceM, kCellSize };
|
||||
dtPolyRef ref = 0;
|
||||
float nearest[3];
|
||||
bool overPoly = false;
|
||||
if (dtStatusFailed(query.findNearestPoly(point, extent, &filter, &ref, nearest, &overPoly)) ||
|
||||
!ref || !overPoly || std::fabs(nearest[1] - point[1]) > kCoverHeightToleranceM) return 0;
|
||||
point[1] = nearest[1]; // Keep the XZ from the collision probe.
|
||||
return ref;
|
||||
}
|
||||
|
||||
static bool clearCoverCorridor(dtNavMeshQuery &query, const dtQueryFilter &filter,
|
||||
dtPolyRef startRef, const float *start, const float *end, dtPolyRef endRef = 0) {
|
||||
float fraction = 0.0f, normal[3];
|
||||
dtPolyRef trail[32];
|
||||
int count = 0;
|
||||
dtStatus status = query.raycast(startRef, start, end, &filter, &fraction, normal, trail, &count, 32);
|
||||
return dtStatusSucceed(status) && !dtStatusDetail(status, DT_BUFFER_TOO_SMALL) &&
|
||||
fraction >= 1.0f && (!endRef || (count > 0 && trail[count - 1] == endRef));
|
||||
}
|
||||
|
||||
static bool verifyCover(WtoCoverPoint &cover, dtNavMeshQuery &query,
|
||||
const dtQueryFilter &filter, float retreatMinM) {
|
||||
dtPolyRef startRef = verifyCoverPosition(query, filter, cover.pos);
|
||||
if (!startRef) return false;
|
||||
|
||||
const float heading = std::atan2(-cover.coverDir[1], -cover.coverDir[0]);
|
||||
bool hasRetreat = false;
|
||||
for (float offset : { 0.0f, 0.45f, -0.45f }) {
|
||||
float rx = std::cos(heading + offset), rz = std::sin(heading + offset);
|
||||
float end[3] = { cover.pos[0] + rx * retreatMinM, cover.pos[1], cover.pos[2] + rz * retreatMinM };
|
||||
if (!clearCoverCorridor(query, filter, startRef, cover.pos, end)) continue;
|
||||
cover.retreatDir[0] = rx;
|
||||
cover.retreatDir[1] = rz;
|
||||
hasRetreat = true;
|
||||
break;
|
||||
}
|
||||
if (!hasRetreat) return false;
|
||||
|
||||
auto validatePeek = [&](float (&point)[3]) {
|
||||
dtPolyRef endRef = verifyCoverPosition(query, filter, point);
|
||||
return endRef && clearCoverCorridor(query, filter, startRef, cover.pos, point, endRef);
|
||||
};
|
||||
cover.hasPeekLeft = cover.hasPeekLeft && validatePeek(cover.peekLeft);
|
||||
cover.hasPeekRight = cover.hasPeekRight && validatePeek(cover.peekRight);
|
||||
return true;
|
||||
}
|
||||
|
||||
static unsigned int peekCount(const WtoCoverPoint &cover) {
|
||||
return cover.hasPeekLeft + cover.hasPeekRight;
|
||||
}
|
||||
|
||||
static std::vector<WtoCoverPoint> filterAndDedupeCovers(
|
||||
const std::vector<WtoCoverPoint> &raw,
|
||||
dtNavMeshQuery *q1
|
||||
const std::vector<WtoCoverPoint> &raw, dtNavMeshQuery &query, const CoverBake &tuning
|
||||
) {
|
||||
dtQueryFilter filter;
|
||||
filter.setIncludeFlags(WTO_FLAG_WALK);
|
||||
filter.setExcludeFlags(WTO_FLAG_DEEP_WATER | WTO_FLAG_BLOCKED);
|
||||
const float ext[3] = { 3.0f, 4.0f, 3.0f };
|
||||
std::vector<WtoCoverPoint> usable;
|
||||
for (WtoCoverPoint cover : raw) {
|
||||
if (verifyCover(cover, query, filter, tuning.retreatMinM)) usable.push_back(cover);
|
||||
}
|
||||
|
||||
/* Sort by quality: hulldown & corner-peek full covers first, then wider/taller covers */
|
||||
std::vector<size_t> order(raw.size());
|
||||
for (size_t i = 0; i < raw.size(); ++i) order[i] = i;
|
||||
std::sort(order.begin(), order.end(), [&](size_t a, size_t b) {
|
||||
auto score = [](const WtoCoverPoint &c) {
|
||||
float s = c.widthM * 2.0f + std::min(c.heightM, 4.0f);
|
||||
if (c.kind == WTO_COVER_HULLDOWN) s += 3.0f;
|
||||
if (c.hasPeekLeft || c.hasPeekRight) s += 2.5f;
|
||||
return s;
|
||||
std::sort(usable.begin(), usable.end(), [](const WtoCoverPoint &a, const WtoCoverPoint &b) {
|
||||
auto rank = [](const WtoCoverPoint &c) {
|
||||
return std::make_tuple(peekCount(c), c.widthM, c.heightM);
|
||||
};
|
||||
return score(raw[a]) > score(raw[b]);
|
||||
if (rank(a) != rank(b)) return rank(a) > rank(b);
|
||||
return std::tie(a.pos[0], a.pos[1], a.pos[2], a.coverDir[0], a.coverDir[1]) <
|
||||
std::tie(b.pos[0], b.pos[1], b.pos[2], b.coverDir[0], b.coverDir[1]);
|
||||
});
|
||||
|
||||
std::vector<WtoCoverPoint> kept;
|
||||
kept.reserve(2048);
|
||||
static const float kRetreatAngles[3] = { 0.0f, 0.45f, -0.45f };
|
||||
|
||||
for (size_t idx : order) {
|
||||
WtoCoverPoint cp = raw[idx];
|
||||
bool dup = false;
|
||||
for (const auto &k : kept) {
|
||||
float dx = k.pos[0] - cp.pos[0];
|
||||
float dz = k.pos[2] - cp.pos[2];
|
||||
if (dx * dx + dz * dz < 49.0f) { /* 7 m dedupe radius */
|
||||
float dot = k.coverDir[0] * cp.coverDir[0] + k.coverDir[1] * cp.coverDir[1];
|
||||
if (dot > 0.78f && k.kind == cp.kind) { dup = true; break; }
|
||||
}
|
||||
}
|
||||
if (dup) continue;
|
||||
|
||||
/* Verify parking spot is on T1 navmesh and has >= 8 m safe retreat corridor */
|
||||
dtPolyRef startRef = 0;
|
||||
float nearest[3];
|
||||
if (dtStatusFailed(q1->findNearestPoly(cp.pos, ext, &filter, &startRef, nearest)) || !startRef) continue;
|
||||
float snapSq = (nearest[0] - cp.pos[0]) * (nearest[0] - cp.pos[0]) + (nearest[2] - cp.pos[2]) * (nearest[2] - cp.pos[2]);
|
||||
if (snapSq > 9.0f) continue;
|
||||
cp.pos[0] = nearest[0]; cp.pos[1] = nearest[1]; cp.pos[2] = nearest[2];
|
||||
|
||||
bool retreatOk = false;
|
||||
float baseAng = std::atan2(-cp.coverDir[1], -cp.coverDir[0]);
|
||||
for (float da : kRetreatAngles) {
|
||||
float rx = std::cos(baseAng + da);
|
||||
float rz = std::sin(baseAng + da);
|
||||
float endPt[3] = { cp.pos[0] + rx * 8.5f, cp.pos[1], cp.pos[2] + rz * 8.5f };
|
||||
float t = 0.0f;
|
||||
float hitNorm[3];
|
||||
dtPolyRef polys[32];
|
||||
int npolys = 0;
|
||||
if (dtStatusSucceed(q1->raycast(startRef, cp.pos, endPt, &filter, &t, hitNorm, polys, &npolys, 32))) {
|
||||
if (t >= 0.92f) {
|
||||
cp.retreatDir[0] = rx;
|
||||
cp.retreatDir[1] = rz;
|
||||
retreatOk = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!retreatOk) continue;
|
||||
kept.push_back(cp);
|
||||
const float maxLevelDifference = kTiers[WTO_NAV_TIER_STANDARD].maxClimb;
|
||||
for (const auto &cover : usable) {
|
||||
bool duplicate = std::any_of(kept.begin(), kept.end(), [&](const WtoCoverPoint &other) {
|
||||
if (cover.kind != other.kind || std::fabs(cover.pos[1] - other.pos[1]) > maxLevelDifference) return false;
|
||||
float dx = other.pos[0] - cover.pos[0], dz = other.pos[2] - cover.pos[2];
|
||||
float dot = other.coverDir[0] * cover.coverDir[0] + other.coverDir[1] * cover.coverDir[1];
|
||||
// Adjacent samples facing the same way are redundant. Perpendicular and opposite faces remain usable.
|
||||
return dx * dx + dz * dz < 49.0f && dot > 0.78f;
|
||||
});
|
||||
if (!duplicate) kept.push_back(cover);
|
||||
}
|
||||
return kept;
|
||||
}
|
||||
@@ -895,7 +957,7 @@ int main(int argc, char **argv) {
|
||||
geoPath.c_str(), mapMaxX - mapMinX, mapMaxZ - mapMinZ, tilesX, tilesZ, totalTiles, geo.hdr.obsTriCount);
|
||||
|
||||
/* Bucket obstacle triangles once into overlapping tiles */
|
||||
const float pad = 4.5f;
|
||||
const float pad = coverBorderCells(pois.cover) * kCellSize;
|
||||
std::vector<std::vector<uint32_t>> buckets((size_t)totalTiles);
|
||||
for (uint32_t ti = 0; ti < geo.hdr.obsTriCount; ++ti) {
|
||||
const auto &t = geo.obsTris[ti];
|
||||
@@ -950,7 +1012,7 @@ int main(int argc, char **argv) {
|
||||
if (idx >= totalTiles) break;
|
||||
int tx = idx % tilesX;
|
||||
int tz = idx / tilesX;
|
||||
auto br = buildSingleTile(geo, buckets[(size_t)idx], kTiers[tierIdx], tx, tz, mapMinX, mapMinZ, doCovers);
|
||||
auto br = buildSingleTile(geo, buckets[(size_t)idx], kTiers[tierIdx], tx, tz, mapMinX, mapMinZ, pois.cover, doCovers);
|
||||
tierTiles[tierIdx][(size_t)idx] = br.tile;
|
||||
if (doCovers && !br.covers.empty()) {
|
||||
localCov.insert(localCov.end(), br.covers.begin(), br.covers.end());
|
||||
@@ -984,14 +1046,15 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
/* Dedupe + retreat-verify tank covers on Tier 1 */
|
||||
std::vector<WtoCoverPoint> covers = filterAndDedupeCovers(rawCovers, queries[WTO_NAV_TIER_STANDARD]);
|
||||
int fullCnt = 0, hdCnt = 0, peekCnt = 0;
|
||||
std::vector<WtoCoverPoint> covers = filterAndDedupeCovers(rawCovers, *queries[WTO_NAV_TIER_STANDARD], pois.cover);
|
||||
int fullCnt = 0, hdCnt = 0, peekCnt = 0, bothPeeks = 0;
|
||||
for (const auto &c : covers) {
|
||||
if (c.kind == WTO_COVER_FULL) fullCnt++; else hdCnt++;
|
||||
if (c.hasPeekLeft || c.hasPeekRight) peekCnt++;
|
||||
if (peekCount(c) > 0) peekCnt++;
|
||||
if (peekCount(c) == 2) bothPeeks++;
|
||||
}
|
||||
std::printf(" Covers: raw=%zu -> verified=%zu (full=%d, hulldown=%d, withPeek=%d)\n",
|
||||
rawCovers.size(), covers.size(), fullCnt, hdCnt, peekCnt);
|
||||
std::printf(" Covers: raw=%zu -> verified=%zu (full=%d, hulldown=%d, withPeek=%d, bothPeeks=%d)\n",
|
||||
rawCovers.size(), covers.size(), fullCnt, hdCnt, peekCnt, bothPeeks);
|
||||
|
||||
/* Build 8m tactical grid (clearance + simple terrain-horizon exposure) */
|
||||
const float tacCellM = 8.0f;
|
||||
@@ -1165,12 +1228,12 @@ int main(int argc, char **argv) {
|
||||
if (!outPreviewPath.empty()) {
|
||||
const uint32_t pw = 1024, ph = 1024;
|
||||
std::vector<float> pHeight((size_t)pw * ph);
|
||||
std::vector<uint8_t> pFlags((size_t)pw * ph, 0);
|
||||
std::vector<uint16_t> pFlags((size_t)pw * ph, 0);
|
||||
/* Bit 0: T0 walkable, Bit 1: T1 walkable, Bit 2: T2 walkable, Bit 3: shallow water,
|
||||
* Bit 4: deep water, Bit 5: chokepoint, Bit 6: crushable/forest, Bit 7: bridge */
|
||||
* Bit 4: deep water, Bit 5: chokepoint, Bit 6: crushable/forest, Bit 7: bridge, Bit 8: road */
|
||||
dtQueryFilter anyF;
|
||||
anyF.setIncludeFlags(0xFFFF);
|
||||
anyF.setExcludeFlags(0);
|
||||
anyF.setExcludeFlags(WTO_FLAG_BLOCKED);
|
||||
const float ext[3] = { 0.8f, 6.0f, 0.8f };
|
||||
|
||||
std::atomic<uint32_t> nextRow{ 0 };
|
||||
@@ -1190,7 +1253,7 @@ int main(int argc, char **argv) {
|
||||
float wx = mapMinX + ((rx + 0.5f) / pw) * (mapMaxX - mapMinX);
|
||||
float gy = geo.sampleHeight(wx, wz);
|
||||
pHeight[(size_t)rz * pw + rx] = gy;
|
||||
uint8_t fl = 0;
|
||||
uint16_t fl = 0;
|
||||
float pt[3] = { wx, gy, wz };
|
||||
for (uint32_t ti = 0; ti < WTO_NAV_TIER_COUNT; ++ti) {
|
||||
dtPolyRef ref = 0;
|
||||
@@ -1208,6 +1271,7 @@ int main(int argc, char **argv) {
|
||||
else if (area == WTO_AREA_CHOKEPOINT) fl |= 32;
|
||||
else if (area == WTO_AREA_CRUSHABLE || area == WTO_AREA_FOREST) fl |= 64;
|
||||
else if (area == WTO_AREA_BRIDGE) fl |= 128;
|
||||
else if (area == WTO_AREA_ROAD) fl |= 256;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1222,7 +1286,7 @@ int main(int argc, char **argv) {
|
||||
for (auto &th : pWorkers) th.join();
|
||||
|
||||
std::ofstream pout(outPreviewPath, std::ios::binary);
|
||||
pout.write("WTOPREV1", 8);
|
||||
pout.write("WTOPREV2", 8);
|
||||
pout.write(reinterpret_cast<const char *>(&pw), 4);
|
||||
pout.write(reinterpret_cast<const char *>(&ph), 4);
|
||||
pout.write(reinterpret_cast<const char *>(&mapMinX), 4);
|
||||
@@ -1231,7 +1295,7 @@ int main(int argc, char **argv) {
|
||||
pout.write(reinterpret_cast<const char *>(&mapMaxZ), 4);
|
||||
pout.write(reinterpret_cast<const char *>(&geo.hdr.waterLevel), 4);
|
||||
pout.write(reinterpret_cast<const char *>(pHeight.data()), pHeight.size() * sizeof(float));
|
||||
pout.write(reinterpret_cast<const char *>(pFlags.data()), pFlags.size());
|
||||
pout.write(reinterpret_cast<const char *>(pFlags.data()), pFlags.size() * sizeof(uint16_t));
|
||||
uint32_t cCnt = (uint32_t)covers.size();
|
||||
pout.write(reinterpret_cast<const char *>(&cCnt), 4);
|
||||
if (cCnt > 0) pout.write(reinterpret_cast<const char *>(covers.data()), cCnt * sizeof(WtoCoverPoint));
|
||||
|
||||
Reference in new issue
Block a user