Files
WT-Offline/game-ai/ground/wto_ai_fire_gate_common.das
T
N0VAN0M4LY 9229045042 feat(ground-ai): add exposure perception and single ballistic fire gate
Replace the old foliage-ignoring LOS check and 45 m pre-fire ray with
separate vision/hard-occlusion queries, exposure-integrated spotting, and
a single fire-permission gate for every ground weapon:

- Add perception and fire-gate distance/ballistics/budget parameters to
  tuning/ground-ai.jsonc, validated in ground-ai-tuning.ts and compiled
  into wto_ai_config_common.das.
- Extract wto_ai_combat_types_common.das and add wto_ai_sight_common.das:
  visual checks use smoke occluders, ETF_ALL with aimRay material,
  traceTransparencyRayRIGenNormalized foliage transparency, and vehicle/
  wreck checks; hard-occlusion checks test terrain/structures plus both
  ETF_RI and ETF_RI_PHYS without skipping soft foliage, plus swept
  sphere_cast and wreck/vehicle bounds along the full shot distance.
- Rewrite wto_ai_perceive_common.das with per-target exposure integration
  (fast/normal/far fill times and occlusion decay), 4-point visibility
  sampling with continuous >=2-point LOS tracking for the 800 m 4-second
  window, frozen last-seen coordinates with zeroed velocity on LOS loss,
  and impact-time hit direction (no live offender lookup).
- Record per-shell ballistics profiles (speed, Cx, mass, ballisticCaliber)
  in wto_ai_specs_common.das and sync the currently loaded shell from
  gun.chosenBulletType / bulletSets at runtime.
- Add wto_ai_fire_gate_common.das as the sole ground trigger exit: checks
  visual lock, weapon readiness, distance-tier measured chassis speed
  limits (100/300/800 m), 800 m 4-second window, aim settle, per-frame
  check budget, straight muzzle hard-ray, drag-integrated ballistic arc
  sweep, predicted friendly corridor, and every barrel in the primary
  trigger group. Confirm real shot departure from loaded ammo/readiness,
  remove the pure-launcher stock bypass and add_unit_for_target leak, and
  log exact deny/shot reasons.

Verified root and launcher type checks, isolated 2.61.0.58 build (21
ground AI modules packed), and clean in-engine daScript compilation/load
(wto_ai_runtime.das loaded in 186 ms, 0 compile errors). Live battle
acceptance deferred until the full pipeline is wired.
2026-12-03 17:34:59 +08:00

200 lines
9.2 KiB
Plaintext

