Compare commits

..

No commits in common. "9f5b974f82f080c7c5dce2627a68b948691f5bfd" and "f5fd6d0b054b85bcbc7f733c0352e64df25c4be4" have entirely different histories.

12 changed files with 88 additions and 136 deletions

View file

@ -8,10 +8,10 @@ use kubi_shared::{
ServerToClientMessage,
InitData,
ClientInitData,
ClientToServerMessageType,
C_CLIENT_HELLO,
},
client::{Client, ClientId, Username},
channels::Channel,
channels::{CHANNEL_AUTH, CHANNEL_SYS_EVT},
},
player::{Player, PLAYER_HEALTH},
transform::Transform, entity::{Entity, Health}
@ -37,7 +37,7 @@ pub fn authenticate_players(
let ServerEvent::Receive(client_addr, data) = event else{
continue
};
if !event.is_message_of_type::<{ClientToServerMessageType::ClientHello as u8}>() {
if !event.is_message_of_type::<C_CLIENT_HELLO>() {
continue
}
let Some(client) = server.0.client(client_addr) else {
@ -62,7 +62,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "Incorrect password".into()
}).unwrap().into_boxed_slice(),
Channel::Auth as usize,
CHANNEL_AUTH,
SendMode::Reliable
);
continue
@ -72,7 +72,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "This server is password protected".into()
}).unwrap().into_boxed_slice(),
Channel::Auth as usize,
CHANNEL_AUTH,
SendMode::Reliable
);
continue
@ -88,7 +88,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
reason: "Can't find a free spot for you!".into()
}).unwrap().into_boxed_slice(),
Channel::Auth as usize,
CHANNEL_AUTH,
SendMode::Reliable
);
continue
@ -164,7 +164,7 @@ pub fn authenticate_players(
};
other_client.borrow_mut().send(
postcard::to_allocvec(&message).unwrap().into_boxed_slice(),
Channel::SysEvt as usize,
CHANNEL_SYS_EVT,
SendMode::Reliable
);
}
@ -175,7 +175,7 @@ pub fn authenticate_players(
postcard::to_allocvec(&ServerToClientMessage::ServerHello {
init: init_data
}).unwrap().into_boxed_slice(),
Channel::Auth as usize,
CHANNEL_AUTH,
SendMode::Reliable
);

View file

@ -6,8 +6,8 @@ use std::net::SocketAddr;
use kubi_shared::{
networking::{
client::{ClientIdMap, Client},
messages::{ClientToServerMessage, ServerToClientMessage, ClientToServerMessageType},
channels::Channel
messages::{ClientToServerMessage, ServerToClientMessage, C_POSITION_CHANGED},
channels::CHANNEL_MOVE
},
transform::Transform
};
@ -41,10 +41,9 @@ pub fn sync_client_positions(
addrs: View<ClientAddress>,
) {
for event in &events.0 {
let Some(message) = check_message_auth
::<{ClientToServerMessageType::PositionChanged as u8}>
(&server, event, &clients, &addr_map) else { continue };
let Some(message) = check_message_auth::<C_POSITION_CHANGED>(&server, event, &clients, &addr_map) else {
continue;
};
let ClientToServerMessage::PositionChanged { position, velocity: _, direction } = message.message else {
unreachable!()
};
@ -73,7 +72,7 @@ pub fn sync_client_positions(
direction
}
).unwrap().into_boxed_slice(),
Channel::Move as usize,
CHANNEL_MOVE,
SendMode::Reliable
);
}

View file

