Cuberite
A lightweight, fast and extensible game server for Minecraft
Silverfish.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h"
3 
4 #include "Silverfish.h"
5 
6 #include "../World.h"
7 #include "../Chunk.h"
8 #include "../Blocks/BlockHandler.h"
9 #include "../Blocks/BlockInfested.h"
10 
11 
12 
13 
14 
16  Super("Silverfish", mtSilverfish, "entity.silverfish.hurt", "entity.silverfish.death", "entity.silverfish.ambient", 0.4f, 0.3f)
17 {
18 }
19 
20 
21 
22 
23 
25 {
26  // Call on our brethren to attack!
27  // TODO: stop this if /gamerule mobGriefing is set to false
28 
29  // If the entity didn't take any damage, bail:
30  if (!Super::DoTakeDamage(a_TDI))
31  {
32  return false;
33  }
34 
35  // Or, conversely took lethal damage, bail:
36  if (m_Health <= 0)
37  {
38  return true;
39  }
40 
41  if (a_TDI.Attacker == nullptr)
42  {
43  if ((a_TDI.DamageType != dtPoison) && (a_TDI.DamageType != dtPotionOfHarming))
44  {
45  // Bail if attacker doesn't exist and it wasn't a splash potion:
46  return true;
47  }
48  }
49  else if (!a_TDI.Attacker->IsPlayer())
50  {
51  // Bail if it wasn't a player attack:
52  return true;
53  }
54 
55  auto & Random = GetRandomProvider();
56 
57  // Tries to spawn another Silverfish, returning if the search should continue.
58  auto CheckInfested = [this, &Random, Position = GetPosition().Floor()](const Vector3i Offset) mutable
59  {
60  const auto Block = Position + Offset;
62  {
64  return Random.RandBool();
65  }
66  return false;
67  };
68 
69  // Search the faces of an increasingly large cube (so the positions closest get looked at first)
70  // of min 3, max 10, for infested blocks and spawn additional reinforcements:
71  for (int CubeSideLength = 3; CubeSideLength <= 10; CubeSideLength += 2)
72  {
73  const int HalfSide = CubeSideLength / 2;
74 
75  for (int OffsetX = -HalfSide; OffsetX <= HalfSide; OffsetX++)
76  {
77  for (int OffsetZ = -HalfSide; OffsetZ <= HalfSide; OffsetZ++)
78  {
79  if (CheckInfested({ OffsetX, +HalfSide, OffsetZ }) || CheckInfested({ OffsetX, -HalfSide, OffsetZ }))
80  {
81  return true;
82  }
83  }
84  }
85 
86  for (int OffsetX = -HalfSide; OffsetX <= HalfSide; OffsetX++)
87  {
88  for (int OffsetY = -HalfSide + 1; OffsetY <= HalfSide - 1; OffsetY++)
89  {
90  if (CheckInfested({ OffsetX, OffsetY, +HalfSide }) || CheckInfested({ OffsetX, OffsetY, -HalfSide }))
91  {
92  return true;
93  }
94  }
95  }
96 
97  for (int OffsetZ = -HalfSide + 1; OffsetZ <= HalfSide - 1; OffsetZ++)
98  {
99  for (int OffsetY = -HalfSide + 1; OffsetY <= HalfSide - 1; OffsetY++)
100  {
101  if (CheckInfested({ +HalfSide, OffsetY, OffsetZ }) || CheckInfested({ -HalfSide, OffsetY, OffsetZ }))
102  {
103  return true;
104  }
105  }
106  }
107  }
108 
109  return true;
110 }
@ E_BLOCK_SILVERFISH_EGG
Definition: BlockType.h:112
@ dtPoison
Definition: Defines.h:285
@ dtPotionOfHarming
Definition: Defines.h:261
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
@ mtSilverfish
Definition: MonsterTypes.h:58
cEntity * Attacker
Definition: Entity.h:62
eDamageType DamageType
Definition: Entity.h:61
Definition: Entity.h:76
float m_Health
Definition: Entity.h:584
bool IsPlayer(void) const
Definition: Entity.h:160
cWorld * m_World
Definition: Entity.h:624
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Monster.cpp:572
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Silverfish.cpp:24
Vector3< int > Floor(void) const
Returns a new Vector3i with coords set to std::floor() of this vector's coords.
Definition: Vector3.h:177
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
bool DigBlock(Vector3i a_BlockPos, const cEntity *a_Digger=nullptr)
Replaces the specified block with air, and calls the OnBroken block handler.
Definition: World.cpp:2069