Merge pull request #2033 from ludovic111/perf/mob-ai-tick
🤖 Agent: verified: parity for the 75 harnesses parity.py picks passes locally, pr_ready --quick passes on the final head, lan_e2e mobs/hazards pass in-frame and on the server thread. Same results; −1.3% of the dedicated server's tick-thread cycles at 10 bots.
This commit is contained in:
@@ -64,6 +64,31 @@ impl MobWorld<'_> {
|
||||
let b = bounds.deflate(Vec3::new(0.001, 0.001, 0.001));
|
||||
let (lo, hi) = (b.min_position(), b.max_position());
|
||||
let mut out = Fluids::default();
|
||||
// The eye column below, in the box but for a box thinner than the
|
||||
// deflation.
|
||||
let (ex, ez) = (
|
||||
((lo.x + hi.x) / 2.0).floor() as i32,
|
||||
((lo.z + hi.z) / 2.0).floor() as i32,
|
||||
);
|
||||
// Most boxes are in sections without a fluid: no cell to read.
|
||||
let cells = (
|
||||
[
|
||||
(lo.x.floor() as i32).min(ex),
|
||||
lo.y.floor() as i32,
|
||||
(lo.z.floor() as i32).min(ez),
|
||||
],
|
||||
[
|
||||
(hi.x.ceil() as i32 - 1).max(ex),
|
||||
hi.y.ceil() as i32 - 1,
|
||||
(hi.z.ceil() as i32 - 1).max(ez),
|
||||
],
|
||||
);
|
||||
if !self
|
||||
.world
|
||||
.may_hold(cells.0, cells.1, |s| self.facts.get(s).fluid != Fluid::None)
|
||||
{
|
||||
return out;
|
||||
}
|
||||
for x in lo.x.floor() as i32..=(hi.x.ceil() as i32 - 1) {
|
||||
for y in lo.y.floor() as i32..=(hi.y.ceil() as i32 - 1) {
|
||||
for z in lo.z.floor() as i32..=(hi.z.ceil() as i32 - 1) {
|
||||
@@ -92,10 +117,6 @@ impl MobWorld<'_> {
|
||||
// EntityFluidInteraction.update: the eyes are in a fluid block of
|
||||
// the box in the entity's column, between its bottom and its height
|
||||
// for the camera.
|
||||
let (ex, ez) = (
|
||||
((lo.x + hi.x) / 2.0).floor() as i32,
|
||||
((lo.z + hi.z) / 2.0).floor() as i32,
|
||||
);
|
||||
for y in lo.y.floor() as i32..=(hi.y.ceil() as i32 - 1) {
|
||||
let (fluid, h) = self.fluid_height(ex, y, ez);
|
||||
if fluid != Fluid::Water || f64::from(y) + h < lo.y || eye_y < f64::from(y) {
|
||||
@@ -155,14 +176,28 @@ impl MobWorld<'_> {
|
||||
let c = f64::from(c);
|
||||
c < area.max(axis) && c + 1.0 > area.min(axis)
|
||||
};
|
||||
// Where no section of the cursor holds a block reaching out of its
|
||||
// cell, only the cells meeting `area` can hold the support.
|
||||
let (rx, ry, rz) = (range(Axis::X), range(Axis::Y), range(Axis::Z));
|
||||
let outer = self.world.may_hold(
|
||||
[*rx.start(), *ry.start(), *rz.start()],
|
||||
[*rx.end(), *ry.end(), *rz.end()],
|
||||
|s| self.collision.reaches_out(s),
|
||||
);
|
||||
let mut best: Option<(f64, [i32; 3])> = None;
|
||||
for x in range(Axis::X) {
|
||||
for z in range(Axis::Z) {
|
||||
for x in rx {
|
||||
for z in rz.clone() {
|
||||
let column = meets(Axis::X, x) && meets(Axis::Z, z);
|
||||
if !outer && !column {
|
||||
continue;
|
||||
}
|
||||
let Some(chunk) = self.world.chunks.get(&(x >> 4, z >> 4)) else {
|
||||
continue;
|
||||
};
|
||||
let column = meets(Axis::X, x) && meets(Axis::Z, z);
|
||||
for y in range(Axis::Y) {
|
||||
for y in ry.clone() {
|
||||
if !outer && !meets(Axis::Y, y) {
|
||||
continue;
|
||||
}
|
||||
let state = chunk.block(x & 15, y, z & 15);
|
||||
let Ok(block) = self.collision.collision_block(state) else {
|
||||
continue;
|
||||
|
||||
@@ -311,6 +311,40 @@ impl World {
|
||||
.get(&(x >> 4, z >> 4))
|
||||
.map_or(self.air, |chunk| chunk.block(x & 15, y, z & 15))
|
||||
}
|
||||
/// Whether a block of the box `lo..=hi` may match, from the palettes of
|
||||
/// the sections it reaches (a palette of the whole registry may hold
|
||||
/// anything): `false` means [`Self::block`] matches in none of its cells,
|
||||
/// so a scan over them can stop before reading one.
|
||||
pub fn may_hold(
|
||||
&self,
|
||||
lo: [i32; 3],
|
||||
hi: [i32; 3],
|
||||
mut matches: impl FnMut(BlockStateId) -> bool,
|
||||
) -> bool {
|
||||
let top = MIN_Y + SECTIONS as i32 * 16 - 1;
|
||||
// Heights outside the sections read as state 0.
|
||||
if (lo[1] < MIN_Y || hi[1] > top) && matches(BlockStateId(0)) {
|
||||
return true;
|
||||
}
|
||||
let sections = (lo[1].max(MIN_Y) - MIN_Y) >> 4..=(hi[1].min(top) - MIN_Y) >> 4;
|
||||
for cx in lo[0] >> 4..=hi[0] >> 4 {
|
||||
for cz in lo[2] >> 4..=hi[2] >> 4 {
|
||||
let Some(chunk) = self.chunks.get(&(cx, cz)) else {
|
||||
if matches(self.air) {
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
};
|
||||
for s in sections.clone() {
|
||||
let blocks = &chunk.sections[s as usize].blocks;
|
||||
if blocks.is_global() || blocks.maybe_has(|v| matches(BlockStateId(v))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
/// The block entities to tick this tick, in order (`BlockEntities::tickers`).
|
||||
pub fn block_entity_tickers(&mut self) -> Vec<(crate::block_entities::Pos, u32)> {
|
||||
let (chunks, air) = (&self.chunks, self.air);
|
||||
@@ -333,3 +367,32 @@ impl World {
|
||||
self.chunks.contains_key(&(x >> 4, z >> 4))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::World;
|
||||
use crate::chunk::Chunk;
|
||||
use horizon_world::registry::BlockStateId;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A box misses a state only where no section it reaches lists it, and
|
||||
/// unloaded columns or heights past the world count as their air.
|
||||
#[test]
|
||||
fn may_hold_reads_the_palettes_of_the_reached_sections() {
|
||||
let mut world = World::new(BlockStateId(0), 0);
|
||||
let mut chunk = Chunk::empty(0, 0);
|
||||
chunk.set(3, 70, 3, 9);
|
||||
world.chunks.insert((0, 0), Arc::new(chunk));
|
||||
let nine = |s: BlockStateId| s.0 == 9;
|
||||
// The section of y 64..80 holds 9, the one below does not.
|
||||
assert!(world.may_hold([0, 64, 0], [1, 65, 1], nine));
|
||||
assert!(world.may_hold([10, 79, 10], [12, 80, 12], nine));
|
||||
assert!(!world.may_hold([0, 50, 0], [15, 63, 15], nine));
|
||||
// A box over the unloaded column next door sees only air.
|
||||
assert!(!world.may_hold([16, 70, 0], [17, 71, 1], nine));
|
||||
assert!(world.may_hold([16, 70, 0], [17, 71, 1], |s| s.0 == 0));
|
||||
// Past the top, and the palette of air below it.
|
||||
assert!(!world.may_hold([0, 400, 0], [0, 401, 0], nine));
|
||||
assert!(world.may_hold([0, 400, 0], [0, 401, 0], |s| s.0 == 0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user