diff --git a/crates/horizon-port-pack/src/processors.rs b/crates/horizon-port-pack/src/processors.rs index 737a90433..818e5db14 100644 --- a/crates/horizon-port-pack/src/processors.rs +++ b/crates/horizon-port-pack/src/processors.rs @@ -241,6 +241,14 @@ fn as_data(kind: &str, p: &Value, data: &ProcessorData) -> Option> { { vec![processor] } + "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. + vec![json!({"processor_type": "horizon:count_cap", + "blocks": ["minecraft:chest", "minecraft:trapped_chest"], + "min": 1, "max": 2, "chance": 0.2f32 as f64, + "replacement": block("minecraft:cave_air")})] + } _ if ns == "betterdungeons" || ns == "betterdeserttemples" => vec![yung_spawner(kind, p)?], _ => return None, }) @@ -873,4 +881,19 @@ mod tests { ); assert_eq!(carried.values().sum::(), 4); } + + #[test] + fn small_dungeon_chests_are_capped() { + let mut list = json!({"processors": [ + {"processor_type": "betterdungeons:small_dungeon_chest_processor"}]}); + let mut carried = BTreeMap::new(); + assert!(carry("processor_list", &mut list, &data(), &mut carried)); + let cap = &list["processors"][0]; + assert_eq!(cap["processor_type"], "horizon:count_cap"); + assert_eq!( + (cap["min"].as_u64(), cap["max"].as_u64()), + (Some(1), Some(2)) + ); + assert_eq!(cap["replacement"], json!({"id": "minecraft:cave_air"})); + } } diff --git a/crates/horizon-world/src/worldgen/structure/processor.rs b/crates/horizon-world/src/worldgen/structure/processor.rs index 204a168db..922664cba 100644 --- a/crates/horizon-world/src/worldgen/structure/processor.rs +++ b/crates/horizon-world/src/worldgen/structure/processor.rs @@ -183,6 +183,7 @@ pub enum Processor { BlackstoneReplace, SpawnerMobs(Box), Pick(Box), + CountCap(Box), } #[derive(Clone, Debug, Default)] pub struct ProcessorList(pub Vec); @@ -339,6 +340,56 @@ impl Pick { } } +/// `horizon:count_cap`, which `horizon-port-pack` writes for Better +/// Dungeons' chest processor (Java in the mod): of the `blocks` one +/// placement meets, the first `min` stay, the rest each stay when +/// `settings.getRandom(pos).nextFloat() <= chance` until `max` have, and the +/// others become `replacement`. The count is the placement's (the mod's +/// DungeonContext, set up per StructureTemplate.placeInWorld: one chunk of +/// the piece), shared by every such processor in it. +#[derive(Clone, Debug)] +pub struct CountCap { + blocks: BTreeSet, + min: u32, + max: u32, + chance: f32, + replacement: BlockStateId, +} +impl CountCap { + fn decode( + value: &Value, + state: impl Fn(&Value) -> Result, + ) -> Result { + let count = |key: &'static str| { + value[key] + .as_u64() + .and_then(|n| u32::try_from(n).ok()) + .ok_or(DecodeError::Field(key)) + }; + Ok(Self { + blocks: value["blocks"] + .as_array() + .ok_or(DecodeError::Field("blocks"))? + .iter() + .map(|b| { + let b = b.as_str().ok_or(DecodeError::Field("blocks"))?; + Ok(super::super::tags::identifier(b)?.to_string()) + }) + .collect::>()?, + min: count("min")?, + max: count("max")?, + chance: float(value, "chance")?, + replacement: state(&value["replacement"])?, + }) + } + /// Whether a counted block stays, counting it if so. + fn keeps(&self, counted: &mut u32, roll: impl FnOnce() -> f32) -> bool { + let keep = *counted < self.min || (*counted < self.max && roll() <= self.chance); + *counted += u32::from(keep); + keep + } +} + /// `horizon:spawner_mobs`, which `horizon-port-pack` writes for the spawner /// randomizing processors of Moog's Structure Lib and Repurposed Structures /// (Java code in the mods): a spawner gets one mob of a weighted list, @@ -571,6 +622,7 @@ impl Processor { })?)) } "horizon:pick" => Self::Pick(Box::new(Pick::decode(value, state)?)), + "horizon:count_cap" => Self::CountCap(Box::new(CountCap::decode(value, state)?)), _ => return Err(DecodeError::Field("structure processor type")), }) } @@ -624,6 +676,8 @@ pub struct PlaceSettings<'a> { /// StructurePlaceSettings.ignoreEntities / finalizeEntities. pub ignore_entities: bool, pub finalize_entities: bool, + /// The blocks `horizon:count_cap` kept in this placement. + counted: u32, } impl<'a> PlaceSettings<'a> { pub fn new(rotation: Rotation) -> Self { @@ -638,6 +692,7 @@ impl<'a> PlaceSettings<'a> { known_shape: false, ignore_entities: false, finalize_entities: false, + counted: 0, } } pub fn with_processors(mut self, list: &'a ProcessorList) -> Self { @@ -835,6 +890,23 @@ fn process_block( Some(info) } Processor::Capped { .. } => Some(info), + Processor::CountCap(cap) => match cap.blocks.contains(name(info.state)) { + true => { + let pos = info.pos; + let mut counted = settings.counted; + let keep = cap.keeps(&mut counted, || settings.with_random(pos, |r| r.float())); + settings.counted = counted; + Some(match keep { + true => info, + false => BlockInfo { + pos, + state: cap.replacement, + nbt: None, + }, + }) + } + false => Some(info), + }, Processor::Pick(pick) => match name(info.state) == pick.block { true => Some(settings.with_random(info.pos, |r| pick.apply(info, r))), false => Some(info), @@ -956,6 +1028,7 @@ fn process_infos( reference: [i32; 3], settings: &mut PlaceSettings<'_>, ) -> Result, PlacementQueryError> { + settings.counted = 0; let whole = settings .processors .iter() @@ -1617,4 +1690,26 @@ mod tests { ); assert!(Pick::decode(&json!({"block": "a", "default": {"states": []}}), id).is_err()); } + + #[test] + fn count_caps_keep_the_first_then_some() { + let cap = CountCap::decode( + &json!({"blocks": ["chest", "minecraft:trapped_chest"], "min": 1, "max": 2, + "chance": 0.2, "replacement": {"id": "minecraft:cave_air"}}), + |_| Ok(BlockStateId(5)), + ) + .unwrap(); + assert!(cap.blocks.contains("minecraft:chest")); + let mut counted = 0; + // The first stays without a roll. + assert!(cap.keeps(&mut counted, || unreachable!())); + // Then each stays on nextFloat <= 0.2, until two have. + assert!(!cap.keeps(&mut counted, || 0.21)); + assert!(cap.keeps(&mut counted, || 0.2)); + assert!(!cap.keeps(&mut counted, || unreachable!())); + assert_eq!(counted, 2); + assert!( + CountCap::decode(&json!({"blocks": [], "min": -1}), |_| Ok(BlockStateId(5))).is_err() + ); + } } diff --git a/docs/modpacks.md b/docs/modpacks.md index 04e3dd987..b6daee867 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -458,8 +458,11 @@ Two cases the oracle fills in: polished diorite, blank pots) become what its processors make of them: the fixed swaps as vanilla's `rule`, the random ones (suspicious sand, sand and sandstone, upside-down stairs, cobwebs, sherds on the pots) as the game's `horizon:pick`, on the same `nextFloat` as the - mod. 133 of the 1,427 processors are carried; 1,294 are still taken out (blocks swapped, - aged or propped up, YUNG's chest caps). + mod. Better Dungeons' small dungeons keep one chest and each other one with a fifth's + chance, two at most (the game's `horizon:count_cap`, counted per placement as the mod's + DungeonContext is): 12 chests in five small dungeons at seed 1234 become 6. + 136 of the 1,427 processors are carried; 1,291 are still taken out (blocks swapped, 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