Cuberite
A lightweight, fast and extensible game server for Minecraft
Slime.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 "Slime.h"
5 #include "../FastRandom.h"
6 #include "../World.h"
7 
8 
9 
10 
11 
12 cSlime::cSlime(int a_Size) :
13  Super("Slime",
14  mtSlime,
15  fmt::format(FMT_STRING("entity.{}slime.hurt"), GetSizeName(a_Size)),
16  fmt::format(FMT_STRING("entity.{}slime.death"), GetSizeName(a_Size)),
17  "",
18  0.51f * a_Size,
19  0.51f * a_Size
20  ),
21  m_Size(a_Size)
22 {
23  SetMaxHealth(static_cast<float>(a_Size * a_Size));
24  SetAttackDamage(a_Size);
25 }
26 
27 
28 
29 
30 
31 void cSlime::GetDrops(cItems & a_Drops, cEntity * a_Killer)
32 {
33  unsigned int LootingLevel = 0;
34  if (a_Killer != nullptr)
35  {
37  }
38 
39  // Only slimes with the size 1 can drop slimeballs.
40  if (m_Size == 1)
41  {
42  AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_SLIMEBALL);
43  }
44 }
45 
46 
47 
48 
49 
50 bool cSlime::Attack(std::chrono::milliseconds a_Dt)
51 {
52  if (m_Size > 1)
53  {
54  // Only slimes larger than size 1 attack a player.
55  return Super::Attack(a_Dt);
56  }
57 
58  return false;
59 }
60 
61 
62 
63 
64 
66 {
67  if (GetHealth() > 0)
68  {
69  return;
70  }
71 
72  if (m_Size != 1)
73  {
74  auto & Random = GetRandomProvider();
75  int SpawnAmount = Random.RandInt(2, 4);
76 
77  for (int i = 0; i < SpawnAmount; ++i)
78  {
79  double AddX = (i % 2 - 0.5) * m_Size / 4.0;
80  double AddZ = (i / 2 - 0.5) * m_Size / 4.0;
81 
82  // Queue slimes to be spawned after the 1 second death animation has finished playing:
84  Position = GetPosition() + Vector3d(AddX, 0.5, AddZ),
85  Yaw = Random.RandReal(360.0f),
86  Size = m_Size / 2
87  ](cWorld & a_World)
88  {
89  auto NewSlime = std::make_unique<cSlime>(Size);
90  NewSlime->SetPosition(Position);
91  NewSlime->SetYaw(Yaw);
92  a_World.SpawnMobFinalize(std::move(NewSlime));
93  });
94  }
95  }
96  Super::KilledBy(a_TDI);
97 }
98 
99 
100 
101 
102 
104 {
105  if (a_Size == 1)
106  {
107  return "small_";
108  }
109  return "";
110 }
111 
112 
113 
114 
@ E_ITEM_SLIMEBALL
Definition: BlockType.h:386
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
std::chrono::duration< signed int, std::ratio_multiply< std::chrono::milliseconds::period, std::ratio< 50 > >> cTickTime
Definition: Globals.h:364
@ mtSlime
Definition: MonsterTypes.h:61
std::string AString
Definition: StringUtils.h:11
Vector3< double > Vector3d
Definition: Vector3.h:485
Implements custom fmtlib formatting for cChunkCoords.
Definition: ChunkDef.h:104
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
Definition: Entity.h:76
cWorld * m_World
Definition: Entity.h:624
virtual cItem GetEquippedWeapon(void) const
Returns the curently equipped weapon; empty item if none.
Definition: Entity.h:333
float GetHealth(void) const
Returns the health of this entity.
Definition: Entity.h:367
void SetMaxHealth(float a_MaxHealth)
Sets the maximum value for the health.
Definition: Entity.cpp:1887
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
cEnchantments m_Enchantments
Definition: Item.h:166
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
virtual bool Attack(std::chrono::milliseconds a_Dt)
Try to perform attack returns true if attack was deemed successful (hit player, fired projectile,...
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Monster.cpp:602
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:1518
void SetAttackDamage(int a_AttackDamage)
Definition: Monster.h:128
cSlime(int a_Size)
Creates a slime of the specified size; size can be 1, 2 or 4, with 1 is the smallest and 4 is the tal...
Definition: Slime.cpp:12
int m_Size
Size of the slime, with 1 being the smallest.
Definition: Slime.h:37
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Slime.cpp:65
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Slime.cpp:31
virtual bool Attack(std::chrono::milliseconds a_Dt) override
Try to perform attack returns true if attack was deemed successful (hit player, fired projectile,...
Definition: Slime.cpp:50
static AString GetSizeName(int a_Size)
Returns the text describing the slime's size, as used by the client's resource subsystem for sounds.
Definition: Slime.cpp:103
Definition: World.h:53
void ScheduleTask(cTickTime a_DelayTicks, std::function< void(cWorld &)> a_Task)
Queues a lambda task onto the tick thread, with the specified delay.
Definition: World.cpp:2744