Cuberite
A lightweight, fast and extensible game server for Minecraft
Monster.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "../Entities/Pawn.h"
5 #include "MonsterTypes.h"
6 #include "PathFinder.h"
7 
8 
9 class cItem;
10 class cClientHandle;
11 
12 
13 
14 // tolua_begin
15 class cMonster :
16  public cPawn
17 {
18  typedef cPawn super;
19 public:
20 
21  enum eFamily
22  {
23  mfHostile = 0, // Spider, Zombies ...
24  mfPassive = 1, // Cows, Pigs
25  mfAmbient = 2, // Bats
26  mfWater = 3, // Squid, Guardian
27 
29  mfUnhandled, // Nothing. Be sure this is the last and the others are in order
30  } ;
31 
32  // tolua_end
33 
36 
42  cMonster(const AString & a_ConfigName, eMonsterType a_MobType, const AString & a_SoundHurt, const AString & a_SoundDeath, double a_Width, double a_Height);
43 
44  virtual ~cMonster() override;
45 
46  virtual void Destroy(bool a_ShouldBroadcast = true) override;
47 
48  virtual void Destroyed() override;
49 
51 
52  virtual void SpawnOn(cClientHandle & a_ClientHandle) override;
53 
54  virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
55 
56  virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override;
57 
58  virtual void KilledBy(TakeDamageInfo & a_TDI) override;
59 
60  virtual void OnRightClicked(cPlayer & a_Player) override;
61 
62  virtual void HandleFalling(void) override;
63 
65  virtual void MoveToPosition(const Vector3d & a_Position); // tolua_export
66 
67  // tolua_begin
68  eMonsterType GetMobType(void) const { return m_MobType; }
69  eFamily GetMobFamily(void) const;
70  // tolua_end
71 
72  virtual void CheckEventSeePlayer(cChunk & a_Chunk);
73  virtual void EventSeePlayer(cPlayer * a_Player, cChunk & a_Chunk);
74 
75  // tolua_begin
76 
78  bool CanBeLeashed() const { return m_CanBeLeashed; }
79 
81  void SetCanBeLeashed(bool a_CanBeLeashed) { m_CanBeLeashed = a_CanBeLeashed; }
82 
84  bool IsLeashed() const { return (m_LeashedTo != nullptr); }
85 
87  void LeashTo(cEntity & a_Entity, bool a_ShouldBroadcast = true);
88 
90  void Unleash(bool a_ShouldDropLeashPickup);
91 
93  cEntity * GetLeashedTo() const { return m_LeashedTo; }
94 
95  // tolua_end
96 
98  void Unleash(bool a_ShouldDropLeashPickup, bool a_ShouldBroadcast);
99 
101  void SetLeashToPos(Vector3d * pos) { m_LeashToPos = std::unique_ptr<Vector3d>(pos); }
102 
104  Vector3d * GetLeashToPos() const { return m_LeashToPos.get(); }
105 
107  void GetMonsterConfig(const AString & a_Name);
108 
110  virtual bool IsUndead(void);
111 
112  virtual void EventLosePlayer(void);
113  virtual void CheckEventLostPlayer(void);
114 
115  virtual void InStateIdle (std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
116  virtual void InStateChasing (std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
117  virtual void InStateEscaping(std::chrono::milliseconds a_Dt, cChunk & a_Chunk);
118 
119  int GetAttackRate() { return static_cast<int>(m_AttackRate); }
120  void SetAttackRate(float a_AttackRate) { m_AttackRate = a_AttackRate; }
121  void SetAttackRange(int a_AttackRange) { m_AttackRange = a_AttackRange; }
122  void SetAttackDamage(int a_AttackDamage) { m_AttackDamage = a_AttackDamage; }
123  void SetSightDistance(int a_SightDistance) { m_SightDistance = a_SightDistance; }
124 
130  bool CanPickUpLoot() { return m_CanPickUpLoot; }
131  void SetDropChanceWeapon(float a_DropChanceWeapon) { m_DropChanceWeapon = a_DropChanceWeapon; }
132  void SetDropChanceHelmet(float a_DropChanceHelmet) { m_DropChanceHelmet = a_DropChanceHelmet; }
133  void SetDropChanceChestplate(float a_DropChanceChestplate) { m_DropChanceChestplate = a_DropChanceChestplate; }
134  void SetDropChanceLeggings(float a_DropChanceLeggings) { m_DropChanceLeggings = a_DropChanceLeggings; }
135  void SetDropChanceBoots(float a_DropChanceBoots) { m_DropChanceBoots = a_DropChanceBoots; }
136  void SetCanPickUpLoot(bool a_CanPickUpLoot) { m_CanPickUpLoot = a_CanPickUpLoot; }
137  void ResetAttackCooldown();
138 
139  void SetBurnsInDaylight(bool a_BurnsInDaylight) { m_BurnsInDaylight = a_BurnsInDaylight; } // tolua_export
140  bool BurnsInDaylight() const { return m_BurnsInDaylight; } // tolua_export
141 
142  double GetRelativeWalkSpeed(void) const { return m_RelativeWalkSpeed; } // tolua_export
143  void SetRelativeWalkSpeed(double a_WalkSpeed) { m_RelativeWalkSpeed = a_WalkSpeed; } // tolua_export
144 
145  // Overridables to handle ageable mobs
146  virtual bool IsTame (void) const { return false; }
147  virtual bool IsSitting (void) const { return false; }
148 
149  // tolua_begin
150  bool IsBaby (void) const { return m_Age < 0; }
151  int GetAge (void) const { return m_Age; }
152  void SetAge(int a_Age) { m_Age = a_Age; }
153  // tolua_end
154 
155 
156  // tolua_begin
157 
159  bool HasCustomName(void) const { return !m_CustomName.empty(); }
160 
162  const AString & GetCustomName(void) const { return m_CustomName; }
163 
166  void SetCustomName(const AString & a_CustomName);
167 
170 
173  void SetCustomNameAlwaysVisible(bool a_CustomNameAlwaysVisible);
174 
176  static AString MobTypeToString(eMonsterType a_MobType);
177 
179  static AString MobTypeToVanillaName(eMonsterType a_MobType);
180 
182  static eMonsterType StringToMobType(const AString & a_MobTypeName);
183 
185  static eFamily FamilyFromType(eMonsterType a_MobType);
186 
188  static int GetSpawnDelay(cMonster::eFamily a_MobFamily);
189 
190  // tolua_end
191 
193  static AString MobTypeToVanillaNBT(eMonsterType a_MobType);
194 
196  void SetTarget (cPawn * a_NewTarget);
197 
200  void UnsafeUnsetTarget();
201 
203  cPawn * GetTarget();
204 
209  static std::unique_ptr<cMonster> NewMonsterFromType(eMonsterType a_MobType);
210 
213 
214 protected:
215 
218 
221 
224 
227 
232  int FindFirstNonAirBlockPosition(double a_PosX, double a_PosZ);
233 
235  bool ReachedFinalDestination(void) { return ((m_FinalDestination - GetPosition()).SqrLength() < WAYPOINT_RADIUS * WAYPOINT_RADIUS); }
236 
238  bool TargetIsInRange(void)
239  {
240  ASSERT(GetTarget() != nullptr);
241  return ((GetTarget()->GetPosition() - GetPosition()).SqrLength() < (m_AttackRange * m_AttackRange));
242  }
243 
245  inline bool DoesPosYRequireJump(double a_PosY)
246  {
247  return (a_PosY > GetPosY() + 0.8); // Assume that differences up to 0.8 blocks can be walked instead of jumped
248  }
249 
251  void MoveToWayPoint(cChunk & a_Chunk);
252 
254  void StopMovingToPosition();
255 
257  void SetPitchAndYawFromDestination(bool a_IsFollowingPath);
258 
260 
261  std::chrono::milliseconds m_IdleInterval;
262  std::chrono::milliseconds m_DestroyTimer;
263 
267 
270 
276 
283  int m_TicksSinceLastDamaged; // How many ticks ago we were last damaged by a player?
284 
285  void HandleDaylightBurning(cChunk & a_Chunk, bool WouldBurn);
286  bool WouldBurnAt(Vector3d a_Location, cChunk & a_Chunk);
289 
290  int m_Age;
292 
294 
297 
299  std::unique_ptr<Vector3d> m_LeashToPos;
300 
303 
306 
308  void AddRandomDropItem(cItems & a_Drops, unsigned int a_Min, unsigned int a_Max, short a_Item, short a_ItemHealth = 0);
309 
311  void AddRandomUncommonDropItem(cItems & a_Drops, float a_Chance, short a_Item, short a_ItemHealth = 0);
312 
314  void AddRandomRareDropItem(cItems & a_Drops, cItems & a_Items, unsigned int a_LootingLevel);
315 
317  void AddRandomArmorDropItem(cItems & a_Drops, unsigned int a_LootingLevel);
318 
320  void AddRandomWeaponDropItem(cItems & a_Drops, unsigned int a_LootingLevel);
321 
322 private:
327 
329  void CalcLeashActions(std::chrono::milliseconds a_Dt);
330 
331 } ; // tolua_export
double m_RelativeWalkSpeed
Definition: Monster.h:288
Vector3d m_FinalDestination
Coordinates for the ultimate, final destination.
Definition: Monster.h:226
double GetPosY(void) const
Definition: Entity.h:207
virtual void InStateEscaping(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Monster.cpp:832
void SetSightDistance(int a_SightDistance)
Definition: Monster.h:123
virtual void Destroyed() override
Definition: Monster.cpp:156
eMonsterType GetMobType(void) const
Definition: Monster.h:68
float GetDropChanceChestplate()
Definition: Monster.h:127
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:1303
float m_DropChanceWeapon
Definition: Monster.h:277
bool DoesPosYRequireJump(double a_PosY)
Returns whether the monster needs to jump to reach a given height.
Definition: Monster.h:245
static std::unique_ptr< cMonster > NewMonsterFromType(eMonsterType a_MobType)
Creates a new object of the specified mob.
Definition: Monster.cpp:1129
void MoveToWayPoint(cChunk &a_Chunk)
Move in a straight line to the next waypoint in the path, will jump if needed.
Definition: Monster.cpp:180
const AString & GetCustomName(void) const
Gets the custom name of the monster.
Definition: Monster.h:162
double GetRelativeWalkSpeed(void) const
Definition: Monster.h:142
bool m_CanPickUpLoot
Definition: Monster.h:282
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Definition: Monster.cpp:263
float GetDropChanceLeggings()
Definition: Monster.h:128
void SetDropChanceChestplate(float a_DropChanceChestplate)
Definition: Monster.h:133
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:1260
Definition: Player.h:27
cPawn * GetTarget()
Returns the current target.
Definition: Monster.cpp:1120
void SetCanPickUpLoot(bool a_CanPickUpLoot)
Definition: Monster.h:136
cPawn super
Definition: Monster.h:18
cPawn * m_Target
A pointer to the entity this mobile is aiming to reach.
Definition: Monster.h:326
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:1215
bool HasCustomName(void) const
Returns true if the monster has a custom name.
Definition: Monster.h:159
bool IsBaby(void) const
Definition: Monster.h:150
float m_DropChanceHelmet
Definition: Monster.h:278
virtual ~cMonster() override
Definition: Monster.cpp:126
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
MPersonality
Definition: Monster.h:35
float GetDropChanceHelmet()
Definition: Monster.h:126
void SetRelativeWalkSpeed(double a_WalkSpeed)
Definition: Monster.h:143
void AddRandomRareDropItem(cItems &a_Drops, cItems &a_Items, unsigned int a_LootingLevel)
Adds one rare item out of the list of rare items a_Items modified by the looting level a_LootingLevel...
Definition: Monster.cpp:1246
virtual void KilledBy(TakeDamageInfo &a_TDI) override
Called when the health drops below zero.
Definition: Monster.cpp:593
cEntity * GetLeashedTo() const
Returns the entity to where this mob is leashed, returns nullptr if it&#39;s not leashed.
Definition: Monster.h:93
cEntity * m_LeashedTo
Entity leashed to.
Definition: Monster.h:296
Definition: Chunk.h:49
void SetDropChanceBoots(float a_DropChanceBoots)
Definition: Monster.h:135
void SetAttackRate(float a_AttackRate)
Definition: Monster.h:120
enum cMonster::MState m_EMState
bool m_CanBeLeashed
Determines whether a monster can be leashed.
Definition: Monster.h:305
void SetAge(int a_Age)
Definition: Monster.h:152
eMonsterType
Identifies individual monster type, as well as their network type-ID.
Definition: MonsterTypes.h:10
bool ReachedFinalDestination(void)
Returns if the ultimate, final destination has been reached.
Definition: Monster.h:235
int m_SightDistance
Definition: Monster.h:275
bool WouldBurnAt(Vector3d a_Location, cChunk &a_Chunk)
Definition: Monster.cpp:1348
bool m_IsLeashActionJustDone
Mob has ben leashed or unleashed in current player action.
Definition: Monster.h:302
float m_DropChanceBoots
Definition: Monster.h:281
bool IsCustomNameAlwaysVisible(void) const
Is the custom name of this monster always visible? If not, you only see the name when you sight the m...
Definition: Monster.h:169
virtual void CheckEventLostPlayer(void)
Definition: Monster.cpp:726
void SetDropChanceLeggings(float a_DropChanceLeggings)
Definition: Monster.h:134
virtual void EventLosePlayer(void)
Definition: Monster.cpp:757
void SetAttackDamage(int a_AttackDamage)
Definition: Monster.h:122
static int GetSpawnDelay(cMonster::eFamily a_MobFamily)
Returns the spawn delay (number of game ticks between spawn attempts) for the given mob family...
Definition: Monster.cpp:1059
#define WAYPOINT_RADIUS
Definition: PathFinder.h:5
void UnsafeUnsetTarget()
Unset the target without notifying the target entity.
Definition: Monster.cpp:1111
void SetCustomName(const AString &a_CustomName)
Sets the custom name of the monster.
Definition: Monster.cpp:862
int m_AttackRange
Definition: Monster.h:273
bool m_WasLastTargetAPlayer
Definition: Monster.h:293
float GetDropChanceWeapon()
Definition: Monster.h:125
void SetDropChanceWeapon(float a_DropChanceWeapon)
Definition: Monster.h:131
float m_AttackRate
Definition: Monster.h:271
bool m_PathfinderActivated
Stores if pathfinder is being used - set when final destination is set, and unset when stopped moving...
Definition: Monster.h:220
virtual bool DoTakeDamage(TakeDamageInfo &a_TDI) override
Makes this entity take damage specified in the a_TDI.
Definition: Monster.cpp:563
virtual bool IsUndead(void)
Returns whether this mob is undead (skeleton, zombie, etc.)
Definition: Monster.cpp:904
Vector3d m_NextWayPointPosition
Coordinates of the next position that should be reached.
Definition: Monster.h:223
bool WasLastTargetAPlayer() const
Returns if this mob last target was a player to avoid destruction on player quit. ...
Definition: Monster.h:212
void SetPitchAndYawFromDestination(bool a_IsFollowingPath)
Sets the body yaw and head yaw.
Definition: Monster.cpp:470
static eFamily FamilyFromType(eMonsterType a_MobType)
Returns the mob family based on the type.
Definition: Monster.cpp:1009
bool TargetIsInRange(void)
Returns whether or not the target is close enough for attack.
Definition: Monster.h:238
int m_TicksSinceLastDamaged
Definition: Monster.h:283
std::chrono::milliseconds m_DestroyTimer
Definition: Monster.h:262
eMonsterType m_MobType
Definition: Monster.h:264
void SetTarget(cPawn *a_NewTarget)
Sets the target that this mob will chase.
Definition: Monster.cpp:1081
cMonster(const AString &a_ConfigName, eMonsterType a_MobType, const AString &a_SoundHurt, const AString &a_SoundDeath, double a_Width, double a_Height)
Creates the mob object.
Definition: Monster.cpp:79
AString m_SoundDeath
Definition: Monster.h:269
#define ASSERT(x)
Definition: Globals.h:335
static AString MobTypeToVanillaNBT(eMonsterType a_MobType)
Translates the MobType enum to the vanilla nbt name.
Definition: Monster.cpp:951
bool IsLeashed() const
Returns whether the monster is leashed to an entity.
Definition: Monster.h:84
int m_AttackCoolDownTicksLeft
Definition: Monster.h:274
float GetDropChanceBoots()
Definition: Monster.h:129
float m_DropChanceLeggings
Definition: Monster.h:280
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
int FindFirstNonAirBlockPosition(double a_PosX, double a_PosZ)
Finds the lowest non-air block position (not the highest, as cWorld::GetHeight does) If current Y is ...
Definition: Monster.cpp:534
std::chrono::milliseconds m_IdleInterval
Definition: Monster.h:261
float m_DropChanceChestplate
Definition: Monster.h:279
int m_AgingTimer
Definition: Monster.h:291
Definition: Pawn.h:15
virtual void Destroy(bool a_ShouldBroadcast=true) override
Destroys the entity and schedules it for memory freeing; if a_ShouldBroadcast is set to true...
Definition: Monster.cpp:135
virtual void CheckEventSeePlayer(cChunk &a_Chunk)
Definition: Monster.cpp:713
This class wraps cPath.
Definition: PathFinder.h:11
bool BurnsInDaylight() const
Definition: Monster.h:140
void SetCustomNameAlwaysVisible(bool a_CustomNameAlwaysVisible)
Sets the custom name visiblity of this monster.
Definition: Monster.cpp:882
static AString MobTypeToString(eMonsterType a_MobType)
Translates MobType enum to a string, empty string if unknown.
Definition: Monster.cpp:913
std::string AString
Definition: StringUtils.h:13
cPathFinder m_PathFinder
The pathfinder instance handles pathfinding for this monster.
Definition: Monster.h:217
bool CanPickUpLoot()
Definition: Monster.h:130
void HandleDaylightBurning(cChunk &a_Chunk, bool WouldBurn)
Definition: Monster.cpp:1318
static eMonsterType StringToMobType(const AString &a_MobTypeName)
Translates MobType string to the enum, mtInvalidType if not recognized.
Definition: Monster.cpp:970
void SetAttackRange(int a_AttackRange)
Definition: Monster.h:121
int m_AttackDamage
Definition: Monster.h:272
AString m_SoundHurt
Definition: Monster.h:268
void ResetAttackCooldown()
Definition: Monster.cpp:853
void SetDropChanceHelmet(float a_DropChanceHelmet)
Definition: Monster.h:132
virtual bool IsSitting(void) const
Definition: Monster.h:147
void CalcLeashActions(std::chrono::milliseconds a_Dt)
Leash calculations inside Tick function.
Definition: Monster.cpp:402
bool m_CustomNameAlwaysVisible
Definition: Monster.h:266
virtual void EventSeePlayer(cPlayer *a_Player, cChunk &a_Chunk)
Definition: Monster.cpp:747
Vector3d * GetLeashToPos() const
Gets entity position to where mob should be leashed.
Definition: Monster.h:104
Definition: Entity.h:73
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:307
void SetCanBeLeashed(bool a_CanBeLeashed)
Sets whether the mob can be leashed, for extensibility in plugins.
Definition: Monster.h:81
virtual void InStateIdle(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Monster.cpp:767
int GetAttackRate()
Definition: Monster.h:119
void GetMonsterConfig(const AString &a_Name)
Reads the monster configuration for the specified monster name and assigns it to this object...
Definition: Monster.cpp:895
eFamily GetMobFamily(void) const
Definition: Monster.cpp:1411
int GetAge(void) const
Definition: Monster.h:151
virtual void OnRightClicked(cPlayer &a_Player) override
Called when the specified player right-clicks this entity.
Definition: Monster.cpp:672
int m_JumpCoolDown
Definition: Monster.h:259
#define CLASS_PROTODEF(classname)
Definition: Entity.h:11
void LeashTo(cEntity &a_Entity, bool a_ShouldBroadcast=true)
Leash the monster to an entity.
Definition: Monster.cpp:1420
void Unleash(bool a_ShouldDropLeashPickup)
Unleash the monster.
Definition: Monster.cpp:1475
void StopMovingToPosition()
Stops pathfinding.
Definition: Monster.cpp:254
virtual void InStateChasing(std::chrono::milliseconds a_Dt, cChunk &a_Chunk)
Definition: Monster.cpp:822
static AString MobTypeToVanillaName(eMonsterType a_MobType)
Translates MobType enum to the vanilla name of the mob, empty string if unknown.
Definition: Monster.cpp:932
Definition: Item.h:36
virtual bool IsTame(void) const
Definition: Monster.h:146
enum cMonster::MPersonality m_EMPersonality
void SetLeashToPos(Vector3d *pos)
Sets entity position to where is leashed this mob.
Definition: Monster.h:101
void AddRandomUncommonDropItem(cItems &a_Drops, float a_Chance, short a_Item, short a_ItemHealth=0)
Adds a item a_Item with the chance of a_Chance (in percent) to itemdrops a_Drops. ...
Definition: Monster.cpp:1234
void SetBurnsInDaylight(bool a_BurnsInDaylight)
Definition: Monster.h:139
bool m_BurnsInDaylight
Definition: Monster.h:287
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234
virtual void HandleFalling(void) override
Definition: Monster.cpp:524
bool CanBeLeashed() const
Returns whether the mob can be leashed.
Definition: Monster.h:78
AString m_CustomName
Definition: Monster.h:265
std::unique_ptr< Vector3d > m_LeashToPos
Entity pos where this mob was leashed to.
Definition: Monster.h:299
int m_Age
Definition: Monster.h:290