Fixed NPCs talking to themselves

This commit is contained in:
Joshua Barretto 2023-04-05 14:43:19 +01:00
parent 5614eaa7a5
commit 80e4e8deae
4 changed files with 12 additions and 7 deletions

View File

@ -283,7 +283,12 @@ impl Npcs {
} }
/// Queries nearby npcs, not garantueed to work if radius > 32.0 /// Queries nearby npcs, not garantueed to work if radius > 32.0
pub fn nearby(&self, wpos: Vec2<f32>, radius: f32) -> impl Iterator<Item = Actor> + '_ { pub fn nearby(
&self,
this_npc: Option<NpcId>,
wpos: Vec2<f32>,
radius: f32,
) -> impl Iterator<Item = Actor> + '_ {
let chunk_pos = wpos let chunk_pos = wpos
.as_::<i32>() .as_::<i32>()
.map2(TerrainChunkSize::RECT_SIZE.as_::<i32>(), |e, sz| { .map2(TerrainChunkSize::RECT_SIZE.as_::<i32>(), |e, sz| {
@ -301,6 +306,7 @@ impl Npcs {
self.npcs self.npcs
.get(*npc) .get(*npc)
.map_or(false, |npc| npc.wpos.xy().distance_squared(wpos) < r_sqr) .map_or(false, |npc| npc.wpos.xy().distance_squared(wpos) < r_sqr)
&& Some(*npc) != this_npc
}) })
.map(Actor::Npc) .map(Actor::Npc)
}) })

View File

@ -452,11 +452,11 @@ fn timeout(time: f64) -> impl FnMut(&mut NpcCtx) -> bool + Clone + Send + Sync {
fn socialize() -> impl Action { fn socialize() -> impl Action {
now(|ctx| { now(|ctx| {
// TODO: Bit odd, should wait for a while after greeting // 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 .state
.data() .data()
.npcs .npcs
.nearby(ctx.npc.wpos.xy(), 8.0) .nearby(Some(ctx.npc_id), ctx.npc.wpos.xy(), 8.0)
.choose(&mut ctx.rng) .choose(&mut ctx.rng)
{ {
just(move |ctx| ctx.controller.greet(other)).boxed() just(move |ctx| ctx.controller.greet(other)).boxed()

View File

@ -33,8 +33,6 @@ fn on_setup(ctx: EventCtx<SyncNpcs, OnSetup>) {
fn on_death(ctx: EventCtx<SyncNpcs, OnDeath>) { fn on_death(ctx: EventCtx<SyncNpcs, OnDeath>) {
let data = &mut *ctx.state.data_mut(); let data = &mut *ctx.state.data_mut();
println!("NPC DIED!");
// Remove NPC from home population // Remove NPC from home population
if let Some(home) = data if let Some(home) = data
.npcs .npcs

View File

@ -489,7 +489,6 @@ fn handle_rtsim_actions(bdata: &mut BehaviorData) -> bool {
bdata.agent.awareness.set_maximally_aware(); bdata.agent.awareness.set_maximally_aware();
bdata.controller.push_action(ControlAction::Stand); bdata.controller.push_action(ControlAction::Stand);
bdata.controller.push_action(ControlAction::Talk);
bdata.controller.push_utterance(UtteranceKind::Greeting); bdata.controller.push_utterance(UtteranceKind::Greeting);
bdata bdata
.agent_data .agent_data
@ -506,8 +505,10 @@ fn handle_rtsim_actions(bdata: &mut BehaviorData) -> bool {
bdata.agent_data.chat_npc(msg, bdata.event_emitter); 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 /// Handle timed events, like looking at the player we are talking to