ludic/examples/chronorift/overworld.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

140 lines
4.3 KiB
Text

# overworld.ludic — boot, movement/encounter input, and the shop system.
handler Boot phase Start {
png_load("assets/kenney/tiny-town/Tiles/tile_0000.png") # 0 grass
png_load("assets/kenney/tiny-town/Tiles/tile_0004.png") # 1 tree
png_load("assets/kenney/tiny-town/Tiles/tile_0025.png") # 2 path
png_load("assets/kenney/tiny-town/Tiles/tile_0073.png") # 3 house
png_load("assets/kenney/tiny-town/Tiles/tile_0112.png") # 4 arch
png_load("assets/kenney/tiny-dungeon/Tiles/tile_0096.png") # 5 hero
png_load("assets/kenney/tiny-dungeon/Tiles/tile_0108.png") # 6 slime
png_load("assets/kenney/tiny-dungeon/Tiles/tile_0120.png") # 7 skeleton
png_load("assets/kenney/tiny-dungeon/Tiles/tile_0110.png") # 8 boss
png_load("assets/kenney/tiny-dungeon/Tiles/tile_0084.png") # 9 mage
png_load("assets/kenney/tiny-dungeon/Tiles/tile_0048.png") # 10 floor
png_load("assets/kenney/tiny-dungeon/Tiles/tile_0026.png") # 11 wall
build_field(0)
spawn Player {
Pos { x: 11, y: 15 }
Actor { kind: 5 }
Party { slot: 0 }
Stats { hp: 40, maxhp: 40, mp: 8, maxmp: 8, atk: 12, def: 6, lvl: 1, xp: 0, guard: 0 }
}
spawn Ally {
Party { slot: 1 }
Stats { hp: 26, maxhp: 26, mp: 16, maxmp: 16, atk: 8, def: 4, lvl: 1, xp: 0, guard: 0 }
}
set_reg(R_MODE, 0)
set_reg(R_ENC, 4)
set_reg(R_MAP, 0)
set_reg(R_BOSS, 0)
set_reg(R_GOLD, 20)
set_reg(R_POTION, 2)
}
handler Control phase Input {
if reg(R_MODE) == 0 {
for (p) in query [Pos, {Player}] {
let k = key()
var nx = p.x
var ny = p.y
if k == 'w' { ny = p.y - 1 }
if k == 's' { ny = p.y + 1 }
if k == 'a' { nx = p.x - 1 }
if k == 'd' { nx = p.x + 1 }
var moved = 0
if walkable(nx, ny) {
if nx != p.x or ny != p.y {
p.x = nx
p.y = ny
moved = 1
}
}
if moved == 1 {
let tc = tile(p.x, p.y)
if tc == 'D' {
build_dungeon(0)
p.x = 3
p.y = 2
set_reg(R_MAP, 1)
}
if tc == 'S' {
build_field(0)
p.x = 36
p.y = 3
set_reg(R_MAP, 0)
}
if tc == 'B' {
if reg(R_BOSS) == 0 {
set_reg(R_ETYPE, 2)
spawn Enemy {
Actor { kind: 8 }
Stats { hp: 120, maxhp: 120, mp: 0, maxmp: 0, atk: 18, def: 10, lvl: 5, xp: 300, guard: 0 }
}
set_reg(R_MODE, 1)
set_reg(R_PHASE, 0)
set_reg(R_CUR, 0)
}
}
if tc == 'h' {
set_reg(R_MODE, 4)
set_reg(R_CUR, 0)
}
if tc != 'D' {
if tc != 'S' {
if tc != 'B' {
if tc != 'h' {
set_reg(R_ENC, reg(R_ENC) - 1)
if reg(R_ENC) <= 0 {
set_reg(R_ENC, rng_range(4, 9))
var et = 0
if reg(R_MAP) == 1 { et = 1 }
if reg(R_MAP) == 0 {
if rng_chance(40) { et = 1 }
}
set_reg(R_ETYPE, et)
if et == 0 {
spawn Enemy {
Actor { kind: 6 }
Stats { hp: 18, maxhp: 18, mp: 0, maxmp: 0, atk: 8, def: 2, lvl: 1, xp: 7, guard: 0 }
}
}
if et == 1 {
spawn Enemy {
Actor { kind: 7 }
Stats { hp: 30, maxhp: 30, mp: 0, maxmp: 0, atk: 11, def: 5, lvl: 1, xp: 15, guard: 0 }
}
}
set_reg(R_MODE, 1)
set_reg(R_PHASE, 0)
set_reg(R_CUR, 0)
}
}
}
}
}
}
if k == 'k' { save() }
if k == 'l' { if load() { } }
}
}
}
# shop (at the house 'h' tile): buy potions with gold
handler Shop phase Update {
if reg(R_MODE) == 4 {
let k = key()
if k == 'w' { set_reg(R_CUR, max(0, reg(R_CUR) - 1)) }
if k == 's' { set_reg(R_CUR, min(1, reg(R_CUR) + 1)) }
if is_confirm(k) {
if reg(R_CUR) == 0 {
if reg(R_GOLD) >= 10 {
set_reg(R_GOLD, reg(R_GOLD) - 10)
set_reg(R_POTION, reg(R_POTION) + 1)
}
}
if reg(R_CUR) == 1 { set_reg(R_MODE, 0) }
}
}
}