better lighting

This commit is contained in:
asrael 2026-01-31 09:31:17 -06:00
parent 805a2713a3
commit 6ed4e17065
75 changed files with 6417 additions and 3667 deletions

View file

@ -6,6 +6,7 @@ extern crate alloc;
use pxl8d::*;
use pxl8d::chunk::ChunkId;
use pxl8d::chunk::stream::ClientChunkState;
use pxl8d::math::Vec3;
const TICK_RATE: u64 = 30;
const TICK_NS: u64 = 1_000_000_000 / TICK_RATE;
@ -73,14 +74,15 @@ pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 {
let mut player_id: Option<u64> = None;
let mut last_client_tick: u64 = 0;
let mut client_chunks = ClientChunkState::new();
let mut client_stream_radius: i32 = 3;
let mut sequence: u32 = 0;
let mut last_tick = get_time_ns();
let mut entities_buf = [protocol::pxl8_entity_state {
let mut entities_buf = [pxl8d::pxl8_entity_state {
entity_id: 0,
userdata: [0u8; 56],
}; 64];
let mut inputs_buf: [protocol::pxl8_input_msg; 16] = unsafe { core::mem::zeroed() };
let mut inputs_buf: [pxl8d::pxl8_input_msg; 16] = unsafe { core::mem::zeroed() };
pxl8_debug!("[SERVER] Entering main loop");
loop {
@ -91,15 +93,15 @@ pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 {
last_tick = now;
let dt = (elapsed as f32) / 1_000_000_000.0;
let mut latest_input: Option<protocol::pxl8_input_msg> = None;
let mut latest_input: Option<pxl8d::pxl8_input_msg> = None;
while let Some(msg_type) = transport.recv() {
match msg_type {
x if x == protocol::pxl8_msg_type::PXL8_MSG_INPUT as u8 => {
x if x == pxl8d::pxl8_msg_type::PXL8_MSG_INPUT as u8 => {
latest_input = Some(transport.get_input());
}
x if x == protocol::pxl8_msg_type::PXL8_MSG_COMMAND as u8 => {
x if x == pxl8d::pxl8_msg_type::PXL8_MSG_COMMAND as u8 => {
let cmd = transport.get_command();
if cmd.cmd_type == protocol::pxl8_cmd_type::PXL8_CMD_SPAWN_ENTITY as u16 {
if cmd.cmd_type == pxl8d::pxl8_cmd_type::PXL8_CMD_SPAWN_ENTITY as u16 {
let (x, y, z, _yaw, _pitch) = extract_spawn_position(&cmd.payload);
player_id = Some(sim.spawn_player(x, y, z) as u64);
@ -110,6 +112,46 @@ pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 {
pxl8_debug!("[SERVER] Sending CHUNK_ENTER for BSP id=1");
transport.send_chunk_enter(1, transport::CHUNK_TYPE_BSP, sequence);
sequence = sequence.wrapping_add(1);
pxl8_debug!("[SERVER] Preloading voxel chunks around spawn and door");
let spawn_pos = Vec3 { x, y, z };
let door_y = sim.voxels.find_surface_y(942.0, 416.0);
let door_pos = Vec3 { x: 942.0, y: door_y, z: 416.0 };
pxl8_debug!("[SERVER] Door placed at surface y={}", door_y);
sim.voxels.load_chunks_around(spawn_pos, client_stream_radius);
sim.voxels.load_chunks_around(door_pos, client_stream_radius);
client_chunks.request_vxl_radius(spawn_pos, client_stream_radius, &sim.voxels);
client_chunks.request_vxl_radius(door_pos, client_stream_radius, &sim.voxels);
} else if cmd.cmd_type == pxl8d::pxl8_cmd_type::PXL8_CMD_EXIT_CHUNK as u16 {
sim.world.clear_active();
client_chunks.clear_pending();
client_chunks.clear_known_vxl();
client_chunks.clear_known_bsp();
transport.send_chunk_exit(sequence);
sequence = sequence.wrapping_add(1);
let exit_x = f32::from_be_bytes([cmd.payload[0], cmd.payload[1], cmd.payload[2], cmd.payload[3]]);
let exit_y = f32::from_be_bytes([cmd.payload[4], cmd.payload[5], cmd.payload[6], cmd.payload[7]]);
let exit_z = f32::from_be_bytes([cmd.payload[8], cmd.payload[9], cmd.payload[10], cmd.payload[11]]);
let exit_pos = Vec3 { x: exit_x, y: exit_y, z: exit_z };
sim.teleport_player(exit_x, exit_y, exit_z);
sim.voxels.load_chunks_around(exit_pos, client_stream_radius);
client_chunks.request_vxl_radius(exit_pos, client_stream_radius, &sim.voxels);
} else if cmd.cmd_type == pxl8d::pxl8_cmd_type::PXL8_CMD_ENTER_CHUNK as u16 {
let chunk_id = u32::from_be_bytes([
cmd.payload[0], cmd.payload[1], cmd.payload[2], cmd.payload[3]
]);
pxl8_debug!("[SERVER] Enter chunk command - entering BSP {}", chunk_id);
if sim.world.contains(&ChunkId::Bsp(chunk_id)) {
sim.world.set_active(ChunkId::Bsp(chunk_id));
client_chunks.request(ChunkId::Bsp(chunk_id));
transport.send_chunk_enter(chunk_id, transport::CHUNK_TYPE_BSP, sequence);
sequence = sequence.wrapping_add(1);
}
} else if cmd.cmd_type == pxl8d::pxl8_cmd_type::PXL8_CMD_SET_CHUNK_SETTINGS as u16 {
let render_dist = i32::from_be_bytes([
cmd.payload[0], cmd.payload[1], cmd.payload[2], cmd.payload[3]
]);
client_stream_radius = render_dist.clamp(1, 8);
}
}
_ => {}
@ -132,7 +174,7 @@ pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 {
}
});
let header = protocol::pxl8_snapshot_header {
let header = pxl8d::pxl8_snapshot_header {
entity_count: count as u16,
event_count: 0,
player_id: player_id.unwrap_or(0),
@ -147,54 +189,36 @@ pub extern "C" fn main(_argc: i32, _argv: *const *const u8) -> i32 {
if let Some(player) = sim.get_player_position(pid) {
let pos = Vec3 { x: player.0, y: player.1, z: player.2 };
if sim.world.active().is_some() {
while let Some(chunk_id) = client_chunks.next_pending() {
match chunk_id {
ChunkId::Bsp(id) => {
pxl8_debug!("[SERVER] Processing pending BSP chunk");
if let Some(chunk) = sim.world.get(&chunk_id) {
if let Some(bsp) = chunk.as_bsp() {
let msgs = bsp_to_messages(bsp, id, chunk.version());
pxl8_debug!("[SERVER] BSP serialized, queueing messages");
client_chunks.queue_messages(msgs);
client_chunks.mark_sent(chunk_id, chunk.version());
}
}
}
ChunkId::Vxl(cx, cy, cz) => {
if let Some(chunk) = sim.voxels.get_chunk(cx, cy, cz) {
let msgs = transport::ChunkMessage::from_voxel(chunk, 1);
sim.voxels.load_chunks_around(pos, client_stream_radius);
client_chunks.request_vxl_radius(pos, client_stream_radius, &sim.voxels);
while let Some(chunk_id) = client_chunks.next_pending() {
match chunk_id {
ChunkId::Bsp(id) => {
if let Some(chunk) = sim.world.get(&chunk_id) {
if let Some(bsp) = chunk.as_bsp() {
let msgs = bsp_to_messages(bsp, id, chunk.version());
client_chunks.queue_messages(msgs);
client_chunks.mark_sent(chunk_id, 1);
client_chunks.mark_sent(chunk_id, chunk.version());
}
}
}
}
for _ in 0..4 {
if let Some(msg) = client_chunks.next_message() {
transport.send_chunk(&msg, sequence);
sequence = sequence.wrapping_add(1);
}
}
} else {
client_chunks.request_vxl_radius(pos, 2, &sim.voxels);
for _ in 0..2 {
if let Some(chunk_id) = client_chunks.next_pending() {
if let ChunkId::Vxl(cx, cy, cz) = chunk_id {
if let Some(chunk) = sim.voxels.get_chunk(cx, cy, cz) {
let msgs = transport::ChunkMessage::from_voxel(chunk, 1);
for msg in &msgs {
transport.send_chunk(msg, sequence);
sequence = sequence.wrapping_add(1);
}
client_chunks.mark_sent(chunk_id, 1);
}
ChunkId::Vxl(cx, cy, cz) => {
if let Some(chunk) = sim.voxels.get_chunk(cx, cy, cz) {
let msgs = transport::ChunkMessage::from_voxel(chunk, 1);
client_chunks.queue_messages(msgs);
client_chunks.mark_sent(chunk_id, 1);
}
}
}
}
for _ in 0..8 {
if let Some(msg) = client_chunks.next_message() {
transport.send_chunk(&msg, sequence);
sequence = sequence.wrapping_add(1);
}
}
}
}
}
@ -250,7 +274,7 @@ fn bsp_to_messages(bsp: &bsp::Bsp, id: u32, version: u32) -> alloc::vec::Vec<tra
data.extend_from_slice(&p.normal.y.to_be_bytes());
data.extend_from_slice(&p.normal.z.to_be_bytes());
data.extend_from_slice(&p.dist.to_be_bytes());
data.extend_from_slice(&p.plane_type.to_be_bytes());
data.extend_from_slice(&p.type_.to_be_bytes());
}
for f in &bsp.faces {