Files
WT-Offline/native/wto-nav/wto_nav_queries.cpp
T
N0VAN0M4LY ba947fe5b6 feat(ground-ai): follow baked navmesh paths with sliced searches
Native engine (native/wto-nav):
- Path searches are queued per slot and advanced with Detour's sliced
  search inside a per-frame time budget (navEngine.pathBudgetMsPerFrame),
  so a 4 km search no longer stalls a frame. The synchronous find_path
  op is gone.
- The battle's pack is read on a background thread when the mission
  finishes loading (prefetch); load_map only takes the result.
- New mesh queries: probe (surface, flags, room to the mesh edge),
  raycast, nearest walkable point, and dynamic cost circles for wrecks,
  jams and fresh obstacles that later searches route around.

Ground AI scripts:
- route picks the mesh tier by vehicle width and the cost filter by
  tracked or wheeled, plans on goal change, drift off the corridor, a
  timer or reported obstacles, and falls back to the stock road mesh
  only when the battle decided on the fallback.
- follow steers at a speed-scaled lookahead point and brakes ahead of
  sharp corners, narrow passages and the destination.
- avoid casts rays from both front hull corners that drive through
  objects the bake marks crushable, follows slower traffic, steps aside
  or yields by size priority in narrow passages.
- unstick escalates deterministically: back up along the corridor, mark
  a jam and replan, then log the spot and hand the order back.
- Flank lanes are built through the queued searches; rallies are not
  released while their lanes are still being built.
- Removed the 9-arc forward probing, 7-arc reverse probing, 145 m
  straight shortcuts, 165 m snapping and random sideways detours.

Tuning: new pathFollow section, navEngine.goalSnapRadiusM and
pathBudgetMsPerFrame, navBake.crushableMaxRiExtraHp passed to the
scripts; retired navigation.cornerBrakeAccel, stuckWindowSec,
stuckMinMoveM and unstickReverseSec. Launcher fields and en/zh-CN labels
updated.

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, offline engine
run on avg_poland (prefetch, sliced searches within budget, slot
replacement, dynamic costs), isolated game copy build. daScript
compilation and in-battle behaviour still need the in-game run.
2026-12-05 17:38:41 +08:00

165 lines
5.4 KiB
C++

