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
|
#include "stdafx.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.phys.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.damagesource.h"
#include "com.mojang.nbt.h"
#include "Fireball.h"
#include "net.minecraft.world.level.dimension.h"
#include "SharedConstants.h"
// 4J - added common ctor code.
void Fireball::_init()
{
xTile = -1;
yTile = -1;
zTile = -1;
lastTile = 0;
inGround = false;
flightTime = 0;
life = 0;
owner = nullptr;
xPower = 0.0;
yPower = 0.0;
zPower = 0.0;
}
Fireball::Fireball(Level *level) : Entity( level )
{
// 4J Stu - This function call had to be moved here from the Entity ctor to ensure that
// the derived version of the function is called
this->defineSynchedData();
_init();
setSize(16 / 16.0f, 16 / 16.0f);
}
void Fireball::defineSynchedData()
{
}
bool Fireball::shouldRenderAtSqrDistance(double distance)
{
double size = bb->getSize() * 4;
size *= 64.0f;
return distance < size * size;
}
Fireball::Fireball(Level *level, double x, double y, double z, double xa, double ya, double za) : Entity( level )
{
// 4J Stu - This function call had to be moved here from the Entity ctor to ensure that
// the derived version of the function is called
this->defineSynchedData();
_init();
setSize(16 / 16.0f, 16 / 16.0f);
moveTo(x, y, z, yRot, xRot);
setPos(x, y, z);
double dd = sqrt(xa * xa + ya * ya + za * za);
// Fix for #69150 - [CRASH] TU8: Code: Gameplay: Nether portal mechanics can become permanently broken, causing a hard lock upon usage.
// IF xa, ya and za are 0 then dd is 0 and the xa/dd etc return NAN
if(dd == 0.0)
{
xPower = 0.0;
yPower = 0.0;
zPower = 0.0;
}
else
{
xPower = xa / dd * 0.10;
yPower = ya / dd * 0.10;
zPower = za / dd * 0.10;
}
}
Fireball::Fireball(Level *level, shared_ptr<LivingEntity> mob, double xa, double ya, double za) : Entity ( level )
{
// 4J Stu - This function call had to be moved here from the Entity ctor to ensure that
// the derived version of the function is called
this->defineSynchedData();
_init();
owner = mob;
setSize(16 / 16.0f, 16 / 16.0f);
moveTo(mob->x, mob->y, mob->z, mob->yRot, mob->xRot);
setPos(x, y, z);
heightOffset = 0;
xd = yd = zd = 0.0;
xa += random->nextGaussian() * 0.4;
ya += random->nextGaussian() * 0.4;
za += random->nextGaussian() * 0.4;
double dd = sqrt(xa * xa + ya * ya + za * za);
// Fix for #69150 - [CRASH] TU8: Code: Gameplay: Nether portal mechanics can become permanently broken, causing a hard lock upon usage.
// IF xa, ya and za are 0 then dd is 0 and the xa/dd etc return NAN
if(dd == 0.0)
{
xPower = 0.0;
yPower = 0.0;
zPower = 0.0;
}
else
{
xPower = xa / dd * 0.10;
yPower = ya / dd * 0.10;
zPower = za / dd * 0.10;
}
}
void Fireball::tick()
{
// 4J-PB - Moved forward from 1.2.3
//if (!level->isClientSide && (owner == nullptr || owner->removed))
if (!level->isClientSide)
{
if((owner != nullptr && owner->removed) || !level->hasChunkAt(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z)))
{
app.DebugPrintf("Fireball removed - owner is null or removed is true for owner\n");
remove();
return;
}
else
{
// 4J-PB - TU9 bug fix - fireballs can hit the edge of the world, and stay there
int minXZ = - (level->dimension->getXZSize() * 16 ) / 2;
int maxXZ = (level->dimension->getXZSize() * 16 ) / 2 - 1;
if ((x<=minXZ) || (x>=maxXZ) || (z<=minXZ) || (z>=maxXZ))
{
remove();
app.DebugPrintf("Fireball removed - end of world\n");
return;
}
}
}
Entity::tick();
//app.DebugPrintf("Fireball x %d, y %d, z%d\n",(int)x,(int)y,(int)z);
if(shouldBurn()) setOnFire(1);
if (inGround)
{
int tile = level->getTile(xTile, yTile, zTile);
if (tile == lastTile)
{
life++;
if (life == SharedConstants::TICKS_PER_SECOND * 30)
{
remove();
app.DebugPrintf("Fireball removed - life is 20*60\n");
}
return;
}
else
{
inGround = false;
xd *= random->nextFloat() * 0.2f;
yd *= random->nextFloat() * 0.2f;
zd *= random->nextFloat() * 0.2f;
life = 0;
flightTime = 0;
}
}
else
{
flightTime++;
}
MemSect(41);
Vec3 *from = Vec3::newTemp(x, y, z);
Vec3 *to = Vec3::newTemp(x + xd, y + yd, z + zd);
HitResult *res = level->clip(from, to);
from = Vec3::newTemp(x, y, z);
to = Vec3::newTemp(x + xd, y + yd, z + zd);
if (res != nullptr)
{
to = Vec3::newTemp(res->pos->x, res->pos->y, res->pos->z);
}
shared_ptr<Entity> hitEntity = nullptr;
vector<shared_ptr<Entity> > *objects = level->getEntities(shared_from_this(), bb->expand(xd, yd, zd)->grow(1, 1, 1));
double nearest = 0;
for ( auto& e : *objects )
{
if ( e == nullptr || !e->isPickable() || (e->is(owner) )) continue; //4J Stu - Never collide with the owner (Enderdragon) // && flightTime < 25)) continue;
float rr = 0.3f;
AABB *bb = e->bb->grow(rr, rr, rr);
HitResult *p = bb->clip(from, to);
if (p != nullptr)
{
double dd = from->distanceTo(p->pos);
if (dd < nearest || nearest == 0)
{
hitEntity = e;
nearest = dd;
}
delete p;
}
}
if (hitEntity != nullptr)
{
delete res;
res = new HitResult(hitEntity);
}
MemSect(0);
if (res != nullptr)
{
onHit(res);
}
delete res;
x += xd;
y += yd;
z += zd;
double sd = sqrt(xd * xd + zd * zd);
yRot = static_cast<float>(atan2(zd, xd) * 180 / PI) + 90;
xRot = static_cast<float>(atan2(sd, yd) * 180 / PI) - 90;
while (xRot - xRotO < -180)
xRotO -= 360;
while (xRot - xRotO >= 180)
xRotO += 360;
while (yRot - yRotO < -180)
yRotO -= 360;
while (yRot - yRotO >= 180)
yRotO += 360;
xRot = xRotO + (xRot - xRotO) * 0.2f;
yRot = yRotO + (yRot - yRotO) * 0.2f;
float inertia = getInertia();
if (isInWater())
{
for (int i = 0; i < 4; i++)
{
float s = 1 / 4.0f;
level->addParticle(eParticleType_bubble, x - xd * s, y - yd * s, z - zd * s, xd, yd, zd);
}
inertia = 0.80f;
}
xd += xPower;
yd += yPower;
zd += zPower;
xd *= inertia;
yd *= inertia;
zd *= inertia;
// 4J-PB - bug fix for the fireballs in a saved game - they are saved with no/very small velocity, so end up hanging around in the air
if (!level->isClientSide)
{
if((abs(xd)<0.002) && (abs(yd)<0.002) && (abs(zd)<0.002))
{
xd=0.0;
zd=0.0;
yd=0.0;
app.DebugPrintf("Removing a fireball with zero velocity\n");
remove();
}
}
level->addParticle(getTrailParticleType(), x, y + 0.5f, z, 0, 0.01, 0);
setPos(x, y, z);
}
float Fireball::getInertia()
{
return 0.95f;
}
void Fireball::addAdditonalSaveData(CompoundTag *tag)
{
tag->putShort(L"xTile", static_cast<short>(xTile));
tag->putShort(L"yTile", static_cast<short>(yTile));
tag->putShort(L"zTile", static_cast<short>(zTile));
tag->putByte(L"inTile", static_cast<byte>(lastTile));
tag->putByte(L"inGround", static_cast<byte>(inGround ? 1 : 0));
tag->put(L"direction", newDoubleList(3, xd, yd, zd));
}
void Fireball::readAdditionalSaveData(CompoundTag *tag)
{
xTile = tag->getShort(L"xTile");
yTile = tag->getShort(L"yTile");
zTile = tag->getShort(L"zTile");
lastTile = tag->getByte(L"inTile") & 0xff;
inGround = tag->getByte(L"inGround") == 1;
// Load the stored direction and apply it to the fireball
// if it has no stored direction, remove it.
if (tag->contains(L"direction"))
{
ListTag<DoubleTag> *listTag = (ListTag<DoubleTag> *)tag->getList(L"direction");
xd = ((DoubleTag *) listTag->get(0))->data;
yd = ((DoubleTag *) listTag->get(1))->data;
zd = ((DoubleTag *) listTag->get(2))->data;
}
else
{
remove();
}
}
bool Fireball::isPickable()
{
return true;
}
float Fireball::getPickRadius()
{
return 1;
}
bool Fireball::hurt(DamageSource *source, float damage)
{
if (isInvulnerable()) return false;
markHurt();
if (source->getEntity() != nullptr)
{
Vec3 *lookAngle = source->getEntity()->getLookAngle();
if (lookAngle != nullptr)
{
xd = lookAngle->x;
yd = lookAngle->y;
zd = lookAngle->z;
xPower = xd * 0.1;
yPower = yd * 0.1;
zPower = zd * 0.1;
}
if ( source->getEntity()->instanceof(eTYPE_LIVINGENTITY) )
{
owner = dynamic_pointer_cast<LivingEntity>( source->getEntity() );
}
return true;
}
return false;
}
float Fireball::getShadowHeightOffs()
{
return 0;
}
float Fireball::getBrightness(float a)
{
return 1.0f;
}
int Fireball::getLightColor(float a)
{
return 15 << 20 | 15 << 4;
}
ePARTICLE_TYPE Fireball::getTrailParticleType()
{
return eParticleType_smoke;
}
bool Fireball::shouldBurn()
{
return true;
}
|