Add event AI target priority shifts

This commit is contained in:
2026-06-18 14:40:05 +09:00
parent 0c0a49ad57
commit a2ecef7624
10 changed files with 193 additions and 11 deletions

View File

@@ -0,0 +1,68 @@
extends SceneTree
const BattleStateScript := preload("res://scripts/core/battle_state.gd")
func _init() -> void:
var failures: Array[String] = []
var state = BattleStateScript.new()
if not state.load_battle("res://data/scenarios/001_yellow_turbans.json"):
push_error("Could not load AI priority smoke scenario.")
quit(1)
return
_check_priority_action(state, failures)
_check_priority_changes_enemy_targeting(state, failures)
if failures.is_empty():
print("event ai priority smoke ok")
quit(0)
return
for failure in failures:
push_error(failure)
quit(1)
func _check_priority_action(state, failures: Array[String]) -> void:
var logs: Array[String] = []
var feedback: Array[Dictionary] = []
state.log_added.connect(func(message: String) -> void:
logs.append(message)
)
state.combat_feedback_requested.connect(func(unit_id: String, text: String, kind: String) -> void:
feedback.append({"unit_id": unit_id, "text": text, "kind": kind})
)
state._execute_event_action({
"type": "set_ai_target_priority",
"unit_id": "xiahou_dun",
"priority": 9,
"text": "Xiahou Dun draws the enemy's focus."
})
var target: Dictionary = state.get_unit("xiahou_dun")
if int(target.get("ai_target_priority", 0)) != 9:
failures.append("AI target priority did not update on event action")
if not logs.has("Xiahou Dun draws the enemy's focus."):
failures.append("AI priority event log missing")
if not _feedback_has(feedback, "xiahou_dun", "TARGET", "priority"):
failures.append("AI priority feedback missing")
func _check_priority_changes_enemy_targeting(state, failures: Array[String]) -> void:
var enemy: Dictionary = state.get_unit("yellow_turban_1")
var target: Dictionary = state._find_nearest_target(enemy, "player")
if str(target.get("id", "")) != "xiahou_dun":
failures.append("AI priority did not affect nearest target scoring: %s" % str(target.get("id", "")))
func _feedback_has(feedback: Array[Dictionary], expected_unit_id: String, expected_text: String, expected_kind: String) -> bool:
for entry in feedback:
if (
str(entry.get("unit_id", "")) == expected_unit_id
and str(entry.get("text", "")) == expected_text
and str(entry.get("kind", "")) == expected_kind
):
return true
return false

View File

@@ -93,7 +93,7 @@ $validConditionTypes = @(
"any"
)
$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")
$validActions = @("log", "dialogue", "set_objective", "grant_item", "grant_items", "spawn_deployment", "spawn_deployments", "withdraw_unit", "withdraw_units", "set_ai_target_priority")
$validEffects = @("heal_hp", "heal_mp", "cure_status")
$validItemKinds = @("weapon", "armor", "accessory", "consumable")
$validItemRarities = @("common", "named")
@@ -647,6 +647,59 @@ function Check-Event-Withdraw-Units($UnitIds, $KnownUnitIds, [string]$Context) {
}
}
function Check-Event-Unit-Ids($Action, $KnownUnitIds, [string]$Context) {
$unitIds = @()
if (Has-Prop $Action "unit_ids") {
if (-not ($Action.unit_ids -is [System.Array])) {
Fail "$Context unit_ids must be an array."
}
$unitIds = @($Action.unit_ids)
} else {
$unitId = [string](Get-Prop $Action "unit_id" (Get-Prop $Action "id" ""))
if (-not [string]::IsNullOrWhiteSpace($unitId)) {
$unitIds = @($unitId)
}
}
if ($unitIds.Count -le 0) {
Fail "$Context needs unit_id or unit_ids."
}
$seenUnitIds = New-Object System.Collections.Generic.HashSet[string]
foreach ($unitIdValue in $unitIds) {
$unitId = [string]$unitIdValue
if ([string]::IsNullOrWhiteSpace($unitId)) {
Fail "$Context has empty unit id."
}
if (-not $KnownUnitIds.Contains($unitId)) {
Fail "$Context references unknown unit: $unitId"
}
if (-not $seenUnitIds.Add($unitId)) {
Fail "$Context has duplicate unit: $unitId"
}
}
}
function Check-Event-Ai-Target-Priority($Action, $KnownUnitIds, [string]$Context) {
Check-Event-Unit-Ids $Action $KnownUnitIds $Context
if (-not (Has-Prop $Action "priority")) {
Fail "$Context must set priority."
}
$priorityValue = Get-Prop $Action "priority" 0
if (-not ($priorityValue -is [int] -or $priorityValue -is [long])) {
Fail "$Context priority must be an integer."
}
$priority = [int]$priorityValue
if ($priority -lt 0 -or $priority -gt 20) {
Fail "$Context priority must be between 0 and 20."
}
if ((Has-Prop $Action "text") -and [string]::IsNullOrWhiteSpace([string](Get-Prop $Action "text" ""))) {
Fail "$Context text must not be empty."
}
if ((Has-Prop $Action "silent") -and -not ((Get-Prop $Action "silent" $false) -is [bool])) {
Fail "$Context silent must be boolean."
}
}
function Check-Class-Equipment-Slots() {
$weaponTypes = @()
$armorTypes = @()
@@ -1880,6 +1933,9 @@ foreach ($scenario in $campaign.scenarios) {
if ($actionType -eq "withdraw_units") {
Check-Event-Withdraw-Units (Get-Prop $action "unit_ids" $null) $knownUnitIds "Scenario $scenarioId event $eventId withdraw_units"
}
if ($actionType -eq "set_ai_target_priority") {
Check-Event-Ai-Target-Priority $action $knownUnitIds "Scenario $scenarioId event $eventId set_ai_target_priority"
}
}
}