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"
|
|
|
|
#include "Item.h"
|
|
|
|
#include "ClientHandle.h"
|
|
|
|
#include "Player.h"
|
2012-09-23 13:19:34 -04:00
|
|
|
#include "UI/Window.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "World.h"
|
|
|
|
#include "Root.h"
|
|
|
|
#include "Pickup.h"
|
2013-04-06 17:21:57 -04:00
|
|
|
#include "Noise.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include <json/json.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cWorld;
|
|
|
|
class cRoot;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-06 17:21:57 -04:00
|
|
|
cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ) :
|
|
|
|
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ)
|
|
|
|
{
|
|
|
|
cBlockEntityWindowOwner::SetBlockEntity(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
|
2013-04-06 17:21:57 -04:00
|
|
|
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-04-06 17:21:57 -04:00
|
|
|
cBlockEntityWindowOwner::SetBlockEntity(this);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChestEntity::~cChestEntity()
|
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
cWindow * Window = GetWindow();
|
|
|
|
if (Window != NULL)
|
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-10-20 17:53:09 -04:00
|
|
|
void cChestEntity::Destroy(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Drop items
|
|
|
|
cItems Pickups;
|
2012-10-20 17:53:09 -04:00
|
|
|
for (int i = 0; i < c_ChestHeight * c_ChestWidth; ++i)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
if (!m_Content[i].IsEmpty())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Pickups.push_back(m_Content[i]);
|
|
|
|
m_Content[i].Empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
const cItem * cChestEntity::GetSlot(int a_Slot) const
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
if ((a_Slot > -1) && (a_Slot < c_ChestHeight * c_ChestWidth))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
return &m_Content[a_Slot];
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-10-20 17:53:09 -04:00
|
|
|
return NULL;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-20 09:25:54 -04:00
|
|
|
void cChestEntity::SetSlot(int a_Slot, const cItem & a_Item)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-09-20 09:25:54 -04:00
|
|
|
if ((a_Slot > -1) && (a_Slot < c_ChestHeight * c_ChestWidth))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_Content[a_Slot] = a_Item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-06 17:21:57 -04:00
|
|
|
void cChestEntity::GenerateRandomLootWithBooks(const cLootProbab * a_LootProbabs, int a_CountLootProbabs, int a_NumSlots, int a_Seed)
|
|
|
|
{
|
|
|
|
// Calculate the total weight:
|
|
|
|
int TotalProbab = 1;
|
|
|
|
for (int i = 0; i < a_CountLootProbabs; i++)
|
|
|
|
{
|
|
|
|
TotalProbab += a_LootProbabs[i].m_Weight;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-04-06 17:21:57 -04:00
|
|
|
|
|
|
|
// Pick the loot items:
|
|
|
|
cNoise Noise(a_Seed);
|
|
|
|
for (int i = 0; i < a_NumSlots; i++)
|
|
|
|
{
|
|
|
|
int Rnd = (Noise.IntNoise1DInt(i) / 7);
|
|
|
|
int LootRnd = Rnd % TotalProbab;
|
|
|
|
Rnd >>= 8;
|
|
|
|
cItem CurrentLoot = cItem(E_ITEM_BOOK, 1, 0); // TODO: enchantment
|
|
|
|
for (int j = 0; j < a_CountLootProbabs; j++)
|
|
|
|
{
|
|
|
|
LootRnd -= a_LootProbabs[i].m_Weight;
|
|
|
|
if (LootRnd < 0)
|
|
|
|
{
|
|
|
|
CurrentLoot = a_LootProbabs[i].m_Item;
|
|
|
|
CurrentLoot.m_ItemCount = a_LootProbabs[i].m_MinAmount + (Rnd % (a_LootProbabs[i].m_MaxAmount - a_LootProbabs[i].m_MinAmount));
|
|
|
|
Rnd >>= 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // for j - a_LootProbabs[]
|
|
|
|
SetSlot(Rnd % ARRAYCOUNT(m_Content), CurrentLoot);
|
|
|
|
} // for i - NumSlots
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
bool cChestEntity::LoadFromJson(const Json::Value & a_Value)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
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;
|
2012-10-20 17:53:09 -04:00
|
|
|
for (Json::Value::iterator itr = AllSlots.begin(); itr != AllSlots.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
cItem Item;
|
|
|
|
Item.FromJson( *itr );
|
|
|
|
SetSlot( SlotIdx, Item );
|
|
|
|
SlotIdx++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
void cChestEntity::SaveToJson(Json::Value & a_Value)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
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;
|
2012-10-20 17:53:09 -04:00
|
|
|
for (unsigned int i = 0; i < NumSlots; i++)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Json::Value Slot;
|
2012-10-20 17:53:09 -04:00
|
|
|
const cItem * Item = GetSlot(i);
|
|
|
|
if (Item != NULL)
|
|
|
|
{
|
|
|
|
Item->GetJson(Slot);
|
|
|
|
}
|
|
|
|
AllSlots.append(Slot);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
a_Value["Slots"] = AllSlots;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-09-20 09:25:54 -04:00
|
|
|
if (GetWindow() == NULL)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-20 17:53:09 -04:00
|
|
|
OpenNewWindow();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-09-20 09:25:54 -04:00
|
|
|
if (GetWindow())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
if( a_Player->GetWindow() != GetWindow() )
|
|
|
|
{
|
|
|
|
a_Player->OpenWindow( GetWindow() );
|
2012-09-20 09:25:54 -04:00
|
|
|
GetWindow()->SendWholeWindow(*a_Player->GetClientHandle());
|
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
|
|
|
|
int ChunkX, ChunkY = 0, ChunkZ;
|
|
|
|
cChunkDef::BlockToChunk(m_PosX, m_PosY, m_PosZ, ChunkX, ChunkZ);
|
|
|
|
m_World->MarkChunkDirty(ChunkX, ChunkY, ChunkZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-20 17:53:09 -04:00
|
|
|
void cChestEntity::OpenNewWindow(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
// 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) ||
|
|
|
|
m_World->DoWithChestAt(m_PosX , m_PosY, m_PosZ - 1, OpenDbl) ||
|
|
|
|
m_World->DoWithChestAt(m_PosX , m_PosY, m_PosZ + 1, OpenDbl)
|
|
|
|
)
|
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
|
|
|
|
|
|
|
// There is no chest neighbor, open a single-chest window:
|
|
|
|
OpenWindow(new cChestWindow(this));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|