From 26acaf8ebd533704c4e9d002539c5c6ca9ad2bae Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:24:12 +0200 Subject: [PATCH] Port the vanilla particle engine and client animateTick effects particles.rs becomes a ParticleEngine with ~80 vanilla particle types (constructors, ticks, size curves, light, sprite sets from particles/*.json) and camera-rotation quads. animate_tick.rs ports ClientLevel.animateTick: torches, furnaces, campfires (with block-entity smoke), candles, leaves drips and falling leaves, lava/water, fluid drips, portals, enchanting tables, end rods, spore blossoms, redstone, bubble columns and biome ambient particles. Rain splashes follow tickWeatherEffects. Adds a particle control command. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/animate_tick.rs | 1084 +++++++++ crates/horizon-client/src/game.rs | 47 +- crates/horizon-client/src/game/control.rs | 2 +- .../horizon-client/src/game/render_control.rs | 34 + crates/horizon-client/src/game/render_tick.rs | 151 ++ crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/particles.rs | 2163 ++++++++++++++++- 7 files changed, 3379 insertions(+), 103 deletions(-) create mode 100644 crates/horizon-client/src/animate_tick.rs create mode 100644 crates/horizon-client/src/game/render_tick.rs diff --git a/crates/horizon-client/src/animate_tick.rs b/crates/horizon-client/src/animate_tick.rs new file mode 100644 index 000000000..bf156e425 --- /dev/null +++ b/crates/horizon-client/src/animate_tick.rs @@ -0,0 +1,1084 @@ +//! Client-side ambient block effects: `ClientLevel.animateTick` (667 random +//! blocks within 16 and 32 blocks per tick calling `Block.animateTick` and +//! `FluidState.animateTick`, fluid drips and biome ambient particles) and the +//! campfire block-entity particle tick. Particle positions, odds and sounds +//! follow the vanilla block classes. +use crate::{ + biome::Biomes, + blocks::{BlockFacts, Fluid}, + particles::{ParticleEngine, ParticleOptions, ParticleType as T}, + world::World, +}; +use horizon_core::{ + geometry::Axis, + random::{LegacyRandom, RandomSource}, +}; +use horizon_world::{ + block_collision::BlockCollisionRegistry, + registry::{BlockRegistry, BlockStateId}, + worldgen::tags::TagDefinitions, +}; +use std::{collections::BTreeMap, path::Path}; + +#[derive(Clone, Copy, Debug, PartialEq)] +enum Animate { + None, + Torch(T), + WallTorch(T, [i32; 3]), + RedstoneTorch, + RedstoneWallTorch([i32; 3]), + Furnace([i32; 3]), + BlastFurnace([i32; 3]), + Smoker, + Campfire { + spawn_particles: bool, + }, + /// Leaves: optional falling-leaf particle and chance, tinted by foliage. + Leaves(Option<(T, f32)>), + NetherPortal, + EnchantingTable, + EndRod([i32; 3]), + SporeBlossom, + CryingObsidian, + Mycelium, + Candles(u8), + CandleCake, + BrewingStand, + EnderChest, + /// FallingBlock with its dust colour. + Falling(u32), + RedstoneOre, + RedstoneWire { + power: u8, + sides: [u8; 4], + }, + RespawnAnchor, + BubbleColumn { + drag: bool, + }, + Fire, +} +fn step(facing: &str) -> [i32; 3] { + match facing { + "north" => [0, 0, -1], + "south" => [0, 0, 1], + "west" => [-1, 0, 0], + "east" => [1, 0, 0], + "up" => [0, 1, 0], + _ => [0, -1, 0], + } +} +fn classify(name: &str, prop: &dyn Fn(&str) -> Option) -> Animate { + let name = name.strip_prefix("minecraft:").unwrap_or(name); + let lit = prop("lit").as_deref() == Some("true"); + let facing = || step(&prop("facing").unwrap_or_default()); + match name { + "torch" => Animate::Torch(T::Flame), + "soul_torch" => Animate::Torch(T::SoulFireFlame), + "copper_torch" => Animate::Torch(T::CopperFireFlame), + "wall_torch" => Animate::WallTorch(T::Flame, facing()), + "soul_wall_torch" => Animate::WallTorch(T::SoulFireFlame, facing()), + "copper_wall_torch" => Animate::WallTorch(T::CopperFireFlame, facing()), + "redstone_torch" if lit => Animate::RedstoneTorch, + "redstone_wall_torch" if lit => Animate::RedstoneWallTorch(facing()), + "furnace" if lit => Animate::Furnace(facing()), + "blast_furnace" if lit => Animate::BlastFurnace(facing()), + "smoker" if lit => Animate::Smoker, + "campfire" if lit => Animate::Campfire { + spawn_particles: true, + }, + "soul_campfire" if lit => Animate::Campfire { + spawn_particles: false, + }, + "oak_leaves" | "birch_leaves" | "jungle_leaves" | "acacia_leaves" | "dark_oak_leaves" + | "mangrove_leaves" => Animate::Leaves(Some((T::TintedLeaves, 0.01))), + "cherry_leaves" => Animate::Leaves(Some((T::CherryLeaves, 0.1))), + "pale_oak_leaves" => Animate::Leaves(Some((T::PaleOakLeaves, 0.02))), + "spruce_leaves" | "azalea_leaves" | "flowering_azalea_leaves" => Animate::Leaves(None), + "nether_portal" => Animate::NetherPortal, + "enchanting_table" => Animate::EnchantingTable, + "end_rod" => Animate::EndRod(facing()), + "spore_blossom" => Animate::SporeBlossom, + "crying_obsidian" => Animate::CryingObsidian, + "mycelium" => Animate::Mycelium, + "brewing_stand" => Animate::BrewingStand, + "ender_chest" => Animate::EnderChest, + "sand" => Animate::Falling(0xDBD3A0), + "red_sand" => Animate::Falling(0xA95821), + "gravel" => Animate::Falling(0x807C7B), + "redstone_ore" | "deepslate_redstone_ore" if lit => Animate::RedstoneOre, + "redstone_wire" => { + let power = prop("power").and_then(|p| p.parse().ok()).unwrap_or(0); + let side = |d: &str| match prop(d).as_deref() { + Some("up") => 2, + Some("side") => 1, + _ => 0, + }; + if power == 0 { + Animate::None + } else { + Animate::RedstoneWire { + power, + sides: [side("north"), side("east"), side("south"), side("west")], + } + } + } + "respawn_anchor" if prop("charges").is_some_and(|c| c != "0") => Animate::RespawnAnchor, + "bubble_column" => Animate::BubbleColumn { + drag: prop("drag").as_deref() == Some("true"), + }, + "fire" | "soul_fire" => Animate::Fire, + _ if name.ends_with("candle_cake") && lit => Animate::CandleCake, + _ if (name == "candle" || name.ends_with("_candle")) && lit => { + Animate::Candles(prop("candles").and_then(|c| c.parse().ok()).unwrap_or(1)) + } + _ => Animate::None, + } +} +/// `RedstoneWireBlock.COLORS`. +fn redstone_color(power: u8) -> [f32; 3] { + let f = f32::from(power) / 15.0; + let r = f * 0.6 + if f > 0.0 { 0.4 } else { 0.3 }; + let g = (f * f * 0.7 - 0.5).clamp(0.0, 1.0); + let b = (f * f * 0.6 - 0.7).clamp(0.0, 1.0); + // DustParticleOptions stores the packed int colour. + let q = |c: f32| ((c * 255.0) as u8) as f32 / 255.0; + [q(r), q(g), q(b)] +} +/// `CandleBlock.PARTICLE_OFFSETS` by candle count (pixels). +fn candle_offsets(count: u8) -> &'static [[f64; 3]] { + match count { + 1 => &[[8.0, 8.0, 8.0]], + 2 => &[[6.0, 7.0, 8.0], [10.0, 8.0, 7.0]], + 3 => &[[8.0, 5.0, 10.0], [6.0, 7.0, 8.0], [9.0, 8.0, 7.0]], + _ => &[ + [7.0, 5.0, 9.0], + [10.0, 7.0, 9.0], + [6.0, 7.0, 6.0], + [9.0, 8.0, 6.0], + ], + } +} + +/// Per-state animate behaviour and the block tags it needs. +pub struct AnimateTables { + kinds: Vec, + impermeable: Vec, + power_provider: Vec, + power_transmitter: Vec, + /// Biome id -> ambient particles and probabilities (`minecraft:visual/ambient_particles`). + ambient: Vec>, +} +impl AnimateTables { + pub fn load(root: &Path, registry: &BlockRegistry, biomes: &Biomes) -> crate::Result { + let kinds = (0..registry.len() as u32) + .map(|id| { + registry.state(BlockStateId(id)).map_or(Animate::None, |s| { + classify(&s.name, &|k| s.properties.get(k).cloned()) + }) + }) + .collect(); + let bundle: BTreeMap = + serde_json::from_slice(&std::fs::read(root.join("block-tags.json"))?)?; + let mut definitions = TagDefinitions::default(); + for (name, text) in bundle { + definitions.apply_json(&name, "vanilla", &text)?; + } + let built = + definitions.build(|name, _| registry.default_state(name).map(|_| name.to_owned()))?; + let by_state = |tag: &str| -> Vec { + let names = built.tags.get(tag); + (0..registry.len() as u32) + .map(|id| { + registry + .state(BlockStateId(id)) + .is_some_and(|s| names.is_some_and(|n| n.iter().any(|t| *t == s.name))) + }) + .collect() + }; + let mut ambient = vec![Vec::new(); biomes.list.len()]; + for (name, &id) in &biomes.index { + let path = root.join(format!( + "data/minecraft/worldgen/biome/{}.json", + name.trim_start_matches("minecraft:") + )); + let Ok(bytes) = std::fs::read(path) else { + continue; + }; + let Ok(json) = serde_json::from_slice::(&bytes) else { + continue; + }; + let list = &json["attributes"]["minecraft:visual/ambient_particles"]; + let entries = list["argument"].as_array().or_else(|| list.as_array()); + for entry in entries.into_iter().flatten() { + let ty = entry["particle"]["type"].as_str().and_then(T::from_name); + let probability = entry["probability"].as_f64().unwrap_or(0.0) as f32; + if let (Some(ty), Some(slot)) = (ty, ambient.get_mut(id as usize)) { + slot.push((ty, probability)); + } + } + } + Ok(Self { + kinds, + impermeable: by_state("minecraft:impermeable"), + power_provider: by_state("minecraft:enchantment_power_provider"), + power_transmitter: by_state("minecraft:enchantment_power_transmitter"), + ambient, + }) + } + fn kind(&self, state: BlockStateId) -> Animate { + self.kinds + .get(state.0 as usize) + .copied() + .unwrap_or(Animate::None) + } + /// Whether a state ticks campfire particles (`CampfireBlockEntity.particleTick`). + pub fn is_lit_campfire(&self, state: u32) -> bool { + matches!(self.kind(BlockStateId(state)), Animate::Campfire { .. }) + } +} + +/// World access for ambient effects. +pub struct AnimateContext<'a> { + pub world: &'a World, + pub facts: &'a BlockFacts, + pub collision: &'a BlockCollisionRegistry<'a>, + pub biomes: &'a Biomes, + pub registry: &'a BlockRegistry, + /// `Level.isRainingAt`. + pub raining_at: &'a dyn Fn([i32; 3]) -> bool, +} +impl AnimateContext<'_> { + fn state(&self, p: [i32; 3]) -> BlockStateId { + self.world.block(p[0], p[1], p[2]) + } + fn is_air(&self, p: [i32; 3]) -> bool { + self.facts.is_air(self.state(p)) + } + /// `isFaceSturdy` approximated by a full collision cube. + fn sturdy(&self, p: [i32; 3]) -> bool { + self.facts.get(self.state(p)).full_collision + } + fn shape_y(&self, p: [i32; 3]) -> Option<(f64, f64, [f64; 4])> { + let shape = self + .collision + .cached_shapes() + .cached_shape(self.state(p)) + .ok()??; + if shape.is_empty() { + return Some((0.0, 0.0, [0.0; 4])); + } + Some(( + shape.min(Axis::Y), + shape.max(Axis::Y), + [ + shape.min(Axis::X), + shape.max(Axis::X), + shape.min(Axis::Z), + shape.max(Axis::Z), + ], + )) + } + fn foliage(&self, p: [i32; 3]) -> u32 { + let biome = self + .world + .chunks + .get(&(p[0] >> 4, p[2] >> 4)) + .map_or(0, |c| c.biome(p[0], p[1], p[2])); + self.biomes.get(biome).foliage + } +} + +fn add(engine: &mut ParticleEngine, ty: T, p: [f64; 3], v: [f64; 3]) { + engine.add(&ParticleOptions::simple(ty), p, v); +} +fn sound(engine: &mut ParticleEngine, name: &'static str, p: [f64; 3], volume: f32, pitch: f32) { + engine.sounds.push((name, p, volume, pitch)); +} +fn redstone(engine: &mut ParticleEngine, p: [f64; 3]) { + engine.add(&ParticleOptions::dust([1.0, 0.0, 0.0], 1.0), p, [0.0; 3]); +} + +/// `ClientLevel.animateTick` around the player's block position. +pub fn animate_tick( + tables: &AnimateTables, + engine: &mut ParticleEngine, + ctx: &AnimateContext, + player: [i32; 3], +) { + for _ in 0..667 { + do_animate_tick(tables, engine, ctx, player, 16); + do_animate_tick(tables, engine, ctx, player, 32); + } +} +fn next_int(r: &mut LegacyRandom, bound: i32) -> i32 { + r.next_int_bound(bound).unwrap_or(0) +} +fn do_animate_tick( + tables: &AnimateTables, + engine: &mut ParticleEngine, + ctx: &AnimateContext, + center: [i32; 3], + r: i32, +) { + let random = engine.random(); + let x = center[0] + next_int(random, r) - next_int(random, r); + let y = center[1] + next_int(random, r) - next_int(random, r); + let z = center[2] + next_int(random, r) - next_int(random, r); + let pos = [x, y, z]; + if !ctx.world.loaded(x, z) { + return; + } + let state = ctx.state(pos); + block_animate_tick(tables, engine, ctx, state, pos); + let facts = ctx.facts.get(state); + if facts.fluid != Fluid::None { + fluid_animate_tick( + engine, + ctx, + pos, + facts.fluid, + facts.fluid_level, + facts.fluid_falling, + ); + if next_int(engine.random(), 10) == 0 { + let drip = if facts.fluid == Fluid::Water { + T::DrippingWater + } else { + T::DrippingLava + }; + let top_solid = ctx.sturdy(pos); + try_spawn_drip(tables, engine, ctx, [x, y - 1, z], drip, top_solid); + } + } + if !facts.full_collision { + let biome = ctx + .world + .chunks + .get(&(x >> 4, z >> 4)) + .map_or(0, |c| c.biome(x, y, z)); + for &(ty, probability) in tables.ambient.get(biome as usize).into_iter().flatten() { + // AmbientParticle.canSpawn: random.nextFloat() <= probability. + let random = engine.random(); + if random.next_float() <= probability { + let p = [ + f64::from(x) + random.next_double(), + f64::from(y) + random.next_double(), + f64::from(z) + random.next_double(), + ]; + add(engine, ty, p, [0.0; 3]); + } + } + } +} +/// `ClientLevel.trySpawnDripParticles`. +fn try_spawn_drip( + tables: &AnimateTables, + engine: &mut ParticleEngine, + ctx: &AnimateContext, + pos: [i32; 3], + drip: T, + top_solid: bool, +) { + let state = ctx.state(pos); + if ctx.facts.get(state).fluid != Fluid::None { + return; + } + let Some((bottom, top, bounds)) = ctx.shape_y(pos) else { + return; + }; + let spawn = |engine: &mut ParticleEngine, x1: f64, x2: f64, z1: f64, z2: f64, y: f64| { + let r = engine.random(); + let (a, b) = (r.next_double(), r.next_double()); + add( + engine, + drip, + [x1 + (x2 - x1) * a, y, z1 + (z2 - z1) * b], + [0.0; 3], + ); + }; + let (fx, fy, fz) = (f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])); + if top < 1.0 { + if top_solid { + spawn(engine, fx, fx + 1.0, fz, fz + 1.0, fy + 1.0 - 0.05); + } + } else if !tables + .impermeable + .get(state.0 as usize) + .copied() + .unwrap_or(false) + { + if bottom > 0.0 { + spawn( + engine, + fx + bounds[0], + fx + bounds[1], + fz + bounds[2], + fz + bounds[3], + fy + bottom - 0.05, + ); + } else { + let below = [pos[0], pos[1] - 1, pos[2]]; + let below_top = ctx.shape_y(below).map_or(1.0, |s| s.1); + if below_top < 1.0 && ctx.facts.get(ctx.state(below)).fluid == Fluid::None { + spawn( + engine, + fx + bounds[0], + fx + bounds[1], + fz + bounds[2], + fz + bounds[3], + fy - 0.05, + ); + } + } + } +} +/// `WaterFluid.animateTick` / `LavaFluid.animateTick`. +fn fluid_animate_tick( + engine: &mut ParticleEngine, + ctx: &AnimateContext, + pos: [i32; 3], + fluid: Fluid, + level: u8, + falling: bool, +) { + let (fx, fy, fz) = (f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])); + if fluid == Fluid::Water { + let source = level >= 8 && !falling; + let random = engine.random(); + if !source && !falling { + if next_int(random, 64) == 0 { + let volume = random.next_float() * 0.25 + 0.75; + let pitch = random.next_float() + 0.5; + sound( + engine, + "minecraft:block.water.ambient", + [fx + 0.5, fy + 0.5, fz + 0.5], + volume, + pitch, + ); + } + } else if next_int(random, 10) == 0 { + let p = [ + fx + random.next_double(), + fy + random.next_double(), + fz + random.next_double(), + ]; + add(engine, T::Underwater, p, [0.0; 3]); + } + return; + } + let above = [pos[0], pos[1] + 1, pos[2]]; + if ctx.is_air(above) { + let random = engine.random(); + if next_int(random, 100) == 0 { + let p = [ + fx + random.next_double(), + fy + 1.0, + fz + random.next_double(), + ]; + add(engine, T::Lava, p, [0.0; 3]); + let random = engine.random(); + let (volume, pitch) = ( + 0.2 + random.next_float() * 0.2, + 0.9 + random.next_float() * 0.15, + ); + sound(engine, "minecraft:block.lava.pop", p, volume, pitch); + } + let random = engine.random(); + if next_int(random, 200) == 0 { + let (volume, pitch) = ( + 0.2 + random.next_float() * 0.2, + 0.9 + random.next_float() * 0.15, + ); + sound( + engine, + "minecraft:block.lava.ambient", + [fx, fy, fz], + volume, + pitch, + ); + } + } +} +#[allow(clippy::too_many_lines)] +fn block_animate_tick( + tables: &AnimateTables, + engine: &mut ParticleEngine, + ctx: &AnimateContext, + state: BlockStateId, + pos: [i32; 3], +) { + let (fx, fy, fz) = (f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])); + match tables.kind(state) { + Animate::None => {} + Animate::Torch(flame) => { + let p = [fx + 0.5, fy + 0.7, fz + 0.5]; + add(engine, T::Smoke, p, [0.0; 3]); + add(engine, flame, p, [0.0; 3]); + } + Animate::WallTorch(flame, facing) => { + let p = [ + fx + 0.5 - 0.27 * f64::from(facing[0]), + fy + 0.7 + 0.22, + fz + 0.5 - 0.27 * f64::from(facing[2]), + ]; + add(engine, T::Smoke, p, [0.0; 3]); + add(engine, flame, p, [0.0; 3]); + } + Animate::RedstoneTorch => { + let r = engine.random(); + let p = [ + fx + 0.5 + (r.next_double() - 0.5) * 0.2, + fy + 0.7 + (r.next_double() - 0.5) * 0.2, + fz + 0.5 + (r.next_double() - 0.5) * 0.2, + ]; + redstone(engine, p); + } + Animate::RedstoneWallTorch(facing) => { + let r = engine.random(); + let p = [ + fx + 0.5 + (r.next_double() - 0.5) * 0.2 - 0.27 * f64::from(facing[0]), + fy + 0.7 + (r.next_double() - 0.5) * 0.2 + 0.22, + fz + 0.5 + (r.next_double() - 0.5) * 0.2 - 0.27 * f64::from(facing[2]), + ]; + redstone(engine, p); + } + Animate::Furnace(facing) | Animate::BlastFurnace(facing) => { + let blast = matches!(tables.kind(state), Animate::BlastFurnace(_)); + let (x, y, z) = (fx + 0.5, fy, fz + 0.5); + let r = engine.random(); + if r.next_double() < 0.1 { + let name = if blast { + "minecraft:block.blastfurnace.fire_crackle" + } else { + "minecraft:block.furnace.fire_crackle" + }; + sound(engine, name, [x, y, z], 1.0, 1.0); + } + let r = engine.random(); + let ss = r.next_double() * 0.6 - 0.3; + let dx = if facing[0] != 0 { + f64::from(facing[0]) * 0.52 + } else { + ss + }; + let dy = r.next_double() * if blast { 9.0 } else { 6.0 } / 16.0; + let dz = if facing[2] != 0 { + f64::from(facing[2]) * 0.52 + } else { + ss + }; + let p = [x + dx, y + dy, z + dz]; + add(engine, T::Smoke, p, [0.0; 3]); + if !blast { + add(engine, T::Flame, p, [0.0; 3]); + } + } + Animate::Smoker => { + let r = engine.random(); + if r.next_double() < 0.1 { + sound( + engine, + "minecraft:block.smoker.smoke", + [fx + 0.5, fy, fz + 0.5], + 1.0, + 1.0, + ); + } + add(engine, T::Smoke, [fx + 0.5, fy + 1.1, fz + 0.5], [0.0; 3]); + } + Animate::Campfire { spawn_particles } => { + let r = engine.random(); + if next_int(r, 10) == 0 { + let (volume, pitch) = (0.5 + r.next_float(), r.next_float() * 0.7 + 0.6); + sound( + engine, + "minecraft:block.campfire.crackle", + [fx + 0.5, fy + 0.5, fz + 0.5], + volume, + pitch, + ); + } + let r = engine.random(); + if spawn_particles && next_int(r, 5) == 0 { + let count = next_int(r, 1) + 1; + for _ in 0..count { + let r = engine.random(); + let v = [ + f64::from(r.next_float() / 2.0), + 5.0e-5, + f64::from(r.next_float() / 2.0), + ]; + add(engine, T::Lava, [fx + 0.5, fy + 0.5, fz + 0.5], v); + } + } + } + Animate::Leaves(falling) => { + let below = [pos[0], pos[1] - 1, pos[2]]; + // LeavesBlock.makeDrippingWaterParticles. + if (ctx.raining_at)([pos[0], pos[1] + 1, pos[2]]) && next_int(engine.random(), 15) == 1 + { + let below_facts = ctx.facts.get(ctx.state(below)); + if !below_facts.can_occlude || !ctx.sturdy(below) { + let r = engine.random(); + let p = [fx + r.next_double(), fy - 0.05, fz + r.next_double()]; + add(engine, T::DrippingWater, p, [0.0; 3]); + } + } + // FallingParticlesLeavesBlock.makeFallingLeavesParticles. + if let Some((ty, chance)) = falling + && engine.random().next_float() < chance + && !ctx.sturdy(below) + { + let r = engine.random(); + let p = [fx + r.next_double(), fy - 0.05, fz + r.next_double()]; + let options = if ty == T::TintedLeaves { + let c = ctx.foliage(pos); + let f = |s: u32| ((c >> s) & 255) as f32 / 255.0; + ParticleOptions::colored(ty, [f(16), f(8), f(0), 1.0]) + } else { + ParticleOptions::simple(ty) + }; + engine.add(&options, p, [0.0; 3]); + } + } + Animate::NetherPortal => { + let r = engine.random(); + if next_int(r, 100) == 0 { + let pitch = r.next_float() * 0.4 + 0.8; + sound( + engine, + "minecraft:block.portal.ambient", + [fx + 0.5, fy + 0.5, fz + 0.5], + 0.5, + pitch, + ); + } + let along_z = ctx.state([pos[0] - 1, pos[1], pos[2]]) == state + || ctx.state([pos[0] + 1, pos[1], pos[2]]) == state; + for _ in 0..4 { + let r = engine.random(); + let mut p = [ + fx + r.next_double(), + fy + r.next_double(), + fz + r.next_double(), + ]; + let mut v = [ + f64::from(r.next_float() - 0.5) * 0.5, + f64::from(r.next_float() - 0.5) * 0.5, + f64::from(r.next_float() - 0.5) * 0.5, + ]; + let flip = f64::from(next_int(r, 2) * 2 - 1); + if along_z { + p[2] = fz + 0.5 + 0.25 * flip; + v[2] = f64::from(r.next_float() * 2.0) * flip; + } else { + p[0] = fx + 0.5 + 0.25 * flip; + v[0] = f64::from(r.next_float() * 2.0) * flip; + } + add(engine, T::Portal, p, v); + } + } + Animate::EnchantingTable => { + for ox in -2i32..=2 { + for oy in 0..=1 { + for oz in -2i32..=2 { + if ox.abs() != 2 && oz.abs() != 2 { + continue; + } + if next_int(engine.random(), 16) != 0 { + continue; + } + let provider = ctx.state([pos[0] + ox, pos[1] + oy, pos[2] + oz]); + let between = ctx.state([pos[0] + ox / 2, pos[1] + oy, pos[2] + oz / 2]); + if tables + .power_provider + .get(provider.0 as usize) + .copied() + .unwrap_or(false) + && tables + .power_transmitter + .get(between.0 as usize) + .copied() + .unwrap_or(false) + { + let r = engine.random(); + let v = [ + f64::from(ox as f32 + r.next_float() - 0.5), + f64::from(oy as f32 - r.next_float() - 1.0), + f64::from(oz as f32 + r.next_float() - 0.5), + ]; + add(engine, T::Enchant, [fx + 0.5, fy + 2.0, fz + 0.5], v); + } + } + } + } + } + Animate::EndRod(facing) => { + let r = engine.random(); + let x = fx + 0.55 - f64::from(r.next_float() * 0.1); + let y = fy + 0.55 - f64::from(r.next_float() * 0.1); + let z = fz + 0.55 - f64::from(r.next_float() * 0.1); + let d = f64::from(0.4 - (r.next_float() + r.next_float()) * 0.4); + if next_int(r, 5) == 0 { + let v = [ + r.next_gaussian() * 0.005, + r.next_gaussian() * 0.005, + r.next_gaussian() * 0.005, + ]; + let p = [ + x + f64::from(facing[0]) * d, + y + f64::from(facing[1]) * d, + z + f64::from(facing[2]) * d, + ]; + add(engine, T::EndRod, p, v); + } + } + Animate::SporeBlossom => { + let r = engine.random(); + let p = [fx + r.next_double(), fy + 0.7, fz + r.next_double()]; + add(engine, T::FallingSporeBlossom, p, [0.0; 3]); + for _ in 0..14 { + let r = engine.random(); + let a = [ + pos[0] + next_int(r, 21) - 10, + pos[1] - next_int(r, 10), + pos[2] + next_int(r, 21) - 10, + ]; + if !ctx.facts.get(ctx.state(a)).full_collision { + let r = engine.random(); + let p = [ + f64::from(a[0]) + r.next_double(), + f64::from(a[1]) + r.next_double(), + f64::from(a[2]) + r.next_double(), + ]; + add(engine, T::SporeBlossomAir, p, [0.0; 3]); + } + } + } + Animate::CryingObsidian => { + let r = engine.random(); + if next_int(r, 5) == 0 { + // Direction.getRandom: down, up, north, south, west, east. + let d = [ + [0, -1, 0], + [0, 1, 0], + [0, 0, -1], + [0, 0, 1], + [-1, 0, 0], + [1, 0, 0], + ][next_int(r, 6) as usize]; + if d != [0, 1, 0] { + let relative = [pos[0] + d[0], pos[1] + d[1], pos[2] + d[2]]; + if !ctx.sturdy(relative) { + let r = engine.random(); + let off = |s: i32, r: &mut LegacyRandom| { + if s == 0 { + r.next_double() + } else { + 0.5 + f64::from(s) * 0.6 + } + }; + let p = [fx + off(d[0], r), fy + off(d[1], r), fz + off(d[2], r)]; + add(engine, T::DrippingObsidianTear, p, [0.0; 3]); + } + } + } + } + Animate::Mycelium => { + let r = engine.random(); + if next_int(r, 10) == 0 { + let p = [fx + r.next_double(), fy + 1.1, fz + r.next_double()]; + add(engine, T::Mycelium, p, [0.0; 3]); + } + } + Animate::Candles(count) => { + for o in candle_offsets(count) { + candle( + engine, + [fx + o[0] / 16.0, fy + o[1] / 16.0, fz + o[2] / 16.0], + ); + } + } + Animate::CandleCake => candle(engine, [fx + 0.5, fy + 1.0, fz + 0.5]), + Animate::BrewingStand => { + let r = engine.random(); + let p = [ + fx + 0.4 + f64::from(r.next_float() * 0.2), + fy + 0.7 + f64::from(r.next_float() * 0.3), + fz + 0.4 + f64::from(r.next_float() * 0.2), + ]; + add(engine, T::Smoke, p, [0.0; 3]); + } + Animate::EnderChest => { + for _ in 0..3 { + let r = engine.random(); + let flip_x = f64::from(next_int(r, 2) * 2 - 1); + let flip_z = f64::from(next_int(r, 2) * 2 - 1); + let p = [ + fx + 0.5 + 0.25 * flip_x, + fy + f64::from(r.next_float()), + fz + 0.5 + 0.25 * flip_z, + ]; + let v = [ + f64::from(r.next_float()) * flip_x, + f64::from(r.next_float() - 0.5) * 0.125, + f64::from(r.next_float()) * flip_z, + ]; + add(engine, T::Portal, p, v); + } + } + Animate::Falling(color) => { + if next_int(engine.random(), 16) == 0 { + let below = ctx.facts.get(ctx.state([pos[0], pos[1] - 1, pos[2]])); + // FallingBlock.isFree: air, fire, liquids or replaceable. + if below.replaceable + || below.fluid != Fluid::None + || ctx.is_air([pos[0], pos[1] - 1, pos[2]]) + { + let r = engine.random(); + let p = [fx + r.next_double(), fy - 0.05, fz + r.next_double()]; + engine.add( + &ParticleOptions::block(T::FallingDust, String::new(), color), + p, + [0.0; 3], + ); + } + } + } + Animate::RedstoneOre => { + for d in [ + [0, -1, 0], + [0, 1, 0], + [0, 0, -1], + [0, 0, 1], + [-1, 0, 0], + [1, 0, 0], + ] { + let relative = [pos[0] + d[0], pos[1] + d[1], pos[2] + d[2]]; + if ctx.facts.get(ctx.state(relative)).solid_render { + continue; + } + let r = engine.random(); + let mut c = |s: i32| { + if s != 0 { + 0.5 + 0.5625 * f64::from(s) + } else { + f64::from(r.next_float()) + } + }; + let p = [fx + c(d[0]), fy + c(d[1]), fz + c(d[2])]; + redstone(engine, p); + } + } + Animate::RedstoneWire { power, sides } => { + let color = redstone_color(power); + let horizontals = [[0, 0, -1], [1, 0, 0], [0, 0, 1], [-1, 0, 0]]; + let line = |engine: &mut ParticleEngine, + side: [i32; 3], + along: [i32; 3], + from: f32, + to: f32| { + let span = to - from; + let r = engine.random(); + if r.next_float() < 0.2 * span { + let on = from + span * r.next_float(); + let c = |i: usize| { + 0.5 + 0.4375 * f64::from(side[i]) + f64::from(on) * f64::from(along[i]) + }; + engine.add( + &ParticleOptions::dust(color, 1.0), + [fx + c(0), fy + c(1), fz + c(2)], + [0.0; 3], + ); + } + }; + for (i, h) in horizontals.into_iter().enumerate() { + match sides[i] { + 2 => { + line(engine, h, [0, 1, 0], -0.5, 0.5); + line(engine, [0, -1, 0], h, 0.0, 0.5); + } + 1 => line(engine, [0, -1, 0], h, 0.0, 0.5), + _ => line(engine, [0, -1, 0], h, 0.0, 0.3), + } + } + } + Animate::RespawnAnchor => { + let r = engine.random(); + if next_int(r, 100) == 0 { + sound( + engine, + "minecraft:block.respawn_anchor.ambient", + [fx + 0.5, fy + 0.5, fz + 0.5], + 1.0, + 1.0, + ); + } + let r = engine.random(); + let p = [ + fx + 0.5 + (0.5 - r.next_double()), + fy + 1.0, + fz + 0.5 + (0.5 - r.next_double()), + ]; + let ya = f64::from(r.next_float() * 0.04); + add(engine, T::ReversePortal, p, [0.0, ya, 0.0]); + } + Animate::BubbleColumn { drag } => { + if drag { + add(engine, T::CurrentDown, [fx + 0.5, fy + 0.8, fz], [0.0; 3]); + let r = engine.random(); + if next_int(r, 200) == 0 { + let (volume, pitch) = (0.2 + r.next_float() * 0.2, 0.9 + r.next_float() * 0.15); + sound( + engine, + "minecraft:block.bubble_column.whirlpool_ambient", + [fx, fy, fz], + volume, + pitch, + ); + } + } else { + add( + engine, + T::BubbleColumnUp, + [fx + 0.5, fy, fz + 0.5], + [0.0, 0.04, 0.0], + ); + let r = engine.random(); + let p = [ + fx + f64::from(r.next_float()), + fy + f64::from(r.next_float()), + fz + f64::from(r.next_float()), + ]; + add(engine, T::BubbleColumnUp, p, [0.0, 0.04, 0.0]); + let r = engine.random(); + if next_int(r, 200) == 0 { + let (volume, pitch) = (0.2 + r.next_float() * 0.2, 0.9 + r.next_float() * 0.15); + sound( + engine, + "minecraft:block.bubble_column.upwards_ambient", + [fx, fy, fz], + volume, + pitch, + ); + } + } + } + Animate::Fire => { + let r = engine.random(); + if next_int(r, 24) == 0 { + let (volume, pitch) = (1.0 + r.next_float(), r.next_float() * 0.7 + 0.3); + sound( + engine, + "minecraft:block.fire.ambient", + [fx + 0.5, fy + 0.5, fz + 0.5], + volume, + pitch, + ); + } + } + } +} +/// `AbstractCandleBlock.addParticlesAndSound`. +fn candle(engine: &mut ParticleEngine, p: [f64; 3]) { + let r = engine.random(); + let chance = r.next_float(); + if chance < 0.3 { + add(engine, T::Smoke, p, [0.0; 3]); + if chance < 0.17 { + let r = engine.random(); + let (volume, pitch) = (1.0 + r.next_float(), r.next_float() * 0.7 + 0.3); + sound( + engine, + "minecraft:block.candle.ambient", + [p[0] + 0.5, p[1] + 0.5, p[2] + 0.5], + volume, + pitch, + ); + } + } + add(engine, T::SmallFlame, p, [0.0; 3]); +} +/// `CampfireBlockEntity.particleTick` for every lit campfire (no cooking items). +pub fn campfire_particles<'a>( + tables: &AnimateTables, + engine: &mut ParticleEngine, + ctx: &AnimateContext, + blocks: impl Iterator, +) { + for &(pos, state) in blocks { + if !tables.is_lit_campfire(state) { + continue; + } + let signal = ctx + .registry + .state(BlockStateId(state)) + .is_some_and(|s| s.properties.get("signal_fire").map(String::as_str) == Some("true")); + let r = engine.random(); + if r.next_float() < 0.11 { + let count = next_int(r, 2) + 2; + for _ in 0..count { + make_campfire_smoke(engine, pos, signal); + } + } + } +} +/// `CampfireBlock.makeParticles` (always visible, overriding the limiter). +pub fn make_campfire_smoke(engine: &mut ParticleEngine, pos: [i32; 3], signal: bool) { + let r = engine.random(); + let sign = |r: &mut LegacyRandom| if r.next_boolean() { 1.0 } else { -1.0 }; + let dx = r.next_double() / 3.0; + let x = f64::from(pos[0]) + 0.5 + dx * sign(r); + let y = f64::from(pos[1]) + r.next_double() + r.next_double(); + let dz = r.next_double() / 3.0; + let z = f64::from(pos[2]) + 0.5 + dz * sign(r); + let ty = if signal { + T::CampfireSignalSmoke + } else { + T::CampfireCosySmoke + }; + engine.create(&ParticleOptions::simple(ty), [x, y, z], [0.0, 0.07, 0.0]); +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn classifies_animated_blocks() { + let none = |_: &str| None; + assert_eq!(classify("minecraft:torch", &none), Animate::Torch(T::Flame)); + let lit = |k: &str| (k == "lit").then(|| "true".to_owned()); + assert_eq!( + classify("minecraft:furnace", &lit), + Animate::Furnace([0, -1, 0]) + ); + assert_eq!(classify("minecraft:furnace", &none), Animate::None); + let wall = |k: &str| (k == "facing").then(|| "north".to_owned()); + assert_eq!( + classify("minecraft:soul_wall_torch", &wall), + Animate::WallTorch(T::SoulFireFlame, [0, 0, -1]) + ); + let candles = |k: &str| match k { + "lit" => Some("true".to_owned()), + "candles" => Some("3".to_owned()), + _ => None, + }; + assert_eq!( + classify("minecraft:red_candle", &candles), + Animate::Candles(3) + ); + assert_eq!( + classify("minecraft:red_candle_cake", &lit), + Animate::CandleCake + ); + } + #[test] + fn redstone_colours_match_wire_table() { + // RedstoneWireBlock.COLORS[15] = (1.0, 0.2, 0.1) before int packing. + let c = redstone_color(15); + assert!((c[0] - 1.0).abs() < 1e-6); + assert!((c[1] - (51.0 / 255.0)).abs() < 0.01); + } +} diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a6ff6a17b..35f8b04a6 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2,6 +2,7 @@ //! block interaction, inventory and HUD. mod control; mod render_control; +mod render_tick; use horizon_client::{ Result, assets::Assets, @@ -278,6 +279,7 @@ pub struct Resources { mob_types: HashMap>, providers: Providers, block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer, + animate: horizon_client::animate_tick::AnimateTables, nether_environment: Environment, behaviours: Behaviours, names: Names, @@ -340,7 +342,7 @@ struct Game<'a> { audio: Audio, dropped: Vec, next_entity: u32, - particles: Vec, + particles: horizon_client::particles::ParticleEngine, spawn_search: u32, mobs: Vec, arrows: Vec, @@ -1397,13 +1399,7 @@ impl Game<'_> { let eye = self.player.as_ref().map(|p| [p.position.x, p.position.y + f64::from(self.eye), p.position.z]); self.res.block_entity_render.tick(r.block_entities(), eye); } - let facts = &self.res.facts; - let world = &self.world; - self.particles.retain_mut(|p| p.tick(world, |id| facts.states[id as usize].full_collision)); - if self.particles.len() > 4000 { - let excess = self.particles.len() - 4000; - self.particles.drain(..excess); - } + self.tick_particles(); if self.right && self.use_delay == 0 && self.screen == Screen::Playing { self.use_item()?; self.use_delay = 4; @@ -2190,34 +2186,10 @@ impl Game<'_> { } } } - // Camera-facing particle quads. - let (yaw, pitch) = (self.yaw.to_radians(), self.pitch.to_radians()); - let forward = glam::Vec3::new(-yaw.sin() * pitch.cos(), -pitch.sin(), yaw.cos() * pitch.cos()); - let right = forward.cross(glam::Vec3::Y).normalize_or_zero(); - let up = right.cross(forward).normalize_or_zero(); + // Particles (ParticleEngine.extract): camera-facing quads. + let world = &self.world; + self.particles.render(camera, self.yaw, self.pitch, alpha as f32, &|b| world.light(b[0], b[1], b[2]), &mut out); let atlas = &self.res.atlas; - for p in &self.particles { - let Some(r) = atlas.sprites.get(&p.sprite).copied() else { continue }; - let a = alpha; - let pos = glam::DVec3::new( - p.previous[0] + (p.position[0] - p.previous[0]) * a, - p.previous[1] + (p.position[1] - p.previous[1]) * a, - p.previous[2] + (p.position[2] - p.previous[2]) * a, - ) - camera; - let c = pos.as_vec3(); - let light = self.world.light(p.position[0].floor() as i32, p.position[1].floor() as i32, p.position[2].floor() as i32); - let packed = u32::from(light >> 4) * 16 | (u32::from(light & 15) * 16) << 8; - // TerrainParticle colour: 0.6 base brightness times the block tint. - let ch = |sft: u32| (((p.tint >> sft) & 255) as f32 * 0.6) as u32; - let color = 0xFF00_0000 | ch(0) << 16 | ch(8) << 8 | ch(16); - let (du, dv) = (r.u1 - r.u0, r.v1 - r.v0); - let (u0, v0, u1, v1) = (r.u0 + du * p.uv[0], r.v0 + dv * p.uv[1], r.u0 + du * p.uv[2], r.v0 + dv * p.uv[3]); - let s = p.size; - for (k, uv) in [(-1.0, -1.0), (1.0, -1.0), (1.0, 1.0), (-1.0, 1.0)].iter().zip([[u0, v1], [u1, v1], [u1, v0], [u0, v0]]) { - let v = c + right * (k.0 * s) + up * (k.1 * s); - out.push(Vertex { pos: v.to_array(), uv, layer: 0, color, light: packed }); - } - } // Breaking progress overlay (destroy_stage_0..9). if let Some(m) = &self.mining && m.progress > 0.0 @@ -4658,6 +4630,7 @@ pub fn run() -> Result<()> { let audio = Audio::new(&root); let world = World::new(facts.air, 0); let creative = options.creative; + let animate = horizon_client::animate_tick::AnimateTables::load(&root, registry_owner.registry, &biomes)?; let mut resources = Resources { assets, facts, @@ -4670,6 +4643,7 @@ pub fn run() -> Result<()> { layer_names, providers: Providers::load(&root), block_entity_render: horizon_client::block_entity_render::BlockEntityRenderer::load(&root, ®istry_owner.registry)?, + animate, nether_environment: Environment::load_dimension(&root, "the_nether", false)?, behaviours: Behaviours::load(registry_owner.registry, &root)?, names: Names::load(&root), @@ -4691,6 +4665,7 @@ pub fn run() -> Result<()> { settings.render_distance = d; } let look = options.look; + let particles = horizon_client::particles::ParticleEngine::new(&root, &resources.atlas.sprites); let mut game = Game { res: &mut resources, collision: &collision, @@ -4741,7 +4716,7 @@ pub fn run() -> Result<()> { audio, dropped: Vec::new(), next_entity: 0, - particles: Vec::new(), + particles, spawn_search: 0, mobs: Vec::new(), arrows: Vec::new(), diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 64eeb7a78..1f46c678b 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -66,7 +66,7 @@ impl Game<'_> { let ok = || Ok(Some(json!({ "ok": true }))); match words[0] { "help" => Ok(Some(json!({ "commands": HELP.iter().chain(super::render_control::HELP).collect::>() }))), - "setblock" | "fill" => self.render_command(&words), + "setblock" | "fill" | "particle" => self.render_command(&words), "status" => Ok(Some(self.control_status())), "key" => { let key = key_code(arg(1)?).ok_or_else(|| format!("unknown key {}", words[1]))?; diff --git a/crates/horizon-client/src/game/render_control.rs b/crates/horizon-client/src/game/render_control.rs index c425988ec..dc198971d 100644 --- a/crates/horizon-client/src/game/render_control.rs +++ b/crates/horizon-client/src/game/render_control.rs @@ -2,12 +2,14 @@ //! updates (`setblock`, `fill`) to build test scenes. use super::Game; use horizon_client::Result; +use horizon_core::random::RandomSource; use horizon_world::registry::BlockStateId; use serde_json::{Value, json}; pub const HELP: &[&str] = &[ "setblock X Y Z STATE set a block state (name[prop=value,...]) without updates", "fill X1 Y1 Z1 X2 Y2 Z2 STATE fill a box with a block state without updates", + "particle NAME X Y Z [DX DY DZ] [COUNT] spawn particles (vanilla /particle spread)", ]; impl Game<'_> { @@ -68,6 +70,38 @@ impl Game<'_> { } Ok(Some(json!({ "ok": true, "blocks": count }))) } + "particle" => { + use horizon_client::particles::{ParticleOptions, ParticleType}; + let ty = ParticleType::from_name(words.get(1).ok_or("missing particle")?) + .ok_or("unknown particle type")?; + let num = |i: usize, default: f64| -> Result { + Ok(words.get(i).map_or(Ok(default), |w| w.parse::())?) + }; + let at = [num(2, 0.0)?, num(3, 0.0)?, num(4, 0.0)?]; + let delta = [num(5, 0.0)?, num(6, 0.0)?, num(7, 0.0)?]; + let count = num(8, 1.0)? as u32; + let options = match ty { + ParticleType::Dust => ParticleOptions::dust([1.0, 0.0, 0.0], 1.0), + ParticleType::Block | ParticleType::FallingDust | ParticleType::DustPillar => { + ParticleOptions::block(ty, "block/stone".into(), 0x808080) + } + _ => ParticleOptions::simple(ty), + }; + // ServerLevel.sendParticles: count 0 uses the delta as velocity. + if count == 0 { + self.particles.create(&options, at, delta); + } + for _ in 0..count { + let r = self.particles.random(); + let p = [ + at[0] + r.next_gaussian() * delta[0], + at[1] + r.next_gaussian() * delta[1], + at[2] + r.next_gaussian() * delta[2], + ]; + self.particles.create(&options, p, [0.0; 3]); + } + Ok(Some(json!({ "ok": true, "live": self.particles.len() }))) + } other => Err(format!("unknown render command {other}").into()), } } diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs new file mode 100644 index 000000000..6aca570a0 --- /dev/null +++ b/crates/horizon-client/src/game/render_tick.rs @@ -0,0 +1,151 @@ +//! Per-tick client visuals: the particle engine, `ClientLevel.animateTick`, +//! campfire smoke and rain splashes (`ClientLevel.tickWeatherEffects`). +use super::{Dim, Game}; +use horizon_client::{ + animate_tick::{AnimateContext, animate_tick, campfire_particles}, + biome::Biomes, + blocks::{BlockFacts, Fluid}, + chunk::{HEIGHT, MIN_Y}, + particles::{ParticleLevel, ParticleOptions, ParticleType}, + world::World, +}; +use horizon_core::random::{LegacyRandom, RandomSource}; + +/// The first motion-blocking block at or below `from` (Heightmap MOTION_BLOCKING), plus one. +fn motion_blocking(world: &World, facts: &BlockFacts, x: i32, z: i32, from: i32) -> i32 { + let mut y = from.min(MIN_Y + HEIGHT - 1); + while y > MIN_Y { + let f = facts.get(world.block(x, y, z)); + if f.full_collision || f.fluid != Fluid::None || f.name.contains("leaves") { + return y + 1; + } + y -= 1; + } + MIN_Y +} +/// `Biome.getPrecipitationAt` is rain (not snow) at this position. +fn rains_at(world: &World, biomes: &Biomes, x: i32, y: i32, z: i32) -> bool { + let Some(chunk) = world.chunks.get(&(x >> 4, z >> 4)) else { + return false; + }; + let b = biomes.get(chunk.biome(x, y, z)); + if !b.precipitation { + return false; + } + let t = if y > 80 { + b.temperature - (y - 80) as f32 * 0.05 / 40.0 + } else { + b.temperature + }; + t >= 0.15 +} + +impl Game<'_> { + pub(super) fn tick_particles(&mut self) { + let eye = self.eye_position(); + if let Some(e) = eye { + self.particles.camera = e; + } + let (rain, _) = if self.dim == Dim::Overworld { + self.world.weather.levels(1.0) + } else { + (0.0, 0.0) + }; + let world = &self.world; + let facts = &self.res.facts; + let biomes = &self.res.biomes; + let raining = rain > 0.2; + let raining_at = |p: [i32; 3]| { + raining + && motion_blocking(world, facts, p[0], p[2], p[1] + 64) <= p[1] + && rains_at(world, biomes, p[0], p[1], p[2]) + }; + let ctx = AnimateContext { + world, + facts, + collision: self.collision, + biomes, + registry: self.res.assets.registry, + raining_at: &raining_at, + }; + if let Some(e) = eye { + let block = [ + e[0].floor() as i32, + e[1].floor() as i32, + e[2].floor() as i32, + ]; + animate_tick(&self.res.animate, &mut self.particles, &ctx, block); + if rain > 0.0 { + // ClientLevel.tickWeatherEffects: rain splashes (weather radius 10). + let mut random = LegacyRandom::new(self.world.game_time.wrapping_mul(312_987_231)); + let radius = 10; + let diameter = 2 * radius + 1; + let count = (0.225 * (diameter * diameter) as f32 * rain * rain) as i32; + for _ in 0..count { + let x = block[0] + random.next_int_bound(diameter).unwrap_or(0) - radius; + let z = block[2] + random.next_int_bound(diameter).unwrap_or(0) - radius; + let top = motion_blocking(world, facts, x, z, block[1] + 64); + if top <= MIN_Y + || top > block[1] + 10 + || top < block[1] - 10 + || !rains_at(world, biomes, x, top, z) + { + continue; + } + let below = top - 1; + let (bx, bz) = (random.next_double(), random.next_double()); + let state = world.block(x, below, z); + let f = facts.get(state); + let block_top = match self.collision.cached_shapes().cached_shape(state) { + Ok(Some(shape)) if !shape.is_empty() => { + shape.max_at(horizon_core::geometry::Axis::Y, bx, bz) + } + _ => 0.0, + }; + let fluid_top = if f.fluid == Fluid::None { + 0.0 + } else { + f64::from(f.fluid_level) / 9.0 + }; + let smoke = f.fluid == Fluid::Lava + || &*f.name == "minecraft:magma_block" + || (f.name.ends_with("campfire") + && self.res.animate.is_lit_campfire(state.0)); + let ty = if smoke { + ParticleType::Smoke + } else { + ParticleType::Rain + }; + let p = [ + f64::from(x) + bx, + f64::from(below) + block_top.max(fluid_top), + f64::from(z) + bz, + ]; + self.particles + .add(&ParticleOptions::simple(ty), p, [0.0; 3]); + } + } + } + if let Some(r) = &self.renderer { + campfire_particles( + &self.res.animate, + &mut self.particles, + &ctx, + r.block_entities(), + ); + } + let level = ParticleLevel { + world, + facts, + collision: self.collision, + player: self + .player + .as_ref() + .map(|p| ([p.position.x, p.position.y, p.position.z], p.velocity.y)), + }; + self.particles.tick(&level); + for (name, at, volume, pitch) in std::mem::take(&mut self.particles.sounds) { + self.audio.play(name, Some(at), volume, pitch); + } + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index cf9315fdd..f977a1514 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -1,6 +1,7 @@ //! Horizon native client: resources, world streaming, rendering and the //! bindings between the validated simulation pieces and the game loop. #![forbid(unsafe_code)] +pub mod animate_tick; pub mod assets; pub mod attribute_updates; pub mod audio; diff --git a/crates/horizon-client/src/particles.rs b/crates/horizon-client/src/particles.rs index aaac918f3..01df21209 100644 --- a/crates/horizon-client/src/particles.rs +++ b/crates/horizon-client/src/particles.rs @@ -1,87 +1,2118 @@ -//! Block particles (`TerrainParticle`): destroy bursts and mining crumbs that -//! sample a random quarter of the block's texture, fall with gravity and -//! expire after a randomized lifetime. -use crate::world::World; -use horizon_core::random::{LegacyRandom, RandomSource}; +//! Client particle engine (`ParticleEngine`, `Particle`, `SingleQuadParticle`) +//! and the vanilla particle types players see most: block crumbs, smoke, +//! flames, lava pops, drips, splashes and rain, bubbles, portal and enchant +//! glyphs, crits, hearts, villager particles, explosions, campfire smoke, +//! notes, redstone dust, totem, end rod, biome ash and spores, falling leaves, +//! spells and dragon breath. Constructors, ticks, sizes, colours and light +//! follow the vanilla classes; sprite sets come from `particles/*.json`. +use crate::{ + blocks::{BlockFacts, Fluid}, + collisions::SceneCollisions, + hud::Rect, + mesher::Vertex, + world::World, +}; +use glam::{DVec3, Quat, Vec3 as FVec3}; +use horizon_core::{ + geometry::{Aabb, Axis, Vec3}, + random::{LegacyRandom, RandomSource}, +}; +use horizon_world::block_collision::BlockCollisionRegistry; +use std::{collections::HashMap, path::Path}; + +macro_rules! particle_types { + ($($variant:ident = $name:literal),* $(,)?) => { + /// Vanilla particle types with a client implementation here. + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] + pub enum ParticleType { $($variant),* } + impl ParticleType { + pub const ALL: &'static [ParticleType] = &[$(ParticleType::$variant),*]; + /// Registry path (`minecraft:`), also the sprite-set file name. + pub fn name(self) -> &'static str { + match self { $(ParticleType::$variant => $name),* } + } + pub fn from_name(name: &str) -> Option { + let name = name.strip_prefix("minecraft:").unwrap_or(name); + match name { $($name => Some(ParticleType::$variant),)* _ => None } + } + } + }; +} +particle_types! { + Block = "block", + BlockCrumble = "block_crumble", + DustPillar = "dust_pillar", + FallingDust = "falling_dust", + Smoke = "smoke", + LargeSmoke = "large_smoke", + WhiteSmoke = "white_smoke", + Flame = "flame", + SoulFireFlame = "soul_fire_flame", + CopperFireFlame = "copper_fire_flame", + SmallFlame = "small_flame", + Lava = "lava", + DrippingWater = "dripping_water", + FallingWater = "falling_water", + DrippingLava = "dripping_lava", + FallingLava = "falling_lava", + LandingLava = "landing_lava", + DrippingHoney = "dripping_honey", + FallingHoney = "falling_honey", + LandingHoney = "landing_honey", + FallingNectar = "falling_nectar", + FallingSporeBlossom = "falling_spore_blossom", + SporeBlossomAir = "spore_blossom_air", + DrippingObsidianTear = "dripping_obsidian_tear", + FallingObsidianTear = "falling_obsidian_tear", + LandingObsidianTear = "landing_obsidian_tear", + DrippingDripstoneWater = "dripping_dripstone_water", + FallingDripstoneWater = "falling_dripstone_water", + DrippingDripstoneLava = "dripping_dripstone_lava", + FallingDripstoneLava = "falling_dripstone_lava", + Splash = "splash", + Rain = "rain", + Bubble = "bubble", + BubblePop = "bubble_pop", + BubbleColumnUp = "bubble_column_up", + CurrentDown = "current_down", + Underwater = "underwater", + Portal = "portal", + ReversePortal = "reverse_portal", + Enchant = "enchant", + Nautilus = "nautilus", + VaultConnection = "vault_connection", + Crit = "crit", + EnchantedHit = "enchanted_hit", + DamageIndicator = "damage_indicator", + Heart = "heart", + AngryVillager = "angry_villager", + HappyVillager = "happy_villager", + Composter = "composter", + Mycelium = "mycelium", + Dolphin = "dolphin", + EggCrack = "egg_crack", + Poof = "poof", + Explosion = "explosion", + ExplosionEmitter = "explosion_emitter", + CampfireCosySmoke = "campfire_cosy_smoke", + CampfireSignalSmoke = "campfire_signal_smoke", + Note = "note", + Dust = "dust", + TotemOfUndying = "totem_of_undying", + EndRod = "end_rod", + Ash = "ash", + WhiteAsh = "white_ash", + CrimsonSpore = "crimson_spore", + WarpedSpore = "warped_spore", + CherryLeaves = "cherry_leaves", + PaleOakLeaves = "pale_oak_leaves", + TintedLeaves = "tinted_leaves", + Effect = "effect", + InstantEffect = "instant_effect", + EntityEffect = "entity_effect", + Witch = "witch", + Infested = "infested", + Cloud = "cloud", + Sneeze = "sneeze", + Snowflake = "snowflake", + Soul = "soul", + SculkSoul = "sculk_soul", + DragonBreath = "dragon_breath", +} +impl ParticleType { + /// `ParticleType.getOverrideLimiter`: spawned regardless of distance. + pub fn override_limiter(self) -> bool { + matches!( + self, + Self::DamageIndicator | Self::ExplosionEmitter | Self::Explosion | Self::Poof + ) + } +} + +/// `ParticleOptions` with the parameters of the option classes used here. +#[derive(Clone, Debug)] +pub struct ParticleOptions { + pub ty: ParticleType, + /// `BlockParticleOption`: the block's particle sprite and terrain tint. + pub block: Option<(String, u32)>, + /// RGB(A) for dust, spells, entity effects and tinted leaves. + pub color: [f32; 4], + /// `DustParticleOptions.scale`, `PowerParticleOption.power`, `SpellParticleOption.power`. + pub scale: f32, +} +impl ParticleOptions { + pub fn simple(ty: ParticleType) -> Self { + Self { + ty, + block: None, + color: [1.0; 4], + scale: 1.0, + } + } + pub fn block(ty: ParticleType, sprite: String, tint: u32) -> Self { + Self { + block: Some((sprite, tint)), + ..Self::simple(ty) + } + } + /// `DustParticleOptions(color, scale)`. + pub fn dust(rgb: [f32; 3], scale: f32) -> Self { + Self { + color: [rgb[0], rgb[1], rgb[2], 1.0], + scale: scale.clamp(0.01, 4.0), + ..Self::simple(ParticleType::Dust) + } + } + pub fn colored(ty: ParticleType, rgba: [f32; 4]) -> Self { + Self { + color: rgba, + ..Self::simple(ty) + } + } +} +#[derive(Clone, Debug)] +enum Sprite { + /// Index into the engine's sprite table. + Index(u32), + /// An atlas sprite by name (block textures for terrain particles). + Named(String), +} +#[derive(Clone, Copy, Debug, PartialEq)] +enum Size { + Fixed, + /// `quadSize * clamp((age + a) / lifetime * 32, 0, 1)`. + GrowIn, + Flame, + Lava, + Portal, + ReversePortal, +} +#[derive(Clone, Copy, Debug, PartialEq)] +enum Light { + World, + FullBright, + /// `LightCoordsUtil.withBlock(light, 15)`. + Block15, + /// `addSmoothBlockEmission` with the age fraction. + AgeEmission, + /// `addSmoothBlockEmission` with the age fraction to the fourth power. + AgeEmission4, +} +#[derive(Clone, Copy, Debug, PartialEq)] +enum Behaviour { + Base, + /// BaseAshSmokeParticle / Explode / Dust / Snowflake / EmissiveRising: base tick, sprite from age. + Animated, + Lava, + Drip { + fluid: Fluid, + stage: DripStage, + next: Option, + sound: Option<&'static str>, + }, + WaterDrop, + Bubble, + BubblePop, + BubbleColumnUp, + CurrentDown { + angle: f32, + }, + SuspendedTown, + Portal { + start: [f64; 3], + }, + ReversePortal, + FlyTowards { + start: [f64; 3], + alpha: [f32; 4], + }, + Crit, + HugeExplosion, + ExplosionSeed, + Campfire, + SimpleAnimated { + fade: Option<[f32; 3]>, + }, + Spell { + original_alpha: f32, + }, + Cloud, + Snowflake, + Leaves { + rot_speed: f32, + spin: f32, + wind: f32, + swirl: bool, + flow: bool, + flow_scale: [f64; 2], + swirl_period: f64, + }, + FallingDust { + rot_speed: f32, + }, + DragonBreath { + hit_ground: bool, + }, +} +#[derive(Clone, Copy, Debug, PartialEq)] +enum DripStage { + Hang, + CoolingHang, + Fall, + FallAndLand, + Land, +} + +/// One live particle (`Particle` + `SingleQuadParticle` state). #[derive(Clone, Debug)] pub struct Particle { + pub ty: ParticleType, pub position: [f64; 3], pub previous: [f64; 3], pub velocity: [f64; 3], - pub age: u32, - pub lifetime: u32, - pub size: f32, - pub sprite: String, - /// Sub-rectangle of the sprite in 0..1 (u0, v0, u1, v1). - pub uv: [f32; 4], - pub tint: u32, - pub gravity: f64, + bb: Aabb, + bb_width: f32, + bb_height: f32, pub on_ground: bool, + has_physics: bool, + /// Particles that move without collision (`move` overridden). + free_move: bool, + stopped_by_collision: bool, + removed: bool, + pub age: i32, + pub lifetime: i32, + gravity: f32, + friction: f32, + speed_up_when_y_blocked: bool, + /// `quadSize`. + pub size: f32, + pub rgba: [f32; 4], + roll: f32, + o_roll: f32, + sprite: Sprite, + /// Age-driven sprite set (`setSpriteFromAge`). + set: Option, + /// Sub-rectangle of the sprite (u0, v0, u1, v1) in 0..1. + pub uv: [f32; 4], + size_curve: Size, + light: Light, + behaviour: Behaviour, + no_render: bool, + /// Ticked once when created (`CritParticle` constructor). + pre_tick: bool, +} +fn aabb_at(p: [f64; 3], w: f32, h: f32) -> Aabb { + let w = f64::from(w) / 2.0; + Aabb::new( + Vec3::new(p[0] - w, p[1], p[2] - w), + Vec3::new(p[0] + w, p[1] + f64::from(h), p[2] + w), + ) } impl Particle { - /// TerrainParticle(level, x, y, z, xa, ya, za, state) - pub fn terrain(random: &mut LegacyRandom, position: [f64; 3], velocity: [f64; 3], sprite: String, tint: u32) -> Self { - // Particle constructor: random jitter of velocity, normalized speed. - let mut v = [ - velocity[0] + f64::from(random.next_float() * 2.0 - 1.0) * 0.4, - velocity[1] + f64::from(random.next_float() * 2.0 - 1.0) * 0.4, - velocity[2] + f64::from(random.next_float() * 2.0 - 1.0) * 0.4, - ]; - let speed = f64::from(random.next_float() + random.next_float() + 1.0) * 0.15; - let length = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt().max(1e-9); - for (i, c) in v.iter_mut().enumerate() { - *c = *c / length * speed * 0.4; - if i == 1 { - *c += 0.1; - } - } - let u = random.next_float() * 3.0 / 4.0; - let w = random.next_float() * 3.0 / 4.0; - // SingleQuadParticle quadSize: 0.1 * (rand*0.5+0.5) * 2, halved for terrain. - let size = 0.1 * (random.next_float() * 0.5 + 0.5); - let lifetime = (4.0 / (random.next_float() * 0.9 + 0.1)) as u32; + /// `Particle(level, x, y, z)` + `SingleQuadParticle` (no initial velocity). + fn at(random: &mut LegacyRandom, ty: ParticleType, p: [f64; 3]) -> Self { + let mut particle = Self::blank(ty, p); + particle.lifetime = (4.0 / (random.next_float() * 0.9 + 0.1)) as i32; + particle.size = 0.1 * (random.next_float() * 0.5 + 0.5) * 2.0; + particle + } + fn blank(ty: ParticleType, p: [f64; 3]) -> Self { Self { - position, - previous: position, - velocity: v, - age: 0, - lifetime, - size, - sprite, - uv: [u, w, u + 0.25, w + 0.25], - tint, - gravity: 1.0, + ty, + position: p, + previous: p, + velocity: [0.0; 3], + bb: aabb_at(p, 0.2, 0.2), + bb_width: 0.2, + bb_height: 0.2, on_ground: false, + has_physics: true, + free_move: false, + stopped_by_collision: false, + removed: false, + age: 0, + lifetime: 0, + gravity: 0.0, + friction: 0.98, + speed_up_when_y_blocked: false, + size: 0.0, + rgba: [1.0; 4], + roll: 0.0, + o_roll: 0.0, + sprite: Sprite::Index(u32::MAX), + set: None, + uv: [0.0, 0.0, 1.0, 1.0], + size_curve: Size::Fixed, + light: Light::World, + behaviour: Behaviour::Base, + no_render: false, + pre_tick: false, } } - pub fn tick(&mut self, world: &World, solid: impl Fn(u16) -> bool) -> bool { - self.previous = self.position; - self.age += 1; - if self.age >= self.lifetime { - return false; - } - self.velocity[1] -= 0.04 * self.gravity; - for axis in 0..3 { - let mut next = self.position; - next[axis] += self.velocity[axis]; - let block = world.block(next[0].floor() as i32, next[1].floor() as i32, next[2].floor() as i32); - if solid(block.0 as u16) { - if axis == 1 && self.velocity[1] < 0.0 { - self.on_ground = true; - } - self.velocity[axis] = 0.0; - } else { - self.position = next; + /// `Particle(level, x, y, z, xa, ya, za)`: jittered, normalized initial velocity. + fn moving(random: &mut LegacyRandom, ty: ParticleType, p: [f64; 3], v: [f64; 3]) -> Self { + let lifetime = (4.0 / (random.next_float() * 0.9 + 0.1)) as i32; + let mut d = [ + v[0] + f64::from((random.next_float() * 2.0 - 1.0) * 0.4), + v[1] + f64::from((random.next_float() * 2.0 - 1.0) * 0.4), + v[2] + f64::from((random.next_float() * 2.0 - 1.0) * 0.4), + ]; + let speed = f64::from((random.next_float() + random.next_float() + 1.0) * 0.15); + let length = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt(); + for (i, c) in d.iter_mut().enumerate() { + *c = *c / length * speed * f64::from(0.4f32); + if i == 1 { + *c += f64::from(0.1f32); } } + let mut particle = Self::blank(ty, p); + particle.lifetime = lifetime; + particle.velocity = d; + particle.size = 0.1 * (random.next_float() * 0.5 + 0.5) * 2.0; + particle + } + /// `TerrainParticle(level, x, y, z, xa, ya, za, state)`: a crumb from a + /// random quarter of the block's particle sprite, tinted like the block. + pub fn terrain( + random: &mut LegacyRandom, + position: [f64; 3], + velocity: [f64; 3], + sprite: String, + tint: u32, + ) -> Self { + let mut p = Self::moving(random, ParticleType::Block, position, velocity); + p.sprite = Sprite::Named(sprite); + p.gravity = 1.0; + let c = |shift: u32| 0.6 * ((tint >> shift) & 255) as f32 / 255.0; + p.rgba = [c(16), c(8), c(0), 1.0]; + p.size /= 2.0; + let uo = random.next_float() * 3.0; + let vo = random.next_float() * 3.0; + // getU0 = (uo + 1) / 4, getU1 = uo / 4: the crumb is mirrored horizontally. + p.uv = [(uo + 1.0) / 4.0, vo / 4.0, uo / 4.0, (vo + 1.0) / 4.0]; + p + } + /// `Particle.setPower`. + pub fn set_power(&mut self, power: f32) { + let power = f64::from(power); + self.velocity[0] *= power; + self.velocity[1] = (self.velocity[1] - f64::from(0.1f32)) * power + f64::from(0.1f32); + self.velocity[2] *= power; + } + /// `SingleQuadParticle.scale` (quad and collision box). + pub fn scale(&mut self, scale: f32) { + self.size *= scale; + self.set_size(0.2 * scale, 0.2 * scale); + } + fn set_size(&mut self, w: f32, h: f32) { + if w != self.bb_width || h != self.bb_height { + self.bb_width = w; + self.bb_height = h; + let min = self.bb.min_position(); + let max = self.bb.max_position(); + let nx = (min.x + max.x - f64::from(w)) / 2.0; + let nz = (min.z + max.z - f64::from(w)) / 2.0; + self.bb = Aabb::new( + Vec3::new(nx, min.y, nz), + Vec3::new(nx + f64::from(w), min.y + f64::from(h), nz + f64::from(w)), + ); + } + } + fn set_pos(&mut self, p: [f64; 3]) { + self.position = p; + self.bb = aabb_at(p, self.bb_width, self.bb_height); + } + fn set_location_from_bb(&mut self) { + let (min, max) = (self.bb.min_position(), self.bb.max_position()); + self.position = [(min.x + max.x) / 2.0, min.y, (min.z + max.z) / 2.0]; + } + fn set_color(&mut self, r: f32, g: f32, b: f32) { + self.rgba[0] = r; + self.rgba[1] = g; + self.rgba[2] = b; + } + pub fn is_alive(&self) -> bool { + !self.removed + } + /// `Particle.move` with `Entity.collideBoundingBox` against block collision shapes. + fn move_by(&mut self, level: &ParticleLevel, d: [f64; 3]) { + if self.free_move { + self.bb = self.bb.move_by(Vec3::new(d[0], d[1], d[2])); + self.set_location_from_bb(); + return; + } + if self.stopped_by_collision { + return; + } + let original = d; + let mut moved = d; + let length2 = d[0] * d[0] + d[1] * d[1] + d[2] * d[2]; + if self.has_physics && length2 != 0.0 && length2 < 100.0 * 100.0 { + moved = level.collide(self.bb, d); + } + if moved != [0.0; 3] { + self.bb = self.bb.move_by(Vec3::new(moved[0], moved[1], moved[2])); + self.set_location_from_bb(); + } + if original[1].abs() >= f64::from(1.0e-5f32) && moved[1].abs() < f64::from(1.0e-5f32) { + self.stopped_by_collision = true; + } + self.on_ground = original[1] != moved[1] && original[1] < 0.0; + if original[0] != moved[0] { + self.velocity[0] = 0.0; + } + if original[2] != moved[2] { + self.velocity[2] = 0.0; + } + } + /// `Particle.tick`. + fn base_tick(&mut self, level: &ParticleLevel) { + self.previous = self.position; + let age = self.age; + self.age += 1; + if age >= self.lifetime { + self.removed = true; + return; + } + self.velocity[1] -= 0.04 * f64::from(self.gravity); + self.move_by(level, self.velocity); + if self.speed_up_when_y_blocked && self.position[1] == self.previous[1] { + self.velocity[0] *= 1.1; + self.velocity[2] *= 1.1; + } + let f = f64::from(self.friction); for c in &mut self.velocity { - *c *= 0.98; + *c *= f; } if self.on_ground { - self.velocity[0] *= 0.7; - self.velocity[2] *= 0.7; + self.velocity[0] *= f64::from(0.7f32); + self.velocity[2] *= f64::from(0.7f32); + } + } + fn quad_size(&self, a: f32) -> f32 { + let t = (self.age as f32 + a) / self.lifetime as f32; + match self.size_curve { + Size::Fixed => self.size, + Size::GrowIn => self.size * (t * 32.0).clamp(0.0, 1.0), + Size::Flame => self.size * (1.0 - t * t * 0.5), + Size::Lava => self.size * (1.0 - t * t), + Size::Portal => { + let s = 1.0 - t; + self.size * (1.0 - s * s) + } + Size::ReversePortal => { + self.size * (1.0 - (self.age as f32 + a) / (self.lifetime as f32 * 1.5)) + } } - true + } +} + +/// World access for particle physics. +pub struct ParticleLevel<'a> { + pub world: &'a World, + pub facts: &'a BlockFacts, + pub collision: &'a BlockCollisionRegistry<'a>, + /// Local player feet position and vertical velocity (`PlayerCloudParticle`). + pub player: Option<([f64; 3], f64)>, +} +impl ParticleLevel<'_> { + fn collide(&self, bb: Aabb, d: [f64; 3]) -> [f64; 3] { + let mut scene = SceneCollisions { + world: self.world, + binding: self.collision, + }; + match scene.resolve(bb, Vec3::new(d[0], d[1], d[2]), 0.0, false) { + Ok(v) => [v.x, v.y, v.z], + Err(_) => d, + } + } + fn block_at(p: [f64; 3]) -> [i32; 3] { + [ + p[0].floor() as i32, + p[1].floor() as i32, + p[2].floor() as i32, + ] + } + /// The fluid at a block and `FluidState.getHeight`. + fn fluid(&self, b: [i32; 3]) -> (Fluid, f64) { + let s = self.facts.get(self.world.block(b[0], b[1], b[2])); + if s.fluid == Fluid::None { + return (Fluid::None, 0.0); + } + let above = self.facts.get(self.world.block(b[0], b[1] + 1, b[2])); + let height = if above.fluid == s.fluid { + 1.0 + } else { + f64::from(s.fluid_level) / 9.0 + }; + (s.fluid, height) + } + /// `getCollisionShape(...).max(Y, x, z)` for cached shapes. + fn collision_top(&self, b: [i32; 3], fx: f64, fz: f64) -> f64 { + let state = self.world.block(b[0], b[1], b[2]); + match self.collision.cached_shapes().cached_shape(state) { + Ok(Some(shape)) if !shape.is_empty() => shape.max_at(Axis::Y, fx, fz), + Ok(Some(_)) => 0.0, + _ => { + if self.facts.get(state).full_collision { + 1.0 + } else { + 0.0 + } + } + } + } +} + +/// `ParticleEngine` plus the sprite sets of `ParticleResources`. +pub struct ParticleEngine { + particles: Vec, + pending: Vec, + random: LegacyRandom, + names: Vec, + rects: Vec>, + atlas: HashMap, + sets: HashMap, + set_sprites: Vec>, + /// Camera position for the 32-block spawn limiter. + pub camera: [f64; 3], + /// Local sounds requested by particles (dripstone drips): (event, position, volume, pitch). + pub sounds: Vec<(&'static str, [f64; 3], f32, f32)>, +} +/// `ParticleGroup.MAX_PARTICLES` (per render group; one group here). +const MAX_PARTICLES: usize = 16384; +impl ParticleEngine { + pub fn new(root: &Path, atlas: &HashMap) -> Self { + let mut engine = Self { + particles: Vec::new(), + pending: Vec::new(), + random: LegacyRandom::new(0x7061_7274), + names: Vec::new(), + rects: Vec::new(), + atlas: atlas.clone(), + sets: HashMap::new(), + set_sprites: Vec::new(), + camera: [0.0; 3], + sounds: Vec::new(), + }; + let dir = root.join("assets/minecraft/particles"); + for &ty in ParticleType::ALL { + let Ok(bytes) = std::fs::read(dir.join(format!("{}.json", ty.name()))) else { + continue; + }; + let Ok(json) = serde_json::from_slice::(&bytes) else { + continue; + }; + let sprites: Vec = json["textures"] + .as_array() + .map(|a| { + a.iter() + .filter_map(|t| t.as_str()) + .map(|t| { + engine + .intern(&format!("particle/{}", t.trim_start_matches("minecraft:"))) + }) + .collect() + }) + .unwrap_or_default(); + if !sprites.is_empty() { + engine.sets.insert(ty, engine.set_sprites.len() as u32); + engine.set_sprites.push(sprites); + } + } + engine + } + fn intern(&mut self, name: &str) -> u32 { + if let Some(i) = self.names.iter().position(|n| n == name) { + return i as u32; + } + self.names.push(name.to_owned()); + self.rects.push(self.atlas.get(name).copied()); + (self.names.len() - 1) as u32 + } + pub fn len(&self) -> usize { + self.particles.len() + } + pub fn is_empty(&self) -> bool { + self.particles.is_empty() + } + pub fn clear(&mut self) { + self.particles.clear(); + self.pending.clear(); + } + /// `ParticleEngine.add` (via `particlesToAdd`, applied on the next tick). + pub fn push(&mut self, particle: Particle) { + self.pending.push(particle); + } + /// Random generator shared by particle constructors and spawners. + pub fn random(&mut self) -> &mut LegacyRandom { + &mut self.random + } + /// `ClientLevel.addParticle`: dropped beyond 32 blocks of the camera unless + /// the type overrides the limiter. + pub fn add(&mut self, options: &ParticleOptions, p: [f64; 3], v: [f64; 3]) { + let c = self.camera; + let d2 = (p[0] - c[0]).powi(2) + (p[1] - c[1]).powi(2) + (p[2] - c[2]).powi(2); + if !options.ty.override_limiter() && d2 > 1024.0 { + return; + } + self.create(options, p, v); + } + /// `ParticleEngine.createParticle`: always spawned. + pub fn create(&mut self, options: &ParticleOptions, p: [f64; 3], v: [f64; 3]) { + if let Some(particle) = self.make(options, p, v) { + self.pending.push(particle); + } + } + /// `SpriteSet.get(random)`. + fn sprite_from_set(&mut self, ty: ParticleType) -> Sprite { + match self.sets.get(&ty) { + Some(&set) => { + let sprites = &self.set_sprites[set as usize]; + Sprite::Index( + sprites[self + .random + .next_int_bound(sprites.len() as i32) + .unwrap_or(0) as usize], + ) + } + None => Sprite::Index(u32::MAX), + } + } + fn set_of(&self, ty: ParticleType) -> Option { + self.sets.get(&ty).copied() + } + fn first(&self, ty: ParticleType) -> Sprite { + self.set_of(ty).map_or(Sprite::Index(u32::MAX), |s| { + Sprite::Index(self.set_sprites[s as usize][0]) + }) + } + /// `SingleQuadParticle.setSpriteFromAge`. + fn sprite_from_age(&self, p: &mut Particle) { + if p.removed { + return; + } + if let Some(set) = p.set { + let sprites = &self.set_sprites[set as usize]; + let lifetime = p.lifetime.max(1); + let index = (p.age.max(0) as i64 * (sprites.len() as i64 - 1) / i64::from(lifetime)) + .clamp(0, sprites.len() as i64 - 1); + p.sprite = Sprite::Index(sprites[index as usize]); + } + } + #[allow(clippy::too_many_lines)] + fn make(&mut self, o: &ParticleOptions, p: [f64; 3], v: [f64; 3]) -> Option { + use ParticleType as T; + let ty = o.ty; + let set = self.set_of(ty); + let r = &mut self.random; + let mut particle = match ty { + T::Block | T::BlockCrumble | T::DustPillar => { + let (sprite, tint) = o.block.clone()?; + let mut q = Particle::terrain(r, p, v, sprite, tint); + q.ty = ty; + if ty == T::BlockCrumble { + q.velocity = [0.0; 3]; + q.lifetime = r.next_int_bound(10).unwrap_or(0) + 1; + } else if ty == T::DustPillar { + q.velocity = [ + r.next_gaussian() / 30.0, + v[1] + r.next_gaussian() / 2.0, + r.next_gaussian() / 30.0, + ]; + q.lifetime = r.next_int_bound(20).unwrap_or(0) + 20; + } + return Some(q); + } + T::FallingDust => { + let (_, tint) = o.block.clone()?; + let mut q = Particle::at(r, ty, p); + q.set_color( + ((tint >> 16) & 255) as f32 / 255.0, + ((tint >> 8) & 255) as f32 / 255.0, + (tint & 255) as f32 / 255.0, + ); + q.size *= 0.674_999_95; + let base = (32.0 / (r.next_float() * 0.8 + 0.2)) as i32; + q.lifetime = ((base as f32 * 0.9) as i32).max(1); + let rot_speed = (r.next_float() - 0.5) * 0.1; + q.roll = r.next_float() * std::f32::consts::TAU; + q.size_curve = Size::GrowIn; + q.behaviour = Behaviour::FallingDust { rot_speed }; + q.set = set; + q + } + T::Smoke | T::LargeSmoke | T::WhiteSmoke | T::Ash | T::WhiteAsh => { + // BaseAshSmokeParticle(dir, xa, scale, colorRandom, maxLifetime, gravity, hasPhysics). + let (dir, scale, color_random, max_lifetime, gravity, physics, vel) = match ty { + T::Smoke | T::WhiteSmoke => ([0.1, 0.1, 0.1], 1.0, 0.3, 8, -0.1, true, v), + T::LargeSmoke => ([0.1, 0.1, 0.1], 2.5, 0.3, 8, -0.1, true, v), + T::Ash => ([0.1, -0.1, 0.1], 1.0, 0.5, 20, 0.1, false, [0.0; 3]), + _ => { + let xa = f64::from(r.next_float()) * -1.9 * f64::from(r.next_float()) * 0.1; + let ya = f64::from(r.next_float()) + * -0.5 + * f64::from(r.next_float()) + * 0.1 + * 5.0; + let za = f64::from(r.next_float()) * -1.9 * f64::from(r.next_float()) * 0.1; + ([0.1, -0.1, 0.1], 1.0, 0.0, 20, 0.0125, false, [xa, ya, za]) + } + }; + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.friction = 0.96; + q.gravity = gravity; + q.speed_up_when_y_blocked = true; + for i in 0..3 { + q.velocity[i] = q.velocity[i] * f64::from(dir[i] as f32) + vel[i]; + } + let col = r.next_float() * color_random; + q.set_color(col, col, col); + q.size *= 0.75 * scale; + q.lifetime = ((f64::from(max_lifetime) / (f64::from(r.next_float()) * 0.8 + 0.2) + * f64::from(scale)) as i32) + .max(1); + q.has_physics = physics; + if ty == T::WhiteSmoke { + q.set_color(0.729_411_8, 0.694_117_67, 0.760_784_3); + } else if ty == T::WhiteAsh { + q.set_color(186.0 / 255.0, 177.0 / 255.0, 194.0 / 255.0); + } + q.size_curve = Size::GrowIn; + q.behaviour = Behaviour::Animated; + q.set = set; + q + } + T::Flame + | T::SoulFireFlame + | T::CopperFireFlame + | T::SmallFlame + | T::Soul + | T::SculkSoul => { + // RisingParticle. + let mut q = Particle::moving(r, ty, p, v); + q.friction = 0.96; + for i in 0..3 { + q.velocity[i] = q.velocity[i] * f64::from(0.01f32) + v[i]; + } + for i in 0..3 { + q.position[i] += f64::from((r.next_float() - r.next_float()) * 0.05); + } + // RisingParticle moves x/y/z but not the bounding box. + q.previous = p; + q.lifetime = (8.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32 + 4; + if matches!(ty, T::Soul | T::SculkSoul) { + q.scale(1.5); + q.behaviour = Behaviour::Animated; + q.set = set; + if ty == T::SculkSoul { + q.light = Light::Block15; + } + } else { + q.free_move = true; + q.size_curve = Size::Flame; + q.light = Light::AgeEmission; + if ty == T::SmallFlame { + q.scale(0.5); + } + } + q + } + T::Lava => { + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.gravity = 0.75; + q.friction = 0.999; + q.velocity[0] *= f64::from(0.8f32); + q.velocity[1] *= f64::from(0.8f32); + q.velocity[2] *= f64::from(0.8f32); + q.velocity[1] = f64::from(r.next_float() * 0.4 + 0.05); + q.size *= r.next_float() * 2.0 + 0.2; + q.lifetime = (16.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.size_curve = Size::Lava; + q.light = Light::Block15; + q.behaviour = Behaviour::Lava; + q + } + T::DrippingWater + | T::FallingWater + | T::DrippingLava + | T::FallingLava + | T::LandingLava + | T::DrippingHoney + | T::FallingHoney + | T::LandingHoney + | T::FallingNectar + | T::FallingSporeBlossom + | T::DrippingObsidianTear + | T::FallingObsidianTear + | T::LandingObsidianTear + | T::DrippingDripstoneWater + | T::FallingDripstoneWater + | T::DrippingDripstoneLava + | T::FallingDripstoneLava => { + let mut q = Particle::at(r, ty, p); + q.set_size(0.01, 0.01); + q.gravity = 0.06; + let water = [0.2, 0.3, 1.0]; + let lava = [1.0, 0.285_714_3, 0.083_333_336]; + let tear = [0.511_718_75, 0.031_25, 0.890_625]; + let (fluid, stage, next, color, sound) = match ty { + T::DrippingWater => ( + Fluid::Water, + DripStage::Hang, + Some(T::FallingWater), + Some(water), + None, + ), + T::FallingWater => ( + Fluid::Water, + DripStage::FallAndLand, + Some(T::Splash), + Some(water), + None, + ), + T::DrippingLava => ( + Fluid::Lava, + DripStage::CoolingHang, + Some(T::FallingLava), + None, + None, + ), + T::FallingLava => ( + Fluid::Lava, + DripStage::FallAndLand, + Some(T::LandingLava), + Some(lava), + None, + ), + T::LandingLava => (Fluid::Lava, DripStage::Land, None, Some(lava), None), + T::DrippingHoney => ( + Fluid::None, + DripStage::Hang, + Some(T::FallingHoney), + Some([0.622, 0.508, 0.082]), + None, + ), + T::FallingHoney => ( + Fluid::None, + DripStage::FallAndLand, + Some(T::LandingHoney), + Some([0.582, 0.448, 0.082]), + Some("minecraft:block.beehive.drip"), + ), + T::LandingHoney => ( + Fluid::None, + DripStage::Land, + None, + Some([0.522, 0.408, 0.082]), + None, + ), + T::FallingNectar => ( + Fluid::None, + DripStage::Fall, + None, + Some([0.92, 0.782, 0.72]), + None, + ), + T::FallingSporeBlossom => ( + Fluid::None, + DripStage::Fall, + None, + Some([0.32, 0.5, 0.22]), + None, + ), + T::DrippingObsidianTear => ( + Fluid::None, + DripStage::Hang, + Some(T::FallingObsidianTear), + Some(tear), + None, + ), + T::FallingObsidianTear => ( + Fluid::None, + DripStage::FallAndLand, + Some(T::LandingObsidianTear), + Some(tear), + None, + ), + T::LandingObsidianTear => { + (Fluid::None, DripStage::Land, None, Some(tear), None) + } + T::DrippingDripstoneWater => ( + Fluid::Water, + DripStage::Hang, + Some(T::FallingDripstoneWater), + Some(water), + None, + ), + T::FallingDripstoneWater => ( + Fluid::Water, + DripStage::FallAndLand, + Some(T::Splash), + Some(water), + Some("minecraft:block.pointed_dripstone.drip_water"), + ), + T::DrippingDripstoneLava => ( + Fluid::Lava, + DripStage::CoolingHang, + Some(T::FallingDripstoneLava), + None, + None, + ), + _ => ( + Fluid::Lava, + DripStage::FallAndLand, + Some(T::LandingLava), + Some(lava), + Some("minecraft:block.pointed_dripstone.drip_lava"), + ), + }; + match stage { + DripStage::Hang | DripStage::CoolingHang => { + q.gravity *= 0.02; + q.lifetime = 40; + } + DripStage::FallAndLand => { + q.lifetime = (64.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32 + } + DripStage::Land => { + q.lifetime = (16.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32 + } + DripStage::Fall => {} + } + match ty { + T::FallingHoney | T::FallingObsidianTear => q.gravity = 0.01, + T::DrippingHoney | T::DrippingObsidianTear => { + q.gravity *= 0.01; + q.lifetime = 100; + } + T::LandingHoney => { + q.lifetime = (128.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32 + } + T::LandingObsidianTear => { + q.lifetime = (28.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32 + } + T::FallingNectar => { + q.lifetime = (16.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.gravity = 0.007; + } + T::FallingSporeBlossom => { + q.lifetime = (64.0 / (r.next_float() * (0.9 - 0.1) + 0.1)) as i32; + q.gravity = 0.005; + } + _ => {} + } + if matches!( + ty, + T::DrippingObsidianTear | T::FallingObsidianTear | T::LandingObsidianTear + ) { + q.light = Light::Block15; + } + if let Some(c) = color { + q.set_color(c[0], c[1], c[2]); + } + q.behaviour = Behaviour::Drip { + fluid, + stage, + next, + sound, + }; + q + } + T::Rain | T::Splash => { + // WaterDropParticle. + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.velocity[0] *= f64::from(0.3f32); + q.velocity[1] = f64::from(r.next_float() * 0.2 + 0.1); + q.velocity[2] *= f64::from(0.3f32); + q.set_size(0.01, 0.01); + q.gravity = 0.06; + q.lifetime = (8.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + if ty == T::Splash { + q.gravity = 0.04; + if v[1] == 0.0 && (v[0] != 0.0 || v[2] != 0.0) { + q.velocity = [v[0], 0.1, v[2]]; + } + } + q.behaviour = Behaviour::WaterDrop; + q + } + T::Bubble | T::BubbleColumnUp => { + let mut q = Particle::at(r, ty, p); + q.set_size(0.02, 0.02); + q.size *= r.next_float() * 0.6 + 0.2; + for i in 0..3 { + q.velocity[i] = + v[i] * f64::from(0.2f32) + f64::from((r.next_float() * 2.0 - 1.0) * 0.02); + } + if ty == T::Bubble { + q.lifetime = (8.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.behaviour = Behaviour::Bubble; + } else { + q.gravity = -0.125; + q.friction = 0.85; + q.lifetime = (40.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.behaviour = Behaviour::BubbleColumnUp; + } + q + } + T::BubblePop => { + let mut q = Particle::at(r, ty, p); + q.lifetime = 4; + q.gravity = 0.008; + q.velocity = v; + q.behaviour = Behaviour::BubblePop; + q.set = set; + q + } + T::CurrentDown => { + let mut q = Particle::at(r, ty, p); + q.lifetime = (r.next_float() * 60.0) as i32 + 30; + q.has_physics = false; + q.velocity = [0.0, -0.05, 0.0]; + q.set_size(0.02, 0.02); + q.size *= r.next_float() * 0.6 + 0.2; + q.gravity = 0.002; + q.behaviour = Behaviour::CurrentDown { angle: 0.0 }; + q + } + T::Underwater | T::CrimsonSpore | T::WarpedSpore | T::SporeBlossomAir => { + let below = [p[0], p[1] - 0.125, p[2]]; + let mut q = if ty == T::Underwater { + let mut q = Particle::at(r, ty, below); + q.set_size(0.01, 0.01); + q.size *= r.next_float() * 0.6 + 0.2; + q + } else { + let vel = match ty { + T::CrimsonSpore => [ + r.next_gaussian() * 1.0e-6, + r.next_gaussian() * 1.0e-4, + r.next_gaussian() * 1.0e-6, + ], + T::WarpedSpore => [ + 0.0, + f64::from(r.next_float()) * -1.9 * f64::from(r.next_float()) * 0.1, + 0.0, + ], + _ => [0.0, f64::from(-0.8f32), 0.0], + }; + let mut q = Particle::moving(r, ty, below, vel); + q.set_size(0.01, 0.01); + q.size *= r.next_float() * 0.6 + 0.6; + q + }; + q.lifetime = (16.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.has_physics = false; + q.friction = 1.0; + q.gravity = 0.0; + match ty { + T::Underwater => q.set_color(0.4, 0.4, 0.7), + T::CrimsonSpore => q.set_color(0.9, 0.4, 0.5), + T::WarpedSpore => { + q.set_color(0.1, 0.1, 0.3); + q.set_size(0.001, 0.001); + } + _ => { + q.lifetime = r.next_int_bound(501).unwrap_or(0) + 500; + q.gravity = 0.01; + q.set_color(0.32, 0.5, 0.22); + } + } + q + } + T::Portal | T::ReversePortal => { + let mut q = Particle::at(r, ty, p); + q.velocity = v; + q.size = 0.1 * (r.next_float() * 0.2 + 0.5); + let br = r.next_float() * 0.6 + 0.4; + q.set_color(br * 0.9, br * 0.3, br); + q.lifetime = (r.next_float() * 10.0) as i32 + 40; + q.free_move = true; + q.light = Light::AgeEmission4; + if ty == T::Portal { + q.size_curve = Size::Portal; + q.behaviour = Behaviour::Portal { start: p }; + } else { + q.size *= 1.5; + q.lifetime = (r.next_float() * 2.0) as i32 + 60; + q.size_curve = Size::ReversePortal; + q.behaviour = Behaviour::ReversePortal; + } + q + } + T::Enchant | T::Nautilus | T::VaultConnection => { + let mut q = Particle::at(r, ty, p); + let alpha = if ty == T::VaultConnection { + [0.0, 0.6, 0.25, 1.0] + } else { + [1.0, 1.0, 0.0, 1.0] + }; + q.rgba[3] = alpha[0]; + q.velocity = v; + let start = [p[0] + v[0], p[1] + v[1], p[2] + v[2]]; + q.previous = start; + q.set_pos(start); + q.position = start; + q.size = 0.1 * (r.next_float() * 0.5 + 0.2); + let br = r.next_float() * 0.6 + 0.4; + q.set_color(0.9 * br, 0.9 * br, br); + q.has_physics = false; + q.lifetime = (r.next_float() * 10.0) as i32 + 30; + q.free_move = true; + q.light = if ty == T::VaultConnection { + Light::Block15 + } else { + Light::AgeEmission4 + }; + q.behaviour = Behaviour::FlyTowards { start: p, alpha }; + if ty == T::VaultConnection { + q.scale(1.5); + } + q + } + T::Crit | T::EnchantedHit | T::DamageIndicator => { + let vel = if ty == T::DamageIndicator { + [v[0], v[1] + 1.0, v[2]] + } else { + v + }; + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.friction = 0.7; + q.gravity = 0.5; + for i in 0..3 { + q.velocity[i] = q.velocity[i] * f64::from(0.1f32) + vel[i] * 0.4; + } + let col = r.next_float() * 0.3 + 0.6; + q.set_color(col, col, col); + q.size *= 0.75; + q.lifetime = ((6.0 / (f64::from(r.next_float()) * 0.8 + 0.6)) as i32).max(1); + q.has_physics = false; + q.size_curve = Size::GrowIn; + q.behaviour = Behaviour::Crit; + q.pre_tick = true; + q + } + T::Heart | T::AngryVillager => { + let at = if ty == T::AngryVillager { + [p[0], p[1] + 0.5, p[2]] + } else { + p + }; + let mut q = Particle::moving(r, ty, at, [0.0; 3]); + q.speed_up_when_y_blocked = true; + q.friction = 0.86; + for c in &mut q.velocity { + *c *= f64::from(0.01f32); + } + q.velocity[1] += 0.1; + q.size *= 1.5; + q.lifetime = 16; + q.has_physics = false; + q.size_curve = Size::GrowIn; + q + } + T::HappyVillager | T::Composter | T::Mycelium | T::Dolphin | T::EggCrack => { + let mut q = Particle::moving(r, ty, p, v); + let br = r.next_float() * 0.1 + 0.2; + q.set_color(br, br, br); + q.set_size(0.02, 0.02); + q.size *= r.next_float() * 0.6 + 0.5; + for c in &mut q.velocity { + *c *= f64::from(0.02f32); + } + q.lifetime = (20.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.free_move = true; + q.behaviour = Behaviour::SuspendedTown; + match ty { + T::HappyVillager | T::EggCrack => q.set_color(1.0, 1.0, 1.0), + T::Composter => { + q.set_color(1.0, 1.0, 1.0); + q.lifetime = 3 + r.next_int_bound(5).unwrap_or(0); + } + T::Dolphin => { + q.set_color(0.3, 0.5, 1.0); + q.rgba[3] = 1.0 - r.next_float() * 0.7; + q.lifetime /= 2; + } + _ => {} + } + q + } + T::Poof | T::Snowflake => { + let mut q = Particle::at(r, ty, p); + if ty == T::Poof { + q.gravity = -0.1; + q.friction = 0.9; + } else { + q.gravity = 0.225; + q.friction = 1.0; + } + for i in 0..3 { + q.velocity[i] = v[i] + f64::from((r.next_float() * 2.0 - 1.0) * 0.05); + } + if ty == T::Poof { + let col = r.next_float() * 0.3 + 0.7; + q.set_color(col, col, col); + q.size = 0.1 * (r.next_float() * r.next_float() * 6.0 + 1.0); + } else { + q.size = 0.1 * (r.next_float() * r.next_float() + 1.0); + } + q.lifetime = (16.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32 + 2; + q.behaviour = if ty == T::Poof { + Behaviour::Animated + } else { + Behaviour::Snowflake + }; + if ty == T::Snowflake { + q.set_color(0.923, 0.964, 0.999); + } + q.set = set; + q + } + T::Explosion => { + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.lifetime = 6 + r.next_int_bound(4).unwrap_or(0); + let col = r.next_float() * 0.6 + 0.4; + q.set_color(col, col, col); + q.size = 2.0 * (1.0 - v[0] as f32 * 0.5); + q.light = Light::FullBright; + q.behaviour = Behaviour::HugeExplosion; + q.set = set; + q + } + T::ExplosionEmitter => { + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.lifetime = 8; + q.no_render = true; + q.behaviour = Behaviour::ExplosionSeed; + q + } + T::CampfireCosySmoke | T::CampfireSignalSmoke => { + let mut q = Particle::at(r, ty, p); + q.scale(3.0); + q.set_size(0.25, 0.25); + q.lifetime = r.next_int_bound(50).unwrap_or(0) + + if ty == T::CampfireSignalSmoke { + 280 + } else { + 80 + }; + q.gravity = 3.0e-6; + q.velocity = [v[0], v[1] + f64::from(r.next_float() / 500.0), v[2]]; + q.rgba[3] = if ty == T::CampfireSignalSmoke { + 0.95 + } else { + 0.9 + }; + q.behaviour = Behaviour::Campfire; + q + } + T::Note => { + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.friction = 0.66; + q.speed_up_when_y_blocked = true; + for c in &mut q.velocity { + *c *= f64::from(0.01f32); + } + q.velocity[1] += 0.2; + let color = v[0] as f32; + let channel = |offset: f32| { + ((color + offset) * std::f32::consts::TAU) + .sin() + .mul_add(0.65, 0.35) + .max(0.0) + }; + q.set_color(channel(0.0), channel(1.0 / 3.0), channel(2.0 / 3.0)); + q.size *= 1.5; + q.lifetime = 6; + q.size_curve = Size::GrowIn; + q + } + T::Dust => { + let mut q = Particle::moving(r, ty, p, v); + q.friction = 0.96; + q.speed_up_when_y_blocked = true; + for c in &mut q.velocity { + *c *= f64::from(0.1f32); + } + q.size *= 0.75 * o.scale; + let base = (8.0 / (r.next_double() * 0.8 + 0.2)) as i32; + q.lifetime = ((base as f32 * o.scale).max(1.0)) as i32; + let factor = r.next_float() * 0.4 + 0.6; + let mut randomize = |c: f32| (r.next_float() * 0.2 + 0.8) * c * factor; + let rgb = [ + randomize(o.color[0]), + randomize(o.color[1]), + randomize(o.color[2]), + ]; + q.set_color(rgb[0], rgb[1], rgb[2]); + q.size_curve = Size::GrowIn; + q.behaviour = Behaviour::Animated; + q.set = set; + q + } + T::TotemOfUndying | T::EndRod => { + let mut q = Particle::at(r, ty, p); + q.friction = 0.91; + q.gravity = if ty == T::TotemOfUndying { + 1.25 + } else { + 0.0125 + }; + q.velocity = v; + q.size *= 0.75; + q.lifetime = 60 + r.next_int_bound(12).unwrap_or(0); + let mut fade = None; + if ty == T::TotemOfUndying { + q.friction = 0.6; + if r.next_int_bound(4).unwrap_or(1) == 0 { + q.set_color( + 0.6 + r.next_float() * 0.2, + 0.6 + r.next_float() * 0.3, + r.next_float() * 0.2, + ); + } else { + q.set_color( + 0.1 + r.next_float() * 0.2, + 0.4 + r.next_float() * 0.3, + r.next_float() * 0.2, + ); + } + } else { + fade = Some([ + 0xF2 as f32 / 255.0, + 0xDE as f32 / 255.0, + 0xC9 as f32 / 255.0, + ]); + q.free_move = true; + } + q.light = Light::FullBright; + q.behaviour = Behaviour::SimpleAnimated { fade }; + q.set = set; + q + } + T::CherryLeaves | T::PaleOakLeaves | T::TintedLeaves => { + let (fall, side, swirl, flow, scale, start) = if ty == T::CherryLeaves { + (0.25f32, 2.0f32, false, true, 1.0f32, 0.0f32) + } else { + (0.07, 10.0, true, false, 2.0, 0.021) + }; + let mut q = Particle::at(r, ty, p); + let rot_speed = (if r.next_boolean() { -30.0f32 } else { 30.0 }).to_radians(); + let spin = (if r.next_boolean() { -5.0f32 } else { 5.0 }).to_radians(); + q.lifetime = 300; + q.gravity = fall * 1.2 * 0.0025; + let s = scale * if r.next_boolean() { 0.05 } else { 0.075 }; + q.size = s; + q.set_size(s, s); + q.friction = 1.0; + q.velocity[1] = -f64::from(start); + let pr = r.next_float(); + let flow_scale = [ + f64::from(pr * 60.0).to_radians().cos() * f64::from(side), + f64::from(pr * 60.0).to_radians().sin() * f64::from(side), + ]; + let swirl_period = f64::from(1000.0 + pr * 3000.0).to_radians(); + if ty == T::TintedLeaves { + q.set_color(o.color[0], o.color[1], o.color[2]); + } + q.behaviour = Behaviour::Leaves { + rot_speed, + spin, + wind: side, + swirl, + flow, + flow_scale, + swirl_period, + }; + q + } + T::Effect | T::InstantEffect | T::EntityEffect | T::Witch | T::Infested => { + // SpellParticle: x/z velocity from a shared random, y from the option. + let xa = 0.5 - r.next_double(); + let za = 0.5 - r.next_double(); + let mut q = Particle::moving(r, ty, p, [xa, v[1], za]); + q.friction = 0.96; + q.gravity = -0.1; + q.speed_up_when_y_blocked = true; + q.velocity[1] *= f64::from(0.2f32); + if v[0] == 0.0 && v[2] == 0.0 { + q.velocity[0] *= f64::from(0.1f32); + q.velocity[2] *= f64::from(0.1f32); + } + q.size *= 0.75; + q.lifetime = (8.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.has_physics = false; + match ty { + T::Witch => { + let b = r.next_float() * 0.5 + 0.35; + q.set_color(b, 0.0, b); + } + T::EntityEffect => { + q.set_color(o.color[0], o.color[1], o.color[2]); + q.rgba[3] = o.color[3]; + } + T::Effect | T::InstantEffect => { + q.set_color(o.color[0], o.color[1], o.color[2]); + q.set_power(o.scale); + } + _ => {} + } + q.behaviour = Behaviour::Spell { + original_alpha: q.rgba[3], + }; + q.set = set; + q + } + T::Cloud | T::Sneeze => { + let mut q = Particle::moving(r, ty, p, [0.0; 3]); + q.friction = 0.96; + for i in 0..3 { + q.velocity[i] = q.velocity[i] * f64::from(0.1f32) + v[i]; + } + let col = 1.0 - r.next_float() * 0.3; + q.set_color(col, col, col); + q.size *= 1.875; + let base = (8.0 / (f64::from(r.next_float()) * 0.8 + 0.3)) as i32; + q.lifetime = ((base as f32 * 2.5).max(1.0)) as i32; + q.has_physics = false; + if ty == T::Sneeze { + q.set_color(0.22, 1.0, 0.53); + q.rgba[3] = 0.4; + } + q.size_curve = Size::GrowIn; + q.behaviour = Behaviour::Cloud; + q.set = set; + q + } + T::DragonBreath => { + let mut q = Particle::at(r, ty, p); + q.friction = 0.96; + q.velocity = v; + q.set_color( + r.next_float() * (0.874_509_8 - 0.717_647_1) + 0.717_647_1, + 0.0, + r.next_float() * (0.976_470_6 - 0.823_529_4) + 0.823_529_4, + ); + q.size *= 0.75; + q.lifetime = (20.0 / (f64::from(r.next_float()) * 0.8 + 0.2)) as i32; + q.has_physics = false; + q.size_curve = Size::GrowIn; + q.behaviour = Behaviour::DragonBreath { hit_ground: false }; + q.set = set; + q.set_power(o.scale); + q + } + }; + if particle.set.is_some() { + particle.sprite = self.first(ty); + self.sprite_from_age(&mut particle); + } else if matches!(particle.sprite, Sprite::Index(u32::MAX)) { + particle.sprite = self.sprite_from_set(ty); + } + Some(particle) + } + + /// `ParticleEngine.tick`: tick live particles, then admit queued ones. + pub fn tick(&mut self, level: &ParticleLevel) { + let mut particles = std::mem::take(&mut self.particles); + for p in &mut particles { + self.tick_particle(p, level); + } + particles.retain(|p| !p.removed); + self.particles = particles; + let pending = std::mem::take(&mut self.pending); + for mut p in pending { + if p.pre_tick { + p.pre_tick = false; + self.tick_particle(&mut p, level); + } + // ParticleGroup.add: the last quarter fills with decreasing probability. + let size = self.particles.len(); + if size >= MAX_PARTICLES { + continue; + } + if size >= 12288 { + let free = (MAX_PARTICLES - size) as f32 / 4096.0; + if self.random.next_float() >= free * free { + continue; + } + } + self.particles.push(p); + } + } + #[allow(clippy::too_many_lines)] + fn tick_particle(&mut self, p: &mut Particle, level: &ParticleLevel) { + match p.behaviour { + Behaviour::Base => p.base_tick(level), + Behaviour::Animated => { + p.base_tick(level); + self.sprite_from_age(p); + } + Behaviour::Snowflake => { + p.base_tick(level); + self.sprite_from_age(p); + p.velocity[0] *= f64::from(0.95f32); + p.velocity[1] *= f64::from(0.9f32); + p.velocity[2] *= f64::from(0.95f32); + } + Behaviour::Lava => { + p.base_tick(level); + if !p.removed { + let odds = p.age as f32 / p.lifetime as f32; + if self.random.next_float() > odds { + self.add( + &ParticleOptions::simple(ParticleType::Smoke), + p.position, + p.velocity, + ); + } + } + } + Behaviour::Crit => { + p.base_tick(level); + p.rgba[1] *= 0.96; + p.rgba[2] *= 0.9; + } + Behaviour::Drip { + fluid, + stage, + next, + sound, + } => { + p.previous = p.position; + // preMoveUpdate + if stage == DripStage::CoolingHang { + p.rgba[0] = 1.0; + p.rgba[1] = 16.0 / (40 - p.lifetime + 16) as f32; + p.rgba[2] = 4.0 / (40 - p.lifetime + 8) as f32; + } + let lifetime = p.lifetime; + p.lifetime -= 1; + if lifetime <= 0 { + p.removed = true; + if matches!(stage, DripStage::Hang | DripStage::CoolingHang) + && let Some(next) = next + { + self.add(&ParticleOptions::simple(next), p.position, p.velocity); + } + } + if p.removed { + return; + } + p.velocity[1] -= f64::from(p.gravity); + p.move_by(level, p.velocity); + // postMoveUpdate + match stage { + DripStage::Hang | DripStage::CoolingHang => { + for c in &mut p.velocity { + *c *= 0.02; + } + } + DripStage::Fall if p.on_ground => p.removed = true, + DripStage::FallAndLand if p.on_ground => { + p.removed = true; + if let Some(next) = next { + self.add(&ParticleOptions::simple(next), p.position, [0.0; 3]); + } + if let Some(sound) = sound { + let volume = self.random.next_float() * 0.7 + 0.3; + self.sounds.push((sound, p.position, volume, 1.0)); + } + } + _ => {} + } + if p.removed { + return; + } + for c in &mut p.velocity { + *c *= f64::from(0.98f32); + } + if fluid != Fluid::None { + let b = ParticleLevel::block_at(p.position); + let (f, height) = level.fluid(b); + if f == fluid && p.position[1] < f64::from(b[1]) + height { + p.removed = true; + } + } + } + Behaviour::WaterDrop => { + p.previous = p.position; + let lifetime = p.lifetime; + p.lifetime -= 1; + if lifetime <= 0 { + p.removed = true; + return; + } + p.velocity[1] -= f64::from(p.gravity); + p.move_by(level, p.velocity); + for c in &mut p.velocity { + *c *= f64::from(0.98f32); + } + if p.on_ground { + if self.random.next_float() < 0.5 { + p.removed = true; + } + p.velocity[0] *= f64::from(0.7f32); + p.velocity[2] *= f64::from(0.7f32); + } + let b = ParticleLevel::block_at(p.position); + let top = level.collision_top( + b, + p.position[0] - f64::from(b[0]), + p.position[2] - f64::from(b[2]), + ); + let offset = top.max(level.fluid(b).1); + if offset > 0.0 && p.position[1] < f64::from(b[1]) + offset { + p.removed = true; + } + } + Behaviour::Bubble => { + p.previous = p.position; + let lifetime = p.lifetime; + p.lifetime -= 1; + if lifetime <= 0 { + p.removed = true; + return; + } + p.velocity[1] += 0.002; + p.move_by(level, p.velocity); + for c in &mut p.velocity { + *c *= f64::from(0.85f32); + } + if level.fluid(ParticleLevel::block_at(p.position)).0 != Fluid::Water { + p.removed = true; + } + } + Behaviour::BubbleColumnUp => { + p.base_tick(level); + if !p.removed && level.fluid(ParticleLevel::block_at(p.position)).0 != Fluid::Water + { + p.removed = true; + } + } + Behaviour::BubblePop => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + p.velocity[1] -= f64::from(p.gravity); + p.move_by(level, p.velocity); + self.sprite_from_age(p); + } + Behaviour::CurrentDown { angle } => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + p.velocity[0] += f64::from(0.6 * angle.cos()); + p.velocity[2] += f64::from(0.6 * angle.sin()); + p.velocity[0] *= 0.07; + p.velocity[2] *= 0.07; + p.move_by(level, p.velocity); + if level.fluid(ParticleLevel::block_at(p.position)).0 != Fluid::Water || p.on_ground + { + p.removed = true; + } + p.behaviour = Behaviour::CurrentDown { + angle: angle + 0.08, + }; + } + Behaviour::SuspendedTown => { + p.previous = p.position; + let lifetime = p.lifetime; + p.lifetime -= 1; + if lifetime <= 0 { + p.removed = true; + return; + } + p.move_by(level, p.velocity); + for c in &mut p.velocity { + *c *= 0.99; + } + } + Behaviour::Portal { start } => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + let a = p.age as f32 / p.lifetime as f32; + let pos = 1.0 - (-a + a * a * 2.0); + p.position = [ + start[0] + p.velocity[0] * f64::from(pos), + start[1] + p.velocity[1] * f64::from(pos) + f64::from(1.0 - a), + start[2] + p.velocity[2] * f64::from(pos), + ]; + } + Behaviour::ReversePortal => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + let m = f64::from(p.age as f32 / p.lifetime as f32); + for i in 0..3 { + p.position[i] += p.velocity[i] * m; + } + } + Behaviour::FlyTowards { start, .. } => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + let pos = 1.0 - p.age as f32 / p.lifetime as f32; + let mut pp = 1.0 - pos; + pp *= pp; + pp *= pp; + p.position = [ + start[0] + p.velocity[0] * f64::from(pos), + start[1] + p.velocity[1] * f64::from(pos) - f64::from(pp * 1.2), + start[2] + p.velocity[2] * f64::from(pos), + ]; + } + Behaviour::HugeExplosion => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + self.sprite_from_age(p); + } + Behaviour::ExplosionSeed => { + for _ in 0..6 { + let r = &mut self.random; + let at = [ + p.position[0] + (r.next_double() - r.next_double()) * 4.0, + p.position[1] + (r.next_double() - r.next_double()) * 4.0, + p.position[2] + (r.next_double() - r.next_double()) * 4.0, + ]; + let f = f64::from(p.age as f32 / p.lifetime as f32); + self.add( + &ParticleOptions::simple(ParticleType::Explosion), + at, + [f, 0.0, 0.0], + ); + } + p.age += 1; + if p.age == p.lifetime { + p.removed = true; + } + } + Behaviour::Campfire => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age < p.lifetime && p.rgba[3] > 0.0 { + let r = &mut self.random; + let dx = r.next_float() / 5000.0 * if r.next_boolean() { 1.0 } else { -1.0 }; + p.velocity[0] += f64::from(dx); + let dz = r.next_float() / 5000.0 * if r.next_boolean() { 1.0 } else { -1.0 }; + p.velocity[2] += f64::from(dz); + p.velocity[1] -= f64::from(p.gravity); + p.move_by(level, p.velocity); + if p.age >= p.lifetime - 60 && p.rgba[3] > 0.01 { + p.rgba[3] -= 0.015; + } + } else { + p.removed = true; + } + } + Behaviour::SimpleAnimated { fade } => { + p.base_tick(level); + self.sprite_from_age(p); + if p.age > p.lifetime / 2 { + p.rgba[3] = 1.0 - (p.age as f32 - (p.lifetime / 2) as f32) / p.lifetime as f32; + if let Some(f) = fade { + for i in 0..3 { + p.rgba[i] += (f[i] - p.rgba[i]) * 0.2; + } + } + } + } + Behaviour::Spell { original_alpha } => { + p.base_tick(level); + self.sprite_from_age(p); + p.rgba[3] += (original_alpha - p.rgba[3]) * 0.05; + } + Behaviour::Cloud => { + p.base_tick(level); + if !p.removed { + self.sprite_from_age(p); + if let Some((player, vy)) = level.player { + let d2 = (player[0] - p.position[0]).powi(2) + + (player[1] - p.position[1]).powi(2) + + (player[2] - p.position[2]).powi(2); + if d2 < 4.0 && p.position[1] > player[1] { + let y = p.position[1] + (player[1] - p.position[1]) * 0.2; + p.velocity[1] += (vy - p.velocity[1]) * 0.2; + p.set_pos([p.position[0], y, p.position[2]]); + } + } + } + } + Behaviour::Leaves { + mut rot_speed, + spin, + wind, + swirl, + flow, + flow_scale, + swirl_period, + } => { + p.previous = p.position; + let lifetime = p.lifetime; + p.lifetime -= 1; + if lifetime <= 0 { + p.removed = true; + } + if !p.removed { + let alive = (300 - p.lifetime) as f32; + let relative = (alive / 300.0).min(1.0); + let (mut xa, mut za) = (0.0f64, 0.0f64); + if flow { + xa += flow_scale[0] * f64::from(relative).powf(1.25); + za += flow_scale[1] * f64::from(relative).powf(1.25); + } + if swirl { + let rel = f64::from(relative); + xa += rel * (rel * swirl_period).cos() * f64::from(wind); + za += rel * (rel * swirl_period).sin() * f64::from(wind); + } + p.velocity[0] += xa * f64::from(0.0025f32); + p.velocity[2] += za * f64::from(0.0025f32); + p.velocity[1] -= f64::from(p.gravity); + rot_speed += spin / 20.0; + p.o_roll = p.roll; + p.roll += rot_speed / 20.0; + p.move_by(level, p.velocity); + if p.on_ground + || p.lifetime < 299 && (p.velocity[0] == 0.0 || p.velocity[2] == 0.0) + { + p.removed = true; + } + if !p.removed { + let f = f64::from(p.friction); + for c in &mut p.velocity { + *c *= f; + } + } + } + p.behaviour = Behaviour::Leaves { + rot_speed, + spin, + wind, + swirl, + flow, + flow_scale, + swirl_period, + }; + } + Behaviour::FallingDust { rot_speed } => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + self.sprite_from_age(p); + p.o_roll = p.roll; + p.roll += std::f32::consts::PI * rot_speed * 2.0; + if p.on_ground { + p.o_roll = 0.0; + p.roll = 0.0; + } + p.move_by(level, p.velocity); + p.velocity[1] -= f64::from(0.003f32); + p.velocity[1] = p.velocity[1].max(f64::from(-0.14f32)); + } + Behaviour::DragonBreath { mut hit_ground } => { + p.previous = p.position; + let age = p.age; + p.age += 1; + if age >= p.lifetime { + p.removed = true; + return; + } + self.sprite_from_age(p); + if p.on_ground { + p.velocity[1] = 0.0; + hit_ground = true; + } + if hit_ground { + p.velocity[1] += 0.002; + } + p.move_by(level, p.velocity); + if p.position[1] == p.previous[1] { + p.velocity[0] *= 1.1; + p.velocity[2] *= 1.1; + } + let f = f64::from(p.friction); + p.velocity[0] *= f; + p.velocity[2] *= f; + if hit_ground { + p.velocity[1] *= f; + } + p.behaviour = Behaviour::DragonBreath { hit_ground }; + } + } + } + + /// Camera-facing quads (`QuadParticleRenderState.renderRotatedQuad`). + pub fn render( + &self, + camera: DVec3, + yaw: f32, + pitch: f32, + partial: f32, + light_at: &dyn Fn([i32; 3]) -> u8, + out: &mut Vec, + ) { + // Camera.setRotation: rotationYXZ(PI - yRot, -xRot, 0). + let base = Quat::from_rotation_y(std::f32::consts::PI - yaw.to_radians()) + * Quat::from_rotation_x(-pitch.to_radians()); + for p in &self.particles { + if p.no_render { + continue; + } + let rect = match &p.sprite { + Sprite::Index(i) => self.rects.get(*i as usize).copied().flatten(), + Sprite::Named(name) => self.atlas.get(name).copied(), + }; + let Some(r) = rect else { continue }; + let a = f64::from(partial); + let pos = DVec3::new( + p.previous[0] + (p.position[0] - p.previous[0]) * a, + p.previous[1] + (p.position[1] - p.previous[1]) * a, + p.previous[2] + (p.position[2] - p.previous[2]) * a, + ) - camera; + let center = pos.as_vec3(); + let rotation = if p.roll != 0.0 { + base * Quat::from_rotation_z(p.o_roll + (p.roll - p.o_roll) * partial) + } else { + base + }; + let size = p.quad_size(partial); + let mut alpha = p.rgba[3]; + if let Behaviour::FlyTowards { alpha: la, .. } = p.behaviour { + alpha = lifetime_alpha(la, p.age, p.lifetime, partial); + } + let block = ParticleLevel::block_at(p.position); + let packed = light_at(block); + let (sky, mut block_light) = (u32::from(packed >> 4) * 16, u32::from(packed & 15) * 16); + let mut sky_light = sky; + let t = ((p.age as f32 + partial) / p.lifetime as f32).clamp(0.0, 1.0); + match p.light { + Light::World => {} + Light::FullBright => { + block_light = 240; + sky_light = 240; + } + Light::Block15 => block_light = 240, + Light::AgeEmission => block_light = (block_light + (t * 240.0) as u32).min(240), + Light::AgeEmission4 => { + let b = p.age as f32 / p.lifetime as f32; + let b = (b * b) * (b * b); + block_light = (block_light + (b.clamp(0.0, 1.0) * 240.0) as u32).min(240); + } + } + let light = sky_light | block_light << 8; + let c = |v: f32| (v.clamp(0.0, 1.0) * 255.0) as u32; + let color = c(alpha) << 24 | c(p.rgba[2]) << 16 | c(p.rgba[1]) << 8 | c(p.rgba[0]); + let (du, dv) = (r.u1 - r.u0, r.v1 - r.v0); + let (u0, v0, u1, v1) = ( + r.u0 + du * p.uv[0], + r.v0 + dv * p.uv[1], + r.u0 + du * p.uv[2], + r.v0 + dv * p.uv[3], + ); + for (n, uv) in [ + ((1.0, -1.0), [u1, v1]), + ((1.0, 1.0), [u1, v0]), + ((-1.0, 1.0), [u0, v0]), + ((-1.0, -1.0), [u0, v1]), + ] { + let v = center + rotation * FVec3::new(n.0, n.1, 0.0) * size; + out.push(Vertex { + pos: v.to_array(), + uv, + layer: 0, + color, + light, + }); + } + } + } +} +/// `Particle.LifetimeAlpha.currentAlphaForAge`. +fn lifetime_alpha(a: [f32; 4], age: i32, lifetime: i32, partial: f32) -> f32 { + if (a[0] - a[1]).abs() < 1.0e-5 { + return a[0]; + } + let t = ((age as f32 + partial) / lifetime as f32 - a[2]) / (a[3] - a[2]); + a[0] + (a[1] - a[0]) * t.clamp(0.0, 1.0) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn particle_names_round_trip() { + for &ty in ParticleType::ALL { + assert_eq!( + ParticleType::from_name(&format!("minecraft:{}", ty.name())), + Some(ty) + ); + } + assert_eq!(ParticleType::from_name("minecraft:not_a_particle"), None); + } + #[test] + fn terrain_crumb_samples_a_mirrored_quarter() { + let mut r = LegacyRandom::new(1); + let p = Particle::terrain(&mut r, [0.5; 3], [0.0; 3], "block/stone".into(), 0xFFFFFF); + assert!((p.uv[0] - p.uv[2] - 0.25).abs() < 1e-6); + assert!((p.uv[3] - p.uv[1] - 0.25).abs() < 1e-6); + assert!((p.rgba[0] - 0.6).abs() < 1e-6); + // The initial speed is normalized to 0.15..0.45 * 0.4 plus 0.1 upwards. + let s = + (p.velocity[0].powi(2) + (p.velocity[1] - 0.1).powi(2) + p.velocity[2].powi(2)).sqrt(); + assert!((0.15 * 0.4 - 1e-6..=0.45 * 0.4 + 1e-6).contains(&s)); + } + #[test] + fn sizes_follow_vanilla_curves() { + let mut r = LegacyRandom::new(2); + let mut p = Particle::at(&mut r, ParticleType::Flame, [0.0; 3]); + p.size = 1.0; + p.lifetime = 10; + p.age = 5; + p.size_curve = Size::Flame; + assert!((p.quad_size(0.0) - (1.0 - 0.25 * 0.5)).abs() < 1e-6); + p.size_curve = Size::GrowIn; + p.age = 0; + assert!((p.quad_size(0.1) - (0.1f32 / 10.0 * 32.0)).abs() < 1e-6); + } + #[test] + fn lifetime_alpha_interpolates() { + let a = [0.0, 0.6, 0.25, 1.0]; + assert_eq!(lifetime_alpha(a, 0, 40, 0.0), 0.0); + assert!((lifetime_alpha(a, 40, 40, 0.0) - 0.6).abs() < 1e-6); } }