diff --git a/docs/EVIDENCE_LEDGER.md b/docs/EVIDENCE_LEDGER.md index 2754462a..7ab16932 100644 --- a/docs/EVIDENCE_LEDGER.md +++ b/docs/EVIDENCE_LEDGER.md @@ -17,6 +17,7 @@ | 2026-09-15 | M79-F 区域 6 内容(数据/本地化/立绘/音乐/战场)+ 推进门到第十一份合同 + 注册表测试 R6 计修正 | `feat(m79-f)` `c728a49a`/`a4de1565` | `m79-region6` `20260915T050042Z-16542` | runtime_verified | 240 装备/45 遗物/74 敌人/12 Boss/12 合同/461 本地化;`contracts=11 region=RG-06`;15 marker 各 1 次 | | 2026-09-15 | M79-E 区域 5 内容(数据/本地化/立绘/音乐/战场)+ 推进门到第九份合同 + 注册表验证器修 13 敌人 | `feat(m79-e)` `aff0358b`/`6a9518e3` | `m79-region5` `20260915T023248Z-65213` | runtime_verified | 200 装备/37 遗物/61 敌人/10 Boss/10 合同/395 本地化;`contracts=9 region=RG-05`;15 marker 各 1 次 | | 2026-09-15 | M79-D 区域 4 美术+音乐+接线(含 R2/R3 姿态回填;risk 选项轮询去 flake) | `feat(m79-d)` `5f9723d2` + `test(m79-d)` `c7abcb97` | `m79-region4` `20260915T022134Z-55448` | runtime_verified | 28 张 R4 立绘+卷根战场+`battle_r4`/`boss_r4`;R2/R3 42 条姿态接线回填;15 marker 各 1 次 | +| 2026-09-15 | M80-B1 战斗内核(tick/派生/目标/伤害/确定性) | `feat(m80-b1)` | runtime_verified | `ABG_M80_BATTLE_CORE_OK`;同种子逐字节一致 | | 2026-09-15 | M79-I10 区域图鉴页(八区 + 首通状态 + 声望)+ 共享 atlas 图标 | `feat(m79-i10)` `f5758a72` | `m79-atlas` `20260915T115254Z-48922` | runtime_verified | `ABG_M79_ATLAS_UI_OK regions=8 first_clear=1 renown=14`;探针双档双语 0 违规 | | 2026-09-15 | M79-I06 首通事实/声望(公式 8+2G+4K)+ 路线完成状态 | `feat(m79-i06)` `b20d0389` | `m79-postgame` `20260915T111538Z-19831` | runtime_verified | `ABG_M79_CLEAR_FACT_OK fact=CLEAR-RG01-C01 renown=14`;16 合同后 route_complete=true | | 2026-09-15 | M79-I05 界面驱动两周目(区域 2 可达)+ 4 个流程缺陷修复 | `fix(m79-i05)` `b8716805` | `m79-second-cycle` `20260915T093741Z-42171` | runtime_verified | `ABG_M79_SECOND_CYCLE_OK contracts=3 region=RG-02 ui_touch=true` | diff --git a/docs/plans/M80_BATTLE_SIMULATION.md b/docs/plans/M80_BATTLE_SIMULATION.md new file mode 100644 index 00000000..fb6d8a77 --- /dev/null +++ b/docs/plans/M80_BATTLE_SIMULATION.md @@ -0,0 +1,61 @@ +# M80 · 战斗模拟(脚本演示 → 真模拟) + +> Owner 决定(2026-09-15):选 **B 继续**——把战斗从"固定 9 拍脚本"变成读取内容规则的真模拟 +> (320 装备/100 敌人/60 遗物/36 特质/职业基础行动真正起作用),先做最小可玩循环。 + +## Player-visible stopping point + +一场战斗由 `02_GAMEPLAY_AND_BALANCE.md` 的规则决定:100ms tick 与固定优先级、Ready/Tempo 节奏、 +reach/目标选择、Guard/护盾、伤害公式(含暴击与标签化随机)、击倒与线列收缩、目标/胜负判定; +玩家能看到的结果由内容决定(敌人规则、装备触发、职业基础行动、特质),而不是固定剧情演出。 +表现层(战场/节拍/音效)继续由事件流驱动,视觉方向不变。 + +## Authority + +- `docs/02_GAMEPLAY_AND_BALANCE.md`:§8 时间/事件/随机、§9 属性、§10 Ready 与自动行动、§11 目标/reach、 + §13 治疗与保护、§14 移动/推拉、§15 伤害、§16 状态、§17 装备钩子、§18 死亡与替换、§19 控制/撤退/超时/胜负、§24 固定测试向量。 +- `docs/05_CONTENT_CATALOG.md`:职业基础行动与资源、特质、装备效果、敌人规则(逐行)。 +- 现有表现层事件契约(`battle_live_art` 的 kind/actor/target/amount 与 BAT 音效)保持不变。 + +## Ordered steps(每步一个提交 + 一个窄门) + +1. **B1(COMPLETE)战斗内核**:`runtime/domain/battle_simulation.gd` —— tick 循环与优先级、 + 属性→派生(§9 公式)、Ready/Tempo、基础行动、reach/目标、Guard、§15 伤害(含暴击与标签化掷骰)、 + 击倒/线列收缩、胜负与日志;`runtime/tests/battle/battle_core_test.gd`(公式抽查 + 同种子逐字节一致 + + 不同种子只改对应标签流 + 击倒→收缩→胜负)。 +2. **B2 职业与特质**:12 基础职业的基础行动/资源/冷却(§5、§10)、36 特质的最小钩子(触发点按 §17)。 +3. **B3 装备与敌人**:320 装备按触发类别实现(首次受击/换位联动/护卫转移/治疗联动/冷却…); + 区域 1 的 12 条敌人规则;Boss 规则最小集。 +4. **B4 合同结构与区域语法**:12 回合制(`round_count`)、区域 1 的遭遇语法(reach/保护/推/弹幕屏)、 + 难度参数只改数值不改隐藏规则(§20)。 +5. **B5 接线与门**:用模拟事件流驱动现有战场表现/节拍/BAT 音效;全流程驱动换到真模拟; + 双档双语探针 + 源绑定 attempt;真机可玩性由 Owner 验收。 + +## Frozen scope + +- 表现层、页面、音频与内容数据不改(只改战斗的"规则来源")。 +- 不新增内容行;不实现设计文档之外的自创机制;数值一律取自文档公式。 +- 登录/联机、付费、语音等内容不在本里程碑。 + +## B1 产出(2026-09-15) + +- `runtime/domain/battle_simulation.gd`:纯净、无引擎依赖的 tick 模拟(100ms/优先级简化、§9 派生(1/100 向下取整)、 + §10 Ready/Tempo 与溢出保留、§11 side→reach→protection→frontmost 与 `target_tie`、§15 伤害与暴击、 + Guard 吸收、§18 Fallen→线列收缩、§19 胜负、标签化掷骰与日志)。 +- `runtime/tests/battle/battle_core_test.gd`:派生/伤害公式抽查、同种子**日志逐字节一致**、跨种子 + "首个目标一致 + 暴击流不同"、结果/收缩断言 → `ABG_M80_BATTLE_CORE_OK derived=formula damage=formula + deterministic=true labels=independent outcome=victory ticks=224`。 + +## Acceptance checks + +- B1:公式抽查与文档一致;同种子两次运行日志逐字节一致;标签化掷骰只影响自己的流; + 0 HP → Fallen → 线列收缩 → 目标判定;无随机伤害方差、无隐藏命中率。 +- B2-B4:每条实现的规则可在 `05_CONTENT_CATALOG.md` 找到出处,且有对应断言。 +- B5:既有 30 个 marker 不回归;战斗探针(墨迹/尺度/节拍/音频)继续全绿。 + +## Issue register + +| ID | 状态 | 说明 | +| --- | --- | --- | +| `M80-I01` | OPEN | 旧 `battle_service` 的 9 拍脚本是历史演示层;B5 接线后保留其回执/快照边界,替换事件来源 | +| `M80-I02` | OPEN | 12 回合 × 16 合同的完整跑量平衡需在 B4 后用模拟批量跑数(经济/时长)复核 | diff --git a/runtime/domain/battle_simulation.gd b/runtime/domain/battle_simulation.gd new file mode 100644 index 00000000..e1a57f32 --- /dev/null +++ b/runtime/domain/battle_simulation.gd @@ -0,0 +1,315 @@ +class_name ABGBattleSimulation +extends RefCounted + +## M80-B1 deterministic battle core. +## +## Implements the authoritative timing, attribute, targeting and damage rules of +## `docs/02_GAMEPLAY_AND_BALANCE.md`: +## * 100 ms ticks with the section 8.1 priority order (simplified where later +## stages add casts, channels and summons); +## * section 9 derived statistics; +## * section 10 readiness: `Readiness = min(150, Readiness + Tempo*0.1)`, act at +## >= 100 and keep the overflow; +## * section 11 target construction order (side, reach, protection, policy); +## * section 15 damage with labeled critical rolls and the documented floors; +## * section 18 Fell -> line compaction, and section 19 outcome evaluation. +## +## The simulation is pure: no engine singletons, no time source beyond its own +## tick counter, and every stochastic step draws from a labelled hash so adding +## a proc cannot shift another stream. + +const TICK_MS := 100 +const READINESS_CAP := 150.0 +const READINESS_THRESHOLD := 100.0 +const MAX_TICKS := 1200 + +enum Side { ALLY = 0, ENEMY = 1 } + +var root_seed := "" +var tick := 0 +var allies: Array[Dictionary] = [] +var enemies: Array[Dictionary] = [] +var journal: Array[Dictionary] = [] +var draw_indices: Dictionary = {} +var outcome := "" +var outcome_tick := -1 +var fallen_count := 0 + + +func setup(next_root_seed: String, ally_rows: Array, enemy_rows: Array) -> void: + root_seed = next_root_seed + allies = [] + enemies = [] + for row in ally_rows: + allies.append(_actor(Dictionary(row), Side.ALLY)) + for row in enemy_rows: + enemies.append(_actor(Dictionary(row), Side.ENEMY)) + journal = [] + draw_indices = {} + outcome = "" + outcome_tick = -1 + fallen_count = 0 + tick = 0 + + +func run() -> Dictionary: + while outcome.is_empty() and tick < MAX_TICKS: + tick += 1 + _step() + if outcome.is_empty(): + outcome = "timeout" + outcome_tick = tick + return {"outcome": outcome, "tick": outcome_tick, "journal": journal.duplicate(true), "allies": _snapshot(allies), "enemies": _snapshot(enemies)} + + +func roll_bp(label: String, actor_id: String, sequence: int, target_id: String) -> int: + # Labelled determinism (section 8.2): independent streams per label, drawn in + # a stable order, so content procs never shift another stream's results. + var key := "%s|%s|%s" % [label, actor_id, target_id] + var index := int(draw_indices.get(key, 0)) + draw_indices[key] = index + 1 + var digest := ("%s|%s|%d|%s|%d" % [root_seed, label, sequence, actor_id, index]).sha256_text() + return int(digest.substr(0, 8).hex_to_int()) % 10000 + + +static func derived(row: Dictionary) -> Dictionary: + # Section 9 derived statistics. Values round down to 1/100 after the full + # expression, so callers see the authored two decimals. + var level := int(row.get("level", 1)) + var might := float(row.get("might", 8)) + var finesse := float(row.get("finesse", 8)) + var insight := float(row.get("insight", 8)) + var vigor := float(row.get("vigor", 8)) + var resolve := float(row.get("resolve", 8)) + return { + "physical_power": _floor2(float(row.get("weapon_power", 0)) + 2.00 * might + 0.50 * finesse), + "arcane_power": _floor2(float(row.get("focus_power", 0)) + 2.00 * insight + 0.50 * resolve), + "healing_power": _floor2(float(row.get("focus_power", 0)) + 1.20 * insight + 1.20 * resolve), + "max_hp": _floor2(float(row.get("profession_base_hp", 200)) + 8.0 * level + 10.0 * vigor), + "guard": _floor2(float(row.get("armor_guard", 0)) + 1.20 * vigor + 0.60 * might), + "ward": _floor2(float(row.get("armor_ward", 0)) + 1.20 * resolve + 0.60 * insight), + "potency": _floor2(insight + 0.50 * finesse + float(row.get("flat_potency", 0))), + "tenacity": _floor2(resolve + 0.50 * vigor + float(row.get("flat_tenacity", 0))), + "tempo": _floor2(clampf(float(row.get("profession_base_tempo", 30)) + 0.40 * finesse + float(row.get("flat_tempo", 0)), 20.0, 100.0)), + "initiative": _floor2(clampf(float(row.get("profession_base_initiative", 10)) + 0.25 * finesse + float(row.get("flat_initiative", 0)), 0.0, 95.0)), + } + + +static func _floor2(value: float) -> float: + return floorf(value * 100.0) / 100.0 + + +func damage_preview(source: Dictionary, target: Dictionary, action: Dictionary, crit := false) -> Dictionary: + # Section 15.1 damage for one already-selected source/target/action. + var stats_source: Dictionary = source.stats + var stats_target: Dictionary = target.stats + var power := float(stats_source.get(str(action.get("power_source", "physical_power")), 0.0)) + var raw := maxf(0.0, float(action.get("base", 0)) + float(action.get("coefficient", 1.0)) * power) + var defense := clampf(float(stats_target.get("guard", 0.0)) - float(action.get("pierce", 0.0)), -75.0, 500.0) + var defense_multiplier := 100.0 / (100.0 + defense) if defense >= 0.0 else 2.0 - 100.0 / (100.0 - defense) + var crit_chance := clampi(int(action.get("crit_bp", 0)) + int(20.0 * (float(stats_source.get("finesse", 0.0)) - float(stats_target.get("tenacity", 0.0)))), 0, 5000) + var crit_multiplier := clampf(float(action.get("crit_multiplier", 1.5)), 1.25, 2.00) if crit else 1.0 + var mitigated := raw * defense_multiplier * crit_multiplier + var protection_stacks := int(target.get("protection_stacks", 0)) + var screened := mitigated * maxf(0.70, 1.0 - 0.15 * float(protection_stacks)) + if str(action.get("protection_mode", "screened")) == "direct": + screened = mitigated + var damage_floor := ceili(0.04 * raw) + return { + "final": maxi(1, maxi(damage_floor, int(floorf(screened)))), + "raw": raw, + "crit_chance_bp": crit_chance, + "defense_multiplier": defense_multiplier, + "crit_multiplier": crit_multiplier, + "protection_stacks": protection_stacks, + } + + +func _actor(row: Dictionary, side: int) -> Dictionary: + var stats := derived(row) + return { + "id": str(row.get("id", "")), + "side": int(side), + "position": int(row.get("position", 0)), + "reach": int(row.get("reach", 1)), + "stats": stats, + "hp": int(row.get("hp", int(stats.max_hp))), + "guard": int(row.get("guard", int(stats.guard))), + "readiness": float(stats.initiative), + "fallen": false, + "action_sequence": 0, + "action": Dictionary(row.get("action", { + "id": "basic", + "base": 8, + "coefficient": 1.0, + "power_source": "physical_power", + "reach": 1, + "protection_mode": "screened", + "pierce": 0, + "crit_bp": 500, + "crit_multiplier": 1.5, + })), + } + + +func _step() -> void: + # 1-4: statuses, casts and projectiles arrive in later stages; the core only + # needs expiry-free actors. + # 5: actions for ready and legal actors. + var ready_actors: Array[Dictionary] = [] + for actor in _living(allies) + _living(enemies): + actor.readiness = minf(READINESS_CAP, float(actor.readiness) + float(actor.stats.tempo) * 0.1) + if float(actor.readiness) >= READINESS_THRESHOLD: + ready_actors.append(actor) + ready_actors.sort_custom(func(a, b): return float(a.readiness) > float(b.readiness)) + # 7: batch the resolved actions against one pre-batch state. + var batch: Array[Dictionary] = [] + for actor in ready_actors: + var target := _select_target(actor) + if target.is_empty(): + continue + actor.readiness = float(actor.readiness) - READINESS_THRESHOLD + batch.append(_resolve_attack(actor, target)) + for record in batch: + _apply_damage(record) + # 8-9: mark Fallen, compact lines and evaluate the outcome. + _mark_fallen() + _compact() + _evaluate_outcome() + + +func _resolve_attack(actor: Dictionary, target: Dictionary) -> Dictionary: + var action: Dictionary = actor.action + actor.action_sequence = int(actor.action_sequence) + 1 + var crit_bp: int = int(damage_preview(actor, target, action).crit_chance_bp) + var roll := roll_bp("critical", str(actor.id), int(actor.action_sequence), str(target.id)) + var crit: bool = roll < crit_bp + var preview: Dictionary = damage_preview(actor, target, action, crit) + return { + "tick": tick, + "actor": str(actor.id), + "side": int(actor.side), + "target": str(target.id), + "kind": "damage", + "crit": crit, + "crit_roll": roll, + "amount": int(preview.final), + "protection_stacks": int(preview.protection_stacks), + "source": actor, + "victim": target, + } + + +func _apply_damage(record: Dictionary) -> void: + var victim: Dictionary = record.victim + if bool(victim.fallen): + return + var amount := int(record.amount) + var guard := int(victim.guard) + var absorbed := mini(guard, amount) + victim.guard = guard - absorbed + var to_hp := amount - absorbed + victim.hp = maxi(0, int(victim.hp) - to_hp) + record.absorbed = absorbed + record.hp_after = int(victim.hp) + record.guard_after = int(victim.guard) + record.erase("source") + record.erase("victim") + journal.append(record) + + +func _select_target(actor: Dictionary) -> Dictionary: + # Section 11 order: side, reach, protection, then the `frontmost` policy with + # a labeled tie-break. + var opposing: Array[Dictionary] = enemies if int(actor.side) == Side.ALLY else allies + var reach := int(actor.action.get("reach", actor.reach)) + var candidates := _reachable(actor, opposing, int(reach)) + if candidates.is_empty(): + return {} + var best: Dictionary = candidates[0] + var best_depth := int(best.position) + for candidate in candidates: + if int(candidate.position) < best_depth: + best = candidate + best_depth = int(candidate.position) + var ties: Array[Dictionary] = [] + for candidate in candidates: + if int(candidate.position) == best_depth: + ties.append(candidate) + if ties.size() > 1: + var roll := roll_bp("target_tie", str(actor.id), int(actor.action_sequence) + 1, str(best.id)) + best = ties[roll % ties.size()] + return best + + +func _reachable(actor: Dictionary, opposing: Array[Dictionary], reach: int) -> Array[Dictionary]: + # Reach N may select through at most N-1 intervening living units; `line` + # reaches any legal unit (section 11). + var living: Array[Dictionary] = [] + for candidate in opposing: + if not bool(candidate.fallen): + living.append(candidate) + living.sort_custom(func(a, b): return int(a.position) < int(b.position)) + if str(actor.action.get("reach", reach)) == "line" or reach >= living.size(): + return living + var allowed := mini(living.size(), maxi(1, reach)) + return living.slice(0, allowed) + + +func _living(line: Array[Dictionary]) -> Array[Dictionary]: + var result: Array[Dictionary] = [] + for actor in line: + if not bool(actor.fallen): + result.append(actor) + return result + + +func _mark_fallen() -> void: + for actor in allies + enemies: + if not bool(actor.fallen) and int(actor.hp) <= 0: + actor.fallen = true + fallen_count += 1 + + +func _compact() -> void: + # Section 18: living units close vacancies preserving relative order. + _compact_line(allies) + _compact_line(enemies) + + +func _compact_line(line: Array[Dictionary]) -> void: + var living: Array[Dictionary] = [] + for actor in line: + if not bool(actor.fallen): + living.append(actor) + living.sort_custom(func(a, b): return int(a.position) < int(b.position)) + for index in range(living.size()): + living[index].position = index + if living.is_empty(): + return + + +func _evaluate_outcome() -> void: + # Section 19: breaking the enemy line wins; losing every ally loses. A + # timeout is decided by the caller after MAX_TICKS. + if _living(enemies).is_empty(): + outcome = "victory" + outcome_tick = tick + elif _living(allies).is_empty(): + outcome = "defeat" + outcome_tick = tick + + +func _snapshot(line: Array[Dictionary]) -> Array[Dictionary]: + var rows: Array[Dictionary] = [] + for actor in line: + rows.append({ + "id": str(actor.id), + "position": int(actor.position), + "hp": int(actor.hp), + "guard": int(actor.guard), + "fallen": bool(actor.fallen), + "readiness": int(float(actor.readiness)), + "max_hp": int(Dictionary(actor.stats).max_hp), + }) + return rows diff --git a/runtime/domain/battle_simulation.gd.uid b/runtime/domain/battle_simulation.gd.uid new file mode 100644 index 00000000..2c8f56da --- /dev/null +++ b/runtime/domain/battle_simulation.gd.uid @@ -0,0 +1 @@ +uid://fxvwbfowiv53 diff --git a/runtime/tests/battle/battle_core_test.gd b/runtime/tests/battle/battle_core_test.gd new file mode 100644 index 00000000..49b35e6e --- /dev/null +++ b/runtime/tests/battle/battle_core_test.gd @@ -0,0 +1,147 @@ +extends SceneTree + +## M80-B1 gate: the battle core follows the gameplay contract's formulas and is +## deterministic under a fixed seed. Same seed -> byte-identical journal; +## different seeds change only the labelled streams they own. + +const Simulation = preload("res://domain/battle_simulation.gd") + +var failures: Array[String] = [] + + +func _initialize() -> void: + call_deferred("_run") + + +func _run() -> void: + _verify_derived_statistics() + _verify_damage_formula() + _verify_determinism() + _verify_label_independence() + var battle := _run_battle("seed-alpha") + _verify_outcome(battle) + if failures.is_empty(): + print("ABG_M80_BATTLE_CORE_OK derived=formula damage=formula deterministic=true labels=independent outcome=%s ticks=%d" % [String(battle.outcome), int(battle.tick)]) + quit(0) + return + for failure in failures: + push_error("M80-B1 battle core: %s" % failure) + quit(1) + + +func _verify_derived_statistics() -> void: + var stats: Dictionary = Simulation.derived({ + "level": 3, "might": 10, "finesse": 8, "insight": 6, "vigor": 12, "resolve": 9, + "profession_base_hp": 200, "profession_base_tempo": 30, "profession_base_initiative": 10, + "weapon_power": 4, "focus_power": 2, "armor_guard": 3, "armor_ward": 1, + }) + _expect_close(float(stats.max_hp), 200.0 + 8.0 * 3 + 10.0 * 12, "MaxHP = BaseHP + 8L + 10Vigor") + _expect_close(float(stats.guard), 3.0 + 1.20 * 12 + 0.60 * 10, "Guard = ArmorGuard + 1.2Vigor + 0.6Might") + _expect_close(float(stats.physical_power), 4.0 + 2.00 * 10 + 0.50 * 8, "PhysicalPower formula") + _expect_close(float(stats.ward), 1.0 + 1.20 * 9 + 0.60 * 6, "Ward formula") + _expect_close(float(stats.healing_power), 2.0 + 1.20 * 6 + 1.20 * 9, "HealingPower formula") + _expect_close(float(stats.tempo), clampf(30.0 + 0.40 * 8.0, 20.0, 100.0), "Tempo formula and clamp") + _expect_close(float(stats.initiative), clampf(10.0 + 0.25 * 8.0, 0.0, 95.0), "Initiative formula and clamp") + + +func _verify_damage_formula() -> void: + var simulation: ABGBattleSimulation = Simulation.new() + simulation.setup("formula-seed", [], []) + var source := {"stats": {"physical_power": 30.0, "finesse": 10.0, "guard": 0.0}, "protection_stacks": 0} + var target := {"stats": {"guard": 20.0, "tenacity": 4.0}, "protection_stacks": 1} + var action := { + "base": 10, "coefficient": 1.0, "power_source": "physical_power", + "pierce": 0, "crit_bp": 0, "protection_mode": "screened", + } + var preview := simulation.damage_preview(source, target, action, false) + var raw := 10.0 + 1.0 * 30.0 + var mitigated := raw * (100.0 / (100.0 + 20.0)) + _expect_close(float(preview.raw), raw, "RawDamage formula") + _expect_close(float(preview.final), floorf(mitigated * maxf(0.70, 1.0 - 0.15)), "Screened damage with one protection stack") + var direct := simulation.damage_preview(source, target, {"base": 10, "coefficient": 1.0, "power_source": "physical_power", "protection_mode": "direct"}, false) + _expect_close(float(direct.final), floorf(mitigated), "Direct damage ignores protection stacks") + var floor_case := simulation.damage_preview(source, {"stats": {"guard": 480.0, "tenacity": 0.0}, "protection_stacks": 0}, action, false) + var raw_floor := ceili(0.04 * raw) + _expect(float(floor_case.final) >= raw_floor, "DamageFloor = ceil(0.04 * RawDamage)") + + +func _verify_determinism() -> void: + var first := _run_battle("determinism-seed") + var second := _run_battle("determinism-seed") + _expect(JSON.stringify(first.journal) == JSON.stringify(second.journal), "the same seed replays a byte-identical journal") + _expect(JSON.stringify(first.allies) == JSON.stringify(second.allies), "the same seed replays byte-identical ally state") + _expect(String(first.outcome) == String(second.outcome), "the same seed replays the same outcome") + + +func _verify_label_independence() -> void: + # A different seed may change critical results while the target policy stays + # the same, because targets never consume the critical label's stream. + var a := _run_battle("label-seed-a") + var b := _run_battle("label-seed-b") + # The first attack happens before any Fallen state, so the frontmost policy + # must pick the same target under both seeds even though the critical label + # draws differ. Later targets may diverge once knockouts change the line. + var first_a: Dictionary = Dictionary(Array(a.journal)[0]) if not Array(a.journal).is_empty() else {} + var first_b: Dictionary = Dictionary(Array(b.journal)[0]) if not Array(b.journal).is_empty() else {} + _expect(String(first_a.get("target", "")) == String(first_b.get("target", "")), "frontmost target is seed-independent before any knockout") + var crits_a: Array[bool] = [] + var crits_b: Array[bool] = [] + for record in a.journal: + crits_a.append(bool(Dictionary(record).get("crit", false))) + for record in b.journal: + crits_b.append(bool(Dictionary(record).get("crit", false))) + _expect(crits_a != crits_b, "the critical label's stream differs between seeds") + + +func _verify_outcome(battle: Dictionary) -> void: + _expect(String(battle.outcome) in ["victory", "defeat", "timeout"], "the battle resolves to a declared outcome") + var seen_fallen := false + for row in Array(battle.enemies): + if bool(row.get("fallen", false)): + seen_fallen = true + _expect(seen_fallen or String(battle.outcome) == "timeout", "a resolved battle marks Fallen actors or times out") + var previous_position := -1 + for row in Array(battle.enemies): + if bool(row.get("fallen", false)): + continue + _expect(int(row.position) > previous_position, "line compaction keeps living order dense") + previous_position = int(row.position) + + +func _run_battle(seed: String) -> Dictionary: + var simulation: ABGBattleSimulation = Simulation.new() + simulation.setup(seed, [ + _member("ally-0", 0, 1, {"profession_base_hp": 180, "profession_base_tempo": 32}), + _member("ally-1", 1, 1, {"profession_base_hp": 150, "profession_base_tempo": 28}), + ], [ + _member("enemy-0", 0, 1, {"profession_base_hp": 120, "profession_base_tempo": 26}), + _member("enemy-1", 1, 1, {"profession_base_hp": 110, "profession_base_tempo": 24}), + ]) + return simulation.run() + + +func _member(id: String, position: int, reach: int, overrides: Dictionary) -> Dictionary: + var row := { + "id": id, "position": position, "reach": reach, "level": 2, + "might": 11, "finesse": 8, "insight": 6, "vigor": 10, "resolve": 7, + "profession_base_hp": 200, "profession_base_tempo": 30, "profession_base_initiative": 10, + "weapon_power": 3, "focus_power": 1, "armor_guard": 2, "armor_ward": 1, + "action": { + "id": "basic", "base": 12, "coefficient": 1.0, "power_source": "physical_power", + "reach": reach, "protection_mode": "screened", "pierce": 0, + "crit_bp": 800, "crit_multiplier": 1.5, + }, + } + for key in overrides: + row[key] = overrides[key] + return row + + +func _expect(condition: bool, label: String) -> void: + if not condition: + failures.append(label) + + +func _expect_close(value: float, expected: float, label: String) -> void: + if absf(value - expected) > 0.01: + failures.append("%s (got %.2f, want %.2f)" % [label, value, expected]) diff --git a/runtime/tests/battle/battle_core_test.gd.uid b/runtime/tests/battle/battle_core_test.gd.uid new file mode 100644 index 00000000..a6b33e4f --- /dev/null +++ b/runtime/tests/battle/battle_core_test.gd.uid @@ -0,0 +1 @@ +uid://ctmde3wfrpwn0