1
0

Floody fluid simulator has been converted to use direct chunk access

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1236 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2013-03-02 15:44:31 +00:00
parent 476cd25ade
commit 66670f5d5c
7 changed files with 236 additions and 142 deletions

View File

@ -29,6 +29,7 @@
#include "BlockArea.h" #include "BlockArea.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "Blocks/BlockHandler.h" #include "Blocks/BlockHandler.h"
#include "Simulator/FluidSimulator.h"
#include <json/json.h> #include <json/json.h>
@ -78,6 +79,8 @@ cChunk::cChunk(
, m_NeighborXP(a_NeighborXP) , m_NeighborXP(a_NeighborXP)
, m_NeighborZM(a_NeighborZM) , m_NeighborZM(a_NeighborZM)
, m_NeighborZP(a_NeighborZP) , m_NeighborZP(a_NeighborZP)
, m_WaterSimulatorData(a_World->GetWaterSimulator()->CreateChunkData())
, m_LavaSimulatorData (a_World->GetLavaSimulator ()->CreateChunkData())
{ {
if (a_NeighborXM != NULL) if (a_NeighborXM != NULL)
{ {
@ -145,6 +148,8 @@ cChunk::~cChunk()
{ {
m_NeighborZP->m_NeighborZM = NULL; m_NeighborZP->m_NeighborZM = NULL;
} }
delete m_WaterSimulatorData;
delete m_LavaSimulatorData;
} }

View File

@ -47,6 +47,7 @@ class cPawn;
class cPickup; class cPickup;
class cChunkDataSerializer; class cChunkDataSerializer;
class cBlockArea; class cBlockArea;
class cFluidSimulatorData;
typedef std::list<cClientHandle *> cClientHandleList; typedef std::list<cClientHandle *> cClientHandleList;
typedef cItemCallback<cEntity> cEntityCallback; typedef cItemCallback<cEntity> cEntityCallback;
@ -267,7 +268,9 @@ public:
bool UnboundedRelFastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta); bool UnboundedRelFastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta);
// Simulator data: // Simulator data:
cFireSimulatorChunkData & GetFireSimulatorData(void) { return m_FireSimulatorData; } cFireSimulatorChunkData & GetFireSimulatorData (void) { return m_FireSimulatorData; }
cFluidSimulatorData * GetWaterSimulatorData(void) { return m_WaterSimulatorData; }
cFluidSimulatorData * GetLavaSimulatorData (void) { return m_LavaSimulatorData; }
private: private:
@ -312,7 +315,10 @@ private:
cChunk * m_NeighborZM; // Neighbor at [X, Z - 1] cChunk * m_NeighborZM; // Neighbor at [X, Z - 1]
cChunk * m_NeighborZP; // Neighbor at [X, Z + 1] cChunk * m_NeighborZP; // Neighbor at [X, Z + 1]
// Per-chunk simulator data:
cFireSimulatorChunkData m_FireSimulatorData; cFireSimulatorChunkData m_FireSimulatorData;
cFluidSimulatorData * m_WaterSimulatorData;
cFluidSimulatorData * m_LavaSimulatorData;
void RemoveBlockEntity(cBlockEntity * a_BlockEntity); void RemoveBlockEntity(cBlockEntity * a_BlockEntity);

View File

@ -8,25 +8,25 @@
#include "DelayedFluidSimulator.h" #include "DelayedFluidSimulator.h"
#include "../World.h" #include "../World.h"
#include "../Chunk.h"
cDelayedFluidSimulator::cDelayedFluidSimulator(cWorld & a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid, int a_TickDelay) : ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
super(a_World, a_Fluid, a_StationaryFluid), // cDelayedFluidSimulatorChunkData:
m_TickDelay(a_TickDelay),
m_Slots(NULL), cDelayedFluidSimulatorChunkData::cDelayedFluidSimulatorChunkData(int a_TickDelay) :
m_CurrentSlotNum(0) m_Slots(new cCoordWithIntList[a_TickDelay])
{ {
m_Slots = new CoordsArray[a_TickDelay];
} }
cDelayedFluidSimulator::~cDelayedFluidSimulator() cDelayedFluidSimulatorChunkData::~cDelayedFluidSimulatorChunkData()
{ {
delete[] m_Slots; delete[] m_Slots;
m_Slots = NULL; m_Slots = NULL;
@ -36,6 +36,22 @@ cDelayedFluidSimulator::~cDelayedFluidSimulator()
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cDelayedFluidSimulator:
cDelayedFluidSimulator::cDelayedFluidSimulator(cWorld & a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid, int a_TickDelay) :
super(a_World, a_Fluid, a_StationaryFluid),
m_TickDelay(a_TickDelay),
m_AddSlotNum(a_TickDelay - 1),
m_SimSlotNum(0),
m_TotalBlocks(0)
{
}
void cDelayedFluidSimulator::AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) void cDelayedFluidSimulator::AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk)
{ {
if ((a_BlockY < 0) || (a_BlockY >= cChunkDef::Height)) if ((a_BlockY < 0) || (a_BlockY >= cChunkDef::Height))
@ -44,25 +60,29 @@ void cDelayedFluidSimulator::AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ,
return; return;
} }
// TODO: This can be optimized: int RelX = a_BlockX - a_Chunk->GetPosX() * cChunkDef::Width;
BLOCKTYPE BlockType = m_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ); int RelZ = a_BlockZ - a_Chunk->GetPosZ() * cChunkDef::Width;
BLOCKTYPE BlockType = a_Chunk->GetBlock(RelX, a_BlockY, RelZ);
if (BlockType != m_FluidBlock) if (BlockType != m_FluidBlock)
{ {
return; return;
} }
CoordsArray & Blocks = m_Slots[m_CurrentSlotNum]; void * ChunkDataRaw = (m_FluidBlock == E_BLOCK_WATER) ? a_Chunk->GetWaterSimulatorData() : a_Chunk->GetLavaSimulatorData();
cDelayedFluidSimulatorChunkData * ChunkData = (cDelayedFluidSimulatorChunkData *)ChunkDataRaw;
cCoordWithIntList & Blocks = ChunkData->m_Slots[m_AddSlotNum];
// Check for duplicates: // Check for duplicates:
for (CoordsArray::iterator itr = Blocks.begin(), end = Blocks.end(); itr != end; ++itr) for (cCoordWithIntList::iterator itr = Blocks.begin(), end = Blocks.end(); itr != end; ++itr)
{ {
if ((itr->x == a_BlockX) && (itr->y == a_BlockY) && (itr->z == a_BlockZ)) if ((itr->x == RelX) && (itr->y == a_BlockY) && (itr->z == RelZ))
{ {
return; return;
} }
} }
Blocks.push_back(Vector3i(a_BlockX, a_BlockY, a_BlockZ)); ++m_TotalBlocks;
Blocks.push_back(cCoordWithInt(RelX, a_BlockY, RelZ));
} }
@ -71,22 +91,35 @@ void cDelayedFluidSimulator::AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ,
void cDelayedFluidSimulator::Simulate(float a_Dt) void cDelayedFluidSimulator::Simulate(float a_Dt)
{ {
int SlotNum = m_CurrentSlotNum + 1; if (m_TotalBlocks > 0)
if (SlotNum >= m_TickDelay)
{ {
SlotNum = 0; LOGD("DFS: %d blocks in queue", m_TotalBlocks);
} }
m_AddSlotNum = m_SimSlotNum;
CoordsArray & Blocks = m_Slots[SlotNum]; m_SimSlotNum += 1;
if (m_SimSlotNum >= m_TickDelay)
// Simulate the blocks in the scheduled slot:
for (CoordsArray::iterator itr = Blocks.begin(), end = Blocks.end(); itr != end; ++itr)
{ {
SimulateBlock(itr->x, itr->y, itr->z); m_SimSlotNum = 0;
} }
Blocks.clear(); }
m_CurrentSlotNum = SlotNum;
void cDelayedFluidSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk)
{
void * ChunkDataRaw = (m_FluidBlock == E_BLOCK_WATER) ? a_Chunk->GetWaterSimulatorData() : a_Chunk->GetLavaSimulatorData();
cDelayedFluidSimulatorChunkData * ChunkData = (cDelayedFluidSimulatorChunkData *)ChunkDataRaw;
cCoordWithIntList & Blocks = ChunkData->m_Slots[m_SimSlotNum];
// Simulate the blocks in the scheduled slot:
for (cCoordWithIntList::iterator itr = Blocks.begin(), end = Blocks.end(); itr != end; ++itr)
{
SimulateBlock(a_Chunk, itr->x, itr->y, itr->z);
}
m_TotalBlocks -= Blocks.size();
Blocks.clear();
} }

