Files
WT-Offline/game-ai/ground/wto_ai_nav_common.das
T
N0VAN0M4LY 975a28441a feat(ground-ai): implement modular zero-heap tank AI daScript layers
Add 14 modular daScript files under game-ai/ground covering per-vehicle
DataBlock specs, foliage-filtered terrain and cliff crest lookahead,
flat world snapshots, anti-clustering telemetry and active dispersion,
cap-insider-bounded macro scheduling, sticky 6-channel perception,
ridge snipe/reverse withdraw tactics, wide-projected NavMesh routing,
bidirectional forward/reverse DWA local planning, closed-loop
VehiclePhys chassis control, and settled ballistic fire control.
2026-12-03 12:12:17 +08:00

249 lines
11 KiB
Plaintext

// Bidirectional non-holonomic local planner:
// - Directed 7-arc reverse trajectory planner for ridge withdrawals, tactical peeks, cliff escapes,
// and jam unsticking (enabling sustained 30-50m reverse transfers);
// - Stationary combat hull discipline (zero hull-pivot whip on turreted tanks during ENGAGE_HALT);
// - Forward 9-arc DWA rollout with cliff rejection, TTC ally overtaking, and jam/crowd penalties.
options indenting = 2
options no_global_heap = true
module wto_ai_nav_common shared
require ecs
require math
require %appGame.wtoai.wto_ai_config_common
require %appGame.wtoai.wto_ai_math_common
require %appGame.wtoai.wto_ai_types_common
require %appGame.wtoai.wto_ai_terrain_common
require %appGame.wtoai.wto_ai_world_common
require %appGame.wtoai.wto_ai_crowd_common
require %appGame.wtoai.wto_ai_route_common
[never_inline]
def private wto_wreck_or_ally_clearance(world : WtoWorldSnap const&; selfEid : EntityId;
pt : float3; tSec, selfRad : float) : float
var pen = 0.0
for w in range(world.wreckCount)
let r = selfRad + max(world.wrecks[w].halfLen, world.wrecks[w].halfWid) + WTO_AI_TERRAIN_MARGIN_M
let dSq = wto_xz_dist_sq(pt, world.wrecks[w].pos)
if dSq < r * r
return 1.0e6
elif dSq < wto_sq(r + 3.5)
pen += 18.0
for i in range(world.unitCount)
assume u = world.units[i]
if !u.isAlive || !u.isGround || u.eid == selfEid
continue
let predPos = u.pos + u.vel * min(tSec, 1.6)
let r = selfRad + max(u.halfLen, u.halfWid) + WTO_AI_TERRAIN_MARGIN_M
let dSq = wto_xz_dist_sq(pt, predPos)
if dSq < r * r
return 1.0e6
elif dSq < wto_sq(r + 5.0)
pen += 22.0
return pen
// Evaluate 7 curved rear trajectories (`16m` behind chassis) toward `reverseGoal`.
[never_inline]
def private wto_eval_reverse_arcs(world : WtoWorldSnap const&; now : float;
myPos, hullFwd, reverseGoal : float3;
var slot : WtoBotSlot&; var outRearDir : float3&) : bool
let rearBase = -wto_xz_normalize(hullFwd, float3(1.0, 0.0, 0.0))
let baseYaw = atan2(rearBase.z, rearBase.x)
let wishDir = wto_xz_normalize(reverseGoal - myPos, rearBase)
let selfRad = max(slot.spec.halfWid, 1.65)
var bestScore = -1.0e9
var found = false
outRearDir = rearBase
for a in range(7)
let steerFrac = float(a - 3) / 3.0
let arcCurv = steerFrac * 0.042
var cur = myPos
var blocked = false
var arcPen = abs(steerFrac) * 2.5
for s in range(1, 5)
let dist = float(s) * 3.8
let yaw = baseYaw + arcCurv * dist
let stepDir = float3(cos(yaw), 0.0, sin(yaw))
let nx = cur.x + stepDir.x * 3.8
let nz = cur.z + stepDir.z * 3.8
var gy = cur.y
if !wto_terrain_drivable_y(cur, nx, nz, gy)
blocked = true
break
if wto_terrain_hard_ray(cur + float3(0.0, 1.25, 0.0), stepDir, 4.2) < 3.7
blocked = true
break
let nxt = float3(nx, gy, nz)
let dynPen = wto_wreck_or_ally_clearance(world, slot.eid, nxt, dist / max(1.8, slot.spec.vMaxRev), selfRad)
if dynPen >= 1.0e5
blocked = true
break
arcPen += dynPen * 0.25 + wto_crowd_point_penalty(world, slot.army, slot.eid, nxt) * 0.2
cur = nxt
if blocked
continue
let firstYaw = baseYaw + arcCurv * 5.5
let candDir = float3(cos(firstYaw), 0.0, sin(firstYaw))
if wto_terrain_has_cliff_ahead(myPos, candDir, 15.0)
continue
let score = dot(candDir, wishDir) * 35.0 - arcPen - wto_world_jam_penalty(world, cur, now) * 0.4
if score > bestScore
bestScore = score
outRearDir = candDir
found = true
return found
// Evaluate 9 curved forward trajectories (`22m` ahead) with hard cliff rejection and traffic cost.
[never_inline]
def private wto_eval_forward_arcs(world : WtoWorldSnap const&; now : float;
myPos, hullFwd, wishDir : float3; speedCmd : float;
var slot : WtoBotSlot&; var outFwdDir : float3&) : bool
let fwdBase = wto_xz_normalize(hullFwd, float3(1.0, 0.0, 0.0))
let baseYaw = atan2(fwdBase.z, fwdBase.x)
let selfRad = max(slot.spec.halfWid, 1.65)
var bestScore = -1.0e9
var found = false
outFwdDir = wishDir
for a in range(9)
let steerFrac = float(a - 4) / 4.0
let arcCurv = steerFrac * 0.038
let firstYaw = baseYaw + arcCurv * 6.5
let candDir = float3(cos(firstYaw), 0.0, sin(firstYaw))
if wto_terrain_has_cliff_ahead(myPos, candDir, WTO_AI_TERRAIN_CREST_LOOKAHEAD_M)
continue
var cur = myPos
var blocked = false
var arcPen = abs(steerFrac) * 2.2
for s in range(1, 6)
let dist = float(s) * 4.2
let yaw = baseYaw + arcCurv * dist
let stepDir = float3(cos(yaw), 0.0, sin(yaw))
let nx = cur.x + stepDir.x * 4.2
let nz = cur.z + stepDir.z * 4.2
var gy = cur.y
if !wto_terrain_drivable_y(cur, nx, nz, gy)
blocked = true
break
if wto_terrain_hard_ray(cur + float3(0.0, 1.3, 0.0), stepDir, 4.6) < 4.1
blocked = true
break
let nxt = float3(nx, gy, nz)
let dynPen = wto_wreck_or_ally_clearance(world, slot.eid, nxt, dist / max(2.5, speedCmd), selfRad)
if dynPen >= 1.0e5
blocked = true
break
arcPen += dynPen * 0.3 + wto_crowd_point_penalty(world, slot.army, slot.eid, nxt) * 0.35
cur = nxt
if blocked
continue
let align = dot(candDir, wishDir) * 40.0 + dot(candDir, slot.nav.steerDir) * 4.5
let score = align - arcPen - wto_world_jam_penalty(world, cur, now)
if score > bestScore
bestScore = score
outFwdDir = candDir
found = true
return found
[never_inline]
def private wto_set_drive_cmd(var cmd : WtoDriveCmd&; mode : int; targetPos, desiredDir : float3;
speedMps, frontClr, rearClr : float)
cmd.valid = true
cmd.mode = mode
cmd.targetPos = targetPos
cmd.desiredDir = desiredDir
cmd.speedMps = speedMps
cmd.frontClearM = frontClr
cmd.rearClearM = rearClr
[never_inline]
def private wto_handle_stationary_engage(myPos, hullFwd : float3; var slot : WtoBotSlot&)
// Casemate / limited-traverse TDs rotate hull ONLY while target is outside 78% of gun arc.
if slot.spec.isLimitedTraverse && !!slot.perceive.targetEid
let toTgt = wto_xz_normalize(slot.perceive.knownPos - myPos, hullFwd)
let angDeg = acos(clamp(dot(toTgt, hullFwd), -1.0, 1.0)) * WTO_RAD_TO_DEG
if angDeg > slot.spec.yawLimitDeg * 0.78
wto_set_drive_cmd(slot.driveCmd, WTO_DRIVE_PIVOT, myPos + toTgt * 18.0, toTgt, 2.0, slot.nav.frontClearM, slot.nav.rearClearM)
return
// Turreted tanks (and aligned TDs) hold chassis completely still so barrel settles cleanly!
wto_set_drive_cmd(slot.driveCmd, WTO_DRIVE_HALT, myPos, hullFwd, 0.0, slot.nav.frontClearM, slot.nav.rearClearM)
[never_inline]
def private wto_check_stuck_watchdog(var world : WtoWorldSnap&; now : float;
myPos, wishDir : float3; var slot : WtoBotSlot&)
assume nav = slot.nav
let movingIntent = slot.tactics.desiredSpeedMps > 1.2 && now >= nav.reverseUntil
if !movingIntent
nav.stuckOrigin = myPos
nav.stuckCheckAt = now + WTO_AI_NAV_STUCK_WINDOW_SEC
return
if nav.stuckCheckAt < -50.0
nav.stuckOrigin = myPos
nav.stuckCheckAt = now + WTO_AI_NAV_STUCK_WINDOW_SEC
return
if now < nav.stuckCheckAt
return
let moved = sqrt(wto_xz_dist_sq(myPos, nav.stuckOrigin))
nav.stuckOrigin = myPos
nav.stuckCheckAt = now + WTO_AI_NAV_STUCK_WINDOW_SEC
if moved >= WTO_AI_NAV_STUCK_MIN_MOVE_M
return
wto_world_record_jam(world, myPos + wishDir * 6.0, now)
wto_route_request_detour(world, now, myPos, wishDir, slot)
let backIdx = clamp(slot.route.corridorIdx - 2, 0, max(0, slot.route.corridorLen - 1))
nav.reverseAnchor = slot.route.corridorLen > 0 ? slot.route.corridor[backIdx] : (myPos - wishDir * 22.0)
nav.reverseUntil = now + max(WTO_AI_NAV_UNSTICK_REV_SEC, 3.6)
nav.reverseCount++
[never_inline]
def private wto_try_reverse_drive(world : WtoWorldSnap const&; now : float;
myPos, hullFwd : float3; var slot : WtoBotSlot&) : bool
let posMode = slot.tactics.posture
let wantTacRev = posMode == WTO_POSTURE_REVERSE_WITHDRAW || posMode == WTO_POSTURE_PEEK_BACK
if !wantTacRev && now >= slot.nav.reverseUntil
return false
let revGoal = wantTacRev ? slot.tactics.moveGoal : slot.nav.reverseAnchor
var rearDir = -hullFwd
if slot.nav.rearClearM > 2.4 && wto_eval_reverse_arcs(world, now, myPos, hullFwd, revGoal, slot, rearDir)
slot.nav.steerDir = rearDir
wto_set_drive_cmd(slot.driveCmd, WTO_DRIVE_REVERSE, myPos + rearDir * 18.0, rearDir, max(2.0, slot.spec.vMaxRev), slot.nav.frontClearM, slot.nav.rearClearM)
return true
slot.nav.reverseUntil = -100.0
return false
[never_inline]
def public wto_nav_update(var world : WtoWorldSnap&; now : float;
myPos, rawHullFwd : float3; var slot : WtoBotSlot&)
if !WTO_AI_LAYER_NAVIGATION
slot.driveCmd.valid = false
return
let hullFwd = wto_xz_normalize(rawHullFwd, float3(1.0, 0.0, 0.0))
let probeBase = myPos + float3(0.0, 1.25, 0.0)
slot.nav.frontClearM = wto_terrain_hard_ray(probeBase, hullFwd, 24.0)
slot.nav.rearClearM = wto_terrain_hard_ray(probeBase, -hullFwd, 18.0)
slot.nav.cliffAhead = wto_terrain_has_cliff_ahead(myPos, hullFwd, WTO_AI_TERRAIN_CREST_LOOKAHEAD_M)
if slot.tactics.posture == WTO_POSTURE_ENGAGE_HALT || slot.tactics.desiredSpeedMps <= 0.05
slot.nav.stuckCheckAt = now + WTO_AI_NAV_STUCK_WINDOW_SEC
wto_handle_stationary_engage(myPos, hullFwd, slot)
return
if wto_try_reverse_drive(world, now, myPos, hullFwd, slot)
return
let wp = wto_route_select_waypoint(world, now, myPos, slot.tactics.moveGoal, slot)
let wishDir = wto_xz_normalize(wp - myPos, hullFwd)
wto_check_stuck_watchdog(world, now, myPos, wishDir, slot)
var bestDir = wishDir
if !wto_eval_forward_arcs(world, now, myPos, hullFwd, wishDir, slot.tactics.desiredSpeedMps, slot, bestDir)
if slot.nav.rearClearM > 3.2
slot.nav.reverseAnchor = myPos - hullFwd * 26.0
slot.nav.reverseUntil = now + 4.8
slot.nav.reverseCount++
wto_set_drive_cmd(slot.driveCmd, WTO_DRIVE_REVERSE, slot.nav.reverseAnchor, -hullFwd, max(2.0, slot.spec.vMaxRev), slot.nav.frontClearM, slot.nav.rearClearM)
return
bestDir = wishDir
slot.nav.steerDir = bestDir
let turnDeg = acos(clamp(dot(bestDir, hullFwd), -1.0, 1.0)) * WTO_RAD_TO_DEG
let needPivot = turnDeg >= WTO_AI_NAV_PIVOT_ANGLE_DEG || (slot.nav.frontClearM < slot.spec.halfLen + 1.8 && turnDeg > 32.0)
wto_set_drive_cmd(slot.driveCmd, needPivot ? WTO_DRIVE_PIVOT : WTO_DRIVE_CRUISE, myPos + bestDir * 22.0, bestDir, slot.tactics.desiredSpeedMps, slot.nav.frontClearM, slot.nav.rearClearM)