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  return;
89 }
90 
91 
92 
93 
94 
95 void cLeashKnot::GetDrops(cItems & a_Items, cEntity * a_Killer)
96 {
97  if ((a_Killer != nullptr) && a_Killer->IsPlayer() && !static_cast<cPlayer *>(a_Killer)->IsGameModeCreative())
98  {
99  a_Items.push_back(cItem(E_ITEM_LEASH));
100  }
101 }
102 
103 
104 
105 
106 
107 void cLeashKnot::SpawnOn(cClientHandle & a_ClientHandle)
108 {
109  super::SpawnOn(a_ClientHandle);
110  a_ClientHandle.SendSpawnObject(*this, 77, GetProtocolFacing(), static_cast<Byte>(GetYaw()), static_cast<Byte>(GetPitch()));
111  a_ClientHandle.SendEntityMetadata(*this);
112 }
113 
114 
115 
116 
117 
118 void cLeashKnot::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
119 {
120  m_TicksAlive++;
121 
122  if ((m_TicksAlive % TICK_STEP) != 0)
123  {
124  return;
125  }
126 
128  {
130 
131  if (m_TicksToSelfDestroy <= 0)
132  {
133  Destroy();
134  m_World->BroadcastSoundEffect("entity.leashknot.break", GetPosition(), 1, 1);
135  }
136  }
137 }
138 
139 
140 
141 
142 
144 {
145  cLeashKnot * LeashKnot = nullptr;
146  a_WorldInterface.ForEachEntityInBox(cBoundingBox(a_BlockPos, 0.5, 1), [&](cEntity & a_Entity)
147  {
148  if (a_Entity.IsLeashKnot())
149  {
150  LeashKnot = static_cast<cLeashKnot *>(&a_Entity);
151  return true;
152  }
153  return false;
154  }
155  );
156 
157  return LeashKnot;
158 }
159 
160 
161 
162 
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:2729
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: LeashKnot.cpp:83
double GetPitch(void) const
Definition: Entity.h:210
Byte GetProtocolFacing() const
Returns the direction in which the entity is facing.
Definition: HangingEntity.h:38
eEntityType GetEntityType(void) const
Definition: Entity.h:168
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:95
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
virtual void KilledBy(TakeDamageInfo &a_TDI)
Called when the health drops below zero.
Definition: Entity.cpp:791
cWorld * m_World
Definition: Entity.h:620
Definition: Player.h:27
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
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...
void SendSpawnObject(const cEntity &a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch)
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: LeashKnot.cpp:118
bool m_ShouldSelfDestroy
When a fence is destroyed, the knot on it gets destroyed after a while.
Definition: LeashKnot.h:36
Represents two sets of coords, minimum and maximum for each direction.
Definition: BoundingBox.h:23
cLeashKnot(eBlockFace a_BlockFace, Vector3d a_Pos)
Definition: LeashKnot.cpp:17
Definition: Chunk.h:49
static cLeashKnot * FindKnotAtPos(cWorldInterface &a_WorldInterface, Vector3i a_BlockPos)
Returns the leash knot entity representing the knot at the specified position.
Definition: LeashKnot.cpp:143
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: LeashKnot.cpp:28
virtual void Destroy(bool a_ShouldBroadcast=true)
Destroys the entity and schedules it for memory freeing; if a_ShouldBroadcast is set to true...
Definition: Entity.cpp:219
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override
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:107
int m_TicksToSelfDestroy
Definition: LeashKnot.h:37
virtual void OnRightClicked(cPlayer &a_Player)
Called when the specified player right-clicks this entity.
Definition: Entity.h:523
double GetYaw(void) const
Definition: Entity.h:209
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...
eBlockFace
Block face constants, used in PlayerDigging and PlayerBlockPlacement packets and bbox collision calc...
Definition: Defines.h:29
bool IsPlayer(void) const
Definition: Entity.h:171
Definition: Entity.h:73
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:307
#define TICK_STEP
Definition: LeashKnot.cpp:11
void Unleash(bool a_ShouldDropLeashPickup)
Unleash the monster.
Definition: Monster.cpp:1475
long int m_TicksAlive
The number of ticks this entity has been alive for.
Definition: Entity.h:667
Definition: Item.h:36
void SendEntityMetadata(const cEntity &a_Entity)
bool IsLeashKnot(void) const
Definition: Entity.h:183
UInt32 GetUniqueID(void) const
Definition: Entity.h:261
cWorld * GetWorld(void) const
Definition: Entity.h:201
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234