ludic/packages/ludic.gameplay/combat.ludic
Orkuncakilkaya ad548840c7
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 32s
ci / build-and-test (push) Successful in 2m49s
commit-lint / conventional-commits (push) Successful in 5s
docs / build-and-deploy (push) Successful in 30s
feat(engine): #90 atlas-aware Sprite component, #91 become from listeners, 0.3.x ergonomics batch
Closes the two open issues and lands the pending unreleased batch:

- #90: `Sprite { atlas: 1 }` routes esys_sprite through atlas_draw_ex
  (scale/flip/tint), so cell / cell_span / strip ids of any size draw
  through the engine sprite-render system. examples/library/sprite_atlas
  is the pixel-readback regression.
- #91: `become` from an @On(Event) listener / global handler / plain
  function no longer segfaults the compiler; it emits @L_scene_leave()
  (a dispatch on the live scene id) so the leaving scene's on-exit runs.
  UI_* handles are readable from any code (widget table built on first
  use). examples/library/scene_menus covers it.
- fix: a windowed `ludicc -o` build that reaches the audio runtime only
  through the atlas/Assets preload import now links audio.ll +
  AVFoundation (the audio backend link was gated on a game-level
  Audio.* call, so any windowed game declaring Sprite failed to link).
- the hand-written "Unreleased" CHANGELOG section is converted to
  changesets under changes/ so `x release` generates it.
- plus the batch: engine-driven retained UI + UiClicked event, Overlay
  phase, TileSkin tilemap-render system, Key.* constants, Font/Ui/File
  namespaces, Sprite.strip, prefabs, managers, countdown fields,
  enum-typed machines, layer @Queries, ludic.prefs / ludic.dungeon
  packages, Ai.seek pathing, Solids.solid2, cursor confine (mode 3)
  fix, shooter centre-aim fix, reserved-word function diagnostic.

Verified: x test (124/124), x test-tools, check-impl, check-vocabulary,
check-docs, docs-gen + docs-check, bootstrap-cfree (seed is a fixpoint).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-04 01:36:08 +03:00

94 lines
4.2 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
var combat_reflecting: bool = false # thorns never reflect thorns
# the mutable channel for lever 6: listeners rewrite the pending damage here.
var combat_pending: int = 0
@Namespace(Combat) function combat_amount() -> int { return combat_pending }
@Namespace(Combat) function combat_set_amount(n: int) -> void { 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(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 = raw * 2; emit Crit(target: target, src: src) }
combat_pending = combat_mitigate(target, raw)
if emit DamageAboutToApply(target: target, src: src, amount: combat_pending) != 0 { return 0 }
var dmg = 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 combat_reflecting) and (World.has(src, P) != 0) {
combat_reflecting = true
combat_damage(src, target, thorns)
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)
}