Files
WT-Offline/game-ai/ground/wto_ai_fire_common.das
T
N0VAN0M4LY ac35c25851 feat(ground-ai): add ready-rack dual-ammo bot spawn loadouts and shell switching
Select realistic ready-rack dual-shell loadouts for AI tanks on the local
server, inject them natively on bot spawn, and switch between anti-armor
and soft-target shells in battle:

- Add the loadout section to tuning/ground-ai.jsonc (lowReadyRackThreshold,
  topUpAmmoCount, primaryAmmoPct, secondaryMinRounds,
  penetrationCompareDistM), validated in ground-ai-tuning.ts.
- Add the tank-bot-ammo v2 dataset (apps/server/src/game-data/datasets/
  tank-bot-ammo.ts) reading primary cannon weapon name, capacity,
  firstStage/autoLoad shell ready-rack counts (excluding separate charges),
  and selectable shell modifications (using Lanz-Odermatt kinetic fields,
  cumulativeDamage, armorpower, and transitive reqModification chains).
- Add selectBotTankLoadouts (apps/server/src/battle/bot-tank-loadouts.ts):
  carry ready-rack count when >= 15 (top up to 22 when smaller or absent,
  clamped to gun capacity), skip SPAA/belt autocannons, pick highest-tier
  APFSDS/anti-armor primary (~80%) plus HE/HESH/HEAT secondary (~20%, min
  3 rounds) when available, and attach required addModification entries and
  per-tank wtoAmmo_<unit> strings to customBots when mode != stock.
- Hook Unit::changeAircraft (already located dynamically on .37 and .58) in
  native/wto-net/src/local_mp_loadout.c to assign the 24-byte
  ChangeUnitReflection.bullets entries via bullet_vec_assign for LocalMP
  bot spawns (async == 0, non-local bot MPlayer), leaving player spawns
  untouched.
- Track soft-target vs anti-armor shell classes in wto_ai_specs_common.das
  and write gun.nextBulletType in wto_ai_fire_common.das to queue HE/HESH
  against light/narrow targets and APFSDS/AP against MBTs/heavy armor.

Verified MSVC build of wto_ws.dll (0 warnings in project C sources), root
and launcher type checks, 14/14 check-patches on 2.61.0.58, 12/12 LocalMP
module installation, clean daScript load (wto_ai_runtime.das loaded in
211 ms, 0 compile errors), and real 2.61.0.58 loadout selection on
cn_ztz_96a (18 APFSDS + 4 HE), germ_leopard_2a4 (12 DM23 + 3 HEATFS),
ussr_t_80bvm (22 3BM60 + 6 HE), us_m1a2_abrams (14 M829A2 + 4 HEAT-MP),
ussr_t_34_1941 (18 APHEBC + 4 HE), and ussr_zsu_23_4 (stock belt kept).
Live battle acceptance begins in Step 9.
2026-12-03 19:25:04 +08:00

297 lines
12 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
[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_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&)
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 anyReserve = false
var totalLoaded = 0
foreach_primary_gun(unit.weap.Weapons) <| $(var weapon)
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 fc.turretNo < 0
fc.turretNo = weapon.turretNo
fc.gunReady = true
totalLoaded += weapon.countBullets
return
anyGun = true
if gun.isInoperable
return
anyAlive = true
if fc.turretNo < 0
fc.turretNo = weapon.turretNo
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
[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_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, 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.1) * dtStep
let maxPitchStep = max(10.0, slot.spec.turretPitchSpd * 1.1) * dtStep
let yawDelta = clamp(wto_wrap_deg(wishYawDeg - fc.curYawDeg), -maxYawStep, maxYawStep)
let pitchDelta = clamp(wishPitchDeg - fc.curPitchDeg, -maxPitchStep, maxPitchStep)
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 public wto_fire_update(world : WtoWorldSnap const&; var unit : Unit?;
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
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
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_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.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_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
let distXZ = sqrt(wto_xz_dist_sq(unit.pos, slot.perceive.knownPos))
wto_fire_gate_try_shoot(world, unit, now, dt, distXZ, muzzlePos, aimPt, liveBarrelDir, wishWorldDir, slot, fireBudget)