f96101cd47
Lightning (Entity.thunderHit) and evoker fangs (EvokerFangs.dealDamageTo) hit every entity in their box, but thunder_hit and fangs_bite only looked at the window's own player, so a server's players (guests, and single player on the server thread) were never struck (#1480, #1481). Both now run their player part for each guest too, through Game::each_guest_hit. The effects a mob gives the player it acts on (cave spider and bee poison, husk hunger, wither skeleton wither, the nautilus's breath) and an axolotl's help after a kill stayed with the server's own player: acts_on_player now routes MobEvent::EffectPlayer and MobEvent::AxolotlSupportPlayer to the guest the mob sees, like its blows (#1484, #1485). tools/lan_e2e.py gains a strikes check: a bolt hurts each player standing in water, and a cave spider poisons them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
388 lines
14 KiB
Rust
388 lines
14 KiB
Rust
//! Lightning (`ServerLevel.tickThunder`, `LightningBolt`,
|
|
//! `LightningBoltRenderer`): strikes on the highest motion-blocking block or
|
|
//! a living entity under open sky nearby, skeleton trap horses, fire,
|
|
//! `thunderHit` (damage and burning, charged creepers, zombified piglins,
|
|
//! witches, mooshroom colours, turtles, burnt items), lightning rods, the sky
|
|
//! flash, the sounds and the branching additive bolt.
|
|
use super::Game;
|
|
use horizon_client::mobs::{Damage, DamageKind, MobEvent, SpawnSetup, Species};
|
|
use horizon_core::{position::BlockPos, random::RandomSource};
|
|
|
|
pub(super) struct Bolt {
|
|
/// The entity id a server's players know it by (0 until it has one).
|
|
pub id: u32,
|
|
pub at: [f64; 3],
|
|
life: i32,
|
|
flashes: i32,
|
|
seed: i64,
|
|
visual_only: bool,
|
|
/// Mooshrooms switch colour once per bolt.
|
|
converted: Vec<u32>,
|
|
}
|
|
impl Game<'_> {
|
|
/// `ServerChunkCache.tickSpawningChunk`: one `tickThunder` roll per
|
|
/// spawning chunk (within 128 blocks of a player) that is in entity
|
|
/// ticking range.
|
|
pub(super) fn tick_thunder(&mut self) {
|
|
// isRaining (rain level > 0.2) and isThundering (> 0.9, sky light, no ceiling).
|
|
let (rain, thunder) = self.world.weather.levels(1.0);
|
|
if rain <= 0.2 || thunder <= 0.9 || self.dim != super::Dim::Overworld {
|
|
return;
|
|
}
|
|
let (_, mut chunks) = self.spawning_chunks(&self.level_players());
|
|
let levels = self.tickets.levels();
|
|
chunks.retain(|&(x, z)| levels.random_ticking(x, z));
|
|
for (cx, cz) in chunks {
|
|
if self.world.random.next_int_bound(100_000).unwrap_or(1) != 0 {
|
|
continue;
|
|
}
|
|
// getBlockRandomPos(minX, 0, minZ, 15)
|
|
let r = self.world.random.next_int();
|
|
let (x, z) = (cx * 16 + (r & 15), cz * 16 + (r >> 16 & 15));
|
|
let Some(pos) = self.lightning_target(x, z) else {
|
|
continue;
|
|
};
|
|
let p = BlockPos::new(pos[0], pos[1], pos[2]);
|
|
if !self.with_level(|b, level, _| b.is_raining_at(level, p)) {
|
|
continue;
|
|
}
|
|
// Skeleton trap: effective difficulty / 100 (regional difficulty approximated).
|
|
let difficulty = f64::from(self.ui.difficulty) * 0.75;
|
|
let below = &self
|
|
.res
|
|
.facts()
|
|
.get(self.world.block(pos[0], pos[1] - 1, pos[2]))
|
|
.name;
|
|
let trap = self.ui.rules.bool("spawn_mobs")
|
|
&& self.world.random.next_double() < difficulty * 0.01
|
|
&& !below.ends_with("lightning_rod");
|
|
if trap {
|
|
let at = [f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])];
|
|
// ServerLevel.tickChunk: a trap horse waits for a player.
|
|
if let Some(id) = self.spawn_mob("minecraft:skeleton_horse", at, false)
|
|
&& let Some(h) = self.mobs.iter_mut().find(|m| m.id == id)
|
|
{
|
|
h.extra.trap = true;
|
|
h.extra.age = 0;
|
|
}
|
|
}
|
|
let at = [
|
|
f64::from(pos[0]) + 0.5,
|
|
f64::from(pos[1]),
|
|
f64::from(pos[2]) + 0.5,
|
|
];
|
|
self.spawn_bolt(at, trap);
|
|
}
|
|
}
|
|
/// `findLightningTargetAround`: a living entity under open sky in the
|
|
/// column (3 blocks around), else the top of the column.
|
|
fn lightning_target(&mut self, x: i32, z: i32) -> Option<[i32; 3]> {
|
|
if !self.world.loaded(x, z) {
|
|
return None;
|
|
}
|
|
let top =
|
|
self.with_level(|b, level, _| b.motion_blocking_top(level, BlockPos::new(x, 0, z)));
|
|
let sees_sky = |g: &Self, p: [i32; 3]| g.world.light(p[0], p[1], p[2]) >> 4 == 15;
|
|
let mut candidates: Vec<[i32; 3]> = self
|
|
.mobs
|
|
.iter()
|
|
.filter(|m| m.alive())
|
|
.map(|m| {
|
|
[
|
|
m.position.x.floor() as i32,
|
|
m.position.y.floor() as i32,
|
|
m.position.z.floor() as i32,
|
|
]
|
|
})
|
|
.filter(|p| (p[0] - x).abs() <= 3 && (p[2] - z).abs() <= 3 && p[1] >= top.y - 3)
|
|
.filter(|p| sees_sky(self, *p))
|
|
.collect();
|
|
if let Some(p) = self
|
|
.player
|
|
.as_ref()
|
|
.filter(|p| !p.health.dead_or_dying())
|
|
.map(|p| {
|
|
[
|
|
p.position.x.floor() as i32,
|
|
p.position.y.floor() as i32,
|
|
p.position.z.floor() as i32,
|
|
]
|
|
})
|
|
&& (p[0] - x).abs() <= 3
|
|
&& (p[2] - z).abs() <= 3
|
|
&& p[1] >= top.y - 3
|
|
&& sees_sky(self, p)
|
|
{
|
|
candidates.push(p);
|
|
}
|
|
if !candidates.is_empty() {
|
|
let i = self
|
|
.world
|
|
.random
|
|
.next_int_bound(candidates.len() as i32)
|
|
.unwrap_or(0) as usize;
|
|
return Some(candidates[i]);
|
|
}
|
|
Some([x, top.y, z])
|
|
}
|
|
pub(super) fn spawn_bolt(&mut self, at: [f64; 3], visual_only: bool) {
|
|
if !visual_only {
|
|
let ctx = horizon_client::sculk::Context::default();
|
|
self.post_game_event("minecraft:lightning_strike", at, ctx);
|
|
}
|
|
let r = &mut self.world.random;
|
|
let seed = r.next_long();
|
|
let flashes = r.next_int_bound(3).unwrap_or(0) + 1;
|
|
self.lightning_seen[0] += 1;
|
|
self.bolts.push(Bolt {
|
|
id: 0,
|
|
at,
|
|
life: 2,
|
|
flashes,
|
|
seed,
|
|
visual_only,
|
|
converted: Vec::new(),
|
|
});
|
|
}
|
|
/// `ClientLevel.addEntity` for a server's lightning bolt: on a client it
|
|
/// only flashes, sounds and draws (`LightningBolt.tick` off the server).
|
|
pub(super) fn spawn_remote_bolt(&mut self, id: u32, at: [f64; 3]) {
|
|
self.bolts.retain(|b| b.id != id);
|
|
let r = &mut self.world.random;
|
|
let seed = r.next_long();
|
|
let flashes = r.next_int_bound(3).unwrap_or(0) + 1;
|
|
self.lightning_seen[0] += 1;
|
|
self.bolts.push(Bolt {
|
|
id,
|
|
at,
|
|
life: 2,
|
|
flashes,
|
|
seed,
|
|
visual_only: true,
|
|
converted: Vec::new(),
|
|
});
|
|
}
|
|
/// `spawnFire`: fire at the strike point and `extra` random neighbours.
|
|
fn bolt_fire(&mut self, at: [f64; 3], extra: i32) {
|
|
if self.ui.difficulty < 2 {
|
|
return;
|
|
}
|
|
let base = [
|
|
at[0].floor() as i32,
|
|
at[1].floor() as i32,
|
|
at[2].floor() as i32,
|
|
];
|
|
let mut spots = vec![base];
|
|
for _ in 0..extra {
|
|
let r = &mut self.world.random;
|
|
let mut d = || r.next_int_bound(3).unwrap_or(1) - 1;
|
|
spots.push([base[0] + d(), base[1] + d(), base[2] + d()]);
|
|
}
|
|
for p in spots {
|
|
let state = self.world.block(p[0], p[1], p[2]);
|
|
if !self.res.facts().is_air(state) {
|
|
continue;
|
|
}
|
|
// BaseFireBlock.getState: soul fire on soul soil and sand.
|
|
let below = self.res.facts().get(self.world.block(p[0], p[1] - 1, p[2]));
|
|
let name = if matches!(&*below.name, "minecraft:soul_soil" | "minecraft:soul_sand") {
|
|
"minecraft:soul_fire"
|
|
} else {
|
|
"minecraft:fire"
|
|
};
|
|
// FireBlock.canSurvive: a sturdy floor (flammable neighbours aside).
|
|
if below.sturdy_full >> 1 & 1 == 0 {
|
|
continue;
|
|
}
|
|
if let Some(fire) = self.pack.assets.registry.default_state(name) {
|
|
let _ = self.level_set(p, fire);
|
|
}
|
|
}
|
|
}
|
|
/// `LightningBolt.tick` for every bolt.
|
|
pub(super) fn tick_bolts(&mut self) {
|
|
let mut bolts = std::mem::take(&mut self.bolts);
|
|
bolts.retain_mut(|b| {
|
|
if b.life == 2 {
|
|
let pitch = 0.8 + self.world.random.next_float() * 0.2;
|
|
self.audio.play(
|
|
"minecraft:entity.lightning_bolt.thunder",
|
|
Some(b.at),
|
|
10000.0,
|
|
pitch,
|
|
);
|
|
let pitch = 0.5 + self.world.random.next_float() * 0.2;
|
|
self.audio.play(
|
|
"minecraft:entity.lightning_bolt.impact",
|
|
Some(b.at),
|
|
2.0,
|
|
pitch,
|
|
);
|
|
if !b.visual_only {
|
|
self.bolt_fire(b.at, 4);
|
|
self.strike_rod(b.at);
|
|
}
|
|
}
|
|
b.life -= 1;
|
|
if b.life < 0 {
|
|
if b.flashes == 0 {
|
|
return false;
|
|
}
|
|
if b.life < -self.world.random.next_int_bound(10).unwrap_or(0) {
|
|
b.flashes -= 1;
|
|
b.life = 1;
|
|
b.seed = self.world.random.next_long();
|
|
if !b.visual_only {
|
|
self.bolt_fire(b.at, 0);
|
|
}
|
|
}
|
|
}
|
|
if b.life >= 0 {
|
|
if self.flash == 0 {
|
|
self.lightning_seen[1] += 1;
|
|
}
|
|
self.flash = 2;
|
|
if !b.visual_only {
|
|
self.thunder_hit(b);
|
|
}
|
|
}
|
|
true
|
|
});
|
|
bolts.append(&mut self.bolts);
|
|
self.bolts = bolts;
|
|
}
|
|
/// `LightningRodBlock.onLightningStrike`: powered for 8 ticks.
|
|
fn strike_rod(&mut self, at: [f64; 3]) {
|
|
let p = [
|
|
at[0].floor() as i32,
|
|
(at[1] - 1.0e-6).floor() as i32,
|
|
at[2].floor() as i32,
|
|
];
|
|
let state = self.world.block(p[0], p[1], p[2]);
|
|
if !self.res.facts().get(state).name.ends_with("lightning_rod") {
|
|
return;
|
|
}
|
|
let registry = self.pack.assets.registry;
|
|
if let Some(mut s) = registry.state(state).cloned() {
|
|
s.properties.insert("powered".into(), "true".into());
|
|
if let Some(powered) = registry.id(&s) {
|
|
// The rod's onPlace schedules the 8-tick unpowering.
|
|
let _ = self.level_set(p, powered);
|
|
}
|
|
// Level event 3002 with the rod's axis: sparks along it.
|
|
let axis = match s.properties.get("facing").map(String::as_str) {
|
|
Some("east" | "west") => 0,
|
|
Some("north" | "south") => 2,
|
|
_ => 1,
|
|
};
|
|
self.level_event_at(3002, p, axis);
|
|
}
|
|
}
|
|
/// `thunderHit` for entities within 3 blocks (up to 9 above).
|
|
fn thunder_hit(&mut self, b: &mut Bolt) {
|
|
let at = b.at;
|
|
let inside = move |p: [f64; 3]| {
|
|
(p[0] - at[0]).abs() <= 3.0
|
|
&& (p[2] - at[2]).abs() <= 3.0
|
|
&& p[1] >= at[1] - 3.0
|
|
&& p[1] <= at[1] + 9.0
|
|
};
|
|
let damage = Damage {
|
|
kind: DamageKind::Generic,
|
|
attacker: None,
|
|
from: Some(b.at),
|
|
};
|
|
let mut events = Vec::new();
|
|
let peaceful = self.ui.difficulty == 0;
|
|
for m in self.mobs.iter_mut().filter(|m| m.alive()) {
|
|
if !inside([m.position.x, m.position.y, m.position.z]) {
|
|
continue;
|
|
}
|
|
let setup = |m: &horizon_client::mobs::Mob| SpawnSetup {
|
|
yaw: m.yaw,
|
|
persistent: true,
|
|
..Default::default()
|
|
};
|
|
let at = [m.position.x, m.position.y, m.position.z];
|
|
match m.species() {
|
|
Species::Pig if !peaceful => {
|
|
m.removed = true;
|
|
events.push(MobEvent::SpawnKind {
|
|
kind: "minecraft:zombified_piglin".into(),
|
|
at,
|
|
setup: setup(m),
|
|
});
|
|
continue;
|
|
}
|
|
Species::Villager if !peaceful => {
|
|
m.removed = true;
|
|
events.push(MobEvent::SpawnKind {
|
|
kind: "minecraft:witch".into(),
|
|
at,
|
|
setup: setup(m),
|
|
});
|
|
continue;
|
|
}
|
|
Species::Mooshroom => {
|
|
if !b.converted.contains(&m.id) {
|
|
b.converted.push(m.id);
|
|
m.extra.variant = 1 - m.extra.variant.min(1);
|
|
self.audio
|
|
.play("minecraft:entity.mooshroom.convert", Some(at), 2.0, 1.0);
|
|
}
|
|
continue;
|
|
}
|
|
Species::Turtle => {
|
|
m.damage(f32::MAX, damage);
|
|
continue;
|
|
}
|
|
Species::Creeper => m.extra.charged = true,
|
|
_ => {}
|
|
}
|
|
// Entity.thunderHit: burning (8 seconds) and 5 lightning damage.
|
|
m.fire_ticks += 1;
|
|
if m.fire_ticks == 0 {
|
|
m.ignite_for_ticks(160);
|
|
}
|
|
|
|
m.damage(5.0, damage);
|
|
}
|
|
if !events.is_empty() {
|
|
let _ = self.handle_mob_events(events);
|
|
}
|
|
// Every player in the box, the server's players too.
|
|
let strike = move |g: &mut Self| {
|
|
if let Some(p) = g.player.as_ref().map(|p| p.position)
|
|
&& inside([p.x, p.y, p.z])
|
|
{
|
|
if let Some(p) = g.player.as_mut() {
|
|
p.fire_ticks += 1;
|
|
if p.fire_ticks == 0
|
|
&& let Err(error) = p.ignite_for_ticks(160)
|
|
{
|
|
g.error = Some(error);
|
|
}
|
|
}
|
|
g.hurt_player_by("minecraft:lightning_bolt", 5.0);
|
|
}
|
|
};
|
|
strike(self);
|
|
self.each_guest_hit(strike);
|
|
// Dropped items burn up (ItemEntity health 5), save fire-resistant ones.
|
|
self.dropped.retain(|e| {
|
|
!inside([e.position.x, e.position.y, e.position.z])
|
|
|| e.stack.item.contains("netherite")
|
|
});
|
|
}
|
|
/// Simulation-order render inputs; seeded geometry lives in the renderer.
|
|
pub(super) fn bolt_instances(&self) -> Vec<horizon_client::gfx::lightning::Bolt> {
|
|
self.bolts
|
|
.iter()
|
|
.map(|b| horizon_client::gfx::lightning::Bolt {
|
|
at: b.at,
|
|
seed: b.seed,
|
|
})
|
|
.collect()
|
|
}
|
|
}
|