Fixed the naming inconsistencies the cohesion audit flagged, converging on the domain-first style the bulk of the surface already uses (ui_*, text_*, rng_*, font_load, image_load): - load_png -> png_load (asset loaders were split: font_load/image_load - load_sprites -> sprites_load were domain-first, load_* were verb-first) - setreg -> set_reg (missing underscore vs ui_set_int/ui_set_text) Game builtins resolve to their `rt_` runtime function, so the renames are in runtime/native (rt_png_load, rt_sprites_load, rt_set_reg) plus the ~100 example call sites; `become` lowering in emit_machine now emits @fn_rt_set_reg. Purely a surface rename — every renamed call maps to the same runtime symbol, so behavior and golden renders are byte-identical. Vocabulary + docs updated (ludic_syntax.h, grammar, LudicTokens.kt, LANGUAGE.md, README, SYNTAX-REDESIGN). Reseeded; C-free fixpoint holds; goldens identical; 17/17; vocab clean. Left as-is: os_argc/os_arg (compiler-internal intrinsics, already namespaced and consistent with each other; renaming would need a bootstrap dance for little gain). reg/set_reg keep the getter-bare/setter-set_ shape ui_focused/ui_set_int already use. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
172 lines
5.3 KiB
Text
172 lines
5.3 KiB
Text
# combat.ludic — the co-op turn-based battle state machine.
|
|
|
|
# The battle is a state machine over R_PHASE. `machine` dispatches on the
|
|
# register and `become` transitions to a named state — no more `if ph == N`
|
|
# ladders. Knight turn (0,1), Mage turn (2,3), Enemy turn (4).
|
|
|
|
# Menu selections, named instead of matched on bare integers (R_CUR holds one).
|
|
enum KnightAct { Attack, Guard, Item, Flee }
|
|
enum MageAct { Attack, Heal, Guard }
|
|
|
|
handler Battle phase Update {
|
|
if reg(R_MODE) == 1 {
|
|
let k = key()
|
|
for (e) in query [Stats, {Enemy}] {
|
|
machine R_PHASE {
|
|
|
|
state KnightMenu {
|
|
for (st, pt) in query [Stats, Party] {
|
|
if pt.slot == 0 {
|
|
if st.hp <= 0 {
|
|
become MageMenu
|
|
set_reg(R_CUR, 0)
|
|
} else {
|
|
if k == 'w' { set_reg(R_CUR, max(0, reg(R_CUR) - 1)) }
|
|
if k == 's' { set_reg(R_CUR, min(3, reg(R_CUR) + 1)) }
|
|
if is_confirm(k) {
|
|
match reg(R_CUR) {
|
|
KnightAct.Attack => {
|
|
let d = max(1, st.atk - e.def + rng_range(0, 4))
|
|
e.hp = e.hp - d
|
|
set_reg(R_PDMG, d)
|
|
}
|
|
KnightAct.Guard => { st.guard = 1; set_reg(R_PDMG, 0) }
|
|
KnightAct.Item => {
|
|
if reg(R_POTION) > 0 {
|
|
set_reg(R_POTION, reg(R_POTION) - 1)
|
|
st.hp = min(st.maxhp, st.hp + 24)
|
|
set_reg(R_PDMG, 24)
|
|
} else { set_reg(R_PDMG, 0) }
|
|
}
|
|
KnightAct.Flee => {
|
|
if rng_chance(50) { e.hp = 0; set_reg(R_MODE, 0) }
|
|
else { set_reg(R_PDMG, 0) }
|
|
}
|
|
}
|
|
become KnightResolve
|
|
set_reg(R_CUR, 0)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
state KnightResolve {
|
|
if k != 0 {
|
|
if e.hp <= 0 {
|
|
grant_xp(e.xp)
|
|
set_reg(R_GOLD, reg(R_GOLD) + e.xp)
|
|
if reg(R_ETYPE) == 2 { set_reg(R_BOSS, 1); set_reg(R_MODE, 3); set_reg(R_ACK, 0) }
|
|
else { set_reg(R_MODE, 0) }
|
|
become KnightMenu
|
|
} else {
|
|
become MageMenu
|
|
set_reg(R_CUR, 0)
|
|
}
|
|
}
|
|
}
|
|
|
|
state MageMenu {
|
|
for (st, pt) in query [Stats, Party] {
|
|
if pt.slot == 1 {
|
|
if st.hp <= 0 {
|
|
become EnemyTurn
|
|
} else {
|
|
if k == 'i' { set_reg(R_CUR, max(0, reg(R_CUR) - 1)) }
|
|
if k == 'k' { set_reg(R_CUR, min(2, reg(R_CUR) + 1)) }
|
|
if k == 'j' {
|
|
match reg(R_CUR) {
|
|
MageAct.Attack => {
|
|
let d = max(1, st.atk + 5 - e.def + rng_range(0, 5))
|
|
e.hp = e.hp - d
|
|
set_reg(R_PDMG, d)
|
|
}
|
|
MageAct.Heal => {
|
|
if st.mp >= 4 {
|
|
st.mp = st.mp - 4
|
|
for (t2, p2) in query [Stats, Party] {
|
|
if p2.slot == 0 { t2.hp = min(t2.maxhp, t2.hp + 16) }
|
|
}
|
|
set_reg(R_PDMG, 16)
|
|
} else { set_reg(R_PDMG, 0) }
|
|
}
|
|
MageAct.Guard => { st.guard = 1; set_reg(R_PDMG, 0) }
|
|
}
|
|
become MageResolve
|
|
set_reg(R_CUR, 0)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
state MageResolve {
|
|
if k != 0 {
|
|
if e.hp <= 0 {
|
|
grant_xp(e.xp)
|
|
set_reg(R_GOLD, reg(R_GOLD) + e.xp)
|
|
if reg(R_ETYPE) == 2 { set_reg(R_BOSS, 1); set_reg(R_MODE, 3); set_reg(R_ACK, 0) }
|
|
else { set_reg(R_MODE, 0) }
|
|
become KnightMenu
|
|
} else {
|
|
become EnemyTurn
|
|
}
|
|
}
|
|
}
|
|
|
|
state EnemyTurn {
|
|
if k != 0 {
|
|
let done = 0
|
|
for (t3) in query [Stats, {Party}] {
|
|
if done == 0 {
|
|
if t3.hp > 0 {
|
|
let ed = max(1, e.atk - t3.def + rng_range(0, 4))
|
|
if t3.guard == 1 { ed = ed / 2; t3.guard = 0 }
|
|
t3.hp = t3.hp - ed
|
|
set_reg(R_EDMG, ed)
|
|
done = 1
|
|
}
|
|
}
|
|
}
|
|
let alive = 0
|
|
for (t4) in query [Stats, {Party}] {
|
|
if t4.hp > 0 { alive = alive + 1 }
|
|
}
|
|
if alive == 0 { set_reg(R_MODE, 2); set_reg(R_ACK, 0) }
|
|
else {
|
|
become KnightMenu
|
|
set_reg(R_CUR, 0)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
handler CleanBattle phase LateUpdate {
|
|
if reg(R_MODE) != 1 {
|
|
for (e) in query [Stats, {Enemy}] {
|
|
despawn self()
|
|
}
|
|
}
|
|
}
|
|
|
|
handler Meta phase Update {
|
|
let k = key()
|
|
if reg(R_MODE) == 2 {
|
|
if k == 0 { set_reg(R_ACK, 1) }
|
|
if k != 0 {
|
|
if reg(R_ACK) == 1 {
|
|
for (h) in query [Stats, {Party}] { h.hp = h.maxhp }
|
|
set_reg(R_MODE, 0)
|
|
}
|
|
}
|
|
}
|
|
if reg(R_MODE) == 3 {
|
|
if k == 0 { set_reg(R_ACK, 1) }
|
|
if k != 0 {
|
|
if reg(R_ACK) == 1 { set_reg(R_MODE, 0) }
|
|
}
|
|
}
|
|
}
|