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.
219 lines
10 KiB
Plaintext
219 lines
10 KiB
Plaintext
// Single fire-permission gate for every ground weapon (main cannon, launcher, SPAA self-defense).
|
|
// Every shot must pass visual confirmation, weapon readiness, distance-tier speed limits,
|
|
// 800 m continuous LOS window, aim settle, frame pre-fire budget, muzzle hard-ray, drag-integrated
|
|
// ballistic trajectory sweep, predicted friendly corridor, and multi-barrel group validation.
|
|
options indenting = 2
|
|
options no_global_heap = true
|
|
module wto_ai_fire_gate_common shared
|
|
|
|
require ecs
|
|
require math
|
|
require DagorMath
|
|
require Mission
|
|
require Unit
|
|
require Weapon
|
|
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_fire_speed_common
|
|
require %appGame.wtoai.wto_ai_types_common
|
|
require %appGame.wtoai.wto_ai_sight_common
|
|
|
|
[never_inline]
|
|
def private wto_gate_friendly_segment_clear(world : WtoWorldSnap const&; selfEid : EntityId;
|
|
army : int; p0, p1 : float3; tMid : float) : bool
|
|
let delta = p1 - p0
|
|
let segLen = length(delta)
|
|
if segLen < 0.2
|
|
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 predPos = u.pos + u.vel * tMid + float3(0.0, 1.3, 0.0)
|
|
let along = clamp(dot(predPos - p0, dir), 0.0, segLen)
|
|
let clearR = WTO_AI_FC_FRIENDLY_CORRIDOR_M + u.halfWid
|
|
if length_sq(predPos - (p0 + dir * along)) < clearR * clearR
|
|
return false
|
|
return true
|
|
|
|
[never_inline]
|
|
def private wto_gate_check_ballistic_arc(world : WtoWorldSnap const&;
|
|
selfEid, targetEid : EntityId; army : int;
|
|
muzzlePos, liveDir, carrierVel, aimPt : float3;
|
|
speedMps, dragCx, massKg, caliberM : float;
|
|
var outBlock : int&) : int
|
|
let toAim = aimPt - muzzlePos
|
|
let targetDist = length(toAim)
|
|
if targetDist < 1.5
|
|
return WTO_DENY_OK
|
|
let toAimDir = toAim / targetDist
|
|
let area = 0.25 * WTO_PI * caliberM * caliberM
|
|
let dragK = 0.5 * 1.225 * dragCx * area / max(0.1, massKg)
|
|
var cur = muzzlePos
|
|
var vel = liveDir * max(80.0, speedMps) + carrierVel
|
|
var t = 0.0
|
|
var traveled = 0.0
|
|
for step in range(64)
|
|
let spd = length(vel)
|
|
if spd < 20.0
|
|
return WTO_DENY_AIM_ERR
|
|
let remain = max(0.5, targetDist - traveled)
|
|
let segWant = min(WTO_AI_FC_BALLISTIC_STEP_M, remain)
|
|
let dt = clamp(segWant / spd, 0.004, 0.14)
|
|
let accel = float3(0.0, -9.81, 0.0) - vel * (dragK * spd)
|
|
var next = cur + vel * dt + accel * (0.5 * dt * dt)
|
|
let nextProj = dot(next - muzzlePos, toAimDir)
|
|
let isLast = nextProj >= targetDist - 0.5
|
|
if isLast && nextProj > targetDist && length_sq(next - cur) > 0.01
|
|
let curProj = dot(cur - muzzlePos, toAimDir)
|
|
let frac = clamp((targetDist - curProj) / max(0.01, nextProj - curProj), 0.0, 1.0)
|
|
next = lerp(cur, next, frac)
|
|
let trimS = step == 0 ? 0.35 : 0.0
|
|
let trimE = isLast ? 0.65 : 0.0
|
|
let blk = wto_sight_hard_segment_blocked(world, selfEid, targetEid, cur, next, WTO_AI_FC_BALLISTIC_SWEEP_R_M, trimS, trimE)
|
|
if blk != WTO_BLOCK_NONE
|
|
outBlock = blk
|
|
return WTO_DENY_ARC_HARD
|
|
if !wto_gate_friendly_segment_clear(world, selfEid, army, cur, next, t + dt * 0.5)
|
|
return WTO_DENY_FRIENDLY
|
|
traveled += length(next - cur)
|
|
cur = next
|
|
vel = vel + accel * dt
|
|
t += dt
|
|
if isLast
|
|
let missTol = max(3.2, targetDist * 0.012)
|
|
if length_sq(cur - aimPt) > missTol * missTol
|
|
return WTO_DENY_AIM_ERR
|
|
return WTO_DENY_OK
|
|
return WTO_DENY_AIM_ERR
|
|
|
|
[never_inline]
|
|
def private wto_gate_check_barrel(world : WtoWorldSnap const&; unit : Unit const?;
|
|
slot : WtoBotSlot const&; muzzlePos, liveDir, aimPt : float3;
|
|
var outBlock : int&) : int
|
|
let straightBlk = wto_sight_hard_segment_blocked(world, slot.eid, slot.perceive.targetEid, muzzlePos, aimPt, 0.05, 0.35, 0.65)
|
|
if straightBlk != WTO_BLOCK_NONE
|
|
outBlock = straightBlk
|
|
return WTO_DENY_MUZZLE_HARD
|
|
return wto_gate_check_ballistic_arc(world, slot.eid, slot.perceive.targetEid, slot.army,
|
|
muzzlePos, liveDir, unit.vel, aimPt,
|
|
slot.fire.activeSpeedMps, slot.fire.activeDragCx, slot.fire.activeMassKg, slot.fire.activeCaliberM, outBlock)
|
|
|
|
[never_inline]
|
|
def private wto_gate_check_all_primary_guns(world : WtoWorldSnap const&; unit : Unit const?;
|
|
slot : WtoBotSlot const&; primaryMuzzle, primaryDir, aimPt : float3;
|
|
var outBlock : int&) : int
|
|
let primaryRes = wto_gate_check_barrel(world, unit, slot, primaryMuzzle, primaryDir, aimPt, outBlock)
|
|
if primaryRes != WTO_DENY_OK
|
|
return primaryRes
|
|
if unit == null || unit.weap == null
|
|
return WTO_DENY_OK
|
|
for tIdx, turret in iter_range(unit.weap.turret), unit.weap.turret
|
|
if tIdx == slot.fire.turretNo || 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 || weapon.isDummyGun
|
|
continue
|
|
var gunTm : float3x4
|
|
weapon_calcShootTm(weapon, unit.unitTm, gunTm)
|
|
if length_sq(gunTm[0]) <= 0.25
|
|
continue
|
|
let secRes = wto_gate_check_barrel(world, unit, slot, gunTm[3], normalize(gunTm[0]), aimPt, outBlock)
|
|
if secRes != WTO_DENY_OK
|
|
return secRes == WTO_DENY_FRIENDLY ? WTO_DENY_FRIENDLY : WTO_DENY_MULTI_GUN
|
|
return WTO_DENY_OK
|
|
|
|
[never_inline]
|
|
def private wto_gate_track_aim(now : float; offset : float3; target : EntityId;
|
|
aligned, close : bool; var fc : WtoFireState&)
|
|
let age = now - fc.prevAimAt
|
|
let sameTrack = target == fc.aimTargetEid && age >= 0.0 && age <= WTO_AI_TACTICS_TICK_SEC
|
|
if !sameTrack || age > 0.0
|
|
// Compare offsets so steady tracking can pass while the turret is turning.
|
|
fc.aimDriftDegSec = sameTrack && age > 0.0 ? length(offset - fc.prevAimOffset) * WTO_RAD_TO_DEG / age : 0.0
|
|
fc.prevAimOffset = offset
|
|
fc.prevAimAt = now
|
|
fc.aimTargetEid = target
|
|
let settled = sameTrack && aligned && (close || fc.aimDriftDegSec <= WTO_AI_FC_BARREL_SETTLE_DEG_SEC)
|
|
fc.lastAligned = settled
|
|
if !sameTrack
|
|
fc.settleHoldSec = 0.0
|
|
elif settled
|
|
fc.settleHoldSec += age
|
|
else
|
|
fc.settleHoldSec = max(0.0, fc.settleHoldSec - age)
|
|
|
|
[never_inline]
|
|
def private wto_gate_log_deny(now, distXZ, measuredSpeed, speedLimit : float; reason, blk : int; var slot : WtoBotSlot&)
|
|
assume fc = slot.fire
|
|
let changed = fc.lastDenyReason != reason || fc.lastDenyBlock != blk
|
|
fc.lastDenyReason = reason
|
|
fc.lastDenyBlock = blk
|
|
if WTO_AI_DIAG_TELEMETRY && reason >= WTO_DENY_SPEED && (changed || (now - fc.lastDenyLogAt) >= 4.0)
|
|
fc.lastDenyLogAt = now
|
|
print("WTO_AI|DENY|eid={uint(slot.eid)}|reason={reason}|block={blk}|dist={distXZ}|spd={measuredSpeed}|limit={speedLimit}|err={fc.lastAimErrDeg}|drift={fc.aimDriftDegSec}\n")
|
|
|
|
[never_inline]
|
|
def private wto_gate_request_primary_shot(var unit : Unit?; now, distXZ, measuredSpeed, speedLimit : float;
|
|
var slot : WtoBotSlot&)
|
|
assume fc = slot.fire
|
|
unit_single_shot(unit, int(WeaponTriggerGroups.TRIGGER_GROUP_PRIMARY))
|
|
fc.lastShotAt = now
|
|
fc.pendingShotConfirm = true
|
|
fc.pendingShotAt = now
|
|
fc.lastDenyReason = WTO_DENY_OK
|
|
fc.lastDenyBlock = WTO_BLOCK_NONE
|
|
fc.shotErrSumDeg += fc.lastAimErrDeg
|
|
fc.maxShotErrDeg = max(fc.maxShotErrDeg, fc.lastAimErrDeg)
|
|
if !slot.spec.isAutocannonBelt
|
|
fc.settleHoldSec = 0.0
|
|
if WTO_AI_DIAG_TELEMETRY
|
|
print("WTO_AI|SHOT|eid={uint(slot.eid)}|tgt={uint(slot.perceive.targetEid)}|dist={distXZ}|spd={measuredSpeed}|limit={speedLimit}|v0={fc.activeSpeedMps}|err={fc.lastAimErrDeg}\n")
|
|
|
|
[never_inline]
|
|
def public wto_fire_gate_try_shoot(world : WtoWorldSnap const&; var unit : Unit?;
|
|
now, distXZ : float; muzzlePos, aimPt, liveDir, wishDir : float3;
|
|
var slot : WtoBotSlot&; var fireBudget : int&)
|
|
assume fc = slot.fire
|
|
let aimErrDeg = acos(clamp(dot(liveDir, wishDir), -1.0, 1.0)) * WTO_RAD_TO_DEG
|
|
fc.lastAimErrDeg = aimErrDeg
|
|
let measuredSpeed = length(unit.vel)
|
|
let halting = slot.drive.routeBOwned && slot.tactics.state == WTO_STATE_SHORT_HALT
|
|
let speedLimit = halting ? WTO_AI_FC_STOP_SPEED_MAX_MPS : wto_fire_speed_limit_mps(distXZ, slot.spec.vMaxFwd)
|
|
let speedOk = measuredSpeed <= speedLimit
|
|
let close = distXZ <= WTO_AI_FC_CLOSE_RANGE_M
|
|
let allowErrDeg = clamp(atan2(WTO_AI_FC_CLOSE_HIT_RADIUS_M, max(distXZ, 6.0)) * WTO_RAD_TO_DEG, WTO_AI_FC_BASE_ALIGN_DEG, 2.4)
|
|
let withinTraverse = fc.hullAimTarget == INVALID_ENTITY_ID
|
|
wto_gate_track_aim(now, liveDir - wishDir, slot.perceive.targetEid, withinTraverse && aimErrDeg <= allowErrDeg && speedOk, close, fc)
|
|
|
|
let minCadence = slot.spec.isAutocannonBelt ? 0.12 : 0.65
|
|
let reqHold = close ? WTO_AI_FC_CLOSE_SETTLE_HOLD_SEC : WTO_AI_FC_SETTLE_HOLD_SEC
|
|
var deny = WTO_DENY_OK
|
|
if !withinTraverse
|
|
deny = WTO_DENY_TRAVERSE
|
|
elif !fc.gunReady || (now - fc.lastShotAt) < minCadence
|
|
deny = WTO_DENY_NOT_READY
|
|
elif !speedOk
|
|
deny = WTO_DENY_SPEED
|
|
elif distXZ > WTO_AI_FC_LONG_RANGE_M && (slot.perceive.goodLosSec < WTO_AI_FC_LONG_WINDOW_SEC || slot.perceive.visiblePointCount < 2)
|
|
deny = WTO_DENY_LONG_WINDOW
|
|
elif aimErrDeg > allowErrDeg
|
|
deny = WTO_DENY_AIM_ERR
|
|
elif !fc.lastAligned || fc.settleHoldSec < reqHold
|
|
deny = WTO_DENY_SETTLE
|
|
elif fireBudget <= 0
|
|
deny = WTO_DENY_BUDGET
|
|
if deny != WTO_DENY_OK
|
|
wto_gate_log_deny(now, distXZ, measuredSpeed, speedLimit, deny, WTO_BLOCK_NONE, slot)
|
|
return
|
|
fireBudget--
|
|
var blk = WTO_BLOCK_NONE
|
|
let gateRes = wto_gate_check_all_primary_guns(world, unit, slot, muzzlePos, liveDir, aimPt, blk)
|
|
if gateRes != WTO_DENY_OK
|
|
wto_gate_log_deny(now, distXZ, measuredSpeed, speedLimit, gateRes, blk, slot)
|
|
return
|
|
wto_gate_request_primary_shot(unit, now, distXZ, measuredSpeed, speedLimit, slot)
|