diff --git a/apps/server/tuning/ground-ai.jsonc b/apps/server/tuning/ground-ai.jsonc index 723df8f..b6353dd 100644 --- a/apps/server/tuning/ground-ai.jsonc +++ b/apps/server/tuning/ground-ai.jsonc @@ -99,7 +99,9 @@ "minCoverHeightM": 1.55, "peekMinM": 3.5, "peekMaxM": 6.0, - "retreatMinM": 8.0 + "retreatMinM": 8.0, + // Low dry-stone fence models observed to give unreliable shot protection. + "excludedCoverRendinst": ["stone_fence_a", "damaged_stone_fence_a", "damaged_stone_fence_b"] }, "navigation": { diff --git a/native/wto-nav/wto_nav_format.h b/native/wto-nav/wto_nav_format.h index 07aa7f5..ec18188 100644 --- a/native/wto-nav/wto_nav_format.h +++ b/native/wto-nav/wto_nav_format.h @@ -14,7 +14,7 @@ extern "C" { #endif #define WTO_GEO_MAGIC "WTOGEO\0\1" -#define WTO_GEO_VERSION 1u +#define WTO_GEO_VERSION 2u #define WTO_GEO_FLAG_ROAD_MASK 0x01u #define WTO_NAV_MAGIC "WTONAV\0\1" @@ -56,7 +56,8 @@ enum WtoObstacleClass { WTO_OBS_BRIDGE_DECK = 1, /* Walkable bridge decks and ramps */ WTO_OBS_CRUSHABLE = 2, /* Wooden fences, hedges, low-HP destructibles */ WTO_OBS_TREE_THICK = 3, /* Thick immovable tree trunks (r >= 0.25 m) */ - WTO_OBS_TREE_THIN = 4 /* Thin tree trunks / saplings (high-cost forest) */ + WTO_OBS_TREE_THIN = 4, /* Thin tree trunks / saplings (high-cost forest) */ + WTO_OBS_HARD_NO_COVER = 5 /* Vehicle collision with no eligible cover surface */ }; /* Baked tank cover classifications. */ @@ -99,7 +100,7 @@ typedef struct WtoCoverPoint { float peekLeft[3]; /* World XYZ left corner peek spot (valid if hasPeekLeft) */ float peekRight[3]; /* World XYZ right corner peek spot (valid if hasPeekRight) */ float halfAngleDeg; /* Effective covered half-angle around coverDir */ - float heightM; /* Measured hard-obstacle height above ground at cover */ + float heightM; /* Continuous cover height above the final parking point */ float widthM; /* Continuous hard-obstacle width */ uint8_t kind; /* WtoCoverKind */ uint8_t hasPeekLeft; /* 1 if peekLeft is valid and navigable */ diff --git a/tools/navgen/bake/bake.cpp b/tools/navgen/bake/bake.cpp index 2dcfb63..7a91552 100644 --- a/tools/navgen/bake/bake.cpp +++ b/tools/navgen/bake/bake.cpp @@ -17,6 +17,7 @@ #include "DetourCommon.h" #include +#include #include #include #include @@ -25,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -83,7 +85,14 @@ 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 kCoverProbeStartM = 0.6f; static constexpr float kCoverScanM = 5.2f; +static constexpr float kHullLowerM = 0.65f; +static constexpr float kHullUpperM = 1.80f; +static constexpr float kTurretLowerM = 2.05f; +static constexpr float kHullColumnSpacingM = 0.9f; +static constexpr float kExtraCoverColumnM = 2.6f; +static constexpr float kGeometryToleranceM = 0.001f; static constexpr float kPeekSightM = 14.0f; static constexpr float kHullDownSightM = 22.0f; static constexpr float kCoverHeightToleranceM = 1.2f; @@ -127,7 +136,7 @@ static bool loadGeo(const std::string &path, GeoData &out) { std::ifstream in(path, std::ios::binary); if (!in) return false; in.read(reinterpret_cast(&out.hdr), sizeof(WtoGeoHeader)); - if (!in || std::memcmp(out.hdr.magic, WTO_GEO_MAGIC, 8) != 0 || out.hdr.version != WTO_GEO_VERSION) { + if (!in || std::memcmp(out.hdr.magic, WTO_GEO_MAGIC, 8) != 0 || (out.hdr.version != 1u && out.hdr.version != WTO_GEO_VERSION)) { std::fprintf(stderr, "Invalid .wtogeo header: %s\n", path.c_str()); return false; } @@ -258,45 +267,26 @@ static inline float triNormalYRightHanded(const Vec3 &a, const Vec3 &b, const Ve return len > 1e-6f ? (ny / len) : 0.0f; } -/* Query whether a hard solid span exists in rcHeightfield at (wx, wz) intersecting [yLo, yHi]. */ -static bool hfHasSolidInBand(const rcHeightfield &hf, float wx, float wz, float yLo, float yHi, float *outTopY = nullptr) { - int gx = (int)std::floor((wx - hf.bmin[0]) / hf.cs); - int gz = (int)std::floor((wz - hf.bmin[2]) / hf.cs); - if (gx < 0 || gz < 0 || gx >= hf.width || gz >= hf.height) return false; - int sminBand = (int)std::floor((yLo - hf.bmin[1]) / hf.ch); - int smaxBand = (int)std::ceil((yHi - hf.bmin[1]) / hf.ch); - float maxTop = -1e9f; - bool hit = false; - for (const rcSpan *s = hf.spans[gx + gz * hf.width]; s; s = s->next) { - if ((int)s->smax >= sminBand && (int)s->smin <= smaxBand) { - hit = true; - float topY = hf.bmin[1] + s->smax * hf.ch; - if (topY > maxTop) maxTop = topY; - } +static bool hasCoverVoxel(const rcHeightfield &field, float x, float z, float low, float high) { + int gx = (int)std::floor((x - field.bmin[0]) / field.cs); + int gz = (int)std::floor((z - field.bmin[2]) / field.cs); + if (gx < 0 || gz < 0 || gx >= field.width || gz >= field.height) return false; + int first = (int)std::floor((low - field.bmin[1]) / field.ch); + int last = (int)std::ceil((high - field.bmin[1]) / field.ch); + for (const rcSpan *span = field.spans[gx + gz * field.width]; span; span = span->next) { + if ((int)span->smax >= first && (int)span->smin <= last) return true; } - if (hit && outTopY) *outTopY = maxTop; - return hit; + return false; } -/* Raycast along XZ inside tile's hard-obstacle heightfield to check occlusion in [yLo, yHi]. */ -static bool hfTraceBand(const rcHeightfield &hardHf, const GeoData &geo, - float ox, float oz, float dx, float dz, - float maxDist, float yLo, float yHi, float *outTopY = nullptr) { - float step = hardHf.cs * 0.85f; - for (float d = 0.6f; d <= maxDist; d += step) { - float px = ox + dx * d; - float pz = oz + dz * d; - float topY = -1e9f; - if (hfHasSolidInBand(hardHf, px, pz, yLo, yHi, &topY)) { - if (outTopY) *outTopY = topY; - return true; - } - /* Terrain ridge / cliff rise above yLo also acts as natural hard earth cover! */ - float gy = geo.sampleHeight(px, pz); - if (gy >= yLo + 0.35f) { - if (outTopY) *outTopY = gy; - return true; - } +// The voxel bands only nominate a pose. The final test is done on source geometry after snapping Y. +static bool hasCoverCandidate(const rcHeightfield &field, const GeoData &geo, + float x, float z, float dx, float dz, float baseY) { + float low = baseY + kHullLowerM; + float high = baseY + kHullUpperM; + for (float d = kCoverProbeStartM; d <= kCoverScanM; d += field.cs * 0.85f) { + float px = x + dx * d, pz = z + dz * d; + if (hasCoverVoxel(field, px, pz, low, high) || geo.sampleHeight(px, pz) >= low + 0.35f) return true; } return false; } @@ -321,36 +311,9 @@ static bool isCoverBoundary(const rcCompactHeightfield &field, int x, int z, con 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 &covers + const rcConfig &cfg, std::vector &covers ) { static const float directions[8][2] = { { 1.0f, 0.0f }, { 0.70710678f, 0.70710678f }, { 0.0f, 1.0f }, { -0.70710678f, 0.70710678f }, @@ -366,43 +329,19 @@ static void bakeTileCovers( 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; + int hullHits = 0; 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); + float offset = column * kHullColumnSpacingM; + hullHits += hasCoverCandidate(hard, geo, pos.x - dz * offset, pos.z + dx * offset, dx, dz, pos.y); } - if (hullHits < 4 || totalHeight < tuning.minCoverHeightM * hullHits) continue; + if (hullHits < 4) 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); + WtoCoverPoint pose{}; + pose.pos[0] = pos.x; pose.pos[1] = pos.y; pose.pos[2] = pos.z; + pose.coverDir[0] = dx; pose.coverDir[1] = dz; + covers.push_back(pose); } } } @@ -546,7 +485,7 @@ static TileBakeResult buildSingleTile( const Vec3 &v0 = geo.obsVerts[t.i0]; const Vec3 &v1 = geo.obsVerts[t.i1]; const Vec3 &v2 = geo.obsVerts[t.i2]; - if (t.cls == WTO_OBS_HARD_SOLID || t.cls == WTO_OBS_TREE_THICK) { + if (t.cls == WTO_OBS_HARD_SOLID || t.cls == WTO_OBS_HARD_NO_COVER || t.cls == WTO_OBS_TREE_THICK) { rcRasterizeTriangle(&ctx, &v0.x, &v1.x, &v2.x, RC_NULL_AREA, *solid, cfg.walkableClimb); if (hardHf && t.cls == WTO_OBS_HARD_SOLID) { rcRasterizeTriangle(&ctx, &v0.x, &v1.x, &v2.x, 1, *hardHf, 1); @@ -614,7 +553,7 @@ static TileBakeResult buildSingleTile( } } - if (hardHf) bakeTileCovers(*chf, *hardHf, geo, cfg, coverTuning, res.covers); + if (hardHf) bakeTileCovers(*chf, *hardHf, geo, cfg, res.covers); rcFreeHeightField(hardHf); hardHf = nullptr; @@ -818,6 +757,173 @@ static void markChokepoints(dtNavMesh *mesh, dtNavMeshQuery *query, float chokeD } } +struct CoverRay { + float x, z, dx, dz, range; +}; + +struct CoverSpan { + float bottom, top; +}; + +// Cut each triangle by the vertical plane through the probe, then clip the cut +// segment to the horizontal ray length. Its Y interval is the silhouette seen +// from the parking point at this column. +static bool coverTriangleSpan(const CoverRay &ray, const Vec3 &a, const Vec3 &b, const Vec3 &c, + CoverSpan &out) { + const Vec3 points[3] = { a, b, c }; + float along[3], across[3]; + for (int i = 0; i < 3; ++i) { + float x = points[i].x - ray.x, z = points[i].z - ray.z; + along[i] = x * ray.dx + z * ray.dz; + across[i] = z * ray.dx - x * ray.dz; + } + + struct CutPoint { float distance, y; }; + CutPoint cut[3]; + int count = 0; + for (int i = 0; i < 3; ++i) { + const int j = (i + 1) % 3; + if (across[i] == 0.0f) { + cut[count++] = { along[i], points[i].y }; + } else if ((across[i] < 0.0f && across[j] > 0.0f) || (across[i] > 0.0f && across[j] < 0.0f)) { + float fraction = across[i] / (across[i] - across[j]); + cut[count++] = { + along[i] + (along[j] - along[i]) * fraction, + points[i].y + (points[j].y - points[i].y) * fraction + }; + } + } + if (count != 2) return false; + if (cut[0].distance > cut[1].distance) std::swap(cut[0], cut[1]); + if (cut[1].distance < kCoverProbeStartM || cut[0].distance > ray.range) return false; + + float length = cut[1].distance - cut[0].distance; + if (length > kGeometryToleranceM) { + float from = std::max(kCoverProbeStartM, cut[0].distance); + float to = std::min(ray.range, cut[1].distance); + float rise = cut[1].y - cut[0].y; + float y0 = cut[0].y + (from - cut[0].distance) / length * rise; + float y1 = cut[0].y + (to - cut[0].distance) / length * rise; + out = { std::min(y0, y1), std::max(y0, y1) }; + } else { + out = { std::min(cut[0].y, cut[1].y), std::max(cut[0].y, cut[1].y) }; + } + return true; +} + +class CoverGeometry { +public: + explicit CoverGeometry(const GeoData &geo) : geo(geo) { + cols = std::max(1, (int)std::ceil((geo.hdr.boundsMax[0] - geo.hdr.boundsMin[0]) / indexCellM)); + rows = std::max(1, (int)std::ceil((geo.hdr.boundsMax[1] - geo.hdr.boundsMin[1]) / indexCellM)); + cells.resize((size_t)cols * rows); + for (uint32_t i = 0; i < geo.obsTris.size(); ++i) { + const auto &tri = geo.obsTris[i]; + if (tri.cls != WTO_OBS_HARD_SOLID) continue; + + const auto &a = geo.obsVerts[tri.i0]; + const auto &b = geo.obsVerts[tri.i1]; + const auto &c = geo.obsVerts[tri.i2]; + int firstX = column(std::min({ a.x, b.x, c.x })); + int lastX = column(std::max({ a.x, b.x, c.x })); + int firstZ = row(std::min({ a.z, b.z, c.z })); + int lastZ = row(std::max({ a.z, b.z, c.z })); + for (int z = firstZ; z <= lastZ; ++z) { + for (int x = firstX; x <= lastX; ++x) cells[(size_t)z * cols + x].push_back(i); + } + } + } + + void collect(const CoverRay &ray, float lower, std::vector &spans) const { + spans.clear(); + + const float endX = ray.x + ray.dx * ray.range; + const float endZ = ray.z + ray.dz * ray.range; + float terrainTop = geo.sampleHeight(endX, endZ); + for (float d = kCoverProbeStartM; d < ray.range; d += kCellSize * 0.85f) { + terrainTop = std::max(terrainTop, geo.sampleHeight(ray.x + ray.dx * d, ray.z + ray.dz * d)); + } + if (terrainTop >= lower) spans.push_back({ lower, terrainTop }); + + const float startX = ray.x + ray.dx * kCoverProbeStartM; + const float startZ = ray.z + ray.dz * kCoverProbeStartM; + const float xmin = std::min(startX, endX), xmax = std::max(startX, endX); + const float zmin = std::min(startZ, endZ), zmax = std::max(startZ, endZ); + const int lastX = column(xmax), lastZ = row(zmax); + for (int z = row(zmin); z <= lastZ; ++z) { + for (int x = column(xmin); x <= lastX; ++x) { + for (uint32_t i : cells[(size_t)z * cols + x]) { + const auto &tri = geo.obsTris[i]; + const auto &a = geo.obsVerts[tri.i0]; + const auto &b = geo.obsVerts[tri.i1]; + const auto &c = geo.obsVerts[tri.i2]; + if (std::max({ a.y, b.y, c.y }) < lower || + std::max({ a.x, b.x, c.x }) < xmin || std::min({ a.x, b.x, c.x }) > xmax || + std::max({ a.z, b.z, c.z }) < zmin || std::min({ a.z, b.z, c.z }) > zmax) continue; + + CoverSpan span; + if (coverTriangleSpan(ray, a, b, c, span) && span.top >= lower) spans.push_back(span); + } + } + } + } + +private: + static constexpr float indexCellM = kTileWorldSize / 16.0f; + const GeoData &geo; + int cols = 0, rows = 0; + std::vector> cells; + + int column(float x) const { return std::clamp((int)std::floor((x - geo.hdr.boundsMin[0]) / indexCellM), 0, cols - 1); } + int row(float z) const { return std::clamp((int)std::floor((z - geo.hdr.boundsMin[1]) / indexCellM), 0, rows - 1); } +}; + +static float coverColumnHeight(const CoverGeometry &geometry, const WtoCoverPoint &cover, float offset, + std::vector &spans) { + float dx = cover.coverDir[0], dz = cover.coverDir[1]; + float lower = cover.pos[1] + kHullLowerM; + geometry.collect({ cover.pos[0] - dz * offset, cover.pos[2] + dx * offset, dx, dz, kCoverScanM }, lower, spans); + std::sort(spans.begin(), spans.end(), [](const CoverSpan &a, const CoverSpan &b) { return a.bottom < b.bottom; }); + + if (spans.empty() || spans.front().bottom > lower + kGeometryToleranceM) return -std::numeric_limits::infinity(); + float top = lower; + for (const auto &span : spans) { + if (span.bottom > top + kGeometryToleranceM) break; + top = std::max(top, span.top); + } + return top - cover.pos[1]; +} + +static bool coveredSightBand(const CoverGeometry &geometry, const float *pos, const float *direction, float range, + float low, float high, std::vector &spans) { + geometry.collect({ pos[0], pos[2], direction[0], direction[1], range }, pos[1] + low, spans); + return std::any_of(spans.begin(), spans.end(), [&](const CoverSpan &span) { return span.bottom <= pos[1] + high; }); +} + +static bool qualifyCover(WtoCoverPoint &cover, const CoverGeometry &geometry, + const CoverBake &tuning, std::vector &spans) { + std::array heights; + for (size_t i = 0; i < heights.size(); ++i) { + heights[i] = coverColumnHeight(geometry, cover, ((int)i - 2) * kHullColumnSpacingM, spans); + } + std::sort(heights.begin(), heights.end(), [](float a, float b) { return a > b; }); + + // Four columns must each hide the hull through the required height. + if (heights[3] < tuning.minCoverHeightM) return false; + cover.heightM = heights[3]; + + const bool full = heights[2] >= kTurretLowerM; + if (!full && coveredSightBand(geometry, cover.pos, cover.coverDir, kHullDownSightM, 2.15f, 2.75f, spans)) return false; + + cover.kind = full ? WTO_COVER_FULL : WTO_COVER_HULLDOWN; + cover.widthM = 4.0f * kHullColumnSpacingM; + for (float offset : { -kExtraCoverColumnM, kExtraCoverColumnM }) { + if (coverColumnHeight(geometry, cover, offset, spans) >= tuning.minCoverHeightM) cover.widthM += 1.0f; + } + cover.halfAngleDeg = cover.widthM >= 5.0f ? 42.0f : 32.0f; + return true; +} + static dtPolyRef verifyCoverPosition(dtNavMeshQuery &query, const dtQueryFilter &filter, float (&point)[3]) { const float extent[3] = { kCellSize, kCoverHeightToleranceM, kCellSize }; dtPolyRef ref = 0; @@ -839,16 +945,36 @@ static bool clearCoverCorridor(dtNavMeshQuery &query, const dtQueryFilter &filte fraction >= 1.0f && (!endRef || (count > 0 && trail[count - 1] == endRef)); } -static bool verifyCover(WtoCoverPoint &cover, dtNavMeshQuery &query, - const dtQueryFilter &filter, float retreatMinM) { +static bool findCornerPeek(WtoCoverPoint &cover, dtNavMeshQuery &query, const dtQueryFilter &filter, + dtPolyRef startRef, const CoverGeometry &geometry, const CoverBake &tuning, + float side, float (&point)[3], std::vector &spans) { + dtQueryFilter dry = filter; + dry.setExcludeFlags(filter.getExcludeFlags() | WTO_FLAG_SHALLOW_WATER); + for (float distance = tuning.peekMinM; distance <= tuning.peekMaxM; distance += kCellSize) { + float candidate[3] = { + cover.pos[0] - cover.coverDir[1] * side * distance, + cover.pos[1], + cover.pos[2] + cover.coverDir[0] * side * distance + }; + dtPolyRef endRef = verifyCoverPosition(query, dry, candidate); + if (!endRef || !clearCoverCorridor(query, filter, startRef, cover.pos, candidate, endRef)) continue; + if (coveredSightBand(geometry, candidate, cover.coverDir, kPeekSightM, 1.8f, 2.6f, spans)) continue; + std::copy(candidate, candidate + 3, point); + return true; + } + return false; +} + +static bool verifyCover(WtoCoverPoint &cover, dtNavMeshQuery &query, const dtQueryFilter &filter, + const CoverGeometry &geometry, const CoverBake &tuning, std::vector &spans) { dtPolyRef startRef = verifyCoverPosition(query, filter, cover.pos); - if (!startRef) return false; + if (!startRef || !qualifyCover(cover, geometry, tuning, spans)) 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 }; + float end[3] = { cover.pos[0] + rx * tuning.retreatMinM, cover.pos[1], cover.pos[2] + rz * tuning.retreatMinM }; if (!clearCoverCorridor(query, filter, startRef, cover.pos, end)) continue; cover.retreatDir[0] = rx; cover.retreatDir[1] = rz; @@ -857,12 +983,10 @@ static bool verifyCover(WtoCoverPoint &cover, dtNavMeshQuery &query, } 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); + if (cover.kind == WTO_COVER_FULL) { + cover.hasPeekLeft = findCornerPeek(cover, query, filter, startRef, geometry, tuning, -1.0f, cover.peekLeft, spans); + cover.hasPeekRight = findCornerPeek(cover, query, filter, startRef, geometry, tuning, 1.0f, cover.peekRight, spans); + } return true; } @@ -871,14 +995,16 @@ static unsigned int peekCount(const WtoCoverPoint &cover) { } static std::vector filterAndDedupeCovers( - const std::vector &raw, dtNavMeshQuery &query, const CoverBake &tuning + const std::vector &raw, dtNavMeshQuery &query, const CoverBake &tuning, const GeoData &geo ) { dtQueryFilter filter; filter.setIncludeFlags(WTO_FLAG_WALK); filter.setExcludeFlags(WTO_FLAG_DEEP_WATER | WTO_FLAG_BLOCKED); + const CoverGeometry geometry(geo); + std::vector spans; std::vector usable; for (WtoCoverPoint cover : raw) { - if (verifyCover(cover, query, filter, tuning.retreatMinM)) usable.push_back(cover); + if (verifyCover(cover, query, filter, geometry, tuning, spans)) usable.push_back(cover); } std::sort(usable.begin(), usable.end(), [](const WtoCoverPoint &a, const WtoCoverPoint &b) { @@ -925,15 +1051,25 @@ static void serializeTier(const dtNavMeshParams ¶ms, const std::vector 8192) { + std::fprintf(stderr, "Preview size must be between 1 and 8192 pixels.\n"); + return 2; + } + previewSize = (uint32_t)size; + } } if (geoPath.empty() || poisPath.empty() || outNavPath.empty()) { - std::fprintf(stderr, "Usage: wto_navgen_bake --geo --pois --out [--preview ]\n"); + std::fprintf(stderr, "Usage: wto_navgen_bake --geo --pois --out [--preview ] [--preview-size ]\n"); return 2; } @@ -1046,7 +1182,7 @@ int main(int argc, char **argv) { } /* Dedupe + retreat-verify tank covers on Tier 1 */ - std::vector covers = filterAndDedupeCovers(rawCovers, *queries[WTO_NAV_TIER_STANDARD], pois.cover); + std::vector covers = filterAndDedupeCovers(rawCovers, *queries[WTO_NAV_TIER_STANDARD], pois.cover, geo); int fullCnt = 0, hdCnt = 0, peekCnt = 0, bothPeeks = 0; for (const auto &c : covers) { if (c.kind == WTO_COVER_FULL) fullCnt++; else hdCnt++; @@ -1226,7 +1362,7 @@ int main(int argc, char **argv) { /* Write compact raster preview data for PNG visual inspection */ if (!outPreviewPath.empty()) { - const uint32_t pw = 1024, ph = 1024; + const uint32_t pw = previewSize, ph = previewSize; std::vector pHeight((size_t)pw * ph); std::vector pFlags((size_t)pw * ph, 0); /* Bit 0: T0 walkable, Bit 1: T1 walkable, Bit 2: T2 walkable, Bit 3: shallow water, diff --git a/tools/navgen/extract/src/main.rs b/tools/navgen/extract/src/main.rs index b10bf11..1016274 100644 --- a/tools/navgen/extract/src/main.rs +++ b/tools/navgen/extract/src/main.rs @@ -16,7 +16,7 @@ use wt_unpack::vromfs::Vromfs; mod phys_map; const WTO_GEO_MAGIC: &[u8; 8] = b"WTOGEO\0\x01"; -const WTO_GEO_VERSION: u32 = 1; +const WTO_GEO_VERSION: u32 = 2; const WTO_GEO_FLAG_ROAD_MASK: u32 = 1; const OBS_HARD_SOLID: u8 = 0; @@ -24,6 +24,7 @@ const OBS_BRIDGE_DECK: u8 = 1; const OBS_CRUSHABLE: u8 = 2; const OBS_TREE_THICK: u8 = 3; const OBS_TREE_THIN: u8 = 4; +const OBS_HARD_NO_COVER: u8 = 5; #[derive(Debug, Deserialize)] struct LevelPois { @@ -34,6 +35,14 @@ struct LevelPois { bounds_min: [f32; 2], #[serde(rename = "boundsMax")] bounds_max: [f32; 2], + #[serde(rename = "coverBake", default)] + cover: CoverBakeInput, +} + +#[derive(Debug, Default, Deserialize)] +struct CoverBakeInput { + #[serde(rename = "excludedCoverRendinst", default)] + excluded_rendinst: Vec, } #[derive(Debug, Clone)] @@ -264,7 +273,7 @@ fn tri_normal_y(a: [f32; 3], b: [f32; 3], c: [f32; 3]) -> f32 { if len > 1e-6 { (ny / len).abs() } else { 0.0 } } -fn build_cached_collision(c: &Collision, meta: &PoolMeta) -> Option { +fn build_cached_collision(c: &Collision, meta: &PoolMeta, cover_allowed: bool) -> Option { if meta.is_soft_foliage { return None; } @@ -283,6 +292,16 @@ fn build_cached_collision(c: &Collision, meta: &PoolMeta) -> Option= 0.72 { + OBS_BRIDGE_DECK + } else if meta.is_crushable { + OBS_CRUSHABLE + } else { + hard_class + } + }; match n.kind { NodeKind::Mesh | NodeKind::Convex => { if n.indices.len() < 3 { @@ -292,25 +311,13 @@ fn build_cached_collision(c: &Collision, meta: &PoolMeta) -> Option= 0.72 { - OBS_BRIDGE_DECK - } else if meta.is_crushable { - OBS_CRUSHABLE - } else { - OBS_HARD_SOLID - }; + let cls = obstacle_class(p0, p1, p2); tris.push((p0, p1, p2, cls)); } } NodeKind::Box => { for (p0, p1, p2) in box_tris(n) { - let cls = if meta.is_bridge && tri_normal_y(p0, p1, p2) >= 0.72 { - OBS_BRIDGE_DECK - } else if meta.is_crushable { - OBS_CRUSHABLE - } else { - OBS_HARD_SOLID - }; + let cls = obstacle_class(p0, p1, p2); tris.push((p0, p1, p2, cls)); } } @@ -427,6 +434,7 @@ fn main() -> Result<()> { } } + let excluded_cover: HashSet<_> = pois.cover.excluded_rendinst.iter().map(|name| name.to_ascii_lowercase()).collect(); let mut cached_cols: HashMap = HashMap::new(); for (pack_path, items) in &by_pack { let Ok(bytes) = fs::read(pack_path) else { continue }; @@ -434,9 +442,10 @@ fn main() -> Result<()> { for (pool_name, res_name) in items { let Some(res) = pack.find(res_name, COLLISION) else { continue }; let Ok(col) = Collision::parse(res_name, pack.data(res)) else { continue }; - let imp = aces_meta.destr_impulse.get(&pool_name.to_lowercase()).copied(); + let key = pool_name.to_ascii_lowercase(); + let imp = aces_meta.destr_impulse.get(&key).copied(); let meta = classify_pool(pool_name, imp); - if let Some(cc) = build_cached_collision(&col, &meta) { + if let Some(cc) = build_cached_collision(&col, &meta, !excluded_cover.contains(&key)) { cached_cols.insert(pool_name.clone(), cc); } } @@ -447,7 +456,7 @@ fn main() -> Result<()> { let mut verts: Vec<[f32; 3]> = Vec::with_capacity(200_000); let mut tris: Vec<([u32; 3], u8)> = Vec::with_capacity(100_000); let mut inst_used = 0usize; - let mut cls_counts = [0usize; 5]; + let mut cls_counts = [0usize; 6]; for layer in &layers { let instances = layer.instances()?; @@ -484,7 +493,7 @@ fn main() -> Result<()> { } println!( - " RIGz: pools={} cols={} noCol={} instancesInBounds={} obsTris={} [hard={} bridge={} crush={} treeThick={} treeThin={}] ({:.2}s)", + " RIGz: pools={} cols={} noCol={} instancesInBounds={} obsTris={} [hard={} bridge={} crush={} treeThick={} treeThin={} collisionOnly={}] ({:.2}s)", pool_names.len(), cached_cols.len(), no_collision, @@ -495,6 +504,7 @@ fn main() -> Result<()> { cls_counts[2], cls_counts[3], cls_counts[4], + cls_counts[5], t0.elapsed().as_secs_f32() ); diff --git a/tools/navgen/mission-bounds.mts b/tools/navgen/mission-bounds.mts index 3ebb15a..707e76f 100644 --- a/tools/navgen/mission-bounds.mts +++ b/tools/navgen/mission-bounds.mts @@ -157,6 +157,7 @@ function readCoverBake() { peekMinM, peekMaxM: cover.number('peekMaxM', { min: peekMinM }), retreatMinM: cover.number('retreatMinM', { min: 0.5 }), + excludedCoverRendinst: cover.stringList('excludedCoverRendinst'), }; }