Files
WT-Offline/game-ai/ground/wto_ai_sight_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

127 lines
5.0 KiB
Plaintext

// Scene visibility, foliage transparency, smoke, and strict hard-occlusion queries.
// Completely separate from driving's soft-obstacle avoidance in wto_ai_terrain_common.
options indenting = 2
options no_global_heap = true
module wto_ai_sight_common shared
require ecs
require math
require DagorMath
require Dacoll
require RendInst
require PhysMat
require SmokeOccluder
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
let private SIGHT_TERRAIN_MASK = ETF_LMESH | ETF_HEIGHTMAP | ETF_STRUCTURES | ETF_FRT
[never_inline]
def private wto_seg_hits_box_xz(p0, segDir : float3; segLen : float;
boxPos : float3; halfLen, halfWid, heightM : float) : bool
let rad = max(halfLen, halfWid) * 0.88
let center = boxPos + float3(0.0, heightM * 0.5, 0.0)
let rel = center - p0
let along = dot(rel, segDir)
if along < 0.25 || along > segLen - 0.25
return false
let closest = p0 + segDir * along
let dy = abs(closest.y - center.y)
if dy > heightM * 0.55 + 0.35
return false
let dx = closest.x - center.x
let dz = closest.z - center.z
return (dx * dx + dz * dz) <= rad * rad
[never_inline]
def public wto_sight_units_block_segment(world : WtoWorldSnap const&;
selfEid, targetEid : EntityId;
p0, segDir : float3; segLen : float) : int
for w in range(world.wreckCount)
assume wr = world.wrecks[w]
if wr.eid != selfEid && wr.eid != targetEid
if wto_seg_hits_box_xz(p0, segDir, segLen, wr.pos, wr.halfLen, wr.halfWid, 2.2)
return WTO_BLOCK_WRECK
for i in range(world.unitCount)
assume u = world.units[i]
if !u.isAlive || !u.isGround || u.eid == selfEid || u.eid == targetEid
continue
if wto_seg_hits_box_xz(p0, segDir, segLen, u.pos, u.halfLen, u.halfWid, 2.3)
return WTO_BLOCK_VEHICLE
return WTO_BLOCK_NONE
[never_inline]
def public wto_sight_hard_segment_blocked(world : WtoWorldSnap const&;
selfEid, targetEid : EntityId;
fromPos, toPos : float3;
sweepRadius : float; trimStartM, trimEndM : float) : int
let delta = toPos - fromPos
let fullLen = length(delta)
if fullLen <= trimStartM + trimEndM + 0.2
return WTO_BLOCK_NONE
let dir = delta / fullLen
var p0 = fromPos + dir * trimStartM
let traceLen = fullLen - trimStartM - trimEndM
if rayhit_normalized(p0, dir, traceLen, SIGHT_TERRAIN_MASK, PHYSMAT_INVALID)
return WTO_BLOCK_TERRAIN
if rayhit_normalized(p0, dir, traceLen, ETF_RI, PHYSMAT_INVALID)
return WTO_BLOCK_OBJECT
if rayhit_normalized(p0, dir, traceLen, ETF_RI_PHYS, PHYSMAT_INVALID)
return WTO_BLOCK_OBJECT
if sweepRadius > 0.02
var s1 = p0 + dir * traceLen
var queryOut = ShapeQueryOutput()
if sphere_cast(p0, s1, sweepRadius, queryOut)
return WTO_BLOCK_OBJECT
return wto_sight_units_block_segment(world, selfEid, targetEid, p0, dir, traceLen)
[never_inline]
def public wto_sight_visual_clear(world : WtoWorldSnap const&;
selfEid, targetEid : EntityId;
eyePos, targetPt : float3) : int
let delta = targetPt - eyePos
let dist = length(delta)
if dist <= 1.2
return WTO_BLOCK_NONE
let dir = delta / dist
let traceLen = max(0.4, dist - 1.1)
var p0 = eyePos
var p1 = targetPt
if rayhit_smoke_occluders(p0, p1)
return WTO_BLOCK_SMOKE
let aimMat = get_material_id("aimRay")
if rayhit_normalized(p0, dir, traceLen, ETF_ALL, aimMat)
return WTO_BLOCK_TERRAIN
if traceTransparencyRayRIGenNormalized(p0, dir, traceLen, WTO_AI_PERC_FOLIAGE_TRANS)
return WTO_BLOCK_FOLIAGE
return wto_sight_units_block_segment(world, selfEid, targetEid, p0, dir, traceLen)
[never_inline]
def public wto_sight_check_target_samples(world : WtoWorldSnap const&;
selfEid : EntityId; eyePos : float3;
tgt : WtoUnitSnap const&; maxSamples : int;
var outBestPt : float3&) : int
if tgt.isAir
outBestPt = tgt.pos
return wto_sight_visual_clear(world, selfEid, tgt.eid, eyePos, tgt.pos) == WTO_BLOCK_NONE ? 2 : 0
let fwd = wto_xz_normalize(tgt.hullFwd, float3(1.0, 0.0, 0.0))
let left = float3(-fwd.z, 0.0, fwd.x)
let sideOff = max(0.75, tgt.halfWid * 0.72)
var pts : float3[4]
pts[0] = tgt.pos + float3(0.0, 1.15, 0.0)
pts[1] = tgt.pos + float3(0.0, 1.95, 0.0)
pts[2] = tgt.pos + left * sideOff + float3(0.0, 1.25, 0.0)
pts[3] = tgt.pos - left * sideOff + float3(0.0, 1.25, 0.0)
let count = clamp(maxSamples, 1, 4)
var vis = 0
var picked = false
for s in range(count)
if wto_sight_visual_clear(world, selfEid, tgt.eid, eyePos, pts[s]) == WTO_BLOCK_NONE
if !picked
outBestPt = pts[s]
picked = true
vis++
return vis