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

170 lines
6.7 KiB
Plaintext

// 6-channel no-wallhack perception: foliage-filtered visual LOS, close engine & gunfire audio,
// team radio sharing, hit retaliation alert, frozen visual memory, strong target stickiness,
// smoothed target velocity, and fanned-out radio hunting bearings.
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_types_common
require %appGame.wtoai.wto_ai_terrain_common
require %appGame.wtoai.wto_ai_world_common
[never_inline]
def private wto_check_visual_enemy(myEye, lookDir : float3; cosHalfFov : float;
enemy : WtoUnitSnap const&; var outDist : float&) : bool
let aimMid = enemy.pos + float3(0.0, enemy.isAir ? 0.0 : 1.35, 0.0)
let delta = aimMid - myEye
let dist = length(delta)
outDist = dist
let maxRange = enemy.isAir ? 2600.0 : WTO_AI_TACTICS_VISION_RANGE_M
if dist > maxRange || dist < 1.0
return false
let dirXZ = wto_xz_normalize(delta, lookDir)
let inCone = dot(dirXZ, lookDir) >= cosHalfFov
let inPeriph = dist <= WTO_AI_TACTICS_PERIPHERAL_RANGE_M
if !inCone && !inPeriph && !enemy.isAir
return false
if wto_terrain_los_clear(myEye, aimMid)
return true
if !enemy.isAir && dist <= 900.0
let aimTurret = enemy.pos + float3(0.0, 2.05, 0.0)
if wto_terrain_los_clear(myEye, aimTurret)
return true
return false
[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 retaliation alert (never reveals hidden position, only direction).
if (now - p.lastDamagedAt) <= 4.0 && !!p.lastOffenderEid
let offIdx = wto_world_find_unit(world, p.lastOffenderEid)
if offIdx >= 0 && world.units[offIdx].army != slot.army
let d = world.units[offIdx].pos - myPos
p.alertYawRad = atan2(d.z, d.x)
p.alertUntil = now + 4.5
return
// Priority B: Close enemy engine / cannon audio.
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 alert bearing.
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_select_best_visual_enemy(world : WtoWorldSnap const&; now : float;
myEye, lookDir : float3; slot : WtoBotSlot const&) : int
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
var bestIdx = -1
var bestScore = 1.0e9
for i in range(world.unitCount)
assume u = world.units[i]
if !u.isAlive || u.army == slot.army || (u.isAir && !isSpaa)
continue
var dist = 0.0
if !wto_check_visual_enemy(myEye, lookDir, cosHalfFov, u, dist)
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
bestIdx = i
return bestIdx
[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))
let bestIdx = wto_select_best_visual_enemy(world, now, myEye, lookDir, slot)
if bestIdx >= 0
assume tgt = world.units[bestIdx]
let sameTarget = p.targetEid == tgt.eid
p.targetEid = tgt.eid
p.targetIsAir = tgt.isAir
p.hasClearLos = true
p.knownPos = tgt.pos
let alpha = sameTarget ? clamp(dt * 6.0, 0.18, 0.65) : 1.0
p.smoothedVel = sameTarget ? lerp(p.smoothedVel, tgt.vel, alpha) : tgt.vel
p.lastSeenAt = now
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
p.hasClearLos = false
p.smoothedVel *= 0.5
if !!p.targetEid && (now - p.lastSeenAt) > WTO_AI_TACTICS_MEMORY_TTL_SEC
p.targetEid = INVALID_ENTITY_ID
p.lockedEid = INVALID_ENTITY_ID
wto_update_audio_and_radio_alerts(world, now, myPos, slot)
// Compute a fanned-out hunting anchor toward a team radio spot so multiple bots pincer from
// spread-out angles instead of converging onto the exact same coordinate.
[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