Apply fluid currents to primed TNT and show fire in inventory preview

This commit is contained in:
Cosmic
2026-10-14 11:02:27 -04:00
parent cadc4698c1
commit e828354040
2 changed files with 397 additions and 60 deletions
+92 -25
View File
@@ -482,31 +482,12 @@ impl Game<'_> {
* glam::Mat4::from_translation(glam::Vec3::new(0.0, height / 2.0 + offset_y, 0.0))
* glam::Mat4::from_rotation_z(std::f32::consts::PI)
* glam::Mat4::from_rotation_x((y_angle * 20.0).to_radians());
let mut quads: Vec<(f32, [[f32; 2]; 4], [[f32; 2]; 4], u32)> = Vec::new();
for q in verts.as_chunks::<4>().0 {
let p: [glam::Vec3; 4] =
std::array::from_fn(|i| gui.transform_point3(glam::Vec3::from_array(q[i].pos)));
let n = (p[1] - p[0]).cross(p[2] - p[0]);
// The winding gives inward normals: faces with n.z >= 0 point away.
if n.z >= 0.0 {
continue;
}
let n = n.normalize_or_zero();
// Lighting.Entry.ENTITY_IN_UI, roughly: lit from the front and above.
let shade = (0.55 + 0.45 * -n.z - 0.15 * n.y).clamp(0.4, 1.0);
let c = q[0].color;
let ch = |s: u32| ((((c >> s) & 255) as f32 * shade) as u32) << s;
let color = (c & 0xFF00_0000) | ch(0) | ch(8) | ch(16);
let depth = p.iter().map(|v| v.z).sum::<f32>() / 4.0;
quads.push((
depth,
p.map(|v| [v.x, v.y]),
std::array::from_fn(|i| q[i].uv),
color,
));
}
// Far (low z) first: the viewer is on the +z side.
quads.sort_by(|a, b| a.0.total_cmp(&b.0));
let fire = self
.player
.as_ref()
.filter(|p| p.fire_ticks > 0 && !p.spectator)
.map(|p| (p.dimensions.width, p.dimensions.height));
let quads = player_preview_quads(verts, gui, fire, &atlas.sprites);
let start = batch.vertices.len();
for (_, p, uv, color) in quads {
batch.quad(p, uv, color);
@@ -579,3 +560,89 @@ impl Game<'_> {
);
}
}
fn player_preview_quads(
mut verts: Vec<horizon_client::mesher::Vertex>,
gui: glam::Mat4,
fire: Option<(f32, f32)>,
sprites: &std::collections::HashMap<String, horizon_client::hud::Rect>,
) -> Vec<(f32, [[f32; 2]; 4], [[f32; 2]; 4], u32)> {
let flame_start = verts.len();
if let Some((width, height)) = fire {
horizon_client::fire_render::flames(
[0.0; 3],
width,
height,
glam::DVec3::ZERO,
0.0,
sprites,
0xff,
&mut verts,
);
}
let mut quads: Vec<(f32, [[f32; 2]; 4], [[f32; 2]; 4], u32)> = Vec::new();
for (index, q) in verts.as_chunks::<4>().0.iter().enumerate() {
let p: [glam::Vec3; 4] =
std::array::from_fn(|i| gui.transform_point3(glam::Vec3::from_array(q[i].pos)));
let n = (p[1] - p[0]).cross(p[2] - p[0]);
// The winding gives inward normals: faces with n.z >= 0 point away.
if n.z >= 0.0 {
continue;
}
let n = n.normalize_or_zero();
// Lighting.Entry.ENTITY_IN_UI, roughly: lit from the front and above.
let shade = if index * 4 >= flame_start {
1.0
} else {
(0.55 + 0.45 * -n.z - 0.15 * n.y).clamp(0.4, 1.0)
};
let c = q[0].color;
let ch = |s: u32| ((((c >> s) & 255) as f32 * shade) as u32) << s;
let color = (c & 0xFF00_0000) | ch(0) | ch(8) | ch(16);
let depth = p.iter().map(|v| v.z).sum::<f32>() / 4.0;
quads.push((
depth,
p.map(|v| [v.x, v.y]),
std::array::from_fn(|i| q[i].uv),
color,
));
}
// Far (low z) first: the viewer is on the +z side.
quads.sort_by(|a, b| a.0.total_cmp(&b.0));
quads
}
#[cfg(test)]
mod preview_tests {
use super::*;
#[test]
fn burning_inventory_preview_keeps_visible_fullbright_flames() {
let rect = |u0| horizon_client::hud::Rect {
u0,
v0: 0.0,
u1: u0 + 0.1,
v1: 0.1,
w: 16,
h: 16,
};
let sprites = std::collections::HashMap::from([
("block/fire_0".into(), rect(0.0)),
("block/fire_1".into(), rect(0.5)),
]);
for pitch in [-30.0f32, 0.0, 30.0] {
let gui = glam::Mat4::from_scale(glam::Vec3::new(30.0, 30.0, -30.0))
* glam::Mat4::from_rotation_z(std::f32::consts::PI)
* glam::Mat4::from_rotation_x(pitch.to_radians());
assert!(player_preview_quads(Vec::new(), gui, None, &sprites).is_empty());
let quads = player_preview_quads(Vec::new(), gui, Some((0.6, 1.8)), &sprites);
assert!(
!quads.is_empty(),
"inventory projection must not cull all flames"
);
assert!(quads.iter().all(|q| q.3 == 0xffff_ffff));
assert!(quads.iter().any(|q| q.2[0][0] <= 0.1));
assert!(quads.iter().any(|q| q.2[0][0] >= 0.5));
assert!(quads.windows(2).all(|q| q[0].0 <= q[1].0));
}
}
}
+305 -35
View File
@@ -31,6 +31,78 @@ impl PrimedTnt {
),
)
}
fn tick(
&mut self,
world: &horizon_client::world::World,
collision: &horizon_world::block_collision::BlockCollisionRegistry<'_>,
assets: &horizon_client::assets::Assets,
facts: &horizon_client::blocks::BlockFacts,
) -> Result<()> {
self.previous = self.position;
self.velocity.y -= 0.04;
let mut scene = SceneCollisions {
world,
binding: collision,
subject: None,
};
let wanted = self.velocity;
let moved = scene
.resolve(self.bounds(), wanted, 0.0, self.on_ground)
.unwrap_or(Vec3::ZERO);
self.position = Vec3::new(
self.position.x + moved.x,
self.position.y + moved.y,
self.position.z + moved.z,
);
self.on_ground = wanted.y < 0.0 && moved.y != wanted.y;
if moved.x != wanted.x {
self.velocity.x = 0.0;
}
if moved.z != wanted.z {
self.velocity.z = 0.0;
}
if moved.y != wanted.y {
self.velocity.y = 0.0;
}
self.velocity = Vec3::new(
self.velocity.x * 0.98,
self.velocity.y * 0.98,
self.velocity.z * 0.98,
);
if self.on_ground {
self.velocity = Vec3::new(
self.velocity.x * 0.7,
self.velocity.y * -0.5,
self.velocity.z * 0.7,
);
}
self.fuse -= 1;
if self.fuse > 0 {
// PrimedTnt updates immersion after moving and decrementing its fuse.
let mut fluids =
horizon_world::entity_fluid::EntityFluidInteraction::new([WATER, LAVA]);
let mut fluid_world = TntFluidWorld {
tnt: self,
world,
assets,
facts,
};
fluids
.update(&mut fluid_world, false, 4096)
.map_err(|e| std::io::Error::other(e.to_string()))?;
fluids.apply_current(&mut fluid_world, WATER, 0.014)?;
fluids.apply_current(
&mut fluid_world,
LAVA,
if world.fast_lava {
0.007
} else {
0.0023333333333333335
},
)?;
}
Ok(())
}
}
impl Game<'_> {
@@ -83,41 +155,12 @@ impl Game<'_> {
let mut i = 0;
while i < count {
let t = &mut self.tnts[i];
t.previous = t.position;
t.velocity.y -= 0.04;
let mut scene = SceneCollisions {
world: &self.world,
binding: self.collision,
subject: None,
};
let wanted = t.velocity;
let moved = scene
.resolve(t.bounds(), wanted, 0.0, t.on_ground)
.unwrap_or(Vec3::ZERO);
t.position = Vec3::new(
t.position.x + moved.x,
t.position.y + moved.y,
t.position.z + moved.z,
);
t.on_ground = wanted.y < 0.0 && moved.y != wanted.y;
if moved.x != wanted.x {
t.velocity.x = 0.0;
}
if moved.z != wanted.z {
t.velocity.z = 0.0;
}
if moved.y != wanted.y {
t.velocity.y = 0.0;
}
t.velocity = Vec3::new(
t.velocity.x * 0.98,
t.velocity.y * 0.98,
t.velocity.z * 0.98,
);
if t.on_ground {
t.velocity = Vec3::new(t.velocity.x * 0.7, t.velocity.y * -0.5, t.velocity.z * 0.7);
}
t.fuse -= 1;
t.tick(
&self.world,
self.collision,
&self.res.assets,
&self.res.facts,
)?;
if t.fuse <= 0 {
let center = [
t.position.x,
@@ -269,3 +312,230 @@ impl Game<'_> {
}
}
}
use horizon_world::{
entity_fluid::{
CurrentTarget, FluidInteractionWorld, FluidMembership, FluidSections, FluidTagId,
},
fluid::{FluidHeightWorld, FluidKind, FluidState},
fluid_flow::{FluidFlowWorld, flow_at},
};
const WATER: FluidTagId = FluidTagId(0);
const LAVA: FluidTagId = FluidTagId(1);
struct TntFluidWorld<'a> {
tnt: &'a mut PrimedTnt,
world: &'a horizon_client::world::World,
assets: &'a horizon_client::assets::Assets,
facts: &'a horizon_client::blocks::BlockFacts,
}
impl TntFluidWorld<'_> {
fn state(&self, p: BlockPos) -> horizon_world::registry::BlockStateId {
self.world.block(p.x, p.y, p.z)
}
fn fluid_at(&self, p: BlockPos) -> std::io::Result<FluidState> {
FluidState::from_block(
self.assets
.registry
.state(self.state(p))
.ok_or_else(|| std::io::Error::other("missing TNT fluid state"))?,
)
}
}
impl FluidMembership for TntFluidWorld<'_> {
fn fluid_in_tag(&self, kind: FluidKind, tag: FluidTagId) -> bool {
match tag {
WATER => matches!(kind, FluidKind::Water | FluidKind::FlowingWater),
LAVA => matches!(kind, FluidKind::Lava | FluidKind::FlowingLava),
_ => false,
}
}
}
impl CurrentTarget for TntFluidWorld<'_> {
type Error = std::io::Error;
fn is_player_class(&self) -> bool {
false
}
fn velocity(&mut self) -> std::io::Result<Vec3> {
Ok(self.tnt.velocity)
}
fn add_delta_movement(&mut self, v: Vec3) -> std::io::Result<()> {
self.tnt.velocity = Vec3::new(
self.tnt.velocity.x + v.x,
self.tnt.velocity.y + v.y,
self.tnt.velocity.z + v.z,
);
Ok(())
}
}
impl FluidInteractionWorld for TntFluidWorld<'_> {
type Error = std::io::Error;
type Chunk = (i32, i32);
fn interaction_box(&mut self) -> std::io::Result<Option<Aabb>> {
Ok(Some(
self.tnt.bounds().deflate(Vec3::new(0.001, 0.001, 0.001)),
))
}
fn loaded_chunk(&mut self, x: i32, z: i32) -> std::io::Result<Option<Self::Chunk>> {
Ok(self.world.chunks.contains_key(&(x, z)).then_some((x, z)))
}
fn chunk_sections(&mut self, _: Self::Chunk) -> std::io::Result<FluidSections<'_>> {
static SECTIONS: [bool; horizon_client::chunk::SECTIONS] =
[true; horizon_client::chunk::SECTIONS];
Ok(FluidSections {
minimum_section_y: horizon_client::chunk::MIN_Y / 16,
has_fluid: &SECTIONS,
})
}
fn bounding_box_min_y(&self) -> f64 {
self.tnt.position.y
}
fn block_x(&self) -> i32 {
self.tnt.position.x.floor() as i32
}
fn block_z(&self) -> i32 {
self.tnt.position.z.floor() as i32
}
fn eye_y(&mut self) -> std::io::Result<f64> {
Ok(self.tnt.position.y + f64::from(0.98f32 * 0.85f32))
}
fn fluid_state(&mut self, p: BlockPos) -> std::io::Result<FluidState> {
self.fluid_at(p)
}
fn fluid_height(&mut self, s: FluidState, p: BlockPos) -> std::io::Result<f32> {
s.height_at(self, [p.x, p.y, p.z])
}
fn camera_height(&mut self, s: FluidState, p: BlockPos) -> std::io::Result<f32> {
s.camera_height_at(self, [p.x, p.y, p.z])
}
fn flow(&mut self, s: FluidState, p: BlockPos) -> std::io::Result<Vec3> {
flow_at(self, s, p)
}
}
impl FluidHeightWorld for TntFluidWorld<'_> {
type Error = std::io::Error;
fn fluid(&mut self, p: [i32; 3]) -> std::io::Result<FluidState> {
self.fluid_at(BlockPos::new(p[0], p[1], p[2]))
}
fn sturdy_down(&mut self, p: [i32; 3]) -> std::io::Result<bool> {
Ok(self
.facts
.get(self.state(BlockPos::new(p[0], p[1], p[2])))
.sturdy_full
& 1
!= 0)
}
}
impl FluidFlowWorld for TntFluidWorld<'_> {
type Error = std::io::Error;
type Block = horizon_world::registry::BlockStateId;
fn fluid(&mut self, p: BlockPos) -> std::io::Result<FluidState> {
self.fluid_at(p)
}
fn block(&mut self, p: BlockPos) -> std::io::Result<Self::Block> {
Ok(self.state(p))
}
fn blocks_fluid_flow(&self, s: Self::Block) -> bool {
self.assets
.blocks_fluid_flow
.contains(&self.assets.registry.state(s).unwrap().name)
}
fn is_ice_class(&self, s: Self::Block) -> bool {
self.facts
.get(s)
.classes
.iter()
.any(|c| c.as_ref() == "IceBlock")
}
fn is_face_sturdy(
&mut self,
s: Self::Block,
_: BlockPos,
d: horizon_core::geometry::Direction,
) -> std::io::Result<bool> {
Ok(self.facts.get(s).sturdy_full & (1 << d as u8) != 0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn primed_tnt_follows_water_currents_after_moving() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../.local/horizon-client/26.3");
if !root.join("movement-properties.nbt").exists() {
return;
}
let assets = horizon_client::assets::Assets::load(&root).unwrap();
let facts = horizon_client::blocks::BlockFacts::load(&root, assets.registry).unwrap();
let collision = horizon_client::collisions::load(&root, assets.registry).unwrap();
let mut world = horizon_client::world::World::test_scene(&assets, 3).unwrap();
let stone = assets.registry.default_state("minecraft:stone").unwrap();
let water = assets.registry.default_state("minecraft:water").unwrap();
let mut state = assets.registry.state(water).unwrap().clone();
state.properties.insert("level".into(), "1".into());
let flowing = assets.registry.resolve(&state).unwrap();
for x in 19..=22 {
for z in 19..=21 {
world.set(x, 3, z, stone).unwrap();
}
}
world.set(20, 4, 20, water).unwrap();
world.set(21, 4, 20, flowing).unwrap();
let at = Vec3::new(20.5, 4.0, 20.5);
let mut tnt = PrimedTnt {
position: at,
previous: at,
velocity: Vec3::ZERO,
fuse: 80,
on_ground: false,
};
tnt.tick(&world, &collision, &assets, &facts).unwrap();
assert_eq!(
tnt.position.x, at.x,
"current is applied after motion, not before"
);
assert!(
(tnt.velocity.x - 0.014).abs() < 1e-12,
"water must push primed TNT east: {:?}",
tnt.velocity
);
assert_eq!(tnt.fuse, 79);
tnt.tick(&world, &collision, &assets, &facts).unwrap();
assert!((tnt.position.x - at.x - 0.014).abs() < 1e-12);
assert!(tnt.on_ground);
tnt.position = at;
tnt.velocity = Vec3::ZERO;
tnt.fuse = 1;
tnt.tick(&world, &collision, &assets, &facts).unwrap();
assert_eq!(tnt.fuse, 0);
assert_eq!(
tnt.velocity.x, 0.0,
"expired TNT must not receive a current"
);
world.set(21, 4, 20, water).unwrap();
tnt.position = at;
tnt.velocity = Vec3::ZERO;
tnt.fuse = 80;
tnt.tick(&world, &collision, &assets, &facts).unwrap();
assert_eq!(
tnt.velocity.x, 0.0,
"still water must not add horizontal motion"
);
world
.set(
20,
3,
20,
assets.registry.default_state("minecraft:air").unwrap(),
)
.unwrap();
tnt.on_ground = false;
tnt.velocity = Vec3::ZERO;
tnt.tick(&world, &collision, &assets, &facts).unwrap();
assert!(
tnt.position.y < at.y && tnt.velocity.y < 0.0,
"TNT sinks; water does not add buoyancy"
);
}
}