diff --git a/crates/horizon-port-pack/src/processors.rs b/crates/horizon-port-pack/src/processors.rs index db099972f..5dd9c9730 100644 --- a/crates/horizon-port-pack/src/processors.rs +++ b/crates/horizon-port-pack/src/processors.rs @@ -12,7 +12,10 @@ //! preset is on; one that ships off is a no-op and is taken out. //! - Lithostitched's `reference` runs other processor lists in turn: their //! processors are written in its place (Choice Theorem's Overhauled -//! Villages builds most of its lists so). +//! Villages builds most of its lists so). Its `apply_random` and +//! `condition` become the game's `horizon:apply_random` and +//! `horizon:condition`, with the lists inside them carried the same way, +//! and its `set_block` (simple or weighted states) `horizon:set_block`. //! //! What is not here is taken out of its list later and counted //! (`worldgen::upgrade`). The entity NBT is written in 26.3's shape (an @@ -142,26 +145,142 @@ pub fn carry( let Some(list) = processors(list) else { return false; }; - let mut changed = false; - let expanded = references(std::mem::take(list), data, carried, 0); - changed |= expanded.1; - let mut out = Vec::with_capacity(expanded.0.len()); - for p in expanded.0 { - let kind = p["processor_type"].as_str().unwrap_or_default().to_owned(); - match as_data(&kind, &p, data) { - Some(replacement) => { - *carried.entry(kind).or_default() += 1; - out.extend(replacement); - changed = true; - } - None => out.push(p), - } - } + let (out, changed) = carry_all(std::mem::take(list), data, carried); *list = out; changed }) } +/// The processors of one list carried: references written in, then each +/// one with a data form replaced by it. Whether anything changed. +fn carry_all( + list: Vec, + data: &ProcessorData, + carried: &mut BTreeMap, +) -> (Vec, bool) { + let (expanded, mut changed) = references(list, data, carried, 0); + let mut out = Vec::with_capacity(expanded.len()); + for p in expanded { + let kind = p["processor_type"].as_str().unwrap_or_default().to_owned(); + let replacement = match kind.as_str() { + "lithostitched:apply_random" => apply_random(&p, data, carried).map(|p| vec![p]), + "lithostitched:condition" => condition(&p, data, carried).map(|p| vec![p]), + _ => as_data(&kind, &p, data), + }; + match replacement { + Some(replacement) => { + *carried.entry(kind).or_default() += 1; + out.extend(replacement); + changed = true; + } + None => out.push(p), + } + } + (out, changed) +} + +/// A processor list inside a carried processor: an id stays (the game reads +/// that file, carried on its own); an inline one is carried here. +fn nested(list: &Value, data: &ProcessorData, carried: &mut BTreeMap) -> Value { + let mut list = list.clone(); + if let Some(ps) = processors(&mut list) { + *ps = carry_all(std::mem::take(ps), data, carried).0; + } + list +} + +/// Lithostitched's `apply_random` as `horizon:apply_random`: its weighted +/// lists, or its set of lists at weight 1. A tag is not carried. +fn apply_random( + p: &Value, + data: &ProcessorData, + carried: &mut BTreeMap, +) -> Option { + let lists = &p["processor_lists"]; + let weighted = lists.as_array().is_some_and(|a| { + a.iter() + .all(|e| e.get("weight").is_some() && e.get("data").is_some()) + }); + let entries: Vec<(Value, Value)> = if weighted { + lists + .as_array()? + .iter() + .map(|e| (e["weight"].clone(), e["data"].clone())) + .collect() + } else { + match lists { + Value::String(id) if !id.starts_with('#') => vec![(1.into(), lists.clone())], + Value::Array(a) if a.iter().any(|x| x.get("processor_type").is_some()) => { + vec![(1.into(), lists.clone())] + } + Value::Array(a) + if a.iter() + .all(|x| x.as_str().is_some_and(|s| !s.starts_with('#')) || x.is_object()) => + { + a.iter().map(|x| (1.into(), x.clone())).collect() + } + Value::Object(_) => vec![(1.into(), lists.clone())], + _ => return None, + } + }; + let lists: Vec = entries + .into_iter() + .map(|(weight, list)| json!({"weight": weight, "data": nested(&list, data, carried)})) + .collect(); + let mut out = json!({"processor_type": "horizon:apply_random", "processor_lists": lists}); + if let Some(mode) = p.get("mode") { + out["mode"] = mode.clone(); + } + Some(out) +} + +/// Lithostitched's `condition` as `horizon:condition`, when the game runs +/// every condition in it (true, all_of, any_of, not, random_chance, +/// matching_blocks with plain property values); else it stays Java. +fn condition( + p: &Value, + data: &ProcessorData, + carried: &mut BTreeMap, +) -> Option { + fn runs(c: &Value) -> bool { + if let Some(all) = c.as_array() { + return all.iter().all(runs); + } + let kind = c["type"].as_str().unwrap_or_default(); + match kind.strip_prefix("lithostitched:").unwrap_or(kind) { + "true" | "random_chance" => true, + "all_of" | "any_of" => c["conditions"] + .as_array() + .is_some_and(|a| a.iter().all(runs)), + "not" => runs(&c["condition"]), + "matching_blocks" => c.get("properties").is_none_or(|p| { + p.as_object() + .is_some_and(|o| o.values().all(Value::is_string)) + }), + _ => false, + } + } + if !runs(&p["if_true"]) { + return None; + } + // One processor or a list of them. + let mut branch = |key: &str| -> Value { + let list = match p.get(key) { + None => Vec::new(), + Some(Value::Array(a)) => a.clone(), + Some(one) => vec![one.clone()], + }; + carry_all(list, data, carried).0.into() + }; + let (then, otherwise) = (branch("then"), branch("else")); + let mut out = json!({"processor_type": "horizon:condition", "if_true": p["if_true"], + "then": then, "else": otherwise}); + if let Some(mode) = p.get("random_mode") { + out["random_mode"] = mode.clone(); + } + Some(out) +} + /// Repurposed Structures' rule tests and the game's types that run them /// (`horizon_world::worldgen`'s `rule_test` and `structure::processor`). const RULE_TESTS: [(&str, &str); 3] = [ @@ -355,6 +474,28 @@ fn as_data(kind: &str, p: &Value, data: &ProcessorData) -> Option> { p["blocks"].as_object()?; vec![json!({"processor_type": "horizon:block_swap", "blocks": p["blocks"]})] } + "lithostitched:set_block" => { + // A simple or weighted state provider, the block entity left as + // it is. + let provider = p["state_provider"]["type"].as_str()?; + let modifier = p["block_entity_modifier"]["type"].as_str(); + let plain = + modifier.is_none_or(|m| m.trim_start_matches("minecraft:") == "passthrough"); + if !plain + || !matches!( + provider, + "minecraft:simple_state_provider" | "minecraft:weighted_state_provider" + ) + { + return None; + } + let mut out = p.clone(); + out["processor_type"] = "horizon:set_block".into(); + if let Some(o) = out.as_object_mut() { + o.remove("block_entity_modifier"); + } + vec![out] + } "betterdungeons:small_dungeon_chest_processor" => { // Its config's defaults: one chest at least, two at most, a // fifth of the others kept until then; ChestBlock covers both. @@ -1059,6 +1200,59 @@ mod tests { assert_eq!(carried["lithostitched:reference"], 2); } + #[test] + fn lithostitched_random_and_condition_become_the_games() { + let mut data = data(); + let swap = json!({"processor_type": "lithostitched:block_swap", + "blocks": {"minecraft:dirt": "minecraft:grass_block"}}); + data.lists + .insert("v:green".into(), json!({"processors": [swap.clone()]})); + let mut list = json!({"processors": [ + {"processor_type": "lithostitched:apply_random", "mode": "per_piece", "processor_lists": [ + {"weight": 3, "data": "minecraft:empty"}, + {"weight": 1, "data": {"processors": [ + {"processor_type": "lithostitched:reference", "processor_lists": "v:green"}, + {"processor_type": "v:java"}]}}]}, + {"processor_type": "lithostitched:apply_random", "mode": "per_block", + "processor_lists": ["v:green", "v:red"]}, + {"processor_type": "lithostitched:apply_random", "mode": "per_block", + "processor_lists": "#v:colours"}, + {"processor_type": "lithostitched:condition", + "if_true": [{"type": "lithostitched:matching_blocks", "blocks": "minecraft:cobblestone"}], + "then": swap, "else": {"processor_type": "lithostitched:set_block", "state_provider": + {"type": "minecraft:simple_state_provider", "state": {"Name": "minecraft:stone"}}}}, + {"processor_type": "lithostitched:condition", + "if_true": {"type": "lithostitched:matching_biomes", "biomes": "#c:is_cold"}, + "then": []}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data, &mut carried)); + let p = list["processors"].as_array().unwrap(); + let green = json!({"processor_type": "horizon:block_swap", + "blocks": {"minecraft:dirt": "minecraft:grass_block"}}); + // Inline lists are carried; Java left in them is dropped later. + assert_eq!( + p[0], + json!({"processor_type": "horizon:apply_random", "mode": "per_piece", "processor_lists": [ + {"weight": 3, "data": "minecraft:empty"}, + {"weight": 1, "data": {"processors": [green.clone(), {"processor_type": "v:java"}]}}]}) + ); + assert_eq!( + p[1]["processor_lists"], + json!([{"weight": 1, "data": "v:green"}, {"weight": 1, "data": "v:red"}]) + ); + assert_eq!(p[2]["processor_type"], "lithostitched:apply_random"); + assert_eq!( + p[3], + json!({"processor_type": "horizon:condition", + "if_true": [{"type": "lithostitched:matching_blocks", "blocks": "minecraft:cobblestone"}], + "then": [green], "else": [{"processor_type": "horizon:set_block", "state_provider": + {"type": "minecraft:simple_state_provider", "state": {"Name": "minecraft:stone"}}}]}) + ); + assert_eq!(p[4]["processor_type"], "lithostitched:condition"); + assert_eq!(carried["lithostitched:apply_random"], 2); + assert_eq!(carried["lithostitched:condition"], 1); + } + #[test] fn repurposed_rule_tests_become_the_games() { let rule = |input: Value, position: Value| { diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index a6adc2000..4449e9f5b 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -382,7 +382,29 @@ fn drop_java_processors(list: &mut Value, dropped: &mut BTreeMap) } !java }); - processors.len() != before + let mut changed = processors.len() != before; + // The inline lists inside the game's apply_random and condition. + for p in processors.iter_mut() { + let inner: Vec<&mut Value> = match p["processor_type"].as_str() { + Some("horizon:apply_random") => p["processor_lists"] + .as_array_mut() + .into_iter() + .flatten() + .map(|e| &mut e["data"]) + .collect(), + Some("horizon:condition") => { + let o = p.as_object_mut().into_iter().flat_map(|o| o.iter_mut()); + o.filter(|(k, _)| *k == "then" || *k == "else") + .map(|(_, v)| v) + .collect() + } + _ => Vec::new(), + }; + for list in inner { + changed |= drop_java_processors(list, dropped); + } + } + changed } /// 1.21.1 writes a block or fluid state as `{"Name": ..., "Properties": diff --git a/docs/modpacks.md b/docs/modpacks.md index 48e2a28e8..acc354f30 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -464,9 +464,16 @@ Two cases the oracle fills in: Lithostitched's `block_swap` (137 lists, all in ChoiceTheorem's Overhauled Village) is the game's `horizon:block_swap`: the same map, with Block.withPropertiesOf's properties kept; a target block the game does not have (most of them are Nature's Spirit's and Croptopia's) - leaves the block as it is, as the mod does. 273 of the 1,427 processors are carried; 1,154 - are still taken out (Formations' biome replacements, Repurposed's pillars and fluid fixes, - Lithostitched's reference, condition and apply_random, blocks aged or propped up). + leaves the block as it is, as the mod does. Lithostitched's `reference` (CTOV builds most + of its lists from others) has the referenced lists' processors written in its place; its + `apply_random`, `condition` and `set_block` are the game's `horizon:apply_random`, + `horizon:condition` and `horizon:set_block`, with Lithostitched's random (the world seed + plus the name's hash, at the block, piece or structure). In a CTOV plains village at seed + 1234 the farms now grow carrots, potatoes and beetroot besides wheat, and 1,655 + cobblestone blocks become paths, grass, mossy cobblestone and planks. No Lithostitched processor is + dropped now (130 before), only CTOV's compat hooks for other mods inside them (142 copies). + Still taken out: Formations' biome replacements, Repurposed's pillars and fluid fixes, + blocks aged or propped up. In the 128×128-chunk square (seed 1234), every spawner of a mod structure now has its mob: in the Overworld 83 Better Dungeons spawners held the template's placeholder pig and 17 of Moog's were empty (763 spawners with a mob, up from 628); in the Nether the 52 empty ones