aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/LeafTile.cpp
blob: f8f5deae4710a8cb18ab8eff041f123cf5f3ca3a (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "stdafx.h"
#include "..\Minecraft.Client\Minecraft.h"
#include "LeafTile.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.biome.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.stats.h"
#include "net.minecraft.world.h"

const unsigned int LeafTile::LEAF_NAMES[LEAF_NAMES_LENGTH] = {	IDS_TILE_LEAVES_OAK,
	IDS_TILE_LEAVES_SPRUCE,
	IDS_TILE_LEAVES_BIRCH,
	IDS_TILE_LEAVES_JUNGLE,
};

const wstring LeafTile::TEXTURES[2][4] = { {L"leaves", L"leaves_spruce", L"leaves", L"leaves_jungle"}, {L"leaves_opaque", L"leaves_spruce_opaque", L"leaves_opaque", L"leaves_jungle_opaque"},};

LeafTile::LeafTile(int id) : TransparentTile(id, Material::leaves, false, isSolidRender())
{
	checkBuffer = nullptr;
	fancyTextureSet = 0;
	setTicking(true);
}

LeafTile::~LeafTile()
{
	delete [] checkBuffer;
}

int LeafTile::getColor() const
{
	// 4J Stu - Not using this any more
	//double temp = 0.5;
	//double rain = 1.0;

	//return FoliageColor::get(temp, rain);

	return Minecraft::GetInstance()->getColourTable()->getColor( eMinecraftColour_Foliage_Common );
}

int LeafTile::getColor(int data)
{
	if ((data & LEAF_TYPE_MASK) == EVERGREEN_LEAF)
	{
		return FoliageColor::getEvergreenColor();
	}
	if ((data & LEAF_TYPE_MASK) == BIRCH_LEAF)
	{
		return FoliageColor::getBirchColor();
	}

	return FoliageColor::getDefaultColor();
}

int LeafTile::getColor(LevelSource *level, int x, int y, int z)
{
	return getColor(level, x, y, z, level->getData(x, y, z) );
}

// 4J - changed interface to have data passed in, and put existing interface as wrapper above
int LeafTile::getColor(LevelSource *level, int x, int y, int z, int data)
{
	if ((data & LEAF_TYPE_MASK) == EVERGREEN_LEAF)
	{
		return FoliageColor::getEvergreenColor();
	}
	if ((data & LEAF_TYPE_MASK) == BIRCH_LEAF)
	{
		return FoliageColor::getBirchColor();
	}

	int totalRed = 0;
	int totalGreen = 0;
	int totalBlue = 0;

	for (int oz = -1; oz <= 1; oz++)
	{
		for (int ox = -1; ox <= 1; ox++)
		{
			int foliageColor = level->getBiome(x + ox, z + oz)->getFolageColor();

			totalRed += (foliageColor & 0xff0000) >> 16;
			totalGreen += (foliageColor & 0xff00) >> 8;
			totalBlue += (foliageColor & 0xff);
		}
	}

	return (((totalRed / 9) & 0xFF) << 16) | (((totalGreen / 9) & 0xFF) << 8) | (((totalBlue / 9) & 0xFF));
}

void LeafTile::onRemove(Level *level, int x, int y, int z, int id, int data)
{
	int r = 1;
	int r2 = r + 1;

	if (level->hasChunksAt(x - r2, y - r2, z - r2, x + r2, y + r2, z + r2))
	{
		for (int xo = -r; xo <= r; xo++)
			for (int yo = -r; yo <= r; yo++)
				for (int zo = -r; zo <= r; zo++)
				{
					int t = level->getTile(x + xo, y + yo, z + zo);
					if (t == Tile::leaves_Id)
					{
						int currentData = level->getData(x + xo, y + yo, z + zo);
						level->setData(x + xo, y + yo, z + zo, currentData | UPDATE_LEAF_BIT, Tile::UPDATE_NONE);
					}
				}
	}

}

void LeafTile::tick(Level *level, int x, int y, int z, Random *random)
{
	if (level->isClientSide) return;

	int currentData = level->getData(x, y, z);
	if ((currentData & UPDATE_LEAF_BIT) != 0 && (currentData & PERSISTENT_LEAF_BIT) == 0)
	{
		int r = REQUIRED_WOOD_RANGE;
		int r2 = r + 1;

		int W = 32;
		int WW = W * W;
		int WO = W / 2;
		if (checkBuffer == nullptr)
		{
			checkBuffer = new int[W * W * W];
		}

		if (level->hasChunksAt(x - r2, y - r2, z - r2, x + r2, y + r2, z + r2))
		{
			// 4J Stu - Assuming we remain in the same chunk, getTile accesses an array that varies least by y
			// Changing the ordering here to loop by y last
			for (int xo = -r; xo <= r; xo++)
				for (int zo = -r; zo <= r; zo++)
					for (int yo = -r; yo <= r; yo++)
					{
						int t = level->getTile(x + xo, y + yo, z + zo);
						if (t == Tile::treeTrunk_Id)
						{
							checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] = 0;
						}
						else if (t == Tile::leaves_Id)
						{
							checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] = -2;
						}
						else
						{
							checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] = -1;
						}
					}
					for (int i = 1; i <= REQUIRED_WOOD_RANGE; i++)
					{
						for (int xo = -r; xo <= r; xo++)
							for (int yo = -r; yo <= r; yo++)
								for (int zo = -r; zo <= r; zo++)
								{
									if (checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] == i - 1)
									{
										if (checkBuffer[(xo + WO - 1) * WW + (yo + WO) * W + (zo + WO)] == -2)
										{
											checkBuffer[(xo + WO - 1) * WW + (yo + WO) * W + (zo + WO)] = i;
										}
										if (checkBuffer[(xo + WO + 1) * WW + (yo + WO) * W + (zo + WO)] == -2)
										{
											checkBuffer[(xo + WO + 1) * WW + (yo + WO) * W + (zo + WO)] = i;
										}
										if (checkBuffer[(xo + WO) * WW + (yo + WO - 1) * W + (zo + WO)] == -2)
										{
											checkBuffer[(xo + WO) * WW + (yo + WO - 1) * W + (zo + WO)] = i;
										}
										if (checkBuffer[(xo + WO) * WW + (yo + WO + 1) * W + (zo + WO)] == -2)
										{
											checkBuffer[(xo + WO) * WW + (yo + WO + 1) * W + (zo + WO)] = i;
										}
										if (checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO - 1)] == -2)
										{
											checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO - 1)] = i;
										}
										if (checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO + 1)] == -2)
										{
											checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO + 1)] = i;
										}
									}
								}
					}
		}

		int mid = checkBuffer[(WO) * WW + (WO) * W + (WO)];
		if (mid >= 0)
		{
			level->setData(x, y, z, currentData & ~UPDATE_LEAF_BIT, Tile::UPDATE_NONE);
		}
		else
		{
			die(level, x, y, z);
		}
	}

}

