Skip to content

Commit

Permalink
tests: add tests for lever, trapdoor, lamp, and torches
Browse files Browse the repository at this point in the history
  • Loading branch information
StackDoubleFlow committed Sep 17, 2024
1 parent 62748da commit ab76784
Show file tree
Hide file tree
Showing 10 changed files with 517 additions and 101 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-appender = "0.2"
tracing = "0.1"

[dev-dependencies]
mchprs_world ={ path = "./crates/world" }
mchprs_blocks ={ path = "./crates/blocks" }
mchprs_redpiler ={ path = "./crates/redpiler" }
mchprs_redstone = { path = "./crates/redstone" }

[patch.crates-io]
hematite-nbt = { git = "https://github.com/StackDoubleFlow/hematite_nbt" }
3 changes: 2 additions & 1 deletion crates/blocks/src/blocks/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,9 @@ impl BlockTransform for RedstoneWire {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum TrapdoorHalf {
#[default]
Top,
Bottom,
}
Expand Down
100 changes: 6 additions & 94 deletions crates/core/src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use mchprs_blocks::blocks::*;
use mchprs_blocks::items::{Item, ItemStack};
use mchprs_blocks::{BlockFace, BlockPos, SignType};
use mchprs_network::packets::clientbound::{COpenSignEditor, ClientBoundPacket};
use mchprs_redstone::{self as redstone, noteblock};
use mchprs_redstone as redstone;
use mchprs_utils::nbt_unwrap_val;
use mchprs_world::{TickPriority, World};
use mchprs_world::World;

pub fn on_use(
block: Block,
Expand All @@ -18,80 +18,11 @@ pub fn on_use(
pos: BlockPos,
item_in_hand: Option<Item>,
) -> ActionResult {
if redstone::on_use(block, world, pos) {
return ActionResult::Success;
}

match block {
Block::RedstoneRepeater { repeater } => {
let mut repeater = repeater;
repeater.delay += 1;
if repeater.delay > 4 {
repeater.delay -= 4;
}
world.set_block(pos, Block::RedstoneRepeater { repeater });
ActionResult::Success
}
Block::RedstoneComparator { comparator } => {
let mut comparator = comparator;
comparator.mode = comparator.mode.toggle();
redstone::comparator::tick(comparator, world, pos);
world.set_block(pos, Block::RedstoneComparator { comparator });
ActionResult::Success
}
Block::Lever { mut lever } => {
lever.powered = !lever.powered;
world.set_block(pos, Block::Lever { lever });
redstone::update_surrounding_blocks(world, pos);
match lever.face {
LeverFace::Ceiling => {
redstone::update_surrounding_blocks(world, pos.offset(BlockFace::Top));
}
LeverFace::Floor => {
redstone::update_surrounding_blocks(world, pos.offset(BlockFace::Bottom));
}
LeverFace::Wall => redstone::update_surrounding_blocks(
world,
pos.offset(lever.facing.opposite().block_face()),
),
}
ActionResult::Success
}
Block::StoneButton { mut button } => {
if !button.powered {
button.powered = true;
world.set_block(pos, Block::StoneButton { button });
world.schedule_tick(pos, 10, TickPriority::Normal);
redstone::update_surrounding_blocks(world, pos);
match button.face {
ButtonFace::Ceiling => {
redstone::update_surrounding_blocks(world, pos.offset(BlockFace::Top));
}
ButtonFace::Floor => {
redstone::update_surrounding_blocks(world, pos.offset(BlockFace::Bottom));
}
ButtonFace::Wall => redstone::update_surrounding_blocks(
world,
pos.offset(button.facing.opposite().block_face()),
),
}
}
ActionResult::Success
}
Block::RedstoneWire { wire } => {
use redstone::wire;
if wire::is_dot(wire) || wire::is_cross(wire) {
let mut new_wire = if wire::is_cross(wire) {
RedstoneWire::default()
} else {
wire::make_cross(0)
};
new_wire.power = wire.power;
new_wire = wire::get_regulated_sides(new_wire, world, pos);
if wire != new_wire {
world.set_block(pos, Block::RedstoneWire { wire: new_wire });
redstone::update_wire_neighbors(world, pos);
return ActionResult::Success;
}
}
ActionResult::Pass
}
Block::SeaPickle { pickles } => {
if let Some(Item::SeaPickle {}) = item_in_hand {
if pickles < 4 {
Expand All @@ -105,25 +36,6 @@ pub fn on_use(
}
ActionResult::Success
}
Block::NoteBlock { note, powered, .. } => {
let note = (note + 1) % 25;
let instrument = noteblock::get_noteblock_instrument(world, pos);

world.set_block(
pos,
Block::NoteBlock {
instrument,
note,
powered,
},
);

if noteblock::is_noteblock_unblocked(world, pos) {
noteblock::play_note(world, pos, instrument, note);
}

ActionResult::Success
}
b if b.has_block_entity() => {
// Open container
let block_entity = world.get_block_entity(pos);
Expand Down
4 changes: 2 additions & 2 deletions crates/network/src/packets/clientbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,15 +1051,15 @@ impl ClientBoundPacket for CSetHeadRotation {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CUpdateSectionBlocksRecord {
pub x: u8,
pub y: u8,
pub z: u8,
pub block_id: u32,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CUpdateSectionBlocks {
pub chunk_x: i32,
pub chunk_z: i32,
Expand Down
6 changes: 3 additions & 3 deletions crates/redpiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn block_powered_mut(block: &mut Block) -> Option<&mut bool> {
})
}

#[derive(Default, PartialEq, Eq, Debug)]
#[derive(Default, PartialEq, Eq, Debug, Clone)]
pub struct CompilerOptions {
/// Enable optimization passes which may significantly increase compile times.
pub optimize: bool,
Expand All @@ -50,7 +50,7 @@ pub struct CompilerOptions {
pub backend_variant: BackendVariant,
}

#[derive(Debug, Default, PartialEq, Eq)]
#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
pub enum BackendVariant {
#[default]
Direct,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Compiler {

pub fn compile<W: World>(
&mut self,
world: &mut W,
world: &W,
bounds: (BlockPos, BlockPos),
options: CompilerOptions,
ticks: Vec<TickEntry>,
Expand Down
100 changes: 99 additions & 1 deletion crates/redstone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod repeater;
pub mod wire;

use mchprs_blocks::block_entities::BlockEntity;
use mchprs_blocks::blocks::{Block, ButtonFace, LeverFace};
use mchprs_blocks::blocks::{Block, ButtonFace, LeverFace, RedstoneWire};
use mchprs_blocks::{BlockDirection, BlockFace, BlockPos};
use mchprs_world::TickPriority;
use mchprs_world::World;
Expand Down Expand Up @@ -351,3 +351,101 @@ pub fn is_diode(block: Block) -> bool {
Block::RedstoneRepeater { .. } | Block::RedstoneComparator { .. }
)
}

/// Returns true if the action was handled
pub fn on_use(block: Block, world: &mut impl World, pos: BlockPos) -> bool {
match block {
Block::RedstoneRepeater { repeater } => {
let mut repeater = repeater;
repeater.delay += 1;
if repeater.delay > 4 {
repeater.delay -= 4;
}
world.set_block(pos, Block::RedstoneRepeater { repeater });
true
}
Block::RedstoneComparator { comparator } => {
let mut comparator = comparator;
comparator.mode = comparator.mode.toggle();
comparator::tick(comparator, world, pos);
world.set_block(pos, Block::RedstoneComparator { comparator });
true
}
Block::Lever { mut lever } => {
lever.powered = !lever.powered;
world.set_block(pos, Block::Lever { lever });
update_surrounding_blocks(world, pos);
match lever.face {
LeverFace::Ceiling => {
update_surrounding_blocks(world, pos.offset(BlockFace::Top));
}
LeverFace::Floor => {
update_surrounding_blocks(world, pos.offset(BlockFace::Bottom));
}
LeverFace::Wall => update_surrounding_blocks(
world,
pos.offset(lever.facing.opposite().block_face()),
),
}
true
}
Block::StoneButton { mut button } => {
if !button.powered {
button.powered = true;
world.set_block(pos, Block::StoneButton { button });
world.schedule_tick(pos, 10, TickPriority::Normal);
update_surrounding_blocks(world, pos);
match button.face {
ButtonFace::Ceiling => {
update_surrounding_blocks(world, pos.offset(BlockFace::Top));
}
ButtonFace::Floor => {
update_surrounding_blocks(world, pos.offset(BlockFace::Bottom));
}
ButtonFace::Wall => update_surrounding_blocks(
world,
pos.offset(button.facing.opposite().block_face()),
),
}
}
true
}
Block::RedstoneWire { wire } => {
if wire::is_dot(wire) || wire::is_cross(wire) {
let mut new_wire = if wire::is_cross(wire) {
RedstoneWire::default()
} else {
wire::make_cross(0)
};
new_wire.power = wire.power;
new_wire = wire::get_regulated_sides(new_wire, world, pos);
if wire != new_wire {
world.set_block(pos, Block::RedstoneWire { wire: new_wire });
update_wire_neighbors(world, pos);
return true;
}
}
false
}
Block::NoteBlock { note, powered, .. } => {
let note = (note + 1) % 25;
let instrument = noteblock::get_noteblock_instrument(world, pos);

world.set_block(
pos,
Block::NoteBlock {
instrument,
note,
powered,
},
);

if noteblock::is_noteblock_unblocked(world, pos) {
noteblock::play_note(world, pos, instrument, note);
}

true
}
_ => false,
}
}
2 changes: 2 additions & 0 deletions crates/world/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl PalettedBitBuffer {
}
}

#[derive(Clone)]
pub struct ChunkSection {
buffer: PalettedBitBuffer,
block_count: u32,
Expand Down Expand Up @@ -391,6 +392,7 @@ impl Default for ChunkSection {
}
}

#[derive(Clone)]
pub struct Chunk {
pub sections: Vec<ChunkSection>,
pub x: i32,
Expand Down
Loading

0 comments on commit ab76784

Please sign in to comment.