View File

@ -15,6 +15,21 @@
class cDelayedFluidSimulatorChunkData :
public cFluidSimulatorData
{
public:
cDelayedFluidSimulatorChunkData(int a_TickDelay);
virtual ~cDelayedFluidSimulatorChunkData();
/// Slots, one for each delay tick, each containing the blocks to simulate; relative coords. Int param not used.
cCoordWithIntList * m_Slots;
} ;
class cDelayedFluidSimulator : class cDelayedFluidSimulator :
public cFluidSimulator public cFluidSimulator
{ {
@ -22,27 +37,29 @@ class cDelayedFluidSimulator :
public: public:
cDelayedFluidSimulator(cWorld & a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid, int a_TickDelay); cDelayedFluidSimulator(cWorld & a_World, BLOCKTYPE a_Fluid, BLOCKTYPE a_StationaryFluid, int a_TickDelay);
virtual ~cDelayedFluidSimulator();
// cSimulator overrides: // cSimulator overrides:
virtual void AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override; virtual void AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk) override;
virtual void Simulate(float a_Dt) override; virtual void Simulate(float a_Dt) override;
virtual void SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk) override;
virtual cFluidSimulatorData * CreateChunkData(void) override { return new cDelayedFluidSimulatorChunkData(m_TickDelay); }
protected: protected:
typedef std::vector<Vector3i> CoordsArray;
int m_TickDelay; // Count of the m_Slots array int m_TickDelay; // Count of the m_Slots array in each ChunkData
CoordsArray * m_Slots; // Slots, one for each delay tick int m_AddSlotNum; // Index into m_Slots[] where to add new blocks in each ChunkData
int m_CurrentSlotNum; // Index into m_Slots[] where to insert new blocks int m_SimSlotNum; // Index into m_Slots[] where to simulate blocks in each ChunkData
int m_TotalBlocks; // Statistics only: the total number of blocks currently queued
/* /*
Slots: Slots:
| 0 | 1 | ... | m_CurrentSlotNum | m_CurrentSlotNum + 1 | ... | m_TickDelay - 1 | | 0 | 1 | ... | m_AddSlotNum | m_SimSlotNum | ... | m_TickDelay - 1 |
adding blocks here ^ | ^ simulating here adding blocks here ^ | ^ simulating here
*/ */
/// Called from Simulate() to simulate each block in one slot of blocks. Descendants override this method to provide custom simulation. /// Called from SimulateChunk() to simulate each block in one slot of blocks. Descendants override this method to provide custom simulation.
virtual void SimulateBlock(int a_BlockX, int a_BlockY, int a_BlockZ) = 0; virtual void SimulateBlock(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ) = 0;
} ; } ;

