Check the live barrel trajectory against the exposed target and shorten fire delays. Resolve automatic and secondary guns using stock weapon data. Keep short halts and withdrawals coherent, follow the order while engaged, and reverse on the route with direction-aware obstacle recovery. Verified daScript loading and native AI script readiness in the guarded 2.61.0.64 lab client. Battle behavior requires manual acceptance.
188 lines
9.6 KiB
Plaintext
188 lines
9.6 KiB
Plaintext
// Trigger channel (decision layer design, section 6): instead of a speed limit per distance band,
|
|
// a shot is taken when its estimated hit probability reaches a fire threshold that moves with the
|
|
// situation. The hit probability comes from the angular size of the target's visible part against
|
|
// the expected angular error of the shot: the crew's aim, the bot's own motion (much less with a
|
|
// stabilizer), the uncertainty of the lead on a crossing target, and where the barrel points now.
|
|
// It also asks the hull channel for the highest speed at which the shot would be good enough.
|
|
options indenting = 2
|
|
options no_global_heap = true
|
|
module wto_ai_trigger_common shared
|
|
|
|
require ecs
|
|
require math
|
|
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_threat_common
|
|
|
|
// Error function, Abramowitz and Stegun 7.1.26 (absolute error below 1.5e-7).
|
|
def private wto_trig_erf(x : float) : float
|
|
let ax = abs(x)
|
|
let t = 1.0 / (1.0 + 0.3275911 * ax)
|
|
let poly = ((((1.061405429 * t - 1.453152027) * t + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t
|
|
let y = 1.0 - poly * exp(-ax * ax)
|
|
return x >= 0.0 ? y : -y
|
|
|
|
// Chance that a shot with mean error `offset` and normal spread `sigma` lands within +-half.
|
|
def private wto_trig_window(half, offset, sigma : float) : float
|
|
let s = max(1.0e-5, sigma) * 1.41421356
|
|
return clamp(0.5 * (wto_trig_erf((half - offset) / s) + wto_trig_erf((half + offset) / s)), 0.0, 1.0)
|
|
|
|
// Hit probability against a target `halfW` x `halfH` metres at `dist` (angles in radians).
|
|
def private wto_trig_prob(halfW, halfH, dist : float; offset : float2; sigmaRad : float) : float
|
|
let d = max(dist, 5.0)
|
|
return wto_trig_window(atan2(halfW, d), offset.x, sigmaRad) * wto_trig_window(atan2(halfH, d), offset.y, sigmaRad)
|
|
|
|
def public wto_trig_aim_error(actual, wanted : float3) : float2
|
|
let across = wto_xz_normalize(float3(-wanted.z, 0.0, wanted.x), float3(0.0, 0.0, 1.0))
|
|
let up = cross(across, wanted)
|
|
let ahead = dot(actual, wanted)
|
|
return float2(atan2(dot(actual, across), ahead), atan2(dot(actual, up), ahead))
|
|
|
|
// Expected angular spread of a shot (radians) at own speed and hull turn rate.
|
|
def private wto_trig_sigma(spec : WtoVehicleSpec const&; speedMps, turnDegSec, leadErrM, dist : float) : float
|
|
let stab = spec.hasVertStab ? WTO_AI_FC_STAB_ERR_FACTOR : 1.0
|
|
let moveDeg = (speedMps * WTO_AI_FC_MOVE_ERR_DEG_PER_MPS + turnDegSec * WTO_AI_FC_TURN_ERR_DEG_PER_DEG_SEC) * stab
|
|
let base = WTO_AI_FC_AIM_ERROR_DEG * WTO_DEG_TO_RAD
|
|
let move = moveDeg * WTO_DEG_TO_RAD
|
|
let lead = leadErrM / max(dist, 10.0)
|
|
return sqrt(base * base + move * move + lead * lead)
|
|
|
|
// Half width and half height (m) of the visible part of the target as seen from `myPos`.
|
|
def public wto_trig_target_half(myPos : float3; tgt : WtoUnitSnap const&; dist : float; visCount : int;
|
|
var halfW, halfH : float&)
|
|
let los = wto_xz_normalize(tgt.pos - myPos, float3(1.0, 0.0, 0.0))
|
|
let perp = float3(-los.z, 0.0, los.x)
|
|
let fwd = wto_xz_normalize(tgt.hullFwd, los)
|
|
let left = float3(-fwd.z, 0.0, fwd.x)
|
|
let sampled = dist > WTO_AI_FC_LONG_RANGE_M ? 2.0 : 4.0
|
|
let part = 0.35 + 0.65 * clamp(float(visCount) / sampled, 0.0, 1.0)
|
|
halfW = (abs(dot(fwd, perp)) * tgt.halfLen + abs(dot(left, perp)) * tgt.halfWid) * part
|
|
halfH = WTO_AI_FC_TARGET_HALF_HEIGHT_M * part
|
|
|
|
// Fire threshold (decision layer design, 6.1): lower when the target aims at the bot, is close,
|
|
// has just fired (reloading) or is about to slip out of sight; higher for big slow-loading guns
|
|
// and while pulling back to cover.
|
|
[never_inline]
|
|
def private wto_trig_threshold(now, dist, reloadSec : float; ammo : int; slot : WtoBotSlot const&) : float
|
|
assume p = slot.perceive
|
|
var thr = WTO_AI_FC_FIRE_THRESHOLD
|
|
for i in range(slot.threat.count)
|
|
if slot.threat.entries[i].eid == p.targetEid && slot.threat.entries[i].aiming
|
|
thr -= WTO_AI_FC_AIMING_BONUS
|
|
if dist < WTO_AI_FC_CLOSE_RANGE_M
|
|
thr -= WTO_AI_FC_CLOSE_BONUS * (1.0 - dist / WTO_AI_FC_CLOSE_RANGE_M)
|
|
if now - p.targetShotAt <= WTO_AI_FC_ENEMY_RELOAD_SEC
|
|
thr -= WTO_AI_FC_ENEMY_RELOAD_BONUS
|
|
if p.targetFleeting
|
|
thr -= WTO_AI_FC_FLEETING_BONUS
|
|
thr += WTO_AI_FC_RELOAD_PENALTY * clamp(reloadSec / WTO_AI_FC_LONG_RELOAD_SEC, 0.0, 1.0)
|
|
if ammo >= 0 && ammo < WTO_AI_FC_LOW_AMMO_ROUNDS
|
|
thr += WTO_AI_FC_LOW_AMMO_PENALTY * (1.0 - float(ammo) / float(WTO_AI_FC_LOW_AMMO_ROUNDS))
|
|
if slot.tactics.state == WTO_STATE_RETREAT_COVER
|
|
thr += WTO_AI_FC_RETREAT_PENALTY
|
|
return clamp(thr, WTO_AI_FC_MIN_THRESHOLD, 0.95)
|
|
|
|
// Highest own speed (driving straight, barrel on target) at which the shot still reaches the
|
|
// threshold: 99 when the current speed is fine or when even standing would not help.
|
|
[never_inline]
|
|
def private wto_trig_speed_request(spec : WtoVehicleSpec const&; halfW, halfH, dist, leadErrM, speedNow, thr : float) : float
|
|
if wto_trig_prob(halfW, halfH, dist, float2(0.0, 0.0), wto_trig_sigma(spec, speedNow, 0.0, leadErrM, dist)) >= thr
|
|
return 99.0
|
|
if wto_trig_prob(halfW, halfH, dist, float2(0.0, 0.0), wto_trig_sigma(spec, 0.0, 0.0, leadErrM, dist)) < thr
|
|
return 99.0
|
|
var lo = 0.0
|
|
var hi = speedNow
|
|
for _i in range(6)
|
|
let mid = 0.5 * (lo + hi)
|
|
if wto_trig_prob(halfW, halfH, dist, float2(0.0, 0.0), wto_trig_sigma(spec, mid, 0.0, leadErrM, dist)) >= thr
|
|
lo = mid
|
|
else
|
|
hi = mid
|
|
return lo <= WTO_AI_FC_STOP_SPEED_MAX_MPS ? 0.0 : lo
|
|
|
|
// Evaluates the main weapon's shot at the engaged target this frame and fills slot.trigger:
|
|
// hitProb (with the barrel where it points now), hitProbAligned (barrel on target), threshold,
|
|
// the autocannon aim slack and the speed request. True when the shot is good enough.
|
|
[never_inline]
|
|
def public wto_trig_evaluate(world : WtoWorldSnap const&; now : float; myPos : float3; speedNow, turnDegSec,
|
|
dist : float; offset : float2; var slot : WtoBotSlot&) : bool
|
|
assume tr = slot.trigger
|
|
assume p = slot.perceive
|
|
let tgtIdx = wto_world_find_unit(world, p.targetEid)
|
|
if tgtIdx < 0
|
|
tr.hitProb = 0.0
|
|
tr.hitProbAligned = 0.0
|
|
tr.wantSpeedMps = 99.0
|
|
return false
|
|
assume tgt = world.units[tgtIdx]
|
|
var halfW = 1.5
|
|
var halfH = 1.0
|
|
wto_trig_target_half(myPos, tgt, dist, p.visiblePointCount, halfW, halfH)
|
|
let los = wto_xz_normalize(tgt.pos - myPos, float3(1.0, 0.0, 0.0))
|
|
let latSpeed = abs(dot(p.estVel, float3(-los.z, 0.0, los.x)))
|
|
let tof = dist / max(120.0, slot.fire.activeSpeedMps)
|
|
let leadErrM = latSpeed * tof * WTO_AI_EST_LEAD_ERR_FRAC * (1.0 - 0.72 * p.velQuality)
|
|
let sigma = wto_trig_sigma(slot.spec, speedNow, turnDegSec, leadErrM, dist)
|
|
tr.threshold = wto_trig_threshold(now, dist, slot.spec.reloadSec, slot.fire.ammoRemaining, slot)
|
|
tr.hitProb = wto_trig_prob(halfW, halfH, dist, offset, sigma)
|
|
tr.hitProbAligned = wto_trig_prob(halfW, halfH, dist, float2(0.0, 0.0), sigma)
|
|
tr.aimSlackDeg = max(WTO_AI_FC_BASE_ALIGN_DEG, WTO_AI_FC_AC_AIM_SLACK * atan2(0.5 * (halfW + halfH), max(dist, 5.0)) * WTO_RAD_TO_DEG)
|
|
if slot.fire.gunReady && !slot.fire.reloading && !slot.spec.isAutocannon
|
|
tr.wantSpeedMps = wto_trig_speed_request(slot.spec, halfW, halfH, dist, leadErrM, speedNow, tr.threshold)
|
|
tr.wantSpeedAt = now
|
|
else
|
|
tr.wantSpeedMps = 99.0
|
|
tr.wantSpeedAt = -100.0
|
|
return tr.hitProb >= tr.threshold
|
|
|
|
// Autocannon and coaxial bursts (decision layer design, 6.3): fire for burstSec, pause for
|
|
// burstPauseSec. Within a burst the weapon fires whenever it is roughly on target and stops while
|
|
// it is not; a new burst starts only when it is. `started` is set when this call opened a burst.
|
|
def public wto_trig_burst_gate(now : float; aimOk : bool; var burstUntil, pauseUntil : float&; var started : bool&) : int
|
|
started = false
|
|
if now < burstUntil
|
|
return aimOk ? WTO_DENY_OK : WTO_DENY_AIM_ERR
|
|
if now < pauseUntil
|
|
return WTO_DENY_BURST_PAUSE
|
|
if !aimOk
|
|
return WTO_DENY_AIM_ERR
|
|
burstUntil = now + WTO_AI_FC_BURST_SEC
|
|
pauseUntil = burstUntil + WTO_AI_FC_BURST_PAUSE_SEC
|
|
started = true
|
|
return WTO_DENY_OK
|
|
|
|
// Track burst schedules and speed requests by target. Leaving it starts a fresh opportunity.
|
|
def public wto_trig_set_target(eid : EntityId; var tr : WtoTriggerState&)
|
|
if eid == tr.targetEid
|
|
return
|
|
tr.targetEid = eid
|
|
tr.biasEpoch++
|
|
tr.wantSpeedMps = 99.0
|
|
tr.wantSpeedAt = -100.0
|
|
tr.hitProb = 0.0
|
|
tr.hitProbAligned = 0.0
|
|
tr.burstUntil = -100.0
|
|
tr.burstPauseUntil = -100.0
|
|
tr.secondary = WtoWeaponBurst()
|
|
tr.coaxial = WtoWeaponBurst()
|
|
|
|
[never_inline]
|
|
def public wto_trig_aux_aim_ok(now, dist, ownSpeed : float; aimErr : float2; myPos : float3;
|
|
target : WtoUnitSnap const&; mount : WtoMountFire const&;
|
|
slot : WtoBotSlot const&) : bool
|
|
var halfW = 0.0
|
|
var halfH = 0.0
|
|
wto_trig_target_half(myPos, target, dist, slot.perceive.visiblePointCount, halfW, halfH)
|
|
if mount.automatic
|
|
let slack = atan2(0.5 * (halfW + halfH), max(dist, 5.0)) * WTO_AI_FC_AC_AIM_SLACK
|
|
return length(aimErr) <= max(WTO_AI_FC_BASE_ALIGN_DEG * WTO_DEG_TO_RAD, slack)
|
|
let los = wto_xz_normalize(target.pos - myPos, float3(1.0, 0.0, 0.0))
|
|
let lateral = abs(dot(slot.perceive.estVel, float3(-los.z, 0.0, los.x)))
|
|
let leadErr = lateral * (dist / mount.shell.speedMps) * WTO_AI_EST_LEAD_ERR_FRAC * (1.0 - 0.72 * slot.perceive.velQuality)
|
|
let turn = abs(slot.drive.omegaYawRad) * WTO_RAD_TO_DEG
|
|
let sigma = wto_trig_sigma(slot.spec, ownSpeed, turn, leadErr, dist)
|
|
return wto_trig_prob(halfW, halfH, dist, aimErr, sigma) >= wto_trig_threshold(now, dist, 0.0, -1, slot)
|