ludic/packages/ludic.gameplay/stats.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

158 lines
6.2 KiB
Text

# stats.ludic — attributes + a timed modifier stack (#57 foundation).
#
# `Stats` is a POD attribute bundle shared by the RPG, shooter and NPC families.
# On top of the raw fields sits a *modifier stack*: each buff/debuff is its own
# tiny `Modifier` entity (target + stat + op + amount + ttl), so the stack is
# unbounded, captured by world_save, and expired by one engine system. A queried
# stat is `base + flat`, then scaled by the summed percent — the standard
# flat-then-percent order — computed on demand, never mutating the base.
#
# Stat codes (the `stat` argument everywhere below):
# 0 = max_hp 1 = atk 2 = def 3 = spd 4 = max_mp
enum StatKind { MaxHp, Attack, Defense, Speed, MaxMp, DamagePct, CritPct, LeechPct, Thorns, FireRatePct } # Stats.* stat codes
enum ModifyOp { Flat, Percent } # Stats.modify op
# The build stats Combat.damage applies by itself: the attacker's damage_pct and
# crit_pct (a crit doubles), its leech_pct chance to heal leech_hp on a hit, the
# defender's thorns dealt back, and fire_rate_pct the weapon system reads.
property Stats {
hp: int = 0, max_hp: int = 0,
mp: int = 0, max_mp: int = 0,
atk: int = 0, def: int = 0, spd: int = 0,
level: int = 1, xp: int = 0,
damage_pct: int = 100, crit_pct: int = 0, leech_pct: int = 0, leech_hp: int = 1, thorns: int = 0, fire_rate_pct: int = 100
}
# a Stats field of an entity by name, or `fallback` when absent
function stats_field(e: int, name: pointer, fallback: int) -> int {
let P = World.prop_id("Stats")
if (P < 0) or (World.has(e, P) == 0) { return fallback }
let f = World.field_id(P, name)
if f < 0 { return fallback }
return World.get(e, P, f)
}
# the Stats field a stat code names
function stats_field_name(stat: int) -> pointer {
match stat {
StatKind.MaxHp => { return "max_hp" }
StatKind.Attack => { return "atk" }
StatKind.Defense => { return "def" }
StatKind.Speed => { return "spd" }
StatKind.MaxMp => { return "max_mp" }
StatKind.DamagePct => { return "damage_pct" }
StatKind.CritPct => { return "crit_pct" }
StatKind.LeechPct => { return "leech_pct" }
StatKind.Thorns => { return "thorns" }
_ => { return "fire_rate_pct" }
}
}
# Stats.add(e, stat, amount): change a base stat in place (a permanent upgrade)
@Namespace(Stats) function stats_add(e: int, stat: int, amount: int) -> void {
let P = World.prop_id("Stats")
if (P < 0) or (World.has(e, P) == 0) { return }
let f = World.field_id(P, stats_field_name(stat))
if f >= 0 { World.set(e, P, f, World.get(e, P, f) + amount) }
}
# Stats.scale_hp(e, percent): hp and max_hp scaled (a floor's difficulty)
@Namespace(Stats) function stats_scale_hp(e: int, percent: int) -> void {
let P = World.prop_id("Stats")
if (P < 0) or (World.has(e, P) == 0) { return }
let fhp = World.field_id(P, "hp")
let fmax = World.field_id(P, "max_hp")
let hp = World.get(e, P, fhp) * percent / 100
World.set(e, P, fhp, hp)
World.set(e, P, fmax, hp)
}
# a single timed modifier, carried on its own entity so the stack is dynamic.
# op: 0 = flat add, 1 = percent add. ttl: frames left (0 or less = permanent).
property Modifier { target: int = 0, stat: int = 0, op: int = 0, amount: int = 0, ttl: int = 0 }
model ModifierFx { Modifier }
@OnSpawn(ModifierFx) handler ModifierFxInit { }
# esys_modifier (Update): count down every timed modifier and despawn the expired
# ones. Permanent modifiers (ttl <= 0) are left alone.
@EngineSystem(Modifier, Update) function esys_modifier() -> void {
let P = World.prop_id("Modifier")
if P < 0 { return }
let fttl = World.field_id(P, "ttl")
if fttl < 0 { return }
var e = World.query_next(P, 0)
while e >= 0 {
let ttl = World.get(e, P, fttl)
var nxt = World.query_next(P, e + 1)
if ttl > 0 {
let left = ttl - 1
if left <= 0 { despawn e } else { World.set(e, P, fttl, left) }
}
e = nxt
}
}
# map a stat code to the matching Stats field name.
function stat_field(stat: int) -> pointer {
if stat == 0 { return "max_hp" }
if stat == 1 { return "atk" }
if stat == 2 { return "def" }
if stat == 3 { return "spd" }
if stat == 4 { return "max_mp" }
return "atk"
}
# Stats.base(e, stat) — the raw stored field, no modifiers.
@Namespace(Stats) function stats_base(e: int, stat: int) -> int {
let P = World.prop_id("Stats")
if P < 0 { return 0 }
let f = World.field_id(P, stat_field(stat))
if f < 0 { return 0 }
return World.get(e, P, f)
}
# Stats.total(e, stat) — the effective value: (base + flat) * (100 + percent)/100.
@Namespace(Stats) function stats_total(e: int, stat: int) -> int {
let base = stats_base(e, stat)
let P = World.prop_id("Modifier")
if P < 0 { return base }
let ft = World.field_id(P, "target")
let fs = World.field_id(P, "stat")
let fo = World.field_id(P, "op")
let fa = World.field_id(P, "amount")
var flat = 0
var pct = 0
var m = World.query_next(P, 0)
while m >= 0 {
if (World.get(m, P, ft) == e) and (World.get(m, P, fs) == stat) {
let amt = World.get(m, P, fa)
if World.get(m, P, fo) == 1 { pct = pct + amt } else { flat = flat + amt }
}
m = World.query_next(P, m + 1)
}
return (base + flat) * (100 + pct) / 100
}
# Stats.modify(target, stat, op, amount, ttl) — push a modifier onto the stack.
@Namespace(Stats) function stats_modify(target: int, stat: int, op: int, amount: int, ttl: int) -> int {
let m = World.model_id("ModifierFx")
if m < 0 { return 0 - 1 }
let e = World.spawn(m)
let P = World.prop_id("Modifier")
World.set(e, P, World.field_id(P, "target"), target)
World.set(e, P, World.field_id(P, "stat"), stat)
World.set(e, P, World.field_id(P, "op"), op)
World.set(e, P, World.field_id(P, "amount"), amount)
World.set(e, P, World.field_id(P, "ttl"), ttl)
return e
}
# Stats.clear_mods(e) — drop every modifier targeting an entity (e.g. on unequip).
@Namespace(Stats) function stats_clear_mods(target: int) -> void {
let P = World.prop_id("Modifier")
if P < 0 { return }
let ft = World.field_id(P, "target")
var m = World.query_next(P, 0)
while m >= 0 {
var nxt = World.query_next(P, m + 1)
if World.get(m, P, ft) == target { despawn m }
m = nxt
}
}