Cuberite
A lightweight, fast and extensible game server for Minecraft
AggressiveMonster.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 "AggressiveMonster.h"
5 
6 #include "../World.h"
7 #include "../Entities/Player.h"
8 #include "../LineBlockTracer.h"
9 
10 
11 
12 
13 
14 cAggressiveMonster::cAggressiveMonster(const AString & a_ConfigName, eMonsterType a_MobType, const AString & a_SoundHurt, const AString & a_SoundDeath, const AString & a_SoundAmbient, float a_Width, float a_Height) :
15  Super(a_ConfigName, a_MobType, a_SoundHurt, a_SoundDeath, a_SoundAmbient, a_Width, a_Height)
16 {
18 }
19 
20 
21 
22 
23 
24 // What to do if in Chasing State
25 void cAggressiveMonster::InStateChasing(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
26 {
27  Super::InStateChasing(a_Dt, a_Chunk);
28 
29  if (GetTarget() != nullptr)
30  {
32  }
33 }
34 
35 
36 
37 
38 
40 {
41  Super::EventSeePlayer(a_Player, a_Chunk);
43 }
44 
45 
46 
47 
48 
49 void cAggressiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
50 {
51  Super::Tick(a_Dt, a_Chunk);
52  if (!IsTicking())
53  {
54  // The base class tick destroyed us
55  return;
56  }
57 
58  // Set or clear m_Target depending on rules for this Monster:
59  if (m_EMState == CHASING)
60  {
62  }
63  else
64  {
65  CheckEventSeePlayer(a_Chunk);
66  }
67 
68  if (
69  (GetTarget() != nullptr) &&
70  TargetIsInRange() &&
72  *GetWorld(),
73  GetPosition().addedY(GetHeight()),
74  GetTarget()->GetPosition().addedY(GetTarget()->GetHeight()),
76  ) &&
77  (GetHealth() > 0.0)
78  )
79  {
80  // Attack if reached destination, target isn't null, and have a clear line of sight to target (so won't attack through walls)
81  Attack(a_Dt);
82  }
83 }
84 
85 
86 
87 
88 
89 bool cAggressiveMonster::Attack(std::chrono::milliseconds a_Dt)
90 {
91  if ((GetTarget() == nullptr) || (m_AttackCoolDownTicksLeft != 0))
92  {
93  return false;
94  }
95 
96  // Setting this higher gives us more wiggle room for attackrate
98 
99  double KnockbackAmount = 9;
100  GetTarget()->TakeDamage(dtMobAttack, this, m_AttackDamage, KnockbackAmount);
101 
102  return true;
103 }
cEntity::TakeDamage
void TakeDamage(cEntity &a_Attacker)
Makes this pawn take damage from an attack by a_Attacker.
Definition: Entity.cpp:272
cLineBlockTracer::losAirWater
@ losAirWater
Definition: LineBlockTracer.h:41
cMonster::CHASING
@ CHASING
Definition: Monster.h:40
cMonster::m_AttackCoolDownTicksLeft
int m_AttackCoolDownTicksLeft
Definition: Monster.h:313
cMonster::CheckEventLostPlayer
virtual void CheckEventLostPlayer(std::chrono::milliseconds a_Dt)
Definition: Monster.cpp:778
cEntity::GetWorld
cWorld * GetWorld(void) const
Definition: Entity.h:190
cMonster::TargetIsInRange
bool TargetIsInRange(void)
Returns whether or not the target is close enough for attack.
Definition: Monster.h:276
cMonster::GetTarget
cPawn * GetTarget()
Returns the current target.
Definition: Monster.cpp:1237
Globals.h
cMonster::m_EMPersonality
enum cMonster::MPersonality m_EMPersonality
cAggressiveMonster::Attack
virtual bool Attack(std::chrono::milliseconds a_Dt)
Try to perform attack returns true if attack was deemed successful (hit player, fired projectile,...
Definition: AggressiveMonster.cpp:89
cEntity::GetHeight
float GetHeight(void) const
Definition: Entity.h:193
cMonster::ResetAttackCooldown
void ResetAttackCooldown()
Definition: Monster.cpp:931
eMonsterType
eMonsterType
Identifies individual monster type.
Definition: MonsterTypes.h:10
cEntity::GetHealth
float GetHealth(void) const
Returns the health of this entity.
Definition: Entity.h:367
cAggressiveMonster::Tick
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: AggressiveMonster.cpp:49
cMonster::IsNetherNative
virtual bool IsNetherNative(void)
Returns whether this mob spawns in the Nether in Vanilla.
Definition: Monster.cpp:982
cMonster::CheckEventSeePlayer
virtual void CheckEventSeePlayer(cChunk &a_Chunk)
Definition: Monster.cpp:732
AggressiveMonster.h
cMonster::InStateChasing
virtual void InStateChasing(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Monster.cpp:900
cAggressiveMonster::EventSeePlayer
virtual void EventSeePlayer(cPlayer *a_Player, cChunk &a_Chunk) override
Definition: AggressiveMonster.cpp:39
cEntity::IsTicking
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
cChunk
Definition: Chunk.h:35
cMonster::EventSeePlayer
virtual void EventSeePlayer(cPlayer *a_Player, cChunk &a_Chunk)
Definition: Monster.cpp:823
cEntity
Definition: Entity.h:75
cPlayer
Definition: Player.h:27
cLineBlockTracer::losAirWaterLava
@ losAirWaterLava
Definition: LineBlockTracer.h:40
cMonster::m_EMState
enum cMonster::MState m_EMState
dtMobAttack
@ dtMobAttack
Definition: Defines.h:271
cEntity::GetPosition
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
cMonster::AGGRESSIVE
@ AGGRESSIVE
Definition: Monster.h:41
cLineBlockTracer::LineOfSightTrace
static bool LineOfSightTrace(cWorld &a_World, const Vector3d &a_Start, const Vector3d &a_End, int a_Sight)
Returns true if the two positions are within line of sight (not obscured by blocks).
Definition: LineBlockTracer.cpp:42
cAggressiveMonster::InStateChasing
virtual void InStateChasing(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: AggressiveMonster.cpp:25
cMonster::m_AttackDamage
int m_AttackDamage
Definition: Monster.h:311
cMonster::Tick
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Monster.cpp:263
cAggressiveMonster::cAggressiveMonster
cAggressiveMonster(const AString &a_ConfigName, eMonsterType a_MobType, const AString &a_SoundHurt, const AString &a_SoundDeath, const AString &a_SoundAmbient, float a_Width, float a_Height)
Definition: AggressiveMonster.cpp:14
AString
std::string AString
Definition: StringUtils.h:11
cMonster::MoveToPosition
virtual void MoveToPosition(const Vector3d &a_Position)
Engage pathfinder and tell it to calculate a path to a given position, and move the mob accordingly.
Definition: Monster.cpp:244