1
0

Use pitch lookup in noteblock block entity (#4826)

- Removed the calculation in the noteblock block entity

I did the calculation in python if anyone is interested where the numbers are from

Co-authored-by: 12xx12 <12xx12100@gmail.com>
This commit is contained in:
12xx12 2020-09-05 17:07:01 +02:00 committed by GitHub
parent 040380321c
commit 2d197e147e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 220 additions and 50 deletions

View File

@ -1415,14 +1415,43 @@ World:ForEachChestInChunk(Player:GetChunkX(), Player:GetChunkZ(),
cNoteEntity =
{
Desc = [[
This class represents a note block entity in the world. It takes care of the note block's pitch,
This class represents a note block entity in the world. It takes care of the note block's note,
and also can play the sound, either when the {{cPlayer|player}} right-clicks it, redstone activates
it, or upon a plugin's request.</p>
<p>
The pitch is stored as an integer between 0 and 24.
The note is stored as an integer between 0 and 24.
]],
Functions =
{
GetNote =
{
Returns =
{
{
Type = "number",
},
},
Notes = "Returns the current note set for the block",
},
IncrementNote =
{
Notes = "Adds 1 to the current note. Wraps around to 0 when the note cannot go any higher.",
},
MakeSound =
{
Notes = "Plays the sound for all {{cClientHandle|clients}} near this block.",
},
SetNote =
{
Params =
{
{
Name = "Note",
Type = "number",
},
},
Notes = "Sets a new note for the block.",
},
GetPitch =
{
Returns =
@ -1450,7 +1479,7 @@ World:ForEachChestInChunk(Player:GetChunkX(), Player:GetChunkZ(),
Type = "number",
},
},
Notes = "Sets a new pitch for the block.",
Notes = "Sets a new note for the block.",
},
},
Inherits = "cBlockEntity",

View File

@ -10,6 +10,7 @@
#include "../Entities/Player.h"
#include "LuaState.h"
#include "../BlockInfo.h"
#include "../BlockEntities/NoteEntity.h"
@ -468,6 +469,105 @@ static int tolua_set_cItem_m_Lore(lua_State * tolua_S)
/** function: cNoteEntity: GetNote */
static int tolua_cNoteEntity_GetPitch(lua_State * tolua_S)
{
cLuaState LuaState(tolua_S);
if (
!LuaState.CheckParamUserType(1, "cNoteEntity") ||
!LuaState.CheckParamEnd(2)
)
{
return 0;
}
cNoteEntity * Self = nullptr;
if (!LuaState.GetStackValues(1, Self))
{
tolua_error(LuaState, "Failed to read parameters", nullptr);
}
if (Self == nullptr)
{
tolua_error(LuaState, "invalid 'self' in function 'GetPitch'", nullptr);
}
LuaState.Push(Self->GetNote());
LOGWARNING("Warning: 'cNoteEntity:GetPitch' function is deprecated. Please use 'cNoteEntity:GetNote' instead.");
LuaState.LogStackTrace(0);
return 1;
}
/** function: cNoteEntity: IncrementNote */
static int tolua_cNoteEntity_IncrementPitch(lua_State * tolua_S)
{
cLuaState LuaState(tolua_S);
if (
!LuaState.CheckParamUserType(1, "cNoteEntity") ||
!LuaState.CheckParamEnd(2)
)
{
return 0;
}
cNoteEntity * Self = nullptr;
if (!LuaState.GetStackValues(1, Self))
{
tolua_error(LuaState, "Failed to read parameters", nullptr);
}
if (Self == nullptr)
{
tolua_error(LuaState, "invalid 'self' in function 'SetPitch'", nullptr);
}
Self->IncrementNote();
LOGWARNING("Warning: 'cNoteEntity:IncrementPitch' function is deprecated. Please use 'cNoteEntity:IncrementNote' instead.");
LuaState.LogStackTrace(0);
return 1;
}
/** function: cNoteEntity: SetNote */
static int tolua_cNoteEntity_SetPitch(lua_State * tolua_S)
{
cLuaState LuaState(tolua_S);
if (
!LuaState.CheckParamUserType(1, "cNoteEntity") ||
!LuaState.CheckParamNumber(2) ||
!LuaState.CheckParamEnd(3)
)
{
return 0;
}
cNoteEntity * Self = nullptr;
int Note = -1;
if (!LuaState.GetStackValues(1, Self, Note))
{
tolua_error(LuaState, "Failed to read parameters", nullptr);
}
if (Self == nullptr)
{
tolua_error(LuaState, "invalid 'self' in function 'SetPitch'", nullptr);
}
Self->SetNote(Note % 25);
LOGWARNING("Warning: 'cNoteEntity:SetPitch' function is deprecated. Please use 'cNoteEntity:SetNote' instead.");
LuaState.LogStackTrace(0);
return 1;
}
/** function: cWorld:SetSignLines */
static int tolua_cWorld_SetSignLines(lua_State * tolua_S)
@ -708,6 +808,12 @@ void DeprecatedBindings::Bind(lua_State * tolua_S)
tolua_variable(tolua_S, "m_Lore", tolua_get_cItem_m_Lore, tolua_set_cItem_m_Lore);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cNoteEntity");
tolua_function(tolua_S, "GetPitch", tolua_cNoteEntity_GetPitch);
tolua_function(tolua_S, "IncrementPitch", tolua_cNoteEntity_IncrementPitch);
tolua_function(tolua_S, "SetPitch", tolua_cNoteEntity_SetPitch);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cWorld");
tolua_function(tolua_S, "GrowTree", tolua_cWorld_GrowTree);
tolua_function(tolua_S, "GrowTreeByBiome", tolua_cWorld_GrowTreeByBiome);

View File

@ -11,7 +11,7 @@
cNoteEntity::cNoteEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
Super(a_BlockType, a_BlockMeta, a_Pos, a_World),
m_Pitch(0)
m_Note(0)
{
ASSERT(a_BlockType == E_BLOCK_NOTE_BLOCK);
}
@ -24,7 +24,7 @@ void cNoteEntity::CopyFrom(const cBlockEntity & a_Src)
{
Super::CopyFrom(a_Src);
auto & src = static_cast<const cNoteEntity &>(a_Src);
m_Pitch = src.m_Pitch;
m_Note = src.m_Note;
}
@ -34,7 +34,7 @@ void cNoteEntity::CopyFrom(const cBlockEntity & a_Src)
bool cNoteEntity::UsedBy(cPlayer * a_Player)
{
UNUSED(a_Player);
IncrementPitch();
IncrementNote();
MakeSound();
return true;
}
@ -45,8 +45,8 @@ bool cNoteEntity::UsedBy(cPlayer * a_Player)
void cNoteEntity::MakeSound(void)
{
char instrument;
AString sampleName;
char Instrument;
AString SampleName;
switch (m_World->GetBlock(m_Pos.addedY(-1)))
{
@ -96,8 +96,8 @@ void cNoteEntity::MakeSound(void)
case E_BLOCK_WOODEN_PRESSURE_PLATE:
case E_BLOCK_WOODEN_SLAB:
{
instrument = E_INST_DOUBLE_BASS;
sampleName = "block.note.bass";
Instrument = E_INST_DOUBLE_BASS;
SampleName = "block.note.bass";
break;
}
@ -105,8 +105,8 @@ void cNoteEntity::MakeSound(void)
case E_BLOCK_SAND:
case E_BLOCK_SOULSAND:
{
instrument = E_INST_SNARE_DRUM;
sampleName = "block.note.snare";
Instrument = E_INST_SNARE_DRUM;
SampleName = "block.note.snare";
break;
}
@ -118,8 +118,8 @@ void cNoteEntity::MakeSound(void)
case E_BLOCK_STAINED_GLASS:
case E_BLOCK_STAINED_GLASS_PANE:
{
instrument = E_INST_CLICKS;
sampleName = "block.note.hat";
Instrument = E_INST_CLICKS;
SampleName = "block.note.hat";
break;
}
@ -195,63 +195,61 @@ void cNoteEntity::MakeSound(void)
case E_BLOCK_WHITE_SHULKER_BOX:
case E_BLOCK_YELLOW_SHULKER_BOX:
{
instrument = E_INST_BASS_DRUM;
sampleName = "block.note.basedrum";
Instrument = E_INST_BASS_DRUM;
SampleName = "block.note.basedrum";
break;
}
case E_BLOCK_CLAY:
{
instrument = E_INST_FLUTE;
sampleName = "block.note.flute";
Instrument = E_INST_FLUTE;
SampleName = "block.note.flute";
break;
}
case E_BLOCK_GOLD_BLOCK:
{
instrument = E_INST_BELL;
sampleName = "block.note.bell";
Instrument = E_INST_BELL;
SampleName = "block.note.bell";
break;
}
case E_BLOCK_WOOL:
{
instrument = E_INST_GUITAR;
sampleName = "block.note.guitar";
Instrument = E_INST_GUITAR;
SampleName = "block.note.guitar";
break;
}
case E_BLOCK_PACKED_ICE:
{
instrument = E_INST_CHIME;
sampleName = "block.note.chime";
Instrument = E_INST_CHIME;
SampleName = "block.note.chime";
break;
}
case E_BLOCK_BONE_BLOCK:
{
instrument = E_INST_XYLOPHONE;
sampleName = "block.note.xylophone";
Instrument = E_INST_XYLOPHONE;
SampleName = "block.note.xylophone";
break;
}
default:
{
instrument = E_INST_HARP_PIANO;
sampleName = "block.note.harp";
Instrument = E_INST_HARP_PIANO;
SampleName = "block.note.harp";
break;
}
}
m_World->BroadcastBlockAction(m_Pos, static_cast<Byte>(instrument), static_cast<Byte>(m_Pitch), E_BLOCK_NOTE_BLOCK);
m_World->BroadcastBlockAction(m_Pos, static_cast<Byte>(Instrument), static_cast<Byte>(m_Note), E_BLOCK_NOTE_BLOCK);
// TODO: instead of calculating the power function over and over, make a precalculated table - there's only 24 pitches after all
float calcPitch = static_cast<float>(pow(2.0f, static_cast<float>(m_Pitch - 12.0f) / 12.0f));
m_World->BroadcastSoundEffect(
sampleName,
SampleName,
m_Pos,
3.0f,
calcPitch
PitchFromNote(m_Note)
);
}
@ -259,29 +257,66 @@ void cNoteEntity::MakeSound(void)
char cNoteEntity::GetPitch(void)
unsigned char cNoteEntity::GetNote(void)
{
return m_Pitch;
return m_Note;
}
void cNoteEntity::SetPitch(char a_Pitch)
void cNoteEntity::SetNote(unsigned char a_Note)
{
m_Pitch = a_Pitch % 25;
m_Note = a_Note % 25;
}
void cNoteEntity::IncrementPitch(void)
void cNoteEntity::IncrementNote(void)
{
SetPitch(m_Pitch + 1);
SetNote(m_Note + 1);
}
float cNoteEntity::PitchFromNote(unsigned char a_Pitch)
{
// This replaces the calculation of:
// float calcPitch = static_cast<float>(pow(2.0f, static_cast<float>(m_Note - 12.0f) / 12.0f));
// So 2 ^ ((m_Note - 12) / 12)
switch (a_Pitch)
{
case 0: return 0.5f;
case 1: return 0.5297315471796477f;
case 2: return 0.5612310241546865f;
case 3: return 0.5946035575013605f;
case 4: return 0.6299605249474366f;
case 5: return 0.6674199270850172f;
case 6: return 0.7071067811865476f;
case 7: return 0.7491535384383408f;
case 8: return 0.7937005259840998f;
case 9: return 0.8408964152537145f;
case 10: return 0.8908987181403393f;
case 11: return 0.9438743126816935f;
case 12: return 1.0f;
case 13: return 1.0594630943592953f;
case 14: return 1.122462048309373f;
case 15: return 1.189207115002721f;
case 16: return 1.2599210498948732f;
case 17: return 1.3348398541700344f;
case 18: return 1.4142135623730951f;
case 19: return 1.4983070768766815f;
case 20: return 1.5874010519681994f;
case 21: return 1.681792830507429f;
case 22: return 1.7817974362806785f;
case 23: return 1.887748625363387f;
case 24: return 2.0f;
}
UNREACHABLE("Converted unknown pitch value");
}

View File

@ -40,13 +40,12 @@ public: // tolua_export
/** Creates a new note entity. a_World may be nullptr */
cNoteEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World);
virtual ~cNoteEntity() override {}
// tolua_begin
char GetPitch(void);
void SetPitch(char a_Pitch);
void IncrementPitch(void);
unsigned char GetNote(void);
void SetNote(unsigned char a_Note);
void IncrementNote(void);
void MakeSound(void);
// tolua_end
@ -56,10 +55,11 @@ public: // tolua_export
virtual bool UsedBy(cPlayer * a_Player) override;
virtual void SendTo(cClientHandle &) override {}
/** Returns the relative pitch (used in the protocol)
from a note value between 0 and 24 (used in m_Note). */
static float PitchFromNote(unsigned char a_Note);
private:
char m_Pitch;
unsigned char m_Note;
} ; // tolua_export

View File

@ -532,7 +532,7 @@ public:
{
mWriter.BeginCompound("");
AddBasicTileEntity(a_Note, "Music");
mWriter.AddByte("note", static_cast<Byte>(a_Note->GetPitch()));
mWriter.AddByte("note", static_cast<Byte>(a_Note->GetNote()));
mWriter.EndCompound();
}

View File

@ -1419,7 +1419,7 @@ OwnedBlockEntity cWSSAnvil::LoadNoteBlockFromNBT(const cParsedNBT & a_NBT, int a
int note = a_NBT.FindChildByName(a_TagIdx, "note");
if (note >= 0)
{
NoteBlock->SetPitch(static_cast<char>(a_NBT.GetByte(note)));
NoteBlock->SetNote(static_cast<char>(a_NBT.GetByte(note)));
}
return NoteBlock;
}