Replace direct reads of true enemy velocity with an own-sight sample
estimator, add 2-5 s last-seen exit watching, and sample bounded aiming
bias once per shooting opportunity:
- Add the estimation section to tuning/ground-ai.jsonc (watchMinSec,
watchMaxSec, velSampleMinSpanSec, velStableSpanSec, velJumpResetMps,
baseAimSigmaM, leadErrorFrac, maxBiasM), validated in
ground-ai-tuning.ts and compiled into WTO_AI_EST_* constants.
- Add wto_ai_estimate_common.das:
- Maintain a 6-sample timestamped ring buffer from each bot's own
visual sightings; compute smoothed observed velocity and convergence
quality, dropping quality and re-seeding bias on sudden braking/turns
or target switches.
- On LOS loss from an active ground engagement, freeze watchPos at the
last-seen exit point, clear observed velocity, and draw one random
2-5 s watch duration; stationary overwatch/defend bots hold their
post while watching, whereas pushing/retreating bots keep moving
while keeping their turret on the exit.
- Compute lead from observed velocity and loaded-shell flight time,
sampling one bounded bias vector per shooting opportunity (target,
shot count, state, 12 m position cell) that narrows as observation
quality improves and still passes the single fire-permission gate.
Verified root and launcher type checks, isolated 2.61.0.58 build (25
ground AI modules packed), and clean in-engine daScript compilation/load
(wto_ai_runtime.das loaded in 184 ms, 0 compile errors). Live battle
acceptance deferred until the full pipeline is wired.
234 lines
8.8 KiB
Plaintext
234 lines
8.8 KiB
Plaintext
// Exposure-integrated visual perception with strict foliage/smoke/hard occlusion,
|
|
// directional-only audio/hit/radio clues, and frozen last-seen memory.
|
|
options indenting = 2
|
|
options no_global_heap = true
|
|
module wto_ai_perceive_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_combat_types_common
|
|
require %appGame.wtoai.wto_ai_types_common
|
|
require %appGame.wtoai.wto_ai_terrain_common
|
|
require %appGame.wtoai.wto_ai_sight_common
|
|
require %appGame.wtoai.wto_ai_estimate_common
|
|
require %appGame.wtoai.wto_ai_world_common
|
|
|
|
[never_inline]
|
|
def private wto_exp_find_or_alloc(var p : WtoPerceiveState&; eid : EntityId) : int
|
|
var freeIdx = -1
|
|
var minIdx = 0
|
|
var minScore = 2.0
|
|
for i in range(WTO_MAX_EXPOSURE_TRACKS)
|
|
if p.expEids[i] == eid
|
|
return i
|
|
if !p.expEids[i] && freeIdx < 0
|
|
freeIdx = i
|
|
if p.expScore[i] < minScore
|
|
minScore = p.expScore[i]
|
|
minIdx = i
|
|
let dst = freeIdx >= 0 ? freeIdx : minIdx
|
|
p.expEids[dst] = eid
|
|
p.expScore[dst] = 0.0
|
|
p.expGoodLosSec[dst] = 0.0
|
|
return dst
|
|
|
|
[never_inline]
|
|
def private wto_exp_decay_all(world : WtoWorldSnap const&; dt : float; var p : WtoPerceiveState&)
|
|
let decay = WTO_AI_PERC_DECAY_PER_SEC * dt
|
|
for i in range(WTO_MAX_EXPOSURE_TRACKS)
|
|
if !p.expEids[i]
|
|
continue
|
|
let uIdx = wto_world_find_unit(world, p.expEids[i])
|
|
if uIdx < 0 || !world.units[uIdx].isAlive
|
|
p.expEids[i] = INVALID_ENTITY_ID
|
|
p.expScore[i] = 0.0
|
|
p.expGoodLosSec[i] = 0.0
|
|
continue
|
|
p.expScore[i] = max(0.0, p.expScore[i] - decay)
|
|
if p.expScore[i] <= 0.001
|
|
p.expEids[i] = INVALID_ENTITY_ID
|
|
p.expScore[i] = 0.0
|
|
p.expGoodLosSec[i] = 0.0
|
|
|
|
[never_inline]
|
|
def private wto_spot_fill_time(dist : float; cosAngle : float; isShooting : bool; visCount : int) : float
|
|
let cosFront = cos(WTO_AI_PERC_FRONT_CONE_DEG * WTO_DEG_TO_RAD)
|
|
var t = WTO_AI_PERC_FAR_SPOT_SEC
|
|
if dist < WTO_AI_PERC_NEAR_SPOT_M || (isShooting && dist < WTO_AI_PERC_FIRING_SPOT_M)
|
|
t = WTO_AI_PERC_FAST_SPOT_SEC
|
|
elif dist <= 550.0 && cosAngle >= cosFront
|
|
t = WTO_AI_PERC_NORMAL_SPOT_SEC
|
|
if visCount >= 3
|
|
t *= 0.85
|
|
return max(0.08, t)
|
|
|
|
[never_inline]
|
|
def private wto_update_audio_and_radio_alerts(world : WtoWorldSnap const&; now : float;
|
|
myPos : float3; var slot : WtoBotSlot&)
|
|
assume p = slot.perceive
|
|
// Priority A: Recent hit direction captured at impact time (never reveals hidden true position).
|
|
if (now - p.lastDamagedAt) <= 4.0 && !!p.lastOffenderEid
|
|
p.alertYawRad = p.lastHitYawRad
|
|
p.alertUntil = now + 4.5
|
|
return
|
|
// Priority B: Close enemy engine / cannon sound direction clue.
|
|
var bestAudioDist = 1.0e9
|
|
for i in range(world.unitCount)
|
|
assume u = world.units[i]
|
|
if !u.isAlive || !u.isGround || u.army == slot.army
|
|
continue
|
|
let d = sqrt(wto_xz_dist_sq(myPos, u.pos))
|
|
let engRange = WTO_AI_TACTICS_ENGINE_AUDIO_M * (0.35 + 0.65 * u.engineRpm)
|
|
let heard = d <= engRange || (u.isShooting && d <= WTO_AI_TACTICS_SHOT_AUDIO_M)
|
|
if heard && d < bestAudioDist
|
|
bestAudioDist = d
|
|
let dir = u.pos - myPos
|
|
p.alertYawRad = atan2(dir.z, dir.x)
|
|
p.alertUntil = now + 3.2
|
|
if bestAudioDist < 1.0e8
|
|
return
|
|
// Priority C: Friendly radio spot direction clue (alert only, never enables firing).
|
|
let radioSq = wto_sq(WTO_AI_TACTICS_RADIO_SHARE_M)
|
|
for r in range(WTO_MAX_RADIO)
|
|
assume sp = world.radioSpots[r]
|
|
if sp.untilAt > now && sp.army == slot.army && wto_xz_dist_sq(myPos, sp.pos) <= radioSq
|
|
let dir = sp.pos - myPos
|
|
p.alertYawRad = atan2(dir.z, dir.x)
|
|
p.alertUntil = now + 2.8
|
|
return
|
|
|
|
[never_inline]
|
|
def private wto_scan_visual_exposure(world : WtoWorldSnap const&; now, dt : float;
|
|
myEye, lookDir : float3; var slot : WtoBotSlot&;
|
|
var outBestIdx, outVisCount : int&;
|
|
var outGoodLosSec : float&; var outAimPt : float3&)
|
|
assume p = slot.perceive
|
|
let cosHalfFov = cos(0.5 * WTO_AI_TACTICS_VISION_FOV_DEG * WTO_DEG_TO_RAD)
|
|
let isSpaa = slot.spec.role == WTO_ROLE_SPAA
|
|
let decay = WTO_AI_PERC_DECAY_PER_SEC * dt
|
|
var bestScore = 1.0e9
|
|
outBestIdx = -1
|
|
outVisCount = 0
|
|
outGoodLosSec = 0.0
|
|
for i in range(world.unitCount)
|
|
assume u = world.units[i]
|
|
if !u.isAlive || u.army == slot.army || (u.isAir && !isSpaa)
|
|
continue
|
|
let delta = u.pos - myEye
|
|
let dist = length(delta)
|
|
let maxRange = u.isAir ? 2600.0 : WTO_AI_TACTICS_VISION_RANGE_M
|
|
if dist > maxRange || dist < 1.0
|
|
continue
|
|
let dirXZ = wto_xz_normalize(delta, lookDir)
|
|
let cosAng = dot(dirXZ, lookDir)
|
|
let inFov = cosAng >= cosHalfFov || (dist <= WTO_AI_PERC_PERIPH_CLOSE_M && cosAng >= 0.0)
|
|
if !inFov && !u.isAir
|
|
continue
|
|
let maxSamples = dist > WTO_AI_FC_LONG_RANGE_M ? 2 : 4
|
|
var samplePt = u.pos + float3(0.0, 1.3, 0.0)
|
|
let vis = wto_sight_check_target_samples(world, slot.eid, myEye, u, maxSamples, samplePt)
|
|
if vis <= 0
|
|
for k in range(WTO_MAX_EXPOSURE_TRACKS)
|
|
if p.expEids[k] == u.eid
|
|
p.expGoodLosSec[k] = 0.0
|
|
continue
|
|
let tIdx = wto_exp_find_or_alloc(p, u.eid)
|
|
let fillSec = wto_spot_fill_time(dist, cosAng, u.isShooting, vis)
|
|
p.expScore[tIdx] = clamp(p.expScore[tIdx] + decay + dt / fillSec, 0.0, 1.0)
|
|
p.expGoodLosSec[tIdx] = vis >= 2 ? (p.expGoodLosSec[tIdx] + dt) : 0.0
|
|
if p.expScore[tIdx] < 0.999
|
|
continue
|
|
var score = dist - (u.isAir && isSpaa ? 900.0 : 0.0)
|
|
if u.eid == p.lockedEid && now < p.lockedUntil
|
|
score -= 320.0
|
|
elif u.eid == p.lastOffenderEid && (now - p.lastDamagedAt) <= 5.0
|
|
score -= 90.0
|
|
elif u.isShooting && dist < 220.0
|
|
score -= 35.0
|
|
if dist < 45.0
|
|
score -= 180.0
|
|
if score < bestScore
|
|
bestScore = score
|
|
outBestIdx = i
|
|
outVisCount = vis
|
|
outGoodLosSec = p.expGoodLosSec[tIdx]
|
|
outAimPt = samplePt
|
|
|
|
[never_inline]
|
|
def public wto_perceive_update(var world : WtoWorldSnap&; now, dt : float;
|
|
myPos, hullFwd : float3; var slot : WtoBotSlot&)
|
|
assume p = slot.perceive
|
|
let myEye = myPos + float3(0.0, 2.15, 0.0)
|
|
var lookDir = wto_xz_normalize(hullFwd, float3(1.0, 0.0, 0.0))
|
|
if slot.fire.servoInit
|
|
let worldYaw = atan2(hullFwd.z, hullFwd.x) - slot.fire.curYawDeg * WTO_DEG_TO_RAD
|
|
lookDir = float3(cos(worldYaw), 0.0, sin(worldYaw))
|
|
|
|
wto_exp_decay_all(world, dt, p)
|
|
var bestIdx = -1
|
|
var visCount = 0
|
|
var goodLosSec = 0.0
|
|
var aimPt = myPos
|
|
wto_scan_visual_exposure(world, now, dt, myEye, lookDir, slot, bestIdx, visCount, goodLosSec, aimPt)
|
|
if bestIdx >= 0
|
|
assume tgt = world.units[bestIdx]
|
|
p.targetEid = tgt.eid
|
|
p.targetIsAir = tgt.isAir
|
|
p.hasClearLos = true
|
|
p.visiblePointCount = visCount
|
|
p.goodLosSec = goodLosSec
|
|
p.visibleAimPt = aimPt
|
|
p.knownPos = tgt.pos
|
|
p.lastSeenAt = now
|
|
p.wasEngaging = !tgt.isAir
|
|
p.watchUntil = -100.0
|
|
wto_est_push_visual_sample(now, tgt.eid, tgt.pos, p)
|
|
if p.lockedEid != tgt.eid || now >= p.lockedUntil - 0.5
|
|
p.lockedEid = tgt.eid
|
|
p.lockedUntil = now + WTO_AI_TACTICS_STICKY_SEC
|
|
if !tgt.isAir && (now - p.lastRadioSentAt) >= 2.5
|
|
p.lastRadioSentAt = now
|
|
wto_world_broadcast_radio(world, slot.army, tgt.eid, tgt.pos, now)
|
|
return
|
|
|
|
// Lost visual confirmation: immediately revoke LOS, freeze exit point for 2-5 s watch, zero velocity.
|
|
p.hasClearLos = false
|
|
p.visiblePointCount = 0
|
|
p.goodLosSec = 0.0
|
|
wto_est_on_los_lost(now, slot.eid, p)
|
|
let tgtIdx = wto_world_find_unit(world, p.targetEid)
|
|
if !!p.targetEid && (tgtIdx < 0 || !world.units[tgtIdx].isAlive || (now - p.lastSeenAt) > WTO_AI_TACTICS_MEMORY_TTL_SEC)
|
|
p.targetEid = INVALID_ENTITY_ID
|
|
p.lockedEid = INVALID_ENTITY_ID
|
|
p.watchUntil = -100.0
|
|
wto_update_audio_and_radio_alerts(world, now, myPos, slot)
|
|
|
|
[never_inline]
|
|
def public wto_perceive_pick_hunt_pos(world : WtoWorldSnap const&; army : int; eid : EntityId;
|
|
myPos : float3; now : float; var outPos : float3&) : bool
|
|
var bestDistSq = wto_sq(min(WTO_AI_TACTICS_RADIO_SHARE_M, 560.0))
|
|
var found = false
|
|
var spotPos = myPos
|
|
for r in range(WTO_MAX_RADIO)
|
|
assume sp = world.radioSpots[r]
|
|
if sp.untilAt <= now || sp.army != army
|
|
continue
|
|
let dSq = wto_xz_dist_sq(myPos, sp.pos)
|
|
if dSq < bestDistSq && dSq > 2500.0
|
|
bestDistSq = dSq
|
|
spotPos = sp.pos
|
|
found = true
|
|
if !found
|
|
return false
|
|
let toSpot = wto_xz_normalize(spotPos - myPos, float3(1.0, 0.0, 0.0))
|
|
let lat = float3(-toSpot.z, 0.0, toSpot.x)
|
|
let fanOffset = float(int(uint(eid) % 5u) - 2) * 28.0
|
|
var cand = spotPos - toSpot * 65.0 + lat * fanOffset
|
|
cand = wto_clamp_battle_xz(cand, world.battleCenter, world.battleRadius, 40.0)
|
|
if !wto_terrain_point_valid(army, cand)
|
|
cand = spotPos
|
|
outPos = cand
|
|
return true
|