-
-
Notifications
You must be signed in to change notification settings - Fork 637
/
Entity.cpp
2444 lines (1820 loc) · 53.5 KB
/
Entity.cpp
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Entity.h"
#include "Player.h"
#include "../BlockInfo.h"
#include "../World.h"
#include "../Root.h"
#include "../Matrix4.h"
#include "../ClientHandle.h"
#include "../Chunk.h"
#include "../Simulator/FluidSimulator.h"
#include "../Bindings/PluginManager.h"
#include "../LineBlockTracer.h"
#include "../Items/ItemHandler.h"
#include "../FastRandom.h"
#include "../NetherPortalScanner.h"
#include "../BoundingBox.h"
#include "../WorldStorage/NamespaceSerializer.h"
static UInt32 GetNextUniqueID(void)
{
static std::atomic<UInt32> counter(1);
return counter.fetch_add(1);
}
////////////////////////////////////////////////////////////////////////////////
// cEntity:
cEntity::cEntity(eEntityType a_EntityType, Vector3d a_Pos, float a_Width, float a_Height):
m_UniqueID(GetNextUniqueID()),
m_Health(1),
m_MaxHealth(1),
m_AttachedTo(nullptr),
m_Attachee(nullptr),
m_bDirtyHead(false),
m_bDirtyOrientation(false),
m_bHasSentNoSpeed(true),
m_bOnGround(false),
m_Gravity(-9.81f),
m_AirDrag(0.02f),
m_LastSentPosition(a_Pos),
m_LastPosition(a_Pos),
m_EntityType(a_EntityType),
m_World(nullptr),
m_IsFireproof(false),
m_TicksSinceLastBurnDamage(0),
m_TicksSinceLastLavaDamage(0),
m_TicksSinceLastFireDamage(0),
m_TicksLeftBurning(0),
m_TicksSinceLastVoidDamage(0),
m_IsInFire(false),
m_IsInLava(false),
m_IsInWater(false),
m_IsHeadInWater(false),
m_AirLevel(MAX_AIR_LEVEL),
m_AirTickTimer(DROWNING_TICKS),
m_TicksAlive(0),
m_IsTicking(false),
m_ParentChunk(nullptr),
m_HeadYaw(0.0),
m_Rot(0.0, 0.0, 0.0),
m_Position(a_Pos),
m_WaterSpeed(0, 0, 0),
m_Mass (0.001), // Default 1g
m_Width(a_Width),
m_Height(a_Height),
m_InvulnerableTicks(0)
{
m_WorldChangeInfo.m_NewWorld = nullptr;
}
const char * cEntity::GetClass(void) const
{
return "cEntity";
}
const char * cEntity::GetClassStatic(void)
{
return "cEntity";
}
const char * cEntity::GetParentClass(void) const
{
return "";
}
bool cEntity::Initialize(OwnedEntity a_Self, cWorld & a_EntityWorld)
{
if (cPluginManager::Get()->CallHookSpawningEntity(a_EntityWorld, *this))
{
return false;
}
/*
// DEBUG:
FLOGD("Initializing entity #{0} ({1}) at {2:.02f}",
m_UniqueID, GetClass(), m_Pos
);
*/
ASSERT(a_Self->IsPlayer() || (m_World == nullptr)); // Players' worlds are loaded from disk.
ASSERT(GetParentChunk() == nullptr);
SetWorld(&a_EntityWorld);
a_EntityWorld.AddEntity(std::move(a_Self));
return true;
}
void cEntity::OnAcquireSpectator(cPlayer & a_Player)
{
m_Spectators.push_back(&a_Player);
}
void cEntity::OnAddToWorld(cWorld & a_World)
{
// Spawn the entity on the clients:
m_LastSentPosition = GetPosition();
a_World.BroadcastSpawnEntity(*this);
BroadcastLeashedMobs();
}
void cEntity::OnLoseSpectator(cPlayer & a_Player)
{
const auto Spectator = std::find(m_Spectators.begin(), m_Spectators.end(), &a_Player);
ASSERT(Spectator != m_Spectators.end());
std::swap(*Spectator, m_Spectators.back());
m_Spectators.pop_back();
}
void cEntity::OnRemoveFromWorld(cWorld & a_World)
{
// Remove all mobs from the leashed list of mobs:
while (!m_LeashedMobs.empty())
{
m_LeashedMobs.front()->Unleash(false, true);
}
for (const auto Player : m_Spectators)
{
Player->OnLoseSpectated();
}
m_Spectators.clear();
if (m_AttachedTo != nullptr)
{
Detach();
}
if (m_Attachee != nullptr)
{
m_Attachee->Detach();
}
a_World.BroadcastDestroyEntity(*this);
}
void cEntity::WrapHeadYaw(void)
{
m_HeadYaw = NormalizeAngleDegrees(m_HeadYaw);
}
void cEntity::WrapRotation(void)
{
m_Rot.x = NormalizeAngleDegrees(m_Rot.x);
m_Rot.y = NormalizeAngleDegrees(m_Rot.y);
}
void cEntity::WrapSpeed(void)
{
m_Speed.x = Clamp(m_Speed.x, -78.0, 78.0);
m_Speed.y = Clamp(m_Speed.y, -78.0, 78.0);
m_Speed.z = Clamp(m_Speed.z, -78.0, 78.0);
}
void cEntity::SetParentChunk(cChunk * a_Chunk)
{
m_ParentChunk = a_Chunk;
}
void cEntity::Destroy()
{
SetIsTicking(false);
// Unleash leashed mobs
while (!m_LeashedMobs.empty())
{
m_LeashedMobs.front()->Unleash(true, true);
}
auto ParentChunkCoords = cChunkDef::BlockToChunk(GetPosition());
m_World->QueueTask([this, ParentChunkCoords](cWorld & a_World)
{
LOGD("Destroying entity #%i (%s) from chunk (%d, %d)",
this->GetUniqueID(), this->GetClass(),
ParentChunkCoords.m_ChunkX, ParentChunkCoords.m_ChunkZ
);
UNUSED(ParentChunkCoords); // Non Debug mode only
// Make sure that RemoveEntity returned a valid smart pointer
// Also, not storing the returned pointer means automatic destruction
VERIFY(a_World.RemoveEntity(*this));
});
}
void cEntity::TakeDamage(cEntity & a_Attacker)
{
int RawDamage = a_Attacker.GetRawDamageAgainst(*this);
TakeDamage(dtAttack, &a_Attacker, RawDamage, a_Attacker.GetKnockbackAmountAgainst(*this));
}
void cEntity::TakeDamage(eDamageType a_DamageType, cEntity * a_Attacker, int a_RawDamage, double a_KnockbackAmount)
{
float FinalDamage = static_cast<float>(a_RawDamage);
float ArmorCover = GetArmorCoverAgainst(a_Attacker, a_DamageType, a_RawDamage);
ApplyArmorDamage(static_cast<int>(ArmorCover));
cEntity::TakeDamage(a_DamageType, a_Attacker, a_RawDamage, FinalDamage, a_KnockbackAmount);
}
void cEntity::TakeDamage(eDamageType a_DamageType, UInt32 a_AttackerID, int a_RawDamage, double a_KnockbackAmount)
{
m_World->DoWithEntityByID(a_AttackerID, [=](cEntity & a_Attacker)
{
cPawn * Attacker;
if (a_Attacker.IsPawn())
{
Attacker = static_cast<cPawn*>(&a_Attacker);
}
else
{
Attacker = nullptr;
}
TakeDamage(a_DamageType, Attacker, a_RawDamage, a_KnockbackAmount);
return true;
}
);
}
void cEntity::TakeDamage(eDamageType a_DamageType, cEntity * a_Attacker, int a_RawDamage, float a_FinalDamage, double a_KnockbackAmount)
{
TakeDamageInfo TDI;
TDI.DamageType = a_DamageType;
if ((a_Attacker != nullptr) && a_Attacker->IsPawn())
{
TDI.Attacker = a_Attacker;
}
else
{
TDI.Attacker = nullptr;
}
if (a_RawDamage <= 0)
{
a_RawDamage = 0;
}
TDI.RawDamage = a_RawDamage;
if (a_FinalDamage <= 0)
{
a_FinalDamage = 0;
}
TDI.FinalDamage = a_FinalDamage;
Vector3d Heading(0, 0, 0);
if (a_Attacker != nullptr)
{
Heading = a_Attacker->GetLookVector();
}
int KnockbackHeight = 3;
if (IsPlayer())
{
KnockbackHeight = 8;
}
// Apply slight height to knockback
Vector3d FinalKnockback = Vector3d(Heading.x * a_KnockbackAmount, Heading.y + KnockbackHeight, Heading.z * a_KnockbackAmount);
TDI.Knockback = FinalKnockback;
DoTakeDamage(TDI);
}
void cEntity::SetYawFromSpeed(void)
{
const double EPS = 0.0000001;
if ((std::abs(m_Speed.x) < EPS) && (std::abs(m_Speed.z) < EPS))
{
// atan2() may overflow or is undefined, pick any number
SetYaw(0);
return;
}
SetYaw(atan2(m_Speed.x, m_Speed.z) * 180 / M_PI);
}
void cEntity::SetPitchFromSpeed(void)
{
const double EPS = 0.0000001;
double xz = sqrt(m_Speed.x * m_Speed.x + m_Speed.z * m_Speed.z); // Speed XZ-plane component
if ((std::abs(xz) < EPS) && (std::abs(m_Speed.y) < EPS))
{
// atan2() may overflow or is undefined, pick any number
SetPitch(0);
return;
}
SetPitch(atan2(m_Speed.y, xz) * 180 / M_PI);
}
bool cEntity::DoTakeDamage(TakeDamageInfo & a_TDI)
{
if (m_Health <= 0)
{
// Can't take damage if already dead
return false;
}
if (m_InvulnerableTicks > 0)
{
// Entity is invulnerable
return false;
}
if (cRoot::Get()->GetPluginManager()->CallHookTakeDamage(*this, a_TDI))
{
return false;
}
if (IsPainting())
{
KilledBy(a_TDI);
if (a_TDI.Attacker != nullptr)
{
a_TDI.Attacker->Killed(*this, a_TDI.DamageType);
}
return true;
}
if ((a_TDI.Attacker != nullptr) && (a_TDI.Attacker->IsPlayer()))
{
cPlayer * Player = static_cast<cPlayer *>(a_TDI.Attacker);
Player->GetEquippedItem().GetHandler().OnEntityAttack(Player, this);
// Whether an enchantment boosted this attack's damage.
bool MagicalCriticalHit = false;
// IsOnGround() only is false if the player is moving downwards
// Ref: https://minecraft.wiki/w/Damage#Critical_Hits
if (!Player->IsOnGround())
{
if ((a_TDI.DamageType == dtAttack) || (a_TDI.DamageType == dtArrowAttack))
{
a_TDI.FinalDamage *= 1.5f; // 150% damage
m_World->BroadcastEntityAnimation(*this, EntityAnimation::EntityGetsCriticalHit);
}
}
const cEnchantments & Enchantments = Player->GetEquippedItem().m_Enchantments;
int SharpnessLevel = static_cast<int>(Enchantments.GetLevel(cEnchantments::enchSharpness));
int SmiteLevel = static_cast<int>(Enchantments.GetLevel(cEnchantments::enchSmite));
int BaneOfArthropodsLevel = static_cast<int>(Enchantments.GetLevel(cEnchantments::enchBaneOfArthropods));
if (SharpnessLevel > 0)
{
MagicalCriticalHit = true;
a_TDI.FinalDamage += 1.25f * SharpnessLevel;
}
else if (SmiteLevel > 0)
{
if (IsMob())
{
cMonster * Monster = static_cast<cMonster *>(this);
switch (Monster->GetMobType())
{
case mtSkeleton:
case mtWither:
case mtZombie:
case mtZombiePigman:
case mtZombieVillager:
{
MagicalCriticalHit = true;
a_TDI.FinalDamage += 2.5f * SmiteLevel;
break;
}
default: break;
}
}
}
else if (BaneOfArthropodsLevel > 0)
{
if (IsMob())
{
cMonster * Monster = static_cast<cMonster *>(this);
switch (Monster->GetMobType())
{
case mtSpider:
case mtCaveSpider:
case mtSilverfish:
case mtEndermite:
{
MagicalCriticalHit = true;
a_TDI.FinalDamage += 2.5f * BaneOfArthropodsLevel;
// The duration of the effect is a random value between 1 and 1.5 seconds at level I,
// increasing the max duration by 0.5 seconds each level.
// Ref: https://minecraft.wiki/w/Enchanting#Bane_of_Arthropods
int Duration = 20 + GetRandomProvider().RandInt(BaneOfArthropodsLevel * 10); // Duration in ticks.
Monster->AddEntityEffect(cEntityEffect::effSlowness, Duration, 4);
break;
}
default: break;
}
}
}
int FireAspectLevel = static_cast<int>(Enchantments.GetLevel(cEnchantments::enchFireAspect));
if (FireAspectLevel > 0)
{
int BurnTicks = 3;
if (FireAspectLevel > 1)
{
BurnTicks += 4 * (FireAspectLevel - 1);
}
if (!IsMob() && !IsInWater())
{
StartBurning(BurnTicks * 20);
}
else if (IsMob() && !IsInWater())
{
cMonster * Monster = static_cast<cMonster *>(this);
switch (Monster->GetMobType())
{
case mtGhast:
case mtZombiePigman:
case mtMagmaCube:
{
break;
}
default:
{
MagicalCriticalHit = true;
StartBurning(BurnTicks * 20);
}
}
}
}
if (MagicalCriticalHit)
{
m_World->BroadcastEntityAnimation(*this, EntityAnimation::EntityGetsMagicalCriticalHit);
}
unsigned int ThornsLevel = 0;
const cItem ArmorItems[] = { GetEquippedHelmet(), GetEquippedChestplate(), GetEquippedLeggings(), GetEquippedBoots() };
for (size_t i = 0; i < ARRAYCOUNT(ArmorItems); i++)
{
const cItem & Item = ArmorItems[i];
ThornsLevel = std::max(ThornsLevel, Item.m_Enchantments.GetLevel(cEnchantments::enchThorns));
}
if (ThornsLevel > 0)
{
int Chance = static_cast<int>(ThornsLevel * 15);
auto & Random = GetRandomProvider();
if (Random.RandBool(Chance / 100.0))
{
a_TDI.Attacker->TakeDamage(dtAttack, this, 0, Random.RandReal(1.0f, 4.0f), 0);
}
}
Player->GetStatistics().Custom[CustomStatistic::DamageDealt] += FloorC<StatisticsManager::StatValue>(a_TDI.FinalDamage * 10 + 0.5);
}
m_Health -= a_TDI.FinalDamage;
m_Health = std::max(m_Health, 0.0f);
// Add knockback:
if ((IsMob() || IsPlayer()) && (a_TDI.Attacker != nullptr))
{
SetSpeed(a_TDI.Knockback);
}
m_World->BroadcastEntityAnimation(*this, [&a_TDI]
{
switch (a_TDI.DamageType)
{
case eDamageType::dtBurning: return EntityAnimation::PawnBurns;
case eDamageType::dtDrowning: return EntityAnimation::PawnDrowns;
default: return EntityAnimation::PawnHurts;
}
}());
m_InvulnerableTicks = 10;
if (m_Health <= 0)
{
KilledBy(a_TDI);
if (a_TDI.Attacker != nullptr)
{
a_TDI.Attacker->Killed(*this, a_TDI.DamageType);
}
}
return true;
}
int cEntity::GetRawDamageAgainst(const cEntity & a_Receiver)
{
// Returns the hitpoints that this pawn can deal to a_Receiver using its equipped items
// Ref: https://minecraft.wiki/w/Damage#Dealing_damage as of 2012_12_20
switch (this->GetEquippedWeapon().m_ItemType)
{
case E_ITEM_WOODEN_SWORD: return 4;
case E_ITEM_GOLD_SWORD: return 4;
case E_ITEM_STONE_SWORD: return 5;
case E_ITEM_IRON_SWORD: return 6;
case E_ITEM_DIAMOND_SWORD: return 7;
case E_ITEM_WOODEN_AXE: return 3;
case E_ITEM_GOLD_AXE: return 3;
case E_ITEM_STONE_AXE: return 4;
case E_ITEM_IRON_AXE: return 5;
case E_ITEM_DIAMOND_AXE: return 6;
case E_ITEM_WOODEN_PICKAXE: return 2;
case E_ITEM_GOLD_PICKAXE: return 2;
case E_ITEM_STONE_PICKAXE: return 3;
case E_ITEM_IRON_PICKAXE: return 4;
case E_ITEM_DIAMOND_PICKAXE: return 5;
case E_ITEM_WOODEN_SHOVEL: return 1;
case E_ITEM_GOLD_SHOVEL: return 1;
case E_ITEM_STONE_SHOVEL: return 2;
case E_ITEM_IRON_SHOVEL: return 3;
case E_ITEM_DIAMOND_SHOVEL: return 4;
}
// All other equipped items give a damage of 1:
return 1;
}
void cEntity::ApplyArmorDamage(int DamageBlocked)
{
// cEntities don't necessarily have armor to damage.
}
bool cEntity::ArmorCoversAgainst(eDamageType a_DamageType)
{
// Ref.: https://minecraft.wiki/w/Armor#Effects as of 2012_12_20
switch (a_DamageType)
{
case dtOnFire:
case dtSuffocating:
case dtDrowning: // TODO: This one could be a special case - in various MC versions (PC vs XBox) it is and isn't armor-protected
case dtEnderPearl:
case dtStarving:
case dtInVoid:
case dtPoisoning:
case dtWithering:
case dtPotionOfHarming:
case dtFalling:
case dtLightning:
case dtPlugin:
case dtEnvironment:
{
return false;
}
case dtAttack:
case dtArrowAttack:
case dtCactusContact:
case dtMagmaContact:
case dtLavaContact:
case dtFireContact:
case dtExplosion:
{
return true;
}
}
UNREACHABLE("Unsupported damage type");
}
float cEntity::GetEnchantmentCoverAgainst(const cEntity * a_Attacker, eDamageType a_DamageType, int a_Damage)
{
int TotalEPF = 0;
const cItem ArmorItems[] = { GetEquippedHelmet(), GetEquippedChestplate(), GetEquippedLeggings(), GetEquippedBoots() };
for (size_t i = 0; i < ARRAYCOUNT(ArmorItems); i++)
{
const cItem & Item = ArmorItems[i];
if ((a_DamageType != dtInVoid) && (a_DamageType != dtAdmin) && (a_DamageType != dtStarving))
{
TotalEPF += static_cast<int>(Item.m_Enchantments.GetLevel(cEnchantments::enchProtection)) * 1;
}
if ((a_DamageType == dtBurning) || (a_DamageType == dtFireContact) || (a_DamageType == dtLavaContact) || (a_DamageType == dtMagmaContact))
{
TotalEPF += static_cast<int>(Item.m_Enchantments.GetLevel(cEnchantments::enchFireProtection)) * 2;
}
if ((a_DamageType == dtFalling) || (a_DamageType == dtEnderPearl))
{
TotalEPF += static_cast<int>(Item.m_Enchantments.GetLevel(cEnchantments::enchFeatherFalling)) * 3;
}
if (a_DamageType == dtExplosion)
{
TotalEPF += static_cast<int>(Item.m_Enchantments.GetLevel(cEnchantments::enchBlastProtection)) * 2;
}
// Note: Also blocks against fire charges, etc.
if (a_DamageType == dtProjectile)
{
TotalEPF += static_cast<int>(Item.m_Enchantments.GetLevel(cEnchantments::enchProjectileProtection)) * 2;
}
}
int CappedEPF = std::min(20, TotalEPF);
return (a_Damage * CappedEPF / 25.0f);
}
float cEntity::GetEnchantmentBlastKnockbackReduction()
{
UInt32 MaxLevel = 0;
const cItem ArmorItems[] = { GetEquippedHelmet(), GetEquippedChestplate(), GetEquippedLeggings(), GetEquippedBoots() };
for (auto & Item : ArmorItems)
{
UInt32 Level = Item.m_Enchantments.GetLevel(cEnchantments::enchBlastProtection);
if (Level > MaxLevel)
{
// Get max blast protection
MaxLevel = Level;
}
}
// Max blast protect level is 4, each level provide 15% knock back reduction
MaxLevel = std::min<UInt32>(MaxLevel, 4);
return MaxLevel * 0.15f;
}
float cEntity::GetArmorCoverAgainst(const cEntity * a_Attacker, eDamageType a_DamageType, int a_Damage)
{
// Returns the hitpoints out of a_RawDamage that the currently equipped armor would cover
// Filter out damage types that are not protected by armor:
if (!ArmorCoversAgainst(a_DamageType))
{
return 0;
}
// Add up all armor points:
// Ref.: https://minecraft.wiki/w/Armor#Defense_points
int ArmorValue = 0;
int Toughness = 0;
switch (GetEquippedHelmet().m_ItemType)
{
case E_ITEM_LEATHER_CAP: ArmorValue += 1; break;
case E_ITEM_GOLD_HELMET: ArmorValue += 2; break;
case E_ITEM_CHAIN_HELMET: ArmorValue += 2; break;
case E_ITEM_IRON_HELMET: ArmorValue += 2; break;
case E_ITEM_DIAMOND_HELMET: ArmorValue += 3; Toughness += 2; break;
}
switch (GetEquippedChestplate().m_ItemType)
{
case E_ITEM_LEATHER_TUNIC: ArmorValue += 3; break;
case E_ITEM_GOLD_CHESTPLATE: ArmorValue += 5; break;
case E_ITEM_CHAIN_CHESTPLATE: ArmorValue += 5; break;
case E_ITEM_IRON_CHESTPLATE: ArmorValue += 6; break;
case E_ITEM_DIAMOND_CHESTPLATE: ArmorValue += 8; Toughness += 2; break;
}
switch (GetEquippedLeggings().m_ItemType)
{
case E_ITEM_LEATHER_PANTS: ArmorValue += 2; break;
case E_ITEM_GOLD_LEGGINGS: ArmorValue += 3; break;
case E_ITEM_CHAIN_LEGGINGS: ArmorValue += 4; break;
case E_ITEM_IRON_LEGGINGS: ArmorValue += 5; break;
case E_ITEM_DIAMOND_LEGGINGS: ArmorValue += 6; Toughness += 2; break;
}
switch (GetEquippedBoots().m_ItemType)
{
case E_ITEM_LEATHER_BOOTS: ArmorValue += 1; break;
case E_ITEM_GOLD_BOOTS: ArmorValue += 1; break;
case E_ITEM_CHAIN_BOOTS: ArmorValue += 1; break;
case E_ITEM_IRON_BOOTS: ArmorValue += 2; break;
case E_ITEM_DIAMOND_BOOTS: ArmorValue += 3; Toughness += 2; break;
}
// TODO: Special armor cases, such as wool, saddles, dog's collar
// Ref.: https://minecraft.wiki/w/Armor#Mob_armor as of 2012_12_20
float Reduction = std::max(ArmorValue / 5.0f, ArmorValue - a_Damage / (2.0f + Toughness / 4.0f));
return (a_Damage * std::min(20.0f, Reduction) / 25.0f);
}
double cEntity::GetKnockbackAmountAgainst(const cEntity & a_Receiver)
{
// Default knockback for entities
double Knockback = 10;
// If we're sprinting, bump up the knockback
if (IsSprinting())
{
Knockback = 15;
}
// Check for knockback enchantments (punch only applies to shot arrows)
unsigned int KnockbackLevel = GetEquippedWeapon().m_Enchantments.GetLevel(cEnchantments::enchKnockback);
unsigned int KnockbackLevelMultiplier = 8;
Knockback += KnockbackLevelMultiplier * KnockbackLevel;
return Knockback;
}
void cEntity::KilledBy(TakeDamageInfo & a_TDI)
{
m_Health = 0;
cRoot::Get()->GetPluginManager()->CallHookKilling(*this, a_TDI.Attacker, a_TDI);
if (m_Health > 0)
{
// Plugin wants to 'unkill' the pawn. Abort
return;
}
// If the victim is a player the hook is handled by the cPlayer class
if (!IsPlayer())
{
AString emptystring;
cRoot::Get()->GetPluginManager()->CallHookKilled(*this, a_TDI, emptystring);
}
// Drop loot, unless the attacker was a creative mode player:
if (
(a_TDI.Attacker == nullptr) ||
!a_TDI.Attacker->IsPlayer() ||
!static_cast<cPlayer *>(a_TDI.Attacker)->IsGameModeCreative()
)
{
cItems Drops;
GetDrops(Drops, a_TDI.Attacker);
m_World->SpawnItemPickups(Drops, GetPosX(), GetPosY(), GetPosZ());
}
m_TicksAlive = 0;
m_World->BroadcastEntityAnimation(*this, EntityAnimation::PawnDies);
}
void cEntity::Heal(int a_HitPoints)
{
m_Health += a_HitPoints;
m_Health = std::min(m_Health, m_MaxHealth);
}
void cEntity::SetHealth(float a_Health)
{
m_Health = Clamp(a_Health, 0.0f, m_MaxHealth);
}
void cEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
ASSERT(IsTicking());
ASSERT(GetWorld() != nullptr);
m_TicksAlive++;
if (m_InvulnerableTicks > 0)
{
m_InvulnerableTicks--;
}
// Non-players are destroyed as soon as they fall out of the world:
if ((GetPosY() < 0) && (!IsPlayer()))
{
Destroy();
return;
}
if (m_AttachedTo != nullptr)
{
SetPosition(m_AttachedTo->GetPosition());
}
else
{
if (!a_Chunk.IsValid())
{
return;
}
// Position changed -> Super::Tick() called:
GET_AND_VERIFY_CURRENT_CHUNK(NextChunk, POSX_TOINT, POSZ_TOINT);
// Set swim states (water, lava, and fire):
SetSwimState(*NextChunk);
// Handle catching on fire and burning:
TickBurning(*NextChunk);
// Damage players if they are in the void
if (GetPosY() < VOID_BOUNDARY)
{
TickInVoid(*NextChunk);
}
else
{
m_TicksSinceLastVoidDamage = 0;
}
// Handle cactus damage or destruction:
if (
IsMob() || IsPickup() ||
(IsPlayer() && !((static_cast<cPlayer *>(this))->IsGameModeCreative() || (static_cast<cPlayer *>(this))->IsGameModeSpectator()))
)
{
DetectCacti();
}
// Handle magma block damage
if
(
IsOnGround() &&
(
(IsMob() && !static_cast<cPawn *>(this)->IsFireproof()) ||
(
IsPlayer() && !((static_cast<cPlayer *>(this))->IsGameModeCreative() || (static_cast<cPlayer *>(this))->IsGameModeSpectator())
&& !static_cast<cPlayer *>(this)->IsFireproof()
&& !static_cast<cPlayer *>(this)->HasEntityEffect(cEntityEffect::effFireResistance)
)
)
)
{
DetectMagma();
}
// Handle drowning:
if (IsMob() || IsPlayer())
{
HandleAir();
}
if (!DetectPortal()) // Our chunk is invalid if we have moved to another world
{
// None of the above functions changed position, we remain in the chunk of NextChunk
HandlePhysics(a_Dt, *NextChunk);
}
}
}