diff options
author | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2013-05-25 13:59:13 +0200 |
---|---|---|
committer | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2013-05-25 13:59:13 +0200 |
commit | c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924 (patch) | |
tree | 567565c41bab4e0fbab4eee5e3d2e647a896772c /source | |
parent | Fixed the cWorld:DoWithChestAt(), DoWithDispenserAt() and DoWithFurnaceAt() callbacks binding. (diff) | |
download | cuberite-c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924.tar cuberite-c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924.tar.gz cuberite-c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924.tar.bz2 cuberite-c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924.tar.lz cuberite-c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924.tar.xz cuberite-c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924.tar.zst cuberite-c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924.zip |
Diffstat (limited to 'source')
-rw-r--r-- | source/BlockEntity.h | 19 | ||||
-rw-r--r-- | source/BlockEntityWithItems.h | 71 | ||||
-rw-r--r-- | source/ChestEntity.cpp | 19 | ||||
-rw-r--r-- | source/ChestEntity.h | 43 | ||||
-rw-r--r-- | source/DispenserEntity.cpp | 31 | ||||
-rw-r--r-- | source/DispenserEntity.h | 38 | ||||
-rw-r--r-- | source/ItemGrid.cpp | 41 | ||||
-rw-r--r-- | source/ItemGrid.h | 9 |
8 files changed, 186 insertions, 85 deletions
diff --git a/source/BlockEntity.h b/source/BlockEntity.h index b3273b9c9..0cc7f6077 100644 --- a/source/BlockEntity.h +++ b/source/BlockEntity.h @@ -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; } @@ -60,6 +55,8 @@ public: BLOCKTYPE GetBlockType(void) const { return m_BlockType; } cWorld * GetWorld(void) const {return m_World; } + + // tolua_end virtual void SaveToJson (Json::Value & a_Value) = 0; @@ -81,7 +78,7 @@ protected: BLOCKTYPE m_BlockType; cWorld * m_World; -}; +} ; // tolua_export diff --git a/source/BlockEntityWithItems.h b/source/BlockEntityWithItems.h new file mode 100644 index 000000000..e5753359d --- /dev/null +++ b/source/BlockEntityWithItems.h @@ -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
+
+
+
+
diff --git a/source/ChestEntity.cpp b/source/ChestEntity.cpp index e0f818de0..45c543538 100644 --- a/source/ChestEntity.cpp +++ b/source/ChestEntity.cpp @@ -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(); diff --git a/source/ChestEntity.h b/source/ChestEntity.h index 69e86b86c..60fd7589a 100644 --- a/source/ChestEntity.h +++ b/source/ChestEntity.h @@ -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 diff --git a/source/DispenserEntity.cpp b/source/DispenserEntity.cpp index 1c95172e2..ccf4e0f95 100644 --- a/source/DispenserEntity.cpp +++ b/source/DispenserEntity.cpp @@ -31,9 +31,8 @@ -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
@@ -43,27 +42,25 @@ cDispenserEntity::cDispenserEntity(int a_X, int a_Y, int a_Z, cWorld * a_World) -cDispenserEntity::~cDispenserEntity()
+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)
{
- // Tell window its owner is destroyed
- cWindow * Window = GetWindow();
- if (Window != NULL)
- {
- Window->OwnerDestroyed();
- }
+ SetBlockEntity(this); // cBlockEntityWindowOwner
}
-void cDispenserEntity::Destroy(void)
+cDispenserEntity::~cDispenserEntity()
{
- // Drop items
- cItems Pickups;
- m_Contents.CopyToItems(Pickups);
- m_Contents.Clear();
- m_World->SpawnItemPickups(Pickups, m_PosX, m_PosY, m_PosZ);
+ // Tell window its owner is destroyed
+ cWindow * Window = GetWindow();
+ if (Window != NULL)
+ {
+ Window->OwnerDestroyed();
+ }
}
@@ -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:
diff --git a/source/DispenserEntity.h b/source/DispenserEntity.h index ad755c7db..315452094 100644 --- a/source/DispenserEntity.h +++ b/source/DispenserEntity.h @@ -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); }
+ // tolua_begin
- void SetSlot(int a_SlotNum, const cItem & a_Item) { m_Contents.SetSlot(a_SlotNum, a_Item); }
-
/// 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
diff --git a/source/ItemGrid.cpp b/source/ItemGrid.cpp index f1330dd56..0495105f2 100644 --- a/source/ItemGrid.cpp +++ b/source/ItemGrid.cpp @@ -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++)
diff --git a/source/ItemGrid.h b/source/ItemGrid.h index de8be059a..22d64f076 100644 --- a/source/ItemGrid.h +++ b/source/ItemGrid.h @@ -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;
|