summaryrefslogtreecommitdiffstats
path: root/src/Blocks/BlockDeadBush.h
blob: 68846ff469cf4a867faa437ffc4dae062d7e4090 (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

#pragma once

#include "BlockHandler.h"





class cBlockDeadBushHandler :
	public cBlockHandler
{
	typedef cBlockHandler super;
public:
	cBlockDeadBushHandler(BLOCKTYPE a_BlockType)
		: cBlockHandler(a_BlockType)
	{
	}

	virtual bool DoesIgnoreBuildCollision(cChunkInterface & a_ChunkInterface, Vector3i a_Pos, cPlayer & a_Player, NIBBLETYPE a_Meta) override
	{
		return true;
	}

	virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
	{
		if (a_RelY <= 0)
		{
			return false;
		}

		BLOCKTYPE BelowBlock = a_Chunk.GetBlock(a_RelX, a_RelY - 1, a_RelZ);
		switch (BelowBlock)
		{
			case E_BLOCK_CLAY:
			case E_BLOCK_HARDENED_CLAY:
			case E_BLOCK_STAINED_CLAY:
			case E_BLOCK_SAND:
			{
				return true;
			}
			default: return false;
		}
	}

	virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override
	{
		// Drop 0-3 sticks
		char chance = GetRandomProvider().RandInt<char>(3);
		if (chance != 0)
		{
			a_Pickups.emplace_back(E_ITEM_STICK, chance, 0);
		}
	}

	virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
	{
		UNUSED(a_Meta);
		return 0;
	}

	virtual void DropBlock(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_BlockPluginInterface, cEntity * a_Digger, int a_BlockX, int a_BlockY, int a_BlockZ, bool a_CanDrop) override
	{
		if (a_CanDrop && (a_Digger != nullptr) && (a_Digger->GetEquippedWeapon().m_ItemType == E_ITEM_SHEARS))
		{
			NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta({a_BlockX, a_BlockY, a_BlockZ});
			cItems Drops;
			Drops.Add(m_BlockType, 1, Meta);

			// Allow plugins to modify the pickups:
			a_BlockPluginInterface.CallHookBlockToPickups(a_Digger, a_BlockX, a_BlockY, a_BlockZ, m_BlockType, Meta, Drops);

			// Spawn the pickups:
			if (!Drops.empty())
			{
				auto & r1 = GetRandomProvider();

				// Mid-block position first
				double MicroX, MicroY, MicroZ;
				MicroX = a_BlockX + 0.5;
				MicroY = a_BlockY + 0.5;
				MicroZ = a_BlockZ + 0.5;

				// Add random offset second
				MicroX += r1.RandReal<double>(-0.5, 0.5);
				MicroZ += r1.RandReal<double>(-0.5, 0.5);

				a_WorldInterface.SpawnItemPickups(Drops, MicroX, MicroY, MicroZ);
			}
			return;
		}

		super::DropBlock(a_ChunkInterface, a_WorldInterface, a_BlockPluginInterface, a_Digger, a_BlockX, a_BlockY, a_BlockZ, a_CanDrop);
	}
} ;