Files
windroost/p2_godot_sample/scripts/batch_runner.gd
T
wushenghua e7b55e1c17 feat: align runtime with paper numbers and build the battle HUD carrier
Numbers. The paper runner (tools/mvp_validation_sim.py) and the Godot
runtime adapter were two independent rewrites of the same rules and had
drifted: seed derivation, damage order, the weakness rule and the shield
rebuild semantics all differed, so the runtime never actually reproduced
the paper numbers and P2-BAT-01 could never pass.

- add data/mvp_battle_rules.json as the single source of balance data and
  of the per-run seed derivation, read by both engines
- derive the efficiency factor from SHA-256 (48-bit prefix) so Python and
  GDScript land on the same float64
- port the paper semantics into battle_sim.gd and expose the simulation as
  a stepwise state machine so a live scene can drive it at 1x/2x/3x
- add tools/mvp_parity_gate.py: diffs 300 results field by field, event
  log by event log, and exits non-zero on any difference
- freeze data/mvp_validation_seed_reference.json so unapproved numeric
  drift fails the gate
- note that GDScript's roundf() is half-away-from-zero while Python's
  round() is banker's rounding; the runtime uses String.num instead

UI. The sample had no real UI carrier, no localization and no touch path,
so the NinePatch, dynamic text, safe area and input cases had nothing to
be proven against.

- add the battle HUD, laid out from the contract table rather than container
  stretch ratios, reproducing the 390x844 rectangles exactly and adapting
  360x800 / 430x932 with the stage kept as priority space
- add runtime text tables for zh_CN and en_US and a key validator that
  proves both locales carry the same keys and every content id has a string
- add NinePatch frames with a 6px border band and 6px cut corners
- add the live battle scene, the capture runner and the 1x/2x/3x
  equivalence test

tools/run_p2_checks.sh runs the nine gates in one command; all pass.
2026-11-16 22:22:55 +08:00

134 lines
4.3 KiB
GDScript

extends SceneTree
## Batch runner for the P2/P3a numeric contract.
##
## Runs every (scenario, seed) pair declared by the seed pack and writes the
## same document shape as `tools/mvp_validation_sim.py`. `tools/mvp_parity_gate.py`
## diffs the two documents so the runtime can never silently drift away from the
## agreed paper numbers.
const OUTPUT_NAME := "batch_sim_results.json"
func _init() -> void:
var fixtures := FrozenFixture.load_all()
var fixture_check := FrozenFixture.validate(fixtures)
if not fixture_check["pass"]:
printerr("P2_BATCH_FIXTURE_CHECK_FAIL %s" % JSON.stringify(fixture_check))
quit(2)
return
var seed_pack: Dictionary = fixtures["seed"]
var rules: Dictionary = fixtures["rules"]
var seed_range: Dictionary = seed_pack.get("seed_range", {})
var seed_start := int(seed_range.get("start", 10001))
var seed_count := int(seed_range.get("count", 100))
var seed_step := int(seed_range.get("step", 1))
var results: Array = []
var summaries: Array = []
for scenario in seed_pack.get("scenarios", []):
var timeout := float(scenario.get("timeout_seconds", 60.0))
var scenario_results: Array = []
for offset in range(seed_count):
var seed_value := seed_start + offset * seed_step
scenario_results.append(P2BattleSim.run_scenario(rules, scenario, seed_value, timeout))
results.append_array(scenario_results)
summaries.append(_summarize(scenario, scenario_results))
var document := {
"schema": 1,
"source_pack": "data/mvp_validation_seed_pack.json",
"rules_source": "data/mvp_battle_rules.json",
"tick_seconds": float(rules.get("tick_seconds", 0.1)),
"determinism_rule": str(seed_pack.get("result_schema", {}).get("determinism_rule", "")),
"summaries": summaries,
"results": results,
}
var artifact_dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(artifact_dir)
var output_path := artifact_dir.path_join(OUTPUT_NAME)
var output := FileAccess.open(output_path, FileAccess.WRITE)
if output == null:
printerr("P2_BATCH_OUTPUT_OPEN_FAIL %s" % output_path)
quit(3)
return
output.store_string(JSON.stringify(document, "\t", true))
output.close()
print(
"P2_BATCH_SIM %s"
% JSON.stringify(
{
"results": results.size(),
"scenarios": summaries.size(),
"output": output_path,
}
)
)
for summary in summaries:
print(
"P2_BATCH_SUMMARY %s win_rate=%s duration=%s duration_pass=%s win_rate_pass=%s"
% [
str(summary.get("scenario_id", "?")),
str(summary.get("win_rate", "?")),
str(summary.get("average_duration_seconds", "?")),
str(summary.get("duration_target_pass", "?")),
str(summary.get("win_rate_target_pass", "?")),
]
)
quit(0)
func _summarize(scenario: Dictionary, results: Array) -> Dictionary:
var wins := 0
var durations: Array = []
for result in results:
if result["result"] == "win":
wins += 1
durations.append(float(result["duration_seconds"]))
var win_rate := 0.0
if results.size() > 0:
win_rate = float(wins) / float(results.size())
var total := 0.0
var min_duration := INF
var max_duration := -INF
for duration in durations:
total += duration
if duration < min_duration:
min_duration = duration
if duration > max_duration:
max_duration = duration
var average := 0.0
if durations.size() > 0:
average = total / float(durations.size())
var target_duration: Array = scenario.get("target_duration_seconds", [0.0, INF])
var target_win_rate: Array = scenario.get("target_win_rate", [0.0, 1.0])
var duration_pass := false
if durations.size() > 0:
duration_pass = (
min_duration >= float(target_duration[0]) and max_duration <= float(target_duration[1])
)
var win_rate_pass := (
float(target_win_rate[0]) <= win_rate and win_rate <= float(target_win_rate[1])
)
return {
"scenario_id": str(scenario["id"]),
"mode": str(scenario["mode"]),
"runs": results.size(),
"wins": wins,
"losses": results.size() - wins,
"win_rate": _round_metric(win_rate),
"average_duration_seconds": _round_metric(average),
"min_duration_seconds": min_duration,
"max_duration_seconds": max_duration,
"target_duration_seconds": target_duration,
"target_win_rate": target_win_rate,
"duration_target_pass": duration_pass,
"win_rate_target_pass": win_rate_pass,
}
func _round_metric(value: float) -> float:
return float(String.num(value, 3))