Files
windroost/p2_godot_sample/scripts/capture_runner.gd
T
wushenghua f1f2c31a94 feat: side systems runtime — partners/records/black market (L5 runtime)
Continued FULL_GAME_GOAL. The three side systems had authored content but no
runtime, so none of their numbers were actually used. Added a Python-authoritative
engine and a GDScript mirror (parity gate over 3 profile tiers) plus a real UI.

- 5 partners: tiered low-pressure tasks derived from play counters (distance,
  relics, records, wins, new skies), no combat and no failure penalty, claim
  once per tier for gold/crystal.
- 167 records: progress from play counters, stage rewards every third record,
  one-time claims, no miss penalty or daily streak; list shows claimable
  milestones and X/167 progress.
- 6 black-market offers: crystal-only purchases (relic/emblem/material/cosmetic/
  time/recovery), no pay-to-win; insufficient balance is a clean rejection.
- Skyport 旅团事务 page with three tabs; claims/purchases route through the
  real engines and persist. Added battle_wins/total_gold_earned/partner_claims/
  revive_charges/cosmetics profile fields with normalization, and victory now
  increments the win/earned counters in both slice and world flows.
- side_systems_ui_test drives tabs/claims/purchases/locale through real hooks;
  registered alongside parity in the full gate. Added p3a_side capture and
  ignored regenerated artifact files.

All gates pass incl. 25 UI captures; P2_CHECKS_PASS. prototype/ untracked.
2026-11-18 16:36:17 +08:00

265 lines
12 KiB
GDScript

