1
0

cChestEntity and cDispenserEntity now inherit from a common ancestor, cBlockEntityWithItems

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1507 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2013-05-25 11:59:13 +00:00
parent e3136c2d0f
commit c640e9346c
9 changed files with 246 additions and 137 deletions

View File

@ -932,26 +932,6 @@
<Filter
Name="Entities"
>
<File
RelativePath="..\source\BlockEntity.h"
>
</File>
<File
RelativePath="..\source\ChestEntity.cpp"
>
</File>
<File
RelativePath="..\source\ChestEntity.h"
>
</File>
<File
RelativePath="..\source\DispenserEntity.cpp"
>
</File>
<File
RelativePath="..\source\DispenserEntity.h"
>
</File>
<File
RelativePath="..\source\Doors.h"
>
@ -972,22 +952,6 @@
RelativePath="..\source\FallingBlock.h"
>
</File>
<File
RelativePath="..\source\FurnaceEntity.cpp"
>
</File>
<File
RelativePath="..\source\FurnaceEntity.h"
>
</File>
<File
RelativePath="..\source\JukeboxEntity.cpp"
>
</File>
<File
RelativePath="..\source\JukeboxEntity.h"
>
</File>
<File
RelativePath="..\source\Ladder.h"
>
@ -1000,14 +964,6 @@
RelativePath="..\source\Minecart.h"
>
</File>
<File
RelativePath="..\source\NoteEntity.cpp"
>
</File>
<File
RelativePath="..\source\NoteEntity.h"
>
</File>
<File
RelativePath="..\source\Pawn.cpp"
>
@ -1036,14 +992,6 @@
RelativePath="..\source\Sign.h"
>
</File>
<File
RelativePath="..\source\SignEntity.cpp"
>
</File>
<File
RelativePath="..\source\SignEntity.h"
>
</File>
<File
RelativePath="..\source\Stairs.h"
>
@ -2345,6 +2293,66 @@
>
</File>
</Filter>
<Filter
Name="BlockEntities"
>
<File
RelativePath="..\source\BlockEntity.h"
>
</File>
<File
RelativePath="..\source\BlockEntityWithItems.h"
>
</File>
<File
RelativePath="..\source\ChestEntity.cpp"
>
</File>
<File
RelativePath="..\source\ChestEntity.h"
>
</File>
<File
RelativePath="..\source\DispenserEntity.cpp"
>
</File>
<File
RelativePath="..\source\DispenserEntity.h"
>
</File>
<File
RelativePath="..\source\FurnaceEntity.cpp"
>
</File>
<File
RelativePath="..\source\FurnaceEntity.h"
>
</File>
<File
RelativePath="..\source\JukeboxEntity.cpp"
>
</File>
<File
RelativePath="..\source\JukeboxEntity.h"
>
</File>
<File
RelativePath="..\source\NoteEntity.cpp"
>
</File>
<File
RelativePath="..\source\NoteEntity.h"
>
</File>
<File
RelativePath="..\source\SignEntity.cpp"
>
</File>
<File
RelativePath="..\source\SignEntity.h"
>
</File>
</Filter>
</Filter>
<Filter
Name="Config files"

View File

@ -20,19 +20,10 @@ class cPacket;
// tolua_begin
class cBlockEntity
{
protected:
cBlockEntity(BLOCKTYPE a_BlockType, int a_BlockX, int a_BlockY, int a_BlockZ) : // Used when generating
m_PosX(a_BlockX),
m_PosY(a_BlockY),
m_PosZ(a_BlockZ),
m_BlockType(a_BlockType),
m_World(NULL)
{
}
cBlockEntity(BLOCKTYPE a_BlockType, int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
m_PosX(a_BlockX),
m_PosY(a_BlockY),
@ -43,6 +34,8 @@ protected:
}
public:
// tolua_end
virtual ~cBlockEntity() {}; // force a virtual destructor in all descendants
virtual void Destroy(void) {};
@ -52,6 +45,8 @@ public:
m_World = a_World;
}
// tolua_begin
// Position, in absolute block coordinates:
int GetPosX(void) const { return m_PosX; }
int GetPosY(void) const { return m_PosY; }
@ -61,6 +56,8 @@ public:
cWorld * GetWorld(void) const {return m_World; }
// tolua_end
virtual void SaveToJson (Json::Value & a_Value) = 0;
virtual void UsedBy( cPlayer * a_Player ) = 0;
@ -81,7 +78,7 @@ protected:
BLOCKTYPE m_BlockType;
cWorld * m_World;
};
} ; // tolua_export

View File

