2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOSSERVER_CREATURE_H
20 #define MANGOSSERVER_CREATURE_H
24 #include "UpdateMask.h"
25 #include "ItemPrototype.h"
27 #include "Database/DatabaseEnv.h"
39 enum CreatureFlagsExtra
41 CREATURE_FLAG_EXTRA_INSTANCE_BIND
= 0x00000001, // creature kill bind instance with killer and killer's group
42 CREATURE_FLAG_EXTRA_CIVILIAN
= 0x00000002, // not aggro (ignore faction/reputation hostility)
43 CREATURE_FLAG_EXTRA_NO_PARRY
= 0x00000004, // creature can't parry
44 CREATURE_FLAG_EXTRA_NO_PARRY_HASTEN
= 0x00000008, // creature can't counter-attack at parry
45 CREATURE_FLAG_EXTRA_NO_BLOCK
= 0x00000010, // creature can't block
46 CREATURE_FLAG_EXTRA_NO_CRUSH
= 0x00000020, // creature can't do crush attacks
47 CREATURE_FLAG_EXTRA_NO_XP_AT_KILL
= 0x00000040, // creature kill not provide XP
48 CREATURE_FLAG_EXTRA_INVISIBLE
= 0x00000080, // creature is always invisible for player (mostly trigger creatures)
49 CREATURE_FLAG_EXTRA_NOT_TAUNTABLE
= 0x00000100, // creature is immune to taunt auras and effect attack me
52 // GCC have alternative #pragma pack(N) syntax and old gcc version not support pack(push,N), also any gcc version not support it at some platform
53 #if defined( __GNUC__ )
59 #define MAX_KILL_CREDIT 2
61 // from `creature_template` table
65 uint32 DifficultyEntry
[MAX_DIFFICULTY
- 1];
66 uint32 KillCredit
[MAX_KILL_CREDIT
];
67 uint32 DisplayID_A
[2];
68 uint32 DisplayID_H
[2];
91 uint32 baseattacktime
;
92 uint32 rangeattacktime
;
93 uint32 unit_class
; // enum Classes. Note only 4 classes are known for creatures.
94 uint32 unit_flags
; // enum UnitFlags mask values
96 uint32 family
; // enum CreatureFamily values (optional)
103 uint32 rangedattackpower
;
104 uint32 type
; // enum CreatureType values
105 uint32 type_flags
; // enum CreatureTypeFlags mask values
107 uint32 pickpocketLootId
;
115 uint32 spells
[CREATURE_MAX_SPELLS
];
116 uint32 PetSpellDataId
;
125 uint32 questItems
[6];
129 uint32 MechanicImmuneMask
;
134 SkillType
GetRequiredLootSkill() const
136 if(type_flags
& CREATURE_TYPEFLAGS_HERBLOOT
)
137 return SKILL_HERBALISM
;
138 else if(type_flags
& CREATURE_TYPEFLAGS_MININGLOOT
)
140 else if(type_flags
& CREATURE_TYPEFLAGS_ENGINEERLOOT
)
141 return SKILL_ENGINERING
;
143 return SKILL_SKINNING
; // normal case
146 bool IsExotic() const
148 return (type_flags
& CREATURE_TYPEFLAGS_EXOTIC
);
151 bool isTameable(bool exotic
) const
153 if(type
!= CREATURE_TYPE_BEAST
|| family
== 0 || (type_flags
& CREATURE_TYPEFLAGS_TAMEABLE
) == 0)
156 // if can tame exotic then can tame any temable
157 return exotic
|| !IsExotic();
161 struct CreatureLocale
163 std::vector
<std::string
> Name
;
164 std::vector
<std::string
> SubName
;
167 struct GossipMenuItemsLocale
169 std::vector
<std::string
> OptionText
;
170 std::vector
<std::string
> BoxText
;
173 struct PointOfInterestLocale
175 std::vector
<std::string
> IconName
;
181 uint32 equipentry
[3];
184 // from `creature` table
187 uint32 id
; // entry in creature_template
196 uint32 spawntimesecs
;
198 uint32 currentwaypoint
;
206 struct CreatureDataAddonAura
212 // from `creature_addon` table
213 struct CreatureDataAddon
221 CreatureDataAddonAura
const* auras
; // loaded as char* "spell1 eff1 spell2 eff2 ... "
224 struct CreatureModelInfo
227 float bounding_radius
;
230 uint32 modelid_other_gender
;
233 enum InhabitTypeValues
238 INHABIT_ANYWHERE
= INHABIT_GROUND
| INHABIT_WATER
| INHABIT_AIR
241 // Enums used by StringTextData::Type (CreatureEventAI)
246 CHAT_TYPE_TEXT_EMOTE
= 2,
247 CHAT_TYPE_BOSS_EMOTE
= 3,
248 CHAT_TYPE_WHISPER
= 4,
249 CHAT_TYPE_BOSS_WHISPER
= 5,
250 CHAT_TYPE_ZONE_YELL
= 6
253 //Selection method used by SelectTarget (CreatureEventAI)
256 ATTACKING_TARGET_RANDOM
= 0, //Just selects a random target
257 ATTACKING_TARGET_TOPAGGRO
, //Selects targes from top aggro to bottom
258 ATTACKING_TARGET_BOTTOMAGGRO
, //Selects targets from bottom aggro to top
260 ATTACKING_TARGET_RANDOM_PLAYER, //Just selects a random target (player only)
261 ATTACKING_TARGET_TOPAGGRO_PLAYER, //Selects targes from top aggro to bottom (player only)
262 ATTACKING_TARGET_BOTTOMAGGRO_PLAYER, //Selects targets from bottom aggro to top (player only)
266 // GCC have alternative #pragma pack() syntax and old gcc version not support pack(pop), also any gcc version not support it at some platform
267 #if defined( __GNUC__ )
276 VendorItem(uint32 _item
, uint32 _maxcount
, uint32 _incrtime
, uint32 _ExtendedCost
)
277 : item(_item
), maxcount(_maxcount
), incrtime(_incrtime
), ExtendedCost(_ExtendedCost
) {}
280 uint32 maxcount
; // 0 for infinity item amount
281 uint32 incrtime
; // time for restore items amount if maxcount != 0
284 typedef std::vector
<VendorItem
*> VendorItemList
;
286 struct VendorItemData
288 VendorItemList m_items
;
290 VendorItem
* GetItem(uint32 slot
) const
292 if(slot
>=m_items
.size()) return NULL
;
293 return m_items
[slot
];
295 bool Empty() const { return m_items
.empty(); }
296 uint8
GetItemCount() const { return m_items
.size(); }
297 void AddItem( uint32 item
, uint32 maxcount
, uint32 ptime
, uint32 ExtendedCost
)
299 m_items
.push_back(new VendorItem(item
, maxcount
, ptime
, ExtendedCost
));
301 bool RemoveItem( uint32 item_id
);
302 VendorItem
const* FindItem(uint32 item_id
) const;
303 size_t FindItemSlot(uint32 item_id
) const;
307 for (VendorItemList::const_iterator itr
= m_items
.begin(); itr
!= m_items
.end(); ++itr
)
313 struct VendorItemCount
315 explicit VendorItemCount(uint32 _item
, uint32 _count
)
316 : itemId(_item
), count(_count
), lastIncrementTime(time(NULL
)) {}
320 time_t lastIncrementTime
;
323 typedef std::list
<VendorItemCount
> VendorItemCounts
;
327 TrainerSpell() : spell(0), spellCost(0), reqSkill(0), reqSkillValue(0), reqLevel(0), learnedSpell(0) {}
329 TrainerSpell(uint32 _spell
, uint32 _spellCost
, uint32 _reqSkill
, uint32 _reqSkillValue
, uint32 _reqLevel
, uint32 _learnedspell
)
330 : spell(_spell
), spellCost(_spellCost
), reqSkill(_reqSkill
), reqSkillValue(_reqSkillValue
), reqLevel(_reqLevel
), learnedSpell(_learnedspell
)
336 uint32 reqSkillValue
;
341 bool IsCastable() const { return learnedSpell
!= spell
; }
344 typedef UNORDERED_MAP
<uint32
/*spellid*/, TrainerSpell
> TrainerSpellMap
;
346 struct TrainerSpellData
348 TrainerSpellData() : trainerType(0) {}
350 TrainerSpellMap spellList
;
351 uint32 trainerType
; // trainer type based at trainer spells, can be different from creature_template value.
352 // req. for correct show non-prof. trainers like weaponmaster, allowed values 0 and 2.
353 TrainerSpell
const* Find(uint32 spell_id
) const;
354 void Clear() { spellList
.clear(); }
357 typedef std::map
<uint32
,time_t> CreatureSpellCooldowns
;
359 // max different by z coordinate for creature aggro reaction
360 #define CREATURE_Z_ATTACK_RANGE 3
362 #define MAX_VENDOR_ITEMS 150 // Limitation in 3.x.x item count in SMSG_LIST_INVENTORY
364 class MANGOS_DLL_SPEC Creature
: public Unit
374 void RemoveFromWorld();
376 bool Create(uint32 guidlow
, Map
*map
, uint32 phaseMask
, uint32 Entry
, uint32 team
, const CreatureData
*data
= NULL
);
377 bool LoadCreaturesAddon(bool reload
= false);
378 void SelectLevel(const CreatureInfo
*cinfo
);
379 void LoadEquipment(uint32 equip_entry
, bool force
=false);
381 uint32
GetDBTableGUIDLow() const { return m_DBTableGuid
; }
382 char const* GetSubName() const { return GetCreatureInfo()->SubName
; }
384 void Update( uint32 time
); // overwrited Unit::Update
385 void GetRespawnCoord(float &x
, float &y
, float &z
, float* ori
= NULL
, float* dist
=NULL
) const;
386 uint32
GetEquipmentId() const { return m_equipmentId
; }
388 bool isPet() const { return m_isPet
; }
389 bool isVehicle() const { return m_isVehicle
; }
390 void SetCorpseDelay(uint32 delay
) { m_corpseDelay
= delay
; }
391 bool isTotem() const { return m_isTotem
; }
392 bool isRacialLeader() const { return GetCreatureInfo()->RacialLeader
; }
393 bool isCivilian() const { return GetCreatureInfo()->flags_extra
& CREATURE_FLAG_EXTRA_CIVILIAN
; }
394 bool canWalk() const { return GetCreatureInfo()->InhabitType
& INHABIT_GROUND
; }
395 bool canSwim() const { return GetCreatureInfo()->InhabitType
& INHABIT_WATER
; }
396 bool canFly() const { return GetCreatureInfo()->InhabitType
& INHABIT_AIR
; }
397 ///// TODO RENAME THIS!!!!!
398 bool isCanTrainingOf(Player
* player
, bool msg
) const;
399 bool isCanInteractWithBattleMaster(Player
* player
, bool msg
) const;
400 bool isCanTrainingAndResetTalentsOf(Player
* pPlayer
) const;
401 bool IsOutOfThreatArea(Unit
* pVictim
) const;
402 bool IsImmunedToSpell(SpellEntry
const* spellInfo
);
403 // redefine Unit::IsImmunedToSpell
404 bool IsImmunedToSpellEffect(SpellEntry
const* spellInfo
, uint32 index
) const;
405 // redefine Unit::IsImmunedToSpellEffect
411 uint32 rank
= GetCreatureInfo()->rank
;
412 return rank
!= CREATURE_ELITE_NORMAL
&& rank
!= CREATURE_ELITE_RARE
;
415 bool isWorldBoss() const
420 return GetCreatureInfo()->rank
== CREATURE_ELITE_WORLDBOSS
;
423 uint32
getLevelForTarget(Unit
const* target
) const; // overwrite Unit::getLevelForTarget for boss level support
425 bool IsInEvadeMode() const;
427 bool AIM_Initialize();
429 void AI_SendMoveToPacket(float x
, float y
, float z
, uint32 time
, MonsterMovementFlags MovementFlags
, uint8 type
);
430 CreatureAI
* AI() { return i_AI
; }
432 void AddMonsterMoveFlag(MonsterMovementFlags f
) { m_monsterMoveFlags
= MonsterMovementFlags(m_monsterMoveFlags
| f
); }
433 void RemoveMonsterMoveFlag(MonsterMovementFlags f
) { m_monsterMoveFlags
= MonsterMovementFlags(m_monsterMoveFlags
& ~f
); }
434 bool HasMonsterMoveFlag(MonsterMovementFlags f
) const { return m_monsterMoveFlags
& f
; }
435 MonsterMovementFlags
GetMonsterMoveFlags() const { return m_monsterMoveFlags
; }
436 void SetMonsterMoveFlags(MonsterMovementFlags f
) { m_monsterMoveFlags
= f
; }
438 void SendMonsterMoveWithSpeed(float x
, float y
, float z
, uint32 transitTime
= 0, Player
* player
= NULL
);
439 void SendMonsterMoveWithSpeedToCurrentDestination(Player
* player
= NULL
);
441 uint32
GetShieldBlockValue() const //dunno mob block value
443 return (getLevel()/2 + uint32(GetStat(STAT_STRENGTH
)/20));
446 SpellSchoolMask
GetMeleeDamageSchoolMask() const { return m_meleeDamageSchoolMask
; }
447 void SetMeleeDamageSchool(SpellSchools school
) { m_meleeDamageSchoolMask
= SpellSchoolMask(1 << school
); }
449 void _AddCreatureSpellCooldown(uint32 spell_id
, time_t end_time
);
450 void _AddCreatureCategoryCooldown(uint32 category
, time_t apply_time
);
451 void AddCreatureSpellCooldown(uint32 spellid
);
452 bool HasSpellCooldown(uint32 spell_id
) const;
453 bool HasCategoryCooldown(uint32 spell_id
) const;
455 bool HasSpell(uint32 spellID
) const;
457 bool UpdateEntry(uint32 entry
, uint32 team
=ALLIANCE
, const CreatureData
* data
=NULL
);
458 bool UpdateStats(Stats stat
);
459 bool UpdateAllStats();
460 void UpdateResistances(uint32 school
);
462 void UpdateMaxHealth();
463 void UpdateMaxPower(Powers power
);
464 void UpdateAttackPowerAndDamage(bool ranged
= false);
465 void UpdateDamagePhysical(WeaponAttackType attType
);
466 uint32
GetCurrentEquipmentId() { return m_equipmentId
; }
467 float GetSpellDamageMod(int32 Rank
);
469 VendorItemData
const* GetVendorItems() const;
470 uint32
GetVendorItemCurrentCount(VendorItem
const* vItem
);
471 uint32
UpdateVendorItemCurrentCount(VendorItem
const* vItem
, uint32 used_count
);
473 TrainerSpellData
const* GetTrainerSpells() const;
475 CreatureInfo
const *GetCreatureInfo() const { return m_creatureInfo
; }
476 CreatureDataAddon
const* GetCreatureAddon() const;
478 std::string
GetAIName() const;
479 std::string
GetScriptName() const;
480 uint32
GetScriptId() const;
482 void Say(int32 textId
, uint32 language
, uint64 TargetGuid
) { MonsterSay(textId
,language
,TargetGuid
); }
483 void Yell(int32 textId
, uint32 language
, uint64 TargetGuid
) { MonsterYell(textId
,language
,TargetGuid
); }
484 void TextEmote(int32 textId
, uint64 TargetGuid
, bool IsBossEmote
= false) { MonsterTextEmote(textId
,TargetGuid
,IsBossEmote
); }
485 void Whisper(int32 textId
, uint64 receiver
, bool IsBossWhisper
= false) { MonsterWhisper(textId
,receiver
,IsBossWhisper
); }
486 void YellToZone(int32 textId
, uint32 language
, uint64 TargetGuid
) { MonsterYellToZone(textId
,language
,TargetGuid
); }
488 // overwrite WorldObject function for proper name localization
489 const char* GetNameForLocaleIdx(int32 locale_idx
) const;
491 void setDeathState(DeathState s
); // overwrite virtual Unit::setDeathState
494 bool LoadFromDB(uint32 guid
, Map
*map
);
497 virtual void SaveToDB(uint32 mapid
, uint8 spawnMask
, uint32 phaseMask
);
498 virtual void DeleteFromDB(); // overwrited in Pet
501 bool lootForPickPocketed
;
503 Player
*GetLootRecipient() const;
504 bool hasLootRecipient() const { return m_lootRecipient
!=0; }
506 void SetLootRecipient (Unit
* unit
);
507 void AllLootRemovedFromCorpse();
509 SpellEntry
const *reachWithSpellAttack(Unit
*pVictim
);
510 SpellEntry
const *reachWithSpellCure(Unit
*pVictim
);
512 uint32 m_spells
[CREATURE_MAX_SPELLS
];
513 CreatureSpellCooldowns m_CreatureSpellCooldowns
;
514 CreatureSpellCooldowns m_CreatureCategoryCooldowns
;
515 uint32 m_GlobalCooldown
;
517 float GetAttackDistance(Unit
const* pl
) const;
519 void SendAIReaction(AiReaction reactionType
);
521 void DoFleeToGetAssistance();
522 void CallForHelp(float fRadius
);
523 void CallAssistance();
524 void SetNoCallAssistance(bool val
) { m_AlreadyCallAssistance
= val
; }
525 void SetNoSearchAssistance(bool val
) { m_AlreadySearchedAssistance
= val
; }
526 bool HasSearchedAssistance() { return m_AlreadySearchedAssistance
; }
527 bool CanAssistTo(const Unit
* u
, const Unit
* enemy
, bool checkfaction
= true) const;
529 MovementGeneratorType
GetDefaultMovementType() const { return m_defaultMovementType
; }
530 void SetDefaultMovementType(MovementGeneratorType mgt
) { m_defaultMovementType
= mgt
; }
532 // for use only in LoadHelper, Map::Add Map::CreatureCellRelocation
533 Cell
const& GetCurrentCell() const { return m_currentCell
; }
534 void SetCurrentCell(Cell
const& cell
) { m_currentCell
= cell
; }
536 bool IsVisibleInGridForPlayer(Player
* pl
) const;
539 bool isDeadByDefault() const { return m_isDeadByDefault
; };
541 void ForcedDespawn(uint32 timeMSToDespawn
= 0);
543 time_t const& GetRespawnTime() const { return m_respawnTime
; }
544 time_t GetRespawnTimeEx() const;
545 void SetRespawnTime(uint32 respawn
) { m_respawnTime
= respawn
? time(NULL
) + respawn
: 0; }
547 void SaveRespawnTime();
549 uint32
GetRespawnDelay() const { return m_respawnDelay
; }
550 void SetRespawnDelay(uint32 delay
) { m_respawnDelay
= delay
; }
552 float GetRespawnRadius() const { return m_respawnradius
; }
553 void SetRespawnRadius(float dist
) { m_respawnradius
= dist
; }
555 uint32 m_groupLootTimer
; // (msecs)timer used for group loot
556 uint64 lootingGroupLeaderGUID
; // used to find group which is looting corpse
558 void SendZoneUnderAttackMessage(Player
* attacker
);
560 void SetInCombatWithZone();
562 bool hasQuest(uint32 quest_id
) const;
563 bool hasInvolvedQuest(uint32 quest_id
) const;
565 GridReference
<Creature
> &GetGridRef() { return m_gridRef
; }
566 bool isRegeneratingHealth() { return m_regenHealth
; }
567 virtual uint8
GetPetAutoSpellSize() const { return CREATURE_MAX_SPELLS
; }
568 virtual uint32
GetPetAutoSpellOnPos(uint8 pos
) const
570 if (pos
>= CREATURE_MAX_SPELLS
|| m_charmInfo
->GetCharmSpell(pos
)->GetType() != ACT_ENABLED
)
573 return m_charmInfo
->GetCharmSpell(pos
)->GetAction();
576 void SetCombatStartPosition(float x
, float y
, float z
) { CombatStartX
= x
; CombatStartY
= y
; CombatStartZ
= z
; }
577 void GetCombatStartPosition(float &x
, float &y
, float &z
) { x
= CombatStartX
; y
= CombatStartY
; z
= CombatStartZ
; }
579 void SetSummonPoint(float fX
, float fY
, float fZ
, float fOrient
) { m_summonXpoint
= fX
; m_summonYpoint
= fY
; m_summonZpoint
= fZ
; m_summonOrientation
= fOrient
; }
580 void GetSummonPoint(float &fX
, float &fY
, float &fZ
, float &fOrient
) const { fX
= m_summonXpoint
; fY
= m_summonYpoint
; fZ
= m_summonZpoint
; fOrient
= m_summonOrientation
; }
582 uint32
GetGlobalCooldown() const { return m_GlobalCooldown
; }
584 void SetDeadByDefault (bool death_state
) { m_isDeadByDefault
= death_state
; }
586 bool isActiveObject() const { return m_isActiveObject
|| HasAuraType(SPELL_AURA_BIND_SIGHT
) || HasAuraType(SPELL_AURA_FAR_SIGHT
); }
587 void SetActiveObjectState(bool on
);
589 void SendAreaSpiritHealerQueryOpcode(Player
*pl
);
592 bool CreateFromProto(uint32 guidlow
,uint32 Entry
,uint32 team
, const CreatureData
*data
= NULL
);
593 bool InitEntry(uint32 entry
, uint32 team
=ALLIANCE
, const CreatureData
* data
=NULL
);
596 VendorItemCounts m_vendorItemCounts
;
598 void _RealtimeSetCreatureInfo();
600 static float _GetHealthMod(int32 Rank
);
601 static float _GetDamageMod(int32 Rank
);
604 uint64 m_lootRecipient
;
607 uint32 m_deathTimer
; // (msecs)timer for death or corpse disappearance
608 time_t m_respawnTime
; // (secs) time of next respawn
609 uint32 m_respawnDelay
; // (secs) delay between corpse disappearance and respawning
610 uint32 m_corpseDelay
; // (secs) delay between death and corpse disappearance
611 float m_respawnradius
;
613 bool m_isPet
; // set only in Pet::Pet
614 bool m_isVehicle
; // set only in Vehicle::Vehicle
615 bool m_isTotem
; // set only in Totem::Totem
616 void RegenerateMana();
617 void RegenerateHealth();
618 MovementGeneratorType m_defaultMovementType
;
619 Cell m_currentCell
; // store current cell where creature listed
620 uint32 m_DBTableGuid
; ///< For new or temporary creatures is 0 for saved it is lowguid
621 uint32 m_equipmentId
;
623 bool m_AlreadyCallAssistance
;
624 bool m_AlreadySearchedAssistance
;
627 bool m_isDeadByDefault
;
629 SpellSchoolMask m_meleeDamageSchoolMask
;
630 uint32 m_originalEntry
;
636 float m_summonXpoint
;
637 float m_summonYpoint
;
638 float m_summonZpoint
;
639 float m_summonOrientation
;
642 GridReference
<Creature
> m_gridRef
;
643 CreatureInfo
const* m_creatureInfo
; // in difficulty mode > 0 can different from ObjMgr::GetCreatureTemplate(GetEntry())
644 bool m_isActiveObject
;
645 MonsterMovementFlags m_monsterMoveFlags
;
648 class AssistDelayEvent
: public BasicEvent
651 AssistDelayEvent(const uint64
& victim
, Unit
& owner
) : BasicEvent(), m_victim(victim
), m_owner(owner
) { }
653 bool Execute(uint64 e_time
, uint32 p_time
);
654 void AddAssistant(const uint64
& guid
) { m_assistants
.push_back(guid
); }
659 std::list
<uint64
> m_assistants
;
663 class ForcedDespawnDelayEvent
: public BasicEvent
666 ForcedDespawnDelayEvent(Creature
& owner
) : BasicEvent(), m_owner(owner
) { }
667 bool Execute(uint64 e_time
, uint32 p_time
);