Client-side migrated

This commit is contained in:
griffi-gh 2023-03-08 01:31:38 +01:00
parent 9d122961b5
commit a07aad39ca
17 changed files with 257 additions and 155 deletions

View file

@ -1,6 +1,8 @@
{ {
"editor.tabSize": 2, "editor.tabSize": 2,
"rust-analyzer.diagnostics.disabled": [ "rust-analyzer.diagnostics.disabled": [
"unresolved-method" //rust-analyzer issue #14269 //rust-analyzer issue #14269,
"unresolved-method",
"unresolved-import"
] ]
} }

115
Cargo.lock generated
View file

@ -66,6 +66,15 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "atomic-polyfill"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3ff7eb3f316534d83a8a2c3d1674ace8a5a71198eba31e2e2b597833f699b28"
dependencies = [
"critical-section",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -87,25 +96,6 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "bincode"
version = "2.0.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bb50c5a2ef4b9b1e7ae73e3a73b52ea24b20312d629f9c4df28260b7ad2c3c4"
dependencies = [
"bincode_derive",
"serde",
]
[[package]]
name = "bincode_derive"
version = "2.0.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a45a23389446d2dd25dc8e73a7a3b3c43522b630cac068927f0649d43d719d2"
dependencies = [
"virtue",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -203,6 +193,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "cobs"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
[[package]] [[package]]
name = "cocoa" name = "cocoa"
version = "0.24.1" version = "0.24.1"
@ -302,6 +298,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "critical-section"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52"
[[package]] [[package]]
name = "crossbeam-channel" name = "crossbeam-channel"
version = "0.5.7" version = "0.5.7"
@ -753,6 +755,15 @@ dependencies = [
"gl_generator", "gl_generator",
] ]
[[package]]
name = "hash32"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
@ -771,6 +782,20 @@ dependencies = [
"ahash 0.8.3", "ahash 0.8.3",
] ]
[[package]]
name = "heapless"
version = "0.7.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db04bc24a18b9ea980628ecf00e6c0264f3c1426dac36c00cb49b6fbad8b0743"
dependencies = [
"atomic-polyfill",
"hash32",
"rustc_version",
"serde",
"spin",
"stable_deref_trait",
]
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.4.1" version = "0.4.1"
@ -908,9 +933,11 @@ dependencies = [
"kubi-shared", "kubi-shared",
"log", "log",
"nohash-hasher", "nohash-hasher",
"postcard",
"rayon", "rayon",
"shipyard", "shipyard",
"strum", "strum",
"uflow",
"winapi", "winapi",
] ]
@ -945,11 +972,12 @@ name = "kubi-shared"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bincode",
"bracket-noise", "bracket-noise",
"glam", "glam",
"postcard",
"rand", "rand",
"rand_xoshiro", "rand_xoshiro",
"serde",
"shipyard", "shipyard",
"strum", "strum",
] ]
@ -1374,6 +1402,17 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
] ]
[[package]]
name = "postcard"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfa512cd0d087cc9f99ad30a1bf64795b67871edbead083ffc3a4dfafa59aa00"
dependencies = [
"cobs",
"heapless",
"serde",
]
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.17" version = "0.2.17"
@ -1529,6 +1568,15 @@ version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.36.9" version = "0.36.9"
@ -1593,6 +1641,12 @@ dependencies = [
"tiny-skia", "tiny-skia",
] ]
[[package]]
name = "semver"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58bc9567378fc7690d6b2addae4e60ac2eeea07becb2c64b9f218b53865cba2a"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.152" version = "1.0.152"
@ -1717,6 +1771,12 @@ dependencies = [
"lock_api", "lock_api",
] ]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"
@ -1850,6 +1910,15 @@ dependencies = [
"winnow", "winnow",
] ]
[[package]]
name = "uflow"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3be4d71c1c106a57b0333ac2c28bd4521e0b16a2b98fe84405cdf7f544be46b6"
dependencies = [
"rand",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.8" version = "1.0.8"
@ -1874,12 +1943,6 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "virtue"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b60dcd6a64dd45abf9bd426970c9843726da7fc08f44cd6fcebf68c21220a63"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View file

@ -1,5 +1,4 @@
use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut}; use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut};
use kubi_udp::server::{Server, ServerConfig, ServerEvent};
use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage}; use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage};
use std::time::Duration; use std::time::Duration;
use crate::config::ConfigTable; use crate::config::ConfigTable;

