Cuberite
A lightweight, fast and extensible game server for Minecraft
Monster.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "IncludeAllMonsters.h"
5 #include "../Root.h"
6 #include "../Server.h"
7 #include "../ClientHandle.h"
8 #include "../Items/ItemHandler.h"
9 #include "../World.h"
10 #include "../EffectID.h"
11 #include "../Entities/Player.h"
12 #include "../Entities/ExpOrb.h"
13 #include "../MonsterConfig.h"
14 #include "../BoundingBox.h"
15 
16 #include "../Chunk.h"
17 #include "../FastRandom.h"
18 
19 #include "PathFinder.h"
20 #include "../Entities/LeashKnot.h"
21 
22 
23 
24 
25 
31 static const struct
32 {
34  const char * m_lcName;
35  const char * m_VanillaName;
36  const char * m_VanillaNameNBT;
37 } g_MobTypeNames[] =
38 {
39  {mtBat, "bat", "Bat", "bat"},
40  {mtBlaze, "blaze", "Blaze", "blaze"},
41  {mtCaveSpider, "cavespider", "CaveSpider", "cave_spider"},
42  {mtChicken, "chicken", "Chicken", "chicken"},
43  {mtCow, "cow", "Cow", "cow"},
44  {mtCreeper, "creeper", "Creeper", "creeper"},
45  {mtEnderman, "enderman", "Enderman", "enderman"},
46  {mtEnderDragon, "enderdragon", "EnderDragon", "ender_dragon"},
47  {mtGhast, "ghast", "Ghast", "ghast"},
48  {mtGiant, "giant", "Giant", "giant"},
49  {mtGuardian, "guardian", "Guardian", "guardian"},
50  {mtHorse, "horse", "EntityHorse", "horse"},
51  {mtIronGolem, "irongolem", "VillagerGolem", "iron_golem"},
52  {mtMagmaCube, "magmacube", "LavaSlime", "magma_cube"},
53  {mtMooshroom, "mooshroom", "MushroomCow", "mooshroom"},
54  {mtOcelot, "ocelot", "Ozelot", "ocelot"},
55  {mtPig, "pig", "Pig", "pig"},
56  {mtRabbit, "rabbit", "Rabbit", "rabbit"},
57  {mtSheep, "sheep", "Sheep", "sheep"},
58  {mtSilverfish, "silverfish", "Silverfish", "silverfish"},
59  {mtSkeleton, "skeleton", "Skeleton", "skeleton"},
60  {mtSlime, "slime", "Slime", "slime"},
61  {mtSnowGolem, "snowgolem", "SnowMan", "snow_golem"},
62  {mtSpider, "spider", "Spider", "spider"},
63  {mtSquid, "squid", "Squid", "squid"},
64  {mtVillager, "villager", "Villager", "villager"},
65  {mtWitch, "witch", "Witch", "witch"},
66  {mtWither, "wither", "WitherBoss", "wither"},
67  {mtWolf, "wolf", "Wolf", "wolf"},
68  {mtZombie, "zombie", "Zombie", "zombie"},
69  {mtZombiePigman, "zombiepigman", "PigZombie", "zombie_pigman"},
70 } ;
71 
72 
73 
74 
75 
77 // cMonster:
78 
79 cMonster::cMonster(const AString & a_ConfigName, eMonsterType a_MobType, const AString & a_SoundHurt, const AString & a_SoundDeath, double a_Width, double a_Height)
80  : super(etMonster, a_Width, a_Height)
81  , m_EMState(IDLE)
82  , m_EMPersonality(AGGRESSIVE)
83  , m_PathFinder(a_Width, a_Height)
84  , m_PathfinderActivated(false)
85  , m_JumpCoolDown(0)
86  , m_IdleInterval(0)
87  , m_DestroyTimer(0)
88  , m_MobType(a_MobType)
89  , m_CustomName("")
90  , m_CustomNameAlwaysVisible(false)
91  , m_SoundHurt(a_SoundHurt)
92  , m_SoundDeath(a_SoundDeath)
93  , m_AttackRate(3)
94  , m_AttackDamage(1)
95  , m_AttackRange(1)
96  , m_AttackCoolDownTicksLeft(0)
97  , m_SightDistance(25)
98  , m_DropChanceWeapon(0.085f)
99  , m_DropChanceHelmet(0.085f)
100  , m_DropChanceChestplate(0.085f)
101  , m_DropChanceLeggings(0.085f)
102  , m_DropChanceBoots(0.085f)
103  , m_CanPickUpLoot(true)
104  , m_TicksSinceLastDamaged(100)
105  , m_BurnsInDaylight(false)
106  , m_RelativeWalkSpeed(1)
107  , m_Age(1)
108  , m_AgingTimer(20 * 60 * 20) // about 20 minutes
109  , m_WasLastTargetAPlayer(false)
110  , m_LeashedTo(nullptr)
111  , m_LeashToPos(nullptr)
112  , m_IsLeashActionJustDone(false)
113  , m_CanBeLeashed(GetMobFamily() == eFamily::mfPassive)
114  , m_Target(nullptr)
115 {
116  if (!a_ConfigName.empty())
117  {
118  GetMonsterConfig(a_ConfigName);
119  }
120 }
121 
122 
123 
124 
125 
127 {
128  ASSERT(GetTarget() == nullptr);
129 }
130 
131 
132 
133 
134 
135 void cMonster::Destroy(bool a_ShouldBroadcast)
136 {
137  if (IsLeashed())
138  {
139  cEntity * LeashedTo = GetLeashedTo();
140  Unleash(false, a_ShouldBroadcast);
141 
142  // Remove leash knot if there are no more mobs leashed to
143  if (!LeashedTo->HasAnyMobLeashed() && LeashedTo->IsLeashKnot())
144  {
145  LeashedTo->Destroy();
146  }
147  }
148 
149  super::Destroy(a_ShouldBroadcast);
150 }
151 
152 
153 
154 
155 
157 {
158  SetTarget(nullptr); // Tell them we're no longer targeting them.
160 }
161 
162 
163 
164 
165 
167 {
168  a_Client.SendSpawnMob(*this);
169 
170  if (IsLeashed())
171  {
172  a_Client.SendLeashEntity(*this, *this->GetLeashedTo());
173  }
174 }
175 
176 
177 
178 
179 
181 {
183  {
184  return;
185  }
186 
187  if (m_JumpCoolDown <= 0)
188  {
190  {
191  if (
192  (IsOnGround() && (GetSpeed().SqrLength() <= 0.5)) || // If walking on the ground, we need to slow down first, otherwise we miss the jump
193  IsInWater()
194  )
195  {
196  m_bOnGround = false;
197  m_JumpCoolDown = 20;
198  AddPosY(1.6); // Jump!!
199  SetSpeedY(1);
200  SetSpeedX(3.2 * (m_NextWayPointPosition.x - GetPosition().x)); // Move forward in a preset speed.
201  SetSpeedZ(3.2 * (m_NextWayPointPosition.z - GetPosition().z)); // The numbers were picked based on trial and error
202  }
203  }
204  }
205  else
206  {
207  --m_JumpCoolDown;
208  }
209 
211  if ((std::abs(Distance.x) > 0.05) || (std::abs(Distance.z) > 0.05))
212  {
213  Distance.y = 0;
214  Distance.Normalize();
215 
216  if (m_bOnGround)
217  {
218  Distance *= 2.5f;
219  }
220  else if (IsInWater())
221  {
222  Distance *= 1.3f;
223  }
224  else
225  {
226  // Don't let the mob move too much if he's falling.
227  Distance *= 0.25f;
228  }
229  // Apply walk speed:
230  Distance *= m_RelativeWalkSpeed;
231  /* Reduced default speed.
232  Close to Vanilla, easier for mobs to follow m_NextWayPointPositions, hence
233  better pathfinding. */
234  Distance *= 0.5;
235  AddSpeedX(Distance.x);
236  AddSpeedZ(Distance.z);
237  }
238 }
239 
240 
241 
242 
243 
244 void cMonster::MoveToPosition(const Vector3d & a_Position)
245 {
246  m_FinalDestination = a_Position;
247  m_PathfinderActivated = true;
248 }
249 
250 
251 
252 
253 
255 {
256  m_PathfinderActivated = false;
257 }
258 
259 
260 
261 
262 
263 void cMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
264 {
265  super::Tick(a_Dt, a_Chunk);
266  if (!IsTicking())
267  {
268  // The base class tick destroyed us
269  return;
270  }
272 
273  ASSERT((GetTarget() == nullptr) || (GetTarget()->IsPawn() && (GetTarget()->GetWorld() == GetWorld())));
275  {
277  }
278 
279  if (m_Health <= 0)
280  {
281  // The mob is dead, but we're still animating the "puff" they leave when they die
282  m_DestroyTimer += a_Dt;
283  if (m_DestroyTimer > std::chrono::seconds(1))
284  {
285  Destroy(true);
286  }
287  return;
288  }
289 
290  if (m_TicksSinceLastDamaged < 100)
291  {
293  }
294  if ((GetTarget() != nullptr))
295  {
296  ASSERT(GetTarget()->IsTicking());
297 
298  if (GetTarget()->IsPlayer())
299  {
300  if (!static_cast<cPlayer *>(GetTarget())->CanMobsTarget())
301  {
302  SetTarget(nullptr);
303  m_EMState = IDLE;
304  }
305  }
306  }
307 
308  // Process the undead burning in daylight.
309  HandleDaylightBurning(*Chunk, WouldBurnAt(GetPosition(), *Chunk));
310 
311  bool a_IsFollowingPath = false;
313  {
314  if (ReachedFinalDestination() || (m_LeashToPos != nullptr))
315  {
316  StopMovingToPosition(); // Simply sets m_PathfinderActivated to false.
317  }
318  else
319  {
320  // Note that m_NextWayPointPosition is actually returned by GetNextWayPoint)
322  {
324  {
325  /* If I burn in daylight, and I won't burn where I'm standing, and I'll burn in my next position, and at least one of those is true:
326  1. I am idle
327  2. I was not hurt by a player recently.
328  Then STOP. */
329  if (
332  !WouldBurnAt(GetPosition(), *Chunk)
333  )
334  {
335  // If we burn in daylight, and we would burn at the next step, and we won't burn where we are right now, and we weren't provoked recently:
337  }
338  else
339  {
340  a_IsFollowingPath = true; // Used for proper body / head orientation only.
341  MoveToWayPoint(*Chunk);
342  }
343  break;
344  }
346  {
348  break;
349  }
350  default:
351  {
352 
353  }
354  }
355  }
356  }
357 
358  SetPitchAndYawFromDestination(a_IsFollowingPath);
359 
360  switch (m_EMState)
361  {
362  case IDLE:
363  {
364  // If enemy passive we ignore checks for player visibility.
365  InStateIdle(a_Dt, a_Chunk);
366  break;
367  }
368  case CHASING:
369  {
370  // If we do not see a player anymore skip chasing action.
371  InStateChasing(a_Dt, a_Chunk);
372  break;
373  }
374  case ESCAPING:
375  {
376  InStateEscaping(a_Dt, a_Chunk);
377  break;
378  }
379  case ATTACKING: break;
380  } // switch (m_EMState)
381 
382  // Leash calculations
383  CalcLeashActions(a_Dt);
384 
386 
387  if (m_AgingTimer > 0)
388  {
389  m_AgingTimer--;
390  if ((m_AgingTimer <= 0) && IsBaby())
391  {
392  SetAge(1);
394  }
395  }
396 }
397 
398 
399 
400 
401 
402 void cMonster::CalcLeashActions(std::chrono::milliseconds a_Dt)
403 {
404  // This mob just spotted in the world and [m_LeashToPos not null] shows that should be leashed to a leash knot at m_LeashToPos.
405  // This keeps trying until knot is found. Leash knot may be in a different chunk that needn't or can't be loaded yet.
406  if (!IsLeashed() && (m_LeashToPos != nullptr))
407  {
409  if (LeashKnot != nullptr)
410  {
411  LeashTo(*LeashKnot);
412  SetLeashToPos(nullptr);
413  }
414  }
415 
416  if (!IsLeashed())
417  {
418  return;
419  }
420 
421  static const double CloseFollowDistance = 1.8; // The closest the mob will path towards the leashed to entity
422  static const double LeashNaturalLength = 5.0; // The closest the mob is actively pulled towards the entity
423  static const double LeashMaximumLength = 10.0; // Length where the leash breaks
424  static const double LeashSpringConstant = 20.0; // How stiff the leash is
425 
426  const auto LeashedToPos = m_LeashedTo->GetPosition();
427  const auto Displacement = LeashedToPos - GetPosition();
428  const auto Distance = Displacement.Length();
429  const auto Direction = Displacement.NormalizeCopy();
430 
431  // If the leash is over-extended, break the leash:
432  if (Distance > LeashMaximumLength)
433  {
434  LOGD("Leash broken (distance)");
435  Unleash(false);
436  return;
437  }
438 
439  // If the mob isn't following close enough, pull the mob towards the leashed to entity:
440  if (Distance > LeashNaturalLength)
441  {
442  // Accelerate monster towards the leashed to entity:
443  const auto Extension = Distance - LeashNaturalLength;
444  auto Acceleration = Direction * (Extension * LeashSpringConstant);
445 
446  // Stop mobs from floating up when on the ground
447  if (IsOnGround() && (Acceleration.y < std::abs(GetGravity())))
448  {
449  Acceleration.y = 0.0;
450  }
451 
452  // Apply the acceleration
453  using namespace std::chrono;
454  AddSpeed(Acceleration * duration_cast<duration<double>>(a_Dt).count());
455  }
456 
457  // Passively follow the leashed to entity:
458  if (Distance > CloseFollowDistance)
459  {
460  const Vector3d TargetBlock((LeashedToPos - Direction * CloseFollowDistance).Floor());
461  // Move to centre of target block face
462  MoveToPosition(TargetBlock + Vector3d{ 0.5, 0.0, 0.5 });
463  }
464 }
465 
466 
467 
468 
469 
470 void cMonster::SetPitchAndYawFromDestination(bool a_IsFollowingPath)
471 {
472  Vector3d BodyDistance;
473  if (!a_IsFollowingPath && (GetTarget() != nullptr))
474  {
475  BodyDistance = GetTarget()->GetPosition() - GetPosition();
476  }
477  else
478  {
479  BodyDistance = m_NextWayPointPosition - GetPosition();
480  }
481  double BodyRotation, BodyPitch;
482  BodyDistance.Normalize();
483  VectorToEuler(BodyDistance.x, BodyDistance.y, BodyDistance.z, BodyRotation, BodyPitch);
484  SetYaw(BodyRotation);
485 
486  Vector3d HeadDistance;
487  if (GetTarget() != nullptr)
488  {
489  if (GetTarget()->IsPlayer()) // Look at a player
490  {
491  HeadDistance = GetTarget()->GetPosition() - GetPosition();
492  }
493  else // Look at some other entity
494  {
495  HeadDistance = GetTarget()->GetPosition() - GetPosition();
496  // HeadDistance.y = GetTarget()->GetPosY() + GetHeight();
497  }
498  }
499  else // Look straight
500  {
501  HeadDistance = BodyDistance;
502  HeadDistance.y = 0;
503  }
504 
505  double HeadRotation, HeadPitch;
506  HeadDistance.Normalize();
507  VectorToEuler(HeadDistance.x, HeadDistance.y, HeadDistance.z, HeadRotation, HeadPitch);
508  if ((std::abs(BodyRotation - HeadRotation) < 70) && (std::abs(HeadPitch) < 60))
509  {
510  SetHeadYaw(HeadRotation);
511  SetPitch(-HeadPitch);
512  }
513  else
514  {
515  SetHeadYaw(BodyRotation);
516  SetPitch(0);
517  }
518 }
519 
520 
521 
522 
523 
525 {
528 }
529 
530 
531 
532 
533 
534 int cMonster::FindFirstNonAirBlockPosition(double a_PosX, double a_PosZ)
535 {
536  int PosY = POSY_TOINT;
537  PosY = Clamp(PosY, 0, cChunkDef::Height);
538 
539  if (!cBlockInfo::IsSolid(m_World->GetBlock(FloorC(a_PosX), PosY, FloorC(a_PosZ))))
540  {
541  while (!cBlockInfo::IsSolid(m_World->GetBlock(FloorC(a_PosX), PosY, FloorC(a_PosZ))) && (PosY > 0))
542  {
543  PosY--;
544  }
545 
546  return PosY + 1;
547  }
548  else
549  {
550  while ((PosY < cChunkDef::Height) && cBlockInfo::IsSolid(m_World->GetBlock(static_cast<int>(floor(a_PosX)), PosY, static_cast<int>(floor(a_PosZ)))))
551  {
552  PosY++;
553  }
554 
555  return PosY;
556  }
557 }
558 
559 
560 
561 
562 
564 {
565  if (!super::DoTakeDamage(a_TDI))
566  {
567  return false;
568  }
569 
570  if (!m_SoundHurt.empty() && (m_Health > 0))
571  {
573  }
574 
575  if ((a_TDI.Attacker != nullptr) && a_TDI.Attacker->IsPawn())
576  {
577  if (
578  (!a_TDI.Attacker->IsPlayer()) ||
579  (static_cast<cPlayer *>(a_TDI.Attacker)->CanMobsTarget())
580  )
581  {
582  SetTarget(static_cast<cPawn*>(a_TDI.Attacker));
583  }
585  }
586  return true;
587 }
588 
589 
590 
591 
592 
594 {
595  super::KilledBy(a_TDI);
596  if (m_SoundHurt != "")
597  {
599  }
600  int Reward;
601  switch (m_MobType)
602  {
603  // Animals
604  case mtChicken:
605  case mtCow:
606  case mtHorse:
607  case mtPig:
608  case mtRabbit:
609  case mtSheep:
610  case mtSquid:
611  case mtMooshroom:
612  case mtOcelot:
613  case mtWolf:
614  {
615  Reward = GetRandomProvider().RandInt(1, 3);
616  break;
617  }
618 
619  // Monsters
620  case mtCaveSpider:
621  case mtCreeper:
622  case mtEnderman:
623  case mtGhast:
624  case mtGuardian:
625  case mtSilverfish:
626  case mtSkeleton:
627  case mtSpider:
628  case mtWitch:
629  case mtZombie:
630  case mtZombiePigman:
631  case mtSlime:
632  case mtMagmaCube:
633  {
634  Reward = GetRandomProvider().RandInt(6, 8);
635  break;
636  }
637  case mtBlaze:
638  {
639  Reward = 10;
640  break;
641  }
642 
643  // Bosses
644  case mtEnderDragon:
645  {
646  Reward = 12000;
647  break;
648  }
649  case mtWither:
650  {
651  Reward = 50;
652  break;
653  }
654 
655  default:
656  {
657  Reward = 0;
658  break;
659  }
660  }
661  if ((a_TDI.Attacker != nullptr) && (!IsBaby()))
662  {
664  }
665  m_DestroyTimer = std::chrono::milliseconds(0);
666 }
667 
668 
669 
670 
671 
673 {
674  super::OnRightClicked(a_Player);
675 
676  const cItem & EquippedItem = a_Player.GetEquippedItem();
677  if ((EquippedItem.m_ItemType == E_ITEM_NAME_TAG) && !EquippedItem.m_CustomName.empty())
678  {
679  SetCustomName(EquippedItem.m_CustomName);
680  if (!a_Player.IsGameModeCreative())
681  {
682  a_Player.GetInventory().RemoveOneEquippedItem();
683  }
684  }
685 
686  // Using leashes
687  m_IsLeashActionJustDone = false;
688  if (IsLeashed() && (GetLeashedTo() == &a_Player)) // a player can only unleash a mob leashed to him
689  {
690  Unleash(!a_Player.IsGameModeCreative());
691  }
692  else if (IsLeashed())
693  {
694  // Mob is already leashed but client anticipates the server action and draws a leash link, so we need to send current leash to cancel it
695  m_World->BroadcastLeashEntity(*this, *this->GetLeashedTo());
696  }
697  else if (CanBeLeashed() && (EquippedItem.m_ItemType == E_ITEM_LEASH))
698  {
699  if (!a_Player.IsGameModeCreative())
700  {
701  a_Player.GetInventory().RemoveOneEquippedItem();
702  }
703  LeashTo(a_Player);
704  }
705 }
706 
707 
708 
709 
710 
711 // Checks to see if EventSeePlayer should be fired
712 // monster sez: Do I see the player
714 {
715  m_World->DoWithNearestPlayer(GetPosition(), static_cast<float>(m_SightDistance), [&](cPlayer & a_Player) -> bool
716  {
717  EventSeePlayer(&a_Player, a_Chunk);
718  return true;
719  }, false);
720 }
721 
722 
723 
724 
725 
727 {
728  if (GetTarget() != nullptr)
729  {
730  if ((GetTarget()->GetPosition() - GetPosition()).Length() > m_SightDistance)
731  {
732  EventLosePlayer();
733  }
734  }
735  else
736  {
737  EventLosePlayer();
738  }
739 }
740 
741 
742 
743 
744 
745 // What to do if player is seen
746 // default to change state to chasing
747 void cMonster::EventSeePlayer(cPlayer * a_SeenPlayer, cChunk & a_Chunk)
748 {
749  UNUSED(a_Chunk);
750  SetTarget(a_SeenPlayer);
751 }
752 
753 
754 
755 
756 
758 {
759  SetTarget(nullptr);
760  m_EMState = IDLE;
761 }
762 
763 
764 
765 
766 
767 void cMonster::InStateIdle(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
768 {
770  {
771  return; // Still getting there
772  }
773 
774  m_IdleInterval += a_Dt;
775 
776  if (m_IdleInterval > std::chrono::seconds(1))
777  {
778  auto & Random = GetRandomProvider();
779 
780  // At this interval the results are predictable
781  int rem = Random.RandInt(1, 7);
782  m_IdleInterval -= std::chrono::seconds(1); // So nothing gets dropped when the server hangs for a few seconds
783 
784  Vector3d Dist;
785  Dist.x = static_cast<double>(Random.RandInt(-5, 5));
786  Dist.z = static_cast<double>(Random.RandInt(-5, 5));
787 
788  if ((Dist.SqrLength() > 2) && (rem >= 3))
789  {
790 
791  Vector3d Destination(GetPosX() + Dist.x, GetPosition().y, GetPosZ() + Dist.z);
792 
793  cChunk * Chunk = a_Chunk.GetNeighborChunk(static_cast<int>(Destination.x), static_cast<int>(Destination.z));
794  if ((Chunk == nullptr) || !Chunk->IsValid())
795  {
796  return;
797  }
798 
799  BLOCKTYPE BlockType;
800  NIBBLETYPE BlockMeta;
801  int RelX = static_cast<int>(Destination.x) - Chunk->GetPosX() * cChunkDef::Width;
802  int RelZ = static_cast<int>(Destination.z) - Chunk->GetPosZ() * cChunkDef::Width;
803  int YBelowUs = static_cast<int>(Destination.y) - 1;
804  if (YBelowUs >= 0)
805  {
806  Chunk->GetBlockTypeMeta(RelX, YBelowUs, RelZ, BlockType, BlockMeta);
807  if (BlockType != E_BLOCK_STATIONARY_WATER) // Idle mobs shouldn't enter water on purpose
808  {
809  MoveToPosition(Destination);
810  }
811  }
812  }
813  }
814 }
815 
816 
817 
818 
819 
820 // What to do if in Chasing State
821 // This state should always be defined in each child class
822 void cMonster::InStateChasing(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
823 {
824  UNUSED(a_Dt);
825 }
826 
827 
828 
829 
830 
831 // What to do if in Escaping State
832 void cMonster::InStateEscaping(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
833 {
834  UNUSED(a_Dt);
835 
836  if (GetTarget() != nullptr)
837  {
838  Vector3d newloc = GetPosition();
839  newloc.x = (GetTarget()->GetPosition().x < newloc.x)? (newloc.x + m_SightDistance): (newloc.x - m_SightDistance);
840  newloc.z = (GetTarget()->GetPosition().z < newloc.z)? (newloc.z + m_SightDistance): (newloc.z - m_SightDistance);
841  MoveToPosition(newloc);
842  }
843  else
844  {
845  m_EMState = IDLE; // This shouldnt be required but just to be safe
846  }
847 }
848 
849 
850 
851 
852 
854 {
855  m_AttackCoolDownTicksLeft = static_cast<int>(3 * 20 * m_AttackRate); // A second has 20 ticks, an attack rate of 1 means 1 hit every 3 seconds
856 }
857 
858 
859 
860 
861 
862 void cMonster::SetCustomName(const AString & a_CustomName)
863 {
864  m_CustomName = a_CustomName;
865 
866  // The maximal length is 64
867  if (a_CustomName.length() > 64)
868  {
869  m_CustomName = a_CustomName.substr(0, 64);
870  }
871 
872  if (m_World != nullptr)
873  {
875  }
876 }
877 
878 
879 
880 
881 
882 void cMonster::SetCustomNameAlwaysVisible(bool a_CustomNameAlwaysVisible)
883 {
884  m_CustomNameAlwaysVisible = a_CustomNameAlwaysVisible;
885  if (m_World != nullptr)
886  {
888  }
889 }
890 
891 
892 
893 
894 
896 {
897  cRoot::Get()->GetMonsterConfig()->AssignAttributes(this, a_Name);
898 }
899 
900 
901 
902 
903 
905 {
906  return false;
907 }
908 
909 
910 
911 
912 
914 {
915  // Mob types aren't sorted, so we need to search linearly:
916  for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
917  {
918  if (g_MobTypeNames[i].m_Type == a_MobType)
919  {
920  return g_MobTypeNames[i].m_lcName;
921  }
922  }
923 
924  // Not found:
925  return "";
926 }
927 
928 
929 
930 
931 
933 {
934  // Mob types aren't sorted, so we need to search linearly:
935  for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
936  {
937  if (g_MobTypeNames[i].m_Type == a_MobType)
938  {
939  return g_MobTypeNames[i].m_VanillaName;
940  }
941  }
942 
943  // Not found:
944  return "";
945 }
946 
947 
948 
949 
950 
952 {
953  // Mob types aren't sorted, so we need to search linearly:
954  for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
955  {
956  if (g_MobTypeNames[i].m_Type == a_MobType)
957  {
958  return g_MobTypeNames[i].m_VanillaNameNBT;
959  }
960  }
961 
962  // Not found:
963  return "";
964 }
965 
966 
967 
968 
969 
971 {
972  AString lcName = StrToLower(a_Name);
973 
974  // Search Cuberite name:
975  for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
976  {
977  if (strcmp(g_MobTypeNames[i].m_lcName, lcName.c_str()) == 0)
978  {
979  return g_MobTypeNames[i].m_Type;
980  }
981  }
982 
983  // Not found. Search Vanilla name:
984  for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
985  {
986  if (strcmp(StrToLower(g_MobTypeNames[i].m_VanillaName).c_str(), lcName.c_str()) == 0)
987  {
988  return g_MobTypeNames[i].m_Type;
989  }
990  }
991 
992  // Search in NBT name
993  for (size_t i = 0; i < ARRAYCOUNT(g_MobTypeNames); i++)
994  {
995  if (strcmp(StrToLower(g_MobTypeNames[i].m_VanillaNameNBT).c_str(), lcName.c_str()) == 0)
996  {
997  return g_MobTypeNames[i].m_Type;
998  }
999  }
1000 
1001  // Not found:
1002  return mtInvalidType;
1003 }
1004 
1005 
1006 
1007 
1008 
1010 {
1011  // Passive-agressive mobs are counted in mob spawning code as passive
1012 
1013  switch (a_Type)
1014  {
1015  case mtBat: return mfAmbient;
1016  case mtBlaze: return mfHostile;
1017  case mtCaveSpider: return mfHostile;
1018  case mtChicken: return mfPassive;
1019  case mtCow: return mfPassive;
1020  case mtCreeper: return mfHostile;
1021  case mtEnderDragon: return mfNoSpawn;
1022  case mtEnderman: return mfHostile;
1023  case mtGhast: return mfHostile;
1024  case mtGiant: return mfNoSpawn;
1025  case mtGuardian: return mfWater; // Just because they have special spawning conditions. If Watertemples have been added, this needs to be edited!
1026  case mtHorse: return mfPassive;
1027  case mtIronGolem: return mfPassive;
1028  case mtMagmaCube: return mfHostile;
1029  case mtMooshroom: return mfHostile;
1030  case mtOcelot: return mfPassive;
1031  case mtPig: return mfPassive;
1032  case mtRabbit: return mfPassive;
1033  case mtSheep: return mfPassive;
1034  case mtSilverfish: return mfHostile;
1035  case mtSkeleton: return mfHostile;
1036  case mtSlime: return mfHostile;
1037  case mtSnowGolem: return mfNoSpawn;
1038  case mtSpider: return mfHostile;
1039  case mtSquid: return mfWater;
1040  case mtVillager: return mfPassive;
1041  case mtWitch: return mfHostile;
1042  case mtWither: return mfNoSpawn;
1043  case mtWolf: return mfHostile;
1044  case mtZombie: return mfHostile;
1045  case mtZombiePigman: return mfHostile;
1046 
1047  default:
1048  {
1049  ASSERT(!"Unhandled mob type");
1050  return mfUnhandled;
1051  }
1052  }
1053 }
1054 
1055 
1056 
1057 
1058 
1060 {
1061  switch (a_MobFamily)
1062  {
1063  case mfHostile: return 40;
1064  case mfPassive: return 40;
1065  case mfAmbient: return 40;
1066  case mfWater: return 400;
1067  case mfNoSpawn: return -1;
1068  default:
1069  {
1070  ASSERT(!"Unhandled mob family");
1071  return -1;
1072  }
1073  }
1074 }
1075 
1076 
1077 
1078 
1079 
1081 void cMonster::SetTarget (cPawn * a_NewTarget)
1082 {
1083  ASSERT((a_NewTarget == nullptr) || (IsTicking()));
1084  if (m_Target == a_NewTarget)
1085  {
1086  return;
1087  }
1088  cPawn * OldTarget = m_Target;
1089  m_Target = a_NewTarget;
1090 
1091  if (OldTarget != nullptr)
1092  {
1093  // Notify the old target that we are no longer targeting it.
1094  OldTarget->NoLongerTargetingMe(this);
1095  }
1096 
1097  if (a_NewTarget != nullptr)
1098  {
1099  ASSERT(a_NewTarget->IsTicking());
1100  // Notify the new target that we are now targeting it.
1101  m_Target->TargetingMe(this);
1103  }
1104 
1105 }
1106 
1107 
1108 
1109 
1110 
1112 {
1113  m_Target = nullptr;
1114 }
1115 
1116 
1117 
1118 
1119 
1121 {
1122  return m_Target;
1123 }
1124 
1125 
1126 
1127 
1128 
1129 std::unique_ptr<cMonster> cMonster::NewMonsterFromType(eMonsterType a_MobType)
1130 {
1131  auto & Random = GetRandomProvider();
1132 
1133  // Create the mob entity
1134  switch (a_MobType)
1135  {
1136  case mtMagmaCube:
1137  {
1138  return cpp14::make_unique<cMagmaCube>(1 << Random.RandInt(2)); // Size 1, 2 or 4
1139  }
1140  case mtSlime:
1141  {
1142  return cpp14::make_unique<cSlime>(1 << Random.RandInt(2)); // Size 1, 2 or 4
1143  }
1144  case mtSkeleton:
1145  {
1146  // TODO: Actual detection of spawning in Nether
1147  return cpp14::make_unique<cSkeleton>(false);
1148  }
1149  case mtVillager:
1150  {
1151  int VillagerType = Random.RandInt(6);
1152  if (VillagerType == 6)
1153  {
1154  // Give farmers a better chance of spawning
1155  VillagerType = 0;
1156  }
1157 
1158  return cpp14::make_unique<cVillager>(static_cast<cVillager::eVillagerType>(VillagerType));
1159  }
1160  case mtHorse:
1161  {
1162  // Horses take a type (species), a colour, and a style (dots, stripes, etc.)
1163  int HorseType = Random.RandInt(7);
1164  int HorseColor = Random.RandInt(6);
1165  int HorseStyle = Random.RandInt(4);
1166  int HorseTameTimes = Random.RandInt(1, 6);
1167 
1168  if ((HorseType == 5) || (HorseType == 6) || (HorseType == 7))
1169  {
1170  // Increase chances of normal horse (zero)
1171  HorseType = 0;
1172  }
1173 
1174  return cpp14::make_unique<cHorse>(HorseType, HorseColor, HorseStyle, HorseTameTimes);
1175  }
1176 
1177  case mtBat: return cpp14::make_unique<cBat>();
1178  case mtBlaze: return cpp14::make_unique<cBlaze>();
1179  case mtCaveSpider: return cpp14::make_unique<cCaveSpider>();
1180  case mtChicken: return cpp14::make_unique<cChicken>();
1181  case mtCow: return cpp14::make_unique<cCow>();
1182  case mtCreeper: return cpp14::make_unique < cCreeper>();
1183  case mtEnderDragon: return cpp14::make_unique<cEnderDragon>();
1184  case mtEnderman: return cpp14::make_unique<cEnderman>();
1185  case mtGhast: return cpp14::make_unique<cGhast>();
1186  case mtGiant: return cpp14::make_unique<cGiant>();
1187  case mtGuardian: return cpp14::make_unique<cGuardian>();
1188  case mtIronGolem: return cpp14::make_unique<cIronGolem>();
1189  case mtMooshroom: return cpp14::make_unique<cMooshroom>();
1190  case mtOcelot: return cpp14::make_unique<cOcelot>();
1191  case mtPig: return cpp14::make_unique<cPig>();
1192  case mtRabbit: return cpp14::make_unique<cRabbit>();
1193  case mtSheep: return cpp14::make_unique<cSheep>();
1194  case mtSilverfish: return cpp14::make_unique<cSilverfish>();
1195  case mtSnowGolem: return cpp14::make_unique<cSnowGolem>();
1196  case mtSpider: return cpp14::make_unique<cSpider>();
1197  case mtSquid: return cpp14::make_unique<cSquid>();
1198  case mtWitch: return cpp14::make_unique<cWitch>();
1199  case mtWither: return cpp14::make_unique<cWither>();
1200  case mtWolf: return cpp14::make_unique<cWolf>();
1201  case mtZombie: return cpp14::make_unique<cZombie>(false); // TODO: Infected zombie parameter
1202  case mtZombiePigman: return cpp14::make_unique<cZombiePigman>();
1203  default:
1204  {
1205  ASSERT(!"Unhandled mob type whilst trying to spawn mob!");
1206  return nullptr;
1207  }
1208  }
1209 }
1210 
1211 
1212 
1213 
1214 
1215 void cMonster::AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth)
1216 {
1217  auto Count = GetRandomProvider().RandInt<unsigned int>(a_Min, a_Max);
1218  auto MaxStackSize = static_cast<unsigned char>(ItemHandler(a_Item)->GetMaxStackSize());
1219  while (Count > MaxStackSize)
1220  {
1221  a_Drops.emplace_back(a_Item, MaxStackSize, a_ItemHealth);
1222  Count -= MaxStackSize;
1223  }
1224  if (Count > 0)
1225  {
1226  a_Drops.emplace_back(a_Item, Count, a_ItemHealth);
1227  }
1228 }
1229 
1230 
1231 
1232 
1233 
1234 void cMonster::AddRandomUncommonDropItem(cItems & a_Drops, float a_Chance, short a_Item, short a_ItemHealth)
1235 {
1236  if (GetRandomProvider().RandBool(a_Chance / 100.0))
1237  {
1238  a_Drops.push_back(cItem(a_Item, 1, a_ItemHealth));
1239  }
1240 }
1241 
1242 
1243 
1244 
1245 
1246 void cMonster::AddRandomRareDropItem(cItems & a_Drops, cItems & a_Items, unsigned int a_LootingLevel)
1247 {
1248  auto & r1 = GetRandomProvider();
1249  if (r1.RandBool((5 + a_LootingLevel) / 200.0))
1250  {
1251  size_t Rare = r1.RandInt<size_t>(a_Items.Size() - 1);
1252  a_Drops.push_back(a_Items.at(Rare));
1253  }
1254 }
1255 
1256 
1257 
1258 
1259 
1260 void cMonster::AddRandomArmorDropItem(cItems & a_Drops, unsigned int a_LootingLevel)
1261 {
1262  auto & r1 = GetRandomProvider();
1263 
1264  double LootingBonus = a_LootingLevel / 100.0;
1265 
1266  if (r1.RandBool(m_DropChanceHelmet + LootingBonus))
1267  {
1268  if (!GetEquippedHelmet().IsEmpty())
1269  {
1270  a_Drops.push_back(GetEquippedHelmet());
1271  }
1272  }
1273 
1274  if (r1.RandBool(m_DropChanceChestplate + LootingBonus))
1275  {
1276  if (!GetEquippedChestplate().IsEmpty())
1277  {
1278  a_Drops.push_back(GetEquippedChestplate());
1279  }
1280  }
1281 
1282  if (r1.RandBool(m_DropChanceLeggings + LootingBonus))
1283  {
1284  if (!GetEquippedLeggings().IsEmpty())
1285  {
1286  a_Drops.push_back(GetEquippedLeggings());
1287  }
1288  }
1289 
1290  if (r1.RandBool(m_DropChanceBoots + LootingBonus))
1291  {
1292  if (!GetEquippedBoots().IsEmpty())
1293  {
1294  a_Drops.push_back(GetEquippedBoots());
1295  }
1296  }
1297 }
1298 
1299 
1300 
1301 
1302 
1303 void cMonster::AddRandomWeaponDropItem(cItems & a_Drops, unsigned int a_LootingLevel)
1304 {
1305  if (GetRandomProvider().RandBool(m_DropChanceWeapon + (a_LootingLevel / 100.0)))
1306  {
1307  if (!GetEquippedWeapon().IsEmpty())
1308  {
1309  a_Drops.push_back(GetEquippedWeapon());
1310  }
1311  }
1312 }
1313 
1314 
1315 
1316 
1317 
1318 void cMonster::HandleDaylightBurning(cChunk & a_Chunk, bool WouldBurn)
1319 {
1320  if (!m_BurnsInDaylight)
1321  {
1322  return;
1323  }
1324 
1325  int RelY = POSY_TOINT;
1326  if ((RelY < 0) || (RelY >= cChunkDef::Height))
1327  {
1328  // Outside the world
1329  return;
1330  }
1331  if (!a_Chunk.IsLightValid())
1332  {
1334  return;
1335  }
1336 
1337  if (!IsOnFire() && WouldBurn)
1338  {
1339  // Burn for 100 ticks, then decide again
1340  StartBurning(100);
1341  }
1342 }
1343 
1344 
1345 
1346 
1347 
1348 bool cMonster::WouldBurnAt(Vector3d a_Location, cChunk & a_Chunk)
1349 {
1350  // If the Y coord is out of range, return the most logical result without considering anything else:
1351  int RelY = FloorC(a_Location.y);
1352  if (RelY >= cChunkDef::Height)
1353  {
1354  // Always burn above the world
1355  return true;
1356  }
1357  if (RelY <= 0)
1358  {
1359  // The mob is about to die, no point in burning
1360  return false;
1361  }
1362 
1363  PREPARE_REL_AND_CHUNK(a_Location, a_Chunk);
1364  if (!RelSuccess)
1365  {
1366  return false;
1367  }
1368 
1369  if (
1370  (Chunk->GetBlock(Rel.x, Rel.y, Rel.z) != E_BLOCK_SOULSAND) && // Not on soulsand
1371  (GetWorld()->GetTimeOfDay() < 12000 + 1000) && // Daytime
1372  GetWorld()->IsWeatherSunnyAt(POSX_TOINT, POSZ_TOINT) && // Not raining
1373  !IsInWater() // Isn't swimming
1374  )
1375  {
1376  int MobHeight = CeilC(a_Location.y + GetHeight()) - 1; // The block Y coord of the mob's head
1377  if (MobHeight >= cChunkDef::Height)
1378  {
1379  return true;
1380  }
1381  // Start with the highest block and scan down to just above the mob's head.
1382  // If a non transparent is found, return false (do not burn). Otherwise return true.
1383  // Note that this loop is not a performance concern as transparent blocks are rare and the loop almost always bailes out
1384  // instantly.(An exception is e.g. standing under a long column of glass).
1385  int CurrentBlock = Chunk->GetHeight(Rel.x, Rel.z);
1386  while (CurrentBlock > MobHeight)
1387  {
1388  BLOCKTYPE Block = Chunk->GetBlock(Rel.x, CurrentBlock, Rel.z);
1389  if (
1390  // Do not burn if a block above us meets one of the following conditions:
1391  (!cBlockInfo::IsTransparent(Block)) ||
1392  (Block == E_BLOCK_LEAVES) ||
1393  (Block == E_BLOCK_NEW_LEAVES) ||
1394  (IsBlockWater(Block))
1395  )
1396  {
1397  return false;
1398  }
1399  --CurrentBlock;
1400  }
1401  return true;
1402 
1403  }
1404  return false;
1405 }
1406 
1407 
1408 
1409 
1410 
1412 {
1413  return FamilyFromType(m_MobType);
1414 }
1415 
1416 
1417 
1418 
1419 
1420 void cMonster::LeashTo(cEntity & a_Entity, bool a_ShouldBroadcast)
1421 {
1422  // Do nothing if already leashed
1423  if (m_LeashedTo != nullptr)
1424  {
1425  return;
1426  }
1427 
1428  m_LeashedTo = &a_Entity;
1429 
1430  a_Entity.AddLeashedMob(this);
1431 
1432  if (a_ShouldBroadcast)
1433  {
1434  m_World->BroadcastLeashEntity(*this, a_Entity);
1435  }
1436 
1437  m_IsLeashActionJustDone = true;
1438 }
1439 
1440 
1441 
1442 
1443 
1444 void cMonster::Unleash(bool a_ShouldDropLeashPickup, bool a_ShouldBroadcast)
1445 {
1446  // Do nothing if not leashed
1447  if (m_LeashedTo == nullptr)
1448  {
1449  return;
1450  }
1451 
1453 
1454  m_LeashedTo = nullptr;
1455 
1456  if (a_ShouldDropLeashPickup)
1457  {
1458  cItems Pickups;
1459  Pickups.Add(cItem(E_ITEM_LEASH, 1, 0));
1460  GetWorld()->SpawnItemPickups(Pickups, GetPosX() + 0.5, GetPosY() + 0.5, GetPosZ() + 0.5);
1461  }
1462 
1463  if (a_ShouldBroadcast)
1464  {
1466  }
1467 
1468  m_IsLeashActionJustDone = true;
1469 }
1470 
1471 
1472 
1473 
1474 
1475 void cMonster::Unleash(bool a_ShouldDropLeashPickup)
1476 {
1477  Unleash(a_ShouldDropLeashPickup, true);
1478 }
int GetChunkZ(void) const
Definition: Entity.h:219
double m_RelativeWalkSpeed
Definition: Monster.h:288
Vector3d m_FinalDestination
Coordinates for the ultimate, final destination.
Definition: Monster.h:226
double GetPosY(void) const
Definition: Entity.h:207
bool IsValid(void) const
Returns true iff the chunk block data is valid (loaded / generated)
Definition: Chunk.h:72
virtual void InStateEscaping(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Monster.cpp:832
bool IsLightValid(void) const
Definition: Chunk.h:95
double GetPosX(void) const
Definition: Entity.h:206
virtual void Destroyed() override
Definition: Monster.cpp:156
BLOCKTYPE GetBlock(Vector3i a_BlockPos)
Returns the block type at the specified position.
Definition: World.h:416
T x
Definition: Vector3.h:17
bool IsBlockWater(BLOCKTYPE a_BlockType)
Definition: Defines.h:436
void AddRandomWeaponDropItem(cItems &a_Drops, unsigned int a_LootingLevel)
Adds weapon that is equipped with the chance saved in m_DropChance... to the drop.
Definition: Monster.cpp:1303
float m_DropChanceWeapon
Definition: Monster.h:277
bool DoesPosYRequireJump(double a_PosY)
Returns whether the monster needs to jump to reach a given height.
Definition: Monster.h:245
static std::unique_ptr< cMonster > NewMonsterFromType(eMonsterType a_MobType)
Creates a new object of the specified mob.
Definition: Monster.cpp:1129
void MoveToWayPoint(cChunk &a_Chunk)
Move in a straight line to the next waypoint in the path, will jump if needed.
Definition: Monster.cpp:180
virtual cItem GetEquippedHelmet(void) const
Returns the currently equipped helmet; empty item if none.
Definition: Entity.h:349
static bool IsSolid(BLOCKTYPE a_Type)
Definition: BlockInfo.h:48
void AddSpeedZ(double a_AddSpeedZ)
Definition: Entity.cpp:2163
void GetBlockTypeMeta(Vector3i a_RelPos, BLOCKTYPE &a_BlockType, NIBBLETYPE &a_BlockMeta) const
Definition: Chunk.cpp:2230
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
size_t Size(void) const
Definition: Item.h:257
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI)
Makes this entity take damage specified in the a_TDI.
Definition: Entity.cpp:384
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed.
Definition: Inventory.cpp:207
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Monster.cpp:263
MTRand & GetRandomProvider()
Returns the current thread&#39;s random number source.
Definition: FastRandom.cpp:20
#define GET_AND_VERIFY_CURRENT_CHUNK(ChunkVarName, X, Z)
Definition: Entity.h:34
bool HasAnyMobLeashed() const
Returs whether the entity has any mob leashed to.
Definition: Entity.h:553
const char * m_lcName
Definition: Monster.cpp:34
static const int Width
Definition: ChunkDef.h:134
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
cWorld * m_World
Definition: Entity.h:620
void AddRandomArmorDropItem(cItems &a_Drops, unsigned int a_LootingLevel)
Adds armor that is equipped with the chance saved in m_DropChance... to the drop. ...
Definition: Monster.cpp:1260
const cItem & GetEquippedItem(void) const
Definition: Player.h:142
void SetHeadYaw(double a_HeadYaw)
Definition: Entity.cpp:2027
virtual char GetMaxStackSize(void)
Returns the maximum stack size for a given item.
Definition: Player.h:27
cPawn * GetTarget()
Returns the current target.
Definition: Monster.cpp:1120
bool IsEmpty(void) const
Definition: Item.h:116
virtual bool IsOnFire(void) const
Definition: Entity.h:486
void SendLeashEntity(const cEntity &a_Entity, const cEntity &a_EntityLeashedTo)
cPawn * m_Target
A pointer to the entity this mobile is aiming to reach.
Definition: Monster.h:326
const Vector3d & GetSpeed(void) const
Exported in ManualBindings.
Definition: Entity.h:310
void AddRandomDropItem(cItems &a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth=0)
Adds a random number of a_Item between a_Min and a_Max to itemdrops a_Drops.
Definition: Monster.cpp:1215
virtual cItem GetEquippedBoots(void) const
Returns the currently equipped boots; empty item if none.
Definition: Entity.h:358
bool IsBaby(void) const
Definition: Monster.h:150
virtual bool IsInWater(void) const
Returns true if any part of the entity is in a water block.
Definition: Entity.h:500
float m_DropChanceHelmet
Definition: Monster.h:278
virtual ~cMonster() override
Definition: Monster.cpp:126
void SetPitch(double a_Pitch)
Definition: Entity.cpp:2070
virtual void MoveToPosition(const Vector3d &a_Position)
Engage pathfinder and tell it to calculate a path to a given position, and move the mob accordingly...
Definition: Monster.cpp:244
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
float m_Health
Definition: Entity.h:584
void AddRandomRareDropItem(cItems &a_Drops, cItems &a_Items, unsigned int a_LootingLevel)
Adds one rare item out of the list of rare items a_Items modified by the looting level a_LootingLevel...
Definition: Monster.cpp:1246
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Monster.cpp:593
cEntity * GetLeashedTo() const
Returns the entity to where this mob is leashed, returns nullptr if it&#39;s not leashed.
Definition: Monster.h:93
cEntity * m_LeashedTo
Entity leashed to.
Definition: Monster.h:296
Definition: Chunk.h:49
bool IsWeatherSunnyAt(int a_BlockX, int a_BlockZ)
Returns true if it is sunny at the specified location.
Definition: World.h:997
virtual void Destroyed() override
Definition: Pawn.cpp:39
void SpawnItemPickups(const cItems &a_Pickups, Vector3i a_BlockPos, double a_FlyAwaySpeed=1.0, bool a_IsPlayerCreated=false)
Spawns item pickups for each item in the list.
Definition: World.cpp:1936
void SendSpawnMob(const cMonster &a_Mob)
T y
Definition: Vector3.h:17
static cLeashKnot * FindKnotAtPos(cWorldInterface &a_WorldInterface, Vector3i a_BlockPos)
Returns the leash knot entity representing the knot at the specified position.
Definition: LeashKnot.cpp:143
void AddSpeedX(double a_AddSpeedX)
Definition: Entity.cpp:2145
enum cMonster::MState m_EMState
void SetAge(int a_Age)
Definition: Monster.h:152
void NoLongerTargetingMe(cMonster *a_Monster)
Remove the monster from the list of monsters targeting this pawn.
Definition: Pawn.cpp:251
eMonsterType
Identifies individual monster type, as well as their network type-ID.
Definition: MonsterTypes.h:10
virtual void Destroy(bool a_ShouldBroadcast=true)
Destroys the entity and schedules it for memory freeing; if a_ShouldBroadcast is set to true...
Definition: Entity.cpp:219
bool ReachedFinalDestination(void)
Returns if the ultimate, final destination has been reached.
Definition: Monster.h:235
double GetHeight(void) const
Definition: Entity.h:204
T z
Definition: Vector3.h:17
int m_SightDistance
Definition: Monster.h:275
bool WouldBurnAt(Vector3d a_Location, cChunk &a_Chunk)
Definition: Monster.cpp:1348
static const int Height
Definition: ChunkDef.h:135
bool m_IsLeashActionJustDone
Mob has ben leashed or unleashed in current player action.
Definition: Monster.h:302
bool IsPawn(void) const
Definition: Entity.h:174
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override
float m_DropChanceBoots
Definition: Monster.h:281
void SetSpeedZ(double a_SpeedZ)
Sets the speed in the Z axis, leaving the other speed components intact.
Definition: Entity.cpp:2118
virtual void CheckEventLostPlayer(void)
Definition: Monster.cpp:726
virtual void EventLosePlayer(void)
Definition: Monster.cpp:757
IntType RandInt(IntType a_Min, IntType a_Max)
Return a random IntType in the range [a_Min, a_Max].
Definition: FastRandom.h:78
static const struct @18 g_MobTypeNames[]
Map for eType <-> string Needs to be alpha-sorted by the strings, because binary search is used in St...
const char * m_VanillaName
Definition: Monster.cpp:35
void Normalize(void)
Definition: Vector3.h:44
void StartBurning(int a_TicksLeftBurning)
Puts the entity on fire for the specified amount of ticks.
Definition: Entity.cpp:1819
static int GetSpawnDelay(cMonster::eFamily a_MobFamily)
Returns the spawn delay (number of game ticks between spawn attempts) for the given mob family...
Definition: Monster.cpp:1059
#define WAYPOINT_RADIUS
Definition: PathFinder.h:5
void UnsafeUnsetTarget()
Unset the target without notifying the target entity.
Definition: Monster.cpp:1111
void SetCustomName(const AString &a_CustomName)
Sets the custom name of the monster.
Definition: Monster.cpp:862
bool m_WasLastTargetAPlayer
Definition: Monster.h:293
virtual bool IsOnGround(void) const
Returns whether the entity is on ground or not.
Definition: Entity.h:518
void AddLeashedMob(cMonster *a_Monster)
Adds a mob to the leashed list of mobs.
Definition: Entity.cpp:2233
Direction
virtual cItem GetEquippedLeggings(void) const
Returns the currently equipped leggings; empty item if none.
Definition: Entity.h:355
AString m_CustomName
Definition: Item.h:213
void Add(const cItem &a_Item)
Definition: Item.h:254
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2201
float m_AttackRate
Definition: Monster.h:271
bool m_PathfinderActivated
Stores if pathfinder is being used - set when final destination is set, and unset when stopped moving...
Definition: Monster.h:220
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Monster.cpp:563
virtual bool IsUndead(void)
Returns whether this mob is undead (skeleton, zombie, etc.)
Definition: Monster.cpp:904
Vector3d m_NextWayPointPosition
Coordinates of the next position that should be reached.
Definition: Monster.h:223
int GetPosX(void) const
Definition: Chunk.h:150
virtual int GetTimeOfDay(void) const override
Definition: World.h:110
void SetPitchAndYawFromDestination(bool a_IsFollowingPath)
Sets the body yaw and head yaw.
Definition: Monster.cpp:470
static eFamily FamilyFromType(eMonsterType a_MobType)
Returns the mob family based on the type.
Definition: Monster.cpp:1009
int m_TicksSinceLastDamaged
Definition: Monster.h:283
void AddSpeed(double a_AddSpeedX, double a_AddSpeedY, double a_AddSpeedZ)
Definition: Entity.cpp:2136
void RemoveLeashedMob(cMonster *a_Monster)
Removes a mob from the leashed list of mobs.
Definition: Entity.cpp:2245
#define POSX_TOINT
Definition: Entity.h:29
std::chrono::milliseconds m_DestroyTimer
Definition: Monster.h:262
virtual void HandleFalling(void)
Definition: Pawn.cpp:283
#define POSY_TOINT
Definition: Entity.h:30
eMonsterType m_MobType
Definition: Monster.h:264
void SetTarget(cPawn *a_NewTarget)
Sets the target that this mob will chase.
Definition: Monster.cpp:1081
cMonster(const AString &a_ConfigName, eMonsterType a_MobType, const AString &a_SoundHurt, const AString &a_SoundDeath, double a_Width, double a_Height)
Creates the mob object.
Definition: Monster.cpp:79
AString m_SoundDeath
Definition: Monster.h:269
bool DoWithNearestPlayer(Vector3d a_Pos, double a_RangeLimit, cPlayerListCallback a_Callback, bool a_CheckLineOfSight=true, bool a_IgnoreSpectator=true)
Calls the callback for nearest player for given position, Returns false if player not found...
Definition: World.cpp:2639
float GetGravity(void) const
Definition: Entity.h:286
#define ASSERT(x)
Definition: Globals.h:335
static AString MobTypeToVanillaNBT(eMonsterType a_MobType)
Translates the MobType enum to the vanilla nbt name.
Definition: Monster.cpp:951
#define LOGD(...)
Definition: LoggerSimple.h:40
bool IsLeashed() const
Returns whether the monster is leashed to an entity.
Definition: Monster.h:84
int m_AttackCoolDownTicksLeft
Definition: Monster.h:274
bool m_bTouchGround
Definition: Pawn.h:78
float m_DropChanceLeggings
Definition: Monster.h:280
virtual void SpawnOn(cClientHandle &a_ClientHandle) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
Definition: Monster.cpp:166
int FindFirstNonAirBlockPosition(double a_PosX, double a_PosZ)
Finds the lowest non-air block position (not the highest, as cWorld::GetHeight does) If current Y is ...
Definition: Monster.cpp:534
virtual void BroadcastLeashEntity(const cEntity &a_Entity, const cEntity &a_EntityLeashedTo) override
std::chrono::milliseconds m_IdleInterval
Definition: Monster.h:261
#define UNUSED
Definition: Globals.h:152
float m_DropChanceChestplate
Definition: Monster.h:279
int m_AgingTimer
Definition: Monster.h:291
Definition: Pawn.h:15
virtual void Destroy(bool a_ShouldBroadcast=true) override
Destroys the entity and schedules it for memory freeing; if a_ShouldBroadcast is set to true...
Definition: Monster.cpp:135
virtual void CheckEventSeePlayer(cChunk &a_Chunk)
Definition: Monster.cpp:713
T Clamp(T a_Value, T a_Min, T a_Max)
Clamp X to the specified range.
Definition: Globals.h:351
virtual void OnRightClicked(cPlayer &a_Player)
Called when the specified player right-clicks this entity.
Definition: Entity.h:523
void SetCustomNameAlwaysVisible(bool a_CustomNameAlwaysVisible)
Sets the custom name visiblity of this monster.
Definition: Monster.cpp:882
static AString MobTypeToString(eMonsterType a_MobType)
Translates MobType enum to a string, empty string if unknown.
Definition: Monster.cpp:913
std::string AString
Definition: StringUtils.h:13
cPathFinder m_PathFinder
The pathfinder instance handles pathfinding for this monster.
Definition: Monster.h:217
cMonsterConfig * GetMonsterConfig(void)
Definition: Root.h:88
void HandleDaylightBurning(cChunk &a_Chunk, bool WouldBurn)
Definition: Monster.cpp:1318
double SqrLength(void) const
Definition: Vector3.h:100
static eMonsterType StringToMobType(const AString &a_MobTypeName)
Translates MobType string to the enum, mtInvalidType if not recognized.
Definition: Monster.cpp:970
bool IsPlayer(void) const
Definition: Entity.h:171
AString m_SoundHurt
Definition: Monster.h:268
void SetSpeedY(double a_SpeedY)
Sets the speed in the Y axis, leaving the other speed components intact.
Definition: Entity.cpp:2109
virtual std::vector< UInt32 > SpawnSplitExperienceOrbs(Vector3d a_Pos, int a_Reward) override
Spawns experience orbs of the specified total value at the given location.
Definition: World.cpp:2045
ePathFinderStatus GetNextWayPoint(cChunk &a_Chunk, const Vector3d &a_Source, Vector3d *a_Destination, Vector3d *a_OutputWaypoint, bool a_DontCare=false)
Updates the PathFinder&#39;s internal state and returns a waypoint.
Definition: PathFinder.cpp:22
void ResetAttackCooldown()
Definition: Monster.cpp:853
static bool IsTransparent(BLOCKTYPE a_Type)
Definition: BlockInfo.h:32
eVillagerType
Definition: Villager.h:17
void SetSpeedX(double a_SpeedX)
Sets the speed in the X axis, leaving the other speed components intact.
Definition: Entity.cpp:2100
void AssignAttributes(cMonster *a_Monster, const AString &a_Name)
int GetChunkX(void) const
Definition: Entity.h:218
int GetPosZ(void) const
Definition: Chunk.h:151
static cRoot * Get()
Definition: Root.h:51
void CalcLeashActions(std::chrono::milliseconds a_Dt)
Leash calculations inside Tick function.
Definition: Monster.cpp:402
bool m_CustomNameAlwaysVisible
Definition: Monster.h:266
virtual void EventSeePlayer(cPlayer *a_Player, cChunk &a_Chunk)
Definition: Monster.cpp:747
virtual void BroadcastUnleashEntity(const cEntity &a_Entity) override
cChunk * GetNeighborChunk(int a_BlockX, int a_BlockZ)
Returns the chunk into which the specified block belongs, by walking the neighbors.
Definition: Chunk.cpp:2279
virtual cItem GetEquippedWeapon(void) const
Returns the curently equipped weapon; empty item if none.
Definition: Entity.h:346
Definition: Entity.h:73
void QueueLightChunk(int a_ChunkX, int a_ChunkZ, std::unique_ptr< cChunkCoordCallback > a_Callback={})
Queues a chunk for lighting; a_Callback is called after the chunk is lighted.
Definition: World.cpp:2952
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:307
const char * m_VanillaNameNBT
Definition: Monster.cpp:36
eMonsterType m_Type
Definition: Monster.cpp:33
virtual void InStateIdle(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Monster.cpp:767
bool m_bOnGround
Stores if the entity is on the ground.
Definition: Entity.h:604
void GetMonsterConfig(const AString &a_Name)
Reads the monster configuration for the specified monster name and assigns it to this object...
Definition: Monster.cpp:895
eFamily GetMobFamily(void) const
Definition: Monster.cpp:1411
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Monster.cpp:672
int m_JumpCoolDown
Definition: Monster.h:259
double GetPosZ(void) const
Definition: Entity.h:208
bool IsGameModeCreative(void) const
Returns true if the player is in Creative mode, either explicitly, or by inheriting from current worl...
Definition: Player.cpp:1260
void TargetingMe(cMonster *a_Monster)
Add the monster to the list of monsters targeting this pawn.
Definition: Pawn.cpp:271
void AddPosY(double a_AddPosY)
Definition: Entity.h:248
#define POSZ_TOINT
Definition: Entity.h:31
short m_ItemType
Definition: Item.h:209
void LeashTo(cEntity &a_Entity, bool a_ShouldBroadcast=true)
Leash the monster to an entity.
Definition: Monster.cpp:1420
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Pawn.cpp:49
void SetYaw(double a_Yaw)
Definition: Entity.cpp:2059
void Unleash(bool a_ShouldDropLeashPickup)
Unleash the monster.
Definition: Monster.cpp:1475
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:254
virtual void InStateChasing(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Monster.cpp:822
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Pawn.cpp:129
AString StrToLower(const AString &s)
Returns a lower-cased copy of the string.
std::enable_if< std::is_arithmetic< T >::value, C >::type FloorC(T a_Value)
Floors a value, then casts it to C (an int by default)
Definition: Globals.h:362
cInventory & GetInventory(void)
Definition: Player.h:136
static AString MobTypeToVanillaName(eMonsterType a_MobType)
Translates MobType enum to the vanilla name of the mob, empty string if unknown.
Definition: Monster.cpp:932
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:290
virtual cItem GetEquippedChestplate(void) const
Returns the currently equipped chestplate; empty item if none.
Definition: Entity.h:352
Definition: Item.h:36
cItemHandler * ItemHandler(int a_ItemType)
Definition: ItemHandler.h:174
bool IsLeashKnot(void) const
Definition: Entity.h:183
void SetLeashToPos(Vector3d *pos)
Sets entity position to where is leashed this mob.
Definition: Monster.h:101
void AddRandomUncommonDropItem(cItems &a_Drops, float a_Chance, short a_Item, short a_ItemHealth=0)
Adds a item a_Item with the chance of a_Chance (in percent) to itemdrops a_Drops. ...
Definition: Monster.cpp:1234
bool m_BurnsInDaylight
Definition: Monster.h:287
cEntity * Attacker
Definition: Entity.h:60
#define PREPARE_REL_AND_CHUNK(Position, OriginalChunk)
Definition: Chunk.h:43
cWorld * GetWorld(void) const
Definition: Entity.h:201
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
virtual void HandleFalling(void) override
Definition: Monster.cpp:524
bool CanBeLeashed() const
Returns whether the mob can be leashed.
Definition: Monster.h:78
AString m_CustomName
Definition: Monster.h:265
virtual void BroadcastMovementUpdate(const cClientHandle *a_Exclude=nullptr)
Updates clients of changes in the entity.
Definition: Entity.cpp:1878
std::enable_if< std::is_arithmetic< T >::value, C >::type CeilC(T a_Value)
Ceils a value, then casts it to C (an int by default)
Definition: Globals.h:369
void VectorToEuler(double a_X, double a_Y, double a_Z, double &a_Pan, double &a_Pitch)
Definition: Defines.h:928
std::unique_ptr< Vector3d > m_LeashToPos
Entity pos where this mob was leashed to.
Definition: Monster.h:299