1
0
cuberite-2a/source/cSandSimulator.cpp
lapayo94@gmail.com 2ca40c819e - Pickups should now burn in fire
- The player no longer gets an empty bucket when in creative mode
- improved the simulators again (moved to std::list because this should be faster with so many objects) (But the water simulation still is very slow)

git-svn-id: http://mc-server.googlecode.com/svn/trunk@153 0a769ca7-a7f5-676a-18bf-c427514a06d6
2011-12-29 02:44:21 +00:00

85 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "cSandSimulator.h"
#include "cWorld.h"
#include "Vector3i.h"
#include "BlockID.h"
#include "Defines.h"
#include <vector>
cSandSimulator::cSandSimulator( cWorld* a_World )
: cSimulator(a_World)
, m_Blocks(new std::list <Vector3i *>)
, m_Buffer(new std::list <Vector3i *>)
{
}
cSandSimulator::~cSandSimulator()
{
delete m_Buffer;
delete m_Blocks;
}
void cSandSimulator::Simulate( float a_Dt )
{
m_Buffer->clear();
std::swap( m_Blocks, m_Buffer );
for( std::list<Vector3i *>::iterator itr = m_Buffer->begin(); itr != m_Buffer->end(); ++itr )
{
Vector3i *Pos = *itr;
char BlockID = m_World->GetBlock(Pos->x, Pos->y, Pos->z);
if(!IsAllowedBlock(BlockID))
continue;
char BottomBlock = m_World->GetBlock( Pos->x, Pos->y - 1, Pos->z );
if( IsPassable(BottomBlock) )
{
m_World->SetBlock( Pos->x, Pos->y, Pos->z, E_BLOCK_AIR, 0 );
m_World->SetBlock( Pos->x, Pos->y - 1, Pos->z, BlockID, 0 );
}
}
}
bool cSandSimulator::IsAllowedBlock( char a_BlockID )
{
return a_BlockID == E_BLOCK_SAND
|| a_BlockID == E_BLOCK_GRAVEL;
}
void cSandSimulator::AddBlock(int a_X, int a_Y, int a_Z)
{
if(!IsAllowedBlock(m_World->GetBlock(a_X, a_Y, a_Z))) //This should save very much time because it doesn´t have to iterate through all blocks
return;
Vector3i *Block = new Vector3i(a_X, a_Y, a_Z);
//check for duplicates
for( std::list<Vector3i *>::iterator itr = m_Blocks->begin(); itr != m_Blocks->end(); ++itr )
{
Vector3i *Pos = *itr;
if( Pos->x == a_X && Pos->y == a_Y && Pos->z == a_Z )
return;
}
m_Blocks->push_back(Block);
}
bool cSandSimulator::IsPassable( char a_BlockID )
{
return a_BlockID == E_BLOCK_AIR
|| IsBlockWater(a_BlockID)
|| IsBlockLava(a_BlockID);
}
void cSandSimulator::WakeUp( int a_X, int a_Y, int a_Z )
{
//Nothing else needs to be simulated :D (Bugs not included :s)
AddBlock( a_X, a_Y+1, a_Z );
AddBlock( a_X, a_Y, a_Z );
}