#!/usr/bin/env bash
# Does the pad actually DISCHARGE a weapon? Hold each candidate input in turn
# and photograph the HUD's ammo counters.
#
# Why this exists: the autopilot's HUD kill counters read WARSHIPS 0000 /
# WARPLANES 0000 at the end of every run, in every targeting configuration, so
# "fraction of frames with the guns commanded on" was never measuring anything.
# Before tuning aim any further, settle the prior question — whether the fire
# command reaches the gun at all. The HUD carries a live ammo count (`MAIN MPM
# 00300`, matched to `LoadingCount` by the weapon RE), so the counter falling
# during a hold is direct evidence of a discharge, and the counter sitting still
# through every button is direct evidence that we have never fired a shot.
#
# `RB fires` came from an earlier session; this re-tests it rather than assuming
# it, and sweeps the other buttons so a wrong mapping cannot hide.
set -u
export HOME=/sylph-home/re SDL_AUDIODRIVER=dummy DISPLAY=:98
export PYTHONPATH=/sylph-home/.local/lib/python3.12/site-packages
SD="$(cd "$(dirname "$0")" && pwd)"
SHOTS=/sylph-home/re/shots
HOLD="${1:-4}"

shot(){ screenshot "$SHOTS/fire-$1.png" >/dev/null 2>&1; echo "  shot $1"; }

"$SD/launch_mission.sh" fly || { echo "BOOT FAILED"; exit 1; }
sleep 3
echo "=== probing (hold ${HOLD}s each) ==="
shot "00-idle"

# RB first: it is the incumbent claim. Then the rest, so a wrong mapping cannot
# hide behind it. A/B/X/Y may also switch weapon or open something — that is
# fine for a probe, and the final frame records wherever it ended up.
for b in RB LB Y X B A LS RS; do
  vgamepad press "$b"; sleep "$HOLD"; shot "hold-$b"; vgamepad release "$b"; sleep 1.5
done

# triggers are analogue, not buttons
vgamepad trig RT 1.0; sleep "$HOLD"; shot "hold-RT"; vgamepad trig RT 0.0; sleep 1.5
vgamepad trig LT 1.0; sleep "$HOLD"; shot "hold-LT"; vgamepad trig LT 0.0; sleep 1.5
vgamepad reset
shot "99-final"
echo "PROBE DONE"