@ -2,8 +2,8 @@ use shipyard::{Unique, UniqueView, UniqueViewMut, Workload, IntoWorkload, AllSto
use glam::IVec3;
use hashbrown::HashMap;
use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ClientToServerMessageType},
channels::Channel,
messages::{ClientToServerMessage, ServerToClientMessage, C_CHUNK_SUB_REQUEST, C_QUEUE_BLOCK},
channels::{CHANNEL_WORLD, CHANNEL_BLOCK},
client::Client,
};
use uflow::{server::RemoteClient, SendMode};
@ -45,11 +45,7 @@ pub fn send_chunk_compressed(
ser_message.truncate(1);
ser_message.append(&mut compressed);
let ser_message = ser_message.into_boxed_slice();
client.borrow_mut().send(
ser_message,
Channel::World as usize,
SendMode::Reliable
);
client.borrow_mut().send(ser_message, CHANNEL_WORLD, SendMode::Reliable);
Ok(())
}
@ -63,10 +59,9 @@ fn process_chunk_requests(
clients: View<Client>
) {
for event in &events.0 {
let Some(message) = check_message_auth
::<{ClientToServerMessageType::ChunkSubRequest as u8}>
(&server, event, &clients, &addr_map) else { continue };
let Some(message) = check_message_auth::<C_CHUNK_SUB_REQUEST>(&server, event, &clients, &addr_map) else {
continue;
};
let ClientToServerMessage::ChunkSubRequest { chunk: chunk_position } = message.message else {
unreachable!()
};
@ -156,10 +151,9 @@ fn process_block_queue_messages(
addrs: View<ClientAddress>,
) {
for event in &events.0 {
let Some(message) = check_message_auth
::<{ClientToServerMessageType::QueueBlock as u8}>
(&server, event, &clients, &addr_map) else { continue };
let Some(message) = check_message_auth::<C_QUEUE_BLOCK>(&server, event, &clients, &addr_map) else {
continue;
};
let ClientToServerMessage::QueueBlock { item } = message.message else { unreachable!() };
//TODO place in our own queue, for now just send to other clients
log::info!("Placed block {:?} at {}", item.block_type, item.position);
@ -178,7 +172,7 @@ fn process_block_queue_messages(
postcard::to_allocvec(
&ServerToClientMessage::QueueBlock { item }
).unwrap().into_boxed_slice(),
Channel::Block as usize,
CHANNEL_BLOCK,
SendMode::Reliable,
);
}

View file

@ -1,9 +1,6 @@
#[repr(u8)]
pub enum Channel {
Generic = 0,
Auth = 1,
World = 2,
Block = 3,
Move = 4,
SysEvt = 5,
}
pub const CHANNEL_GENERIC: usize = 0;
pub const CHANNEL_AUTH: usize = 1;
pub const CHANNEL_WORLD: usize = 2;
pub const CHANNEL_BLOCK: usize = 3;
pub const CHANNEL_MOVE: usize = 4;
pub const CHANNEL_SYS_EVT: usize = 5;

View file

@ -5,86 +5,60 @@ use super::client::ClientId;
pub const PROTOCOL_ID: u16 = 0;
pub trait ToMessageType<T> {
fn message_type(&self) -> T;
}
#[repr(u8)]
#[non_exhaustive]
pub enum ClientToServerMessageType {
ClientHello = 0,
PositionChanged = 1,
ChunkSubRequest = 2,
ChunkUnsubscribe = 3,
QueueBlock = 4,
}
pub const C_CLIENT_HELLO: u8 = 0;
pub const C_POSITION_CHANGED: u8 = 1;
pub const C_CHUNK_SUB_REQUEST: u8 = 2;
pub const C_CHUNK_UNUBSCRIBE: u8 = 3;
pub const C_QUEUE_BLOCK: u8 = 4;
#[derive(Serialize, Deserialize, Clone)]
#[repr(u8)]
#[non_exhaustive]
pub enum ClientToServerMessage {
ClientHello {
username: String,
password: Option<String>,
} = ClientToServerMessageType::ClientHello as u8,
} = C_CLIENT_HELLO,
PositionChanged {
position: Vec3,
velocity: Vec3,
direction: Quat,
} = ClientToServerMessageType::PositionChanged as u8,
} = C_POSITION_CHANGED,
ChunkSubRequest {
chunk: IVec3,
} = ClientToServerMessageType::ChunkSubRequest as u8,
} = C_CHUNK_SUB_REQUEST,
ChunkUnsubscribe {
chunk: IVec3,
} = ClientToServerMessageType::ChunkUnsubscribe as u8,
} = C_CHUNK_UNUBSCRIBE,
QueueBlock {
item: QueuedBlock
} = ClientToServerMessageType::QueueBlock as u8,
} = C_QUEUE_BLOCK,
}
impl ToMessageType<ClientToServerMessageType> for ClientToServerMessage {
fn message_type(&self) -> ClientToServerMessageType {
match self {
ClientToServerMessage::ClientHello { .. } => ClientToServerMessageType::ClientHello,
ClientToServerMessage::PositionChanged { .. } => ClientToServerMessageType::PositionChanged,
ClientToServerMessage::ChunkSubRequest { .. } => ClientToServerMessageType::ChunkSubRequest,
ClientToServerMessage::ChunkUnsubscribe { .. } => ClientToServerMessageType::ChunkUnsubscribe,
ClientToServerMessage::QueueBlock { .. } => ClientToServerMessageType::QueueBlock,
}
}
}
#[repr(u8)]
#[non_exhaustive]
pub enum ServerToClientMessageType {
ServerHello = 0,
ServerFuckOff = 1,
PlayerPositionChanged = 2,
ChunkResponse = 3,
QueueBlock = 4,
PlayerConnected = 5,
PlayerDisconnected = 6,
}
pub const S_SERVER_HELLO: u8 = 0;
pub const S_SERVER_FUCK_OFF: u8 = 1;
pub const S_PLAYER_POSITION_CHANGED: u8 = 2;
pub const S_CHUNK_RESPONSE: u8 = 3;
pub const S_QUEUE_BLOCK: u8 = 4;
pub const S_PLAYER_CONNECTED: u8 = 5;
pub const S_PLAYER_DISCONNECTED: u8 = 6;
#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Clone)]
#[repr(u8)]
#[non_exhaustive]
pub enum ServerToClientMessage {
ServerHello {
init: InitData
} = ServerToClientMessageType::ServerHello as u8,
} = S_SERVER_HELLO,
ServerFuckOff {
reason: String,
} = ServerToClientMessageType::ServerFuckOff as u8,
} = S_SERVER_FUCK_OFF,
PlayerPositionChanged {
client_id: ClientId,
position: Vec3,
direction: Quat,
} = ServerToClientMessageType::PlayerPositionChanged as u8,
} = S_PLAYER_POSITION_CHANGED,
///## WARNING: THIS IS COMPRESSED
///MESSAGES OF THIS TYPE ARE FULLY
@ -95,33 +69,19 @@ pub enum ServerToClientMessage {
#[serde_as(as = "Box<[[[_; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]>")]
data: BlockData,
queued: Vec<QueuedBlock>,
} = ServerToClientMessageType::ChunkResponse as u8,
} = S_CHUNK_RESPONSE,
QueueBlock {
item: QueuedBlock
} = ServerToClientMessageType::QueueBlock as u8,
} = S_QUEUE_BLOCK,
PlayerConnected {
init: ClientInitData
} = ServerToClientMessageType::PlayerConnected as u8,
} = S_PLAYER_CONNECTED,
PlayerDisconnected {
id: ClientId
} = ServerToClientMessageType::PlayerDisconnected as u8,
}
impl ToMessageType<ServerToClientMessageType> for ServerToClientMessage {
fn message_type(&self) -> ServerToClientMessageType {
match self {
ServerToClientMessage::ServerHello { .. } => ServerToClientMessageType::ServerHello,
ServerToClientMessage::ServerFuckOff { .. } => ServerToClientMessageType::ServerFuckOff,
ServerToClientMessage::PlayerPositionChanged { .. } => ServerToClientMessageType::PlayerPositionChanged,
ServerToClientMessage::ChunkResponse { .. } => ServerToClientMessageType::ChunkResponse,
ServerToClientMessage::QueueBlock { .. } => ServerToClientMessageType::QueueBlock,
ServerToClientMessage::PlayerConnected { .. } => ServerToClientMessageType::PlayerConnected,
ServerToClientMessage::PlayerDisconnected { .. } => ServerToClientMessageType::PlayerDisconnected,
}
}
} = S_PLAYER_DISCONNECTED,
}
//---

