Files
WT-Offline/game-ai/ground/wto_ai_fire_common.das
T
N0VAN0M4LY 0843fde6ff fix(ground-ai): respect mounted gun traverse limits
Read the selected gun mount limits from the vehicle data and constrain its aiming commands. Send out-of-traverse aim requests to the chassis driver and require alignment before firing.

Verified type checks, executable patch checks and builds on 2.61.0.37 and 2.61.0.64, and guarded daScript initialization on 2.61.0.64. In-battle acceptance remains pending.
2026-12-05 18:36:19 +08:00

246 lines
9.4 KiB
Plaintext

// Turret servo, active loaded-shell inspection, and ballistic aim solver.
// All ground weapon firing goes exclusively through wto_fire_gate_try_shoot.
options indenting = 2
options no_global_heap = true
module wto_ai_fire_common shared
require ecs
require app
require math
require strings
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_combat_types_common
require %appGame.wtoai.wto_ai_types_common
require %appGame.wtoai.wto_ai_world_common
require %appGame.wtoai.wto_ai_estimate_common
require %appGame.wtoai.wto_ai_fire_gate_common
require %appGame.wtoai.wto_ai_traverse_common
[never_inline]
def private wto_sync_active_shell(gun : WTGun const?; spec : WtoVehicleSpec const&; var fc : WtoFireState&)
if gun == null
return
let chosenIdx = int(gun.chosenBulletType)
if fc.loadedBulletIdx == chosenIdx && fc.activeSpeedMps > 80.0
return
fc.loadedBulletIdx = chosenIdx
fc.activeSpeedMps = spec.muzzleSpeedMps
fc.activeDragCx = spec.shellDragCx
fc.activeMassKg = spec.shellMassKg
fc.activeCaliberM = spec.shellCaliberM
if chosenIdx >= 0 && chosenIdx < length(gun.properties.bulletSets)
assume bs = gun.properties.bulletSets[chosenIdx]
if length(bs.bullet) > 0 && bs.bullet[0] != null
let bName = bs.bullet[0].getName
if !empty(bName)
let h = ecs_hash(bName)
for i in range(spec.shellCount)
if spec.shells[i].nameHash == h
fc.activeSpeedMps = spec.shells[i].speedMps
fc.activeDragCx = spec.shells[i].dragCx
fc.activeMassKg = spec.shells[i].massKg
fc.activeCaliberM = spec.shells[i].caliberM
return
[never_inline]
def private wto_select_next_shell(var gun : WTGun?; spec : WtoVehicleSpec const&; wantSoft : bool)
if gun == null || spec.isAutocannonBelt
return
let setCount = length(gun.properties.bulletSets)
if setCount < 2
return
var bestSoft = -1
var bestArmor = -1
var firstAvail = -1
for bsIdx in range(setCount)
assume bs = gun.properties.bulletSets[bsIdx]
if bs.bulletsCount <= 0 || length(bs.bullet) <= 0 || bs.bullet[0] == null
continue
if firstAvail < 0
firstAvail = bsIdx
let bName = bs.bullet[0].getName
if empty(bName)
continue
let h = ecs_hash(bName)
for si in range(spec.shellCount)
if spec.shells[si].nameHash != h
continue
if spec.shells[si].isSoftShell && bestSoft < 0
bestSoft = bsIdx
elif spec.shells[si].isAntiArmor && bestArmor < 0
bestArmor = bsIdx
break
var wantIdx = wantSoft ? (bestSoft >= 0 ? bestSoft : bestArmor) : (bestArmor >= 0 ? bestArmor : firstAvail)
if wantIdx < 0
wantIdx = firstAvail
if wantIdx >= 0 && int(gun.nextBulletType) != wantIdx
gun.nextBulletType = uint8(wantIdx)
[never_inline]
def private wto_inspect_primary_weapon(var unit : Unit?; now : float;
spec : WtoVehicleSpec const&; wantSoftShell : bool;
var fc : WtoFireState&) : int
let previousTurret = fc.turretNo
fc.turretNo = -1
fc.gunReady = false
fc.reloading = false
fc.gunDamaged = false
if unit == null || unit.weap == null
return -1
var selected = -1
var nextIndex = 0
var anyGun = false
var anyAlive = false
var anyReserve = false
var totalLoaded = 0
foreach_primary_gun(unit.weap.Weapons) <| $(var weapon)
let index = nextIndex
nextIndex++
if weapon == null || weapon.isDummyGun
return
var gun = as_gun(weapon)
if gun == null
if weapon.prop.launcher && weapon.countBullets > 0
anyGun = true
anyAlive = true
anyReserve = true
if selected < 0
selected = index
fc.turretNo = weapon.turretNo
fc.gunReady = true
totalLoaded += weapon.countBullets
return
anyGun = true
if gun.isInoperable
return
anyAlive = true
if selected < 0
selected = index
fc.turretNo = weapon.turretNo
if fc.turretNo != previousTurret
fc.loadedBulletIdx = -1
wto_select_next_shell(gun, spec, wantSoftShell)
wto_sync_active_shell(gun, spec, fc)
if gun.isReadyToShoot
fc.gunReady = true
totalLoaded += max(0, gun.currentLoadedAmmoCount)
if gun.currentLoadedAmmoCount > 0 || gun.currentBulletSet.bulletsCount > 0 || weapon.countBullets > 0
anyReserve = true
fc.gunDamaged = anyGun && !anyAlive
fc.reloading = anyAlive && !fc.gunReady && anyReserve
if fc.pendingShotConfirm
if (fc.prevLoadedAmmo >= 0 && totalLoaded < fc.prevLoadedAmmo) || !fc.gunReady
fc.shotCount++
fc.pendingShotConfirm = false
elif (now - fc.pendingShotAt) > 0.35
fc.pendingShotConfirm = false
fc.prevLoadedAmmo = totalLoaded
return selected
[never_inline]
def private wto_solve_ballistic_aim(muzzlePos, myVel : float3; var slot : WtoBotSlot&;
aimBase : float3; var outAimPt, outWishDir : float3&) : float
var leadPt = aimBase
let rawDist = wto_est_compute_biased_lead(muzzlePos, myVel, aimBase, slot, leadPt)
let dragLoss = clamp(slot.fire.activeDragCx * 0.00011 * rawDist, 0.0, 0.28)
let vEff = max(120.0, slot.fire.activeSpeedMps * (1.0 - dragLoss))
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_muzzle(unit : Unit const?; primaryIndex : int;
var outPos, outDir : float3&) : bool
if unit == null || unit.weap == null || primaryIndex < 0
return false
var sampled = false
get_weapons_by_trigger_group(unit.weap.Weapons, WeaponTriggerGroups.TRIGGER_GROUP_PRIMARY) <| $(weapons : array<Weapon const? const> const#)
if primaryIndex >= length(weapons)
return
assume weapon = weapons[primaryIndex]
if weapon == null
return
var gunTm : float3x4
weapon_calcShootTm(weapon, unit.unitTm, gunTm)
if length_sq(gunTm[0]) > 0.25
outPos = gunTm[3]
outDir = normalize(gunTm[0])
sampled = true
return sampled
def private wto_clear_aim_tracking(var fc : WtoFireState&)
fc.aimTargetEid = INVALID_ENTITY_ID
fc.aimDriftDegSec = 0.0
fc.settleHoldSec = 0.0
fc.lastAligned = false
fc.lastDenyReason = WTO_DENY_NO_TARGET
[never_inline]
def public wto_fire_update(world : WtoWorldSnap const&; var unit : Unit?; className : string;
now, dt : float; var slot : WtoBotSlot&; var fireBudget : int&)
if !WTO_AI_LAYER_FIRE_CONTROL || unit == null || unit.weap == null
return
// SPAA vs aircraft only delegates to stock air-target fire control (never ground targets).
if slot.tactics.spaaYieldAir
wto_set_hull_aim(unit, false, 0.0, unit.unitTm[0], slot)
wto_clear_aim_tracking(slot.fire)
var gm = as_gm(unit)
if gm != null
*gm |> set_check_visibility_target(true)
*gm |> set_ignore_stop_for_fire(true)
set_unit_attack_type(unit, "attack_target")
return
set_unit_attack_type(unit, "dont_aim")
var wantSoftShell = false
if slot.perceive.hasClearLos && !!slot.perceive.targetEid && !slot.perceive.targetIsAir
let tgtIdx = wto_world_find_unit(world, slot.perceive.targetEid)
if tgtIdx >= 0
wantSoftShell = world.units[tgtIdx].isLightTag || world.units[tgtIdx].halfWid < 1.35
let primaryIndex = wto_inspect_primary_weapon(unit, now, slot.spec, wantSoftShell, slot.fire)
var muzzlePos = unit.pos + float3(0.0, 1.95, 0.0)
var liveBarrelDir = unit.unitTm[0]
let _preSample = wto_sample_muzzle(unit, primaryIndex, 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.state == WTO_STATE_DISABLED))
if engageValid
let baseAim = slot.perceive.visibleAimPt
distToTgt = wto_solve_ballistic_aim(muzzlePos, unit.vel, slot, baseAim, aimPt, wishWorldDir)
elif now < slot.perceive.watchUntil
wishWorldDir = normalize(slot.perceive.watchPos - muzzlePos)
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_mounted_gun(unit, className, dt, wishYaw, wishPitch, slot)
let canEngage = engageValid && distToTgt <= WTO_AI_FC_MAX_RANGE_M && wto_sample_muzzle(unit, primaryIndex, muzzlePos, liveBarrelDir)
wto_set_hull_aim(unit, canEngage, wishYaw, liveBarrelDir, slot)
if !canEngage
wto_clear_aim_tracking(slot.fire)
return
let distXZ = sqrt(wto_xz_dist_sq(unit.pos, slot.perceive.knownPos))
wto_fire_gate_try_shoot(world, unit, now, distXZ, muzzlePos, aimPt, liveBarrelDir, wishWorldDir, slot, fireBudget)