Skip to content

Commit

Permalink
Made tick handling more efficient and faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweattypalms committed Oct 26, 2024
1 parent 7f68ca4 commit 50b64e9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ ctor = { workspace = true }
parking_lot = { workspace = true }
tracing = { workspace = true }
tokio = { workspace = true }
rayon = { workspace = true }
futures = { workspace = true }
serde_json = { workspace = true }
async-trait = "0.1.83"


[[bin]]
name = "ferrumc"
path = "src/main.rs"
2 changes: 2 additions & 0 deletions src/bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Security or something like that
#![forbid(unsafe_code)]

#![feature(slice_as_chunks)]

use ferrumc_ecs::Universe;
use ferrumc_net::ServerState;
use std::sync::{Arc};
Expand Down
47 changes: 35 additions & 12 deletions src/bin/src/packet_handlers/tick_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use ferrumc_net::errors::NetError;
use ferrumc_net::packets::outgoing::update_time::TickEvent;
use ferrumc_net::packets::outgoing::update_time::UpdateTimePacket;
use ferrumc_net::GlobalState;
use ferrumc_net_codec::encode::NetEncodeOpts;
use ferrumc_net_codec::encode::{NetEncode, NetEncodeOpts};
use futures::StreamExt;
use tracing::error;

#[event_handler]
Expand All @@ -17,21 +18,43 @@ async fn handle_tick(event: TickEvent, state: GlobalState) -> Result<TickEvent,
///////

let packet = UpdateTimePacket::new(event.tick, event.tick % 24000);
let packet = {
let mut buffer = Vec::new();
packet.encode(&mut buffer, &NetEncodeOpts::WithLength)?;
buffer
};

let query = state
.universe
.query::<(&mut StreamWriter, &ConnectionState)>();

for (mut writer, connection_state) in query {
if let ConnectionState::Play = *connection_state {
if let Err(e) = writer
.send_packet(&packet, &NetEncodeOpts::WithLength)
.await
{
error!("Error sending update_time packet: {}", e);
.query::<(&mut StreamWriter, &ConnectionState)>()
.into_entities()
.into_iter()
.filter_map(|entity| {
let conn_state = state.universe.get::<ConnectionState>(entity).ok()?;
if matches!(*conn_state, ConnectionState::Play) {
Some(entity)
} else {
None
}
}
}
})
.collect::<Vec<_>>();

tokio::spawn(
futures::stream::iter(query.into_iter())
.fold((state, packet), move |(state, packet), entity| {
async move {
if let Ok(mut writer) = state.universe.get_mut::<StreamWriter>(entity) {
if let Err(e) = writer
.send_packet(&packet.as_slice(), &NetEncodeOpts::None)
.await
{
error!("Error sending update_time packet: {}", e);
}
}

(state, packet)
}
})
);
Ok(event)
}
8 changes: 8 additions & 0 deletions src/lib/ecs/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ impl<'a, Q: QueryItem> Query<'a, Q> {
_marker: std::marker::PhantomData,
}
}

pub fn entities(&self) -> &[Entity] {
&self.entities
}

pub fn into_entities(self) -> Vec<Entity> {
self.entities
}
}


Expand Down

0 comments on commit 50b64e9

Please sign in to comment.