Skip to content

Commit

Permalink
preheat chunks on server
Browse files Browse the repository at this point in the history
  • Loading branch information
griffi-gh committed May 3, 2024
1 parent dac3c10 commit 11ad2cd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 42 deletions.
1 change: 1 addition & 0 deletions Server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ timeout_ms = 10000

[world]
seed = 0xfeb_face_dead_cafe
preheat_radius = 8

[query]
name = "Kubi Server"
75 changes: 38 additions & 37 deletions kubi-server/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
use shipyard::{AllStoragesView, Unique};
use serde::{Serialize, Deserialize};
use std::{fs, net::SocketAddr};

#[derive(Serialize, Deserialize)]
pub struct ConfigTableServer {
pub address: SocketAddr,
pub max_clients: usize,
pub timeout_ms: u64,
pub password: Option<String>,
}

#[derive(Serialize, Deserialize)]
pub struct ConfigTableWorld {
pub seed: u64,
}

#[derive(Serialize, Deserialize)]
pub struct ConfigTableQuery {
pub name: Option<String>
}

#[derive(Unique, Serialize, Deserialize)]
pub struct ConfigTable {
pub server: ConfigTableServer,
pub world: ConfigTableWorld,
pub query: ConfigTableQuery,
}

pub fn read_config(
storages: AllStoragesView,
) {
log::info!("Reading config...");
let config_str = fs::read_to_string("Server.toml").expect("No config file found");
let config: ConfigTable = toml::from_str(&config_str).expect("Invalid configuration file");
storages.add_unique(config);
}
use shipyard::{AllStoragesView, Unique};
use serde::{Serialize, Deserialize};
use std::{fs, net::SocketAddr};

#[derive(Serialize, Deserialize)]
pub struct ConfigTableServer {
pub address: SocketAddr,
pub max_clients: usize,
pub timeout_ms: u64,
pub password: Option<String>,
}

#[derive(Serialize, Deserialize)]
pub struct ConfigTableWorld {
pub seed: u64,
pub preheat_radius: u32,
}

#[derive(Serialize, Deserialize)]
pub struct ConfigTableQuery {
pub name: Option<String>
}

#[derive(Unique, Serialize, Deserialize)]
pub struct ConfigTable {
pub server: ConfigTableServer,
pub world: ConfigTableWorld,
pub query: ConfigTableQuery,
}

pub fn read_config(
storages: AllStoragesView,
) {
log::info!("Reading config...");
let config_str = fs::read_to_string("Server.toml").expect("No config file found");
let config: ConfigTable = toml::from_str(&config_str).expect("Invalid configuration file");
storages.add_unique(config);
}
4 changes: 2 additions & 2 deletions kubi-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use shipyard::{World, Workload, IntoWorkload};
use shipyard::{IntoWorkload, Workload, WorkloadModificator, World};
use std::{thread, time::Duration};

mod util;
Expand All @@ -19,7 +19,7 @@ fn initialize() -> Workload {
read_config,
bind_server,
init_client_maps,
init_world,
init_world.after_all(read_config),
).into_workload()
}

Expand Down
29 changes: 26 additions & 3 deletions kubi-server/src/world.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use shipyard::{Unique, UniqueView, UniqueViewMut, Workload, IntoWorkload, AllStoragesView, View, Get, NonSendSync, IntoIter};
use shipyard::{AllStoragesView, Get, IntoIter, IntoWorkload, NonSendSync, SystemModificator, Unique, UniqueView, UniqueViewMut, View, Workload};
use glam::IVec3;
use hashbrown::HashMap;
use kubi_shared::{
Expand Down Expand Up @@ -265,10 +265,33 @@ fn init_chunk_manager_and_block_queue(
storages.add_unique(LocalBlockQueue::default());
}

pub fn preheat_world(
mut chunk_manager: UniqueViewMut<ChunkManager>,
task_manager: UniqueView<ChunkTaskManager>,
config: UniqueView<ConfigTable>,
) {
let r = config.world.preheat_radius as i32;
for x in -r..=r {
for y in -r..=r {
for z in -r..=r {
let chunk_position = IVec3::new(x, y, z);
let mut chunk = Chunk::new();
chunk.state = ChunkState::Loading;
chunk_manager.chunks.insert(chunk_position, chunk);
task_manager.spawn_task(ChunkTask::LoadChunk {
position: chunk_position,
seed: config.world.seed,
});
}
}
}
}

pub fn init_world() -> Workload {
(
init_chunk_manager_and_block_queue,
init_chunk_task_manager,
init_chunk_manager_and_block_queue.before_all(preheat_world),
init_chunk_task_manager.before_all(preheat_world),
preheat_world,
).into_workload()
}

Expand Down

0 comments on commit 11ad2cd

Please sign in to comment.