Natural spawning: answer mob box queries from a grid after a few scans
GameLevel's isUnobstructed, noCollision (living shulkers) and the spawners' entity count each scanned every mob, computing each 1.5 KB mob's box. A spawn pass holds the game immutably, so after eight queries it builds a grid of the mobs' boxes once and reads candidates from it; the first queries still scan, as the build costs a few scans. 100 box tests, 2000 mobs: 3.48 -> 0.31 ms (grid build included) Also gives the mob_ai_probe example the new others_index field. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UxXpf7d4RgpespL2G2UsGE
This commit is contained in:
@@ -1027,6 +1027,7 @@ fn main() -> horizon_client::Result<()> {
|
||||
nearest_player: player.as_ref().filter(|p| !p.spectator).map(|p| p.position),
|
||||
others: $snapshot,
|
||||
others_grid: $grid,
|
||||
others_index: None,
|
||||
daylight: (world.day_time.rem_euclid(24000)) < 12000,
|
||||
game_time: world.game_time,
|
||||
day_time: world.day_time,
|
||||
|
||||
@@ -9,6 +9,7 @@ use horizon_client::{
|
||||
mobs::{
|
||||
Mob, MobWorld, Species,
|
||||
data::{self, MobData},
|
||||
seen_grid::SeenGrid,
|
||||
spawn_rules::{Dimension, SpawnLevel},
|
||||
spawn_selection::{
|
||||
self, Category, Counted, Heightmap, NaturalLevel, SpawnCost, SpawnPlayer, SpawnRequest,
|
||||
@@ -75,8 +76,72 @@ pub(super) struct GameLevel<'g, 'a> {
|
||||
pub(super) pending: std::cell::RefCell<Vec<Aabb>>,
|
||||
/// The types and boxes of what a spawner created this tick.
|
||||
pub(super) pending_ids: std::cell::RefCell<Vec<(String, Aabb)>>,
|
||||
/// The game's mobs by box, built once a pass has asked more than
|
||||
/// [`SCANNED_QUERIES`] box queries (the mobs do not change while it
|
||||
/// holds the game); the first ones scan, as the build costs a few scans.
|
||||
pub(super) boxes: std::cell::OnceCell<MobBoxes>,
|
||||
pub(super) box_queries: std::cell::Cell<u32>,
|
||||
}
|
||||
const SCANNED_QUERIES: u32 = 8;
|
||||
/// A mob not removed: its index in `Game::mobs`, box, alive, and whether
|
||||
/// it is a living shulker.
|
||||
type MobBox = (usize, Aabb, bool, bool);
|
||||
fn mob_box(i: usize, m: &Mob) -> MobBox {
|
||||
let alive = m.alive();
|
||||
(
|
||||
i,
|
||||
m.bounds(),
|
||||
alive,
|
||||
alive && m.species() == Species::Shulker,
|
||||
)
|
||||
}
|
||||
/// The boxes of the game's mobs not removed, and a grid over them.
|
||||
pub(super) struct MobBoxes {
|
||||
entries: Vec<MobBox>,
|
||||
grid: SeenGrid,
|
||||
}
|
||||
impl MobBoxes {
|
||||
fn new(mobs: &[Mob]) -> Self {
|
||||
let entries: Vec<_> = mobs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, m)| !m.removed)
|
||||
.map(|(i, m)| mob_box(i, m))
|
||||
.collect();
|
||||
let grid = SeenGrid::new(entries.iter().map(|e| e.1));
|
||||
Self { entries, grid }
|
||||
}
|
||||
/// The entries whose boxes meet `area` (and possibly a few more).
|
||||
fn near(&self, area: Aabb) -> impl Iterator<Item = MobBox> + '_ {
|
||||
let mut near = Vec::new();
|
||||
let indexed = self.grid.candidates(area, &mut near);
|
||||
let all = (!indexed).then(|| self.entries.iter());
|
||||
near.into_iter()
|
||||
.map(|i| self.entries[i as usize])
|
||||
.chain(all.into_iter().flatten().copied())
|
||||
}
|
||||
}
|
||||
impl GameLevel<'_, '_> {
|
||||
/// The mobs not removed whose boxes may meet `area` (callers test them).
|
||||
fn mobs_near(&self, area: Aabb) -> Box<dyn Iterator<Item = MobBox> + '_> {
|
||||
let asked = self.box_queries.get() + 1;
|
||||
self.box_queries.set(asked);
|
||||
if asked <= SCANNED_QUERIES && self.boxes.get().is_none() {
|
||||
return Box::new(
|
||||
self.game
|
||||
.mobs
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, m)| !m.removed)
|
||||
.map(|(i, m)| mob_box(i, m)),
|
||||
);
|
||||
}
|
||||
Box::new(
|
||||
self.boxes
|
||||
.get_or_init(|| MobBoxes::new(&self.game.mobs))
|
||||
.near(area),
|
||||
)
|
||||
}
|
||||
fn biome(&self, x: i32, y: i32, z: i32) -> Option<&BiomeInfo> {
|
||||
let chunk = self.game.world.chunks.get(&(x >> 4, z >> 4))?;
|
||||
Some(self.game.res.biomes().get(chunk.biome(x, y, z)))
|
||||
@@ -218,12 +283,9 @@ impl NaturalLevel for GameLevel<'_, '_> {
|
||||
// getEntityCollisions(null, box): living shulkers are solid.
|
||||
let grown = bounds.inflate(Vec3::new(1e-7, 1e-7, 1e-7));
|
||||
no_collision_empty(&mut scene, bounds)
|
||||
&& !self.game.mobs.iter().any(|m| {
|
||||
m.species() == Species::Shulker
|
||||
&& m.alive()
|
||||
&& !m.removed
|
||||
&& m.bounds().intersects(grown)
|
||||
})
|
||||
&& !self
|
||||
.mobs_near(grown)
|
||||
.any(|(_, b, _, shulker)| shulker && b.intersects(grown))
|
||||
&& !self
|
||||
.pending_ids
|
||||
.borrow()
|
||||
@@ -238,10 +300,8 @@ impl NaturalLevel for GameLevel<'_, '_> {
|
||||
!player
|
||||
&& !self.pending.borrow().iter().any(|b| b.intersects(bounds))
|
||||
&& !self
|
||||
.game
|
||||
.mobs
|
||||
.iter()
|
||||
.any(|m| m.alive() && !m.removed && m.bounds().intersects(bounds))
|
||||
.mobs_near(bounds)
|
||||
.any(|(_, b, alive, _)| alive && b.intersects(bounds))
|
||||
}
|
||||
fn spawn_mobs(&self) -> bool {
|
||||
self.game.ui.rules.bool("spawn_mobs")
|
||||
@@ -283,10 +343,8 @@ impl horizon_client::mobs::spawner::SpawnerLevel for GameLevel<'_, '_> {
|
||||
}
|
||||
fn count_entities(&self, id: &str, bounds: Aabb) -> usize {
|
||||
let mobs = self
|
||||
.game
|
||||
.mobs
|
||||
.iter()
|
||||
.filter(|m| !m.removed && m.kind.id == id && m.bounds().intersects(bounds))
|
||||
.mobs_near(bounds)
|
||||
.filter(|&(i, b, ..)| self.game.mobs[i].kind.id == id && b.intersects(bounds))
|
||||
.count();
|
||||
let pending = self
|
||||
.pending_ids
|
||||
@@ -305,6 +363,8 @@ impl<'a> Game<'a> {
|
||||
players: self.level_players(),
|
||||
pending: Default::default(),
|
||||
pending_ids: Default::default(),
|
||||
boxes: Default::default(),
|
||||
box_queries: Default::default(),
|
||||
}
|
||||
}
|
||||
/// Creates a mob the spawner accepted, finalized the game's way; the
|
||||
|
||||
@@ -2346,6 +2346,58 @@ mod sensor_scan_tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Natural spawning's `isUnobstructed` box test against every mob (real
|
||||
/// `Mob`s), by scan and by a grid built once for the pass.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn spawn_box_cost() {
|
||||
let kind = kind();
|
||||
for n in [500u32, 1000, 2000] {
|
||||
let (seen, _) = herd(n, 384.0);
|
||||
let mobs: Vec<Mob> = seen
|
||||
.iter()
|
||||
.map(|s| Mob::new(s.id, kind.clone(), s.position, 0.0))
|
||||
.collect();
|
||||
let mut seed = 7u64;
|
||||
let mut next = |range: f64| {
|
||||
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
|
||||
(seed >> 11) as f64 / (1u64 << 53) as f64 * range
|
||||
};
|
||||
let queries: Vec<Aabb> = (0..100)
|
||||
.map(|_| {
|
||||
let (x, z) = (next(384.0), next(384.0));
|
||||
Aabb::new(Vec3::new(x, 64.0, z), Vec3::new(x + 0.9, 65.4, z + 0.9))
|
||||
})
|
||||
.collect();
|
||||
let t = std::time::Instant::now();
|
||||
let a: Vec<bool> = queries
|
||||
.iter()
|
||||
.map(|&q| {
|
||||
mobs.iter()
|
||||
.any(|m| m.alive() && !m.removed && m.bounds().intersects(q))
|
||||
})
|
||||
.collect();
|
||||
let before = t.elapsed().as_secs_f64() * 1e3;
|
||||
let t = std::time::Instant::now();
|
||||
let boxes: Vec<Aabb> = mobs.iter().map(|m| m.bounds()).collect();
|
||||
let grid = seen_grid::SeenGrid::new(boxes.iter().copied());
|
||||
let build = t.elapsed().as_secs_f64() * 1e3;
|
||||
let mut near = Vec::new();
|
||||
let b: Vec<bool> = queries
|
||||
.iter()
|
||||
.map(|&q| {
|
||||
assert!(grid.candidates(q, &mut near));
|
||||
near.iter().any(|&i| boxes[i as usize].intersects(q))
|
||||
})
|
||||
.collect();
|
||||
let after = t.elapsed().as_secs_f64() * 1e3;
|
||||
assert_eq!(a, b);
|
||||
println!(
|
||||
"{n} mobs, 100 spawn box tests: {before:.3} ms -> {after:.3} ms (grid build {build:.3} ms included)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// One tick of sensing: a twentieth of the mobs fire their sensor.
|
||||
#[test]
|
||||
#[ignore]
|
||||
|
||||
Reference in New Issue
Block a user