From d26a71146932bdf9f151e564ee9a76eeec2c5390 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Thu, 13 Apr 2023 12:22:28 +0100 Subject: [PATCH] Update site populations when NPCs move home --- rtsim/src/rule/npc_ai.rs | 4 ++-- rtsim/src/rule/simulate_npcs.rs | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/rtsim/src/rule/npc_ai.rs b/rtsim/src/rule/npc_ai.rs index c683aa6e3f..409f0b3ed1 100644 --- a/rtsim/src/rule/npc_ai.rs +++ b/rtsim/src/rule/npc_ai.rs @@ -715,8 +715,8 @@ fn villager(visiting_site: SiteId) -> impl Action { } } else if matches!(ctx.npc.profession, Some(Profession::Guard)) && ctx.rng.gen_bool(0.5) { if let Some(plaza_wpos) = choose_plaza(ctx, visiting_site) { - return important( - travel_to_point(plaza_wpos, 0.45) + return casual( + travel_to_point(plaza_wpos, 0.4) .debug(|| "patrol") .interrupt_with(|ctx| { if ctx.rng.gen_bool(0.0003) { diff --git a/rtsim/src/rule/simulate_npcs.rs b/rtsim/src/rule/simulate_npcs.rs index 0bdc0c9156..0350a414bb 100644 --- a/rtsim/src/rule/simulate_npcs.rs +++ b/rtsim/src/rule/simulate_npcs.rs @@ -151,7 +151,7 @@ fn on_death(ctx: EventCtx) { fn on_tick(ctx: EventCtx) { let data = &mut *ctx.state.data_mut(); - for npc in data.npcs.npcs.values_mut().filter(|npc| !npc.is_dead) { + for (npc_id, npc) in data.npcs.npcs.iter_mut().filter(|(_, npc)| !npc.is_dead) { if matches!(npc.mode, SimulationMode::Simulated) { // Simulate NPC movement when riding if let Some(riding) = &npc.riding { @@ -273,6 +273,16 @@ fn on_tick(ctx: EventCtx) { // Move home if required if let Some(new_home) = npc.controller.new_home.take() { + // Remove the NPC from their old home population + if let Some(old_home) = npc.home { + if let Some(old_home) = data.sites.get_mut(old_home) { + old_home.population.remove(&npc_id); + } + } + // Add the NPC to their new home population + if let Some(new_home) = data.sites.get_mut(new_home) { + new_home.population.insert(npc_id); + } npc.home = Some(new_home); } }