220 lines
12 KiB
GDScript
220 lines
12 KiB
GDScript
class_name TurnResolver
|
|
extends RefCounted
|
|
|
|
const TurnResultType = preload("res://scripts/domain/turn_result.gd")
|
|
const CombatEngineType = preload("res://scripts/domain/combat/combat_engine.gd")
|
|
const EnemyBehaviorsType = preload("res://scripts/domain/ai/enemy_behaviors.gd")
|
|
const CompanionBehaviorsType = preload("res://scripts/domain/ai/companion_behaviors.gd")
|
|
const CompanionStateType = preload("res://scripts/domain/entities/companion_state.gd")
|
|
|
|
static func resolve_player_action(state: GameState, direction: Vector2i) -> Array[TurnResult]:
|
|
var results: Array[TurnResult] = []
|
|
if direction == Vector2i.ZERO:
|
|
return results
|
|
var player: ActorState = state.player
|
|
var destination: Vector2i = player.cell + direction
|
|
if abs(direction.x) == 1 and abs(direction.y) == 1:
|
|
# Diagonal movement cannot squeeze between blocking cardinal cells. A
|
|
# closed door must therefore gate the room instead of being bypassable at
|
|
# either corner; walls and chests obey the same geometry.
|
|
var horizontal := player.cell + Vector2i(direction.x, 0)
|
|
var vertical := player.cell + Vector2i(0, direction.y)
|
|
if not state.map.is_walkable(horizontal) or not state.map.is_walkable(vertical):
|
|
results.append(TurnResultType.new(player.id, &"blocked", player.cell, destination, &"", 0, "Your way is blocked."))
|
|
return results
|
|
var enemy: ActorState = _enemy_at(state, destination)
|
|
if enemy != null:
|
|
# A companion-owning run takes the neutral shared-XP seam. Combat must
|
|
# not also mutate an XP ledger; F1 will remain the sole future owner of
|
|
# progression (levels, thresholds, and rewards).
|
|
var combat := CombatEngineType.new().resolve_attack(player, enemy, state.companion == null)
|
|
_share_companion_xp(state, combat, enemy)
|
|
_append_combat_results(results, combat, player.cell, destination)
|
|
if not enemy.is_alive():
|
|
state.enemies.erase(enemy.id)
|
|
else:
|
|
append_enemy_response(state, results)
|
|
_append_companion_response(state, results)
|
|
_tick_wounds(state, results)
|
|
return results
|
|
if not state.map.is_walkable(destination):
|
|
results.append(TurnResultType.new(player.id, &"blocked", player.cell, destination, &"", 0, "Your way is blocked."))
|
|
return results
|
|
var from: Vector2i = player.cell
|
|
player.cell = destination
|
|
results.append(TurnResultType.new(player.id, &"moved", from, destination, &"", 0, "You move to (%d, %d)." % [destination.x, destination.y]))
|
|
append_enemy_response(state, results)
|
|
_append_companion_response(state, results)
|
|
_tick_wounds(state, results)
|
|
return results
|
|
|
|
static func _share_companion_xp(state: GameState, outcome: Dictionary, defeated: ActorState) -> void:
|
|
if defeated.is_alive() or state.companion == null:
|
|
return
|
|
# This seam deliberately owns only neutral earned-XP allocation. It does
|
|
# not advance levels or invoke any progression code: that remains F1 work.
|
|
var reward := maxi(0, defeated.xp_reward)
|
|
if reward == 0:
|
|
return
|
|
var player_level := state.player.progression.level
|
|
var allocation := state.companion.share_xp(state.player, reward)
|
|
var levels_gained := state.player.progression.level - player_level
|
|
for descriptor: Dictionary in outcome.events:
|
|
if descriptor.kind == &"xp_granted":
|
|
descriptor.payload = {"xp": allocation.player_xp, "companion_xp": allocation.companion_xp}
|
|
if levels_gained > 0:
|
|
descriptor.payload["levels_gained"] = levels_gained
|
|
return
|
|
# `grant_xp=false` above prevents the legacy combat ledger mutation. Emit
|
|
# one equivalent journal descriptor after the neutral allocation instead.
|
|
var xp_payload := {"xp": allocation.player_xp, "companion_xp": allocation.companion_xp}
|
|
if levels_gained > 0:
|
|
xp_payload["levels_gained"] = levels_gained
|
|
outcome.events.append({
|
|
"kind": &"xp_granted",
|
|
"actor_id": state.player.id,
|
|
"target_id": defeated.id,
|
|
"cell": state.player.cell,
|
|
"payload": xp_payload,
|
|
})
|
|
|
|
static func _enemy_at(state: GameState, cell: Vector2i) -> ActorState:
|
|
for enemy: ActorState in state.enemies.values():
|
|
if enemy.cell == cell:
|
|
return enemy
|
|
return null
|
|
|
|
static func append_enemy_response(state: GameState, results: Array[TurnResult]) -> void:
|
|
# Dictionary iteration is not a gameplay ordering contract. Stable ID order
|
|
# preserves identical enemy events for the same seed and player action.
|
|
var enemy_ids: Array[StringName] = []
|
|
for enemy_id: StringName in state.enemies:
|
|
enemy_ids.append(enemy_id)
|
|
# StringName's comparison order is implementation-defined; compare the textual
|
|
# IDs so turn ordering remains deterministic across Godot runs and platforms.
|
|
enemy_ids.sort_custom(func(first: StringName, second: StringName) -> bool: return String(first) < String(second))
|
|
var occupied := _occupied_enemy_cells(state)
|
|
for enemy_id in enemy_ids:
|
|
var enemy: ActorState = state.enemies.get(enemy_id)
|
|
if enemy == null or not enemy.is_alive():
|
|
continue
|
|
var target := _enemy_target(state, enemy)
|
|
var decision: Dictionary = EnemyBehaviorsType.decide(enemy, target, state.map, occupied)
|
|
if decision.has("diagnostic"):
|
|
results.append(TurnResultType.new(
|
|
enemy.id,
|
|
&"enemy_behavior_unsupported",
|
|
enemy.cell,
|
|
enemy.cell,
|
|
&"",
|
|
0,
|
|
"%s holds: unsupported AI behavior '%s'." % [_actor_label(enemy.id), decision.behavior],
|
|
{"diagnostic": decision.diagnostic, "behavior": decision.behavior}
|
|
))
|
|
match decision.get("kind", &"idle"):
|
|
&"melee":
|
|
if target == state.player and _support_companion_intercepts(state, enemy, results):
|
|
continue
|
|
_append_combat_results(results, CombatEngineType.new().resolve_attack(enemy, target), enemy.cell, target.cell)
|
|
if target == state.player and not state.player.is_alive():
|
|
return
|
|
&"move":
|
|
var move_target: Vector2i = decision.get("to_cell", enemy.cell)
|
|
# The behavior contract has already rejected these cells; retain the
|
|
# resolver guard as the authoritative state mutation boundary.
|
|
if state.map.is_walkable(move_target) and move_target != target.cell and not occupied.has(move_target):
|
|
var from: Vector2i = enemy.cell
|
|
occupied.erase(from)
|
|
enemy.cell = move_target
|
|
occupied[move_target] = true
|
|
results.append(TurnResultType.new(enemy.id, &"enemy_moved", from, move_target, &"", 0, "%s moves." % _actor_label(enemy.id)))
|
|
|
|
static func _enemy_target(state: GameState, enemy: ActorState) -> ActorState:
|
|
var companion: CompanionState = state.companion
|
|
if companion == null or not companion.is_alive():
|
|
return state.player
|
|
var player_distance := enemy.cell.distance_squared_to(state.player.cell)
|
|
var companion_distance := enemy.cell.distance_squared_to(companion.actor.cell)
|
|
return companion.actor if companion_distance <= player_distance else state.player
|
|
|
|
static func _support_companion_intercepts(state: GameState, enemy: ActorState, results: Array[TurnResult]) -> bool:
|
|
var companion: CompanionState = state.companion
|
|
if companion == null or not companion.is_alive() or CompanionStateType.normalize_stance(companion.stance) != CompanionStateType.Stance.DEFEND_ME:
|
|
return false
|
|
if companion.actor.cell.distance_to(state.player.cell) > 1.5:
|
|
return false
|
|
var outcome := CombatEngineType.new().resolve_attack(enemy, companion.actor)
|
|
var start := results.size()
|
|
_append_combat_results(results, outcome, enemy.cell, companion.actor.cell)
|
|
var interception: TurnResult = results[start]
|
|
interception.kind = &"companion_intercepted"
|
|
interception.message = "Your companion intercepts the attack."
|
|
if not companion.is_alive():
|
|
var death: TurnResult = results[start + 1]
|
|
death.kind = &"companion_died"
|
|
death.message = "Your companion falls, but your run continues."
|
|
return true
|
|
|
|
static func _append_companion_response(state: GameState, results: Array[TurnResult]) -> void:
|
|
var companion: CompanionState = state.companion
|
|
if companion == null or not companion.is_alive():
|
|
return
|
|
var occupied := _occupied_enemy_cells(state)
|
|
occupied[companion.actor.cell] = true
|
|
var detected_cells := state.compute_visible_cells(state.map, state.player.cell, state.PLAYER_VISION_RADIUS)
|
|
var decision := CompanionBehaviorsType.decide(companion, state.player, state.enemies, state.map, occupied, detected_cells)
|
|
match decision.get("kind", &"idle"):
|
|
&"move":
|
|
var target: Vector2i = decision.get("to_cell", companion.actor.cell)
|
|
if state.map.is_walkable(target) and target != state.player.cell and not _occupied_enemy_cells(state).has(target):
|
|
var from := companion.actor.cell
|
|
companion.actor.cell = target
|
|
results.append(TurnResultType.new(companion.actor.id, &"companion_moved", from, target, &"", 0, "Your companion follows."))
|
|
&"melee":
|
|
var enemy: ActorState = state.enemies.get(StringName(decision.get("target_id", "")))
|
|
if enemy != null and enemy.is_alive() and companion.actor.cell.distance_to(enemy.cell) <= 1.5:
|
|
_append_combat_results(results, CombatEngineType.new().resolve_attack(companion.actor, enemy), companion.actor.cell, enemy.cell)
|
|
results.back().kind = &"companion_melee"
|
|
results.back().message = "%s attacks." % companion.name
|
|
if not enemy.is_alive():
|
|
state.enemies.erase(enemy.id)
|
|
&"heal_player":
|
|
var before := state.player.hit_points
|
|
state.player.hit_points = mini(state.player.effective_max_hit_points(), before + int(decision.get("amount", 0)))
|
|
var healed := state.player.hit_points - before
|
|
if healed > 0:
|
|
results.append(TurnResultType.new(companion.actor.id, &"companion_healed_player", companion.actor.cell, state.player.cell, state.player.id, -healed, "%s restores %d PV." % [companion.name, healed], {"healing": healed}))
|
|
|
|
static func _occupied_enemy_cells(state: GameState) -> Dictionary[Vector2i, bool]:
|
|
var occupied: Dictionary[Vector2i, bool] = {state.player.cell: true}
|
|
for enemy: ActorState in state.enemies.values():
|
|
if enemy.is_alive():
|
|
occupied[enemy.cell] = true
|
|
return occupied
|
|
|
|
static func _append_combat_results(results: Array[TurnResult], outcome: Dictionary, from_cell: Vector2i, to_cell: Vector2i) -> void:
|
|
# Keep the prototype TurnResult seam stable for existing action callers while
|
|
# carrying the authoritative C2 descriptors to Game's GameEvent journal.
|
|
var descriptors: Array = outcome.events
|
|
var first: Dictionary = descriptors[0]
|
|
var actor_id := StringName(first.actor_id)
|
|
var target_id := StringName(first.target_id)
|
|
var legacy_kind := &"melee" if actor_id == &"player" else &"enemy_melee"
|
|
var damage := int(outcome.damage)
|
|
var attack_method := String(first.payload.get("attack_method", "mains nues" if actor_id == &"player" else "attaque naturelle"))
|
|
var message := "You strike the goblin with %s for %d." % [attack_method, damage] if actor_id == &"player" else "%s hits you with %s for %d." % [_actor_label(actor_id), attack_method, damage]
|
|
results.append(TurnResultType.new(actor_id, legacy_kind, from_cell, to_cell, target_id, damage, message, {"domain_events": descriptors, "attack_method": attack_method}))
|
|
if not bool(outcome.hit):
|
|
results.back().message = "You miss the goblin with %s." % attack_method if actor_id == &"player" else "%s misses you with %s." % [_actor_label(actor_id), attack_method]
|
|
if not (results.back().payload.domain_events as Array).filter(func(event: Dictionary) -> bool: return event.kind == &"actor_died").is_empty():
|
|
var defeated := target_id if actor_id == &"player" else actor_id
|
|
results.append(TurnResultType.new(defeated, &"enemy_defeated", to_cell, to_cell, actor_id, 0, "%s is defeated." % _actor_label(defeated)))
|
|
|
|
static func _actor_label(actor_id: StringName) -> String:
|
|
return "You" if actor_id == &"player" else "The goblin"
|
|
|
|
static func _tick_wounds(state: GameState, results: Array[TurnResult]) -> void:
|
|
for actor: ActorState in [state.player] + state.enemies.values():
|
|
for slot in actor.wounds.tick():
|
|
results.append(TurnResultType.new(actor.id, &"wound_recovered", actor.cell, actor.cell, &"", 0, "%s's %s wound recovers." % [_actor_label(actor.id), slot], {"slot": String(slot)}))
|