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-02-28 08:39:20 -05:00
|
|
|
cSandSimulator::cSandSimulator(cWorld & a_World)
|
2012-06-14 09:06:06 -04:00
|
|
|
: cSimulator(a_World)
|
|
|
|
, m_Blocks(new BlockList)
|
|
|
|
, m_Buffer(new BlockList)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
cSandSimulator::~cSandSimulator()
|
|
|
|
{
|
|
|
|
delete m_Buffer;
|
|
|
|
delete m_Blocks;
|
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 08:39:20 -05:00
|
|
|
void cSandSimulator::Simulate(float a_Dt)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_Buffer->clear();
|
|
|
|
std::swap( m_Blocks, m_Buffer );
|
|
|
|
|
|
|
|
for( BlockList::iterator itr = m_Buffer->begin(); itr != m_Buffer->end(); ++itr )
|
|
|
|
{
|
|
|
|
Vector3i Pos = *itr;
|
2013-02-28 08:39:20 -05:00
|
|
|
BLOCKTYPE BlockID = m_World.GetBlock(Pos.x, Pos.y, Pos.z);
|
2012-06-14 09:06:06 -04:00
|
|
|
if(!IsAllowedBlock(BlockID))
|
|
|
|
continue;
|
|
|
|
|
2013-02-28 08:39:20 -05:00
|
|
|
BLOCKTYPE BottomBlock = m_World.GetBlock( Pos.x, Pos.y - 1, Pos.z );
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
if( IsPassable(BottomBlock) )
|
|
|
|
{
|
2013-02-28 08:39:20 -05:00
|
|
|
cFallingBlock * FallingBlock = new cFallingBlock(Pos, BlockID);
|
|
|
|
FallingBlock->Initialize(&m_World);
|
|
|
|
m_World.SetBlock( Pos.x, Pos.y, Pos.z, E_BLOCK_AIR, 0 );
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-03 04:52:11 -04:00
|
|
|
bool cSandSimulator::IsAllowedBlock( BLOCKTYPE a_BlockType )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-10-03 04:52:11 -04:00
|
|
|
return a_BlockType == E_BLOCK_SAND
|
|
|
|
|| a_BlockType == E_BLOCK_GRAVEL;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 02:42:45 -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-02-28 02:42:45 -05:00
|
|
|
// TODO: Optimize this by passing the block type along
|
|
|
|
int RelX = a_BlockX;
|
|
|
|
int RelY = a_BlockY;
|
|
|
|
int RelZ = a_BlockZ;
|
|
|
|
int ChunkX, ChunkZ;
|
|
|
|
cChunkDef::AbsoluteToRelative(RelX, RelY, RelZ, ChunkX, ChunkZ);
|
|
|
|
if (!IsAllowedBlock(a_Chunk->GetBlock(RelX, RelY, RelZ)))
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
2013-02-28 02:42:45 -05:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-02-28 02:42:45 -05:00
|
|
|
Vector3i Block(a_BlockX, a_BlockY, a_BlockZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
//check for duplicates
|
2013-02-28 02:42:45 -05:00
|
|
|
for (BlockList::iterator itr = m_Blocks->begin(); itr != m_Blocks->end(); ++itr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
Vector3i Pos = *itr;
|
2013-02-28 02:42:45 -05:00
|
|
|
if ((Pos.x == a_BlockX) && (Pos.y == a_BlockY) && (Pos.z == a_BlockZ))
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
2013-02-28 02:42:45 -05:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_Blocks->push_back(Block);
|
|
|
|
}
|
|
|
|
|
2012-09-30 12:37:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 02:42:45 -05:00
|
|
|
bool cSandSimulator::IsPassable(BLOCKTYPE a_BlockType)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-02-28 02:42:45 -05:00
|
|
|
return (a_BlockType == E_BLOCK_AIR)
|
2012-10-03 04:52:11 -04:00
|
|
|
|| IsBlockWater(a_BlockType)
|
|
|
|
|| IsBlockLava(a_BlockType)
|
2013-02-28 02:42:45 -05:00
|
|
|
|| (a_BlockType == E_BLOCK_FIRE);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|