aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/Explosion.cpp
blob: 65e719e40d0802ff64388126118e2a40cd5966a9 (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
#include "stdafx.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.entity.item.h"
#include "net.minecraft.world.item.enchantment.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.world.damagesource.h"
#include "TilePos.h"
#include "Explosion.h"
#include "SoundTypes.h"

Explosion::Explosion(Level *level, shared_ptr<Entity> source, double x, double y, double z, float r)
{
	fire = false;
	random = new Random();

	this->level = level;
	this->source = source;
	this->r = r;
	this->x = x;
	this->y = y;
	this->z = z;

	destroyBlocks = true;
	size = 16;
}

Explosion::~Explosion()
{
	delete random;
	for( auto& it : hitPlayers )
	{
		delete it.second;
	}
}

void Explosion::explode()
{
	float oR = r;

	int size = 16;
	for (int xx = 0; xx < size; xx++)
	{
		for (int yy = 0; yy < size; yy++)
		{
			for (int zz = 0; zz < size; zz++)
			{
				if ((xx != 0 && xx != size - 1) && (yy != 0 && yy != size - 1) && (zz != 0 && zz != size - 1)) continue;

				double xd = xx / (size - 1.0f) * 2 - 1;
				double yd = yy / (size - 1.0f) * 2 - 1;
				double zd = zz / (size - 1.0f) * 2 - 1;
				double d = sqrt(xd * xd + yd * yd + zd * zd);

				xd /= d;
				yd /= d;
				zd /= d;

				float remainingPower = r * (0.7f + level->random->nextFloat() * 0.6f);
				double xp = x;
				double yp = y;
				double zp = z;

				float stepSize = 0.3f;
				while (remainingPower > 0)
				{
					int xt = Mth::floor(xp);
					int yt = Mth::floor(yp);
					int zt = Mth::floor(zp);
					int t = level->getTile(xt, yt, zt);
					if (t > 0)
					{
						Tile *tile = Tile::tiles[t];
						float resistance = source != nullptr ? source->getTileExplosionResistance(this, level, xt, yt, zt, tile) : tile->getExplosionResistance(source);
						remainingPower -= (resistance + 0.3f) * stepSize;
					}
					if (remainingPower > 0&& (source == nullptr || source->shouldTileExplode(this, level, xt, yt, zt, t, remainingPower)))
					{
						toBlow.insert(TilePos(xt, yt, zt));
					}

					xp += xd * stepSize;
					yp += yd * stepSize;
					zp += zd * stepSize;
					remainingPower -= stepSize * 0.75f;
				}
				// if (xd*xd+yd*yd+zd*zd>1) continue;
			}
		}
	}

	r *= 2.0f;
	int x0 = Mth::floor(x - r - 1);
	int x1 = Mth::floor(x + r + 1);
	int y0 = Mth::floor(y - r - 1);
	int y1 = Mth::floor(y + r + 1);
	int z0 = Mth::floor(z - r - 1);
	int z1 = Mth::floor(z + r + 1);

	// Fix for 360 #123866 - [CRASH] TU13: Code: Compliance: Placing the TNT next to Ender Crystals will crash the title after a certain amount of time.
	// If we explode something next to an EnderCrystal then it creates a new explosion that overwrites the shared vector in the level
	// So copy it here instead of directly using the shared one
	vector<shared_ptr<Entity> > *levelEntities = level->getEntities(source, AABB::newTemp(x0, y0, z0, x1, y1, z1));
	vector<shared_ptr<Entity> > entities(levelEntities->begin(), levelEntities->end() );
	Vec3 *center = Vec3::newTemp(x, y, z);

	for ( auto& e : entities )
	{
		// 4J Stu - If the entity is not in a block that would be blown up, then they should not be damaged
		// Fix for #46606 - TU5: Content: Gameplay: The player can be damaged and killed by explosions behind obsidian walls
		bool canDamage = false;
		for ( auto& it2 : toBlow )
		{
			if(e->bb->intersects(it2.x,it2.y,it2.z,it2.x + 1,it2.y + 1,it2.z + 1))
			{
				canDamage = true;
				break;
			}
		}

		double dist = e->distanceTo(x, y, z) / r;
		if (dist <= 1)
		{
			double xa = e->x - x;
			double ya = e->y + e->getHeadHeight() - y;
			double za = e->z - z;

			double da = sqrt(xa * xa + ya * ya + za * za);

			// 4J Stu - Added this check to remove divide by zero errors (or rather the issues caused by
			// the values being set to NaN and used in comparisons a bit later on e.g. fireball)
			if( da == 0 )
			{
				xa = ya = za = 0.0;
			}
			else
			{
				xa /= da;
				ya /= da;
				za /= da;
			}

			double sp = level->getSeenPercent(center, e->bb);
			double pow = (1 - dist) * sp;
			if(canDamage) e->hurt(DamageSource::explosion(this), static_cast<int>((pow * pow + pow) / 2 * 8 * r + 1));

			double kbPower = ProtectionEnchantment::getExplosionKnockbackAfterDampener(e, pow);
			e->xd += xa *kbPower;
			e->yd += ya *kbPower;
			e->zd += za *kbPower;					

			
			if (e->instanceof(eTYPE_PLAYER))
			{
				shared_ptr<Player> player = dynamic_pointer_cast<Player>(e);
				//app.DebugPrintf("Adding player knockback (%f,%f,%f)\n", xa * pow, ya * pow, za * pow);
				hitPlayers.insert( playerVec3Map::value_type( player, Vec3::newPermanent(xa * pow, ya * pow, za * pow)));
			}
		}
	}
	r = oR;
}


void Explosion::finalizeExplosion(bool generateParticles, vector<TilePos> *toBlowDirect/*=nullptr*/)		// 4J - added toBlowDirect parameter
{
	level->playSound(x, y, z, eSoundType_RANDOM_EXPLODE, 4, (1 + (level->random->nextFloat() - level->random->nextFloat()) * 0.2f) * 0.7f);
	if (r < 2 || !destroyBlocks)
	{
		level->addParticle(eParticleType_largeexplode, x, y, z, 1.0f, 0, 0);
	}
	else
	{
		level->addParticle(eParticleType_hugeexplosion, x, y, z, 1.0f, 0, 0);
	}
	
	// 4J - use pointer to vector directly passed in if this is available - used to speed up calling this from an incoming packet
	vector<TilePos> *toBlowArray = toBlowDirect ? toBlowDirect : new vector<TilePos>( toBlow.begin(), toBlow.end() );
	if (destroyBlocks)
	{
		//toBlowArray.addAll(toBlow);
		// TODO 4J Stu - Reverse iterator
		PIXBeginNamedEvent(0,"Finalizing explosion size %d",toBlow.size());
		app.DebugPrintf("Finalizing explosion size %d\n",toBlow.size());
		static const int MAX_EXPLODE_PARTICLES = 50;
		// 4J - try and make at most MAX_EXPLODE_PARTICLES pairs of particles
		int fraction = static_cast<int>(toBlowArray->size()) / MAX_EXPLODE_PARTICLES;
		if( fraction == 0 ) fraction = 1;
		size_t j = toBlowArray->size() - 1;
		//for (size_t j = toBlowArray->size() - 1; j >= 0; j--)
		for (auto it = toBlowArray->rbegin(); it != toBlowArray->rend(); ++it)
		{
			TilePos *tp = &(*it); //&toBlowArray->at(j);
			int xt = tp->x;
			int yt = tp->y;
			int zt = tp->z;
			// if (xt >= 0 && yt >= 0 && zt >= 0 && xt < width && yt < depth &&
			// zt < height) {
			int t = level->getTile(xt, yt, zt);

			if (generateParticles)
			{
				if( ( j % fraction ) == 0  )
				{
					double xa = xt + level->random->nextFloat();
					double ya = yt + level->random->nextFloat();
					double za = zt + level->random->nextFloat();

					double xd = xa - x;
					double yd = ya - y;
					double zd = za - z;

					double dd = sqrt(xd * xd + yd * yd + zd * zd);

					xd /= dd;
					yd /= dd;
					zd /= dd;

					double speed = 0.5 / (dd / r + 0.1);
					speed *= (level->random->nextFloat() * level->random->nextFloat() + 0.3f);
					xd *= speed;
					yd *= speed;
					zd *= speed;

					level->addParticle(eParticleType_explode, (xa + x * 1) / 2, (ya + y * 1) / 2, (za + z * 1) / 2, xd, yd, zd);
					level->addParticle(eParticleType_smoke, xa, ya, za, xd, yd, zd);
				}
			}

			if (t > 0)
			{
				Tile *tile = Tile::tiles[t];

				if (tile->dropFromExplosion(this))
				{
					tile->spawnResources(level, xt, yt, zt, level->getData(xt, yt, zt), 1.0f / r, 0);
				}
				level->setTileAndData(xt, yt, zt, 0, 0, Tile::UPDATE_ALL);
				tile->wasExploded(level, xt, yt, zt, this);
			}

			--j;
		}
	}

	if (fire)
	{
		for (auto it = toBlowArray->rbegin(); it != toBlowArray->rend(); ++it)
		{
			TilePos *tp = &(*it); //&toBlowArray->at(j);
			int xt = tp->x;
			int yt = tp->y;
			int zt = tp->z;
			int t = level->getTile(xt, yt, zt);
			int b = level->getTile(xt, yt - 1, zt);
			if (t == 0 && Tile::solid[b] && random->nextInt(3) == 0)
			{
				level->setTileAndUpdate(xt, yt, zt, Tile::fire_Id);
			}
		}
	}

	PIXEndNamedEvent();
	if( toBlowDirect == nullptr )	delete toBlowArray;
}

Explosion::playerVec3Map *Explosion::getHitPlayers()
{
	return &hitPlayers;
}

Vec3 *Explosion::getHitPlayerKnockback( shared_ptr<Player> player )
{
	auto it = hitPlayers.find(player);

	if(it == hitPlayers.end() ) return Vec3::newTemp(0.0,0.0,0.0);

	return it->second;
}

shared_ptr<LivingEntity> Explosion::getSourceMob()
{
	if (source == nullptr) return nullptr;
	if (source->instanceof(eTYPE_PRIMEDTNT)) return dynamic_pointer_cast<PrimedTnt>(source)->getOwner();
	if (source->instanceof(eTYPE_LIVINGENTITY)) return dynamic_pointer_cast<LivingEntity>(source);
	return nullptr;
}