aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/FireTile.cpp
blob: 49f859b513b4d658565f225b44225114f6554a77 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "stdafx.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.dimension.h"
#include "net.minecraft.world.h"
#include "net.minecraft.world.level.tile.h"
#include "FireTile.h"
#include "SoundTypes.h"
#include "..\Minecraft.Client\MinecraftServer.h"
#include "..\Minecraft.Client\PlayerList.h"

// AP - added for Vita to set Alpha Cut out
#include "IntBuffer.h"
#include "..\Minecraft.Client\Tesselator.h"


const wstring FireTile::TEXTURE_FIRST = L"fire_0";
const wstring FireTile::TEXTURE_SECOND = L"fire_1";

FireTile::FireTile(int id) : Tile(id, Material::fire,isSolidRender())
{
	flameOdds = new int[256];
	memset( flameOdds,0,sizeof(int)*256);

	burnOdds = new int[256];
	memset( burnOdds,0,sizeof(int)*256);

	icons = nullptr;

	setTicking(true);
}

FireTile::~FireTile()
{
	delete [] flameOdds;
	delete [] burnOdds;
}

void FireTile::init()
{
	setFlammable(Tile::wood_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::woodSlab_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::woodSlabHalf_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::fence_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::stairs_wood_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::stairs_birchwood_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::stairs_sprucewood_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::stairs_junglewood_Id, FLAME_HARD, BURN_MEDIUM);
	setFlammable(Tile::treeTrunk_Id, FLAME_HARD, BURN_HARD);
	setFlammable(Tile::leaves_Id, FLAME_EASY, BURN_EASY);
	setFlammable(Tile::bookshelf_Id, FLAME_EASY, BURN_MEDIUM);
	setFlammable(Tile::tnt_Id, FLAME_MEDIUM, BURN_INSTANT);
	setFlammable(Tile::tallgrass_Id, FLAME_INSTANT, BURN_INSTANT);
	setFlammable(Tile::wool_Id, FLAME_EASY, BURN_EASY);
	setFlammable(Tile::vine_Id, FLAME_MEDIUM, BURN_INSTANT);
	setFlammable(Tile::coalBlock_Id, FLAME_HARD, BURN_HARD);
	setFlammable(Tile::hayBlock_Id, FLAME_INSTANT, BURN_MEDIUM);
}

void FireTile::setFlammable(int id, int flame, int burn)
{
	flameOdds[id] = flame;
	burnOdds[id] = burn;
}

AABB *FireTile::getAABB(Level *level, int x, int y, int z)
{
	return nullptr;
}

bool FireTile::blocksLight()
{
	return false;
}

bool FireTile::isSolidRender(bool isServerLevel)
{
	return false;
}

bool FireTile::isCubeShaped()
{
	return false;
}

int FireTile::getRenderShape()
{
	return Tile::SHAPE_FIRE;
}

int FireTile::getResourceCount(Random *random)
{
	return 0;
}

int FireTile::getTickDelay(Level *level)
{
	return 30;
}