void LeafTile::animateTick(Level *level, int x, int y, int z, Random *random)
{
	if (level->isRainingAt(x, y + 1, z) && !level->isTopSolidBlocking(x, y - 1, z) && random->nextInt(15) == 1)
	{
		double xx = x + random->nextFloat();
		double yy = y - 0.05;
		double zz = z + random->nextFloat();

		level->addParticle(eParticleType_dripWater, xx, yy, zz, 0, 0, 0);
	}
}

void LeafTile::die(Level *level, int x, int y, int z)
{
	Tile::spawnResources(level, x, y, z, level->getData(x, y, z), 0);
	level->removeTile(x, y, z);
}

int LeafTile::getResourceCount(Random *random)
{
	return random->nextInt(20) == 0 ? 1 : 0;
}

int LeafTile::getResource(int data, Random *random, int playerBonusLevel)
{
	return Tile::sapling_Id;
}

// 4J DCR: Brought forward from 1.2
void LeafTile::spawnResources(Level *level, int x, int y, int z, int data, float odds, int playerBonusLevel)
{
	if (!level->isClientSide)
	{
		int chance = 20;
		if ((data & LEAF_TYPE_MASK) == JUNGLE_LEAF)
		{
			chance = 40;
		}
		if (playerBonusLevel > 0)
		{
			chance -= 2 << playerBonusLevel;
			if (chance < 10)
			{
				chance = 10;
			}
		}
		if (level->random->nextInt(chance) == 0)
		{
			int type = getResource(data, level->random,playerBonusLevel);
			popResource(level, x, y, z, std::make_shared<ItemInstance>(type, 1, getSpawnResourcesAuxValue(data)));
		}

		chance = 200;
		if (playerBonusLevel > 0)
		{
			chance -= 10 << playerBonusLevel;
			if (chance < 40)
			{
				chance = 40;
			}
		}
		if ((data & LEAF_TYPE_MASK) == NORMAL_LEAF && level->random->nextInt(chance) == 0)
		{
			popResource(level, x, y, z, std::make_shared<ItemInstance>(Item::apple_Id, 1, 0));
		}
	}
}

