fix(ground-ai): spread team routes, pass vehicles and keep off deep water

First in-game run of the path follower on Berlin and Poland showed whole
teams queueing on one road, bots ramming stopped teammates and wrecks,
bots clipping building corners, bots sliding into the Poland river and
the same bot getting stuck at the same spot dozens of times.

Native engine (native/wto-nav):
- Team traffic: a bot's finished path counts for its team for
  trafficHoldSec; the team's later searches pay trafficCostPerPath more
  per counted path through a polygon (up to trafficCostMaxMul), so
  teammates spread over parallel routes. A slot's previous path stops
  counting when it asks for a new one.
- Path corners are moved up to cornerOffsetM away from the edge they
  turn around, when the mesh allows both legs; off-mesh link endpoints
  stay put.
- path_request takes the team; load_map takes the new settings;
  get_stats reports live traffic paths and moved corners.

Bake (tools/navgen):
- Water deeper than navBake.maxFordDepthM (0.9 m, was a fixed 0.6 m)
  is a wall before erosion, so walkable ground keeps the tier radius
  from it, plus deepWaterMarginM on banks lower than
  deepWaterBankHeightM above the water line. Quays and ramps above it
  keep only the radius. The preview marks deep water from the terrain.

Ground AI scripts:
- The lookahead point is pulled back along the path until the mesh
  allows the straight line to it, so corners are no longer cut through
  buildings.
- New traffic module replaces the fixed sideways step: hull length and
  width along the heading; stopped vehicles, wrecks and parked players
  are passed on the side with room, or the bot stops short and after
  blockWaitSec marks the spot and plans again; moving traffic ahead is
  followed; oncoming traffic is passed in the open and in narrow
  passages the bigger vehicle goes first. Wrecks carry their heading.
- A hull pressed against a vehicle or wreck backs off after
  contactReverseSec instead of the full stuck window.
- Jams and confirmed obstacles cost far more, and a given-up place is
  all but closed (giveUpRadiusM, giveUpCostMul), so replanning leaves
  instead of returning to it.

Tuning: navEngine trafficCostPerPath, trafficCostMaxMul, trafficHoldSec,
cornerOffsetM; pathFollow stationarySpeedMps, passMarginM, passSpeedMps,
blockWaitSec, blockTtlSec, contactGapM, contactReverseSec, giveUpRadiusM,
giveUpCostMul; avoidShiftM removed; jamCostMul 4 -> 12, rubbleCostMul
8 -> 25; navBake maxFordDepthM, deepWaterMarginM, deepWaterBankHeightM
(bake only). Launcher fields and en/zh-CN labels updated (978/978 keys).

Checked: root and launcher typecheck, MSVC build with no warnings in
project code, check-patches for 2.61.0.58 and 2.61.0.60, rebake of
avg_berlin, avg_poland and avg_hurtgen (all spawn-to-zone pairs still
connected on every tier), offline run on the new avg_poland pack (eight
same-team paths spread over different roads, traffic released after
the hold time, corners moved, pump within budget), isolated game copy
build. daScript compilation and in-battle behaviour still need the
in-game run.
This commit is contained in:
N0VAN0M4LY committed 2026-12-05 21:39:07 +08:00
1 parent ba947fe5b6
commit 413bd55c54
25 files changed
+817 -161

No files matched your search

