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

275 lines
12 KiB
Plaintext

// Deterministic custom fire-control & ballistics actuator:
// - Keeps stock C++ ground inaccuracy ("attack_target" sway & target list bloat) locked out via
// "dont_aim", yielding only for SPAA vs aircraft or missile-only vehicles;
// - Reads authoritative `gun.isReadyToShoot`, `gun.currentLoadedAmmoCount`, and live muzzle
// `weapon_calcShootTm` transform (`gunTm[3]` muzzle position, `gunTm[0]` barrel vector);
// - Solves ballistic elevation & smoothed velocity lead via `Ballistics::calculate_arc_low_start_dir`;
// - Enforces strict barrel angular-velocity settle (`settleHoldSec`) + friendly corridor safety
// before triggering `unit_single_shot`.
options indenting = 2
options no_global_heap = true
module wto_ai_fire_common shared
require ecs
require app
require math
require DagorMath
require Mission
require Unit
require IGroundModel
require Weapon
require Ballistics
require %game.utils.utils_common
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_world_to_hull_deg(worldDir, hullFwd, hullUp, hullLeft : float3;
var outYawDeg, outPitchDeg : float&)
let len = length(worldDir)
if len < 1.0e-5
outYawDeg = 0.0
outPitchDeg = 0.0
return
let d = worldDir / len
let f = dot(d, hullFwd)
let u = dot(d, hullUp)
let l = dot(d, hullLeft)
let horiz = sqrt(f * f + l * l)
outYawDeg = horiz > 1.0e-5 ? (-atan2(l, f) * WTO_RAD_TO_DEG) : 0.0
outPitchDeg = clamp(atan2(u, max(1.0e-5, horiz)) * WTO_RAD_TO_DEG, -24.0, 65.0)
[never_inline]
def private wto_hull_deg_to_world(yawDeg, pitchDeg : float; hullFwd, hullUp, hullLeft : float3) : float3
let yr = yawDeg * WTO_DEG_TO_RAD
let pr = pitchDeg * WTO_DEG_TO_RAD
let cp = cos(pr)
let dir = hullFwd * (cp * cos(yr)) - hullLeft * (cp * sin(yr)) + hullUp * sin(pr)
let l = length(dir)
return l > 1.0e-5 ? (dir / l) : hullFwd
[never_inline]
def private wto_inspect_primary_weapon(unit : Unit const?; var fc : WtoFireState&)
fc.turretNo = -1
fc.gunReady = false
fc.reloading = false
fc.gunDamaged = false
if unit == null || unit.weap == null
return
var anyGun = false
var anyAlive = false
var anyLoaded = false
foreach_primary_gun(unit.weap.Weapons) <| $(weapon)
if weapon == null || weapon.isDummyGun || weapon.prop.launcher
return
let gun = as_gun(weapon)
if gun == null
return
anyGun = true
if gun.isInoperable
return
anyAlive = true
if fc.turretNo < 0
fc.turretNo = weapon.turretNo
if gun.isReadyToShoot
fc.gunReady = true
if gun.currentLoadedAmmoCount > 0 || gun.currentBulletSet.bulletsCount > 0 || weapon.countBullets > 0
anyLoaded = true
fc.gunDamaged = anyGun && !anyAlive
fc.reloading = anyAlive && !fc.gunReady && anyLoaded
[never_inline]
def private wto_friendly_corridor_clear(world : WtoWorldSnap const&; selfEid : EntityId;
army : int; muzzlePos, aimPos : float3) : bool
let delta = aimPos - muzzlePos
let segLen = length(delta)
if segLen < 4.0
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 core = u.pos + float3(0.0, 1.3, 0.0)
let along = dot(core - muzzlePos, dir)
if along <= 1.5 || along >= segLen - 1.5
continue
let clearR = WTO_AI_FC_FRIENDLY_CORRIDOR_M + u.halfWid
if length_sq(core - (muzzlePos + dir * along)) < clearR * clearR
return false
return true
[never_inline]
def private wto_solve_ballistic_aim(muzzlePos : float3; myVel : float3; slot : WtoBotSlot const&;
tgtPos : float3; var outAimPt, outWishDir : float3&) : float
// Prefer center-of-mass (+1.25m); shift up to turret (+1.85m) when lower hull is terrain-masked!
var aimPt = tgtPos + float3(0.0, 1.25, 0.0)
if !wto_terrain_los_clear(muzzlePos, aimPt)
aimPt = tgtPos + float3(0.0, 1.88, 0.0)
let rawDist = length(aimPt - muzzlePos)
let dragLoss = clamp(slot.spec.shellDragCx * 0.00011 * rawDist, 0.0, 0.28)
let vEff = max(180.0, slot.spec.muzzleSpeedMps * (1.0 - dragLoss))
var leadPt = aimPt
for _iter in range(2)
let tof = clamp(length(leadPt - muzzlePos) / vEff, 0.0, 3.2)
let selfComp = slot.spec.hasVertStab ? (myVel * (tof * 0.15)) : float3(0.0, 0.0, 0.0)
leadPt = aimPt + slot.perceive.smoothedVel * tof - selfComp
outAimPt = leadPt
var arcDir = float3(0.0, 0.0, 0.0)
if calculate_arc_low_start_dir(muzzlePos, leadPt, vEff, arcDir) && length_sq(arcDir) > 0.25
outWishDir = normalize(arcDir)
else
let d = max(1.0, length(leadPt - muzzlePos))
let tof = d / vEff
let dropComp = float3(0.0, 0.5 * 9.81 * tof * tof, 0.0)
outWishDir = normalize(leadPt + dropComp - muzzlePos)
return rawDist
[never_inline]
def private wto_sample_turret_muzzle(unit : Unit const?; turretNo : int;
var outPos, outDir : float3&) : bool
if unit == null || unit.weap == null
return false
for tIdx, turret in iter_range(unit.weap.turret), unit.weap.turret
if (turretNo >= 0 && tIdx != turretNo) || (turretNo < 0 && 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
continue
var gunTm : float3x4
weapon_calcShootTm(weapon, unit.unitTm, gunTm)
if length_sq(gunTm[0]) > 0.25
outPos = gunTm[3]
outDir = normalize(gunTm[0])
return true
return false
[never_inline]
def private wto_drive_turret_and_sample_barrel(var unit : Unit?; dt : float; now : float;
wishYawDeg, wishPitchDeg : float;
var slot : WtoBotSlot&;
var outMuzzlePos, outLiveDir : float3&)
assume fc = slot.fire
let dtStep = clamp(dt, 0.005, 0.12)
for tIdx, turret in iter_range(unit.weap.turret), unit.weap.turret
if (fc.turretNo >= 0 && tIdx != fc.turretNo) || (fc.turretNo < 0 && int(turret.triggerGroupNo) != int(WeaponTriggerGroups.TRIGGER_GROUP_PRIMARY))
continue
fc.curYawDeg = turret.currentState.yaw
fc.curPitchDeg = turret.currentState.pitch
if !fc.servoInit
fc.cmdYawDeg = fc.curYawDeg
fc.cmdPitchDeg = fc.curPitchDeg
fc.prevPitchDeg = fc.curPitchDeg
fc.servoInit = true
let pitchSpd = abs(fc.curPitchDeg - fc.prevPitchDeg) / dtStep
fc.prevPitchDeg = fc.curPitchDeg
if slot.perceive.hasClearLos && wishPitchDeg < fc.curPitchDeg - 2.3 && pitchSpd < 0.8
fc.pitchClampSec = min(2.0, fc.pitchClampSec + dtStep)
fc.pitchBlocked = fc.pitchClampSec >= 0.75
else
fc.pitchClampSec = max(0.0, fc.pitchClampSec - dtStep * 2.5)
if fc.pitchClampSec <= 0.05
fc.pitchBlocked = false
let maxYawStep = max(24.0, slot.spec.turretYawSpd * 1.35) * dtStep
let maxPitchStep = max(10.0, slot.spec.turretPitchSpd * 1.45) * dtStep
let yawDelta = clamp(wto_wrap_deg(wishYawDeg - fc.curYawDeg), -maxYawStep * 3.0, maxYawStep * 3.0)
let pitchDelta = clamp(wishPitchDeg - fc.curPitchDeg, -maxPitchStep * 3.0, maxPitchStep * 3.0)
fc.cmdYawDeg = wto_wrap_deg(fc.curYawDeg + yawDelta)
fc.cmdPitchDeg = clamp(fc.curPitchDeg + pitchDelta, slot.spec.minPitchDeg - 2.0, slot.spec.maxPitchDeg + 2.0)
turret.yawLim = fc.cmdYawDeg
turret.pitchLim = fc.cmdPitchDeg
break
var uPos = unit.pos
updateWeaponAnims(*unit.weap, dtStep, 1.0, uPos)
if !wto_sample_turret_muzzle(unit, fc.turretNo, outMuzzlePos, outLiveDir)
outLiveDir = wto_hull_deg_to_world(fc.curYawDeg, fc.curPitchDeg, unit.unitTm[0], unit.unitTm[1], unit.unitTm[2])
let bAge = now - fc.prevBarrelAt
if bAge > 1.0e-4 && bAge <= 0.25
let stepDeg = acos(clamp(dot(outLiveDir, fc.prevBarrelDir), -1.0, 1.0)) * WTO_RAD_TO_DEG
fc.barrelOmegaDegSec = lerp(stepDeg / bAge, fc.barrelOmegaDegSec, 0.45)
else
fc.barrelOmegaDegSec = 0.0
fc.prevBarrelDir = outLiveDir
fc.prevBarrelAt = now
[never_inline]
def private wto_try_delegate_stock_fire(var unit : Unit?; var gm : IGroundModel?; slot : WtoBotSlot const&) : bool
if !slot.tactics.spaaYieldAir && !(slot.spec.isPureLauncher && slot.perceive.hasClearLos && !!slot.perceive.targetEid)
return false
if gm != null
*gm |> set_check_visibility_target(true)
*gm |> set_ignore_stop_for_fire(slot.tactics.spaaYieldAir)
if slot.spec.isPureLauncher
var tgtU = get_unit_by_eid(slot.perceive.targetEid)
if tgtU != null && tgtU.isAlive
add_unit_for_target(*gm, tgtU)
set_unit_attack_type(unit, "attack_target")
return true
[never_inline]
def private wto_try_pull_trigger(world : WtoWorldSnap const&; var unit : Unit?;
now, dt, distToTgt : float; muzzlePos, aimPt, liveDir, wishDir : float3;
var slot : WtoBotSlot&)
assume fc = slot.fire
let aimErrDeg = acos(clamp(dot(liveDir, wishDir), -1.0, 1.0)) * WTO_RAD_TO_DEG
fc.lastAimErrDeg = aimErrDeg
let allowErrDeg = clamp(atan2(WTO_AI_FC_CLOSE_HIT_RADIUS_M, max(distToTgt, 6.0)) * WTO_RAD_TO_DEG, WTO_AI_FC_BASE_ALIGN_DEG, 2.4)
let chassisStable = slot.spec.hasVertStab || length(unit.vel) <= WTO_AI_FC_UNSTAB_STOP_MPS || distToTgt < 22.0
let settled = aimErrDeg <= allowErrDeg && fc.barrelOmegaDegSec <= WTO_AI_FC_BARREL_SETTLE_DEG_SEC && chassisStable
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 || fc.settleHoldSec < WTO_AI_FC_SETTLE_HOLD_SEC || (now - fc.lastShotAt) < minCadence
return
if wto_terrain_hard_ray(muzzlePos, liveDir, min(distToTgt - 1.5, 45.0)) < min(distToTgt - 2.0, 42.0)
return
if !wto_friendly_corridor_clear(world, slot.eid, slot.army, muzzlePos, aimPt)
return
unit_single_shot(unit, int(WeaponTriggerGroups.TRIGGER_GROUP_PRIMARY))
fc.lastShotAt = now
fc.shotCount++
fc.shotErrSumDeg += aimErrDeg
fc.maxShotErrDeg = max(fc.maxShotErrDeg, aimErrDeg)
if !slot.spec.isAutocannonBelt
fc.settleHoldSec = 0.0
[never_inline]
def public wto_fire_update(world : WtoWorldSnap const&; var unit : Unit?;
now, dt : float; var slot : WtoBotSlot&)
if !WTO_AI_LAYER_FIRE_CONTROL || unit == null || unit.weap == null
return
if wto_try_delegate_stock_fire(unit, as_gm(unit), slot)
return
set_unit_attack_type(unit, "dont_aim")
wto_inspect_primary_weapon(unit, slot.fire)
var muzzlePos = unit.pos + float3(0.0, 1.95, 0.0)
var liveBarrelDir = unit.unitTm[0]
let _preSample = wto_sample_turret_muzzle(unit, slot.fire.turretNo, muzzlePos, liveBarrelDir)
let hFwd = unit.unitTm[0]
var wishWorldDir = hFwd
var aimPt = muzzlePos + hFwd * 50.0
var distToTgt = 9999.0
let engageValid = (slot.perceive.hasClearLos && !!slot.perceive.targetEid && !slot.perceive.targetIsAir &&
!(slot.spec.role == WTO_ROLE_SPAA && slot.tactics.posture == WTO_POSTURE_RETREAT))
if engageValid
distToTgt = wto_solve_ballistic_aim(muzzlePos, unit.vel, slot, slot.perceive.knownPos, aimPt, wishWorldDir)
elif !!slot.perceive.targetEid && (now - slot.perceive.lastSeenAt) <= WTO_AI_TACTICS_MEMORY_TTL_SEC
wishWorldDir = normalize((slot.perceive.knownPos + float3(0.0, 1.4, 0.0)) - muzzlePos)
elif now < slot.perceive.alertUntil
wishWorldDir = float3(cos(slot.perceive.alertYawRad), 0.0, sin(slot.perceive.alertYawRad))
var wishYaw = 0.0
var wishPitch = 0.0
wto_world_to_hull_deg(wishWorldDir, hFwd, unit.unitTm[1], unit.unitTm[2], wishYaw, wishPitch)
wto_drive_turret_and_sample_barrel(unit, dt, now, wishYaw, wishPitch, slot, muzzlePos, liveBarrelDir)
if !engageValid || distToTgt > WTO_AI_FC_MAX_RANGE_M
slot.fire.settleHoldSec = 0.0
slot.fire.lastAligned = false
return
wto_try_pull_trigger(world, unit, now, dt, distToTgt, muzzlePos, aimPt, liveBarrelDir, wishWorldDir, slot)