@ -0,0 +1,71 @@
// BlockEntityWithItems.h
// Declares the cBlockEntityWithItems class representing a common ancestor for all block entities that have an ItemGrid
#pragma once
#include "BlockEntity.h"
#include "ItemGrid.h"
// tolua_begin
class cBlockEntityWithItems :
public cBlockEntity
{
typedef cBlockEntity super;
public:
// tolua_end
cBlockEntityWithItems(
BLOCKTYPE a_BlockType, // Type of the block that the entity represents
int a_BlockX, int a_BlockY, int a_BlockZ, // Position of the block entity
int a_ItemGridWidth, int a_ItemGridHeight, // Dimensions of the ItemGrid
cWorld * a_World // Optional world to assign to the entity
) :
super(a_BlockType, a_BlockX, a_BlockY, a_BlockZ, a_World),
m_Contents(a_ItemGridWidth, a_ItemGridHeight)
{
}
virtual void Destroy(void) override
{
// Drop the contents as pickups:
ASSERT(m_World != NULL);
cItems Pickups;
m_Contents.CopyToItems(Pickups);
m_Contents.Clear();
m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ);
}
// tolua_begin
const cItem & GetSlot(int a_SlotNum) const { return m_Contents.GetSlot(a_SlotNum); }
const cItem & GetSlot(int a_X, int a_Y) const { return m_Contents.GetSlot(a_X, a_Y); }
void SetSlot(int a_SlotNum, const cItem & a_Item) { m_Contents.SetSlot(a_SlotNum, a_Item); }
void SetSlot(int a_X, int a_Y, const cItem & a_Item) { m_Contents.SetSlot(a_X, a_Y, a_Item); }
/// Returns the ItemGrid used for storing the contents
cItemGrid & GetContents(void) { return m_Contents; }
// tolua_end
/// Const version of the GetContents() function for C++ type-safety
const cItemGrid & GetContents(void) const { return m_Contents; }
protected:
cItemGrid m_Contents;
} ; // tolua_export

View File

@ -24,8 +24,7 @@ class cRoot;
cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ) :
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ),
m_Contents(c_ChestWidth, c_ChestHeight)
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, NULL)
{
cBlockEntityWindowOwner::SetBlockEntity(this);
}
@ -35,8 +34,7 @@ cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ) :
cChestEntity::cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, a_World),
m_Contents(c_ChestWidth, c_ChestHeight)
super(E_BLOCK_CHEST, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World)
{
cBlockEntityWindowOwner::SetBlockEntity(this);
}
@ -58,19 +56,6 @@ cChestEntity::~cChestEntity()
void cChestEntity::Destroy(void)
{
// Drop items
cItems Pickups;
m_Contents.CopyToItems(Pickups);
m_Contents.Clear();
m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ);
}
bool cChestEntity::LoadFromJson(const Json::Value & a_Value)
{
m_PosX = a_Value.get("x", 0).asInt();

View File

@ -1,9 +1,8 @@
#pragma once
#include "BlockEntity.h"
#include "BlockEntityWithItems.h"
#include "UI/WindowOwner.h"
#include "ItemGrid.h"
@ -24,47 +23,39 @@ class cNBTData;
// tolua_begin
class cChestEntity :
public cBlockEntity,
public cBlockEntityWithItems,
public cBlockEntityWindowOwner
{
typedef cBlockEntity super;
typedef cBlockEntityWithItems super;
public:
cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ); // Used while generating
enum {
ContentsHeight = 3,
ContentsWidth = 9,
} ;
/// Constructor used while generating a chunk; sets m_World to NULL
cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ);
// tolua_end
/// Constructor used for normal operation
cChestEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World);
virtual ~cChestEntity();
virtual void Destroy();
static const char * GetClassStatic() { return "cChestEntity"; }
static const char * GetClassStatic(void) { return "cChestEntity"; }
// tolua_begin
const cItem & GetSlot(int a_Slot) const { return m_Contents.GetSlot(a_Slot); }
void SetSlot(int a_Slot, const cItem & a_Item ) { m_Contents.SetSlot(a_Slot, a_Item); }
// tolua_end
bool LoadFromJson( const Json::Value& a_Value );
bool LoadFromJson(const Json::Value& a_Value);
// cBlockEntity overrides:
virtual void SaveToJson(Json::Value& a_Value ) override;
virtual void SaveToJson(Json::Value & a_Value ) override;
virtual void SendTo(cClientHandle & a_Client) override;
virtual void UsedBy(cPlayer * a_Player); // tolua_export
virtual void UsedBy(cPlayer * a_Player);
/// Opens a new chest window for this chests. Scans for neighbors to open a double chest window, if appropriate.
void OpenNewWindow(void);
const cItemGrid & GetContents(void) const { return m_Contents; }
cItemGrid & GetContents(void) { return m_Contents; } // tolua_export
static const int c_ChestWidth = 9;
static const int c_ChestHeight = 3;
private:
cItemGrid m_Contents;
} ; // tolua_export

View File

