Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions crates/blocks/src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ noop_block_transform!(
u32,
bool,
BlockColorVariant,
BlockFacing,
TrapdoorHalf,
SignType,
ButtonFace,
Expand Down Expand Up @@ -89,6 +88,33 @@ impl BlockTransform for BlockDirection {
}
}

impl BlockTransform for BlockFacing {
fn rotate90(&mut self) {
*self = match self {
BlockFacing::North => BlockFacing::East,
BlockFacing::East => BlockFacing::South,
BlockFacing::South => BlockFacing::West,
BlockFacing::West => BlockFacing::North,
BlockFacing::Up | BlockFacing::Down => *self,
}
}

fn flip(&mut self, dir: FlipDirection) {
match dir {
FlipDirection::FlipX => match self {
BlockFacing::East => *self = BlockFacing::West,
BlockFacing::West => *self = BlockFacing::East,
_ => {}
},
FlipDirection::FlipZ => match self {
BlockFacing::North => *self = BlockFacing::South,
BlockFacing::South => *self = BlockFacing::North,
_ => {}
},
}
}
}

impl Block {
pub fn has_block_entity(self) -> bool {
matches!(
Expand Down Expand Up @@ -743,22 +769,31 @@ blocks! {
transparent: true,
cube: true,
},
Observer {
RedstoneObserver {
props: {
facing: BlockFacing
observer: RedstoneObserver
},
get_id: (facing.get_id() << 1) + 12551,
from_id_offset: 12551,
from_id(id): 12551..=12561 => {
facing: BlockFacing::from_id(id >> 1)
get_id: {
observer.facing.get_id() * 2
+ !observer.powered as u32
+ 12550
},
from_id_offset: 12550,
from_id(id): 12550..=12561 => {
observer: {
RedstoneObserver::new(
BlockFacing::from_id(id >> 1),
(id & 1) == 0
)
}
},
from_names(_name): {
"observer" => {
facing: Default::default()
observer: Default::default()
}
},
get_name: "observer",
solid: true,
transparent: true,
cube: true,
},
SeaPickle {
Expand Down
16 changes: 16 additions & 0 deletions crates/blocks/src/blocks/props.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{Block, BlockDirection, BlockProperty, BlockTransform, FlipDirection};
use std::str::FromStr;
use crate::BlockFacing;

#[derive(Copy, Clone, Debug, PartialEq, Eq, BlockProperty, BlockTransform)]
pub struct RedstoneRepeater {
Expand Down Expand Up @@ -105,6 +106,21 @@ impl RedstoneComparator {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, BlockProperty, BlockTransform)]
pub struct RedstoneObserver {
pub facing: BlockFacing,
pub powered: bool
}

impl RedstoneObserver {
pub fn new(facing: BlockFacing, powered: bool) -> RedstoneObserver {
RedstoneObserver {
facing,
powered,
}
}
}

#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
pub enum LeverFace {
Floor,
Expand Down
6 changes: 6 additions & 0 deletions crates/blocks/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ items! {
from_id(_id): 658 => {},
block: true,
},
Observer {
props: {},
get_id: 665,
from_id(_id): 665 => {},
block: true,
},
Hopper {
props: {},
get_id: 666,
Expand Down
36 changes: 36 additions & 0 deletions crates/blocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ impl BlockFace {
_ => panic!("invalid BlockFace with id {}", id),
}
}

pub fn opposite(self) -> BlockFace {
use BlockFace::*;
match self {
Bottom => Top,
Top => Bottom,
North => South,
South => North,
West => East,
East => West,
}
}
}

impl BlockFace {
Expand Down Expand Up @@ -399,6 +411,30 @@ impl BlockFacing {
other => other,
}
}

pub fn opposite(self) -> BlockFacing {
use BlockFacing::*;
match self {
North => South,
South => North,
East => West,
West => East,
Up => Down,
Down => Up,
}
}

pub fn block_face(&self) -> BlockFace {
use BlockFacing::*;
match self {
North => BlockFace::North,
East => BlockFace::East,
South => BlockFace::South,
West => BlockFace::West,
Up => BlockFace::Top,
Down => BlockFace::Bottom,
}
}
}

impl ToString for BlockFacing {
Expand Down
39 changes: 24 additions & 15 deletions crates/core/src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use mchprs_blocks::items::{Item, ItemStack};
use mchprs_blocks::{BlockFace, BlockPos, SignType};
use mchprs_network::packets::clientbound::{COpenSignEditor, ClientBoundPacket};
use mchprs_redstone as redstone;
use mchprs_redstone::change_block;
use mchprs_utils::nbt_unwrap_val;
use mchprs_world::World;

Expand All @@ -17,34 +18,36 @@ pub fn on_use(
player: &mut Player,
pos: BlockPos,
item_in_hand: Option<Item>,
) -> ActionResult {
) -> (ActionResult, bool) {
if redstone::on_use(block, world, pos) {
return ActionResult::Success;
return (ActionResult::Success, true);
}

match block {
Block::SeaPickle { pickles } => {
if let Some(Item::SeaPickle {}) = item_in_hand {
let changed = if let Some(Item::SeaPickle {}) = item_in_hand {
if pickles < 4 {
world.set_block(
change_block(
world,
pos,
Block::SeaPickle {
pickles: pickles + 1,
},
);
}
}
ActionResult::Success
true
} else { false }
} else { false };
(ActionResult::Success, changed)
}
b if b.has_block_entity() => {
// Open container
let block_entity = world.get_block_entity(pos);
if let Some(BlockEntity::Container { inventory, ty, .. }) = block_entity {
player.open_container(inventory, *ty);
}
ActionResult::Success
(ActionResult::Success, false)
}
_ => ActionResult::Pass,
_ => (ActionResult::Pass, false),
}
}

Expand Down Expand Up @@ -110,6 +113,12 @@ pub fn get_state_for_placement(
lit: redstone::redstone_lamp_should_be_lit(world, pos),
},
Item::RedstoneBlock {} => Block::RedstoneBlock {},
Item::Observer {} => Block::RedstoneObserver {
observer: RedstoneObserver {
facing: context.player.get_facing(),
powered: false
}
},
Item::Hopper {} => Block::Hopper {},
Item::Terracotta {} => Block::Terracotta {},
Item::ColoredTerracotta { color } => Block::ColoredTerracotta { color },
Expand Down Expand Up @@ -218,7 +227,7 @@ pub fn place_in_world(
}
};
}
world.set_block(pos, block);
change_block(world, pos, block);
change_surrounding_blocks(world, pos);
if let Block::RedstoneWire { .. } = block {
redstone::update_wire_neighbors(world, pos);
Expand All @@ -234,12 +243,12 @@ pub fn destroy(block: Block, world: &mut impl World, pos: BlockPos) {

match block {
Block::RedstoneWire { .. } => {
world.set_block(pos, Block::Air {});
change_block(world, pos, Block::Air {});
change_surrounding_blocks(world, pos);
redstone::update_wire_neighbors(world, pos);
}
Block::Lever { lever } => {
world.set_block(pos, Block::Air {});
change_block(world, pos, Block::Air {});
// This is a horrible idea, don't do this.
// One day this will be fixed, but for now... too bad!
match lever.face {
Expand All @@ -264,7 +273,7 @@ pub fn destroy(block: Block, world: &mut impl World, pos: BlockPos) {
}
}
_ => {
world.set_block(pos, Block::Air {});
change_block(world, pos, Block::Air {});
change_surrounding_blocks(world, pos);
redstone::update_surrounding_blocks(world, pos);
}
Expand Down Expand Up @@ -334,7 +343,7 @@ pub fn change(block: Block, world: &mut impl World, pos: BlockPos, direction: Bl
}
if let Block::RedstoneWire { wire } = block {
let new_state = redstone::wire::on_neighbor_changed(wire, world, pos, direction);
if world.set_block(pos, Block::RedstoneWire { wire: new_state }) {
if change_block(world, pos, Block::RedstoneWire { wire: new_state }) {
redstone::update_wire_neighbors(world, pos);
}
}
Expand Down Expand Up @@ -402,7 +411,7 @@ pub fn use_item_on_block(
ctx.block_pos,
Some(item.item_type),
)
.is_success()
.0.is_success()
{
return false;
}
Expand Down
7 changes: 5 additions & 2 deletions crates/core/src/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,11 @@ impl Plot {
let block = self.world.get_block(pos);
match block {
Block::StonePressurePlate { .. } => {
self.world
.set_block(pos, Block::StonePressurePlate { powered });
mchprs_redstone::change_block(
&mut self.world,
pos,
Block::StonePressurePlate { powered }
);
mchprs_redstone::update_surrounding_blocks(&mut self.world, pos);
mchprs_redstone::update_surrounding_blocks(
&mut self.world,
Expand Down
6 changes: 2 additions & 4 deletions crates/redpiler/src/backend/direct/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ fn compile_node(
match weight.ty {
LinkType::Default => {
if default_input_count >= MAX_INPUTS {
panic!(
"Exceeded the maximum number of default inputs {}",
MAX_INPUTS
);
panic!("Exceeded the maximum number of default inputs {}", MAX_INPUTS);
}
default_input_count += 1;
default_inputs.ss_counts[ss as usize] += 1;
Expand Down Expand Up @@ -109,6 +106,7 @@ fn compile_node(
far_input: far_input.map(|value| NonMaxU8::new(value).unwrap()),
facing_diode: *facing_diode,
},
CNodeType::Observer => NodeType::Observer,
CNodeType::Lamp => NodeType::Lamp,
CNodeType::Button => NodeType::Button,
CNodeType::Lever => NodeType::Lever,
Expand Down
Loading