Cuberite
A lightweight, fast and extensible game server for Minecraft
NoteEntity.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "NoteEntity.h"
5 #include "../World.h"
6 #include "json/value.h"
7 #include "../Entities/Player.h"
8 
9 
10 
11 
12 
13 cNoteEntity::cNoteEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
14  Super(a_BlockType, a_BlockMeta, a_Pos, a_World),
15  m_Note(0)
16 {
17  ASSERT(a_BlockType == E_BLOCK_NOTE_BLOCK);
18 }
19 
20 
21 
22 
23 
25 {
26  Super::CopyFrom(a_Src);
27  auto & src = static_cast<const cNoteEntity &>(a_Src);
28  m_Note = src.m_Note;
29 }
30 
31 
32 
33 
34 
35 bool cNoteEntity::UsedBy(cPlayer * a_Player)
36 {
38  IncrementNote();
39  MakeSound();
40  return true;
41 }
42 
43 
44 
45 
46 
48 {
49  char Instrument;
50  AString SampleName;
51 
52  switch (m_World->GetBlock(m_Pos.addedY(-1)))
53  {
58  case E_BLOCK_BIRCH_DOOR:
62  case E_BLOCK_BOOKCASE:
63  case E_BLOCK_CHEST:
71  case E_BLOCK_FENCE:
75  case E_BLOCK_JUKEBOX:
80  case E_BLOCK_LOG:
81  case E_BLOCK_NEW_LOG:
82  case E_BLOCK_NOTE_BLOCK:
83  case E_BLOCK_OAK_DOOR:
86  case E_BLOCK_PLANKS:
87  case E_BLOCK_SIGN_POST:
93  case E_BLOCK_TRAPDOOR:
96  case E_BLOCK_WALLSIGN:
99  {
101  SampleName = "block.note.bass";
102  break;
103  }
104 
105  case E_BLOCK_GRAVEL:
106  case E_BLOCK_SAND:
107  case E_BLOCK_SOULSAND:
108  {
110  SampleName = "block.note.snare";
111  break;
112  }
113 
114  case E_BLOCK_BEACON:
115  case E_BLOCK_GLASS:
116  case E_BLOCK_GLASS_PANE:
117  case E_BLOCK_GLOWSTONE:
118  case E_BLOCK_SEA_LANTERN:
121  {
123  SampleName = "block.note.hat";
124  break;
125  }
126 
127  case E_BLOCK_BEDROCK:
131  case E_BLOCK_BRICK:
134  case E_BLOCK_COAL_ORE:
135  case E_BLOCK_COBBLESTONE:
139  case E_BLOCK_DIAMOND_ORE:
140  case E_BLOCK_DISPENSER:
143  case E_BLOCK_DROPPER:
144  case E_BLOCK_EMERALD_ORE:
146  case E_BLOCK_END_BRICKS:
148  case E_BLOCK_END_STONE:
149  case E_BLOCK_ENDER_CHEST:
150  case E_BLOCK_FURNACE:
151  case E_BLOCK_GOLD_ORE:
154  case E_BLOCK_IRON_ORE:
155  case E_BLOCK_LAPIS_ORE:
159  case E_BLOCK_LIT_FURNACE:
161  case E_BLOCK_MAGMA:
162  case E_BLOCK_MOB_SPAWNER:
168  case E_BLOCK_NETHERRACK:
169  case E_BLOCK_OBSERVER:
170  case E_BLOCK_OBSIDIAN:
178  case E_BLOCK_PURPUR_SLAB:
189  case E_BLOCK_SANDSTONE:
191  case E_BLOCK_STONE:
195  case E_BLOCK_STONE_SLAB:
198  {
200  SampleName = "block.note.basedrum";
201  break;
202  }
203 
204  case E_BLOCK_CLAY:
205  {
207  SampleName = "block.note.flute";
208  break;
209  }
210 
211  case E_BLOCK_GOLD_BLOCK:
212  {
214  SampleName = "block.note.bell";
215  break;
216  }
217 
218  case E_BLOCK_WOOL:
219  {
221  SampleName = "block.note.guitar";
222  break;
223  }
224 
225  case E_BLOCK_PACKED_ICE:
226  {
228  SampleName = "block.note.chime";
229  break;
230  }
231 
232  case E_BLOCK_BONE_BLOCK:
233  {
235  SampleName = "block.note.xylophone";
236  break;
237  }
238 
239  default:
240  {
242  SampleName = "block.note.harp";
243  break;
244  }
245  }
246 
248 
250  SampleName,
251  m_Pos,
252  3.0f,
254  );
255 }
256 
257 
258 
259 
260 
261 unsigned char cNoteEntity::GetNote(void)
262 {
263  return m_Note;
264 }
265 
266 
267 
268 
269 
270 void cNoteEntity::SetNote(unsigned char a_Note)
271 {
272  m_Note = a_Note % 25;
273 }
274 
275 
276 
277 
278 
280 {
281  SetNote(m_Note + 1);
282 }
283 
284 
285 
286 
287 
288 float cNoteEntity::PitchFromNote(unsigned char a_Pitch)
289 {
290  // This replaces the calculation of:
291  // float calcPitch = static_cast<float>(pow(2.0f, static_cast<float>(m_Note - 12.0f) / 12.0f));
292  // So 2 ^ ((m_Note - 12) / 12)
293  switch (a_Pitch)
294  {
295  case 0: return 0.5f;
296  case 1: return 0.5297315471796477f;
297  case 2: return 0.5612310241546865f;
298  case 3: return 0.5946035575013605f;
299  case 4: return 0.6299605249474366f;
300  case 5: return 0.6674199270850172f;
301  case 6: return 0.7071067811865476f;
302  case 7: return 0.7491535384383408f;
303  case 8: return 0.7937005259840998f;
304  case 9: return 0.8408964152537145f;
305  case 10: return 0.8908987181403393f;
306  case 11: return 0.9438743126816935f;
307  case 12: return 1.0f;
308  case 13: return 1.0594630943592953f;
309  case 14: return 1.122462048309373f;
310  case 15: return 1.189207115002721f;
311  case 16: return 1.2599210498948732f;
312  case 17: return 1.3348398541700344f;
313  case 18: return 1.4142135623730951f;
314  case 19: return 1.4983070768766815f;
315  case 20: return 1.5874010519681994f;
316  case 21: return 1.681792830507429f;
317  case 22: return 1.7817974362806785f;
318  case 23: return 1.887748625363387f;
319  case 24: return 2.0f;
320  }
321  UNREACHABLE("Converted unknown pitch value");
322 }
@ E_INST_DOUBLE_BASS
Definition: NoteEntity.h:13
@ E_INST_BELL
Definition: NoteEntity.h:18
@ E_INST_CLICKS
Definition: NoteEntity.h:15
@ E_INST_CHIME
Definition: NoteEntity.h:20
@ E_INST_SNARE_DRUM
Definition: NoteEntity.h:14
@ E_INST_FLUTE
Definition: NoteEntity.h:17
@ E_INST_GUITAR
Definition: NoteEntity.h:19
@ E_INST_XYLOPHONE
Definition: NoteEntity.h:21
@ E_INST_HARP_PIANO
Definition: NoteEntity.h:12
@ E_INST_BASS_DRUM
Definition: NoteEntity.h:16
@ E_BLOCK_STAINED_GLASS
Definition: BlockType.h:110
@ E_BLOCK_WHITE_SHULKER_BOX
Definition: BlockType.h:238
@ E_BLOCK_NETHER_BRICK_STAIRS
Definition: BlockType.h:129
@ E_BLOCK_PINK_SHULKER_BOX
Definition: BlockType.h:244
@ E_BLOCK_STONE_PRESSURE_PLATE
Definition: BlockType.h:84
@ E_BLOCK_BLUE_SHULKER_BOX
Definition: BlockType.h:249
@ E_BLOCK_OAK_WOOD_STAIRS
Definition: BlockType.h:63
@ E_BLOCK_STANDING_BANNER
Definition: BlockType.h:195
@ E_BLOCK_PURPUR_BLOCK
Definition: BlockType.h:220
@ E_BLOCK_CLAY
Definition: BlockType.h:96
@ E_BLOCK_DARK_OAK_FENCE
Definition: BlockType.h:210
@ E_BLOCK_DAYLIGHT_SENSOR
Definition: BlockType.h:167
@ E_BLOCK_PURPUR_STAIRS
Definition: BlockType.h:222
@ E_BLOCK_JUKEBOX
Definition: BlockType.h:99
@ E_BLOCK_BROWN_SHULKER_BOX
Definition: BlockType.h:250
@ E_BLOCK_BONE_BLOCK
Definition: BlockType.h:235
@ E_BLOCK_DOUBLE_WOODEN_SLAB
Definition: BlockType.h:140
@ E_BLOCK_COBBLESTONE_STAIRS
Definition: BlockType.h:81
@ E_BLOCK_TRAPDOOR
Definition: BlockType.h:111
@ E_BLOCK_LIGHT_BLUE_SHULKER_BOX
Definition: BlockType.h:241
@ E_BLOCK_REDSTONE_ORE
Definition: BlockType.h:87
@ E_BLOCK_COAL_ORE
Definition: BlockType.h:26
@ E_BLOCK_BIRCH_FENCE_GATE
Definition: BlockType.h:203
@ E_BLOCK_DARK_OAK_WOOD_STAIRS
Definition: BlockType.h:183
@ E_BLOCK_DIAMOND_ORE
Definition: BlockType.h:66
@ E_BLOCK_BRICK_STAIRS
Definition: BlockType.h:123
@ E_BLOCK_ACACIA_FENCE_GATE
Definition: BlockType.h:206
@ E_BLOCK_BRICK
Definition: BlockType.h:55
@ E_BLOCK_STONE_BRICK_STAIRS
Definition: BlockType.h:124
@ E_BLOCK_WOOL
Definition: BlockType.h:45
@ E_BLOCK_ENCHANTMENT_TABLE
Definition: BlockType.h:131
@ E_BLOCK_FURNACE
Definition: BlockType.h:73
@ E_BLOCK_SIGN_POST
Definition: BlockType.h:76
@ E_BLOCK_END_PORTAL_FRAME
Definition: BlockType.h:135
@ E_BLOCK_PRISMARINE_BLOCK
Definition: BlockType.h:187
@ E_BLOCK_GOLD_BLOCK
Definition: BlockType.h:51
@ E_BLOCK_RED_NETHER_BRICK
Definition: BlockType.h:234
@ E_BLOCK_OAK_FENCE_GATE
Definition: BlockType.h:122
@ E_BLOCK_STONE_BRICKS
Definition: BlockType.h:113
@ E_BLOCK_PURPUR_PILLAR
Definition: BlockType.h:221
@ E_BLOCK_MOSSY_COBBLESTONE
Definition: BlockType.h:58
@ E_BLOCK_OBSERVER
Definition: BlockType.h:237
@ E_BLOCK_STAINED_GLASS_PANE
Definition: BlockType.h:179
@ E_BLOCK_BEACON
Definition: BlockType.h:153
@ E_BLOCK_SPRUCE_DOOR
Definition: BlockType.h:212
@ E_BLOCK_MAGENTA_SHULKER_BOX
Definition: BlockType.h:240
@ E_BLOCK_REDSTONE_ORE_GLOWING
Definition: BlockType.h:88
@ E_BLOCK_BIRCH_WOOD_STAIRS
Definition: BlockType.h:150
@ E_BLOCK_CYAN_SHULKER_BOX
Definition: BlockType.h:247
@ E_BLOCK_LIME_SHULKER_BOX
Definition: BlockType.h:243
@ E_BLOCK_LAPIS_ORE
Definition: BlockType.h:31
@ E_BLOCK_PURPLE_SHULKER_BOX
Definition: BlockType.h:248
@ E_BLOCK_CHEST
Definition: BlockType.h:64
@ E_BLOCK_DOUBLE_RED_SANDSTONE_SLAB
Definition: BlockType.h:200
@ E_BLOCK_END_BRICKS
Definition: BlockType.h:225
@ E_BLOCK_BIRCH_DOOR
Definition: BlockType.h:213
@ E_BLOCK_BLOCK_OF_COAL
Definition: BlockType.h:192
@ E_BLOCK_JUNGLE_WOOD_STAIRS
Definition: BlockType.h:151
@ E_BLOCK_LIT_FURNACE
Definition: BlockType.h:74
@ E_BLOCK_FENCE
Definition: BlockType.h:100
@ E_BLOCK_STONE_SLAB
Definition: BlockType.h:54
@ E_BLOCK_ACACIA_WOOD_STAIRS
Definition: BlockType.h:182
@ E_BLOCK_EMERALD_ORE
Definition: BlockType.h:144
@ E_BLOCK_ORANGE_SHULKER_BOX
Definition: BlockType.h:239
@ E_BLOCK_GLASS_PANE
Definition: BlockType.h:117
@ E_BLOCK_BEDROCK
Definition: BlockType.h:17
@ E_BLOCK_DOUBLE_STONE_SLAB
Definition: BlockType.h:53
@ E_BLOCK_JUNGLE_DOOR
Definition: BlockType.h:214
@ E_BLOCK_PACKED_ICE
Definition: BlockType.h:193
@ E_BLOCK_NEW_LOG
Definition: BlockType.h:181
@ E_BLOCK_OBSIDIAN
Definition: BlockType.h:59
@ E_BLOCK_SOULSAND
Definition: BlockType.h:103
@ E_BLOCK_RED_SANDSTONE_STAIRS
Definition: BlockType.h:199
@ E_BLOCK_BOOKCASE
Definition: BlockType.h:57
@ E_BLOCK_RED_SANDSTONE
Definition: BlockType.h:198
@ E_BLOCK_IRON_ORE
Definition: BlockType.h:25
@ E_BLOCK_TRAPPED_CHEST
Definition: BlockType.h:161
@ E_BLOCK_OAK_DOOR
Definition: BlockType.h:77
@ E_BLOCK_PLANKS
Definition: BlockType.h:15
@ E_BLOCK_SPRUCE_FENCE_GATE
Definition: BlockType.h:202
@ E_BLOCK_GRAY_SHULKER_BOX
Definition: BlockType.h:245
@ E_BLOCK_GRAVEL
Definition: BlockType.h:23
@ E_BLOCK_SPRUCE_WOOD_STAIRS
Definition: BlockType.h:149
@ E_BLOCK_YELLOW_SHULKER_BOX
Definition: BlockType.h:242
@ E_BLOCK_DARK_OAK_FENCE_GATE
Definition: BlockType.h:205
@ E_BLOCK_JUNGLE_FENCE_GATE
Definition: BlockType.h:204
@ E_BLOCK_HUGE_BROWN_MUSHROOM
Definition: BlockType.h:114
@ E_BLOCK_WOODEN_PRESSURE_PLATE
Definition: BlockType.h:86
@ E_BLOCK_SANDSTONE
Definition: BlockType.h:34
@ E_BLOCK_HUGE_RED_MUSHROOM
Definition: BlockType.h:115
@ E_BLOCK_GREEN_SHULKER_BOX
Definition: BlockType.h:251
@ E_BLOCK_BLACK_SHULKER_BOX
Definition: BlockType.h:253
@ E_BLOCK_SPRUCE_FENCE
Definition: BlockType.h:207
@ E_BLOCK_GOLD_ORE
Definition: BlockType.h:24
@ E_BLOCK_STONE
Definition: BlockType.h:11
@ E_BLOCK_NETHER_QUARTZ_ORE
Definition: BlockType.h:170
@ E_BLOCK_LIGHT_GRAY_SHULKER_BOX
Definition: BlockType.h:246
@ E_BLOCK_WOODEN_SLAB
Definition: BlockType.h:141
@ E_BLOCK_SEA_LANTERN
Definition: BlockType.h:188
@ E_BLOCK_NOTE_BLOCK
Definition: BlockType.h:35
@ E_BLOCK_DARK_OAK_DOOR
Definition: BlockType.h:216
@ E_BLOCK_ACACIA_DOOR
Definition: BlockType.h:215
@ E_BLOCK_SANDSTONE_STAIRS
Definition: BlockType.h:143
@ E_BLOCK_QUARTZ_BLOCK
Definition: BlockType.h:172
@ E_BLOCK_NETHERRACK
Definition: BlockType.h:102
@ E_BLOCK_LOG
Definition: BlockType.h:27
@ E_BLOCK_ACACIA_FENCE
Definition: BlockType.h:211
@ E_BLOCK_DROPPER
Definition: BlockType.h:176
@ E_BLOCK_ENDER_CHEST
Definition: BlockType.h:145
@ E_BLOCK_GLOWSTONE
Definition: BlockType.h:104
@ E_BLOCK_SAND
Definition: BlockType.h:22
@ E_BLOCK_DISPENSER
Definition: BlockType.h:33
@ E_BLOCK_MAGMA
Definition: BlockType.h:232
@ E_BLOCK_PURPUR_DOUBLE_SLAB
Definition: BlockType.h:223
@ E_BLOCK_GLASS
Definition: BlockType.h:30
@ E_BLOCK_INVERTED_DAYLIGHT_SENSOR
Definition: BlockType.h:197
@ E_BLOCK_END_STONE
Definition: BlockType.h:136
@ E_BLOCK_COBBLESTONE_WALL
Definition: BlockType.h:154
@ E_BLOCK_PURPUR_SLAB
Definition: BlockType.h:224
@ E_BLOCK_MOB_SPAWNER
Definition: BlockType.h:62
@ E_BLOCK_NETHER_BRICK_FENCE
Definition: BlockType.h:128
@ E_BLOCK_RED_SANDSTONE_SLAB
Definition: BlockType.h:201
@ E_BLOCK_QUARTZ_STAIRS
Definition: BlockType.h:173
@ E_BLOCK_RED_SHULKER_BOX
Definition: BlockType.h:252
@ E_BLOCK_WALLSIGN
Definition: BlockType.h:82
@ E_BLOCK_BIRCH_FENCE
Definition: BlockType.h:208
@ E_BLOCK_NETHER_BRICK
Definition: BlockType.h:127
@ E_BLOCK_CRAFTING_TABLE
Definition: BlockType.h:68
@ E_BLOCK_WALL_BANNER
Definition: BlockType.h:196
@ E_BLOCK_JUNGLE_FENCE
Definition: BlockType.h:209
@ E_BLOCK_COBBLESTONE
Definition: BlockType.h:14
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
#define UNREACHABLE(x)
Definition: Globals.h:288
#define ASSERT(x)
Definition: Globals.h:276
unsigned char Byte
Definition: Globals.h:161
std::string AString
Definition: StringUtils.h:11
Vector3i m_Pos
Position in absolute block coordinates.
Definition: BlockEntity.h:113
virtual void CopyFrom(const cBlockEntity &a_Src)
Copies all properties of a_Src into this entity, except for its m_World and location.
Definition: BlockEntity.cpp:66
cWorld * m_World
Definition: BlockEntity.h:126
void SetNote(unsigned char a_Note)
Definition: NoteEntity.cpp:270
void MakeSound(void)
Definition: NoteEntity.cpp:47
unsigned char GetNote(void)
Definition: NoteEntity.cpp:261
virtual void CopyFrom(const cBlockEntity &a_Src) override
Copies all properties of a_Src into this entity, except for its m_World and location.
Definition: NoteEntity.cpp:24
void IncrementNote(void)
Definition: NoteEntity.cpp:279
unsigned char m_Note
Definition: NoteEntity.h:62
virtual bool UsedBy(cPlayer *a_Player) override
Called when a player uses this entity; should open the UI window.
Definition: NoteEntity.cpp:35
cNoteEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld *a_World)
Creates a new note entity.
Definition: NoteEntity.cpp:13
static float PitchFromNote(unsigned char a_Note)
Returns the relative pitch (used in the protocol) from a note value between 0 and 24 (used in m_Note)...
Definition: NoteEntity.cpp:288
Definition: Player.h:29
StatisticsManager & GetStatistics()
Return the associated statistic and achievement manager.
Definition: Player.h:237
std::unordered_map< CustomStatistic, StatValue > Custom
Vector3< T > addedY(T a_AddY) const
Returns a copy of this vector moved by the specified amount on the y axis.
Definition: Vector3.h:314
Definition: World.h:53
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
virtual void BroadcastBlockAction(Vector3i a_BlockPos, Byte a_Byte1, Byte a_Byte2, BLOCKTYPE a_BlockType, const cClientHandle *a_Exclude=nullptr) override
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override