2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "ChestEntity.h"
|
2013-05-28 15:12:47 -04:00
|
|
|
#include "../Item.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2014-12-13 09:06:55 -05:00
|
|
|
#include "../UI/ChestWindow.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-06 18:50:22 -04:00
|
|
|
cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World, BLOCKTYPE a_Type) :
|
|
|
|
super(a_Type, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World),
|
2014-07-12 17:30:34 -04:00
|
|
|
m_NumActivePlayers(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChestEntity::~cChestEntity()
|
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
cWindow * Window = GetWindow();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
Window->OwnerDestroyed();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-24 03:58:26 -04:00
|
|
|
void cChestEntity::SendTo(cClientHandle & a_Client)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-24 03:58:26 -04:00
|
|
|
// The chest entity doesn't need anything sent to the client when it's created / gets in the viewdistance
|
|
|
|
// All the actual handling is in the cWindow UI code that gets called when the chest is rclked
|
|
|
|
|
2012-08-06 16:10:16 -04:00
|
|
|
UNUSED(a_Client);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-06 16:10:16 -04:00
|
|
|
void cChestEntity::UsedBy(cPlayer * a_Player)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-13 02:13:56 -04:00
|
|
|
// If the window is not created, open it anew:
|
|
|
|
cWindow * Window = GetWindow();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window == nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
OpenNewWindow();
|
2013-06-13 02:13:56 -04:00
|
|
|
Window = GetWindow();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-06-13 02:13:56 -04:00
|
|
|
|
|
|
|
// Open the window for the player:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Window != nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-13 02:13:56 -04:00
|
|
|
if (a_Player->GetWindow() != Window)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-06-13 02:13:56 -04:00
|
|
|
a_Player->OpenWindow(Window);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is rather a hack
|
|
|
|
// Instead of marking the chunk as dirty upon chest contents change, we mark it dirty now
|
|
|
|
// We cannot properly detect contents change, but such a change doesn't happen without a player opening the chest first.
|
|
|
|
// The few false positives aren't much to worry about
|
2013-04-13 17:02:10 -04:00
|
|
|
int ChunkX, ChunkZ;
|
2013-08-03 14:05:07 -04:00
|
|
|
cChunkDef::BlockToChunk(m_PosX, m_PosZ, ChunkX, ChunkZ);
|
2014-07-06 18:50:22 -04:00
|
|
|
m_World->MarkChunkDirty(ChunkX, ChunkZ, true);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
void cChestEntity::OpenNewWindow(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-06-22 09:47:05 -04:00
|
|
|
// TODO: cats are an obstruction
|
2015-02-14 17:11:38 -05:00
|
|
|
if ((GetPosY() < cChunkDef::Height - 1) && cBlockInfo::IsSolid(GetWorld()->GetBlock(GetPosX(), GetPosY() + 1, GetPosZ())))
|
2014-06-22 09:47:05 -04:00
|
|
|
{
|
|
|
|
// Obstruction, don't open
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
// Callback for opening together with neighbor chest:
|
|
|
|
class cOpenDouble :
|
|
|
|
public cChestCallback
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
cChestEntity * m_ThisChest;
|
|
|
|
public:
|
|
|
|
cOpenDouble(cChestEntity * a_ThisChest) :
|
|
|
|
m_ThisChest(a_ThisChest)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
2012-10-20 17:53:09 -04:00
|
|
|
|
|
|
|
virtual bool Item(cChestEntity * a_Chest) override
|
|
|
|
{
|
2015-02-14 17:11:38 -05:00
|
|
|
if ((a_Chest->GetPosY() < cChunkDef::Height - 1) && cBlockInfo::IsSolid(a_Chest->GetWorld()->GetBlock(a_Chest->GetPosX(), a_Chest->GetPosY() + 1, a_Chest->GetPosZ())))
|
2014-06-22 09:47:05 -04:00
|
|
|
{
|
|
|
|
// Obstruction, don't open
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
// The primary chest should eb the one with lesser X or Z coord:
|
|
|
|
cChestEntity * Primary = a_Chest;
|
|
|
|
cChestEntity * Secondary = m_ThisChest;
|
|
|
|
if (
|
|
|
|
(Primary->GetPosX() > Secondary->GetPosX()) ||
|
|
|
|
(Primary->GetPosZ() > Secondary->GetPosZ())
|
|
|
|
)
|
|
|
|
{
|
|
|
|
std::swap(Primary, Secondary);
|
|
|
|
}
|
|
|
|
m_ThisChest->OpenWindow(new cChestWindow(Primary, Secondary));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
// Scan neighbors for adjacent chests:
|
|
|
|
cOpenDouble OpenDbl(this);
|
|
|
|
if (
|
|
|
|
m_World->DoWithChestAt(m_PosX - 1, m_PosY, m_PosZ, OpenDbl) ||
|
|
|
|
m_World->DoWithChestAt(m_PosX + 1, m_PosY, m_PosZ, OpenDbl) ||
|
2014-07-18 03:57:34 -04:00
|
|
|
m_World->DoWithChestAt(m_PosX, m_PosY, m_PosZ - 1, OpenDbl) ||
|
|
|
|
m_World->DoWithChestAt(m_PosX, m_PosY, m_PosZ + 1, OpenDbl)
|
2012-10-20 17:53:09 -04:00
|
|
|
)
|
2012-08-19 15:42:32 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
// The double-chest window has been opened in the callback
|
|
|
|
return;
|
2012-08-19 15:42:32 -04:00
|
|
|
}
|
2012-10-20 17:53:09 -04:00
|
|
|
|
2014-07-17 16:50:58 -04:00
|
|
|
// There is no chest neighbor, open a single-chest window:
|
2012-10-20 17:53:09 -04:00
|
|
|
OpenWindow(new cChestWindow(this));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|