Skip to content

Commit

Permalink
Replace noteblock note to pitch function with LUT
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul1365972 committed Jan 9, 2024
1 parent f29b868 commit b4c7ba2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 0 additions & 4 deletions crates/blocks/src/blocks/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,3 @@ impl FromStr for Instrument {
})
}
}

pub fn noteblock_note_to_pitch(note: u32) -> f32 {
f32::powf(2.0, (note as f32 - 12.0) / 12.0)
}
2 changes: 1 addition & 1 deletion crates/core/src/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl World for PlotWorld {
volume: f32,
pitch: f32,
) {
// We do not know the players location here, so we send the sound packet to all players
// FIXME: We do not know the players location here, so we send the sound packet to all players
// A notchian server would only send to players in hearing distance (volume.clamp(0.0, 1.0) * 16.0)
let sound_effect_data = CSoundEffect {
sound_id,
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/redpiler/backend/direct/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::*;
#[inline(always)]
pub(super) fn update_node(
scheduler: &mut TickScheduler,
sound_events: &mut Vec<Event>,
events: &mut Vec<Event>,
nodes: &mut Nodes,
node_id: NodeId,
) {
Expand Down Expand Up @@ -117,7 +117,7 @@ pub(super) fn update_node(
let node = &mut nodes[node_id];
set_node(node, should_be_powered);
if should_be_powered {
sound_events.push(Event::NoteBlockPlay(node_id));
events.push(Event::NoteBlockPlay(node_id));
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions crates/core/src/redstone/noteblock.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use mchprs_blocks::blocks::{noteblock_note_to_pitch, Block, Instrument};
use mchprs_blocks::blocks::{Block, Instrument};
use mchprs_blocks::{BlockFace, BlockPos};

use crate::world::World;

// LUT generated via f32::powf(2.0, (note as f32 - 12.0) / 12.0)
// This is hardcoded because at this point floating point operations are not allowed in const contexts
const PITCHES_TABLE: [f32; 25] = [
0.5, 0.5297315, 0.561231, 0.59460354, 0.62996054, 0.6674199, 0.70710677, 0.74915355, 0.7937005,
0.8408964, 0.8908987, 0.9438743, 1.0, 1.0594631, 1.122462, 1.1892071, 1.2599211, 1.3348398,
1.4142135, 1.4983071, 1.587401, 1.6817929, 1.7817974, 1.8877486, 2.0,
];

pub fn is_noteblock_unblocked(world: &impl World, pos: BlockPos) -> bool {
matches!(world.get_block(pos.offset(BlockFace::Top)), Block::Air {})
}
Expand All @@ -17,6 +25,6 @@ pub fn play_note(world: &mut impl World, pos: BlockPos, instrument: Instrument,
instrument.to_sound_id(),
2, // Sound Caregory ID for Records
3.0,
noteblock_note_to_pitch(note),
PITCHES_TABLE[note as usize],
);
}

0 comments on commit b4c7ba2

Please sign in to comment.