WIP: block behaviour level module checkpoint
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -550,6 +550,7 @@ props! {
|
||||
orientation = "orientation",
|
||||
crafting = "crafting",
|
||||
side_chain = "side_chain",
|
||||
copper_golem_pose = "copper_golem_pose",
|
||||
}
|
||||
|
||||
fn fail(message: &str) -> io::Error {
|
||||
|
||||
@@ -0,0 +1,599 @@
|
||||
//! `FlowingFluid` (water and lava) ticking and spreading, `LiquidBlock`
|
||||
//! interactions, liquid containers (waterlogging) and bucket pickup.
|
||||
use super::{
|
||||
Dimension, LevelAccess, Sim,
|
||||
classes::{Class, Interface},
|
||||
update::{HORIZONTAL, above, below, rel},
|
||||
};
|
||||
use crate::{
|
||||
fluid::{FluidKind, FluidState},
|
||||
registry::BlockStateId,
|
||||
};
|
||||
use horizon_core::{
|
||||
geometry::{Axis, Direction, ShapeOp, VoxelShape},
|
||||
position::BlockPos,
|
||||
random::RandomSource,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// `LiquidBlock.POSSIBLE_FLOW_DIRECTIONS`.
|
||||
const POSSIBLE_FLOW_DIRECTIONS: [Direction; 5] = [
|
||||
Direction::Down,
|
||||
Direction::South,
|
||||
Direction::North,
|
||||
Direction::East,
|
||||
Direction::West,
|
||||
];
|
||||
|
||||
fn is_water(kind: FluidKind) -> bool {
|
||||
matches!(kind, FluidKind::Water | FluidKind::FlowingWater)
|
||||
}
|
||||
fn source_of(kind: FluidKind) -> FluidKind {
|
||||
if is_water(kind) { FluidKind::Water } else { FluidKind::Lava }
|
||||
}
|
||||
fn flowing_of(kind: FluidKind) -> FluidKind {
|
||||
if is_water(kind) { FluidKind::FlowingWater } else { FluidKind::FlowingLava }
|
||||
}
|
||||
|
||||
/// `FlowingFluid.SpreadContext`: per-spread caches keyed by horizontal offset.
|
||||
struct SpreadContext {
|
||||
origin: BlockPos,
|
||||
states: HashMap<i16, BlockStateId>,
|
||||
holes: HashMap<i16, bool>,
|
||||
}
|
||||
impl SpreadContext {
|
||||
fn key(&self, pos: BlockPos) -> i16 {
|
||||
let x = pos.x.wrapping_sub(self.origin.x).wrapping_add(128) & 0xff;
|
||||
let z = pos.z.wrapping_sub(self.origin.z).wrapping_add(128) & 0xff;
|
||||
((x << 8) | z) as i16
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
fn fast_lava(&self) -> bool {
|
||||
self.level.dimension() == Dimension::Nether
|
||||
}
|
||||
/// `Fluid.getTickDelay`.
|
||||
pub(crate) fn fluid_tick_delay(&self, kind: FluidKind) -> i32 {
|
||||
match kind {
|
||||
FluidKind::Empty => 0,
|
||||
FluidKind::Water | FluidKind::FlowingWater => 5,
|
||||
_ => {
|
||||
if self.fast_lava() {
|
||||
10
|
||||
} else {
|
||||
30
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn drop_off(&self, kind: FluidKind) -> i32 {
|
||||
if is_water(kind) || self.fast_lava() { 1 } else { 2 }
|
||||
}
|
||||
fn slope_find_distance(&self, kind: FluidKind) -> i32 {
|
||||
if is_water(kind) || self.fast_lava() { 4 } else { 2 }
|
||||
}
|
||||
fn can_convert_to_source(&self, kind: FluidKind) -> bool {
|
||||
let rules = self.level.game_rules();
|
||||
if is_water(kind) { rules.water_source_conversion } else { rules.lava_source_conversion }
|
||||
}
|
||||
fn fluid_height(&self, fluid: FluidState, pos: BlockPos) -> f32 {
|
||||
if fluid.kind() == FluidKind::Empty {
|
||||
return 0.0;
|
||||
}
|
||||
if fluid.kind().same_family(self.fluid_at(above(pos)).kind()) {
|
||||
1.0
|
||||
} else {
|
||||
fluid.own_height()
|
||||
}
|
||||
}
|
||||
/// `FluidState.canBeReplacedWith(level, pos, other, direction)`.
|
||||
fn can_be_replaced_with(&self, fluid: FluidState, pos: BlockPos, other: FluidKind, direction: Direction) -> bool {
|
||||
match fluid.kind() {
|
||||
FluidKind::Empty => true,
|
||||
FluidKind::Water | FluidKind::FlowingWater => direction == Direction::Down && !is_water(other),
|
||||
_ => self.fluid_height(fluid, pos) >= 0.444_444_45 && is_water(other),
|
||||
}
|
||||
}
|
||||
|
||||
// ---- shape occlusion ----
|
||||
|
||||
fn collision_or_empty(&self, state: BlockStateId) -> (i32, VoxelShape) {
|
||||
let index = self.b.facts(state).shape;
|
||||
match self.b.collision_shape(state) {
|
||||
Some(shape) => (index, shape.clone()),
|
||||
None => (-1, VoxelShape::empty()),
|
||||
}
|
||||
}
|
||||
/// `FlowingFluid.canPassThroughWall`.
|
||||
fn can_pass_through_wall(&self, direction: Direction, source: BlockStateId, target: BlockStateId) -> bool {
|
||||
let (target_index, target_shape) = self.collision_or_empty(target);
|
||||
let block = VoxelShape::block();
|
||||
if target_shape.same_identity(&block) {
|
||||
return false;
|
||||
}
|
||||
let (source_index, source_shape) = self.collision_or_empty(source);
|
||||
if source_shape.same_identity(&block) {
|
||||
return false;
|
||||
}
|
||||
let empty = VoxelShape::empty();
|
||||
if source_shape.same_identity(&empty) && target_shape.same_identity(&empty) {
|
||||
return true;
|
||||
}
|
||||
let cacheable = source_index >= 0 && target_index >= 0;
|
||||
let key = (source_index, target_index, direction as u8);
|
||||
if cacheable {
|
||||
if let Some(result) = self.b.occlusion_cache.lock().ok().and_then(|cache| cache.get(&key).copied()) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
let result = !merged_face_occludes(&source_shape, &target_shape, direction);
|
||||
if cacheable {
|
||||
if let Ok(mut cache) = self.b.occlusion_cache.lock() {
|
||||
cache.insert(key, result);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
// ---- liquid containers ----
|
||||
|
||||
/// `LiquidBlockContainer` membership (`canHoldAnyFluid` first clause).
|
||||
fn is_liquid_container(&self, state: BlockStateId) -> bool {
|
||||
self.b.implements(state, Interface::LiquidBlockContainer) || self.b.implements(state, Interface::SimpleWaterlogged)
|
||||
}
|
||||
/// `LiquidBlockContainer.canPlaceLiquid(null, level, pos, state, fluid)`.
|
||||
pub(crate) fn can_place_liquid(&self, state: BlockStateId, fluid: FluidKind) -> bool {
|
||||
let b = self.b;
|
||||
let data = b.data(state);
|
||||
if data.is(Class::Kelp)
|
||||
|| data.is(Class::KelpPlant)
|
||||
|| data.is(Class::Seagrass)
|
||||
|| data.is(Class::TallSeagrass)
|
||||
|| data.is(Class::Barrier)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if data.is(Class::Slab) && b.table.text(state, b.props.kind) == "double" {
|
||||
return false;
|
||||
}
|
||||
fluid == FluidKind::Water
|
||||
}
|
||||
/// `LiquidBlockContainer.placeLiquid`.
|
||||
pub(crate) fn place_liquid(&mut self, pos: BlockPos, state: BlockStateId, fluid: FluidState) -> bool {
|
||||
let b = self.b;
|
||||
if !self.can_place_liquid(state, fluid.kind()) || fluid.kind() != FluidKind::Water || !b.table.has(state, b.props.waterlogged) {
|
||||
return false;
|
||||
}
|
||||
if b.is_waterlogged(state) {
|
||||
return false;
|
||||
}
|
||||
let mut next = b.table.set_bool(state, b.props.waterlogged, true);
|
||||
let data = b.data(state);
|
||||
if (data.is(Class::Candle) || data.is(Class::Campfire)) && b.table.bool(state, b.props.lit) {
|
||||
next = b.table.set_bool(next, b.props.lit, false);
|
||||
if data.is(Class::Campfire) {
|
||||
self.emit(super::LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.generic.extinguish_fire".into(),
|
||||
volume: 1.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
} else {
|
||||
self.emit(super::LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.candle.extinguish".into(),
|
||||
volume: 1.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
self.set_block_and_update(pos, next);
|
||||
let delay = self.fluid_tick_delay(fluid.kind());
|
||||
self.schedule_fluid_tick(pos, fluid.kind(), delay);
|
||||
true
|
||||
}
|
||||
/// `BucketPickup.pickupBlock`: true when a fluid was taken.
|
||||
pub(crate) fn pickup_block(&mut self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
let b = self.b;
|
||||
if b.instance_of(state, Class::Liquid) {
|
||||
if b.table.int(state, b.props.level) == 0 {
|
||||
self.set_block(pos, b.air(), 11);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if b.implements(state, Interface::SimpleWaterlogged) && b.table.has(state, b.props.waterlogged) {
|
||||
if b.is_waterlogged(state) {
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.waterlogged, false));
|
||||
if !self.can_survive(state, pos) {
|
||||
self.destroy_block(pos, true, 512);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if b.is_block(state, b.named.powder_snow) {
|
||||
self.set_block(pos, b.air(), 11);
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
fn can_hold_any_fluid(&self, state: BlockStateId) -> bool {
|
||||
self.is_liquid_container(state) || self.b.in_set(state, &self.b.tags.washed_away_by_fluids)
|
||||
}
|
||||
fn can_hold_specific_fluid(&self, state: BlockStateId, fluid: FluidKind) -> bool {
|
||||
if self.is_liquid_container(state) { self.can_place_liquid(state, fluid) } else { true }
|
||||
}
|
||||
fn can_hold_fluid(&self, state: BlockStateId, fluid: FluidKind) -> bool {
|
||||
self.can_hold_any_fluid(state) && self.can_hold_specific_fluid(state, fluid)
|
||||
}
|
||||
|
||||
// ---- FlowingFluid ----
|
||||
|
||||
fn is_source_of(&self, kind: FluidKind, fluid: FluidState) -> bool {
|
||||
fluid.kind().same_family(kind) && fluid.is_source()
|
||||
}
|
||||
/// `FlowingFluid.getNewLiquid`.
|
||||
fn new_liquid(&self, kind: FluidKind, pos: BlockPos, state: BlockStateId) -> FluidState {
|
||||
let b = self.b;
|
||||
let mut highest = 0;
|
||||
let mut sources = 0;
|
||||
for direction in HORIZONTAL {
|
||||
let neighbour = self.rel_state(pos, direction);
|
||||
let fluid = b.fluid(neighbour);
|
||||
if fluid.kind().same_family(kind) && self.can_pass_through_wall(direction, state, neighbour) {
|
||||
if fluid.is_source() {
|
||||
sources += 1;
|
||||
}
|
||||
highest = highest.max(i32::from(fluid.amount()));
|
||||
}
|
||||
}
|
||||
if sources >= 2 && self.can_convert_to_source(kind) {
|
||||
let below_state = self.below_state(pos);
|
||||
if b.is_solid(below_state) || self.is_source_of(kind, b.fluid(below_state)) {
|
||||
return FluidState::new(source_of(kind), 8, false);
|
||||
}
|
||||
}
|
||||
let above_state = self.above_state(pos);
|
||||
let above_fluid = b.fluid(above_state);
|
||||
if above_fluid.kind() != FluidKind::Empty
|
||||
&& above_fluid.kind().same_family(kind)
|
||||
&& self.can_pass_through_wall(Direction::Up, state, above_state)
|
||||
{
|
||||
return FluidState::new(flowing_of(kind), 8, true);
|
||||
}
|
||||
let amount = highest - self.drop_off(kind);
|
||||
if amount <= 0 {
|
||||
FluidState::EMPTY
|
||||
} else {
|
||||
FluidState::new(flowing_of(kind), amount as u8, false)
|
||||
}
|
||||
}
|
||||
fn is_water_hole(&self, kind: FluidKind, top: BlockStateId, bottom: BlockStateId) -> bool {
|
||||
if !self.can_pass_through_wall(Direction::Down, top, bottom) {
|
||||
return false;
|
||||
}
|
||||
if self.b.fluid(bottom).kind().same_family(kind) {
|
||||
true
|
||||
} else {
|
||||
self.can_hold_fluid(bottom, flowing_of(kind))
|
||||
}
|
||||
}
|
||||
fn can_maybe_pass_through(&self, kind: FluidKind, source: BlockStateId, direction: Direction, test: BlockStateId, test_fluid: FluidState) -> bool {
|
||||
!self.is_source_of(kind, test_fluid) && self.can_hold_any_fluid(test) && self.can_pass_through_wall(direction, source, test)
|
||||
}
|
||||
fn can_pass_through(&self, kind: FluidKind, source: BlockStateId, direction: Direction, test: BlockStateId, test_fluid: FluidState) -> bool {
|
||||
self.can_maybe_pass_through(kind, source, direction, test, test_fluid) && self.can_hold_specific_fluid(test, flowing_of(kind))
|
||||
}
|
||||
fn context_state(&self, context: &mut SpreadContext, pos: BlockPos) -> BlockStateId {
|
||||
let key = context.key(pos);
|
||||
if let Some(state) = context.states.get(&key) {
|
||||
return *state;
|
||||
}
|
||||
let state = self.state(pos);
|
||||
context.states.insert(key, state);
|
||||
state
|
||||
}
|
||||
fn context_is_hole(&self, kind: FluidKind, context: &mut SpreadContext, pos: BlockPos) -> bool {
|
||||
let key = context.key(pos);
|
||||
if let Some(hole) = context.holes.get(&key) {
|
||||
return *hole;
|
||||
}
|
||||
let state = self.context_state(context, pos);
|
||||
let below_state = self.below_state(pos);
|
||||
let hole = self.is_water_hole(kind, state, below_state);
|
||||
context.holes.insert(key, hole);
|
||||
hole
|
||||
}
|
||||
fn slope_distance(&self, kind: FluidKind, pos: BlockPos, pass: i32, from: Direction, state: BlockStateId, context: &mut SpreadContext) -> i32 {
|
||||
let mut lowest = 1000;
|
||||
for direction in HORIZONTAL {
|
||||
if direction == from {
|
||||
continue;
|
||||
}
|
||||
let test_pos = rel(pos, direction);
|
||||
let test_state = self.context_state(context, test_pos);
|
||||
let test_fluid = self.b.fluid(test_state);
|
||||
if self.can_pass_through(kind, state, direction, test_state, test_fluid) {
|
||||
if self.context_is_hole(kind, context, test_pos) {
|
||||
return pass;
|
||||
}
|
||||
if pass < self.slope_find_distance(kind) {
|
||||
let distance = self.slope_distance(kind, test_pos, pass + 1, direction.opposite(), test_state, context);
|
||||
lowest = lowest.min(distance);
|
||||
}
|
||||
}
|
||||
}
|
||||
lowest
|
||||
}
|
||||
/// `FlowingFluid.getSpread` (EnumMap: iteration in Direction order).
|
||||
fn spread_targets(&self, kind: FluidKind, pos: BlockPos, state: BlockStateId) -> Vec<(Direction, FluidState)> {
|
||||
let mut lowest = 1000;
|
||||
let mut result: Vec<(Direction, FluidState)> = Vec::new();
|
||||
let mut context: Option<SpreadContext> = None;
|
||||
for direction in HORIZONTAL {
|
||||
let test_pos = rel(pos, direction);
|
||||
let test_state = self.state(test_pos);
|
||||
let test_fluid = self.b.fluid(test_state);
|
||||
if !self.can_maybe_pass_through(kind, state, direction, test_state, test_fluid) {
|
||||
continue;
|
||||
}
|
||||
let new_fluid = self.new_liquid(kind, test_pos, test_state);
|
||||
if !self.can_hold_specific_fluid(test_state, new_fluid.kind()) {
|
||||
continue;
|
||||
}
|
||||
let context = context.get_or_insert_with(|| SpreadContext {
|
||||
origin: pos,
|
||||
states: HashMap::new(),
|
||||
holes: HashMap::new(),
|
||||
});
|
||||
let distance = if self.context_is_hole(kind, context, test_pos) {
|
||||
0
|
||||
} else {
|
||||
self.slope_distance(kind, test_pos, 1, direction.opposite(), test_state, context)
|
||||
};
|
||||
if distance < lowest {
|
||||
result.clear();
|
||||
}
|
||||
if distance <= lowest {
|
||||
if self.can_be_replaced_with(test_fluid, test_pos, new_fluid.kind(), direction) {
|
||||
result.push((direction, new_fluid));
|
||||
}
|
||||
lowest = distance;
|
||||
}
|
||||
}
|
||||
result.sort_by_key(|(direction, _)| *direction as u8);
|
||||
result
|
||||
}
|
||||
fn source_neighbour_count(&self, kind: FluidKind, pos: BlockPos) -> i32 {
|
||||
HORIZONTAL.into_iter().filter(|d| self.is_source_of(kind, self.fluid_at(rel(pos, *d)))).count() as i32
|
||||
}
|
||||
/// `FlowingFluid.spreadTo` (with LavaFluid's water-to-stone override).
|
||||
fn spread_to(&mut self, kind: FluidKind, pos: BlockPos, state: BlockStateId, direction: Direction, target: FluidState) {
|
||||
let b = self.b;
|
||||
if !is_water(kind) && direction == Direction::Down && b.is_water_fluid(self.fluid_at(pos).kind()) {
|
||||
if b.instance_of(state, Class::Liquid) {
|
||||
self.set_block_and_update(pos, b.default_of(b.named.stone));
|
||||
}
|
||||
self.level_event(1501, pos, 0);
|
||||
return;
|
||||
}
|
||||
if self.is_liquid_container(state) {
|
||||
self.place_liquid(pos, state, target);
|
||||
} else {
|
||||
if !b.is_air(state) {
|
||||
if is_water(kind) {
|
||||
self.drop_resources(state, pos);
|
||||
} else {
|
||||
self.level_event(1501, pos, 0);
|
||||
}
|
||||
}
|
||||
let legacy = b.legacy_block(target);
|
||||
self.set_block_and_update(pos, legacy);
|
||||
}
|
||||
}
|
||||
fn spread_to_sides(&mut self, kind: FluidKind, pos: BlockPos, fluid: FluidState, state: BlockStateId) {
|
||||
let mut neighbour = i32::from(fluid.amount()) - self.drop_off(kind);
|
||||
if fluid.falling() {
|
||||
neighbour = 7;
|
||||
}
|
||||
if neighbour > 0 {
|
||||
for (direction, new_fluid) in self.spread_targets(kind, pos, state) {
|
||||
let neighbour_pos = rel(pos, direction);
|
||||
let neighbour_state = self.state(neighbour_pos);
|
||||
self.spread_to(kind, neighbour_pos, neighbour_state, direction, new_fluid);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `FlowingFluid.spread`.
|
||||
fn spread(&mut self, kind: FluidKind, pos: BlockPos, state: BlockStateId, fluid: FluidState) {
|
||||
if fluid.kind() == FluidKind::Empty {
|
||||
return;
|
||||
}
|
||||
let below_pos = below(pos);
|
||||
let below_state = self.state(below_pos);
|
||||
let below_fluid = self.b.fluid(below_state);
|
||||
if self.can_maybe_pass_through(kind, state, Direction::Down, below_state, below_fluid) {
|
||||
let new_below = self.new_liquid(kind, below_pos, below_state);
|
||||
if self.can_be_replaced_with(below_fluid, below_pos, new_below.kind(), Direction::Down)
|
||||
&& self.can_hold_specific_fluid(below_state, new_below.kind())
|
||||
{
|
||||
self.spread_to(kind, below_pos, below_state, Direction::Down, new_below);
|
||||
if self.source_neighbour_count(kind, pos) >= 3 {
|
||||
self.spread_to_sides(kind, pos, fluid, state);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
if fluid.is_source() || !self.is_water_hole(kind, state, below_state) {
|
||||
self.spread_to_sides(kind, pos, fluid, state);
|
||||
}
|
||||
}
|
||||
/// `FlowingFluid.tick` for the fluid at `pos`.
|
||||
pub(crate) fn fluid_tick(&mut self, pos: BlockPos) {
|
||||
let mut state = self.state(pos);
|
||||
let mut fluid = self.b.fluid(state);
|
||||
let kind = fluid.kind();
|
||||
if kind == FluidKind::Empty {
|
||||
return;
|
||||
}
|
||||
if !fluid.is_source() {
|
||||
let new_fluid = self.new_liquid(kind, pos, state);
|
||||
let delay = self.spread_delay(kind, pos, fluid, new_fluid);
|
||||
if new_fluid.kind() == FluidKind::Empty {
|
||||
fluid = new_fluid;
|
||||
state = self.b.air();
|
||||
self.set_block_and_update(pos, state);
|
||||
} else if new_fluid != fluid {
|
||||
fluid = new_fluid;
|
||||
state = self.b.legacy_block(fluid);
|
||||
self.set_block_and_update(pos, state);
|
||||
self.schedule_fluid_tick(pos, fluid.kind(), delay);
|
||||
}
|
||||
}
|
||||
self.spread(kind, pos, state, fluid);
|
||||
}
|
||||
fn spread_delay(&mut self, kind: FluidKind, pos: BlockPos, old: FluidState, new: FluidState) -> i32 {
|
||||
let delay = self.fluid_tick_delay(kind);
|
||||
if is_water(kind) {
|
||||
return delay;
|
||||
}
|
||||
if old.kind() != FluidKind::Empty
|
||||
&& new.kind() != FluidKind::Empty
|
||||
&& !old.falling()
|
||||
&& !new.falling()
|
||||
&& self.fluid_height(new, pos) > self.fluid_height(old, pos)
|
||||
&& self.random().next_int_bound(4).unwrap_or(0) != 0
|
||||
{
|
||||
delay * 4
|
||||
} else {
|
||||
delay
|
||||
}
|
||||
}
|
||||
|
||||
// ---- LiquidBlock ----
|
||||
|
||||
/// `LiquidBlock.shouldSpreadLiquid`, including lava's conversions.
|
||||
pub(crate) fn should_spread_liquid(&mut self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
let b = self.b;
|
||||
if !b.is_lava_fluid(b.fluid(state).kind()) {
|
||||
return true;
|
||||
}
|
||||
let over_soul_soil = b.is_block(self.below_state(pos), b.named.soul_soil);
|
||||
for direction in POSSIBLE_FLOW_DIRECTIONS {
|
||||
let neighbour = rel(pos, direction.opposite());
|
||||
if b.is_water_fluid(self.fluid_at(neighbour).kind()) {
|
||||
let block = if self.fluid_at(pos).is_source() { b.named.obsidian } else { b.named.cobblestone };
|
||||
self.set_block_and_update(pos, b.default_of(block));
|
||||
self.level_event(1501, pos, 0);
|
||||
return false;
|
||||
}
|
||||
if over_soul_soil && b.is_block(self.state(neighbour), b.named.blue_ice) {
|
||||
self.set_block_and_update(pos, b.default_of(b.named.basalt));
|
||||
self.level_event(1501, pos, 0);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
/// `LiquidBlock.onPlace` / `neighborChanged`.
|
||||
pub(crate) fn liquid_block_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
if self.should_spread_liquid(pos, state) {
|
||||
let kind = self.b.fluid(state).kind();
|
||||
let delay = self.fluid_tick_delay(kind);
|
||||
self.schedule_fluid_tick(pos, kind, delay);
|
||||
}
|
||||
if self.should_bubble_column_occupy(state) {
|
||||
let below_state = self.below_state(pos);
|
||||
let block = self.b.block(state);
|
||||
self.try_schedule_bubble_column(pos, below_state, block);
|
||||
}
|
||||
}
|
||||
|
||||
/// `LavaFluid.randomTick`.
|
||||
pub(crate) fn lava_random_tick(&mut self, pos: BlockPos) {
|
||||
if !self.level.can_spread_fire_around(pos) {
|
||||
return;
|
||||
}
|
||||
let passes = self.random().next_int_bound(3).unwrap_or(0);
|
||||
if passes > 0 {
|
||||
let mut test = pos;
|
||||
for _ in 0..passes {
|
||||
let dx = self.random().next_int_bound(3).unwrap_or(0) - 1;
|
||||
let dz = self.random().next_int_bound(3).unwrap_or(0) - 1;
|
||||
test = test.offset(dx, 1, dz);
|
||||
if !self.level.is_loaded(test) {
|
||||
return;
|
||||
}
|
||||
let state = self.state(test);
|
||||
if self.b.is_air(state) {
|
||||
if Direction::ALL.into_iter().any(|d| self.lava_flammable(rel(test, d))) {
|
||||
let fire = self.fire_state_for(test);
|
||||
self.set_block_and_update(test, fire);
|
||||
return;
|
||||
}
|
||||
} else if self.b.tag("minecraft:blocks_lava_fire_spread").is_some_and(|tag| self.b.in_set(state, tag)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _ in 0..3 {
|
||||
let dx = self.random().next_int_bound(3).unwrap_or(0) - 1;
|
||||
let dz = self.random().next_int_bound(3).unwrap_or(0) - 1;
|
||||
let test = pos.offset(dx, 0, dz);
|
||||
if !self.level.is_loaded(test) {
|
||||
return;
|
||||
}
|
||||
if self.is_empty_block(above(test)) && self.lava_flammable(test) {
|
||||
let fire = self.fire_state_for(test);
|
||||
self.set_block_and_update(above(test), fire);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn lava_flammable(&self, pos: BlockPos) -> bool {
|
||||
if self.is_inside_build_height(pos.y) && !self.level.is_loaded(pos) {
|
||||
return false;
|
||||
}
|
||||
self.b.has_flag(self.state(pos), super::data::state_flags::IGNITED_BY_LAVA)
|
||||
}
|
||||
}
|
||||
|
||||
/// `Shapes.mergedFaceOccludes(shape, occluder, direction)`.
|
||||
pub(crate) fn merged_face_occludes(shape: &VoxelShape, occluder: &VoxelShape, direction: Direction) -> bool {
|
||||
let block = VoxelShape::block();
|
||||
if shape.same_identity(&block) || occluder.same_identity(&block) {
|
||||
return true;
|
||||
}
|
||||
let positive = matches!(direction, Direction::Up | Direction::South | Direction::East);
|
||||
let axis = match direction {
|
||||
Direction::West | Direction::East => Axis::X,
|
||||
Direction::Down | Direction::Up => Axis::Y,
|
||||
_ => Axis::Z,
|
||||
};
|
||||
let (first, second) = if positive { (shape, occluder) } else { (occluder, shape) };
|
||||
let max_face = match axis {
|
||||
Axis::X => Direction::East,
|
||||
Axis::Y => Direction::Up,
|
||||
Axis::Z => Direction::South,
|
||||
};
|
||||
let empty = VoxelShape::empty();
|
||||
// SliceShape(first, axis, last layer) / SliceShape(second, axis, 0): the face
|
||||
// shapes sample the same boundary layers when the shapes touch the face.
|
||||
let first_slice = if !first.is_empty() && (first.max(axis) - 1.0).abs() <= 1.0e-7 {
|
||||
first.face_shape(max_face).unwrap_or_else(|_| empty.clone())
|
||||
} else {
|
||||
empty.clone()
|
||||
};
|
||||
let second_slice = if !second.is_empty() && second.min(axis).abs() <= 1.0e-7 {
|
||||
second.face_shape(max_face.opposite()).unwrap_or_else(|_| empty.clone())
|
||||
} else {
|
||||
empty.clone()
|
||||
};
|
||||
let union = match first_slice.join(&second_slice, ShapeOp::OR) {
|
||||
Ok(shape) => shape,
|
||||
Err(_) => return false,
|
||||
};
|
||||
!block.join_is_not_empty(&union, ShapeOp::ONLY_FIRST).unwrap_or(true)
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
//! `neighborChanged`, `onPlace`, `affectNeighborsAfterRemoval` and
|
||||
//! `updateIndirectNeighbourShapes` for the overriding vanilla classes.
|
||||
use super::{
|
||||
LevelAccess, Sim,
|
||||
classes::Class,
|
||||
states::BlockId,
|
||||
update::{HORIZONTAL, below, rel},
|
||||
};
|
||||
use crate::registry::BlockStateId;
|
||||
use horizon_core::{geometry::Direction, position::BlockPos, random::RandomSource};
|
||||
|
||||
impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
/// `BlockState.handleNeighborChanged`.
|
||||
pub fn neighbor_changed(&mut self, state: BlockStateId, pos: BlockPos, changed: BlockId, moved: bool) {
|
||||
let b = self.b;
|
||||
let block = b.block(state);
|
||||
let chain = b.data(state).chain.clone();
|
||||
for class in chain {
|
||||
match class {
|
||||
Class::AbstractSkull => self.skull_neighbor_changed(pos, state),
|
||||
Class::BaseRail => self.rail_neighbor_changed(pos, state, changed, moved),
|
||||
Class::Bell => self.bell_neighbor_changed(pos, state),
|
||||
Class::BigDripleaf => {
|
||||
if self.has_neighbor_signal(pos) {
|
||||
self.big_dripleaf_reset_tilt(pos, state);
|
||||
}
|
||||
}
|
||||
Class::CopperBulb => self.copper_bulb_check(pos, state),
|
||||
Class::Crafter => self.crafter_neighbor_changed(pos, state),
|
||||
Class::Diode => self.diode_neighbor_changed(pos, state),
|
||||
Class::Dispenser => self.dispenser_neighbor_changed(pos, state),
|
||||
Class::Door => self.door_neighbor_changed(pos, state, changed),
|
||||
Class::FenceGate => self.gate_neighbor_changed(pos, state),
|
||||
Class::FrostedIce => {
|
||||
if b.block_data(changed).is(Class::FrostedIce) && self.frosted_ice_fewer_neighbours_than(pos, 2) {
|
||||
self.frosted_ice_melt(pos, state);
|
||||
}
|
||||
}
|
||||
Class::Hopper => self.hopper_check_powered(pos, state),
|
||||
Class::Liquid => self.liquid_block_changed(pos, state),
|
||||
Class::Note => self.note_neighbor_changed(pos, state),
|
||||
Class::RedstoneLamp => self.lamp_neighbor_changed(pos, state),
|
||||
Class::RedstoneTorch => self.torch_neighbor_changed(pos, state),
|
||||
Class::RedstoneWire => {
|
||||
if self.can_survive(state, pos) {
|
||||
self.wire_update_power(pos, state);
|
||||
} else {
|
||||
self.drop_resources(state, pos);
|
||||
self.remove_block(pos, false);
|
||||
}
|
||||
}
|
||||
Class::Sponge => self.sponge_try_absorb(pos),
|
||||
Class::Tnt => self.tnt_check(pos),
|
||||
Class::TrapDoor => self.trapdoor_neighbor_changed(pos, state),
|
||||
_ => continue,
|
||||
}
|
||||
let _ = block;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// `BlockState.onPlace`.
|
||||
pub fn on_place(&mut self, state: BlockStateId, pos: BlockPos, old: BlockStateId, moved: bool) {
|
||||
let b = self.b;
|
||||
let block = b.block(state);
|
||||
let same = b.block(old) == block;
|
||||
let chain = b.data(state).chain.clone();
|
||||
for class in chain {
|
||||
match class {
|
||||
Class::Fire => {
|
||||
self.base_fire_on_place(pos, state, same);
|
||||
let delay = 30 + self.random().next_int_bound(10).unwrap_or(0);
|
||||
self.schedule_tick(pos, block, delay);
|
||||
}
|
||||
Class::BaseFire => self.base_fire_on_place(pos, state, same),
|
||||
Class::DetectorRail | Class::BaseRail => {
|
||||
if !same {
|
||||
self.rail_update_state(pos, state, moved);
|
||||
}
|
||||
}
|
||||
Class::Brushable => self.schedule_tick(pos, block, 2),
|
||||
Class::Composter => {
|
||||
if b.table.int(state, b.props.level) == 7 {
|
||||
self.schedule_tick(pos, block, 20);
|
||||
}
|
||||
}
|
||||
Class::CopperBulb => {
|
||||
if !same {
|
||||
self.copper_bulb_check(pos, state);
|
||||
}
|
||||
}
|
||||
Class::CoralFan | Class::CoralPlant | Class::CoralWallFan => self.coral_try_schedule_die_tick(state, pos),
|
||||
Class::Diode => self.diode_update_neighbors_in_front(pos, state),
|
||||
Class::FallingBlock => {
|
||||
let delay = if b.data(state).is(Class::DragonEgg) { 5 } else { 2 };
|
||||
self.schedule_tick(pos, block, delay);
|
||||
}
|
||||
Class::Frogspawn => {
|
||||
let delay = self.random().next_int_between(3600, 12000).unwrap_or(3600);
|
||||
self.schedule_tick(pos, block, delay);
|
||||
}
|
||||
Class::FrostedIce => {
|
||||
let delay = self.random().next_int_between_inclusive(60, 120).unwrap_or(60);
|
||||
self.schedule_tick(pos, block, delay);
|
||||
}
|
||||
Class::Hopper => {
|
||||
if !same {
|
||||
self.hopper_check_powered(pos, state);
|
||||
}
|
||||
}
|
||||
Class::LightningRod => {
|
||||
if !same && b.table.bool(state, b.props.powered) && !self.ticks.has_block_tick(pos, block) {
|
||||
self.schedule_tick(pos, block, 8);
|
||||
}
|
||||
}
|
||||
Class::Liquid => self.liquid_block_changed(pos, state),
|
||||
Class::Observer => {
|
||||
if !same && b.table.bool(state, b.props.powered) && !self.ticks.has_block_tick(pos, block) {
|
||||
let next = b.table.set_bool(state, b.props.powered, false);
|
||||
self.set_block(pos, next, 18);
|
||||
self.observer_update_in_front(pos, next);
|
||||
}
|
||||
}
|
||||
Class::RedstoneTorch => self.torch_notify_neighbors(pos, state),
|
||||
Class::RedstoneWire => {
|
||||
if !same {
|
||||
self.wire_update_power(pos, state);
|
||||
for direction in [Direction::Up, Direction::Down] {
|
||||
self.update_neighbors_at(rel(pos, direction), block);
|
||||
}
|
||||
self.wire_update_neighbors_of_neighboring_wires(pos);
|
||||
}
|
||||
}
|
||||
Class::Scaffolding => self.schedule_tick(pos, block, 1),
|
||||
Class::SculkSensor => {
|
||||
if !same && b.table.int(state, b.props.power) > 0 && !self.ticks.has_block_tick(pos, block) {
|
||||
self.set_block(pos, b.table.set_int(state, b.props.power, 0), 18);
|
||||
}
|
||||
}
|
||||
Class::Sponge => {
|
||||
if !same {
|
||||
self.sponge_try_absorb(pos);
|
||||
}
|
||||
}
|
||||
Class::WetSponge => {
|
||||
if self.level.dimension() == super::Dimension::Nether {
|
||||
self.set_block_and_update(pos, b.default_of(b.named.sponge));
|
||||
self.level_event(2009, pos, 0);
|
||||
let pitch = (1.0 + self.random().next_float() * 0.2) * 0.7;
|
||||
self.emit(super::LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.wet_sponge.dries".into(),
|
||||
volume: 1.0,
|
||||
pitch,
|
||||
});
|
||||
}
|
||||
}
|
||||
Class::Target => {
|
||||
if !same && b.table.int(state, b.props.power) > 0 && !self.ticks.has_block_tick(pos, block) {
|
||||
self.set_block(pos, b.table.set_int(state, b.props.power, 0), 18);
|
||||
}
|
||||
}
|
||||
Class::Tnt => {
|
||||
if !same {
|
||||
self.tnt_check(pos);
|
||||
}
|
||||
}
|
||||
Class::TripWire => {
|
||||
if !same {
|
||||
self.tripwire_update_source(pos, state);
|
||||
}
|
||||
}
|
||||
Class::TurtleEgg => {
|
||||
if b.in_set(self.below_state(pos), &b.tags.sand) {
|
||||
self.level_event(2012, pos, 15);
|
||||
}
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// `BlockState.affectNeighborsAfterRemoval`.
|
||||
pub fn affect_neighbors_after_removal(&mut self, state: BlockStateId, pos: BlockPos, moved: bool) {
|
||||
let b = self.b;
|
||||
let block = b.block(state);
|
||||
let chain = b.data(state).chain.clone();
|
||||
for class in chain {
|
||||
match class {
|
||||
Class::AbstractFurnace
|
||||
| Class::Barrel
|
||||
| Class::BrewingStand
|
||||
| Class::Chest
|
||||
| Class::ChiseledBookShelf
|
||||
| Class::Crafter
|
||||
| Class::CreakingHeart
|
||||
| Class::DecoratedPot
|
||||
| Class::Dispenser
|
||||
| Class::Hopper
|
||||
| Class::Jukebox
|
||||
| Class::ShulkerBox
|
||||
| Class::CopperGolemStatue => self.update_neighbour_for_output_signal(pos, block),
|
||||
Class::Shelf => self.update_neighbour_for_output_signal(pos, block),
|
||||
Class::BasePressurePlate => {
|
||||
if !moved && self.plate_signal_for_state(state) > 0 {
|
||||
self.plate_update_neighbours(pos, block);
|
||||
}
|
||||
}
|
||||
Class::BaseRail => {
|
||||
if !moved {
|
||||
if self.rail_shape(state).starts_with("ascending") {
|
||||
self.update_neighbors_at(super::update::above(pos), block);
|
||||
}
|
||||
if !b.data(state).is(Class::Rail) {
|
||||
self.update_neighbors_at(pos, block);
|
||||
self.update_neighbors_at(below(pos), block);
|
||||
}
|
||||
}
|
||||
}
|
||||
Class::Button | Class::Lever => {
|
||||
if !moved && b.table.bool(state, b.props.powered) {
|
||||
self.attached_update_neighbours(pos, state);
|
||||
}
|
||||
}
|
||||
Class::LightningRod => {
|
||||
if b.table.bool(state, b.props.powered) {
|
||||
let facing = b.facing(state);
|
||||
self.update_neighbors_at(rel(pos, facing.opposite()), block);
|
||||
}
|
||||
}
|
||||
Class::Lectern => {
|
||||
if b.table.bool(state, b.props.powered) {
|
||||
self.update_neighbors_at(below(pos), block);
|
||||
}
|
||||
}
|
||||
Class::Diode => {
|
||||
if !moved {
|
||||
self.diode_update_neighbors_in_front(pos, state);
|
||||
}
|
||||
}
|
||||
Class::Observer => {
|
||||
if b.table.bool(state, b.props.powered) && self.ticks.has_block_tick(pos, block) {
|
||||
let off = b.table.set_bool(state, b.props.powered, false);
|
||||
self.observer_update_in_front(pos, off);
|
||||
}
|
||||
}
|
||||
Class::RedstoneTorch => {
|
||||
if !moved {
|
||||
self.torch_notify_neighbors(pos, state);
|
||||
}
|
||||
}
|
||||
Class::RedstoneWire => {
|
||||
if !moved {
|
||||
for direction in Direction::ALL {
|
||||
self.update_neighbors_at(rel(pos, direction), block);
|
||||
}
|
||||
self.wire_update_power(pos, state);
|
||||
self.wire_update_neighbors_of_neighboring_wires(pos);
|
||||
}
|
||||
}
|
||||
Class::SculkSensor => {
|
||||
if b.table.text(state, b.table_phase_key()) == "active" {
|
||||
self.update_neighbors_at(pos, block);
|
||||
self.update_neighbors_at(below(pos), block);
|
||||
}
|
||||
}
|
||||
Class::TripWire => {
|
||||
if !moved {
|
||||
let powered = b.table.set_bool(state, b.props.powered, true);
|
||||
self.tripwire_update_source(pos, powered);
|
||||
}
|
||||
}
|
||||
Class::TripWireHook => {
|
||||
if !moved {
|
||||
self.tripwire_hook_removed(pos, state);
|
||||
}
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// `BlockState.updateIndirectNeighbourShapes` (only redstone wire overrides it).
|
||||
pub fn update_indirect_neighbour_shapes(&mut self, state: BlockStateId, pos: BlockPos, flags: i32, limit: i32) {
|
||||
if self.b.is_block(state, self.b.named.redstone_wire) {
|
||||
self.wire_update_indirect(state, pos, flags, limit);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- misc helpers ----
|
||||
|
||||
/// `BaseFireBlock.onPlace` (portal ignition is not modelled).
|
||||
fn base_fire_on_place(&mut self, pos: BlockPos, state: BlockStateId, same: bool) {
|
||||
if !same && !self.can_survive(state, pos) {
|
||||
self.remove_block(pos, false);
|
||||
}
|
||||
}
|
||||
/// `SpongeBlock.tryAbsorbWater`.
|
||||
pub(crate) fn sponge_try_absorb(&mut self, start: BlockPos) {
|
||||
if self.sponge_remove_water(start) {
|
||||
let b = self.b;
|
||||
self.set_block(start, b.default_of(b.named.wet_sponge), 2);
|
||||
self.emit(super::LevelEvent::Sound {
|
||||
pos: start,
|
||||
sound: "minecraft:block.sponge.absorb".into(),
|
||||
volume: 1.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
fn sponge_remove_water(&mut self, start: BlockPos) -> bool {
|
||||
use std::collections::{HashSet, VecDeque};
|
||||
let b = self.b;
|
||||
let mut queue = VecDeque::from([(start, 0)]);
|
||||
let mut visited = HashSet::new();
|
||||
let mut count = 0;
|
||||
while let Some((pos, depth)) = queue.pop_front() {
|
||||
if !visited.insert(pos) {
|
||||
continue;
|
||||
}
|
||||
let accept = if pos == start {
|
||||
true
|
||||
} else {
|
||||
let state = self.state(pos);
|
||||
if !b.is_water_fluid(self.fluid_at(pos).kind()) {
|
||||
false
|
||||
} else if self.pickup_block(pos, state) {
|
||||
true
|
||||
} else {
|
||||
if b.instance_of(state, Class::Liquid) {
|
||||
self.set_block_and_update(pos, b.air());
|
||||
} else {
|
||||
let n = b.named;
|
||||
let block = b.block(state);
|
||||
if block != n.kelp && block != n.kelp_plant && block != n.seagrass && block != n.tall_seagrass {
|
||||
continue;
|
||||
}
|
||||
self.drop_resources(state, pos);
|
||||
self.set_block_and_update(pos, b.air());
|
||||
}
|
||||
true
|
||||
}
|
||||
};
|
||||
if !accept {
|
||||
continue;
|
||||
}
|
||||
count += 1;
|
||||
if count >= 65 {
|
||||
return count > 1;
|
||||
}
|
||||
if depth < 6 {
|
||||
for direction in Direction::ALL {
|
||||
queue.push_back((rel(pos, direction), depth + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
count > 1
|
||||
}
|
||||
fn frosted_ice_fewer_neighbours_than(&self, pos: BlockPos, limit: i32) -> bool {
|
||||
let mut count = 0;
|
||||
for direction in Direction::ALL {
|
||||
if self.b.data(self.rel_state(pos, direction)).is(Class::FrostedIce) {
|
||||
count += 1;
|
||||
if count >= limit {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
/// `FrostedIceBlock.melt` → `IceBlock.melt`.
|
||||
pub(crate) fn frosted_ice_melt(&mut self, pos: BlockPos, _state: BlockStateId) {
|
||||
self.ice_melt(pos);
|
||||
}
|
||||
/// `IceBlock.melt`.
|
||||
pub(crate) fn ice_melt(&mut self, pos: BlockPos) {
|
||||
let b = self.b;
|
||||
if self.level.dimension() == super::Dimension::Nether {
|
||||
self.remove_block(pos, false);
|
||||
} else {
|
||||
let water = b.default_of(b.named.water);
|
||||
self.set_block_and_update(pos, water);
|
||||
self.neighbor_changed_at(pos, b.named.water);
|
||||
}
|
||||
}
|
||||
fn big_dripleaf_reset_tilt(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
if b.table.text(state, b.props.tilt) != "none" {
|
||||
let next = b.table.set_text(state, b.props.tilt, "none");
|
||||
self.set_block(pos, next, 2);
|
||||
self.emit(super::LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.big_dripleaf.tilt_up".into(),
|
||||
volume: 1.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
self.ticks.blocks.clear_area(horizon_core::ticks::TickArea::new(pos, pos));
|
||||
}
|
||||
}
|
||||
pub(crate) fn horizontal_neighbours(pos: BlockPos) -> [BlockPos; 4] {
|
||||
HORIZONTAL.map(|d| rel(pos, d))
|
||||
}
|
||||
}
|
||||
|
||||
impl super::Behaviours {
|
||||
pub(crate) fn table_phase_key(&self) -> super::states::PropKey {
|
||||
self.props.sculk_sensor_phase
|
||||
}
|
||||
}
|
||||
@@ -233,6 +233,17 @@ pub trait LevelAccess {
|
||||
fn is_unobstructed(&self, _pos: BlockPos, _state: BlockStateId) -> bool {
|
||||
true
|
||||
}
|
||||
/// Comparator input from a block entity (`getAnalogOutputSignal` of
|
||||
/// containers, jukeboxes, lecterns, ...). State-only sources (cake,
|
||||
/// composter, cauldrons, beehives, ...) are evaluated by the library.
|
||||
fn block_entity_analog_signal(&self, _pos: BlockPos, _state: BlockStateId, _direction: Direction) -> i32 {
|
||||
0
|
||||
}
|
||||
/// Weak/strong signal of a block-entity-driven source (trapped chest
|
||||
/// openers); other sources are evaluated by the library.
|
||||
fn block_entity_signal(&self, _pos: BlockPos, _state: BlockStateId, _direction: Direction) -> i32 {
|
||||
0
|
||||
}
|
||||
/// Count of entities intersecting `area` that trigger pressure plates or
|
||||
/// tripwires under `query`.
|
||||
fn count_entities(&self, _area: Aabb, _query: EntityQuery) -> usize {
|
||||
@@ -348,4 +359,17 @@ pub(crate) struct Sim<'a, L: LevelAccess + ?Sized> {
|
||||
pub b: &'a Behaviours,
|
||||
pub level: &'a mut L,
|
||||
pub ticks: &'a mut Ticks,
|
||||
/// `RedstoneWireBlock.shouldSignal` is false while a wire samples its
|
||||
/// neighbours (`getBlockSignal`).
|
||||
pub wire_signal_off: std::cell::Cell<bool>,
|
||||
}
|
||||
impl Behaviours {
|
||||
pub(crate) fn sim<'a, L: LevelAccess + ?Sized>(&'a self, level: &'a mut L, ticks: &'a mut Ticks) -> Sim<'a, L> {
|
||||
Sim {
|
||||
b: self,
|
||||
level,
|
||||
ticks,
|
||||
wire_signal_off: std::cell::Cell::new(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,998 @@
|
||||
//! Redstone: `SignalGetter`, redstone wire (default evaluator), torches,
|
||||
//! diodes (repeaters, comparators), observers, lamps and the power-driven
|
||||
//! responses of doors, trapdoors, gates and other consumers.
|
||||
use super::{
|
||||
LevelAccess, LevelEvent, Sim,
|
||||
classes::Class,
|
||||
data::state_flags,
|
||||
query::{clockwise, counter_clockwise},
|
||||
states::BlockId,
|
||||
update::{HORIZONTAL, above, below, rel},
|
||||
};
|
||||
use crate::registry::BlockStateId;
|
||||
use horizon_core::{geometry::Direction, position::BlockPos, random::RandomSource, ticks::TickPriority};
|
||||
|
||||
const SIGNAL_ORDER: [Direction; 6] = [
|
||||
Direction::Down,
|
||||
Direction::Up,
|
||||
Direction::North,
|
||||
Direction::South,
|
||||
Direction::West,
|
||||
Direction::East,
|
||||
];
|
||||
|
||||
impl<L: LevelAccess + ?Sized> Sim<'_, L> {
|
||||
fn nibble(word: u32, direction: Direction) -> i32 {
|
||||
((word >> (4 * direction as u32)) & 15) as i32
|
||||
}
|
||||
pub(crate) fn is_signal_source(&self, state: BlockStateId) -> bool {
|
||||
if self.b.is_block(state, self.b.named.redstone_wire) {
|
||||
return !self.wire_signal_off.get();
|
||||
}
|
||||
self.b.has_flag(state, state_flags::SIGNAL_SOURCE)
|
||||
}
|
||||
/// `BlockState.getSignal(level, pos, direction)`.
|
||||
pub(crate) fn state_signal(&self, state: BlockStateId, pos: BlockPos, direction: Direction) -> i32 {
|
||||
let b = self.b;
|
||||
if !b.has_flag(state, state_flags::SIGNAL_SOURCE) {
|
||||
return 0;
|
||||
}
|
||||
let data = b.data(state);
|
||||
match data.class {
|
||||
Class::RedstoneWire => self.wire_signal(state, pos, direction),
|
||||
Class::Comparator => {
|
||||
if b.facing(state) == direction && b.table.bool(state, b.props.powered) {
|
||||
self.comparator_output(pos)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
Class::TrappedChest => self.level.block_entity_signal(pos, state, direction),
|
||||
_ => Self::nibble(b.facts(state).weak, direction),
|
||||
}
|
||||
}
|
||||
/// `BlockState.getDirectSignal(level, pos, direction)`.
|
||||
pub(crate) fn state_direct_signal(&self, state: BlockStateId, pos: BlockPos, direction: Direction) -> i32 {
|
||||
let b = self.b;
|
||||
if !b.has_flag(state, state_flags::SIGNAL_SOURCE) {
|
||||
return 0;
|
||||
}
|
||||
match b.data(state).class {
|
||||
Class::RedstoneWire => {
|
||||
if self.wire_signal_off.get() {
|
||||
0
|
||||
} else {
|
||||
self.wire_signal(state, pos, direction)
|
||||
}
|
||||
}
|
||||
Class::Comparator => self.state_signal(state, pos, direction),
|
||||
Class::TrappedChest => {
|
||||
if direction == Direction::Up {
|
||||
self.level.block_entity_signal(pos, state, direction)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
_ => Self::nibble(b.facts(state).strong, direction),
|
||||
}
|
||||
}
|
||||
pub(crate) fn comparator_output(&self, pos: BlockPos) -> i32 {
|
||||
self.ticks.comparator_outputs.get(&pos).copied().unwrap_or(0)
|
||||
}
|
||||
fn wire_signal(&self, state: BlockStateId, pos: BlockPos, direction: Direction) -> i32 {
|
||||
if self.wire_signal_off.get() || direction == Direction::Down {
|
||||
return 0;
|
||||
}
|
||||
let power = self.b.table.int(state, self.b.props.power);
|
||||
if power == 0 {
|
||||
return 0;
|
||||
}
|
||||
if direction != Direction::Up {
|
||||
let connected = self.wire_connection_state(state, pos);
|
||||
if self.b.table.text(connected, self.direction_prop(direction.opposite())) == "none" {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
power
|
||||
}
|
||||
/// `SignalGetter.getDirectSignal(pos, direction)`.
|
||||
pub fn direct_signal(&self, pos: BlockPos, direction: Direction) -> i32 {
|
||||
self.state_direct_signal(self.state(pos), pos, direction)
|
||||
}
|
||||
/// `SignalGetter.getDirectSignalTo`.
|
||||
pub fn direct_signal_to(&self, pos: BlockPos) -> i32 {
|
||||
let mut result = 0;
|
||||
for direction in SIGNAL_ORDER {
|
||||
result = result.max(self.direct_signal(rel(pos, direction), direction));
|
||||
if result >= 15 {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
/// `SignalGetter.getSignal(pos, direction)`.
|
||||
pub fn signal(&self, pos: BlockPos, direction: Direction) -> i32 {
|
||||
let state = self.state(pos);
|
||||
let signal = self.state_signal(state, pos, direction);
|
||||
if self.is_redstone_conductor(state, pos) {
|
||||
signal.max(self.direct_signal_to(pos))
|
||||
} else {
|
||||
signal
|
||||
}
|
||||
}
|
||||
pub fn has_signal(&self, pos: BlockPos, direction: Direction) -> bool {
|
||||
self.signal(pos, direction) > 0
|
||||
}
|
||||
/// `SignalGetter.hasNeighborSignal`.
|
||||
pub fn has_neighbor_signal(&self, pos: BlockPos) -> bool {
|
||||
SIGNAL_ORDER.into_iter().any(|direction| self.signal(rel(pos, direction), direction) > 0)
|
||||
}
|
||||
/// `SignalGetter.getBestNeighborSignal`.
|
||||
pub fn best_neighbor_signal(&self, pos: BlockPos) -> i32 {
|
||||
let mut best = 0;
|
||||
for direction in SIGNAL_ORDER {
|
||||
let signal = self.signal(rel(pos, direction), direction);
|
||||
if signal >= 15 {
|
||||
return 15;
|
||||
}
|
||||
best = best.max(signal);
|
||||
}
|
||||
best
|
||||
}
|
||||
/// `SignalGetter.getControlInputSignal`.
|
||||
fn control_input_signal(&self, pos: BlockPos, direction: Direction, only_diodes: bool) -> i32 {
|
||||
let b = self.b;
|
||||
let state = self.state(pos);
|
||||
if only_diodes {
|
||||
if b.instance_of(state, Class::Diode) { self.direct_signal(pos, direction) } else { 0 }
|
||||
} else if b.is_block(state, b.named.redstone_block) {
|
||||
15
|
||||
} else if b.is_block(state, b.named.redstone_wire) {
|
||||
b.table.int(state, b.props.power)
|
||||
} else if self.is_signal_source(state) {
|
||||
self.direct_signal(pos, direction)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
// ---- redstone wire ----
|
||||
|
||||
fn should_wire_connect_to(&self, state: BlockStateId, direction: Option<Direction>) -> bool {
|
||||
let b = self.b;
|
||||
match b.data(state).class {
|
||||
Class::RedstoneWire => true,
|
||||
Class::Observer => direction == Some(b.facing(state)),
|
||||
Class::Repeater => {
|
||||
let facing = b.facing(state);
|
||||
direction == Some(facing) || direction == Some(facing.opposite())
|
||||
}
|
||||
_ => self.is_signal_source(state) && direction.is_some(),
|
||||
}
|
||||
}
|
||||
fn wire_can_survive_on(&self, state: BlockStateId) -> bool {
|
||||
self.b.is_face_sturdy(state, Direction::Up) || self.b.is_block(state, self.b.named.hopper)
|
||||
}
|
||||
fn wire_connecting_side(&self, pos: BlockPos, direction: Direction, can_connect_up: bool) -> &'static str {
|
||||
let b = self.b;
|
||||
let relative_pos = rel(pos, direction);
|
||||
let relative = self.state(relative_pos);
|
||||
if can_connect_up {
|
||||
let placeable_above = b.instance_of(relative, Class::TrapDoor) || self.wire_can_survive_on(relative);
|
||||
if placeable_above && self.should_wire_connect_to(self.state(above(relative_pos)), None) {
|
||||
return if b.is_face_sturdy(relative, direction.opposite()) { "up" } else { "side" };
|
||||
}
|
||||
}
|
||||
if !self.should_wire_connect_to(relative, Some(direction))
|
||||
&& (self.is_redstone_conductor(relative, relative_pos) || !self.should_wire_connect_to(self.state(below(relative_pos)), None))
|
||||
{
|
||||
"none"
|
||||
} else {
|
||||
"side"
|
||||
}
|
||||
}
|
||||
fn wire_can_connect_up(&self, pos: BlockPos) -> bool {
|
||||
!self.is_redstone_conductor(self.above_state(pos), pos)
|
||||
}
|
||||
fn wire_connected(&self, state: BlockStateId, direction: Direction) -> bool {
|
||||
self.b.table.text(state, self.direction_prop(direction)) != "none"
|
||||
}
|
||||
fn wire_is_cross(&self, state: BlockStateId) -> bool {
|
||||
HORIZONTAL.into_iter().all(|d| self.wire_connected(state, d))
|
||||
}
|
||||
fn wire_is_dot(&self, state: BlockStateId) -> bool {
|
||||
!HORIZONTAL.into_iter().any(|d| self.wire_connected(state, d))
|
||||
}
|
||||
pub(crate) fn wire_cross_state(&self, power: i32) -> BlockStateId {
|
||||
let b = self.b;
|
||||
let mut state = b.default_of(b.named.redstone_wire);
|
||||
for direction in HORIZONTAL {
|
||||
state = b.table.set_text(state, self.direction_prop(direction), "side");
|
||||
}
|
||||
b.table.set_int(state, b.props.power, power)
|
||||
}
|
||||
/// `RedstoneWireBlock.getConnectionState`.
|
||||
pub(crate) fn wire_connection_state(&self, state: BlockStateId, pos: BlockPos) -> BlockStateId {
|
||||
let b = self.b;
|
||||
let table = &b.table;
|
||||
let was_dot = self.wire_is_dot(state);
|
||||
let mut result = table.set_int(b.default_of(b.named.redstone_wire), b.props.power, table.int(state, b.props.power));
|
||||
let can_up = self.wire_can_connect_up(pos);
|
||||
for direction in HORIZONTAL {
|
||||
if !self.wire_connected(result, direction) {
|
||||
let side = self.wire_connecting_side(pos, direction, can_up);
|
||||
result = table.set_text(result, self.direction_prop(direction), side);
|
||||
}
|
||||
}
|
||||
if was_dot && self.wire_is_dot(result) {
|
||||
return result;
|
||||
}
|
||||
let north = self.wire_connected(result, Direction::North);
|
||||
let south = self.wire_connected(result, Direction::South);
|
||||
let east = self.wire_connected(result, Direction::East);
|
||||
let west = self.wire_connected(result, Direction::West);
|
||||
let ns_empty = !north && !south;
|
||||
let ew_empty = !east && !west;
|
||||
if !west && ns_empty {
|
||||
result = table.set_text(result, b.props.west, "side");
|
||||
}
|
||||
if !east && ns_empty {
|
||||
result = table.set_text(result, b.props.east, "side");
|
||||
}
|
||||
if !north && ew_empty {
|
||||
result = table.set_text(result, b.props.north, "side");
|
||||
}
|
||||
if !south && ew_empty {
|
||||
result = table.set_text(result, b.props.south, "side");
|
||||
}
|
||||
result
|
||||
}
|
||||
pub(crate) fn redstone_wire_update_shape(
|
||||
&self,
|
||||
state: BlockStateId,
|
||||
pos: BlockPos,
|
||||
direction: Direction,
|
||||
_npos: BlockPos,
|
||||
neighbour: BlockStateId,
|
||||
) -> BlockStateId {
|
||||
let b = self.b;
|
||||
match direction {
|
||||
Direction::Down => {
|
||||
if self.wire_can_survive_on(neighbour) {
|
||||
state
|
||||
} else {
|
||||
b.air()
|
||||
}
|
||||
}
|
||||
Direction::Up => self.wire_connection_state(state, pos),
|
||||
_ => {
|
||||
let side = self.wire_connecting_side(pos, direction, self.wire_can_connect_up(pos));
|
||||
let key = self.direction_prop(direction);
|
||||
if (side != "none") == self.wire_connected(state, direction) && !self.wire_is_cross(state) {
|
||||
b.table.set_text(state, key, side)
|
||||
} else {
|
||||
let power = b.table.int(state, b.props.power);
|
||||
let cross = b.table.set_text(self.wire_cross_state(power), key, side);
|
||||
self.wire_connection_state(cross, pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `RedstoneWireBlock.updateIndirectNeighbourShapes`.
|
||||
pub(crate) fn wire_update_indirect(&mut self, state: BlockStateId, pos: BlockPos, flags: i32, limit: i32) {
|
||||
let b = self.b;
|
||||
let wire = b.named.redstone_wire;
|
||||
for direction in HORIZONTAL {
|
||||
if b.table.text(state, self.direction_prop(direction)) != "none" && !b.is_block(self.rel_state(pos, direction), wire) {
|
||||
let down = below(rel(pos, direction));
|
||||
if b.is_block(self.state(down), wire) {
|
||||
let neighbour = rel(down, direction.opposite());
|
||||
let neighbour_state = self.state(neighbour);
|
||||
self.neighbor_shape_changed(direction.opposite(), down, neighbour, neighbour_state, flags, limit);
|
||||
}
|
||||
let up = above(rel(pos, direction));
|
||||
if b.is_block(self.state(up), wire) {
|
||||
let neighbour = rel(up, direction.opposite());
|
||||
let neighbour_state = self.state(neighbour);
|
||||
self.neighbor_shape_changed(direction.opposite(), up, neighbour, neighbour_state, flags, limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fn wire_block_signal(&self, pos: BlockPos) -> i32 {
|
||||
self.wire_signal_off.set(true);
|
||||
let signal = self.best_neighbor_signal(pos);
|
||||
self.wire_signal_off.set(false);
|
||||
signal
|
||||
}
|
||||
fn wire_power_of(&self, state: BlockStateId) -> i32 {
|
||||
if self.b.is_block(state, self.b.named.redstone_wire) {
|
||||
self.b.table.int(state, self.b.props.power)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
fn wire_incoming_signal(&self, pos: BlockPos) -> i32 {
|
||||
let mut signal = 0;
|
||||
for direction in HORIZONTAL {
|
||||
let neighbour_pos = rel(pos, direction);
|
||||
let neighbour = self.state(neighbour_pos);
|
||||
signal = signal.max(self.wire_power_of(neighbour));
|
||||
let above_pos = above(pos);
|
||||
if self.is_redstone_conductor(neighbour, neighbour_pos) && !self.is_redstone_conductor(self.state(above_pos), above_pos) {
|
||||
signal = signal.max(self.wire_power_of(self.state(above(neighbour_pos))));
|
||||
} else if !self.is_redstone_conductor(neighbour, neighbour_pos) {
|
||||
signal = signal.max(self.wire_power_of(self.state(below(neighbour_pos))));
|
||||
}
|
||||
}
|
||||
(signal - 1).max(0)
|
||||
}
|
||||
/// `DefaultRedstoneWireEvaluator.updatePowerStrength`.
|
||||
pub(crate) fn wire_update_power(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let block_signal = self.wire_block_signal(pos);
|
||||
let target = if block_signal == 15 {
|
||||
15
|
||||
} else {
|
||||
block_signal.max(self.wire_incoming_signal(pos))
|
||||
};
|
||||
if self.b.table.int(state, self.b.props.power) != target {
|
||||
if self.state(pos) == state {
|
||||
let next = self.b.table.set_int(state, self.b.props.power, target);
|
||||
self.set_block(pos, next, 2);
|
||||
}
|
||||
// Vanilla iterates a HashSet of the position and its six
|
||||
// neighbours; BlockPos hash order is reproduced below.
|
||||
let wire = self.b.named.redstone_wire;
|
||||
let mut targets: Vec<BlockPos> = std::iter::once(pos).chain(Direction::ALL.map(|d| rel(pos, d))).collect();
|
||||
java_hash_set_order(&mut targets);
|
||||
for target_pos in targets {
|
||||
self.update_neighbors_at(target_pos, wire);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn wire_check_corner(&mut self, pos: BlockPos) {
|
||||
let wire = self.b.named.redstone_wire;
|
||||
if self.b.is_block(self.state(pos), wire) {
|
||||
self.update_neighbors_at(pos, wire);
|
||||
for direction in Direction::ALL {
|
||||
self.update_neighbors_at(rel(pos, direction), wire);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn wire_update_neighbors_of_neighboring_wires(&mut self, pos: BlockPos) {
|
||||
for direction in HORIZONTAL {
|
||||
self.wire_check_corner(rel(pos, direction));
|
||||
}
|
||||
for direction in HORIZONTAL {
|
||||
let target = rel(pos, direction);
|
||||
if self.is_redstone_conductor(self.state(target), target) {
|
||||
self.wire_check_corner(above(target));
|
||||
} else {
|
||||
self.wire_check_corner(below(target));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- diodes ----
|
||||
|
||||
pub(crate) fn diode_delay(&self, state: BlockStateId) -> i32 {
|
||||
if self.b.instance_of(state, Class::Repeater) {
|
||||
self.b.table.int(state, self.b.props.delay) * 2
|
||||
} else {
|
||||
2
|
||||
}
|
||||
}
|
||||
pub(crate) fn diode_is_locked(&self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
self.b.instance_of(state, Class::Repeater) && self.diode_alternate_signal(pos, state) > 0
|
||||
}
|
||||
fn diode_alternate_signal(&self, pos: BlockPos, state: BlockStateId) -> i32 {
|
||||
let facing = self.b.facing(state);
|
||||
let only_diodes = self.b.instance_of(state, Class::Repeater);
|
||||
let cw = clockwise(facing);
|
||||
let ccw = counter_clockwise(facing);
|
||||
self.control_input_signal(rel(pos, cw), cw, only_diodes)
|
||||
.max(self.control_input_signal(rel(pos, ccw), ccw, only_diodes))
|
||||
}
|
||||
fn diode_input_signal(&self, pos: BlockPos, state: BlockStateId) -> i32 {
|
||||
let b = self.b;
|
||||
let facing = b.facing(state);
|
||||
let target = rel(pos, facing);
|
||||
let input = self.signal(target, facing);
|
||||
let mut result = if input >= 15 {
|
||||
input
|
||||
} else {
|
||||
let target_state = self.state(target);
|
||||
input.max(self.wire_power_of(target_state))
|
||||
};
|
||||
if b.instance_of(state, Class::Comparator) {
|
||||
let target_state = self.state(target);
|
||||
if b.has_flag(target_state, state_flags::ANALOG_OUTPUT) {
|
||||
result = self.analog_output(target_state, target, facing.opposite());
|
||||
} else if result < 15 && self.is_redstone_conductor(target_state, target) {
|
||||
let further = rel(target, facing);
|
||||
let further_state = self.state(further);
|
||||
if b.has_flag(further_state, state_flags::ANALOG_OUTPUT) {
|
||||
result = self.analog_output(further_state, further, facing.opposite());
|
||||
}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
fn diode_should_turn_on(&self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
if self.b.instance_of(state, Class::Comparator) {
|
||||
let input = self.diode_input_signal(pos, state);
|
||||
if input == 0 {
|
||||
return false;
|
||||
}
|
||||
let side = self.diode_alternate_signal(pos, state);
|
||||
input > side || (input == side && self.b.table.text(state, self.b.props.mode) == "compare")
|
||||
} else {
|
||||
self.diode_input_signal(pos, state) > 0
|
||||
}
|
||||
}
|
||||
fn diode_should_prioritize(&self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
let direction = self.b.facing(state).opposite();
|
||||
let opposite = self.rel_state(pos, direction);
|
||||
self.b.instance_of(opposite, Class::Diode) && self.b.facing(opposite) != direction
|
||||
}
|
||||
pub(crate) fn diode_update_neighbors_in_front(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let direction = self.b.facing(state);
|
||||
let opposite_pos = rel(pos, direction.opposite());
|
||||
let block = self.b.block(state);
|
||||
self.neighbor_changed_at(opposite_pos, block);
|
||||
self.update_neighbors_at_except(opposite_pos, block, direction);
|
||||
}
|
||||
fn comparator_calculate_output(&self, pos: BlockPos, state: BlockStateId) -> i32 {
|
||||
let input = self.diode_input_signal(pos, state);
|
||||
if input == 0 {
|
||||
return 0;
|
||||
}
|
||||
let alternate = self.diode_alternate_signal(pos, state);
|
||||
if alternate > input {
|
||||
0
|
||||
} else if self.b.table.text(state, self.b.props.mode) == "subtract" {
|
||||
input - alternate
|
||||
} else {
|
||||
input
|
||||
}
|
||||
}
|
||||
/// `DiodeBlock.checkTickOnNeighbor` (and the comparator override).
|
||||
pub(crate) fn diode_check_tick_on_neighbor(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let block = self.b.block(state);
|
||||
if self.b.instance_of(state, Class::Comparator) {
|
||||
if !self.ticks.will_block_tick_this_tick(pos, block) {
|
||||
let output = self.comparator_calculate_output(pos, state);
|
||||
let old = self.comparator_output(pos);
|
||||
if output != old || self.b.table.bool(state, self.b.props.powered) != self.diode_should_turn_on(pos, state) {
|
||||
let priority = if self.diode_should_prioritize(pos, state) { TickPriority::High } else { TickPriority::Normal };
|
||||
self.schedule_tick_priority(pos, block, 2, priority);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if self.diode_is_locked(pos, state) {
|
||||
return;
|
||||
}
|
||||
let on = self.b.table.bool(state, self.b.props.powered);
|
||||
let should = self.diode_should_turn_on(pos, state);
|
||||
if on != should && !self.ticks.will_block_tick_this_tick(pos, block) {
|
||||
let priority = if self.diode_should_prioritize(pos, state) {
|
||||
TickPriority::ExtremelyHigh
|
||||
} else if on {
|
||||
TickPriority::VeryHigh
|
||||
} else {
|
||||
TickPriority::High
|
||||
};
|
||||
let delay = self.diode_delay(state);
|
||||
self.schedule_tick_priority(pos, block, delay, priority);
|
||||
}
|
||||
}
|
||||
/// `DiodeBlock.tick` / `ComparatorBlock.tick`.
|
||||
pub(crate) fn diode_tick(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
if b.instance_of(state, Class::Comparator) {
|
||||
self.comparator_refresh(pos, state);
|
||||
return;
|
||||
}
|
||||
if self.diode_is_locked(pos, state) {
|
||||
return;
|
||||
}
|
||||
let on = b.table.bool(state, b.props.powered);
|
||||
let should = self.diode_should_turn_on(pos, state);
|
||||
if on && !should {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.powered, false), 2);
|
||||
} else if !on {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.powered, true), 2);
|
||||
if !should {
|
||||
let delay = self.diode_delay(state);
|
||||
self.schedule_tick_priority(pos, b.block(state), delay, TickPriority::VeryHigh);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn comparator_refresh(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let output = self.comparator_calculate_output(pos, state);
|
||||
let old = self.ticks.comparator_outputs.insert(pos, output).unwrap_or(0);
|
||||
if old != output || b.table.text(state, b.props.mode) == "compare" {
|
||||
let should = self.diode_should_turn_on(pos, state);
|
||||
let on = b.table.bool(state, b.props.powered);
|
||||
if on && !should {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.powered, false), 2);
|
||||
} else if !on && should {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.powered, true), 2);
|
||||
}
|
||||
self.diode_update_neighbors_in_front(pos, state);
|
||||
}
|
||||
}
|
||||
/// `DiodeBlock.neighborChanged`.
|
||||
pub(crate) fn diode_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let block = self.b.block(state);
|
||||
if !self.b.is_block(self.state(pos), block) {
|
||||
return;
|
||||
}
|
||||
if self.can_survive(state, pos) {
|
||||
self.diode_check_tick_on_neighbor(pos, state);
|
||||
} else {
|
||||
self.drop_resources(state, pos);
|
||||
self.remove_block(pos, false);
|
||||
for direction in Direction::ALL {
|
||||
self.update_neighbors_at(rel(pos, direction), block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `getAnalogOutputSignal` for state-only sources; others ask the caller.
|
||||
pub(crate) fn analog_output(&self, state: BlockStateId, pos: BlockPos, direction: Direction) -> i32 {
|
||||
let b = self.b;
|
||||
let p = b.props;
|
||||
match b.data(state).class {
|
||||
Class::Beehive => b.table.int(state, p.honey_level),
|
||||
Class::Cake => (7 - b.table.int(state, p.bites)) * 2,
|
||||
Class::CandleCake => 14,
|
||||
Class::Composter | Class::LayeredCauldron => b.table.int(state, p.level),
|
||||
Class::CopperBulb => {
|
||||
if b.table.bool(self.state(pos), p.lit) {
|
||||
15
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
Class::EndPortalFrame => {
|
||||
if b.table.bool(state, p.eye) {
|
||||
15
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
Class::LavaCauldron => 3,
|
||||
Class::RespawnAnchor => {
|
||||
let charges = b.table.int(state, p.charges);
|
||||
((charges - 0) as f32 / 4.0 * 15.0).floor() as i32
|
||||
}
|
||||
Class::CopperGolemStatue => {
|
||||
let pose = b.table.text(state, p.copper_golem_pose);
|
||||
["standing", "sitting", "running", "star"].iter().position(|p| *p == pose).map_or(1, |i| i as i32 + 1)
|
||||
}
|
||||
_ => self.level.block_entity_analog_signal(pos, state, direction),
|
||||
}
|
||||
}
|
||||
|
||||
// ---- torches ----
|
||||
|
||||
fn torch_has_neighbor_signal(&self, pos: BlockPos, state: BlockStateId) -> bool {
|
||||
if self.b.instance_of(state, Class::RedstoneWallTorch) {
|
||||
let opposite = self.b.facing(state).opposite();
|
||||
self.has_signal(rel(pos, opposite), opposite)
|
||||
} else {
|
||||
self.has_signal(below(pos), Direction::Down)
|
||||
}
|
||||
}
|
||||
pub(crate) fn torch_notify_neighbors(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let block = self.b.block(state);
|
||||
for direction in Direction::ALL {
|
||||
self.update_neighbors_at(rel(pos, direction), block);
|
||||
}
|
||||
}
|
||||
pub(crate) fn torch_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let block = self.b.block(state);
|
||||
if self.b.table.bool(state, self.b.props.lit) == self.torch_has_neighbor_signal(pos, state)
|
||||
&& !self.ticks.will_block_tick_this_tick(pos, block)
|
||||
{
|
||||
self.schedule_tick(pos, block, 2);
|
||||
}
|
||||
}
|
||||
fn torch_toggled_too_frequently(&mut self, pos: BlockPos, add: bool) -> bool {
|
||||
if add {
|
||||
let time = self.level.game_time();
|
||||
self.ticks.torch_toggles.push((pos, time));
|
||||
}
|
||||
self.ticks.torch_toggles.iter().filter(|(p, _)| *p == pos).take(8).count() >= 8
|
||||
}
|
||||
pub(crate) fn torch_tick(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let signal = self.torch_has_neighbor_signal(pos, state);
|
||||
let time = self.level.game_time();
|
||||
while self.ticks.torch_toggles.first().is_some_and(|(_, when)| time - when > 60) {
|
||||
self.ticks.torch_toggles.remove(0);
|
||||
}
|
||||
if b.table.bool(state, b.props.lit) {
|
||||
if signal {
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.lit, false));
|
||||
if self.torch_toggled_too_frequently(pos, true) {
|
||||
self.level_event(1502, pos, 0);
|
||||
let block = b.block(self.state(pos));
|
||||
self.schedule_tick(pos, block, 160);
|
||||
}
|
||||
}
|
||||
} else if !signal && !self.torch_toggled_too_frequently(pos, false) {
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.lit, true));
|
||||
}
|
||||
}
|
||||
|
||||
// ---- observers ----
|
||||
|
||||
pub(crate) fn observer_start_signal(&mut self, pos: BlockPos, block: BlockId) {
|
||||
if !self.ticks.has_block_tick(pos, block) {
|
||||
self.schedule_tick(pos, block, 2);
|
||||
}
|
||||
}
|
||||
pub(crate) fn observer_update_in_front(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let direction = self.b.facing(state);
|
||||
let opposite = rel(pos, direction.opposite());
|
||||
let block = self.b.block(state);
|
||||
self.neighbor_changed_at(opposite, block);
|
||||
self.update_neighbors_at_except(opposite, block, direction);
|
||||
}
|
||||
pub(crate) fn observer_tick(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
if b.table.bool(state, b.props.powered) {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.powered, false), 2);
|
||||
} else {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.powered, true), 2);
|
||||
self.schedule_tick(pos, b.block(state), 2);
|
||||
}
|
||||
self.observer_update_in_front(pos, state);
|
||||
}
|
||||
|
||||
// ---- buttons, levers, plates ----
|
||||
|
||||
/// `ButtonBlock/LeverBlock.updateNeighbours`.
|
||||
pub(crate) fn attached_update_neighbours(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let front = self.face_attached_connected(state).opposite();
|
||||
let block = self.b.block(state);
|
||||
self.update_neighbors_at(pos, block);
|
||||
self.update_neighbors_at(rel(pos, front), block);
|
||||
}
|
||||
pub(crate) fn plate_update_neighbours(&mut self, pos: BlockPos, block: BlockId) {
|
||||
self.update_neighbors_at(pos, block);
|
||||
self.update_neighbors_at(below(pos), block);
|
||||
}
|
||||
pub(crate) fn plate_signal_for_state(&self, state: BlockStateId) -> i32 {
|
||||
let b = self.b;
|
||||
if b.instance_of(state, Class::WeightedPressurePlate) {
|
||||
b.table.int(state, b.props.power)
|
||||
} else if b.table.bool(state, b.props.powered) {
|
||||
15
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
fn plate_signal_strength(&self, pos: BlockPos, state: BlockStateId) -> i32 {
|
||||
use horizon_core::geometry::{Aabb, Vec3};
|
||||
let b = self.b;
|
||||
let area = Aabb::new(
|
||||
Vec3::new(f64::from(pos.x) + 0.0625, f64::from(pos.y), f64::from(pos.z) + 0.0625),
|
||||
Vec3::new(f64::from(pos.x) + 0.9375, f64::from(pos.y) + 0.25, f64::from(pos.z) + 0.9375),
|
||||
);
|
||||
let data = b.data(state);
|
||||
if data.is(Class::WeightedPressurePlate) {
|
||||
let max = data.max_weight.max(1);
|
||||
let count = (self.level.count_entities(area, super::EntityQuery::Everything) as i32).min(max);
|
||||
if count > 0 {
|
||||
(count.min(max) as f32 / max as f32 * 15.0).ceil() as i32
|
||||
} else {
|
||||
0
|
||||
}
|
||||
} else {
|
||||
let query = if data.set_type.as_ref().is_some_and(|s| s.mobs_only) {
|
||||
super::EntityQuery::Mobs
|
||||
} else {
|
||||
super::EntityQuery::Everything
|
||||
};
|
||||
if self.level.count_entities(area, query) > 0 { 15 } else { 0 }
|
||||
}
|
||||
}
|
||||
/// `BasePressurePlateBlock.checkPressed`.
|
||||
pub(crate) fn plate_check_pressed(&mut self, pos: BlockPos, state: BlockStateId, old_signal: i32) {
|
||||
let b = self.b;
|
||||
let signal = self.plate_signal_strength(pos, state);
|
||||
let was = old_signal > 0;
|
||||
let is = signal > 0;
|
||||
let block = b.block(state);
|
||||
if old_signal != signal {
|
||||
let new = if b.instance_of(state, Class::WeightedPressurePlate) {
|
||||
b.table.set_int(state, b.props.power, signal)
|
||||
} else {
|
||||
b.table.set_bool(state, b.props.powered, signal > 0)
|
||||
};
|
||||
self.set_block(pos, new, 2);
|
||||
self.plate_update_neighbours(pos, block);
|
||||
}
|
||||
if let Some(set) = b.data(state).set_type.clone() {
|
||||
if !is && was {
|
||||
self.sound(pos, &set.plate_off, 1.0, 1.0);
|
||||
self.game_event(pos, "minecraft:block_deactivate");
|
||||
} else if is && !was {
|
||||
self.sound(pos, &set.plate_on, 1.0, 1.0);
|
||||
self.game_event(pos, "minecraft:block_activate");
|
||||
}
|
||||
}
|
||||
if is {
|
||||
let delay = if b.instance_of(state, Class::WeightedPressurePlate) { 10 } else { 20 };
|
||||
self.schedule_tick(pos, block, delay);
|
||||
}
|
||||
}
|
||||
/// `ButtonBlock.press`.
|
||||
pub(crate) fn button_press(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.powered, true));
|
||||
self.attached_update_neighbours(pos, state);
|
||||
let data = b.data(state);
|
||||
self.schedule_tick(pos, b.block(state), data.ticks_to_stay_pressed);
|
||||
if let Some(set) = data.set_type.clone() {
|
||||
self.sound(pos, &set.button_on, 1.0, 1.0);
|
||||
}
|
||||
self.game_event(pos, "minecraft:block_activate");
|
||||
}
|
||||
/// `ButtonBlock.checkPressed` with no arrows present.
|
||||
pub(crate) fn button_tick(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
if !b.table.bool(state, b.props.powered) {
|
||||
return;
|
||||
}
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.powered, false));
|
||||
self.attached_update_neighbours(pos, state);
|
||||
if let Some(set) = b.data(state).set_type.clone() {
|
||||
self.sound(pos, &set.button_off, 1.0, 1.0);
|
||||
}
|
||||
self.game_event(pos, "minecraft:block_deactivate");
|
||||
}
|
||||
/// `LeverBlock.pull`.
|
||||
pub(crate) fn lever_pull(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let state = b.table.cycle(state, b.props.powered);
|
||||
self.set_block_and_update(pos, state);
|
||||
self.attached_update_neighbours(pos, state);
|
||||
let powered = b.table.bool(state, b.props.powered);
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.lever.click".into(),
|
||||
volume: 0.3,
|
||||
pitch: if powered { 0.6 } else { 0.5 },
|
||||
});
|
||||
self.game_event(pos, if powered { "minecraft:block_activate" } else { "minecraft:block_deactivate" });
|
||||
}
|
||||
|
||||
// ---- consumers ----
|
||||
|
||||
pub(crate) fn door_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId, changed: BlockId) {
|
||||
let b = self.b;
|
||||
let p = b.props;
|
||||
let other = if b.table.text(state, p.half) == "lower" { Direction::Up } else { Direction::Down };
|
||||
let signal = self.has_neighbor_signal(pos) || self.has_neighbor_signal(rel(pos, other));
|
||||
if b.block(state) != changed && signal != b.table.bool(state, p.powered) {
|
||||
if signal != b.table.bool(state, p.open) {
|
||||
self.door_sound(pos, state, signal);
|
||||
self.game_event(pos, if signal { "minecraft:block_open" } else { "minecraft:block_close" });
|
||||
}
|
||||
let next = b.table.set_bool(b.table.set_bool(state, p.powered, signal), p.open, signal);
|
||||
self.set_block(pos, next, 2);
|
||||
}
|
||||
}
|
||||
pub(crate) fn door_sound(&mut self, pos: BlockPos, state: BlockStateId, open: bool) {
|
||||
if let Some(set) = self.b.data(state).set_type.clone() {
|
||||
let pitch = self.random().next_float() * 0.1 + 0.9;
|
||||
let sound = if open { set.door_open.clone() } else { set.door_close.clone() };
|
||||
self.sound(pos, &sound, 1.0, pitch);
|
||||
}
|
||||
}
|
||||
pub(crate) fn trapdoor_sound(&mut self, pos: BlockPos, state: BlockStateId, open: bool) {
|
||||
if let Some(set) = self.b.data(state).set_type.clone() {
|
||||
let pitch = self.random().next_float() * 0.1 + 0.9;
|
||||
let sound = if open { set.trapdoor_open.clone() } else { set.trapdoor_close.clone() };
|
||||
self.sound(pos, &sound, 1.0, pitch);
|
||||
self.game_event(pos, if open { "minecraft:block_open" } else { "minecraft:block_close" });
|
||||
}
|
||||
}
|
||||
pub(crate) fn trapdoor_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let p = b.props;
|
||||
let signal = self.has_neighbor_signal(pos);
|
||||
if signal != b.table.bool(state, p.powered) {
|
||||
let mut state = state;
|
||||
if b.table.bool(state, p.open) != signal {
|
||||
state = b.table.set_bool(state, p.open, signal);
|
||||
self.trapdoor_sound(pos, state, signal);
|
||||
}
|
||||
self.set_block(pos, b.table.set_bool(state, p.powered, signal), 2);
|
||||
if b.is_waterlogged(state) {
|
||||
self.schedule_water_tick(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn gate_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let p = b.props;
|
||||
let power = self.has_neighbor_signal(pos);
|
||||
if b.table.bool(state, p.powered) != power {
|
||||
let next = b.table.set_bool(b.table.set_bool(state, p.powered, power), p.open, power);
|
||||
self.set_block(pos, next, 2);
|
||||
if b.table.bool(state, p.open) != power {
|
||||
let data = b.data(state);
|
||||
let sound = if power { data.gate_open.clone() } else { data.gate_close.clone() };
|
||||
let pitch = self.random().next_float() * 0.1 + 0.9;
|
||||
if let Some(sound) = sound {
|
||||
self.sound(pos, &sound, 1.0, pitch);
|
||||
}
|
||||
self.game_event(pos, if power { "minecraft:block_open" } else { "minecraft:block_close" });
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn lamp_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let lit = b.table.bool(state, b.props.lit);
|
||||
if lit != self.has_neighbor_signal(pos) {
|
||||
if lit {
|
||||
self.schedule_tick(pos, b.block(state), 4);
|
||||
} else {
|
||||
self.set_block(pos, b.table.cycle(state, b.props.lit), 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) fn lamp_tick(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
if b.table.bool(state, b.props.lit) && !self.has_neighbor_signal(pos) {
|
||||
self.set_block(pos, b.table.cycle(state, b.props.lit), 2);
|
||||
}
|
||||
}
|
||||
/// `CopperBulbBlock.checkAndFlip`.
|
||||
pub(crate) fn copper_bulb_check(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let p = b.props;
|
||||
let signal = self.has_neighbor_signal(pos);
|
||||
if signal != b.table.bool(state, p.powered) {
|
||||
let mut next = state;
|
||||
if !b.table.bool(state, p.powered) {
|
||||
next = b.table.cycle(next, p.lit);
|
||||
let sound = if b.table.bool(next, p.lit) {
|
||||
"minecraft:block.copper_bulb.turn_on"
|
||||
} else {
|
||||
"minecraft:block.copper_bulb.turn_off"
|
||||
};
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: sound.into(),
|
||||
volume: 1.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
}
|
||||
self.set_block(pos, b.table.set_bool(next, p.powered, signal), 3);
|
||||
}
|
||||
}
|
||||
/// `NoteBlock.playNote` (block event; instrument pitch from `note`).
|
||||
pub(crate) fn note_play(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let instrument = b.table.text(state, b.props.instrument).to_owned();
|
||||
let mob_head = super::data::INSTRUMENTS.iter().position(|n| *n == instrument).is_some_and(|i| super::data::instrument_works_above(i as u8));
|
||||
if mob_head || b.is_air(self.above_state(pos)) {
|
||||
let note = b.table.int(state, b.props.note);
|
||||
let pitch = if mob_head { 1.0 } else { 2f64.powf(f64::from(note - 12) / 12.0) as f32 };
|
||||
let sound = match instrument.as_str() {
|
||||
"custom_head" => "minecraft:ui.button.click".to_owned(),
|
||||
"dragon" => "minecraft:block.note_block.imitate.ender_dragon".to_owned(),
|
||||
name if mob_head => format!("minecraft:block.note_block.imitate.{name}"),
|
||||
name => format!("minecraft:block.note_block.{name}"),
|
||||
};
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: sound.into(),
|
||||
volume: 3.0,
|
||||
pitch,
|
||||
});
|
||||
self.game_event(pos, "minecraft:note_block_play");
|
||||
}
|
||||
}
|
||||
pub(crate) fn note_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let signal = self.has_neighbor_signal(pos);
|
||||
if signal != b.table.bool(state, b.props.powered) {
|
||||
if signal {
|
||||
self.note_play(pos, state);
|
||||
}
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.powered, signal));
|
||||
}
|
||||
}
|
||||
/// `TntBlock.prime` then `removeBlock`.
|
||||
pub(crate) fn tnt_check(&mut self, pos: BlockPos) {
|
||||
if self.has_neighbor_signal(pos) {
|
||||
self.emit(LevelEvent::PrimeTnt { pos });
|
||||
self.remove_block(pos, false);
|
||||
}
|
||||
}
|
||||
pub(crate) fn dispenser_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let trigger = self.has_neighbor_signal(pos) || self.has_neighbor_signal(above(pos));
|
||||
let triggered = b.table.bool(state, b.props.triggered);
|
||||
if trigger && !triggered {
|
||||
self.schedule_tick(pos, b.block(state), 4);
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.triggered, true), 2);
|
||||
} else if !trigger && triggered {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.triggered, false), 2);
|
||||
}
|
||||
}
|
||||
pub(crate) fn hopper_check_powered(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let enabled = !self.has_neighbor_signal(pos);
|
||||
if enabled != b.table.bool(state, b.props.enabled) {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.enabled, enabled), 2);
|
||||
}
|
||||
}
|
||||
pub(crate) fn skull_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let signal = self.has_neighbor_signal(pos);
|
||||
if signal != b.table.bool(state, b.props.powered) {
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.powered, signal), 2);
|
||||
}
|
||||
}
|
||||
pub(crate) fn bell_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let signal = self.has_neighbor_signal(pos);
|
||||
if signal != b.table.bool(state, b.props.powered) {
|
||||
if signal {
|
||||
self.emit(LevelEvent::Sound {
|
||||
pos,
|
||||
sound: "minecraft:block.bell.use".into(),
|
||||
volume: 2.0,
|
||||
pitch: 1.0,
|
||||
});
|
||||
self.game_event(pos, "minecraft:block_change");
|
||||
}
|
||||
self.set_block_and_update(pos, b.table.set_bool(state, b.props.powered, signal));
|
||||
}
|
||||
}
|
||||
pub(crate) fn crafter_neighbor_changed(&mut self, pos: BlockPos, state: BlockStateId) {
|
||||
let b = self.b;
|
||||
let trigger = self.has_neighbor_signal(pos);
|
||||
let triggered = b.table.bool(state, b.props.triggered);
|
||||
if trigger && !triggered {
|
||||
self.schedule_tick(pos, b.block(state), 4);
|
||||
self.set_block(pos, b.table.set_bool(state, b.props.triggered, true), 2);
|
||||
} else if !trigger && triggered {
|
||||
let next = b.table.set_bool(b.table.set_bool(state, b.props.triggered, false), b.props.crafting, false);
|
||||
self.set_block(pos, next, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Order `positions` as a `java.util.HashSet<BlockPos>` of capacity 16 would
|
||||
/// iterate them (bucket index of the spread hash, then insertion order).
|
||||
fn java_hash_set_order(positions: &mut Vec<BlockPos>) {
|
||||
fn hash(pos: BlockPos) -> i32 {
|
||||
// Vec3i.hashCode: (y + z * 31) * 31 + x
|
||||
(pos.y.wrapping_add(pos.z.wrapping_mul(31))).wrapping_mul(31).wrapping_add(pos.x)
|
||||
}
|
||||
let mut unique: Vec<BlockPos> = Vec::new();
|
||||
for pos in positions.iter() {
|
||||
if !unique.contains(pos) {
|
||||
unique.push(*pos);
|
||||
}
|
||||
}
|
||||
let buckets = 16;
|
||||
let bucket = |pos: BlockPos| {
|
||||
let h = hash(pos);
|
||||
((h ^ ((h as u32) >> 16) as i32) & (buckets - 1)) as usize
|
||||
};
|
||||
let mut ordered: Vec<(usize, usize, BlockPos)> = unique.iter().enumerate().map(|(i, p)| (bucket(*p), i, *p)).collect();
|
||||
ordered.sort_by_key(|(b, i, _)| (*b, *i));
|
||||
*positions = ordered.into_iter().map(|(_, _, p)| p).collect();
|
||||
}
|
||||
@@ -19,6 +19,11 @@ pub struct Ticks {
|
||||
/// `Level.randValue`, the LCG behind `getBlockRandomPos`.
|
||||
pub random_tick_value: i32,
|
||||
pub(crate) updater: NeighborUpdater,
|
||||
/// `ComparatorBlockEntity.output` per comparator position (block-entity
|
||||
/// state owned by the level; persist it with the chunk if desired).
|
||||
pub comparator_outputs: std::collections::HashMap<BlockPos, i32>,
|
||||
/// `RedstoneTorchBlock.RECENT_TOGGLES` (position, game time).
|
||||
pub torch_toggles: Vec<(BlockPos, i64)>,
|
||||
}
|
||||
impl Default for Ticks {
|
||||
fn default() -> Self {
|
||||
@@ -35,6 +40,8 @@ impl Ticks {
|
||||
sub_tick: 0,
|
||||
random_tick_value: random_tick_seed,
|
||||
updater: NeighborUpdater::default(),
|
||||
comparator_outputs: Default::default(),
|
||||
torch_toggles: Vec::new(),
|
||||
}
|
||||
}
|
||||
/// Register a loaded chunk's saved block and fluid ticks.
|
||||
|
||||
Reference in New Issue
Block a user