Cuberite
A lightweight, fast and extensible game server for Minecraft
Ghast.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 "Ghast.h"
5 #include "../World.h"
6 #include "../Entities/GhastFireballEntity.h"
7 
8 
9 
10 
12  Super("Ghast", mtGhast, "entity.ghast.hurt", "entity.ghast.death", "entity.ghast.ambient", 4, 4),
13  m_IsCharging(false),
14  m_FlightCooldown(5),
15  m_TicksUntilShot(10)
16 {
17  SetGravity(0);
18  SetAirDrag(0);
19 }
20 
21 
22 
23 
24 
25 bool cGhast::Attack(std::chrono::milliseconds a_Dt)
26 {
27  if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0) && (!m_IsCharging))
28  {
29  auto & Random = GetRandomProvider();
30  auto SoundPitchMultiplier = 1.0f + (Random.RandReal(1.0f) - Random.RandReal(1.0f)) * 0.2f;
31 
32  m_World->BroadcastSoundEffect("entity.ghast.warn", GetPosition(), 4.0f, SoundPitchMultiplier * 0.9f);
33  m_IsCharging = true;
35  return true;
36  }
37  return false;
38 }
39 
40 
41 
42 
43 
45 {
46  // No fall damage
47  if (a_TDI.DamageType == dtFalling)
48  {
49  return false;
50  }
51 
52  return Super::DoTakeDamage(a_TDI);
53 }
54 
55 
56 
57 
58 
59 void cGhast::GetDrops(cItems & a_Drops, cEntity * a_Killer)
60 {
61  unsigned int LootingLevel = 0;
62  if (a_Killer != nullptr)
63  {
65  }
66  AddRandomDropItem(a_Drops, 0, 2 + LootingLevel, E_ITEM_GUNPOWDER);
67  AddRandomDropItem(a_Drops, 0, 1 + LootingLevel, E_ITEM_GHAST_TEAR);
68 }
69 
70 
71 
72 
73 
74 void cGhast::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
75 {
76  Super::Tick(a_Dt, a_Chunk);
77  if (!IsTicking())
78  {
79  // The base class tick destroyed us
80  return;
81  }
82 
83  if ((m_IsCharging) && (m_TicksUntilShot-- == 0))
84  {
85  m_TicksUntilShot = 10;
86  m_IsCharging = false;
88 
89  Vector3d Speed = GetLookVector() * 20;
90  Speed.y = Speed.y + 1;
91 
92  auto GhastBall = std::make_unique<cGhastFireballEntity>(this, GetPosition(), Speed);
93  auto GhastBallPtr = GhastBall.get();
94  GhastBallPtr->Initialize(std::move(GhastBall), *m_World);
95 
96  m_World->BroadcastSoundEffect("entity.ghast.shoot", GetPosition(), 4.0f, 1.0f);
97 
99  }
100 
101  // TODO: Better flying
102  if (m_FlightCooldown-- == 0)
103  {
104  m_FlightCooldown = 5;
105  auto & Random = GetRandomProvider();
106  auto SpeedVector = Vector3d(Random.RandReal(-0.3, 0.3), Random.RandReal(-0.4, 0.4), Random.RandReal(-0.3, 0.3));
107 
108  if (GetPosY() > 120)
109  {
110  SpeedVector = Vector3d(Random.RandReal(-0.4, 0.4), Random.RandReal(-0.45, 0.4), Random.RandReal(-0.4, 0.4));
111  }
112 
113  AddSpeed(SpeedVector);
114  }
115 }
@ E_ITEM_GHAST_TEAR
Definition: BlockType.h:415
@ E_ITEM_GUNPOWDER
Definition: BlockType.h:333
@ dtFalling
Definition: Defines.h:249
MTRand & GetRandomProvider()
Returns the current thread's random number source.
Definition: FastRandom.cpp:12
@ mtGhast
Definition: MonsterTypes.h:31
Vector3< double > Vector3d
Definition: Vector3.h:485
Definition: Chunk.h:36
unsigned int GetLevel(int a_EnchantmentID) const
Returns the level for the specified enchantment; 0 if not stored.
eDamageType DamageType
Definition: Entity.h:61
Definition: Entity.h:76
void AddSpeed(double a_AddSpeedX, double a_AddSpeedY, double a_AddSpeedZ)
Definition: Entity.cpp:2194
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
void SetGravity(float a_Gravity)
Definition: Entity.h:282
cWorld * m_World
Definition: Entity.h:624
double GetPosY(void) const
Definition: Entity.h:196
void SetAirDrag(float a_AirDrag)
Definition: Entity.h:286
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
Vector3d GetLookVector(void) const
Definition: Entity.cpp:2267
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
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Ghast.cpp:74
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: Ghast.cpp:25
cGhast()
Definition: Ghast.cpp:11
int m_FlightCooldown
Number of ticks until the ghast tries to fly to another position.
Definition: Ghast.h:35
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Ghast.cpp:44
virtual void GetDrops(cItems &a_Drops, cEntity *a_Killer=nullptr) override
Returns the list of drops for this pawn when it is killed.
Definition: Ghast.cpp:59
int m_TicksUntilShot
Number of ticks until a projectile is created.
Definition: Ghast.h:39
bool m_IsCharging
Specifies whether or not the ghast has started shooting a fireball.
Definition: Ghast.h:32
void ResetAttackCooldown()
Definition: Monster.cpp:937
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
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Monster.cpp:572
int m_AttackCoolDownTicksLeft
Definition: Monster.h:313
T y
Definition: Vector3.h:17
virtual void BroadcastEntityMetadata(const cEntity &a_Entity, const cClientHandle *a_Exclude=nullptr) override
virtual void BroadcastSoundEffect(const AString &a_SoundName, Vector3d a_Position, float a_Volume, float a_Pitch, const cClientHandle *a_Exclude=nullptr) override