View file

@ -29,7 +29,7 @@ pub(crate) mod delta_time;
pub(crate) mod cursor_lock;
pub(crate) mod control_flow;
pub(crate) mod state;
pub(crate) mod kubi_ui_integration;
pub(crate) mod guiv2_integration;
pub(crate) mod networking;
pub(crate) mod init;
pub(crate) mod color;
@ -76,7 +76,7 @@ use control_flow::{exit_on_esc, insert_control_flow_unique, RequestExit};
use state::{is_ingame, is_ingame_or_loading, is_loading, init_state, update_state, is_connecting};
use networking::{update_networking, update_networking_late, is_multiplayer, disconnect_on_exit, is_singleplayer};
use init::initialize_from_args;
use kubi_ui_integration::{kubi_ui_init, kubi_ui_begin, kubi_ui_end, kubi_ui_draw};
use guiv2_integration::{kubi_ui_init, kubi_ui_begin, kubi_ui_end, kubi_ui_draw};
use loading_screen::update_loading_screen;
use connecting_screen::switch_to_loading_if_connected;
use fixed_timestamp::init_fixed_timestamp_storage;

View file

@ -6,7 +6,7 @@ use crate::{
state::{GameState, NextState},
rendering::WindowSize,
input::RawKbmInputState,
kubi_ui_integration::UiState,
guiv2_integration::UiState,
};
fn render_progressbar(

View file

@ -1,9 +1,11 @@
use shipyard::{UniqueViewMut, View, IntoIter, AllStoragesViewMut};
use uflow::{client::Event as ClientEvent, SendMode};
use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
use kubi_shared::{
networking::{
messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO},
state::ClientJoinState,
channels::Channel,
channels::CHANNEL_AUTH,
},
};
use crate::player::{spawn_local_player_multiplayer, spawn_remote_player_multiplayer};
use super::{UdpClient, NetworkEvent};
@ -25,7 +27,7 @@ pub fn say_hello(
postcard::to_allocvec(
&ClientToServerMessage::ClientHello { username, password }
).unwrap().into_boxed_slice(),
Channel::Auth as usize,
CHANNEL_AUTH,
SendMode::Reliable
);
}
@ -38,7 +40,7 @@ pub fn check_server_hello_response(
let ClientEvent::Receive(data) = &event.0 else {
return None
};
if !event.is_message_of_type::<{ServerToClientMessageType::ServerHello as u8}>() {
if !event.is_message_of_type::<S_SERVER_HELLO>() {
return None
}
let Ok(parsed_message) = postcard::from_bytes(data) else {

View file

@ -4,8 +4,8 @@ use uflow::{SendMode, client::Event as ClientEvent};
use kubi_shared::{
transform::Transform,
networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
channels::Channel,
messages::{ClientToServerMessage, ServerToClientMessage, S_PLAYER_POSITION_CHANGED, S_PLAYER_CONNECTED},
channels::CHANNEL_MOVE,
client::ClientIdMap,
},
};
@ -35,7 +35,7 @@ pub fn send_player_movement_events(
velocity: Vec3::ZERO,
direction: *direction
}).unwrap().into_boxed_slice(),
Channel::Move as usize,
CHANNEL_MOVE,
SendMode::TimeSensitive
);
}
@ -51,7 +51,7 @@ pub fn receive_player_movement_events(
continue
};
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerPositionChanged as u8}>() {
if !event.is_message_of_type::<S_PLAYER_POSITION_CHANGED>() {
continue
}
@ -83,7 +83,7 @@ pub fn receive_player_connect_events(
let ClientEvent::Receive(data) = &event.0 else {
return None
};
if !event.is_message_of_type::<{ServerToClientMessageType::PlayerConnected as u8}>() {
if !event.is_message_of_type::<S_PLAYER_CONNECTED>() {
return None
};
let Ok(parsed_message) = postcard::from_bytes(data) else {

View file

@ -4,8 +4,8 @@ use lz4_flex::decompress_size_prepended;
use anyhow::{Result, Context};
use kubi_shared::{
networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
channels::Channel,
messages::{ClientToServerMessage, ServerToClientMessage, S_CHUNK_RESPONSE, S_QUEUE_BLOCK},
channels::CHANNEL_BLOCK,
},
queue::QueuedBlock
};
@ -31,7 +31,7 @@ pub fn inject_network_responses_into_manager_queue(
events: View<NetworkEvent>
) {
for event in events.iter() {
if event.is_message_of_type::<{ServerToClientMessageType::ChunkResponse as u8}>() {
if event.is_message_of_type::<S_CHUNK_RESPONSE>() {
let NetworkEvent(ClientEvent::Receive(data)) = &event else { unreachable!() };
let packet = decompress_chunk_packet(data).expect("Chunk decode failed");
let ServerToClientMessage::ChunkResponse {
@ -62,7 +62,7 @@ pub fn send_block_place_events(
soft: false
}
}).unwrap().into_boxed_slice(),
Channel::Block as usize,
CHANNEL_BLOCK,
SendMode::Reliable,
);
}
@ -76,7 +76,7 @@ pub fn recv_block_place_events(
let ClientEvent::Receive(data) = &event.0 else {
continue
};
if !event.is_message_of_type::<{ServerToClientMessageType::QueueBlock as u8}>() {
if !event.is_message_of_type::<S_QUEUE_BLOCK>() {
continue
}
let Ok(parsed_message) = postcard::from_bytes(data) else {

View file

@ -17,7 +17,7 @@ use super::{
primitives::cube::CubePrimitive,
};
const SMOL: f32 = 0.0025;
const SMOL: f32 = 0.0001;
pub fn render_selection_box(
lookat: View<LookingAtBlock>,