2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cChestEntity.h"
|
|
|
|
#include "cItem.h"
|
|
|
|
#include "cClientHandle.h"
|
|
|
|
#include "cPlayer.h"
|
|
|
|
#include "cWindow.h"
|
2011-12-29 08:16:23 -05:00
|
|
|
#include "cWorld.h"
|
|
|
|
#include "cRoot.h"
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cPickup.h"
|
2011-10-30 20:52:20 -04:00
|
|
|
#include "cChunk.h"
|
2012-01-29 14:28:19 -05:00
|
|
|
#include <json/json.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-12-29 08:16:23 -05:00
|
|
|
class cWorld;
|
|
|
|
class cRoot;
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
cChestEntity::cChestEntity(int a_X, int a_Y, int a_Z, cWorld * a_World)
|
|
|
|
: cBlockEntity( E_BLOCK_CHEST, a_X, a_Y, a_Z, a_World)
|
2011-12-29 08:16:23 -05:00
|
|
|
, m_TopChest( false )
|
2012-02-13 16:47:03 -05:00
|
|
|
, m_JoinedChest( NULL )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
m_Content = new cItem[ c_ChestHeight*c_ChestWidth ];
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cChestEntity::~cChestEntity()
|
|
|
|
{
|
|
|
|
if( GetWindow() )
|
|
|
|
{
|
|
|
|
GetWindow()->OwnerDestroyed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_Content )
|
|
|
|
{
|
|
|
|
delete [] m_Content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cChestEntity::Destroy()
|
|
|
|
{
|
|
|
|
// Drop items
|
2011-12-29 08:16:23 -05:00
|
|
|
for( int i = 0; i < c_ChestHeight * c_ChestWidth; ++i )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
if( !m_Content[i].IsEmpty() )
|
|
|
|
{
|
2012-02-13 16:47:03 -05:00
|
|
|
cPickup * Pickup = new cPickup( m_PosX * 32 + 16, m_PosY * 32 + 16, m_PosZ * 32 + 16, m_Content[i], 0, 1.f, 0 );
|
|
|
|
Pickup->Initialize(m_World);
|
2011-10-03 14:41:19 -04:00
|
|
|
m_Content[i].Empty();
|
|
|
|
}
|
|
|
|
}
|
2011-12-29 08:16:23 -05:00
|
|
|
if (m_JoinedChest)
|
2012-02-13 16:47:03 -05:00
|
|
|
{
|
2011-12-29 08:16:23 -05:00
|
|
|
m_JoinedChest->RemoveJoinedChest(this);
|
2012-02-13 16:47:03 -05:00
|
|
|
}
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cItem * cChestEntity::GetSlot( int a_Slot )
|
|
|
|
{
|
|
|
|
if( a_Slot > -1 && a_Slot < c_ChestHeight*c_ChestWidth )
|
|
|
|
{
|
|
|
|
return &m_Content[ a_Slot ];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cChestEntity::SetSlot( int a_Slot, cItem & a_Item )
|
|
|
|
{
|
|
|
|
if( a_Slot > -1 && a_Slot < c_ChestHeight*c_ChestWidth )
|
|
|
|
{
|
|
|
|
m_Content[a_Slot] = a_Item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-30 11:01:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define READ(File, Var) \
|
|
|
|
if (File.Read(&Var, sizeof(Var)) != sizeof(Var)) \
|
|
|
|
{ \
|
|
|
|
LOGERROR("ERROR READING cChestEntity %s FROM FILE (line %d)", #Var, __LINE__); \
|
|
|
|
return false; \
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
|
|
|
|
2012-01-30 11:01:45 -05:00
|
|
|
bool cChestEntity::LoadFromFile(cFile & f)
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
2012-01-30 11:01:45 -05:00
|
|
|
READ(f, m_PosX);
|
|
|
|
READ(f, m_PosY);
|
|
|
|
READ(f, m_PosZ);
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
unsigned int NumSlots = 0;
|
2012-01-30 11:01:45 -05:00
|
|
|
READ(f, NumSlots);
|
2011-10-03 14:41:19 -04:00
|
|
|
for(unsigned int i = 0; i < NumSlots; i++)
|
|
|
|
{
|
|
|
|
cItem Item;
|
2012-01-30 11:01:45 -05:00
|
|
|
READ(f, Item.m_ItemID);
|
|
|
|
READ(f, Item.m_ItemCount);
|
|
|
|
READ(f, Item.m_ItemHealth);
|
2011-10-03 14:41:19 -04:00
|
|
|
SetSlot( i, Item );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-30 11:01:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
bool cChestEntity::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;
|
2011-10-31 17:30:14 -04:00
|
|
|
Item.FromJson( *itr );
|
2011-10-03 14:41:19 -04:00
|
|
|
SetSlot( SlotIdx, Item );
|
|
|
|
SlotIdx++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cChestEntity::SaveToJson( Json::Value& a_Value )
|
|
|
|
{
|
|
|
|
a_Value["x"] = m_PosX;
|
|
|
|
a_Value["y"] = m_PosY;
|
|
|
|
a_Value["z"] = m_PosZ;
|
|
|
|
|
|
|
|
unsigned int NumSlots = c_ChestHeight*c_ChestWidth;
|
|
|
|
Json::Value AllSlots;
|
|
|
|
for(unsigned int i = 0; i < NumSlots; i++)
|
|
|
|
{
|
|
|
|
Json::Value Slot;
|
|
|
|
cItem* Item = GetSlot( i );
|
2011-10-31 17:30:14 -04:00
|
|
|
if( Item ) Item->GetJson( Slot );
|
2011-10-03 14:41:19 -04:00
|
|
|
AllSlots.append( Slot );
|
|
|
|
}
|
|
|
|
a_Value["Slots"] = AllSlots;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cChestEntity::SendTo( cClientHandle* a_Client, cServer* a_Server )
|
|
|
|
{
|
|
|
|
(void)a_Client;
|
|
|
|
(void)a_Server;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
void cChestEntity::UsedBy( cPlayer & a_Player )
|
|
|
|
{
|
|
|
|
LOG("Used a chest");
|
|
|
|
// m_Content[0].m_ItemCount = 1;
|
|
|
|
// m_Content[0].m_ItemID = E_ITEM_STONE;
|
|
|
|
// m_Content[0].m_ItemHealth = 0;
|
|
|
|
|
|
|
|
if( !GetWindow() )
|
|
|
|
{
|
|
|
|
cWindow* Window = new cWindow( this, true );
|
2011-12-29 08:16:23 -05:00
|
|
|
Window->SetSlots( GetContents(), GetChestHeight()*c_ChestWidth );
|
2011-10-03 14:41:19 -04:00
|
|
|
Window->SetWindowID( 1 );
|
2011-12-25 08:44:22 -05:00
|
|
|
Window->SetWindowType( cWindow::Chest );
|
2011-10-03 14:41:19 -04:00
|
|
|
Window->SetWindowTitle("UberChest");
|
2011-12-29 08:16:23 -05:00
|
|
|
Window->GetOwner()->SetEntity(this);
|
2011-10-03 14:41:19 -04:00
|
|
|
OpenWindow( Window );
|
|
|
|
}
|
2011-12-29 08:16:23 -05:00
|
|
|
if ( GetWindow() )
|
2011-10-03 14:41:19 -04:00
|
|
|
{
|
|
|
|
if( a_Player.GetWindow() != GetWindow() )
|
|
|
|
{
|
|
|
|
a_Player.OpenWindow( GetWindow() );
|
|
|
|
GetWindow()->SendWholeWindow( a_Player.GetClientHandle() );
|
|
|
|
}
|
|
|
|
}
|
2011-12-29 08:16:23 -05:00
|
|
|
cPacket_BlockAction ChestOpen;
|
|
|
|
ChestOpen.m_PosX = GetPosX();
|
|
|
|
ChestOpen.m_PosY = (short)GetPosY();
|
|
|
|
ChestOpen.m_PosZ = GetPosZ();
|
|
|
|
ChestOpen.m_Byte1 = (char)1;
|
|
|
|
ChestOpen.m_Byte2 = (char)1;
|
2012-02-13 16:47:03 -05:00
|
|
|
m_World->GetChunkOfBlock(m_PosX, m_PosY, m_PosZ)->Broadcast(&ChestOpen);
|
2011-12-29 08:16:23 -05:00
|
|
|
}
|
|
|
|
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-29 08:16:23 -05:00
|
|
|
cItem *cChestEntity::GetContents(bool a_OnlyThis)
|
|
|
|
{
|
|
|
|
if (m_JoinedChest && !a_OnlyThis)
|
|
|
|
{
|
|
|
|
cItem *Combined = new cItem[GetChestHeight()*c_ChestWidth];
|
|
|
|
int i;
|
|
|
|
cItem *first = (m_TopChest) ? GetContents(true) : m_JoinedChest->GetContents(true);
|
|
|
|
cItem *second = (!m_TopChest) ? GetContents(true) : m_JoinedChest->GetContents(true);
|
|
|
|
for (i=0; i < GetChestHeight()*c_ChestWidth; i++)
|
|
|
|
{
|
|
|
|
int index = i % c_ChestHeight*c_ChestWidth;
|
|
|
|
if (i < c_ChestHeight*c_ChestWidth)
|
|
|
|
Combined[index] = first[index];
|
|
|
|
else
|
|
|
|
Combined[index] = second[index];
|
|
|
|
}
|
|
|
|
return Combined;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return m_Content;
|
2011-10-03 14:41:19 -04:00
|
|
|
}
|
2012-02-13 16:47:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|