#!/usr/bin/env bash
# Plan 47's timing check: the timing audit's probes (game/test/sim/timing_probe.gd)
# with OVERCLOCKED's fixed pass (e2), open play's stock mix drawn on its own
# (stock) and GAME TIMING UNLOCKED's own steps at 30 and 60 HZ (u30, u60):
# UNLOCKED's odds should be the stock mix's (within the samples' noise) at
# either TICKRATE, and e2's apart. One line per probe and schedule, in
# parallel (half the cores); logs in out/timing-check/.
#   tools/bin/timing-check [N=150] [MODE ...]
#     MODE: goalie thin demon still inside standard bomb faceoff (default:
#     goalie thin demon still)
source "$(dirname "${BASH_SOURCE[0]}")/_common.sh"
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
n=150
if [[ $# -gt 0 && "$1" =~ ^[0-9]+$ ]]; then n="$1"; shift; fi
modes=("$@")
[[ ${#modes[@]} -eq 0 ]] && modes=(goalie thin demon still)
out="$MW_ROOT/out/timing-check"
mkdir -p "$out"
par=$(( $(nproc) / 2 )); (( par >= 1 )) || par=1
jobs=()
for m in "${modes[@]}"; do
  for s in e2 stock u30 u60; do jobs+=("$m $s"); done
done
printf '%s\n' "${jobs[@]}" | xargs -P "$par" -L 1 bash -c \
  '"$0" -s res://test/sim/timing_probe.gd -- "$1" "$2" '"$n"' > "'"$out"'/$1_$2.log" 2>&1' "$here/godot-headless"
for j in "${jobs[@]}"; do
  read -r m s <<< "$j"
  grep -E "^($m|still)" "$out/${m}_$s.log" || echo "$m $s: no result (see $out/${m}_$s.log)"
done
