summaryrefslogtreecommitdiffstats
path: root/source/SandSimulator.cpp
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-09-24 00:09:57 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-09-24 00:09:57 +0200
commitecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26 (patch)
tree898e37fe747f0fdcefeb88f833557fd45db3347c /source/SandSimulator.cpp
parentSource files cleanup: ChunkDataSerializer is Protocol-related (diff)
downloadcuberite-ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26.tar
cuberite-ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26.tar.gz
cuberite-ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26.tar.bz2
cuberite-ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26.tar.lz
cuberite-ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26.tar.xz
cuberite-ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26.tar.zst
cuberite-ecfe6ab65bd1fc2c7f5733fe6ef4e6ddaac44a26.zip
Diffstat (limited to 'source/SandSimulator.cpp')
-rw-r--r--source/SandSimulator.cpp91
1 files changed, 91 insertions, 0 deletions
diff --git a/source/SandSimulator.cpp b/source/SandSimulator.cpp
new file mode 100644
index 000000000..1dd9d3d88
--- /dev/null
+++ b/source/SandSimulator.cpp
@@ -0,0 +1,91 @@
+
+#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
+
+#include "SandSimulator.h"
+#include "World.h"
+#include "Vector3i.h"
+#include "BlockID.h"
+#include "Defines.h"
+
+
+
+
+
+cSandSimulator::cSandSimulator( cWorld* a_World )
+ : cSimulator(a_World)
+ , m_Blocks(new BlockList)
+ , m_Buffer(new BlockList)
+{
+
+}
+
+cSandSimulator::~cSandSimulator()
+{
+ delete m_Buffer;
+ delete m_Blocks;
+}
+
+void cSandSimulator::Simulate( float a_Dt )
+{
+ m_Buffer->clear();
+ std::swap( m_Blocks, m_Buffer );
+
+ for( BlockList::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(a_X, a_Y, a_Z);
+
+ //check for duplicates
+ for( BlockList::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)
+ || a_BlockID == E_BLOCK_FIRE;
+
+}
+
+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 );
+}