/*
* Point queries on the loaded pack (wto_nav_runtime.h) for the ground AI's path follower: what
* lies under a vehicle (surface, flags, distance to the mesh edge), whether the mesh lets it go
* straight somewhere, the nearest walkable point, and dynamic cost circles (wrecks, jams, rubble)
* that later path searches route around.
*/
#include "wto_nav_internal.h"
#include <float.h>
#include <string.h>
namespace wtonav {
DynamicCost g_dynamic[WTO_NAV_RT_MAX_DYNAMIC];
uint32_t g_dynamic_count;
void dynamic_reset_locked()
{
g_dynamic_count = 0;
}
void dynamic_expire_locked(float now)
{
uint32_t kept = 0;
for (uint32_t i = 0; i < g_dynamic_count; i++) {
if (g_dynamic[i].until >= now)
g_dynamic[kept++] = g_dynamic[i];
}
g_dynamic_count = kept;
}
namespace {
bool valid_query(uint32_t tier, uint32_t filter)
{
return g_pack != nullptr && tier < WTO_NAV_TIER_COUNT && filter < WTO_NAV_RT_FILTER_COUNT;
}
/* An existing circle overlapping most of the new one, or -1. */
int find_dynamic(const float pos[3], float radius)
{
for (uint32_t i = 0; i < g_dynamic_count; i++) {
const float dx = g_dynamic[i].pos[0] - pos[0], dz = g_dynamic[i].pos[2] - pos[2];
const float reach = (g_dynamic[i].radius > radius ? g_dynamic[i].radius : radius) * 0.5f;
if (dx * dx + dz * dz <= reach * reach)
return (int)i;
}
return -1;
}
int soonest_dynamic()
{
int best = 0;
for (uint32_t i = 1; i < g_dynamic_count; i++) {
if (g_dynamic[i].until < g_dynamic[best].until)
best = (int)i;
}
return best;
}
} // namespace
} // namespace wtonav
using namespace wtonav;
extern "C" int wto_nav_probe(uint32_t tier, uint32_t filter, const float pos[3], float snap_m, float wall_radius_m, WtoNavProbe *probe)
{
memset(probe, 0, sizeof(*probe));
AcquireSRWLockExclusive(&g_lock); /* findDistanceToWall uses the query's node pool */
if (!valid_query(tier, filter) || !(snap_m > 0.0f)) {
ReleaseSRWLockExclusive(&g_lock);
return 0;
}
g_stats.meshQueries++;
dtNavMeshQuery *query = g_pack->pointQueries[tier];
const QueryFilter &qf = g_filters[filter];
dtPolyRef ref = 0;
if (snap_to_mesh(query, qf, snap_m, g_pack->heightSpan, pos, &ref, probe->nearest)) {
const dtMeshTile *tile = nullptr;
const dtPoly *poly = nullptr;
probe->onMesh = 1;
probe->offsetM = dtVdist2D(pos, probe->nearest);
if (dtStatusSucceed(g_pack->meshes[tier]->getTileAndPolyByRef(ref, &tile, &poly))) {
probe->area = poly->getArea();
probe->flags = poly->flags;
}
float hit_pos[3], hit_normal[3];
float dist = wall_radius_m;
if (wall_radius_m > 0.0f &&
dtStatusFailed(query->findDistanceToWall(ref, probe->nearest, wall_radius_m, &qf, &dist, hit_pos, hit_normal)))
dist = wall_radius_m;
probe->wallDistM = dist;
}
ReleaseSRWLockExclusive(&g_lock);
return 1;
}
extern "C" int wto_nav_raycast(uint32_t tier, uint32_t filter, float snap_m, const float start[3], const float goal[3],
float *hit_frac, float hit[3])
{
*hit_frac = 0.0f;
dtVcopy(hit, start);
AcquireSRWLockShared(&g_lock);
if (!valid_query(tier, filter) || !(snap_m > 0.0f)) {
ReleaseSRWLockShared(&g_lock);
return -1;
}
InterlockedIncrement((volatile LONG *)&g_stats.meshQueries);
const dtNavMeshQuery *query = g_pack->pointQueries[tier];
const QueryFilter &qf = g_filters[filter];
dtPolyRef ref = 0;
float from[3];
int clear = -1;
if (snap_to_mesh(query, qf, snap_m, g_pack->heightSpan, start, &ref, from)) {
float t = 0.0f, normal[3];
dtPolyRef visited[64];
int visited_count = 0;
if (dtStatusSucceed(query->raycast(ref, from, goal, &qf, &t, normal, visited, &visited_count, 64))) {
clear = t == FLT_MAX ? 1 : 0;
*hit_frac = t == FLT_MAX ? 1.0f : t;
dtVlerp(hit, from, goal, *hit_frac);
}
}
ReleaseSRWLockShared(&g_lock);
return clear;
}
extern "C" int wto_nav_nearest(uint32_t tier, uint32_t filter, float radius_m, const float pos[3], float out[3])
{
int found = 0;
AcquireSRWLockShared(&g_lock);
if (valid_query(tier, filter) && radius_m > 0.0f) {
InterlockedIncrement((volatile LONG *)&g_stats.meshQueries);
dtPolyRef ref = 0;
found = snap_to_mesh(g_pack->pointQueries[tier], g_filters[filter], radius_m, g_pack->heightSpan, pos, &ref, out);
}
ReleaseSRWLockShared(&g_lock);
return found;
}
extern "C" uint32_t wto_nav_add_dynamic(const float pos[3], float radius_m, float cost_mul, float until)
{
if (!(radius_m > 0.0f) || !(cost_mul > 0.0f))
return 0;
AcquireSRWLockExclusive(&g_lock);
int at = find_dynamic(pos, radius_m);
if (at >= 0) {
DynamicCost &d = g_dynamic[at];
dtVcopy(d.pos, pos);
d.radius = d.radius > radius_m ? d.radius : radius_m;
d.mul = d.mul > cost_mul ? d.mul : cost_mul;
d.until = d.until > until ? d.until : until;
} else {
at = g_dynamic_count < WTO_NAV_RT_MAX_DYNAMIC ? (int)g_dynamic_count++ : soonest_dynamic();
DynamicCost &d = g_dynamic[at];
dtVcopy(d.pos, pos);
d.radius = radius_m;
d.mul = cost_mul;
d.until = until;
}
const uint32_t count = g_dynamic_count;
ReleaseSRWLockExclusive(&g_lock);
return count;
}