@ -31,9 +31,19 @@
cDispenserEntity::cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World) :
cBlockEntity(E_BLOCK_DISPENSER, a_X, a_Y, a_Z, a_World),
m_Contents(3, 3),
cDispenserEntity::cDispenserEntity(int a_BlockX, int a_BlockY, int a_BlockZ) :
super(E_BLOCK_DISPENSER, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, NULL),
m_ShouldDispense(false)
{
SetBlockEntity(this); // cBlockEntityWindowOwner
}
cDispenserEntity::cDispenserEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) :
super(E_BLOCK_DISPENSER, a_BlockX, a_BlockY, a_BlockZ, ContentsWidth, ContentsHeight, a_World),
m_ShouldDispense(false)
{
SetBlockEntity(this); // cBlockEntityWindowOwner
@ -57,19 +67,6 @@ cDispenserEntity::~cDispenserEntity()
void cDispenserEntity::Destroy(void)
{
// Drop items
cItems Pickups;
m_Contents.CopyToItems(Pickups);
m_Contents.Clear();
m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ);
}
void cDispenserEntity::Dispense(void)
{
int Disp_X = m_PosX;
@ -97,7 +94,7 @@ void cDispenserEntity::Dispense(void)
// Pick an item to dispense:
MTRand r1;
int RandomSlot = r1.randInt(SlotsCnt);
int RandomSlot = r1.randInt(SlotsCnt - 1);
cItem & Drop = m_Contents.GetSlot(OccupiedSlots[RandomSlot]);
// Dispense the item:

View File

@ -1,9 +1,8 @@
#pragma once
#include "BlockEntity.h"
#include "BlockEntityWithItems.h"
#include "UI/WindowOwner.h"
#include "ItemGrid.h"
@ -21,14 +20,29 @@ class cServer;
// tolua_begin
class cDispenserEntity :
public cBlockEntity,
public cBlockEntityWithItems,
public cBlockEntityWindowOwner
{
typedef cBlockEntityWithItems super;
public:
cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World);
enum {
ContentsHeight = 3,
ContentsWidth = 3,
} ;
/// Constructor used while generating a chunk; sets m_World to NULL
cDispenserEntity(int a_BlockX, int a_BlockY, int a_BlockZ);
// tolua_end
/// Constructor used for normal operation
cDispenserEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World);
virtual ~cDispenserEntity();
virtual void Destroy(void);
static const char * GetClassStatic(void) { return "cDispenserEntity"; }
bool LoadFromJson(const Json::Value & a_Value);
@ -38,22 +52,18 @@ public:
virtual bool Tick(float a_Dt) override;
virtual void UsedBy(cPlayer * a_Player) override;
const cItem & GetSlot(int a_SlotNum) const { return m_Contents.GetSlot(a_SlotNum); }
void SetSlot(int a_SlotNum, const cItem & a_Item) { m_Contents.SetSlot(a_SlotNum, a_Item); }
// tolua_begin
/// Sets the dispenser to dispense an item in the next tick
void Activate(void);
const cItemGrid & GetContents(void) const { return m_Contents; }
cItemGrid & GetContents(void) { return m_Contents; }
// tolua_end
private:
cItemGrid m_Contents;
bool m_ShouldDispense; ///< If true, the dispenser will dispense an item in the next tick
bool m_ShouldDispense; ///< If true, the dispenser will dispense an item in the next tick
void Dispense(void);
};
} ; // tolua_export

View File

@ -391,6 +391,15 @@ int cItemGrid::GetFirstEmptySlot(void) const
int cItemGrid::GetFirstUsedSlot(void) const
{
return GetNextUsedSlot(-1);
}
int cItemGrid::GetLastEmptySlot(void) const
{
for (int i = m_NumSlots - 1; i >= 0; i--)
@ -407,6 +416,22 @@ int cItemGrid::GetLastEmptySlot(void) const
int cItemGrid::GetLastUsedSlot(void) const
{
for (int i = m_NumSlots - 1; i >= 0; i--)
{
if (!m_Slots[i].IsEmpty())
{
return i;
}
}
return -1;
}
int cItemGrid::GetNextEmptySlot(int a_StartFrom) const
{
for (int i = a_StartFrom + 1; i < m_NumSlots; i++)
@ -423,6 +448,22 @@ int cItemGrid::GetNextEmptySlot(int a_StartFrom) const
int cItemGrid::GetNextUsedSlot(int a_StartFrom) const
{
for (int i = a_StartFrom + 1; i < m_NumSlots; i++)
{
if (!m_Slots[i].IsEmpty())
{
return i;
}
}
return -1;
}
void cItemGrid::CopyToItems(cItems & a_Items) const
{
for (int i = 0; i < m_NumSlots; i++)

View File

@ -107,12 +107,21 @@ public:
/// Returns the index of the first empty slot; -1 if all full
int GetFirstEmptySlot(void) const;
/// Returns the index of the first non-empty slot; -1 if all empty
int GetFirstUsedSlot(void) const;
/// Returns the index of the last empty slot; -1 if all full
int GetLastEmptySlot(void) const;
/// Returns the index of the last used slot; -1 if all empty
int GetLastUsedSlot(void) const;
/// Returns the index of the first empty slot following a_StartFrom (a_StartFrom is not checked)
int GetNextEmptySlot(int a_StartFrom) const;
/// Returns the index of the first used slot following a_StartFrom (a_StartFrom is not checked)
int GetNextUsedSlot(int a_StartFrom) const;
/// Copies the contents into a cItems object; preserves the original a_Items contents
void CopyToItems(cItems & a_Items) const;