void LeafTile::playerDestroy(Level *level, shared_ptr<Player> player, int x, int y, int z, int data)
{
	if (!level->isClientSide && player->getSelectedItem() != nullptr && player->getSelectedItem()->id == Item::shears->id)
	{
		player->awardStat(
			GenericStats::blocksMined(id),
			GenericStats::param_blocksMined(id,data,1)
			);

		// drop leaf block instead of sapling
		popResource(level, x, y, z, std::make_shared<ItemInstance>(Tile::leaves_Id, 1, data & LEAF_TYPE_MASK));
	}
	else
	{
		TransparentTile::playerDestroy(level, player, x, y, z, data);
	}
}

int LeafTile::getSpawnResourcesAuxValue(int data)
{
	return data & LEAF_TYPE_MASK;
}

bool LeafTile::isSolidRender(bool isServerLevel)
{
	// 4J Stu - The server level shouldn't care how the tile is rendered!
	// Fix for #9407 - Gameplay: Destroying a block of snow on top of trees, removes any adjacent snow.
	if(isServerLevel) return true;
	return !allowSame;
}

Icon *LeafTile::getTexture(int face, int data)
{
	if ((data & LEAF_TYPE_MASK) == EVERGREEN_LEAF)
	{
		return icons[fancyTextureSet][EVERGREEN_LEAF];
	}
	if ((data & LEAF_TYPE_MASK) == JUNGLE_LEAF)
	{
		return icons[fancyTextureSet][JUNGLE_LEAF];
	}
	if ((data & LEAF_TYPE_MASK) == BIRCH_LEAF)
	{
		return icons[fancyTextureSet][BIRCH_LEAF];
	}
	return icons[fancyTextureSet][0];
}

void LeafTile::setFancy(bool fancyGraphics)
{
	allowSame = fancyGraphics;
	fancyTextureSet = (fancyGraphics ? 0 : 1);
}

shared_ptr<ItemInstance> LeafTile::getSilkTouchItemInstance(int data)
{
	return std::make_shared<ItemInstance>(id, 1, data & LEAF_TYPE_MASK);
}

void LeafTile::stepOn(Level *level, int x, int y, int z, shared_ptr<Entity> entity)
{
	TransparentTile::stepOn(level, x, y, z, entity);
}

bool LeafTile::shouldTileTick(Level *level, int x,int y,int z)
{
	int currentData = level->getData(x, y, z);
	return (currentData & UPDATE_LEAF_BIT) != 0;
}

unsigned int LeafTile::getDescriptionId(int iData /*= -1*/)
{
	int leafIndex = iData & LEAF_TYPE_MASK;
	return LeafTile::LEAF_NAMES[leafIndex];
}

void LeafTile::registerIcons(IconRegister *iconRegister)
{
	for (int fancy = 0; fancy < 2; fancy++)
	{
		//icons[fancy] = new Icon[TEXTURES[fancy].length];

		for (int i = 0; i < 4; i++)
		{
			icons[fancy][i] = iconRegister->registerIcon(TEXTURES[fancy][i]);
		}
	}
}