95149579ff
Minecarts follow OldMinecartBehavior: rails and slopes, powered and activator rails, coming off the track, the rider's push, players and carts pushing each other, placement, breaking and saving, with chest and hopper containers, furnace fuel and push, and TNT priming and explosions. They render on the rail between the front and back points with their display block. Jukeboxes take jukebox_playable discs, play the song with the rainbow Now Playing overlay and note particles, emit 15 while playing, drive comparators, eject the disc and save it. Name tags name mobs (saved as CustomName); the name shows above the head on a translucent background when targeted or always visible, and Dinnerbone and Grumm render upside down. Blocks are now picked and outlined with their vanilla outline shapes (exported per state with the edges LevelRenderer draws), so rails, slabs, torches and plants no longer act as full cubes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
271 lines
11 KiB
Rust
271 lines
11 KiB
Rust
//! Per-state gameplay facts exported from vanilla's own block-state accessors
|
|
//! (`gameplay-facts.json`), indexed by registry state ID.
|
|
use crate::Result;
|
|
use horizon_world::registry::{BlockRegistry, BlockStateId};
|
|
use serde_json::Value;
|
|
use std::{collections::HashMap, fs::File, io::BufReader, path::Path, sync::Arc};
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum RenderShape {
|
|
Model,
|
|
Invisible,
|
|
Animated,
|
|
}
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Fluid {
|
|
None,
|
|
Water,
|
|
Lava,
|
|
}
|
|
#[derive(Clone, Debug)]
|
|
pub struct Sounds {
|
|
pub volume: f32,
|
|
pub pitch: f32,
|
|
pub break_sound: Arc<str>,
|
|
pub step: Arc<str>,
|
|
pub place: Arc<str>,
|
|
pub hit: Arc<str>,
|
|
pub fall: Arc<str>,
|
|
}
|
|
#[derive(Clone, Debug)]
|
|
pub struct StateFacts {
|
|
pub name: Arc<str>,
|
|
pub emission: u8,
|
|
pub dampening: u8,
|
|
pub skylight_down: bool,
|
|
pub shape_occlusion: bool,
|
|
pub can_occlude: bool,
|
|
pub solid_render: bool,
|
|
pub hardness: f32,
|
|
pub requires_tool: bool,
|
|
pub replaceable: bool,
|
|
pub random_ticks: bool,
|
|
pub block_entity: bool,
|
|
pub render_shape: RenderShape,
|
|
pub full_collision: bool,
|
|
pub friction: f32,
|
|
pub explosion_resistance: f32,
|
|
pub item: Arc<str>,
|
|
pub sounds: Sounds,
|
|
pub fluid: Fluid,
|
|
/// Fluid amount 1..=8 (8 = source/falling surface), 0 when there is no fluid.
|
|
pub fluid_level: u8,
|
|
pub fluid_falling: bool,
|
|
pub waterlogged: bool,
|
|
/// `hasOffsetFunction`: the model is jittered per column.
|
|
pub random_offset: bool,
|
|
/// `BlockState.isSolid()` (legacy solidity; fluid surfaces read it).
|
|
pub legacy_solid: bool,
|
|
/// `isFaceSturdy(.., SupportType.FULL)` by Direction ordinal.
|
|
pub sturdy_full: u8,
|
|
/// The block's class chain (`TransparentBlock`, `LeavesBlock`, ...), most specific first.
|
|
pub classes: Arc<[Arc<str>]>,
|
|
/// `getShape` (the outline used for picking) as boxes in block space,
|
|
/// without the random offset; and its edges as LevelRenderer draws them.
|
|
pub outline: Arc<[[f32; 6]]>,
|
|
pub outline_edges: Arc<[[f32; 6]]>,
|
|
}
|
|
/// The 12 edges of the unit cube (outline data predating the export).
|
|
fn cube_edges() -> Vec<[f32; 6]> {
|
|
let mut edges = Vec::new();
|
|
for a in 0..3 {
|
|
for (u, v) in [(0., 0.), (1., 0.), (0., 1.), (1., 1.)] {
|
|
let mut from = [0.0f32; 3];
|
|
let (b, c) = ((a + 1) % 3, (a + 2) % 3);
|
|
from[b] = u;
|
|
from[c] = v;
|
|
let mut to = from;
|
|
to[a] = 1.0;
|
|
edges.push([from[0], from[1], from[2], to[0], to[1], to[2]]);
|
|
}
|
|
}
|
|
edges
|
|
}
|
|
impl StateFacts {
|
|
pub fn has_class(&self, name: &str) -> bool {
|
|
self.classes.iter().any(|c| &**c == name)
|
|
}
|
|
}
|
|
pub struct BlockFacts {
|
|
pub states: Vec<StateFacts>,
|
|
pub air: BlockStateId,
|
|
}
|
|
impl BlockFacts {
|
|
pub fn load(root: &Path, registry: &BlockRegistry) -> Result<Self> {
|
|
let raw: Value = serde_json::from_reader(BufReader::new(File::open(
|
|
root.join("gameplay-facts.json"),
|
|
)?))?;
|
|
let entries = raw["states"]
|
|
.as_array()
|
|
.ok_or("missing gameplay state facts")?;
|
|
if entries.len() != registry.len() {
|
|
return Err("gameplay facts do not match the block registry".into());
|
|
}
|
|
// Class chains and per-state behaviour flags exported from the actual block objects.
|
|
let behaviours: Value = serde_json::from_reader(BufReader::new(File::open(
|
|
root.join("block-behaviours.json"),
|
|
)?))?;
|
|
let behaviour_states = behaviours["states"]
|
|
.as_array()
|
|
.filter(|s| s.len() == registry.len())
|
|
.ok_or("block behaviour facts do not match the block registry")?;
|
|
let mut class_names: HashMap<String, Arc<str>> = HashMap::new();
|
|
let classes: HashMap<&str, Arc<[Arc<str>]>> = behaviours["blocks"]
|
|
.as_array()
|
|
.ok_or("missing block behaviour classes")?
|
|
.iter()
|
|
.filter_map(|b| {
|
|
let chain = b["class"]
|
|
.as_array()?
|
|
.iter()
|
|
.filter_map(Value::as_str)
|
|
.map(|c| {
|
|
class_names
|
|
.entry(c.to_owned())
|
|
.or_insert_with(|| Arc::from(c))
|
|
.clone()
|
|
});
|
|
Some((b["name"].as_str()?, chain.collect()))
|
|
})
|
|
.collect();
|
|
let mut names: HashMap<String, Arc<str>> = HashMap::new();
|
|
let mut intern = |s: &str| -> Arc<str> {
|
|
names
|
|
.entry(s.to_owned())
|
|
.or_insert_with(|| Arc::from(s))
|
|
.clone()
|
|
};
|
|
let mut shapes: HashMap<String, Arc<[[f32; 6]]>> = HashMap::new();
|
|
let mut shape = |v: &Value, fallback: &dyn Fn() -> Vec<[f32; 6]>| -> Arc<[[f32; 6]]> {
|
|
let Some(list) = v.as_array() else {
|
|
return fallback().into();
|
|
};
|
|
shapes
|
|
.entry(v.to_string())
|
|
.or_insert_with(|| {
|
|
list.iter()
|
|
.filter_map(|b| {
|
|
let b = b.as_array()?;
|
|
let n =
|
|
|i: usize| b.get(i).and_then(Value::as_f64).unwrap_or(0.0) as f32;
|
|
Some([n(0), n(1), n(2), n(3), n(4), n(5)])
|
|
})
|
|
.collect()
|
|
})
|
|
.clone()
|
|
};
|
|
let mut states = Vec::with_capacity(entries.len());
|
|
for (index, entry) in entries.iter().enumerate() {
|
|
let id = BlockStateId(index as u32);
|
|
if entry["id"].as_u64() != Some(index as u64) {
|
|
return Err("gameplay facts are not in registry order".into());
|
|
}
|
|
let state = registry.state(id).ok_or("missing registry state")?;
|
|
let num = |k: &str| entry[k].as_f64().unwrap_or(0.0);
|
|
let flag = |k: &str| entry[k].as_bool().unwrap_or(false);
|
|
let sound = &entry["sound"];
|
|
let sound_id = |k: &str| sound[k]["id"].as_str().unwrap_or("").to_owned();
|
|
let prop = |k: &str| state.properties.get(k).map(String::as_str);
|
|
let (fluid, fluid_level, fluid_falling) = match state.name.as_str() {
|
|
"minecraft:water" | "minecraft:lava" => {
|
|
let level: u8 = prop("level").and_then(|v| v.parse().ok()).unwrap_or(0);
|
|
let kind = if state.name == "minecraft:water" {
|
|
Fluid::Water
|
|
} else {
|
|
Fluid::Lava
|
|
};
|
|
// LiquidBlock: level 0 is the source; 1..7 flowing; >=8 falling.
|
|
if level == 0 {
|
|
(kind, 8, false)
|
|
} else if level >= 8 {
|
|
(kind, 8, true)
|
|
} else {
|
|
(kind, 8 - level, false)
|
|
}
|
|
}
|
|
"minecraft:bubble_column"
|
|
| "minecraft:kelp"
|
|
| "minecraft:kelp_plant"
|
|
| "minecraft:seagrass"
|
|
| "minecraft:tall_seagrass" => (Fluid::Water, 8, false),
|
|
_ if prop("waterlogged") == Some("true") => (Fluid::Water, 8, false),
|
|
_ => (Fluid::None, 0, false),
|
|
};
|
|
let air = matches!(
|
|
state.name.as_str(),
|
|
"minecraft:air" | "minecraft:cave_air" | "minecraft:void_air"
|
|
);
|
|
let cube = move || {
|
|
if air {
|
|
Vec::new()
|
|
} else {
|
|
vec![[0.0, 0.0, 0.0, 1.0, 1.0, 1.0]]
|
|
}
|
|
};
|
|
let edges = move || if air { Vec::new() } else { cube_edges() };
|
|
let outline = shape(&entry["outline"], &cube);
|
|
let outline_edges = shape(&entry["outline_edges"], &edges);
|
|
states.push(StateFacts {
|
|
outline,
|
|
outline_edges,
|
|
name: intern(&state.name),
|
|
emission: num("emission") as u8,
|
|
dampening: num("dampening") as u8,
|
|
skylight_down: flag("skylight_down"),
|
|
shape_occlusion: flag("shape_occlusion"),
|
|
can_occlude: flag("can_occlude"),
|
|
solid_render: flag("solid_render"),
|
|
hardness: num("hardness") as f32,
|
|
requires_tool: flag("requires_tool"),
|
|
replaceable: flag("replaceable"),
|
|
random_ticks: flag("random_ticks"),
|
|
block_entity: flag("block_entity"),
|
|
render_shape: match entry["render_shape"].as_str() {
|
|
Some("INVISIBLE") => RenderShape::Invisible,
|
|
Some("ENTITYBLOCK_ANIMATED") => RenderShape::Animated,
|
|
_ => RenderShape::Model,
|
|
},
|
|
full_collision: flag("full_collision"),
|
|
friction: num("friction") as f32,
|
|
explosion_resistance: num("explosion_resistance") as f32,
|
|
item: intern(entry["item"].as_str().unwrap_or("minecraft:air")),
|
|
sounds: Sounds {
|
|
volume: sound["volume"].as_f64().unwrap_or(1.0) as f32,
|
|
pitch: sound["pitch"].as_f64().unwrap_or(1.0) as f32,
|
|
break_sound: intern(&sound_id("break")),
|
|
step: intern(&sound_id("step")),
|
|
place: intern(&sound_id("place")),
|
|
hit: intern(&sound_id("hit")),
|
|
fall: intern(&sound_id("fall")),
|
|
},
|
|
fluid,
|
|
fluid_level,
|
|
fluid_falling,
|
|
waterlogged: prop("waterlogged") == Some("true"),
|
|
random_offset: flag("offset"),
|
|
legacy_solid: behaviour_states[index][1].as_u64().unwrap_or(0) & 1 != 0,
|
|
sturdy_full: (behaviour_states[index][0].as_u64().unwrap_or(0) & 63) as u8,
|
|
classes: classes
|
|
.get(state.name.as_str())
|
|
.cloned()
|
|
.ok_or("missing block behaviour class chain")?,
|
|
});
|
|
}
|
|
Ok(Self {
|
|
states,
|
|
air: registry
|
|
.default_state("minecraft:air")
|
|
.ok_or("missing air")?,
|
|
})
|
|
}
|
|
pub fn get(&self, id: BlockStateId) -> &StateFacts {
|
|
&self.states[id.0 as usize]
|
|
}
|
|
pub fn is_air(&self, id: BlockStateId) -> bool {
|
|
matches!(
|
|
&*self.states[id.0 as usize].name,
|
|
"minecraft:air" | "minecraft:cave_air" | "minecraft:void_air"
|
|
)
|
|
}
|
|
}
|