Cuberite
A lightweight, fast and extensible game server for Minecraft
Sheep.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 "Sheep.h"
5 #include "../Entities/Player.h"
6 #include "../World.h"
7 #include "../EffectID.h"
8 #include "../FastRandom.h"
9 
10 
11 
12 
13 
14 cSheep::cSheep(int a_Color) :
15  super("Sheep", mtSheep, "entity.sheep.hurt", "entity.sheep.death", 0.6, 1.3),
16  m_IsSheared(false),
17  m_WoolColor(a_Color),
18  m_TimeToStopEating(-1)
19 {
20  // Generate random wool color.
21  if (m_WoolColor == -1)
22  {
24  }
25 
26  if ((m_WoolColor < 0) || (m_WoolColor > 15))
27  {
28  m_WoolColor = 0;
29  }
30 }
31 
32 
33 
34 
35 
36 void cSheep::GetDrops(cItems & a_Drops, cEntity * a_Killer)
37 {
38  if (IsBaby())
39  {
40  return; // Babies don't drop items
41  }
42 
43  if (!m_IsSheared)
44  {
45  a_Drops.push_back(cItem(E_BLOCK_WOOL, 1, static_cast<short>(m_WoolColor)));
46  }
47 
48  unsigned int LootingLevel = 0;
49  if (a_Killer != nullptr)
50  {
52  }
53  AddRandomDropItem(a_Drops, 1, 3 + LootingLevel, IsOnFire() ? E_ITEM_COOKED_MUTTON : E_ITEM_RAW_MUTTON);
54 }
55 
56 
57 
58 
59 
61 {
62  super::OnRightClicked(a_Player);
63 
64  const cItem & EquippedItem = a_Player.GetEquippedItem();
65  if ((EquippedItem.m_ItemType == E_ITEM_SHEARS) && !IsSheared() && !IsBaby())
66  {
67  m_IsSheared = true;
69  a_Player.UseEquippedItem();
70 
71  cItems Drops;
72  char NumDrops = GetRandomProvider().RandInt<char>(1, 3);
73  Drops.emplace_back(E_BLOCK_WOOL, NumDrops, static_cast<short>(m_WoolColor));
74  m_World->SpawnItemPickups(Drops, GetPosX(), GetPosY(), GetPosZ(), 10);
75  m_World->BroadcastSoundEffect("entity.sheep.shear", GetPosition(), 1.0f, 1.0f);
76  }
77  else if ((EquippedItem.m_ItemType == E_ITEM_DYE) && (m_WoolColor != 15 - EquippedItem.m_ItemDamage))
78  {
79  m_WoolColor = 15 - EquippedItem.m_ItemDamage;
80  if (!a_Player.IsGameModeCreative())
81  {
83  }
85  }
86 }
87 
88 
89 
90 
91 
92 void cSheep::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
93 {
94  super::Tick(a_Dt, a_Chunk);
95  if (!IsTicking())
96  {
97  // The base class tick destroyed us
98  return;
99  }
100  int PosX = POSX_TOINT;
101  int PosY = POSY_TOINT - 1;
102  int PosZ = POSZ_TOINT;
103 
104  if ((PosY <= 0) || (PosY >= cChunkDef::Height))
105  {
106  return;
107  }
108 
109  if (m_TimeToStopEating > 0)
110  {
113 
114  if (m_TimeToStopEating == 0)
115  {
116  if (m_World->GetBlock(PosX, PosY, PosZ) == E_BLOCK_GRASS) // Make sure grass hasn't been destroyed in the meantime
117  {
118  // The sheep ate the grass so we change it to dirt
119  m_World->SetBlock(PosX, PosY, PosZ, E_BLOCK_DIRT, 0);
121  m_IsSheared = false;
123  }
124  }
125  }
126  else
127  {
128  if (GetRandomProvider().RandBool(1.0 / 600.0))
129  {
130  if (m_World->GetBlock(PosX, PosY, PosZ) == E_BLOCK_GRASS)
131  {
133  m_TimeToStopEating = 40;
134  }
135  }
136  }
137 }
138 
139 
140 
141 
142 
144 {
145  static const struct
146  {
147  short Parent1, Parent2, Child;
148  } ColorInheritance[] =
149  {
159  };
160  cSheep * Parent1 = static_cast<cSheep *>(a_Parent1);
161  cSheep * Parent2 = static_cast<cSheep *>(a_Parent2);
162  for (size_t i = 0; i < ARRAYCOUNT(ColorInheritance); i++)
163  {
164  if (
165  ((Parent1->GetFurColor() == ColorInheritance[i].Parent1) && (Parent2->GetFurColor() == ColorInheritance[i].Parent2)) ||
166  ((Parent1->GetFurColor() == ColorInheritance[i].Parent2) && (Parent2->GetFurColor() == ColorInheritance[i].Parent1))
167  )
168  {
169  SetFurColor(ColorInheritance[i].Child);
171  return;
172  }
173  }
174  SetFurColor(GetRandomProvider().RandBool() ? Parent1->GetFurColor() : Parent2->GetFurColor());
176 }
177 
178 
179 
180 
181 
183 {
184  int Chance = GetRandomProvider().RandInt(100);
185 
186  if (Chance <= 81)
187  {
188  return E_META_WOOL_WHITE;
189  }
190  else if (Chance <= 86)
191  {
192  return E_META_WOOL_BLACK;
193  }
194  else if (Chance <= 91)
195  {
196  return E_META_WOOL_GRAY;
197  }
198  else if (Chance <= 96)
199  {
200  return E_META_WOOL_LIGHTGRAY;
201  }
202  else if (Chance <= 99)
203  {
204  return E_META_WOOL_BROWN;
205  }
206  else
207  {
208  return E_META_WOOL_PINK;
209  }
210 }
double GetPosY(void) const
Definition: Entity.h:207
double GetPosX(void) const
Definition: Entity.h:206
bool IsSheared(void) const
Definition: Sheep.h:39
BLOCKTYPE GetBlock(Vector3i a_BlockPos)
Returns the block type at the specified position.
Definition: World.h:416
short m_ItemDamage
Definition: Item.h:211
int m_TimeToStopEating
Definition: Sheep.h:48
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
MTRand & GetRandomProvider()
Returns the current thread&#39;s random number source.
Definition: FastRandom.cpp:20
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
cWorld * m_World
Definition: Entity.h:620
const cItem & GetEquippedItem(void) const
Definition: Player.h:142
Definition: Player.h:27
virtual bool IsOnFire(void) const
Definition: Entity.h:486
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.
bool IsBaby(void) const
Definition: Monster.h:150
virtual void InheritFromParents(cPassiveMonster *a_Parent1, cPassiveMonster *a_Parent2) override
Called after the baby is born, allows the baby to inherit the parents&#39; properties (color...
Definition: Sheep.cpp:143
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
static NIBBLETYPE GenerateNaturalRandomColor(void)
Generates a random color for the sheep like the vanilla server.
Definition: Sheep.cpp:182
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Sheep.cpp:92
cSheep(int a_Color=-1)
The number is the color of the sheep.
Definition: Sheep.cpp:14
Definition: Chunk.h:49
Definition: Sheep.h:10
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
static const int Height
Definition: ChunkDef.h:135
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override
int GetFurColor(void) const
Definition: Sheep.h:42
cEnchantments m_Enchantments
Definition: Item.h:212
IntType RandInt(IntType a_Min, IntType a_Max)
Return a random IntType in the range [a_Min, a_Max].
Definition: FastRandom.h:78
virtual void BroadcastSoundParticleEffect(const EffectID a_EffectID, Vector3i a_SrcPos, int a_Data, const cClientHandle *a_Exclude=nullptr) override
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2201
void SetFurColor(int a_WoolColor)
Definition: Sheep.h:43
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Sheep.cpp:36
#define POSX_TOINT
Definition: Entity.h:29
#define POSY_TOINT
Definition: Entity.h:30
bool m_IsSheared
Definition: Sheep.h:46
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
void UseEquippedItem(short a_Damage=1)
Damage the player&#39;s equipped item by a_Damage, possibly less if the equipped item is enchanted...
Definition: Player.cpp:2397
virtual cItem GetEquippedWeapon(void) const
Returns the curently equipped weapon; empty item if none.
Definition: Entity.h:346
Definition: Entity.h:73
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:307
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
#define POSZ_TOINT
Definition: Entity.h:31
short m_ItemType
Definition: Item.h:209
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:254
cInventory & GetInventory(void)
Definition: Player.h:136
void SetBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
Sets the block at the specified coords to the specified value.
Definition: World.cpp:1873
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:290
Definition: Item.h:36
int m_WoolColor
Definition: Sheep.h:47
virtual void BroadcastEntityStatus(const cEntity &a_Entity, Int8 a_Status, const cClientHandle *a_Exclude=nullptr) override
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 OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Sheep.cpp:60