// Single fire-permission gate for every ground weapon (main cannon, launcher, SPAA self-defense).
// Every shot must pass visual confirmation, weapon readiness, distance-tier speed limits,
// 800 m continuous LOS window, aim settle, frame pre-fire budget, muzzle hard-ray, drag-integrated
// ballistic trajectory sweep, predicted friendly corridor, and multi-barrel group validation.
options indenting = 2
options no_global_heap = true
module wto_ai_fire_gate_common shared
require ecs
require math
require DagorMath
require Mission
require Unit
require Weapon
require %game.utils.utils_common
require %appGame.wtoai.wto_ai_config_common
require %appGame.wtoai.wto_ai_math_common
require %appGame.wtoai.wto_ai_combat_types_common
require %appGame.wtoai.wto_ai_types_common
require %appGame.wtoai.wto_ai_sight_common
[never_inline]
def private wto_gate_friendly_segment_clear(world : WtoWorldSnap const&; selfEid : EntityId;
army : int; p0, p1 : float3; tMid : float) : bool
let delta = p1 - p0
let segLen = length(delta)
if segLen < 0.2
return true
let dir = delta / segLen
for i in range(world.unitCount)
assume u = world.units[i]
if !u.isAlive || !u.isGround || u.army != army || u.eid == selfEid
continue
let predPos = u.pos + u.vel * tMid + float3(0.0, 1.3, 0.0)
let along = clamp(dot(predPos - p0, dir), 0.0, segLen)
let clearR = WTO_AI_FC_FRIENDLY_CORRIDOR_M + u.halfWid
if length_sq(predPos - (p0 + dir * along)) < clearR * clearR
return false
return true
[never_inline]
def private wto_gate_check_ballistic_arc(world : WtoWorldSnap const&;
selfEid, targetEid : EntityId; army : int;
muzzlePos, liveDir, carrierVel, aimPt : float3;
speedMps, dragCx, massKg, caliberM : float;
var outBlock : int&) : int
let toAim = aimPt - muzzlePos
let targetDist = length(toAim)
if targetDist < 1.5
return WTO_DENY_OK
let toAimDir = toAim / targetDist
let area = 0.25 * WTO_PI * caliberM * caliberM
let dragK = 0.5 * 1.225 * dragCx * area / max(0.1, massKg)
var cur = muzzlePos
var vel = liveDir * max(80.0, speedMps) + carrierVel
var t = 0.0
var traveled = 0.0
for step in range(64)
let spd = length(vel)
if spd < 20.0
return WTO_DENY_AIM_ERR
let remain = max(0.5, targetDist - traveled)
let segWant = min(WTO_AI_FC_BALLISTIC_STEP_M, remain)
let dt = clamp(segWant / spd, 0.004, 0.14)
let accel = float3(0.0, -9.81, 0.0) - vel * (dragK * spd)
var next = cur + vel * dt + accel * (0.5 * dt * dt)
let nextProj = dot(next - muzzlePos, toAimDir)
let isLast = nextProj >= targetDist - 0.5
if isLast && nextProj > targetDist && length_sq(next - cur) > 0.01
let curProj = dot(cur - muzzlePos, toAimDir)
let frac = clamp((targetDist - curProj) / max(0.01, nextProj - curProj), 0.0, 1.0)
next = lerp(cur, next, frac)
let trimS = step == 0 ? 0.35 : 0.0
let trimE = isLast ? 0.65 : 0.0
let blk = wto_sight_hard_segment_blocked(world, selfEid, targetEid, cur, next, WTO_AI_FC_BALLISTIC_SWEEP_R_M, trimS, trimE)
if blk != WTO_BLOCK_NONE
outBlock = blk
return WTO_DENY_ARC_HARD
if !wto_gate_friendly_segment_clear(world, selfEid, army, cur, next, t + dt * 0.5)
return WTO_DENY_FRIENDLY
traveled += length(next - cur)
cur = next
vel = vel + accel * dt
t += dt
if isLast
let missTol = max(3.2, targetDist * 0.012)
if length_sq(cur - aimPt) > missTol * missTol
return WTO_DENY_AIM_ERR
return WTO_DENY_OK
return WTO_DENY_AIM_ERR
[never_inline]
def private wto_gate_check_barrel(world : WtoWorldSnap const&; unit : Unit const?;
slot : WtoBotSlot const&; muzzlePos, liveDir, aimPt : float3;
var outBlock : int&) : int
let straightBlk = wto_sight_hard_segment_blocked(world, slot.eid, slot.perceive.targetEid, muzzlePos, aimPt, 0.05, 0.35, 0.65)
if straightBlk != WTO_BLOCK_NONE
outBlock = straightBlk
return WTO_DENY_MUZZLE_HARD
return wto_gate_check_ballistic_arc(world, slot.eid, slot.perceive.targetEid, slot.army,
muzzlePos, liveDir, unit.vel, aimPt,
slot.fire.activeSpeedMps, slot.fire.activeDragCx, slot.fire.activeMassKg, slot.fire.activeCaliberM, outBlock)
[never_inline]
def private wto_gate_check_all_primary_guns(world : WtoWorldSnap const&; unit : Unit const?;
slot : WtoBotSlot const&; primaryMuzzle, primaryDir, aimPt : float3;
var outBlock : int&) : int
let primaryRes = wto_gate_check_barrel(world, unit, slot, primaryMuzzle, primaryDir, aimPt, outBlock)
if primaryRes != WTO_DENY_OK
return primaryRes
if unit == null || unit.weap == null
return WTO_DENY_OK
for tIdx, turret in iter_range(unit.weap.turret), unit.weap.turret
if tIdx == slot.fire.turretNo || int(turret.triggerGroupNo) != int(WeaponTriggerGroups.TRIGGER_GROUP_PRIMARY)
continue
let weapon = get_weapon(unit.weap.Weapons, int(turret.triggerNo), int(turret.weaponIndex))
if weapon == null || weapon.isDummyGun
continue
var gunTm : float3x4
weapon_calcShootTm(weapon, unit.unitTm, gunTm)
if length_sq(gunTm[0]) <= 0.25
continue
let secRes = wto_gate_check_barrel(world, unit, slot, gunTm[3], normalize(gunTm[0]), aimPt, outBlock)
if secRes != WTO_DENY_OK
return secRes == WTO_DENY_FRIENDLY ? WTO_DENY_FRIENDLY : WTO_DENY_MULTI_GUN
return WTO_DENY_OK
[never_inline]
def private wto_gate_speed_allowed(distXZ, measuredSpeed : float; spec : WtoVehicleSpec const&) : bool
if distXZ > WTO_AI_FC_MID_RANGE_M
return measuredSpeed <= WTO_AI_FC_STOP_SPEED_MAX_MPS
if distXZ > WTO_AI_FC_CLOSE_RANGE_M
let slowCap = min(spec.vMaxFwd * WTO_AI_FC_MID_SLOW_RATIO, WTO_AI_FC_MID_SLOW_MAX_MPS)
return measuredSpeed <= slowCap
return true
[never_inline]
def private wto_gate_log_deny(now, distXZ, measuredSpeed : float; reason, blk : int; var slot : WtoBotSlot&)
assume fc = slot.fire
let changed = fc.lastDenyReason != reason || fc.lastDenyBlock != blk
fc.lastDenyReason = reason
fc.lastDenyBlock = blk
if WTO_AI_DIAG_TELEMETRY && reason >= WTO_DENY_SPEED && (changed || (now - fc.lastDenyLogAt) >= 4.0)
fc.lastDenyLogAt = now
print("WTO_AI|DENY|eid={uint(slot.eid)}|reason={reason}|block={blk}|dist={distXZ}|spd={measuredSpeed}\n")
[never_inline]
def public wto_fire_gate_try_shoot(world : WtoWorldSnap const&; var unit : Unit?;
now, dt, distXZ : float; muzzlePos, aimPt, liveDir, wishDir : float3;
var slot : WtoBotSlot&; var fireBudget : int&)
assume fc = slot.fire
let aimErrDeg = acos(clamp(dot(liveDir, wishDir), -1.0, 1.0)) * WTO_RAD_TO_DEG
fc.lastAimErrDeg = aimErrDeg
let measuredSpeed = length(unit.vel)
let speedOk = wto_gate_speed_allowed(distXZ, measuredSpeed, slot.spec)
let allowErrDeg = clamp(atan2(WTO_AI_FC_CLOSE_HIT_RADIUS_M, max(distXZ, 6.0)) * WTO_RAD_TO_DEG, WTO_AI_FC_BASE_ALIGN_DEG, 2.4)
let settled = aimErrDeg <= allowErrDeg && fc.barrelOmegaDegSec <= WTO_AI_FC_BARREL_SETTLE_DEG_SEC && speedOk
fc.lastAligned = settled
fc.settleHoldSec = settled ? (fc.settleHoldSec + clamp(dt, 0.005, 0.10)) : max(0.0, fc.settleHoldSec - dt * 2.0)
let minCadence = slot.spec.isAutocannonBelt ? 0.12 : 0.65
if !fc.gunReady || (now - fc.lastShotAt) < minCadence
wto_gate_log_deny(now, distXZ, measuredSpeed, WTO_DENY_NOT_READY, WTO_BLOCK_NONE, slot)
return
if !speedOk
wto_gate_log_deny(now, distXZ, measuredSpeed, WTO_DENY_SPEED, WTO_BLOCK_NONE, slot)
return
if distXZ > WTO_AI_FC_LONG_RANGE_M && (slot.perceive.goodLosSec < WTO_AI_FC_LONG_WINDOW_SEC || slot.perceive.visiblePointCount < 2)
wto_gate_log_deny(now, distXZ, measuredSpeed, WTO_DENY_LONG_WINDOW, WTO_BLOCK_NONE, slot)
return
if aimErrDeg > allowErrDeg
wto_gate_log_deny(now, distXZ, measuredSpeed, WTO_DENY_AIM_ERR, WTO_BLOCK_NONE, slot)
return
let reqHold = distXZ <= WTO_AI_FC_CLOSE_RANGE_M ? WTO_AI_FC_CLOSE_SETTLE_HOLD_SEC : WTO_AI_FC_SETTLE_HOLD_SEC
if fc.settleHoldSec < reqHold
wto_gate_log_deny(now, distXZ, measuredSpeed, WTO_DENY_SETTLE, WTO_BLOCK_NONE, slot)
return
if fireBudget <= 0
wto_gate_log_deny(now, distXZ, measuredSpeed, WTO_DENY_BUDGET, WTO_BLOCK_NONE, slot)
return
fireBudget--
var blk = WTO_BLOCK_NONE
let gateRes = wto_gate_check_all_primary_guns(world, unit, slot, muzzlePos, liveDir, aimPt, blk)
if gateRes != WTO_DENY_OK
wto_gate_log_deny(now, distXZ, measuredSpeed, gateRes, blk, slot)
return
unit_single_shot(unit, int(WeaponTriggerGroups.TRIGGER_GROUP_PRIMARY))
fc.lastShotAt = now
fc.pendingShotConfirm = true
fc.pendingShotAt = now
fc.lastDenyReason = WTO_DENY_OK
fc.lastDenyBlock = WTO_BLOCK_NONE
fc.shotErrSumDeg += aimErrDeg
fc.maxShotErrDeg = max(fc.maxShotErrDeg, aimErrDeg)
if !slot.spec.isAutocannonBelt
fc.settleHoldSec = 0.0
if WTO_AI_DIAG_TELEMETRY
print("WTO_AI|SHOT|eid={uint(slot.eid)}|tgt={uint(slot.perceive.targetEid)}|dist={distXZ}|spd={measuredSpeed}|v0={fc.activeSpeedMps}|err={aimErrDeg}\n")