feat: port melee combat and wounds
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://cox0vsuwmj701
|
||||
Reference in New Issue
Block a user