From 512962e3ca2d4c9d37db3232cdf6a3d28927981c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 11:09:58 +0200 Subject: [PATCH] Carry: Repurposed Structures' and Moog's jigsaws keep their layout rules Their jigsaw structures, the Nether variants too, get a horizon:piece_limited key for the game's piece-limited assembler: y_allowance, burying_type, cannot_spawn_in_liquid, the pools whose fallbacks ignore the bounds, the Nether variants' land search and ledge, and each structure's piece counts from the mod's rs_pieces_spawn_counts or msl_pieces_spawn_counts files. use_bounding_box_hack becomes vanilla's use_expansion_hack. Lithostitched's jigsaw stays vanilla's. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-port-pack/src/carry.rs | 3 + crates/horizon-port-pack/src/worldgen.rs | 238 +++++++++++++++++++++-- 2 files changed, 225 insertions(+), 16 deletions(-) diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index 6a31834a3..9ef4ee2fb 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -363,6 +363,7 @@ pub fn carry( let mut block_loot = BTreeSet::new(); let mut biome_conventions = BTreeSet::new(); let set_tags = crate::worldgen::structure_set_tags(&mut zip); + let piece_counts = crate::worldgen::piece_counts(&mut zip); for i in 0..zip.len() { let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; let name = entry.name().to_owned(); @@ -432,6 +433,8 @@ pub fn carry( let path = rel.trim_start_matches("worldgen/structure/"); let structure = format!("{id}:{}", path.trim_end_matches(".json")); changed |= crate::worldgen::disables(&structure, &mut v); + changed |= + crate::worldgen::add_piece_counts(&structure, &mut v, &piece_counts); } if changed { bytes = serde_json::to_vec_pretty(&v).unwrap_or(bytes); diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index 1ad836ad1..269751ef1 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -26,6 +26,19 @@ const JIGSAWS: [&str; 3] = [ "moogs_structures:moogs_structures_generic_jigsaw_structure", "repurposed_structures:generic_jigsaw_structure", ]; +/// The Nether variants of the piece-limited jigsaws: laid out the same, +/// then moved onto land. +const NETHER_JIGSAWS: [&str; 2] = [ + "moogs_structures:moogs_structures_generic_nether_jigsaw_structure", + "repurposed_structures:generic_nether_jigsaw_structure", +]; +/// Layout rules the piece-limited assembler keeps (`horizon:piece_limited`). +const PIECE_LIMITED_RULES: [&str; 4] = [ + "y_allowance", + "burying_type", + "use_bounding_box_hack", + "pools_that_ignore_boundaries", +]; /// Placements that are vanilla's random spread with extra rules. const SPREADS: [&str; 2] = [ "moogs_structures:advanced_random_spread", @@ -213,12 +226,22 @@ fn upgrade_types( yung_structure(v, lost); true } - Some(t) if JIGSAWS.contains(&t) => { - let liquid = match t.split_once(':').map(|(ns, _)| ns) { - Some("moogs_structures") => "top", - _ => "lowest", + Some(t) if JIGSAWS.contains(&t) || NETHER_JIGSAWS.contains(&t) => { + let library = match t.split_once(':').map(|(ns, _)| ns) { + Some("moogs_structures") => Some("moogs"), + Some("repurposed_structures") => Some("repurposed"), + _ => None, }; - start_checks(v, liquid, lost); + let liquid = if library == Some("moogs") { + "top" + } else { + "lowest" + }; + let limited = library.map(|l| piece_limited(v, l)); + start_checks(v, liquid, lost, limited.is_some()); + if let Some(limited) = limited { + v["horizon:piece_limited"] = limited; + } v["type"] = "minecraft:jigsaw".into(); true } @@ -399,7 +422,7 @@ fn yung_condition(c: &Value, lost: &mut BTreeMap) -> Value { /// Moves a Repurposed Structures or Moog's jigsaw's start rules under /// `horizon:start_checks`, and counts the layout rules it loses. `liquid` is /// the block its no-start-in-liquid rule reads. -fn start_checks(v: &mut Value, liquid: &str, lost: &mut BTreeMap) { +fn start_checks(v: &mut Value, liquid: &str, lost: &mut BTreeMap, limited: bool) { let Some(o) = v.as_object_mut() else { return; }; @@ -420,7 +443,8 @@ fn start_checks(v: &mut Value, liquid: &str, lost: &mut BTreeMap) } } for rule in LAYOUT_RULES { - if o.remove(rule).is_some_and(|r| r != Value::Bool(false)) { + let kept = limited && PIECE_LIMITED_RULES.contains(&rule); + if o.remove(rule).is_some_and(|r| r != Value::Bool(false)) && !kept { *lost.entry(rule.into()).or_default() += 1; } } @@ -429,6 +453,121 @@ fn start_checks(v: &mut Value, liquid: &str, lost: &mut BTreeMap) } } +/// A Repurposed Structures or Moog's jigsaw's layout rules for the game's +/// piece-limited assembler: y_allowance, burying, the pools whose fallbacks +/// ignore the bounds, liquid, and the Nether variants' land search. The +/// bounding box hack is vanilla's expansion hack. The pieces' counts are +/// added from the mod's count files ([`piece_counts`]). +fn piece_limited(v: &mut Value, library: &str) -> Value { + let mut out = serde_json::Map::new(); + out.insert("library".into(), library.into()); + let lower = |x: &Value| x.as_str().map(str::to_ascii_lowercase); + for (from, to) in [("min_y_allowed", "min_y"), ("max_y_allowed", "max_y")] { + if let Some(n) = v.get("y_allowance").and_then(|y| y.get(from)) { + out.insert(to.into(), n.clone()); + } + } + if let Some(b) = v.get("burying_type").and_then(lower) { + out.insert("burying".into(), b.into()); + } + if v["cannot_spawn_in_liquid"].as_bool() == Some(true) { + out.insert("no_liquid".into(), true.into()); + } + if let Some(pools) = v + .get("pools_that_ignore_boundaries") + .filter(|p| p.is_array()) + { + out.insert("ignore_bounds_pools".into(), pools.clone()); + } + if v["use_bounding_box_hack"].as_bool() == Some(true) { + v["use_expansion_hack"] = true.into(); + } + if let Some(o) = v.as_object_mut() { + if let Some(search) = o.remove("land_search_direction") { + out.insert("land_search".into(), lower(&search).into()); + } + if let Some(ledge) = o.remove("ledge_offset_y") { + out.insert("ledge_offset_y".into(), ledge); + } + } + out.into() +} + +/// The pieces' spawn counts of the structures a mod ships, by structure id: +/// Repurposed Structures' `rs_pieces_spawn_counts` (each file names its +/// `target_structure`) and Moog's `msl_pieces_spawn_counts` (the file's id), +/// in the game's `horizon:piece_limited` shape. +pub fn piece_counts( + zip: &mut zip::ZipArchive, +) -> BTreeMap { + let mut out = BTreeMap::new(); + for i in 0..zip.len() { + let Ok(mut entry) = zip.by_index(i) else { + continue; + }; + let name = entry.name().to_owned(); + let Some((ns, rest)) = name + .strip_prefix("data/") + .and_then(|r| r.strip_suffix(".json")) + .and_then(|r| r.split_once('/')) + else { + continue; + }; + let (rs, path) = if let Some(p) = rest.strip_prefix("rs_pieces_spawn_counts/") { + (true, p) + } else if let Some(p) = rest.strip_prefix("msl_pieces_spawn_counts/") { + (false, p) + } else { + continue; + }; + let mut text = String::new(); + let Ok(v) = entry + .read_to_string(&mut text) + .map_err(|_| ()) + .and_then(|_| serde_json::from_str::(&text).map_err(|_| ())) + else { + continue; + }; + let id = match v["target_structure"].as_str() { + Some(target) if rs => target.to_owned(), + _ if rs => continue, + _ => format!("{ns}:{path}"), + }; + let pieces: Vec = v["pieces_spawn_counts"] + .as_array() + .into_iter() + .flatten() + .filter_map(|p| { + let mut e = serde_json::Map::new(); + e.insert("name".into(), p["nbt_piece_name"].as_str()?.into()); + for (from, to) in [ + ("always_spawn_this_many", "required"), + ("never_spawn_more_than_this_many", "max"), + ("minimum_distance_from_center_piece", "min_depth"), + ] { + if let Some(n) = p.get(from).filter(|n| n.is_number()) { + e.insert(to.into(), n.clone()); + } + } + Some(Value::Object(e)) + }) + .collect(); + out.insert(id, pieces.into()); + } + out +} + +/// Adds a structure's piece counts to its `horizon:piece_limited` key. +pub fn add_piece_counts(id: &str, v: &mut Value, counts: &BTreeMap) -> bool { + match (counts.get(id), v.get_mut("horizon:piece_limited")) { + (Some(pieces), Some(limited)) => { + limited["pieces"] = pieces.clone(); + true + } + _ => false, + } +} + /// AdvancedRandomSpread's spacing and separation, as Moog's computes them /// with its default config: 1.65 times the file's, rounded, the separation /// below the spacing. `when_replacing` (other numbers while a preset @@ -691,9 +830,8 @@ mod tests { let mut s = json!({"type": "lithostitched:jigsaw", "start_pool": "a:b"}); assert!(upgrade("structure", &mut s, &mut report, &NO_TAGS)); assert_eq!(s["type"], "minecraft:jigsaw"); - let mut nether = - json!({"type": "moogs_structures:moogs_structures_generic_nether_jigsaw_structure"}); - assert!(!upgrade("structure", &mut nether, &mut report, &NO_TAGS)); + let mut formation = json!({"type": "formations:simple"}); + assert!(!upgrade("structure", &mut formation, &mut report, &NO_TAGS)); let mut set = json!({"placement": {"type": "moogs_structures:advanced_random_spread", "spacing": 10, "separation": 5, "min_distance_from_world_origin": 250}}); assert!(upgrade("structure_set", &mut set, &mut report, &NO_TAGS)); @@ -730,10 +868,7 @@ mod tests { assert_eq!( report.unsupported, BTreeMap::from([ - ( - "moogs_structures:moogs_structures_generic_nether_jigsaw_structure".into(), - 1 - ), + ("formations:simple".into(), 1), ("somelib:element".into(), 1), ]) ); @@ -771,9 +906,12 @@ mod tests { let mut plain = json!({"type": "lithostitched:jigsaw", "cannot_spawn_in_liquid": false}); assert!(upgrade("structure", &mut plain, &mut report, &NO_TAGS)); assert!(plain.get("horizon:start_checks").is_none()); + // The layout rules go to the piece-limited assembler. + assert!(report.rules_lost.is_empty()); assert_eq!( - report.rules_lost, - BTreeMap::from([("burying_type".into(), 1), ("y_allowance".into(), 1)]) + moogs["horizon:piece_limited"], + json!({"library": "moogs", "min_y": 60, "burying": "lowest_corner", + "no_liquid": true}) ); } @@ -957,6 +1095,74 @@ mod tests { assert!(!disables("betterwitchhuts:witch_circle", &mut circle)); } + #[test] + fn piece_limited_jigsaws_keep_their_layout() { + let mut report = Upgrade::default(); + let mut nether = json!({"type": "repurposed_structures:generic_nether_jigsaw_structure", + "start_pool": "rs:x", "size": 8, "y_allowance": {"min_y_allowed": 40, "max_y_allowed": 90}, + "cannot_spawn_in_liquid": true, "pools_that_ignore_boundaries": ["rs:roof"], + "use_bounding_box_hack": true, "land_search_direction": "HIGHEST_LAND", + "ledge_offset_y": 2}); + assert!(upgrade("structure", &mut nether, &mut report, &NO_TAGS)); + assert_eq!(nether["type"], "minecraft:jigsaw"); + assert_eq!( + nether["horizon:piece_limited"], + json!({"library": "repurposed", "min_y": 40, "max_y": 90, "no_liquid": true, + "ignore_bounds_pools": ["rs:roof"], "land_search": "highest_land", + "ledge_offset_y": 2}) + ); + assert_eq!(nether["use_expansion_hack"], true); + for gone in [ + "y_allowance", + "use_bounding_box_hack", + "land_search_direction", + "ledge_offset_y", + ] { + assert!(nether.get(gone).is_none(), "{gone}"); + } + let mut moogs = json!({"type": "moogs_structures:moogs_structures_generic_jigsaw_structure", + "burying_type": "AVERAGE_LAND"}); + assert!(upgrade("structure", &mut moogs, &mut report, &NO_TAGS)); + assert_eq!( + moogs["horizon:piece_limited"], + json!({"library": "moogs", "burying": "average_land"}) + ); + // Lithostitched's jigsaw is vanilla's: no key. + let mut litho = json!({"type": "lithostitched:jigsaw"}); + assert!(upgrade("structure", &mut litho, &mut report, &NO_TAGS)); + assert!(litho.get("horizon:piece_limited").is_none()); + assert!(report.rules_lost.is_empty(), "{:?}", report.rules_lost); + assert!(report.unsupported.is_empty()); + + let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); + let opts = zip::write::SimpleFileOptions::default(); + for (name, text) in [ + ( + "data/rs/rs_pieces_spawn_counts/city.json", + r#"{"target_structure": "rs:city_nether", "pieces_spawn_counts": [ + {"nbt_piece_name": "rs:city/center", "always_spawn_this_many": 1, + "never_spawn_more_than_this_many": 1, "minimum_distance_from_center_piece": 3}]}"#, + ), + ( + "data/mvs/msl_pieces_spawn_counts/tower.json", + r#"{"pieces_spawn_counts": [{"nbt_piece_name": "mvs:top", "never_spawn_more_than_this_many": 2}]}"#, + ), + ] { + zip.start_file(name, opts).unwrap(); + std::io::Write::write_all(&mut zip, text.as_bytes()).unwrap(); + } + let mut zip = zip::ZipArchive::new(zip.finish().unwrap()).unwrap(); + let counts = piece_counts(&mut zip); + assert_eq!( + counts["rs:city_nether"], + json!([{"name": "rs:city/center", "required": 1, "max": 1, "min_depth": 3}]) + ); + assert_eq!(counts["mvs:tower"], json!([{"name": "mvs:top", "max": 2}])); + assert!(add_piece_counts("rs:city_nether", &mut nether, &counts)); + assert_eq!(nether["horizon:piece_limited"]["pieces"][0]["required"], 1); + assert!(!add_piece_counts("rs:other", &mut nether, &counts)); + } + #[test] fn writes_block_states_in_26_3_shape() { let mut report = Upgrade::default();