ludic/examples/chronorift/combat.ludic
Orkuncakilkaya e1d7797e29 Phase 6f: let = immutable, var = mutable (binding-only)
Bindings now signal mutability the way Rust/Swift do, instead of `let` meaning
"local" and `var` meaning "module-level":

  - `let x = e`  -> immutable binding; a later `x = …` is a compile error
    (`cannot assign to immutable 'x' … use var`).
  - `var x = e`  -> mutable binding, at local OR module scope (position decides
    scope; the keyword decides mutability).
  - `const`      -> unchanged (compile-time).

Immutability is of the *binding*, not the object: `let n = new Node; n.kind = 1`
is fine (mutation through the reference); only rebinding `n` is rejected. The
check lives in emit_assign — a direct `name =` whose target is a `let` local
(loc_mut == 0) errors; field/index targets and `var`/param/loop bindings are
unaffected.

Delivered as three reseeds so the self-hosting compiler never had to compile
source its own rules would reject:
  A) add `var` as a local statement + per-local mutability tracking (loc_mut),
     no enforcement;
  B) migrate every reassigned `let` -> `var` across the compiler, runtime and
     examples (337 declarations), driven by a per-function, string/comment-aware
     scan (binding targets only, never `x.f =` / `x[i] =`);
  C) turn on the check. bootstrap-cfree (compiler vs its own source) and every
     golden build (which splices the runtime) then proved zero reassigned `let`
     was missed anywhere.

Also folded in: removed leftover debug instrumentation in block() (a `cur=` /
print_int(777…) trace on the separator-error path) and fixed parse.ludic's stale
header comment (no more `struct`). Reseeded (21711 lines); C-free fixpoint holds;
goldens identical; 17/17; vocab + doc-fences clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-27 23:52:59 +03:00

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 {
var done = 0
for (t3) in query [Stats, {Party}] {
if done == 0 {
if t3.hp > 0 {
var 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
}
}
}
var 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) }
}
}
}