/* * 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 #include 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; }