View File

@ -8,6 +8,7 @@
#include "FloodyFluidSimulator.h" #include "FloodyFluidSimulator.h"
#include "../World.h" #include "../World.h"
#include "../Chunk.h"
#include "../BlockArea.h" #include "../BlockArea.h"
#include "../Blocks/BlockHandler.h" #include "../Blocks/BlockHandler.h"
@ -44,36 +45,22 @@ cFloodyFluidSimulator::cFloodyFluidSimulator(
void cFloodyFluidSimulator::SimulateBlock(int a_BlockX, int a_BlockY, int a_BlockZ) void cFloodyFluidSimulator::SimulateBlock(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ)
{ {
FLOG("Simulating block {%d, %d, %d}", a_BlockX, a_BlockY, a_BlockZ); NIBBLETYPE MyMeta = a_Chunk->GetMeta(a_RelX, a_RelY, a_RelZ);
if (!IsAnyFluidBlock(a_Chunk->GetBlock(a_RelX, a_RelY, a_RelZ)))
cBlockArea Area;
int MinBlockY = std::max(0, a_BlockY - 1);
int MaxBlockY = std::min(+cChunkDef::Height, a_BlockY + 1);
if (!Area.Read(&m_World, a_BlockX - 1, a_BlockX + 1, MinBlockY, MaxBlockY, a_BlockZ - 1, a_BlockZ + 1))
{
// Cannot read the immediate neighborhood, probably too close to an unloaded chunk. Bail out.
// TODO: Shouldn't we re-schedule?
FLOG(" Cannot read area, bailing out.");
return;
}
int y = (a_BlockY > 0) ? 1 : 0; // Relative y-coord of this block in Area
NIBBLETYPE MyMeta = Area.GetRelBlockMeta(1, y, 1);
if (!IsAnyFluidBlock(Area.GetRelBlockType(1, y, 1)))
{ {
// Can happen - if a block is scheduled for simulating and gets replaced in the meantime. // Can happen - if a block is scheduled for simulating and gets replaced in the meantime.
FLOG(" Not my type: exp %d, got %d", m_FluidBlock, Area.GetRelBlockType(1, y, 1));
return; return;
} }
if (MyMeta != 0) if (MyMeta != 0)
{ {
// Source blocks aren't checked for tributaries, others are. // Source blocks aren't checked for tributaries, others are.
if (CheckTributaries(a_BlockX, a_BlockY, a_BlockZ, Area, MyMeta)) if (CheckTributaries(a_Chunk, a_RelX, a_RelY, a_RelZ, MyMeta))
{ {
// Has no tributary, has been decreased, no more processing needed (neighbors have been scheduled by the decrease) // Has no tributary, has been decreased (in CheckTributaries()),
// no more processing needed (neighbors have been scheduled by the decrease)
return; return;
} }
} }
@ -82,107 +69,128 @@ void cFloodyFluidSimulator::SimulateBlock(int a_BlockX, int a_BlockY, int a_Bloc
// If this is a source block or was falling, the new meta is just the falloff // If this is a source block or was falling, the new meta is just the falloff
// Otherwise it is the current meta plus falloff (may be larger than max height, will be checked later) // Otherwise it is the current meta plus falloff (may be larger than max height, will be checked later)
NIBBLETYPE NewMeta = ((MyMeta == 0) || ((MyMeta & 0x08) != 0)) ? m_Falloff : (MyMeta + m_Falloff); NIBBLETYPE NewMeta = ((MyMeta == 0) || ((MyMeta & 0x08) != 0)) ? m_Falloff : (MyMeta + m_Falloff);
bool SpreadFurther = true;
BLOCKTYPE Below = Area.GetRelBlockType(1, 0, 1); if (a_RelY > 0)
if ((a_BlockY > 0) && (IsPassableForFluid(Below) || IsBlockLava(Below) || IsBlockWater(Below))) {
BLOCKTYPE Below = a_Chunk->GetBlock(a_RelX, a_RelY - 1, a_RelZ);
if (IsPassableForFluid(Below) || IsBlockLava(Below) || IsBlockWater(Below))
{ {
// Spread only down, possibly washing away what's there or turning lava to stone / cobble / obsidian: // Spread only down, possibly washing away what's there or turning lava to stone / cobble / obsidian:
SpreadToNeighbor(a_BlockX, a_BlockY - 1, a_BlockZ, Area, 8); SpreadToNeighbor(a_Chunk, a_RelX, a_RelY - 1, a_RelZ, 8);
SpreadFurther = false;
} }
else if (NewMeta < 8) // Can reach there
{
// If source creation is on, check for it here: // If source creation is on, check for it here:
if ( else if (
(m_NumNeighborsForSource > 0) && // Source creation is on (m_NumNeighborsForSource > 0) && // Source creation is on
(MyMeta == m_Falloff) && // Only exactly one block away from a source (fast bail-out) (MyMeta == m_Falloff) && // Only exactly one block away from a source (fast bail-out)
!IsPassableForFluid(Below) && // Only exactly 1 block deep !IsPassableForFluid(Below) && // Only exactly 1 block deep
CheckNeighborsForSource(a_BlockX, a_BlockY, a_BlockZ, Area) // Did we create a source? CheckNeighborsForSource(a_Chunk, a_RelX, a_RelY, a_RelZ) // Did we create a source?
) )
{ {
// We created a source, no more spreading is to be done now // We created a source, no more spreading is to be done now
// Also has been re-scheduled for ticking in the next wave // Also has been re-scheduled for ticking in the next wave, so no marking is needed
return; return;
} }
}
if (SpreadFurther && (NewMeta < 8))
{
// Spread to the neighbors: // Spread to the neighbors:
SpreadToNeighbor(a_BlockX - 1, a_BlockY, a_BlockZ, Area, NewMeta); SpreadToNeighbor(a_Chunk, a_RelX - 1, a_RelY, a_RelZ, NewMeta);
SpreadToNeighbor(a_BlockX + 1, a_BlockY, a_BlockZ, Area, NewMeta); SpreadToNeighbor(a_Chunk, a_RelX + 1, a_RelY, a_RelZ, NewMeta);
SpreadToNeighbor(a_BlockX, a_BlockY, a_BlockZ - 1, Area, NewMeta); SpreadToNeighbor(a_Chunk, a_RelX, a_RelY, a_RelZ - 1, NewMeta);
SpreadToNeighbor(a_BlockX, a_BlockY, a_BlockZ + 1, Area, NewMeta); SpreadToNeighbor(a_Chunk, a_RelX, a_RelY, a_RelZ + 1, NewMeta);
} }
// Mark as processed: // Mark as processed:
m_World.FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, m_StationaryFluidBlock, MyMeta); a_Chunk->FastSetBlock(a_RelX, a_RelY, a_RelZ, m_StationaryFluidBlock, MyMeta);
} }
bool cFloodyFluidSimulator::CheckTributaries(int a_BlockX, int a_BlockY, int a_BlockZ, const cBlockArea & a_Area, NIBBLETYPE a_MyMeta) bool cFloodyFluidSimulator::CheckTributaries(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_MyMeta)
{ {
bool IsFed = false;
int y = (a_BlockY > 0) ? 1 : 0; // Relative y-coord of this block in Area
// If we have a section above, check if there's fluid above this block that would feed it: // If we have a section above, check if there's fluid above this block that would feed it:
if (a_BlockY < cChunkDef::Height - 1) if (a_RelY < cChunkDef::Height - 1)
{ {
IsFed = IsAnyFluidBlock(a_Area.GetRelBlockType(1, 2, 1)); if (IsAnyFluidBlock(a_Chunk->GetBlock(a_RelX, a_RelY + 1, a_RelZ)))
{
// This block is fed from above, no more processing needed
return false;
}
} }
// If not fed from above, check if there's a feed from the side (but not if it's a downward-flowing block): // Not fed from above, check if there's a feed from the side (but not if it's a downward-flowing block):
if (!IsFed && (a_MyMeta != 8)) if (a_MyMeta != 8)
{ {
IsFed = ( BLOCKTYPE BlockType;
(IsAllowedBlock(a_Area.GetRelBlockType(0, y, 1)) && IsHigherMeta(a_Area.GetRelBlockMeta(0, y, 1), a_MyMeta)) || NIBBLETYPE BlockMeta;
(IsAllowedBlock(a_Area.GetRelBlockType(2, y, 1)) && IsHigherMeta(a_Area.GetRelBlockMeta(2, y, 1), a_MyMeta)) || static const Vector3i Coords[] =
(IsAllowedBlock(a_Area.GetRelBlockType(1, y, 0)) && IsHigherMeta(a_Area.GetRelBlockMeta(1, y, 0), a_MyMeta)) || {
(IsAllowedBlock(a_Area.GetRelBlockType(1, y, 2)) && IsHigherMeta(a_Area.GetRelBlockMeta(1, y, 2), a_MyMeta)) Vector3i( 1, 0, 0),
); Vector3i(-1, 0, 0),
Vector3i( 0, 0, 1),
Vector3i( 0, 0, -1),
} ;
for (int i = 0; i < ARRAYCOUNT(Coords); i++)
{
if (!a_Chunk->UnboundedRelGetBlock(a_RelX + Coords[i].x, a_RelY, a_RelZ + Coords[i].z, BlockType, BlockMeta))
{
continue;
} }
if (IsAllowedBlock(BlockType) && IsHigherMeta(BlockMeta, a_MyMeta))
// If not fed, decrease by m_Falloff levels:
if (!IsFed)
{ {
// This block is fed, no more processing needed
return false;
}
} // for i - Coords[]
} // if not fed from above
// Block is not fed, decrease by m_Falloff levels:
if (a_MyMeta >= 8) if (a_MyMeta >= 8)
{ {
FLOG(" Not fed and downwards, turning into non-downwards meta %d", m_Falloff); FLOG(" Not fed and downwards, turning into non-downwards meta %d", m_Falloff);
m_World.SetBlock(a_BlockX, a_BlockY, a_BlockZ, m_StationaryFluidBlock, m_Falloff); a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, m_StationaryFluidBlock, m_Falloff);
} }
else else
{ {
a_MyMeta += m_Falloff; a_MyMeta += m_Falloff;
if (a_MyMeta < 8) if (a_MyMeta < 8)
{ {
FLOG(" Not fed, decreasing from %d to %d", a_MyMeta, a_MyMeta + m_Falloff); FLOG(" Not fed, decreasing from %d to %d", a_MyMeta - m_Falloff, a_MyMeta);
m_World.SetBlock(a_BlockX, a_BlockY, a_BlockZ, m_StationaryFluidBlock, a_MyMeta); a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, m_StationaryFluidBlock, a_MyMeta);
} }
else else
{ {
FLOG(" Not fed, meta %d, erasing altogether", a_MyMeta); FLOG(" Not fed, meta %d, erasing altogether", a_MyMeta);
m_World.SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0); a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, E_BLOCK_AIR, 0);
} }
} }
return true; return true;
}
return false;
} }
void cFloodyFluidSimulator::SpreadToNeighbor(int a_BlockX, int a_BlockY, int a_BlockZ, const cBlockArea & a_Area, NIBBLETYPE a_NewMeta) void cFloodyFluidSimulator::SpreadToNeighbor(cChunk * a_NearChunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_NewMeta)
{ {
ASSERT(a_NewMeta <= 8); // Invalid meta values ASSERT(a_NewMeta <= 8); // Invalid meta values
ASSERT(a_NewMeta > 0); // Source blocks aren't spread ASSERT(a_NewMeta > 0); // Source blocks aren't spread
BLOCKTYPE Block = a_Area.GetBlockType(a_BlockX, a_BlockY, a_BlockZ); BLOCKTYPE BlockType;
NIBBLETYPE BlockMeta;
if (IsAllowedBlock(Block)) if (!a_NearChunk->UnboundedRelGetBlock(a_RelX, a_RelY, a_RelZ, BlockType, BlockMeta))
{ {
NIBBLETYPE Meta = a_Area.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); // Chunk not available
if ((Meta == a_NewMeta) || IsHigherMeta(Meta, a_NewMeta)) return;
}
if (IsAllowedBlock(BlockType))
{
if ((BlockMeta == a_NewMeta) || IsHigherMeta(BlockMeta, a_NewMeta))
{ {
// Don't spread there, there's already a higher or same level there // Don't spread there, there's already a higher or same level there
return; return;
@ -192,15 +200,15 @@ void cFloodyFluidSimulator::SpreadToNeighbor(int a_BlockX, int a_BlockY, int a_B
// Check water - lava interaction: // Check water - lava interaction:
if (m_FluidBlock == E_BLOCK_LAVA) if (m_FluidBlock == E_BLOCK_LAVA)
{ {
if (IsBlockWater(Block)) if (IsBlockWater(BlockType))
{ {
// Lava flowing into water, change to stone / cobblestone based on direction: // Lava flowing into water, change to stone / cobblestone based on direction:
BLOCKTYPE NewBlock = (a_NewMeta == 8) ? E_BLOCK_STONE : E_BLOCK_COBBLESTONE; BLOCKTYPE NewBlock = (a_NewMeta == 8) ? E_BLOCK_STONE : E_BLOCK_COBBLESTONE;
FLOG(" Lava flowing into water, turning water at {%d, %d, %d} into stone", FLOG(" Lava flowing into water, turning water at {%d, %d, %d} into stone",
a_BlockX, a_BlockY, a_BlockZ, a_RelX, a_RelY, a_RelZ,
ItemTypeToString(NewBlock).c_str() ItemTypeToString(NewBlock).c_str()
); );
m_World.SetBlock(a_BlockX, a_BlockY, a_BlockZ, NewBlock, 0); a_NearChunk->UnboundedRelSetBlock(a_RelX, a_RelY, a_RelZ, NewBlock, 0);
// TODO: Sound effect // TODO: Sound effect
@ -209,14 +217,14 @@ void cFloodyFluidSimulator::SpreadToNeighbor(int a_BlockX, int a_BlockY, int a_B
} }
else if (m_FluidBlock == E_BLOCK_WATER) else if (m_FluidBlock == E_BLOCK_WATER)
{ {
if (IsBlockLava(Block)) if (IsBlockLava(BlockType))
{ {
// Water flowing into lava, change to cobblestone / obsidian based on dest block: // Water flowing into lava, change to cobblestone / obsidian based on dest block:
BLOCKTYPE NewBlock = (a_Area.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) == 0) ? E_BLOCK_OBSIDIAN : E_BLOCK_COBBLESTONE; BLOCKTYPE NewBlock = (BlockMeta == 0) ? E_BLOCK_OBSIDIAN : E_BLOCK_COBBLESTONE;
FLOG(" Water flowing into lava, turning lava at {%d, %d, %d} into %s", FLOG(" Water flowing into lava, turning lava at {%d, %d, %d} into %s",
a_BlockX, a_BlockY, a_BlockZ, ItemTypeToString(NewBlock).c_str() a_RelX, a_RelY, a_RelZ, ItemTypeToString(NewBlock).c_str()
); );
m_World.SetBlock(a_BlockX, a_BlockY, a_BlockZ, NewBlock, 0); a_NearChunk->UnboundedRelSetBlock(a_RelX, a_RelY, a_RelZ, NewBlock, 0);
// TODO: Sound effect // TODO: Sound effect
@ -228,32 +236,37 @@ void cFloodyFluidSimulator::SpreadToNeighbor(int a_BlockX, int a_BlockY, int a_B
ASSERT(!"Unknown fluid!"); ASSERT(!"Unknown fluid!");
} }
if (!IsPassableForFluid(Block)) if (!IsPassableForFluid(BlockType))
{ {
// Can't spread there // Can't spread there
return; return;
} }
// Wash away the block there, if possible: // Wash away the block there, if possible:
if (CanWashAway(Block)) if (CanWashAway(BlockType))
{ {
cBlockHandler * Handler = BlockHandler(Block); cBlockHandler * Handler = BlockHandler(BlockType);
if (Handler->DoesDropOnUnsuitable()) if (Handler->DoesDropOnUnsuitable())
{ {
Handler->DropBlock(&m_World, NULL, a_BlockX, a_BlockY, a_BlockZ); Handler->DropBlock(
} &m_World, NULL,
a_NearChunk->GetPosX() * cChunkDef::Width + a_RelX,
a_RelY,
a_NearChunk->GetPosZ() * cChunkDef::Width + a_RelZ
);
} }
} // if (CanWashAway)
// Spread: // Spread:
FLOG(" Spreading to {%d, %d, %d} with meta %d", a_BlockX, a_BlockY, a_BlockZ, a_NewMeta); FLOG(" Spreading to {%d, %d, %d} with meta %d", a_RelX, a_RelY, a_RelZ, a_NewMeta);
m_World.SetBlock(a_BlockX, a_BlockY, a_BlockZ, m_FluidBlock, a_NewMeta); a_NearChunk->UnboundedRelSetBlock(a_RelX, a_RelY, a_RelZ, m_FluidBlock, a_NewMeta);
} }
bool cFloodyFluidSimulator::CheckNeighborsForSource(int a_BlockX, int a_BlockY, int a_BlockZ, const cBlockArea & a_Area) bool cFloodyFluidSimulator::CheckNeighborsForSource(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ)
{ {
FLOG(" Checking neighbors for source creation"); FLOG(" Checking neighbors for source creation");
@ -261,19 +274,23 @@ bool cFloodyFluidSimulator::CheckNeighborsForSource(int a_BlockX, int a_BlockY,
{ {
Vector3i(-1, 0, 0), Vector3i(-1, 0, 0),
Vector3i( 1, 0, 0), Vector3i( 1, 0, 0),
Vector3i( 0, 0,-1), Vector3i( 0, 0, -1),
Vector3i( 0, 0, 1), Vector3i( 0, 0, 1),
} ; } ;
int NumNeeded = m_NumNeighborsForSource; int NumNeeded = m_NumNeighborsForSource;
for (int i = 0; i < ARRAYCOUNT(NeighborCoords); i++) for (int i = 0; i < ARRAYCOUNT(NeighborCoords); i++)
{ {
int x = a_BlockX + NeighborCoords[i].x; int x = a_RelX + NeighborCoords[i].x;
int y = a_BlockY + NeighborCoords[i].y; int y = a_RelY + NeighborCoords[i].y;
int z = a_BlockZ + NeighborCoords[i].z; int z = a_RelZ + NeighborCoords[i].z;
BLOCKTYPE BlockType; BLOCKTYPE BlockType;
NIBBLETYPE BlockMeta; NIBBLETYPE BlockMeta;
a_Area.GetBlockTypeMeta(x, y, z, BlockType, BlockMeta); if (!a_Chunk->UnboundedRelGetBlock(x, y, z, BlockType, BlockMeta))
{
// Neighbor not available, skip it
continue;
}
FLOG(" Neighbor at {%d, %d, %d}: %s", x, y, z, ItemToFullString(cItem(BlockType, 1, BlockMeta)).c_str()); FLOG(" Neighbor at {%d, %d, %d}: %s", x, y, z, ItemToFullString(cItem(BlockType, 1, BlockMeta)).c_str());
if ((BlockMeta == 0) && IsAnyFluidBlock(BlockType)) if ((BlockMeta == 0) && IsAnyFluidBlock(BlockType))
{ {
@ -283,7 +300,7 @@ bool cFloodyFluidSimulator::CheckNeighborsForSource(int a_BlockX, int a_BlockY,
{ {
// Found enough, turn into a source and bail out // Found enough, turn into a source and bail out
FLOG(" Found enough neighbor sources, turning into a source"); FLOG(" Found enough neighbor sources, turning into a source");
m_World.SetBlock(a_BlockX, a_BlockY, a_BlockZ, m_FluidBlock, 0); a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, m_FluidBlock, 0);
return true; return true;
} }
} }

View File

@ -36,16 +36,16 @@ protected:
int m_NumNeighborsForSource; int m_NumNeighborsForSource;
// cDelayedFluidSimulator overrides: // cDelayedFluidSimulator overrides:
virtual void SimulateBlock(int a_BlockX, int a_BlockY, int a_BlockZ) override; virtual void SimulateBlock(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override;
/// Checks tributaries, if not fed, decreases the block's level and returns true /// Checks tributaries, if not fed, decreases the block's level and returns true
bool CheckTributaries(int a_BlockX, int a_BlockY, int a_BlockZ, const cBlockArea & a_Area, NIBBLETYPE a_MyMeta); bool CheckTributaries(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_MyMeta);
/// Spreads into the specified block, if the blocktype there allows. a_Area is for checking. /// Spreads into the specified block, if the blocktype there allows. a_Area is for checking.
void SpreadToNeighbor(int a_BlockX, int a_BlockY, int a_BlockZ, const cBlockArea & a_Area, NIBBLETYPE a_NewMeta); void SpreadToNeighbor(cChunk * a_NearChunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_NewMeta);
/// Checks if there are enough neighbors to create a source at the coords specified; turns into source and returns true if so /// Checks if there are enough neighbors to create a source at the coords specified; turns into source and returns true if so
bool CheckNeighborsForSource(int a_BlockX, int a_BlockY, int a_BlockZ, const cBlockArea & a_Area); bool CheckNeighborsForSource(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ);
} ; } ;

View File

@ -22,6 +22,19 @@ enum Direction
/** This is a base class for all fluid simulator data classes.
Needed so that cChunk can properly delete instances of fluid simulator data, no matter what simulator it's using
*/
class cFluidSimulatorData
{
public:
virtual ~cFluidSimulatorData() {}
} ;
class cFluidSimulator : class cFluidSimulator :
public cSimulator public cSimulator
{ {
@ -36,6 +49,9 @@ public:
/// Gets the flowing direction. If a_Over is true also the block over the current block affects the direction (standard) /// Gets the flowing direction. If a_Over is true also the block over the current block affects the direction (standard)
virtual Direction GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over = true); virtual Direction GetFlowingDirection(int a_X, int a_Y, int a_Z, bool a_Over = true);
/// Creates a ChunkData object for the simulator to use. The simulator returns the correct object type.
virtual cFluidSimulatorData * CreateChunkData(void) { return NULL; }
bool IsFluidBlock (BLOCKTYPE a_BlockType) const { return (a_BlockType == m_FluidBlock); } bool IsFluidBlock (BLOCKTYPE a_BlockType) const { return (a_BlockType == m_FluidBlock); }
bool IsStationaryFluidBlock(BLOCKTYPE a_BlockType) const { return (a_BlockType == m_StationaryFluidBlock); } bool IsStationaryFluidBlock(BLOCKTYPE a_BlockType) const { return (a_BlockType == m_StationaryFluidBlock); }
bool IsAnyFluidBlock (BLOCKTYPE a_BlockType) const { return ((a_BlockType == m_FluidBlock) || (a_BlockType == m_StationaryFluidBlock)); } bool IsAnyFluidBlock (BLOCKTYPE a_BlockType) const { return ((a_BlockType == m_FluidBlock) || (a_BlockType == m_StationaryFluidBlock)); }