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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#pragma once
#include "BlockHandler.h"
#include "../World.h"
#include "../FastRandom.h"
class cBlockSaplingHandler :
public cBlockHandler
{
public:
cBlockSaplingHandler(BLOCKTYPE a_BlockType)
: cBlockHandler(a_BlockType)
{
}
virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override
{
// Only the first 2 bits contain the display information and the 4th bit is for the growth indicator, but, we use 0x07 for forward compatibility
a_Pickups.push_back(cItem(E_BLOCK_SAPLING, 1, a_BlockMeta & 0x07));
}
virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
{
return (a_RelY > 0) && IsBlockTypeOfDirt(a_Chunk.GetBlock(a_RelX, a_RelY - 1, a_RelZ));
}
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 = std::max(a_Chunk.GetBlockLight(a_RelX, a_RelY, a_RelZ), a_Chunk.GetTimeAlteredLight(a_Chunk.GetSkyLight(a_RelX, a_RelY, a_RelZ)));
// Only grow if we have the right amount of light
if (Light > 8)
{
auto & random = GetRandomProvider();
// Only grow if we are in the right growth stage and have the right amount of space around them.
if (((Meta & 0x08) != 0) && random.RandBool(0.45) && CanGrowAt(a_Chunk, a_RelX, a_RelY, a_RelZ, Meta))
{
int BlockX = a_RelX + a_Chunk.GetPosX() * cChunkDef::Width;
int BlockZ = a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width;
a_Chunk.GetWorld()->GrowTree(BlockX, a_RelY, BlockZ);
}
// Only move to the next growth stage if we haven't gone there yet
else if (((Meta & 0x08) == 0) && random.RandBool(0.45))
{
a_Chunk.SetMeta(a_RelX, a_RelY, a_RelZ, Meta | 0x08);
}
}
}
bool CanGrowAt(cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta)
{
a_Meta = a_Meta & 0x07;
int CheckHeight = 0;
bool LargeTree = false;
// Get the height to check against
switch (a_Meta)
{
case E_META_SAPLING_APPLE:
{
CheckHeight = 5;
break;
}
case E_META_SAPLING_CONIFER:
{
CheckHeight = 7;
if (IsLargeTree(a_Chunk, a_RelX, a_RelY, a_RelZ, a_Meta))
{
CheckHeight = 16;
LargeTree = true;
}
break;
}
case E_META_SAPLING_BIRCH:
{
CheckHeight = 6;
break;
}
case E_META_SAPLING_JUNGLE:
{
CheckHeight = 7;
if (IsLargeTree(a_Chunk, a_RelX, a_RelY, a_RelZ, a_Meta))
{
CheckHeight = 13;
LargeTree = true;
}
break;
}
// Acacias don't need horizontal clearance
case E_META_SAPLING_ACACIA:
{
if (!IsLargeTree(a_Chunk, a_RelX, a_RelY, a_RelZ, a_Meta))
{
return false;
}
CheckHeight = 7;
LargeTree = true;
break;
}
// Dark Oaks don't need horizontal clearance
case E_META_SAPLING_DARK_OAK:
{
if (!IsLargeTree(a_Chunk, a_RelX, a_RelY, a_RelZ, a_Meta))
{
return false;
}
CheckHeight = 7;
LargeTree = true;
break;
}
}
// We should always get a valid CheckHeight
ASSERT(CheckHeight != 0);
// Don't grow a tree if we don't have enough space left above it in the chunk
if ((a_RelY + CheckHeight) > cChunkDef::Height)
{
return false;
}
bool CanGrow = true;
// Validate the neighbor blocks. They cannot be solid.
BLOCKTYPE check = E_BLOCK_AIR;
a_Chunk.UnboundedRelGetBlockType(a_RelX - 1, a_RelY, a_RelZ, check);
CanGrow = CanGrow && cBlockInfo::IsTransparent(check);
a_Chunk.UnboundedRelGetBlockType(a_RelX + 1, a_RelY, a_RelZ, check);
CanGrow = CanGrow && cBlockInfo::IsTransparent(check);
a_Chunk.UnboundedRelGetBlockType(a_RelX, a_RelY, a_RelZ - 1, check);
CanGrow = CanGrow && cBlockInfo::IsTransparent(check);
a_Chunk.UnboundedRelGetBlockType(a_RelX, a_RelY, a_RelZ + 1, check);
CanGrow = CanGrow && cBlockInfo::IsTransparent(check);
while (CheckHeight && CanGrow)
{
check = a_Chunk.GetBlock(a_RelX, a_RelY + CheckHeight, a_RelZ);
CanGrow = CanGrow && ((check == E_BLOCK_AIR) || (check == E_BLOCK_LEAVES));
// We have to check above the neighboring saplings as well
if (LargeTree)
{
a_Chunk.UnboundedRelGetBlockType(a_RelX + 1, a_RelY + CheckHeight, a_RelZ, check);
CanGrow = CanGrow && ((check == E_BLOCK_AIR) || (check == E_BLOCK_LEAVES));
a_Chunk.UnboundedRelGetBlockType(a_RelX, a_RelY + CheckHeight, a_RelZ + 1, check);
CanGrow = CanGrow && ((check == E_BLOCK_AIR) || (check == E_BLOCK_LEAVES));
a_Chunk.UnboundedRelGetBlockType(a_RelX + 1, a_RelY + CheckHeight, a_RelZ + 1, check);
CanGrow = CanGrow && ((check == E_BLOCK_AIR) || (check == E_BLOCK_LEAVES));
}
--CheckHeight;
}
return CanGrow;
}
private:
bool IsLargeTree(cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta)
{
BLOCKTYPE type;
NIBBLETYPE meta;
bool LargeTree = true;
a_Chunk.UnboundedRelGetBlock(a_RelX + 1, a_RelY, a_RelZ, type, meta);
LargeTree = LargeTree && (type == E_BLOCK_SAPLING) && ((a_Meta & meta) == a_Meta);
a_Chunk.UnboundedRelGetBlock(a_RelX + 1, a_RelY, a_RelZ + 1, type, meta);
LargeTree = LargeTree && (type == E_BLOCK_SAPLING) && ((a_Meta & meta) == a_Meta);
a_Chunk.UnboundedRelGetBlock(a_RelX, a_RelY, a_RelZ + 1, type, meta);
LargeTree = LargeTree && (type == E_BLOCK_SAPLING) && ((a_Meta & meta) == a_Meta);
return LargeTree;
}
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
{
UNUSED(a_Meta);
return 7;
}
} ;
|