Merge pull request #674 from ludovic111/feat/wide-block-states

🤖 Agent: merged by Ludovic's AI agent after local checks on a6592eb1: workspace clippy clean; horizon-client/world/protocol tests pass; lan, chunk_stage, dedicated_server, block_entity_storage, chunk_generation PASS.
This commit is contained in:
Ludovic Marie
2026-10-17 18:41:58 +02:00
committed by GitHub
48 changed files with 407 additions and 224 deletions
+1 -6
View File
@@ -22,12 +22,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let port: u16 = args.next().ok_or("port")?.parse()?;
let assets = Assets::load(&root)?;
let facts = BlockFacts::load(&root, assets.registry)?;
let state = |name: &str| {
assets
.registry
.default_state(name)
.map_or(0, |s| s.0 as u16)
};
let state = |name: &str| assets.registry.default_state(name).map_or(0, |s| s.0);
let sync = Arc::new(SyncData::load(&root)?);
let biomes: Vec<String> = sync
.dynamic
@@ -232,7 +232,7 @@ fn incremental(
c.lit = true;
world.insert((x, z), Arc::new(c));
}
let palette: Vec<u16> = [
let palette: Vec<u32> = [
"minecraft:air",
"minecraft:stone",
"minecraft:torch",
@@ -242,7 +242,7 @@ fn incremental(
"minecraft:glass",
]
.iter()
.filter_map(|n| registry.default_state(n).map(|s| s.0 as u16))
.filter_map(|n| registry.default_state(n).map(|s| s.0))
.chain(
facts
.states
@@ -257,7 +257,7 @@ fn incremental(
]
.contains(&&*f.name)
})
.map(|(i, _)| i as u16)
.map(|(i, _)| i as u32)
.step_by(3),
)
.collect();
@@ -56,16 +56,16 @@ fn main() -> horizon_client::Result<()> {
"resource_load_ms={:.3}",
start.elapsed().as_secs_f64() * 1000.0
);
let inactive: Vec<u16> = facts
let inactive: Vec<u32> = facts
.states
.iter()
.enumerate()
.filter(|(_, f)| !f.random_ticks)
.take(128)
.map(|(i, _)| i as u16)
.map(|(i, _)| i as u32)
.collect();
let active = registry.default_state("minecraft:grass_block").unwrap().0 as u16;
assert!(facts.get(BlockStateId(u32::from(active))).random_ticks);
let active = registry.default_state("minecraft:grass_block").unwrap().0;
assert!(facts.get(BlockStateId(active)).random_ticks);
for (name, palette) in [("sparse", 1), ("dense", 128)] {
let start = Instant::now();
let mut chunks = FxHashMap::default();
@@ -4,7 +4,7 @@
//! Exact NBT equality is always validated. No GPU, compression or disk I/O timed.
use horizon_client::{
biome::Biomes,
chunk::{BLOCK_STRATEGY, Chunk},
chunk::{Chunk, block_strategy},
containers::BlockEntity,
items::ItemStack,
save::{self, ChunkExtras},
@@ -64,7 +64,7 @@ fn fixture(per_chunk: usize) -> (Vec<Arc<Chunk>>, Entities, Names) {
};
let mut template = Chunk::empty(0, 0);
for s in &mut template.sections[..8] {
Arc::make_mut(s).blocks = PalettedContainer::new(BLOCK_STRATEGY, 1);
Arc::make_mut(s).blocks = PalettedContainer::new(block_strategy(), 1);
}
template.heights = Arc::new([64; 256]);
for i in 0..256 {
@@ -257,9 +257,9 @@ fn main() -> horizon_client::Result<()> {
);
Ok(())
}
fn tables_air(assets: &Assets) -> u16 {
fn tables_air(assets: &Assets) -> u32 {
assets
.registry
.default_state("minecraft:air")
.map_or(0, |s| s.0 as u16)
.map_or(0, |s| s.0)
}
@@ -83,7 +83,7 @@ fn scene(
for block in row["blocks"].as_array().ok_or("missing blocks")? {
let (x, y, z) = (int(&block[0])?, int(&block[1])?, int(&block[2])?);
let i = chunk_index(x, z)?;
chunks[i].set(x & 15, y, z & 15, u16::try_from(int(&block[3])?)?);
chunks[i].set(x & 15, y, z & 15, u32::try_from(int(&block[3])?)?);
}
for cell in row["lights"].as_array().ok_or("missing lights")? {
let (x, y, z) = (int(&cell[0])?, int(&cell[1])?, int(&cell[2])?);
+3
View File
@@ -189,6 +189,9 @@ impl Assets {
.default_state(&format!("minecraft:{name}"))
.ok_or_else(|| invalid("missing test-scene material"))?;
}
// Chunks hold every state of this registry: content mods can take it
// past vanilla's 16-bit global palette.
crate::chunk::set_block_states(registry.len());
let registry: &'static BlockRegistry = Box::leak(Box::new(registry));
Self::with_models(
root,
+38 -24
View File
@@ -9,11 +9,26 @@ pub const MIN_Y: i32 = -64;
pub const HEIGHT: i32 = 384;
pub const SECTIONS: usize = (HEIGHT / 16) as usize;
/// Block states are `PalettedContainer`s over the pinned registry (35,723
/// states: a 16-bit global palette); biomes are 4x4x4 containers over the
/// biome table (67 entries: a 7-bit global palette).
pub const BLOCK_STRATEGY: Strategy = Strategy::blocks(16);
/// Biomes are 4x4x4 containers over the biome table (67 entries: a 7-bit
/// global palette).
pub const BIOME_STRATEGY: Strategy = Strategy::biomes(7);
/// Bits of the block states' global palette: 16 for vanilla's 35,723 states
/// (what vanilla servers and clients send), more when content mods add states
/// past 65,536 (docs/modpacks.md). Set once, before the first chunk.
static BLOCK_BITS: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(16);
/// Block states are `PalettedContainer`s over the registry: vanilla's 16-bit
/// global palette, wider with enough content mods.
pub fn block_strategy() -> Strategy {
Strategy::blocks(BLOCK_BITS.load(std::sync::atomic::Ordering::Relaxed))
}
/// Sizes the block states' global palette for a registry of `states`.
pub fn set_block_states(states: usize) {
let bits = horizon_core::paletted::ceil_log2(states).max(16);
BLOCK_BITS.store(bits, std::sync::atomic::Ordering::Relaxed);
}
/// Packed light per block: sky light in the high nibble, block light in the low one.
#[derive(Clone)]
pub enum Light {
@@ -43,7 +58,7 @@ pub struct Section {
impl Default for Section {
fn default() -> Self {
Self {
blocks: PalettedContainer::new(BLOCK_STRATEGY, 0),
blocks: PalettedContainer::new(block_strategy(), 0),
biomes: PalettedContainer::new(BIOME_STRATEGY, 0),
light: Light::Uniform(0xF0),
}
@@ -51,27 +66,26 @@ impl Default for Section {
}
impl Section {
#[inline]
pub fn get(&self, i: usize) -> u16 {
self.blocks.get(i) as u16
pub fn get(&self, i: usize) -> u32 {
self.blocks.get(i)
}
pub fn set(&mut self, i: usize, v: u16) -> u16 {
self.blocks.set(i, u32::from(v)) as u16
pub fn set(&mut self, i: usize, v: u32) -> u32 {
self.blocks.set(i, v)
}
/// All 4,096 block states in index order.
pub fn unpack(&self, out: &mut [u16; 4096]) {
self.blocks.unpack_u16(out);
pub fn unpack(&self, out: &mut [u32; 4096]) {
self.blocks.unpack(out);
}
/// The block states of a whole section at once.
pub fn from_blocks(values: &[u16; 4096]) -> PalettedContainer {
let wide: Vec<u32> = values.iter().map(|&v| u32::from(v)).collect();
PalettedContainer::from_values(BLOCK_STRATEGY, &wide)
pub fn from_blocks(values: &[u32; 4096]) -> PalettedContainer {
PalettedContainer::from_values(block_strategy(), values)
}
#[inline]
pub fn light(&self, i: usize) -> u8 {
self.light.get(i)
}
pub fn is_uniform(&self, v: u16) -> bool {
self.blocks.is_uniform(u32::from(v))
pub fn is_uniform(&self, v: u32) -> bool {
self.blocks.is_uniform(v)
}
/// Heap bytes held by the block, biome and light storage (benchmark accounting).
pub fn heap_bytes(&self) -> usize {
@@ -147,7 +161,7 @@ impl Chunk {
}
}
#[inline]
pub fn get(&self, x: i32, y: i32, z: i32) -> u16 {
pub fn get(&self, x: i32, y: i32, z: i32) -> u32 {
let s = (y - MIN_Y) >> 4;
if !(0..SECTIONS as i32).contains(&s) {
return 0;
@@ -156,11 +170,11 @@ impl Chunk {
}
#[inline]
pub fn block(&self, x: i32, y: i32, z: i32) -> BlockStateId {
BlockStateId(u32::from(self.get(x, y, z)))
BlockStateId(self.get(x, y, z))
}
/// The highest y where `matches`, scanning down. Sections holding only
/// `skip` are passed over whole: `matches` must be false for it.
pub fn highest(&self, skip: u16, mut matches: impl FnMut(i32) -> bool) -> Option<i32> {
pub fn highest(&self, skip: u32, mut matches: impl FnMut(i32) -> bool) -> Option<i32> {
self.sections
.iter()
.enumerate()
@@ -171,7 +185,7 @@ impl Chunk {
(base..base + 16).rev().find(|&y| matches(y))
})
}
pub fn set(&mut self, x: i32, y: i32, z: i32, v: u16) -> u16 {
pub fn set(&mut self, x: i32, y: i32, z: i32, v: u32) -> u32 {
let s = (y - MIN_Y) >> 4;
if !(0..SECTIONS as i32).contains(&s) {
return v;
@@ -228,15 +242,15 @@ mod tests {
Arc::make_mut(&mut chunk.sections[5]).set(i, cave_air);
}
chunk.set(3, 150, 4, stone);
let full = |c: &Chunk, matches: &dyn Fn(u16) -> bool| {
let full = |c: &Chunk, matches: &dyn Fn(u32) -> bool| {
(MIN_Y..MIN_Y + HEIGHT)
.rev()
.find(|&y| matches(c.get(3, y, 4)))
};
for matches in [
&(|v: u16| v != air) as &dyn Fn(u16) -> bool,
&|v: u16| v == stone,
&|v: u16| v == cave_air,
&(|v: u32| v != air) as &dyn Fn(u32) -> bool,
&|v: u32| v == stone,
&|v: u32| v == cave_air,
] {
let fast = chunk.highest(air, |y| matches(chunk.get(3, y, 4)));
assert_eq!(fast, full(&chunk, matches));
+1 -1
View File
@@ -76,7 +76,7 @@ impl<'a> IntoIterator for &'a ChunkMap {
}
}
impl crate::light::LightWorld for ChunkMap {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u16> {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u32> {
self.columns.state(x, y, z)
}
fn light(&self, x: i32, y: i32, z: i32) -> Option<u8> {
+9 -1
View File
@@ -1209,7 +1209,15 @@ impl Recipes {
for (prefix, path) in files {
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
let name = format!("{prefix}{stem}");
let v: Value = serde_json::from_slice(&fs::read(&path)?)?;
// Vanilla's RecipeManager logs a file that does not parse and
// goes on ("Couldn't parse data file").
let v: Value = match serde_json::from_slice(&fs::read(&path)?) {
Ok(v) => v,
Err(e) => {
log::warn!("Horizon: recipe {name} not loaded: {e}");
continue;
}
};
let text = |k: &str| v[k].as_str().unwrap_or("").to_owned();
meta.insert(name.clone(), (text("category"), text("group")));
let ing = |k: &str| Ingredient::parse(&v[k], items);
+2 -2
View File
@@ -4,7 +4,7 @@ use horizon_world::{nbt::Tag, registry::BlockRegistry};
/// The layers of a flat generator, bottom up from the world's minimum Y.
pub struct FlatTerrain {
layers: Vec<u16>,
layers: Vec<u32>,
biome: u16,
}
impl FlatTerrain {
@@ -30,7 +30,7 @@ impl FlatTerrain {
for layer in list {
let block = crate::save::text(layer.get("block"))?;
let height = crate::save::number(layer.get("height")).unwrap_or(0).max(0);
let state = registry.default_state(&block)?.0 as u16;
let state = registry.default_state(&block)?.0;
layers.extend(std::iter::repeat_n(state, height as usize));
}
}
+1 -1
View File
@@ -1127,7 +1127,7 @@ struct Game<'a> {
lan_events: Vec<(i32, [i32; 3], i32, Option<u32>)>,
/// Block events (position, b0, b1, state) and entity events (id, event,
/// position) for the guests.
lan_block_events: Vec<([i32; 3], i32, i32, u16)>,
lan_block_events: Vec<([i32; 3], i32, i32, u32)>,
lan_entity_events: Vec<(i32, i8, [f64; 3])>,
/// `ContainerOpenersCounter`: how many players (the host and the guests)
/// have each chest open.
+4 -7
View File
@@ -2312,7 +2312,7 @@ impl Game<'_> {
};
let from = self.source_position().map(|v| v.floor() as i32);
const RANGE: i32 = 256;
let mut cache: std::collections::HashMap<u16, Option<&'static str>> =
let mut cache: std::collections::HashMap<u32, Option<&'static str>> =
std::collections::HashMap::new();
let mut best: Option<(i64, [i32; 3], &'static str)> = None;
let (cx0, cz0) = ((from[0] - RANGE) >> 4, (from[2] - RANGE) >> 4);
@@ -2323,15 +2323,12 @@ impl Game<'_> {
continue;
};
for (i, section) in chunk.sections.iter().enumerate() {
let mut wanted_state = |v: u32| {
let state = v as u16;
let mut wanted_state = |state: u32| {
cache
.entry(state)
.or_insert_with(|| {
self.poi_type(horizon_world::registry::BlockStateId(u32::from(
state,
)))
.filter(|t| wanted.iter().any(|w| w == t))
self.poi_type(horizon_world::registry::BlockStateId(state))
.filter(|t| wanted.iter().any(|w| w == t))
})
.is_some()
};
+1 -1
View File
@@ -333,7 +333,7 @@ impl Game<'_> {
}
for i in 0..4096 {
let id = section.get(i);
if !hearts.get(usize::from(id)).copied().unwrap_or(false) {
if !hearts.get(id as usize).copied().unwrap_or(false) {
continue;
}
let (lx, lz, ly) = ((i & 15) as i32, ((i >> 4) & 15) as i32, (i >> 8) as i32);
+2 -2
View File
@@ -262,7 +262,7 @@ fn mob_meta(m: &Mob, hub: &Hub) -> Vec<(u8, i32, Value)> {
meta.push((
16,
serializer::OPTIONAL_BLOCK_STATE,
Value::Id(i32::from(state.0 as u16)),
Value::Id(state.0 as i32),
));
}
}
@@ -950,7 +950,7 @@ impl Game<'_> {
/// `Level.blockEvent` for the guests (`ClientboundBlockEventPacket`).
pub(super) fn lan_block_event(&mut self, pos: [i32; 3], b0: i32, b1: i32) {
if self.audio.capture.is_some() {
let state = self.world.block(pos[0], pos[1], pos[2]).0 as u16;
let state = self.world.block(pos[0], pos[1], pos[2]).0;
self.lan_block_events.push((pos, b0, b1, state));
}
}
@@ -206,7 +206,7 @@ impl NaturalLevel for GameLevel<'_, '_> {
.chunks
.get(&(x >> 4, z >> 4))
.and_then(|chunk| {
chunk.highest(world.air.0 as u16, |y| {
chunk.highest(world.air.0, |y| {
spawn_selection::heightmap_blocks(self, kind, x, y, z)
})
})
+28 -4
View File
@@ -9,7 +9,7 @@
use super::{Dim, Game, Mining, Screen};
use horizon_client::{
Result,
chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section},
chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section, block_strategy},
containers::{BlockEntity, Furnace, FurnaceKind},
entities::{Experience, ItemEntity},
hud::{HudBatch, rgba},
@@ -22,7 +22,11 @@ use horizon_client::{
streaming::{ChunkGenerator, Proto, Streaming},
world::World,
};
use horizon_core::{geometry::Vec3, paletted::PalettedContainer, random::RandomSource};
use horizon_core::{
geometry::Vec3,
paletted::{PalettedContainer, Strategy},
random::RandomSource,
};
use horizon_protocol::{
entity_data::{ITEM_ENTITY_ITEM, Value},
item::{Hashed, Stack},
@@ -633,11 +637,31 @@ impl Game<'_> {
if !(0..SECTIONS as i32).contains(&target) {
continue;
}
let strategy = BLOCK_STRATEGY;
// A global section is as wide as the server's registry: 16 bits
// from a vanilla server, wider from a Horizon host with the same
// content mods. Read it at that width, then widen it to ours.
let ours = block_strategy();
let strategy = if ours.wire_has_palette(s.states.bits) {
ours
} else {
Strategy::blocks(s.states.bits)
};
let palette = s.states.palette.iter().map(|&p| p as u32).collect();
let words = s.states.data.iter().map(|&w| w as u64).collect();
let blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words)
let mut blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words)
.map_err(|e| e.to_string())?;
if strategy.global_bits != ours.global_bits {
let mut values = [0u32; 4096];
blocks.unpack(&mut values);
// Ids past our registry (a host with mods we lack) read as air.
let limit = 1u64 << ours.global_bits;
for v in &mut values {
if u64::from(*v) >= limit {
*v = 0;
}
}
blocks = PalettedContainer::from_values(ours, &values);
}
let mut section = Section {
blocks,
biomes: PalettedContainer::from_values(BIOME_STRATEGY, &cells),
+8 -11
View File
@@ -301,11 +301,11 @@ impl Game<'_> {
self.world.watched = t.kinds.iter().map(|k| *k != 0).collect();
self.sculk.tables = t;
}
fn kind_of_state(&self, state: u16) -> u8 {
fn kind_of_state(&self, state: u32) -> u8 {
self.sculk
.tables
.kinds
.get(usize::from(state))
.get(state as usize)
.copied()
.unwrap_or(0)
}
@@ -345,10 +345,10 @@ impl Game<'_> {
// SculkShriekerBlockEntity.preRemoveSideEffects.
if old_kind == KIND_SHRIEKER
&& let Some(BlockEntity::Sculk(e)) = self.world.block_entities.get(&key)
&& self.shrieker_shrieking(BlockStateId(u32::from(old)))
&& self.shrieker_shrieking(BlockStateId(old))
{
let warning = e.warning_level;
self.shrieker_respond(key, BlockStateId(u32::from(old)), warning);
self.shrieker_respond(key, BlockStateId(old), warning);
}
if matches!(
self.world.block_entities.get(&key),
@@ -375,17 +375,14 @@ impl Game<'_> {
let y0 = MIN_Y + s as i32 * 16;
// Without a sculk state in its palette, no block of the section
// is one: most sections are passed over without reading a block.
if !section
.blocks
.maybe_has(|v| u16::try_from(v).is_ok_and(|v| self.kind_of_state(v) != 0))
{
if !section.blocks.maybe_has(|v| self.kind_of_state(v) != 0) {
continue;
}
if section.blocks.uniform().is_some() {
// A uniform section of sculk blocks does not exist.
continue;
}
let mut a = [0u16; 4096];
let mut a = [0u32; 4096];
section.unpack(&mut a);
for (i, v) in a.iter().enumerate() {
if self.kind_of_state(*v) != 0 {
@@ -855,12 +852,12 @@ impl Game<'_> {
let state = self.world.block(key.0, key.1, key.2);
let is_warden = matches!(who, Who::Mob(id)
if self.mobs.iter().any(|m| m.id == id && m.species() == Species::Warden));
match self.kind_of_state(state.0 as u16) {
match self.kind_of_state(state.0) {
KIND_SENSOR | KIND_CALIBRATED => {
if is_warden || !self.res.behaviours.sculk_sensor_can_activate(state) {
continue;
}
let calibrated = self.kind_of_state(state.0 as u16) == KIND_CALIBRATED;
let calibrated = self.kind_of_state(state.0) == KIND_CALIBRATED;
if calibrated {
let back = self.calibrated_signal(key);
if back != 0 && back != 1 {
+9 -1
View File
@@ -385,7 +385,7 @@ fn sprite_split() -> u32 {
.unwrap_or(SPRITE_SPLIT)
}
/// Sprite arrays terrain.wgsl binds (`sprites`, `sprites_1`...).
const SPRITE_ARRAYS: u32 = 4;
const SPRITE_ARRAYS: u32 = 8;
/// The views of terrain.wgsl's sprite arrays, the unused ones a 1-layer
/// stand-in.
fn sprite_views(
@@ -441,6 +441,10 @@ fn terrain_group(
view(5, &sprites[1]),
view(6, &sprites[2]),
view(7, &sprites[3]),
view(8, &sprites[4]),
view(9, &sprites[5]),
view(10, &sprites[6]),
view(11, &sprites[7]),
],
})
}
@@ -743,6 +747,10 @@ impl Renderer {
entry(5, vf, sprite_array),
entry(6, vf, sprite_array),
entry(7, vf, sprite_array),
entry(8, vf, sprite_array),
entry(9, vf, sprite_array),
entry(10, vf, sprite_array),
entry(11, vf, sprite_array),
],
});
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
+17 -13
View File
@@ -124,7 +124,7 @@ struct Encoded {
/// A section's blocks that have a block entity type, as (index, state) in
/// index order (y, then z, then x).
type Entities = Arc<[(u16, u16)]>;
type Entities = Arc<[(u16, u32)]>;
/// One section's light as DataLayers (two blocks per byte, the even one in
/// the low nibble), and whether each holds any light.
@@ -326,23 +326,23 @@ impl ChunkEncoder {
// The heightmaps depend on the level's height.
self.heights.get_mut().unwrap().clear();
}
pub fn entity_type(&self, state: u16) -> Option<i32> {
pub fn entity_type(&self, state: u32) -> Option<i32> {
self.entity_types
.get(usize::from(state))
.get(state as usize)
.filter(|t| **t >= 0)
.map(|t| i32::from(*t))
}
pub fn is_air(&self, state: u16) -> bool {
pub fn is_air(&self, state: u32) -> bool {
self.flags
.get(usize::from(state))
.get(state as usize)
.is_none_or(|f| f & NON_AIR == 0)
}
fn section(&self, section: &Section) -> (horizon_protocol::chunk::Section, Vec<(u16, u16)>) {
fn section(&self, section: &Section) -> (horizon_protocol::chunk::Section, Vec<(u16, u32)>) {
let mut values = Vec::with_capacity(BLOCKS);
let (mut non_empty, mut fluids, mut entities) = (0i16, 0i16, Vec::new());
let mut count = |state: u16, n: i16| {
let f = self.flags.get(usize::from(state)).copied().unwrap_or(0);
let mut count = |state: u32, n: i16| {
let f = self.flags.get(state as usize).copied().unwrap_or(0);
if f & NON_AIR != 0 {
non_empty += n;
if f & FLUID != 0 {
@@ -354,17 +354,17 @@ impl ChunkEncoder {
// One block everywhere (all air, mostly): what from_values would give.
let uniform = section.blocks.uniform();
if let Some(v) = uniform {
if count(v as u16, BLOCKS as i16) {
entities = (0..BLOCKS as u16).map(|i| (i, v as u16)).collect();
if count(v, BLOCKS as i16) {
entities = (0..BLOCKS as u16).map(|i| (i, v)).collect();
}
} else {
let mut unpacked = [0u16; BLOCKS];
let mut unpacked = [0u32; BLOCKS];
section.unpack(&mut unpacked);
for (i, &v) in unpacked.iter().enumerate() {
if count(v, 1) {
entities.push((i as u16, v));
}
values.push(i32::from(v));
values.push(v as i32);
}
}
let biome_values: Vec<i32> = (0..BIOMES)
@@ -386,7 +386,11 @@ impl ChunkEncoder {
palette: vec![v as i32],
data: Vec::new(),
},
None => Paletted::from_values(&values, Strategy::Blocks, 16),
None => Paletted::from_values(
&values,
Strategy::Blocks,
crate::chunk::block_strategy().global_bits,
),
},
biomes: Paletted::from_values(&biome_values, Strategy::Biomes, self.biome_bits),
},
+2 -2
View File
@@ -79,11 +79,11 @@ impl Hub {
}
/// ClientboundBlockEventPacket (`PlayerList.broadcast` within 64 blocks).
pub fn block_event(&self, pos: [i32; 3], b0: i32, b1: i32, state: u16) {
pub fn block_event(&self, pos: [i32; 3], b0: i32, b1: i32, state: u32) {
let block = self
.encoder
.block_ids
.get(usize::from(state))
.get(state as usize)
.copied()
.unwrap_or(0);
let at = [f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])];
+2 -2
View File
@@ -1193,7 +1193,7 @@ impl Hub {
let (x, y, z) = positions[0];
C::BlockUpdate(play::BlockUpdate {
pos: [x, y, z],
state: i32::from(world.block(x, y, z).0 as u16),
state: world.block(x, y, z).0 as i32,
})
} else {
C::SectionBlocksUpdate(play::SectionBlocksUpdate {
@@ -1222,7 +1222,7 @@ impl Hub {
let Some(tag) = super::chunks::entity_tag(entity) else {
continue;
};
let state = world.block(x, y, z).0 as u16;
let state = world.block(x, y, z).0;
let Some(kind) = self.encoder.entity_type(state) else {
continue;
};
+12 -12
View File
@@ -165,14 +165,14 @@ impl LightTables {
fn edge_occluded(&self, top: usize, bottom: usize) -> bool {
self.cells[bottom].dampening != 0 || self.blocked(top, bottom, Direction::Down as usize)
}
pub fn emission(&self, state: u16) -> u8 {
pub fn emission(&self, state: u32) -> u8 {
self.cells[state as usize].emission
}
pub fn dampening(&self, state: u16) -> u8 {
pub fn dampening(&self, state: u32) -> u8 {
self.cells[state as usize].dampening
}
/// Whether a state change can change light (`hasDifferentLightProperties`).
pub fn differs(&self, old: u16, new: u16) -> bool {
pub fn differs(&self, old: u32, new: u32) -> bool {
let (a, b) = (&self.cells[old as usize], &self.cells[new as usize]);
old != new
&& (a.dampening != b.dampening || a.emission != b.emission || a.shaped || b.shaped)
@@ -211,12 +211,12 @@ pub fn light_chunk(
// up to 14 blocks into it (sky light there is always 15).
let h = ((top_section + 1) * 16).min(HEIGHT as usize);
let at = |x: usize, y: usize, z: usize| (y * W + z) * W + x;
let mut states = vec![0u16; W * W * h];
let mut states = vec![0u32; W * W * h];
let mut sky = vec![0u8; W * W * h];
let mut block = vec![0u8; W * W * h];
let mut queue = VecDeque::new();
let mut sky_queue = VecDeque::new();
let mut ids = Box::new([0u16; 4096]);
let mut ids = Box::new([0u32; 4096]);
for (dz, row) in region.iter().enumerate() {
for (dx, chunk) in row.iter().enumerate() {
let Some(chunk) = chunk else { continue };
@@ -391,13 +391,13 @@ pub fn apply_relighting(chunk: &mut Chunk, lights: Vec<Light>) -> Vec<(i32, i32,
/// Resident chunks the incremental relighter reads and writes.
pub trait LightWorld {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u16>;
fn state(&self, x: i32, y: i32, z: i32) -> Option<u32>;
/// Packed light (sky << 4 | block), None outside loaded chunks.
fn light(&self, x: i32, y: i32, z: i32) -> Option<u8>;
fn set_light(&mut self, x: i32, y: i32, z: i32, packed: u8);
}
impl<S: std::hash::BuildHasher> LightWorld for HashMap<(i32, i32), Arc<Chunk>, S> {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u16> {
fn state(&self, x: i32, y: i32, z: i32) -> Option<u32> {
if !(MIN_Y..MIN_Y + HEIGHT).contains(&y) {
return None;
}
@@ -611,10 +611,10 @@ fn relight_layer(
mod tests {
use super::*;
const AIR: u16 = 0;
const STONE: u16 = 1;
const TORCH: u16 = 2;
const SLAB: u16 = 3;
const AIR: u32 = 0;
const STONE: u32 = 1;
const TORCH: u32 = 2;
const SLAB: u32 = 3;
/// Air, stone, torch and a bottom slab (full bottom face, half sides,
/// empty top), with a third face shape for the half sides.
fn tables() -> LightTables {
@@ -889,7 +889,7 @@ mod tests {
for (si, s) in fixture.sections.iter_mut().enumerate() {
let section = Arc::make_mut(s);
for i in 0..4096 {
section.set(i, ((i + si) % 16) as u16);
section.set(i, ((i + si) % 16) as u32);
}
for i in 0..64 {
section.biomes.set(i, (i % 4) as u32);
+8 -13
View File
@@ -434,17 +434,17 @@ fn pi(x: i32, y: i32, z: i32) -> usize {
}
/// Blocks and light of one section plus a `B`-block border.
struct Padded {
blocks: Vec<u16>,
blocks: Vec<u32>,
light: Vec<u8>,
}
impl Padded {
fn new(region: &[[Option<&Chunk>; 3]; 3], sy: i32) -> Self {
let mut blocks = vec![0u16; P * P * P];
let mut blocks = vec![0u32; P * P * P];
let mut light = vec![0u8; P * P * P];
let base_y = MIN_Y + sy * 16;
// The section itself is unpacked in one pass; the border cells of the
// neighbouring sections are read one by one.
let mut own = Box::new([0u16; 4096]);
let mut own = Box::new([0u32; 4096]);
let center = region[1][1].map(|c| &c.sections[sy as usize]);
if let Some(section) = center {
section.unpack(&mut own);
@@ -643,10 +643,8 @@ impl Mesher<'_> {
}
let state = &info[id as usize];
if state.block_entity {
out.block_entities.push((
[center.x * 16 + x, base_y + y, center.z * 16 + z],
u32::from(id),
));
out.block_entities
.push(([center.x * 16 + x, base_y + y, center.z * 16 + z], id));
}
if state.solid
&& Direction::ALL.iter().all(|d| {
@@ -665,10 +663,7 @@ impl Mesher<'_> {
continue;
}
let world = BlockPos::new(center.x * 16 + x, base_y + y, center.z * 16 + z);
let Ok(models) = self
.assets
.model_catalog
.collect(BlockStateId(id.into()), world)
let Ok(models) = self.assets.model_catalog.collect(BlockStateId(id), world)
else {
continue;
};
@@ -700,8 +695,8 @@ impl Mesher<'_> {
&& self
.visibility
.should_render_face(
BlockStateId(id.into()),
BlockStateId(neighbor.into()),
BlockStateId(id),
BlockStateId(neighbor),
d,
true,
)
+6 -6
View File
@@ -159,28 +159,28 @@ struct FlowView<'a> {
info: &'a [Info],
}
impl FlowView<'_> {
fn id(&self, p: BlockPos) -> u16 {
fn id(&self, p: BlockPos) -> u32 {
self.pad.blocks[pi(p.x, p.y, p.z)]
}
}
impl FluidFlowWorld for FlowView<'_> {
type Error = Infallible;
type Block = u16;
type Block = u32;
fn fluid(&mut self, p: BlockPos) -> Result<FluidState, Infallible> {
Ok(self.info[self.id(p) as usize].fluid)
}
fn block(&mut self, p: BlockPos) -> Result<u16, Infallible> {
fn block(&mut self, p: BlockPos) -> Result<u32, Infallible> {
Ok(self.id(p))
}
fn blocks_fluid_flow(&self, state: u16) -> bool {
fn blocks_fluid_flow(&self, state: u32) -> bool {
self.info[state as usize].blocks_flow
}
fn is_ice_class(&self, state: u16) -> bool {
fn is_ice_class(&self, state: u32) -> bool {
self.info[state as usize].ice
}
fn is_face_sturdy(
&mut self,
state: u16,
state: u32,
_: BlockPos,
d: Direction,
) -> Result<bool, Infallible> {
@@ -13,12 +13,12 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() {
let visibility = crate::models::load_visibility(&root, assets.registry).unwrap();
let tables = MeshTables::new(&assets, &facts, visibility.clone()).unwrap();
let biomes = Biomes::load(&root).unwrap();
let state = |name| assets.registry.default_state(name).unwrap().0 as u16;
let state = |name| assets.registry.default_state(name).unwrap().0;
let oak = state("minecraft:oak_leaves");
let birch = state("minecraft:birch_leaves");
let roots = state("minecraft:mangrove_roots");
let stone = state("minecraft:stone");
let mesh = |blocks: &[([i32; 3], u16)], cull_leaves, cutout_leaves| {
let mesh = |blocks: &[([i32; 3], u32)], cull_leaves, cutout_leaves| {
let mut center = Chunk::empty(0, 0);
let mut east = Chunk::empty(1, 0);
for &([x, y, z], state) in blocks {
@@ -51,7 +51,7 @@ fn leaf_culling_removes_only_internal_leaf_and_root_faces() {
let vertices = mesh(&[(at, id)], false, true);
let models = assets
.model_catalog
.collect(BlockStateId(id.into()), BlockPos::new(at[0], at[1], at[2]))
.collect(BlockStateId(id), BlockPos::new(at[0], at[1], at[2]))
.unwrap();
let faces: Vec<_> = models.iter().flat_map(|(_, model)| &model.faces).collect();
assert_eq!(vertices.len(), faces.len() * 4);
@@ -105,7 +105,7 @@ fn fixture(mut opaque: impl FnMut(i32, i32, i32) -> bool) -> Padded {
for y in 0..16 {
for z in 0..16 {
for x in 0..16 {
pad.blocks[pi(x, y, z)] = u16::from(opaque(x, y, z)) + 2 * ((x + y + z) as u16 & 1);
pad.blocks[pi(x, y, z)] = u32::from(opaque(x, y, z)) + 2 * ((x + y + z) as u32 & 1);
}
}
}
+7 -7
View File
@@ -75,8 +75,8 @@ impl Palette {
}
}
#[inline]
fn get(&self, id: u16) -> (u8, u8) {
let i = usize::from(id);
fn get(&self, id: u32) -> (u8, u8) {
let i = id as usize;
(
self.color.get(i).copied().unwrap_or(0),
self.flags.get(i).copied().unwrap_or(AIR),
@@ -137,7 +137,7 @@ fn scan_column(
(0, MIN_Y as i16, 0)
}
/// The highest block of a chunk worth scanning from.
fn top_of(chunk: &Chunk, air: u16, layer: Layer) -> i32 {
fn top_of(chunk: &Chunk, air: u32, layer: Layer) -> i32 {
let surface = (0..SECTIONS)
.rev()
.find(|&s| {
@@ -152,7 +152,7 @@ fn top_of(chunk: &Chunk, air: u16, layer: Layer) -> i32 {
Layer::Below(head) => surface.min(head),
}
}
fn scan_tile(chunk: &Chunk, palette: &Palette, air: u16, layer: Layer, revision: u64) -> Tile {
fn scan_tile(chunk: &Chunk, palette: &Palette, air: u32, layer: Layer, revision: u64) -> Tile {
let top = top_of(chunk, air, layer);
let mut tile = Tile {
color: [0; 256],
@@ -287,7 +287,7 @@ impl Minimap {
) -> usize {
let palette = self.palette.get_or_insert_with(|| Palette::new(facts));
let started = Instant::now();
let air = world.air.0 as u16;
let air = world.air.0;
// Tiles of unloaded or distant chunks go.
self.tiles.retain(|k, _| {
(k.0 - center.0).abs() <= radius + 2
@@ -417,7 +417,7 @@ mod tests {
(LAVA, 2),
])
}
fn chunk(columns: &[(i32, i32, &[(i32, u16)])]) -> Chunk {
fn chunk(columns: &[(i32, i32, &[(i32, u32)])]) -> Chunk {
let mut c = Chunk::empty(0, 0);
for &(x, z, blocks) in columns {
for &(y, id) in blocks {
@@ -467,7 +467,7 @@ mod tests {
fn below_a_height_shows_the_floor_and_cuts_walls() {
let p = palette();
// A cave: stone everywhere from 0 to 20, air at 10 and 11 in column 0.
let mut blocks: Vec<(i32, u16)> = (0..=20).map(|y| (y, 2)).collect();
let mut blocks: Vec<(i32, u32)> = (0..=20).map(|y| (y, 2)).collect();
let solid = blocks.clone();
blocks.retain(|&(y, _)| y != 10 && y != 11);
let c = chunk(&[(0, 0, &blocks), (1, 0, &solid)]);
+6 -4
View File
@@ -323,7 +323,7 @@ impl<'a> Plan<'a> {
}
let list: Vec<String> = by_mod.iter().map(|(m, n)| format!("{m} {n}")).collect();
return Err(format!(
"{next} block states, more than the {MAX_STATES} chunks can hold ({} in the game; \
"{next} block states, more than the {MAX_STATES} a world can hold ({} in the game; \
the mods: {})",
names.len(),
list.join(", ")
@@ -644,9 +644,11 @@ const NBT_TABLES: [&str; 6] = [
/// In a root built here: the content mods' ids, the namespaces loaders
/// read besides `minecraft`.
const MARKER: &str = "horizon-mod-content.json";
/// Block states a world can hold: chunk sections store a state in 16 bits
/// (crate::chunk). Mods past it are refused, not wrapped onto other blocks.
pub const MAX_STATES: u32 = 1 << 16;
/// Block states a world can hold. Chunk sections widen their global palette
/// past vanilla's 16 bits to fit the registry (crate::chunk::set_block_states);
/// this caps it at 20 bits, about 29 times vanilla's 35,723 states. Mods past
/// it are refused, not wrapped onto other blocks.
pub const MAX_STATES: u32 = 1 << 20;
/// The content mods' namespaces in a root built by [`build`]; none in any
/// other root, so vanilla's loaders read exactly what they did.
+24 -4
View File
@@ -266,17 +266,17 @@ fn refuses_unknown_templates_and_vanilla_ids() {
}
#[test]
fn refuses_more_states_than_chunks_hold() {
fn refuses_more_states_than_a_world_holds() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-states-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let mut m = ruby_mod(&scratch.join("ruby"));
// Redstone wire has 1296 states: 40 copies pass any game's 65536.
// Redstone wire has 1296 states: 810 copies pass 2^20 with any game.
let mut wire = m.content.blocks[0].clone();
wire.template = "minecraft:redstone_wire".into();
for i in 0..40 {
for i in 0..810 {
let mut b = wire.clone();
b.id = format!("ruby:wire_{i}");
m.content.blocks.push(b);
@@ -284,13 +284,33 @@ fn refuses_more_states_than_chunks_hold() {
let e = build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap_err();
assert!(
e.to_string()
.contains("more than the 65536 chunks can hold"),
.contains("more than the 1048576 a world can hold"),
"{e}"
);
assert!(e.to_string().contains("the mods: ruby "), "{e}");
fs::remove_dir_all(&scratch).unwrap();
}
#[test]
fn builds_past_sixteen_bit_states() {
let Some(base) = prepared() else {
return;
};
let scratch = std::env::temp_dir().join(format!("horizon-mod-wide-{}", std::process::id()));
let _ = fs::remove_dir_all(&scratch);
let mut m = ruby_mod(&scratch.join("ruby"));
// 40 redstone wires: 51,840 states, past 65,536 with vanilla's 35,723.
let mut wire = m.content.blocks[0].clone();
wire.template = "minecraft:redstone_wire".into();
for i in 0..40 {
let mut b = wire.clone();
b.id = format!("ruby:wire_{i}");
m.content.blocks.push(b);
}
build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap();
fs::remove_dir_all(&scratch).unwrap();
}
#[test]
fn no_content_keeps_the_base_root() {
let base = Path::new("/nonexistent/base");
+42 -8
View File
@@ -42,25 +42,52 @@ impl ParsedModels {
/// Blocks require every referenced resource, including parents. Items instead
/// skip broken resources and use the resolver's missing-model fallback.
pub(crate) fn block_library(
pub(crate) fn block_library(&self, root: &Path, pending: Vec<String>) -> Result<ModelLibrary> {
self.block_models(root, pending, false)
}
/// [`Self::block_library`], but a model that does not read takes the
/// built-in missing model, logged, as vanilla's model discovery does
/// ("Missing block model"). Mods' content gets this; vanilla's resources
/// always read.
pub(crate) fn block_library_or_missing(
&self,
root: &Path,
pending: Vec<String>,
) -> Result<ModelLibrary> {
self.block_models(root, pending, true)
}
fn block_models(
&self,
root: &Path,
mut pending: Vec<String>,
or_missing: bool,
) -> Result<ModelLibrary> {
let mut models = BTreeMap::new();
let mut skipped = std::collections::BTreeSet::new();
while let Some(name) = pending.pop() {
if models.contains_key(&name) {
if models.contains_key(&name) || skipped.contains(&name) {
continue;
}
let model = match self.models.get(&name) {
Some(model) => Arc::clone(model),
None => {
let (namespace, path) =
name.split_once(':').ok_or("invalid model identifier")?;
let text = fs::read_to_string(
root.join(format!("assets/{namespace}/models/{path}.json")),
)?;
Arc::new(CuboidModel::from_json(&text)?)
let read = || -> Result<CuboidModel> {
let (namespace, path) =
name.split_once(':').ok_or("invalid model identifier")?;
let text = fs::read_to_string(
root.join(format!("assets/{namespace}/models/{path}.json")),
)?;
Ok(CuboidModel::from_json(&text)?)
};
match read() {
Ok(model) => Arc::new(model),
Err(e) if or_missing => {
log::warn!("Horizon: missing block model {name}: {e}");
skipped.insert(name);
continue;
}
Err(e) => return Err(e),
}
}
};
if let Some(parent) = &model.parent {
@@ -141,6 +168,13 @@ mod tests {
.to_string()
};
assert_eq!(load(&direct), load(&parsed));
// Mods' content: vanilla's missing model instead of an error.
let lenient = parsed.block_library_or_missing(&fixture.0, vec![name.into()])?;
assert_eq!(
format!("{:?}", lenient.resolve(name)?.elements()),
format!("{:?}", lenient.resolve(MISSING_MODEL)?.elements()),
"{name}"
);
}
let items = parsed.into_item_library()?;
let block = shared.resolve("horizon:nested/child")?;
+30 -8
View File
@@ -136,11 +136,29 @@ impl StateModels {
})
.collect::<Result<_>>()?;
let possible: Vec<Properties> = serde_json::from_value(facts["states"].clone())?;
let resource = fs::read_to_string(
// Vanilla's BlockStateModelLoader: a definition that does not
// read or bind is logged and its block gets no model; the other
// blocks still load.
let loaded = fs::read_to_string(
root.join(format!("assets/{namespace}/blockstates/{path}.json")),
)?;
let definition = BlockStateDefinition::from_json(&resource)?;
let bound = definition.bind(&schema, &possible)?;
)
.map_err(|e| e.to_string())
.and_then(|text| BlockStateDefinition::from_json(&text).map_err(|e| e.to_string()))
.and_then(|definition| {
let bound = definition
.bind(&schema, &possible)
.map_err(|e| e.to_string())?;
Ok((definition, bound))
});
let (definition, bound) = match loaded {
Ok(loaded) => loaded,
Err(e) => {
log::warn!(
"Horizon: blockstate definition of {namespace}:{path} not loaded: {e}"
);
continue;
}
};
let native_order: Vec<_> = by_name
.get(name.as_str())
.map(Vec::as_slice)
@@ -185,10 +203,14 @@ impl StateModels {
}
choices.push(CompiledChoices::new(&definition)?);
}
let library = models.block_library(
root,
variants.iter().map(|(name, _, _)| name.clone()).collect(),
)?;
let names: Vec<String> = variants.iter().map(|(name, _, _)| name.clone()).collect();
let library = match models.block_library(root, names.clone()) {
Ok(library) => library,
Err(e) => {
log::warn!("Horizon: block models incomplete ({e}); using the missing model");
models.block_library_or_missing(root, names)?
}
};
let mut baked = BTreeMap::new();
for (name, turns, lock) in variants {
let model = library.resolve(&name)?;
+2 -2
View File
@@ -54,12 +54,12 @@ impl<T: Copy> PoiSections<T> {
return cached.pois.clone();
}
let pois: SectionPois<T> = if section.blocks.maybe_has(|v| kind(v).is_some()) {
let mut states = [0u16; 4096];
let mut states = [0u32; 4096];
section.unpack(&mut states);
states
.iter()
.enumerate()
.filter_map(|(i, &state)| kind(u32::from(state)).map(|poi| (i as u16, poi)))
.filter_map(|(i, &state)| kind(state).map(|poi| (i as u16, poi)))
.collect()
} else {
Arc::from([])
@@ -134,7 +134,7 @@ mod tests {
let mut chunk = Chunk::empty(0, 0);
for i in [0, 9, SECTIONS - 1] {
Arc::make_mut(&mut chunk.sections[i]).blocks =
Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u16));
Section::from_blocks(&std::array::from_fn(|j| (j % 300) as u32));
}
let chunk = Arc::new(chunk);
let mut cache = RandomTickSections::default();
@@ -176,12 +176,9 @@ mod tick_parity {
}
fn set_raw(&mut self, pos: BlockPos, state: BlockStateId) -> Option<BlockStateId> {
let chunk = self.chunks.get_mut(&(pos.x >> 4, pos.z >> 4))?;
Some(BlockStateId(u32::from(Arc::make_mut(chunk).set(
pos.x,
pos.y,
pos.z,
state.0 as u16,
))))
Some(BlockStateId(
Arc::make_mut(chunk).set(pos.x, pos.y, pos.z, state.0),
))
}
fn is_loaded(&self, pos: BlockPos) -> bool {
self.chunks.contains_key(&(pos.x >> 4, pos.z >> 4))
@@ -233,7 +230,7 @@ mod tick_parity {
// Every sampled block can run real behaviour and change to dirt.
for i in [4, 7] {
Arc::make_mut(&mut c.sections[i]).blocks =
crate::chunk::Section::from_blocks(&[grass.0 as u16; 4096]);
crate::chunk::Section::from_blocks(&[grass.0; 4096]);
}
Arc::new(c)
};
+8 -11
View File
@@ -16,7 +16,7 @@
use crate::{
Result,
biome::Biomes,
chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS},
chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, block_strategy},
streaming::ChunkGenerator,
};
use horizon_core::{
@@ -238,7 +238,7 @@ pub struct SaveTables {
default_state: Vec<bool>,
block_entity: Vec<bool>,
heightmaps: crate::assets::Properties,
air: u16,
air: u32,
}
impl SaveTables {
pub fn new(assets: &crate::assets::Assets) -> Self {
@@ -266,9 +266,7 @@ impl SaveTables {
default_state,
block_entity: Vec::new(),
heightmaps: assets.properties.clone(),
air: registry
.default_state("minecraft:air")
.map_or(0, |s| s.0 as u16),
air: registry.default_state("minecraft:air").map_or(0, |s| s.0),
}
}
/// Which states carry a block entity (`BlockFacts`), to drop entries for
@@ -1035,8 +1033,8 @@ fn decode_column(
}
}
}
let air = registry.default_state("minecraft:air")?.0 as u16;
let mut cache: HashMap<String, u16> = HashMap::new();
let air = registry.default_state("minecraft:air")?.0;
let mut cache: HashMap<String, u32> = HashMap::new();
let Some(Tag::List(sections)) = tag.get("sections") else {
return Some(chunk);
};
@@ -1051,7 +1049,7 @@ fn decode_column(
if let Some(states) = section.get("block_states")
&& let Some(Tag::List(palette)) = states.get("palette")
{
let ids: Vec<u16> = palette
let palette: Vec<u32> = palette
.iter()
.map(|p| {
let state = BlockState::from_nbt(p).ok();
@@ -1074,7 +1072,7 @@ fn decode_column(
registry.id(&full).or(Some(default))
})
})
.map_or(air, |id| id.0 as u16)
.map_or(air, |id| id.0)
})
})
.collect();
@@ -1085,9 +1083,8 @@ fn decode_column(
}
_ => None,
};
let palette: Vec<u32> = ids.iter().map(|&id| u32::from(id)).collect();
if let Ok(blocks) =
PalettedContainer::from_packed(BLOCK_STRATEGY, palette, data.as_deref())
PalettedContainer::from_packed(block_strategy(), palette, data.as_deref())
{
target.blocks = blocks;
}
+9 -1
View File
@@ -31,6 +31,10 @@ struct Globals {
@group(0) @binding(5) var sprites_1: texture_2d_array<f32>;
@group(0) @binding(6) var sprites_2: texture_2d_array<f32>;
@group(0) @binding(7) var sprites_3: texture_2d_array<f32>;
@group(0) @binding(8) var sprites_4: texture_2d_array<f32>;
@group(0) @binding(9) var sprites_5: texture_2d_array<f32>;
@group(0) @binding(10) var sprites_6: texture_2d_array<f32>;
@group(0) @binding(11) var sprites_7: texture_2d_array<f32>;
// How many sprite arrays hold layers (a pipeline constant): 1 for vanilla,
// which then samples exactly as before.
override SPRITE_ARRAYS: u32 = 1u;
@@ -47,7 +51,11 @@ fn sprite(uv: vec2<f32>, layer: u32) -> vec4<f32> {
case 0u: { return textureSampleGrad(sprites, sprite_sampler, uv, l, dx, dy); }
case 1u: { return textureSampleGrad(sprites_1, sprite_sampler, uv, l, dx, dy); }
case 2u: { return textureSampleGrad(sprites_2, sprite_sampler, uv, l, dx, dy); }
default: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); }
case 3u: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); }
case 4u: { return textureSampleGrad(sprites_4, sprite_sampler, uv, l, dx, dy); }
case 5u: { return textureSampleGrad(sprites_5, sprite_sampler, uv, l, dx, dy); }
case 6u: { return textureSampleGrad(sprites_6, sprite_sampler, uv, l, dx, dy); }
default: { return textureSampleGrad(sprites_7, sprite_sampler, uv, l, dx, dy); }
}
}
+12 -12
View File
@@ -4,24 +4,24 @@ use crate::{chunk::Chunk, streaming::ChunkGenerator};
use horizon_world::registry::BlockRegistry;
pub struct TestTerrain {
stone: u16,
dirt: u16,
grass: u16,
water: u16,
sand: u16,
bedrock: u16,
log: u16,
leaves: u16,
short_grass: u16,
stone: u32,
dirt: u32,
grass: u32,
water: u32,
sand: u32,
bedrock: u32,
log: u32,
leaves: u32,
short_grass: u32,
plains: u16,
}
impl TestTerrain {
pub fn new(registry: &BlockRegistry, biomes: &crate::biome::Biomes) -> crate::Result<Self> {
let id = |name: &str| -> crate::Result<u16> {
let id = |name: &str| -> crate::Result<u32> {
Ok(registry
.default_state(name)
.ok_or_else(|| format!("missing {name}"))?
.0 as u16)
.0)
};
Ok(Self {
stone: id("minecraft:stone")?,
@@ -41,7 +41,7 @@ impl TestTerrain {
]
.into(),
})?
.0 as u16,
.0,
short_grass: id("minecraft:short_grass")?,
plains: biomes.id("minecraft:plains"),
})
+2 -2
View File
@@ -119,7 +119,7 @@ impl VanillaTerrain {
chunk.terrain.min_y().max(MIN_Y),
(chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT),
);
let mut values = Box::new([0u16; 4096]);
let mut values = Box::new([0u32; 4096]);
for si in 0..SECTIONS as i32 {
let base = MIN_Y + si * 16;
if base + 16 <= lo || base >= hi {
@@ -130,7 +130,7 @@ impl VanillaTerrain {
for y in base.max(lo)..(base + 16).min(hi) {
for z in 0..16 {
for x in 0..16 {
let id = chunk.block(bx + x, y, bz + z).0 as u16;
let id = chunk.block(bx + x, y, bz + z).0;
values[((y & 15) << 8 | z << 4 | x) as usize] = id;
any |= id != 0;
}
+8 -9
View File
@@ -21,7 +21,7 @@ pub struct World {
pub dirty_light: crate::fast_hash::FxHashSet<(i32, i32)>,
/// Edits on lit chunks (position, old state, new state), relit
/// incrementally by streaming.
pub light_edits: Vec<((i32, i32, i32), u16, u16)>,
pub light_edits: Vec<((i32, i32, i32), u32, u32)>,
/// Saved entities of chunks that just loaded, spawned by the game.
pub loaded_entities: Vec<((i32, i32), Vec<horizon_world::nbt::Tag>)>,
/// Chunks changed since the last save.
@@ -36,7 +36,7 @@ pub struct World {
/// blocks, which need a block entity).
pub watched: Vec<bool>,
/// Writes of watched states: position, previous and new state.
pub watched_writes: Vec<((i32, i32, i32), u16, u16)>,
pub watched_writes: Vec<((i32, i32, i32), u32, u32)>,
/// `ChunkAccess.inhabitedTime` of resident chunks: ticks a player spent
/// within spawning range (local difficulty).
pub inhabited: HashMap<(i32, i32), i64>,
@@ -198,12 +198,11 @@ impl World {
if y < MIN_Y || y >= MIN_Y + (SECTIONS as i32) * 16 {
return Err("write outside world height".into());
}
let previous = Arc::make_mut(chunk).set(x & 15, y, z & 15, state.0 as u16);
if u32::from(previous) != state.0 {
let watches = |id: u16| self.watched.get(usize::from(id)).copied().unwrap_or(false);
if watches(previous) || watches(state.0 as u16) {
self.watched_writes
.push(((x, y, z), previous, state.0 as u16));
let previous = Arc::make_mut(chunk).set(x & 15, y, z & 15, state.0);
if previous != state.0 {
let watches = |id: u32| self.watched.get(id as usize).copied().unwrap_or(false);
if watches(previous) || watches(state.0) {
self.watched_writes.push(((x, y, z), previous, state.0));
}
self.revision = self.revision.wrapping_add(1);
if let Some(log) = &mut self.block_log {
@@ -225,7 +224,7 @@ impl World {
}
let _ = sy;
if self.chunks.get(&(x >> 4, z >> 4)).is_some_and(|c| c.lit) {
self.light_edits.push(((x, y, z), previous, state.0 as u16));
self.light_edits.push(((x, y, z), previous, state.0));
} else {
for dz in -1..=1 {
for dx in -1..=1 {
+5 -5
View File
@@ -38,7 +38,7 @@ fn resources() -> Option<std::path::PathBuf> {
root.join("items.json").exists().then_some(root)
}
const STONE: u16 = 1;
const STONE: u32 = 1;
fn flat_world() -> World {
let mut world = World::new(BlockStateId(0), 0);
@@ -163,7 +163,7 @@ struct Server {
hub: Hub,
world: World,
next_entity: u32,
placed: Vec<(u32, [i32; 3], u16, i32)>,
placed: Vec<(u32, [i32; 3], u32, i32)>,
}
impl Server {
fn tick(&mut self, entities: &[EntityState]) {
@@ -203,7 +203,7 @@ impl Server {
if let S::UseItemOn(p) = *packet {
let at = [p.pos[0], p.pos[1] + 1, p.pos[2]];
ctx.world
.set(at[0], at[1], at[2], BlockStateId(u32::from(STONE)))
.set(at[0], at[1], at[2], BlockStateId(STONE))
.unwrap();
self.placed.push((conn, at, STONE, p.sequence));
self.hub.ack(conn, p.sequence);
@@ -349,7 +349,7 @@ fn status_and_join() {
chunks += 1;
let sections = l.chunk.sections(24, 16, 7).map_err(|e| e.to_string())?;
// The floor is in the lowest section.
assert_eq!(sections[0].states.get(4 * 256), i32::from(STONE));
assert_eq!(sections[0].states.get(4 * 256), STONE as i32);
assert_eq!(sections[0].non_empty, 256);
assert_eq!(sections[1].non_empty, 0);
assert!(!l.light.sky.is_empty());
@@ -395,7 +395,7 @@ fn status_and_join() {
assert!(chunks >= 9, "only {chunks} chunks");
assert_eq!(
block_update,
Some(([9, MIN_Y + 5, 9], i32::from(STONE))),
Some(([9, MIN_Y + 5, 9], STONE as i32)),
"placed block was not broadcast"
);
Ok(bot.name)
@@ -107,7 +107,7 @@ fn columns_loaded_past_the_render_distance_mesh_when_they_come_in_range() {
/// A one-block cave under the sky: a stone floor at y 62 with a glowstone,
/// and a roof at y 64 with holes, so that every light change in the cave is
/// sampled by a face.
fn roofed(x: i32, z: i32, stone: u16, glowstone: u16) -> Chunk {
fn roofed(x: i32, z: i32, stone: u32, glowstone: u32) -> Chunk {
let mut chunk = Chunk::empty(x, z);
for lz in 0..16 {
for lx in 0..16 {
@@ -143,7 +143,7 @@ fn relit_columns_keep_every_mesh_current() {
biomes: Arc::new(Biomes::load(&root).unwrap()),
assets: assets.clone(),
});
let state = |name: &str| assets.registry.default_state(name).unwrap().0 as u16;
let state = |name: &str| assets.registry.default_state(name).unwrap().0;
let (stone, glowstone, torch) = (
state("minecraft:stone"),
state("minecraft:glowstone"),
@@ -185,9 +185,7 @@ fn relit_columns_keep_every_mesh_current() {
let (bx, bz) = (16 + lx, lz);
world.set(bx, 64, bz, BlockStateId(0)).unwrap();
if i % 3 == 0 {
world
.set(bx, 63, bz, BlockStateId(u32::from(torch)))
.unwrap();
world.set(bx, 63, bz, BlockStateId(torch)).unwrap();
}
}
}
@@ -0,0 +1,40 @@
//! Block states past vanilla's 16-bit global palette (docs/modpacks.md):
//! content mods can take the registry past 65,536 states. Its own test binary,
//! since the palette width is set once for the whole process.
use horizon_client::chunk::{Chunk, Section, block_strategy, set_block_states};
#[test]
fn sections_hold_states_past_sixteen_bits() {
// Vanilla's width until the registry needs more (one test: the width is
// global to the process).
assert_eq!(block_strategy().global_bits, 16);
set_block_states(35_723);
assert_eq!(block_strategy().global_bits, 16);
set_block_states(100_000);
assert_eq!(block_strategy().global_bits, 17);
// A few states past 65,536 stay in the section's own palette.
let mut chunk = Chunk::empty(0, 0);
chunk.set(1, 70, 2, 70_000);
chunk.set(3, 70, 4, 99_999);
assert_eq!(chunk.get(1, 70, 2), 70_000);
assert_eq!(chunk.get(3, 70, 4), 99_999);
assert_eq!(chunk.get(0, 70, 0), 0);
// More than 256 distinct states take the global palette at 17 bits.
let mut values = [0u32; 4096];
for (i, v) in values.iter_mut().enumerate() {
*v = 65_000 + (i as u32 % 1000) * 30;
}
let mut section = Section {
blocks: Section::from_blocks(&values),
..Section::default()
};
let mut out = [0u32; 4096];
section.unpack(&mut out);
assert_eq!(out, values);
section.set(17, 99_998);
assert_eq!(section.get(17), 99_998);
assert_eq!(section.get(18), values[18]);
}
+1 -1
View File
@@ -789,7 +789,7 @@ fn build_with_gameplay<G: GameplayStateSource>(
// Collision shapes.
let collision_tag = nbt::read_named(
&fs::read(root.join("cached-collision.nbt"))?,
Limits::default(),
Limits::tables(),
)
.map_err(|e| invalid(e.to_string()))?
.0
+5 -3
View File
@@ -100,11 +100,13 @@ impl Default for Limits {
impl Limits {
/// The prepared per-state block tables (`cached-collision.nbt`,
/// `block-properties.nbt`, ...): trusted files with a row per block
/// state, which content mods' states grow past the default caps.
/// state, which content mods' states grow past the default caps
/// (vanilla's 35,723 states fill about a tenth of these; ATM10's ported
/// mods, 193,762 states, about half).
pub fn tables() -> Self {
Self {
max_bytes: 1 << 28,
max_nodes: 16_000_000,
max_bytes: 1 << 30,
max_nodes: 128_000_000,
max_depth: 512,
}
}
@@ -192,7 +192,7 @@ impl FeatureCatalog {
if names.is_empty() {
return Ok(serde_json::to_string(&format!("#{tag}"))?);
}
if names.len() > 8192 {
if names.len() > super::state_provider::MAX_TAG_BLOCKS {
return Err(DecodeError::Field("placed-feature tag size"));
}
names
@@ -93,7 +93,7 @@ impl<'r> StateProviderBindings<'r> {
}
let mut bound_tags = BTreeMap::new();
for (name, entries) in tags {
if entries.len() > 8192 {
if entries.len() > MAX_TAG_BLOCKS {
return Err(DecodeError::Field("provider tag size"));
}
let values = entries
@@ -558,6 +558,11 @@ impl BlockStateProvider {
}
}
/// Blocks a bound block tag may list (8,192 before content mods, vanilla's
/// tags fit well within it): content mods' blocks join the shared tags
/// (`mineable/pickaxe` takes every stone block of a pack).
pub(crate) const MAX_TAG_BLOCKS: usize = 1 << 20;
/// Bind the pinned registry's full-axis/full-facing/horizontal-facing property
/// families. A five-way facing or two-axis property is a different property and
/// is intentionally left unchanged. Unrelated properties retain their values.
@@ -41,7 +41,13 @@ fn bound_recursive_providers_and_registry_lists_before_sampling() {
let branching = serde_json::json!({"if_true":{"type":"all_of","predicates":vec![serde_json::json!({"type":"true"});7]},"then":{"id":"stone"}});
let oversized = serde_json::json!({"type":"rule_based","rules":vec![branching;8192]});
assert!(BlockStateProvider::from_json(&oversized.to_string(), &bindings).is_err());
let tags = BTreeMap::from([("horizon:oversized".into(), vec!["stone".into(); 8193])]);
// Content mods' blocks join shared tags: the cap is 2^20 blocks.
let tags = BTreeMap::from([("horizon:large".into(), vec!["stone".into(); 8193])]);
assert!(StateProviderBindings::new(&registry, BTreeSet::new(), &tags).is_ok());
let tags = BTreeMap::from([(
"horizon:oversized".into(),
vec!["stone".into(); (1 << 20) + 1],
)]);
assert!(StateProviderBindings::new(&registry, BTreeSet::new(), &tags).is_err());
let tags = BTreeMap::from([("tag".into(), vec![]), ("minecraft:tag".into(), vec![])]);
assert!(StateProviderBindings::new(&registry, BTreeSet::new(), &tags).is_err());
+13 -5
View File
@@ -237,11 +237,19 @@ How the game loads them, without touching vanilla's path:
- Loaders that read only `data/minecraft/` (recipes, loot, tags) read every namespace, as
vanilla does. With no mods there is only `minecraft`, so nothing changes.
- The overlay is cached like the resource-pack overlay and rebuilt when a mod file changes.
- **Limits.** Chunk sections store a block state in 16 bits: vanilla 26.3 has 35,723 states,
so mods may add 29,813. Past that the content root is refused with the states per mod
(Macaw's Doors, Fences, Paths and Trapdoors alone add 36,154: not all four fit). Block
sprites fill texture arrays of 2048 layers, four at most (8,192; vanilla takes 1,250,
identical textures share a layer). Wider state ids are the next step for big packs.
- **Limits.** Chunk sections size their global palette to the registry: 16 bits for vanilla's
35,723 states (the same chunks, saves and packets as before), one more bit each time content
mods double it, up to 2^20 states (1,048,576). Past that the content root is refused with the
states per mod. ATM10's 42 carried mods take the registry to 193,762 states (18 bits). On the
network the width is the registry's: unchanged for vanilla clients as long as no content mod
is on, wider only between Horizons that share the same mods; a Horizon with content mods still
reads a vanilla server's 16-bit chunks. Block sprites fill texture arrays of 2048 layers, eight
at most (16,384; vanilla takes 1,250, identical textures share a layer; ATM10's 42 take 9,790).
- **Broken mod files load as vanilla loads them.** A blockstate definition that does not bind
(a multipart value the block does not have), a block model that is missing or does not parse,
and a recipe that does not parse are logged and skipped: the block gets no model or the
missing model, the recipe is absent, everything else loads. Vanilla's own files never hit
these paths.
- Not in 0.2: multiplayer registry sync (single player and the integrated server first), worlds
opened without a mod they used (vanilla turns unknown blocks into air; we will ask first),
vanilla ids redefined by a mod (refused).