Cuberite
A lightweight, fast and extensible game server for Minecraft
ExpOrb.cpp
Go to the documentation of this file.
1 #include "Globals.h"
2 
3 #include "ExpOrb.h"
4 #include "Player.h"
5 #include "../ClientHandle.h"
6 
7 
8 cExpOrb::cExpOrb(Vector3d a_Pos, int a_Reward):
9  Super(etExpOrb, a_Pos, 0.5f, 0.5f),
10  m_Reward(a_Reward),
11  m_Timer(0)
12 {
13  SetMaxHealth(5);
14  SetHealth(5);
15  SetGravity(-16);
16  SetAirDrag(0.02f);
17 }
18 
19 
20 
21 
22 
24 {
25  a_Client.SendExperienceOrb(*this);
26  m_bDirtyOrientation = false;
27  m_bDirtyHead = false;
28 }
29 
30 
31 
32 
33 
34 void cExpOrb::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
35 {
36  DetectCacti();
37  m_TicksAlive++;
38 
39  // Find closest player within 6.5 meter (slightly increase detect range to have same effect in client)
40  bool FoundPlayer = m_World->DoWithNearestPlayer(GetPosition(), 6.5, [&](cPlayer & a_Player) -> bool
41  {
42  Vector3f a_PlayerPos(a_Player.GetPosition());
43  a_PlayerPos.y += 0.8f;
44  Vector3f a_Distance = a_PlayerPos - GetPosition();
45  double Distance = a_Distance.Length();
46 
47  if (Distance < 0.7f)
48  {
49  a_Player.DeltaExperience(m_Reward);
50 
51  m_World->BroadcastSoundEffect("entity.experience_orb.pickup", GetPosition(), 0.5f, (0.75f + (static_cast<float>((GetUniqueID() * 23) % 32)) / 64));
52  Destroy();
53  return true;
54  }
55 
56  // Experience orb will "float" or glide toward the player up to a distance of 6 blocks.
57  // speeding up as they get nearer to the player, Speed range 6 - 12 m per second, accelerate 60 m per second^2
58  Vector3d SpeedDelta(a_Distance);
59  SpeedDelta.Normalize();
60  SpeedDelta *= 3;
61 
62  Vector3d CurrentSpeed = GetSpeed();
63  CurrentSpeed += SpeedDelta;
64  if (CurrentSpeed.Length() > 12)
65  {
66  CurrentSpeed.Normalize();
67  CurrentSpeed *= 12;
68  }
69 
70  SetSpeed(CurrentSpeed);
71  m_Gravity = 0;
72 
73  return true;
74  }, false, true); // Don't check line of sight, ignore spectator mode player
75 
76  if (!FoundPlayer)
77  {
78  m_Gravity = -16;
79  }
80 
81  HandlePhysics(a_Dt, a_Chunk);
83 
84  m_Timer += a_Dt;
85  if (m_Timer >= std::chrono::minutes(5))
86  {
87  Destroy();
88  }
89 }
90 
91 
92 
93 
94 
96 {
97  if (a_TDI.DamageType == dtCactusContact)
98  {
99  Destroy();
100  return true;
101  }
102 
103  return Super::DoTakeDamage(a_TDI);
104 }
105 
106 
107 
108 
109 
110 std::vector<int> cExpOrb::Split(int a_Reward)
111 {
112  const static std::array<int, 11> BaseValue = {{1, 3, 7, 17, 37, 73, 149, 307, 617, 1237, 2477}};
113 
114  std::vector<int> Rewards;
115  size_t Index = BaseValue.size() - 1; // Last one
116 
117  while (a_Reward > 0)
118  {
119  while (a_Reward < BaseValue[Index])
120  {
121  Index--;
122  }
123 
124  a_Reward -= BaseValue[Index];
125  Rewards.push_back(BaseValue[Index]);
126  }
127 
128  return Rewards;
129 }
130 
131 
@ dtCactusContact
Definition: Defines.h:253
unsigned char Distance(const BlockState Block)
Definition: Chunk.h:36
void SendExperienceOrb(const cExpOrb &a_ExpOrb)
eDamageType DamageType
Definition: Entity.h:61
Definition: Entity.h:76
const Vector3d & GetSpeed(void) const
Exported in ManualBindings.
Definition: Entity.h:300
bool m_bDirtyOrientation
Stores whether our yaw / pitch / roll (body orientation) has been set manually.
Definition: Entity.h:597
void SetHealth(float a_Health)
Sets the health of this entity; doesn't broadcast any hurt animation.
Definition: Entity.cpp:900
float m_Gravity
Stores gravity that is applied to an entity every tick For realistic effects, this should be negative...
Definition: Entity.h:608
void SetGravity(float a_Gravity)
Definition: Entity.h:282
void SetSpeed(double a_SpeedX, double a_SpeedY, double a_SpeedZ)
Sets the speed of the entity, measured in m / sec.
Definition: Entity.cpp:2157
virtual void DetectCacti(void)
Detects the time for application of cacti damage.
Definition: Entity.cpp:1363
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
virtual void HandlePhysics(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Handles the physics of the entity - updates position based on speed, updates speed based on environme...
Definition: Entity.cpp:1001
void SetAirDrag(float a_AirDrag)
Definition: Entity.h:286
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI)
Makes this entity take damage specified in the a_TDI.
Definition: Entity.cpp:404
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
long int m_TicksAlive
The number of ticks this entity has been alive for.
Definition: Entity.h:667
bool m_bDirtyHead
Stores whether head yaw has been set manually.
Definition: Entity.h:594
virtual void BroadcastMovementUpdate(const cClientHandle *a_Exclude=nullptr)
Updates clients of changes in the entity.
Definition: Entity.cpp:1966
cExpOrb(Vector3d a_Pos, int a_Reward)
Definition: ExpOrb.cpp:8
int m_Reward
Definition: ExpOrb.h:51
static std::vector< int > Split(int a_Reward)
Split reward into small values according to regular Minecraft rules.
Definition: ExpOrb.cpp:110
std::chrono::milliseconds m_Timer
The number of ticks that the entity has existed / timer between collect and destroy; in msec.
Definition: ExpOrb.h:54
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: ExpOrb.cpp:34
virtual void SpawnOn(cClientHandle &a_Client) override
Descendants override this function to send a command to the specified client to spawn the entity on t...
Definition: ExpOrb.cpp:23
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: ExpOrb.cpp:95
Definition: Player.h:29
int DeltaExperience(int a_Xp_delta)
Definition: Player.cpp:279
T y
Definition: Vector3.h:17
double Length(void) const
Definition: Vector3.h:100
void Normalize(void)
Definition: Vector3.h:49
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override
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