2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "SandSimulator.h"
|
2012-10-13 05:53:28 -04:00
|
|
|
#include "../World.h"
|
|
|
|
#include "../BlockID.h"
|
|
|
|
#include "../Defines.h"
|
|
|
|
#include "../FallingBlock.h"
|
2013-02-28 02:42:45 -05:00
|
|
|
#include "../Chunk.h"
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
cSandSimulator::cSandSimulator(cWorld & a_World, cIniFile & a_IniFile) :
|
|
|
|
cSimulator(a_World),
|
|
|
|
m_TotalBlocks(0)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
m_IsInstantFall = a_IniFile.GetValueSetB("Physics", "SandInstantFall", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSandSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChunk * a_Chunk)
|
|
|
|
{
|
|
|
|
cSandSimulatorChunkData & ChunkData = a_Chunk->GetSandSimulatorData();
|
|
|
|
if (ChunkData.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
int BaseX = a_Chunk->GetPosX() * cChunkDef::Width;
|
|
|
|
int BaseZ = a_Chunk->GetPosZ() * cChunkDef::Width;
|
|
|
|
for (cSandSimulatorChunkData::const_iterator itr = ChunkData.begin(), end = ChunkData.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
BLOCKTYPE BlockType = a_Chunk->GetBlock(itr->x, itr->y, itr->z);
|
|
|
|
if (!IsAllowedBlock(BlockType) || (itr->y <= 0))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLOCKTYPE BlockBelow = (itr->y > 0) ? a_Chunk->GetBlock(itr->x, itr->y - 1, itr->z) : E_BLOCK_AIR;
|
|
|
|
if (CanStartFallingThrough(BlockBelow))
|
|
|
|
{
|
|
|
|
if (m_IsInstantFall)
|
|
|
|
{
|
|
|
|
DoInstantFall(a_Chunk, itr->x, itr->y, itr->z);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Vector3i Pos;
|
|
|
|
Pos.x = itr->x + BaseX;
|
|
|
|
Pos.y = itr->y;
|
|
|
|
Pos.z = itr->z + BaseZ;
|
|
|
|
cFallingBlock * FallingBlock = new cFallingBlock(Pos, BlockType, a_Chunk->GetMeta(itr->x, itr->y, itr->z));
|
|
|
|
FallingBlock->Initialize(&m_World);
|
|
|
|
a_Chunk->SetBlock(itr->x, itr->y, itr->z, E_BLOCK_AIR, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_TotalBlocks -= ChunkData.size();
|
|
|
|
ChunkData.clear();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
bool cSandSimulator::IsAllowedBlock(BLOCKTYPE a_BlockType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_SAND:
|
|
|
|
case E_BLOCK_GRAVEL:
|
|
|
|
case E_BLOCK_ANVIL:
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
void cSandSimulator::AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
if (a_Chunk == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int RelX = a_BlockX - a_Chunk->GetPosX() * cChunkDef::Width;
|
|
|
|
int RelZ = a_BlockZ - a_Chunk->GetPosZ() * cChunkDef::Width;
|
|
|
|
if (!IsAllowedBlock(a_Chunk->GetBlock(RelX, a_BlockY, RelZ)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
// Check for duplicates:
|
|
|
|
cSandSimulatorChunkData & ChunkData = a_Chunk->GetSandSimulatorData();
|
|
|
|
for (cSandSimulatorChunkData::iterator itr = ChunkData.begin(); itr != ChunkData.end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-03 03:40:37 -05:00
|
|
|
if ((itr->x == RelX) && (itr->y == a_BlockY) && (itr->z == RelZ))
|
2013-03-02 14:57:09 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_TotalBlocks += 1;
|
|
|
|
ChunkData.push_back(cCoordWithInt(RelX, a_BlockY, RelZ));
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cSandSimulator::CanStartFallingThrough(BLOCKTYPE a_BlockType)
|
|
|
|
{
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_AIR:
|
|
|
|
case E_BLOCK_FIRE:
|
|
|
|
case E_BLOCK_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_STATIONARY_WATER:
|
|
|
|
case E_BLOCK_WATER:
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2013-03-02 14:57:09 -05:00
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
bool cSandSimulator::CanContinueFallThrough(BLOCKTYPE a_BlockType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_AIR:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_BROWN_MUSHROOM:
|
|
|
|
case E_BLOCK_COBWEB:
|
|
|
|
case E_BLOCK_CROPS:
|
|
|
|
case E_BLOCK_DEAD_BUSH:
|
|
|
|
case E_BLOCK_DETECTOR_RAIL:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_FIRE:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_FLOWER_POT:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_LAVA:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_LEVER:
|
|
|
|
case E_BLOCK_MINECART_TRACKS:
|
|
|
|
case E_BLOCK_MELON_STEM:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_POWERED_RAIL:
|
|
|
|
case E_BLOCK_PUMPKIN_STEM:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_REDSTONE_REPEATER_OFF:
|
|
|
|
case E_BLOCK_REDSTONE_REPEATER_ON:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_REDSTONE_TORCH_OFF:
|
|
|
|
case E_BLOCK_REDSTONE_TORCH_ON:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_REDSTONE_WIRE:
|
|
|
|
case E_BLOCK_RED_MUSHROOM:
|
|
|
|
case E_BLOCK_RED_ROSE:
|
|
|
|
case E_BLOCK_SIGN_POST:
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_WATER:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_STONE_BUTTON:
|
|
|
|
case E_BLOCK_STONE_PRESSURE_PLATE:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_TALL_GRASS:
|
|
|
|
case E_BLOCK_TORCH:
|
|
|
|
case E_BLOCK_TRAPDOOR:
|
|
|
|
case E_BLOCK_TRIPWIRE:
|
|
|
|
case E_BLOCK_TRIPWIRE_HOOK:
|
|
|
|
case E_BLOCK_WALLSIGN:
|
|
|
|
case E_BLOCK_WATER:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_WOODEN_BUTTON:
|
|
|
|
case E_BLOCK_WOODEN_PRESSURE_PLATE:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_YELLOW_FLOWER:
|
2013-03-02 14:57:09 -05:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
bool cSandSimulator::IsReplacedOnRematerialization(BLOCKTYPE a_BlockType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
switch (a_BlockType)
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_AIR:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_DEAD_BUSH:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_FIRE:
|
|
|
|
case E_BLOCK_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_STATIONARY_WATER:
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_TALL_GRASS:
|
2013-03-03 03:40:37 -05:00
|
|
|
case E_BLOCK_WATER:
|
2013-03-02 14:57:09 -05:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-28 02:42:45 -05:00
|
|
|
}
|
2013-03-02 14:57:09 -05:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-03 03:40:37 -05:00
|
|
|
bool cSandSimulator::DoesBreakFallingThrough(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
2013-03-02 14:57:09 -05:00
|
|
|
{
|
|
|
|
switch (a_BlockType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 14:57:09 -05:00
|
|
|
case E_BLOCK_STONE_SLAB:
|
|
|
|
case E_BLOCK_WOODEN_SLAB:
|
2013-02-28 02:42:45 -05:00
|
|
|
{
|
2013-03-03 03:40:37 -05:00
|
|
|
return ((a_BlockMeta & 0x08) == 0); // Only a bottom-slab breaks the block
|
2013-02-28 02:42:45 -05:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-03-02 14:57:09 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cSandSimulator::FinishFalling(
|
|
|
|
cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ,
|
|
|
|
BLOCKTYPE a_FallingBlockType, NIBBLETYPE a_FallingBlockMeta
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ASSERT(a_BlockY < cChunkDef::Height);
|
|
|
|
|
|
|
|
BLOCKTYPE CurrentBlockType = a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
if ((a_FallingBlockType == E_BLOCK_ANVIL) || IsReplacedOnRematerialization(CurrentBlockType))
|
|
|
|
{
|
|
|
|
// Rematerialize the material here:
|
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, a_FallingBlockType, a_FallingBlockMeta);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a pickup instead:
|
|
|
|
cItems Pickups;
|
|
|
|
Pickups.Add((ENUM_ITEM_ID)a_FallingBlockType, 1, a_FallingBlockMeta);
|
|
|
|
a_World->SpawnItemPickups(Pickups, (double)a_BlockX + 0.5, (double)a_BlockY + 0.5, (double)a_BlockZ + 0.5, 0);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-02 14:57:09 -05:00
|
|
|
void cSandSimulator::DoInstantFall(cChunk * a_Chunk, int a_RelX, int a_RelY, int a_RelZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-03-02 15:24:18 -05:00
|
|
|
// Remove the original block:
|
|
|
|
BLOCKTYPE FallingBlockType;
|
|
|
|
NIBBLETYPE FallingBlockMeta;
|
|
|
|
a_Chunk->GetBlockTypeMeta(a_RelX, a_RelY, a_RelZ, FallingBlockType, FallingBlockMeta);
|
|
|
|
a_Chunk->SetBlock(a_RelX, a_RelY, a_RelZ, E_BLOCK_AIR, 0);
|
|
|
|
|
|
|
|
// Search for a place to put it:
|
|
|
|
for (int y = a_RelY - 1; y >= 0; y--)
|
|
|
|
{
|
2013-03-03 03:40:37 -05:00
|
|
|
BLOCKTYPE BlockType;
|
|
|
|
NIBBLETYPE BlockMeta;
|
|
|
|
a_Chunk->GetBlockTypeMeta(a_RelX, y, a_RelZ, BlockType, BlockMeta);
|
|
|
|
int BlockY;
|
|
|
|
if (DoesBreakFallingThrough(BlockType, BlockMeta))
|
|
|
|
{
|
|
|
|
BlockY = y;
|
|
|
|
}
|
|
|
|
else if (!CanContinueFallThrough(BlockType))
|
|
|
|
{
|
|
|
|
BlockY = y + 1;
|
|
|
|
}
|
|
|
|
else
|
2013-03-02 15:24:18 -05:00
|
|
|
{
|
|
|
|
// Can fall further down
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish the fall at the found bottom:
|
|
|
|
int BlockX = a_RelX + a_Chunk->GetPosX() * cChunkDef::Width;
|
|
|
|
int BlockZ = a_RelZ + a_Chunk->GetPosZ() * cChunkDef::Width;
|
2013-03-03 03:40:37 -05:00
|
|
|
FinishFalling(&m_World, BlockX, BlockY, BlockZ, FallingBlockType, FallingBlockMeta);
|
2013-03-02 15:24:18 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The block just "fell off the world" without leaving a trace
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-03-02 14:57:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|