+91 -7
View File
@@ -19,8 +19,10 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <cfloat>
#include <chrono>
#include <cmath>
#include <deque>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
@@ -57,9 +59,17 @@ struct CoverBake {
float retreatMinM;
};
/* navBake water settings (ground-ai.jsonc, copied into the POI file by mission-bounds.mts). */
struct WaterBake {
float maxFordDepthM; /* Deeper water is a wall */
float marginM; /* Extra room kept from it on low banks */
float bankHeightM; /* Banks up to this height above the water line get the extra room */
};
struct PoisData {
CoverBake cover{};
float chokepointMaxWidthVehicles = 0.0f;
WaterBake water{ -1.0f, -1.0f, -1.0f };
std::vector<PoiPt> t1Spawns;
std::vector<PoiPt> t2Spawns;
std::vector<PoiPt> caps;
@@ -83,7 +93,6 @@ static constexpr float kCellSize = 0.50f;
static constexpr float kCellHeight = 0.25f;
static constexpr int kTileSize = 256; /* 128.0 m per tile */
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;
@@ -275,7 +284,15 @@ static bool loadPois(const std::string &path, PoisData &out) {
return false;
}
out.chokepointMaxWidthVehicles = readJsonNumber(content, "chokepointMaxWidthVehicles", -1.0f);
if (!(out.chokepointMaxWidthVehicles >= 1.0f && out.chokepointMaxWidthVehicles <= 4.0f)) {
out.water = {
readJsonNumber(content, "maxFordDepthM", -1.0f),
readJsonNumber(content, "deepWaterMarginM", -1.0f),
readJsonNumber(content, "deepWaterBankHeightM", -1.0f)
};
if (!(out.chokepointMaxWidthVehicles >= 1.0f && out.chokepointMaxWidthVehicles <= 4.0f) ||
!(out.water.maxFordDepthM >= 0.1f && out.water.maxFordDepthM <= 3.0f) ||
!(out.water.marginM >= 0.0f && out.water.marginM <= 10.0f) ||
!(out.water.bankHeightM >= 0.0f && out.water.bankHeightM <= 10.0f)) {
std::fprintf(stderr, "Invalid navigation baking settings in %s. Regenerate it with mission-bounds.mts.\n", path.c_str());
return false;
}
@@ -328,6 +345,65 @@ static bool isCoverArea(unsigned char area) {
return area != RC_NULL_AREA && area != WTO_AREA_DEEP_WATER && area != WTO_AREA_SHALLOW_WATER;
}
/* Turns deep water into a wall before erosion, so walkable ground keeps the tier's radius from it
* like from any obstacle. Returns the indices of the water spans for wallOffWaterBank. */
static std::vector<int> wallOffDeepWater(rcCompactHeightfield &chf) {
std::vector<int> water;
for (int i = 0; i < chf.spanCount; ++i) {
if (chf.areas[i] == WTO_AREA_DEEP_WATER) {
chf.areas[i] = RC_NULL_AREA;
water.push_back(i);
}
}
return water;
}
/* After erosion: removes every span within `reachCells` of deep water (radius plus bank margin)
* whose surface is at most `maxSpanY` (cells), i.e. low bank that slopes into the water. The walk
* follows span connections and stops at higher ground, so bridges, quays and ramps above the
* water line keep only the erosion. Each span keeps the cell of its nearest water source; a span
* is revisited when a closer source reaches it. */
static void wallOffWaterBank(rcCompactHeightfield &chf, const std::vector<int> &water, float reachCells, int maxSpanY) {
if (water.empty() || !(reachCells > 0.0f)) return;
std::vector<int> cellOf(chf.spanCount, -1);
for (int z = 0; z < chf.height; ++z) {
for (int x = 0; x < chf.width; ++x) {
const rcCompactCell &c = chf.cells[x + z * chf.width];
for (int i = (int)c.index, n = (int)(c.index + c.count); i < n; ++i) cellOf[i] = x + z * chf.width;
}
}
const float limit = reachCells * reachCells;
std::vector<int> source(chf.spanCount, -1);
std::vector<float> best(chf.spanCount, FLT_MAX);
std::deque<int> open;
for (int i : water) {
source[i] = cellOf[i];
best[i] = 0.0f;
open.push_back(i);
}
while (!open.empty()) {
const int i = open.front();
open.pop_front();
const int cell = cellOf[i];
const int x = cell % chf.width, z = cell / chf.width;
const int sx = source[i] % chf.width, sz = source[i] / chf.width;
for (int dir = 0; dir < 4; ++dir) {
const int con = rcGetCon(chf.spans[i], dir);
if (con == RC_NOT_CONNECTED) continue;
const int nx = x + rcGetDirOffsetX(dir), nz = z + rcGetDirOffsetY(dir);
const int ni = (int)chf.cells[nx + nz * chf.width].index + con;
const float d2 = (float)((nx - sx) * (nx - sx) + (nz - sz) * (nz - sz));
if (d2 > limit || d2 >= best[ni] || (int)chf.spans[ni].y > maxSpanY) continue;
best[ni] = d2;
source[ni] = source[i];
open.push_back(ni);
}
}
for (int i = 0; i < chf.spanCount; ++i) {
if (best[i] < FLT_MAX) chf.areas[i] = RC_NULL_AREA;
}
}
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);
@@ -493,7 +569,7 @@ static TileBakeResult buildSingleTile(
int tx, int tz,
float mapMinX, float mapMinZ,
const CoverBake &coverTuning, bool bakeCovers,
float chokeClearanceM
float chokeClearanceM, const WaterBake &water
) {
TileBakeResult res;
res.tile.tx = tx;
@@ -514,7 +590,7 @@ static TileBakeResult buildSingleTile(
cfg.mergeRegionArea = 400;
cfg.maxVertsPerPoly = 6;
cfg.tileSize = kTileSize;
cfg.borderSize = cfg.walkableRadius + 3;
cfg.borderSize = cfg.walkableRadius + (int)std::ceil(water.marginM / cfg.cs) + 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));
// Erosion walls off the heightfield edge; chokepoint reach must stay clear of that band.
@@ -567,7 +643,7 @@ static TileBakeResult buildSingleTile(
float cy = (a.y + b.y + c.y) * (1.0f / 3.0f);
if (hasWater) {
float depth = waterY - cy;
if (depth > kShallowWaterMaxM) return WTO_AREA_DEEP_WATER;
if (depth > water.maxFordDepthM) return WTO_AREA_DEEP_WATER;
if (depth > kShallowWaterMinM) return WTO_AREA_SHALLOW_WATER;
}
if (ny < cosRoughSlope) return WTO_AREA_ROUGH;
@@ -647,12 +723,16 @@ static TileBakeResult buildSingleTile(
rcFreeHeightField(solid);
solid = nullptr;
/* Erode walkable area by tank radius (guaranteed full hull clearance from hard obstacles!) */
/* Erode walkable area by tank radius (guaranteed full hull clearance from hard obstacles and
* deep water), then keep the bank margin from deep water on top of it. */
const std::vector<int> deepWater = wallOffDeepWater(*chf);
if (!rcErodeWalkableArea(&ctx, cfg.walkableRadius, *chf)) {
rcFreeHeightField(hardHf);
rcFreeCompactHeightfield(chf);
return res;
}
wallOffWaterBank(*chf, deepWater, (tier.agentRadius + water.marginM) / cfg.cs,
(int)std::floor((waterY + water.bankHeightM - cfg.bmin[1]) / cfg.ch));
/* Tag crushable fences & thin forest areas directly onto overlapping walkable cells (O(N), fast!) */
for (const auto &sm : softMarks) {
@@ -1339,7 +1419,8 @@ 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], tierIdx, tx, tz, mapMinX, mapMinZ, pois.cover, doCovers, chokeClearanceM);
auto br = buildSingleTile(geo, buckets[(size_t)idx], tierIdx, tx, tz, mapMinX, mapMinZ, pois.cover, doCovers,
chokeClearanceM, pois.water);
tierTiles[tierIdx][(size_t)idx] = br.tile;
if (doCovers && !br.covers.empty()) {
localCov.insert(localCov.end(), br.covers.begin(), br.covers.end());
@@ -1605,6 +1686,9 @@ int main(int argc, char **argv) {
}
}
}
/* Deep water is no longer part of any mesh; mark it from the terrain. */
if ((fl & 7u) == 0 && geo.hdr.waterLevel > -1e5f && geo.hdr.waterLevel - gy > pois.water.maxFordDepthM)
fl |= 16;
pFlags[(size_t)rz * pw + rx] = fl;
}
}