From e15e30a030c6013d3ae631188ad8a790c627e9aa Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 13 Mar 2015 22:29:27 +0000 Subject: Fixed confusion over Item Frame directions --- src/Entities/HangingEntity.cpp | 55 ++----------------------------- src/Entities/HangingEntity.h | 75 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 65 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/HangingEntity.cpp b/src/Entities/HangingEntity.cpp index a6b9c40c8..a3d05204a 100644 --- a/src/Entities/HangingEntity.cpp +++ b/src/Entities/HangingEntity.cpp @@ -11,7 +11,7 @@ cHangingEntity::cHangingEntity(eEntityType a_EntityType, eBlockFace a_Facing, double a_X, double a_Y, double a_Z) : cEntity(a_EntityType, a_X, a_Y, a_Z, 0.8, 0.8), - m_Facing(a_Facing) + m_Facing(cHangingEntity::BlockFaceToProtocolFace(a_Facing)) { SetMaxHealth(1); SetHealth(1); @@ -21,59 +21,10 @@ cHangingEntity::cHangingEntity(eEntityType a_EntityType, eBlockFace a_Facing, do -void cHangingEntity::SetFacing(eBlockFace a_Facing) -{ - // Y-based faces are not allowed: - switch (a_Facing) - { - case BLOCK_FACE_NONE: - case BLOCK_FACE_YM: - case BLOCK_FACE_YP: - { - LOGWARNING("%s: Invalid facing: %d. Ignoring.", __FUNCTION__, a_Facing); - ASSERT(!"Tried to set a bad facing!"); - return; - } - default: break; - } - - m_Facing = a_Facing; -} - - - - - void cHangingEntity::SpawnOn(cClientHandle & a_ClientHandle) { - int Dir = 0; - - // The client uses different values for item frame directions and block faces. Our constants are for the block faces, so we convert them here to item frame faces - switch (m_Facing) - { - case BLOCK_FACE_ZP: Dir = 0; break; - case BLOCK_FACE_ZM: Dir = 2; break; - case BLOCK_FACE_XM: Dir = 1; break; - case BLOCK_FACE_XP: Dir = 3; break; - default: - { - LOGINFO("Invalid facing (%d) in a cHangingEntity at {%d, %d, %d}, adjusting to BLOCK_FACE_XP.", - m_Facing, (int)GetPosX(), (int)GetPosY(), (int)GetPosZ() - ); - Dir = 3; - } - } - - if ((Dir == 0) || (Dir == 2)) // Probably a client bug, but two directions are flipped and contrary to the norm, so we do -180 - { - SetYaw((Dir * 90) - 180); - } - else - { - SetYaw(Dir * 90); - } - - a_ClientHandle.SendSpawnObject(*this, 71, Dir, (Byte)GetYaw(), (Byte)GetPitch()); + SetYaw(GetProtocolFacing() * 90); + a_ClientHandle.SendSpawnObject(*this, 71, GetProtocolFacing(), (Byte)GetYaw(), (Byte)GetPitch()); a_ClientHandle.SendEntityMetadata(*this); } diff --git a/src/Entities/HangingEntity.h b/src/Entities/HangingEntity.h index d1ef79a68..de93a73fe 100644 --- a/src/Entities/HangingEntity.h +++ b/src/Entities/HangingEntity.h @@ -24,28 +24,77 @@ public: // tolua_begin /** Returns the direction in which the entity is facing. */ - eBlockFace GetFacing() const { return m_Facing; } - + eBlockFace GetFacing() const { return cHangingEntity::ProtocolFaceToBlockFace(m_Facing); } + /** Set the direction in which the entity is facing. */ - void SetFacing(eBlockFace a_Facing); - - /** Returns the X coord of the block in which the entity resides. */ - int GetBlockX() const { return POSX_TOINT; } - - /** Returns the Y coord of the block in which the entity resides. */ - int GetBlockY() const { return POSY_TOINT; } - - /** Returns the Z coord of the block in which the entity resides. */ - int GetBlockZ() const { return POSZ_TOINT; } + void SetFacing(eBlockFace a_Facing) { m_Facing = cHangingEntity::BlockFaceToProtocolFace(a_Facing); } // tolua_end + /** Returns the direction in which the entity is facing. */ + Byte GetProtocolFacing() const { return m_Facing; } + + /** Set the direction in which the entity is facing. */ + void SetProtocolFacing(Byte a_Facing) + { + ASSERT((a_Facing <= 3) && (a_Facing >= 0)); + m_Facing = a_Facing; + } + private: virtual void SpawnOn(cClientHandle & a_ClientHandle) override; virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override {} - eBlockFace m_Facing; + /** Converts protocol hanging item facing to eBlockFace values */ + inline static eBlockFace ProtocolFaceToBlockFace(Byte a_ProtocolFace) + { + eBlockFace Dir; + + // The client uses different values for item frame directions and block faces. Our constants are for the block faces, so we convert them here to item frame faces + switch (a_ProtocolFace) + { + case 0: Dir = BLOCK_FACE_ZP; break; + case 2: Dir = BLOCK_FACE_ZM; break; + case 1: Dir = BLOCK_FACE_XM; break; + case 3: Dir = BLOCK_FACE_XP; break; + default: + { + LOGINFO("Invalid facing (%d) in a cHangingEntity, adjusting to BLOCK_FACE_XP.", a_ProtocolFace); + ASSERT(!"Tried to convert a bad facing!"); + + Dir = cHangingEntity::ProtocolFaceToBlockFace(3); + } + } + + return Dir; + } + + /** Converts eBlockFace values to protocol hanging item faces */ + inline static Byte BlockFaceToProtocolFace(eBlockFace a_BlockFace) + { + Byte Dir; + + // The client uses different values for item frame directions and block faces. Our constants are for the block faces, so we convert them here to item frame faces + switch (a_BlockFace) + { + case BLOCK_FACE_ZP: Dir = 0; break; + case BLOCK_FACE_ZM: Dir = 2; break; + case BLOCK_FACE_XM: Dir = 1; break; + case BLOCK_FACE_XP: Dir = 3; break; + default: + { + LOGINFO("Invalid facing (%d) in a cHangingEntity, adjusting to BLOCK_FACE_XP.", a_BlockFace); + // Uncomment when entities are initialised with their real data, instead of dummy values: ASSERT(!"Tried to convert a bad facing!"); + + Dir = cHangingEntity::BlockFaceToProtocolFace(BLOCK_FACE_XP); + } + } + + return Dir; + } + + Byte m_Facing; }; // tolua_export -- cgit v1.2.3 From 3d398baf355fd0dee0e282a73c61fe79f288db8f Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 13 Mar 2015 23:05:06 +0000 Subject: cPainting saving implemented Additionally, it now inherits from cHangingEntity. --- src/Entities/HangingEntity.cpp | 2 -- src/Entities/HangingEntity.h | 13 +++++++++---- src/Entities/ItemFrame.cpp | 11 +++++++++++ src/Entities/ItemFrame.h | 1 + src/Entities/Painting.cpp | 18 ++++-------------- src/Entities/Painting.h | 18 ++++++------------ 6 files changed, 31 insertions(+), 32 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/HangingEntity.cpp b/src/Entities/HangingEntity.cpp index a3d05204a..a37d8702e 100644 --- a/src/Entities/HangingEntity.cpp +++ b/src/Entities/HangingEntity.cpp @@ -24,8 +24,6 @@ cHangingEntity::cHangingEntity(eEntityType a_EntityType, eBlockFace a_Facing, do void cHangingEntity::SpawnOn(cClientHandle & a_ClientHandle) { SetYaw(GetProtocolFacing() * 90); - a_ClientHandle.SendSpawnObject(*this, 71, GetProtocolFacing(), (Byte)GetYaw(), (Byte)GetPitch()); - a_ClientHandle.SendEntityMetadata(*this); } diff --git a/src/Entities/HangingEntity.h b/src/Entities/HangingEntity.h index de93a73fe..8c9e93622 100644 --- a/src/Entities/HangingEntity.h +++ b/src/Entities/HangingEntity.h @@ -41,10 +41,14 @@ public: m_Facing = a_Facing; } -private: +protected: virtual void SpawnOn(cClientHandle & a_ClientHandle) override; - virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override {} + virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override + { + UNUSED(a_Dt); + UNUSED(a_Chunk); + } /** Converts protocol hanging item facing to eBlockFace values */ inline static eBlockFace ProtocolFaceToBlockFace(Byte a_ProtocolFace) @@ -84,8 +88,9 @@ private: case BLOCK_FACE_XP: Dir = 3; break; default: { - LOGINFO("Invalid facing (%d) in a cHangingEntity, adjusting to BLOCK_FACE_XP.", a_BlockFace); - // Uncomment when entities are initialised with their real data, instead of dummy values: ASSERT(!"Tried to convert a bad facing!"); + // Uncomment when entities are initialised with their real data, instead of dummy values: + // LOGINFO("Invalid facing (%d) in a cHangingEntity, adjusting to BLOCK_FACE_XP.", a_BlockFace); + // ASSERT(!"Tried to convert a bad facing!"); Dir = cHangingEntity::BlockFaceToProtocolFace(BLOCK_FACE_XP); } diff --git a/src/Entities/ItemFrame.cpp b/src/Entities/ItemFrame.cpp index dfffcd3ed..4e6e38f1f 100644 --- a/src/Entities/ItemFrame.cpp +++ b/src/Entities/ItemFrame.cpp @@ -92,3 +92,14 @@ void cItemFrame::GetDrops(cItems & a_Items, cEntity * a_Killer) + +void cItemFrame::SpawnOn(cClientHandle & a_ClientHandle) +{ + super::SpawnOn(a_ClientHandle); + a_ClientHandle.SendSpawnObject(*this, 71, GetProtocolFacing(), (Byte)GetYaw(), (Byte)GetPitch()); + a_ClientHandle.SendEntityMetadata(*this); +} + + + + diff --git a/src/Entities/ItemFrame.h b/src/Entities/ItemFrame.h index ced8c37af..2444b26a3 100644 --- a/src/Entities/ItemFrame.h +++ b/src/Entities/ItemFrame.h @@ -42,6 +42,7 @@ private: virtual void OnRightClicked(cPlayer & a_Player) override; virtual void KilledBy(TakeDamageInfo & a_TDI) override; virtual void GetDrops(cItems & a_Items, cEntity * a_Killer) override; + virtual void SpawnOn(cClientHandle & a_ClientHandle) override; cItem m_Item; Byte m_ItemRotation; diff --git a/src/Entities/Painting.cpp b/src/Entities/Painting.cpp index 6f6277f28..02a8f6ed0 100644 --- a/src/Entities/Painting.cpp +++ b/src/Entities/Painting.cpp @@ -10,10 +10,9 @@ -cPainting::cPainting(const AString & a_Name, int a_Direction, double a_X, double a_Y, double a_Z) - : cEntity(etPainting, a_X, a_Y, a_Z, 1, 1), - m_Name(a_Name), - m_Direction(a_Direction) +cPainting::cPainting(const AString & a_Name, eBlockFace a_Direction, double a_X, double a_Y, double a_Z) + : cHangingEntity(etPainting, a_Direction, a_X, a_Y, a_Z), + m_Name(a_Name) { } @@ -24,6 +23,7 @@ cPainting::cPainting(const AString & a_Name, int a_Direction, double a_X, double void cPainting::SpawnOn(cClientHandle & a_Client) { + super::SpawnOn(a_Client); a_Client.SendPaintingSpawn(*this); } @@ -31,16 +31,6 @@ void cPainting::SpawnOn(cClientHandle & a_Client) -void cPainting::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) -{ - UNUSED(a_Dt); - UNUSED(a_Chunk); -} - - - - - void cPainting::GetDrops(cItems & a_Items, cEntity * a_Killer) { if ((a_Killer != nullptr) && a_Killer->IsPlayer() && !((cPlayer *)a_Killer)->IsGameModeCreative()) diff --git a/src/Entities/Painting.h b/src/Entities/Painting.h index 6e8a382fc..20968d4f0 100644 --- a/src/Entities/Painting.h +++ b/src/Entities/Painting.h @@ -1,7 +1,7 @@ #pragma once -#include "Entity.h" +#include "HangingEntity.h" @@ -9,9 +9,9 @@ // tolua_begin class cPainting : - public cEntity + public cHangingEntity { - typedef cEntity super; + typedef cHangingEntity super; public: @@ -19,19 +19,14 @@ public: CLASS_PROTODEF(cPainting) - cPainting(const AString & a_Name, int a_Direction, double a_X, double a_Y, double a_Z); + cPainting(const AString & a_Name, eBlockFace a_Direction, double a_X, double a_Y, double a_Z); - // tolua_begin - - const AString & GetName(void) const { return m_Name; } - int GetDirection(void) const { return m_Direction; } - - // tolua_end + /** Returns the protocol name of the painting */ + const AString & GetName(void) const { return m_Name; } // tolua_export private: virtual void SpawnOn(cClientHandle & a_Client) override; - virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; virtual void GetDrops(cItems & a_Items, cEntity * a_Killer) override; virtual void KilledBy(TakeDamageInfo & a_TDI) override { @@ -40,7 +35,6 @@ private: } AString m_Name; - int m_Direction; }; // tolua_export -- cgit v1.2.3 From cb8ea41c9088112d08222d70c820d80ab2d1ad81 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 13 Mar 2015 23:22:09 +0000 Subject: Fixed style violations --- src/Entities/HangingEntity.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Entities') diff --git a/src/Entities/HangingEntity.h b/src/Entities/HangingEntity.h index 8c9e93622..507502ac6 100644 --- a/src/Entities/HangingEntity.h +++ b/src/Entities/HangingEntity.h @@ -27,7 +27,7 @@ public: eBlockFace GetFacing() const { return cHangingEntity::ProtocolFaceToBlockFace(m_Facing); } /** Set the direction in which the entity is facing. */ - void SetFacing(eBlockFace a_Facing) { m_Facing = cHangingEntity::BlockFaceToProtocolFace(a_Facing); } + void SetFacing(eBlockFace a_Facing) { m_Facing = cHangingEntity::BlockFaceToProtocolFace(a_Facing); } // tolua_end @@ -88,7 +88,7 @@ protected: case BLOCK_FACE_XP: Dir = 3; break; default: { - // Uncomment when entities are initialised with their real data, instead of dummy values: + // Uncomment when entities are initialised with their real data, instead of dummy values: // LOGINFO("Invalid facing (%d) in a cHangingEntity, adjusting to BLOCK_FACE_XP.", a_BlockFace); // ASSERT(!"Tried to convert a bad facing!"); -- cgit v1.2.3