sourcegames/dungeon/dungeon.xtl
1⍝!/usr/bin/env xetal
2⍝│ The dungeon, scripted (seed 1), with the rules of Crawl.xtl on
3⍝│ lib/Ecs: the world as a matrix, what each entity is as the components
4⍝│ it has, and every rule as one expression over all of them.
5"cr:" u̲se< "Crawl"
6"ec:" u̲se< "Ecs"
7
8⍝ A new game (seed 1): one of the maps, with you, the way out, two rats,
9⍝ two goblins, two potions and two piles of gold placed on its floor at
10⍝ random. The world: a row per entity slot (32) and a column per field
11⍝ (20: alive, then for each component whether the entity has it, then
12⍝ its fields); 10 entities are alive.
13start ← ᶜʳn̲ew 0
14(W, _, _, _, _) ← start
15s̲hape W
16ᵉᶜc̲ount W
17
18⍝ What each entity is, as the components it has: a row each, a 1 where
19⍝ it has hit points, attack, chase, heal, gold (its kind's table in
20⍝ dungeon.toml gives it those; nothing in the code names a kind).
21ᶜʳh̲as start
22
23⍝ A few turns (right twice, down twice, left twice): what happens is
24⍝ said, and the monsters that see you come closer. The steps are the
25⍝ rows of a matrix (a row and a column each), taken one after another.
26ᵘw̲alk ← { s D → 0 = t̲ally D ? s◆ (s ᶜʳt̲urn f̲irst D) ᵘw̲alk 1 d̲rop D }
27walked ← start ᵘw̲alk 6 2 r̲eshape 0 1 0 1 1 0 1 0 0 -1 0 -1.0
28(_, _, _, m, _) ← walked
29m
30ᶜʳy̲ou walked
31_ ← ⎕S̲HOW ᶜʳd̲raw walked
32
33⍝ What the game guarantees, checked by a_ssert< (silent when it holds).
34ᵘw̲orld ← { (W, g, n, m, lv) → W }
35_ ← "10 = ec:c_ount u:w_orld cr:n_ew 0" a̲ssert< "a new dungeon holds ten entities"
36_ ← "6 = 3 s_elect cr:y_ou walked" a̲ssert< "six turns taken"
a̲ssert< expands to
({ @ → (10 = ᵉᶜc̲ount ᵘw̲orld ᶜʳn̲ew 0) ? @◆ ⎕E̲RR "assertion failed: 10 = ec:c_ount u:w_orld cr:n_ew 0 (a new dungeon holds ten entities) [games/dungeon/dungeon.xtl:35]"◆ @ } @)