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

171 lines
7.0 KiB
Plaintext

// Authoritative per-vehicle specification loader: reads unit & weapon DataBlocks once per
// spawned bot (`gamedata/units/<className>.blk`) to extract role, gun stabilizer,
// turret slew rates, shell muzzle velocity, and chassis capabilities.
options indenting = 2
options no_global_heap = true
module wto_ai_specs_common shared
require ecs
require app
require math
require strings
require DagorMath
require DagorDataBlock
require Unit
require IGroundModel
require HeavyVehicleModel
require Weapon
require %game.utils.utils_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_traverse_common
[never_inline]
def private wto_parse_role_strings(unitType : string; expClass : string; isLightTag : bool) : int
if unitType == "typeSPAA" || expClass == "exp_SPAA"
return WTO_ROLE_SPAA
if unitType == "typeTankDestroyer" || expClass == "exp_tank_destroyer"
return WTO_ROLE_TD
if unitType == "typeHeavyTank" || expClass == "exp_heavy_tank"
return WTO_ROLE_HEAVY
if unitType == "typeLightTank" || isLightTag
return WTO_ROLE_LIGHT
return WTO_ROLE_MEDIUM
[never_inline]
def private wto_record_shell_block(bBlk : DataBlock const&; var spec : WtoVehicleSpec&; isDefault : bool)
let spd = bBlk |> datablock_getReal("speed", 0.0)
if spd <= 80.0 || spd >= 2600.0
return
let cx = clamp(bBlk |> datablock_getReal("Cx", 0.35), 0.05, 1.5)
let mass = clamp(bBlk |> datablock_getReal("mass", 10.0), 0.05, 250.0)
let rawCal = bBlk |> datablock_getReal("ballisticCaliber", 0.0)
let cal = clamp(rawCal > 0.002 ? rawCal : bBlk |> datablock_getReal("caliber", 0.09), 0.005, 0.4)
if isDefault && spec.shellCount == 0
spec.muzzleSpeedMps = spd
spec.shellDragCx = cx
spec.shellMassKg = mass
spec.shellCaliberM = cal
let bName = bBlk |> datablock_getStr("bulletName", "")
if empty(bName) || spec.shellCount >= WTO_MAX_SHELL_PROFILES
return
let h = ecs_hash(bName)
for i in range(spec.shellCount)
if spec.shells[i].nameHash == h
return
let idx = spec.shellCount
let bt = bBlk |> datablock_getStr("bulletType", "")
let isSoft = find(bt, "he_frag") >= 0 || find(bt, "hesh") >= 0
let isNonCombat = find(bt, "smoke") >= 0 || find(bt, "practice") >= 0 || find(bt, "shrapnel") >= 0
spec.shells[idx].nameHash = h
spec.shells[idx].speedMps = spd
spec.shells[idx].dragCx = cx
spec.shells[idx].massKg = mass
spec.shells[idx].caliberM = cal
spec.shells[idx].isSoftShell = isSoft
spec.shells[idx].isAntiArmor = !isSoft && !isNonCombat
spec.shellCount = idx + 1
[never_inline]
def private wto_parse_weapon_blk(weapBlkPath : string; var spec : WtoVehicleSpec&)
if empty(weapBlkPath) || find(weapBlkPath, "dummy_weapon") >= 0
return
datablock_load_cached(weapBlkPath, DataBlockReadFlag.ROBUST) <| $(wBlk)
let isRocket = wBlk |> datablock_getBool("rocketGun", false)
let isCannon = wBlk |> datablock_getBool("cannon", false)
let isBelt = wBlk |> datablock_getBool("isBulletBelt", false)
if isRocket && !isCannon
return
spec.isAutocannonBelt = isBelt
spec.shellCount = 0
datablock_for_each_block(wBlk, "bullet") <| $(bBlk)
wto_record_shell_block(bBlk, spec, true)
datablock_for_each_block(wBlk) <| $(subBlk)
datablock_for_each_block(subBlk, "bullet") <| $(bBlk)
wto_record_shell_block(bBlk, spec, false)
[never_inline]
def private wto_parse_unit_blk(className : string; isLightTag : bool; var spec : WtoVehicleSpec&) : bool
if empty(className)
return false
let relPath = wto_unit_model_path(className)
var loaded = false
datablock_load_cached(relPath, DataBlockReadFlag.ROBUST) <| $(mBlk)
if mBlk.paramCount == 0u && mBlk.blockCount == 0u
return
loaded = true
let uType = mBlk |> datablock_getStr("type", "")
let expCls = mBlk |> datablock_getStr("expClass", "")
spec.role = wto_parse_role_strings(uType, expCls, isLightTag)
let fwdKmh = mBlk |> datablock_getReal("maxFwdSpeed", 0.0)
let revKmh = mBlk |> datablock_getReal("maxRevSpeed", 0.0)
if fwdKmh > 5.0
spec.vMaxFwd = clamp(fwdKmh / 3.6, 4.0, 28.0)
if revKmh > 1.0
spec.vMaxRev = clamp(revKmh / 3.6, 1.0, 16.0)
let physBlk = mBlk |> datablock_get_block_by_name("VehiclePhys")
if physBlk != null
let mechanics = *physBlk |> datablock_get_block_by_name("mechanics")
if mechanics != null
spec.canPivot = (*mechanics |> datablock_getReal("neutralGearRatio", 0.0)) > 0.0
let commonBlk = mBlk |> datablock_get_block_by_name("commonWeapons")
if commonBlk == null
return
var primaryParsed = false
datablock_for_each_block(*commonBlk, "Weapon") <| $(weapBlk)
if primaryParsed
return
let trig = weapBlk |> datablock_getStr("triggerGroup", "primary")
let wPath = weapBlk |> datablock_getStr("blk", "")
if trig != "primary" || empty(wPath) || find(wPath, "dummy_weapon") >= 0 || find(wPath, "machinegun") >= 0
return
primaryParsed = true
let sy = weapBlk |> datablock_getReal("speedYaw", 24.0)
let sp = weapBlk |> datablock_getReal("speedPitch", 8.0)
spec.turretYawSpd = clamp(sy, 4.0, 120.0)
spec.turretPitchSpd = clamp(sp, 2.0, 90.0)
let stabBlk = weapBlk |> datablock_get_block_by_name("gunStabilizer")
if stabBlk != null
spec.hasVertStab = *stabBlk |> datablock_getBool("hasVertical", false)
wto_parse_weapon_blk(wPath, spec)
return loaded
[never_inline]
def private wto_apply_runtime_fallbacks(unit : Unit const?; gm : IGroundModel const?;
isLightTag : bool; blkLoaded : bool; var spec : WtoVehicleSpec&)
if gm != null
let box = gm.bodyBoundingBox
spec.halfLen = max(2.0, max(abs(box.boxMin.x), abs(box.boxMax.x)) + 0.15)
spec.halfWid = max(1.0, max(abs(box.boxMin.z), abs(box.boxMax.z)) + 0.18)
let hv = unit != null ? as_hv(unit) : null
if hv != null && hv.getMaxSpeed > 1.5 && wto_is_finite(hv.getMaxSpeed)
spec.vMaxFwd = min(spec.vMaxFwd, hv.getMaxSpeed)
if unit == null || unit.weap == null
return
var gunCount = 0
var launcherCount = 0
foreach_primary_gun(unit.weap.Weapons) <| $(weapon)
if weapon == null || weapon.isDummyGun
return
if weapon.prop.launcher
launcherCount++
elif as_gun(weapon) != null
gunCount++
spec.isPureLauncher = gunCount == 0 && launcherCount > 0
if !blkLoaded
if length(unit.weap.turret) == 0 && gunCount > 0
spec.role = WTO_ROLE_TD
elif isLightTag
spec.role = WTO_ROLE_LIGHT
elif spec.vMaxFwd <= 10.5 && spec.halfLen >= 3.3
spec.role = WTO_ROLE_HEAVY
[never_inline]
def public wto_specs_load(eid : EntityId; className : string; unit : Unit const?;
gm : IGroundModel const?; var spec : WtoVehicleSpec&)
let isLightTag = has(eid, "unit_tag__light_vehicle")
let blkLoaded = wto_parse_unit_blk(className, isLightTag, spec)
wto_apply_runtime_fallbacks(unit, gm, isLightTag, blkLoaded, spec)
spec.ready = true