Cuberite
A lightweight, fast and extensible game server for Minecraft
Skeleton.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 "Skeleton.h"
5 #include "../World.h"
6 #include "../Entities/ArrowEntity.h"
7 #include "../ClientHandle.h"
8 
9 
10 
11 
13  Super("Skeleton", mtSkeleton, "entity.skeleton.hurt", "entity.skeleton.death", "entity.skeleton.ambient", 0.6f, 1.99f),
14  m_ChargingBow(false)
15 {
16 }
17 
18 
19 
20 
21 
22 void cSkeleton::GetDrops(cItems & a_Drops, cEntity * a_Killer)
23 {
24  unsigned int LootingLevel = 0;
25  if (a_Killer != nullptr)
26  {
28  }
29  AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_ARROW);
30 
31  AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_BONE);
32  AddRandomArmorDropItem(a_Drops, LootingLevel);
33  AddRandomWeaponDropItem(a_Drops, LootingLevel);
34 }
35 
36 
37 
38 
39 
40 void cSkeleton::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
41 {
42  Super::Tick(a_Dt, a_Chunk);
43 
44  if (!IsTicking())
45  {
46  // The base class tick destroyed us
47  return;
48  }
49 
50  if (m_ChargingBow && (m_EMState == IDLE))
51  {
52  // releasing bow if no more target is found
53  m_ChargingBow = false;
55  }
56 }
57 
58 
59 
60 
61 
62 bool cSkeleton::Attack(std::chrono::milliseconds a_Dt)
63 {
64  StopMovingToPosition(); // Todo handle this in a better way, the skeleton does some uneeded recalcs due to inStateChasing
65  auto & Random = GetRandomProvider();
66 
67  if (!m_ChargingBow)
68  {
69  // updating pulling animation
70  m_ChargingBow = true;
72  }
73 
74  if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0))
75  {
76  Vector3d Inaccuracy = Vector3d(Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25));
77  Vector3d Speed = (GetTarget()->GetPosition() + Inaccuracy - GetPosition()) * 5;
78  Speed.y += Random.RandInt(-1, 1);
79 
80  auto Arrow = std::make_unique<cArrowEntity>(this, GetPosition().addedY(1), Speed);
81  auto ArrowPtr = Arrow.get();
82  if (!ArrowPtr->Initialize(std::move(Arrow), *m_World))
83  {
84  return false;
85  }
86 
87  // releasing bow after arrow was shot
88  m_ChargingBow = false;
90 
92  return true;
93  }
94  return false;
95 }
96 
97 
98 
99 
100 
101 void cSkeleton::SpawnOn(cClientHandle & a_ClientHandle)
102 {
103  Super::SpawnOn(a_ClientHandle);
104  a_ClientHandle.SendEntityEquipment(*this, 0, cItem(E_ITEM_BOW));
105 }
@ E_ITEM_ARROW
Definition: BlockType.h:306
@ E_ITEM_BONE
Definition: BlockType.h:397
@ E_ITEM_BOW
Definition: BlockType.h:305
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
@ mtSkeleton
Definition: MonsterTypes.h:59
Vector3< double > Vector3d
Definition: Vector3.h:485
Definition: Chunk.h:36
void SendEntityEquipment(const cEntity &a_Entity, short a_SlotNum, const cItem &a_Item)
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
cWorld * m_World
Definition: Entity.h:624
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
Definition: Item.h:37
cEnchantments m_Enchantments
Definition: Item.h:166
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
void ResetAttackCooldown()
Definition: Monster.cpp:937
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: Monster.cpp:167
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
cPawn * GetTarget()
Returns the current target.
Definition: Monster.cpp:1243
@ IDLE
Definition: Monster.h:40
void AddRandomWeaponDropItem(cItems &a_Drops, unsigned int a_LootingLevel)
Adds weapon that is equipped with the chance saved in m_DropChance... to the drop.
Definition: Monster.cpp:1606
void AddRandomArmorDropItem(cItems &a_Drops, unsigned int a_LootingLevel)
Adds armor that is equipped with the chance saved in m_DropChance... to the drop.
Definition: Monster.cpp:1563
enum cMonster::MState m_EMState
int m_AttackCoolDownTicksLeft
Definition: Monster.h:313
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:255
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Skeleton.cpp:22
bool m_ChargingBow
Definition: Skeleton.h:33
virtual bool Attack(std::chrono::milliseconds a_Dt) override
Try to perform attack returns true if attack was deemed successful (hit player, fired projectile,...
Definition: Skeleton.cpp:62
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: Skeleton.cpp:101
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Skeleton.cpp:40
T y
Definition: Vector3.h:17
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override