From a78ad8de79a091f9806c4215843216c55e34f2d4 Mon Sep 17 00:00:00 2001 From: crabman Date: Sat, 2 Mar 2024 19:54:51 +0000 Subject: [PATCH] revive test-server --- server/src/events/entity_manipulation.rs | 39 ++++++++------ server/src/events/invite.rs | 6 ++- server/src/lib.rs | 12 +++-- server/src/lod.rs | 2 +- server/src/sys/msg/character_screen.rs | 67 +++++++++++------------- server/src/sys/terrain.rs | 17 +++--- server/src/sys/terrain_sync.rs | 9 +++- server/src/test_world.rs | 18 +++++-- 8 files changed, 95 insertions(+), 75 deletions(-) diff --git a/server/src/events/entity_manipulation.rs b/server/src/events/entity_manipulation.rs index eaa031e871..12e7546c96 100644 --- a/server/src/events/entity_manipulation.rs +++ b/server/src/events/entity_manipulation.rs @@ -1,3 +1,5 @@ +#[cfg(not(feature = "worldgen"))] +use crate::test_world::{IndexOwned, World}; use crate::{ client::Client, comp::{ @@ -67,7 +69,8 @@ use specs::{ use std::{collections::HashMap, iter, sync::Arc, time::Duration}; use tracing::{debug, warn}; use vek::{Vec2, Vec3}; -use world::World; +#[cfg(feature = "worldgen")] +use world::{IndexOwned, World}; use super::{event_dispatch, ServerEvent}; @@ -286,6 +289,7 @@ fn handle_exp_gain( #[derive(SystemData)] pub struct DestroyEventData<'a> { entities: Entities<'a>, + #[cfg(feature = "worldgen")] rtsim: WriteExpect<'a, RtSim>, id_maps: Read<'a, IdMaps>, msm: ReadExpect<'a, MaterialStatManifest>, @@ -293,7 +297,7 @@ pub struct DestroyEventData<'a> { time: Read<'a, Time>, program_time: ReadExpect<'a, ProgramTime>, world: ReadExpect<'a, Arc>, - index: ReadExpect<'a, world::IndexOwned>, + index: ReadExpect<'a, IndexOwned>, areas_container: Read<'a, AreasContainer>, outcomes: Read<'a, EventBus>, create_item_drop: Read<'a, EventBus>, @@ -725,23 +729,24 @@ impl ServerEvent for DestroyEvent { }; let actor = entity_as_actor(ev.entity); + #[cfg(feature = "worldgen")] if let Some(actor) = actor { data.rtsim.hook_rtsim_actor_death( - &data.world, - data.index.as_index_ref(), - actor, - data.positions.get(ev.entity).map(|p| p.0), - ev.cause - .by - .as_ref() - .and_then( - |(DamageContributor::Solo(entity_uid) - | DamageContributor::Group { entity_uid, .. })| { - data.id_maps.uid_entity(*entity_uid) - }, - ) - .and_then(entity_as_actor), - ); + &data.world, + data.index.as_index_ref(), + actor, + data.positions.get(ev.entity).map(|p| p.0), + ev.cause + .by + .as_ref() + .and_then( + |(DamageContributor::Solo(entity_uid) + | DamageContributor::Group { entity_uid, .. })| { + data.id_maps.uid_entity(*entity_uid) + }, + ) + .and_then(entity_as_actor), + ); } if should_delete { diff --git a/server/src/events/invite.rs b/server/src/events/invite.rs index 89acc101d2..bf2a46533b 100644 --- a/server/src/events/invite.rs +++ b/server/src/events/invite.rs @@ -3,6 +3,8 @@ use super::{ group_manip::{self, update_map_markers}, ServerEvent, }; +#[cfg(not(feature = "worldgen"))] +use crate::test_world::IndexOwned; use crate::{client::Client, Settings}; use common::{ comp::{ @@ -24,6 +26,8 @@ use specs::{ }; use std::time::{Duration, Instant}; use tracing::{error, warn}; +#[cfg(feature = "worldgen")] +use world::IndexOwned; /// Time before invite times out const INVITE_TIMEOUT_DUR: Duration = Duration::from_secs(31); @@ -222,7 +226,7 @@ pub struct InviteResponseData<'a> { entities: Entities<'a>, group_manager: Write<'a, GroupManager>, trades: Write<'a, Trades>, - index: ReadExpect<'a, world::IndexOwned>, + index: ReadExpect<'a, IndexOwned>, id_maps: Read<'a, IdMaps>, invites: WriteStorage<'a, Invite>, pending_invites: WriteStorage<'a, PendingInvites>, diff --git a/server/src/lib.rs b/server/src/lib.rs index f2a914de28..c17585ca4f 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -308,7 +308,7 @@ impl Server { #[cfg(feature = "worldgen")] let map = world.get_map_data(index.as_index_ref(), &pools); #[cfg(not(feature = "worldgen"))] - let map = WorldMapMsg { + let map = common_net::msg::WorldMapMsg { dimensions_lg: Vec2::zero(), max_height: 1.0, rgba: Grid::new(Vec2::new(1, 1), 1), @@ -320,16 +320,18 @@ impl Server { default_chunk: Arc::new(world.generate_oob_chunk()), }; + #[cfg(feature = "worldgen")] + let map_size_lg = world.sim().map_size_lg(); + #[cfg(not(feature = "worldgen"))] + let map_size_lg = world.map_size_lg(); + let lod = lod::Lod::from_world(&world, index.as_index_ref(), &pools); report_stage(ServerInitStage::StartingSystems); let mut state = State::server( Arc::clone(&pools), - #[cfg(feature = "worldgen")] - world.sim().map_size_lg(), - #[cfg(not(feature = "worldgen"))] - common::terrain::map::MapSizeLg::new(Vec2::one()).unwrap(), + map_size_lg, Arc::clone(&map.default_chunk), |dispatcher_builder| { add_local_systems(dispatcher_builder); diff --git a/server/src/lod.rs b/server/src/lod.rs index 60e0c57461..0dd2966030 100644 --- a/server/src/lod.rs +++ b/server/src/lod.rs @@ -37,7 +37,7 @@ impl Lod { } #[cfg(not(feature = "worldgen"))] - pub fn from_world(world: &World, index: IndexRef, _threadpool: &rayon::ThreadPool) -> Self { + pub fn from_world(_world: &World, _index: IndexRef, _threadpool: &rayon::ThreadPool) -> Self { Self::default() } diff --git a/server/src/sys/msg/character_screen.rs b/server/src/sys/msg/character_screen.rs index 4ea0812412..fb4e34ce33 100644 --- a/server/src/sys/msg/character_screen.rs +++ b/server/src/sys/msg/character_screen.rs @@ -174,6 +174,37 @@ impl Sys { alias )))?; } else if let Some(player) = players.get(entity) { + #[cfg(feature = "worldgen")] + let waypoint = start_site.and_then(|site_idx| { + // TODO: This corresponds to the ID generation logic in + // `world/src/lib.rs`. Really, we should have + // a way to consistently refer to sites, but that's a job for rtsim2 + // and the site changes that it will require. Until then, this code is + // very hacky. + world + .civs() + .sites + .iter() + .find(|(_, site)| site.site_tmp.map(|i| i.id()) == Some(site_idx)) + .map(Some) + .unwrap_or_else(|| { + error!( + "Tried to create character with starting site index {}, but \ + such a site does not exist", + site_idx + ); + None + }) + .map(|(_, site)| { + let wpos2d = TerrainChunkSize::center_wpos(site.center); + Waypoint::new( + world.find_accessible_pos(index.as_index_ref(), wpos2d, false), + time, + ) + }) + }); + #[cfg(not(feature = "worldgen"))] + let waypoint = Some(Waypoint::new(world.get_center().with_z(10).as_(), time)); if let Err(error) = character_creator::create_character( entity, player.uuid().to_string(), @@ -182,41 +213,7 @@ impl Sys { offhand.clone(), body, character_updater, - #[cfg(feature = "worldgen")] - start_site.and_then(|site_idx| { - // TODO: This corresponds to the ID generation logic in - // `world/src/lib.rs`. Really, we should have - // a way to consistently refer to sites, but that's a job for rtsim2 - // and the site changes that it will require. Until then, this code is - // very hacky. - world - .civs() - .sites - .iter() - .find(|(_, site)| site.site_tmp.map(|i| i.id()) == Some(site_idx)) - .map(Some) - .unwrap_or_else(|| { - error!( - "Tried to create character with starting site index {}, \ - but such a site does not exist", - site_idx - ); - None - }) - .map(|(_, site)| { - let wpos2d = TerrainChunkSize::center_wpos(site.center); - Waypoint::new( - world.find_accessible_pos( - index.as_index_ref(), - wpos2d, - false, - ), - time, - ) - }) - }), - #[cfg(not(feature = "worldgen"))] - None, + waypoint, ) { debug!( ?error, diff --git a/server/src/sys/terrain.rs b/server/src/sys/terrain.rs index dca05b74b0..9247b492aa 100644 --- a/server/src/sys/terrain.rs +++ b/server/src/sys/terrain.rs @@ -254,8 +254,12 @@ impl<'a> System<'a> for Sys { } let max_view_distance = server_settings.max_view_distance.unwrap_or(u32::MAX); + #[cfg(feature = "worldgen")] + let world_size = world.sim().get_size(); + #[cfg(not(feature = "worldgen"))] + let world_size = world.map_size_lg().chunks().map(u32::from); let (presences_position_entities, presences_positions) = prepare_player_presences( - &world, + world_size, max_view_distance, &entities, &positions, @@ -688,7 +692,7 @@ fn prepare_for_vd_check( } pub fn prepare_player_presences<'a, P>( - world: &World, + world_size: Vec2, max_view_distance: u32, entities: &Entities<'a>, positions: P, @@ -704,14 +708,7 @@ where let world_aabr_in_chunks = Aabr { min: Vec2::zero(), // NOTE: Cast is correct because chunk coordinates must fit in an i32 (actually, i16). - #[cfg(feature = "worldgen")] - max: world - .sim() - .get_size() - .map(|x| x.saturating_sub(1)) - .as_::(), - #[cfg(not(feature = "worldgen"))] - max: Vec2::one(), + max: world_size.map(|x| x.saturating_sub(1)).as_::(), }; let (mut presences_positions_entities, mut presences_positions): (Vec<_>, Vec<_>) = diff --git a/server/src/sys/terrain_sync.rs b/server/src/sys/terrain_sync.rs index 74606e50d6..a42b0ad07c 100644 --- a/server/src/sys/terrain_sync.rs +++ b/server/src/sys/terrain_sync.rs @@ -1,3 +1,5 @@ +#[cfg(not(feature = "worldgen"))] +use crate::test_world::World; use crate::{chunk_serialize::ChunkSendEntry, client::Client, Settings}; use common::{ comp::{Pos, Presence}, @@ -9,7 +11,7 @@ use common_state::TerrainChanges; use rayon::prelude::*; use specs::{Entities, Join, Read, ReadExpect, ReadStorage}; use std::sync::Arc; -use world::World; +#[cfg(feature = "worldgen")] use world::World; /// This systems sends new chunks to clients as well as changes to existing /// chunks @@ -46,8 +48,11 @@ impl<'a> System<'a> for Sys { ) { let max_view_distance = server_settings.max_view_distance.unwrap_or(u32::MAX); #[cfg(feature = "worldgen")] + let world_size = world.sim().get_size(); + #[cfg(not(feature = "worldgen"))] + let world_size = world.map_size_lg().chunks().as_(); let (presences_position_entities, _) = super::terrain::prepare_player_presences( - &world, + world_size, max_view_distance, &entities, &positions, diff --git a/server/src/test_world.rs b/server/src/test_world.rs index eaaf930a6e..24a4a3424c 100644 --- a/server/src/test_world.rs +++ b/server/src/test_world.rs @@ -14,7 +14,7 @@ use std::time::Duration; use vek::*; const DEFAULT_WORLD_CHUNKS_LG: MapSizeLg = - if let Ok(map_size_lg) = MapSizeLg::new(Vec2 { x: 1, y: 1 }) { + if let Ok(map_size_lg) = MapSizeLg::new(Vec2 { x: 8, y: 8 }) { map_size_lg } else { panic!("Default world chunk size does not satisfy required invariants."); @@ -51,7 +51,8 @@ impl World { _index: IndexRef, chunk_pos: Vec2, _rtsim_resources: Option>, - _should_continue: impl FnMut() -> bool, + // TODO: misleading name + mut _should_continue: impl FnMut() -> bool, _time: Option<(TimeOfDay, Calendar)>, ) -> Result<(TerrainChunk, ChunkSupplement), ()> { let (x, y) = chunk_pos.map(|e| e.to_le_bytes()).into_tuple(); @@ -66,7 +67,7 @@ impl World { Ok(( TerrainChunk::new( - 256 + if rng.gen::() < 64 { height } else { 0 }, + if rng.gen::() < 64 { height } else { 0 }, Block::new(BlockKind::Grass, Rgb::new(11, 102, 35)), Block::air(SpriteKind::Empty), TerrainChunkMeta::void(), @@ -75,5 +76,14 @@ impl World { )) } - pub fn get_location_name(&self, _index: IndexRef, _wpos2d: Vec2) -> Option { None } + pub fn get_center(&self) -> Vec2 { + // FIXME: Assumes that TerrainChunkSize::RECT_SIZE.x == + // TerrainChunkSize::RECT_SIZE.y + DEFAULT_WORLD_CHUNKS_LG.chunks().as_::() / 2 * TerrainChunkSize::RECT_SIZE.x as u32 + } + + pub fn get_location_name(&self, _index: IndexRef, _wpos2d: Vec2) -> Option { + // Test world has no locations + None + } }