- runtime: HEADLESS_FRAME_PATH, STICK_DEADZONE / STICK_LEFT_X/Y, key and byte codes as char literals throughout (`k == 'w'`, `fill(rt_map, ' ', …)`) - ludic.gameplay/stats: drop the duplicate `stat_field` (it answered "atk" for every build stat); Stats.base uses stats_field_name - ludic.shooter: compare aim modes and fire patterns with AimMode.* and WeaponPattern.* instead of raw ints; STICK_RIGHT_X/Y - ludic.npcai: DecisionMade / brain_set_state use AiState.* - examples/games/menu.ludic uses Font.load / Ui.* with FONT_PATH and BACKDROP named; strings.ludic header says what it prints - whole tree: `x = x + 1` → `x += 1` (single-term right-hand sides only), `0 - x` → `-x`, ASCII codes → char literals; every .ludic and every ```ludic fence reformatted with the fixed formatter (whitespace only) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
148 lines
5.9 KiB
Text
148 lines
5.9 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
|
|
}
|
|
}
|
|
|
|
# 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, stats_field_name(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 += amt } else { 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 -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
|
|
}
|
|
}
|