diff --git a/crates/horizon-client/src/environment.rs b/crates/horizon-client/src/environment.rs index 83c6b95f7..09f00947f 100644 --- a/crates/horizon-client/src/environment.rs +++ b/crates/horizon-client/src/environment.rs @@ -133,13 +133,23 @@ pub struct Sky { pub cloud_color: [f32; 4], /// `gameplay/sky_light_level`, the server-side sky darkening. pub sky_light_level: f32, + /// Environmental fog (`visual/fog_start_distance` / `fog_end_distance`). + pub fog_start: f32, + pub fog_end: f32, } impl Environment { pub fn load(root: &Path) -> Result { - let timeline: Value = - serde_json::from_slice(&fs::read(root.join("data/minecraft/timeline/day.json"))?)?; + Self::load_dimension(root, "overworld", true) + } + /// A dimension's base attributes, optionally driven by the day timeline. + pub fn load_dimension(root: &Path, dimension: &str, day_cycle: bool) -> Result { + let timeline: Value = if day_cycle { + serde_json::from_slice(&fs::read(root.join("data/minecraft/timeline/day.json"))?)? + } else { + serde_json::json!({"period_ticks": 24000, "tracks": {}}) + }; let dimension: Value = serde_json::from_slice(&fs::read( - root.join("data/minecraft/dimension_type/overworld.json"), + root.join(format!("data/minecraft/dimension_type/{dimension}.json")), )?)?; let mut tracks = HashMap::new(); for (name, track) in timeline["tracks"].as_object().ok_or("invalid timeline")? { @@ -180,7 +190,8 @@ impl Environment { .filter_map(|(k, v)| Some((k.to_owned(), parse_value(&Value::String(v.into()))?))) .collect(); base.insert("minecraft:visual/sky_light_factor".into(), [1.0, 0.0, 0.0, 0.0]); - base.insert("minecraft:gameplay/sky_light_level".into(), [1.0, 0.0, 0.0, 0.0]); + // Sky light level in light levels (the timeline multiplies it). + base.insert("minecraft:gameplay/sky_light_level".into(), [15.0, 0.0, 0.0, 0.0]); for (k, v) in dimension["attributes"].as_object().into_iter().flatten() { if let Some(v) = parse_value(v) { base.insert(k.clone(), v); @@ -257,7 +268,9 @@ impl Environment { "minecraft:visual/cloud_color", self.base("minecraft:visual/cloud_color"), ), - sky_light_level: scalar("minecraft:gameplay/sky_light_level"), + sky_light_level: scalar("minecraft:gameplay/sky_light_level") / 15.0, + fog_start: self.base.get("minecraft:visual/fog_start_distance").map_or(0.0, |v| v[0]), + fog_end: self.base.get("minecraft:visual/fog_end_distance").map_or(1024.0, |v| v[0]), } } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f2f24e61e..4a9951cdc 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -62,6 +62,7 @@ struct Options { look: Option<(f32, f32)>, time: Option, weather: Option, + dimension: Option, } impl Options { fn read() -> Result> { @@ -88,6 +89,7 @@ impl Options { look: None, time: None, weather: None, + dimension: None, }; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { @@ -127,6 +129,7 @@ impl Options { "--world" => o.world = Some(value), "--time" => o.time = Some(value.parse()?), "--weather" => o.weather = Some(value), + "--dimension" => o.dimension = Some(if value.contains(':') { value } else { format!("minecraft:{value}") }), "--look" => { let (y, p) = value.split_once(',').ok_or("--look takes YAW,PITCH")?; o.look = Some((y.parse()?, p.parse()?)); @@ -175,6 +178,7 @@ pub struct Resources { mob_types: HashMap>, providers: Providers, chest_model: Option, + nether_environment: Environment, /// clouds.png: (width, height, filled cells). clouds: (u32, u32, Vec), context: Arc, @@ -266,6 +270,35 @@ struct Game<'a> { flash: u32, /// Sleeping in a bed: (bed head position, ticks asleep). sleeping: Option<([i32; 3], u32)>, + dim: Dim, + world_root: PathBuf, + /// A player carried through a portal, re-placed once the destination loads. + travelling: Option<(PlayerMotion, Vec3)>, + portal_ticks: u32, + portal_cooldown: u32, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Dim { + Overworld, + Nether, +} +impl Dim { + fn id(self) -> &'static str { + match self { + Self::Overworld => "minecraft:overworld", + Self::Nether => "minecraft:the_nether", + } + } + fn from_id(id: &str) -> Self { + if id == "minecraft:the_nether" { Self::Nether } else { Self::Overworld } + } + /// Region folder inside the world (vanilla's DIM-1 for the Nether). + fn folder(self) -> Option<&'static str> { + match self { + Self::Overworld => None, + Self::Nether => Some("DIM-1"), + } + } } fn facing_offset(facing: &str) -> [i32; 3] { match facing { @@ -978,6 +1011,19 @@ impl Game<'_> { } /// A column whose surface is a full solid, non-fluid, non-leaf block open to the sky. fn land_column(&self, x: i32, z: i32) -> Option { + if self.dim == Dim::Nether { + // Below the bedrock ceiling: a solid floor with two air blocks above. + for y in (32..120).rev() { + let floor = self.res.facts.get(self.world.block(x, y - 1, z)); + if floor.full_collision && floor.fluid == Fluid::None + && self.res.facts.is_air(self.world.block(x, y, z)) + && self.res.facts.is_air(self.world.block(x, y + 1, z)) + { + return Some(y); + } + } + return None; + } let mut y = MIN_Y + HEIGHT - 1; while y > MIN_Y { let s = self.world.block(x, y, z); @@ -994,6 +1040,15 @@ impl Game<'_> { if self.player.is_some() { return Ok(()); } + if let Some((_, target)) = &self.travelling { + let (cx, cz) = ((target.x.floor() as i32) >> 4, (target.z.floor() as i32) >> 4); + self.streaming.center = (cx, cz); + if (-2..=2).all(|dz| (-2..=2).all(|dx| self.streaming.is_lit(cx + dx, cz + dz))) { + let (player, target) = self.travelling.take().ok_or("travel")?; + self.place_after_travel(player, target)?; + } + return Ok(()); + } let saved = self.level.position.map(|p| Vec3::new(p[0], p[1], p[2])); if self.spawn.is_none() { self.spawn = self.level.spawn.map(|s| Vec3::new(s[0] + 0.5, s[1], s[2] + 0.5)); @@ -1149,6 +1204,7 @@ impl Game<'_> { self.tick_experience(); self.tick_hand(); self.tick_sleep(); + self.tick_portal()?; if let Some(p) = &self.player { // Player.aiStep bob and Entity.walkDist (0.6 per block). let (dx, dz) = (p.position.x - self.previous_position.x, p.position.z - self.previous_position.z); @@ -1999,6 +2055,9 @@ impl Game<'_> { } fn sky_darken(&self) -> i32 { + if self.dim == Dim::Nether { + return 11; + } let mut sky = self.res.environment.sample(self.world.day_time, 0, 0); let (rain, thunder) = self.world.weather.levels(1.0); horizon_client::environment::apply_weather(&mut sky, rain, thunder); @@ -2594,17 +2653,13 @@ impl Game<'_> { saturation: 5.0, ..Level::default() }); - let save = Arc::new(SaveDir::new(save_root.clone())); - let res = &*self.res; - let mut streaming = if self.options.test_terrain { - let inner = Arc::new(TestTerrain::new(res.assets.registry, &res.biomes)?); - Streaming::new(Arc::new(Persistent { inner, save: save.clone(), registry: res.assets.clone(), biomes: res.biomes.clone() }), res.context.clone(), res.threads) - } else { - let inner = Arc::new(VanillaTerrain::new(&self.options.resources.join("worldgen"), res.assets.registry, &res.biomes, level.seed).map_err(|e| { - format!("{e}\nPrepare the worldgen bundle with: python3 tools/prepare_native_client.py") - })?); - Streaming::new(Arc::new(Persistent { inner, save: save.clone(), registry: res.assets.clone(), biomes: res.biomes.clone() }), res.context.clone(), res.threads) - }; + let mut level = level; + if let Some(d) = &self.options.dimension { + level.dimension = d.clone(); + level.position = None; + } + let dim = Dim::from_id(&level.dimension); + let (mut streaming, save) = self.make_streaming(&save_root, dim, level.seed)?; streaming.radius = self.settings.render_distance; let mut world = World::new(self.res.facts.air, level.seed); world.game_time = level.time; @@ -2647,6 +2702,9 @@ impl Game<'_> { self.inventory = inventory; self.save = save; self.level = level; + self.dim = dim; + self.world_root = save_root.clone(); + self.travelling = None; self.player = None; self.spawn = None; self.spawn_search = 0; @@ -2666,12 +2724,248 @@ impl Game<'_> { r.clear_sections(); } self.apply_settings(); + self.streaming.sky_light = dim == Dim::Overworld; + self.streaming.nether_shading = dim == Dim::Nether; // New worlds get their level.dat right away. if !save_root.join("level.dat").exists() { self.level.write(&save_root)?; } Ok(()) } + /// Streaming and region storage for one dimension of a world. + fn make_streaming(&self, world_root: &std::path::Path, dim: Dim, seed: i64) -> Result<(Streaming, Arc)> { + let region_root = match dim.folder() { + Some(f) => world_root.join(f), + None => world_root.to_owned(), + }; + let save = Arc::new(SaveDir::new(region_root)); + let res = &*self.res; + let mut streaming = if self.options.test_terrain && dim == Dim::Overworld { + let inner = Arc::new(TestTerrain::new(res.assets.registry, &res.biomes)?); + Streaming::new(Arc::new(Persistent { inner, save: save.clone(), registry: res.assets.clone(), biomes: res.biomes.clone() }), res.context.clone(), res.threads) + } else { + let inner = Arc::new(VanillaTerrain::for_dimension(&self.options.resources.join("worldgen"), res.assets.registry, &res.biomes, seed, dim.id()).map_err(|e| { + format!("{e}\nPrepare the worldgen bundle with: python3 tools/prepare_native_client.py") + })?); + Streaming::new(Arc::new(Persistent { inner, save: save.clone(), registry: res.assets.clone(), biomes: res.biomes.clone() }), res.context.clone(), res.threads) + }; + streaming.radius = self.settings.render_distance; + streaming.sky_light = dim == Dim::Overworld; + streaming.nether_shading = dim == Dim::Nether; + Ok((streaming, save)) + } + /// Change dimension through a portal (Entity.changeDimension with + /// PortalForcer placement once the destination has loaded). + fn enter_dimension(&mut self, target: Dim) -> Result<()> { + let Some(player) = self.player.take() else { return Ok(()) }; + // Save the departing dimension's changed chunks. + let keys: Vec<_> = self.world.modified.drain().collect(); + let chunks: Vec<_> = keys.iter().filter_map(|k| self.world.chunks.get(k)).map(|c| self.with_entities(c)).collect(); + self.save.save(&chunks, &self.res.assets.registry, &self.res.biomes)?; + // Coordinates scale by 8 between the Overworld and the Nether. + let scale = match (self.dim, target) { + (Dim::Overworld, Dim::Nether) => 1.0 / 8.0, + (Dim::Nether, Dim::Overworld) => 8.0, + _ => 1.0, + }; + let destination = Vec3::new(player.position.x * scale, player.position.y, player.position.z * scale); + let (streaming, save) = self.make_streaming(&self.world_root.clone(), target, self.level.seed)?; + let mut world = World::new(self.res.facts.air, self.level.seed); + world.game_time = self.world.game_time; + world.day_time = self.world.day_time; + world.weather = self.world.weather; + self.streaming = streaming; + self.world = world; + self.save = save; + self.dim = target; + self.level.dimension = target.id().to_owned(); + self.mobs.clear(); + self.arrows.clear(); + self.dropped.clear(); + self.particles.clear(); + self.orbs.clear(); + self.mining = None; + self.travelling = Some((player, destination)); + self.portal_cooldown = 300; + if let Some(r) = self.renderer.as_mut() { + r.clear_sections(); + } + self.apply_settings(); + Ok(()) + } + /// PortalShape: an obsidian frame around 2..=21 by 3..=21 empty blocks. + fn portal_shape(&self, start: [i32; 3], x_axis: bool) -> Option<([i32; 3], i32, i32)> { + let name = |p: [i32; 3]| self.res.facts.get(self.world.block(p[0], p[1], p[2])).name.clone(); + let empty = |p: [i32; 3]| { + let n = name(p); + matches!(&*n, "minecraft:air" | "minecraft:cave_air" | "minecraft:fire" | "minecraft:soul_fire" | "minecraft:nether_portal") + }; + let frame = |p: [i32; 3]| &*name(p) == "minecraft:obsidian"; + let d = if x_axis { [1, 0, 0] } else { [0, 0, 1] }; + let mut bottom = start; + let mut n = 0; + while empty([bottom[0], bottom[1] - 1, bottom[2]]) && n < 21 { + bottom[1] -= 1; + n += 1; + } + if !frame([bottom[0], bottom[1] - 1, bottom[2]]) { + return None; + } + let mut left = bottom; + n = 0; + while empty([left[0] - d[0], left[1], left[2] - d[2]]) && n < 21 { + left = [left[0] - d[0], left[1], left[2] - d[2]]; + n += 1; + } + if !frame([left[0] - d[0], left[1], left[2] - d[2]]) { + return None; + } + let mut width = 0; + while width <= 21 { + let p = [left[0] + d[0] * width, left[1], left[2] + d[2] * width]; + if !empty(p) || !frame([p[0], p[1] - 1, p[2]]) { + break; + } + width += 1; + } + if !(2..=21).contains(&width) || !frame([left[0] + d[0] * width, left[1], left[2] + d[2] * width]) { + return None; + } + let mut height = 0; + 'rows: while height <= 21 { + let y = left[1] + height; + if !frame([left[0] - d[0], y, left[2] - d[2]]) || !frame([left[0] + d[0] * width, y, left[2] + d[2] * width]) { + break; + } + for i in 0..width { + if !empty([left[0] + d[0] * i, y, left[2] + d[2] * i]) { + break 'rows; + } + } + height += 1; + } + let top_ok = (0..width).all(|i| frame([left[0] + d[0] * i, left[1] + height, left[2] + d[2] * i])); + ((3..=21).contains(&height) && top_ok).then_some((left, width, height)) + } + /// BaseFireBlock.onPlace: lighting fire inside a frame creates a portal. + fn try_light_portal(&mut self, at: [i32; 3]) -> Result { + for x_axis in [true, false] { + if let Some((origin, width, height)) = self.portal_shape(at, x_axis) { + let d = if x_axis { [1, 0, 0] } else { [0, 0, 1] }; + let registry = &self.res.assets.registry; + let Some(default) = registry.default_state("minecraft:nether_portal") else { return Ok(false) }; + let mut portal = registry.state(default).cloned().ok_or("portal state")?; + portal.properties.insert("axis".into(), if x_axis { "x" } else { "z" }.into()); + let id = registry.resolve(&portal).unwrap_or(default); + for h in 0..height { + for w in 0..width { + self.world.set(origin[0] + d[0] * w, origin[1] + h, origin[2] + d[2] * w, id)?; + } + } + return Ok(true); + } + } + Ok(false) + } + /// PortalForcer: reuse a nearby portal, else build a 2x3 obsidian portal. + fn place_after_travel(&mut self, mut player: PlayerMotion, target: Vec3) -> Result<()> { + let (tx, tz) = (target.x.floor() as i32, target.z.floor() as i32); + let radius = if self.dim == Dim::Nether { 16 } else { 32 }; + let (min_y, max_y) = if self.dim == Dim::Nether { (1, 126) } else { (MIN_Y + 1, MIN_Y + HEIGHT - 3) }; + let portal_name = "minecraft:nether_portal"; + let mut best: Option<(i64, [i32; 3])> = None; + for z in tz - radius..=tz + radius { + for x in tx - radius..=tx + radius { + if !self.world.loaded(x, z) { + continue; + } + for y in min_y..max_y { + if &*self.res.facts.get(self.world.block(x, y, z)).name == portal_name + && &*self.res.facts.get(self.world.block(x, y - 1, z)).name != portal_name + { + let d = i64::from(x - tx).pow(2) + i64::from(z - tz).pow(2) + i64::from(y - target.y as i32).pow(2); + if best.is_none_or(|b| d < b.0) { + best = Some((d, [x, y, z])); + } + } + } + } + } + let spot = match best { + Some((_, p)) => p, + None => { + // Build: obsidian frame 4 wide, 5 tall, with a floor, carved into the terrain. + let y = (target.y.floor() as i32).clamp(min_y + 1, max_y - 5); + let set = |g: &mut Self, p: [i32; 3], name: &str| -> Result<()> { + if let Some(id) = g.res.assets.registry.default_state(name) { + g.world.set(p[0], p[1], p[2], id)?; + } + Ok(()) + }; + for dz in -1..=1 { + for dx in -1..=2 { + set(self, [tx + dx, y - 1, tz + dz], "minecraft:obsidian")?; + for dy in 0..4 { + set(self, [tx + dx, y + dy, tz + dz], "minecraft:air")?; + } + } + } + for w in -1..=2 { + for h in -1..=3 { + if w == -1 || w == 2 || h == -1 || h == 3 { + set(self, [tx + w, y + h, tz], "minecraft:obsidian")?; + } + } + } + self.try_light_portal([tx, y, tz])?; + [tx, y, tz] + } + }; + player.position = Vec3::new(f64::from(spot[0]) + 0.5, f64::from(spot[1]), f64::from(spot[2]) + 0.5); + player.velocity = Vec3::ZERO; + self.previous_position = player.position; + self.player = Some(player); + self.apply_equipment(); + let at = [f64::from(spot[0]), f64::from(spot[1]), f64::from(spot[2])]; + self.audio.play("minecraft:block.portal.travel", Some(at), 0.25, 1.0); + Ok(()) + } + fn tick_portal(&mut self) -> Result<()> { + let Some(p) = self.player.as_ref() else { return Ok(()) }; + let w = f64::from(p.dimensions.width) / 2.0; + let (lo, hi) = ([p.position.x - w, p.position.y, p.position.z - w], [p.position.x + w, p.position.y + f64::from(p.dimensions.height), p.position.z + w]); + let mut inside = false; + for x in lo[0].floor() as i32..=hi[0].floor() as i32 { + for y in lo[1].floor() as i32..=hi[1].floor() as i32 { + for z in lo[2].floor() as i32..=hi[2].floor() as i32 { + if &*self.res.facts.get(self.world.block(x, y, z)).name == "minecraft:nether_portal" { + inside = true; + } + } + } + } + if !inside { + self.portal_ticks = 0; + self.portal_cooldown = self.portal_cooldown.saturating_sub(1); + return Ok(()); + } + if self.portal_cooldown > 0 { + return Ok(()); + } + if self.portal_ticks == 0 { + let at = self.eye_position(); + self.audio.play("minecraft:block.portal.trigger", at, 0.25, self.world.random.next_float() * 0.4 + 0.8); + } + self.portal_ticks += 1; + // Player.getPortalWaitTime: 80 ticks, or 1 when invulnerable (creative). + let wait = if self.creative { 1 } else { 80 }; + if self.portal_ticks >= wait { + self.portal_ticks = 0; + let target = if self.dim == Dim::Overworld { Dim::Nether } else { Dim::Overworld }; + self.enter_dimension(target)?; + } + Ok(()) + } fn close_world(&mut self) -> Result<()> { self.save_game()?; self.streaming.paused = true; @@ -2907,7 +3201,9 @@ impl Game<'_> { if item == "flint_and_steel" || item == "fire_charge" { let target = [hit.pos[0] + hit.face[0], hit.pos[1] + hit.face[1], hit.pos[2] + hit.face[2]]; if self.res.facts.is_air(self.world.block(target[0], target[1], target[2])) { - self.set_state_named(target, "minecraft:fire", &[])?; + if !self.try_light_portal(target)? { + self.set_state_named(target, "minecraft:fire", &[])?; + } let sound = if item == "fire_charge" { "minecraft:item.firecharge.use" } else { "minecraft:item.flintandsteel.use" }; self.audio.play(sound, Some(at(target)), 1.0, self.world.random.next_float() * 0.4 + 0.8); if item == "fire_charge" { @@ -3437,7 +3733,8 @@ impl Game<'_> { level.inventory.push((103 - i as u8, s.item.to_string(), s.count, s.damage)); } } - level.write(&self.save.root)?; + level.dimension = self.dim.id().to_owned(); + level.write(&self.world_root)?; self.message = Some(("Saved the game".into(), Instant::now())); Ok(()) } @@ -3470,8 +3767,9 @@ impl Game<'_> { .map(|c| c.biome(block[0], block[1], block[2])) .unwrap_or(0); let info = self.res.biomes.get(biome).clone(); - let mut sky = self.res.environment.sample(self.world.day_time, info.sky, info.fog); - let (rain, thunder) = self.world.weather.levels(alpha as f32); + let environment = if self.dim == Dim::Nether { &self.res.nether_environment } else { &self.res.environment }; + let mut sky = environment.sample(self.world.day_time, info.sky, info.fog); + let (rain, thunder) = if self.dim == Dim::Overworld { self.world.weather.levels(alpha as f32) } else { (0.0, 0.0) }; horizon_client::environment::apply_weather(&mut sky, rain, thunder); if self.flash > 0 { // Lightning flash brightens the sky (LevelRenderer skyFlashTime). @@ -3489,10 +3787,16 @@ impl Game<'_> { let hud = self.hud(size); let title = self.title.is_some(); let entities = if title { Vec::new() } else { self.entity_geometry(camera, alpha) }; - let celestial = if title { self.panorama_geometry() } else { self.celestial_geometry(&sky) }; + let celestial = if title { + self.panorama_geometry() + } else if self.dim == Dim::Overworld { + self.celestial_geometry(&sky) + } else { + Vec::new() + }; let hand = if title || self.screen == Screen::Inventory { Vec::new() } else { self.hand_geometry(alpha) }; let mut entities = entities; - if !title { + if !title && self.dim == Dim::Overworld { self.cloud_geometry(&sky, camera, alpha, &mut entities); self.weather_geometry(camera, alpha, &mut entities); } @@ -3515,6 +3819,8 @@ impl Game<'_> { distance: (self.streaming.radius * 16) as f32, underwater, title, + skybox: self.dim == Dim::Overworld, + environment_fog: (self.dim == Dim::Nether).then_some((sky.fog_start, sky.fog_end)), // The camera inside an opaque block sees nothing through it: disable. cave_culling: !self.res.facts.get(self.world.block(block[0], block[1], block[2])).solid_render, water_fog, @@ -3954,6 +4260,7 @@ pub fn run() -> Result<()> { layer_names, providers: Providers::load(&root), chest_model: horizon_client::mobs::load_model(&root, "minecraft:chest#main"), + nether_environment: Environment::load_dimension(&root, "the_nether", false)?, clouds: horizon_client::hud::decode(&root.join("assets/minecraft/textures/environment/clouds.png")) .map(|(w, h, px)| (w, h, px.chunks_exact(4).map(|p| p[3] > 0).collect())) .unwrap_or((1, 1, vec![false])), @@ -4047,6 +4354,11 @@ pub fn run() -> Result<()> { bob: (0.0, 0.0), flash: 0, sleeping: None, + dim: Dim::Overworld, + world_root: PathBuf::new(), + travelling: None, + portal_ticks: 0, + portal_cooldown: 0, }; match game.options.world.clone() { Some(folder) => { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index a960dfc4f..df948d44c 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -47,6 +47,10 @@ pub struct Frame<'a> { pub cave_culling: bool, /// Title screen: only the panorama (in `celestial`) and the HUD. pub title: bool, + /// Dimensions without a skybox (the Nether) clear to the fog colour. + pub skybox: bool, + /// Environmental fog start/end (Nether 10..96), if any. + pub environment_fog: Option<(f32, f32)>, pub water_fog: [f32; 3], pub outline: Option<[f64; 6]>, pub hud: &'a HudBatch, @@ -957,7 +961,10 @@ impl Renderer { ( [frame.sky.fog_color[0], frame.sky.fog_color[1], frame.sky.fog_color[2], 1.0], // FogRenderer: render-distance fog starts at max(d - 16, 0)... d. - [f32::MAX, f32::MAX, (d - d.min(64.0) * 0.25).max(0.0), d], + { + let (start, end) = frame.environment_fog.unwrap_or((f32::MAX, f32::MAX)); + [start, end, (d - d.min(64.0) * 0.25).max(0.0), d] + }, ) }; let globals = Globals { @@ -1144,7 +1151,7 @@ impl Renderer { ..Default::default() }); pass.set_bind_group(0, &self.group, &[]); - if !frame.underwater { + if !frame.underwater && frame.skybox { if !frame.title { pass.set_pipeline(&self.sky); pass.draw(0..3, 0..1); diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index 61d808ad3..5f479e3f1 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -35,7 +35,8 @@ impl LightTables { } } /// `region[dz][dx]` holds the chunks around the centre (index 1,1). -pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables) -> Vec> { +/// `sky` is false in dimensions without skylight (the Nether). +pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables, sky_light: bool) -> Vec> { let center = region[1][1].expect("centre chunk"); // Highest occupied section in the region bounds the work. let mut top_section = 0; @@ -80,7 +81,7 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables) -> V let mut lowest = vec![h as i32; W * W]; for z in 0..W { for x in 0..W { - if !present(x, z) { + if !present(x, z) || !sky_light { continue; } let mut y = h as i32 - 1; @@ -94,7 +95,7 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables) -> V } for z in 0..W { for x in 0..W { - if !present(x, z) { + if !present(x, z) || !sky_light { continue; } let own = lowest[z * W + x]; @@ -156,15 +157,16 @@ pub fn light_chunk(region: &[[Option<&Chunk>; 3]; 3], tables: &LightTables) -> V let mut sections = Vec::with_capacity(SECTIONS); for (si, section) in center.sections.iter().enumerate() { let mut out = (**section).clone(); + let open = if sky_light { 0xF0 } else { 0x00 }; if si * 16 >= h { - out.light = Light::Uniform(0xF0); + out.light = Light::Uniform(open); } else { let mut data = Box::new([0u8; 4096]); for ly in 0..16 { let y = si * 16 + ly; if y >= h { for i in 0..256 { - data[ly << 8 | i] = 0xF0; + data[ly << 8 | i] = open; } continue; } diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index d149483e1..28750057a 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -307,6 +307,8 @@ fn pack_light(sky: f32, block: f32) -> u32 { (sky * 16.0).clamp(0.0, 255.0) as u32 | ((block * 16.0).clamp(0.0, 255.0) as u32) << 8 } pub struct Mesher<'a> { + /// CardinalLighting.NETHER: brighter up/down faces. + pub nether_shading: bool, pub assets: &'a Assets, pub tables: &'a MeshTables, pub biomes: &'a Biomes, @@ -437,10 +439,13 @@ impl Mesher<'_> { [x, y, z] }; let tint = Self::tint(state, q.tint_index, colors, x, z); - let directional = match q.shade_direction_override { + let mut directional = match q.shade_direction_override { Some(d) => shade(d), None => shade(facing), }; + if self.nether_shading && matches!(q.shade_direction_override.unwrap_or(facing), horizon_assets::model::Direction::Up | horizon_assets::model::Direction::Down) { + directional = 0.9; + } let info = &self.tables.info; let light_at = |p: [i32; 3]| -> u32 { let i = pi(p[0], p[1], p[2]); diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 67e15bd85..6997be4f8 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -366,6 +366,8 @@ pub struct Level { pub xp_progress: f32, pub xp_total: i32, pub weather: crate::world::Weather, + /// Player.Dimension (minecraft:overworld / minecraft:the_nether). + pub dimension: String, } impl Level { pub fn read(dir: &Path) -> Option { @@ -421,6 +423,10 @@ impl Level { health: player.map_or(20.0, |p| float(p.get("Health"))), food: player.map_or(20, |p| long(p.get("foodLevel")) as i32), saturation: player.map_or(5.0, |p| float(p.get("foodSaturationLevel"))), + dimension: match player.and_then(|p| p.get("Dimension")) { + Some(Tag::String(s)) => s.to_string_lossy(), + _ => "minecraft:overworld".into(), + }, weather: crate::world::Weather { clear_time: long(data.get("clearWeatherTime")) as i32, rain_time: long(data.get("rainTime")) as i32, @@ -470,7 +476,7 @@ impl Level { .collect(), ), ), - ("Dimension", string("minecraft:overworld")), + ("Dimension", string(if self.dimension.is_empty() { "minecraft:overworld" } else { &self.dimension })), ]; if let Some(p) = self.position { player.push(("Pos", Tag::List(p.iter().map(|v| Tag::Double(v.to_bits())).collect()))); diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 74bced489..60e404f76 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -44,8 +44,8 @@ enum Job { Generate(i32, i32), Decorate(i32, i32, Box<[Proto; 9]>), Finish(Proto), - Light(i32, i32, Box, u64), - Mesh(i32, i32, i32, Box, u64), + Light(i32, i32, Box, u64, bool), + Mesh(i32, i32, i32, Box, u64, bool), } pub enum Done { Generated(Chunk), @@ -106,6 +106,9 @@ pub struct Streaming { pub radius: i32, /// No work is scheduled while paused (title screen). pub paused: bool, + /// Dimension flags: sky light, Nether face shading. + pub sky_light: bool, + pub nether_shading: bool, pub generated_total: u64, pub meshed_total: u64, } @@ -134,6 +137,7 @@ impl Streaming { .spawn(move || { let mut worker = generator.worker(); let mut mesher = Mesher { + nether_shading: false, assets: &context.assets, tables: &context.tables, biomes: &context.biomes, @@ -162,10 +166,11 @@ impl Streaming { Done::Decorated(x, z, region) } Job::Finish(proto) => Done::Generated(generator.finish(proto)), - Job::Light(x, z, region, seq) => { - Done::Lit(x, z, light_chunk(&borrow(®ion), &context.light), seq) + Job::Light(x, z, region, seq, sky) => { + Done::Lit(x, z, light_chunk(&borrow(®ion), &context.light, sky), seq) } - Job::Mesh(x, sy, z, region, seq) => { + Job::Mesh(x, sy, z, region, seq, nether) => { + mesher.nether_shading = nether; Done::Meshed(x, sy, z, mesher.mesh(&borrow(®ion), sy), seq) } }; @@ -196,6 +201,8 @@ impl Streaming { center: (0, 0), radius: 8, paused: false, + sky_light: true, + nether_shading: false, generated_total: 0, meshed_total: 0, } @@ -419,7 +426,7 @@ impl Streaming { world.dirty_light.remove(&(x, z)); self.sequence += 1; self.lighting.insert((x, z), self.sequence); - self.submit(self.distance(x, z) * 4 + 1, Job::Light(x, z, region, self.sequence)); + self.submit(self.distance(x, z) * 4 + 1, Job::Light(x, z, region, self.sequence, self.sky_light)); } // Meshing once the column and its neighbours are lit. let dirty: Vec<_> = world.dirty_sections.iter().copied().collect(); @@ -443,7 +450,7 @@ impl Streaming { self.meshing.insert((x, sy, z), self.sequence); self.submit( self.distance(x, z) * 4 + if first { 2 } else { 0 }, - Job::Mesh(x, sy, z, region, self.sequence), + Job::Mesh(x, sy, z, region, self.sequence, self.nether_shading), ); } (uploads, removed) diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index 4f2d2e47e..6c49822ea 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -24,9 +24,12 @@ enum VanillaProto { } impl VanillaTerrain { pub fn new(dir: &Path, registry: &'static BlockRegistry, biomes: &Biomes, seed: i64) -> crate::Result { + Self::for_dimension(dir, registry, biomes, seed, "minecraft:overworld") + } + pub fn for_dimension(dir: &Path, registry: &'static BlockRegistry, biomes: &Biomes, seed: i64, dimension: &str) -> crate::Result { let data: Arc = GeneratorData::load(dir, registry)?; let biome_map = data.biome_names().iter().map(|name| biomes.id(name)).collect(); - let generator = OverworldGenerator::new(data, seed)?; + let generator = OverworldGenerator::for_dimension(data, seed, "minecraft:normal", dimension)?; let unsupported = generator.unsupported_features(); if !unsupported.is_empty() { eprintln!( @@ -97,7 +100,9 @@ impl ChunkGenerator for VanillaTerrain { let pos = chunk.position(); let mut out = Chunk::empty(pos.x, pos.z); let (bx, bz) = (pos.x * 16, pos.z * 16); - for y in MIN_Y..MIN_Y + HEIGHT { + // Only the generated range (the Nether is 0..256) is copied. + let (lo, hi) = (chunk.terrain.min_y().max(MIN_Y), (chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT)); + for y in lo..hi { for z in 0..16 { for x in 0..16 { let id = chunk.block(bx + x, y, bz + z).0 as u16; @@ -111,7 +116,8 @@ impl ChunkGenerator for VanillaTerrain { for qy in 0..HEIGHT / 4 { for qz in 0..4 { for qx in 0..4 { - let id = chunk.biome_id_at(bx + qx * 4, MIN_Y + qy * 4, bz + qz * 4); + let y = (MIN_Y + qy * 4).clamp(lo, hi - 1); + let id = chunk.biome_id_at(bx + qx * 4, y, bz + qz * 4); let index = id.0 as usize; biomes[(qy * 16 + qz * 4 + qx) as usize] = self.biome_map.get(index).copied().unwrap_or(0); }