101 lines
4.6 KiB
Text
101 lines
4.6 KiB
Text
# combat.ludic — the shared damage pipeline (#57 foundation).
|
|
#
|
|
# One place every family routes hits through, so buffs/armor/crits/lifesteal are
|
|
# written once as event listeners rather than re-invented per genre. The pipeline
|
|
# is the honest no-closure realisation of lever 4 (events) + lever 6 (a mutable
|
|
# hook):
|
|
#
|
|
# 1. mitigate the raw amount by the target's effective defence (Stats.total);
|
|
# 2. publish the pending amount and emit `cancellable DamageAboutToApply` —
|
|
# a listener may veto the hit (cancel) or *rewrite the number* by calling
|
|
# Combat.set_amount(n) (the mutable channel, since `emit` only returns the
|
|
# cancelled flag);
|
|
# 3. apply the final amount to Stats.hp, emit `Damaged`, and emit `Died` when hp
|
|
# reaches zero.
|
|
#
|
|
# Determinism: integer arithmetic, fixed listener order, no wall-clock — a hit
|
|
# replays identically. Combat never moves or despawns bodies itself; the game
|
|
# reacts to `Died` (respawn, ragdoll, loot) so the policy stays with the game.
|
|
|
|
event cancellable DamageAboutToApply { target: int = 0, src: int = 0, amount: int = 0 }
|
|
event Damaged { target: int = 0, src: int = 0, amount: int = 0 }
|
|
event Died { e: int = 0, src: int = 0 }
|
|
event Healed { e: int = 0, amount: int = 0 }
|
|
event Crit { target: int = 0, src: int = 0 } # the attacker's crit_pct fired
|
|
|
|
# the faction table: FAC_MAX ids square, every pair unset (faction.ludic)
|
|
const FAC_MAX: int = 32
|
|
function fac_table() -> words { return words(FAC_MAX * FAC_MAX) }
|
|
|
|
state GameplayState {
|
|
combat_reflecting: bool = false # thorns never reflect thorns
|
|
combat_pending: int = 0
|
|
fac_tbl: words = fac_table()
|
|
}
|
|
|
|
# the mutable channel for lever 6: listeners rewrite the pending damage here.
|
|
|
|
@Namespace(Combat) function combat_amount(gameplay_st: GameplayState) -> int { return gameplay_st.combat_pending }
|
|
@Namespace(Combat) function combat_set_amount(gameplay_st: mut GameplayState, n: int) -> void { gameplay_st.combat_pending = n }
|
|
|
|
# Combat.mitigate(target, raw) — raw minus effective defence, floored at 1 so any
|
|
# landed hit chips at least a point (a common, tunable default). A game that wants
|
|
# a different formula overrides via the DamageAboutToApply hook.
|
|
@Namespace(Combat) function combat_mitigate(target: int, raw: int) -> int {
|
|
let d = stats_total(target, 2) # stat 2 = def
|
|
var v = raw - d
|
|
if v < 1 { v = 1 }
|
|
return v
|
|
}
|
|
|
|
# Combat.damage(target, src, amount) — the full pipeline. Returns the damage
|
|
# actually applied (0 when vetoed or the target has no Stats/hp).
|
|
@Namespace(Combat) function combat_damage(gameplay_st: mut GameplayState, target: int, src: int, amount: int) -> int {
|
|
let P = World.prop_id("Stats")
|
|
if P < 0 { return 0 }
|
|
if World.has(target, P) == 0 { return 0 }
|
|
let fhp = World.field_id(P, "hp")
|
|
if fhp < 0 { return 0 }
|
|
|
|
# the attacker's build: damage_pct, then a crit_pct chance to double
|
|
var raw = amount * stats_field(src, "damage_pct", 100) / 100
|
|
let crit = stats_field(src, "crit_pct", 0)
|
|
if (crit > 0) and Random.chance(percent: crit) { raw *= 2; emit Crit(target: target, src: src) }
|
|
gameplay_st.combat_pending = combat_mitigate(target, raw)
|
|
if emit DamageAboutToApply(target: target, src: src, amount: gameplay_st.combat_pending) != 0 { return 0 }
|
|
var dmg = gameplay_st.combat_pending
|
|
if dmg < 0 { dmg = 0 }
|
|
|
|
var hp = World.get(target, P, fhp) - dmg
|
|
if hp < 0 { hp = 0 }
|
|
World.set(target, P, fhp, hp)
|
|
emit Damaged(target: target, src: src, amount: dmg)
|
|
if hp <= 0 { emit Died(e: target, src: src) }
|
|
# the attacker's leech, and the defender's thorns (never reflected back again)
|
|
if dmg > 0 {
|
|
let leech = stats_field(src, "leech_pct", 0)
|
|
if (leech > 0) and Random.chance(percent: leech) { combat_heal(src, stats_field(src, "leech_hp", 1)) }
|
|
let thorns = stats_field(target, "thorns", 0)
|
|
if (thorns > 0) and (src != target) and (not gameplay_st.combat_reflecting) and (World.has(src, P) != 0) {
|
|
gameplay_st.combat_reflecting = true
|
|
combat_damage(gameplay_st, src, target, thorns)
|
|
gameplay_st.combat_reflecting = false
|
|
}
|
|
}
|
|
return dmg
|
|
}
|
|
|
|
# Combat.heal(e, amount) — restore hp, clamped to max_hp (effective, so a +max_hp
|
|
# buff raises the ceiling), and emit `Healed`.
|
|
@Namespace(Combat) function combat_heal(e: int, amount: int) -> void {
|
|
let P = World.prop_id("Stats")
|
|
if P < 0 { return }
|
|
if World.has(e, P) == 0 { return }
|
|
let fhp = World.field_id(P, "hp")
|
|
if fhp < 0 { return }
|
|
let cap = stats_total(e, 0) # stat 0 = max_hp
|
|
var hp = World.get(e, P, fhp) + amount
|
|
if (cap > 0) and (hp > cap) { hp = cap }
|
|
World.set(e, P, fhp, hp)
|
|
emit Healed(e: e, amount: amount)
|
|
}
|