2012-10-14 13:06:21 -04:00
|
|
|
|
|
|
|
// FloodyFluidSimulator.cpp
|
|
|
|
|
|
|
|
// Interfaces to the cFloodyFluidSimulator that represents a fluid simulator that tries to flood everything :)
|
|
|
|
// http://forum.mc-server.org/showthread.php?tid=565
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "FloodyFluidSimulator.h"
|
|
|
|
#include "../World.h"
|
2013-03-02 10:44:31 -05:00
|
|
|
#include "../Chunk.h"
|
2012-10-14 13:06:21 -04:00
|
|
|
#include "../BlockArea.h"
|
|
|
|
#include "../Blocks/BlockHandler.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-14 16:10:03 -04:00
|
|
|
// Enable or disable detailed logging
|
2012-10-20 07:47:50 -04:00
|
|
|
#if 0
|
2012-10-16 15:34:07 -04:00
|
|
|
#define FLOG LOGD
|
2012-10-14 16:10:03 -04:00
|
|
|
#else
|
|
|
|
#define FLOG(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-04 00:21:07 -05:00
|
|
|
cFloodyFluidSimulator::cFloodyFluidSimulator(
|
2013-02-28 08:39:20 -05:00
|
|
|
cWorld & a_World,
|
2013-01-04 00:21:07 -05:00
|
|
|
BLOCKTYPE a_Fluid,
|
|
|
|
BLOCKTYPE a_StationaryFluid,
|
|
|
|
NIBBLETYPE a_Falloff,
|
|
|
|
int a_TickDelay,
|
|
|
|
int a_NumNeighborsForSource
|
|
|
|
) :
|
2012-10-14 13:06:21 -04:00
|
|
|
super(a_World, a_Fluid, a_StationaryFluid, a_TickDelay),
|
2013-01-04 00:21:07 -05:00
|
|
|
m_Falloff(a_Falloff),
|
|
|
|
m_NumNeighborsForSource(a_NumNeighborsForSource)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
void cFloodyFluidSimulator::SimulateBlock(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG("Simulating block {%d, %d, %d}: block %d, meta %d",
|
|
|
|
a_Chunk->GetPosX() * cChunkDef::Width + a_RelX, a_RelY, a_Chunk->GetPosZ() * cChunkDef::Width + a_RelZ,
|
|
|
|
a_Chunk->GetBlock(a_RelX, a_RelY, a_RelZ),
|
|
|
|
a_Chunk->GetMeta(a_RelX, a_RelY, a_RelZ)
|
|
|
|
);
|
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
NIBBLETYPE MyMeta = a_Chunk->GetMeta(a_RelX, a_RelY, a_RelZ);
|
|
|
|
if (!IsAnyFluidBlock(a_Chunk->GetBlock(a_RelX, a_RelY, a_RelZ)))
|
2012-10-14 14:30:16 -04:00
|
|
|
{
|
|
|
|
// Can happen - if a block is scheduled for simulating and gets replaced in the meantime.
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG(" BadBlockType exit");
|
2012-10-14 14:30:16 -04:00
|
|
|
return;
|
|
|
|
}
|
2012-10-14 13:06:21 -04:00
|
|
|
|
|
|
|
if (MyMeta != 0)
|
|
|
|
{
|
2013-01-04 00:21:07 -05:00
|
|
|
// Source blocks aren't checked for tributaries, others are.
|
2013-03-02 10:44:31 -05:00
|
|
|
if (CheckTributaries(a_Chunk, a_RelX, a_RelY, a_RelZ, MyMeta))
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
// Has no tributary, has been decreased (in CheckTributaries()),
|
|
|
|
// no more processing needed (neighbors have been scheduled by the decrease)
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG(" CheckTributaries exit");
|
2012-10-14 13:06:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New meta for the spreading to neighbors:
|
|
|
|
// 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)
|
|
|
|
NIBBLETYPE NewMeta = ((MyMeta == 0) || ((MyMeta & 0x08) != 0)) ? m_Falloff : (MyMeta + m_Falloff);
|
2013-03-02 10:44:31 -05:00
|
|
|
bool SpreadFurther = true;
|
|
|
|
if (a_RelY > 0)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
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:
|
|
|
|
SpreadToNeighbor(a_Chunk, a_RelX, a_RelY - 1, a_RelZ, 8);
|
|
|
|
SpreadFurther = false;
|
|
|
|
}
|
2013-01-04 00:21:07 -05:00
|
|
|
// If source creation is on, check for it here:
|
2013-03-02 10:44:31 -05:00
|
|
|
else if (
|
2013-01-04 00:21:07 -05:00
|
|
|
(m_NumNeighborsForSource > 0) && // Source creation is on
|
|
|
|
(MyMeta == m_Falloff) && // Only exactly one block away from a source (fast bail-out)
|
|
|
|
!IsPassableForFluid(Below) && // Only exactly 1 block deep
|
2013-03-02 10:44:31 -05:00
|
|
|
CheckNeighborsForSource(a_Chunk, a_RelX, a_RelY, a_RelZ) // Did we create a source?
|
2013-01-04 00:21:07 -05:00
|
|
|
)
|
|
|
|
{
|
|
|
|
// We created a source, no more spreading is to be done now
|
2013-03-02 10:44:31 -05:00
|
|
|
// Also has been re-scheduled for ticking in the next wave, so no marking is needed
|
2013-01-04 00:21:07 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-03-02 10:44:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SpreadFurther && (NewMeta < 8))
|
|
|
|
{
|
2012-10-14 13:06:21 -04:00
|
|
|
// Spread to the neighbors:
|
2013-03-02 10:44:31 -05:00
|
|
|
SpreadToNeighbor(a_Chunk, a_RelX - 1, a_RelY, a_RelZ, NewMeta);
|
|
|
|
SpreadToNeighbor(a_Chunk, a_RelX + 1, a_RelY, a_RelZ, NewMeta);
|
|
|
|
SpreadToNeighbor(a_Chunk, a_RelX, a_RelY, a_RelZ - 1, NewMeta);
|
|
|
|
SpreadToNeighbor(a_Chunk, a_RelX, a_RelY, a_RelZ + 1, NewMeta);
|
2012-10-14 13:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as processed:
|
2013-03-02 10:44:31 -05:00
|
|
|
a_Chunk->FastSetBlock(a_RelX, a_RelY, a_RelZ, m_StationaryFluidBlock, MyMeta);
|
2012-10-14 13:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
bool cFloodyFluidSimulator::CheckTributaries(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_MyMeta)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
|
|
|
// If we have a section above, check if there's fluid above this block that would feed it:
|
2013-03-02 10:44:31 -05:00
|
|
|
if (a_RelY < cChunkDef::Height - 1)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
if (IsAnyFluidBlock(a_Chunk->GetBlock(a_RelX, a_RelY + 1, a_RelZ)))
|
|
|
|
{
|
|
|
|
// This block is fed from above, no more processing needed
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG(" Fed from above");
|
2013-03-02 10:44:31 -05:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-14 13:06:21 -04:00
|
|
|
}
|
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
// Not fed from above, check if there's a feed from the side (but not if it's a downward-flowing block):
|
|
|
|
if (a_MyMeta != 8)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
|
|
|
static const Vector3i Coords[] =
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
Vector3i( 1, 0, 0),
|
|
|
|
Vector3i(-1, 0, 0),
|
|
|
|
Vector3i( 0, 0, 1),
|
|
|
|
Vector3i( 0, 0, -1),
|
|
|
|
} ;
|
|
|
|
for (int i = 0; i < ARRAYCOUNT(Coords); i++)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
if (!a_Chunk->UnboundedRelGetBlock(a_RelX + Coords[i].x, a_RelY, a_RelZ + Coords[i].z, BlockType, BlockMeta))
|
2012-10-16 15:52:55 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
continue;
|
2012-10-16 15:52:55 -04:00
|
|
|
}
|
2013-03-02 10:44:31 -05:00
|
|
|
if (IsAllowedBlock(BlockType) && IsHigherMeta(BlockMeta, a_MyMeta))
|
2012-10-16 15:52:55 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
// This block is fed, no more processing needed
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG(" Fed from {%d, %d, %d}, type %d, meta %d",
|
|
|
|
a_Chunk->GetPosX() * cChunkDef::Width + a_RelX + Coords[i].x,
|
|
|
|
a_RelY,
|
|
|
|
a_Chunk->GetPosZ() * cChunkDef::Width + a_RelZ + Coords[i].z,
|
|
|
|
BlockType, BlockMeta
|
|
|
|
);
|
2013-03-02 10:44:31 -05:00
|
|
|
return false;
|
2012-10-16 15:52:55 -04:00
|
|
|
}
|
2013-03-02 10:44:31 -05:00
|
|
|
} // for i - Coords[]
|
|
|
|
} // if not fed from above
|
|
|
|
|
|
|
|
// Block is not fed, decrease by m_Falloff levels:
|
|
|
|
if (a_MyMeta >= 8)
|
|
|
|
{
|
|
|
|
FLOG(" Not fed and downwards, turning into non-downwards meta %d", m_Falloff);
|
|
|
|
a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, m_StationaryFluidBlock, m_Falloff);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
a_MyMeta += m_Falloff;
|
|
|
|
if (a_MyMeta < 8)
|
|
|
|
{
|
|
|
|
FLOG(" Not fed, decreasing from %d to %d", a_MyMeta - m_Falloff, a_MyMeta);
|
|
|
|
a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, m_StationaryFluidBlock, a_MyMeta);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FLOG(" Not fed, meta %d, erasing altogether", a_MyMeta);
|
|
|
|
a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, E_BLOCK_AIR, 0);
|
2012-10-14 13:06:21 -04:00
|
|
|
}
|
|
|
|
}
|
2013-03-02 10:44:31 -05:00
|
|
|
return true;
|
2012-10-14 13:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
void cFloodyFluidSimulator::SpreadToNeighbor(cChunk * a_NearChunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_NewMeta)
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2012-10-14 14:30:16 -04:00
|
|
|
ASSERT(a_NewMeta <= 8); // Invalid meta values
|
|
|
|
ASSERT(a_NewMeta > 0); // Source blocks aren't spread
|
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
|
|
|
if (!a_NearChunk->UnboundedRelGetBlock(a_RelX, a_RelY, a_RelZ, BlockType, BlockMeta))
|
|
|
|
{
|
|
|
|
// Chunk not available
|
|
|
|
return;
|
|
|
|
}
|
2012-10-14 13:06:21 -04:00
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
if (IsAllowedBlock(BlockType))
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
if ((BlockMeta == a_NewMeta) || IsHigherMeta(BlockMeta, a_NewMeta))
|
2012-10-14 16:10:03 -04:00
|
|
|
{
|
|
|
|
// Don't spread there, there's already a higher or same level there
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check water - lava interaction:
|
|
|
|
if (m_FluidBlock == E_BLOCK_LAVA)
|
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
if (IsBlockWater(BlockType))
|
2012-10-14 16:10:03 -04:00
|
|
|
{
|
|
|
|
// Lava flowing into water, change to stone / cobblestone based on direction:
|
|
|
|
BLOCKTYPE NewBlock = (a_NewMeta == 8) ? E_BLOCK_STONE : E_BLOCK_COBBLESTONE;
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG(" Lava flowing into water, turning water at rel {%d, %d, %d} into stone",
|
2013-03-02 10:44:31 -05:00
|
|
|
a_RelX, a_RelY, a_RelZ,
|
2012-10-14 16:10:03 -04:00
|
|
|
ItemTypeToString(NewBlock).c_str()
|
|
|
|
);
|
2013-03-02 10:44:31 -05:00
|
|
|
a_NearChunk->UnboundedRelSetBlock(a_RelX, a_RelY, a_RelZ, NewBlock, 0);
|
2012-10-14 16:10:03 -04:00
|
|
|
|
|
|
|
// TODO: Sound effect
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_FluidBlock == E_BLOCK_WATER)
|
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
if (IsBlockLava(BlockType))
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2012-10-14 16:10:03 -04:00
|
|
|
// Water flowing into lava, change to cobblestone / obsidian based on dest block:
|
2013-03-02 10:44:31 -05:00
|
|
|
BLOCKTYPE NewBlock = (BlockMeta == 0) ? E_BLOCK_OBSIDIAN : E_BLOCK_COBBLESTONE;
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG(" Water flowing into lava, turning lava at rel {%d, %d, %d} into %s",
|
2013-03-02 10:44:31 -05:00
|
|
|
a_RelX, a_RelY, a_RelZ, ItemTypeToString(NewBlock).c_str()
|
2012-10-14 16:10:03 -04:00
|
|
|
);
|
2013-03-02 10:44:31 -05:00
|
|
|
a_NearChunk->UnboundedRelSetBlock(a_RelX, a_RelY, a_RelZ, NewBlock, 0);
|
2012-10-14 16:10:03 -04:00
|
|
|
|
|
|
|
// TODO: Sound effect
|
|
|
|
|
2012-10-14 13:06:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-10-14 16:10:03 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(!"Unknown fluid!");
|
|
|
|
}
|
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
if (!IsPassableForFluid(BlockType))
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
|
|
|
// Can't spread there
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wash away the block there, if possible:
|
2013-03-02 10:44:31 -05:00
|
|
|
if (CanWashAway(BlockType))
|
2012-10-14 13:06:21 -04:00
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
cBlockHandler * Handler = BlockHandler(BlockType);
|
2012-10-14 13:06:21 -04:00
|
|
|
if (Handler->DoesDropOnUnsuitable())
|
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
Handler->DropBlock(
|
|
|
|
&m_World, NULL,
|
|
|
|
a_NearChunk->GetPosX() * cChunkDef::Width + a_RelX,
|
|
|
|
a_RelY,
|
|
|
|
a_NearChunk->GetPosZ() * cChunkDef::Width + a_RelZ
|
|
|
|
);
|
2012-10-14 13:06:21 -04:00
|
|
|
}
|
2013-03-02 10:44:31 -05:00
|
|
|
} // if (CanWashAway)
|
2012-10-14 13:06:21 -04:00
|
|
|
|
|
|
|
// Spread:
|
2013-03-22 12:48:45 -04:00
|
|
|
FLOG(" Spreading to {%d, %d, %d} with meta %d",
|
|
|
|
a_NearChunk->GetPosX() * cChunkDef::Width + a_RelX,
|
|
|
|
a_RelY,
|
|
|
|
a_NearChunk->GetPosZ() * cChunkDef::Width + a_RelZ,
|
|
|
|
a_NewMeta
|
|
|
|
);
|
2013-03-02 10:44:31 -05:00
|
|
|
a_NearChunk->UnboundedRelSetBlock(a_RelX, a_RelY, a_RelZ, m_FluidBlock, a_NewMeta);
|
2012-10-14 13:06:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-04 00:21:07 -05:00
|
|
|
|
2013-03-02 10:44:31 -05:00
|
|
|
bool cFloodyFluidSimulator::CheckNeighborsForSource(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ)
|
2013-01-04 00:21:07 -05:00
|
|
|
{
|
|
|
|
FLOG(" Checking neighbors for source creation");
|
|
|
|
|
|
|
|
static const Vector3i NeighborCoords[] =
|
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
Vector3i(-1, 0, 0),
|
|
|
|
Vector3i( 1, 0, 0),
|
|
|
|
Vector3i( 0, 0, -1),
|
|
|
|
Vector3i( 0, 0, 1),
|
2013-01-04 00:21:07 -05:00
|
|
|
} ;
|
|
|
|
|
|
|
|
int NumNeeded = m_NumNeighborsForSource;
|
|
|
|
for (int i = 0; i < ARRAYCOUNT(NeighborCoords); i++)
|
|
|
|
{
|
2013-03-02 10:44:31 -05:00
|
|
|
int x = a_RelX + NeighborCoords[i].x;
|
|
|
|
int y = a_RelY + NeighborCoords[i].y;
|
|
|
|
int z = a_RelZ + NeighborCoords[i].z;
|
2013-01-04 00:21:07 -05:00
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
2013-03-02 10:44:31 -05:00
|
|
|
if (!a_Chunk->UnboundedRelGetBlock(x, y, z, BlockType, BlockMeta))
|
|
|
|
{
|
|
|
|
// Neighbor not available, skip it
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-22 12:48:45 -04:00
|
|
|
// FLOG(" Neighbor at {%d, %d, %d}: %s", x, y, z, ItemToFullString(cItem(BlockType, 1, BlockMeta)).c_str());
|
2013-01-04 00:21:07 -05:00
|
|
|
if ((BlockMeta == 0) && IsAnyFluidBlock(BlockType))
|
|
|
|
{
|
|
|
|
NumNeeded--;
|
2013-03-22 12:48:45 -04:00
|
|
|
// FLOG(" Found a neighbor source at {%d, %d, %d}, NumNeeded := %d", x, y, z, NumNeeded);
|
2013-01-04 00:21:07 -05:00
|
|
|
if (NumNeeded == 0)
|
|
|
|
{
|
|
|
|
// Found enough, turn into a source and bail out
|
2013-03-22 12:48:45 -04:00
|
|
|
// FLOG(" Found enough neighbor sources, turning into a source");
|
2013-03-02 10:44:31 -05:00
|
|
|
a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, m_FluidBlock, 0);
|
2013-01-04 00:21:07 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-22 12:48:45 -04:00
|
|
|
// FLOG(" Not enough neighbors for turning into a source, NumNeeded = %d", NumNeeded);
|
2013-01-04 00:21:07 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|