Skip to content

Commit

Permalink
Deduplicate noteblock logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul1365972 committed Dec 14, 2023
1 parent 16a211e commit 3c113bc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
33 changes: 17 additions & 16 deletions crates/blocks/src/blocks/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,23 +497,24 @@ impl Instrument {
impl ToString for Instrument {
fn to_string(&self) -> String {
match self {
Instrument::Harp => "harp".to_owned(),
Instrument::Basedrum => "basedrum".to_owned(),
Instrument::Snare => "snare".to_owned(),
Instrument::Hat => "hat".to_owned(),
Instrument::Bass => "bass".to_owned(),
Instrument::Flute => "flute".to_owned(),
Instrument::Bell => "bell".to_owned(),
Instrument::Guitar => "guitar".to_owned(),
Instrument::Chime => "chime".to_owned(),
Instrument::Xylophone => "xylophone".to_owned(),
Instrument::IronXylophone => "iron_xylophone".to_owned(),
Instrument::CowBell => "cow_bell".to_owned(),
Instrument::Didgeridoo => "didgeridoo".to_owned(),
Instrument::Bit => "bit".to_owned(),
Instrument::Banjo => "banjo".to_owned(),
Instrument::Pling => "pling".to_owned(),
Instrument::Harp => "harp",
Instrument::Basedrum => "basedrum",
Instrument::Snare => "snare",
Instrument::Hat => "hat",
Instrument::Bass => "bass",
Instrument::Flute => "flute",
Instrument::Bell => "bell",
Instrument::Guitar => "guitar",
Instrument::Chime => "chime",
Instrument::Xylophone => "xylophone",
Instrument::IronXylophone => "iron_xylophone",
Instrument::CowBell => "cow_bell",
Instrument::Didgeridoo => "didgeridoo",
Instrument::Bit => "bit",
Instrument::Banjo => "banjo",
Instrument::Pling => "pling",
}
.to_owned()
}
}

Expand Down
11 changes: 3 additions & 8 deletions crates/core/src/redpiler/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
use super::JITBackend;
use crate::redpiler::compile_graph::{CompileGraph, LinkType, NodeIdx};
use crate::redpiler::{block_powered_mut, bool_to_ss};
use crate::redstone::noteblock;
use crate::world::World;
use mchprs_blocks::block_entities::BlockEntity;
use mchprs_blocks::blocks::{noteblock_note_to_pitch, Block, ComparatorMode, Instrument};
use mchprs_blocks::blocks::{Block, ComparatorMode, Instrument};
use mchprs_blocks::BlockPos;
use mchprs_world::{TickEntry, TickPriority};
use nodes::{NodeId, Nodes};
Expand Down Expand Up @@ -641,13 +642,7 @@ impl JITBackend for DirectBackend {
match event {
Event::NoteBlockPlay(node_id) => {
let &(pos, instrument, note) = self.noteblock_map.get(&node_id).unwrap();
world.play_sound(
pos,
instrument.to_sound_id(),
2, // Sound Caregory ID for Records
3.0,
noteblock_note_to_pitch(note),
);
noteblock::play_note(world, pos, instrument, note);
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions crates/core/src/redpiler/passes/identify_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use super::Pass;
use crate::redpiler::compile_graph::{CompileGraph, CompileNode, NodeState, NodeType};
use crate::redpiler::{CompilerInput, CompilerOptions};
use crate::redstone;
use crate::redstone::{self, noteblock};
use crate::world::{for_each_block_optimized, World};
use mchprs_blocks::block_entities::BlockEntity;
use mchprs_blocks::blocks::{Block, Instrument, RedstoneComparator, RedstoneRepeater};
use mchprs_blocks::{BlockFace, BlockPos};
use mchprs_blocks::blocks::{Block, RedstoneComparator, RedstoneRepeater};
use mchprs_blocks::BlockPos;

pub struct IdentifyNodes;

Expand Down Expand Up @@ -117,9 +117,8 @@ fn identify_block<W: World>(
instrument: _,
note,
powered,
} if world.get_block(pos.offset(BlockFace::Top)) == (Block::Air {}) => {
let below = world.get_block(pos.offset(BlockFace::Bottom));
let instrument = Instrument::from_block_below(below);
} if noteblock::is_noteblock_unblocked(world, pos) => {
let instrument = noteblock::get_noteblock_instrument(world, pos);
(
NodeType::NoteBlock { instrument, note },
NodeState::simple(powered),
Expand Down
16 changes: 5 additions & 11 deletions crates/core/src/redstone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
//! scenerio (i.e. regular buiding)
pub mod comparator;
pub mod noteblock;
pub mod repeater;
pub mod wire;

use crate::world::World;
use mchprs_blocks::block_entities::BlockEntity;
use mchprs_blocks::blocks::{noteblock_note_to_pitch, Block, ButtonFace, Instrument, LeverFace};
use mchprs_blocks::blocks::{Block, ButtonFace, LeverFace};
use mchprs_blocks::{BlockDirection, BlockFace, BlockPos};
use mchprs_world::TickPriority;

Expand Down Expand Up @@ -225,8 +226,7 @@ pub fn update(block: Block, world: &mut impl World, pos: BlockPos) {
let should_be_powered = redstone_lamp_should_be_lit(world, pos);
if powered != should_be_powered {
// Hack: Update the instrument only just before the noteblock is updated
let instrument =
Instrument::from_block_below(world.get_block(pos.offset(BlockFace::Bottom)));
let instrument = noteblock::get_noteblock_instrument(world, pos);
let new_block = Block::NoteBlock {
instrument,
note,
Expand All @@ -241,15 +241,9 @@ pub fn update(block: Block, world: &mut impl World, pos: BlockPos) {

if should_be_powered
&& is_not_powered
&& world.get_block(pos.offset(BlockFace::Top)) == (Block::Air {})
&& noteblock::is_noteblock_unblocked(world, pos)
{
world.play_sound(
pos,
instrument.to_sound_id(),
2, // Sound Caregory ID for Records
3.0,
noteblock_note_to_pitch(note),
);
noteblock::play_note(world, pos, instrument, note);
}
world.set_block(pos, new_block);
}
Expand Down
22 changes: 22 additions & 0 deletions crates/core/src/redstone/noteblock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use mchprs_blocks::blocks::{noteblock_note_to_pitch, Block, Instrument};
use mchprs_blocks::{BlockFace, BlockPos};

use crate::world::World;

pub fn is_noteblock_unblocked(world: &impl World, pos: BlockPos) -> bool {
matches!(world.get_block(pos.offset(BlockFace::Top)), Block::Air {})
}

pub fn get_noteblock_instrument(world: &impl World, pos: BlockPos) -> Instrument {
Instrument::from_block_below(world.get_block(pos.offset(BlockFace::Bottom)))
}

pub fn play_note(world: &mut impl World, pos: BlockPos, instrument: Instrument, note: u32) {
world.play_sound(
pos,
instrument.to_sound_id(),
2, // Sound Caregory ID for Records
3.0,
noteblock_note_to_pitch(note),
);
}

0 comments on commit 3c113bc

Please sign in to comment.