void FireTile::tick(Level *level, int x, int y, int z, Random *random)
{
	if (!level->getGameRules()->getBoolean(GameRules::RULE_DOFIRETICK))
	{
		return;
	}

	// 4J added - we don't want fire to do anything that might create new fire, or destroy this fire, if we aren't actually tracking (for network) the chunk this is in in the player
	// chunk map. If we did change something in that case, then the change wouldn't get sent to any player that had already received that full chunk, and so we'd just become desynchronised.
	// Seems safest just to do an addToTickNextTick here instead with a decent delay, to make sure that we will get ticked again in the future, when we might again be in a chunk
	// that is being tracked.
	if( !level->isClientSide )		// Note - should only be being ticked on the server
	{
		if( !MinecraftServer::getInstance()->getPlayers()->isTrackingTile(x, y, z, level->dimension->id) )
		{
			level->addToTickNextTick(x, y, z, id, getTickDelay(level) * 5);
			return;
		}
	}


	bool infiniBurn = level->getTile(x, y - 1, z) == Tile::netherRack_Id;
	if (level->dimension->id == 1)		// 4J - was == instanceof TheEndDimension
	{
		if (level->getTile(x, y - 1, z) == Tile::unbreakable_Id) infiniBurn = true;
	}

	if (!mayPlace(level, x, y, z))
	{
		level->removeTile(x, y, z);
	}

	if (!infiniBurn && level->isRaining())
	{
		if (level->isRainingAt(x, y, z) || level->isRainingAt(x - 1, y, z) || level->isRainingAt(x + 1, y, z) || level->isRainingAt(x, y, z - 1) || level->isRainingAt(x, y, z + 1)) {

			level->removeTile(x, y, z);
			return;
		}
	}

	int age = level->getData(x, y, z);
	if (age < 15)
	{
		level->setData(x, y, z, age + random->nextInt(3) / 2, Tile::UPDATE_NONE);
	}
	level->addToTickNextTick(x, y, z, id, getTickDelay(level) + random->nextInt(10));

	if (!infiniBurn && !isValidFireLocation(level, x, y, z))
	{
		if (!level->isTopSolidBlocking(x, y - 1, z) || age > 3) level->removeTile(x, y, z);
		return;
	}

	if (!infiniBurn && !canBurn(level, x, y - 1, z))
	{
		if (age == 15 && random->nextInt(4) == 0)
		{
			level->removeTile(x, y, z);
			return;
		}
	}

	bool isHumid = level->isHumidAt(x, y, z);
	int extra = 0;
	if (isHumid)
	{
		extra = -50;
	}
	checkBurnOut(level, x + 1, y, z, 300 + extra, random, age);
	checkBurnOut(level, x - 1, y, z, 300 + extra, random, age);
	checkBurnOut(level, x, y - 1, z, 250 + extra, random, age);
	checkBurnOut(level, x, y + 1, z, 250 + extra, random, age);
	checkBurnOut(level, x, y, z - 1, 300 + extra, random, age);
	checkBurnOut(level, x, y, z + 1, 300 + extra, random, age);
	if( app.GetGameHostOption(eGameHostOption_FireSpreads) )
	{
		for (int xx = x - 1; xx <= x + 1; xx++)
		{
			for (int zz = z - 1; zz <= z + 1; zz++)
			{
				for (int yy = y - 1; yy <= y + 4; yy++)
				{
					if (xx == x && yy == y && zz == z) continue;

					int rate = 100;
					if (yy > y + 1)
					{
						rate += ((yy - (y + 1)) * 100);
					}

					int fodds = getFireOdds(level, xx, yy, zz);
					if (fodds > 0) {
						int odds = (fodds + 40 + (level->difficulty * 7)) / (age + 30);
						if (isHumid)
						{
							odds /= 2;
						}
						if (odds > 0 && random->nextInt(rate) <= odds)
						{
							if (!(level->isRaining() && level->isRainingAt(xx, yy, zz) || level->isRainingAt(xx - 1, yy, z) || level->isRainingAt(xx + 1, yy, zz) || level->isRainingAt(xx, yy, zz - 1) || level->isRainingAt(xx, yy, zz + 1)))
							{
								int tAge = age + random->nextInt(5) / 4;
								if (tAge > 15) tAge = 15;
								level->setTileAndData(xx, yy, zz, id, tAge, Tile::UPDATE_ALL);
							}
						}
					}
				}
			}
		}
	}
}

bool FireTile::canInstantlyTick()
{
	return false;
}

void FireTile::checkBurnOut(Level *level, int x, int y, int z, int chance, Random *random, int age)
{
	int odds = burnOdds[level->getTile(x, y, z)];
	if (random->nextInt(chance) < odds)
	{
		bool wasTnt = level->getTile(x, y, z) == Tile::tnt_Id;
		if (random->nextInt(age + 10) < 5 && !level->isRainingAt(x, y, z) && app.GetGameHostOption(eGameHostOption_FireSpreads))
		{
			int tAge = age + random->nextInt(5) / 4;
			if (tAge > 15) tAge = 15;
			level->setTileAndData(x, y, z, id, tAge, Tile::UPDATE_ALL);
		} else
		{
			level->removeTile(x, y, z);
		}
		if (wasTnt)
		{
			Tile::tnt->destroy(level, x, y, z, TntTile::EXPLODE_BIT);
		}
	}
}

bool FireTile::isValidFireLocation(Level *level, int x, int y, int z)
{
	if (canBurn(level, x + 1, y, z)) return true;
	if (canBurn(level, x - 1, y, z)) return true;
	if (canBurn(level, x, y - 1, z)) return true;
	if (canBurn(level, x, y + 1, z)) return true;
	if (canBurn(level, x, y, z - 1)) return true;
	if (canBurn(level, x, y, z + 1)) return true;

	return false;
}

int FireTile::getFireOdds(Level *level, int x, int y, int z)
{
	int odds = 0;
	if (!level->isEmptyTile(x, y, z)) return 0;

	odds = getFlammability(level, x + 1, y, z, odds);
	odds = getFlammability(level, x - 1, y, z, odds);
	odds = getFlammability(level, x, y - 1, z, odds);
	odds = getFlammability(level, x, y + 1, z, odds);
	odds = getFlammability(level, x, y, z - 1, odds);
	odds = getFlammability(level, x, y, z + 1, odds);

	return odds;
}

bool FireTile::mayPick()
{
	return false;
}

bool FireTile::canBurn(LevelSource *level, int x, int y, int z)
{
	return flameOdds[level->getTile(x, y, z)] > 0;
}

