2017-09-19 04:34:08 -04:00
|
|
|
|
2014-04-11 18:01:15 -04:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
|
|
|
#include "BeaconEntity.h"
|
|
|
|
#include "../BlockArea.h"
|
2014-07-30 15:59:35 -04:00
|
|
|
#include "../Entities/Player.h"
|
2014-12-13 09:06:55 -05:00
|
|
|
#include "../UI/BeaconWindow.h"
|
2016-11-18 14:00:04 -05:00
|
|
|
#include "../ClientHandle.h"
|
2014-04-11 18:01:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
cBeaconEntity::cBeaconEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World):
|
|
|
|
Super(a_BlockType, a_BlockMeta, a_BlockX, a_BlockY, a_BlockZ, 1, 1, a_World),
|
2016-11-18 14:00:04 -05:00
|
|
|
m_IsActive(false),
|
|
|
|
m_BeaconLevel(0),
|
|
|
|
m_PrimaryEffect(cEntityEffect::effNoEffect),
|
|
|
|
m_SecondaryEffect(cEntityEffect::effNoEffect)
|
2014-04-11 18:01:15 -04:00
|
|
|
{
|
2017-06-15 09:32:33 -04:00
|
|
|
ASSERT(a_BlockType == E_BLOCK_BEACON);
|
2017-08-10 13:11:27 -04:00
|
|
|
if (m_World != nullptr)
|
|
|
|
{
|
|
|
|
UpdateBeacon();
|
|
|
|
}
|
2014-04-11 18:01:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-30 15:59:35 -04:00
|
|
|
char cBeaconEntity::CalculatePyramidLevel(void)
|
2014-04-11 18:01:15 -04:00
|
|
|
{
|
|
|
|
cBlockArea Area;
|
2014-07-30 15:59:35 -04:00
|
|
|
int MinY = std::max(GetPosY() - 4, 0);
|
|
|
|
int MaxY = std::max(GetPosY() - 1, 0);
|
2014-04-11 18:35:13 -04:00
|
|
|
|
2014-04-11 18:01:15 -04:00
|
|
|
Area.Read(
|
2017-02-05 10:00:38 -05:00
|
|
|
*m_World,
|
2014-04-19 07:05:58 -04:00
|
|
|
GetPosX() - 4, GetPosX() + 4,
|
|
|
|
MinY, MaxY,
|
|
|
|
GetPosZ() - 4, GetPosZ() + 4,
|
2014-04-11 18:35:13 -04:00
|
|
|
cBlockArea::baTypes
|
2014-04-11 18:01:15 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
int Layer = 1;
|
|
|
|
int MiddleXZ = 4;
|
|
|
|
|
2014-07-30 15:59:35 -04:00
|
|
|
for (int Y = (Area.GetSizeY() - 1); Y >= 0; Y--)
|
2014-04-11 18:01:15 -04:00
|
|
|
{
|
|
|
|
for (int X = MiddleXZ - Layer; X <= (MiddleXZ + Layer); X++)
|
|
|
|
{
|
|
|
|
for (int Z = MiddleXZ - Layer; Z <= (MiddleXZ + Layer); Z++)
|
|
|
|
{
|
|
|
|
if (!IsMineralBlock(Area.GetRelBlockType(X, Y, Z)))
|
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<char>(Layer - 1);
|
2014-04-11 18:01:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 18:13:16 -04:00
|
|
|
Layer++;
|
2014-04-11 18:01:15 -04:00
|
|
|
}
|
|
|
|
|
2015-07-29 11:04:03 -04:00
|
|
|
return static_cast<char>(Layer - 1);
|
2014-07-30 15:59:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-31 06:13:11 -04:00
|
|
|
bool cBeaconEntity::IsValidEffect(cEntityEffect::eType a_Effect, char a_BeaconLevel)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2014-07-31 12:15:39 -04:00
|
|
|
switch (a_Effect)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2014-07-31 12:15:39 -04:00
|
|
|
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;
|
2014-07-30 15:59:35 -04:00
|
|
|
|
2014-07-31 17:19:05 -04:00
|
|
|
default:
|
|
|
|
{
|
2015-05-24 07:56:56 -04:00
|
|
|
LOGD("%s: Invalid beacon effect: %d", __FUNCTION__, static_cast<int>(a_Effect));
|
2014-07-31 17:19:05 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 15:59:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-31 12:15:39 -04:00
|
|
|
bool cBeaconEntity::SetPrimaryEffect(cEntityEffect::eType a_Effect)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2014-07-31 06:13:11 -04:00
|
|
|
if (!IsValidEffect(a_Effect, m_BeaconLevel))
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2014-08-01 18:14:05 -04:00
|
|
|
m_PrimaryEffect = cEntityEffect::effNoEffect;
|
2014-07-30 15:59:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-31 06:13:11 -04:00
|
|
|
m_PrimaryEffect = a_Effect;
|
2014-07-30 16:54:19 -04:00
|
|
|
|
|
|
|
// Send window update:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (GetWindow() != nullptr)
|
2014-07-30 16:54:19 -04:00
|
|
|
{
|
2014-07-31 06:13:11 -04:00
|
|
|
GetWindow()->SetProperty(1, m_PrimaryEffect);
|
2014-07-30 16:54:19 -04:00
|
|
|
}
|
2014-07-30 15:59:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-31 12:15:39 -04:00
|
|
|
bool cBeaconEntity::SetSecondaryEffect(cEntityEffect::eType a_Effect)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2014-07-31 06:13:11 -04:00
|
|
|
if (!IsValidEffect(a_Effect, m_BeaconLevel))
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2014-08-01 18:14:05 -04:00
|
|
|
m_SecondaryEffect = cEntityEffect::effNoEffect;
|
2014-07-30 15:59:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-31 06:13:11 -04:00
|
|
|
m_SecondaryEffect = a_Effect;
|
2014-07-30 16:54:19 -04:00
|
|
|
|
|
|
|
// Send window update:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (GetWindow() != nullptr)
|
2014-07-30 16:54:19 -04:00
|
|
|
{
|
2014-07-31 06:13:11 -04:00
|
|
|
GetWindow()->SetProperty(2, m_SecondaryEffect);
|
2014-07-30 16:54:19 -04:00
|
|
|
}
|
2014-07-30 15:59:35 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cBeaconEntity::IsBeaconBlocked(void)
|
|
|
|
{
|
|
|
|
for (int Y = m_PosY; Y < cChunkDef::Height; ++Y)
|
|
|
|
{
|
|
|
|
BLOCKTYPE Block = m_World->GetBlock(m_PosX, Y, m_PosZ);
|
|
|
|
if (!cBlockInfo::IsTransparent(Block))
|
|
|
|
{
|
2014-07-31 06:13:11 -04:00
|
|
|
return true;
|
2014-07-30 15:59:35 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-31 06:13:11 -04:00
|
|
|
return false;
|
2014-04-11 18:01:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cBeaconEntity::IsMineralBlock(BLOCKTYPE a_BlockType)
|
|
|
|
{
|
2014-07-21 09:19:48 -04:00
|
|
|
switch (a_BlockType)
|
2014-04-11 18:01:15 -04:00
|
|
|
{
|
|
|
|
case E_BLOCK_DIAMOND_BLOCK:
|
|
|
|
case E_BLOCK_GOLD_BLOCK:
|
|
|
|
case E_BLOCK_IRON_BLOCK:
|
|
|
|
case E_BLOCK_EMERALD_BLOCK:
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-30 15:59:35 -04:00
|
|
|
void cBeaconEntity::UpdateBeacon(void)
|
|
|
|
{
|
2014-07-30 16:54:19 -04:00
|
|
|
int OldBeaconLevel = m_BeaconLevel;
|
|
|
|
|
2014-07-30 15:59:35 -04:00
|
|
|
if (IsBeaconBlocked())
|
|
|
|
{
|
|
|
|
m_IsActive = false;
|
|
|
|
m_BeaconLevel = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_BeaconLevel = CalculatePyramidLevel();
|
|
|
|
m_IsActive = (m_BeaconLevel > 0);
|
|
|
|
}
|
|
|
|
|
2017-08-21 10:00:49 -04:00
|
|
|
if ((m_BeaconLevel != OldBeaconLevel) && (m_BeaconLevel == 4))
|
2014-07-30 16:54:19 -04:00
|
|
|
{
|
|
|
|
// Send window update:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (GetWindow() != nullptr)
|
2014-07-30 16:54:19 -04:00
|
|
|
{
|
|
|
|
GetWindow()->SetProperty(0, m_BeaconLevel);
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
Vector3d BeaconPosition(m_PosX, m_PosY, m_PosZ);
|
|
|
|
GetWorld()->ForEachPlayer([=](cPlayer & a_Player)
|
2017-08-21 10:00:49 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
Vector3d Distance = BeaconPosition - a_Player.GetPosition();
|
2017-08-21 10:00:49 -04:00
|
|
|
if (
|
|
|
|
(std::abs(Distance.y) <= 14) &&
|
|
|
|
(std::abs(Distance.x) <= 20) &&
|
|
|
|
(std::abs(Distance.z) <= 20)
|
|
|
|
)
|
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
a_Player.AwardAchievement(eStatistic::achFullBeacon);
|
2017-08-21 10:00:49 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
);
|
2017-08-21 10:00:49 -04:00
|
|
|
}
|
2014-07-30 15:59:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cBeaconEntity::GiveEffects(void)
|
|
|
|
{
|
|
|
|
if (!m_IsActive || (m_BeaconLevel < 0))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Radius = m_BeaconLevel * 10 + 10;
|
|
|
|
short EffectLevel = 0;
|
2014-07-31 06:13:11 -04:00
|
|
|
if ((m_BeaconLevel >= 4) && (m_PrimaryEffect == m_SecondaryEffect))
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
|
|
|
EffectLevel = 1;
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:59:25 -04:00
|
|
|
bool HasSecondaryEffect = (m_BeaconLevel >= 4) && (m_PrimaryEffect != m_SecondaryEffect) && (m_SecondaryEffect > 0);
|
2014-07-30 15:59:35 -04:00
|
|
|
|
2017-09-11 17:20:49 -04:00
|
|
|
Vector3d BeaconPosition(m_PosX, m_PosY, m_PosZ);
|
|
|
|
GetWorld()->ForEachPlayer([=](cPlayer & a_Player)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
auto PlayerPosition = a_Player.GetPosition();
|
|
|
|
if (PlayerPosition.y > BeaconPosition.y)
|
2014-10-20 16:55:07 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
PlayerPosition.y = BeaconPosition.y;
|
2014-10-20 16:55:07 -04:00
|
|
|
}
|
2014-07-30 15:59:35 -04:00
|
|
|
|
|
|
|
// TODO: Vanilla minecraft uses an AABB check instead of a radius one
|
2017-09-11 17:20:49 -04:00
|
|
|
if ((PlayerPosition - BeaconPosition).Length() <= Radius)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
a_Player.AddEntityEffect(m_PrimaryEffect, 180, EffectLevel);
|
2014-07-30 15:59:35 -04:00
|
|
|
|
2017-10-02 15:59:25 -04:00
|
|
|
if (HasSecondaryEffect)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
2017-09-11 17:20:49 -04:00
|
|
|
a_Player.AddEntityEffect(m_SecondaryEffect, 180, 0);
|
2014-07-30 15:59:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-11 17:20:49 -04:00
|
|
|
);
|
2014-07-30 15:59:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-15 09:32:33 -04:00
|
|
|
void cBeaconEntity::CopyFrom(const cBlockEntity & a_Src)
|
|
|
|
{
|
|
|
|
Super::CopyFrom(a_Src);
|
2018-05-02 03:50:36 -04:00
|
|
|
auto & src = static_cast<const cBeaconEntity &>(a_Src);
|
2017-06-15 09:32:33 -04:00
|
|
|
m_BeaconLevel = src.m_BeaconLevel;
|
|
|
|
m_Contents.CopyFrom(src.m_Contents);
|
|
|
|
m_IsActive = src.m_IsActive;
|
|
|
|
m_PrimaryEffect = src.m_PrimaryEffect;
|
|
|
|
m_SecondaryEffect = src.m_SecondaryEffect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cBeaconEntity::SendTo(cClientHandle & a_Client)
|
|
|
|
{
|
|
|
|
a_Client.SendUpdateBlockEntity(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-11 16:12:26 -05:00
|
|
|
bool cBeaconEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
|
2014-04-11 18:01:15 -04:00
|
|
|
{
|
2014-07-30 15:59:35 -04:00
|
|
|
// Update the beacon every 4 seconds
|
|
|
|
if ((GetWorld()->GetWorldAge() % 80) == 0)
|
|
|
|
{
|
|
|
|
UpdateBeacon();
|
|
|
|
GiveEffects();
|
|
|
|
}
|
2014-04-11 18:01:15 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-01 17:12:44 -05:00
|
|
|
bool cBeaconEntity::UsedBy(cPlayer * a_Player)
|
2014-04-11 18:01:15 -04:00
|
|
|
{
|
2014-07-30 15:59:35 -04:00
|
|
|
cWindow * Window = GetWindow();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window == nullptr)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
|
|
|
OpenWindow(new cBeaconWindow(m_PosX, m_PosY, m_PosZ, this));
|
|
|
|
Window = GetWindow();
|
|
|
|
}
|
2015-03-10 14:40:53 -04:00
|
|
|
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window != nullptr)
|
2014-07-30 15:59:35 -04:00
|
|
|
{
|
|
|
|
// if (a_Player->GetWindow() != Window)
|
|
|
|
// -> Because mojang doesn't send a 'close window' packet when you click the cancel button in the beacon inventory ...
|
|
|
|
{
|
2017-05-29 15:33:30 -04:00
|
|
|
a_Player->OpenWindow(*Window);
|
2014-07-30 15:59:35 -04:00
|
|
|
}
|
|
|
|
}
|
2015-12-01 17:12:44 -05:00
|
|
|
return true;
|
2014-04-11 18:01:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|