Language: named colors, namespaced Screen/Input/Random/Map API, named args
An opinionated, game-facing surface for the language: - Color.<Name>: 221 named colors resolved to 0xRRGGBB at compile time (selfhost/emit_color.ludic). - Namespaced builtins Screen.* / Input.key / Random.* / Map.*, so calls read as subject.action; present() -> Screen.show(), clear -> Screen.clear, fill_rect -> Screen.fill_rectangle, etc. - Named arguments, e.g. Screen.fill_rectangle(x:, y:, width:, height:, color:), reordered to the callee's parameters at emit time. - machine/become can run over a named program-scope var, not just a register. - Migrate examples off numeric registers to named vars; snake/menu/chronorift render byte-identical to the goldens and the bootstrap fixpoint is preserved. - Regenerate the checked-in seed (selfhost/ludicc.seed.ll). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
9e4837428c
commit
a3a1e4d160
14 changed files with 11784 additions and 7493 deletions
|
|
@ -1,50 +1,50 @@
|
|||
# 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).
|
||||
# The battle is a state machine over the `turn_phase` variable. `machine`
|
||||
# dispatches on its value 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).
|
||||
# Menu selections, named instead of matched on bare integers (menu_cursor 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()
|
||||
if game_mode == 1 {
|
||||
let k = Input.key()
|
||||
for (e) in query [Stats, {Enemy}] {
|
||||
machine R_PHASE {
|
||||
machine turn_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)
|
||||
menu_cursor = 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 k == 'w' { menu_cursor = max(0, menu_cursor - 1) }
|
||||
if k == 's' { menu_cursor = min(3, menu_cursor + 1) }
|
||||
if is_confirm(k) {
|
||||
match reg(R_CUR) {
|
||||
match menu_cursor {
|
||||
KnightAct.Attack => {
|
||||
let d = max(1, st.atk - e.def + rng_range(0, 4))
|
||||
let d = max(1, st.atk - e.def + Random.range(0, 4))
|
||||
e.hp = e.hp - d
|
||||
set_reg(R_PDMG, d)
|
||||
player_hit = d
|
||||
}
|
||||
KnightAct.Guard => { st.guard = 1; set_reg(R_PDMG, 0) }
|
||||
KnightAct.Guard => { st.guard = 1; player_hit = 0 }
|
||||
KnightAct.Item => {
|
||||
if reg(R_POTION) > 0 {
|
||||
set_reg(R_POTION, reg(R_POTION) - 1)
|
||||
if potions > 0 {
|
||||
potions = potions - 1
|
||||
st.hp = min(st.maxhp, st.hp + 24)
|
||||
set_reg(R_PDMG, 24)
|
||||
} else { set_reg(R_PDMG, 0) }
|
||||
player_hit = 24
|
||||
} else { player_hit = 0 }
|
||||
}
|
||||
KnightAct.Flee => {
|
||||
if rng_chance(50) { e.hp = 0; set_reg(R_MODE, 0) }
|
||||
else { set_reg(R_PDMG, 0) }
|
||||
if Random.chance(50) { e.hp = 0; game_mode = 0 }
|
||||
else { player_hit = 0 }
|
||||
}
|
||||
}
|
||||
become KnightResolve
|
||||
set_reg(R_CUR, 0)
|
||||
menu_cursor = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -55,13 +55,13 @@ handler Battle phase Update {
|
|||
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) }
|
||||
gold = gold + e.xp
|
||||
if enemy_type == 2 { boss_down = 1; game_mode = 3; ack = 0 }
|
||||
else { game_mode = 0 }
|
||||
become KnightMenu
|
||||
} else {
|
||||
become MageMenu
|
||||
set_reg(R_CUR, 0)
|
||||
menu_cursor = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -72,14 +72,14 @@ handler Battle phase Update {
|
|||
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 == 'i' { menu_cursor = max(0, menu_cursor - 1) }
|
||||
if k == 'k' { menu_cursor = min(2, menu_cursor + 1) }
|
||||
if k == 'j' {
|
||||
match reg(R_CUR) {
|
||||
match menu_cursor {
|
||||
MageAct.Attack => {
|
||||
let d = max(1, st.atk + 5 - e.def + rng_range(0, 5))
|
||||
let d = max(1, st.atk + 5 - e.def + Random.range(0, 5))
|
||||
e.hp = e.hp - d
|
||||
set_reg(R_PDMG, d)
|
||||
player_hit = d
|
||||
}
|
||||
MageAct.Heal => {
|
||||
if st.mp >= 4 {
|
||||
|
|
@ -87,13 +87,13 @@ handler Battle phase Update {
|
|||
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) }
|
||||
player_hit = 16
|
||||
} else { player_hit = 0 }
|
||||
}
|
||||
MageAct.Guard => { st.guard = 1; set_reg(R_PDMG, 0) }
|
||||
MageAct.Guard => { st.guard = 1; player_hit = 0 }
|
||||
}
|
||||
become MageResolve
|
||||
set_reg(R_CUR, 0)
|
||||
menu_cursor = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -104,9 +104,9 @@ handler Battle phase Update {
|
|||
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) }
|
||||
gold = gold + e.xp
|
||||
if enemy_type == 2 { boss_down = 1; game_mode = 3; ack = 0 }
|
||||
else { game_mode = 0 }
|
||||
become KnightMenu
|
||||
} else {
|
||||
become EnemyTurn
|
||||
|
|
@ -120,10 +120,10 @@ handler Battle phase Update {
|
|||
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))
|
||||
var ed = max(1, e.atk - t3.def + Random.range(0, 4))
|
||||
if t3.guard == 1 { ed = ed / 2; t3.guard = 0 }
|
||||
t3.hp = t3.hp - ed
|
||||
set_reg(R_EDMG, ed)
|
||||
enemy_hit = ed
|
||||
done = 1
|
||||
}
|
||||
}
|
||||
|
|
@ -132,10 +132,10 @@ handler Battle phase Update {
|
|||
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) }
|
||||
if alive == 0 { game_mode = 2; ack = 0 }
|
||||
else {
|
||||
become KnightMenu
|
||||
set_reg(R_CUR, 0)
|
||||
menu_cursor = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -145,7 +145,7 @@ handler Battle phase Update {
|
|||
}
|
||||
|
||||
handler CleanBattle phase LateUpdate {
|
||||
if reg(R_MODE) != 1 {
|
||||
if game_mode != 1 {
|
||||
for (e) in query [Stats, {Enemy}] {
|
||||
despawn self()
|
||||
}
|
||||
|
|
@ -153,20 +153,20 @@ handler CleanBattle phase LateUpdate {
|
|||
}
|
||||
|
||||
handler Meta phase Update {
|
||||
let k = key()
|
||||
if reg(R_MODE) == 2 {
|
||||
if k == 0 { set_reg(R_ACK, 1) }
|
||||
let k = Input.key()
|
||||
if game_mode == 2 {
|
||||
if k == 0 { ack = 1 }
|
||||
if k != 0 {
|
||||
if reg(R_ACK) == 1 {
|
||||
if ack == 1 {
|
||||
for (h) in query [Stats, {Party}] { h.hp = h.maxhp }
|
||||
set_reg(R_MODE, 0)
|
||||
game_mode = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
if reg(R_MODE) == 3 {
|
||||
if k == 0 { set_reg(R_ACK, 1) }
|
||||
if game_mode == 3 {
|
||||
if k == 0 { ack = 1 }
|
||||
if k != 0 {
|
||||
if reg(R_ACK) == 1 { set_reg(R_MODE, 0) }
|
||||
if ack == 1 { game_mode = 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
# draw.ludic — the Render system (overworld, battle, and overlay screens).
|
||||
|
||||
handler Draw phase Render {
|
||||
let m = reg(R_MODE)
|
||||
clear(0x101018)
|
||||
let m = game_mode
|
||||
Screen.clear(0x101018)
|
||||
|
||||
if m == 0 {
|
||||
var cx = 0
|
||||
|
|
@ -15,7 +15,7 @@ handler Draw phase Render {
|
|||
let ty0 = cy / 16
|
||||
for ty in ty0 .. ty0 + 16 {
|
||||
for tx in tx0 .. tx0 + 21 {
|
||||
let c = tile(tx, ty)
|
||||
let c = Map.tile(tx, ty)
|
||||
let sx = tx * 16 - cx
|
||||
let sy = ty * 16 - cy
|
||||
var base = SPR_GRASS
|
||||
|
|
@ -31,7 +31,7 @@ handler Draw phase Render {
|
|||
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) }
|
||||
if boss_down == 0 { draw_sprite(SPR_BOSS, sx, sy) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,99 +40,99 @@ handler Draw phase Render {
|
|||
}
|
||||
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)
|
||||
Screen.fill_rectangle(4, 4, 96, 38, 0x0c0c16)
|
||||
Screen.draw_rectangle(4, 4, 96, 38, 0x3a3a55)
|
||||
Screen.draw_text(8, 8, "LV", 0xffe08a, 1)
|
||||
Screen.draw_number(24, 8, h.lvl, 0xffffff, 1)
|
||||
Screen.draw_text(8, 18, "HP", 0x88ff88, 1)
|
||||
Screen.draw_number(24, 18, h.hp, 0xffffff, 1)
|
||||
Screen.draw_text(8, 28, "G", 0xffd24a, 1)
|
||||
Screen.draw_number(18, 28, gold, 0xffffff, 1)
|
||||
Screen.draw_text(54, 28, "PO", 0x88ffcc, 1)
|
||||
Screen.draw_number(72, 28, potions, 0xffffff, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if m == 1 {
|
||||
fill_rect(0, 0, 320, 176, 0x241a2e)
|
||||
Screen.fill_rectangle(0, 0, 320, 176, 0x241a2e)
|
||||
for (e, a) in query [Stats, Actor, {Enemy}] {
|
||||
var bs = 5
|
||||
if reg(R_ETYPE) == 2 { bs = 7 }
|
||||
if enemy_type == 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)
|
||||
Screen.draw_text(100, 20, enemy_name(enemy_type), 0xffffff, 1)
|
||||
Screen.fill_rectangle(100, 30, 120, 8, 0x401018)
|
||||
Screen.fill_rectangle(100, 30, e.hp * 120 / e.maxhp, 8, 0xe04040)
|
||||
}
|
||||
let ph = reg(R_PHASE)
|
||||
let ph = turn_phase
|
||||
var 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)
|
||||
Screen.fill_rectangle(0, 176, 320, 64, 0x0c0c16)
|
||||
Screen.draw_rectangle(0, 176, 320, 64, 0x3a3a55)
|
||||
for (st, pt) in query [Stats, Party] {
|
||||
let hx = 6 + pt.slot * 92
|
||||
var spr = 5
|
||||
if pt.slot == 1 { spr = 9 }
|
||||
if pt.slot == act { frame_rect(hx - 2, 180, 88, 44, 0xffff00) }
|
||||
if pt.slot == act { Screen.draw_rectangle(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)
|
||||
if pt.slot == 0 { Screen.draw_text(hx + 36, 182, "KNIGHT", 0xffe08a, 1) }
|
||||
if pt.slot == 1 { Screen.draw_text(hx + 36, 182, "MAGE", 0xc9a0ff, 1) }
|
||||
Screen.draw_text(hx + 36, 195, "HP", 0x88ff88, 1)
|
||||
Screen.draw_number(hx + 56, 195, st.hp, 0xffffff, 1)
|
||||
Screen.draw_text(hx + 36, 207, "MP", 0x88aaff, 1)
|
||||
Screen.draw_number(hx + 56, 207, st.mp, 0xffffff, 1)
|
||||
}
|
||||
let cur = reg(R_CUR)
|
||||
let cur = menu_cursor
|
||||
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) }
|
||||
Screen.draw_text(206, 181, "P1 KNIGHT", 0xffe08a, 1)
|
||||
Screen.draw_text(214, 192, "ATTACK", 0xffffff, 1)
|
||||
Screen.draw_text(214, 203, "DEFEND", 0xffffff, 1)
|
||||
Screen.draw_text(214, 214, "ITEM", 0xffffff, 1)
|
||||
Screen.draw_text(256, 214, "x", 0x777777, 1)
|
||||
Screen.draw_number(264, 214, potions, 0xffd24a, 1)
|
||||
Screen.draw_text(214, 225, "RUN", 0xffffff, 1)
|
||||
if ph == 0 { Screen.draw_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) }
|
||||
Screen.draw_text(206, 182, "P2 MAGE", 0xc9a0ff, 1)
|
||||
Screen.draw_text(214, 196, "ATTACK", 0xffffff, 1)
|
||||
Screen.draw_text(214, 208, "HEAL", 0xffffff, 1)
|
||||
Screen.draw_text(214, 220, "DEFEND", 0xffffff, 1)
|
||||
if ph == 2 { Screen.draw_text(204, 196 + cur * 12, ">", 0xffff00, 1) }
|
||||
}
|
||||
if ph == 4 { text(206, 200, "ENEMY TURN", 0xff8888, 1) }
|
||||
if ph == 4 { Screen.draw_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)
|
||||
Screen.draw_text(6, 232, "DMG", 0xffff88, 1)
|
||||
Screen.draw_number(34, 232, player_hit, 0xffffff, 1)
|
||||
}
|
||||
if ph == 4 {
|
||||
text(6, 232, "FOE HITS", 0xff8888, 1)
|
||||
text_int(66, 232, reg(R_EDMG), 0xffffff, 1)
|
||||
Screen.draw_text(6, 232, "FOE HITS", 0xff8888, 1)
|
||||
Screen.draw_number(66, 232, enemy_hit, 0xffffff, 1)
|
||||
}
|
||||
}
|
||||
|
||||
if m == 2 {
|
||||
text(112, 104, "YOU FELL", 0xff5555, 2)
|
||||
text(84, 140, "PRESS ANY KEY", 0xffffff, 1)
|
||||
Screen.draw_text(112, 104, "YOU FELL", 0xff5555, 2)
|
||||
Screen.draw_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)
|
||||
Screen.draw_text(60, 96, "THE RIFT IS SEALED", 0x88ffcc, 2)
|
||||
Screen.draw_text(96, 132, "VICTORY!", 0xffe060, 3)
|
||||
Screen.draw_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)
|
||||
Screen.fill_rectangle(60, 60, 200, 120, 0x141422)
|
||||
Screen.draw_rectangle(60, 60, 200, 120, 0x6a6a95)
|
||||
Screen.draw_text(96, 72, "ITEM SHOP", 0xffe060, 2)
|
||||
Screen.draw_text(80, 104, "BUY POTION", 0xffffff, 1)
|
||||
Screen.draw_text(180, 104, "10 G", 0xffd24a, 1)
|
||||
Screen.draw_text(80, 122, "LEAVE", 0xffffff, 1)
|
||||
Screen.draw_text(70, 104 + menu_cursor * 18, ">", 0xffff00, 1)
|
||||
Screen.draw_text(80, 150, "GOLD", 0xffd24a, 1)
|
||||
Screen.draw_number(120, 150, gold, 0xffffff, 1)
|
||||
Screen.draw_text(160, 150, "POTIONS", 0x88ffcc, 1)
|
||||
Screen.draw_number(226, 150, potions, 0xffffff, 1)
|
||||
}
|
||||
present()
|
||||
Screen.show()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,18 +25,18 @@ handler Boot phase Start {
|
|||
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)
|
||||
game_mode = 0
|
||||
encounter = 4
|
||||
world_map = 0
|
||||
boss_down = 0
|
||||
gold = 20
|
||||
potions = 2
|
||||
}
|
||||
|
||||
handler Control phase Input {
|
||||
if reg(R_MODE) == 0 {
|
||||
if game_mode == 0 {
|
||||
for (p) in query [Pos, {Player}] {
|
||||
let k = key()
|
||||
let k = Input.key()
|
||||
var nx = p.x
|
||||
var ny = p.y
|
||||
if k == 'w' { ny = p.y - 1 }
|
||||
|
|
@ -52,48 +52,48 @@ handler Control phase Input {
|
|||
}
|
||||
}
|
||||
if moved == 1 {
|
||||
let tc = tile(p.x, p.y)
|
||||
let tc = Map.tile(p.x, p.y)
|
||||
if tc == 'D' {
|
||||
build_dungeon(0)
|
||||
p.x = 3
|
||||
p.y = 2
|
||||
set_reg(R_MAP, 1)
|
||||
world_map = 1
|
||||
}
|
||||
if tc == 'S' {
|
||||
build_field(0)
|
||||
p.x = 36
|
||||
p.y = 3
|
||||
set_reg(R_MAP, 0)
|
||||
world_map = 0
|
||||
}
|
||||
if tc == 'B' {
|
||||
if reg(R_BOSS) == 0 {
|
||||
set_reg(R_ETYPE, 2)
|
||||
if boss_down == 0 {
|
||||
enemy_type = 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)
|
||||
game_mode = 1
|
||||
turn_phase = 0
|
||||
menu_cursor = 0
|
||||
}
|
||||
}
|
||||
if tc == 'h' {
|
||||
set_reg(R_MODE, 4)
|
||||
set_reg(R_CUR, 0)
|
||||
game_mode = 4
|
||||
menu_cursor = 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))
|
||||
encounter = encounter - 1
|
||||
if encounter <= 0 {
|
||||
encounter = Random.range(4, 9)
|
||||
var et = 0
|
||||
if reg(R_MAP) == 1 { et = 1 }
|
||||
if reg(R_MAP) == 0 {
|
||||
if rng_chance(40) { et = 1 }
|
||||
if world_map == 1 { et = 1 }
|
||||
if world_map == 0 {
|
||||
if Random.chance(40) { et = 1 }
|
||||
}
|
||||
set_reg(R_ETYPE, et)
|
||||
enemy_type = et
|
||||
if et == 0 {
|
||||
spawn Enemy {
|
||||
Actor { kind: 6 }
|
||||
|
|
@ -106,9 +106,9 @@ handler Control phase Input {
|
|||
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)
|
||||
game_mode = 1
|
||||
turn_phase = 0
|
||||
menu_cursor = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -123,18 +123,18 @@ handler Control phase Input {
|
|||
|
||||
# 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 game_mode == 4 {
|
||||
let k = Input.key()
|
||||
if k == 'w' { menu_cursor = max(0, menu_cursor - 1) }
|
||||
if k == 's' { menu_cursor = min(1, menu_cursor + 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 menu_cursor == 0 {
|
||||
if gold >= 10 {
|
||||
gold = gold - 10
|
||||
potions = potions + 1
|
||||
}
|
||||
}
|
||||
if reg(R_CUR) == 1 { set_reg(R_MODE, 0) }
|
||||
if menu_cursor == 1 { game_mode = 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ fn tile_sprite(c: int) -> int {
|
|||
return SPR_GRASS
|
||||
}
|
||||
fn walkable(x: int, y: int) -> bool {
|
||||
let c = tile(x, y)
|
||||
let c = Map.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 }
|
||||
|
|
@ -23,47 +23,47 @@ fn enemy_name(et: int) -> str {
|
|||
}
|
||||
|
||||
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, "########################################")
|
||||
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, "========================================")
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -29,15 +29,15 @@ 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
|
||||
var game_mode: int = 0 # 0 field, 1 battle, 2 gameover, 3 victory
|
||||
var turn_phase: int = 0
|
||||
var menu_cursor: int = 0
|
||||
var encounter: int = 0
|
||||
var enemy_type: int = 0 # 0 slime, 1 skeleton, 2 boss
|
||||
var player_hit: int = 0
|
||||
var enemy_hit: int = 0
|
||||
var gold: int = 0
|
||||
var world_map: int = 0 # 0 overworld, 1 dungeon
|
||||
var boss_down: int = 0 # boss defeated flag
|
||||
var ack: int = 0 # debounce for end screens
|
||||
var potions: int = 0 # potions in inventory
|
||||
|
|
|
|||
|
|
@ -4,29 +4,28 @@
|
|||
# and activation. The game only loads a font, opens the UI, and reads clicks.
|
||||
program Menu {
|
||||
|
||||
const R_FONT: int = 0
|
||||
const R_MSG: int = 1
|
||||
var title_font: int = 0
|
||||
|
||||
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
|
||||
label text: "CHRONO RIFT" font: title_font size: 26 fg: 0xffe060 align: center
|
||||
label text: "a Ludic retained-UI demo" font: title_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
|
||||
button id: NewGame text: "New Game" font: title_font size: 16 w: 236
|
||||
button id: Continue text: "Continue" font: title_font size: 16 w: 236
|
||||
button id: Options text: "Options" font: title_font size: 16 w: 236
|
||||
button id: Quit text: "Quit" font: title_font size: 16 w: 236
|
||||
}
|
||||
}
|
||||
|
||||
handler Boot phase Start {
|
||||
set_reg(R_FONT, font_load("/System/Library/Fonts/Supplemental/Arial.ttf"))
|
||||
title_font = font_load("/System/Library/Fonts/Supplemental/Arial.ttf")
|
||||
ui_build()
|
||||
ui_open(UI_MainMenu)
|
||||
}
|
||||
|
||||
handler Nav phase Update {
|
||||
ui_tick(key())
|
||||
ui_tick(Input.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)") }
|
||||
|
|
@ -34,8 +33,8 @@ program Menu {
|
|||
}
|
||||
|
||||
handler Draw phase Render {
|
||||
clear(0x0e0e16)
|
||||
Screen.clear(0x0e0e16)
|
||||
ui_render()
|
||||
present()
|
||||
Screen.show()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,33 +12,33 @@
|
|||
# 201 900 frame 3: Play.World.Step, then Hud.Draw
|
||||
# 202 900 frame 4: Step reaches 2 -> quit(); Hud.Draw paints the last frame
|
||||
#
|
||||
# ./selfhost/game-build.sh build/ludicc examples/scenes.ludic /tmp/scenes
|
||||
# bin/x game-build bin/ludicc examples/scenes.ludic /tmp/scenes
|
||||
# printf 'aaaa' | /tmp/scenes
|
||||
program SceneDemo {
|
||||
const R_N: int = 0
|
||||
var counter: int = 0
|
||||
|
||||
handler Boot phase Start { set_reg(R_N, 0); print(1000) }
|
||||
handler Boot phase Start { counter = 0; print(1000) }
|
||||
|
||||
scene Title start {
|
||||
on enter { print(1) }
|
||||
on exit { print(2) }
|
||||
layer Main {
|
||||
handler Tick phase Update {
|
||||
set_reg(R_N, reg(R_N) + 1)
|
||||
print(100 + reg(R_N))
|
||||
if reg(R_N) >= 2 { become Play }
|
||||
counter = counter + 1
|
||||
print(100 + counter)
|
||||
if counter >= 2 { become Play }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scene Play {
|
||||
on enter { print(3); set_reg(R_N, 0) }
|
||||
on enter { print(3); counter = 0 }
|
||||
on exit { print(4) }
|
||||
layer World {
|
||||
handler Step phase Update {
|
||||
set_reg(R_N, reg(R_N) + 1)
|
||||
print(200 + reg(R_N))
|
||||
if reg(R_N) >= 2 { quit() }
|
||||
counter = counter + 1
|
||||
print(200 + counter)
|
||||
if counter >= 2 { quit() }
|
||||
}
|
||||
}
|
||||
layer Hud {
|
||||
|
|
|
|||
|
|
@ -1,71 +1,93 @@
|
|||
# ============================================================================
|
||||
# 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
|
||||
# snake.ludic — a small, complete game in Ludic. No sprites or assets: every
|
||||
# pixel is drawn from primitives. Build: bin/x app examples/snake.ludic
|
||||
#
|
||||
# It shows the everyday shape of a Ludic game: an ECS for the moving parts (one
|
||||
# entity per snake segment), named program state for the rest, and a Render
|
||||
# handler that paints the frame with the Screen API and named colors.
|
||||
# ============================================================================
|
||||
program Snake {
|
||||
|
||||
property Pos { x: int = 0, y: int = 0 }
|
||||
property Seg { order: int = 0 }
|
||||
property Seg { order: int = 0 } # 0 = head, 1.. = body toward the tail
|
||||
|
||||
const GW: int = 20
|
||||
const GH: int = 15
|
||||
const TS: int = 16
|
||||
const GRID_W: int = 20 # playfield, in tiles
|
||||
const GRID_H: int = 15
|
||||
const TILE: int = 16 # pixels per tile
|
||||
|
||||
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
|
||||
const DIR_UP: int = 0
|
||||
const DIR_DOWN: int = 1
|
||||
const DIR_LEFT: int = 2
|
||||
const DIR_RIGHT: int = 3
|
||||
|
||||
fn reset(x: int) -> int {
|
||||
const MODE_PLAY: int = 0
|
||||
const MODE_OVER: int = 1
|
||||
|
||||
# ---- the game's whole state, named ----------------------------------------
|
||||
var direction: int = DIR_RIGHT
|
||||
var length: int = 3
|
||||
var tick: int = 0
|
||||
var food_x: int = 14
|
||||
var food_y: int = 7
|
||||
var mode: int = MODE_PLAY
|
||||
var score: int = 0
|
||||
var ate: int = 0 # did the head reach the food this step?
|
||||
var tail_x: int = 0 # where the tail sat before it moved (grow here)
|
||||
var tail_y: int = 0
|
||||
var acked: int = 0 # game-over key must be released before restart
|
||||
|
||||
# ---- custom shades this game uses (values the named palette doesn't carry) -
|
||||
const SHADE_BG: int = 0x0d1020 # backdrop behind the board
|
||||
const SHADE_TILE_A: int = 0x141a2e # checkerboard, light square
|
||||
const SHADE_TILE_B: int = 0x10162a # checkerboard, dark square
|
||||
const SHADE_FOOD: int = 0xe0405a
|
||||
const SHADE_BODY: int = 0x40c060
|
||||
const SHADE_HEAD: int = 0x9dffb0
|
||||
const SHADE_SCORE: int = 0xffe060
|
||||
const SHADE_OVER: int = 0xff5555
|
||||
|
||||
fn reset(unused: 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 } }
|
||||
set_reg(R_DIR, 3)
|
||||
set_reg(R_LEN, 3)
|
||||
set_reg(R_TICK, 0)
|
||||
set_reg(R_MODE, 0)
|
||||
set_reg(R_SCORE, 0)
|
||||
set_reg(R_FX, 14)
|
||||
set_reg(R_FY, 7)
|
||||
direction = DIR_RIGHT
|
||||
length = 3
|
||||
tick = 0
|
||||
mode = MODE_PLAY
|
||||
score = 0
|
||||
food_x = 14
|
||||
food_y = 7
|
||||
return 0
|
||||
}
|
||||
|
||||
handler Boot phase Start { reset(0) }
|
||||
|
||||
handler Input phase Input {
|
||||
let k = key()
|
||||
if reg(R_MODE) == 0 {
|
||||
if k == 'w' { if reg(R_DIR) != 1 { set_reg(R_DIR, 0) } }
|
||||
if k == 's' { if reg(R_DIR) != 0 { set_reg(R_DIR, 1) } }
|
||||
if k == 'a' { if reg(R_DIR) != 3 { set_reg(R_DIR, 2) } }
|
||||
if k == 'd' { if reg(R_DIR) != 2 { set_reg(R_DIR, 3) } }
|
||||
let k = Input.key()
|
||||
if mode == MODE_PLAY {
|
||||
if k == 'w' { if direction != DIR_DOWN { direction = DIR_UP } }
|
||||
if k == 's' { if direction != DIR_UP { direction = DIR_DOWN } }
|
||||
if k == 'a' { if direction != DIR_RIGHT { direction = DIR_LEFT } }
|
||||
if k == 'd' { if direction != DIR_LEFT { direction = DIR_RIGHT } }
|
||||
}
|
||||
if reg(R_MODE) == 1 {
|
||||
if k == 0 { set_reg(R_ACK, 1) }
|
||||
if k != 0 { if reg(R_ACK) == 1 { reset(0) } }
|
||||
if mode == MODE_OVER {
|
||||
if k == 0 { acked = 1 }
|
||||
if k != 0 { if acked == 1 { reset(0) } }
|
||||
}
|
||||
}
|
||||
|
||||
handler Move phase Update {
|
||||
if reg(R_MODE) == 0 {
|
||||
set_reg(R_TICK, reg(R_TICK) + 1)
|
||||
if reg(R_TICK) >= 6 {
|
||||
set_reg(R_TICK, 0)
|
||||
let len = reg(R_LEN)
|
||||
if mode == MODE_PLAY {
|
||||
tick = tick + 1
|
||||
if tick >= 6 {
|
||||
tick = 0
|
||||
# remember the tail cell, then drag every segment onto the one ahead of it
|
||||
for (pt, stg) in query [Pos, Seg] {
|
||||
if stg.order == len - 1 { set_reg(R_TX, pt.x); set_reg(R_TY, pt.y) }
|
||||
if stg.order == length - 1 { tail_x = pt.x; tail_y = pt.y }
|
||||
}
|
||||
for step in 0 .. len {
|
||||
let i = len - 1 - step
|
||||
for step in 0 .. length {
|
||||
let i = length - 1 - step
|
||||
if i >= 1 {
|
||||
for (pa, sa) in query [Pos, Seg] {
|
||||
if sa.order == i {
|
||||
|
|
@ -76,61 +98,66 @@ program Snake {
|
|||
}
|
||||
}
|
||||
}
|
||||
set_reg(R_ATE, 0)
|
||||
# move the head, check the walls, check the food
|
||||
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 { set_reg(R_MODE, 1) }
|
||||
if ph.x >= GW { set_reg(R_MODE, 1) }
|
||||
if ph.y < 0 { set_reg(R_MODE, 1) }
|
||||
if ph.y >= GH { set_reg(R_MODE, 1) }
|
||||
if ph.x == reg(R_FX) { if ph.y == reg(R_FY) { set_reg(R_ATE, 1) } }
|
||||
if direction == DIR_UP { ph.y = ph.y - 1 }
|
||||
if direction == DIR_DOWN { ph.y = ph.y + 1 }
|
||||
if direction == DIR_LEFT { ph.x = ph.x - 1 }
|
||||
if direction == DIR_RIGHT { ph.x = ph.x + 1 }
|
||||
if ph.x < 0 { mode = MODE_OVER }
|
||||
if ph.x >= GRID_W { mode = MODE_OVER }
|
||||
if ph.y < 0 { mode = MODE_OVER }
|
||||
if ph.y >= GRID_H { mode = MODE_OVER }
|
||||
if ph.x == food_x { if ph.y == food_y { ate = 1 } }
|
||||
}
|
||||
}
|
||||
# self-collision: head onto any body segment
|
||||
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 { set_reg(R_MODE, 1) } }
|
||||
if pb.x == ph.x { if pb.y == ph.y { mode = MODE_OVER } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if reg(R_ATE) == 1 {
|
||||
set_reg(R_SCORE, reg(R_SCORE) + 1)
|
||||
spawn Body { Seg { order: len }; Pos { x: reg(R_TX), y: reg(R_TY) } }
|
||||
set_reg(R_LEN, len + 1)
|
||||
set_reg(R_FX, rng_range(0, GW - 1))
|
||||
set_reg(R_FY, rng_range(0, GH - 1))
|
||||
if ate == 1 {
|
||||
score = score + 1
|
||||
spawn Body { Seg { order: length }; Pos { x: tail_x, y: tail_y } }
|
||||
length = length + 1
|
||||
food_x = Random.range(low: 0, high: GRID_W - 1)
|
||||
food_y = Random.range(low: 0, high: GRID_H - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handler Draw phase Render {
|
||||
clear(0x0d1020)
|
||||
for gy in 0 .. GH {
|
||||
for gx in 0 .. GW {
|
||||
var col = 0x141a2e
|
||||
if (gx + gy) % 2 == 0 { col = 0x10162a }
|
||||
fill_rect(gx * TS, gy * TS, TS, TS, col)
|
||||
Screen.clear(SHADE_BG)
|
||||
# checkerboard field
|
||||
for gy in 0 .. GRID_H {
|
||||
for gx in 0 .. GRID_W {
|
||||
var tile_color = SHADE_TILE_A
|
||||
if (gx + gy) % 2 == 0 { tile_color = SHADE_TILE_B }
|
||||
Screen.fill_rectangle(x: gx * TILE, y: gy * TILE, width: TILE, height: TILE, color: tile_color)
|
||||
}
|
||||
}
|
||||
fill_rect(reg(R_FX) * TS + 3, reg(R_FY) * TS + 3, TS - 6, TS - 6, 0xe0405a)
|
||||
# food, inset a few pixels inside its tile
|
||||
Screen.fill_rectangle(x: food_x * TILE + 3, y: food_y * TILE + 3, width: TILE - 6, height: TILE - 6, color: SHADE_FOOD)
|
||||
# every snake segment, straight from the ECS
|
||||
for (p, s) in query [Pos, Seg] {
|
||||
var col = 0x40c060
|
||||
if s.order == 0 { col = 0x9dffb0 }
|
||||
fill_rect(p.x * TS + 1, p.y * TS + 1, TS - 2, TS - 2, col)
|
||||
var seg_color = SHADE_BODY
|
||||
if s.order == 0 { seg_color = SHADE_HEAD } # the head is brighter
|
||||
Screen.fill_rectangle(x: p.x * TILE + 1, y: p.y * TILE + 1, width: TILE - 2, height: TILE - 2, color: seg_color)
|
||||
}
|
||||
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)
|
||||
Screen.draw_text(x: 6, y: 4, text: "SCORE", color: Color.White, scale: 1)
|
||||
Screen.draw_number(x: 52, y: 4, value: score, color: SHADE_SCORE, scale: 1)
|
||||
if mode == MODE_OVER {
|
||||
Screen.draw_text(x: 112, y: 104, text: "GAME OVER", color: SHADE_OVER, scale: 2)
|
||||
Screen.draw_text(x: 84, y: 140, text: "PRESS ANY KEY", color: Color.White, scale: 1)
|
||||
}
|
||||
present()
|
||||
Screen.show()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,9 @@ fn rt_shutdown() -> void {
|
|||
}
|
||||
|
||||
# ---- framebuffer ----------------------------------------------------------
|
||||
fn rt_screen_w() -> int { return rt_fbw }
|
||||
fn rt_screen_h() -> int { return rt_fbh }
|
||||
|
||||
fn rt_clear(c: int) -> void {
|
||||
let n = rt_fbw * rt_fbh
|
||||
for i in 0 .. n {
|
||||
|
|
|
|||
236
selfhost/emit_color.ludic
Normal file
236
selfhost/emit_color.ludic
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
# ============================================================================
|
||||
# emit_color.ludic — the named-color palette, resolved at compile time.
|
||||
#
|
||||
# `Color.Name` in a game lowers to a plain 0xRRGGBB int here: no runtime cost,
|
||||
# no allocation, identical codegen to writing the hex by hand. Unknown names are
|
||||
# a compile error (color_lookup returns -1, which emit_expr reports).
|
||||
#
|
||||
# GENERATED by scratchpad/palette.py from the single source-of-truth palette.
|
||||
# Edit the palette there and regenerate; do not hand-edit this file.
|
||||
# ============================================================================
|
||||
|
||||
fn color_lookup(name: ptr) -> int {
|
||||
if (name == "White") { return 0xFFFFFF }
|
||||
if (name == "Snow") { return 0xFFFAFA }
|
||||
if (name == "Ivory") { return 0xFFFFF0 }
|
||||
if (name == "EggShellWhite") { return 0xF0EAD6 }
|
||||
if (name == "FloralWhite") { return 0xFFFAF0 }
|
||||
if (name == "SeaShell") { return 0xFFF5EE }
|
||||
if (name == "Linen") { return 0xFAF0E6 }
|
||||
if (name == "AntiqueWhite") { return 0xFAEBD7 }
|
||||
if (name == "OldLace") { return 0xFDF5E6 }
|
||||
if (name == "Beige") { return 0xF5F5DC }
|
||||
if (name == "Cream") { return 0xFFFDD0 }
|
||||
if (name == "Honeydew") { return 0xF0FFF0 }
|
||||
if (name == "MintCream") { return 0xF5FFFA }
|
||||
if (name == "Azure") { return 0xF0FFFF }
|
||||
if (name == "AliceBlue") { return 0xF0F8FF }
|
||||
if (name == "GhostWhite") { return 0xF8F8FF }
|
||||
if (name == "WhiteSmoke") { return 0xF5F5F5 }
|
||||
if (name == "Lavender") { return 0xE6E6FA }
|
||||
if (name == "Bone") { return 0xE3DAC9 }
|
||||
if (name == "Parchment") { return 0xF1E9D2 }
|
||||
if (name == "Gainsboro") { return 0xDCDCDC }
|
||||
if (name == "LightGray") { return 0xD3D3D3 }
|
||||
if (name == "Silver") { return 0xC0C0C0 }
|
||||
if (name == "Ash") { return 0xB2BEB5 }
|
||||
if (name == "DarkGray") { return 0xA9A9A9 }
|
||||
if (name == "Gray") { return 0x808080 }
|
||||
if (name == "DimGray") { return 0x696969 }
|
||||
if (name == "Nickel") { return 0x727472 }
|
||||
if (name == "Slate") { return 0x708090 }
|
||||
if (name == "SlateGray") { return 0x708090 }
|
||||
if (name == "LightSlateGray") { return 0x778899 }
|
||||
if (name == "Gunmetal") { return 0x2A3439 }
|
||||
if (name == "Charcoal") { return 0x36454F }
|
||||
if (name == "Graphite") { return 0x1C1C1C }
|
||||
if (name == "Onyx") { return 0x353839 }
|
||||
if (name == "Jet") { return 0x343434 }
|
||||
if (name == "Black") { return 0x000000 }
|
||||
if (name == "EerieBlack") { return 0x1B1B1B }
|
||||
if (name == "RaisinBlack") { return 0x242124 }
|
||||
if (name == "Ebony") { return 0x555D50 }
|
||||
if (name == "Red") { return 0xFF0000 }
|
||||
if (name == "Crimson") { return 0xDC143C }
|
||||
if (name == "Scarlet") { return 0xFF2400 }
|
||||
if (name == "Vermilion") { return 0xE34234 }
|
||||
if (name == "FireBrick") { return 0xB22222 }
|
||||
if (name == "Cinnabar") { return 0xE44D2E }
|
||||
if (name == "DarkRed") { return 0x8B0000 }
|
||||
if (name == "Maroon") { return 0x800000 }
|
||||
if (name == "Ruby") { return 0xE0115F }
|
||||
if (name == "Cardinal") { return 0xC41E3A }
|
||||
if (name == "IndianRed") { return 0xCD5C5C }
|
||||
if (name == "Rust") { return 0xB7410E }
|
||||
if (name == "Sangria") { return 0x92000A }
|
||||
if (name == "Redwood") { return 0xA45A52 }
|
||||
if (name == "Cerise") { return 0xDE3163 }
|
||||
if (name == "Amaranth") { return 0xE52B50 }
|
||||
if (name == "Carmine") { return 0x960018 }
|
||||
if (name == "Chestnut") { return 0x954535 }
|
||||
if (name == "Brick") { return 0xCB4154 }
|
||||
if (name == "TerraCotta") { return 0xE2725B }
|
||||
if (name == "Pink") { return 0xFFC0CB }
|
||||
if (name == "LightPink") { return 0xFFB6C1 }
|
||||
if (name == "HotPink") { return 0xFF69B4 }
|
||||
if (name == "DeepPink") { return 0xFF1493 }
|
||||
if (name == "PaleVioletRed") { return 0xDB7093 }
|
||||
if (name == "Rose") { return 0xFF007F }
|
||||
if (name == "Blush") { return 0xDE5D83 }
|
||||
if (name == "Salmon") { return 0xFA8072 }
|
||||
if (name == "LightSalmon") { return 0xFFA07A }
|
||||
if (name == "DarkSalmon") { return 0xE9967A }
|
||||
if (name == "Coral") { return 0xFF7F50 }
|
||||
if (name == "Watermelon") { return 0xFC6C85 }
|
||||
if (name == "Flamingo") { return 0xFC8EAC }
|
||||
if (name == "Bubblegum") { return 0xFFC1CC }
|
||||
if (name == "Fuchsia") { return 0xFF00FF }
|
||||
if (name == "Magenta") { return 0xFF00FF }
|
||||
if (name == "Mauve") { return 0xE0B0FF }
|
||||
if (name == "Puce") { return 0xCC8899 }
|
||||
if (name == "Thistle") { return 0xD8BFD8 }
|
||||
if (name == "Orchid") { return 0xDA70D6 }
|
||||
if (name == "Orange") { return 0xFFA500 }
|
||||
if (name == "DarkOrange") { return 0xFF8C00 }
|
||||
if (name == "Tangerine") { return 0xF28500 }
|
||||
if (name == "Pumpkin") { return 0xFF7518 }
|
||||
if (name == "Apricot") { return 0xFBCEB1 }
|
||||
if (name == "Peach") { return 0xFFE5B4 }
|
||||
if (name == "Cantaloupe") { return 0xFFA62B }
|
||||
if (name == "Amber") { return 0xFFBF00 }
|
||||
if (name == "Bronze") { return 0xCD7F32 }
|
||||
if (name == "Copper") { return 0xB87333 }
|
||||
if (name == "Marigold") { return 0xEAA221 }
|
||||
if (name == "Carrot") { return 0xED9121 }
|
||||
if (name == "Persimmon") { return 0xEC5800 }
|
||||
if (name == "Papaya") { return 0xFF9E2C }
|
||||
if (name == "Sunset") { return 0xFAD6A5 }
|
||||
if (name == "Yellow") { return 0xFFFF00 }
|
||||
if (name == "LightYellow") { return 0xFFFFE0 }
|
||||
if (name == "Gold") { return 0xFFD700 }
|
||||
if (name == "Goldenrod") { return 0xDAA520 }
|
||||
if (name == "Lemon") { return 0xFFF700 }
|
||||
if (name == "Canary") { return 0xFFEF00 }
|
||||
if (name == "Mustard") { return 0xFFDB58 }
|
||||
if (name == "Flax") { return 0xEEDC82 }
|
||||
if (name == "Wheat") { return 0xF5DEB3 }
|
||||
if (name == "Corn") { return 0xFBEC5D }
|
||||
if (name == "Dandelion") { return 0xF0E130 }
|
||||
if (name == "Saffron") { return 0xF4C430 }
|
||||
if (name == "Khaki") { return 0xF0E68C }
|
||||
if (name == "DarkKhaki") { return 0xBDB76B }
|
||||
if (name == "Straw") { return 0xE4D96F }
|
||||
if (name == "Brown") { return 0x8B4513 }
|
||||
if (name == "SaddleBrown") { return 0x8B4513 }
|
||||
if (name == "Sienna") { return 0xA0522D }
|
||||
if (name == "Chocolate") { return 0xD2691E }
|
||||
if (name == "Peru") { return 0xCD853F }
|
||||
if (name == "Tan") { return 0xD2B48C }
|
||||
if (name == "BurlyWood") { return 0xDEB887 }
|
||||
if (name == "Sand") { return 0xC2B280 }
|
||||
if (name == "Coffee") { return 0x6F4E37 }
|
||||
if (name == "Espresso") { return 0x4B3621 }
|
||||
if (name == "Mahogany") { return 0xC04000 }
|
||||
if (name == "Walnut") { return 0x773F1A }
|
||||
if (name == "Umber") { return 0x635147 }
|
||||
if (name == "Sepia") { return 0x704214 }
|
||||
if (name == "Taupe") { return 0x483C32 }
|
||||
if (name == "Fawn") { return 0xE5AA70 }
|
||||
if (name == "Caramel") { return 0xC68E17 }
|
||||
if (name == "Cocoa") { return 0xD2691E }
|
||||
if (name == "Hazel") { return 0x8E7618 }
|
||||
if (name == "Wenge") { return 0x645452 }
|
||||
if (name == "Green") { return 0x008000 }
|
||||
if (name == "Lime") { return 0x00FF00 }
|
||||
if (name == "LimeGreen") { return 0x32CD32 }
|
||||
if (name == "LawnGreen") { return 0x7CFC00 }
|
||||
if (name == "Chartreuse") { return 0x7FFF00 }
|
||||
if (name == "GreenYellow") { return 0xADFF2F }
|
||||
if (name == "SpringGreen") { return 0x00FF7F }
|
||||
if (name == "MintGreen") { return 0x98FF98 }
|
||||
if (name == "SeaGreen") { return 0x2E8B57 }
|
||||
if (name == "MediumSeaGreen") { return 0x3CB371 }
|
||||
if (name == "ForestGreen") { return 0x228B22 }
|
||||
if (name == "DarkGreen") { return 0x006400 }
|
||||
if (name == "OliveDrab") { return 0x6B8E23 }
|
||||
if (name == "Olive") { return 0x808000 }
|
||||
if (name == "Moss") { return 0x8A9A5B }
|
||||
if (name == "Fern") { return 0x4F7942 }
|
||||
if (name == "Emerald") { return 0x50C878 }
|
||||
if (name == "Jade") { return 0x00A86B }
|
||||
if (name == "Malachite") { return 0x0BDA51 }
|
||||
if (name == "Shamrock") { return 0x009E60 }
|
||||
if (name == "Pistachio") { return 0x93C572 }
|
||||
if (name == "Avocado") { return 0x568203 }
|
||||
if (name == "Pine") { return 0x01796F }
|
||||
if (name == "Sage") { return 0x9CAF88 }
|
||||
if (name == "Kelly") { return 0x4CBB17 }
|
||||
if (name == "Hunter") { return 0x355E3B }
|
||||
if (name == "Basil") { return 0x579229 }
|
||||
if (name == "Clover") { return 0x2E8B57 }
|
||||
if (name == "Juniper") { return 0x6D9A79 }
|
||||
if (name == "Neon") { return 0x39FF14 }
|
||||
if (name == "Cyan") { return 0x00FFFF }
|
||||
if (name == "Aqua") { return 0x00FFFF }
|
||||
if (name == "LightCyan") { return 0xE0FFFF }
|
||||
if (name == "PaleTurquoise") { return 0xAFEEEE }
|
||||
if (name == "Aquamarine") { return 0x7FFFD4 }
|
||||
if (name == "Turquoise") { return 0x40E0D0 }
|
||||
if (name == "MediumTurquoise") { return 0x48D1CC }
|
||||
if (name == "DarkTurquoise") { return 0x00CED1 }
|
||||
if (name == "Teal") { return 0x008080 }
|
||||
if (name == "DarkCyan") { return 0x008B8B }
|
||||
if (name == "CadetBlue") { return 0x5F9EA0 }
|
||||
if (name == "Lagoon") { return 0x018E8E }
|
||||
if (name == "Seafoam") { return 0x93E9BE }
|
||||
if (name == "Cerulean") { return 0x007BA7 }
|
||||
if (name == "SkyBlueLight") { return 0x80DAEB }
|
||||
if (name == "Robin") { return 0x00CCCC }
|
||||
if (name == "Verdigris") { return 0x43B3AE }
|
||||
if (name == "Celadon") { return 0xACE1AF }
|
||||
if (name == "Blue") { return 0x0000FF }
|
||||
if (name == "LightBlue") { return 0xADD8E6 }
|
||||
if (name == "PowderBlue") { return 0xB0E0E6 }
|
||||
if (name == "SkyBlue") { return 0x87CEEB }
|
||||
if (name == "LightSkyBlue") { return 0x87CEFA }
|
||||
if (name == "DeepSkyBlue") { return 0x00BFFF }
|
||||
if (name == "DodgerBlue") { return 0x1E90FF }
|
||||
if (name == "CornflowerBlue") { return 0x6495ED }
|
||||
if (name == "SteelBlue") { return 0x4682B4 }
|
||||
if (name == "RoyalBlue") { return 0x4169E1 }
|
||||
if (name == "MediumBlue") { return 0x0000CD }
|
||||
if (name == "DarkBlue") { return 0x00008B }
|
||||
if (name == "Navy") { return 0x000080 }
|
||||
if (name == "MidnightBlue") { return 0x191970 }
|
||||
if (name == "Cobalt") { return 0x0047AB }
|
||||
if (name == "Sapphire") { return 0x0F52BA }
|
||||
if (name == "Denim") { return 0x1560BD }
|
||||
if (name == "Indigo") { return 0x4B0082 }
|
||||
if (name == "Prussian") { return 0x003153 }
|
||||
if (name == "Ultramarine") { return 0x3F00FF }
|
||||
if (name == "Periwinkle") { return 0xCCCCFF }
|
||||
if (name == "Iris") { return 0x5A4FCF }
|
||||
if (name == "Glaucous") { return 0x6082B6 }
|
||||
if (name == "Zaffre") { return 0x0014A8 }
|
||||
if (name == "Berry") { return 0x2E2D88 }
|
||||
if (name == "Purple") { return 0x800080 }
|
||||
if (name == "Violet") { return 0xEE82EE }
|
||||
if (name == "DarkViolet") { return 0x9400D3 }
|
||||
if (name == "BlueViolet") { return 0x8A2BE2 }
|
||||
if (name == "MediumPurple") { return 0x9370DB }
|
||||
if (name == "Amethyst") { return 0x9966CC }
|
||||
if (name == "Plum") { return 0x8E4585 }
|
||||
if (name == "Eggplant") { return 0x614051 }
|
||||
if (name == "Grape") { return 0x6F2DA8 }
|
||||
if (name == "Wine") { return 0x722F37 }
|
||||
if (name == "Mulberry") { return 0xC54B8C }
|
||||
if (name == "Lilac") { return 0xC8A2C8 }
|
||||
if (name == "Wisteria") { return 0xC9A0DC }
|
||||
if (name == "Heliotrope") { return 0xDF73FF }
|
||||
if (name == "Byzantium") { return 0x702963 }
|
||||
if (name == "Tyrian") { return 0x66023C }
|
||||
if (name == "RebeccaPurple") { return 0x663399 }
|
||||
if (name == "Orchid2") { return 0xAF69EF }
|
||||
return -1
|
||||
}
|
||||
|
||||
|
|
@ -116,7 +116,89 @@ fn emit_bin(e: Node) -> Val {
|
|||
return val(r, "int")
|
||||
}
|
||||
|
||||
# ---- named arguments -------------------------------------------------------
|
||||
# An argument list is either all-positional or all-named. When named, each kid
|
||||
# is an E_FINIT (label -> value); this rewrites e.kids into plain value exprs in
|
||||
# the order the callee declares its parameters, so the rest of emit_call is
|
||||
# oblivious to whether the caller used names.
|
||||
fn args_are_named(e: Node) -> bool {
|
||||
var i = 0
|
||||
while i < len(e.kids) { if e.kids[i].kind == E_FINIT { return true }; i = i + 1 }
|
||||
return false
|
||||
}
|
||||
fn reorder_named(e: Node, labels: []ptr) -> void {
|
||||
if not args_are_named(e) { return }
|
||||
var i = 0
|
||||
while i < len(e.kids) {
|
||||
if e.kids[i].kind != E_FINIT { perr("named and positional arguments cannot be mixed in one call") }
|
||||
i = i + 1
|
||||
}
|
||||
if len(e.kids) != len(labels) { perr("wrong number of arguments") }
|
||||
let out = new []Node
|
||||
var li = 0
|
||||
while li < len(labels) {
|
||||
var found: Node = null
|
||||
var k = 0
|
||||
while k < len(e.kids) { if (e.kids[k].s == labels[li]) { found = e.kids[k] }; k = k + 1 }
|
||||
if (found == null) { perr(`no argument named {labels[li]}`) }
|
||||
push(out, found.a)
|
||||
li = li + 1
|
||||
}
|
||||
e.kids = out
|
||||
}
|
||||
# The parameter labels of a resolved fn/extern, in declaration order.
|
||||
fn param_labels(fn: Node) -> []ptr {
|
||||
let out = new []ptr
|
||||
var i = 0
|
||||
while i < len(fn.kids) { if fn.kids[i].kind == N_PARAM { push(out, fn.kids[i].s) }; i = i + 1 }
|
||||
return out
|
||||
}
|
||||
|
||||
# ---- namespaced builtins: Screen.* / Random.* / Input.* --------------------
|
||||
# The game-facing API reads as `subject.action(...)`. Each method maps to a bare
|
||||
# runtime builtin plus the parameter labels callers may use as named arguments;
|
||||
# after reordering we rewrite the callee to that bare name and fall back into the
|
||||
# ordinary builtin path (which resolves it to its rt_ function).
|
||||
fn emit_ns_call(ns: ptr, meth: ptr, e: Node) -> Val {
|
||||
var bare: ptr = null
|
||||
let labels = new []ptr
|
||||
if (ns == "Screen") {
|
||||
if (meth == "clear") { bare = "clear"; push(labels, "color") }
|
||||
if (meth == "fill_rectangle") { bare = "fill_rect"; push(labels, "x"); push(labels, "y"); push(labels, "width"); push(labels, "height"); push(labels, "color") }
|
||||
if (meth == "draw_rectangle") { bare = "frame_rect"; push(labels, "x"); push(labels, "y"); push(labels, "width"); push(labels, "height"); push(labels, "color") }
|
||||
if (meth == "put_pixel") { bare = "put_px"; push(labels, "x"); push(labels, "y"); push(labels, "color") }
|
||||
if (meth == "draw_text") { bare = "text"; push(labels, "x"); push(labels, "y"); push(labels, "text"); push(labels, "color"); push(labels, "scale") }
|
||||
if (meth == "draw_number") { bare = "text_int"; push(labels, "x"); push(labels, "y"); push(labels, "value"); push(labels, "color"); push(labels, "scale") }
|
||||
if (meth == "show") { bare = "present" }
|
||||
if (meth == "width") { bare = "screen_w" }
|
||||
if (meth == "height") { bare = "screen_h" }
|
||||
if (meth == "status") { bare = "status"; push(labels, "text") }
|
||||
}
|
||||
if (ns == "Map") {
|
||||
if (meth == "size") { bare = "map_size"; push(labels, "width"); push(labels, "height") }
|
||||
if (meth == "row") { bare = "map_row"; push(labels, "y"); push(labels, "cells") }
|
||||
if (meth == "tile") { bare = "tile"; push(labels, "x"); push(labels, "y") }
|
||||
}
|
||||
if (ns == "Random") {
|
||||
if (meth == "range") { bare = "rng_range"; push(labels, "low"); push(labels, "high") }
|
||||
if (meth == "chance") { bare = "rng_chance"; push(labels, "percent") }
|
||||
if (meth == "seed") { bare = "seed"; push(labels, "value") }
|
||||
}
|
||||
if (ns == "Input") {
|
||||
if (meth == "key") { bare = "key" }
|
||||
}
|
||||
if (bare == null) { perr(`unknown builtin {ns}.{meth}`) }
|
||||
reorder_named(e, labels)
|
||||
let id = node(E_ID); id.s = bare; e.a = id
|
||||
return emit_call(e)
|
||||
}
|
||||
|
||||
fn emit_call(e: Node) -> Val {
|
||||
# `Subject.action(...)` — a namespaced builtin (Screen/Random/Input).
|
||||
if e.a.kind == E_MEMBER {
|
||||
if e.a.a.kind == E_ID { return emit_ns_call(e.a.a.s, e.a.s, e) }
|
||||
perr("call target is not a function")
|
||||
}
|
||||
let name = e.a.s
|
||||
if (name == "self") { if nself == 0 { return val("0", "entity") }; return val(emit_bind(`load i32, ptr {self_stk[nself - 1]}`), "entity") }
|
||||
if (name == "key") { return val(emit_bind("load i32, ptr @L_key"), "int") }
|
||||
|
|
@ -253,6 +335,7 @@ fn emit_call(e: Node) -> Val {
|
|||
# extern fn: a direct call to the declared link symbol (no @fn_ prefix)
|
||||
let ext = find_extern(name)
|
||||
if (ext != null) {
|
||||
reorder_named(e, param_labels(ext))
|
||||
let eargs = new []ptr
|
||||
let eatys = new []ptr
|
||||
var ei = 0
|
||||
|
|
@ -280,6 +363,7 @@ fn emit_call(e: Node) -> Val {
|
|||
if (fn2 == null) { perr(`unknown function {name}`) }
|
||||
cname = rtname
|
||||
}
|
||||
reorder_named(e, param_labels(fn2))
|
||||
# evaluate args first (their IR is emitted before the call instruction)
|
||||
let args = new []ptr
|
||||
let atys = new []ptr
|
||||
|
|
@ -332,8 +416,13 @@ fn emit_expr(e: Node) -> Val {
|
|||
perr(`unknown identifier {e.s}`)
|
||||
}
|
||||
if e.kind == E_MEMBER {
|
||||
if e.a.kind == E_ID { # `Enum.Variant` -> its ordinal, a compile-time int
|
||||
let ord = enum_ordinal(e.a.s, e.s)
|
||||
if e.a.kind == E_ID {
|
||||
if (e.a.s == "Color") { # `Color.Name` -> its 0xRRGGBB int, at compile time
|
||||
let cv = color_lookup(e.s)
|
||||
if (cv < 0) { perr(`unknown color Color.{e.s}`) }
|
||||
return val(itoa(cv), "int")
|
||||
}
|
||||
let ord = enum_ordinal(e.a.s, e.s) # `Enum.Variant` -> its ordinal, a compile-time int
|
||||
if ord >= 0 { return val(itoa(ord), "int") }
|
||||
}
|
||||
let bt = static_type(e.a) # `x.field` where field is @Computed -> inline it
|
||||
|
|
|
|||
|
|
@ -1,10 +1,25 @@
|
|||
# emit_machine.ludic — `machine <reg> { state Name = v { .. } }` dispatches on a
|
||||
# register's value; `become Name` stores the target state's value back. Both are
|
||||
# reg()/set_reg() calls, resolved to the runtime like any other builtin.
|
||||
# emit_machine.ludic — `machine <state> { state Name = v { .. } }` dispatches on
|
||||
# an int state store; `become Name` writes the target state's value back. The
|
||||
# store is either a named program-scope `var` (loaded/stored directly) or, for
|
||||
# older code, a register index (reg()/set_reg()).
|
||||
|
||||
# If `a` names a program-scope `var`, return it; else null (a register index).
|
||||
fn machine_var(a: Node) -> Node {
|
||||
if a.kind == E_ID {
|
||||
let g = find_global(a.s)
|
||||
if (g != null) { if g.kind == N_VAR { return g } }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fn emit_machine(st: Node) -> void {
|
||||
let regv = emit_expr(st.a)
|
||||
let s = emit_bind(`call i32 @fn_rt_reg(i32 {regv.code})`)
|
||||
let gv = machine_var(st.a)
|
||||
var s = ""
|
||||
if (gv != null) { s = emit_bind(`load i32, ptr @g_{st.a.s}`) }
|
||||
else {
|
||||
let regv = emit_expr(st.a)
|
||||
s = emit_bind(`call i32 @fn_rt_reg(i32 {regv.code})`)
|
||||
}
|
||||
if nmach < len(mach_stk) { mach_stk[nmach] = st } else { push(mach_stk, st) }
|
||||
nmach = nmach + 1
|
||||
let endl = lbl("smend")
|
||||
|
|
@ -37,9 +52,14 @@ fn emit_become(st: Node) -> void {
|
|||
var i = 0
|
||||
while i < len(m.kids) { if (m.kids[i].s == st.s) { target = m.kids[i] }; i = i + 1 }
|
||||
if (target != null) {
|
||||
let regv = emit_expr(m.a)
|
||||
let gv = machine_var(m.a)
|
||||
let sv = emit_expr(target.b)
|
||||
emit(" call void @fn_rt_set_reg(i32 "); emit(regv.code); emit(", i32 "); emit(sv.code); emit(")\n")
|
||||
if (gv != null) {
|
||||
emit(" store i32 "); emit(sv.code); emit(", ptr @g_"); emit(m.a.s); emit("\n")
|
||||
} else {
|
||||
let regv = emit_expr(m.a)
|
||||
emit(" call void @fn_rt_set_reg(i32 "); emit(regv.code); emit(", i32 "); emit(sv.code); emit(")\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
18268
selfhost/ludicc.seed.ll
18268
selfhost/ludicc.seed.ll
File diff suppressed because it is too large
Load diff
|
|
@ -48,9 +48,22 @@ fn ptype() -> ptr {
|
|||
# ---- expressions -----------------------------------------------------------
|
||||
fn expr() -> Node { return p_or() }
|
||||
|
||||
# Call arguments. Each argument is either positional (`expr`) or named
|
||||
# (`name: expr`) — a named argument is an identifier immediately followed by a
|
||||
# colon, which is unambiguous inside a call. Named args are stored as E_FINIT
|
||||
# (s=label, a=value) and reordered to the callee's parameter order at emit time.
|
||||
fn args_call(call: Node) -> void {
|
||||
eat_op("("); skipnl()
|
||||
while not is_op(")") { push(call.kids, expr()); skipnl(); if is_op(",") { pi = pi + 1; skipnl() } }
|
||||
while not is_op(")") {
|
||||
let t = toks[pi]
|
||||
let nx = toks[pi + 1]
|
||||
if t.kind == TK_ID and nx.kind == TK_OP and (nx.text == ":") {
|
||||
let fi = node(E_FINIT); fi.s = eat_id(); eat_op(":"); skipnl(); fi.a = expr(); push(call.kids, fi)
|
||||
} else {
|
||||
push(call.kids, expr())
|
||||
}
|
||||
skipnl(); if is_op(",") { pi = pi + 1; skipnl() }
|
||||
}
|
||||
eat_op(")")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue