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  Printf("entity.%sslime.hurt", GetSizeName(a_Size).c_str()),
16  Printf("entity.%sslime.death", GetSizeName(a_Size).c_str()),
17  0.6 * a_Size,
18  0.6 * a_Size
19  ),
20  m_Size(a_Size)
21 {
22  SetMaxHealth(static_cast<float>(a_Size * a_Size));
23  SetAttackDamage(a_Size);
24 }
25 
26 
27 
28 
29 
30 void cSlime::GetDrops(cItems & a_Drops, cEntity * a_Killer)
31 {
32  unsigned int LootingLevel = 0;
33  if (a_Killer != nullptr)
34  {
36  }
37 
38  // Only slimes with the size 1 can drop slimeballs.
39  if (m_Size == 1)
40  {
41  AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_SLIMEBALL);
42  }
43 }
44 
45 
46 
47 
48 
49 bool cSlime::Attack(std::chrono::milliseconds a_Dt)
50 {
51  if (m_Size > 1)
52  {
53  // Only slimes larger than size 1 attack a player.
54  return super::Attack(a_Dt);
55  }
56 
57  return false;
58 }
59 
60 
61 
62 
63 
65 {
66  if (GetHealth() > 0)
67  {
68  return;
69  }
70 
71  if (m_Size != 1)
72  {
73  auto & Random = GetRandomProvider();
74  int SpawnAmount = Random.RandInt(2, 4);
75 
76  for (int i = 0; i < SpawnAmount; ++i)
77  {
78  double AddX = (i % 2 - 0.5) * m_Size / 4.0;
79  double AddZ = (i / 2 - 0.5) * m_Size / 4.0;
80 
81  auto NewSlime = cpp14::make_unique<cSlime>(m_Size / 2);
82  NewSlime->SetPosition(GetPosX() + AddX, GetPosY() + 0.5, GetPosZ() + AddZ);
83  NewSlime->SetYaw(Random.RandReal(360.0f));
84  m_World->SpawnMobFinalize(std::move(NewSlime));
85  }
86  }
87  super::KilledBy(a_TDI);
88 }
89 
90 
91 
92 
93 
95 {
96  if (a_Size == 1)
97  {
98  return "small_";
99  }
100  return "";
101 }
102 
103 
104 
105 
double GetPosY(void) const
Definition: Entity.h:207
double GetPosX(void) const
Definition: Entity.h:206
virtual bool Attack(std::chrono::milliseconds a_Dt)
Try to perform attack returns true if attack was deemed successful (hit player, fired projectile...
MTRand & GetRandomProvider()
Returns the current thread&#39;s random number source.
Definition: FastRandom.cpp:20
cWorld * m_World
Definition: Entity.h:620
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
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Monster.cpp:593
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
void SetMaxHealth(float a_MaxHealth)
Sets the maximum value for the health.
Definition: Entity.cpp:1798
cEnchantments m_Enchantments
Definition: Item.h:212
void SetAttackDamage(int a_AttackDamage)
Definition: Monster.h:122
AString & Printf(AString &str, const char *format, fmt::ArgList args)
Output the formatted text into the string.
Definition: StringUtils.cpp:55
float GetHealth(void) const
Returns the health of this entity.
Definition: Entity.h:380
static AString GetSizeName(int a_Size)
Returns the text describing the slime&#39;s size, as used by the client&#39;s resource subsystem for sounds...
Definition: Slime.cpp:94
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:30
UInt32 SpawnMobFinalize(std::unique_ptr< cMonster > a_Monster)
Wraps cEntity::Initialize, doing Monster-specific things before spawning the monster.
Definition: World.cpp:3211
std::string AString
Definition: StringUtils.h:13
virtual cItem GetEquippedWeapon(void) const
Returns the curently equipped weapon; empty item if none.
Definition: Entity.h:346
Definition: Entity.h:73
double GetPosZ(void) const
Definition: Entity.h:208
int m_Size
Size of the slime, with 1 being the smallest.
Definition: Slime.h:36
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Slime.cpp:64
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
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:49