Repository-cleanup / DX pass folding three tracker items into one coherent change, verified green end to end (`bin/x test` 49/0, `bin/x selfhost-test` 29/0, `bin/x test-tools` 29/0). #28 — curate & categorise examples/ - 42 flat entries regrouped into intent-revealing subdirs: games/, rendering/, ecs/, events/, networking/, lang/, library/ (was lib/). - chronorift dir-vs-file duplication resolved: the entry file and its import modules now live together under games/chronorift(.ludic). - Every path reference updated repo-wide (test runner, editor-tool drivers, docs/site, design docs). - New examples/README.md indexes the whole set with run commands. - Showcase examples without a self-asserting entry (hello, events, net_rt) now get a compile-only rot guard in `bin/x test`, so nothing here rots silently. #30 — text-diffable golden baseline - The 4 binary selfhost/golden/*.ppm blobs are replaced by a single selfhost/golden/renders.sha256 manifest (SHA-256 per render). Hashes are byte-identical to the old PPMs, so the baseline is unchanged — only its form. - game_case now compares framebuffer hashes; a regression shows as a changed hex line in review, not "binary files differ". - New `bin/x golden` regenerates the manifest deliberately (review with `git diff selfhost/golden/renders.sha256`). #27 — PPM & asset handling - Headless renders now write build/out.ppm, never the repo root; `x app`, `x clean`, messaging and .gitignore updated to match. Nothing is written to the working root any more. - Redundant local Kenney .zip archives removed (the art ships extracted; .gitignore already excludes *.zip). CC0 License.txt files retained. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
172 lines
5.1 KiB
Text
172 lines
5.1 KiB
Text
# combat.ludic — the co-op turn-based battle state machine.
|
|
|
|
# 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 (menu_cursor holds one).
|
|
enum KnightAct { Attack, Guard, Item, Flee }
|
|
enum MageAct { Attack, Heal, Guard }
|
|
|
|
handler Battle phase Update {
|
|
if game_mode == 1 {
|
|
let k = Input.key()
|
|
for (e) in query [Stats, {Enemy}] {
|
|
machine turn_phase {
|
|
|
|
state KnightMenu {
|
|
for (st, pt) in query [Stats, Party] {
|
|
if pt.slot == 0 {
|
|
if st.hp <= 0 {
|
|
become MageMenu
|
|
menu_cursor = 0
|
|
} else {
|
|
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 menu_cursor {
|
|
KnightAct.Attack => {
|
|
let d = max(1, st.atk - e.def + Random.range(0, 4))
|
|
e.hp = e.hp - d
|
|
player_hit = d
|
|
}
|
|
KnightAct.Guard => { st.guard = 1; player_hit = 0 }
|
|
KnightAct.Item => {
|
|
if potions > 0 {
|
|
potions = potions - 1
|
|
st.hp = min(st.maxhp, st.hp + 24)
|
|
player_hit = 24
|
|
} else { player_hit = 0 }
|
|
}
|
|
KnightAct.Flee => {
|
|
if Random.chance(50) { e.hp = 0; game_mode = 0 }
|
|
else { player_hit = 0 }
|
|
}
|
|
}
|
|
become KnightResolve
|
|
menu_cursor = 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
state KnightResolve {
|
|
if k != 0 {
|
|
if e.hp <= 0 {
|
|
grant_xp(e.xp)
|
|
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
|
|
menu_cursor = 0
|
|
}
|
|
}
|
|
}
|
|
|
|
state MageMenu {
|
|
for (st, pt) in query [Stats, Party] {
|
|
if pt.slot == 1 {
|
|
if st.hp <= 0 {
|
|
become EnemyTurn
|
|
} else {
|
|
if k == 'i' { menu_cursor = max(0, menu_cursor - 1) }
|
|
if k == 'k' { menu_cursor = min(2, menu_cursor + 1) }
|
|
if k == 'j' {
|
|
match menu_cursor {
|
|
MageAct.Attack => {
|
|
let d = max(1, st.atk + 5 - e.def + Random.range(0, 5))
|
|
e.hp = e.hp - d
|
|
player_hit = d
|
|
}
|
|
MageAct.Heal => {
|
|
if st.mp >= 4 {
|
|
st.mp = st.mp - 4
|
|
for (t2, p2) in query [Stats, Party] {
|
|
if p2.slot == 0 { t2.hp = min(t2.maxhp, t2.hp + 16) }
|
|
}
|
|
player_hit = 16
|
|
} else { player_hit = 0 }
|
|
}
|
|
MageAct.Guard => { st.guard = 1; player_hit = 0 }
|
|
}
|
|
become MageResolve
|
|
menu_cursor = 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
state MageResolve {
|
|
if k != 0 {
|
|
if e.hp <= 0 {
|
|
grant_xp(e.xp)
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
state EnemyTurn {
|
|
if k != 0 {
|
|
var done = 0
|
|
for (t3) in query [Stats, {Party}] {
|
|
if done == 0 {
|
|
if t3.hp > 0 {
|
|
var ed = max(1, e.atk - t3.def + Random.range(0, 4))
|
|
if t3.guard == 1 { ed = ed / 2; t3.guard = 0 }
|
|
t3.hp = t3.hp - ed
|
|
enemy_hit = ed
|
|
done = 1
|
|
}
|
|
}
|
|
}
|
|
var alive = 0
|
|
for (t4) in query [Stats, {Party}] {
|
|
if t4.hp > 0 { alive = alive + 1 }
|
|
}
|
|
if alive == 0 { game_mode = 2; ack = 0 }
|
|
else {
|
|
become KnightMenu
|
|
menu_cursor = 0
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
handler CleanBattle phase LateUpdate {
|
|
if game_mode != 1 {
|
|
for (e) in query [Stats, {Enemy}] {
|
|
despawn self()
|
|
}
|
|
}
|
|
}
|
|
|
|
handler Meta phase Update {
|
|
let k = Input.key()
|
|
if game_mode == 2 {
|
|
if k == 0 { ack = 1 }
|
|
if k != 0 {
|
|
if ack == 1 {
|
|
for (h) in query [Stats, {Party}] { h.hp = h.maxhp }
|
|
game_mode = 0
|
|
}
|
|
}
|
|
}
|
|
if game_mode == 3 {
|
|
if k == 0 { ack = 1 }
|
|
if k != 0 {
|
|
if ack == 1 { game_mode = 0 }
|
|
}
|
|
}
|
|
}
|