Implement native vine attachment priority and placement
This commit is contained in:
@@ -41,6 +41,7 @@ pub mod state_provider;
|
||||
pub mod tags;
|
||||
pub mod terrain;
|
||||
pub mod value_provider;
|
||||
pub mod vines;
|
||||
pub mod weighted;
|
||||
use horizon_core::noise::{NoiseBuildError, NormalNoise, Normalization};
|
||||
use serde_json::value::RawValue;
|
||||
|
||||
@@ -48,6 +48,7 @@ pub enum FeatureDefinition {
|
||||
BlockPile(super::block_pile::BlockPileFeature),
|
||||
Disk(super::disk::DiskFeature),
|
||||
Spring(super::spring::SpringFeature),
|
||||
Vines(super::vines::VinesFeature),
|
||||
BlockBlob(super::block_blob::BlockBlobFeature),
|
||||
SimpleBlock(super::simple_block::SimpleBlockFeature),
|
||||
FillLayer(FillLayerFeature),
|
||||
@@ -253,6 +254,10 @@ impl FeatureDefinition {
|
||||
)?)?;
|
||||
match kind.as_str() {
|
||||
"minecraft:no_op" => Ok(Self::NoOp),
|
||||
"minecraft:vines" => super::vines::VinesFeature::new(
|
||||
blocks.ok_or(DecodeError::Field("vine feature bindings"))?,
|
||||
)
|
||||
.map(Self::Vines),
|
||||
"minecraft:spring_feature" => super::spring::SpringFeature::from_json(
|
||||
json,
|
||||
blocks.ok_or(DecodeError::Field("spring bindings"))?,
|
||||
@@ -577,6 +582,10 @@ impl FeatureDefinition {
|
||||
.place(world, random, origin, run.blocks)
|
||||
.map_err(FeatureExecutionError::Basic),
|
||||
Self::NoOp => Ok(true),
|
||||
Self::Vines(feature) => feature.place(world, origin).map_err(|e| match e {
|
||||
BlockFeatureError::World(e) => FeatureExecutionError::Basic(FeatureError::World(e)),
|
||||
BlockFeatureError::Query(e) => FeatureExecutionError::Query(e),
|
||||
}),
|
||||
Self::Spring(feature) => feature.place(world, origin).map_err(|e| match e {
|
||||
BlockFeatureError::World(e) => FeatureExecutionError::Basic(FeatureError::World(e)),
|
||||
BlockFeatureError::Query(e) => FeatureExecutionError::Query(e),
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
//! One vine attachment in native direction priority order.
|
||||
use super::{
|
||||
DecodeError,
|
||||
block_feature::{BlockFeatureError, FeatureBlockBindings},
|
||||
feature::FeatureWorld,
|
||||
placement::PlacementContext,
|
||||
};
|
||||
use crate::registry::BlockStateId;
|
||||
use horizon_core::geometry::Direction;
|
||||
const DIRECTIONS: [Direction; 5] = [
|
||||
Direction::Up,
|
||||
Direction::North,
|
||||
Direction::South,
|
||||
Direction::West,
|
||||
Direction::East,
|
||||
];
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct VinesFeature {
|
||||
air: [BlockStateId; 3],
|
||||
states: [BlockStateId; 5],
|
||||
}
|
||||
impl VinesFeature {
|
||||
pub fn new(bindings: &FeatureBlockBindings<'_>) -> Result<Self, DecodeError> {
|
||||
let registry = bindings.registry();
|
||||
let default = registry
|
||||
.default_state("minecraft:vine")
|
||||
.ok_or(DecodeError::Field("vine block missing"))?;
|
||||
let mut states = [default; 5];
|
||||
for (i, name) in ["up", "north", "south", "west", "east"]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let mut state = registry.state(default).unwrap().clone();
|
||||
state.properties.insert(name.into(), "true".into());
|
||||
states[i] = registry
|
||||
.id(&state)
|
||||
.ok_or(DecodeError::Field("vine face missing"))?;
|
||||
}
|
||||
Ok(Self {
|
||||
air: bindings.air,
|
||||
states,
|
||||
})
|
||||
}
|
||||
pub fn place<W: FeatureWorld + PlacementContext>(
|
||||
&self,
|
||||
world: &mut W,
|
||||
p: [i32; 3],
|
||||
) -> Result<bool, BlockFeatureError<W::Error>> {
|
||||
if !self
|
||||
.air
|
||||
.contains(&world.block(p).map_err(BlockFeatureError::World)?)
|
||||
{
|
||||
return Ok(false);
|
||||
}
|
||||
for (direction, state) in DIRECTIONS.into_iter().zip(self.states) {
|
||||
if world
|
||||
.attachment_at(p, direction)
|
||||
.map_err(BlockFeatureError::Query)?
|
||||
{
|
||||
world
|
||||
.set_block(p, state, 2)
|
||||
.map_err(BlockFeatureError::World)?;
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user