Skip to content

Commit

Permalink
fix(world): cache the end biome id
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspeedfpv committed Nov 28, 2024
1 parent 5db47e3 commit d50cf02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ async fn main() -> Result<()> {
let world = read_world(&args.map_dir)?;
info!("Done.");

let state = Arc::new(state::State::new(VERSION, VERSION_NUM, args));

info!("Generating world chunk packets");
let world_cache = WorldCache::from(world);
let world_cache = WorldCache::from_anvil(state.clone(), world);
info!("Done.");

let state = Arc::new(state::State::new(VERSION, VERSION_NUM, args));

#[cfg(feature = "lan")]
net::spawn_lan_broadcast(state.clone()).await?;

Expand Down
18 changes: 11 additions & 7 deletions src/net/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* <https://www.gnu.org/licenses/>.
*/

use std::cmp::Ordering;
use std::{cmp::Ordering, sync::Arc};

use rayon::prelude::*;

Expand All @@ -31,15 +31,16 @@ use crate::{
Encoder,
},
world::{blocks::Blocks, World},
CrawlState,
};

#[derive(Debug)]
pub struct WorldCache {
pub encoded: Vec<Vec<u8>>,
}

impl From<World> for WorldCache {
fn from(world: World) -> Self {
impl WorldCache {
pub fn from_anvil(crawlstate: CrawlState, world: World) -> Self {
let mut chunks = world.0.iter().collect::<Vec<_>>();

chunks.sort_by(|((ax, az), _), ((bx, bz), _)| {
Expand All @@ -54,7 +55,7 @@ impl From<World> for WorldCache {

let chunks = chunks
.par_iter()
.map(|(_, c)| ChunkDataUpdateLightC::new(c, &block_states))
.map(|(_, c)| ChunkDataUpdateLightC::new(crawlstate.clone(), c, &block_states))
.collect::<Vec<ChunkDataUpdateLightC<'_>>>();

let encoded = chunks
Expand All @@ -76,13 +77,15 @@ impl From<World> for WorldCache {
pub struct RegistryCache {
pub encoded: Vec<u8>,
pub the_end_id: VarInt,
pub the_end_biome_id: u16,
}

impl From<&AllRegistries> for RegistryCache {
fn from(registry: &AllRegistries) -> Self {
let mut encoder = Encoder::new();

let dimensions = Registry::from(registry.dimension_type.clone());
let biomes = Registry::from(registry.biome.clone());
encoder
.append_packet(&Registry::from(registry.trim_material.clone()))
.expect("Failed to encode trim material");
Expand All @@ -92,9 +95,6 @@ impl From<&AllRegistries> for RegistryCache {
encoder
.append_packet(&Registry::from(registry.banner_pattern.clone()))
.expect("Failed to encode banner pattern");
encoder
.append_packet(&Registry::from(registry.biome.clone()))
.expect("Failed to encode biome");
encoder
.append_packet(&Registry::from(registry.chat_type.clone()))
.expect("Failed to encode chat type");
Expand All @@ -104,6 +104,9 @@ impl From<&AllRegistries> for RegistryCache {
encoder
.append_packet(&dimensions)
.expect("Failed to encode dimensions");
encoder
.append_packet(&biomes)
.expect("Failed to encode biomes");
encoder
.append_packet(&Registry::from(registry.wolf_variant.clone()))
.expect("Failed to encode wolf variants");
Expand All @@ -114,6 +117,7 @@ impl From<&AllRegistries> for RegistryCache {
Self {
encoded: encoder.take().to_vec(),
the_end_id: VarInt(dimensions.index_of("minecraft:the_end")),
the_end_biome_id: biomes.index_of("minecraft:the_end") as u16,
}
}
}
17 changes: 12 additions & 5 deletions src/protocol/packets/play/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* <https://www.gnu.org/licenses/>.
*/

use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};

use bit_vec::BitVec;
use bytes::BufMut;
Expand All @@ -32,6 +32,7 @@ use crate::{
self,
blocks::{BlockState, Blocks},
},
CrawlState,
};

#[derive(Debug)]
Expand Down Expand Up @@ -258,7 +259,11 @@ impl Encode for ChunkDataUpdateLightC<'_> {
}

impl ChunkSection {
pub fn anvil_to_sec(value: &world::Section, block_states: &Blocks) -> Self {
pub fn anvil_to_sec(
crawlstate: CrawlState,
value: &world::Section,
block_states: &Blocks,
) -> Self {
let mut blocks = Vec::new();
let bit_length = (64 - (value.block_states.palette.len() as u64).leading_zeros()).max(4);

Expand Down Expand Up @@ -379,19 +384,21 @@ impl ChunkSection {
},
biomes: PalettedContainer {
bits_per_entry: 0,
palette: Palette::SingleValued(BlockState(0)),
palette: Palette::SingleValued(BlockState(
crawlstate.registry_cache.the_end_biome_id,
)),
data_array: fastnbt::LongArray::new(vec![]),
},
}
}
}

impl ChunkDataUpdateLightC<'_> {
pub fn new(value: &world::Chunk, block_states: &Blocks) -> Self {
pub fn new(crawlstate: CrawlState, value: &world::Chunk, block_states: &Blocks) -> Self {
let data = value
.sections
.iter()
.map(|sec| ChunkSection::anvil_to_sec(sec, block_states))
.map(|sec| ChunkSection::anvil_to_sec(crawlstate.clone(), sec, block_states))
.collect::<Vec<_>>();

let block_entities = value
Expand Down

0 comments on commit d50cf02

Please sign in to comment.