libraryextensions/scene/demos/Endless.xtl
Endless.xtl -- the endless world and its player, shared by the voxel demos that walk or fly in it (voxels-endless, voxels-fly, and those after): columns of 16 by 16 blocks made around the player and dropped behind it, each made over three frames so no frame waits long, and the player -- walking, swimming, flying -- moving against them. Import: "en:" u_se< "Endless"
The state
ˡreach : Int
the columns are kept this many (16-block steps) from the player's, each way, and made within it; beyond one more they are dropped
ˡreach ← 3
ˡp̲layer : Box a -> a
en:p_layer s: the player's ten numbers
ˡp̲layer ← { s → d̲isclose 1 s̲elect s }
ˡn̲umbers : Box a -> a
en:n_umbers s: the columns' numbers
ˡn̲umbers ← { s → d̲isclose 3 s̲elect s }
ˡs̲ettings : Box a -> a
en:s_ettings s: the window, the next number, the job, the start
ˡs̲ettings ← { s → d̲isclose 4 s̲elect s }
ˡw̲indow : Box a -> a
en:w_indow s: the scene window the world is drawn in
ˡw̲indow ← { s → 1 s̲elect ˡs̲ettings s }
ʰk̲ey : Num a => a -> Float
ʰk̲ey ← { c → f̲loat (10000 × 1000 + 1 s̲elect c) + 1000 + 2 s̲elect c }
ʰw̲ithKeys : a -> Box a -> Box a
ʰw̲ithKeys ← { v s → (1 t̲ake s) c̲at (e̲nclose v) c̲at 2 d̲rop s }
ʰw̲ithNumbers : a -> Box a -> Box a
ʰw̲ithNumbers ← { v s → (2 t̲ake s) c̲at (e̲nclose v) c̲at 3 d̲rop s }
ʰw̲ithSettings : a -> Box a -> Box a
ʰw̲ithSettings ← { v s → (3 t̲ake s) c̲at (e̲nclose v) c̲at 4 d̲rop s }
ˡw̲ithPlayer : a -> Box a -> Box a
en:w_ithPlayer p s: the state with the player p
ˡw̲ithPlayer ← { p s → (e̲nclose p) c̲at 1 d̲rop s }
ˡn̲ew : Num a => a -> Box Float
en:n_ew w: a world for window w, no columns made yet, the player standing above the highest ground of column (0, 0), facing north
ˡn̲ew ← { w → hv ← r̲avel ᵛˣh̲eightsAt 0 0 16 i ← f̲irst w̲here hv = 'm̲ax r̲/ hv x ← (f̲loat (i − 1) d̲iv 16) + 0.5 y ← f̲loat 2 + i s̲elect hv z ← (f̲loat (i − 1) m̲od 16) + 0.5 p ← x c̲at y c̲at z c̲at 0.0 0.0 0.0 0.0 0.0 0.0 0.0 none ← 0 r̲eshape 0.0 set ← (f̲loat w) c̲at 1.0 0.0 0.0 0.0 0.0 c̲at x c̲at y c̲at z (e̲nclose p) c̲at (e̲nclose none) c̲at (e̲nclose none) c̲at (e̲nclose set) c̲at e̲nclose none }
ˡs̲tart : Box a -> a
en:s_tart s: where the player started (x y z)
ˡs̲tart ← { s → 3 t̲ake 6 d̲rop ˡs̲ettings s }
Making and dropping columns
ʰh̲ome : Box Float -> Int
the player's column (cx cz)
ʰh̲ome ← { s → p ← ˡp̲layer s (f̲loor (1 s̲elect p) ÷ 16.0) c̲at f̲loor (3 s̲elect p) ÷ 16.0 }
ʰt̲rim : Box Float -> Box Float
ʰt̲rim ← { s → h ← ʰh̲ome s ks ← ˡk̲eys s far ← ((a̲bs (ʰc̲x ks) − 1 s̲elect h) > ˡreach + 1) + (a̲bs (ʰc̲z ks) − 2 s̲elect h) > ˡreach + 1 0 = '+ r̲/ far ? s j ← f̲irst w̲here far > 0 n ← j s̲elect ˡn̲umbers s w ← ˡw̲indow s gone ← '{ w ˢᶜr̲emove! (10.0 × n) + f̲loat ⍵ } m̲ap r̲ange 7 keep ← j ≠ r̲ange t̲ally ks s ← (keep r̲eplicate ks) ʰw̲ithKeys s s ← (keep r̲eplicate ˡn̲umbers s) ʰw̲ithNumbers s ((5 + j) t̲ake s) c̲at (6 + j) d̲rop s }
ʰs̲endOne : Float -> Int -> Int
ʰs̲endOne ← { nt f → w ← 1 s̲elect nt t ← f̲loor 3 s̲elect nt sel ← (5 s̲elect₂ f) = t 0 = '+ r̲/ sel ? 0 head ← (f̲loat w) c̲at ((10.0 × 2 s̲elect nt) + f̲loat t) c̲at (1 + t) s̲elect ᵛˣcolors head ˢᶜq̲uads! ᵛˣc̲orners sel r̲eplicate f }
ˡs̲tream : Box Float -> Box Float
en:s_tream s: one frame's work on the world: the first column too far away dropped, then a third of making a column -- the nearest missing one's blocks and solid mask (part 0), its faces (part 1), or sending them to scene (part 2) -- about 12 ms at most
ˡs̲tream ← { s → s ← ʰt̲rim s set ← ˡs̲ettings s part ← 3 s̲elect set part = 1.0 ? ʰf̲aces s part = 2.0 ? ʰs̲end s ʰb̲locks s }
ʰb̲locks : Box Float -> Box Float
ʰb̲locks ← { s → h ← ʰh̲ome s ks ← ˡk̲eys s r ← ˡreach o ← (r̲ange 1 + 2 × r) − 1 + r dx ← r̲avel o '+ t̲able 0 × o dz ← r̲avel (0 × o) '+ t̲able o ws ← f̲loat (10000 × 1000 + dx + 1 s̲elect h) + 1000 + dz + 2 s̲elect h miss ← (0 = ws m̲ember? ks) r̲eplicate (r̲ange t̲ally ws) 0 = t̲ally miss ? s d ← (miss s̲elect dx × dx) + miss s̲elect dz × dz i ← f̲irst (d = 'm̲in r̲/ d) r̲eplicate miss c ← ((i s̲elect dx) + 1 s̲elect h) c̲at (i s̲elect dz) + 2 s̲elect h b ← ᵛˣc̲olumnBlocks c set ← ˡs̲ettings s n ← 2 s̲elect set set ← (1 s̲elect set) c̲at (n + 1.0) c̲at 1.0 c̲at (f̲loat c) c̲at n c̲at 6 d̲rop set s ← (ks c̲at i s̲elect ws) ʰw̲ithKeys s s ← ((ˡn̲umbers s) c̲at n) ʰw̲ithNumbers s s ← (set) ʰw̲ithSettings s s ← (f̲loat r̲avel b) ʰw̲ithJob s s c̲at e̲nclose f̲loat ᵛˣc̲olumnMask b }
ʰf̲aces : Box Float -> Box Float
ʰf̲aces ← { s → set ← ˡs̲ettings s c ← f̲loor 4 5 s̲elect set b ← 18 32 18 r̲eshape f̲loor d̲isclose 5 s̲elect s f ← c ᵛˣc̲olumnFaces b s ← ((2 t̲ake set) c̲at 2.0 c̲at 3 d̲rop set) ʰw̲ithSettings s (f̲loat r̲avel f) ʰw̲ithJob s }
ʰs̲end : Box Float -> Box Float
ʰs̲end ← { s → set ← ˡs̲ettings s v ← d̲isclose 5 s̲elect s f ← (((t̲ally v) d̲iv 5) c̲at 5) r̲eshape f̲loor v n ← 6 s̲elect set w ← 1 s̲elect set sent ← '{ (w c̲at n c̲at f̲loat ⍵) ʰs̲endOne f } m̲ap r̲ange 7 s ← ((2 t̲ake set) c̲at 0.0 c̲at 3 d̲rop set) ʰw̲ithSettings s (0 r̲eshape 0.0) ʰw̲ithJob s }
Collision
ʰc̲ell : (Num a, Truthy a) => Box Float -> Int -> a
ʰc̲ell ← { s v → x ← 1 s̲elect v z ← 2 s̲elect v ys ← 2 d̲rop v i ← (ˡk̲eys s) i̲ndexOf ʰk̲ey (x d̲iv 16) c̲at z d̲iv 16 i > t̲ally ˡk̲eys s ? 1 m ← d̲isclose (5 + i) s̲elect s 0 < '+ r̲/ (1 + (z m̲od 16) + 16 × ys + 32 × x m̲od 16) s̲elect m }
ˡh̲it : Box Float -> Float -> Float
s en:h_it q: whether the player's box (0.6 wide, 1.75 tall) with its feet at q (x y z) overlaps a solid block; below the world is solid, above it air
ˡh̲it ← { s q → x ← 1 s̲elect q y ← 2 s̲elect q z ← 3 s̲elect q cx ← u̲nique f̲loor (x − 0.3) c̲at x + 0.3 cy ← u̲nique f̲loor y c̲at (y + 0.9) c̲at y + 1.75 cz ← u̲nique f̲loor (z − 0.3) c̲at z + 0.3 0 < '+ r̲/ cy < 0 ? 1.0 cy ← (cy ≤ 31) r̲eplicate cy 0 = t̲ally cy ? 0.0 px ← r̲avel cx '{ ⍺ + 0 × ⍵ } t̲able cz pz ← r̲avel cx '{ ⍵ + 0 × ⍺ } t̲able cz hits ← '{ s ʰc̲ell (⍵ s̲elect px) c̲at (⍵ s̲elect pz) c̲at cy } e̲ach r̲ange t̲ally px f̲loat 0 < '+ r̲/ hits }
The player
ʰs̲et : Float -> Float -> Float
ʰs̲et ← { l p → p + (f̲loat (r̲ange 10) = f̲loor 1 s̲elect l) × (2 s̲elect l) − p }
ʰs̲nap : Float -> Float -> Float
ʰs̲nap ← { v a → v > 0.0 ? (f̲loat f̲loor a + 0.3) − 0.3001 (f̲loat 1 + f̲loor a − 0.3) + 0.3001 }
ʰm̲x : Box Float -> Float -> Float
ʰm̲x ← { s p → a ← (1 s̲elect p) + ˡdt × 4 s̲elect p q ← (1 c̲at a) ʰs̲et p 0 = s ˡh̲it q ? q (4 c̲at 0.0) ʰs̲et (1 c̲at (4 s̲elect p) ʰs̲nap a) ʰs̲et p }
ʰm̲z : Box Float -> Float -> Float
ʰm̲z ← { s p → a ← (3 s̲elect p) + ˡdt × 6 s̲elect p q ← (3 c̲at a) ʰs̲et p 0 = s ˡh̲it q ? q (6 c̲at 0.0) ʰs̲et (3 c̲at (6 s̲elect p) ʰs̲nap a) ʰs̲et p }
ʰm̲y : Box Float -> Float -> Float
ʰm̲y ← { s p → a ← (2 s̲elect p) + ˡdt × 5 s̲elect p q ← (9 c̲at 0.0) ʰs̲et (2 c̲at a) ʰs̲et p 0 = s ˡh̲it q ? q (5 s̲elect p) > 0.0 ? (5 c̲at 0.0) ʰs̲et (2 c̲at (f̲loat f̲loor a + 1.75) − 1.7501) ʰs̲et p (9 c̲at 1.0) ʰs̲et (5 c̲at 0.0) ʰs̲et (2 c̲at f̲loat 1 + f̲loor a) ʰs̲et p }
ˡm̲ove : Box Float -> Float -> Box Float
s en:m_ove k: one frame of the player under the controls k (mouse dx dy, then 1 for each held: w a s d space shift left right up down), walking or flying, the camera set to its eye. Walking: W A S D, Shift runs, Space jumps; in water (all air below the sea at 14) it sinks slowly, Space swims up, and from the surface Space climbs onto a bank one block high. Flying: W A S D level by the heading, Space up, Shift down, 15 blocks a second, no gravity, the blocks still stopping it.
ˡm̲ove ← { s k → p ← ˡp̲layer s yaw ← (7 s̲elect p) + (0.005 × 1 s̲elect k) + 2.0 × ˡdt × (10 s̲elect k) − 9 s̲elect k pitch ← -1.5 m̲ax 1.5 m̲in (8 s̲elect p) + (-0.005 × 2 s̲elect k) + 1.5 × ˡdt × (11 s̲elect k) − 12 s̲elect k fwd ← (3 s̲elect k) − 5 s̲elect k side ← (6 s̲elect k) − 4 s̲elect k fly ← 10 s̲elect p wet ← (1.0 − fly) × f̲loat 14.0 > 0.9 + 2 s̲elect p speed ← (fly × 15.0) + (1.0 − fly) × (1.0 − 0.4 × wet) × 4.5 × 1.0 + 8 s̲elect k vx ← speed × ((s̲in yaw) × fwd) + (c̲os yaw) × side vz ← speed × ((0.0 − c̲os yaw) × fwd) + (s̲in yaw) × side surface ← (1.0 − fly) × (f̲loat 14.5 > 2 s̲elect p) × f̲loat 13.0 < 2 s̲elect p jump ← (1.0 − fly) × (1.0 m̲in (9 s̲elect p) + surface × 1.0 − wet) × 7 s̲elect k swim ← wet × 7 s̲elect k fall ← -30.0 m̲ax (5 s̲elect p) − 22.0 × ˡdt sink ← -2.0 m̲ax (5 s̲elect p) − 6.0 × ˡdt walkVy ← (7.5 × jump) + (1.0 − jump) × (4.0 × swim) + (1.0 − swim) × (wet × sink) + (1.0 − wet) × fall flyVy ← ((80.0 − 2 s̲elect p) × 60.0) m̲in 15.0 × (7 s̲elect k) − 8 s̲elect k vy ← (fly × flyVy) + (1.0 − fly) × walkVy p ← (7 c̲at yaw) ʰs̲et (8 c̲at pitch) ʰs̲et (4 c̲at vx) ʰs̲et (5 c̲at vy) ʰs̲et (6 c̲at vz) ʰs̲et p p ← s ʰm̲y s ʰm̲z s ʰm̲x p e ← (ˡw̲indow s) ˢᶜe̲ye! (1 s̲elect p) c̲at ((2 s̲elect p) + 1.6) c̲at (3 s̲elect p) c̲at yaw c̲at pitch p ˡw̲ithPlayer s }
ˡh̲omeAgain : Box Float -> Box Float
en:h_omeAgain s: the player back at the start, standing still, walking
ˡh̲omeAgain ← { s → ((ˡs̲tart s) c̲at 0.0 0.0 0.0 0.0 0.0 0.0 0.0) ˡw̲ithPlayer s }
ˡt̲oggleFly : Box Float -> Box Float
en:t_oggleFly s: flying, or walking again (falling from where it is)
ˡt̲oggleFly ← { s → p ← ˡp̲layer s ((6 t̲ake p) c̲at (7 8 s̲elect p) c̲at 0.0 c̲at 1.0 − 10 s̲elect p) ˡw̲ithPlayer s }
ˡs̲top : Box Float -> Box Float
en:s_top s: the state marked as finished (the next number negative)
ˡs̲top ← { s → ((1 t̲ake ˡs̲ettings s) c̲at (0.0 − 2 s̲elect ˡs̲ettings s) c̲at 2 d̲rop ˡs̲ettings s) ʰw̲ithSettings s }