summaryrefslogtreecommitdiffstats
path: root/source/Blocks/BlockFarmland.h
blob: 16d94d4351ea27543236e0a028dba8ae2e96d964 (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

// BlockFarmland.h

// Declares the cBlcokFarmlandHandler representing the block handler for farmland





#pragma once

#include "BlockHandler.h"
#include "../BlockArea.h"





class cBlockFarmlandHandler :
	public cBlockHandler
{
	typedef cBlockHandler super;
	
public:
	cBlockFarmlandHandler(void) :
		super(E_BLOCK_FARMLAND)
	{
	}


	virtual void OnUpdate(cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ) override
	{
		// TODO: Rain hydrates farmland, too. Check world weather, don't search for water if raining.
		// NOTE: The desert biomes do not get precipitation, so another check needs to be made.
		
		// Search for water in a close proximity:
		// Ref.: http://www.minecraftwiki.net/wiki/Farmland#Hydrated_Farmland_Tiles
		cBlockArea Area;
		if (!Area.Read(a_World, a_BlockX - 4, a_BlockX + 4, a_BlockY, a_BlockY + 1, a_BlockZ - 4, a_BlockZ + 4))
		{
			// Too close to the world edge, cannot check surroudnings; don't tick at all
			return;
		}
		bool Found = false;

		if (a_World->GetWeather() != eWeather_Rain || a_World->GetBiomeAt(a_BlockX, a_BlockZ) == 1)
		{
		int NumBlocks = Area.GetBlockCount();
		BLOCKTYPE * BlockTypes = Area.GetBlockTypes();
		for (int i = 0; i < NumBlocks; i++)
		{
			if (
				(BlockTypes[i] == E_BLOCK_WATER) ||
				(BlockTypes[i] == E_BLOCK_STATIONARY_WATER)
			)
			{
				Found = true;
				break;
			}
		}
		}
		else
		{
			Found = true;
		}
		
		NIBBLETYPE BlockMeta = a_World->GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
		
		if (Found)
		{
			// Water was found, hydrate the block until hydration reaches 7:
			if (BlockMeta < 7)
			{
				a_World->FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, m_BlockType, ++BlockMeta);
			}
			return;
		}

		// Water wasn't found, de-hydrate block:
		if (BlockMeta > 0)
		{
			a_World->FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_FARMLAND, --BlockMeta);
			return;
		}
		
		// Farmland too dry. If nothing is growing on top, turn back to dirt:
		switch (a_World->GetBlock(a_BlockX, a_BlockY + 1, a_BlockZ))
		{
			case E_BLOCK_CROPS:
			case E_BLOCK_MELON_STEM:
			case E_BLOCK_PUMPKIN_STEM:
			{
				// Produce on top, don't revert
				break;
			}
			default:
			{
				a_World->FastSetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_DIRT, 0);
				break;
			}
		}
	}
} ;