Cuberite
A lightweight, fast and extensible game server for Minecraft
Ocelot.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 "Ocelot.h"
5 #include "../World.h"
6 #include "../Entities/Player.h"
7 #include "../Items/ItemHandler.h"
8 #include "../BoundingBox.h"
9 
10 
11 
12 
13 
14 // TODO: Ocelots should have a chance of spawning with two kittens
15 /*
16  if (!IsBaby() && GetRandomProvider().RandBool(1.0 / 7.0))
17  {
18  m_World->SpawnMob(GetPosX(), GetPosY(), GetPosZ(), m_MobType, true);
19  m_World->SpawnMob(GetPosX(), GetPosY(), GetPosZ(), m_MobType, true);
20  }
21 */
22 
24  Super("Ocelot", mtOcelot, "entity.cat.hurt", "entity.cat.death", "entity.cat.ambient", 0.6f, 0.7f),
25  m_IsSitting(false),
26  m_IsTame(false),
27  m_IsBegging(false),
28  m_CatType(ctWildOcelot),
29  m_CheckPlayerTickCount(),
30  m_OwnerName()
31 {
32 }
33 
34 
35 
36 
37 
38 void cOcelot::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
39 {
40  Super::Tick(a_Dt, a_Chunk);
41  if (!IsTicking())
42  {
43  // The base class tick destroyed us
44  return;
45  }
46 
47  if (!IsTame() && !IsBaby())
48  {
49  if (m_CheckPlayerTickCount == 23)
50  {
51  m_World->DoWithNearestPlayer(GetPosition(), 10, [&](cPlayer & a_Player) -> bool
52  {
53  cItems Items;
54  GetBreedingItems(Items);
55  if (Items.ContainsType(a_Player.GetEquippedItem().m_ItemType))
56  {
57  if (!IsBegging())
58  {
59  SetIsBegging(true);
61  }
62 
63  MoveToPosition(a_Player.GetPosition());
64  }
65  else
66  {
67  if (IsBegging())
68  {
69  SetIsBegging(false);
71  }
72  }
73 
74  return true;
75  }, true);
77  }
78  else
79  {
81  }
82  }
83 
84  if (IsTame() && !IsSitting())
85  {
87  }
88  else if (IsSitting())
89  {
91  }
92 
94 }
95 
96 
97 
98 
99 
101 {
102  Vector3d OwnerPos;
103  bool OwnerFlying = false;
104  auto Callback = [&](cPlayer & a_Player)
105  {
106  OwnerPos = a_Player.GetPosition();
107  OwnerFlying = a_Player.IsFlying();
108  return true;
109  };
110 
111  if (m_World->DoWithPlayerByUUID(m_OwnerUUID, Callback))
112  {
113  // The player is present in the world, follow him:
114  double Distance = (OwnerPos - GetPosition()).Length();
115  if (Distance > 12)
116  {
117  if (!OwnerFlying)
118  {
119  OwnerPos.y = FindFirstNonAirBlockPosition(OwnerPos.x, OwnerPos.z);
120  TeleportToCoords(OwnerPos.x, OwnerPos.y, OwnerPos.z);
121  }
122  }
123  if (Distance < 2)
124  {
126  }
127  else
128  {
129  if (!OwnerFlying)
130  {
131  MoveToPosition(OwnerPos);
132  }
133  }
134  }
135 }
136 
137 
138 
139 
140 
142 {
143  if (!IsTame())
144  {
145  if (
146  IsBegging() &&
147  ((a_Player.GetPosition() - GetPosition()).Length() <= 3)
148  )
149  {
150  cItems Items;
151  GetBreedingItems(Items);
152  if (Items.ContainsType(a_Player.GetEquippedItem().m_ItemType))
153  {
154  if (!a_Player.IsGameModeCreative())
155  {
156  a_Player.GetInventory().RemoveOneEquippedItem();
157  }
158 
159  auto & Random = GetRandomProvider();
160 
161  if (Random.RandBool(1.0 / 3.0))
162  {
163  // Taming succeeded
164  SetIsBegging(false);
165 
166  SetMaxHealth(20);
167  SetIsTame(true);
168  SetOwner(a_Player.GetName(), a_Player.GetUUID());
169  SetCatType(static_cast<eCatType>(Random.RandInt<int>(1, 3)));
171  }
172  else
173  {
174  // Taming failed
176  }
177  }
178  }
179  else
180  {
181  Super::OnRightClicked(a_Player);
182  }
183  }
184  else if (a_Player.GetUUID() == m_OwnerUUID)
185  {
186  Super::OnRightClicked(a_Player);
188  }
190 }
191 
192 
193 
194 
195 
196 bool cOcelot::IsCatSittingOnBlock(cWorld * a_World, Vector3d a_BlockPosition)
197 {
198  return a_World->ForEachEntityInBox(
199  cBoundingBox(Vector3d(a_BlockPosition.x, a_BlockPosition.y + 1, a_BlockPosition.z), 1),
200  [=](cEntity & a_Entity)
201  {
202  return (
203  (a_Entity.GetEntityType() == cEntity::etMonster) &&
204  (static_cast<cMonster &>(a_Entity).GetMobType() == eMonsterType::mtOcelot) &&
205  (static_cast<cOcelot &>(a_Entity).IsSitting())
206  );
207  }
208  );
209 }
210 
211 
212 
213 
214 
216 {
217  if (a_TDI.DamageType == dtFalling)
218  {
219  return false;
220  }
221 
222  return Super::DoTakeDamage(a_TDI);
223 }
@ dtFalling
Definition: Defines.h:249
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
@ mtOcelot
Definition: MonsterTypes.h:43
Vector3< double > Vector3d
Definition: Vector3.h:485
unsigned char Distance(const BlockState Block)
Represents two sets of coords, minimum and maximum for each direction.
Definition: BoundingBox.h:24
Definition: Chunk.h:36
eDamageType DamageType
Definition: Entity.h:61
Definition: Entity.h:76
virtual void TeleportToCoords(double a_PosX, double a_PosY, double a_PosZ)
Teleports to the coordinates specified.
Definition: Entity.cpp:1953
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
cWorld * m_World
Definition: Entity.h:624
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
Definition: Player.h:29
const AString & GetName(void) const
Definition: Player.cpp:1299
const cUUID & GetUUID(void) const
Returns the UUID that has been read from the client, or nil if not available.
Definition: Player.cpp:2431
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
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
short m_ItemType
Definition: Item.h:163
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
bool ContainsType(const cItem &a_Item)
Definition: Item.cpp:787
virtual void MoveToPosition(const Vector3d &a_Position)
Engage pathfinder and tell it to calculate a path to a given position, and move the mob accordingly.
Definition: Monster.cpp:245
int FindFirstNonAirBlockPosition(double a_PosX, double a_PosZ)
Finds the lowest non-air block position (not the highest, as cWorld::GetHeight does) If current Y is ...
Definition: Monster.cpp:543
bool IsBaby(void) const
Definition: Monster.h:156
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:255
bool IsBegging(void) const
Definition: Ocelot.h:42
static bool IsCatSittingOnBlock(cWorld *a_World, Vector3d a_BlockPosition)
Returns true if there's a cat sitting above the given position.
Definition: Ocelot.cpp:196
virtual void TickFollowPlayer()
Definition: Ocelot.cpp:100
void SetIsTame(bool a_IsTame)
Definition: Ocelot.h:49
void SetOwner(const AString &a_NewOwnerName, const cUUID &a_NewOwnerUUID)
Definition: Ocelot.h:51
void SetIsBegging(bool a_IsBegging)
Definition: Ocelot.h:50
cOcelot(void)
Definition: Ocelot.cpp:23
bool IsSitting(void) const override
Definition: Ocelot.h:40
virtual void GetBreedingItems(cItems &a_Items) override
Returns the items that make the animal breed - this is usually the same as the ones that make the ani...
Definition: Ocelot.h:33
void SetCatType(eCatType a_CatType)
Definition: Ocelot.h:56
cUUID m_OwnerUUID
Definition: Ocelot.h:70
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Ocelot.cpp:141
int m_CheckPlayerTickCount
Only check for a nearby player holding the breeding items every 23 ticks.
Definition: Ocelot.h:68
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
When hit by someone, run away.
Definition: Ocelot.cpp:215
bool IsTame(void) const override
Definition: Ocelot.h:41
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Ocelot.cpp:38
void SetIsSitting(bool a_IsSitting)
Definition: Ocelot.h:48
eCatType
Definition: Ocelot.h:19
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.
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
When hit by someone, run away.
T x
Definition: Vector3.h:17
T y
Definition: Vector3.h:17
T z
Definition: Vector3.h:17
Definition: World.h:53
bool DoWithPlayerByUUID(const cUUID &a_PlayerUUID, cPlayerListCallback a_Callback)
Finds the player over his uuid and calls the callback.
Definition: World.cpp:2339
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
virtual bool ForEachEntityInBox(const cBoundingBox &a_Box, cEntityCallback a_Callback) override
Calls the callback for each entity that has a nonempty intersection with the specified boundingbox.
Definition: World.cpp:2445
bool DoWithNearestPlayer(Vector3d a_Pos, double a_RangeLimit, cPlayerListCallback a_Callback, bool a_CheckLineOfSight=true, bool a_IgnoreSpectator=true)
Calls the callback for nearest player for given position, Returns false if player not found,...
Definition: World.cpp:2356