1
0

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
This commit is contained in:
madmaxoft@gmail.com 2013-05-28 18:50:44 +00:00
parent 5c1439e11c
commit 3138daa1f8
10 changed files with 72 additions and 51 deletions

View File

@ -28,6 +28,8 @@ protected:
m_PosX(a_BlockX), m_PosX(a_BlockX),
m_PosY(a_BlockY), m_PosY(a_BlockY),
m_PosZ(a_BlockZ), 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_BlockType(a_BlockType),
m_World(a_World) m_World(a_World)
{ {
@ -59,10 +61,14 @@ public:
int GetChunkX(void) const { return FAST_FLOOR_DIV(m_PosX, cChunkDef::Width); } 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 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 // tolua_end
virtual void SaveToJson (Json::Value & a_Value) = 0; 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; virtual void UsedBy( cPlayer * a_Player ) = 0;
/** Sends the packet defining the block entity to the client specified. /** Sends the packet defining the block entity to the client specified.
@ -71,12 +77,14 @@ public:
virtual void SendTo(cClientHandle & a_Client) = 0; 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. /// 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: protected:
int m_PosX; // Position in absolute block coordinates /// Position in absolute block coordinates
int m_PosY; int m_PosX, m_PosY, m_PosZ;
int m_PosZ;
/// Position relative to the chunk, used to speed up ticking
int m_RelX, m_RelZ;
BLOCKTYPE m_BlockType; BLOCKTYPE m_BlockType;

View File

@ -452,7 +452,7 @@ void cChunk::Tick(float a_Dt)
// Tick all block entities in this chunk: // Tick all block entities in this chunk:
for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) 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: // Tick all entities in this chunk:

View File

@ -4,6 +4,7 @@
#include "DispenserEntity.h" #include "DispenserEntity.h"
#include "Player.h" #include "Player.h"
#include "Simulator/FluidSimulator.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 DispY = m_PosY;
int DispZ = m_PosZ; int DispZ = m_RelZ;
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); 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: // Dispense the item:
switch (m_Contents.GetSlot(a_SlotNum).m_ItemType) switch (m_Contents.GetSlot(a_SlotNum).m_ItemType)
{ {
case E_ITEM_BUCKET: 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) switch (DispBlock)
{ {
case E_BLOCK_STATIONARY_WATER: case E_BLOCK_STATIONARY_WATER:
@ -50,7 +58,7 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum)
{ {
if (ScoopUpLiquid(a_SlotNum, E_ITEM_WATER_BUCKET)) 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; break;
} }
@ -59,13 +67,13 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum)
{ {
if (ScoopUpLiquid(a_SlotNum, E_ITEM_LAVA_BUCKET)) 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; break;
} }
default: default:
{ {
DropFromSlot(a_SlotNum); DropFromSlot(a_Chunk, a_SlotNum);
break; break;
} }
} }
@ -74,35 +82,37 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum)
case E_ITEM_WATER_BUCKET: case E_ITEM_WATER_BUCKET:
{ {
BLOCKTYPE DispBlock = m_World->GetBlock(DispX, DispY, DispZ); LOGD("Dispensing water bucket in slot %d; DispBlock is \"%s\" (%d).", a_SlotNum, ItemTypeToString(DispBlock).c_str(), DispBlock);
if (PlaceLiquid(DispBlock, a_SlotNum)) 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 else
{ {
DropFromSlot(a_SlotNum); DropFromSlot(a_Chunk, a_SlotNum);
} }
break; break;
} }
case E_ITEM_LAVA_BUCKET: case E_ITEM_LAVA_BUCKET:
{ {
BLOCKTYPE DispBlock = m_World->GetBlock(DispX, DispY, DispZ); LOGD("Dispensing lava bucket in slot %d; DispBlock is \"%s\" (%d).", a_SlotNum, ItemTypeToString(DispBlock).c_str(), DispBlock);
if (PlaceLiquid(DispBlock, a_SlotNum)) 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 else
{ {
DropFromSlot(a_SlotNum); DropFromSlot(a_Chunk, a_SlotNum);
} }
break; break;
} }
case E_ITEM_SPAWN_EGG: 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); m_Contents.ChangeSlotCount(a_SlotNum, -1);
} }
@ -111,7 +121,7 @@ void cDispenserEntity::DropSpenseFromSlot(int a_SlotNum)
default: default:
{ {
DropFromSlot(a_SlotNum); DropFromSlot(a_Chunk, a_SlotNum);
break; break;
} }
} // switch (ItemType) } // 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 ( if (
(a_BlockInFront != E_BLOCK_AIR) && (a_BlockInFront != E_BLOCK_AIR) &&

View File

@ -27,13 +27,13 @@ public:
private: private:
// cDropSpenser overrides: // 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 /// If such a bucket can fit, adds it to m_Contents and returns true
bool ScoopUpLiquid(int a_SlotNum, short a_BucketItemType); 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 /// 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 } ; // tolua_export

View File

@ -7,6 +7,7 @@
#include "Globals.h" #include "Globals.h"
#include "DropSpenserEntity.h" #include "DropSpenserEntity.h"
#include "Player.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: // Pick one of the occupied slots:
int OccupiedSlots[9]; int OccupiedSlots[9];
int SlotsCnt = 0; int SlotsCnt = 0;
@ -86,16 +82,17 @@ void cDropSpenserEntity::DropSpense(void)
int RandomSlot = m_World->GetTickRandomNumber(SlotsCnt - 1); int RandomSlot = m_World->GetTickRandomNumber(SlotsCnt - 1);
// DropSpense the item, using the specialized behavior in the subclasses: // DropSpense the item, using the specialized behavior in the subclasses:
DropSpenseFromSlot(OccupiedSlots[RandomSlot]); DropSpenseFromSlot(a_Chunk, OccupiedSlots[RandomSlot]);
// Broadcast a smoke and click effects: // 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) switch (Meta)
{ {
case 2: SmokeDir = 1; break; case E_META_DISPENSER_FACING_XM: SmokeDir = 3; break;
case 3: SmokeDir = 7; break; case E_META_DISPENSER_FACING_XP: SmokeDir = 5; break;
case 4: SmokeDir = 3; break; case E_META_DISPENSER_FACING_ZM: SmokeDir = 1; break;
case 5: SmokeDir = 5; 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->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); 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) if (!m_ShouldDropSpense)
{ {
@ -142,7 +139,7 @@ bool cDropSpenserEntity::Tick(float a_Dt)
} }
m_ShouldDropSpense = false; m_ShouldDropSpense = false;
DropSpense(); DropSpense(a_Chunk);
return true; 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 DispX = m_PosX;
int DispY = m_PosY; int DispY = m_PosY;
int DispZ = m_PosZ; 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); AddDropSpenserDir(DispX, DispY, DispZ, Meta);
cItems Pickups; cItems Pickups;

View File

@ -53,7 +53,7 @@ public:
// cBlockEntity overrides: // cBlockEntity overrides:
virtual void SaveToJson(Json::Value & a_Value) override; 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 SendTo(cClientHandle & a_Client) override;
virtual void UsedBy(cPlayer * a_Player) 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. 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 /// 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 / ...) /// 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) /// 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 } ; // tolua_export

View File

@ -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);
} }

View File

@ -35,7 +35,13 @@ public:
protected: protected:
// cDropSpenserEntity overrides: // 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 } ; // tolua_export

View File

@ -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: // DEBUG:

View File

@ -37,7 +37,7 @@ public:
// cBlockEntity overrides: // 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 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; virtual void UsedBy(cPlayer * a_Player) override;
bool StartCooking(void); bool StartCooking(void);