From 3138daa1f8902a9e57d8ff2aa2951a194808b8ae Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 28 May 2013 18:50:44 +0000 Subject: Block entities now receive the cChunk param in their Tick() function They can safely access that chunk and any of its neighbors during ticking. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1526 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/BlockEntity.h | 16 +++++++++++---- source/Chunk.cpp | 2 +- source/DispenserEntity.cpp | 48 ++++++++++++++++++++++++++------------------ source/DispenserEntity.h | 4 ++-- source/DropSpenserEntity.cpp | 29 ++++++++++++-------------- source/DropSpenserEntity.h | 8 ++++---- source/DropperEntity.cpp | 4 ++-- source/DropperEntity.h | 8 +++++++- source/FurnaceEntity.cpp | 2 +- source/FurnaceEntity.h | 2 +- 10 files changed, 72 insertions(+), 51 deletions(-) (limited to 'source') diff --git a/source/BlockEntity.h b/source/BlockEntity.h index 05818437b..a1dba82a6 100644 --- a/source/BlockEntity.h +++ b/source/BlockEntity.h @@ -28,6 +28,8 @@ protected: m_PosX(a_BlockX), m_PosY(a_BlockY), m_PosZ(a_BlockZ), + m_RelX(a_BlockX - cChunkDef::Width * FAST_FLOOR_DIV(a_BlockX, cChunkDef::Width)), + m_RelZ(a_BlockZ - cChunkDef::Width * FAST_FLOOR_DIV(a_BlockZ, cChunkDef::Width)), m_BlockType(a_BlockType), m_World(a_World) { @@ -59,10 +61,14 @@ public: int GetChunkX(void) const { return FAST_FLOOR_DIV(m_PosX, cChunkDef::Width); } int GetChunkZ(void) const { return FAST_FLOOR_DIV(m_PosZ, cChunkDef::Width); } + int GetRelX(void) const { return m_RelX; } + int GetRelZ(void) const { return m_RelZ; } + // tolua_end virtual void SaveToJson (Json::Value & a_Value) = 0; + /// Called when a player uses this entity; should open the UI window virtual void UsedBy( cPlayer * a_Player ) = 0; /** Sends the packet defining the block entity to the client specified. @@ -71,12 +77,14 @@ public: virtual void SendTo(cClientHandle & a_Client) = 0; /// Ticks the entity; returns true if the chunk should be marked as dirty as a result of this ticking. By default does nothing. - virtual bool Tick(float a_Dt) { return false; } + virtual bool Tick(float a_Dt, cChunk & a_Chunk) { return false; } protected: - int m_PosX; // Position in absolute block coordinates - int m_PosY; - int m_PosZ; + /// Position in absolute block coordinates + int m_PosX, m_PosY, m_PosZ; + + /// Position relative to the chunk, used to speed up ticking + int m_RelX, m_RelZ; BLOCKTYPE m_BlockType; diff --git a/source/Chunk.cpp b/source/Chunk.cpp index a119c7c61..56741e3f7 100644 --- a/source/Chunk.cpp +++ b/source/Chunk.cpp @@ -452,7 +452,7 @@ void cChunk::Tick(float a_Dt) // Tick all block entities in this chunk: for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) { - m_IsDirty = (*itr)->Tick(a_Dt) | m_IsDirty; + m_IsDirty = (*itr)->Tick(a_Dt, *this) | m_IsDirty; } // Tick all entities in this chunk: diff --git a/source/DispenserEntity.cpp b/source/DispenserEntity.cpp index 8d64c01e0..6ccd66727 100644 --- a/source/DispenserEntity.cpp +++ b/source/DispenserEntity.cpp @@ -4,6 +4,7 @@ #include "DispenserEntity.h" #include "Player.h" #include "Simulator/FluidSimulator.h" +#include "Chunk.h" @@ -29,20 +30,27 @@ cDispenserEntity::cDispenserEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWo -void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum) +void cDispenserEntity::DropSpenseFromSlot(cChunk & a_Chunk, int a_SlotNum) { - int DispX = m_PosX; + int DispX = m_RelX; int DispY = m_PosY; - int DispZ = m_PosZ; - NIBBLETYPE Meta = m_World->GetBlockMeta(m_PosX, m_PosY, m_PosZ); + int DispZ = m_RelZ; + NIBBLETYPE Meta = a_Chunk.GetMeta(m_RelX, m_PosY, m_RelZ); AddDropSpenserDir(DispX, DispY, DispZ, Meta); + cChunk * DispChunk = a_Chunk.GetRelNeighborChunkAdjustCoords(DispX, DispZ); + if (DispChunk == NULL) + { + // Would dispense into / interact with a non-loaded chunk, ignore the tick + return; + } + BLOCKTYPE DispBlock = DispChunk->GetBlock(DispX, DispY, DispZ); // Dispense the item: switch (m_Contents.GetSlot(a_SlotNum).m_ItemType) { case E_ITEM_BUCKET: { - BLOCKTYPE DispBlock = m_World->GetBlock(DispX, DispY, DispZ); + LOGD("Dispensing empty bucket in slot %d; DispBlock is \"%s\" (%d).", a_SlotNum, ItemTypeToString(DispBlock).c_str(), DispBlock); switch (DispBlock) { case E_BLOCK_STATIONARY_WATER: @@ -50,7 +58,7 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum) { if (ScoopUpLiquid(a_SlotNum, E_ITEM_WATER_BUCKET)) { - m_World->SetBlock(DispX, DispY, DispZ, E_BLOCK_AIR, 0); + DispChunk->SetBlock(DispX, DispY, DispZ, E_BLOCK_AIR, 0); } break; } @@ -59,13 +67,13 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum) { if (ScoopUpLiquid(a_SlotNum, E_ITEM_LAVA_BUCKET)) { - m_World->SetBlock(DispX, DispY, DispZ, E_BLOCK_AIR, 0); + DispChunk->SetBlock(DispX, DispY, DispZ, E_BLOCK_AIR, 0); } break; } default: { - DropFromSlot(a_SlotNum); + DropFromSlot(a_Chunk, a_SlotNum); break; } } @@ -74,35 +82,37 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum) case E_ITEM_WATER_BUCKET: { - BLOCKTYPE DispBlock = m_World->GetBlock(DispX, DispY, DispZ); - if (PlaceLiquid(DispBlock, a_SlotNum)) + LOGD("Dispensing water bucket in slot %d; DispBlock is \"%s\" (%d).", a_SlotNum, ItemTypeToString(DispBlock).c_str(), DispBlock); + if (EmptyLiquidBucket(DispBlock, a_SlotNum)) { - m_World->SetBlock(DispX, DispY, DispZ, E_BLOCK_WATER, 0); + DispChunk->SetBlock(DispX, DispY, DispZ, E_BLOCK_WATER, 0); } else { - DropFromSlot(a_SlotNum); + DropFromSlot(a_Chunk, a_SlotNum); } break; } case E_ITEM_LAVA_BUCKET: { - BLOCKTYPE DispBlock = m_World->GetBlock(DispX, DispY, DispZ); - if (PlaceLiquid(DispBlock, a_SlotNum)) + LOGD("Dispensing lava bucket in slot %d; DispBlock is \"%s\" (%d).", a_SlotNum, ItemTypeToString(DispBlock).c_str(), DispBlock); + if (EmptyLiquidBucket(DispBlock, a_SlotNum)) { - m_World->SetBlock(DispX, DispY, DispZ, E_BLOCK_LAVA, 0); + DispChunk->SetBlock(DispX, DispY, DispZ, E_BLOCK_LAVA, 0); } else { - DropFromSlot(a_SlotNum); + DropFromSlot(a_Chunk, a_SlotNum); } break; } case E_ITEM_SPAWN_EGG: { - if (m_World->SpawnMob(DispX + 0.5, DispY, DispZ + 0.5, m_Contents.GetSlot(a_SlotNum).m_ItemDamage) >= 0) + double MobX = 0.5 + (DispX + DispChunk->GetPosX() * cChunkDef::Width); + double MobZ = 0.5 + (DispZ + DispChunk->GetPosZ() * cChunkDef::Width); + if (m_World->SpawnMob(MobX, DispY, MobZ, m_Contents.GetSlot(a_SlotNum).m_ItemDamage) >= 0) { m_Contents.ChangeSlotCount(a_SlotNum, -1); } @@ -111,7 +121,7 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum) default: { - DropFromSlot(a_SlotNum); + DropFromSlot(a_Chunk, a_SlotNum); break; } } // switch (ItemType) @@ -148,7 +158,7 @@ bool cDispenserEntity::ScoopUpLiquid(int a_SlotNum, short a_BucketItemType) -bool cDispenserEntity::PlaceLiquid(BLOCKTYPE a_BlockInFront, int a_SlotNum) +bool cDispenserEntity::EmptyLiquidBucket(BLOCKTYPE a_BlockInFront, int a_SlotNum) { if ( (a_BlockInFront != E_BLOCK_AIR) && diff --git a/source/DispenserEntity.h b/source/DispenserEntity.h index ad6400921..09270e7f8 100644 --- a/source/DispenserEntity.h +++ b/source/DispenserEntity.h @@ -27,13 +27,13 @@ public: private: // cDropSpenser overrides: - virtual void DropSpenseFromSlot(int a_SlotNum) override; + virtual void DropSpenseFromSlot(cChunk & a_Chunk, int a_SlotNum) override; /// If such a bucket can fit, adds it to m_Contents and returns true bool ScoopUpLiquid(int a_SlotNum, short a_BucketItemType); /// If the a_BlockInFront is liquidable and the empty bucket can fit, does the m_Contents processing and returns true - bool PlaceLiquid(BLOCKTYPE a_BlockInFront, int a_SlotNum); + bool EmptyLiquidBucket(BLOCKTYPE a_BlockInFront, int a_SlotNum); } ; // tolua_export diff --git a/source/DropSpenserEntity.cpp b/source/DropSpenserEntity.cpp index 85eaa8297..0d07ec27b 100644 --- a/source/DropSpenserEntity.cpp +++ b/source/DropSpenserEntity.cpp @@ -7,6 +7,7 @@ #include "Globals.h" #include "DropSpenserEntity.h" #include "Player.h" +#include "Chunk.h" @@ -57,13 +58,8 @@ void cDropSpenserEntity::AddDropSpenserDir(int & a_BlockX, int & a_BlockY, int & -void cDropSpenserEntity::DropSpense(void) +void cDropSpenserEntity::DropSpense(cChunk & a_Chunk) { - int Disp_X = m_PosX; - int Disp_Y = m_PosY; - int Disp_Z = m_PosZ; - NIBBLETYPE Meta = m_World->GetBlockMeta(m_PosX, m_PosY, m_PosZ); - // Pick one of the occupied slots: int OccupiedSlots[9]; int SlotsCnt = 0; @@ -86,16 +82,17 @@ void cDropSpenserEntity::DropSpense(void) int RandomSlot = m_World->GetTickRandomNumber(SlotsCnt - 1); // DropSpense the item, using the specialized behavior in the subclasses: - DropSpenseFromSlot(OccupiedSlots[RandomSlot]); + DropSpenseFromSlot(a_Chunk, OccupiedSlots[RandomSlot]); // Broadcast a smoke and click effects: - NIBBLETYPE SmokeDir = 0; + NIBBLETYPE Meta = a_Chunk.GetMeta(m_RelX, m_PosY, m_RelZ); + int SmokeDir = 0; switch (Meta) { - case 2: SmokeDir = 1; break; - case 3: SmokeDir = 7; break; - case 4: SmokeDir = 3; break; - case 5: SmokeDir = 5; break; + case E_META_DISPENSER_FACING_XM: SmokeDir = 3; break; + case E_META_DISPENSER_FACING_XP: SmokeDir = 5; break; + case E_META_DISPENSER_FACING_ZM: SmokeDir = 1; break; + case E_META_DISPENSER_FACING_ZP: SmokeDir = 7; break; } m_World->BroadcastSoundParticleEffect(2000, m_PosX * 8, m_PosY * 8, m_PosZ * 8, SmokeDir); m_World->BroadcastSoundEffect("random.click", m_PosX * 8, m_PosY * 8, m_PosZ * 8, 1.0f, 1.0f); @@ -134,7 +131,7 @@ void cDropSpenserEntity::SetRedstonePower(bool a_IsPowered) -bool cDropSpenserEntity::Tick(float a_Dt) +bool cDropSpenserEntity::Tick(float a_Dt, cChunk & a_Chunk) { if (!m_ShouldDropSpense) { @@ -142,7 +139,7 @@ bool cDropSpenserEntity::Tick(float a_Dt) } m_ShouldDropSpense = false; - DropSpense(); + DropSpense(a_Chunk); return true; } @@ -231,12 +228,12 @@ void cDropSpenserEntity::UsedBy(cPlayer * a_Player) -void cDropSpenserEntity::DropFromSlot(int a_SlotNum) +void cDropSpenserEntity::DropFromSlot(cChunk & a_Chunk, int a_SlotNum) { int DispX = m_PosX; int DispY = m_PosY; int DispZ = m_PosZ; - NIBBLETYPE Meta = m_World->GetBlockMeta(m_PosX, m_PosY, m_PosZ); + NIBBLETYPE Meta = a_Chunk.GetMeta(m_RelX, m_PosY, m_RelZ); AddDropSpenserDir(DispX, DispY, DispZ, Meta); cItems Pickups; diff --git a/source/DropSpenserEntity.h b/source/DropSpenserEntity.h index d58f74f04..4918f8dfe 100644 --- a/source/DropSpenserEntity.h +++ b/source/DropSpenserEntity.h @@ -53,7 +53,7 @@ public: // cBlockEntity overrides: virtual void SaveToJson(Json::Value & a_Value) override; - virtual bool Tick(float a_Dt) override; + virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; virtual void SendTo(cClientHandle & a_Client) override; virtual void UsedBy(cPlayer * a_Player) override; @@ -75,13 +75,13 @@ protected: bool m_IsPowered; ///< Set to true when the dropspenser receives redstone power. /// Does the actual work on dropspensing an item. Chooses the slot, calls DropSpenseFromSlot() and handles smoke / sound effects - void DropSpense(void); + void DropSpense(cChunk & a_Chunk); /// Override this function to provide the specific behavior for item dropspensing (drop / shoot / pour / ...) - virtual void DropSpenseFromSlot(int a_SlotNum) = 0; + virtual void DropSpenseFromSlot(cChunk & a_Chunk, int a_SlotNum) = 0; /// Helper function, drops one item from the specified slot (like a dropper) - void DropFromSlot(int a_SlotNum); + void DropFromSlot(cChunk & a_Chunk, int a_SlotNum); } ; // tolua_export diff --git a/source/DropperEntity.cpp b/source/DropperEntity.cpp index 39c2cf3bd..be2726cfa 100644 --- a/source/DropperEntity.cpp +++ b/source/DropperEntity.cpp @@ -32,9 +32,9 @@ cDropperEntity::cDropperEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld -void cDropperEntity::DropSpenseFromSlot(int a_SlotNum) +void cDropperEntity::DropSpenseFromSlot(cChunk & a_Chunk, int a_SlotNum) { - DropFromSlot(a_SlotNum); + DropFromSlot(a_Chunk, a_SlotNum); } diff --git a/source/DropperEntity.h b/source/DropperEntity.h index 5f338dee8..ed29bfe95 100644 --- a/source/DropperEntity.h +++ b/source/DropperEntity.h @@ -35,7 +35,13 @@ public: protected: // cDropSpenserEntity overrides: - virtual void DropSpenseFromSlot(int a_SlotNum) override; + virtual void DropSpenseFromSlot(cChunk & a_Chunk, int a_SlotNum) override; + + /** Takes an item from slot a_SlotNum and puts it into the container in front of the dropper. + Called when there's a container directly in front of the dropper, + so the dropper should store items there, rather than dropping. + */ + void PutIntoContainer(cChunk & a_Chunk, int a_SlotNum, BLOCKTYPE a_ContainerBlock, int a_ContainerX, int a_ContainerY, int a_ContainerZ); } ; // tolua_export diff --git a/source/FurnaceEntity.cpp b/source/FurnaceEntity.cpp index f6727c555..f4cefc56a 100644 --- a/source/FurnaceEntity.cpp +++ b/source/FurnaceEntity.cpp @@ -103,7 +103,7 @@ void cFurnaceEntity::UsedBy(cPlayer * a_Player) -bool cFurnaceEntity::Tick( float a_Dt ) +bool cFurnaceEntity::Tick(float a_Dt, cChunk & a_Chunk) { /* // DEBUG: diff --git a/source/FurnaceEntity.h b/source/FurnaceEntity.h index 8373116d4..5bbbef32b 100644 --- a/source/FurnaceEntity.h +++ b/source/FurnaceEntity.h @@ -37,7 +37,7 @@ public: // cBlockEntity overrides: virtual void SaveToJson(Json::Value & a_Value) override; virtual void SendTo(cClientHandle & a_Client) override; - virtual bool Tick(float a_Dt) override; + virtual bool Tick(float a_Dt, cChunk & a_Chunk) override; virtual void UsedBy(cPlayer * a_Player) override; bool StartCooking(void); -- cgit v1.2.3