Add unit defeat event reactions

This commit is contained in:
2026-06-18 14:14:51 +09:00
parent 8c4d7c8822
commit 4f5e949d84
10 changed files with 218 additions and 7 deletions

View File

@@ -0,0 +1,100 @@
extends SceneTree
const BattleStateScript := preload("res://scripts/core/battle_state.gd")
func _init() -> void:
var failures: Array[String] = []
_check_defeat_event_log(failures)
_check_defeat_event_can_delay_victory(failures)
if failures.is_empty():
print("event unit_defeated smoke ok")
quit(0)
return
for failure in failures:
push_error(failure)
quit(1)
func _check_defeat_event_log(failures: Array[String]) -> void:
var state = _loaded_state(failures, "defeat event log")
if state == null:
return
var logs := []
state.log_added.connect(func(message: String) -> void:
logs.append(message)
)
state.fired_event_ids.clear()
state.battle_events.clear()
state.battle_events.append({
"id": "zhang_mancheng_falls",
"once": true,
"when": {"type": "unit_defeated", "unit_ids": ["yellow_turban_1"]},
"actions": [
{"type": "log", "text": "Zhang Mancheng's banner falls."}
]
})
_defeat_unit_with_skill(state, "yellow_turban_1")
if not logs.has("Zhang Mancheng's banner falls."):
failures.append("unit_defeated event log did not fire")
if not state.fired_event_ids.has("zhang_mancheng_falls"):
failures.append("unit_defeated event id was not recorded")
func _check_defeat_event_can_delay_victory(failures: Array[String]) -> void:
var state = _loaded_state(failures, "defeat event victory delay")
if state == null:
return
for enemy in state.get_living_units("enemy"):
if str(enemy.get("id", "")) != "yellow_turban_1":
enemy["alive"] = false
state.fired_event_ids.clear()
state.battle_events.clear()
state.battle_events.append({
"id": "last_rebel_reserve_arrives",
"once": true,
"when": {"type": "unit_defeated", "unit_ids": ["yellow_turban_1"]},
"actions": [
{
"type": "spawn_deployment",
"deployment": {
"unit_id": "last_rebel_reserve",
"name": "Last Rebel Reserve",
"class_id": "bandit",
"team": "enemy",
"level": 1,
"pos": [5, 5],
"base": {"hp": 24, "atk": 9, "def": 4}
}
}
]
})
_defeat_unit_with_skill(state, "yellow_turban_1")
var reserve: Dictionary = state.get_unit("last_rebel_reserve")
if reserve.is_empty() or not bool(reserve.get("alive", false)) or not bool(reserve.get("deployed", true)):
failures.append("unit_defeated reinforcement did not arrive alive and deployed")
if str(state.battle_status) != "active":
failures.append("unit_defeated reinforcement should keep battle active, got %s" % str(state.battle_status))
func _loaded_state(failures: Array[String], label: String):
var state = BattleStateScript.new()
if not state.load_battle("res://data/scenarios/001_yellow_turbans.json"):
failures.append("%s could not load smoke scenario" % label)
return null
return state
func _defeat_unit_with_skill(state, unit_id: String) -> void:
var caster: Dictionary = state.get_unit("cao_cao")
var target: Dictionary = state.get_unit(unit_id)
target["hp"] = 1
state._resolve_skill_damage(caster, target, {
"name": "Smoke Bolt",
"power": 99,
"stat": "int"
})

View File

@@ -92,7 +92,7 @@ $validConditionTypes = @(
"all",
"any"
)
$validTriggers = @("battle_start", "battle_begin", "turn_start", "unit_reaches_tile")
$validTriggers = @("battle_start", "battle_begin", "turn_start", "unit_reaches_tile", "unit_defeated")
$validActions = @("log", "dialogue", "set_objective", "grant_item", "grant_items", "spawn_deployment", "spawn_deployments", "withdraw_unit", "withdraw_units")
$validEffects = @("heal_hp", "heal_mp", "cure_status")
$validItemKinds = @("weapon", "armor", "accessory", "consumable")
@@ -398,6 +398,55 @@ function Check-Unit-Reaches-Event-When($When, [string]$ScenarioId, [int]$Width,
}
}
function Check-Unit-Defeated-Event-When($When, [string]$ScenarioId, $KnownUnitIds) {
$unitIdsValue = $null
if (Has-Prop $When "unit_ids") {
$unitIdsValue = $When.unit_ids
if ($null -ne $unitIdsValue -and -not ($unitIdsValue -is [System.Array])) {
Fail "Scenario $ScenarioId unit_defeated event unit_ids must be an array."
}
foreach ($unitIdValue in @($unitIdsValue)) {
$unitId = [string]$unitIdValue
if ([string]::IsNullOrWhiteSpace($unitId)) {
Fail "Scenario $ScenarioId unit_defeated event has empty unit id."
}
if ($null -ne $KnownUnitIds -and (-not $KnownUnitIds.Contains($unitId))) {
Fail "Scenario $ScenarioId unit_defeated event references unknown unit: $unitId"
}
}
}
$officerIdsValue = $null
if (Has-Prop $When "officer_ids") {
$officerIdsValue = $When.officer_ids
if ($null -ne $officerIdsValue -and -not ($officerIdsValue -is [System.Array])) {
Fail "Scenario $ScenarioId unit_defeated event officer_ids must be an array."
}
foreach ($officerIdValue in @($officerIdsValue)) {
$officerId = [string]$officerIdValue
if ([string]::IsNullOrWhiteSpace($officerId) -or (-not $officerIds.Contains($officerId))) {
Fail "Scenario $ScenarioId unit_defeated event references unknown officer: $officerId"
}
}
}
$team = [string](Get-Prop $When "team" "")
if (-not [string]::IsNullOrWhiteSpace($team) -and $team -ne "player" -and $team -ne "enemy") {
Fail "Scenario $ScenarioId unit_defeated event has unknown team: $team"
}
$unitIdCount = 0
if ($null -ne $unitIdsValue) {
$unitIdCount = @($unitIdsValue).Count
}
$officerIdCount = 0
if ($null -ne $officerIdsValue) {
$officerIdCount = @($officerIdsValue).Count
}
if ([string]::IsNullOrWhiteSpace($team) -and $unitIdCount -le 0 -and $officerIdCount -le 0) {
Fail "Scenario $ScenarioId unit_defeated event needs team, unit_ids, or officer_ids."
}
}
function Resolve-Class-Id($Deployment, [string]$ScenarioId) {
$classId = [string](Get-Prop $Deployment "class_id" "")
if (-not [string]::IsNullOrWhiteSpace($classId)) {
@@ -1763,6 +1812,9 @@ foreach ($scenario in $campaign.scenarios) {
if ($trigger -eq "unit_reaches_tile") {
Check-Unit-Reaches-Event-When $when $scenarioId $width $height $rows $knownUnitIds
}
if ($trigger -eq "unit_defeated") {
Check-Unit-Defeated-Event-When $when $scenarioId $knownUnitIds
}
foreach ($action in (Get-Prop $event "actions" @())) {
$actionType = [string]$action.type
if (-not $validActions.Contains($actionType)) {