mirror of
https://github.com/italicsjenga/valence.git
synced 2025-01-11 15:21:31 +11:00
Use chunk state instead of HashSet
This commit is contained in:
parent
760f4bddcf
commit
d4d169d130
|
@ -1,4 +1,3 @@
|
||||||
use std::collections::HashSet;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
@ -53,7 +52,8 @@ const MAX_PLAYERS: usize = 10;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Config for Game {
|
impl Config for Game {
|
||||||
type ChunkState = ();
|
/// If the chunk should stay loaded at the end of the tick.
|
||||||
|
type ChunkState = bool;
|
||||||
type ClientState = EntityId;
|
type ClientState = EntityId;
|
||||||
type EntityState = ();
|
type EntityState = ();
|
||||||
type PlayerListState = ();
|
type PlayerListState = ();
|
||||||
|
@ -91,8 +91,6 @@ impl Config for Game {
|
||||||
fn update(&self, server: &mut Server<Self>) {
|
fn update(&self, server: &mut Server<Self>) {
|
||||||
let (world_id, world) = server.worlds.iter_mut().next().unwrap();
|
let (world_id, world) = server.worlds.iter_mut().next().unwrap();
|
||||||
|
|
||||||
let mut chunks_to_unload = HashSet::<_>::from_iter(world.chunks.iter().map(|t| t.0));
|
|
||||||
|
|
||||||
server.clients.retain(|_, client| {
|
server.clients.retain(|_, client| {
|
||||||
if client.created_this_tick() {
|
if client.created_this_tick() {
|
||||||
if self
|
if self
|
||||||
|
@ -155,19 +153,27 @@ impl Config for Game {
|
||||||
let p = client.position();
|
let p = client.position();
|
||||||
|
|
||||||
for pos in chunks_in_view_distance(ChunkPos::at(p.x, p.z), dist) {
|
for pos in chunks_in_view_distance(ChunkPos::at(p.x, p.z), dist) {
|
||||||
chunks_to_unload.remove(&pos);
|
if let Some(chunk) = world.chunks.get_mut(pos) {
|
||||||
if world.chunks.get(pos).is_none() {
|
chunk.state = true;
|
||||||
world.chunks.insert(pos, ());
|
} else {
|
||||||
|
world.chunks.insert(pos, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
});
|
});
|
||||||
|
|
||||||
for pos in chunks_to_unload {
|
// Remove chunks outside the view distance of players.
|
||||||
world.chunks.remove(pos);
|
world.chunks.retain(|_, chunk| {
|
||||||
}
|
if chunk.state {
|
||||||
|
chunk.state = false;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Generate chunk data for chunks created this tick.
|
||||||
world.chunks.par_iter_mut().for_each(|(pos, chunk)| {
|
world.chunks.par_iter_mut().for_each(|(pos, chunk)| {
|
||||||
if chunk.created_tick() != server.shared.current_tick() {
|
if chunk.created_tick() != server.shared.current_tick() {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -90,6 +90,10 @@ impl<C: Config> Chunks<C> {
|
||||||
self.chunks.get_mut(&pos.into())
|
self.chunks.get_mut(&pos.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn retain(&mut self, mut f: impl FnMut(ChunkPos, &mut Chunk<C>) -> bool) {
|
||||||
|
self.chunks.retain(|&pos, chunk| f(pos, chunk))
|
||||||
|
}
|
||||||
|
|
||||||
/// Deletes all chunks.
|
/// Deletes all chunks.
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.chunks.clear();
|
self.chunks.clear();
|
||||||
|
|
Loading…
Reference in a new issue