stream world data from pxl8d to pxl8

This commit is contained in:
asrael 2026-01-25 09:26:30 -06:00
parent 39ee0fefb7
commit a71a9840b2
55 changed files with 5290 additions and 2131 deletions

View file

@ -11,7 +11,7 @@
(local bob-amount 4.0)
(local bob-speed 8.0)
(local cam-smoothing 0.25)
(local cell-size 64)
(local chunk-size 64)
(local cursor-sensitivity 0.010)
(local gravity -800)
(local grid-size 64)
@ -32,25 +32,29 @@
(var auto-run-cancel-key nil)
(var bob-time 0)
(var cam-pitch 0)
(var cam-x 1000)
(var cam-x 416)
(var cam-y 64)
(var cam-yaw 0)
(var cam-z 1000)
(var cam-z 416)
(var camera nil)
(var grounded? true)
(var land-squash 0)
(var light-time 0)
(var real-time 0)
(var network nil)
(var smooth-cam-x 1000)
(var smooth-cam-z 1000)
(var velocity-y 0)
(var world nil)
(var ceiling-tex nil)
(var fireball-mesh nil)
(var floor-tex nil)
(var fps-avg 0)
(var fps-sample-count 0)
(var fireball-mesh nil)
(var grounded? true)
(var land-squash 0)
(var last-dt 0.016)
(var light-time 0)
(var lights nil)
(var materials-setup false)
(var network nil)
(var real-time 0)
(var smooth-cam-x 416)
(var smooth-cam-z 416)
(var velocity-y 0)
(var wall-tex nil)
(var world nil)
(local cursor-look? true)
(local FIREBALL_COLOR 218)
@ -247,43 +251,54 @@
(pxl8.set_palette_rgb (+ FIREBALL_COLOR i) r g b)))
(sky.update-gradient 1 2 6 6 10 18)
(pxl8.update_palette_deps)
(set camera (pxl8.create_camera_3d))
(set world (pxl8.create_world))
(set lights (pxl8.create_lights))
(when (not camera)
(set camera (pxl8.create_camera_3d)))
(when (not lights)
(set lights (pxl8.create_lights)))
(when (not fireball-mesh)
(create-fireball-mesh))
(sky.generate-stars 12345)
(create-fireball-mesh)
(set network (net.Net.new {:port 7777}))
(when network
(network:connect)
(network:spawn cam-x cam-y cam-z cam-yaw cam-pitch))
(when (not network)
(set network (net.get))
(when network
(network:spawn cam-x cam-y cam-z cam-yaw cam-pitch)))
(let [result (world:generate {
:type pxl8.PROCGEN_ROOMS
:width 64
:height 64
:seed 42
:min_room_size 5
:max_room_size 10
:num_rooms 20})]
(if (< result 0)
(pxl8.error (.. "Failed to generate rooms - result: " result))
(let [floor-tex (textures.mossy-cobblestone 44444 STONE_FLOOR_START MOSS_COLOR)
wall-tex (textures.ashlar-wall 55555 STONE_WALL_START MOSS_COLOR)
sky-tex (pxl8.create_texture [0] 1 1)]
(when (not world)
(set world (pxl8.get_world)))
(let [result (world:apply_textures [
{:name "floor"
:texture_id floor-tex
:rule (fn [normal] (> normal.y 0.7))}
{:name "ceiling"
:texture_id sky-tex
:rule (fn [normal] (< normal.y -0.7))}
{:name "wall"
:texture_id wall-tex
:rule (fn [normal] (and (<= normal.y 0.7) (>= normal.y -0.7)))}])]
(when (< result 0)
(pxl8.error (.. "Failed to apply textures - result: " result))))))))
(when (not floor-tex)
(set floor-tex (textures.mossy-cobblestone 44444 STONE_FLOOR_START MOSS_COLOR)))
(when (not wall-tex)
(set wall-tex (textures.ashlar-wall 55555 STONE_WALL_START MOSS_COLOR)))
(when (not ceiling-tex)
(set ceiling-tex (pxl8.create_texture [0] 1 1)))
)
(fn setup-materials []
(when (and world (not materials-setup))
(let [chunk (world:active_chunk)]
(when (and chunk (chunk:ready))
(let [bsp (chunk:bsp)]
(when bsp
(let [floor-mat (pxl8.create_material {:texture floor-tex :lighting true})
wall-mat (pxl8.create_material {:texture wall-tex :lighting true})
ceiling-mat (pxl8.create_material {:texture ceiling-tex})]
(bsp:set_material 0 floor-mat)
(bsp:set_material 1 wall-mat)
(bsp:set_material 2 ceiling-mat)
(for [i 0 (- (bsp:face_count) 1)]
(let [n (bsp:face_normal i)]
(bsp:face_set_material i
(if (> n.y 0.7) 0
(< n.y -0.7) 2
1))))
(set materials-setup true))))))))
(fn sample-input []
(var move-forward 0)
@ -328,9 +343,10 @@
(let [input (get-pending-input t)
hist (get-position t)]
(when (and input hist)
(let [(new-x new-z) (apply-movement cam-x cam-z hist.yaw input)]
(set cam-x new-x)
(set cam-z new-z)
(let [(new-x new-z) (apply-movement cam-x cam-z hist.yaw input)
(resolved-x _ resolved-z) (world:resolve_collision cam-x cam-y cam-z new-x cam-y new-z 5)]
(set cam-x resolved-x)
(set cam-z resolved-z)
(store-position t cam-x cam-z hist.yaw))))))))))
(fn update [dt]
@ -343,9 +359,12 @@
(set fps-sample-count 0)
(set fps-avg 0)))
(when (world:is_loaded)
(setup-materials)
(let [chunk (world:active_chunk)]
(when (and chunk (chunk:ready))
(let [input (sample-input)
grid-max (* grid-size cell-size)
grid-max (* grid-size chunk-size)
movement-yaw cam-yaw]
(set time-accumulator (+ time-accumulator dt))
@ -388,18 +407,16 @@
:look_dy input.look_dy
:yaw movement-yaw
:tick client-tick})
(network:update dt)
(when (network:poll)
(let [snapshot (network:snapshot)]
(when (and snapshot (> snapshot.tick last-processed-tick))
(set last-processed-tick snapshot.tick)
(let [player-id (network:player_id)]
(when (> player-id 0)
(let [curr (network:entity_userdata player-id)]
(when curr
(let [srv-x (pxl8.unpack_f32_be curr 0)
srv-z (pxl8.unpack_f32_be curr 8)]
(reconcile snapshot.tick srv-x srv-z)))))))))))]
(let [snapshot (network:snapshot)]
(when (and snapshot (> snapshot.tick last-processed-tick))
(set last-processed-tick snapshot.tick)
(let [player-id (network:player_id)]
(when (> player-id 0)
(let [curr (network:entity_userdata player-id)]
(when curr
(let [srv-x (pxl8.unpack_f32_be curr 0)
srv-z (pxl8.unpack_f32_be curr 8)]
(reconcile snapshot.tick srv-x srv-z))))))))))]
(when (not ok)
(pxl8.error (.. "Network error: " err)))))
@ -430,7 +447,7 @@
(set bob-time (+ (* bob-time 0.8) (* target-phase 0.2))))))
(set light-time (+ light-time (* dt 0.5)))
(set real-time (+ real-time dt)))))
(set real-time (+ real-time dt))))))
(fn frame []
(pxl8.clear 1)
@ -441,10 +458,11 @@
(when (not world)
(pxl8.error "world is nil!"))
(when (and world (not (world:is_loaded)))
(pxl8.text "World not loaded yet..." 5 30 12))
(let [chunk (when world (world:active_chunk))]
(when (and world (or (not chunk) (not (chunk:ready))))
(pxl8.text "Waiting for world data..." 5 30 12))
(when (and camera world (world:is_loaded))
(when (and camera world chunk (chunk:ready))
(let [bob-offset (* (math.sin bob-time) bob-amount)
eye-y (+ cam-y bob-offset land-squash)
forward-x (- (math.sin cam-yaw))
@ -459,8 +477,8 @@
[0 1 0])
(camera:set_perspective 1.047 aspect 1.0 4096.0)
(let [light-x (+ 1000 (* 50 (math.cos light-time)))
light-z (+ 940 (* 50 (math.sin light-time)))
(let [light-x (+ 384 (* 50 (math.cos light-time)))
light-z (+ 324 (* 50 (math.sin light-time)))
light-y 80
phase (+ (* light-x 0.01) 1.7)
f1 (* 0.08 (math.sin (+ (* real-time 2.5) phase)))
@ -484,7 +502,10 @@
(sky.render smooth-cam-x eye-y smooth-cam-z (menu.is-wireframe))
(pxl8.clear_depth)
(world:set_wireframe (menu.is-wireframe) 15)
(when chunk
(let [bsp (chunk:bsp)]
(when bsp
(bsp:set_wireframe (menu.is-wireframe)))))
(world:render [smooth-cam-x eye-y smooth-cam-z])
(when fireball-mesh
@ -509,7 +530,7 @@
(pxl8.text (.. "fps: " (string.format "%.0f" fps-avg)) 5 5 text-color)
(pxl8.text (.. "pos: " (string.format "%.0f" cam-x) ","
(string.format "%.0f" cam-y) ","
(string.format "%.0f" cam-z)) 5 15 text-color)))))
(string.format "%.0f" cam-z)) 5 15 text-color))))))
{:init init
:update update