Estimate hit probability and request movement speed from the weapon solution. Use visible aim samples, stock shell penetration, and separate burst paths.
152 lines
7.7 KiB
Plaintext
152 lines
7.7 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, offsetRad, sigmaRad : float) : float
|
|
let o = offsetRad * 0.7071
|
|
let d = max(dist, 5.0)
|
|
return wto_trig_window(atan2(halfW, d), o, sigmaRad) * wto_trig_window(atan2(halfH, d), o, sigmaRad)
|
|
|
|
// 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 private 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 : float; fleeting : bool; 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 fleeting
|
|
thr -= WTO_AI_FC_FLEETING_BONUS
|
|
thr += WTO_AI_FC_BIG_GUN_PENALTY * clamp((slot.fire.activeCaliberM - 0.075) / 0.05, 0.0, 1.0)
|
|
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, 0.0, wto_trig_sigma(spec, speedNow, 0.0, leadErrM, dist)) >= thr
|
|
return 99.0
|
|
if wto_trig_prob(halfW, halfH, dist, 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, 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, aimErrDeg : float; 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)
|
|
let fleeting = latSpeed >= WTO_AI_FC_FLEETING_LAT_MPS && p.visiblePointCount < 3
|
|
tr.threshold = wto_trig_threshold(now, dist, fleeting, slot)
|
|
tr.hitProb = wto_trig_prob(halfW, halfH, dist, aimErrDeg * WTO_DEG_TO_RAD, sigma)
|
|
tr.hitProbAligned = wto_trig_prob(halfW, halfH, dist, 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)
|
|
tr.wantSpeedMps = wto_trig_speed_request(slot.spec, halfW, halfH, dist, leadErrM, speedNow, tr.threshold)
|
|
tr.wantSpeedAt = now
|
|
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
|
|
|
|
// No engaged target: nothing is requested from the hull.
|
|
def public wto_trig_clear(var tr : WtoTriggerState&)
|
|
tr.hitProb = 0.0
|
|
tr.hitProbAligned = 0.0
|
|
tr.wantSpeedMps = 99.0
|