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 }
cClientHandle
Definition: ClientHandle.h:49
cMonster::AddRandomArmorDropItem
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:1556
E_ITEM_BOW
@ E_ITEM_BOW
Definition: BlockType.h:305
cSkeleton::SpawnOn
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
cMonster::AddRandomWeaponDropItem
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:1599
cMonster::m_AttackCoolDownTicksLeft
int m_AttackCoolDownTicksLeft
Definition: Monster.h:313
cEntity::GetEquippedWeapon
virtual cItem GetEquippedWeapon(void) const
Returns the curently equipped weapon; empty item if none.
Definition: Entity.h:333
cEnchantments::enchLooting
@ enchLooting
Definition: Enchantments.h:64
E_ITEM_BONE
@ E_ITEM_BONE
Definition: BlockType.h:397
Skeleton.h
cMonster::GetTarget
cPawn * GetTarget()
Returns the current target.
Definition: Monster.cpp:1237
Globals.h
mtSkeleton
@ mtSkeleton
Definition: MonsterTypes.h:59
cSkeleton::Tick
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Skeleton.cpp:40
E_ITEM_ARROW
@ E_ITEM_ARROW
Definition: BlockType.h:306
cItem::m_Enchantments
cEnchantments m_Enchantments
Definition: Item.h:166
cMonster::ResetAttackCooldown
void ResetAttackCooldown()
Definition: Monster.cpp:931
cSkeleton::GetDrops
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
cClientHandle::SendEntityEquipment
void SendEntityEquipment(const cEntity &a_Entity, short a_SlotNum, const cItem &a_Item)
Definition: ClientHandle.cpp:2600
cAggressiveMonster::Tick
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: AggressiveMonster.cpp:49
cEntity::m_World
cWorld * m_World
Definition: Entity.h:624
cSkeleton::m_ChargingBow
bool m_ChargingBow
Definition: Skeleton.h:33
GetRandomProvider
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
cItem
Definition: Item.h:36
cMonster::StopMovingToPosition
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:254
Vector3d
Vector3< double > Vector3d
Definition: Vector3.h:485
cItems
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:213
cSkeleton::cSkeleton
cSkeleton()
Definition: Skeleton.cpp:12
cMonster::AddRandomDropItem
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:1511
cWorld::BroadcastEntityMetadata
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
Definition: Broadcaster.cpp:322
cMonster::SpawnOn
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:166
cMonster::IDLE
@ IDLE
Definition: Monster.h:40
cEntity::IsTicking
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
cChunk
Definition: Chunk.h:35
cEntity
Definition: Entity.h:75
cMonster::m_EMState
enum cMonster::MState m_EMState
cSkeleton::Attack
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
cEntity::GetPosition
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
Vector3::y
T y
Definition: Vector3.h:17
cEnchantments::GetLevel
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
Definition: Enchantments.cpp:112
Vector3< double >
Item::Arrow
@ Arrow