extends SceneTree
## Capture runner for the battle HUD.
##
## Produces normal-render frames at every required viewport size and locale,
## and records the live HUD region rectangles so the layout can be checked
## against `BATTLE_HUD_HIGH_FIDELITY_SPEC.md` without eyeballing.
##
## This runner needs a real display: headless Godot has no renderer.
const SCENES_DIR := "res://scenes/"
const OUTPUT_DIR := "res://artifacts/ui_captures/"
const BATTLE_SCENE := "res://scripts/battle_scene.gd"
const SHOTS := [
{"name": "battle_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N03-SEMI", "ticks": 50, "state": "battle"},
{"name": "battle_390_en_US", "size": Vector2i(390, 844), "locale": "en_US", "scenario": "P3A-N03-SEMI", "ticks": 50, "state": "battle"},
{"name": "battle_360_zh_CN", "size": Vector2i(360, 800), "locale": "zh_CN", "scenario": "P3A-N03-SEMI", "ticks": 50, "state": "battle"},
{"name": "battle_430_zh_CN", "size": Vector2i(430, 932), "locale": "zh_CN", "scenario": "P3A-N03-SEMI", "ticks": 50, "state": "battle"},
{"name": "battle_360_en_US", "size": Vector2i(360, 800), "locale": "en_US", "scenario": "P3A-N03-SEMI", "ticks": 50, "state": "battle"},
{"name": "pause_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N03-SEMI", "ticks": 50, "state": "pause"},
{"name": "victory_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 160, "state": "battle"},
{"name": "victory_390_en_US", "size": Vector2i(390, 844), "locale": "en_US", "scenario": "P3A-N01-AUTO", "ticks": 160, "state": "battle"},
{"name": "vfx_shield_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N10-MANUAL", "ticks": 90, "state": "battle", "vfx": "shield_break", "vfx_target": "B-R1-01#1"},
{"name": "vfx_ultimate_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N10-MANUAL", "ticks": 140, "state": "battle", "vfx": "ultimate_burst", "vfx_target": "B-R1-01#1"},
{"name": "p3a_skyport_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true},
{"name": "p3a_map_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "page": "map"},
{"name": "p3a_world_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "page": "world"},
{"name": "p3a_side_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "page": "side"},
{"name": "p3a_event_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "event": "R1-C01-N04"},
{"name": "p3a_victory_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "victory": "R1-C01-N02"},
{"name": "p3a_victory_360_zh_CN", "size": Vector2i(360, 800), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "victory": "R1-C01-N02"},
{"name": "p3a_victory_430_zh_CN", "size": Vector2i(430, 932), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "victory": "R1-C01-N02"},
{"name": "p3a_h02_attack_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 20, "state": "battle", "shell": true, "attack_hero": "H02", "attack_frame": 2},
{"name": "p3a_h03_attack_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 20, "state": "battle", "shell": true, "attack_hero": "H03", "attack_frame": 2},
{"name": "p3a_h04_attack_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 20, "state": "battle", "shell": true, "attack_hero": "H04", "attack_frame": 2, "unlock_all": true},
{"name": "p3a_settings_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "page": "settings"},
{"name": "p3a_settings_390_en_US", "size": Vector2i(390, 844), "locale": "en_US", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "page": "settings"},
{"name": "p3a_growth_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N01-AUTO", "ticks": 0, "state": "battle", "shell": true, "page": "growth"},
{"name": "busy_five_enemies_390_zh_CN", "size": Vector2i(390, 844), "locale": "zh_CN", "scenario": "P3A-N08", "ticks": 40, "state": "battle", "busy": true},
]
func _init() -> void:
_run.call_deferred()
func _run() -> void:
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(OUTPUT_DIR))
var manifest: Array = []
var failures: Array = []
for shot in SHOTS:
var entry := await _capture_shot(shot)
manifest.append(entry)
if not bool(entry["layout_ok"]):
failures.append("%s: %s" % [shot["name"], "; ".join(entry["layout_problems"])])
if str(entry["status"]) != "ok":
failures.append("%s: %s" % [shot["name"], entry["status"]])
var manifest_path := OUTPUT_DIR.path_join("ui_captures_manifest.json")
var file := FileAccess.open(manifest_path, FileAccess.WRITE)
if file != null:
file.store_string(JSON.stringify({"schema": 1, "captures": manifest}, "\t", true))
file.close()
for entry in manifest:
print(
"UI_CAPTURE %s size=%s viewport=%s locale=%s layout_ok=%s file=%s"
% [
entry["name"],
str(entry["size"]),
str(entry["viewport_size"]),
entry["locale"],
str(entry["layout_ok"]),
entry["file"],
]
)
for problem in entry["layout_problems"]:
print(" UI_LAYOUT_PROBLEM %s" % problem)
if failures.is_empty():
print("UI_CAPTURE_PASS %d captures" % manifest.size())
quit(0)
else:
print("UI_CAPTURE_FAIL %s" % "; ".join(failures))
quit(1)
func _capture_shot(shot: Dictionary) -> Dictionary:
var size: Vector2i = shot["size"]
var root := get_root()
# The window must exist before its size can be changed, otherwise the OS
# keeps the default size and the capture silently records the wrong size.
await _wait_frames(2)
root.content_scale_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS
root.content_scale_size = size
root.size = size
await _wait_frames(3)
var real_size := root.get_visible_rect().size
if shot.get("shell", false):
var shell := load("res://scenes/game_root.tscn").instantiate() as GameRoot
root.add_child(shell)
if str(shot.get("page", "skyport")) == "map":
shell.show_page("map")
if str(shot.get("page", "")) == "world":
shell.set_locale(str(shot["locale"]))
shell.show_page("world")
shell.select_world_region_for_test("R3")
shell.select_world_chapter_for_test("R3-C05")
if str(shot.get("page", "")) == "side":
shell.set_locale(str(shot["locale"]))
var sp := shell.get_profile()
sp["highest_node"] = 120
sp["battle_wins"] = 30
sp["crystal"] = 200
ProfileStore.save_profile(sp)
shell.show_page("side")
await _wait_frames(3)
ProfileStore.save_profile(ProfileStore.fresh_profile())
if str(shot.get("page", "")) == "settings":
shell.set_locale(str(shot["locale"]))
shell.show_page("settings")
if str(shot.get("page", "")) == "growth":
shell.set_locale(str(shot["locale"]))
var gp := shell.get_profile()
gp["gold"] = 5000
gp["star_fragments"] = 200
ProfileStore.save_profile(gp)
shell.show_page("growth")
# restore clean test profile after capturing so later shots are unaffected
await _wait_frames(6)
ProfileStore.save_profile(ProfileStore.fresh_profile())
if shot.has("event"):
shell.start_node(str(shot["event"]))
if shot.has("victory"):
shell.reset_profile_for_test()
# H01-H03 unlocked by a fresh profile; N02 unlocks H04, so its
# "new ally" chip is visible on the real player victory screen.
shell.force_victory_for_test(str(shot["victory"]))
await _wait_frames(50) # let the 0.6s coin roll settle before capture
elif shot.has("attack_hero"):
shell.reset_profile_for_test()
if shot.get("unlock_all", false):
shell.unlock_all_for_test()
shell.start_node("R1-C01-N02")
await _wait_frames(6)
var hud := shell.get_battle().hud
if hud != null:
hud.force_hero_attack_frame_for_test(
str(shot["attack_hero"]), int(shot.get("attack_frame", 2))
)
await _wait_frames(3)
else:
await _wait_frames(6)
var shell_image := root.get_texture().get_image()
var shell_path := OUTPUT_DIR.path_join("%s.png" % shot["name"])
var shell_saved := shell_image.save_png(ProjectSettings.globalize_path(shell_path))
root.remove_child(shell)
shell.queue_free()
return {
"name": str(shot["name"]),
"size": [size.x, size.y],
"viewport_size": [real_size.x, real_size.y],
"locale": str(shot["locale"]),
"scenario": "game_shell",
"ticks": 0,
"state": "victory" if shot.has("victory") else ("attack" if shot.has("attack_hero") else str(shot.get("page", "skyport"))),
"file": shell_path,
"status": "ok" if shell_saved == OK else "save_failed:%d" % shell_saved,
"layout_ok": true,
"layout_problems": [],
"region_rects": {},
}
var scene_script := load(BATTLE_SCENE)
var scene: Node = scene_script.new()
root.add_child(scene)
scene.set_process(false)
var battle = scene as BattleScene
battle.setup(str(shot["locale"]), str(shot["scenario"]), 10001)
battle.advance_ticks(int(shot["ticks"]))
if shot.get("busy", false):
# Flood the floating-number pool against five enemies.
for i in range(16):
P2BattleSim.inject_input(
battle.state,
{"time": float(battle.state.get("time", 0.0)), "type": "click_enemy"},
)
battle.advance_ticks(1)
if str(shot["state"]) == "pause":
battle.paused = true
battle.hud.show_modal(
"modal.pause.title",
"modal.pause.resume",
[
{"id": "resume", "label_key": "modal.pause.resume"},
{"id": "settings", "label_key": "modal.pause.settings"},
{"id": "exit", "label_key": "modal.pause.exit"},
]
)
if shot.has("vfx"):
var vfx: VfxView = battle.hud.play_vfx(
str(shot["vfx"]), str(shot.get("vfx_target", ""))
)
if vfx != null:
# Hold a representative frame so the capture is deterministic.
vfx.set_process(false)
for _index in range(2):
vfx.step(1.0 / 12.0)
await _wait_frames(4)
var rects: Dictionary = battle.hud.region_rects()
# The contract is stated at 390x844, so only that size can be diffed
# against the reference rectangles. Other sizes are recorded and their
# own consistency is checked separately.
var reference := Vector2i(390, 844)
var problems: Array[String] = []
if real_size == Vector2(reference):
problems = BattleHudLayout.verify_layout(rects)
elif real_size.x < 340.0 or real_size.y < 700.0:
problems.append("viewport %s is smaller than the smallest required size" % str(real_size))
var image := root.get_texture().get_image()
var output_path := OUTPUT_DIR.path_join("%s.png" % shot["name"])
var saved := image.save_png(ProjectSettings.globalize_path(output_path))
root.remove_child(scene)
scene.queue_free()
return {
"name": str(shot["name"]),
"size": [size.x, size.y],
"viewport_size": [real_size.x, real_size.y],
"locale": str(shot["locale"]),
"scenario": str(shot["scenario"]),
"ticks": int(shot["ticks"]),
"state": str(shot["state"]),
"file": output_path,
"status": "ok" if saved == OK else "save_failed:%d" % saved,
"layout_ok": problems.is_empty(),
"layout_problems": problems,
"region_rects": _rects_to_json(rects),
}
func _rects_to_json(rects: Dictionary) -> Dictionary:
var out: Dictionary = {}
for key in rects.keys():
var rect: Rect2 = rects[key]
out[str(key)] = {
"x": rect.position.x,
"y": rect.position.y,
"w": rect.size.x,
"h": rect.size.y,
}
return out
func _wait_frames(count: int) -> void:
for _index in range(count):
await process_frame
await RenderingServer.frame_post_draw