View file

@ -86,7 +86,7 @@ fn process_finished_tasks(
server.0.send_message(subscriber, ServerToClientMessage::ChunkResponse { server.0.send_message(subscriber, ServerToClientMessage::ChunkResponse {
chunk: chunk_position.to_array(), chunk: chunk_position.to_array(),
data: blocks.clone(), data: blocks.clone(),
queued: queue.iter().map(|item| (item.position.to_array(), item.block_type)).collect() queued: queue
}).map_err(log_error).ok(); }).map_err(log_error).ok();
} }
log::debug!("Chunk {chunk_position} loaded, {} subs", chunk.subscriptions.len()); log::debug!("Chunk {chunk_position} loaded, {} subs", chunk.subscriptions.len());

View file

@ -5,7 +5,8 @@ use rayon::{ThreadPool, ThreadPoolBuilder};
use anyhow::Result; use anyhow::Result;
use kubi_shared::{ use kubi_shared::{
chunk::BlockData, chunk::BlockData,
worldgen::{QueuedBlock, generate_world} worldgen::generate_world,
queue::QueuedBlock,
}; };
pub enum ChunkTask { pub enum ChunkTask {

View file

@ -10,7 +10,8 @@ publish = false
glam = { version = "0.23", features = ["debug-glam-assert", "fast-math", "serde"] } glam = { version = "0.23", features = ["debug-glam-assert", "fast-math", "serde"] }
shipyard = { git = "https://github.com/leudz/shipyard", rev = "eb189f66" } shipyard = { git = "https://github.com/leudz/shipyard", rev = "eb189f66" }
strum = { version = "0.24", features = ["derive"] } strum = { version = "0.24", features = ["derive"] }
bincode = "2.0.0-rc" postcard = { version = "1.0", features = ["alloc"] }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }
anyhow = "1.0" anyhow = "1.0"
bracket-noise = "0.8" bracket-noise = "0.8"
rand = { version = "0.8", default_features = false, features = ["std", "min_const_gen"] } rand = { version = "0.8", default_features = false, features = ["std", "min_const_gen"] }
@ -18,4 +19,4 @@ rand_xoshiro = "0.6"
[features] [features]
default = [] default = []
nightly = ["rand/nightly", "rand/simd_support"] nightly = ["rand/nightly", "rand/simd_support", "serde/unstable"]

View file

@ -1,7 +1,7 @@
use bincode::{Encode, Decode}; use serde::{Serialize, Deserialize};
use strum::EnumIter; use strum::EnumIter;
#[derive(Clone, Copy, Debug, EnumIter)] #[derive(Serialize, Deserialize, Clone, Copy, Debug, EnumIter)]
#[repr(u8)] #[repr(u8)]
pub enum BlockTexture { pub enum BlockTexture {
Stone, Stone,
@ -22,7 +22,7 @@ pub enum BlockTexture {
WaterSolid, WaterSolid,
} }
#[derive(Encode, Decode, Clone, Copy, Debug, PartialEq, Eq, EnumIter)] #[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, EnumIter)]
#[repr(u8)] #[repr(u8)]
pub enum Block { pub enum Block {
Air, Air,

View file

@ -4,3 +4,4 @@ pub mod worldgen;
pub mod chunk; pub mod chunk;
pub mod transform; pub mod transform;
pub mod entity; pub mod entity;
pub mod queue;

View file

@ -1,30 +1,63 @@
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
use bincode::{Encode, Decode}; use serde::{Serialize, Deserialize};
use crate::{chunk::BlockData, block::Block}; use crate::{chunk::BlockData, queue::QueuedBlock};
type IVec3Arr = [i32; 3]; pub type IVec3Arr = [i32; 3];
type Vec3Arr = [f32; 3]; pub type Vec3Arr = [f32; 3];
type QuatArr = [f32; 3]; pub type QuatArr = [f32; 3];
pub const PROTOCOL_ID: u16 = 1; pub const PROTOCOL_ID: u16 = 2;
#[derive(Encode, Decode, Clone)] pub const C_CLIENT_HELLO: u8 = 0;
pub const C_POSITION_CHANGED: u8 = 1;
pub const C_CHUNK_SUB_REQUEST: u8 = 2;
#[derive(Serialize, Deserialize, Clone)]
#[repr(u8)]
pub enum ClientToServerMessage { pub enum ClientToServerMessage {
ClientHello { ClientHello {
username: String, username: String,
password: Option<String>, password: Option<String>,
}, } = C_CLIENT_HELLO,
PositionChanged { PositionChanged {
position: Vec3Arr, position: Vec3Arr,
velocity: Vec3Arr, velocity: Vec3Arr,
direction: QuatArr, direction: QuatArr,
}, } = C_POSITION_CHANGED,
ChunkSubRequest { ChunkSubRequest {
chunk: IVec3Arr, chunk: IVec3Arr,
}, } = C_CHUNK_SUB_REQUEST,
} }
#[derive(Encode, Decode, Clone)] 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;
#[derive(Serialize, Deserialize, Clone)]
#[repr(u8)]
pub enum ServerToClientMessage {
ServerHello {
init: InitData
} = S_SERVER_HELLO,
ServerFuckOff {
reason: String,
} = S_SERVER_FUCK_OFF,
PlayerPositionChanged {
client_id: u8,
position: Vec3Arr,
direction: QuatArr,
} = S_PLAYER_POSITION_CHANGED,
ChunkResponse {
chunk: IVec3Arr,
data: BlockData,
queued: Vec<QueuedBlock>,
} = S_CHUNK_RESPONSE,
}
//---
#[derive(Serialize, Deserialize, Clone)]
pub struct UserInitData { pub struct UserInitData {
pub client_id: NonZeroUsize, //maybe use the proper type instead pub client_id: NonZeroUsize, //maybe use the proper type instead
pub username: String, pub username: String,
@ -33,27 +66,7 @@ pub struct UserInitData {
pub direction: QuatArr, pub direction: QuatArr,
} }
#[derive(Encode, Decode, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct InitData { pub struct InitData {
pub users: Vec<UserInitData> pub users: Vec<UserInitData>
} }
#[derive(Encode, Decode, Clone)]
pub enum ServerToClientMessage {
ServerHello {
init: InitData
},
ServerFuckOff {
reason: String,
},
PlayerPositionChanged {
client_id: u8,
position: Vec3Arr,
direction: QuatArr,
},
ChunkResponse {
chunk: IVec3Arr,
data: BlockData,
queued: Vec<(IVec3Arr, Block)>,
}
}

11
kubi-shared/src/queue.rs Normal file
View file

@ -0,0 +1,11 @@
use glam::IVec3;
use serde::{Serialize, Deserialize};
use crate::block::Block;
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub struct QueuedBlock {
pub position: IVec3,
pub block_type: Block,
/// Only replace air blocks
pub soft: bool,
}

View file

@ -4,7 +4,8 @@ use glam::{IVec3, ivec3, Vec3Swizzles, IVec2};
use rand_xoshiro::Xoshiro256StarStar; use rand_xoshiro::Xoshiro256StarStar;
use crate::{ use crate::{
chunk::{BlockData, CHUNK_SIZE}, chunk::{BlockData, CHUNK_SIZE},
block::Block block::Block,
queue::QueuedBlock,
}; };
fn mountain_ramp(mut x: f32) -> f32 { fn mountain_ramp(mut x: f32) -> f32 {
@ -29,10 +30,6 @@ fn local_y_position(height: i32, chunk_position: IVec3) -> Option<usize> {
(0..CHUNK_SIZE as i32).contains(&position).then_some(position as usize) (0..CHUNK_SIZE as i32).contains(&position).then_some(position as usize)
} }
pub struct QueuedBlock {
pub position: IVec3,
pub block_type: Block,
}
pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<QueuedBlock>) { pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<QueuedBlock>) {
let offset = chunk_position * CHUNK_SIZE as i32; let offset = chunk_position * CHUNK_SIZE as i32;
@ -47,7 +44,8 @@ pub fn generate_world(chunk_position: IVec3, seed: u64) -> (BlockData, Vec<Queue
}); });
queue.push(QueuedBlock { queue.push(QueuedBlock {
position: event_pos, position: event_pos,
block_type: block block_type: block,
soft: true
}); });
} else { } else {
blocks[position.x as usize][position.y as usize][position.z as usize] = block; blocks[position.x as usize][position.y as usize][position.z as usize] = block;

View file

@ -19,6 +19,8 @@ nohash-hasher = "0.2.0"
anyhow = "1.0" anyhow = "1.0"
flume = "0.10" flume = "0.10"
gilrs = { version = "0.10", default_features = false, features = ["xinput"] } gilrs = { version = "0.10", default_features = false, features = ["xinput"] }
uflow = "0.7"
postcard = { version = "1.0", features = ["alloc"] }
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3" } winapi = { version = "0.3" }

View file

@ -1,9 +1,12 @@
use shipyard::{UniqueViewMut, UniqueView, View, IntoIter, ViewMut, EntitiesViewMut, Component, Workload, IntoWorkload}; use shipyard::{UniqueViewMut, UniqueView, View, IntoIter, ViewMut, EntitiesViewMut, Component, Workload, IntoWorkload};
use glium::glutin::event::VirtualKeyCode; use glium::glutin::event::VirtualKeyCode;
use kubi_shared::block::Block; use kubi_shared::{
block::Block,
queue::QueuedBlock,
};
use crate::{ use crate::{
player::MainPlayer, player::MainPlayer,
world::{raycast::{LookingAtBlock, RAYCAST_STEP}, queue::{BlockUpdateQueue, BlockUpdateEvent}}, world::{raycast::{LookingAtBlock, RAYCAST_STEP}, queue::BlockUpdateQueue},
input::{Inputs, PrevInputs, RawKbmInputState}, input::{Inputs, PrevInputs, RawKbmInputState},
events::{EventComponent, player_actions::PlayerActionEvent}, events::{EventComponent, player_actions::PlayerActionEvent},
}; };
@ -67,9 +70,9 @@ fn block_placement_system(
(ray.block_position, Block::Air) (ray.block_position, Block::Air)
}; };
//queue place //queue place
block_event_queue.push(BlockUpdateEvent { block_event_queue.push(QueuedBlock {
position: place_position, position: place_position,
value: place_block, block_type: place_block,
soft: place_block != Block::Air, soft: place_block != Block::Air,
}); });
//send event //send event

