summaryrefslogtreecommitdiffstats
path: root/source/Simulator/SandSimulator.cpp
blob: f9d58f030a648788d33423a5b8343007ec758592 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "SandSimulator.h"
#include "../World.h"
#include "../BlockID.h"
#include "../Defines.h"
#include "../FallingBlock.h"
#include "../Chunk.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;
		BLOCKTYPE BlockID = m_World->GetBlock(Pos.x, Pos.y, Pos.z);
		if(!IsAllowedBlock(BlockID))
			continue;

		BLOCKTYPE BottomBlock = m_World->GetBlock( Pos.x, Pos.y - 1, Pos.z );
		
		if( IsPassable(BottomBlock) )
		{
			cFallingBlock * FallingBlock = new cFallingBlock( Pos, BlockID );
			FallingBlock->Initialize( m_World );
 			m_World->SetBlock( Pos.x, Pos.y, Pos.z, E_BLOCK_AIR, 0 );
		}
	}
		
}





bool cSandSimulator::IsAllowedBlock( BLOCKTYPE a_BlockType )
{
	return a_BlockType == E_BLOCK_SAND
		|| a_BlockType == E_BLOCK_GRAVEL;
}





void cSandSimulator::AddBlock(int a_BlockX, int a_BlockY, int a_BlockZ, cChunk * a_Chunk)
{
	// 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)))
	{
		return;
	}

	Vector3i Block(a_BlockX, a_BlockY, a_BlockZ);
	
	//check for duplicates
	for (BlockList::iterator itr = m_Blocks->begin(); itr != m_Blocks->end(); ++itr)
	{
		Vector3i Pos = *itr;
		if ((Pos.x == a_BlockX) && (Pos.y == a_BlockY) && (Pos.z == a_BlockZ))
		{
			return;
		}
	}

	m_Blocks->push_back(Block);
}





bool cSandSimulator::IsPassable(BLOCKTYPE a_BlockType)
{
	return (a_BlockType == E_BLOCK_AIR)
		|| IsBlockWater(a_BlockType)
		|| IsBlockLava(a_BlockType)
		|| (a_BlockType == E_BLOCK_FIRE);
}