From c640e9346c5e6c72b62b1beeaf5ec6de9cdc6924 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sat, 25 May 2013 11:59:13 +0000 Subject: 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 --- VC2008/MCServer.vcproj | 112 ++++++++++++++++++++++-------------------- source/BlockEntity.h | 19 +++---- source/BlockEntityWithItems.h | 71 ++++++++++++++++++++++++++ source/ChestEntity.cpp | 19 +------ source/ChestEntity.h | 43 +++++++--------- source/DispenserEntity.cpp | 31 ++++++------ source/DispenserEntity.h | 38 ++++++++------ source/ItemGrid.cpp | 41 ++++++++++++++++ source/ItemGrid.h | 9 ++++ 9 files changed, 246 insertions(+), 137 deletions(-) create mode 100644 source/BlockEntityWithItems.h diff --git a/VC2008/MCServer.vcproj b/VC2008/MCServer.vcproj index 9a78c96fb..cf83f7281 100644 --- a/VC2008/MCServer.vcproj +++ b/VC2008/MCServer.vcproj @@ -932,26 +932,6 @@ - - - - - - - - - - @@ -972,22 +952,6 @@ RelativePath="..\source\FallingBlock.h" > - - - - - - - - @@ -1000,14 +964,6 @@ RelativePath="..\source\Minecart.h" > - - - - @@ -1036,14 +992,6 @@ RelativePath="..\source\Sign.h" > - - - - @@ -2345,6 +2293,66 @@ > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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; -- cgit v1.2.3