Compare commits

...
35 changed files with 885 additions and 29 deletions
+51 -2
View File
@@ -1,21 +1,70 @@
class_name ActorState
extends RefCounted
const LimbWoundsType = preload("res://scripts/domain/combat/limb_wounds.gd")
var id: StringName
var cell: Vector2i
var hit_points: int
var max_hit_points: int
# Legacy basic damage remains the unarmed/default weapon damage.
var attack_damage: int
var content_id: StringName
func _init(new_id: StringName, new_cell: Vector2i, new_hit_points: int, new_attack_damage: int, new_content_id: StringName = &"") -> void:
# Data-backed combat stats. Defaults retain the pre-C2 prototype fixtures.
var accuracy := 30
var defense := 0
var armor := 0
var weapon_damage := 0
var damage_variance := 0
var penetration := 0
var crit_chance := 0.0
var crit_mult := 1.5
var block := 0
var stance: StringName = &"none"
var xp_reward := 0
var xp := 0
var loot_table: StringName = &""
var perception := 0
var wounds: LimbWounds
func _init(new_id: StringName, new_cell: Vector2i, new_hit_points: int, new_attack_damage: int, new_content_id: StringName = &"", combat_data: Dictionary = {}) -> void:
id = new_id
cell = new_cell
hit_points = new_hit_points
max_hit_points = new_hit_points
attack_damage = new_attack_damage
content_id = new_content_id if not new_content_id.is_empty() else new_id
accuracy = int(combat_data.get("accuracy", accuracy))
defense = int(combat_data.get("defense", defense))
armor = int(combat_data.get("armor", armor))
weapon_damage = int(combat_data.get("weapon_damage", combat_data.get("damage", attack_damage)))
damage_variance = int(combat_data.get("damage_variance", damage_variance))
penetration = int(combat_data.get("penetration", penetration))
crit_chance = float(combat_data.get("crit_chance", crit_chance))
crit_mult = float(combat_data.get("crit_mult", crit_mult))
block = int(combat_data.get("block", block))
stance = StringName(combat_data.get("stance", stance))
xp_reward = int(combat_data.get("xp_reward", xp_reward))
xp = int(combat_data.get("xp", xp))
loot_table = StringName(combat_data.get("loot_table", loot_table))
perception = int(combat_data.get("perception", combat_data.get("per", perception)))
wounds = LimbWoundsType.new()
func is_alive() -> bool:
return hit_points > 0
func take_damage(amount: int) -> int:
var actual := mini(maxi(0, amount), hit_points)
hit_points -= actual
return actual
func wound_severity(slot: StringName) -> int:
return wounds.severity_at(slot)
func effective_perception() -> int:
return perception - wounds.severity_at(&"head")
func effective_max_hit_points() -> int:
var reduction := int(float(max_hit_points) * 0.05 * wounds.severity_at(&"torso"))
return maxi(1, max_hit_points - reduction)
+65
View File
@@ -0,0 +1,65 @@
class_name CombatEngine
extends RefCounted
# Direct port of the ASCII melee probabilities, reduced to C2's domain inputs.
const MIN_HIT_CHANCE := 0.05
const MAX_HIT_CHANCE := 0.97
const ARM_WOUND_PENALTY := 6
const LEG_WOUND_PENALTY := 6
const DODGE_STANCE_BONUS := 18
const BLOCK_BASE_REDUCTION := 2
var _rolls: Array[float] = []
func _init(scripted_rolls: Array[float] = []) -> void:
_rolls.assign(scripted_rolls)
func resolve_attack(attacker: ActorState, defender: ActorState) -> Dictionary:
var events: Array[Dictionary] = [_event(&"attack_started", attacker, defender, {"stance": String(attacker.stance)})]
var attack_score := attacker.accuracy - attacker.wound_severity(&"arms") * ARM_WOUND_PENALTY
var defense_score := defender.defense * 3 - defender.wound_severity(&"legs") * LEG_WOUND_PENALTY
if defender.stance == &"dodge":
defense_score += DODGE_STANCE_BONUS
var hit_chance := clampf(0.55 + float(attack_score - defense_score) * 0.02, MIN_HIT_CHANCE, MAX_HIT_CHANCE)
if _next_roll() > hit_chance:
return {"hit": false, "critical": false, "damage": 0, "events": events}
events.append(_event(&"attack_hit", attacker, defender, {"hit_chance": hit_chance}))
var critical := _next_roll() < attacker.crit_chance
var base_damage := attacker.weapon_damage + _damage_variance(attacker.damage_variance)
var damage: int
# ASCII player attacks use full target armor and weapon penetration.
# Enemy attacks apply block first, then half of the target's armor.
if attacker.id == &"player":
damage = maxi(1, base_damage - maxi(0, defender.armor - attacker.penetration))
if defender.stance == &"block":
damage = maxi(0, damage - BLOCK_BASE_REDUCTION - defender.block / 2)
else:
damage = base_damage
if defender.stance == &"block":
damage = maxi(0, damage - BLOCK_BASE_REDUCTION - defender.block / 2)
if damage > 0:
damage = maxi(1, damage - defender.armor / 2)
if critical:
damage = int(float(damage) * attacker.crit_mult)
var actual := defender.take_damage(damage)
events.append(_event(&"damage_dealt", attacker, defender, {"damage": actual, "critical": critical}))
if not defender.is_alive():
events.append(_event(&"actor_died", defender, attacker, {"killed_by": String(attacker.id)}))
attacker.xp += defender.xp_reward
events.append(_event(&"xp_granted", attacker, defender, {"xp": defender.xp_reward}))
events.append(_event(&"loot_dropped", defender, attacker, {"loot_table": String(defender.loot_table)}))
return {"hit": true, "critical": critical, "damage": actual, "events": events}
func _damage_variance(variance: int) -> int:
if variance <= 0:
return 0
return int(_next_roll() * float(variance + 1))
func _next_roll() -> float:
if _rolls.is_empty():
return 0.0
return clampf(_rolls.pop_front(), 0.0, 0.999999)
func _event(kind: StringName, actor: ActorState, target: ActorState, payload: Dictionary) -> Dictionary:
return {"kind": kind, "actor_id": actor.id, "target_id": target.id, "cell": actor.cell, "payload": payload}
@@ -0,0 +1 @@
uid://dcxbc8mscls6t
+114
View File
@@ -0,0 +1,114 @@
class_name ConditionEngine
extends RefCounted
const StatusEffectType = preload("res://scripts/domain/combat/status_effect.gd")
var diagnostics := PackedStringArray()
func evaluate(condition: Dictionary, context: Dictionary) -> bool:
if condition.is_empty():
return true
var kind := StringName(condition.get("type", ""))
var player: PlayerState = context.get("player")
if player == null:
_diagnose("Condition requires a player: %s" % kind)
return false
match kind:
&"skill":
return player.effective_skill(StringName(condition.get("skill", ""))) >= int(condition.get("min", 0))
&"attr":
return player.effective_attribute(StringName(condition.get("attr", ""))) >= int(condition.get("min", 0))
&"level":
return player.level >= int(condition.get("min", 1))
&"flag":
return player.flags.get(StringName(condition.get("flag", "")), false) == condition.get("equals", true)
&"reputation":
return player.reputation.get(StringName(condition.get("faction", "")), 0) >= int(condition.get("min", 0))
&"archetype":
if condition.has("is_set"):
return (not player.archetype.is_empty()) == bool(condition["is_set"])
return player.archetype == StringName(condition.get("value", ""))
&"has_race":
if condition.has("is_set"):
return (not player.race.is_empty()) == bool(condition["is_set"])
return player.race == StringName(condition.get("value", ""))
&"payload_flag":
return _payload(context).get(StringName(condition.get("key", "")), false) == condition.get("equals", true)
&"payload_equals":
return _payload(context).get(StringName(condition.get("key", ""))) == condition.get("value")
&"payload_min":
return float(_payload(context).get(StringName(condition.get("key", "")), 0)) >= float(condition.get("min", 0))
&"payload_max":
return float(_payload(context).get(StringName(condition.get("key", "")), 0)) <= float(condition.get("max", 0))
&"payload_contains":
var value: Variant = _payload(context).get(StringName(condition.get("key", "")))
return value is Array and value.has(condition.get("value")) or value is String and String(value).contains(String(condition.get("value", "")))
&"not":
return not evaluate(condition.get("cond", {}), context)
&"all":
for nested: Dictionary in condition.get("conds", []):
if not evaluate(nested, context):
return false
return true
&"any":
for nested: Dictionary in condition.get("conds", []):
if evaluate(nested, context):
return true
return false
_:
_diagnose("Unknown condition type: %s" % kind)
return false
func evaluate_all(conditions: Array, context: Dictionary) -> bool:
for condition: Dictionary in conditions:
if not evaluate(condition, context):
return false
return true
func apply_effects(effects: Array, context: Dictionary) -> Array[Dictionary]:
var applications: Array[Dictionary] = []
for effect: Dictionary in effects:
applications.append(apply_effect(effect, context))
return applications
func apply_effect(effect: Dictionary, context: Dictionary) -> Dictionary:
var player: PlayerState = context.get("player")
var kind := StringName(effect.get("type", ""))
if player == null:
_diagnose("Effect requires a player: %s" % kind)
return {"applied": false, "type": kind}
match kind:
&"set_flag":
player.flags[StringName(effect.get("flag", ""))] = effect.get("value", true)
&"add_attr":
player.modify_attribute(StringName(effect.get("attr", "")), int(effect.get("amount", 1)))
&"add_skill":
player.modify_skill(StringName(effect.get("skill", "")), int(effect.get("amount", 1)))
&"apply_status", &"inflict_status":
player.apply_status(StatusEffectType.new(StringName(effect.get("status", "")), int(effect.get("duration", -1)), int(effect.get("magnitude", 0))))
&"cure_status":
player.clear_status(StringName(effect.get("status", "")))
&"heal":
player.hit_points = mini(player.max_hp(), player.hit_points + int(effect.get("amount", 0)))
&"heal_pct":
player.hit_points = mini(player.max_hp(), player.hit_points + int(player.max_hp() * float(effect.get("pct", 0.0))))
&"damage":
player.hit_points = maxi(0, player.hit_points - int(effect.get("amount", 0)))
&"add_rep":
var faction := StringName(effect.get("faction", ""))
player.reputation[faction] = player.reputation.get(faction, 0) + int(effect.get("amount", 0))
&"message", &"give_item", &"remove_item", &"learn_spell", &"learn_random_spell", &"complete_objective", &"endanger_tourist", &"augment_companion", &"join_party", &"tourist_selfie":
# Later content slices own the corresponding inventory, quest, NPC, and spell mutations.
pass
_:
_diagnose("Unknown effect type: %s" % kind)
return {"applied": false, "type": kind}
return {"applied": true, "type": kind}
func _payload(context: Dictionary) -> Dictionary:
var payload: Variant = context.get("payload", {})
return payload if payload is Dictionary else {}
func _diagnose(message: String) -> void:
# Keep unsupported content visible to callers without silently granting access.
diagnostics.append(message)
@@ -0,0 +1 @@
uid://u44apfxu8ud4
+61
View File
@@ -0,0 +1,61 @@
class_name LimbWounds
extends RefCounted
# ASCII parity: one recoverable wound per body slot, with severity 1..3.
const SLOTS: Array[StringName] = [&"head", &"torso", &"arms", &"legs"]
const DEFAULT_DURATION := 8
const MAX_SEVERITY := 3
var _wounds: Dictionary[StringName, Dictionary] = {}
func apply(slot: StringName, severity: int = 1, duration: int = DEFAULT_DURATION, source: String = "") -> void:
if not SLOTS.has(slot):
return
var existing: Dictionary = _wounds.get(slot, {})
_wounds[slot] = {
"severity": maxi(int(existing.get("severity", 0)), clampi(severity, 1, MAX_SEVERITY)),
"duration": maxi(int(existing.get("duration", 0)), maxi(1, duration)),
"source": source if not source.is_empty() else String(existing.get("source", "")),
}
func severity_at(slot: StringName) -> int:
return int(_wounds.get(slot, {}).get("severity", 0))
func duration_at(slot: StringName) -> int:
return int(_wounds.get(slot, {}).get("duration", 0))
func source_at(slot: StringName) -> String:
return String(_wounds.get(slot, {}).get("source", ""))
func tick(recovery_multiplier: float = 1.0) -> Array[StringName]:
var recovered: Array[StringName] = []
var step := 2 if recovery_multiplier >= 2.0 else 1
for slot in _wounds.keys():
var wound: Dictionary = _wounds[slot]
wound["duration"] = int(wound["duration"]) - step
if int(wound["duration"]) <= 0:
_wounds.erase(slot)
recovered.append(slot)
else:
_wounds[slot] = wound
return recovered
func active_wounds() -> Array[Dictionary]:
var active: Array[Dictionary] = []
for slot in SLOTS:
if _wounds.has(slot):
var wound: Dictionary = _wounds[slot].duplicate(true)
wound["slot"] = slot
active.append(wound)
return active
func to_dictionary() -> Dictionary:
return _wounds.duplicate(true)
static func from_dictionary(data: Dictionary = {}) -> LimbWounds:
var wounds := LimbWounds.new()
for raw_slot in data:
var slot := StringName(raw_slot)
var wound: Dictionary = data[raw_slot]
wounds.apply(slot, int(wound.get("severity", 1)), int(wound.get("duration", DEFAULT_DURATION)), String(wound.get("source", "")))
return wounds
+1
View File
@@ -0,0 +1 @@
uid://cox0vsuwmj701
+55
View File
@@ -0,0 +1,55 @@
class_name StatusEffect
extends RefCounted
const DEFINITIONS := {
&"poison": {"kind": &"dot", "damage_per_turn": 3, "default_duration": 4},
&"bleeding": {"kind": &"dot", "damage_per_turn": 2, "default_duration": 3},
&"burning": {"kind": &"dot", "damage_per_turn": 5, "default_duration": 2},
&"sepsis": {"kind": &"dot", "damage_per_turn": 4, "default_duration": 10},
&"stunned": {"kind": &"control", "skip_turn": true, "default_duration": 1},
&"feared": {"kind": &"control", "forced_flee": true, "default_duration": 2},
&"confused": {"kind": &"control", "random_action": true, "default_duration": 2},
&"wounded": {"kind": &"debuff", "attribute": &"str_", "amount": -1, "default_duration": 6},
&"fatigued": {"kind": &"debuff", "attribute": &"agi", "amount": -1, "default_duration": 5},
&"strengthened": {"kind": &"buff", "attribute": &"str_", "amount": 2, "default_duration": 5},
&"regenerating": {"kind": &"buff", "heal_per_turn": 4, "default_duration": 4},
}
var key: StringName
var duration: int
var magnitude: int
var source: StringName
func _init(new_key: StringName, new_duration: int = -1, new_magnitude: int = 0, new_source: StringName = &"") -> void:
key = new_key
var definition: Dictionary = DEFINITIONS.get(key, {})
duration = new_duration if new_duration >= 0 else int(definition.get("default_duration", 3))
magnitude = new_magnitude if new_magnitude != 0 else _default_magnitude(definition)
source = new_source
func hit_point_delta() -> int:
var definition: Dictionary = DEFINITIONS.get(key, {})
if definition.get("kind") == &"dot":
return -abs(magnitude)
if definition.has("heal_per_turn"):
return abs(magnitude)
return 0
func attribute_modifier() -> Dictionary[StringName, int]:
var definition: Dictionary = DEFINITIONS.get(key, {})
if not definition.has("attribute"):
return {}
return {StringName(definition["attribute"]): magnitude}
func is_expired() -> bool:
return duration <= 0
func to_dictionary() -> Dictionary:
return {"key": key, "duration": duration, "magnitude": magnitude, "source": source}
static func _default_magnitude(definition: Dictionary) -> int:
if definition.has("damage_per_turn"):
return int(definition["damage_per_turn"])
if definition.has("heal_per_turn"):
return int(definition["heal_per_turn"])
return int(definition.get("amount", 0))
@@ -0,0 +1 @@
uid://cm5yjrbld0hq6
+44
View File
@@ -0,0 +1,44 @@
class_name Attributes
extends RefCounted
const ATTRIBUTE_KEYS: Array[StringName] = [&"str_", &"agi", &"end", &"per", &"int_", &"wil", &"cha", &"luc"]
const SKILL_KEYS: Array[StringName] = [
&"melee", &"ranged", &"heavy", &"dodge", &"block", &"medicine", &"lockpicking",
&"traps", &"survival", &"engineering", &"persuasion", &"intimidation", &"bargaining",
&"observation", &"stealth", &"occultism", &"regeneration",
]
const BASE_ATTRIBUTE := 4
const BASE_SKILL := 1
var values: Dictionary[StringName, int] = {}
func _init(initial_values: Dictionary = {}) -> void:
for key in ATTRIBUTE_KEYS:
values[key] = int(initial_values.get(key, BASE_ATTRIBUTE))
func get_attribute(key: StringName) -> int:
return values.get(key, BASE_ATTRIBUTE)
func set_attribute(key: StringName, value: int) -> void:
if not ATTRIBUTE_KEYS.has(key):
push_warning("Unknown attribute key: %s" % key)
return
values[key] = value
func modify_attribute(key: StringName, delta: int) -> void:
set_attribute(key, get_attribute(key) + delta)
func to_dictionary() -> Dictionary[StringName, int]:
return values.duplicate()
static func default_skills(initial_values: Dictionary = {}) -> Dictionary[StringName, int]:
var skills: Dictionary[StringName, int] = {}
for key in SKILL_KEYS:
skills[key] = int(initial_values.get(key, BASE_SKILL))
return skills
static func is_attribute(key: StringName) -> bool:
return ATTRIBUTE_KEYS.has(key)
static func is_skill(key: StringName) -> bool:
return SKILL_KEYS.has(key)
@@ -0,0 +1 @@
uid://dyh5ir5sikdxk
+107
View File
@@ -0,0 +1,107 @@
class_name PlayerState
extends RefCounted
const AttributesType = preload("res://scripts/domain/entities/attributes.gd")
const StatusEffectType = preload("res://scripts/domain/combat/status_effect.gd")
const ActorStateType = preload("res://scripts/domain/actor_state.gd")
var id: StringName = &"player"
var cell := Vector2i.ZERO
var attributes: Attributes
var skills: Dictionary[StringName, int]
var level := 1
var hit_points := 1
var focus := 0
var equipment_attribute_modifiers: Dictionary[StringName, int] = {}
var equipment_skill_modifiers: Dictionary[StringName, int] = {}
var flags: Dictionary[StringName, Variant] = {}
var reputation: Dictionary[StringName, int] = {}
var archetype: StringName = &""
var race: StringName = &""
var status_effects: Array[StatusEffect] = []
func _init(data: Dictionary = {}) -> void:
id = StringName(data.get("id", id))
cell = data.get("cell", cell)
attributes = _attributes_from(data.get("attributes", {}))
skills = AttributesType.default_skills(data.get("skills", {}))
level = maxi(1, int(data.get("level", level)))
for flag in data.get("flags", {}):
flags[StringName(flag)] = data.flags[flag]
for faction in data.get("reputation", {}):
reputation[StringName(faction)] = int(data.reputation[faction])
archetype = StringName(data.get("archetype", archetype))
race = StringName(data.get("race", race))
hit_points = clampi(int(data.get("hit_points", max_hp())), 0, max_hp())
focus = clampi(int(data.get("focus", max_focus())), 0, max_focus())
func max_hp() -> int:
return maxi(1, 50 + attributes.get_attribute(&"end") * 6 + (level - 1) * 8)
func max_focus() -> int:
return maxi(0, 15 + attributes.get_attribute(&"wil") * 5 + (level - 1) * 3)
func effective_attribute(key: StringName) -> int:
var value: int = attributes.get_attribute(key) + equipment_attribute_modifiers.get(key, 0)
for effect in status_effects:
value += effect.attribute_modifier().get(key, 0)
return value
func effective_skill(key: StringName) -> int:
return skills.get(key, 0) + equipment_skill_modifiers.get(key, 0)
func modify_attribute(key: StringName, amount: int) -> void:
attributes.modify_attribute(key, amount)
_normalize_resources()
func modify_skill(key: StringName, amount: int) -> void:
if not AttributesType.is_skill(key):
push_warning("Unknown skill key: %s" % key)
return
skills[key] = maxi(0, skills.get(key, 0) + amount)
func apply_status(effect: StatusEffect) -> void:
var existing := _status_for(effect.key)
if existing == null:
status_effects.append(effect)
return
existing.duration = maxi(existing.duration, effect.duration)
if abs(effect.magnitude) > abs(existing.magnitude):
existing.magnitude = effect.magnitude
func has_status(key: StringName) -> bool:
return _status_for(key) != null
func clear_status(key: StringName) -> void:
status_effects = status_effects.filter(func(effect: StatusEffect) -> bool: return effect.key != key)
func tick_status_effects() -> Array[Dictionary]:
var ticks: Array[Dictionary] = []
for effect in status_effects:
var delta := effect.hit_point_delta()
if delta != 0:
hit_points = clampi(hit_points + delta, 0, max_hp())
ticks.append({"status": effect.key, "hit_point_delta": delta, "remaining_duration": effect.duration - 1})
effect.duration -= 1
status_effects = status_effects.filter(func(effect: StatusEffect) -> bool: return not effect.is_expired())
return ticks
func as_actor_state(attack_damage: int) -> ActorState:
var actor := ActorStateType.new(id, cell, max_hp(), attack_damage)
actor.hit_points = hit_points
return actor
func _normalize_resources() -> void:
hit_points = clampi(hit_points, 0, max_hp())
focus = clampi(focus, 0, max_focus())
func _status_for(key: StringName) -> StatusEffect:
for effect in status_effects:
if effect.key == key:
return effect
return null
static func _attributes_from(value: Variant) -> Attributes:
if value is Attributes:
return value
return AttributesType.new(value if value is Dictionary else {})
@@ -0,0 +1 @@
uid://b8y518h1wldal
+30 -5
View File
@@ -2,6 +2,7 @@ class_name TurnResolver
extends RefCounted
const TurnResultType = preload("res://scripts/domain/turn_result.gd")
const CombatEngineType = preload("res://scripts/domain/combat/combat_engine.gd")
static func resolve_player_action(state: GameState, direction: Vector2i) -> Array[TurnResult]:
var results: Array[TurnResult] = []
@@ -11,13 +12,12 @@ static func resolve_player_action(state: GameState, direction: Vector2i) -> Arra
var destination: Vector2i = player.cell + direction
var enemy: ActorState = _enemy_at(state, destination)
if enemy != null:
enemy.hit_points -= player.attack_damage
results.append(TurnResultType.new(player.id, &"melee", player.cell, destination, enemy.id, player.attack_damage, "You strike the goblin for %d." % player.attack_damage))
_append_combat_results(results, CombatEngineType.new().resolve_attack(player, enemy), player.cell, destination)
if not enemy.is_alive():
state.enemies.erase(enemy.id)
results.append(TurnResultType.new(enemy.id, &"enemy_defeated", enemy.cell, enemy.cell, player.id, 0, "The goblin is defeated."))
else:
append_enemy_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."))
@@ -26,6 +26,7 @@ static func resolve_player_action(state: GameState, direction: Vector2i) -> Arra
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)
_tick_wounds(state, results)
return results
static func _enemy_at(state: GameState, cell: Vector2i) -> ActorState:
@@ -38,8 +39,7 @@ static func append_enemy_response(state: GameState, results: Array[TurnResult])
for enemy: ActorState in state.enemies.values():
var delta: Vector2i = state.player.cell - enemy.cell
if abs(delta.x) + abs(delta.y) == 1:
state.player.hit_points = max(0, state.player.hit_points - enemy.attack_damage)
results.append(TurnResultType.new(enemy.id, &"enemy_melee", enemy.cell, state.player.cell, state.player.id, enemy.attack_damage, "The goblin hits you for %d." % enemy.attack_damage))
_append_combat_results(results, CombatEngineType.new().resolve_attack(enemy, state.player), enemy.cell, state.player.cell)
return
var step: Vector2i = Vector2i(int(sign(delta.x)), int(sign(delta.y)))
if abs(delta.x) >= abs(delta.y):
@@ -51,3 +51,28 @@ static func append_enemy_response(state: GameState, results: Array[TurnResult])
var from: Vector2i = enemy.cell
enemy.cell = target
results.append(TurnResultType.new(enemy.id, &"enemy_moved", from, target, &"", 0, "The goblin moves."))
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 message := "You strike the goblin for %d." % damage if actor_id == &"player" else "The goblin hits you for %d." % damage
results.append(TurnResultType.new(actor_id, legacy_kind, from_cell, to_cell, target_id, damage, message, {"domain_events": descriptors}))
if not bool(outcome.hit):
results.back().message = "You miss the goblin." if actor_id == &"player" else "The goblin misses you."
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, "The goblin is 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)}))
+5 -1
View File
@@ -8,8 +8,11 @@ var to_cell: Vector2i
var target_id: StringName
var damage: int
var message: String
# Optional domain event descriptors. Game converts these to the authoritative
# GameEvent journal without allowing presentation to mutate combat state.
var payload: Dictionary
func _init(new_actor_id: StringName, new_kind: StringName, from: Vector2i, to: Vector2i, target: StringName = &"", amount: int = 0, text: String = "") -> void:
func _init(new_actor_id: StringName, new_kind: StringName, from: Vector2i, to: Vector2i, target: StringName = &"", amount: int = 0, text: String = "", new_payload: Dictionary = {}) -> void:
actor_id = new_actor_id
kind = new_kind
from_cell = from
@@ -17,3 +20,4 @@ func _init(new_actor_id: StringName, new_kind: StringName, from: Vector2i, to: V
target_id = target
damage = amount
message = text
payload = new_payload.duplicate(true)
+1 -1
View File
@@ -46,7 +46,7 @@ static func _spawn_enemy(floor: FloorState, catalog: ContentCatalog, room: RoomS
var definition: Dictionary = definitions[rng.next_int() % definitions.size()]
var content_id := StringName(definition["id"])
var actor_id := StringName("%s_%02d" % [content_id, serial])
floor.enemies[actor_id] = ActorStateType.new(actor_id, cell, int(definition.get("hp", 1)), int(definition.get("attack", 0)), content_id)
floor.enemies[actor_id] = ActorStateType.new(actor_id, cell, int(definition.get("hp", 1)), int(definition.get("attack", 0)), content_id, definition)
occupied[cell] = true
return serial + 1
+18 -1
View File
@@ -118,6 +118,21 @@ static func _actor_to_dict(actor: ActorState) -> Dictionary:
"max_hit_points": actor.max_hit_points,
"attack_damage": actor.attack_damage,
"content_id": String(actor.content_id),
"accuracy": actor.accuracy,
"defense": actor.defense,
"armor": actor.armor,
"weapon_damage": actor.weapon_damage,
"damage_variance": actor.damage_variance,
"penetration": actor.penetration,
"crit_chance": actor.crit_chance,
"crit_mult": actor.crit_mult,
"block": actor.block,
"stance": String(actor.stance),
"xp_reward": actor.xp_reward,
"xp": actor.xp,
"loot_table": String(actor.loot_table),
"perception": actor.perception,
"wounds": actor.wounds.to_dictionary(),
}
static func _actor_from_dict(data: Dictionary) -> ActorState:
@@ -126,9 +141,11 @@ static func _actor_from_dict(data: Dictionary) -> ActorState:
_array_to_cell(data.get("cell", [0, 0])),
int(data.get("max_hit_points", data.get("hit_points", 0))),
int(data.get("attack_damage", 0)),
StringName(data.get("content_id", data.get("id", "")))
StringName(data.get("content_id", data.get("id", ""))),
data
)
actor.hit_points = int(data.get("hit_points", actor.max_hit_points))
actor.wounds = LimbWounds.from_dictionary(data.get("wounds", {}))
return actor
static func _cell_to_array(cell: Vector2i) -> Array[int]:
+35 -16
View File
@@ -273,7 +273,15 @@ func _resolve_player_action(results: Array[TurnResult], sync_camera: bool) -> Ar
_sync_camera_to_player()
_dispatch_audio(results)
var events := _record_events(results)
event_presenter.enqueue(events)
# C2 journals compatibility events, but presentation consumes the
# authoritative structured combat stream exactly once so impacts are not
# duplicated by legacy melee results.
var presentation_events := events
if events.any(func(event: GameEvent) -> bool: return event.kind == &"attack_started"):
presentation_events = events.filter(func(event: GameEvent) -> bool:
return event.kind not in [&"melee", &"enemy_melee", &"enemy_defeated"]
)
event_presenter.enqueue(presentation_events)
event_presenter.present_next()
hud.refresh(game_state, run_state.recent_events(run_state.log.size()), run_state)
return results
@@ -283,24 +291,33 @@ func _record_events(results: Array[TurnResult]) -> Array[GameEvent]:
if results.is_empty():
return events
var turn := run_state.next_turn()
# Structured C2 combat events are journaled before the legacy TurnResult
# compatibility events. This preserves old callers' `recent_events(2)` seam.
for result in results:
events.append(GameEventType.new(
turn,
run_state.current_floor,
result.kind,
{
"from_cell": result.from_cell,
"to_cell": result.to_cell,
"damage": result.damage,
},
result.message,
result.actor_id,
result.target_id,
result.to_cell
))
for descriptor: Dictionary in result.payload.get("domain_events", []):
var payload := {"from_cell": result.from_cell, "to_cell": result.to_cell, "damage": int(descriptor.payload.get("damage", 0))}
payload.merge(descriptor.payload, true)
var event_message := _combat_event_message(descriptor.kind, descriptor.actor_id, int(payload.damage))
if descriptor.kind == &"damage_dealt":
event_message = result.message
events.append(GameEventType.new(turn, run_state.current_floor, descriptor.kind, payload, event_message, descriptor.actor_id, descriptor.target_id, descriptor.cell))
for result in results:
var payload := {"from_cell": result.from_cell, "to_cell": result.to_cell, "damage": result.damage}
payload.merge(result.payload, true)
events.append(GameEventType.new(turn, run_state.current_floor, result.kind, payload, result.message, result.actor_id, result.target_id, result.to_cell))
run_state.append_events(events)
return events
func _combat_event_message(kind: StringName, actor_id: StringName, damage: int) -> String:
match kind:
&"attack_started": return "%s attacks." % ("You" if actor_id == &"player" else "The goblin")
&"attack_hit": return "%s hits." % ("You" if actor_id == &"player" else "The goblin")
&"damage_dealt": return "%s deals %d damage." % [("You" if actor_id == &"player" else "The goblin"), damage]
&"actor_died": return "The goblin is defeated." if actor_id != &"player" else "You are defeated."
&"xp_granted": return "You gain experience."
&"loot_dropped": return "Loot dropped."
return ""
func _dispatch_audio(results: Array[TurnResult]) -> void:
for result in results:
match result.kind:
@@ -314,7 +331,9 @@ func _dispatch_audio(results: Array[TurnResult]) -> void:
audio_library.play_sfx("death")
func _on_event_presented(event: GameEvent) -> void:
if event.kind == &"enemy_defeated":
if event.kind == &"actor_died" and event.actor_id != &"player":
# The structured actor_died event completes before subsequent XP/loot
# no-ops, so removal retains the A3 180 ms death-fade contract.
_sync_enemy_views()
func _get_actor_view(actor_id: StringName) -> ActorView:
+1 -1
View File
@@ -50,7 +50,7 @@ func begin(event: GameEvent, actor_lookup: Callable) -> void:
await _show_movement(event, actor_lookup)
&"melee", &"enemy_melee", &"damage_dealt":
await _show_impact(event, actor_lookup)
&"enemy_defeated":
&"enemy_defeated", &"actor_died":
await _show_death(event, actor_lookup)
# Existing and future domain events are intentionally presentation no-ops.
_:
@@ -0,0 +1,19 @@
extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
func test_current_game_event_and_hud_remain_available_with_attributes_domain_loaded() -> void:
var game := GameScene.instantiate() as Game
add_child_autofree(game)
await get_tree().process_frame
var state := game.get_game_state()
state.enemies[&"goblin"].cell = state.player.cell + Vector2i.RIGHT
game.submit_test_action(Vector2i.RIGHT)
await get_tree().process_frame
var hud := game.get_node("HUD") as GameHud
assert_eq(state.enemies[&"goblin"].hit_points, 3)
assert_true(game.get_run_state().recent_events(2).any(func(event: GameEvent) -> bool: return event.kind == &"melee"))
assert_true(hud.player_health_label.text.contains("Player HP:"))
assert_true(hud.event_log.get_parsed_text().contains("You strike the goblin"))
@@ -0,0 +1 @@
uid://cglna5tkvvmyo
+2 -2
View File
@@ -11,7 +11,7 @@ func test_real_game_scene_changes_domain_hp_immediately_and_reports_one_hit_feed
var presenter := game.get_node("EventPresenter") as EventPresenter
var observed: Array[GameEvent] = []
presenter.event_presented.connect(func(event: GameEvent) -> void:
if event.kind == &"melee":
if event.kind == &"damage_dealt":
observed.append(event)
)
@@ -21,7 +21,7 @@ func test_real_game_scene_changes_domain_hp_immediately_and_reports_one_hit_feed
await _wait_until_hit_presented(observed)
assert_eq(observed.size(), 1)
assert_eq(observed[0].payload["damage"], 3)
assert_eq(game.get_run_state().recent_events(2).filter(func(event: GameEvent) -> bool: return event.kind == &"melee").size(), 1)
assert_eq(game.get_run_state().recent_events(3).filter(func(event: GameEvent) -> bool: return event.kind == &"damage_dealt").size(), 1)
func _wait_until_hit_presented(observed: Array[GameEvent]) -> void:
var deadline := Time.get_ticks_msec() + 1500
+34
View File
@@ -0,0 +1,34 @@
extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
func test_real_game_resolves_damage_and_death_before_a3_feedback_and_journals_structured_events() -> void:
var game := GameScene.instantiate() as Game
add_child_autofree(game)
await get_tree().process_frame
var state := game.get_game_state()
var enemy: ActorState = state.enemies[&"goblin"]
enemy.cell = state.player.cell + Vector2i.RIGHT
enemy.hit_points = 3
enemy.max_hit_points = 3
var presenter := game.get_node("EventPresenter") as EventPresenter
var observed: Array[GameEvent] = []
presenter.event_presented.connect(func(event: GameEvent) -> void:
if event.kind in [&"attack_started", &"attack_hit", &"damage_dealt", &"actor_died", &"xp_granted", &"loot_dropped"]:
observed.append(event)
)
game.submit_test_action(Vector2i.RIGHT)
assert_false(state.enemies.has(&"goblin")) # domain result is synchronous
var recent := game.get_run_state().recent_events(8).filter(func(event: GameEvent) -> bool: return event.kind in [&"attack_started", &"attack_hit", &"damage_dealt", &"actor_died", &"xp_granted", &"loot_dropped"])
assert_eq(recent.map(func(event: GameEvent) -> StringName: return event.kind), [&"attack_started", &"attack_hit", &"damage_dealt", &"actor_died", &"xp_granted", &"loot_dropped"])
assert_eq(recent[2].payload.damage, 3)
await _wait_for_feedback(observed)
assert_eq(observed.map(func(event: GameEvent) -> StringName: return event.kind), [&"attack_started", &"attack_hit", &"damage_dealt", &"actor_died", &"xp_granted", &"loot_dropped"])
assert_string_contains(game.get_node("HUD/EventLog").get_parsed_text(), "defeated")
func _wait_for_feedback(observed: Array[GameEvent]) -> void:
var deadline := Time.get_ticks_msec() + 2500
while observed.size() < 6 and Time.get_ticks_msec() < deadline:
await get_tree().process_frame
+1
View File
@@ -0,0 +1 @@
uid://bry3wa5554uyt
@@ -0,0 +1,21 @@
extends GutTest
const Attributes = preload("res://scripts/domain/entities/attributes.gd")
const PlayerState = preload("res://scripts/domain/entities/player_state.gd")
const ActorState = preload("res://scripts/domain/actor_state.gd")
const GameState = preload("res://scripts/domain/game_state.gd")
const GridMapState = preload("res://scripts/domain/grid_map_state.gd")
func test_player_state_adapter_preserves_actor_and_game_state_contract() -> void:
var player := PlayerState.new({"id": &"player", "cell": Vector2i(2, 2), "attributes": Attributes.new({"end": 5})})
var actor := player.as_actor_state(3)
var map := GridMapState.new(Vector2i(5, 5), GridMapState.CellKind.FLOOR)
var state := GameState.new(map, actor, {})
assert_true(actor is ActorState)
assert_eq(actor.id, &"player")
assert_eq(actor.cell, Vector2i(2, 2))
assert_eq(actor.hit_points, player.hit_points)
assert_eq(actor.max_hit_points, player.max_hp())
assert_eq(actor.attack_damage, 3)
assert_eq(state.player, actor)
@@ -0,0 +1 @@
uid://1rgi8tkrgnlu
+48
View File
@@ -0,0 +1,48 @@
extends GutTest
const AttributesType = preload("res://scripts/domain/entities/attributes.gd")
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
const StatusEffectType = preload("res://scripts/domain/combat/status_effect.gd")
const ConditionEngineType = preload("res://scripts/domain/combat/condition_engine.gd")
func test_data_backed_primary_attributes_skills_and_derived_resources() -> void:
var attributes := AttributesType.new({"end": 6, "wil": 7, "str_": 5})
var player := PlayerStateType.new({"attributes": attributes, "skills": {"traps": 4}, "level": 3})
assert_eq(player.max_hp(), 102)
assert_eq(player.max_focus(), 56)
assert_eq(player.effective_attribute(&"str_"), 5)
assert_eq(player.effective_skill(&"traps"), 4)
func test_effective_attributes_combine_equipment_and_active_status_modifiers() -> void:
var player := PlayerStateType.new({"attributes": {"str_": 4}})
player.equipment_attribute_modifiers = {&"str_": 3}
player.apply_status(StatusEffectType.new(&"wounded", 2))
player.apply_status(StatusEffectType.new(&"strengthened", 2))
assert_eq(player.effective_attribute(&"str_"), 8)
func test_condition_engine_evaluates_content_conditions_and_reports_unknown_conditions() -> void:
var player := PlayerStateType.new({"attributes": {"per": 6}, "skills": {"traps": 8}, "level": 4})
player.flags[&"terminal_open"] = true
var engine := ConditionEngineType.new()
var context := {"player": player, "payload": {"method": "ranged", "tags": ["narrative"]}}
assert_true(engine.evaluate({"type": "all", "conds": [
{"type": "attr", "attr": "per", "min": 6},
{"type": "skill", "skill": "traps", "min": 8},
{"type": "level", "min": 4},
{"type": "flag", "flag": "terminal_open", "equals": true},
{"type": "payload_equals", "key": "method", "value": "ranged"},
{"type": "payload_contains", "key": "tags", "value": "narrative"},
]}, context))
engine.apply_effects([
{"type": "set_flag", "flag": "rewarded"},
{"type": "add_attr", "attr": "per", "amount": 1},
{"type": "add_skill", "skill": "traps", "amount": 1},
], context)
assert_true(player.flags[&"rewarded"])
assert_eq(player.effective_attribute(&"per"), 7)
assert_eq(player.effective_skill(&"traps"), 9)
assert_false(engine.evaluate({"type": "not_a_content_condition"}, context))
assert_true(engine.diagnostics.find("Unknown condition type: not_a_content_condition") >= 0)
+1
View File
@@ -0,0 +1 @@
uid://b45kschi80lx7
+15
View File
@@ -48,6 +48,21 @@ func test_all_current_turn_result_kinds_map_to_complete_game_events_for_one_acti
assert_eq(events[index].target_id, &"goblin" if index % 2 == 0 else &"player")
assert_eq(events[index].cell, Vector2i(index, 2))
func test_structured_combat_descriptors_are_journaled_before_legacy_compatibility_results() -> void:
var game := GameScene.instantiate() as Game
add_child_autofree(game)
await get_tree().process_frame
var descriptors: Array[Dictionary] = [
{"kind": &"attack_started", "actor_id": &"player", "target_id": &"goblin", "cell": Vector2i(2, 2), "payload": {"stance": "none"}},
{"kind": &"attack_hit", "actor_id": &"player", "target_id": &"goblin", "cell": Vector2i(2, 2), "payload": {"hit_chance": 0.75}},
{"kind": &"damage_dealt", "actor_id": &"player", "target_id": &"goblin", "cell": Vector2i(2, 2), "payload": {"damage": 3, "critical": false}},
]
game._record_events([TurnResult.new(&"player", &"melee", Vector2i(2, 2), Vector2i(3, 2), &"goblin", 3, "You strike the goblin for 3.", {"domain_events": descriptors})])
var events := game.get_run_state().recent_events(4)
assert_eq(events.map(func(event: GameEvent) -> StringName: return event.kind), [&"attack_started", &"attack_hit", &"damage_dealt", &"melee"])
assert_eq(events[2].payload, {"from_cell": Vector2i(2, 2), "to_cell": Vector2i(3, 2), "damage": 3, "critical": false})
func test_a_second_resolved_action_gets_one_new_action_turn() -> void:
var game := GameScene.instantiate() as Game
add_child_autofree(game)
+39
View File
@@ -0,0 +1,39 @@
extends GutTest
const LimbWounds = preload("res://scripts/domain/combat/limb_wounds.gd")
const ActorState = preload("res://scripts/domain/actor_state.gd")
func test_all_supported_wounds_merge_by_worst_severity_and_duration() -> void:
var wounds := LimbWounds.new()
for slot in LimbWounds.SLOTS:
wounds.apply(slot, 1, 3, "fixture")
assert_eq(wounds.active_wounds().size(), 4)
wounds.apply(&"arms", 3, 1, "second hit")
assert_eq(wounds.severity_at(&"arms"), 3)
assert_eq(wounds.duration_at(&"arms"), 3)
wounds.apply(&"tail", 3, 8)
assert_eq(wounds.severity_at(&"tail"), 0)
func test_wounds_passively_recover_and_report_recovered_slot() -> void:
var wounds := LimbWounds.new()
wounds.apply(&"legs", 2, 2)
assert_eq(wounds.tick(), [])
assert_eq(wounds.duration_at(&"legs"), 1)
assert_eq(wounds.tick(), [&"legs"])
assert_eq(wounds.severity_at(&"legs"), 0)
func test_round_trip_preserves_wound_state() -> void:
var wounds := LimbWounds.new()
wounds.apply(&"head", 2, 7, "goblin")
var restored := LimbWounds.from_dictionary(wounds.to_dictionary())
assert_eq(restored.severity_at(&"head"), 2)
assert_eq(restored.duration_at(&"head"), 7)
assert_eq(restored.source_at(&"head"), "goblin")
func test_head_and_torso_wounds_reduce_perception_and_maximum_health() -> void:
var actor := ActorState.new(&"player", Vector2i.ZERO, 20, 3, &"player", {"perception": 10})
actor.wounds.apply(&"head", 2, 8)
actor.wounds.apply(&"torso", 2, 8)
assert_eq(actor.effective_perception(), 8)
assert_eq(actor.effective_max_hit_points(), 18)
+1
View File
@@ -0,0 +1 @@
uid://cxet8hxlsbg1e
+70
View File
@@ -0,0 +1,70 @@
extends GutTest
const ActorState = preload("res://scripts/domain/actor_state.gd")
const CombatEngine = preload("res://scripts/domain/combat/combat_engine.gd")
const LimbWounds = preload("res://scripts/domain/combat/limb_wounds.gd")
func _actor(id: StringName, health: int, attack: int, data: Dictionary = {}) -> ActorState:
return ActorState.new(id, Vector2i.ZERO, health, attack, id, data)
func test_data_backed_hit_defense_armor_and_critical_resolution_is_deterministic() -> void:
var attacker := _actor(&"player", 20, 6, {
"accuracy": 30, "weapon_damage": 8, "damage_variance": 0,
"penetration": 2, "crit_chance": 1.0, "crit_mult": 2.0,
})
var defender := _actor(&"goblin", 30, 4, {"defense": 0, "armor": 5})
var engine := CombatEngine.new([0.0, 0.0, 0.0])
var outcome := engine.resolve_attack(attacker, defender)
assert_true(outcome.hit)
assert_true(outcome.critical)
assert_eq(outcome.damage, 10) # (8 - (5 - 2)) * 2
assert_eq(defender.hit_points, 20)
func test_defense_can_make_an_attack_miss_without_damage() -> void:
var attacker := _actor(&"player", 20, 6, {"accuracy": 0})
var defender := _actor(&"goblin", 20, 4, {"defense": 20})
var outcome := CombatEngine.new([0.99]).resolve_attack(attacker, defender)
assert_false(outcome.hit)
assert_eq(outcome.damage, 0)
assert_eq(defender.hit_points, 20)
func test_dodge_and_block_stances_reduce_incoming_damage() -> void:
var enemy := _actor(&"goblin", 20, 8, {"accuracy": 30, "weapon_damage": 8, "damage_variance": 0})
var normal_target := _actor(&"player", 20, 4, {"defense": 0, "armor": 0})
var block_target := _actor(&"blocker", 20, 4, {"defense": 0, "armor": 0, "block": 4, "stance": "block"})
var dodge_target := _actor(&"dodger", 20, 4, {"defense": 0, "stance": "dodge"})
assert_eq(CombatEngine.new([0.0, 0.99]).resolve_attack(enemy, normal_target).damage, 8)
assert_eq(CombatEngine.new([0.0, 0.99]).resolve_attack(enemy, block_target).damage, 4)
assert_false(CombatEngine.new([0.90]).resolve_attack(enemy, dodge_target).hit)
func test_arm_and_leg_wounds_apply_ascii_combat_penalties() -> void:
var attacker := _actor(&"player", 20, 6, {"accuracy": 12})
attacker.wounds = LimbWounds.new()
attacker.wounds.apply(&"arms", 3, 9)
var defender := _actor(&"goblin", 20, 4, {"defense": 2})
assert_false(CombatEngine.new([0.5]).resolve_attack(attacker, defender).hit)
var target := _actor(&"target", 20, 4, {"defense": 0})
target.wounds = LimbWounds.new()
target.wounds.apply(&"legs", 3, 9)
assert_true(CombatEngine.new([0.70, 0.99]).resolve_attack(attacker, target).hit)
func test_defender_armor_and_block_follow_player_and_enemy_ascii_rules() -> void:
var attacker := _actor(&"player", 20, 8, {"accuracy": 30, "weapon_damage": 8, "damage_variance": 0})
var armored_enemy := _actor(&"goblin", 20, 4, {"defense": 0, "armor": 4})
var enemy := _actor(&"brute", 20, 8, {"accuracy": 30, "weapon_damage": 8, "damage_variance": 0})
var armored_player := _actor(&"player", 20, 4, {"defense": 0, "armor": 4})
var blocking_player := _actor(&"blocker", 20, 4, {"defense": 0, "armor": 4, "block": 4, "stance": "block"})
assert_eq(CombatEngine.new([0.0, 0.99]).resolve_attack(attacker, armored_enemy).damage, 4)
assert_eq(CombatEngine.new([0.0, 0.99]).resolve_attack(enemy, armored_player).damage, 6)
assert_eq(CombatEngine.new([0.0, 0.99]).resolve_attack(enemy, blocking_player).damage, 2)
func test_lethal_attack_emits_ordered_combat_death_xp_and_loot_events() -> void:
var attacker := _actor(&"player", 20, 12, {"accuracy": 30, "weapon_damage": 12, "xp": 0})
var defender := _actor(&"goblin", 6, 4, {"defense": 0, "xp_reward": 8, "loot_table": "goblin_basic"})
var outcome := CombatEngine.new([0.0, 0.99]).resolve_attack(attacker, defender)
assert_false(defender.is_alive())
assert_eq(outcome.events.map(func(event: Dictionary) -> StringName: return event.kind), [&"attack_started", &"attack_hit", &"damage_dealt", &"actor_died", &"xp_granted", &"loot_dropped"])
assert_eq(attacker.xp, 8)
assert_eq(outcome.events[2].payload.damage, 6)
assert_eq(outcome.events[5].payload.loot_table, "goblin_basic")
+1
View File
@@ -0,0 +1 @@
uid://drgosggpynk4q
+37
View File
@@ -0,0 +1,37 @@
extends GutTest
const PlayerStateType = preload("res://scripts/domain/entities/player_state.gd")
const StatusEffectType = preload("res://scripts/domain/combat/status_effect.gd")
func test_status_uses_data_default_duration_and_refreshes_without_stacking() -> void:
var player := PlayerStateType.new()
player.apply_status(StatusEffectType.new(&"poison"))
player.apply_status(StatusEffectType.new(&"poison", 6, 5))
assert_eq(player.status_effects.size(), 1)
assert_eq(player.status_effects[0].duration, 6)
assert_eq(player.status_effects[0].magnitude, 5)
func test_status_tick_applies_effect_before_expiring_and_reports_tick() -> void:
var player := PlayerStateType.new({"attributes": {"end": 4}})
player.hit_points = 20
player.apply_status(StatusEffectType.new(&"poison", 2))
player.apply_status(StatusEffectType.new(&"regenerating", 1))
var ticks := player.tick_status_effects()
assert_eq(player.hit_points, 21)
assert_eq(ticks.size(), 2)
assert_eq(ticks[0]["hit_point_delta"], -3)
assert_eq(ticks[1]["hit_point_delta"], 4)
assert_eq(player.status_effects.size(), 1)
assert_eq(player.status_effects[0].key, &"poison")
assert_eq(player.status_effects[0].duration, 1)
func test_expired_status_is_removed_after_its_final_tick() -> void:
var player := PlayerStateType.new()
player.apply_status(StatusEffectType.new(&"stunned", 1))
player.tick_status_effects()
assert_false(player.has_status(&"stunned"))
+1
View File
@@ -0,0 +1 @@
uid://jbbilbj72nq4