sourcegames/dungeon/play.xtl
1⍝!/usr/bin/env xetal
2⍝│ The dungeon: find the way out (>). Arrow keys or WASD move you; walk
3⍝│ into a monster to fight it; walk over potions (!) and gold ($) to pick
4⍝│ them up; r starts again. Each game is a new dungeon (a map and
5⍝│ places drawn from its seed); a game won or lost ends the program (on
6⍝│ the page, Play or any key then starts a new one). On the page the
7⍝│ dungeon is in a dialog and
8⍝│ your keys come to the program as events; at the command line, lines
9⍝│ such as "key Up" on standard input. The rules are Crawl.xtl's, built
10⍝│ on lib/Ecs.
11"cr:" u̲se< "Crawl"
12
13⍝│ A key's step: a row and a column (0 0 for any other key, a turn waited).
14ᵘs̲tep ← { name →
15 k ← ("UP" "DOWN" "LEFT" "RIGHT" "w" "s" "a" "d") i̲ndexOf e̲nclose name
16 ((1 + (k − 1) m̲od 4) s̲elect 9 2 r̲eshape -1 0 1 0 0 -1 0 1 0 0 0 0 0 0 0 0 0 0) × k ≤ 8
17}
18⍝│ Show the dungeon, read the next event, act on a key, again.
19ᵘl̲oop ← { s →
20 (W, g, n, m, lv) ← s
21 _ ← p̲rint! m
22 _ ← ⎕S̲HOW ᶜʳd̲raw s
23 0 < ᶜʳs̲tatus s ? ᵘe̲nd s
24 e ← ⎕E̲VENT @
25 "end" m̲atch ⎕E̲KIND e ? "THE END"
26 n̲ot "key" m̲atch ⎕E̲KIND e ? ᵘl̲oop s
27 name ← f̲ormat ⎕E̲KEY e
28 "r" m̲atch name ? ᵘl̲oop ᶜʳn̲ew 0
29 ᵘl̲oop s ᶜʳt̲urn f̲loat ᵘs̲tep name
30}
31⍝│ How the game ended (the program ends with it).
32ᵘe̲nd ← { s →
33 y ← ᶜʳy̲ou s
34 1 = ᶜʳs̲tatus s ? @ f̲ormat< "You found the way out with {2 s_elect y} gold in {3 s_elect y} turns!"
35 "You were beaten."
36}
37ᵘl̲oop ᶜʳn̲ew 0
f̲ormat< expands to
("You found the way out with " c̲at (f̲ormat (2 s̲elect y)) c̲at " gold in " c̲at (f̲ormat (3 s̲elect y)) c̲at " turns!")