d746be2ca3
game/lan.rs: #586's movement-check rules are set before the per-level passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
373 lines
13 KiB
Rust
373 lines
13 KiB
Rust
//! The creaking heart block entity in the game: `mobs::creaking_heart`
|
|
//! ticks each heart (spawning, letting go of and bleeding for its creaking)
|
|
//! and this applies what it reports to the world, the mobs and the speakers.
|
|
use super::{Game, creatures::seen};
|
|
use horizon_client::{
|
|
Result,
|
|
containers::{BlockEntity, CreakingHeart},
|
|
mobs::{
|
|
Mob, MobType,
|
|
creaking_heart::{self, HeartEvent, HeartHost},
|
|
},
|
|
particles::{ParticleOptions, ParticleType},
|
|
};
|
|
use horizon_core::{
|
|
geometry::Vec3,
|
|
position::BlockPos,
|
|
random::{LegacyRandom, RandomSource},
|
|
};
|
|
use horizon_world::registry::BlockStateId;
|
|
use std::{collections::HashMap, sync::Arc};
|
|
|
|
type Pos = (i32, i32, i32);
|
|
|
|
/// Comparator input of a heart (`getAnalogOutputSignal`).
|
|
pub(super) fn heart_signal(world: &horizon_client::world::World, pos: BlockPos) -> Option<i32> {
|
|
match world.block_entities.get(&(pos.x, pos.y, pos.z))? {
|
|
BlockEntity::CreakingHeart(h) => Some(h.output_signal),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// The game's mobs as a heart sees them, taken out of the game for the tick.
|
|
struct Host<'a> {
|
|
mobs: &'a mut Vec<Mob>,
|
|
added: Vec<Mob>,
|
|
uuids: &'a mut HashMap<u32, [i32; 4]>,
|
|
creaking: Option<Arc<MobType>>,
|
|
next_entity: &'a mut u32,
|
|
spawning: bool,
|
|
}
|
|
impl HeartHost for Host<'_> {
|
|
fn mob(&self, id: u32) -> Option<&Mob> {
|
|
self.mobs
|
|
.iter()
|
|
.chain(self.added.iter())
|
|
.find(|m| m.id == id && !m.removed)
|
|
}
|
|
fn mob_mut(&mut self, id: u32) -> Option<&mut Mob> {
|
|
self.mobs
|
|
.iter_mut()
|
|
.chain(self.added.iter_mut())
|
|
.find(|m| m.id == id && !m.removed)
|
|
}
|
|
fn mob_for_uuid(&self, uuid: [i32; 4]) -> Option<u32> {
|
|
self.uuids
|
|
.iter()
|
|
.find(|(id, u)| **u == uuid && self.mob(**id).is_some())
|
|
.map(|(id, _)| *id)
|
|
}
|
|
fn uuid_for(&mut self, id: u32) -> [i32; 4] {
|
|
*self
|
|
.uuids
|
|
.entry(id)
|
|
.or_insert_with(horizon_client::save::random_uuid)
|
|
}
|
|
fn creaking_type(&self) -> Option<Arc<MobType>> {
|
|
self.creaking.clone()
|
|
}
|
|
fn create_mob(&mut self, kind: Arc<MobType>, at: Vec3) -> Mob {
|
|
*self.next_entity += 1;
|
|
// A new entity's random is its own (Mob::new seeds it).
|
|
let mut m = Mob::new(*self.next_entity, kind, at, 0.0);
|
|
let seed = i64::from(m.id);
|
|
horizon_client::mobs::monster_brain::init(&mut m, seed);
|
|
m
|
|
}
|
|
fn add_mob(&mut self, m: Mob) {
|
|
self.added.push(m);
|
|
}
|
|
fn spawning_monsters(&self) -> bool {
|
|
self.spawning
|
|
}
|
|
}
|
|
|
|
impl Game<'_> {
|
|
fn heart_state_name(&self, state: BlockStateId) -> String {
|
|
self.pack
|
|
.assets
|
|
.registry
|
|
.state(state)
|
|
.and_then(|s| s.properties.get("creaking_heart_state").cloned())
|
|
.unwrap_or_default()
|
|
}
|
|
/// Runs `f` on the heart at `key` with the game's mobs and level random,
|
|
/// then applies what the heart reported.
|
|
fn with_heart(
|
|
&mut self,
|
|
key: Pos,
|
|
heart: &mut CreakingHeart,
|
|
f: impl FnOnce(
|
|
&mut CreakingHeart,
|
|
&horizon_client::mobs::MobWorld,
|
|
&mut LegacyRandom,
|
|
&mut Host,
|
|
&mut Vec<HeartEvent>,
|
|
),
|
|
) -> Result<()> {
|
|
let mut mobs = std::mem::take(&mut self.mobs);
|
|
let mut uuids = std::mem::take(&mut self.persist.uuids);
|
|
let mut next_entity = self.next_entity;
|
|
let mut random = self.world.random.clone();
|
|
let spawning = self.spawning_monsters();
|
|
let creaking = self.res.mob_types.get("minecraft:creaking").cloned();
|
|
let player_uuid = self.level.player_uuid;
|
|
let snapshot: Vec<_> = mobs.iter().map(|m| seen(m, player_uuid)).collect();
|
|
let mut events = Vec::new();
|
|
let added = {
|
|
let w = self.mob_world_base(&snapshot);
|
|
let mut host = Host {
|
|
mobs: &mut mobs,
|
|
added: Vec::new(),
|
|
uuids: &mut uuids,
|
|
creaking,
|
|
next_entity: &mut next_entity,
|
|
spawning,
|
|
};
|
|
f(heart, &w, &mut random, &mut host, &mut events);
|
|
host.added
|
|
};
|
|
mobs.extend(added);
|
|
self.mobs = mobs;
|
|
self.persist.uuids = uuids;
|
|
self.next_entity = next_entity;
|
|
self.world.random = random;
|
|
let mut mob_events = Vec::new();
|
|
for e in events {
|
|
match e {
|
|
HeartEvent::SetBlock(at, state) => self.level_set(at, state)?,
|
|
HeartEvent::Sound(name, at, volume, pitch) => {
|
|
self.audio
|
|
.play(&format!("minecraft:{name}"), Some(at), volume, pitch)
|
|
}
|
|
HeartEvent::Trail {
|
|
at,
|
|
target,
|
|
color,
|
|
duration,
|
|
} => self.particles.add(
|
|
&ParticleOptions::trail(target, color, duration),
|
|
at,
|
|
[0.0; 3],
|
|
),
|
|
HeartEvent::OutputChanged(at) => self.container_changed((at[0], at[1], at[2])),
|
|
HeartEvent::Mob(event) => mob_events.push(event),
|
|
}
|
|
}
|
|
self.handle_mob_events(mob_events)?;
|
|
let _ = key;
|
|
Ok(())
|
|
}
|
|
/// A creaking bound to the heart at `home` was hurt (`creakingHurt`).
|
|
pub(super) fn creaking_hurt(&mut self, home: [i32; 3], _id: u32, by_player: bool) {
|
|
if !by_player {
|
|
return;
|
|
}
|
|
let key = (home[0], home[1], home[2]);
|
|
let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.remove(&key)
|
|
else {
|
|
return;
|
|
};
|
|
let _ = self.with_heart(key, &mut heart, |h, w, lr, host, out| {
|
|
creaking_heart::creaking_hurt(h, home, w, lr, host, out);
|
|
});
|
|
self.world
|
|
.block_entities
|
|
.insert(key, BlockEntity::CreakingHeart(heart));
|
|
}
|
|
/// `Creaking.tearDown`: the crumble particles.
|
|
pub(super) fn creaking_tear_down(&mut self, center: [f64; 3], spread: [f64; 3]) {
|
|
let registry = self.pack.assets.registry;
|
|
let crumble = |this: &mut Self, name: &str, props: &[(&str, &str)], count: i32| {
|
|
let Some(mut state) = registry
|
|
.default_state(name)
|
|
.and_then(|s| registry.state(s).cloned())
|
|
else {
|
|
return;
|
|
};
|
|
for (k, v) in props {
|
|
state.properties.insert((*k).to_owned(), (*v).to_owned());
|
|
}
|
|
let Ok(id) = registry.resolve(&state) else {
|
|
return;
|
|
};
|
|
let Some((sprite, tint)) = this.particle_sprite(id) else {
|
|
return;
|
|
};
|
|
for _ in 0..count {
|
|
let r = &mut this.world.random;
|
|
let p = [
|
|
center[0] + r.next_gaussian() * spread[0],
|
|
center[1] + r.next_gaussian() * spread[1],
|
|
center[2] + r.next_gaussian() * spread[2],
|
|
];
|
|
this.particles.add(
|
|
&ParticleOptions::block(ParticleType::BlockCrumble, sprite.clone(), tint),
|
|
p,
|
|
[0.0; 3],
|
|
);
|
|
}
|
|
};
|
|
crumble(self, "minecraft:pale_oak_wood", &[], 100);
|
|
crumble(
|
|
self,
|
|
"minecraft:creaking_heart",
|
|
&[("creaking_heart_state", "awake")],
|
|
10,
|
|
);
|
|
}
|
|
/// `CreakingHeartBlock.playerWillDestroy`: the heart's creaking twitches to
|
|
/// death and a natural heart drops experience.
|
|
pub(super) fn creaking_heart_broken(&mut self, pos: [i32; 3]) {
|
|
let key = (pos[0], pos[1], pos[2]);
|
|
let Some(BlockEntity::CreakingHeart(mut heart)) = self.world.block_entities.remove(&key)
|
|
else {
|
|
return;
|
|
};
|
|
let _ = self.with_heart(key, &mut heart, |h, _, _, host, out| {
|
|
creaking_heart::remove_protector(h, host, true, out);
|
|
});
|
|
let state = self.world.block(key.0, key.1, key.2);
|
|
let natural = self
|
|
.pack
|
|
.assets
|
|
.registry
|
|
.state(state)
|
|
.and_then(|s| s.properties.get("natural").cloned())
|
|
.is_some_and(|v| v == "true");
|
|
if natural && !self.creative && !self.spectator() {
|
|
let xp = 20 + self.world.random.next_int_bound(5).unwrap_or(0) as u32;
|
|
self.spawn_orbs(
|
|
[
|
|
f64::from(pos[0]) + 0.5,
|
|
f64::from(pos[1]) + 0.5,
|
|
f64::from(pos[2]) + 0.5,
|
|
],
|
|
xp,
|
|
);
|
|
}
|
|
self.world
|
|
.block_entities
|
|
.insert(key, BlockEntity::CreakingHeart(heart));
|
|
}
|
|
/// `CreakingHeartBlockEntity.serverTick` for every heart.
|
|
pub(super) fn tick_creaking_hearts(&mut self) -> Result<()> {
|
|
let keys: Vec<Pos> = self
|
|
.world
|
|
.block_entities
|
|
.iter()
|
|
.filter(|(_, e)| matches!(e, BlockEntity::CreakingHeart(_)))
|
|
.map(|(k, _)| *k)
|
|
.collect();
|
|
for key in keys {
|
|
let Some(BlockEntity::CreakingHeart(mut heart)) =
|
|
self.world.block_entities.remove(&key)
|
|
else {
|
|
continue;
|
|
};
|
|
let state = self.world.block(key.0, key.1, key.2);
|
|
if &*self.res.facts.get(state).name != "minecraft:creaking_heart" {
|
|
// The block is gone: preRemoveSideEffects.
|
|
self.with_heart(key, &mut heart, |h, _, _, host, out| {
|
|
creaking_heart::remove_protector(h, host, false, out);
|
|
})?;
|
|
continue;
|
|
}
|
|
// getTicker: none once uprooted.
|
|
if self.heart_state_name(state) != "uprooted" {
|
|
let pos = [key.0, key.1, key.2];
|
|
self.with_heart(key, &mut heart, |h, w, lr, host, out| {
|
|
creaking_heart::server_tick(h, pos, w, lr, host, out);
|
|
})?;
|
|
}
|
|
self.world
|
|
.block_entities
|
|
.insert(key, BlockEntity::CreakingHeart(heart));
|
|
}
|
|
self.scan_for_hearts();
|
|
Ok(())
|
|
}
|
|
/// `ServerLevel.isSpawningMonsters`.
|
|
fn spawning_monsters(&self) -> bool {
|
|
self.ui.difficulty != 0
|
|
&& self.ui.rules.bool("spawn_mobs")
|
|
&& self.ui.rules.bool("spawn_monsters")
|
|
}
|
|
/// Hearts that came with generated or loaded chunks get their block
|
|
/// entity (`newBlockEntity`) the first time their chunk is seen.
|
|
fn scan_for_hearts(&mut self) {
|
|
let Some(heart) = self
|
|
.pack
|
|
.assets
|
|
.registry
|
|
.default_state("minecraft:creaking_heart")
|
|
else {
|
|
return;
|
|
};
|
|
let keys: Vec<(i32, i32)> = self
|
|
.world
|
|
.chunks
|
|
.iter()
|
|
.filter(|(k, c)| c.lit && !self.creaking.scanned.contains(k))
|
|
.map(|(k, _)| *k)
|
|
.collect();
|
|
for key in keys {
|
|
self.creaking.scanned.insert(key);
|
|
let Some(chunk) = self.world.chunks.get(&key).cloned() else {
|
|
continue;
|
|
};
|
|
if self.creaking.hearts.is_empty() {
|
|
self.creaking.hearts = self
|
|
.res
|
|
.facts
|
|
.states
|
|
.iter()
|
|
.map(|f| &*f.name == "minecraft:creaking_heart")
|
|
.collect();
|
|
}
|
|
let hearts = &self.creaking.hearts;
|
|
let _ = heart;
|
|
for (si, section) in chunk.sections.iter().enumerate() {
|
|
if section.is_uniform(0) {
|
|
continue;
|
|
}
|
|
for i in 0..4096 {
|
|
let id = section.get(i);
|
|
if !hearts.get(id as usize).copied().unwrap_or(false) {
|
|
continue;
|
|
}
|
|
let (lx, lz, ly) = ((i & 15) as i32, ((i >> 4) & 15) as i32, (i >> 8) as i32);
|
|
let pos = (
|
|
key.0 * 16 + lx,
|
|
horizon_client::chunk::MIN_Y + si as i32 * 16 + ly,
|
|
key.1 * 16 + lz,
|
|
);
|
|
self.world
|
|
.block_entities
|
|
.entry(pos)
|
|
.or_insert_with(|| BlockEntity::CreakingHeart(Box::default()));
|
|
}
|
|
}
|
|
}
|
|
let chunks = &self.world.chunks;
|
|
self.creaking.scanned.retain(|k| chunks.contains_key(k));
|
|
}
|
|
}
|
|
|
|
/// What the creaking code remembers between ticks.
|
|
#[derive(Default)]
|
|
pub(super) struct CreakingState {
|
|
/// Chunks already scanned for heart blocks.
|
|
scanned: horizon_client::fast_hash::FxHashSet<(i32, i32)>,
|
|
/// Which block states are creaking hearts, by state id.
|
|
hearts: Vec<bool>,
|
|
}
|
|
|
|
impl CreakingState {
|
|
/// Exchange the chunks scanned for creaking hearts with another level's
|
|
/// (game/levels.rs).
|
|
pub(super) fn swap_level(&mut self, other: &mut Self) {
|
|
std::mem::swap(&mut self.scanned, &mut other.scanned);
|
|
}
|
|
}
|