diff --git a/common/src/states/basic_summon.rs b/common/src/states/basic_summon.rs index 35aed55e29..5b174e943b 100644 --- a/common/src/states/basic_summon.rs +++ b/common/src/states/basic_summon.rs @@ -15,6 +15,7 @@ use crate::{ utils::*, }, terrain::Block, + util::Dir, vol::ReadVol, }; use rand::Rng; @@ -189,13 +190,7 @@ impl CharacterBehavior for Data { // Send server event to create npc output_events.emit_server(ServerEvent::CreateNpc { pos: comp::Pos(collision_vector - Vec3::unit_z() * obstacle_z), - ori: crate::util::Dir::from_unnormalized(Vec3::new( - rng.gen_range(-1.0..=1.0), - rng.gen_range(-1.0..=1.0), - 0.0, - )) - .map(comp::Ori::from) - .unwrap_or_default(), + ori: comp::Ori::from(Dir::random_2d(&mut rng)), npc: NpcBuilder::new(stats, body, comp::Alignment::Owned(*data.uid)) .with_skill_set(skill_set) .with_health(health) diff --git a/common/src/util/dir.rs b/common/src/util/dir.rs index 0cec04489b..162adfc97a 100644 --- a/common/src/util/dir.rs +++ b/common/src/util/dir.rs @@ -1,4 +1,5 @@ use super::{Plane, Projection}; +use rand::Rng; use serde::{Deserialize, Serialize}; use tracing::warn; use vek::*; @@ -79,6 +80,16 @@ impl Dir { }) } + /// Generates a random direction that has a z component of 0 + pub fn random_2d(rng: &mut impl Rng) -> Self { + Self::from_unnormalized(Vec3::new( + rng.gen_range(-1.0..=1.0), + rng.gen_range(-1.0..=1.0), + 0.0, + )) + .unwrap_or(Self::new(Vec3::unit_x())) + } + pub fn slerp(from: Self, to: Self, factor: f32) -> Self { Self(slerp_normalized(from.0, to.0, factor)) } diff --git a/rtsim/src/data/npc.rs b/rtsim/src/data/npc.rs index 62bad677d6..1912667c61 100644 --- a/rtsim/src/data/npc.rs +++ b/rtsim/src/data/npc.rs @@ -96,12 +96,6 @@ pub struct Brain { pub action: Box>, } -// #[derive(Serialize, Deserialize, Default, Clone)] -// pub struct Relations { -// #[serde(skip)] -// pub driver: Option, -// } - #[derive(Serialize, Deserialize)] pub struct Npc { pub uid: u64, @@ -115,7 +109,6 @@ pub struct Npc { pub role: Role, pub home: Option, pub faction: Option, - // pub relations: Relations, pub is_dead: bool, /// The [`Report`]s that the NPC is aware of. @@ -158,7 +151,6 @@ impl Clone for Npc { role: self.role.clone(), home: self.home, faction: self.faction, - // relations: self.relations.clone(), is_dead: self.is_dead, known_reports: self.known_reports.clone(), body: self.body, @@ -192,7 +184,6 @@ impl Npc { role, home: None, faction: None, - // relations: Default::default(), is_dead: false, known_reports: Default::default(), chunk_pos: None, diff --git a/rtsim/src/rule/simulate_npcs.rs b/rtsim/src/rule/simulate_npcs.rs index 4019306f2e..c32faff891 100644 --- a/rtsim/src/rule/simulate_npcs.rs +++ b/rtsim/src/rule/simulate_npcs.rs @@ -1,6 +1,6 @@ use crate::{ data::{npc::SimulationMode, Npc}, - event::{EventCtx, OnDeath, OnMountVolume, OnSetup, OnTick}, + event::{EventCtx, OnDeath, OnMountVolume, OnTick}, RtState, Rule, RuleError, }; use common::{ @@ -21,7 +21,6 @@ pub struct SimulateNpcs; impl Rule for SimulateNpcs { fn start(rtstate: &mut RtState) -> Result { - rtstate.bind(on_setup); rtstate.bind(on_death); rtstate.bind(on_tick); rtstate.bind(on_mount_volume); @@ -30,31 +29,6 @@ impl Rule for SimulateNpcs { } } -fn on_setup(_ctx: EventCtx) { - /* - let data = &mut *ctx.state.data_mut(); - // Add riders to vehicles - let riders = data - .npcs - .iter() - .filter_map(|(id, npc)| Some((id, npc.relations.riding?))) - .collect::>(); - - for (npc_id, ride) in riders { - if let Some(mount) = data.npcs.get_mut(ride.npc) { - if ride.steering && let Some(actor) = mount.relations.driver.replace(Actor::Npc(npc_id)) { - error!("Replaced driver"); - if let Actor::Npc(npc) = actor && let Some(npc) = data.npcs.get_mut(npc) { - npc.relations.riding = None; - } - } - } else { - data.npcs[npc_id].relations.riding = None; - } - } - */ -} - fn on_mount_volume(ctx: EventCtx) { let data = &mut *ctx.state.data_mut(); diff --git a/server/src/state_ext.rs b/server/src/state_ext.rs index 9a01615767..fd6813230a 100644 --- a/server/src/state_ext.rs +++ b/server/src/state_ext.rs @@ -32,6 +32,7 @@ use common::{ rtsim::{Actor, RtSimEntity}, slowjob::SlowJobPool, uid::{IdMaps, Uid}, + util::Dir, LoadoutBuilder, ViewDistances, }; use common_net::{ @@ -785,13 +786,7 @@ impl StateExt for State { let mut rng = rand::thread_rng(); for (pet, body, stats) in pets { - let ori = common::util::Dir::from_unnormalized(Vec3::new( - rng.gen_range(-1.0..=1.0), - rng.gen_range(-1.0..=1.0), - 0.0, - )) - .map(comp::Ori::from) - .unwrap_or_default(); + let ori = comp::Ori::from(Dir::random_2d(&mut rng)); let pet_entity = self .create_npc( player_pos, diff --git a/server/src/sys/terrain.rs b/server/src/sys/terrain.rs index abdab100d6..5bdee93589 100644 --- a/server/src/sys/terrain.rs +++ b/server/src/sys/terrain.rs @@ -2,7 +2,6 @@ use crate::test_world::{IndexOwned, World}; #[cfg(feature = "persistent_world")] use crate::TerrainPersistence; -use rand::Rng; #[cfg(feature = "worldgen")] use world::{IndexOwned, World}; @@ -22,6 +21,7 @@ use common::{ resources::{Time, TimeOfDay}, slowjob::SlowJobPool, terrain::TerrainGrid, + util::Dir, SkillSetBuilder, }; @@ -213,13 +213,7 @@ impl<'a> System<'a> for Sys { } => { server_emitter.emit(ServerEvent::CreateNpc { pos, - ori: common::util::Dir::from_unnormalized(Vec3::new( - rng.gen_range(-1.0..=1.0), - rng.gen_range(-1.0..=1.0), - 0.0, - )) - .map(comp::Ori::from) - .unwrap_or_default(), + ori: comp::Ori::from(Dir::random_2d(&mut rng)), npc: NpcBuilder::new(stats, body, alignment) .with_skill_set(skill_set) .with_health(health)