From 80e4e8deae4bede254a8c74fe1d67a3d875a2313 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Wed, 5 Apr 2023 14:43:19 +0100 Subject: [PATCH] Fixed NPCs talking to themselves --- rtsim/src/data/npc.rs | 8 +++++++- rtsim/src/rule/npc_ai.rs | 4 ++-- rtsim/src/rule/sync_npcs.rs | 2 -- server/src/sys/agent/behavior_tree.rs | 5 +++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/rtsim/src/data/npc.rs b/rtsim/src/data/npc.rs index 23e4fcd902..3a21af4da2 100644 --- a/rtsim/src/data/npc.rs +++ b/rtsim/src/data/npc.rs @@ -283,7 +283,12 @@ impl Npcs { } /// Queries nearby npcs, not garantueed to work if radius > 32.0 - pub fn nearby(&self, wpos: Vec2, radius: f32) -> impl Iterator + '_ { + pub fn nearby( + &self, + this_npc: Option, + wpos: Vec2, + radius: f32, + ) -> impl Iterator + '_ { let chunk_pos = wpos .as_::() .map2(TerrainChunkSize::RECT_SIZE.as_::(), |e, sz| { @@ -301,6 +306,7 @@ impl Npcs { self.npcs .get(*npc) .map_or(false, |npc| npc.wpos.xy().distance_squared(wpos) < r_sqr) + && Some(*npc) != this_npc }) .map(Actor::Npc) }) diff --git a/rtsim/src/rule/npc_ai.rs b/rtsim/src/rule/npc_ai.rs index ac15e8b0b2..73d49aa6fc 100644 --- a/rtsim/src/rule/npc_ai.rs +++ b/rtsim/src/rule/npc_ai.rs @@ -452,11 +452,11 @@ fn timeout(time: f64) -> impl FnMut(&mut NpcCtx) -> bool + Clone + Send + Sync { fn socialize() -> impl Action { now(|ctx| { // TODO: Bit odd, should wait for a while after greeting - if ctx.rng.gen_bool(0.004) && let Some(other) = ctx + if ctx.rng.gen_bool(0.002) && let Some(other) = ctx .state .data() .npcs - .nearby(ctx.npc.wpos.xy(), 8.0) + .nearby(Some(ctx.npc_id), ctx.npc.wpos.xy(), 8.0) .choose(&mut ctx.rng) { just(move |ctx| ctx.controller.greet(other)).boxed() diff --git a/rtsim/src/rule/sync_npcs.rs b/rtsim/src/rule/sync_npcs.rs index 981b1c072c..61b81939af 100644 --- a/rtsim/src/rule/sync_npcs.rs +++ b/rtsim/src/rule/sync_npcs.rs @@ -33,8 +33,6 @@ fn on_setup(ctx: EventCtx) { fn on_death(ctx: EventCtx) { let data = &mut *ctx.state.data_mut(); - println!("NPC DIED!"); - // Remove NPC from home population if let Some(home) = data .npcs diff --git a/server/src/sys/agent/behavior_tree.rs b/server/src/sys/agent/behavior_tree.rs index 5c2607b364..c78e9c38d9 100644 --- a/server/src/sys/agent/behavior_tree.rs +++ b/server/src/sys/agent/behavior_tree.rs @@ -489,7 +489,6 @@ fn handle_rtsim_actions(bdata: &mut BehaviorData) -> bool { bdata.agent.awareness.set_maximally_aware(); bdata.controller.push_action(ControlAction::Stand); - bdata.controller.push_action(ControlAction::Talk); bdata.controller.push_utterance(UtteranceKind::Greeting); bdata .agent_data @@ -506,8 +505,10 @@ fn handle_rtsim_actions(bdata: &mut BehaviorData) -> bool { bdata.agent_data.chat_npc(msg, bdata.event_emitter); }, } + true + } else { + false } - false } /// Handle timed events, like looking at the player we are talking to