sourcegames/maze/Walls.xtl

1⍝│ The maze's rules, a library: maze.xtl (scripted), play.xtl (the game: 2⍝│ arrow keys) and the web page all use these. A maze is a grid of 3⍝│ blocks, wall or open, kept as one vector of 0s and 1s, row by row; a 4⍝│ new one is carved from the game's seed with whole-array operations, 5⍝│ and the way out is put at the open block farthest from the start. 6⍝│ What you see is worked out the same way: the blocks ahead and to each 7⍝│ side, every depth looked up at once, then drawn far to near. A game's 8⍝│ state is a tuple: (the walls, where you are, which way you face (0 9⍝│ north, 1 east, 2 south, 3 west), the steps taken, the blocks seen, 10⍝│ whether the mini map shows, the way out). 11 12"sv:" u̲se< "Svg" 13 14⍝━━ The data 15 16⍝│ The maze's size in cells, and so in blocks: a block per cell, a block 17⍝│ between neighbors (a wall or a way through), and a wall all around. 18size ← f̲loor 'ˢᵛn̲um e̲ach "maze.toml" ⎕L̲IST "size" 19CW ← f̲irst size 20CH ← 2 s̲elect size 21B ← 1 + 2 × CW 22BH ← 1 + 2 × CH 23NB ← B × BH 24⍝│ The colors: the walls by depth, the floor, the ceiling. 25WALLS ← "maze.toml" ⎕L̲IST "walls" 26FLOOR ← d̲isclose f̲irst "maze.toml" ⎕L̲IST "floor" 27CEILING ← d̲isclose f̲irst "maze.toml" ⎕L̲IST "ceiling" 28⍝│ A step north, east, south, west, as a move along the vector. 29STEPS ← (n̲eg B) c̲at 1 c̲at B c̲at -1 30⍝│ A distance beyond any in the maze. 31FAR ← 1000000 32 33⍝━━ A new maze 34 35⍝│ The walls of a new maze, carved row by row in one go (the sidewinder 36⍝│ way): each cell joins its east neighbor at random (the top row always, 37⍝│ the last column never), which cuts each row into runs; each run below 38⍝│ the top row opens north from one of its cells, picked at random. The 39⍝│ runs are a running sum of where they end; the cell picked in each run 40⍝│ is the one with the highest random key in it, every pair of cells 41⍝│ compared in one table. 42ʰc̲arve ← { z → 43 n ← CW × CH 44 k ← (r̲ange n) − 1 45 i ← k d̲iv CW 46 j ← k m̲od CW 47 east ← (j < CW − 1) ∧ (i = 0) ∨ 1 = r̲oll! n r̲eshape 2 48 ends ← 0 + n̲ot east 49 run ← ('+ s̲\ ends) − ends 50 key ← g̲rade r̲oll! n r̲eshape 1000000 51 best ← 'm̲ax r̲/₂ (0 + run '= t̲able run) × (n c̲at n) r̲eshape key 52 north ← (i > 0) ∧ key = best 53 cell ← 1 + (B × 1 + 2 × i) + 1 + 2 × j 54 open ← cell c̲at (east r̲eplicate cell + 1) c̲at north r̲eplicate cell − B 55 n̲ot (r̲ange NB) m̲ember? open 56} 57⍝│ The vector d moved k places (the block k further along in each place), 58⍝│ FAR shifted in at the end. 59ʰs̲hift ← { d k → 60 k > 0 ? (k d̲rop d) c̲at k r̲eshape FAR 61 ((n̲eg k) r̲eshape FAR) c̲at (NB + k) t̲ake d 62} 63⍝│ Every open block's distance from the start, by a flood: each block one 64⍝│ more than its nearest neighbor, all at once, again until nothing 65⍝│ changes (walls stay FAR). 66ʰf̲lood ← { (G, d) → 67 e ← (FAR × 0 + G) m̲ax d m̲in 1 + (d ʰs̲hift 1) m̲in (d ʰs̲hift -1) m̲in (d ʰs̲hift B) m̲in d ʰs̲hift n̲eg B 68 '∧ r̲/ e = d ? d 69 ʰf̲lood (G, e) 70} 71⍝ :: (Any a, Num b, Num c, Truthy d, Num e) => a -> (Int, Int, b, c, d, e, Int) 72⍝│ A new game: a new maze, you in its bottom left cell facing north, the 73⍝│ way out at the open block farthest from you. 74ˡn̲ew ← { z → 75 G ← ʰc̲arve z 76 at ← 2 + B × BH − 2 77 d ← ʰf̲lood (G, FAR × 0 + (r̲ange NB) ≠ at) 78 goal ← f̲irst g̲rade n̲eg d × 0 + d < FAR 79 (G, at, 0, 0, ʰs̲ee (G, at, 0, 0 < NB r̲eshape 0), 0, goal) 80} 81 82⍝━━ Looking 83 84⍝│ Whether the blocks i are walls (beyond the maze counts as wall). 85ʰw̲all ← { (G, i) → (i < 1) ∨ (i > NB) ∨ (1 m̲ax NB m̲in i) s̲elect G } 86⍝│ What you see from at facing f, depths 0 to 5 at once: the blocks ahead 87⍝│ (F), to their left (L) and to their right (R), as block numbers. 88ʰl̲ook ← { (at, f) → 89 k ← (r̲ange 6) − 1 90 F ← at + k × (1 + f) s̲elect STEPS 91 L ← F + (1 + (f + 3) m̲od 4) s̲elect STEPS 92 R ← F + (1 + (f + 1) m̲od 4) s̲elect STEPS 93 (F, L, R) 94} 95⍝│ The depth of the first wall ahead (5 when none is that near). 96ʰd̲epth ← { (G, F) → f̲irst (w̲here 1 d̲rop ʰw̲all (G, F)) c̲at 5 } 97⍝│ The blocks seen with what is in view from at facing f added: the 98⍝│ blocks ahead up to the first wall, and those beside them. 99ʰs̲ee ← { (G, at, f, seen) → 100 (F, L, R) ← ʰl̲ook (at, f) 101 D ← ʰd̲epth (G, F) 102 i ← ((1 + D) t̲ake F) c̲at (D t̲ake L) c̲at D t̲ake R 103 seen ∨ (r̲ange NB) m̲ember? i 104} 105 106⍝━━ Your keys 107 108⍝│ You at to (unless it is a wall, where you stay), a step more. 109ʰm̲oved ← { (G, at, to, f, n, seen, mp, goal) → 110 ʰw̲all (G, to) ? (G, at, f, n, seen, mp, goal) 111 (G, to, f, n + 1, ʰs̲ee (G, to, f, seen), mp, goal) 112} 113⍝ :: (Num a, Truthy b, Num c, Any d) => (Int, Int, Int, a, b, c, d) -> Char -> (Int, Int, Int, a, b, c, d) 114⍝│ A key: up (or w) steps ahead, down (or s) steps back, left and right 115⍝│ (or a and d) turn a quarter, m shows or hides the mini map; other keys 116⍝│ do nothing. 117ˡk̲ey ← { s name → 118 (G, at, f, n, seen, mp, goal) ← s 119 key ← ("UP" "DOWN" "LEFT" "RIGHT" "w" "s" "a" "d" "m") i̲ndexOf e̲nclose name 120 key > 9 ? s 121 key = 9 ? (G, at, f, n, seen, 1 − mp, goal) 122 k ← 1 + (key − 1) m̲od 4 123 t ← (4 + f + (2 × k) − 7) m̲od 4 124 k > 2 ? (G, at, t, n, ʰs̲ee (G, at, t, seen), mp, goal) 125 ʰm̲oved (G, at, at + (1 + (f + 2 × k − 1) m̲od 4) s̲elect STEPS, f, n, seen, mp, goal) 126} 127 128⍝━━ What it shows 129 130⍝ :: (Any a, Eq b, Any c, Any d, Any e, Any f, Num g, Truthy g) => (a, b, c, d, e, f, b) -> g 131⍝│ 1 once you stand on the way out, else 0. 132ˡs̲tatus ← { (G, at, f, n, seen, mp, goal) → 0 + at = goal } 133⍝ :: (Any a, Any c, Any d) => (a, b, b, b, c, d, b) -> b 134⍝│ The steps taken, which way you face, your block, the way out's block. 135ˡy̲ou ← { (G, at, f, n, seen, mp, goal) → n c̲at f c̲at at c̲at goal } 136⍝ :: (Num a, Any b, Any c, Any d, Any e, Any f, Any g) => (a, b, c, d, e, f, g) -> a 137⍝│ The maze as a matrix of blocks, 1 a wall, 0 open. 138ˡg̲rid ← { (G, at, f, n, seen, mp, goal) → (BH c̲at B) r̲eshape 0 + G } 139⍝ :: (Any a, Any b, Any c, Any d, Any e) => (Int, Int, a, b, c, d, e) -> Int 140⍝│ Every open block's distance in steps from where you are (the flood), 141⍝│ walls -1, as a matrix. 142ˡd̲istances ← { (G, at, f, n, seen, mp, goal) → 143 d ← ʰf̲lood (G, FAR × 0 + (r̲ange NB) ≠ at) 144 (BH c̲at B) r̲eshape d − (1 + FAR) × 0 + G 145} 146⍝ :: (Any a, Any b, Any c, Any d, Num e, Any f, Any g) => (a, b, c, d, e, f, g) -> e 147⍝│ The blocks seen so far, as a matrix of 0s and 1s. 148ˡs̲een ← { (G, at, f, n, seen, mp, goal) → (BH c̲at B) r̲eshape 0 + seen } 149⍝│ The view's planes: a block at depth k spans from plane k to plane k+1; 150⍝│ each plane's half width and half height, nearest first. 151H ← 400.0 230.0 130.0 74.0 42.0 24.0 14.0 152V ← 0.75 × H 153CX ← 400.0 154CY ← 310.0 155⍝│ A wall's color at depth k. 156ʰc̲olor ← { k → d̲isclose (1 + k m̲in 4) s̲elect WALLS } 157⍝│ A side of depth k (s: -1 left, 1 right): a wall beside you there (a 158⍝│ slanted panel), or, where that side is open, the face of the wall 159⍝│ beyond the opening. 160ʰs̲ide ← { (k, s, wall, face) → 161 a ← (k + 1) s̲elect H 162 b ← (k + 2) s̲elect H 163 va ← (k + 1) s̲elect V 164 vb ← (k + 2) s̲elect V 165 wall ? @ f̲ormat< "<polygon points=\"{sv:w_hole CX + s * a},{sv:w_hole CY - va} {sv:w_hole CX + s * b},{sv:w_hole CY - vb} {sv:w_hole CX + s * b},{sv:w_hole CY + vb} {sv:w_hole CX + s * a},{sv:w_hole CY + va}\" fill=\"{h:c_olor k}\" stroke=\"#000000\" stroke-width=\"2\"/>"
f̲ormat< expands to
("<polygon points=\"" c̲at (f̲ormat (ˢᵛw̲hole CX + s × a)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY − va)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole CX + s × b)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY − vb)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole CX + s × b)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY + vb)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole CX + s × a)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY + va)) c̲at "\" fill=\"" c̲at (f̲ormat (ʰc̲olor k)) c̲at "\" stroke=\"#000000\" stroke-width=\"2\"/>")
166 face ? @ f̲ormat< "<rect x=\"{sv:w_hole (CX + s * 2.0 * b) - b}\" y=\"{sv:w_hole CY - vb}\" width=\"{sv:w_hole 2.0 * b}\" height=\"{sv:w_hole 2.0 * vb}\" fill=\"{h:c_olor k + 1}\" stroke=\"#000000\" stroke-width=\"2\"/>"
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole (CX + s × 2.0 × b) − b)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − vb)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × b)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × vb)) c̲at "\" fill=\"" c̲at (f̲ormat (ʰc̲olor k + 1)) c̲at "\" stroke=\"#000000\" stroke-width=\"2\"/>")
167 "" 168} 169⍝│ The wall facing you at depth k (on plane k; at depth 5, darkness), and 170⍝│ the way out where it stands ahead (g: its depth plus 1, or nothing), 171⍝│ on the near plane of its block. 172ʰf̲ront ← { k → 173 k ≥ 5 ? @ f̲ormat< "<rect x=\"{sv:w_hole CX - 6 s_elect H}\" y=\"{sv:w_hole CY - 6 s_elect V}\" width=\"{sv:w_hole 2.0 * 6 s_elect H}\" height=\"{sv:w_hole 2.0 * 6 s_elect V}\" fill=\"#000000\"/>"
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX − 6 s̲elect H)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − 6 s̲elect V)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × 6 s̲elect H)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × 6 s̲elect V)) c̲at "\" fill=\"#000000\"/>")
174 a ← (k + 1) s̲elect H 175 v ← (k + 1) s̲elect V 176 @ f̲ormat< "<rect x=\"{sv:w_hole CX - a}\" y=\"{sv:w_hole CY - v}\" width=\"{sv:w_hole 2.0 * a}\" height=\"{sv:w_hole 2.0 * v}\" fill=\"{h:c_olor k}\" stroke=\"#000000\" stroke-width=\"2\"/>"
f̲ormat< expands to
"<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX − a)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − v)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × a)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × v)) c̲at "\" fill=\"" c̲at (f̲ormat (ʰc̲olor k)) c̲at "\" stroke=\"#000000\" stroke-width=\"2\"/>"
177} 178ʰd̲oor ← { g → 179 0 = t̲ally g ? "" 180 k ← f̲irst g 181 a ← (k + 1) s̲elect H 182 v ← (k + 1) s̲elect V 183 @ f̲ormat< "<rect class=\"entity\" x=\"{sv:w_hole CX - 0.45 * a}\" y=\"{sv:w_hole CY - 0.7 * v}\" width=\"{sv:w_hole 0.9 * a}\" height=\"{sv:w_hole 1.7 * v}\" fill=\"#2a9d8f\" stroke=\"#a8f0e6\" stroke-width=\"3\"/><text x=\"{sv:w_hole CX}\" y=\"{sv:w_hole CY + 0.5 * v}\" font-size=\"{sv:w_hole 0.9 * a}\" text-anchor=\"middle\" font-family=\"monospace\" font-weight=\"bold\" fill=\"#ffffff\">&gt;</text>"
f̲ormat< expands to
"<rect class=\"entity\" x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX − 0.45 × a)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − 0.7 × v)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 0.9 × a)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 1.7 × v)) c̲at "\" fill=\"#2a9d8f\" stroke=\"#a8f0e6\" stroke-width=\"3\"/><text x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY + 0.5 × v)) c̲at "\" font-size=\"" c̲at (f̲ormat (ˢᵛw̲hole 0.9 × a)) c̲at "\" text-anchor=\"middle\" font-family=\"monospace\" font-weight=\"bold\" fill=\"#ffffff\">&gt;</text>"
184} 185⍝│ The view: the ceiling and the floor, the wall ahead, the way out if it 186⍝│ is in sight, then each depth's sides from far to near. 187ʰv̲iew ← { (G, at, f, goal) → 188 (F, L, R) ← ʰl̲ook (at, f) 189 D ← ʰd̲epth (G, F) 190 Lw ← ʰw̲all (G, L) 191 Rw ← ʰw̲all (G, R) 192 back ← @ f̲ormat< "<rect y=\"60\" width=\"800\" height=\"250\" fill=\"{CEILING}\"/><rect y=\"310\" width=\"800\" height=\"250\" fill=\"{FLOOR}\"/>"
f̲ormat< expands to
("<rect y=\"60\" width=\"800\" height=\"250\" fill=\"" c̲at (f̲ormat (CEILING)) c̲at "\"/><rect y=\"310\" width=\"800\" height=\"250\" fill=\"" c̲at (f̲ormat (FLOOR)) c̲at "\"/>")
193 front ← ʰf̲ront D 194 door ← ʰd̲oor w̲here goal = D t̲ake F 195 ks ← r̲ev (r̲ange D) − 1 196 sides ← '{ k → e̲nclose (ʰs̲ide (k, -1.0, (k + 1) s̲elect Lw, (k + 2) s̲elect Lw)) c̲at ʰs̲ide (k, 1.0, (k + 1) s̲elect Rw, (k + 2) s̲elect Rw) } e̲ach ks 197 (e̲nclose back) c̲at (e̲nclose front) c̲at (e̲nclose door) c̲at sides 198} 199⍝│ A block of the mini map: a wall or the floor, the way out teal. 200ʰb̲lock ← { (i, wall, out, x0, y0) → 201 fill ← d̲isclose (1 + wall + 2 × out) s̲elect "#2b2d42" "#c9a66b" "#2a9d8f" 202 e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole x0 + 12.0 * f_loat (i - 1) m_od B}\" y=\"{sv:w_hole y0 + 12.0 * f_loat (i - 1) d_iv B}\" width=\"12\" height=\"12\" fill=\"{fill}\"/>"
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole x0 + 12.0 × f̲loat (i − 1) m̲od B)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole y0 + 12.0 × f̲loat (i − 1) d̲iv B)) c̲at "\" width=\"12\" height=\"12\" fill=\"" c̲at (f̲ormat (fill)) c̲at "\"/>")
203} 204⍝│ The mini map in the top right corner: the blocks seen, and you, an 205⍝│ arrow the way you face. 206ʰm̲ap ← { (G, at, f, seen, goal) → 207 x0 ← 788.0 − 12.0 × f̲loat B 208 y0 ← 72.0 209 frame ← e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole x0 - 6.0}\" y=\"{sv:w_hole y0 - 6.0}\" width=\"{sv:w_hole 12.0 + 12.0 * f_loat B}\" height=\"{sv:w_hole 12.0 + 12.0 * f_loat BH}\" fill=\"#000000\" opacity=\"0.75\" stroke=\"#888888\"/>"
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole x0 − 6.0)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole y0 − 6.0)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 12.0 + 12.0 × f̲loat B)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 12.0 + 12.0 × f̲loat BH)) c̲at "\" fill=\"#000000\" opacity=\"0.75\" stroke=\"#888888\"/>")
210 ids ← w̲here seen 211 blocks ← '{ i → ʰb̲lock (i, i s̲elect G, i = goal, x0, y0) } e̲ach ids 212 x ← x0 + 6.0 + 12.0 × f̲loat (at − 1) m̲od B 213 y ← y0 + 6.0 + 12.0 × f̲loat (at − 1) d̲iv B 214 you ← e̲nclose @ f̲ormat< "<g class=\"entity you\" transform=\"translate({sv:w_hole x} {sv:w_hole y}) rotate({(90 * f) - 90})\"><polygon points=\"8,0 -5,-5 -5,5\" fill=\"#e63946\"/></g>"
f̲ormat< expands to
("<g class=\"entity you\" transform=\"translate(" c̲at (f̲ormat (ˢᵛw̲hole x)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole y)) c̲at ") rotate(" c̲at (f̲ormat ((90 × f) − 90)) c̲at ")\"><polygon points=\"8,0 -5,-5 -5,5\" fill=\"#e63946\"/></g>")
215 frame c̲at blocks c̲at you 216} 217⍝│ The mini map when it shows, else nothing. 218ʰm̲apIf ← { (G, at, f, seen, goal, mp) → mp = 0 ? 0 r̲eshape e̲nclose ""◆ ʰm̲ap (G, at, f, seen, goal) } 219⍝│ What the top line says. 220ʰs̲ays ← { (n, f, won, mp) → 221 won = 1 ? @ f̲ormat< "You found the way out in {n} steps!"
f̲ormat< expands to
("You found the way out in " c̲at (f̲ormat (n)) c̲at " steps!")
222 face ← (1 + f) s̲elect "NESW" 223 @ f̲ormat< "Steps {n} | Facing {face} | Arrows: step and turn | m: map"
f̲ormat< expands to
"Steps " c̲at (f̲ormat (n)) c̲at " | Facing " c̲at (f̲ormat (face)) c̲at " | Arrows: step and turn | m: map"
224} 225⍝ :: (Truthy b, Num c) => (Int, Int, Int, a, b, c, Int) -> Char 226⍝│ The picture, as SVG text: the view ahead (cut off at the picture's 227⍝│ edges), the mini map when it shows, and the top line. 228ˡd̲raw ← { (G, at, f, n, seen, mp, goal) → 229 v ← 0.0 0.0 800.0 560.0 230 m ← ʰm̲apIf (G, at, f, seen, goal, mp) 231 top ← (20.0, 42.0, 28.0) ˢᵛw̲ords ʰs̲ays (n, f, 0 + at = goal, mp) 232 clip ← e̲nclose "<clipPath id=\"view\"><rect width=\"800\" height=\"560\"/></clipPath><g clip-path=\"url(#view)\">" 233 v ˢᵛp̲icture (v ˢᵛr̲ect "fill=\"#111111\"") c̲at clip c̲at (ʰv̲iew (G, at, f, goal)) c̲at (e̲nclose "</g>") c̲at m c̲at top 234}