NPCs that live in stale sites just idle instead

This commit is contained in:
Joshua Barretto 2023-03-31 22:09:39 +01:00
parent f5c1e07642
commit 66710d5bc2
4 changed files with 24 additions and 7 deletions

View File

@ -3,7 +3,7 @@ pub use common::rtsim::{NpcId, Profession};
use common::{
comp,
grid::Grid,
rtsim::{FactionId, SiteId, VehicleId, Personality},
rtsim::{FactionId, Personality, SiteId, VehicleId},
store::Id,
vol::RectVolSize,
};

View File

@ -11,7 +11,7 @@ use common::{
comp::{self, Body},
grid::Grid,
resources::TimeOfDay,
rtsim::{WorldSettings, Personality},
rtsim::{Personality, WorldSettings},
terrain::TerrainChunkSize,
vol::RectVolSize,
};

View File

@ -350,7 +350,7 @@ fn goto_2d(wpos2d: Vec2<f32>, speed_factor: f32, goal_dist: f32) -> impl Action
})
}
fn traverse_points<F>(mut next_point: F) -> impl Action<()>
fn traverse_points<F>(mut next_point: F) -> impl Action
where
F: FnMut(&mut NpcCtx) -> Option<Vec2<f32>> + Send + Sync + 'static,
{
@ -397,7 +397,11 @@ fn travel_to_point(wpos: Vec2<f32>) -> impl Action {
let diff = wpos - start;
let n = (diff.magnitude() / WAYPOINT).max(1.0);
let mut points = (1..n as usize + 1).map(move |i| start + diff * (i as f32 / n));
traverse_points(move |_| points.next())
if diff.magnitude() > 1.0 {
traverse_points(move |_| points.next()).boxed()
} else {
finish().boxed()
}
})
.debug(|| "travel to point")
}
@ -538,7 +542,17 @@ fn adventure() -> impl Action {
fn villager(visiting_site: SiteId) -> impl Action {
choose(move |ctx| {
if ctx.npc.current_site != Some(visiting_site) {
if ctx
.state
.data()
.sites
.get(visiting_site)
.map_or(true, |s| s.world_site.is_none())
{
casual(
idle().debug(|| "idling (visiting site does not exist, perhaps it's stale data?)"),
)
} else if ctx.npc.current_site != Some(visiting_site) {
let npc_home = ctx.npc.home;
// Travel to the site we're supposed to be in
urgent(travel_to_site(visiting_site).debug(move || {

View File

@ -6,8 +6,9 @@ use crate::{
use common::{
comp::{self, Body},
grid::Grid,
rtsim::Personality,
terrain::TerrainChunkSize,
vol::RectVolSize, rtsim::Personality,
vol::RectVolSize,
};
use rand::{rngs::ThreadRng, seq::SliceRandom, Rng};
use tracing::warn;
@ -155,7 +156,9 @@ impl Rule for SimulateNpcs {
.world
.sim()
.get(npc.wpos.xy().as_::<i32>() / TerrainChunkSize::RECT_SIZE.as_())
.and_then(|chunk| data.sites.world_site_map.get(chunk.sites.first()?).copied());
.and_then(|chunk| chunk.sites
.iter()
.find_map(|site| data.sites.world_site_map.get(site).copied()));
let chunk_pos =
npc.wpos.xy().as_::<i32>() / TerrainChunkSize::RECT_SIZE.as_::<i32>();