Files
windroost/p2_godot_sample/scripts/frozen_fixture.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

102 lines
3.9 KiB
GDScript

class_name FrozenFixture
extends RefCounted
## Read-only bridge to the repository's frozen P2/P1 JSON contracts.
## The sample does not copy or mutate these files.
const CONTENT_PATH := "res://../data/mvp_content_fixture.json"
const MANIFEST_PATH := "res://../data/mvp_p2_validation_manifest.json"
const SEED_PATH := "res://../data/mvp_validation_seed_pack.json"
const RULES_PATH := "res://../data/mvp_battle_rules.json"
static func _read_json(path: String) -> Dictionary:
var file := FileAccess.open(path, FileAccess.READ)
if file == null:
push_error("P2 fixture could not be opened: %s" % path)
return {}
var parsed = JSON.parse_string(file.get_as_text())
if not parsed is Dictionary:
push_error("P2 fixture is not a JSON object: %s" % path)
return {}
return parsed
static func load_all() -> Dictionary:
return {
"content": _read_json(CONTENT_PATH),
"manifest": _read_json(MANIFEST_PATH),
"seed": _read_json(SEED_PATH),
"rules": _read_json(RULES_PATH),
}
static func validate(fixtures: Dictionary) -> Dictionary:
var content: Dictionary = fixtures.get("content", {})
var manifest: Dictionary = fixtures.get("manifest", {})
var seed: Dictionary = fixtures.get("seed", {})
var rules: Dictionary = fixtures.get("rules", {})
var errors: Array[String] = []
var hero_ids: Array[String] = []
for hero in content.get("heroes", []):
hero_ids.append(str(hero.get("id", "")))
var enemy_ids: Array[String] = []
for enemy in content.get("enemies", []):
enemy_ids.append(str(enemy.get("id", "")))
var boss_ids: Array[String] = []
for boss in content.get("bosses", []):
boss_ids.append(str(boss.get("id", "")))
var fixture: Dictionary = manifest.get("content_fixture", {})
for expected in fixture.get("hero_ids", []):
if not hero_ids.has(str(expected)):
errors.append("missing hero fixture %s" % expected)
for expected in fixture.get("enemy_ids", []):
if not enemy_ids.has(str(expected)):
errors.append("missing enemy fixture %s" % expected)
for expected in fixture.get("boss_ids", []):
if not boss_ids.has(str(expected)):
errors.append("missing boss fixture %s" % expected)
var vfx_ids: Array[String] = []
for vfx in manifest.get("procedural_vfx", []):
vfx_ids.append(str(vfx.get("vfx_id", "")))
if vfx_ids.size() != 3:
errors.append("expected three procedural VFX IDs")
var asset_paths: Array[String] = []
for asset in manifest.get("assets", []):
var asset_path := str(asset.get("path", ""))
asset_paths.append(asset_path)
if not asset_path.is_empty() and not FileAccess.file_exists("res://../%s" % asset_path):
errors.append("missing manifest asset %s" % asset_path)
if seed.get("tick_seconds", -1.0) != 0.1:
errors.append("seed pack tick_seconds is not 0.1")
if seed.get("scenarios", []).size() != 3:
errors.append("seed pack must contain exactly three P3a scenarios")
# The shared battle rules must stay aligned with the seed pack and must
# carry every hero and enemy the scenarios reference.
if rules.is_empty():
errors.append("battle rules file is missing or unreadable")
else:
if float(rules.get("tick_seconds", -1.0)) != float(seed.get("tick_seconds", -2.0)):
errors.append("battle rules tick_seconds does not match the seed pack")
var rule_heroes: Dictionary = rules.get("heroes", {})
var rule_enemies: Dictionary = rules.get("enemies", {})
for scenario in seed.get("scenarios", []):
for hero_id in scenario.get("visible_hero_ids", []):
if not rule_heroes.has(str(hero_id)):
errors.append("battle rules missing hero %s" % hero_id)
for enemy_id in scenario.get("enemy_ids", []):
if not rule_enemies.has(str(enemy_id)):
errors.append("battle rules missing enemy %s" % enemy_id)
return {
"pass": errors.is_empty(),
"errors": errors,
"hero_ids": hero_ids,
"enemy_ids": enemy_ids,
"boss_ids": boss_ids,
"vfx_ids": vfx_ids,
"asset_paths": asset_paths,
"source_paths": {
"content": CONTENT_PATH,
"manifest": MANIFEST_PATH,
"seed": SEED_PATH,
"rules": RULES_PATH,
},
}