int FireTile::getFlammability(Level *level, int x, int y, int z, int odds)
{
	int f = flameOdds[level->getTile(x, y, z)];
	if (f > odds) return f;
	return odds;
}

bool FireTile::mayPlace(Level *level, int x, int y, int z)
{
	return level->isTopSolidBlocking(x, y - 1, z) || isValidFireLocation(level, x, y, z);
}

void FireTile::neighborChanged(Level *level, int x, int y, int z, int type)
{
	if (!level->isTopSolidBlocking(x, y - 1, z) && !isValidFireLocation(level, x, y, z))
	{
		level->removeTile(x, y, z);
		return;
	}
}

void FireTile::onPlace(Level *level, int x, int y, int z)
{
	if (level->dimension->id <= 0 && level->getTile(x, y - 1, z) == Tile::obsidian_Id)
	{
		if (Tile::portalTile->trySpawnPortal(level, x, y, z, true))
		{
			return;
		}
	}
	if (!level->isTopSolidBlocking(x, y - 1, z) && !isValidFireLocation(level, x, y, z))
	{
		level->removeTile(x, y, z);
		return;
	}
	level->addToTickNextTick(x, y, z, id, getTickDelay(level) + level->random->nextInt(10));
}

bool FireTile::isFlammable(int tile)
{
	return flameOdds[tile] > 0;
}

void FireTile::animateTick(Level *level, int x, int y, int z, Random *random)
{
	if (random->nextInt(24) == 0)
	{
		level->playLocalSound(x + 0.5f, y + 0.5f, z + 0.5f,eSoundType_FIRE_FIRE, 1 + random->nextFloat(), random->nextFloat() * 0.7f + 0.3f, false);
	}

	if (level->isTopSolidBlocking(x, y - 1, z) || Tile::fire->canBurn(level, x, y - 1, z))
	{
		for (int i = 0; i < 3; i++)
		{
			float xx = x + random->nextFloat();
			float yy = y + random->nextFloat() * 0.5f + 0.5f;
			float zz = z + random->nextFloat();
			level->addParticle(eParticleType_largesmoke, xx, yy, zz, 0, 0, 0);
		}
	}
	else
	{
		if (Tile::fire->canBurn(level, x - 1, y, z))
		{
			for (int i = 0; i < 2; i++) 
			{
				float xx = x + random->nextFloat() * 0.1f;
				float yy = y + random->nextFloat();
				float zz = z + random->nextFloat();
				level->addParticle(eParticleType_largesmoke, xx, yy, zz, 0, 0, 0);
			}
		}
		if (Tile::fire->canBurn(level, x + 1, y, z))
		{
			for (int i = 0; i < 2; i++)
			{
				float xx = x + 1 - random->nextFloat() * 0.1f;
				float yy = y + random->nextFloat();
				float zz = z + random->nextFloat();
				level->addParticle(eParticleType_largesmoke, xx, yy, zz, 0, 0, 0);
			}
		}
		if (Tile::fire->canBurn(level, x, y, z - 1))
		{
			for (int i = 0; i < 2; i++)
			{
				float xx = x + random->nextFloat();
				float yy = y + random->nextFloat();
				float zz = z + random->nextFloat() * 0.1f;
				level->addParticle(eParticleType_largesmoke, xx, yy, zz, 0, 0, 0);
			}
		}
		if (Tile::fire->canBurn(level, x, y, z + 1))
		{
			for (int i = 0; i < 2; i++)
			{
				float xx = x + random->nextFloat();
				float yy = y + random->nextFloat();
				float zz = z + 1 - random->nextFloat() * 0.1f;
				level->addParticle(eParticleType_largesmoke, xx, yy, zz, 0, 0, 0);
			}
		}
		if (Tile::fire->canBurn(level, x, y + 1, z))
		{
			for (int i = 0; i < 2; i++)
			{
				float xx = x + random->nextFloat();
				float yy = y + 1 - random->nextFloat() * 0.1f;
				float zz = z + random->nextFloat();
				level->addParticle(eParticleType_largesmoke, xx, yy, zz, 0, 0, 0);
			}
		}
	}
}

void FireTile::registerIcons(IconRegister *iconRegister)
{
	icons = new Icon*[2];
	icons[0] = iconRegister->registerIcon(TEXTURE_FIRST);
	icons[1] = iconRegister->registerIcon(TEXTURE_SECOND);
}

Icon *FireTile::getTextureLayer(int layer)
{
#ifdef __PSVITA__
	// AP - alpha cut out is expensive on vita. Set the Alpha Cut out flag
	Tesselator* t = Tesselator::getInstance();
	t->setAlphaCutOut( true );
#endif

	return icons[layer];
}

Icon *FireTile::getTexture(int face, int data)
{
	return icons[0];
}