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", "entity.sheep.ambient", 0.9f, 1.3f),
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.emplace_back(E_BLOCK_WOOL, static_cast<char>(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 
143 void cSheep::InheritFromParents(cMonster * a_Parent1, cMonster * a_Parent2)
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);
170  return;
171  }
172  }
173  SetFurColor(GetRandomProvider().RandBool() ? Parent1->GetFurColor() : Parent2->GetFurColor());
174 }
175 
176 
177 
178 
179 
181 {
182  int Chance = GetRandomProvider().RandInt(100);
183 
184  if (Chance <= 81)
185  {
186  return E_META_WOOL_WHITE;
187  }
188  else if (Chance <= 86)
189  {
190  return E_META_WOOL_BLACK;
191  }
192  else if (Chance <= 91)
193  {
194  return E_META_WOOL_GRAY;
195  }
196  else if (Chance <= 96)
197  {
198  return E_META_WOOL_LIGHTGRAY;
199  }
200  else if (Chance <= 99)
201  {
202  return E_META_WOOL_BROWN;
203  }
204  else
205  {
206  return E_META_WOOL_PINK;
207  }
208 }
@ E_META_WOOL_CYAN
Definition: BlockType.h:1001
@ E_META_WOOL_PINK
Definition: BlockType.h:998
@ E_META_WOOL_GRAY
Definition: BlockType.h:999
@ E_META_WOOL_YELLOW
Definition: BlockType.h:996
@ E_META_WOOL_MAGENTA
Definition: BlockType.h:994
@ E_META_WOOL_RED
Definition: BlockType.h:1006
@ E_META_WOOL_PURPLE
Definition: BlockType.h:1002
@ E_META_WOOL_WHITE
Definition: BlockType.h:992
@ E_META_WOOL_LIGHTGRAY
Definition: BlockType.h:1000
@ E_META_WOOL_GREEN
Definition: BlockType.h:1005
@ E_META_WOOL_BLUE
Definition: BlockType.h:1003
@ E_META_WOOL_ORANGE
Definition: BlockType.h:993
@ E_META_WOOL_BLACK
Definition: BlockType.h:1007
@ E_META_WOOL_LIGHTBLUE
Definition: BlockType.h:995
@ E_META_WOOL_BROWN
Definition: BlockType.h:1004
@ E_META_WOOL_LIGHTGREEN
Definition: BlockType.h:997
@ E_BLOCK_WOOL
Definition: BlockType.h:45
@ E_BLOCK_GRASS
Definition: BlockType.h:12
@ E_BLOCK_DIRT
Definition: BlockType.h:13
@ E_ITEM_DYE
Definition: BlockType.h:396
@ E_ITEM_RAW_MUTTON
Definition: BlockType.h:470
@ E_ITEM_COOKED_MUTTON
Definition: BlockType.h:471
@ E_ITEM_SHEARS
Definition: BlockType.h:404
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:44
@ PARTICLE_BLOCK_BREAK
#define POSX_TOINT
Definition: Entity.h:31
#define POSZ_TOINT
Definition: Entity.h:33
#define POSY_TOINT
Definition: Entity.h:32
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
#define ARRAYCOUNT(X)
Evaluates to the number of elements in an array (compile-time!)
Definition: Globals.h:231
@ mtSheep
Definition: MonsterTypes.h:56
Definition: Chunk.h:36
static const int Height
Definition: ChunkDef.h:125
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
Definition: Entity.h:76
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
double GetPosX(void) const
Definition: Entity.h:195
cWorld * m_World
Definition: Entity.h:624
double GetPosZ(void) const
Definition: Entity.h:197
virtual bool IsOnFire(void) const
Definition: Entity.h:489
double GetPosY(void) const
Definition: Entity.h:196
virtual cItem GetEquippedWeapon(void) const
Returns the curently equipped weapon; empty item if none.
Definition: Entity.h:333
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
cWorld * GetWorld(void) const
Definition: Entity.h:190
Definition: Player.h:29
const cItem & GetEquippedItem(void) const
Definition: Player.h:162
bool IsGameModeCreative(void) const
Returns true if the player is in Creative mode, either explicitly, or by inheriting from current worl...
Definition: Player.cpp:1025
cInventory & GetInventory(void)
Definition: Player.h:156
void UseEquippedItem(short a_Damage=1)
Damage the player's equipped item by a_Damage, possibly less if the equipped item is enchanted.
Definition: Player.cpp:2033
IntType RandInt(IntType a_Min, IntType a_Max)
Return a random IntType in the range [a_Min, a_Max].
Definition: FastRandom.h:78
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
Definition: Item.h:37
cEnchantments m_Enchantments
Definition: Item.h:166
short m_ItemType
Definition: Item.h:163
short m_ItemDamage
Definition: Item.h:165
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
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
bool IsBaby(void) const
Definition: Monster.h:156
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:255
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Sheep.h:12
void SetFurColor(int a_WoolColor)
Definition: Sheep.h:43
bool IsSheared(void) const
Definition: Sheep.h:39
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Sheep.cpp:92
int m_WoolColor
Definition: Sheep.h:47
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Sheep.cpp:60
int m_TimeToStopEating
Definition: Sheep.h:48
cSheep(int a_Color=-1)
The number is the color of the sheep.
Definition: Sheep.cpp:14
static NIBBLETYPE GenerateNaturalRandomColor(void)
Generates a random color for the sheep like the vanilla server.
Definition: Sheep.cpp:180
bool m_IsSheared
Definition: Sheep.h:46
virtual void InheritFromParents(cMonster *a_Parent1, cMonster *a_Parent2) override
Called after the baby is born, allows the baby to inherit the parents' properties (color,...
Definition: Sheep.cpp:143
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
int GetFurColor(void) const
Definition: Sheep.h:42
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
virtual void BroadcastEntityAnimation(const cEntity &a_Entity, EntityAnimation a_Animation, const cClientHandle *a_Exclude=nullptr) override
BLOCKTYPE GetBlock(Vector3i a_BlockPos) const
Returns the block type at the specified position.
Definition: World.h:363
virtual void BroadcastSoundParticleEffect(const EffectID a_EffectID, Vector3i a_SrcPos, int a_Data, const cClientHandle *a_Exclude=nullptr) override
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:1806
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override
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:1743