From 89b1bbdc5fca5a51df1a5dd18ce91f27cb667c04 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 21:59:35 +0200 Subject: Added beacon. --- src/BlockEntities/BeaconEntity.cpp | 309 +++++++++++++++++++++++++++++++--- src/BlockEntities/BeaconEntity.h | 58 +++++-- src/BlockEntities/FlowerPotEntity.cpp | 2 +- 3 files changed, 337 insertions(+), 32 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 4b9662797..af6c124c0 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -3,33 +3,31 @@ #include "BeaconEntity.h" #include "../BlockArea.h" +#include "../Entities/Player.h" -cBeaconEntity::cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) : - super(E_BLOCK_BEACON, a_BlockX, a_BlockY, a_BlockZ, a_World) +cBeaconEntity::cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) + : super(E_BLOCK_BEACON, a_BlockX, a_BlockY, a_BlockZ, 1, 1, a_World) + , m_IsActive(false) + , m_BeaconLevel(0) + , m_PrimaryPotion(cEntityEffect::effNoEffect) + , m_SecondaryPotion(cEntityEffect::effNoEffect) { + UpdateBeacon(); } -int cBeaconEntity::GetPyramidLevel(void) +char cBeaconEntity::CalculatePyramidLevel(void) { cBlockArea Area; - int MinY = GetPosY() - 4; - if (MinY < 0) - { - MinY = 0; - } - int MaxY = GetPosY() - 1; - if (MaxY < 0) - { - MaxY = 0; - } + int MinY = std::max(GetPosY() - 4, 0); + int MaxY = std::max(GetPosY() - 1, 0); Area.Read( m_World, @@ -42,7 +40,7 @@ int cBeaconEntity::GetPyramidLevel(void) int Layer = 1; int MiddleXZ = 4; - for (int Y = Area.GetSizeY() - 1; Y > 0; Y--) + for (int Y = (Area.GetSizeY() - 1); Y >= 0; Y--) { for (int X = MiddleXZ - Layer; X <= (MiddleXZ + Layer); X++) { @@ -50,14 +48,122 @@ int cBeaconEntity::GetPyramidLevel(void) { if (!IsMineralBlock(Area.GetRelBlockType(X, Y, Z))) { - return Layer - 1; + return (Layer - 1); } } } Layer++; } - return Layer - 1; + return (Layer - 1); +} + + + + + +bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel) +{ + if (a_Potion == cEntityEffect::effNoEffect) + { + return true; + } + + switch (a_BeaconLevel) + { + case 4: + { + // Beacon level 4 + if (a_Potion == cEntityEffect::effRegeneration) + { + return true; + } + } + case 3: + { + // Beacon level 3 + if (a_Potion == cEntityEffect::effStrength) + { + return true; + } + } + case 2: + { + // Beacon level 2 + switch (a_Potion) + { + case cEntityEffect::effResistance: + case cEntityEffect::effJumpBoost: + { + return true; + } + } + } + case 1: + { + // Beacon level 1 + switch (a_Potion) + { + case cEntityEffect::effSpeed: + case cEntityEffect::effHaste: + { + return true; + } + } + } + } + return false; +} + + + + + +bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) +{ + LOG("SelectPrimaryPotion!"); + if (!IsValidPotion(a_Potion, m_BeaconLevel)) + { + LOG("FALLSE!"); + return false; + } + + m_PrimaryPotion = a_Potion; + return true; +} + + + + + +bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) +{ + if (!IsValidPotion(a_Potion, m_BeaconLevel)) + { + return false; + } + + m_SecondaryPotion = a_Potion; + return true; +} + + + + + +bool cBeaconEntity::IsBeaconBlocked(void) +{ + bool IsBlocked = false; + for (int Y = m_PosY; Y < cChunkDef::Height; ++Y) + { + BLOCKTYPE Block = m_World->GetBlock(m_PosX, Y, m_PosZ); + if (!cBlockInfo::IsTransparent(Block)) + { + IsBlocked = true; + break; + } + } + return IsBlocked; } @@ -83,8 +189,102 @@ bool cBeaconEntity::IsMineralBlock(BLOCKTYPE a_BlockType) +void cBeaconEntity::UpdateBeacon(void) +{ + if (IsBeaconBlocked()) + { + m_IsActive = false; + m_BeaconLevel = 0; + } + else + { + m_BeaconLevel = CalculatePyramidLevel(); + m_IsActive = (m_BeaconLevel > 0); + } + + // TODO: Add achievement +} + + + + + +void cBeaconEntity::GiveEffects(void) +{ + if (!m_IsActive || (m_BeaconLevel < 0)) + { + return; + } + + int Radius = m_BeaconLevel * 10 + 10; + short EffectLevel = 0; + if ((m_BeaconLevel >= 4) && (m_PrimaryPotion == m_SecondaryPotion)) + { + EffectLevel = 1; + } + + cEntityEffect::eType SecondaryPotion = cEntityEffect::effNoEffect; + if ((m_BeaconLevel >= 4) && (m_PrimaryPotion != m_SecondaryPotion) && (m_SecondaryPotion > 0)) + { + SecondaryPotion = m_SecondaryPotion; + } + + class cPlayerCallback : public cPlayerListCallback + { + int m_Radius; + int m_PosX, m_PosY, m_PosZ; + cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + short m_EffectLevel; + + virtual bool Item(cPlayer * a_Player) + { + Vector3d PlayerPosition = Vector3d(a_Player->GetPosition()); + if (PlayerPosition.y > (double)m_PosY) + { + PlayerPosition.y = (double)m_PosY; + } + + // TODO: Vanilla minecraft uses an AABB check instead of a radius one + Vector3d BeaconPosition = Vector3d(m_PosX, m_PosY, m_PosZ); + if ((PlayerPosition - BeaconPosition).Length() <= m_Radius) + { + a_Player->AddEntityEffect(m_PrimaryPotion, 180, m_EffectLevel); + + if (m_SecondaryPotion != cEntityEffect::effNoEffect) + { + a_Player->AddEntityEffect(m_SecondaryPotion, 180, 0); + } + } + return false; + } + + public: + cPlayerCallback(int a_Radius, int a_PosX, int a_PosY, int a_PosZ, cEntityEffect::eType a_PrimaryPotion, cEntityEffect::eType a_SecondaryPotion, short a_EffectLevel) + : m_Radius(a_Radius) + , m_PosX(a_PosX) + , m_PosY(a_PosY) + , m_PosZ(a_PosZ) + , m_PrimaryPotion(a_PrimaryPotion) + , m_SecondaryPotion(a_SecondaryPotion) + , m_EffectLevel(a_EffectLevel) + {}; + + } PlayerCallback(Radius, m_PosX, m_PosY, m_PosZ, m_PrimaryPotion, SecondaryPotion, EffectLevel); + GetWorld()->ForEachPlayer(PlayerCallback); +} + + + + + bool cBeaconEntity::Tick(float a_Dt, cChunk & a_Chunk) { + // Update the beacon every 4 seconds + if ((GetWorld()->GetWorldAge() % 80) == 0) + { + UpdateBeacon(); + GiveEffects(); + } return false; } @@ -92,23 +292,94 @@ bool cBeaconEntity::Tick(float a_Dt, cChunk & a_Chunk) -void cBeaconEntity::SaveToJson(Json::Value& a_Value) +void cBeaconEntity::UsedBy(cPlayer * a_Player) { + cWindow * Window = GetWindow(); + if (Window == NULL) + { + OpenWindow(new cBeaconWindow(m_PosX, m_PosY, m_PosZ, this)); + Window = GetWindow(); + } + + if (Window != NULL) + { + // if (a_Player->GetWindow() != Window) + // -> Because mojang doesn't send a 'close window' packet when you click the cancel button in the beacon inventory ... + { + a_Player->OpenWindow(Window); + } + } } -void cBeaconEntity::SendTo(cClientHandle & a_Client) + +bool cBeaconEntity::LoadFromJson(const Json::Value & a_Value) +{ + m_PosX = a_Value.get("x", 0).asInt(); + m_PosY = a_Value.get("y", 0).asInt(); + m_PosZ = a_Value.get("z", 0).asInt(); + + Json::Value AllSlots = a_Value.get("Slots", 0); + int SlotIdx = 0; + for (Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr) + { + cItem Item; + Item.FromJson(*itr); + SetSlot(SlotIdx, Item); + SlotIdx++; + } + + m_BeaconLevel = (char)a_Value.get("Level", 0).asInt(); + int PrimaryPotion = a_Value.get("PrimaryPotion", 0).asInt(); + int SecondaryPotion = a_Value.get("SecondaryPotion", 0).asInt(); + + if ((PrimaryPotion >= 0) && (PrimaryPotion <= (int)cEntityEffect::effSaturation)) + { + m_PrimaryPotion = (cEntityEffect::eType)PrimaryPotion; + } + + if ((SecondaryPotion >= 0) && (SecondaryPotion <= (int)cEntityEffect::effSaturation)) + { + m_SecondaryPotion = (cEntityEffect::eType)SecondaryPotion; + } + + return true; +} + + + + + +void cBeaconEntity::SaveToJson(Json::Value& a_Value) { + a_Value["x"] = m_PosX; + a_Value["y"] = m_PosY; + a_Value["z"] = m_PosZ; + + Json::Value AllSlots; + int NumSlots = m_Contents.GetNumSlots(); + for (int i = 0; i < NumSlots; i++) + { + Json::Value Slot; + m_Contents.GetSlot(i).GetJson(Slot); + AllSlots.append(Slot); + } + a_Value["Slots"] = AllSlots; + + a_Value["Level"] = m_BeaconLevel; + a_Value["PrimaryPotion"] = (int)m_PrimaryPotion; + a_Value["SecondaryPotion"] = (int)m_SecondaryPotion; } -void cBeaconEntity::UsedBy(cPlayer * a_Player) +void cBeaconEntity::SendTo(cClientHandle & a_Client) { + a_Client.SendUpdateBlockEntity(*this); } diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index ee1eda391..52111e82a 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -1,7 +1,7 @@ #pragma once -#include "BlockEntity.h" +#include "BlockEntityWithItems.h" @@ -17,26 +17,60 @@ namespace Json class cBeaconEntity : - public cBlockEntity + public cBlockEntityWithItems { - typedef cBlockEntity super; + typedef cBlockEntityWithItems super; public: - - /** The initial constructor */ cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World); - - /** Returns the amount of layers the pyramid below the beacon has. */ - int GetPyramidLevel(void); + + /** Is the beacon active? */ + bool IsActive(void) const { return m_IsActive; } + + /** Returns the beacon level. (0 - 4) */ + char GetBeaconLevel(void) const { return m_BeaconLevel; } + + char GetPrimaryPotion(void) const { return m_PrimaryPotion; } + char GetSecondaryPotion(void) const { return m_SecondaryPotion; } + + /** Select the primary potion. Returns false when the potion is invalid.*/ + bool SelectPrimaryPotion(cEntityEffect::eType a_Potion); + + /** Select the secondary potion. Returns false when the potion is invalid. */ + bool SelectSecondaryPotion(cEntityEffect::eType a_Potion); + + /** Calculate the amount of layers the pyramid below the beacon has. */ + char CalculatePyramidLevel(void); + + /** Is the beacon blocked by non-transparent blocks that are higher than the beacon? */ + bool IsBeaconBlocked(void); /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ static bool IsMineralBlock(BLOCKTYPE a_BlockType); + + /** Returns true if the potion can be used. */ + static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); + + /** Update the beacon. */ + void UpdateBeacon(void); + + /** Give the near-players the effects. */ + void GiveEffects(void); + + bool LoadFromJson(const Json::Value & a_Value); // cBlockEntity overrides: - virtual void SaveToJson(Json::Value& a_Value) override; - virtual void SendTo(cClientHandle & a_Client) override; - virtual void UsedBy(cPlayer * a_Player) override; - virtual bool Tick(float a_Dt, cChunk & /* a_Chunk */) override; + virtual void SaveToJson(Json::Value& a_Value) override; + virtual void SendTo(cClientHandle & a_Client) override; + virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; + virtual void UsedBy(cPlayer * a_Player) override; + +protected: + bool m_IsActive; + char m_BeaconLevel; + + cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + } ; diff --git a/src/BlockEntities/FlowerPotEntity.cpp b/src/BlockEntities/FlowerPotEntity.cpp index 87bf8b921..e001634b8 100644 --- a/src/BlockEntities/FlowerPotEntity.cpp +++ b/src/BlockEntities/FlowerPotEntity.cpp @@ -1,7 +1,7 @@ // FlowerPotEntity.cpp -// Implements the cFlowerPotEntity class representing a single sign in the world +// Implements the cFlowerPotEntity class representing a single flower pot in the world #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "json/json.h" -- cgit v1.2.3 From 81e095adda62e4067ab4c07b0e4c7ce0f3dbce39 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:19:51 +0200 Subject: Exported the beacon. --- src/BlockEntities/BeaconEntity.cpp | 2 -- src/BlockEntities/BeaconEntity.h | 42 ++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 17 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index af6c124c0..38b710181 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -121,10 +121,8 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) { - LOG("SelectPrimaryPotion!"); if (!IsValidPotion(a_Potion, m_BeaconLevel)) { - LOG("FALLSE!"); return false; } diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 52111e82a..4710e91e0 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -1,3 +1,10 @@ +// BeaconEntity.h + +// Declares the cBeaconEntity class representing a single beacon in the world + + + + #pragma once @@ -16,14 +23,26 @@ namespace Json +// tolua_begin class cBeaconEntity : public cBlockEntityWithItems { typedef cBlockEntityWithItems super; public: + // tolua_end + cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World); + bool LoadFromJson(const Json::Value & a_Value); + // cBlockEntity overrides: + virtual void SaveToJson(Json::Value& a_Value) override; + virtual void SendTo(cClientHandle & a_Client) override; + virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; + virtual void UsedBy(cPlayer * a_Player) override; + + // tolua_begin + /** Is the beacon active? */ bool IsActive(void) const { return m_IsActive; } @@ -45,33 +64,26 @@ public: /** Is the beacon blocked by non-transparent blocks that are higher than the beacon? */ bool IsBeaconBlocked(void); - /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ - static bool IsMineralBlock(BLOCKTYPE a_BlockType); - - /** Returns true if the potion can be used. */ - static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); - /** Update the beacon. */ void UpdateBeacon(void); /** Give the near-players the effects. */ void GiveEffects(void); - bool LoadFromJson(const Json::Value & a_Value); - - // cBlockEntity overrides: - virtual void SaveToJson(Json::Value& a_Value) override; - virtual void SendTo(cClientHandle & a_Client) override; - virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; - virtual void UsedBy(cPlayer * a_Player) override; + /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ + static bool IsMineralBlock(BLOCKTYPE a_BlockType); + + /** Returns true if the potion can be used. */ + static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); + + // tolua_end protected: bool m_IsActive; char m_BeaconLevel; cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; - -} ; +} ; // tolua_export -- cgit v1.2.3 From ad4fa6eba823de90f528e199c9f7e61bdb3f55f3 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:22:06 +0200 Subject: Changed return type from GetPrimaryPotion() and GetSecondaryPotion() --- src/BlockEntities/BeaconEntity.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 4710e91e0..07b2b85dc 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -49,8 +49,8 @@ public: /** Returns the beacon level. (0 - 4) */ char GetBeaconLevel(void) const { return m_BeaconLevel; } - char GetPrimaryPotion(void) const { return m_PrimaryPotion; } - char GetSecondaryPotion(void) const { return m_SecondaryPotion; } + cEntityEffect::eType GetPrimaryPotion(void) const { return m_PrimaryPotion; } + cEntityEffect::eType GetSecondaryPotion(void) const { return m_SecondaryPotion; } /** Select the primary potion. Returns false when the potion is invalid.*/ bool SelectPrimaryPotion(cEntityEffect::eType a_Potion); -- cgit v1.2.3 From dcd226d9040409dec41c8f1f8909262946308ab0 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:50:34 +0200 Subject: Added beacon load/save. --- src/BlockEntities/BeaconEntity.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 07b2b85dc..59a4bdbd9 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -41,6 +41,9 @@ public: virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; virtual void UsedBy(cPlayer * a_Player) override; + /** Modify the beacon level. (It is needed to load the beacon corectly) */ + void SetBeaconLevel(char a_Level) { m_BeaconLevel = a_Level; } + // tolua_begin /** Is the beacon active? */ -- cgit v1.2.3 From e6ca5a5ece61d8a4dd69b877a91f567edf2a3f63 Mon Sep 17 00:00:00 2001 From: Howaner Date: Wed, 30 Jul 2014 22:54:19 +0200 Subject: Added window update. --- src/BlockEntities/BeaconEntity.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 38b710181..30d927663 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -127,6 +127,12 @@ bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) } m_PrimaryPotion = a_Potion; + + // Send window update: + if (GetWindow() != NULL) + { + GetWindow()->SetProperty(1, m_PrimaryPotion); + } return true; } @@ -142,6 +148,12 @@ bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) } m_SecondaryPotion = a_Potion; + + // Send window update: + if (GetWindow() != NULL) + { + GetWindow()->SetProperty(2, m_SecondaryPotion); + } return true; } @@ -189,6 +201,8 @@ bool cBeaconEntity::IsMineralBlock(BLOCKTYPE a_BlockType) void cBeaconEntity::UpdateBeacon(void) { + int OldBeaconLevel = m_BeaconLevel; + if (IsBeaconBlocked()) { m_IsActive = false; @@ -200,6 +214,15 @@ void cBeaconEntity::UpdateBeacon(void) m_IsActive = (m_BeaconLevel > 0); } + if (m_BeaconLevel != OldBeaconLevel) + { + // Send window update: + if (GetWindow() != NULL) + { + GetWindow()->SetProperty(0, m_BeaconLevel); + } + } + // TODO: Add achievement } -- cgit v1.2.3 From 556fc908aedcc36388e9d859487b140045e5e33e Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 12:13:11 +0200 Subject: Renamed functions and added beacon json saving. --- src/BlockEntities/BeaconEntity.cpp | 78 +++++++++++++++++++------------------- src/BlockEntities/BeaconEntity.h | 12 +++--- 2 files changed, 44 insertions(+), 46 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 30d927663..c94783ba8 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -13,8 +13,8 @@ cBeaconEntity::cBeaconEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * : super(E_BLOCK_BEACON, a_BlockX, a_BlockY, a_BlockZ, 1, 1, a_World) , m_IsActive(false) , m_BeaconLevel(0) - , m_PrimaryPotion(cEntityEffect::effNoEffect) - , m_SecondaryPotion(cEntityEffect::effNoEffect) + , m_PrimaryEffect(cEntityEffect::effNoEffect) + , m_SecondaryEffect(cEntityEffect::effNoEffect) { UpdateBeacon(); } @@ -62,9 +62,9 @@ char cBeaconEntity::CalculatePyramidLevel(void) -bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel) +bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel) { - if (a_Potion == cEntityEffect::effNoEffect) + if (a_Effect == cEntityEffect::effNoEffect) { return true; } @@ -74,7 +74,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 4: { // Beacon level 4 - if (a_Potion == cEntityEffect::effRegeneration) + if (a_Effect == cEntityEffect::effRegeneration) { return true; } @@ -82,7 +82,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 3: { // Beacon level 3 - if (a_Potion == cEntityEffect::effStrength) + if (a_Effect == cEntityEffect::effStrength) { return true; } @@ -90,7 +90,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 2: { // Beacon level 2 - switch (a_Potion) + switch (a_Effect) { case cEntityEffect::effResistance: case cEntityEffect::effJumpBoost: @@ -102,7 +102,7 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe case 1: { // Beacon level 1 - switch (a_Potion) + switch (a_Effect) { case cEntityEffect::effSpeed: case cEntityEffect::effHaste: @@ -119,19 +119,19 @@ bool cBeaconEntity::IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLe -bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) +bool cBeaconEntity::SelectPrimaryEffect(cEntityEffect::eType a_Effect) { - if (!IsValidPotion(a_Potion, m_BeaconLevel)) + if (!IsValidEffect(a_Effect, m_BeaconLevel)) { return false; } - m_PrimaryPotion = a_Potion; + m_PrimaryEffect = a_Effect; // Send window update: if (GetWindow() != NULL) { - GetWindow()->SetProperty(1, m_PrimaryPotion); + GetWindow()->SetProperty(1, m_PrimaryEffect); } return true; } @@ -140,19 +140,19 @@ bool cBeaconEntity::SelectPrimaryPotion(cEntityEffect::eType a_Potion) -bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) +bool cBeaconEntity::SelectSecondaryEffect(cEntityEffect::eType a_Effect) { - if (!IsValidPotion(a_Potion, m_BeaconLevel)) + if (!IsValidEffect(a_Effect, m_BeaconLevel)) { return false; } - m_SecondaryPotion = a_Potion; + m_SecondaryEffect = a_Effect; // Send window update: if (GetWindow() != NULL) { - GetWindow()->SetProperty(2, m_SecondaryPotion); + GetWindow()->SetProperty(2, m_SecondaryEffect); } return true; } @@ -163,17 +163,15 @@ bool cBeaconEntity::SelectSecondaryPotion(cEntityEffect::eType a_Potion) bool cBeaconEntity::IsBeaconBlocked(void) { - bool IsBlocked = false; for (int Y = m_PosY; Y < cChunkDef::Height; ++Y) { BLOCKTYPE Block = m_World->GetBlock(m_PosX, Y, m_PosZ); if (!cBlockInfo::IsTransparent(Block)) { - IsBlocked = true; - break; + return true; } } - return IsBlocked; + return false; } @@ -239,22 +237,22 @@ void cBeaconEntity::GiveEffects(void) int Radius = m_BeaconLevel * 10 + 10; short EffectLevel = 0; - if ((m_BeaconLevel >= 4) && (m_PrimaryPotion == m_SecondaryPotion)) + if ((m_BeaconLevel >= 4) && (m_PrimaryEffect == m_SecondaryEffect)) { EffectLevel = 1; } - cEntityEffect::eType SecondaryPotion = cEntityEffect::effNoEffect; - if ((m_BeaconLevel >= 4) && (m_PrimaryPotion != m_SecondaryPotion) && (m_SecondaryPotion > 0)) + cEntityEffect::eType SecondaryEffect = cEntityEffect::effNoEffect; + if ((m_BeaconLevel >= 4) && (m_PrimaryEffect != m_SecondaryEffect) && (m_SecondaryEffect > 0)) { - SecondaryPotion = m_SecondaryPotion; + SecondaryEffect = m_SecondaryEffect; } class cPlayerCallback : public cPlayerListCallback { int m_Radius; int m_PosX, m_PosY, m_PosZ; - cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + cEntityEffect::eType m_PrimaryEffect, m_SecondaryEffect; short m_EffectLevel; virtual bool Item(cPlayer * a_Player) @@ -269,28 +267,28 @@ void cBeaconEntity::GiveEffects(void) Vector3d BeaconPosition = Vector3d(m_PosX, m_PosY, m_PosZ); if ((PlayerPosition - BeaconPosition).Length() <= m_Radius) { - a_Player->AddEntityEffect(m_PrimaryPotion, 180, m_EffectLevel); + a_Player->AddEntityEffect(m_PrimaryEffect, 180, m_EffectLevel); - if (m_SecondaryPotion != cEntityEffect::effNoEffect) + if (m_SecondaryEffect != cEntityEffect::effNoEffect) { - a_Player->AddEntityEffect(m_SecondaryPotion, 180, 0); + a_Player->AddEntityEffect(m_SecondaryEffect, 180, 0); } } return false; } public: - cPlayerCallback(int a_Radius, int a_PosX, int a_PosY, int a_PosZ, cEntityEffect::eType a_PrimaryPotion, cEntityEffect::eType a_SecondaryPotion, short a_EffectLevel) + cPlayerCallback(int a_Radius, int a_PosX, int a_PosY, int a_PosZ, cEntityEffect::eType a_PrimaryEffect, cEntityEffect::eType a_SecondaryEffect, short a_EffectLevel) : m_Radius(a_Radius) , m_PosX(a_PosX) , m_PosY(a_PosY) , m_PosZ(a_PosZ) - , m_PrimaryPotion(a_PrimaryPotion) - , m_SecondaryPotion(a_SecondaryPotion) + , m_PrimaryEffect(a_PrimaryEffect) + , m_SecondaryEffect(a_SecondaryEffect) , m_EffectLevel(a_EffectLevel) {}; - } PlayerCallback(Radius, m_PosX, m_PosY, m_PosZ, m_PrimaryPotion, SecondaryPotion, EffectLevel); + } PlayerCallback(Radius, m_PosX, m_PosY, m_PosZ, m_PrimaryEffect, SecondaryEffect, EffectLevel); GetWorld()->ForEachPlayer(PlayerCallback); } @@ -353,17 +351,17 @@ bool cBeaconEntity::LoadFromJson(const Json::Value & a_Value) } m_BeaconLevel = (char)a_Value.get("Level", 0).asInt(); - int PrimaryPotion = a_Value.get("PrimaryPotion", 0).asInt(); - int SecondaryPotion = a_Value.get("SecondaryPotion", 0).asInt(); + int PrimaryEffect = a_Value.get("PrimaryEffect", 0).asInt(); + int SecondaryEffect = a_Value.get("SecondaryEffect", 0).asInt(); - if ((PrimaryPotion >= 0) && (PrimaryPotion <= (int)cEntityEffect::effSaturation)) + if ((PrimaryEffect >= 0) && (PrimaryEffect <= (int)cEntityEffect::effSaturation)) { - m_PrimaryPotion = (cEntityEffect::eType)PrimaryPotion; + m_PrimaryEffect = (cEntityEffect::eType)PrimaryEffect; } - if ((SecondaryPotion >= 0) && (SecondaryPotion <= (int)cEntityEffect::effSaturation)) + if ((SecondaryEffect >= 0) && (SecondaryEffect <= (int)cEntityEffect::effSaturation)) { - m_SecondaryPotion = (cEntityEffect::eType)SecondaryPotion; + m_SecondaryEffect = (cEntityEffect::eType)SecondaryEffect; } return true; @@ -390,8 +388,8 @@ void cBeaconEntity::SaveToJson(Json::Value& a_Value) a_Value["Slots"] = AllSlots; a_Value["Level"] = m_BeaconLevel; - a_Value["PrimaryPotion"] = (int)m_PrimaryPotion; - a_Value["SecondaryPotion"] = (int)m_SecondaryPotion; + a_Value["PrimaryEffect"] = (int)m_PrimaryEffect; + a_Value["SecondaryEffect"] = (int)m_SecondaryEffect; } diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 59a4bdbd9..5cf8da24e 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -52,14 +52,14 @@ public: /** Returns the beacon level. (0 - 4) */ char GetBeaconLevel(void) const { return m_BeaconLevel; } - cEntityEffect::eType GetPrimaryPotion(void) const { return m_PrimaryPotion; } - cEntityEffect::eType GetSecondaryPotion(void) const { return m_SecondaryPotion; } + cEntityEffect::eType GetPrimaryEffect(void) const { return m_PrimaryEffect; } + cEntityEffect::eType GetSecondaryEffect(void) const { return m_SecondaryEffect; } /** Select the primary potion. Returns false when the potion is invalid.*/ - bool SelectPrimaryPotion(cEntityEffect::eType a_Potion); + bool SelectPrimaryEffect(cEntityEffect::eType a_Effect); /** Select the secondary potion. Returns false when the potion is invalid. */ - bool SelectSecondaryPotion(cEntityEffect::eType a_Potion); + bool SelectSecondaryEffect(cEntityEffect::eType a_Effect); /** Calculate the amount of layers the pyramid below the beacon has. */ char CalculatePyramidLevel(void); @@ -77,7 +77,7 @@ public: static bool IsMineralBlock(BLOCKTYPE a_BlockType); /** Returns true if the potion can be used. */ - static bool IsValidPotion(cEntityEffect::eType a_Potion, char a_BeaconLevel); + static bool IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel); // tolua_end @@ -85,7 +85,7 @@ protected: bool m_IsActive; char m_BeaconLevel; - cEntityEffect::eType m_PrimaryPotion, m_SecondaryPotion; + cEntityEffect::eType m_PrimaryEffect, m_SecondaryEffect; } ; // tolua_export -- cgit v1.2.3 From c49d4fd215170da29b5c285cc6a344ec102764c6 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 12:15:18 +0200 Subject: Updated documentation. --- src/BlockEntities/BeaconEntity.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index 5cf8da24e..cc8ee8ad2 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -55,10 +55,10 @@ public: cEntityEffect::eType GetPrimaryEffect(void) const { return m_PrimaryEffect; } cEntityEffect::eType GetSecondaryEffect(void) const { return m_SecondaryEffect; } - /** Select the primary potion. Returns false when the potion is invalid.*/ + /** Select the primary effect. Returns false when the effect is invalid.*/ bool SelectPrimaryEffect(cEntityEffect::eType a_Effect); - /** Select the secondary potion. Returns false when the potion is invalid. */ + /** Select the secondary effect. Returns false when the effect is invalid. */ bool SelectSecondaryEffect(cEntityEffect::eType a_Effect); /** Calculate the amount of layers the pyramid below the beacon has. */ @@ -76,7 +76,7 @@ public: /** Returns true if the block is a diamond block, a golden block, an iron block or an emerald block. */ static bool IsMineralBlock(BLOCKTYPE a_BlockType); - /** Returns true if the potion can be used. */ + /** Returns true if the effect can be used. */ static bool IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel); // tolua_end -- cgit v1.2.3 From 6b1f7e7a45ebc04f39bb54edc85fbe9c9d0659e7 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 18:15:39 +0200 Subject: Renamed "select..." methods to "set..." and better IsValidEffect() function. --- src/BlockEntities/BeaconEntity.cpp | 58 ++++++++------------------------------ src/BlockEntities/BeaconEntity.h | 8 +++--- 2 files changed, 15 insertions(+), 51 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index c94783ba8..55c5ccb7f 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -64,54 +64,18 @@ char cBeaconEntity::CalculatePyramidLevel(void) bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel) { - if (a_Effect == cEntityEffect::effNoEffect) + switch (a_Effect) { - return true; + case cEntityEffect::effRegeneration: return (a_BeaconLevel >= 4); + case cEntityEffect::effStrength: return (a_BeaconLevel >= 3); + case cEntityEffect::effResistance: return (a_BeaconLevel >= 2); + case cEntityEffect::effJumpBoost: return (a_BeaconLevel >= 2); + case cEntityEffect::effSpeed: return (a_BeaconLevel >= 1); + case cEntityEffect::effHaste: return (a_BeaconLevel >= 1); + case cEntityEffect::effNoEffect: return true; } - switch (a_BeaconLevel) - { - case 4: - { - // Beacon level 4 - if (a_Effect == cEntityEffect::effRegeneration) - { - return true; - } - } - case 3: - { - // Beacon level 3 - if (a_Effect == cEntityEffect::effStrength) - { - return true; - } - } - case 2: - { - // Beacon level 2 - switch (a_Effect) - { - case cEntityEffect::effResistance: - case cEntityEffect::effJumpBoost: - { - return true; - } - } - } - case 1: - { - // Beacon level 1 - switch (a_Effect) - { - case cEntityEffect::effSpeed: - case cEntityEffect::effHaste: - { - return true; - } - } - } - } + LOGD("%s: Invalid beacon effect: %d", __FUNCTION__, (int)a_Effect); return false; } @@ -119,7 +83,7 @@ bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLe -bool cBeaconEntity::SelectPrimaryEffect(cEntityEffect::eType a_Effect) +bool cBeaconEntity::SetPrimaryEffect(cEntityEffect::eType a_Effect) { if (!IsValidEffect(a_Effect, m_BeaconLevel)) { @@ -140,7 +104,7 @@ bool cBeaconEntity::SelectPrimaryEffect(cEntityEffect::eType a_Effect) -bool cBeaconEntity::SelectSecondaryEffect(cEntityEffect::eType a_Effect) +bool cBeaconEntity::SetSecondaryEffect(cEntityEffect::eType a_Effect) { if (!IsValidEffect(a_Effect, m_BeaconLevel)) { diff --git a/src/BlockEntities/BeaconEntity.h b/src/BlockEntities/BeaconEntity.h index cc8ee8ad2..0d7150aef 100644 --- a/src/BlockEntities/BeaconEntity.h +++ b/src/BlockEntities/BeaconEntity.h @@ -55,11 +55,11 @@ public: cEntityEffect::eType GetPrimaryEffect(void) const { return m_PrimaryEffect; } cEntityEffect::eType GetSecondaryEffect(void) const { return m_SecondaryEffect; } - /** Select the primary effect. Returns false when the effect is invalid.*/ - bool SelectPrimaryEffect(cEntityEffect::eType a_Effect); + /** Sets the primary effect. Returns false when the effect is invalid. */ + bool SetPrimaryEffect(cEntityEffect::eType a_Effect); - /** Select the secondary effect. Returns false when the effect is invalid. */ - bool SelectSecondaryEffect(cEntityEffect::eType a_Effect); + /** Sets the secondary effect. Returns false when the effect is invalid. */ + bool SetSecondaryEffect(cEntityEffect::eType a_Effect); /** Calculate the amount of layers the pyramid below the beacon has. */ char CalculatePyramidLevel(void); -- cgit v1.2.3 From 09b63565bcdd5977988d390f33de47b17cc7b7b7 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 31 Jul 2014 23:19:05 +0200 Subject: Use "default:" in switch. --- src/BlockEntities/BeaconEntity.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index 55c5ccb7f..805e5e61f 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -73,10 +73,13 @@ bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLe case cEntityEffect::effSpeed: return (a_BeaconLevel >= 1); case cEntityEffect::effHaste: return (a_BeaconLevel >= 1); case cEntityEffect::effNoEffect: return true; - } - LOGD("%s: Invalid beacon effect: %d", __FUNCTION__, (int)a_Effect); - return false; + default: + { + LOGD("%s: Invalid beacon effect: %d", __FUNCTION__, (int)a_Effect); + return false; + } + } } -- cgit v1.2.3