Baseline: Ludic compiler + toolchain, Phase 1 syntax fixes complete

Self-hosted compiler (selfhost/*.ludic), runtime, examples, editor tooling,
and docs. Phase 1 of the syntax-redesign cohesion pass has landed:
edge-system fix, signature-query, when-alias, and the documentation truth-pass.
Suite green (14/14), C-free bootstrap fixpoint holds.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-27 15:15:35 +03:00
commit 985f9ad8f2
418 changed files with 39065 additions and 0 deletions

16
examples/chronorift.ludic Normal file
View file

@ -0,0 +1,16 @@
# ============================================================================
# chronorift.ludic — a 2D windowed co-op JRPG written in Ludic (CC0 Kenney art)
# Overworld + dungeon + random encounters + co-op turn battle + boss + save.
# Compiles to a native macOS app: ludicc -> LLVM IR -> native binary. No C.
#
# This entry file is deliberately thin: the game is split across modules under
# chronorift/, pulled in with `import`. Paths resolve relative to this file, and
# each import is include-guarded so re-imports are free.
# ============================================================================
game ChronoRift {
import "chronorift/world.ludic" # components, entity archetypes, constants
import "chronorift/rules.ludic" # pure rules + map data (functions)
import "chronorift/overworld.ludic" # boot, movement, encounters, shop
import "chronorift/combat.ludic" # co-op turn battle state machine
import "chronorift/draw.ludic" # the Render system
}

View file

@ -0,0 +1,167 @@
# 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).
system Battle phase Update {
if reg(R_MODE) == 1 {
let k = key()
for (e) in query [Stats, {Enemy}] {
machine R_PHASE {
state KnightMenu = 0 {
for (st, pt) in query [Stats, Party] {
if pt.slot == 0 {
if st.hp <= 0 {
become MageMenu
setreg(R_CUR, 0)
} else {
if k == 'w' { setreg(R_CUR, max(0, reg(R_CUR) - 1)) }
if k == 's' { setreg(R_CUR, min(3, reg(R_CUR) + 1)) }
if is_confirm(k) {
match reg(R_CUR) {
0 => {
let d = max(1, st.atk - e.def + rng_range(0, 4))
e.hp = e.hp - d
setreg(R_PDMG, d)
}
1 => { st.guard = 1 setreg(R_PDMG, 0) }
2 => {
if reg(R_POTION) > 0 {
setreg(R_POTION, reg(R_POTION) - 1)
st.hp = min(st.maxhp, st.hp + 24)
setreg(R_PDMG, 24)
} else { setreg(R_PDMG, 0) }
}
3 => {
if rng_chance(50) { e.hp = 0 setreg(R_MODE, 0) }
else { setreg(R_PDMG, 0) }
}
}
become KnightResolve
setreg(R_CUR, 0)
}
}
}
}
}
state KnightResolve = 1 {
if k != 0 {
if e.hp <= 0 {
grant_xp(e.xp)
setreg(R_GOLD, reg(R_GOLD) + e.xp)
if reg(R_ETYPE) == 2 { setreg(R_BOSS, 1) setreg(R_MODE, 3) setreg(R_ACK, 0) }
else { setreg(R_MODE, 0) }
become KnightMenu
} else {
become MageMenu
setreg(R_CUR, 0)
}
}
}
state MageMenu = 2 {
for (st, pt) in query [Stats, Party] {
if pt.slot == 1 {
if st.hp <= 0 {
become EnemyTurn
} else {
if k == 'i' { setreg(R_CUR, max(0, reg(R_CUR) - 1)) }
if k == 'k' { setreg(R_CUR, min(2, reg(R_CUR) + 1)) }
if k == 'j' {
match reg(R_CUR) {
0 => {
let d = max(1, st.atk + 5 - e.def + rng_range(0, 5))
e.hp = e.hp - d
setreg(R_PDMG, d)
}
1 => {
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) }
}
setreg(R_PDMG, 16)
} else { setreg(R_PDMG, 0) }
}
2 => { st.guard = 1 setreg(R_PDMG, 0) }
}
become MageResolve
setreg(R_CUR, 0)
}
}
}
}
}
state MageResolve = 3 {
if k != 0 {
if e.hp <= 0 {
grant_xp(e.xp)
setreg(R_GOLD, reg(R_GOLD) + e.xp)
if reg(R_ETYPE) == 2 { setreg(R_BOSS, 1) setreg(R_MODE, 3) setreg(R_ACK, 0) }
else { setreg(R_MODE, 0) }
become KnightMenu
} else {
become EnemyTurn
}
}
}
state EnemyTurn = 4 {
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
setreg(R_EDMG, ed)
done = 1
}
}
}
let alive = 0
for (t4) in query [Stats, {Party}] {
if t4.hp > 0 { alive = alive + 1 }
}
if alive == 0 { setreg(R_MODE, 2) setreg(R_ACK, 0) }
else {
become KnightMenu
setreg(R_CUR, 0)
}
}
}
}
}
}
}
system CleanBattle phase LateUpdate {
if reg(R_MODE) != 1 {
for (e) in query [Stats, {Enemy}] {
despawn self()
}
}
}
system Meta phase Update {
let k = key()
if reg(R_MODE) == 2 {
if k == 0 { setreg(R_ACK, 1) }
if k != 0 {
if reg(R_ACK) == 1 {
for (h) in query [Stats, {Party}] { h.hp = h.maxhp }
setreg(R_MODE, 0)
}
}
}
if reg(R_MODE) == 3 {
if k == 0 { setreg(R_ACK, 1) }
if k != 0 {
if reg(R_ACK) == 1 { setreg(R_MODE, 0) }
}
}
}

View file

@ -0,0 +1,138 @@
# draw.ludic — the Render system (overworld, battle, and overlay screens).
system Draw phase Render {
let m = reg(R_MODE)
clear(0x101018)
if m == 0 {
let cx = 0
let cy = 0
for (p) in query [Pos, {Player}] {
cx = clamp(p.x * 16 - 152, 0, MW * 16 - 320)
cy = clamp(p.y * 16 - 112, 0, MH * 16 - 240)
}
let tx0 = cx / 16
let ty0 = cy / 16
for ty in ty0 .. ty0 + 16 {
for tx in tx0 .. tx0 + 21 {
let c = tile(tx, ty)
let sx = tx * 16 - cx
let sy = ty * 16 - cy
let base = SPR_GRASS
match c {
'r' => base = SPR_PATH
',', 'S', 'B' => base = SPR_FLOOR
'=' => base = SPR_WALL
}
draw_sprite(base, sx, sy)
if c == 'T' { draw_sprite(SPR_TREE, sx, sy) }
if c == '#' { draw_sprite(SPR_TREE, sx, sy) }
if c == 'H' { draw_sprite(SPR_HOUSE, sx, sy) }
if c == 'D' { draw_sprite(SPR_DOOR, sx, sy) }
if c == 'S' { draw_sprite(SPR_DOOR, sx, sy) }
if c == 'B' {
if reg(R_BOSS) == 0 { draw_sprite(SPR_BOSS, sx, sy) }
}
}
}
for (p, a) in query [Pos, Actor] {
draw_sprite(a.kind, p.x * 16 - cx, p.y * 16 - cy)
}
for (h, pp) in query [Stats, Party] {
if pp.slot == 0 {
fill_rect(4, 4, 96, 38, 0x0c0c16)
frame_rect(4, 4, 96, 38, 0x3a3a55)
text(8, 8, "LV", 0xffe08a, 1)
text_int(24, 8, h.lvl, 0xffffff, 1)
text(8, 18, "HP", 0x88ff88, 1)
text_int(24, 18, h.hp, 0xffffff, 1)
text(8, 28, "G", 0xffd24a, 1)
text_int(18, 28, reg(R_GOLD), 0xffffff, 1)
text(54, 28, "PO", 0x88ffcc, 1)
text_int(72, 28, reg(R_POTION), 0xffffff, 1)
}
}
}
if m == 1 {
fill_rect(0, 0, 320, 176, 0x241a2e)
for (e, a) in query [Stats, Actor, {Enemy}] {
let bs = 5
if reg(R_ETYPE) == 2 { bs = 7 }
draw_sprite_scaled(a.kind, 160 - bs * 8, 44, bs)
text(100, 20, enemy_name(reg(R_ETYPE)), 0xffffff, 1)
fill_rect(100, 30, 120, 8, 0x401018)
fill_rect(100, 30, e.hp * 120 / e.maxhp, 8, 0xe04040)
}
let ph = reg(R_PHASE)
let act = 0
if ph >= 2 { act = 1 }
if ph >= 4 { act = 2 }
fill_rect(0, 176, 320, 64, 0x0c0c16)
frame_rect(0, 176, 320, 64, 0x3a3a55)
for (st, pt) in query [Stats, Party] {
let hx = 6 + pt.slot * 92
let spr = 5
if pt.slot == 1 { spr = 9 }
if pt.slot == act { frame_rect(hx - 2, 180, 88, 44, 0xffff00) }
draw_sprite_scaled(spr, hx, 184, 2)
if pt.slot == 0 { text(hx + 36, 182, "KNIGHT", 0xffe08a, 1) }
if pt.slot == 1 { text(hx + 36, 182, "MAGE", 0xc9a0ff, 1) }
text(hx + 36, 195, "HP", 0x88ff88, 1)
text_int(hx + 56, 195, st.hp, 0xffffff, 1)
text(hx + 36, 207, "MP", 0x88aaff, 1)
text_int(hx + 56, 207, st.mp, 0xffffff, 1)
}
let cur = reg(R_CUR)
if ph == 0 or ph == 1 {
text(206, 181, "P1 KNIGHT", 0xffe08a, 1)
text(214, 192, "ATTACK", 0xffffff, 1)
text(214, 203, "DEFEND", 0xffffff, 1)
text(214, 214, "ITEM", 0xffffff, 1)
text(256, 214, "x", 0x777777, 1)
text_int(264, 214, reg(R_POTION), 0xffd24a, 1)
text(214, 225, "RUN", 0xffffff, 1)
if ph == 0 { text(204, 192 + cur * 11, ">", 0xffff00, 1) }
}
if ph == 2 or ph == 3 {
text(206, 182, "P2 MAGE", 0xc9a0ff, 1)
text(214, 196, "ATTACK", 0xffffff, 1)
text(214, 208, "HEAL", 0xffffff, 1)
text(214, 220, "DEFEND", 0xffffff, 1)
if ph == 2 { text(204, 196 + cur * 12, ">", 0xffff00, 1) }
}
if ph == 4 { text(206, 200, "ENEMY TURN", 0xff8888, 1) }
if ph == 1 or ph == 3 {
text(6, 232, "DMG", 0xffff88, 1)
text_int(34, 232, reg(R_PDMG), 0xffffff, 1)
}
if ph == 4 {
text(6, 232, "FOE HITS", 0xff8888, 1)
text_int(66, 232, reg(R_EDMG), 0xffffff, 1)
}
}
if m == 2 {
text(112, 104, "YOU FELL", 0xff5555, 2)
text(84, 140, "PRESS ANY KEY", 0xffffff, 1)
}
if m == 3 {
text(60, 96, "THE RIFT IS SEALED", 0x88ffcc, 2)
text(96, 132, "VICTORY!", 0xffe060, 3)
text(84, 170, "PRESS ANY KEY", 0xffffff, 1)
}
if m == 4 {
fill_rect(60, 60, 200, 120, 0x141422)
frame_rect(60, 60, 200, 120, 0x6a6a95)
text(96, 72, "ITEM SHOP", 0xffe060, 2)
text(80, 104, "BUY POTION", 0xffffff, 1)
text(180, 104, "10 G", 0xffd24a, 1)
text(80, 122, "LEAVE", 0xffffff, 1)
text(70, 104 + reg(R_CUR) * 18, ">", 0xffff00, 1)
text(80, 150, "GOLD", 0xffd24a, 1)
text_int(120, 150, reg(R_GOLD), 0xffffff, 1)
text(160, 150, "POTIONS", 0x88ffcc, 1)
text_int(226, 150, reg(R_POTION), 0xffffff, 1)
}
present()
}

View file

@ -0,0 +1,140 @@
# overworld.ludic — boot, movement/encounter input, and the shop system.
system Boot phase Start {
load_png("assets/kenney/tiny-town/Tiles/tile_0000.png") # 0 grass
load_png("assets/kenney/tiny-town/Tiles/tile_0004.png") # 1 tree
load_png("assets/kenney/tiny-town/Tiles/tile_0025.png") # 2 path
load_png("assets/kenney/tiny-town/Tiles/tile_0073.png") # 3 house
load_png("assets/kenney/tiny-town/Tiles/tile_0112.png") # 4 arch
load_png("assets/kenney/tiny-dungeon/Tiles/tile_0096.png") # 5 hero
load_png("assets/kenney/tiny-dungeon/Tiles/tile_0108.png") # 6 slime
load_png("assets/kenney/tiny-dungeon/Tiles/tile_0120.png") # 7 skeleton
load_png("assets/kenney/tiny-dungeon/Tiles/tile_0110.png") # 8 boss
load_png("assets/kenney/tiny-dungeon/Tiles/tile_0084.png") # 9 mage
load_png("assets/kenney/tiny-dungeon/Tiles/tile_0048.png") # 10 floor
load_png("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 }
}
setreg(R_MODE, 0)
setreg(R_ENC, 4)
setreg(R_MAP, 0)
setreg(R_BOSS, 0)
setreg(R_GOLD, 20)
setreg(R_POTION, 2)
}
system Control phase Input {
if reg(R_MODE) == 0 {
for (p) in query [Pos, {Player}] {
let k = key()
let nx = p.x
let 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 }
let 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
setreg(R_MAP, 1)
}
if tc == 'S' {
build_field(0)
p.x = 36
p.y = 3
setreg(R_MAP, 0)
}
if tc == 'B' {
if reg(R_BOSS) == 0 {
setreg(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 }
}
setreg(R_MODE, 1)
setreg(R_PHASE, 0)
setreg(R_CUR, 0)
}
}
if tc == 'h' {
setreg(R_MODE, 4)
setreg(R_CUR, 0)
}
if tc != 'D' {
if tc != 'S' {
if tc != 'B' {
if tc != 'h' {
setreg(R_ENC, reg(R_ENC) - 1)
if reg(R_ENC) <= 0 {
setreg(R_ENC, rng_range(4, 9))
let et = 0
if reg(R_MAP) == 1 { et = 1 }
if reg(R_MAP) == 0 {
if rng_chance(40) { et = 1 }
}
setreg(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 }
}
}
setreg(R_MODE, 1)
setreg(R_PHASE, 0)
setreg(R_CUR, 0)
}
}
}
}
}
}
if k == 'k' { save() }
if k == 'l' { if load() { } }
}
}
}
# shop (at the house 'h' tile): buy potions with gold
system Shop phase Update {
if reg(R_MODE) == 4 {
let k = key()
if k == 'w' { setreg(R_CUR, max(0, reg(R_CUR) - 1)) }
if k == 's' { setreg(R_CUR, min(1, reg(R_CUR) + 1)) }
if is_confirm(k) {
if reg(R_CUR) == 0 {
if reg(R_GOLD) >= 10 {
setreg(R_GOLD, reg(R_GOLD) - 10)
setreg(R_POTION, reg(R_POTION) + 1)
}
}
if reg(R_CUR) == 1 { setreg(R_MODE, 0) }
}
}
}

View file

@ -0,0 +1,86 @@
# rules.ludic — pure game rules & map data (functions only, no systems).
fn tile_sprite(c: int) -> int {
match c {
'T', '#' => return SPR_TREE
'H' => return SPR_HOUSE
'D' => return SPR_DOOR
'r', 'h' => return SPR_PATH
',', 'S', 'B' => return SPR_FLOOR
'=' => return SPR_WALL
}
return SPR_GRASS
}
fn walkable(x: int, y: int) -> bool {
let c = tile(x, y)
return c == '.' or c == 'D' or c == 'r' or c == ',' or c == 'S' or c == 'B' or c == 'h'
}
fn is_confirm(k: int) -> bool { return k == 32 or k == 10 or k == 13 }
fn enemy_name(et: int) -> str {
if et == 0 { return "SLIME" }
if et == 1 { return "SKELETON" }
return "RIFT WARDEN"
}
fn build_field(x: int) -> int {
map_size(40, 18)
map_row(0, "########################################")
map_row(1, "#......................................#")
map_row(2, "#...TT.........................TT.....D#")
map_row(3, "#..TT.......................TT.........#")
map_row(4, "#..T.........................T.........#")
map_row(5, "#....................................T.#")
map_row(6, "#.....TT....................TT.......T.#")
map_row(7, "#....TT.....................T..........#")
map_row(8, "#...........rrrrrrrr........HH.........#")
map_row(9, "#..........r........r.......HH.........#")
map_row(10, "#......TT...r........rrrrrrrrhr......T.#")
map_row(11, "#.....TT...r.................r.......T.#")
map_row(12, "#..........r.......TT.......r..........#")
map_row(13, "#..........rrrrrrrrrrrrrrrrr.........T.#")
map_row(14, "#.........TT.........................T.#")
map_row(15, "#........TT..........................T.#")
map_row(16, "#....................................T.#")
map_row(17, "########################################")
return 0
}
fn build_dungeon(x: int) -> int {
map_size(40, 18)
map_row(0, "========================================")
map_row(1, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(2, "=,S,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(3, "=,,,,,====,,,,,,,,,,,,,,,====,,,,,,,,,,=")
map_row(4, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(5, "=,,,,,,,,,,,,,====,,,,,,,,,,,,,,,,,,,,,=")
map_row(6, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,====,,,,,=")
map_row(7, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(8, "=,,,====,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(9, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(10, "=,,,,,,,,,,,,,,,,,,====,,,,,,,,,,,,,,,,=")
map_row(11, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(12, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,====,,,,,,,=")
map_row(13, "=,,,,,,,,,====,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(14, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(15, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,B,,=")
map_row(16, "=,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,=")
map_row(17, "========================================")
return 0
}
fn grant_xp(amount: int) -> int {
for (t) in query [Stats, {Party}] {
if t.hp > 0 {
t.xp = t.xp + amount
while t.xp >= t.lvl * 20 {
t.xp = t.xp - t.lvl * 20
t.lvl = t.lvl + 1
t.maxhp = t.maxhp + 8
t.hp = t.maxhp
t.maxmp = t.maxmp + 2
t.atk = t.atk + 2
t.def = t.def + 1
}
}
}
return 0
}

View file

@ -0,0 +1,43 @@
# world.ludic — the data model: components, entity archetypes, constants.
# Imported by examples/chronorift.ludic. No `game` wrapper (this is a fragment).
component Pos { x: int = 0, y: int = 0 }
component Actor { kind: int = 0 }
component Stats { hp: int = 1, maxhp: int = 1, mp: int = 0, maxmp: int = 0,
atk: int = 1, def: int = 0, lvl: int = 1, xp: int = 0, guard: int = 0 }
component Party { slot: int = 0 }
# Entity KINDS. Identity is stored as one integer per entity (L_kind), not as
# an empty marker component. Query membership with {Player} / {Enemy}.
archetype Player { Pos, Actor, Party, Stats } # the controllable knight
archetype Ally { Party, Stats } # backline mage (no overworld body)
archetype Enemy { Actor, Stats } # battle foe
const MW: int = 40
const MH: int = 18
const SPR_GRASS: int = 0
const SPR_TREE: int = 1
const SPR_PATH: int = 2
const SPR_HOUSE: int = 3
const SPR_DOOR: int = 4
const SPR_HERO: int = 5
const SPR_SLIME: int = 6
const SPR_SKEL: int = 7
const SPR_BOSS: int = 8
const SPR_MAGE: int = 9
const SPR_FLOOR: int = 10
const SPR_WALL: int = 11
const R_MODE: int = 0 # 0 field, 1 battle, 2 gameover, 3 victory
const R_PHASE: int = 1
const R_CUR: int = 2
const R_ENC: int = 3
const R_ETYPE: int = 4 # 0 slime, 1 skeleton, 2 boss
const R_PDMG: int = 5
const R_EDMG: int = 6
const R_GOLD: int = 7
const R_MAP: int = 8 # 0 overworld, 1 dungeon
const R_BOSS: int = 9 # boss defeated flag
const R_ACK: int = 10 # debounce for end screens
const R_POTION: int = 11 # potions in inventory

34
examples/hello.ludic Normal file
View file

@ -0,0 +1,34 @@
# hello.ludic — smallest program that exercises the ECS pipeline
game Hello {
component Pos { x: int = 0, y: int = 0 }
component Vel { dx: int = 0, dy: int = 0 }
system Boot phase Start {
spawn Mob {
Pos = { x = 3, y = 4 }
Vel = { dx = 1, dy = 0 }
}
spawn Mob {
Pos = { x = 10, y = 2 }
Vel = { dx = 0, dy = 1 }
}
}
# a system can declare the entities it operates on: the body then runs once
# per match, with the components bound and self() giving that entity.
system Move phase FixedUpdate
query (p, v) [Pos, Vel]
{
p.x = p.x + v.dx
p.y = p.y + v.dy
}
system Report phase Update {
for (p) in query [Pos] {
print_int(p.x)
print_int(p.y)
}
quit()
}
}

34
examples/lib/arena.ludic Normal file
View file

@ -0,0 +1,34 @@
# ============================================================================
# arena.ludic — a game that links the Ludic shared library next to it.
#
# ludicc examples/lib/combat.ludic --shared -o build/libcombat.dylib
# ludicc examples/lib/arena.ludic -o build/arena -Lbuild -lcombat
#
# `extern fn` binds a name to a symbol resolved at link time. The library the
# symbols come from happens to be written in Ludic, but nothing here depends on
# that: the same declaration binds a C, Rust or Zig library just as well. This
# is how Ludic reaches the outside world without a C shim.
# ============================================================================
game Arena {
extern fn damage(attack: int, armour: int, roll: int) -> int = "damage"
extern fn hits_to_kill(hp: int, attack: int, armour: int) -> int = "hits_to_kill"
extern fn xp_for(level: int, kills: int) -> int = "xp_for"
component Fighter { hp: int = 30, attack: int = 9, armour: int = 2 }
system Boot phase Start {
spawn Hero { Fighter = { hp = 40, attack = 12, armour = 3 } }
spawn Orc { Fighter = { hp = 24, attack = 7, armour = 1 } }
}
system Fight phase Update {
for (f) in query [Fighter] {
print_int(damage(f.attack, f.armour, 0))
print_int(damage(f.attack, f.armour, 20))
print_int(hits_to_kill(f.hp, f.attack, f.armour))
}
print_int(xp_for(3, 4))
quit()
}
}

46
examples/lib/combat.ludic Normal file
View file

@ -0,0 +1,46 @@
# ============================================================================
# combat.ludic — a Ludic *library*, not a game.
#
# `module` instead of `game` means: no entry point, no frame loop. ludicc
# compiles this to a real shared object —
#
# ludicc examples/lib/combat.ludic --shared -o build/libcombat.dylib
#
# — whose `export fn`s are ordinary C-ABI symbols. Anything that can call a
# .dylib/.so/.dll can call these: another Ludic program via `extern fn`, a game
# engine, a test harness, a scripting host. Non-exported functions stay private
# to the library.
# ============================================================================
module Combat {
const CRIT_MULT: int = 2
# damage after armour, with a crit multiplier applied on an exact roll
export fn damage(attack: int, armour: int, roll: int) -> int {
let raw = attack - armour
if raw < 1 { raw = 1 }
if roll == 20 { raw = raw * CRIT_MULT }
return raw
}
# how many hits to drop a target — the loop is here so the caller cannot
# accidentally disagree with `damage` about rounding
export fn hits_to_kill(hp: int, attack: int, armour: int) -> int {
let left = hp
let n = 0
while left > 0 {
left = left - damage(attack, armour, 0)
n = n + 1
}
return n
}
export fn xp_for(level: int, kills: int) -> int {
return curve(level) * kills
}
# private: not exported, so it is not a symbol in the library
fn curve(level: int) -> int {
return 10 + level * level * 3
}
}

41
examples/menu.ludic Normal file
View file

@ -0,0 +1,41 @@
# menu.ludic — a retained-UI title screen. The UI is declared as DATA (the `ui`
# block); the engine lays it out (stacked panel, padding/gap/alignment), draws
# it (background, TrueType labels, focusable buttons), and routes keyboard focus
# and activation. The game only loads a font, opens the UI, and reads clicks.
game Menu {
const R_FONT: int = 0
const R_MSG: int = 1
ui MainMenu {
panel id=Root w=288 pad=16 gap=6 bg=0x1a1a2c border=0x4a4a72 skin="assets/ui/panel.png" inset=10 align=center {
label text="CHRONO RIFT" font=reg(R_FONT) size=26 fg=0xffe060 align=center
label text="a Ludic retained-UI demo" font=reg(R_FONT) size=14 fg=0x9aa0c8 align=center
spacer h=4
button id=NewGame text="New Game" font=reg(R_FONT) size=16 w=236
button id=Continue text="Continue" font=reg(R_FONT) size=16 w=236
button id=Options text="Options" font=reg(R_FONT) size=16 w=236
button id=Quit text="Quit" font=reg(R_FONT) size=16 w=236
}
}
system Boot phase Start {
setreg(R_FONT, font_load("/System/Library/Fonts/Supplemental/Arial.ttf"))
ui_build()
ui_open(UI_MainMenu)
}
system Nav phase Update {
ui_tick(key())
if ui_clicked(UI_NewGame) { ui_set_text(UI_NewGame, "Starting...") }
if ui_clicked(UI_Continue) { ui_set_text(UI_Continue, "No save") }
if ui_clicked(UI_Options) { ui_set_text(UI_Options, "Soon (TM)") }
if ui_clicked(UI_Quit) { quit() }
}
system Draw phase Render {
clear(0x0e0e16)
ui_render()
present()
}
}

37
examples/qdecl.ludic Normal file
View file

@ -0,0 +1,37 @@
# ============================================================================
# qdecl.ludic — a system whose query lives in its signature.
#
# system CleanBattle phase LateUpdate
# query (b, p) [Battle, Pos, {Foe}] where b.hp <= 0
# { … }
#
# is the same thing as writing `for (b, p) in query [...] where ... { … }` around
# the whole body — the loop header just moves into the declaration. The body runs
# once per matching entity, and self() is that entity.
# ============================================================================
game QueryDecl {
component Battle { hp: int = 0, side: int = 0 }
component Pos { x: int = 0, y: int = 0 }
archetype Foe { Battle, Pos }
system Seed phase Start {
spawn Foe { Battle = { hp = 3, side = 1 } }
spawn Foe { Battle = { hp = 0, side = 1 } }
spawn Foe { Battle = { hp = -2, side = 0 } }
}
system CleanBattle phase LateUpdate
query (b, p) [Battle, Pos, {Foe}] where b.hp <= 0 and b.side == 1
{
print_int(p.x)
despawn self()
}
system Census phase Render
query (b) [Battle]
{
print_int(b.hp)
}
system Bye phase Render { quit() }
}

38
examples/scenes.ludic Normal file
View file

@ -0,0 +1,38 @@
# ⚠️ NOT YET IMPLEMENTED by the self-hosted compiler. `scene` / `layer` /
# `on enter` / `on exit` have no parser support today, so this file does NOT
# compile with build/ludicc — it is a design sketch of the intended syntax (see
# LANGUAGE.md §"Scenes & layers"). It is deliberately excluded from test.sh.
# Games that need scene-like states use a mode register + `machine` today, as
# examples/chronorift does.
game SceneDemo {
const R_N: int = 0
system Boot phase Start { setreg(R_N, 0) print_int(1000) }
scene Title start {
on enter { print_int(1) }
on exit { print_int(2) }
layer Main {
system Tick phase Update {
setreg(R_N, reg(R_N) + 1)
print_int(100 + reg(R_N))
if reg(R_N) >= 2 { enter Play }
}
}
}
scene Play {
on enter { print_int(3) setreg(R_N, 0) }
on exit { print_int(4) }
layer World {
system Step phase Update {
setreg(R_N, reg(R_N) + 1)
print_int(200 + reg(R_N))
if reg(R_N) >= 2 { quit() }
}
}
layer Hud {
system Draw phase Render { print_int(900) }
}
}
}

136
examples/snake.ludic Normal file
View file

@ -0,0 +1,136 @@
# ============================================================================
# snake.ludic — a second, unrelated game in Ludic, to prove the language is
# general: compiled by the SAME unmodified ludicc + runtime as the JRPG.
# No sprites/assets — pure primitives. Build: ./build.sh examples/snake.ludic
# ============================================================================
game Snake {
component Pos { x: int = 0, y: int = 0 }
component Seg { order: int = 0 }
const GW: int = 20
const GH: int = 15
const TS: int = 16
const R_DIR: int = 0 # 0 up 1 down 2 left 3 right
const R_LEN: int = 1
const R_TICK: int = 2
const R_FX: int = 3
const R_FY: int = 4
const R_MODE: int = 5 # 0 play 1 gameover
const R_SCORE: int = 6
const R_ATE: int = 7
const R_TX: int = 8
const R_TY: int = 9
const R_ACK: int = 10
fn reset(x: int) -> int {
for (p, s) in query [Pos, Seg] { despawn self() }
spawn S0 { Seg = { order = 0 } Pos = { x = 10, y = 7 } }
spawn S1 { Seg = { order = 1 } Pos = { x = 9, y = 7 } }
spawn S2 { Seg = { order = 2 } Pos = { x = 8, y = 7 } }
setreg(R_DIR, 3)
setreg(R_LEN, 3)
setreg(R_TICK, 0)
setreg(R_MODE, 0)
setreg(R_SCORE, 0)
setreg(R_FX, 14)
setreg(R_FY, 7)
return 0
}
system Boot phase Start { reset(0) }
system Input phase Input {
let k = key()
if reg(R_MODE) == 0 {
if k == 'w' { if reg(R_DIR) != 1 { setreg(R_DIR, 0) } }
if k == 's' { if reg(R_DIR) != 0 { setreg(R_DIR, 1) } }
if k == 'a' { if reg(R_DIR) != 3 { setreg(R_DIR, 2) } }
if k == 'd' { if reg(R_DIR) != 2 { setreg(R_DIR, 3) } }
}
if reg(R_MODE) == 1 {
if k == 0 { setreg(R_ACK, 1) }
if k != 0 { if reg(R_ACK) == 1 { reset(0) } }
}
}
system Move phase Update {
if reg(R_MODE) == 0 {
setreg(R_TICK, reg(R_TICK) + 1)
if reg(R_TICK) >= 6 {
setreg(R_TICK, 0)
let len = reg(R_LEN)
for (pt, stg) in query [Pos, Seg] {
if stg.order == len - 1 { setreg(R_TX, pt.x) setreg(R_TY, pt.y) }
}
for step in 0 .. len {
let i = len - 1 - step
if i >= 1 {
for (pa, sa) in query [Pos, Seg] {
if sa.order == i {
for (pb, sb) in query [Pos, Seg] {
if sb.order == i - 1 { pa.x = pb.x pa.y = pb.y }
}
}
}
}
}
setreg(R_ATE, 0)
for (ph, sh) in query [Pos, Seg] {
if sh.order == 0 {
if reg(R_DIR) == 0 { ph.y = ph.y - 1 }
if reg(R_DIR) == 1 { ph.y = ph.y + 1 }
if reg(R_DIR) == 2 { ph.x = ph.x - 1 }
if reg(R_DIR) == 3 { ph.x = ph.x + 1 }
if ph.x < 0 { setreg(R_MODE, 1) }
if ph.x >= GW { setreg(R_MODE, 1) }
if ph.y < 0 { setreg(R_MODE, 1) }
if ph.y >= GH { setreg(R_MODE, 1) }
if ph.x == reg(R_FX) { if ph.y == reg(R_FY) { setreg(R_ATE, 1) } }
}
}
for (ph, sh) in query [Pos, Seg] {
if sh.order == 0 {
for (pb, sb) in query [Pos, Seg] {
if sb.order > 0 {
if pb.x == ph.x { if pb.y == ph.y { setreg(R_MODE, 1) } }
}
}
}
}
if reg(R_ATE) == 1 {
setreg(R_SCORE, reg(R_SCORE) + 1)
spawn Body { Seg = { order = len } Pos = { x = reg(R_TX), y = reg(R_TY) } }
setreg(R_LEN, len + 1)
setreg(R_FX, rng_range(0, GW - 1))
setreg(R_FY, rng_range(0, GH - 1))
}
}
}
}
system Draw phase Render {
clear(0x0d1020)
for gy in 0 .. GH {
for gx in 0 .. GW {
let col = 0x141a2e
if (gx + gy) % 2 == 0 { col = 0x10162a }
fill_rect(gx * TS, gy * TS, TS, TS, col)
}
}
fill_rect(reg(R_FX) * TS + 3, reg(R_FY) * TS + 3, TS - 6, TS - 6, 0xe0405a)
for (p, s) in query [Pos, Seg] {
let col = 0x40c060
if s.order == 0 { col = 0x9dffb0 }
fill_rect(p.x * TS + 1, p.y * TS + 1, TS - 2, TS - 2, col)
}
text(6, 4, "SCORE", 0xffffff, 1)
text_int(52, 4, reg(R_SCORE), 0xffe060, 1)
if reg(R_MODE) == 1 {
text(112, 104, "GAME OVER", 0xff5555, 2)
text(84, 140, "PRESS ANY KEY", 0xffffff, 1)
}
present()
}
}