View file

@ -1,6 +1,7 @@
use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator}; use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator};
use glium::glutin::event_loop::ControlFlow; use glium::glutin::event_loop::ControlFlow;
use std::net::SocketAddr; use std::net::SocketAddr;
use uflow::client::{Client, Config as ClientConfig, Event as ClientEvent};
use kubi_shared::networking::{ use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage}, messages::{ClientToServerMessage, ServerToClientMessage},
state::ClientJoinState state::ClientJoinState
@ -17,43 +18,34 @@ pub enum GameType {
pub struct ServerAddress(pub SocketAddr); pub struct ServerAddress(pub SocketAddr);
#[derive(Unique)] #[derive(Unique)]
pub struct UdpClient(pub Client<ClientToServerMessage, ServerToClientMessage>); pub struct UdpClient(pub Client);
#[derive(Component)] #[derive(Component)]
pub struct NetworkEvent(pub ClientEvent<ServerToClientMessage>); pub struct NetworkEvent(pub ClientEvent);
fn create_client( impl NetworkEvent {
///Checks if postcard-encoded message has a type
pub fn is_message_of_type<const T: u8>(&self) -> bool {
let ClientEvent::Receive(data) = &self.0 else { return false };
if data.len() == 0 { return false }
data[0] == T
}
}
#[derive(Component)]
pub struct NetworkMessageEvent(pub ServerToClientMessage);
fn connect_client(
storages: AllStoragesView storages: AllStoragesView
) { ) {
log::info!("Creating client"); log::info!("Creating client");
let address = storages.borrow::<UniqueView<ServerAddress>>().unwrap(); let address = storages.borrow::<UniqueView<ServerAddress>>().unwrap();
storages.add_unique(UdpClient(Client::new( let client = Client::connect(address.0, ClientConfig::default()).expect("Client connection failed");
address.0, storages.add_unique(UdpClient(client));
ClientConfig::default()
).unwrap()));
storages.add_unique(ClientJoinState::Disconnected); storages.add_unique(ClientJoinState::Disconnected);
} }
fn connect_client( fn poll_client(
mut client: UniqueViewMut<UdpClient>
) {
log::info!("Connect called");
client.0.connect().unwrap();
}
fn should_connect(
client: UniqueView<UdpClient>
) -> bool {
client.0.has_not_made_connection_attempts()
}
fn update_client(
mut client: UniqueViewMut<UdpClient>,
) {
client.0.update().unwrap();
}
fn insert_client_events(
mut client: UniqueViewMut<UdpClient>, mut client: UniqueViewMut<UdpClient>,
mut entities: EntitiesViewMut, mut entities: EntitiesViewMut,
mut events: ViewMut<EventComponent>, mut events: ViewMut<EventComponent>,
@ -62,7 +54,7 @@ fn insert_client_events(
entities.bulk_add_entity(( entities.bulk_add_entity((
&mut events, &mut events,
&mut network_events, &mut network_events,
), client.0.process_events().map(|event| { ), client.0.step().map(|event| {
(EventComponent, NetworkEvent(event)) (EventComponent, NetworkEvent(event))
})); }));
} }
@ -75,13 +67,19 @@ fn set_client_join_state_to_connected(
} }
fn say_hello( fn say_hello(
client: UniqueViewMut<UdpClient>, mut client: UniqueViewMut<UdpClient>,
) { ) {
log::info!("Authenticating"); log::info!("Authenticating");
client.0.send_message(ClientToServerMessage::ClientHello { client.0.send(
username: "Sbeve".into(), postcard::to_allocvec(
password: None &ClientToServerMessage::ClientHello {
}).unwrap(); username: "Sbeve".into(),
password: None
}
).unwrap().into_boxed_slice(),
0,
uflow::SendMode::Reliable
);
} }
fn check_server_hello_response( fn check_server_hello_response(
@ -89,20 +87,20 @@ fn check_server_hello_response(
mut join_state: UniqueViewMut<ClientJoinState> mut join_state: UniqueViewMut<ClientJoinState>
) { ) {
for event in network_events.iter() { for event in network_events.iter() {
if let ClientEvent::MessageReceived(ServerToClientMessage::ServerHello { init }) = &event.0 { if let ClientEvent::Receive(data) = &event.0 {
log::info!("Joined the server!"); log::info!("Joined the server!");
//TODO handle init data //TODO handle init data
*join_state = ClientJoinState::Joined; *join_state = ClientJoinState::Joined;
return;
} }
} }
} }
pub fn update_networking() -> Workload { pub fn update_networking() -> Workload {
( (
create_client.run_if_missing_unique::<UdpClient>(), connect_client.run_if_missing_unique::<UdpClient>(),
connect_client.run_if(should_connect), poll_client,
update_client,
insert_client_events,
( (
set_client_join_state_to_connected, set_client_join_state_to_connected,
say_hello, say_hello,
@ -118,12 +116,19 @@ pub fn disconnect_on_exit(
mut client: UniqueViewMut<UdpClient>, mut client: UniqueViewMut<UdpClient>,
) { ) {
if let Some(ControlFlow::ExitWithCode(_)) = control_flow.0 { if let Some(ControlFlow::ExitWithCode(_)) = control_flow.0 {
client.0.set_nonblocking(false).expect("Failed to switch socket to blocking mode"); if client.0.is_active() {
if let Err(error) = client.0.disconnect() { client.0.flush();
log::error!("failed to disconnect: {}", error); client.0.disconnect();
} else { while client.0.is_active() { client.0.step(); }
log::info!("Client disconnected"); log::info!("Client disconnected");
} else {
log::info!("Client inactive")
} }
// if let Err(error) = client.0. {
// log::error!("failed to disconnect: {}", error);
// } else {
// log::info!("Client disconnected");
// }
} }
} }
@ -132,7 +137,7 @@ pub fn disconnect_on_exit(
fn if_just_connected( fn if_just_connected(
network_events: View<NetworkEvent>, network_events: View<NetworkEvent>,
) -> bool { ) -> bool {
network_events.iter().any(|event| matches!(&event.0, ClientEvent::Connected(_))) network_events.iter().any(|event| matches!(&event.0, ClientEvent::Connect))
} }
fn is_join_state<const STATE: u8>( fn is_join_state<const STATE: u8>(

View file

@ -2,6 +2,8 @@ use glam::{IVec3, ivec3};
use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType};
use kubi_shared::networking::messages::ClientToServerMessage; use kubi_shared::networking::messages::ClientToServerMessage;
use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload, NonSendSync, track}; use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload, NonSendSync, track};
use kubi_shared::queue::QueuedBlock;
use uflow::SendMode;
use crate::{ use crate::{
player::MainPlayer, player::MainPlayer,
transform::Transform, transform::Transform,
@ -14,7 +16,7 @@ use super::{
ChunkStorage, ChunkMeshStorage, ChunkStorage, ChunkMeshStorage,
chunk::{Chunk, DesiredChunkState, CHUNK_SIZE, ChunkMesh, CurrentChunkState, ChunkData}, chunk::{Chunk, DesiredChunkState, CHUNK_SIZE, ChunkMesh, CurrentChunkState, ChunkData},
tasks::{ChunkTaskManager, ChunkTaskResponse, ChunkTask}, tasks::{ChunkTaskManager, ChunkTaskResponse, ChunkTask},
queue::{BlockUpdateQueue, BlockUpdateEvent}, queue::BlockUpdateQueue
}; };
const MAX_CHUNK_OPS_INGAME: usize = 6; const MAX_CHUNK_OPS_INGAME: usize = 6;
@ -120,7 +122,7 @@ fn unload_downgrade_chunks(
fn start_required_tasks( fn start_required_tasks(
task_manager: UniqueView<ChunkTaskManager>, task_manager: UniqueView<ChunkTaskManager>,
udp_client: Option<UniqueView<UdpClient>>, mut udp_client: Option<UniqueViewMut<UdpClient>>,
mut world: UniqueViewMut<ChunkStorage>, mut world: UniqueViewMut<ChunkStorage>,
) { ) {
if !world.is_modified() { if !world.is_modified() {
@ -133,10 +135,14 @@ fn start_required_tasks(
match chunk.desired_state { match chunk.desired_state {
DesiredChunkState::Loaded | DesiredChunkState::Rendered if chunk.current_state == CurrentChunkState::Nothing => { DesiredChunkState::Loaded | DesiredChunkState::Rendered if chunk.current_state == CurrentChunkState::Nothing => {
//start load task //start load task
if let Some(client) = &udp_client { if let Some(client) = &mut udp_client {
client.0.send_message(ClientToServerMessage::ChunkSubRequest { client.0.send(
chunk: position.to_array() postcard::to_allocvec(&ClientToServerMessage::ChunkSubRequest {
}).unwrap(); chunk: position.to_array()
}).unwrap().into_boxed_slice(),
0,
SendMode::Reliable
);
} else { } else {
task_manager.spawn_task(ChunkTask::LoadChunk { task_manager.spawn_task(ChunkTask::LoadChunk {
seed: 0xbeef_face_dead_cafe, seed: 0xbeef_face_dead_cafe,
@ -208,12 +214,9 @@ fn process_completed_tasks(
chunk.current_state = CurrentChunkState::Loaded; chunk.current_state = CurrentChunkState::Loaded;
//push queued blocks //push queued blocks
for event in queued { //TODO use extend
queue.push(BlockUpdateEvent { for item in queued {
position: event.position, queue.push(item);
value: event.block_type,
soft: true,
});
} }
//increase ops counter //increase ops counter

View file

@ -1,26 +1,17 @@
use glam::{IVec3, ivec3}; use glam::{IVec3, ivec3};
use kubi_shared::{block::Block, chunk::CHUNK_SIZE}; use kubi_shared::{block::Block, chunk::CHUNK_SIZE, queue::QueuedBlock};
use shipyard::{UniqueViewMut, Unique}; use shipyard::{UniqueViewMut, Unique};
use super::ChunkStorage; use super::ChunkStorage;
#[derive(Clone, Copy, Debug)]
pub struct BlockUpdateEvent {
pub position: IVec3,
pub value: Block,
//Only replace air blocks
pub soft: bool,
}
#[derive(Unique, Default, Clone)] #[derive(Unique, Default, Clone)]
pub struct BlockUpdateQueue { pub struct BlockUpdateQueue {
queue: Vec<BlockUpdateEvent> queue: Vec<QueuedBlock>
} }
impl BlockUpdateQueue { impl BlockUpdateQueue {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
pub fn push(&mut self, event: BlockUpdateEvent) { pub fn push(&mut self, event: QueuedBlock) {
self.queue.push(event) self.queue.push(event)
} }
} }
@ -35,7 +26,7 @@ pub fn apply_queued_blocks(
if event.soft && *block != Block::Air { if event.soft && *block != Block::Air {
return false return false
} }
*block = event.value; *block = event.block_type;
//mark chunk as dirty //mark chunk as dirty
let (chunk_pos, block_pos) = ChunkStorage::to_chunk_coords(event.position); let (chunk_pos, block_pos) = ChunkStorage::to_chunk_coords(event.position);
let chunk = world.chunks.get_mut(&chunk_pos).expect("This error should never happen, if it does then something is super fucked up and the whole project needs to be burnt down."); let chunk = world.chunks.get_mut(&chunk_pos).expect("This error should never happen, if it does then something is super fucked up and the whole project needs to be burnt down.");

View file

@ -1,11 +1,12 @@
use flume::{Sender, Receiver}; use flume::{Sender, Receiver};
use glam::IVec3; use glam::IVec3;
use kubi_shared::{ use kubi_shared::{
networking::messages::ServerToClientMessage, networking::messages::{S_CHUNK_RESPONSE, ServerToClientMessage},
worldgen::QueuedBlock queue::QueuedBlock
}; };
use shipyard::{Unique, UniqueView, View, IntoIter}; use shipyard::{Unique, UniqueView, View, IntoIter};
use rayon::{ThreadPool, ThreadPoolBuilder}; use rayon::{ThreadPool, ThreadPoolBuilder};
use uflow::client::Event as ClientEvent;
use super::{ use super::{
chunk::BlockData, chunk::BlockData,
mesh::{generate_mesh, data::MeshGenData}, mesh::{generate_mesh, data::MeshGenData},
@ -82,16 +83,24 @@ pub fn inject_network_responses_into_manager_queue(
events: View<NetworkEvent> events: View<NetworkEvent>
) { ) {
for event in events.iter() { for event in events.iter() {
if let ClientEvent::MessageReceived(ServerToClientMessage::ChunkResponse { chunk, data, queued }) = &event.0 { if event.is_message_of_type::<S_CHUNK_RESPONSE>() {
let position = IVec3::from_array(*chunk); let NetworkEvent(ClientEvent::Receive(data)) = &event else { unreachable!() };
let ServerToClientMessage::ChunkResponse {
chunk, data, queued
} = postcard::from_bytes(data).expect("Chunk decode failed") else { unreachable!() };
manager.add_sussy_response(ChunkTaskResponse::LoadedChunk { manager.add_sussy_response(ChunkTaskResponse::LoadedChunk {
position, position: IVec3::from_array(chunk),
chunk_data: data.clone(), chunk_data: data,
queued: queued.iter().map(|&(position, block_type)| QueuedBlock { queued
position: IVec3::from_array(position),
block_type
}).collect()
}); });
} }
// if let ClientEvent::MessageReceived(ServerToClientMessage::ChunkResponse { &chunk, data, queued }) = &event.0 {
// let position = IVec3::from_array(chunk);
// manager.add_sussy_response(ChunkTaskResponse::LoadedChunk {
// position,
// chunk_data: data.clone(),
// queued
// });
// }
} }
} }