summaryrefslogtreecommitdiffstats
path: root/src/Blocks/BlockCrops.h
blob: e510d5f8d00fb2afc7e6a3683e795fd1af7b88e6 (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

#pragma once

#include "BlockHandler.h"
#include "../FastRandom.h"





/** Common class that takes care of carrots, potatoes and wheat */
class cBlockCropsHandler :
	public cBlockHandler
{
public:
	cBlockCropsHandler(BLOCKTYPE a_BlockType)
		: cBlockHandler(a_BlockType)
	{
	}

	virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_Meta) override
	{
		cFastRandom rand;

		if (a_Meta == 0x7)
		{
			// Is fully grown, drop the entire produce:
			switch (m_BlockType)
			{
				case E_BLOCK_CROPS:
				{
					a_Pickups.push_back(cItem(E_ITEM_WHEAT, 1, 0));
					a_Pickups.push_back(cItem(E_ITEM_SEEDS, (char)(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2), 0));  // [1 .. 3] with high preference of 2
					break;
				}
				case E_BLOCK_CARROTS:
				{
					a_Pickups.push_back(cItem(E_ITEM_CARROT, (char)(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2), 0));  // [1 .. 3] with high preference of 2
					break;
				}
				case E_BLOCK_POTATOES:
				{
					a_Pickups.push_back(cItem(E_ITEM_POTATO, (char)(1 + (rand.NextInt(3) + rand.NextInt(3)) / 2), 0));  // [1 .. 3] with high preference of 2
					if (rand.NextInt(21) == 0)
					{
						// With a 5% chance, drop a poisonous potato as well
						a_Pickups.push_back(cItem(E_ITEM_POISONOUS_POTATO, 1, 0));
					}
					break;
				}
				default:
				{
					ASSERT(!"Unhandled block type");
					break;
				}
			}  // switch (m_BlockType)
		}
		else
		{
			// Drop 1 item of whatever is growing
			switch (m_BlockType)
			{
				case E_BLOCK_CROPS:    a_Pickups.push_back(cItem(E_ITEM_SEEDS,  1, 0)); break;
				case E_BLOCK_CARROTS:  a_Pickups.push_back(cItem(E_ITEM_CARROT, 1, 0)); break;
				case E_BLOCK_POTATOES: a_Pickups.push_back(cItem(E_ITEM_POTATO, 1, 0)); break;
				default:
				{
					ASSERT(!"Unhandled block type");
					break;
				}
			}
		}
	}
	
	virtual void OnUpdate(cChunkInterface & cChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
	{
		NIBBLETYPE Meta     = a_Chunk.GetMeta      (a_RelX, a_RelY, a_RelZ);
		NIBBLETYPE Light    = a_Chunk.GetBlockLight(a_RelX, a_RelY, a_RelZ);
		NIBBLETYPE SkyLight = a_Chunk.GetSkyLight  (a_RelX, a_RelY, a_RelZ);

		if (SkyLight > Light)
		{
			Light = SkyLight;
		}
		
		if ((Meta < 7) && (Light > 8))
		{
			a_Chunk.FastSetBlock(a_RelX, a_RelY, a_RelZ, m_BlockType, ++Meta);
		}
		else if (Light < 9)
		{
			a_Chunk.GetWorld()->DigBlock(a_RelX + a_Chunk.GetPosX() * cChunkDef::Width, a_RelY, a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width);
		}
	}

	virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
	{
		return ((a_RelY > 0) && (a_Chunk.GetBlock(a_RelX, a_RelY - 1, a_RelZ) == E_BLOCK_FARMLAND));
	}

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