Merge pull request #821 from ludovic111/feat/carry-structure-rules
🤖 Agent: merged: carry writes the mods' structure start rules and Moog's spacing. Combined check of main + modpacks-5's six: clippy, tests, worldgen structure_density block_properties registry_fluid chunk_stage PASS.
This commit is contained in:
@@ -362,6 +362,7 @@ pub fn carry(
|
||||
let mut convention = BTreeSet::new();
|
||||
let mut block_loot = BTreeSet::new();
|
||||
let mut biome_conventions = BTreeSet::new();
|
||||
let set_tags = crate::worldgen::structure_set_tags(&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();
|
||||
@@ -421,7 +422,8 @@ pub fn carry(
|
||||
if kind == "structure" {
|
||||
crate::worldgen::convention_tags(&v["biomes"], &mut biome_conventions);
|
||||
}
|
||||
if crate::worldgen::upgrade(kind, &mut v, &mut report.worldgen_types) {
|
||||
if crate::worldgen::upgrade(kind, &mut v, &mut report.worldgen_types, &set_tags)
|
||||
{
|
||||
bytes = serde_json::to_vec_pretty(&v).unwrap_or(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,16 @@
|
||||
//! structure libraries rewritten as vanilla's, and the loader's convention
|
||||
//! biome tags (`c:`) the structures name.
|
||||
//!
|
||||
//! The libraries' types add rules on top of vanilla's (a structure that may
|
||||
//! not start in liquid, a minimum distance from the world origin, a count
|
||||
//! cap per piece). Mapped to vanilla, those rules are lost: the structure
|
||||
//! keeps its pieces and its spacing but can start where the mod would not.
|
||||
//! Each rewrite is counted in the carry report, and every type left as it is
|
||||
//! is listed, so the coverage report can say what generates and how closely.
|
||||
//! The libraries' types add rules on top of vanilla's. The start and
|
||||
//! placement rules of Repurposed Structures' and Moog's jigsaws (no start in
|
||||
//! liquid, biomes and terrain height in a radius, a minimum distance from
|
||||
//! the world origin, sets to keep away from) are written under `horizon:`
|
||||
//! keys the game reads (`horizon_world::worldgen::structure::start_checks`),
|
||||
//! so those structures start where the mod would. Rules that change how the
|
||||
//! pieces are laid out (piece caps, burying, clipping) are lost and counted
|
||||
//! by name. Each rewrite is counted in the carry report, and every type left
|
||||
//! as it is is listed, so the coverage report can say what generates and how
|
||||
//! closely.
|
||||
use serde_json::Value;
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
@@ -27,6 +31,25 @@ const SPREADS: [&str; 2] = [
|
||||
"moogs_structures:advanced_random_spread",
|
||||
"repurposed_structures:advanced_random_spread",
|
||||
];
|
||||
/// Moog's Structure Lib spreads its structures 1.65 times as far apart as
|
||||
/// its files say (AdvancedRandomSpread's constructor).
|
||||
const MOOGS_SPACING: f64 = 1.65;
|
||||
/// The jigsaws' start rules and the `horizon:start_checks` key each maps to.
|
||||
const START_RULES: [(&str, &str); 3] = [
|
||||
("valid_biome_radius_check", "biome_radius"),
|
||||
("terrain_height_radius_check", "terrain_radius"),
|
||||
("allowed_terrain_height_range", "terrain_range"),
|
||||
];
|
||||
/// The jigsaws' rules on laying out pieces, which vanilla's jigsaw has no
|
||||
/// place for.
|
||||
const LAYOUT_RULES: [&str; 6] = [
|
||||
"y_allowance",
|
||||
"burying_type",
|
||||
"use_bounding_box_hack",
|
||||
"pools_that_ignore_boundaries",
|
||||
"enhanced_terrain_adaptation",
|
||||
"vertical_distance_from_start_piece",
|
||||
];
|
||||
/// Pool elements that are vanilla's with extra fields: (mod type, vanilla type).
|
||||
const ELEMENTS: [(&str, &str); 3] = [
|
||||
(
|
||||
@@ -49,14 +72,23 @@ pub struct Upgrade {
|
||||
pub block_states: usize,
|
||||
/// Types left as they are (Horizon skips what it cannot run), by type.
|
||||
pub unsupported: BTreeMap<String, usize>,
|
||||
/// Rules of mapped types the game does not run, by field.
|
||||
pub rules_lost: BTreeMap<String, usize>,
|
||||
}
|
||||
|
||||
/// Rewrites one `data/<ns>/worldgen/<kind>/...` file in place; whether it
|
||||
/// changed.
|
||||
pub fn upgrade(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool {
|
||||
/// `set_tags` are the mod's structure set tags, expanded
|
||||
/// ([`structure_set_tags`]).
|
||||
pub fn upgrade(
|
||||
kind: &str,
|
||||
v: &mut Value,
|
||||
report: &mut Upgrade,
|
||||
set_tags: &BTreeMap<String, Vec<String>>,
|
||||
) -> bool {
|
||||
let states = block_states(v);
|
||||
report.block_states += usize::from(states);
|
||||
upgrade_types(kind, v, report) || states
|
||||
upgrade_types(kind, v, report, set_tags) || states
|
||||
}
|
||||
|
||||
/// 1.21.1 writes a block or fluid state as `{"Name": ..., "Properties":
|
||||
@@ -88,11 +120,22 @@ fn block_states(v: &mut Value) -> bool {
|
||||
}
|
||||
|
||||
/// The library types of one file rewritten as vanilla's; whether it changed.
|
||||
fn upgrade_types(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool {
|
||||
fn upgrade_types(
|
||||
kind: &str,
|
||||
v: &mut Value,
|
||||
report: &mut Upgrade,
|
||||
set_tags: &BTreeMap<String, Vec<String>>,
|
||||
) -> bool {
|
||||
let lost = &mut report.rules_lost;
|
||||
let mut unsupported = |t: &str| *report.unsupported.entry(t.to_owned()).or_default() += 1;
|
||||
let changed = match kind {
|
||||
"structure" => match v["type"].as_str() {
|
||||
Some(t) if JIGSAWS.contains(&t) => {
|
||||
let liquid = match t.split_once(':').map(|(ns, _)| ns) {
|
||||
Some("moogs_structures") => "top",
|
||||
_ => "lowest",
|
||||
};
|
||||
start_checks(v, liquid, lost);
|
||||
v["type"] = "minecraft:jigsaw".into();
|
||||
true
|
||||
}
|
||||
@@ -104,7 +147,13 @@ fn upgrade_types(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool {
|
||||
},
|
||||
"structure_set" => match v["placement"]["type"].as_str() {
|
||||
Some(t) if SPREADS.contains(&t) => {
|
||||
v["placement"]["type"] = "minecraft:random_spread".into();
|
||||
let moogs = t.starts_with("moogs_structures:");
|
||||
let p = &mut v["placement"];
|
||||
if moogs {
|
||||
moogs_spacing(p, lost);
|
||||
}
|
||||
placement_rules(p, set_tags, lost);
|
||||
p["type"] = "minecraft:random_spread".into();
|
||||
true
|
||||
}
|
||||
Some(t) if !t.starts_with("minecraft:") => {
|
||||
@@ -144,6 +193,144 @@ fn upgrade_types(kind: &str, v: &mut Value, report: &mut Upgrade) -> bool {
|
||||
changed
|
||||
}
|
||||
|
||||
/// 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<String, usize>) {
|
||||
let Some(o) = v.as_object_mut() else {
|
||||
return;
|
||||
};
|
||||
let mut checks = serde_json::Map::new();
|
||||
for (from, to) in START_RULES {
|
||||
if let Some(n) = o.remove(from) {
|
||||
checks.insert(to.into(), n);
|
||||
}
|
||||
}
|
||||
if o.remove("cannot_spawn_in_liquid").and_then(|b| b.as_bool()) == Some(true) {
|
||||
checks.insert("liquid".into(), liquid.into());
|
||||
}
|
||||
// y_allowance bounds the terrain heights checked; it also clips pieces,
|
||||
// which is lost.
|
||||
for (from, to) in [("min_y_allowed", "min_y"), ("max_y_allowed", "max_y")] {
|
||||
if let Some(n) = o.get("y_allowance").and_then(|y| y.get(from)) {
|
||||
checks.insert(to.into(), n.clone());
|
||||
}
|
||||
}
|
||||
for rule in LAYOUT_RULES {
|
||||
if o.remove(rule).is_some_and(|r| r != Value::Bool(false)) {
|
||||
*lost.entry(rule.into()).or_default() += 1;
|
||||
}
|
||||
}
|
||||
if !checks.is_empty() {
|
||||
o.insert("horizon:start_checks".into(), checks.into());
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// replaces a vanilla structure) is lost.
|
||||
fn moogs_spacing(p: &mut Value, lost: &mut BTreeMap<String, usize>) {
|
||||
let scaled = |k: &str| (p[k].as_f64().unwrap_or(0.) * MOOGS_SPACING).round() as i64;
|
||||
let spacing = scaled("spacing").max(1);
|
||||
let separation = scaled("separation").min(spacing - 1);
|
||||
p["spacing"] = spacing.into();
|
||||
p["separation"] = separation.into();
|
||||
if let Some(o) = p.as_object_mut() {
|
||||
if o.remove("when_replacing").is_some() {
|
||||
*lost.entry("when_replacing".into()).or_default() += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An advanced random spread's minimum distance from the world origin and
|
||||
/// its super exclusion zone (sets named by a tag) under `horizon:` keys.
|
||||
fn placement_rules(
|
||||
p: &mut Value,
|
||||
set_tags: &BTreeMap<String, Vec<String>>,
|
||||
lost: &mut BTreeMap<String, usize>,
|
||||
) {
|
||||
let Some(o) = p.as_object_mut() else {
|
||||
return;
|
||||
};
|
||||
if let Some(d) = o.remove("min_distance_from_world_origin") {
|
||||
o.insert("horizon:min_distance_from_world_origin".into(), d);
|
||||
}
|
||||
if let Some(zone) = o.remove("super_exclusion_zone") {
|
||||
let sets: Vec<String> = match &zone["other_set"] {
|
||||
Value::String(s) => match s.strip_prefix('#') {
|
||||
Some(tag) => set_tags.get(tag).cloned().unwrap_or_default(),
|
||||
None => vec![s.clone()],
|
||||
},
|
||||
Value::Array(a) => a
|
||||
.iter()
|
||||
.filter_map(|s| Some(s.as_str()?.to_owned()))
|
||||
.collect(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
if sets.is_empty() {
|
||||
*lost.entry("super_exclusion_zone".into()).or_default() += 1;
|
||||
} else {
|
||||
o.insert(
|
||||
"horizon:exclusion_zone".into(),
|
||||
serde_json::json!({"other_sets": sets, "chunk_count": zone["chunk_count"]}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The mod's structure set tags (`data/<ns>/tags/worldgen/structure_set/`),
|
||||
/// by tag id, with the tags they include expanded. Optional entries
|
||||
/// (`{"id": ..., "required": false}`) count as their id.
|
||||
pub fn structure_set_tags<R: Read + std::io::Seek>(
|
||||
zip: &mut zip::ZipArchive<R>,
|
||||
) -> BTreeMap<String, Vec<String>> {
|
||||
let mut raw: BTreeMap<String, Vec<String>> = BTreeMap::new();
|
||||
for i in 0..zip.len() {
|
||||
let Ok(mut entry) = zip.by_index(i) else {
|
||||
continue;
|
||||
};
|
||||
let Some(id) = entry
|
||||
.name()
|
||||
.strip_prefix("data/")
|
||||
.and_then(|r| r.split_once("/tags/worldgen/structure_set/"))
|
||||
.and_then(|(ns, path)| Some(format!("{ns}:{}", path.strip_suffix(".json")?)))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let mut text = String::new();
|
||||
let Ok(v) = entry.read_to_string(&mut text).map_err(drop).and_then(|_| {
|
||||
serde_json::from_str::<Value>(text.trim_start_matches('\u{feff}')).map_err(drop)
|
||||
}) else {
|
||||
continue;
|
||||
};
|
||||
let values = v["values"].as_array().into_iter().flatten();
|
||||
raw.insert(
|
||||
id,
|
||||
values
|
||||
.filter_map(|e| e.as_str().or_else(|| e["id"].as_str()).map(str::to_owned))
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
let expand = |tag: &str| {
|
||||
let (mut out, mut seen, mut todo) = (Vec::new(), BTreeSet::new(), vec![tag.to_owned()]);
|
||||
while let Some(t) = todo.pop() {
|
||||
if !seen.insert(t.clone()) {
|
||||
continue;
|
||||
}
|
||||
for e in raw.get(&t).into_iter().flatten() {
|
||||
match e.strip_prefix('#') {
|
||||
Some(inner) => todo.push(inner.to_owned()),
|
||||
None if !out.contains(e) => out.push(e.clone()),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
};
|
||||
raw.keys().map(|t| (t.clone(), expand(t))).collect()
|
||||
}
|
||||
|
||||
/// The `#c:` biome tags `v` names.
|
||||
pub fn convention_tags(v: &Value, out: &mut BTreeSet<String>) {
|
||||
match v {
|
||||
@@ -231,27 +418,40 @@ mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
const NO_TAGS: BTreeMap<String, Vec<String>> = BTreeMap::new();
|
||||
|
||||
#[test]
|
||||
fn maps_library_types_to_vanillas() {
|
||||
let mut report = Upgrade::default();
|
||||
let mut s = json!({"type": "lithostitched:jigsaw", "start_pool": "a:b"});
|
||||
assert!(upgrade("structure", &mut s, &mut report));
|
||||
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));
|
||||
assert!(!upgrade("structure", &mut nether, &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));
|
||||
assert!(upgrade("structure_set", &mut set, &mut report, &NO_TAGS));
|
||||
assert_eq!(set["placement"]["type"], "minecraft:random_spread");
|
||||
assert_eq!(set["placement"]["spacing"], 10);
|
||||
// Moog's spreads 1.65 times as far as its file says.
|
||||
assert_eq!(set["placement"]["spacing"], 17);
|
||||
assert_eq!(set["placement"]["separation"], 8);
|
||||
assert_eq!(
|
||||
set["placement"]["horizon:min_distance_from_world_origin"],
|
||||
250
|
||||
);
|
||||
assert!(
|
||||
set["placement"]
|
||||
.get("min_distance_from_world_origin")
|
||||
.is_none()
|
||||
);
|
||||
let mut pool = json!({"elements": [
|
||||
{"weight": 1, "element": {"element_type": "moogs_structures:versioned_single_pool_element", "location": "m:x"}},
|
||||
{"weight": 1, "element": {"element_type": "minecraft:list_pool_element", "elements": [
|
||||
{"element_type": "empty_pool_element"}]}},
|
||||
{"weight": 1, "element": {"element_type": "yungsapi:yung_single_element"}}
|
||||
]});
|
||||
assert!(upgrade("template_pool", &mut pool, &mut report));
|
||||
assert!(upgrade("template_pool", &mut pool, &mut report, &NO_TAGS));
|
||||
assert_eq!(
|
||||
pool["elements"][0]["element"]["element_type"],
|
||||
"minecraft:single_pool_element"
|
||||
@@ -274,24 +474,109 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_the_jigsaws_start_rules() {
|
||||
let mut report = Upgrade::default();
|
||||
let mut moogs = json!({"type": "moogs_structures:moogs_structures_generic_jigsaw_structure",
|
||||
"cannot_spawn_in_liquid": true, "terrain_height_radius_check": 2,
|
||||
"allowed_terrain_height_range": 12, "valid_biome_radius_check": 1,
|
||||
"y_allowance": {"min_y_allowed": 60}, "burying_type": "lowest_corner",
|
||||
"use_bounding_box_hack": false, "size": 7});
|
||||
assert!(upgrade("structure", &mut moogs, &mut report, &NO_TAGS));
|
||||
assert_eq!(
|
||||
moogs["horizon:start_checks"],
|
||||
json!({"liquid": "top", "terrain_radius": 2, "terrain_range": 12,
|
||||
"biome_radius": 1, "min_y": 60})
|
||||
);
|
||||
for gone in [
|
||||
"cannot_spawn_in_liquid",
|
||||
"y_allowance",
|
||||
"burying_type",
|
||||
"use_bounding_box_hack",
|
||||
] {
|
||||
assert!(moogs.get(gone).is_none(), "{gone}");
|
||||
}
|
||||
assert_eq!(moogs["size"], 7);
|
||||
// Repurposed Structures reads the block above the bottom.
|
||||
let mut rs = json!({"type": "repurposed_structures:generic_jigsaw_structure",
|
||||
"cannot_spawn_in_liquid": true});
|
||||
assert!(upgrade("structure", &mut rs, &mut report, &NO_TAGS));
|
||||
assert_eq!(rs["horizon:start_checks"], json!({"liquid": "lowest"}));
|
||||
// Nothing to check: no key.
|
||||
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());
|
||||
assert_eq!(
|
||||
report.rules_lost,
|
||||
BTreeMap::from([("burying_type".into(), 1), ("y_allowance".into(), 1)])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn super_exclusion_zones_name_their_sets() {
|
||||
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let opts = zip::write::SimpleFileOptions::default();
|
||||
for (name, text) in [
|
||||
(
|
||||
"data/rs/tags/worldgen/structure_set/avoid.json",
|
||||
r##"{"values": ["minecraft:villages", "#rs:more", {"id": "x:y", "required": false}]}"##,
|
||||
),
|
||||
(
|
||||
"data/rs/tags/worldgen/structure_set/more.json",
|
||||
r##"{"values": ["minecraft:villages", "rs:cities", "#rs:avoid"]}"##,
|
||||
),
|
||||
] {
|
||||
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 tags = structure_set_tags(&mut zip);
|
||||
assert_eq!(tags["rs:avoid"], ["minecraft:villages", "x:y", "rs:cities"]);
|
||||
let mut report = Upgrade::default();
|
||||
let mut set = json!({"placement": {"type": "repurposed_structures:advanced_random_spread",
|
||||
"spacing": 30, "separation": 10,
|
||||
"super_exclusion_zone": {"chunk_count": 4, "other_set": "#rs:avoid"}}});
|
||||
assert!(upgrade("structure_set", &mut set, &mut report, &tags));
|
||||
assert_eq!(set["placement"]["spacing"], 30);
|
||||
assert_eq!(
|
||||
set["placement"]["horizon:exclusion_zone"],
|
||||
json!({"other_sets": ["minecraft:villages", "x:y", "rs:cities"], "chunk_count": 4})
|
||||
);
|
||||
let mut unknown = json!({"placement": {"type": "repurposed_structures:advanced_random_spread",
|
||||
"spacing": 30, "separation": 10,
|
||||
"super_exclusion_zone": {"chunk_count": 4, "other_set": "#rs:none"}}});
|
||||
assert!(upgrade("structure_set", &mut unknown, &mut report, &tags));
|
||||
assert_eq!(report.rules_lost["super_exclusion_zone"], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_block_states_in_26_3_shape() {
|
||||
let mut report = Upgrade::default();
|
||||
let mut list = json!({"processors": [{"processor_type": "minecraft:rule", "rules": [{
|
||||
"input_predicate": {"predicate_type": "minecraft:always_true"},
|
||||
"output_state": {"Name": "minecraft:oak_stairs", "Properties": {"facing": "east"}}}]}]});
|
||||
assert!(upgrade("processor_list", &mut list, &mut report));
|
||||
assert!(upgrade("processor_list", &mut list, &mut report, &NO_TAGS));
|
||||
assert_eq!(
|
||||
list["processors"][0]["rules"][0]["output_state"],
|
||||
json!({"id": "minecraft:oak_stairs", "properties": {"facing": "east"}})
|
||||
);
|
||||
let mut spring = json!({"type": "minecraft:spring_feature", "config": {
|
||||
"state": {"Name": "minecraft:water"}, "valid_blocks": ["minecraft:stone"]}});
|
||||
assert!(upgrade("configured_feature", &mut spring, &mut report));
|
||||
assert!(upgrade(
|
||||
"configured_feature",
|
||||
&mut spring,
|
||||
&mut report,
|
||||
&NO_TAGS
|
||||
));
|
||||
assert_eq!(spring["config"]["state"], json!({"id": "minecraft:water"}));
|
||||
// An object with other keys is not a state; a 26.3 state is left as it is.
|
||||
let mut other = json!({"Name": "x", "weight": 1, "state": {"id": "minecraft:stone"}});
|
||||
assert!(!upgrade("configured_feature", &mut other, &mut report));
|
||||
assert!(!upgrade(
|
||||
"configured_feature",
|
||||
&mut other,
|
||||
&mut report,
|
||||
&NO_TAGS
|
||||
));
|
||||
assert_eq!(report.block_states, 2);
|
||||
assert_eq!(report.mapped, 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user