summaryrefslogtreecommitdiffstats
path: root/src/Blocks/BlockSponge.h
blob: 2e920e856884639dc5be315ccf0ff48181989593 (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

#pragma once

#include "BlockHandler.h"
#include "ChunkInterface.h"





class cBlockSpongeHandler : public cBlockHandler
{
public:
	cBlockSpongeHandler(BLOCKTYPE a_BlockType)
		: cBlockHandler(a_BlockType)
	{
	}


	virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, int a_BlockX, int a_BlockY, int a_BlockZ) override
	{
		if (a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) == E_META_SPONGE_NORMAL)
		{
			AbsorbWater(a_ChunkInterface, a_BlockX, a_BlockY, a_BlockZ);
		}
	}


	virtual void OnPlacedByPlayer(
		cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer * a_Player,
		int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
		int a_CursorX, int a_CursorY, int a_CursorZ,
		BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta
	)
	{
		if (a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) == E_META_SPONGE_NORMAL)
		{
			AbsorbWater(a_ChunkInterface, a_BlockX, a_BlockY, a_BlockZ);
		}
	}


	bool AbsorbWater(cChunkInterface & a_ChunkInterface, int a_BlockX, int a_BlockY, int a_BlockZ)
	{
		std::list<Vector3i> Blocks;
		std::list<Vector3i> WaterBlocks;
		Blocks.push_back(Vector3i(a_BlockX, a_BlockY, a_BlockZ));

		int CurrentRadius = 0;
		while (!Blocks.empty() && (CurrentRadius < 6) && (WaterBlocks.size() <= 64))
		{
			Vector3i BlockLoc = Blocks.front();
			Blocks.pop_front();

			bool ChangedBlock = false;
			for (size_t i = 0; i <= 5; ++i)
			{
				eBlockFace Face = static_cast<eBlockFace>(i);

				int BlockX = BlockLoc.x, BlockY = BlockLoc.y, BlockZ = BlockLoc.z;
				AddFaceDirection(BlockX, BlockY, BlockZ, Face);

				if (IsBlockWater(a_ChunkInterface.GetBlock(BlockX, BlockY, BlockZ)))
				{
					Blocks.push_back(Vector3i(BlockX, BlockY, BlockZ));
					WaterBlocks.push_back(Vector3i(BlockX, BlockY, BlockZ));
					a_ChunkInterface.FastSetBlock(BlockX, BlockY, BlockZ, E_BLOCK_AIR, 0);

					ChangedBlock = true;
				}
			}

			if (ChangedBlock)
			{
				CurrentRadius++;
			}
		}

		if (WaterBlocks.size() > 0)
		{
			// Change the sponge to a wet sponge.
			a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, E_META_SPONGE_WET);

			// Remove the water
			for (auto Block : WaterBlocks)
			{
				a_ChunkInterface.SetBlock(Block.x, Block.y, Block.z, E_BLOCK_AIR, 0);
			}

			// ChunkInterface doesn't have a BroadcastSoundParticleEffect() method :(
			// a_World->BroadcastSoundParticleEffect(2001, (int) a_BlockX * 8.f), (int) (a_BlockY * 8.f), (int) (a_BlockZ * 8.f), E_BLOCK_STATIONARY_WATER);
			return true;
		}
		return false;
	}

} ;