Cuberite
A lightweight, fast and extensible game server for Minecraft
HangingEntity.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 "HangingEntity.h"
5 #include "BlockInfo.h"
6 #include "Player.h"
7 #include "Chunk.h"
8 #include "../ClientHandle.h"
9 
10 
11 
12 
13 
15  Super(a_EntityType, a_Pos, 0.5f, 0.5f),
16  m_Facing(cHangingEntity::BlockFaceToProtocolFace(a_Facing))
17 {
18  SetMaxHealth(1);
19  SetHealth(1);
20 }
21 
22 
23 
24 
25 
27 {
28  return cBlockInfo::IsSolid(a_BlockType) && (a_BlockType != E_BLOCK_REDSTONE_REPEATER_OFF) && (a_BlockType != E_BLOCK_REDSTONE_REPEATER_ON);
29 }
30 
31 
32 
33 
34 
36 {
37  Super::KilledBy(a_TDI);
38 
39  Destroy();
40 }
41 
42 
43 
44 
45 
46 void cHangingEntity::SpawnOn(cClientHandle & a_ClientHandle)
47 {
48  SetYaw(GetProtocolFacing() * 90);
49 }
50 
51 
52 
53 
54 
55 void cHangingEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
56 {
57  UNUSED(a_Dt);
58 
59  // Check for a valid support block once every 64 ticks (3.2 seconds):
60  if ((m_World->GetWorldTickAge() % 64_tick) != 0_tick)
61  {
62  return;
63  }
64 
67  if (!a_Chunk.UnboundedRelGetBlockType(SupportPosition, Block) || IsValidSupportBlock(Block))
68  {
69  return;
70  }
71 
72  // Take environmental damage, intending to self-destruct, with "take damage" handling done by child classes:
73  TakeDamage(dtEnvironment, nullptr, static_cast<int>(GetMaxHealth()), 0);
74 }
@ E_BLOCK_REDSTONE_REPEATER_ON
Definition: BlockType.h:109
@ E_BLOCK_REDSTONE_REPEATER_OFF
Definition: BlockType.h:108
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:41
void AddFaceDirection(int &a_BlockX, int &a_BlockY, int &a_BlockZ, eBlockFace a_BlockFace, bool a_bInverse)
Modifies the specified coords so that they point to the block adjacent to the one specified through i...
Definition: Defines.cpp:378
@ dtEnvironment
Definition: Defines.h:265
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
#define UNUSED
Definition: Globals.h:72
static bool IsSolid(BLOCKTYPE Block)
Is this block solid (player cannot walk through)?
Definition: BlockInfo.cpp:892
Definition: Chunk.h:36
bool UnboundedRelGetBlockType(Vector3i a_RelCoords, BLOCKTYPE &a_BlockType) const
Same as GetBlockType(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap...
Definition: Chunk.cpp:1029
static void AbsoluteToRelative(int &a_X, int &a_Y, int &a_Z, int &a_ChunkX, int &a_ChunkZ)
Converts absolute block coords into relative (chunk + block) coords:
Definition: ChunkDef.h:147
Definition: Entity.h:76
void SetYaw(double a_Yaw)
Definition: Entity.cpp:2125
float GetMaxHealth(void) const
Definition: Entity.h:407
void SetHealth(float a_Health)
Sets the health of this entity; doesn't broadcast any hurt animation.
Definition: Entity.cpp:900
cWorld * m_World
Definition: Entity.h:624
void Destroy()
Destroys the entity, schedules it for memory freeing and broadcasts the DestroyEntity packet.
Definition: Entity.cpp:243
void TakeDamage(cEntity &a_Attacker)
Makes this pawn take damage from an attack by a_Attacker.
Definition: Entity.cpp:272
virtual void KilledBy(TakeDamageInfo &a_TDI)
Called when the health drops below zero.
Definition: Entity.cpp:851
void SetMaxHealth(float a_MaxHealth)
Sets the maximum value for the health.
Definition: Entity.cpp:1887
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
eEntityType
Definition: Entity.h:89
static eBlockFace ProtocolFaceToBlockFace(Byte a_ProtocolFace)
Converts protocol hanging item facing to eBlockFace values.
Definition: HangingEntity.h:52
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Byte GetProtocolFacing() const
Returns the direction in which the entity is facing.
Definition: HangingEntity.h:28
virtual void SpawnOn(cClientHandle &a_ClientHandle) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
static bool IsValidSupportBlock(BLOCKTYPE a_BlockType)
Returns if the given block can support hanging entity placements.
cHangingEntity(eEntityType a_EntityType, eBlockFace a_BlockFace, Vector3d a_Pos)
cTickTimeLong GetWorldTickAge() const
Definition: World.cpp:509