Cuberite
A lightweight, fast and extensible game server for Minecraft
LeashKnot.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 "LeashKnot.h"
5 #include "Player.h"
6 #include "../Mobs/Monster.h"
7 #include "../BoundingBox.h"
8 #include "../ClientHandle.h"
9 
10 // Ticks to wait in Tick function to optimize calculations
11 #define TICK_STEP 10
12 
13 
14 
15 
16 
18  Super(etLeashKnot, a_BlockFace, a_Pos),
19  m_ShouldSelfDestroy(false),
20  m_TicksToSelfDestroy(20 * 1)
21 {
22 }
23 
24 
25 
26 
27 
29 {
30  Super::OnRightClicked(a_Player);
31 
32  TiePlayersLeashedMobs(a_Player, true);
33 
34  GetWorld()->BroadcastEntityMetadata(*this); // Update clients
35 }
36 
37 
38 
39 
40 
41 void cLeashKnot::TiePlayersLeashedMobs(cPlayer & a_Player, bool a_ShouldBroadcast)
42 {
43  // Check leashed nearby mobs to tie them to this knot
44  // taking world from player (instead from this) because this can be called before entity was initialized
45  a_Player.GetWorld()->ForEachEntityInBox(cBoundingBox(GetPosition(), 8, 8, -4), [&](cEntity & a_Entity)
46  {
47  // If the entity is not a monster skip it
48  if (a_Entity.GetEntityType() != cEntity::eEntityType::etMonster)
49  {
50  return false;
51  }
52 
53  auto & PotentialLeashed = static_cast<cMonster&>(a_Entity);
54 
55  // If can't be leashed skip it
56  if (!PotentialLeashed.CanBeLeashed())
57  {
58  return false;
59  }
60 
61  // If it's not leashed to the player skip it
62  if (
63  !PotentialLeashed.IsLeashed() ||
64  !PotentialLeashed.GetLeashedTo()->IsPlayer() ||
65  (PotentialLeashed.GetLeashedTo()->GetUniqueID() != a_Player.GetUniqueID())
66  )
67  {
68  return false;
69  }
70 
71  // All conditions met, unleash from player and leash to fence
72  PotentialLeashed.Unleash(false, false);
73  PotentialLeashed.LeashTo(*this, a_ShouldBroadcast);
74  return false;
75  }
76  );
77 }
78 
79 
80 
81 
82 
84 {
85  Super::KilledBy(a_TDI);
86  m_World->BroadcastSoundEffect("entity.leashknot.break", GetPosition(), 1, 1);
87  Destroy();
88 }
89 
90 
91 
92 
93 
94 void cLeashKnot::GetDrops(cItems & a_Items, cEntity * a_Killer)
95 {
96  if ((a_Killer != nullptr) && a_Killer->IsPlayer() && !static_cast<cPlayer *>(a_Killer)->IsGameModeCreative())
97  {
98  a_Items.emplace_back(E_ITEM_LEASH);
99  }
100 }
101 
102 
103 
104 
105 
106 void cLeashKnot::SpawnOn(cClientHandle & a_ClientHandle)
107 {
108  Super::SpawnOn(a_ClientHandle);
109  a_ClientHandle.SendSpawnEntity(*this);
110  a_ClientHandle.SendEntityMetadata(*this);
111 }
112 
113 
114 
115 
116 
117 void cLeashKnot::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
118 {
119  m_TicksAlive++;
120 
121  if ((m_TicksAlive % TICK_STEP) != 0)
122  {
123  return;
124  }
125 
127  {
129 
130  if (m_TicksToSelfDestroy <= 0)
131  {
132  Destroy();
133  m_World->BroadcastSoundEffect("entity.leashknot.break", GetPosition(), 1, 1);
134  }
135  }
136 }
137 
138 
139 
140 
141 
143 {
144  cLeashKnot * LeashKnot = nullptr;
145  a_WorldInterface.ForEachEntityInBox(cBoundingBox(a_BlockPos, 0.5, 1), [&](cEntity & a_Entity)
146  {
147  if (a_Entity.IsLeashKnot())
148  {
149  LeashKnot = static_cast<cLeashKnot *>(&a_Entity);
150  return true;
151  }
152  return false;
153  }
154  );
155 
156  return LeashKnot;
157 }
158 
159 
160 
161 
@ E_ITEM_LEASH
Definition: BlockType.h:467
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc.
Definition: Defines.h:38
#define TICK_STEP
Definition: LeashKnot.cpp:11
virtual bool ForEachEntityInBox(const cBoundingBox &a_Box, cEntityCallback a_Callback)=0
Calls the callback for each entity that has a nonempty intersection with the specified boundingbox.
Represents two sets of coords, minimum and maximum for each direction.
Definition: BoundingBox.h:24
Definition: Chunk.h:36
void SendSpawnEntity(const cEntity &a_Entity)
void SendEntityMetadata(const cEntity &a_Entity)
Definition: Entity.h:76
bool IsPlayer(void) const
Definition: Entity.h:160
cWorld * m_World
Definition: Entity.h:624
UInt32 GetUniqueID(void) const
Definition: Entity.h:253
void Destroy()
Destroys the entity, schedules it for memory freeing and broadcasts the DestroyEntity packet.
Definition: Entity.cpp:243
eEntityType GetEntityType(void) const
Definition: Entity.h:156
bool IsLeashKnot(void) const
Definition: Entity.h:172
virtual void OnRightClicked(cPlayer &a_Player)
Called when the specified player right-clicks this entity.
Definition: Entity.h:524
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
long int m_TicksAlive
The number of ticks this entity has been alive for.
Definition: Entity.h:667
cWorld * GetWorld(void) const
Definition: Entity.h:190
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
virtual void SpawnOn(cClientHandle &a_ClientHandle) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
virtual void SpawnOn(cClientHandle &a_ClientHandle) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
Definition: LeashKnot.cpp:106
void TiePlayersLeashedMobs(cPlayer &a_Player, bool a_ShouldBroadCast)
Looks for mobs leashed to a player and ties them to this knot.
Definition: LeashKnot.cpp:41
bool m_ShouldSelfDestroy
When a fence is destroyed, the knot on it gets destroyed after a while.
Definition: LeashKnot.h:36
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: LeashKnot.cpp:28
cLeashKnot(eBlockFace a_BlockFace, Vector3d a_Pos)
Definition: LeashKnot.cpp:17
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: LeashKnot.cpp:117
virtual void GetDrops(cItems &a_Items, cEntity *a_Killer) override
Returns the list of drops for this pawn when it is killed.
Definition: LeashKnot.cpp:94
int m_TicksToSelfDestroy
Definition: LeashKnot.h:37
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: LeashKnot.cpp:83
static cLeashKnot * FindKnotAtPos(cWorldInterface &a_WorldInterface, Vector3i a_BlockPos)
Returns the leash knot entity representing the knot at the specified position.
Definition: LeashKnot.cpp:142
Definition: Player.h:29
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
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
void Unleash(bool a_ShouldDropLeashPickup)
Unleash the monster.
Definition: Monster.cpp:1778
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, 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
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override