2010-10-07 15:35:36 +02:00
/*
2017-01-01 16:21:30 +01:00
* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-11 14:16:25 -05:00
*/
2009-10-17 15:51:44 -07:00
2013-03-20 14:18:26 +01:00
#include "Creature.h"
2012-11-20 12:30:30 +01:00
#include "BattlegroundMgr.h"
#include "CellImpl.h"
2008-10-11 14:16:25 -05:00
#include "Common.h"
#include "CreatureAI.h"
#include "CreatureAISelector.h"
2012-11-20 12:30:30 +01:00
#include "CreatureGroups.h"
#include "DatabaseEnv.h"
2010-06-08 15:50:23 -06:00
#include "Formulas.h"
2012-11-20 12:30:30 +01:00
#include "GameEventMgr.h"
#include "GossipDef.h"
2010-06-08 15:50:23 -06:00
#include "GridNotifiers.h"
#include "GridNotifiersImpl.h"
2010-11-29 07:50:31 +01:00
#include "Group.h"
2012-11-20 12:30:30 +01:00
#include "GroupMgr.h"
#include "InstanceScript.h"
#include "Log.h"
#include "LootMgr.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "PoolMgr.h"
#include "QuestDef.h"
#include "SpellAuraEffects.h"
#include "SpellMgr.h"
#include "TemporarySummon.h"
#include "Util.h"
#include "Vehicle.h"
#include "World.h"
#include "WorldPacket.h"
2014-11-19 00:30:31 +02:00
#include "CombatPackets.h"
2015-04-04 18:03:56 +02:00
#include "MiscPackets.h"
2012-11-20 12:30:30 +01:00
2013-10-16 18:37:29 +02:00
#include "Transport.h"
2016-08-31 00:59:04 +02:00
#include "ScriptedGossip.h"
2010-06-25 00:18:01 +02:00
2008-10-11 14:16:25 -05:00
TrainerSpell const * TrainerSpellData :: Find ( uint32 spell_id ) const
{
2009-02-16 20:10:04 -06:00
TrainerSpellMap :: const_iterator itr = spellList . find ( spell_id );
if ( itr != spellList . end ())
return & itr -> second ;
2009-10-17 15:51:44 -07:00
2016-04-02 02:50:44 +02:00
return nullptr ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2012-08-14 18:11:06 +01:00
bool VendorItemData :: RemoveItem ( uint32 item_id , uint8 type )
2008-10-11 14:16:25 -05:00
{
2010-04-30 22:35:37 +02:00
bool found = false ;
2010-05-05 14:30:53 +02:00
for ( VendorItemList :: iterator i = m_items . begin (); i != m_items . end ();)
2008-10-11 14:16:25 -05:00
{
2012-08-14 18:11:06 +01:00
if (( * i ) -> item == item_id && ( * i ) -> Type == type )
2009-12-24 10:20:15 +01:00
{
2010-05-05 14:30:53 +02:00
i = m_items . erase ( i ++ );
2010-04-30 22:35:37 +02:00
found = true ;
2009-12-24 10:20:15 +01:00
}
2010-05-05 14:30:53 +02:00
else
++ i ;
2008-10-11 14:16:25 -05:00
}
2010-04-30 22:35:37 +02:00
return found ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2012-08-14 18:11:06 +01:00
VendorItem const * VendorItemData :: FindItemCostPair ( uint32 item_id , uint32 extendedCost , uint8 type ) const
2008-10-11 14:16:25 -05:00
{
2010-04-07 22:59:46 +02:00
for ( VendorItemList :: const_iterator i = m_items . begin (); i != m_items . end (); ++ i )
2012-08-14 18:11:06 +01:00
if (( * i ) -> item == item_id && ( * i ) -> ExtendedCost == extendedCost && ( * i ) -> Type == type )
2009-12-24 10:20:15 +01:00
return * i ;
2016-04-02 02:50:44 +02:00
return nullptr ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-28 22:57:08 +02:00
uint32 CreatureTemplate :: GetRandomValidModelId () const
2008-10-27 11:48:45 -05:00
{
2009-09-24 11:26:27 -07:00
uint8 c = 0 ;
2008-10-27 11:48:45 -05:00
uint32 modelIDs [ 4 ];
2009-10-17 15:51:44 -07:00
2009-09-21 07:52:16 +02:00
if ( Modelid1 ) modelIDs [ c ++ ] = Modelid1 ;
if ( Modelid2 ) modelIDs [ c ++ ] = Modelid2 ;
if ( Modelid3 ) modelIDs [ c ++ ] = Modelid3 ;
if ( Modelid4 ) modelIDs [ c ++ ] = Modelid4 ;
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
return (( c > 0 ) ? modelIDs [ urand ( 0 , c - 1 )] : 0 );
2008-10-27 11:48:45 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-28 22:57:08 +02:00
uint32 CreatureTemplate :: GetFirstValidModelId () const
2008-10-27 11:48:45 -05:00
{
2010-04-07 19:14:10 +02:00
if ( Modelid1 ) return Modelid1 ;
if ( Modelid2 ) return Modelid2 ;
if ( Modelid3 ) return Modelid3 ;
if ( Modelid4 ) return Modelid4 ;
2008-10-27 11:48:45 -05:00
return 0 ;
}
2009-10-17 15:51:44 -07:00
2015-12-05 18:47:35 +01:00
uint32 CreatureTemplate :: GetFirstInvisibleModel () const
{
CreatureModelInfo const * modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid1 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid1 ;
modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid2 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid2 ;
modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid3 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid3 ;
modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid4 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid4 ;
2015-12-06 00:19:33 +01:00
return 11686 ;
2015-12-05 18:47:35 +01:00
}
uint32 CreatureTemplate :: GetFirstVisibleModel () const
{
CreatureModelInfo const * modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid1 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && ! modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid1 ;
modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid2 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && ! modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid2 ;
modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid3 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && ! modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid3 ;
modelInfo = sObjectMgr -> GetCreatureModelInfo ( Modelid4 );
2015-12-06 00:19:33 +01:00
if ( modelInfo && ! modelInfo -> is_trigger )
2015-12-05 18:47:35 +01:00
return Modelid4 ;
2015-12-06 00:19:33 +01:00
return 17519 ;
2015-12-05 18:47:35 +01:00
}
2008-11-25 16:36:50 -06:00
bool AssistDelayEvent :: Execute ( uint64 /*e_time*/ , uint32 /*p_time*/ )
{
2014-05-20 21:21:33 +02:00
if ( Unit * victim = ObjectAccessor :: GetUnit ( m_owner , m_victim ))
2008-11-25 16:36:50 -06:00
{
while ( ! m_assistants . empty ())
{
2014-05-20 21:21:33 +02:00
Creature * assistant = ObjectAccessor :: GetCreature ( m_owner , * m_assistants . begin ());
2008-11-25 16:36:50 -06:00
m_assistants . pop_front ();
2009-10-17 15:51:44 -07:00
2008-11-25 16:36:50 -06:00
if ( assistant && assistant -> CanAssistTo ( & m_owner , victim ))
{
2008-12-01 12:59:39 -06:00
assistant -> SetNoCallAssistance ( true );
2008-11-25 16:36:50 -06:00
assistant -> CombatStart ( victim );
2010-04-07 19:14:10 +02:00
if ( assistant -> IsAIEnabled )
2008-11-25 16:36:50 -06:00
assistant -> AI () -> AttackStart ( victim );
}
}
}
return true ;
}
2009-10-17 15:51:44 -07:00
2010-03-03 02:48:31 +02:00
CreatureBaseStats const * CreatureBaseStats :: GetBaseStats ( uint8 level , uint8 unitClass )
2010-01-10 22:02:03 +01:00
{
2010-12-22 21:25:23 +01:00
return sObjectMgr -> GetCreatureBaseStats ( level , unitClass );
2010-01-10 22:02:03 +01:00
}
2010-01-16 19:51:59 +03:00
bool ForcedDespawnDelayEvent :: Execute ( uint64 /*e_time*/ , uint32 /*p_time*/ )
{
2016-08-25 23:04:21 +02:00
m_owner . DespawnOrUnsummon ( 0 , m_respawnTimer ); // since we are here, we are not TempSummon as object type cannot change during runtime
2010-01-16 19:51:59 +03:00
return true ;
}
2013-10-16 18:37:29 +02:00
Creature :: Creature ( bool isWorldObject ) : Unit ( isWorldObject ), MapObject (),
2014-10-22 00:35:00 +02:00
m_groupLootTimer ( 0 ), m_PlayerDamageReq ( 0 ),
_pickpocketLootRestore ( 0 ), m_corpseRemoveTime ( 0 ), m_respawnTime ( 0 ),
2016-01-13 15:33:17 +01:00
m_respawnDelay ( 300 ), m_corpseDelay ( 60 ), m_respawnradius ( 0.0f ), m_boundaryCheckTime ( 2500 ), m_combatPulseTime ( 0 ), m_combatPulseDelay ( 0 ), m_reactState ( REACT_AGGRESSIVE ),
2015-02-25 00:13:14 +01:00
m_defaultMovementType ( IDLE_MOTION_TYPE ), m_spawnId ( UI64LIT ( 0 )), m_equipmentId ( 0 ), m_originalEquipmentId ( 0 ), m_AlreadyCallAssistance ( false ),
2016-03-24 16:12:53 +01:00
m_AlreadySearchedAssistance ( false ), m_regenHealth ( true ), m_cannotReachTarget ( false ), m_cannotReachTimer ( 0 ), m_AI_locked ( false ), m_meleeDamageSchoolMask ( SPELL_SCHOOL_MASK_NORMAL ),
2016-08-21 11:07:38 +02:00
m_originalEntry ( 0 ), m_homePosition (), m_transportHomePosition (), m_creatureInfo ( nullptr ), m_creatureData ( nullptr ), m_waypointID ( 0 ), m_path_id ( 0 ), m_formation ( nullptr ), m_focusSpell ( nullptr ), m_focusDelay ( 0 ), m_shouldReacquireTarget ( false ), m_suppressedOrientation ( 0.0f )
2008-10-11 14:16:25 -05:00
{
2010-01-10 01:23:15 +01:00
m_regenTimer = CREATURE_REGEN_INTERVAL ;
2008-10-11 14:16:25 -05:00
m_valuesCount = UNIT_END ;
2014-10-26 22:06:48 +01:00
_dynamicValuesCount = UNIT_DYNAMIC_END ;
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
for ( uint8 i = 0 ; i < MAX_CREATURE_SPELLS ; ++ i )
2008-10-11 14:16:25 -05:00
m_spells [ i ] = 0 ;
2009-10-17 15:51:44 -07:00
2009-04-10 18:11:09 +02:00
DisableReputationGain = false ;
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
m_SightDistance = sWorld -> getFloatConfig ( CONFIG_SIGHT_MONSTER );
2009-08-23 16:27:00 -05:00
m_CombatDistance = 0 ; //MELEE_RANGE;
2009-10-28 16:31:19 -07:00
ResetLootMode (); // restore default loot mode
2016-04-02 02:50:44 +02:00
m_TriggerJustRespawned = false ;
2011-12-20 17:05:08 -05:00
m_isTempWorldObject = false ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
Creature ::~ Creature ()
{
2010-06-21 23:20:58 +02:00
delete i_AI ;
2016-04-02 02:50:44 +02:00
i_AI = nullptr ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
//if (m_uint32Values)
2013-11-08 10:50:51 +01:00
// TC_LOG_ERROR("entities.unit", "Deconstruct Creature Entry = %u", GetEntry());
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: AddToWorld ()
{
///- Register the creature for guid lookup
2010-04-07 19:14:10 +02:00
if ( ! IsInWorld ())
2009-03-23 20:13:37 -06:00
{
2014-01-04 18:22:06 +01:00
if ( GetZoneScript ())
GetZoneScript () -> OnCreatureCreate ( this );
2015-02-25 00:13:14 +01:00
GetMap () -> GetObjectsStore (). Insert < Creature > ( GetGUID (), this );
if ( m_spawnId )
GetMap () -> GetCreatureBySpawnIdStore (). insert ( std :: make_pair ( m_spawnId , this ));
2009-03-23 20:13:37 -06:00
Unit :: AddToWorld ();
2009-09-19 12:13:08 -07:00
SearchFormation ();
2009-04-06 20:33:52 -05:00
AIM_Initialize ();
2010-04-07 19:14:10 +02:00
if ( IsVehicle ())
2009-08-28 11:26:20 -05:00
GetVehicleKit () -> Install ();
2009-03-23 20:13:37 -06:00
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: RemoveFromWorld ()
{
2010-04-07 19:14:10 +02:00
if ( IsInWorld ())
2009-03-23 20:13:37 -06:00
{
2014-01-04 18:22:06 +01:00
if ( GetZoneScript ())
GetZoneScript () -> OnCreatureRemove ( this );
2015-02-25 00:13:14 +01:00
2010-04-07 19:14:10 +02:00
if ( m_formation )
2012-05-30 08:01:02 +02:00
sFormationMgr -> RemoveCreatureFromGroup ( m_formation , this );
2015-02-25 00:13:14 +01:00
2009-03-23 20:13:37 -06:00
Unit :: RemoveFromWorld ();
2015-02-25 00:13:14 +01:00
if ( m_spawnId )
Trinity :: Containers :: MultimapErasePair ( GetMap () -> GetCreatureBySpawnIdStore (), m_spawnId , this );
GetMap () -> GetObjectsStore (). Remove < Creature > ( GetGUID ());
2009-03-23 20:13:37 -06:00
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2009-08-14 22:26:07 -05:00
void Creature :: DisappearAndDie ()
{
2009-08-28 11:26:20 -05:00
DestroyForNearbyPlayers ();
//SetVisibility(VISIBILITY_OFF);
//ObjectAccessor::UpdateObjectVisibility(this);
2013-06-11 19:54:27 -02:30
if ( IsAlive ())
2009-08-26 19:28:21 -05:00
setDeathState ( JUST_DIED );
2010-09-15 06:22:54 +02:00
RemoveCorpse ( false );
2009-08-14 22:26:07 -05:00
}
2009-10-17 15:51:44 -07:00
2009-09-19 12:13:08 -07:00
void Creature :: SearchFormation ()
2008-12-15 00:07:21 +02:00
{
2013-06-11 19:54:27 -02:30
if ( IsSummon ())
2009-04-04 15:04:24 -06:00
return ;
2009-10-17 15:51:44 -07:00
2015-02-25 00:13:14 +01:00
ObjectGuid :: LowType lowguid = GetSpawnId ();
2010-04-07 19:14:10 +02:00
if ( ! lowguid )
2009-04-06 13:27:42 -05:00
return ;
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
CreatureGroupInfoType :: iterator frmdata = sFormationMgr -> CreatureGroupMap . find ( lowguid );
if ( frmdata != sFormationMgr -> CreatureGroupMap . end ())
sFormationMgr -> AddCreatureToGroup ( frmdata -> second -> leaderGUID , this );
2008-12-15 00:07:21 +02:00
}
2009-10-17 15:51:44 -07:00
2010-09-15 06:22:54 +02:00
void Creature :: RemoveCorpse ( bool setSpawnTime )
2008-10-11 14:16:25 -05:00
{
2011-06-27 20:22:53 +02:00
if ( getDeathState () != CORPSE )
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2010-09-15 06:22:54 +02:00
m_corpseRemoveTime = time ( NULL );
2008-10-11 14:16:25 -05:00
setDeathState ( DEAD );
2011-07-12 00:19:50 +02:00
RemoveAllAuras ();
2010-02-03 16:53:40 +02:00
UpdateObjectVisibility ();
2008-10-11 14:16:25 -05:00
loot . clear ();
2009-10-09 20:48:55 -07:00
uint32 respawnDelay = m_respawnDelay ;
2009-12-06 16:41:05 +01:00
if ( IsAIEnabled )
2009-10-09 20:48:55 -07:00
AI () -> CorpseRemoved ( respawnDelay );
2009-10-17 15:51:44 -07:00
2010-09-15 06:22:54 +02:00
// Should get removed later, just keep "compatibility" with scripts
2011-06-12 02:30:32 +02:00
if ( setSpawnTime )
2010-09-15 06:22:54 +02:00
m_respawnTime = time ( NULL ) + respawnDelay ;
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
float x , y , z , o ;
2011-10-18 10:53:34 -04:00
GetRespawnPosition ( x , y , z , & o );
2011-04-29 20:47:02 +02:00
SetHomePosition ( x , y , z , o );
GetMap () -> CreatureRelocation ( this , x , y , z , o );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
/**
* change the entry of creature until respawn
*/
2014-04-28 20:05:36 +02:00
bool Creature :: InitEntry ( uint32 entry , CreatureData const * data /*= nullptr*/ )
2008-10-11 14:16:25 -05:00
{
2013-03-20 14:18:26 +01:00
CreatureTemplate const * normalInfo = sObjectMgr -> GetCreatureTemplate ( entry );
2010-04-07 19:14:10 +02:00
if ( ! normalInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "sql.sql" , "Creature::InitEntry creature entry %u does not exist." , entry );
2008-10-11 14:16:25 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2009-12-17 11:06:50 +01:00
// get difficulty 1 mode entry
2015-01-10 01:35:47 +01:00
CreatureTemplate const * cinfo = nullptr ;
DifficultyEntry const * difficultyEntry = sDifficultyStore . LookupEntry ( GetMap () -> GetSpawnMode ());
while ( ! cinfo && difficultyEntry )
2008-10-11 14:16:25 -05:00
{
2015-01-10 01:35:47 +01:00
int32 idx = CreatureTemplate :: DifficultyIDToDifficultyEntryIndex ( difficultyEntry -> ID );
if ( idx == - 1 )
break ;
2011-02-04 15:45:59 +01:00
2015-01-10 01:35:47 +01:00
if ( normalInfo -> DifficultyEntry [ idx ])
{
cinfo = sObjectMgr -> GetCreatureTemplate ( normalInfo -> DifficultyEntry [ idx ]);
break ;
2008-10-11 14:16:25 -05:00
}
2011-02-04 15:45:59 +01:00
2015-01-10 01:35:47 +01:00
if ( ! difficultyEntry -> FallbackDifficultyID )
break ;
difficultyEntry = sDifficultyStore . LookupEntry ( difficultyEntry -> FallbackDifficultyID );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2015-01-10 01:35:47 +01:00
if ( ! cinfo )
cinfo = normalInfo ;
2013-02-13 20:24:23 +01:00
// Initialize loot duplicate count depending on raid difficulty
2013-02-14 11:41:20 +01:00
if ( GetMap () -> Is25ManRaid ())
2013-02-13 20:24:23 +01:00
loot . maxDuplicates = 3 ;
2013-03-20 14:18:26 +01:00
SetEntry ( entry ); // normal entry always
2008-10-11 14:16:25 -05:00
m_creatureInfo = cinfo ; // map mode related always
2009-10-17 15:51:44 -07:00
2009-05-29 21:24:23 -05:00
// equal to player Race field, but creature does not have race
2014-11-30 19:09:26 +01:00
SetByteValue ( UNIT_FIELD_BYTES_0 , UNIT_BYTES_0_OFFSET_RACE , 0 );
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
// known valid are: CLASS_WARRIOR, CLASS_PALADIN, CLASS_ROGUE, CLASS_MAGE
2014-11-30 19:09:26 +01:00
SetByteValue ( UNIT_FIELD_BYTES_0 , UNIT_BYTES_0_OFFSET_CLASS , uint8 ( cinfo -> unit_class ));
2009-10-17 15:51:44 -07:00
2008-10-27 11:48:45 -05:00
// Cancel load if no model defined
if ( ! ( cinfo -> GetFirstValidModelId ()))
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "sql.sql" , "Creature (Entry: %u) has no model defined in table `creature_template`, can't load. " , entry );
2008-10-11 14:16:25 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2013-03-20 11:30:23 +01:00
uint32 displayID = ObjectMgr :: ChooseDisplayId ( GetCreatureTemplate (), data );
2011-09-15 14:08:17 +02:00
CreatureModelInfo const * minfo = sObjectMgr -> GetCreatureModelRandomGender ( & displayID );
2009-06-15 18:02:12 -05:00
if ( ! minfo ) // Cancel load if no model defined
2008-10-11 14:16:25 -05:00
{
2014-01-04 18:22:06 +01:00
TC_LOG_ERROR ( "sql.sql" , "Creature (Entry: %u) has invalid model %u defined in table `creature_template`, can't load." , entry , displayID );
2008-10-11 14:16:25 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2011-04-28 22:11:45 +02:00
SetDisplayId ( displayID );
SetNativeDisplayId ( displayID );
2014-11-30 19:09:26 +01:00
SetByteValue ( UNIT_FIELD_BYTES_0 , UNIT_BYTES_0_OFFSET_GENDER , minfo -> gender );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// Load creature equipment
2016-05-29 18:51:58 +01:00
if ( ! data || data -> equipmentId == 0 )
LoadEquipment (); // use default equipment (if available)
else if ( data && data -> equipmentId != 0 ) // override, 0 means no equipment
2013-02-20 01:17:00 +00:00
{
m_originalEquipmentId = data -> equipmentId ;
2008-10-11 14:16:25 -05:00
LoadEquipment ( data -> equipmentId );
2013-02-20 01:17:00 +00:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
SetName ( normalInfo -> Name ); // at normal entry always
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
SetFloatValue ( UNIT_MOD_CAST_SPEED , 1.0f );
2012-07-23 00:13:13 +02:00
SetFloatValue ( UNIT_MOD_CAST_HASTE , 1.0f );
2016-09-16 17:22:58 +02:00
SetFloatValue ( UNIT_FIELD_MOD_HASTE , 1.0f );
SetFloatValue ( UNIT_FIELD_MOD_RANGED_HASTE , 1.0f );
SetFloatValue ( UNIT_FIELD_MOD_HASTE_REGEN , 1.0f );
SetFloatValue ( UNIT_FIELD_MOD_TIME_RATE , 1.0f );
2009-10-17 15:51:44 -07:00
2016-04-16 01:36:32 +02:00
SetSpeedRate ( MOVE_WALK , cinfo -> speed_walk );
SetSpeedRate ( MOVE_RUN , cinfo -> speed_run );
SetSpeedRate ( MOVE_SWIM , 1.0f ); // using 1.0 rate
SetSpeedRate ( MOVE_FLIGHT , 1.0f ); // using 1.0 rate
2009-10-17 15:51:44 -07:00
2013-06-11 17:28:16 +02:00
// Will set UNIT_FIELD_BOUNDINGRADIUS and UNIT_FIELD_COMBATREACH
2012-05-30 08:01:02 +02:00
SetObjectScale ( cinfo -> scale );
SetFloatValue ( UNIT_FIELD_HOVERHEIGHT , cinfo -> HoverHeight );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// checked at loading
m_defaultMovementType = MovementGeneratorType ( cinfo -> MovementType );
2010-04-07 23:25:02 +02:00
if ( ! m_respawnradius && m_defaultMovementType == RANDOM_MOTION_TYPE )
2008-10-11 14:16:25 -05:00
m_defaultMovementType = IDLE_MOTION_TYPE ;
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
for ( uint8 i = 0 ; i < MAX_CREATURE_SPELLS ; ++ i )
2012-05-30 08:01:02 +02:00
m_spells [ i ] = GetCreatureTemplate () -> spells [ i ];
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2016-08-23 23:46:24 +02:00
bool Creature :: UpdateEntry ( uint32 entry , CreatureData const * data /*= nullptr*/ , bool updateLevel /* = true */ )
2008-10-11 14:16:25 -05:00
{
2014-04-28 20:05:36 +02:00
if ( ! InitEntry ( entry , data ))
2008-10-11 14:16:25 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
CreatureTemplate const * cInfo = GetCreatureTemplate ();
2010-01-10 22:02:03 +01:00
m_regenHealth = cInfo -> RegenHealth ;
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
// creatures always have melee weapon ready if any unless specified otherwise
if ( ! GetCreatureAddon ())
SetSheath ( SHEATH_STATE_MELEE );
2009-10-17 15:51:44 -07:00
2014-04-28 20:05:36 +02:00
setFaction ( cInfo -> faction );
2009-10-17 15:51:44 -07:00
2015-05-04 12:39:36 +02:00
uint32 unit_flags , dynamicflags ;
uint64 npcflag ;
2010-10-09 17:52:00 +02:00
ObjectMgr :: ChooseCreatureFlags ( cInfo , npcflag , unit_flags , dynamicflags , data );
2010-04-07 19:14:10 +02:00
if ( cInfo -> flags_extra & CREATURE_FLAG_EXTRA_WORLDEVENT )
2015-05-04 12:39:36 +02:00
SetUInt64Value ( UNIT_NPC_FLAGS , npcflag | sGameEventMgr -> GetNPCFlag ( this ));
2008-10-14 11:57:03 -05:00
else
2015-05-04 12:39:36 +02:00
SetUInt64Value ( UNIT_NPC_FLAGS , npcflag );
2009-10-17 15:51:44 -07:00
2010-10-09 17:52:00 +02:00
SetUInt32Value ( UNIT_FIELD_FLAGS , unit_flags );
2012-08-30 04:52:35 +02:00
SetUInt32Value ( UNIT_FIELD_FLAGS_2 , cInfo -> unit_flags2 );
2010-10-09 17:52:00 +02:00
2014-10-26 22:06:48 +01:00
SetUInt32Value ( OBJECT_DYNAMIC_FLAGS , dynamicflags );
2009-10-17 15:51:44 -07:00
2009-01-28 00:25:06 -06:00
RemoveFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_IN_COMBAT );
2009-10-17 15:51:44 -07:00
2016-11-25 17:58:48 +01:00
SetBaseAttackTime ( BASE_ATTACK , cInfo -> BaseAttackTime );
SetBaseAttackTime ( OFF_ATTACK , cInfo -> BaseAttackTime );
SetBaseAttackTime ( RANGED_ATTACK , cInfo -> RangeAttackTime );
2014-01-04 18:22:06 +01:00
2016-08-23 23:46:24 +02:00
if ( updateLevel )
SelectLevel ();
2014-01-04 18:22:06 +01:00
2016-09-24 03:50:20 +02:00
UpdateLevelDependantStats ();
2010-01-10 22:02:03 +01:00
SetMeleeDamageSchool ( SpellSchools ( cInfo -> dmgschool ));
2011-04-28 22:57:08 +02:00
SetModifierValue ( UNIT_MOD_RESISTANCE_HOLY , BASE_VALUE , float ( cInfo -> resistance [ SPELL_SCHOOL_HOLY ]));
SetModifierValue ( UNIT_MOD_RESISTANCE_FIRE , BASE_VALUE , float ( cInfo -> resistance [ SPELL_SCHOOL_FIRE ]));
SetModifierValue ( UNIT_MOD_RESISTANCE_NATURE , BASE_VALUE , float ( cInfo -> resistance [ SPELL_SCHOOL_NATURE ]));
SetModifierValue ( UNIT_MOD_RESISTANCE_FROST , BASE_VALUE , float ( cInfo -> resistance [ SPELL_SCHOOL_FROST ]));
SetModifierValue ( UNIT_MOD_RESISTANCE_SHADOW , BASE_VALUE , float ( cInfo -> resistance [ SPELL_SCHOOL_SHADOW ]));
SetModifierValue ( UNIT_MOD_RESISTANCE_ARCANE , BASE_VALUE , float ( cInfo -> resistance [ SPELL_SCHOOL_ARCANE ]));
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
SetCanModifyStats ( true );
UpdateAllStats ();
2009-10-17 15:51:44 -07:00
2009-12-19 19:10:14 +01:00
// checked and error show at loading templates
2014-04-27 18:13:55 -04:00
if ( FactionTemplateEntry const * factionTemplate = sFactionTemplateStore . LookupEntry ( cInfo -> faction ))
2008-10-11 14:16:25 -05:00
{
2014-11-08 19:26:20 +02:00
if ( factionTemplate -> Flags & FACTION_TEMPLATE_FLAG_PVP )
2009-12-19 19:10:14 +01:00
SetPvP ( true );
else
SetPvP ( false );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2012-12-05 23:34:50 +02:00
// updates spell bars for vehicles and set player's faction - should be called here, to overwrite faction that is set from the new template
if ( IsVehicle ())
2012-12-17 05:39:04 +01:00
{
2012-12-05 23:34:50 +02:00
if ( Player * owner = Creature :: GetCharmerOrOwnerPlayerOrPlayerItself ()) // this check comes in case we don't have a player
2012-12-17 05:39:04 +01:00
{
2012-12-05 23:34:50 +02:00
setFaction ( owner -> getFaction ()); // vehicles should have same as owner faction
2012-12-17 05:39:04 +01:00
owner -> VehicleSpellInitialize ();
2012-12-05 23:34:50 +02:00
}
}
2010-04-21 13:55:43 +02:00
// trigger creature is always not selectable and can not be attacked
2013-06-11 19:54:27 -02:30
if ( IsTrigger ())
2008-11-06 10:27:58 -06:00
SetFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_NOT_SELECTABLE );
2009-10-17 15:51:44 -07:00
2010-06-27 03:47:18 +02:00
InitializeReactState ();
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( cInfo -> flags_extra & CREATURE_FLAG_EXTRA_NO_TAUNT )
2009-01-27 15:23:26 -06:00
{
ApplySpellImmune ( 0 , IMMUNITY_STATE , SPELL_AURA_MOD_TAUNT , true );
2011-04-29 20:47:02 +02:00
ApplySpellImmune ( 0 , IMMUNITY_EFFECT , SPELL_EFFECT_ATTACK_ME , true );
2009-01-27 15:23:26 -06:00
}
2009-10-17 15:51:44 -07:00
2016-07-15 16:48:04 +02:00
if ( cInfo -> InhabitType & INHABIT_ROOT )
SetControlled ( true , UNIT_STATE_ROOT );
2013-06-07 16:05:39 +02:00
UpdateMovementFlags ();
2015-11-22 18:30:42 +01:00
LoadCreaturesAddon ();
2008-10-11 14:16:25 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: Update ( uint32 diff )
{
2016-04-02 02:50:44 +02:00
if ( IsAIEnabled && m_TriggerJustRespawned )
2010-11-03 18:33:47 +01:00
{
2016-04-02 02:50:44 +02:00
m_TriggerJustRespawned = false ;
2010-11-03 18:33:47 +01:00
AI () -> JustRespawned ();
2011-04-22 21:59:37 +02:00
if ( m_vehicleKit )
m_vehicleKit -> Reset ();
2010-11-03 18:33:47 +01:00
}
2013-06-07 16:05:39 +02:00
UpdateMovementFlags ();
2012-12-29 04:01:46 +01:00
2011-09-29 12:43:05 +02:00
switch ( m_deathState )
2008-10-11 14:16:25 -05:00
{
2012-05-30 08:01:02 +02:00
case JUST_RESPAWNED :
// Must not be called, see Creature::setDeathState JUST_RESPAWNED -> ALIVE promoting.
2014-10-22 17:33:55 +02:00
TC_LOG_ERROR ( "entities.unit" , "Creature (%s) in wrong state: JUST_RESPAWNED (4)" , GetGUID (). ToString (). c_str ());
2008-10-11 14:16:25 -05:00
break ;
case JUST_DIED :
2011-07-29 14:18:28 +02:00
// Must not be called, see Creature::setDeathState JUST_DIED -> CORPSE promoting.
2014-10-22 17:33:55 +02:00
TC_LOG_ERROR ( "entities.unit" , "Creature (%s) in wrong state: JUST_DEAD (1)" , GetGUID (). ToString (). c_str ());
2008-10-11 14:16:25 -05:00
break ;
case DEAD :
{
2010-12-24 16:18:23 +01:00
time_t now = time ( NULL );
if ( m_respawnTime <= now )
2008-10-11 14:16:25 -05:00
{
2016-09-09 16:21:27 +02:00
// First check if there are any scripts that object to us respawning
if ( ! sScriptMgr -> CanSpawn ( GetSpawnId (), GetEntry (), GetCreatureTemplate (), GetCreatureData (), GetMap ()))
break ; // Will be rechecked on next Update call
2010-12-24 16:18:23 +01:00
2015-02-25 00:13:14 +01:00
ObjectGuid dbtableHighGuid = ObjectGuid :: Create < HighGuid :: Creature > ( GetMapId (), GetEntry (), m_spawnId );
2012-06-07 23:20:57 +02:00
time_t linkedRespawntime = GetMap () -> GetLinkedRespawnTime ( dbtableHighGuid );
2010-12-24 18:55:50 +01:00
if ( ! linkedRespawntime ) // Can respawn
2009-04-14 14:33:48 +02:00
Respawn ();
2010-12-24 16:18:23 +01:00
else // the master is dead
2008-10-11 14:16:25 -05:00
{
2014-09-14 16:14:12 +02:00
ObjectGuid targetGuid = sObjectMgr -> GetLinkedRespawnGuid ( dbtableHighGuid );
2011-05-16 15:36:02 +02:00
if ( targetGuid == dbtableHighGuid ) // if linking self, never respawn (check delayed to next day)
2010-12-24 18:55:50 +01:00
SetRespawnTime ( DAY );
2009-04-14 14:33:48 +02:00
else
2014-09-14 16:14:12 +02:00
m_respawnTime = ( now > linkedRespawntime ? now : linkedRespawntime ) + urand ( 5 , MINUTE ); // else copy time from master and add a little
2010-12-24 18:55:50 +01:00
SaveRespawnTime (); // also save to DB immediately
2008-10-11 14:16:25 -05:00
}
}
break ;
}
case CORPSE :
{
2012-01-14 15:34:41 +01:00
Unit :: Update ( diff );
2011-07-12 00:19:50 +02:00
// deathstate changed on spells update, prevent problems
if ( m_deathState != CORPSE )
break ;
2014-10-22 00:35:00 +02:00
if ( m_groupLootTimer && ! lootingGroupLowGUID . IsEmpty ())
2010-03-11 22:55:02 +01:00
{
if ( m_groupLootTimer <= diff )
{
2014-10-22 00:35:00 +02:00
if ( Group * group = sGroupMgr -> GetGroupByGUID ( lootingGroupLowGUID ))
2010-03-11 22:55:02 +01:00
group -> EndRoll ( & loot );
2014-10-22 00:35:00 +02:00
2010-03-11 22:55:02 +01:00
m_groupLootTimer = 0 ;
2014-10-22 00:35:00 +02:00
lootingGroupLowGUID . Clear ();
2010-03-11 22:55:02 +01:00
}
2014-10-22 00:35:00 +02:00
else
m_groupLootTimer -= diff ;
2010-03-11 22:55:02 +01:00
}
2010-09-15 06:22:54 +02:00
else if ( m_corpseRemoveTime <= time ( NULL ))
2008-10-11 14:16:25 -05:00
{
2010-09-15 06:22:54 +02:00
RemoveCorpse ( false );
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "entities.unit" , "Removing corpse... %u " , GetUInt32Value ( OBJECT_FIELD_ENTRY ));
2008-10-11 14:16:25 -05:00
}
break ;
}
case ALIVE :
{
2010-04-07 22:59:46 +02:00
Unit :: Update ( diff );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// creature can be dead after Unit::Update call
// CORPSE/DEAD state will processed at next tick (in other case death timer will be updated unexpectedly)
2013-06-11 19:54:27 -02:30
if ( ! IsAlive ())
2008-10-11 14:16:25 -05:00
break ;
2009-10-17 15:51:44 -07:00
2016-08-21 11:07:38 +02:00
if ( m_shouldReacquireTarget && ! IsFocusing ( nullptr , true ))
{
SetTarget ( m_suppressedTarget );
if ( ! m_suppressedTarget . IsEmpty ())
{
if ( WorldObject const * objTarget = ObjectAccessor :: GetWorldObject ( * this , m_suppressedTarget ))
SetFacingToObject ( objTarget );
}
else
SetFacingTo ( m_suppressedOrientation );
m_shouldReacquireTarget = false ;
}
2013-08-20 00:43:03 +02:00
// if creature is charmed, switch to charmed AI (and back)
2010-04-07 19:14:10 +02:00
if ( NeedChangeAI )
2009-03-13 17:48:57 -06:00
{
UpdateCharmAI ();
NeedChangeAI = false ;
IsAIEnabled = true ;
2014-10-21 19:23:32 +02:00
if ( ! IsInEvadeMode () && ! LastCharmerGUID . IsEmpty ())
2013-08-20 00:43:03 +02:00
if ( Unit * charmer = ObjectAccessor :: GetUnit ( * this , LastCharmerGUID ))
2016-03-01 20:12:46 +01:00
if ( CanStartAttack ( charmer , true ))
i_AI -> AttackStart ( charmer );
2013-08-20 00:43:03 +02:00
2014-09-14 16:14:12 +02:00
LastCharmerGUID . Clear ();
2009-03-13 17:48:57 -06:00
}
2009-10-17 15:51:44 -07:00
2016-01-13 15:33:17 +01:00
// periodic check to see if the creature has passed an evade boundary
if ( IsAIEnabled && ! IsInEvadeMode () && IsInCombat ())
{
if ( diff >= m_boundaryCheckTime )
{
AI () -> CheckInRoom ();
m_boundaryCheckTime = 2500 ;
} else
m_boundaryCheckTime -= diff ;
}
2015-09-13 22:02:38 +02:00
// if periodic combat pulse is enabled and we are both in combat and in a dungeon, do this now
if ( m_combatPulseDelay > 0 && IsInCombat () && GetMap () -> IsDungeon ())
{
if ( diff > m_combatPulseTime )
m_combatPulseTime = 0 ;
else
m_combatPulseTime -= diff ;
if ( m_combatPulseTime == 0 )
{
2015-09-25 15:14:53 +02:00
Map :: PlayerList const & players = GetMap () -> GetPlayers ();
if ( ! players . isEmpty ())
for ( Map :: PlayerList :: const_iterator it = players . begin (); it != players . end (); ++ it )
{
if ( Player * player = it -> GetSource ())
{
if ( player -> IsGameMaster ())
continue ;
if ( player -> IsAlive () && this -> IsHostileTo ( player ))
{
if ( CanHaveThreatList ())
AddThreat ( player , 0.0f );
this -> SetInCombatWith ( player );
player -> SetInCombatWith ( this );
}
}
}
2015-09-13 22:02:38 +02:00
m_combatPulseTime = m_combatPulseDelay * IN_MILLISECONDS ;
}
}
2010-04-07 19:14:10 +02:00
if ( ! IsInEvadeMode () && IsAIEnabled )
2008-10-11 14:16:25 -05:00
{
// do not allow the AI to be changed during update
m_AI_locked = true ;
2015-09-13 22:02:38 +02:00
2008-11-20 16:16:57 -06:00
i_AI -> UpdateAI ( diff );
2008-10-11 14:16:25 -05:00
m_AI_locked = false ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// creature can be dead after UpdateAI call
// CORPSE/DEAD state will processed at next tick (in other case death timer will be updated unexpectedly)
2013-06-11 19:54:27 -02:30
if ( ! IsAlive ())
2008-10-11 14:16:25 -05:00
break ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( m_regenTimer > 0 )
2009-10-10 16:26:00 -07:00
{
2010-04-07 19:14:10 +02:00
if ( diff >= m_regenTimer )
2009-10-10 16:26:00 -07:00
m_regenTimer = 0 ;
else
m_regenTimer -= diff ;
}
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
if ( m_regenTimer == 0 )
{
bool bInCombat = IsInCombat () && ( ! GetVictim () || // if IsInCombat() is true and this has no victim
! EnsureVictim () -> GetCharmerOrOwnerPlayerOrPlayerItself () || // or the victim/owner/charmer is not a player
! EnsureVictim () -> GetCharmerOrOwnerPlayerOrPlayerItself () -> IsGameMaster ()); // or the victim/owner/charmer is not a GameMaster
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
if ( ! IsInEvadeMode () && ( ! bInCombat || IsPolymorphed () || CanNotReachTarget ())) // regenerate health if not in combat or if polymorphed
RegenerateHealth ();
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
if ( HasFlag ( UNIT_FIELD_FLAGS_2 , UNIT_FLAG2_REGENERATE_POWER ))
{
if ( getPowerType () == POWER_ENERGY )
Regenerate ( POWER_ENERGY );
else
RegenerateMana ();
}
m_regenTimer = CREATURE_REGEN_INTERVAL ;
2009-08-23 22:36:51 -05:00
}
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
if ( CanNotReachTarget () && ! IsInEvadeMode () && ! GetMap () -> IsRaid ())
{
m_cannotReachTimer += diff ;
if ( m_cannotReachTimer >= CREATURE_NOPATH_EVADE_TIME )
if ( IsAIEnabled )
AI () -> EnterEvadeMode ( CreatureAI :: EVADE_REASON_NO_PATH );
2009-10-17 16:20:24 -07:00
}
2008-10-11 14:16:25 -05:00
break ;
}
default :
break ;
}
2010-08-06 19:23:43 +02:00
2010-12-22 21:25:23 +01:00
sScriptMgr -> OnCreatureUpdate ( this , diff );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: RegenerateMana ()
{
uint32 curValue = GetPower ( POWER_MANA );
uint32 maxValue = GetMaxPower ( POWER_MANA );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
if ( curValue >= maxValue )
return ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint32 addvalue = 0 ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// Combat and any controlled creature
2014-10-21 19:23:32 +02:00
if ( IsInCombat () || ! GetCharmerOrOwnerGUID (). IsEmpty ())
2008-10-11 14:16:25 -05:00
{
2012-09-05 16:40:42 +02:00
float ManaIncreaseRate = sWorld -> getRate ( RATE_POWER_MANA );
2009-10-17 15:51:44 -07:00
2016-04-26 18:56:35 +02:00
addvalue = uint32 (( 27.0f / 5.0f + 17.0f ) * ManaIncreaseRate );
2008-10-11 14:16:25 -05:00
}
else
2010-12-11 20:37:38 +06:00
addvalue = maxValue / 3 ;
2009-10-17 15:51:44 -07:00
2010-01-10 01:23:15 +01:00
// Apply modifiers (if any).
AuraEffectList const & ModPowerRegenPCTAuras = GetAuraEffectsByType ( SPELL_AURA_MOD_POWER_REGEN_PERCENT );
for ( AuraEffectList :: const_iterator i = ModPowerRegenPCTAuras . begin (); i != ModPowerRegenPCTAuras . end (); ++ i )
if (( * i ) -> GetMiscValue () == POWER_MANA )
2012-10-02 12:17:42 +02:00
AddPct ( addvalue , ( * i ) -> GetAmount ());
2010-01-10 01:23:15 +01:00
2010-07-12 00:30:17 +02:00
addvalue += GetTotalAuraModifierByMiscValue ( SPELL_AURA_MOD_POWER_REGEN , POWER_MANA ) * CREATURE_REGEN_INTERVAL / ( 5 * IN_MILLISECONDS );
2010-01-10 01:23:15 +01:00
2008-10-11 14:16:25 -05:00
ModifyPower ( POWER_MANA , addvalue );
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: RegenerateHealth ()
{
if ( ! isRegeneratingHealth ())
return ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint32 curValue = GetHealth ();
uint32 maxValue = GetMaxHealth ();
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
if ( curValue >= maxValue )
return ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint32 addvalue = 0 ;
2009-10-17 15:51:44 -07:00
2008-11-14 16:28:45 -06:00
// Not only pet, but any controlled creature
2014-10-21 20:27:00 +02:00
if ( ! GetCharmerOrOwnerGUID (). IsEmpty ())
2008-10-11 14:16:25 -05:00
{
2010-12-23 23:25:44 +01:00
float HealthIncreaseRate = sWorld -> getRate ( RATE_HEALTH );
2009-10-17 15:51:44 -07:00
2016-04-26 18:56:35 +02:00
addvalue = 0.015f * (( float ) GetMaxHealth ()) * HealthIncreaseRate ;
2008-10-11 14:16:25 -05:00
}
else
addvalue = maxValue / 3 ;
2009-10-17 15:51:44 -07:00
2010-01-10 01:23:15 +01:00
// Apply modifiers (if any).
AuraEffectList const & ModPowerRegenPCTAuras = GetAuraEffectsByType ( SPELL_AURA_MOD_HEALTH_REGEN_PERCENT );
for ( AuraEffectList :: const_iterator i = ModPowerRegenPCTAuras . begin (); i != ModPowerRegenPCTAuras . end (); ++ i )
2012-10-02 12:17:42 +02:00
AddPct ( addvalue , ( * i ) -> GetAmount ());
2010-01-10 01:23:15 +01:00
2010-07-12 00:30:17 +02:00
addvalue += GetTotalAuraModifier ( SPELL_AURA_MOD_REGEN ) * CREATURE_REGEN_INTERVAL / ( 5 * IN_MILLISECONDS );
2010-01-10 01:23:15 +01:00
2008-10-11 14:16:25 -05:00
ModifyHealth ( addvalue );
}
2009-10-17 15:51:44 -07:00
2009-05-21 10:48:00 -05:00
void Creature :: DoFleeToGetAssistance ()
{
2013-06-11 19:54:27 -02:30
if ( ! GetVictim ())
2009-05-21 10:48:00 -05:00
return ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( HasAuraType ( SPELL_AURA_PREVENTS_FLEEING ))
2009-05-21 10:48:00 -05:00
return ;
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
float radius = sWorld -> getFloatConfig ( CONFIG_CREATURE_FAMILY_FLEE_ASSISTANCE_RADIUS );
2009-05-21 10:48:00 -05:00
if ( radius > 0 )
{
2016-04-02 02:50:44 +02:00
Creature * creature = nullptr ;
2009-10-17 15:51:44 -07:00
2011-10-18 10:53:34 -04:00
CellCoord p ( Trinity :: ComputeCellCoord ( GetPositionX (), GetPositionY ()));
2009-05-21 10:48:00 -05:00
Cell cell ( p );
cell . SetNoCreate ();
2013-06-11 19:54:27 -02:30
Trinity :: NearestAssistCreatureInCreatureRangeCheck u_check ( this , GetVictim (), radius );
2011-10-07 11:08:09 -05:00
Trinity :: CreatureLastSearcher < Trinity :: NearestAssistCreatureInCreatureRangeCheck > searcher ( this , creature , u_check );
2009-10-17 15:51:44 -07:00
2009-12-20 15:20:04 +01:00
TypeContainerVisitor < Trinity :: CreatureLastSearcher < Trinity :: NearestAssistCreatureInCreatureRangeCheck > , GridTypeMapContainer > grid_creature_searcher ( searcher );
2009-10-17 15:51:44 -07:00
2010-01-23 22:24:41 +01:00
cell . Visit ( p , grid_creature_searcher , * GetMap (), * this , radius );
2009-10-17 15:51:44 -07:00
2009-05-21 10:48:00 -05:00
SetNoSearchAssistance ( true );
2016-04-16 01:36:32 +02:00
UpdateSpeed ( MOVE_RUN );
2010-01-16 19:35:30 +03:00
2011-10-07 11:08:09 -05:00
if ( ! creature )
2014-04-19 21:48:22 +02:00
//SetFeared(true, EnsureVictim()->GetGUID(), 0, sWorld->getIntConfig(CONFIG_CREATURE_FAMILY_FLEE_DELAY));
2013-03-08 21:41:30 +01:00
/// @todo use 31365
2012-02-03 19:12:41 +02:00
SetControlled ( true , UNIT_STATE_FLEEING );
2009-05-21 10:48:00 -05:00
else
2011-10-07 11:08:09 -05:00
GetMotionMaster () -> MoveSeekAssistance ( creature -> GetPositionX (), creature -> GetPositionY (), creature -> GetPositionZ ());
2009-05-21 10:48:00 -05:00
}
}
2009-10-17 15:51:44 -07:00
2016-03-11 17:09:26 +01:00
bool Creature :: AIM_Destroy ()
{
if ( m_AI_locked )
{
TC_LOG_DEBUG ( "scripts" , "AIM_Destroy: failed to destroy, locked." );
return false ;
}
ASSERT ( ! i_disabledAI ,
"The disabled AI wasn't cleared!" );
delete i_AI ;
i_AI = nullptr ;
IsAIEnabled = false ;
return true ;
}
2008-12-10 20:25:47 -06:00
bool Creature :: AIM_Initialize ( CreatureAI * ai )
2008-10-11 14:16:25 -05:00
{
// make sure nothing can change the AI during AI update
2010-04-07 19:14:10 +02:00
if ( m_AI_locked )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "scripts" , "AIM_Initialize: failed to init, locked." );
2008-10-11 14:16:25 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2016-03-11 17:09:26 +01:00
AIM_Destroy ();
2009-10-17 15:51:44 -07:00
2009-04-06 20:33:52 -05:00
Motion_Initialize ();
2009-10-17 16:20:24 -07:00
2009-04-06 20:33:52 -05:00
i_AI = ai ? ai : FactorySelector :: selectAI ( this );
2016-03-11 17:09:26 +01:00
2009-04-06 20:33:52 -05:00
IsAIEnabled = true ;
2009-04-13 17:53:09 -05:00
i_AI -> InitializeAI ();
2011-04-22 21:59:37 +02:00
// Initialize vehicle
if ( GetVehicleKit ())
GetVehicleKit () -> Reset ();
2009-04-06 20:33:52 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2009-04-06 20:33:52 -05:00
void Creature :: Motion_Initialize ()
{
2010-04-07 19:14:10 +02:00
if ( ! m_formation )
2013-12-12 09:50:52 +01:00
GetMotionMaster () -> Initialize ();
2010-04-07 19:14:10 +02:00
else if ( m_formation -> getLeader () == this )
2009-10-17 16:20:24 -07:00
{
2009-04-07 10:51:45 +02:00
m_formation -> FormationReset ( false );
2013-12-12 09:50:52 +01:00
GetMotionMaster () -> Initialize ();
2009-04-07 10:51:45 +02:00
}
2010-04-07 19:14:10 +02:00
else if ( m_formation -> isFormed ())
2013-12-12 09:50:52 +01:00
GetMotionMaster () -> MoveIdle (); //wait the order of leader
2009-04-06 20:33:52 -05:00
else
2013-12-12 09:50:52 +01:00
GetMotionMaster () -> Initialize ();
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2014-10-26 02:57:28 +02:00
bool Creature :: Create ( ObjectGuid :: LowType guidlow , Map * map , uint32 /*phaseMask*/ , uint32 entry , float x , float y , float z , float ang , CreatureData const * data /*= nullptr*/ , uint32 vehId /*= 0*/ )
2008-10-11 14:16:25 -05:00
{
2009-08-01 00:20:32 +02:00
ASSERT ( map );
SetMap ( map );
2014-06-02 17:43:59 -05:00
2014-06-01 22:27:29 -05:00
if ( data && data -> phaseid )
SetInPhase ( data -> phaseid , false , true );
if ( data && data -> phaseGroup )
2014-12-26 01:38:40 +01:00
for ( auto ph : sDB2Manager . GetPhasesForGroup ( data -> phaseGroup ))
2014-06-01 22:27:29 -05:00
SetInPhase ( ph , false , true );
2009-10-17 15:51:44 -07:00
2014-04-28 20:05:36 +02:00
CreatureTemplate const * cinfo = sObjectMgr -> GetCreatureTemplate ( entry );
2010-11-16 14:00:47 +01:00
if ( ! cinfo )
{
2014-10-27 21:56:12 +00:00
TC_LOG_ERROR ( "sql.sql" , "Creature::Create(): creature template (guidlow: " UI64FMTD ", entry: %u) does not exist." , guidlow , entry );
2010-11-16 14:00:47 +01:00
return false ;
}
2012-05-30 08:01:02 +02:00
//! Relocate before CreateFromProto, to initialize coords and allow
//! returning correct zone id for selecting OutdoorPvP/Battlefield script
2009-05-30 22:15:05 -05:00
Relocate ( x , y , z , ang );
2009-10-17 15:51:44 -07:00
2016-06-21 22:19:31 +02:00
// Check if the position is valid before calling CreateFromProto(), otherwise we might add Auras to Creatures at
// invalid position, triggering a crash about Auras not removed in the destructor
2010-04-07 19:14:10 +02:00
if ( ! IsPositionValid ())
2009-05-30 22:15:05 -05:00
{
2014-10-27 21:56:12 +00:00
TC_LOG_ERROR ( "entities.unit" , "Creature::Create(): given coordinates for creature (guidlow " UI64FMTD ", entry %d) are not valid (X: %f, Y: %f, Z: %f, O: %f)" , guidlow , entry , x , y , z , ang );
2009-05-30 22:15:05 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2016-06-21 22:19:31 +02:00
if ( ! CreateFromProto ( guidlow , entry , data , vehId ))
return false ;
2012-05-30 08:01:02 +02:00
switch ( GetCreatureTemplate () -> rank )
2008-10-11 14:16:25 -05:00
{
2011-02-18 19:48:47 +01:00
case CREATURE_ELITE_RARE :
m_corpseDelay = sWorld -> getIntConfig ( CONFIG_CORPSE_DECAY_RARE );
break ;
case CREATURE_ELITE_ELITE :
m_corpseDelay = sWorld -> getIntConfig ( CONFIG_CORPSE_DECAY_ELITE );
break ;
case CREATURE_ELITE_RAREELITE :
m_corpseDelay = sWorld -> getIntConfig ( CONFIG_CORPSE_DECAY_RAREELITE );
break ;
case CREATURE_ELITE_WORLDBOSS :
m_corpseDelay = sWorld -> getIntConfig ( CONFIG_CORPSE_DECAY_WORLDBOSS );
break ;
default :
m_corpseDelay = sWorld -> getIntConfig ( CONFIG_CORPSE_DECAY_NORMAL );
break ;
}
2009-10-17 15:51:44 -07:00
2011-02-18 19:48:47 +01:00
LoadCreaturesAddon ();
2012-05-30 08:01:02 +02:00
//! Need to be called after LoadCreaturesAddon - MOVEMENTFLAG_HOVER is set there
if ( HasUnitMovementFlag ( MOVEMENTFLAG_HOVER ))
{
z += GetFloatValue ( UNIT_FIELD_HOVERHEIGHT );
//! Relocate again with updated Z coord
Relocate ( x , y , z , ang );
}
2011-05-07 20:09:45 +02:00
uint32 displayID = GetNativeDisplayId ();
2011-09-15 14:08:17 +02:00
CreatureModelInfo const * minfo = sObjectMgr -> GetCreatureModelRandomGender ( & displayID );
2013-06-11 19:54:27 -02:30
if ( minfo && ! IsTotem ()) // Cancel load if no model defined or if totem
2011-02-18 19:48:47 +01:00
{
2011-04-28 22:11:45 +02:00
SetDisplayId ( displayID );
SetNativeDisplayId ( displayID );
2014-11-30 19:09:26 +01:00
SetByteValue ( UNIT_FIELD_BYTES_0 , UNIT_BYTES_0_OFFSET_GENDER , minfo -> gender );
2011-02-18 19:48:47 +01:00
}
2010-02-28 20:30:20 +01:00
2017-03-18 11:31:31 +01:00
LastUsedScriptID = GetScriptId ();
2011-02-18 19:48:47 +01:00
2013-03-08 21:41:30 +01:00
/// @todo Replace with spell, handle from DB
2013-06-11 19:54:27 -02:30
if ( IsSpiritHealer () || IsSpiritGuide ())
2010-11-13 17:18:09 +01:00
{
m_serverSideVisibility . SetValue ( SERVERSIDE_VISIBILITY_GHOST , GHOST_VISIBILITY_GHOST );
m_serverSideVisibilityDetect . SetValue ( SERVERSIDE_VISIBILITY_GHOST , GHOST_VISIBILITY_GHOST );
}
2014-07-11 03:08:20 +02:00
if ( GetCreatureTemplate () -> flags_extra & CREATURE_FLAG_EXTRA_IGNORE_PATHFINDING )
AddUnitState ( UNIT_STATE_IGNORE_PATHFINDING );
2016-02-04 15:46:23 +01:00
if ( GetCreatureTemplate () -> flags_extra & CREATURE_FLAG_EXTRA_IMMUNITY_KNOCKBACK )
{
ApplySpellImmune ( 0 , IMMUNITY_EFFECT , SPELL_EFFECT_KNOCK_BACK , true );
ApplySpellImmune ( 0 , IMMUNITY_EFFECT , SPELL_EFFECT_KNOCK_BACK_DEST , true );
}
2011-02-18 19:48:47 +01:00
return true ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2013-03-04 12:41:17 +01:00
void Creature :: InitializeReactState ()
{
2014-08-14 16:09:14 +02:00
if ( IsTotem () || IsTrigger () || IsCritter () || IsSpiritService ())
2013-03-04 12:41:17 +01:00
SetReactState ( REACT_PASSIVE );
2014-04-28 20:05:36 +02:00
/*
else if (IsCivilian())
SetReactState(REACT_DEFENSIVE);
*/
2013-03-04 12:41:17 +01:00
else
SetReactState ( REACT_AGGRESSIVE );
}
2011-10-07 11:08:09 -05:00
bool Creature :: isCanInteractWithBattleMaster ( Player * player , bool msg ) const
2008-10-11 14:16:25 -05:00
{
2013-06-11 19:54:27 -02:30
if ( ! IsBattleMaster ())
2008-10-11 14:16:25 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2010-12-22 21:25:23 +01:00
BattlegroundTypeId bgTypeId = sBattlegroundMgr -> GetBattleMasterBG ( GetEntry ());
2010-04-07 19:14:10 +02:00
if ( ! msg )
2011-10-07 11:08:09 -05:00
return player -> GetBGAccessByLevel ( bgTypeId );
2009-10-17 15:51:44 -07:00
2011-10-07 11:08:09 -05:00
if ( ! player -> GetBGAccessByLevel ( bgTypeId ))
2008-10-11 14:16:25 -05:00
{
2016-08-31 00:59:04 +02:00
ClearGossipMenuFor ( player );
2011-09-29 12:43:05 +02:00
switch ( bgTypeId )
2008-10-11 14:16:25 -05:00
{
2016-08-31 00:59:04 +02:00
case BATTLEGROUND_AV : SendGossipMenuFor ( player , 7616 , this ); break ;
case BATTLEGROUND_WS : SendGossipMenuFor ( player , 7599 , this ); break ;
case BATTLEGROUND_AB : SendGossipMenuFor ( player , 7642 , this ); break ;
2008-10-11 14:16:25 -05:00
case BATTLEGROUND_EY :
case BATTLEGROUND_NA :
case BATTLEGROUND_BE :
case BATTLEGROUND_AA :
2008-12-24 09:58:26 -06:00
case BATTLEGROUND_RL :
case BATTLEGROUND_SA :
case BATTLEGROUND_DS :
2016-08-31 00:59:04 +02:00
case BATTLEGROUND_RV : SendGossipMenuFor ( player , 10024 , this ); break ;
2008-12-24 09:58:26 -06:00
default : break ;
2008-10-11 14:16:25 -05:00
}
return false ;
}
return true ;
}
2009-10-17 15:51:44 -07:00
2011-10-07 11:08:09 -05:00
bool Creature :: isCanTrainingAndResetTalentsOf ( Player * player ) const
2008-10-11 14:16:25 -05:00
{
2011-10-07 11:08:09 -05:00
return player -> getLevel () >= 10
2012-05-30 08:01:02 +02:00
&& GetCreatureTemplate () -> trainer_type == TRAINER_TYPE_CLASS
&& player -> getClass () == GetCreatureTemplate () -> trainer_class ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
Player * Creature :: GetLootRecipient () const
2008-10-11 14:16:25 -05:00
{
2010-09-15 13:44:40 +02:00
if ( ! m_lootRecipient )
2016-04-02 02:50:44 +02:00
return nullptr ;
2014-10-22 00:35:00 +02:00
2014-10-16 22:47:02 +02:00
return ObjectAccessor :: FindConnectedPlayer ( m_lootRecipient );
2010-09-15 13:44:40 +02:00
}
2011-09-15 14:08:17 +02:00
Group * Creature :: GetLootRecipientGroup () const
2010-09-15 13:44:40 +02:00
{
2014-10-22 00:35:00 +02:00
if ( m_lootRecipientGroup . IsEmpty ())
2016-04-02 02:50:44 +02:00
return nullptr ;
2014-10-22 00:35:00 +02:00
2011-05-05 10:23:00 +02:00
return sGroupMgr -> GetGroupByGUID ( m_lootRecipientGroup );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
void Creature :: SetLootRecipient ( Unit * unit )
2008-10-11 14:16:25 -05:00
{
// set the player whose group should receive the right
// to loot the creature after it dies
2016-04-02 02:50:44 +02:00
// should be set to nullptr after the loot disappears
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
if ( ! unit )
{
2014-09-14 16:14:12 +02:00
m_lootRecipient . Clear ();
2014-10-22 00:35:00 +02:00
m_lootRecipientGroup . Clear ();
2014-10-26 22:06:48 +01:00
RemoveFlag ( OBJECT_DYNAMIC_FLAGS , UNIT_DYNFLAG_LOOTABLE | UNIT_DYNFLAG_TAPPED );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-09-13 22:49:20 +02:00
if ( unit -> GetTypeId () != TYPEID_PLAYER && ! unit -> IsVehicle ())
return ;
2008-10-11 14:16:25 -05:00
Player * player = unit -> GetCharmerOrOwnerPlayerOrPlayerItself ();
2010-04-07 19:14:10 +02:00
if ( ! player ) // normal creature, no player involved
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
m_lootRecipient = player -> GetGUID ();
2011-06-12 01:47:45 +02:00
if ( Group * group = player -> GetGroup ())
2014-10-22 00:35:00 +02:00
m_lootRecipientGroup = group -> GetGUID ();
2010-09-15 13:44:40 +02:00
2014-10-26 22:06:48 +01:00
SetFlag ( OBJECT_DYNAMIC_FLAGS , UNIT_DYNFLAG_TAPPED );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-03-14 00:22:53 +01:00
// return true if this creature is tapped by the player or by a member of his group.
2011-08-19 14:53:40 +02:00
bool Creature :: isTappedBy ( Player const * player ) const
2010-03-14 00:22:53 +01:00
{
if ( player -> GetGUID () == m_lootRecipient )
return true ;
2011-08-19 14:53:40 +02:00
Group const * playerGroup = player -> GetGroup ();
2010-09-15 13:44:40 +02:00
if ( ! playerGroup || playerGroup != GetLootRecipientGroup ()) // if we dont have a group we arent the recipient
return false ; // if creature doesnt have group bound it means it was solo killed by someone else
2010-03-14 00:22:53 +01:00
return true ;
}
2008-10-11 14:16:25 -05:00
void Creature :: SaveToDB ()
{
// this should only be used when the creature has already been loaded
2008-11-14 16:28:45 -06:00
// preferably after adding to map, because mapid may not be valid otherwise
2015-02-25 00:13:14 +01:00
CreatureData const * data = sObjectMgr -> GetCreatureData ( m_spawnId );
2010-04-07 19:14:10 +02:00
if ( ! data )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "entities.unit" , "Creature::SaveToDB failed, cannot get creature data!" );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2016-05-30 23:49:35 +02:00
uint32 mapId = GetTransport () ? GetTransport () -> GetGOInfo () -> moTransport . SpawnMap : GetMapId ();
2013-10-16 18:37:29 +02:00
SaveToDB ( mapId , data -> spawnMask , GetPhaseMask ());
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2015-01-10 01:35:47 +01:00
void Creature :: SaveToDB ( uint32 mapid , uint32 spawnMask , uint32 phaseMask )
2008-10-11 14:16:25 -05:00
{
// update in loaded data
2015-02-25 00:13:14 +01:00
if ( ! m_spawnId )
m_spawnId = sObjectMgr -> GenerateCreatureSpawnId ();
2014-10-22 17:33:55 +02:00
2015-02-25 00:13:14 +01:00
CreatureData & data = sObjectMgr -> NewOrExistCreatureData ( m_spawnId );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint32 displayId = GetNativeDisplayId ();
2015-05-04 12:39:36 +02:00
uint64 npcflag = GetUInt64Value ( UNIT_NPC_FLAGS );
2010-10-09 17:52:00 +02:00
uint32 unit_flags = GetUInt32Value ( UNIT_FIELD_FLAGS );
2014-10-26 22:06:48 +01:00
uint32 dynamicflags = GetUInt32Value ( OBJECT_DYNAMIC_FLAGS );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// check if it's a custom model and if not, use 0 for displayId
2012-05-30 08:01:02 +02:00
CreatureTemplate const * cinfo = GetCreatureTemplate ();
2009-06-15 18:02:12 -05:00
if ( cinfo )
2008-10-11 14:16:25 -05:00
{
2009-09-21 07:52:16 +02:00
if ( displayId == cinfo -> Modelid1 || displayId == cinfo -> Modelid2 ||
displayId == cinfo -> Modelid3 || displayId == cinfo -> Modelid4 )
2009-06-15 18:02:12 -05:00
displayId = 0 ;
2010-10-09 17:52:00 +02:00
if ( npcflag == cinfo -> npcflag )
npcflag = 0 ;
if ( unit_flags == cinfo -> unit_flags )
unit_flags = 0 ;
if ( dynamicflags == cinfo -> dynamicflags )
dynamicflags = 0 ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-07-29 14:18:28 +02:00
// data->guid = guid must not be updated at save
2008-10-11 14:16:25 -05:00
data . id = GetEntry ();
data . mapid = mapid ;
2009-01-31 16:38:50 -06:00
data . phaseMask = phaseMask ;
2008-10-11 14:16:25 -05:00
data . displayid = displayId ;
2013-02-18 12:24:18 +00:00
data . equipmentId = GetCurrentEquipmentId ();
2013-10-16 18:37:29 +02:00
if ( ! GetTransport ())
{
data . posX = GetPositionX ();
data . posY = GetPositionY ();
data . posZ = GetPositionZMinusOffset ();
data . orientation = GetOrientation ();
}
else
{
data . posX = GetTransOffsetX ();
data . posY = GetTransOffsetY ();
data . posZ = GetTransOffsetZ ();
data . orientation = GetTransOffsetO ();
}
2008-10-11 14:16:25 -05:00
data . spawntimesecs = m_respawnDelay ;
// prevent add data integrity problems
2012-05-30 08:01:02 +02:00
data . spawndist = GetDefaultMovementType () == IDLE_MOTION_TYPE ? 0.0f : m_respawnradius ;
2008-10-11 14:16:25 -05:00
data . currentwaypoint = 0 ;
data . curhealth = GetHealth ();
data . curmana = GetPower ( POWER_MANA );
// prevent add data integrity problems
2010-04-07 23:25:02 +02:00
data . movementType = ! m_respawnradius && GetDefaultMovementType () == RANDOM_MOTION_TYPE
2008-10-11 14:16:25 -05:00
? IDLE_MOTION_TYPE : GetDefaultMovementType ();
data . spawnMask = spawnMask ;
2010-10-09 17:52:00 +02:00
data . npcflag = npcflag ;
data . unit_flags = unit_flags ;
data . dynamicflags = dynamicflags ;
2015-04-06 00:40:56 +02:00
2015-03-16 07:28:49 +01:00
data . phaseid = GetDBPhase () > 0 ? GetDBPhase () : 0 ;
data . phaseGroup = GetDBPhase () < 0 ? abs ( GetDBPhase ()) : 0 ;
2009-10-17 15:51:44 -07:00
2011-07-29 14:18:28 +02:00
// update in DB
2010-08-21 03:19:25 +02:00
SQLTransaction trans = WorldDatabase . BeginTransaction ();
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
PreparedStatement * stmt = WorldDatabase . GetPreparedStatement ( WORLD_DEL_CREATURE );
2015-02-25 00:13:14 +01:00
stmt -> setUInt64 ( 0 , m_spawnId );
2012-05-30 08:01:02 +02:00
trans -> Append ( stmt );
uint8 index = 0 ;
stmt = WorldDatabase . GetPreparedStatement ( WORLD_INS_CREATURE );
2015-02-25 00:13:14 +01:00
stmt -> setUInt64 ( index ++ , m_spawnId );
2012-05-30 08:01:02 +02:00
stmt -> setUInt32 ( index ++ , GetEntry ());
stmt -> setUInt16 ( index ++ , uint16 ( mapid ));
2015-01-24 19:49:05 +01:00
stmt -> setUInt32 ( index ++ , spawnMask );
2015-03-16 07:28:49 +01:00
stmt -> setUInt32 ( index ++ , data . phaseid );
stmt -> setUInt32 ( index ++ , data . phaseGroup );
2012-05-30 08:01:02 +02:00
stmt -> setUInt32 ( index ++ , displayId );
2015-01-24 19:49:05 +01:00
stmt -> setUInt8 ( index ++ , GetCurrentEquipmentId ());
2012-05-30 08:01:02 +02:00
stmt -> setFloat ( index ++ , GetPositionX ());
stmt -> setFloat ( index ++ , GetPositionY ());
stmt -> setFloat ( index ++ , GetPositionZ ());
stmt -> setFloat ( index ++ , GetOrientation ());
stmt -> setUInt32 ( index ++ , m_respawnDelay );
stmt -> setFloat ( index ++ , m_respawnradius );
stmt -> setUInt32 ( index ++ , 0 );
stmt -> setUInt32 ( index ++ , GetHealth ());
stmt -> setUInt32 ( index ++ , GetPower ( POWER_MANA ));
stmt -> setUInt8 ( index ++ , uint8 ( GetDefaultMovementType ()));
2015-05-04 12:39:36 +02:00
stmt -> setUInt64 ( index ++ , npcflag );
2012-05-30 08:01:02 +02:00
stmt -> setUInt32 ( index ++ , unit_flags );
stmt -> setUInt32 ( index ++ , dynamicflags );
trans -> Append ( stmt );
2009-10-17 15:51:44 -07:00
2010-08-21 03:19:25 +02:00
WorldDatabase . CommitTransaction ( trans );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2014-01-06 20:30:11 +01:00
void Creature :: SelectLevel ()
2008-10-11 14:16:25 -05:00
{
2014-01-06 20:30:11 +01:00
CreatureTemplate const * cInfo = GetCreatureTemplate ();
2008-10-11 14:16:25 -05:00
// level
2014-01-06 20:30:11 +01:00
uint8 minlevel = std :: min ( cInfo -> maxlevel , cInfo -> minlevel );
uint8 maxlevel = std :: max ( cInfo -> maxlevel , cInfo -> minlevel );
2009-11-01 17:53:07 -08:00
uint8 level = minlevel == maxlevel ? minlevel : urand ( minlevel , maxlevel );
2008-10-11 14:16:25 -05:00
SetLevel ( level );
2016-09-24 03:50:20 +02:00
}
2009-10-17 15:51:44 -07:00
2016-09-24 03:50:20 +02:00
void Creature :: UpdateLevelDependantStats ()
{
CreatureTemplate const * cInfo = GetCreatureTemplate ();
uint32 rank = IsPet () ? 0 : cInfo -> rank ;
CreatureBaseStats const * stats = sObjectMgr -> GetCreatureBaseStats ( getLevel (), cInfo -> unit_class );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// health
float healthmod = _GetHealthMod ( rank );
2009-10-17 15:51:44 -07:00
2014-01-06 20:30:11 +01:00
uint32 basehp = stats -> GenerateHealth ( cInfo );
2009-12-20 20:23:18 -07:00
uint32 health = uint32 ( basehp * healthmod );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
SetCreateHealth ( health );
SetMaxHealth ( health );
SetHealth ( health );
2009-06-11 17:54:59 +02:00
ResetPlayerDamageReq ();
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// mana
2014-01-06 20:30:11 +01:00
uint32 mana = stats -> GenerateMana ( cInfo );
2008-10-11 14:16:25 -05:00
SetCreateMana ( mana );
2009-10-17 15:51:44 -07:00
2012-07-24 15:28:41 +02:00
switch ( getClass ())
{
case CLASS_WARRIOR :
setPowerType ( POWER_RAGE );
break ;
case CLASS_ROGUE :
setPowerType ( POWER_ENERGY );
break ;
default :
2014-01-11 17:26:22 +00:00
SetMaxPower ( POWER_MANA , mana ); // MAX Mana
2012-07-24 15:28:41 +02:00
SetPower ( POWER_MANA , mana );
break ;
}
2009-10-17 15:51:44 -07:00
2010-08-22 12:39:39 -07:00
SetModifierValue ( UNIT_MOD_HEALTH , BASE_VALUE , ( float ) health );
SetModifierValue ( UNIT_MOD_MANA , BASE_VALUE , ( float ) mana );
2008-10-11 14:16:25 -05:00
2014-01-04 18:22:06 +01:00
// damage
2009-10-17 15:51:44 -07:00
2014-01-06 20:30:11 +01:00
float basedamage = stats -> GenerateBaseDamage ( cInfo );
2009-10-17 15:51:44 -07:00
2014-01-04 18:22:06 +01:00
float weaponBaseMinDamage = basedamage ;
2014-08-21 16:56:11 +02:00
float weaponBaseMaxDamage = basedamage * 1.5f ;
2009-10-17 15:51:44 -07:00
2014-01-04 18:22:06 +01:00
SetBaseWeaponDamage ( BASE_ATTACK , MINDAMAGE , weaponBaseMinDamage );
SetBaseWeaponDamage ( BASE_ATTACK , MAXDAMAGE , weaponBaseMaxDamage );
2009-10-17 15:51:44 -07:00
2014-01-04 18:22:06 +01:00
SetBaseWeaponDamage ( OFF_ATTACK , MINDAMAGE , weaponBaseMinDamage );
SetBaseWeaponDamage ( OFF_ATTACK , MAXDAMAGE , weaponBaseMaxDamage );
2009-10-17 15:51:44 -07:00
2014-01-04 18:22:06 +01:00
SetBaseWeaponDamage ( RANGED_ATTACK , MINDAMAGE , weaponBaseMinDamage );
SetBaseWeaponDamage ( RANGED_ATTACK , MAXDAMAGE , weaponBaseMaxDamage );
2009-10-17 15:51:44 -07:00
2014-01-04 18:22:06 +01:00
SetModifierValue ( UNIT_MOD_ATTACK_POWER , BASE_VALUE , stats -> AttackPower );
SetModifierValue ( UNIT_MOD_ATTACK_POWER_RANGED , BASE_VALUE , stats -> RangedAttackPower );
2016-09-24 03:50:20 +02:00
float armor = ( float ) stats -> GenerateArmor ( cInfo ); /// @todo Why is this treated as uint32 when it's a float?
SetModifierValue ( UNIT_MOD_ARMOR , BASE_VALUE , armor );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
float Creature :: _GetHealthMod ( int32 Rank )
{
switch ( Rank ) // define rates for each elite rank
{
case CREATURE_ELITE_NORMAL :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_NORMAL_HP );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_ELITE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_ELITE_HP );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_RAREELITE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_RAREELITE_HP );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_WORLDBOSS :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_WORLDBOSS_HP );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_RARE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_RARE_HP );
2008-10-11 14:16:25 -05:00
default :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_ELITE_HP );
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2013-03-04 12:41:17 +01:00
void Creature :: LowerPlayerDamageReq ( uint32 unDamage )
{
if ( m_PlayerDamageReq )
m_PlayerDamageReq > unDamage ? m_PlayerDamageReq -= unDamage : m_PlayerDamageReq = 0 ;
}
2008-10-11 14:16:25 -05:00
float Creature :: _GetDamageMod ( int32 Rank )
{
switch ( Rank ) // define rates for each elite rank
{
case CREATURE_ELITE_NORMAL :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_NORMAL_DAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_ELITE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_ELITE_DAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_RAREELITE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_RAREELITE_DAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_WORLDBOSS :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_WORLDBOSS_DAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_RARE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_RARE_DAMAGE );
2008-10-11 14:16:25 -05:00
default :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_ELITE_DAMAGE );
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2013-03-20 14:18:26 +01:00
float Creature :: GetSpellDamageMod ( int32 Rank ) const
2008-10-11 14:16:25 -05:00
{
switch ( Rank ) // define rates for each elite rank
{
case CREATURE_ELITE_NORMAL :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_NORMAL_SPELLDAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_ELITE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_ELITE_SPELLDAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_RAREELITE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_RAREELITE_SPELLDAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_WORLDBOSS :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_WORLDBOSS_SPELLDAMAGE );
2008-10-11 14:16:25 -05:00
case CREATURE_ELITE_RARE :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_RARE_SPELLDAMAGE );
2008-10-11 14:16:25 -05:00
default :
2010-12-23 23:25:44 +01:00
return sWorld -> getRate ( RATE_CREATURE_ELITE_ELITE_SPELLDAMAGE );
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2014-10-26 02:57:28 +02:00
bool Creature :: CreateFromProto ( ObjectGuid :: LowType guidlow , uint32 entry , CreatureData const * data /*= nullptr*/ , uint32 vehId /*= 0*/ )
2008-10-11 14:16:25 -05:00
{
2009-05-30 22:15:05 -05:00
SetZoneScript ();
2014-01-04 18:22:06 +01:00
if ( GetZoneScript () && data )
2009-05-30 22:15:05 -05:00
{
2014-04-28 20:05:36 +02:00
entry = GetZoneScript () -> GetCreatureEntry ( guidlow , data );
if ( ! entry )
2009-05-30 22:15:05 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2014-04-28 20:05:36 +02:00
CreatureTemplate const * cinfo = sObjectMgr -> GetCreatureTemplate ( entry );
2010-04-07 19:14:10 +02:00
if ( ! cinfo )
2008-10-11 14:16:25 -05:00
{
2014-10-27 21:56:12 +00:00
TC_LOG_ERROR ( "sql.sql" , "Creature::CreateFromProto(): creature template (guidlow: " UI64FMTD ", entry: %u) does not exist." , guidlow , entry );
2008-10-11 14:16:25 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2014-04-28 20:05:36 +02:00
SetOriginalEntry ( entry );
2009-10-17 15:51:44 -07:00
2014-10-29 21:13:21 +01:00
if ( vehId || cinfo -> VehicleId )
Object :: _Create ( ObjectGuid :: Create < HighGuid :: Vehicle > ( GetMapId (), entry , guidlow ));
else
Object :: _Create ( ObjectGuid :: Create < HighGuid :: Creature > ( GetMapId (), entry , guidlow ));
2009-10-17 15:51:44 -07:00
2014-04-28 20:05:36 +02:00
if ( ! UpdateEntry ( entry , data ))
2008-10-11 14:16:25 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2014-06-15 17:49:17 +02:00
if ( ! vehId )
{
if ( GetCreatureTemplate () -> VehicleId )
{
vehId = GetCreatureTemplate () -> VehicleId ;
entry = GetCreatureTemplate () -> Entry ;
}
else
vehId = cinfo -> VehicleId ;
}
2013-03-13 11:34:58 +01:00
if ( vehId )
2015-04-06 00:40:56 +02:00
CreateVehicleKit ( vehId , entry , true );
2013-02-18 00:39:58 +01:00
2008-10-11 14:16:25 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2016-02-03 12:34:38 +00:00
bool Creature :: LoadCreatureFromDB ( ObjectGuid :: LowType spawnId , Map * map , bool addToMap , bool allowDuplicate )
2008-10-11 14:16:25 -05:00
{
2016-02-03 12:34:38 +00:00
if ( ! allowDuplicate )
{
// If an alive instance of this spawnId is already found, skip creation
// If only dead instance(s) exist, despawn them and spawn a new (maybe also dead) version
const auto creatureBounds = map -> GetCreatureBySpawnIdStore (). equal_range ( spawnId );
std :: vector < Creature *> despawnList ;
if ( creatureBounds . first != creatureBounds . second )
{
for ( auto itr = creatureBounds . first ; itr != creatureBounds . second ; ++ itr )
{
if ( itr -> second -> IsAlive ())
{
2016-04-02 19:22:30 +02:00
TC_LOG_DEBUG ( "maps" , "Would have spawned " UI64FMTD " but %s already exists" , spawnId , creatureBounds . first -> second -> GetGUID (). ToString (). c_str ());
2016-02-03 12:34:38 +00:00
return false ;
}
else
{
despawnList . push_back ( itr -> second );
2016-04-02 19:22:30 +02:00
TC_LOG_DEBUG ( "maps" , "Despawned dead instance of spawn " UI64FMTD " (%s)" , spawnId , itr -> second -> GetGUID (). ToString (). c_str ());
2016-02-03 12:34:38 +00:00
}
}
for ( Creature * despawnCreature : despawnList )
{
despawnCreature -> AddObjectToRemoveList ();
}
}
}
2015-02-25 00:13:14 +01:00
CreatureData const * data = sObjectMgr -> GetCreatureData ( spawnId );
2010-04-07 19:14:10 +02:00
if ( ! data )
2008-10-11 14:16:25 -05:00
{
2015-02-25 00:13:14 +01:00
TC_LOG_ERROR ( "sql.sql" , "Creature (GUID: " UI64FMTD ") not found in table `creature`, can't load. " , spawnId );
2008-10-11 14:16:25 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2015-02-25 00:13:14 +01:00
m_spawnId = spawnId ;
2016-08-08 11:22:11 +02:00
m_creatureData = data ;
2015-02-25 00:13:14 +01:00
if ( ! Create ( map -> GenerateLowGuid < HighGuid :: Creature > (), map , data -> phaseMask , data -> id , data -> posX , data -> posY , data -> posZ , data -> orientation , data ))
2008-10-11 14:16:25 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2009-02-09 08:16:34 -05:00
//We should set first home position, because then AI calls home movement
2011-04-29 20:47:02 +02:00
SetHomePosition ( data -> posX , data -> posY , data -> posZ , data -> orientation );
2009-10-17 15:51:44 -07:00
2009-02-09 08:16:34 -05:00
m_respawnradius = data -> spawndist ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
m_respawnDelay = data -> spawntimesecs ;
2011-06-27 20:22:53 +02:00
m_deathState = ALIVE ;
2009-10-17 15:51:44 -07:00
2015-02-25 00:13:14 +01:00
m_respawnTime = GetMap () -> GetCreatureRespawnTime ( m_spawnId );
2016-09-09 16:21:27 +02:00
// Is the creature script objecting to us spawning? If yes, delay by one second (then re-check in ::Update)
if ( ! m_respawnTime && ! sScriptMgr -> CanSpawn ( spawnId , GetEntry (), GetCreatureTemplate (), GetCreatureData (), map ))
m_respawnTime = time ( NULL ) + 1 ;
2010-04-07 19:14:10 +02:00
if ( m_respawnTime ) // respawn on Update
2008-12-08 16:34:03 -06:00
{
2008-10-11 14:16:25 -05:00
m_deathState = DEAD ;
2012-05-30 08:01:02 +02:00
if ( CanFly ())
2008-12-08 16:34:03 -06:00
{
2016-03-15 22:02:14 +01:00
float tz = map -> GetHeight ( GetPhaseMask (), data -> posX , data -> posY , data -> posZ , true , MAX_FALL_DISTANCE );
if ( data -> posZ - tz > 0.1f && Trinity :: IsValidMapCoord ( tz ))
2011-04-29 20:47:02 +02:00
Relocate ( data -> posX , data -> posY , tz );
2008-12-08 16:34:03 -06:00
}
}
2009-10-17 15:51:44 -07:00
2016-08-08 11:22:11 +02:00
SetSpawnHealth ();
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// checked at creature_template loading
m_defaultMovementType = MovementGeneratorType ( data -> movementType );
2009-10-17 15:51:44 -07:00
2015-02-25 00:13:14 +01:00
loot . SetGUID ( ObjectGuid :: Create < HighGuid :: LootObject > ( data -> mapid , data -> id , GetMap () -> GenerateLowGuid < HighGuid :: LootObject > ()));
2014-12-30 02:23:37 +02:00
2011-11-28 14:05:25 -05:00
if ( addToMap && ! GetMap () -> AddToMap ( this ))
return false ;
2008-10-11 14:16:25 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2014-01-04 18:22:06 +01:00
void Creature :: SetCanDualWield ( bool value )
{
Unit :: SetCanDualWield ( value );
UpdateDamagePhysical ( OFF_ATTACK );
}
2013-02-18 12:24:18 +00:00
void Creature :: LoadEquipment ( int8 id , bool force /*= true*/ )
2008-10-11 14:16:25 -05:00
{
2013-02-18 12:24:18 +00:00
if ( id == 0 )
2008-10-11 14:16:25 -05:00
{
if ( force )
{
2013-02-18 12:24:18 +00:00
for ( uint8 i = 0 ; i < MAX_EQUIPMENT_ITEMS ; ++ i )
2015-07-01 02:22:03 +02:00
SetVirtualItem ( i , 0 );
2008-10-11 14:16:25 -05:00
m_equipmentId = 0 ;
}
2016-01-30 11:40:24 +02:00
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-02-18 12:24:18 +00:00
EquipmentInfo const * einfo = sObjectMgr -> GetEquipmentInfo ( GetEntry (), id );
2008-10-11 14:16:25 -05:00
if ( ! einfo )
return ;
2009-10-17 15:51:44 -07:00
2013-02-18 12:24:18 +00:00
m_equipmentId = id ;
2015-07-01 02:22:03 +02:00
for ( uint8 i = 0 ; i < MAX_EQUIPMENT_ITEMS ; ++ i )
2017-02-19 10:58:08 +01:00
SetVirtualItem ( i , einfo -> Items [ i ]. ItemId , einfo -> Items [ i ]. AppearanceModId , einfo -> Items [ i ]. ItemVisual );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2016-08-08 11:22:11 +02:00
void Creature :: SetSpawnHealth ()
{
uint32 curhealth ;
if ( ! m_regenHealth )
{
if ( m_creatureData )
{
curhealth = m_creatureData -> curhealth ;
if ( curhealth )
{
curhealth = uint32 ( curhealth * _GetHealthMod ( GetCreatureTemplate () -> rank ));
if ( curhealth < 1 )
curhealth = 1 ;
}
SetPower ( POWER_MANA , m_creatureData -> curmana );
}
else
curhealth = GetHealth ();
}
else
{
curhealth = GetMaxHealth ();
SetPower ( POWER_MANA , GetMaxPower ( POWER_MANA ));
}
SetHealth (( m_deathState == ALIVE || m_deathState == JUST_RESPAWNED ) ? curhealth : 0 );
}
2008-10-11 14:16:25 -05:00
bool Creature :: hasQuest ( uint32 quest_id ) const
{
2010-12-22 21:25:23 +01:00
QuestRelationBounds qr = sObjectMgr -> GetCreatureQuestRelationBounds ( GetEntry ());
2010-09-13 13:18:27 +02:00
for ( QuestRelations :: const_iterator itr = qr . first ; itr != qr . second ; ++ itr )
2008-10-11 14:16:25 -05:00
{
2010-04-07 23:25:02 +02:00
if ( itr -> second == quest_id )
2008-10-11 14:16:25 -05:00
return true ;
}
return false ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
bool Creature :: hasInvolvedQuest ( uint32 quest_id ) const
{
2010-12-22 21:25:23 +01:00
QuestRelationBounds qir = sObjectMgr -> GetCreatureQuestInvolvedRelationBounds ( GetEntry ());
2010-09-13 13:18:27 +02:00
for ( QuestRelations :: const_iterator itr = qir . first ; itr != qir . second ; ++ itr )
2008-10-11 14:16:25 -05:00
{
2010-04-07 23:25:02 +02:00
if ( itr -> second == quest_id )
2008-10-11 14:16:25 -05:00
return true ;
}
return false ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: DeleteFromDB ()
{
2015-02-25 00:13:14 +01:00
if ( ! m_spawnId )
2008-10-11 14:16:25 -05:00
{
2014-10-22 17:33:55 +02:00
TC_LOG_ERROR ( "entities.unit" , "Trying to delete not saved %s!" , GetGUID (). ToString (). c_str ());
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2015-02-25 00:13:14 +01:00
GetMap () -> RemoveCreatureRespawnTime ( m_spawnId );
sObjectMgr -> DeleteCreatureData ( m_spawnId );
2009-10-17 15:51:44 -07:00
2010-08-21 03:19:25 +02:00
SQLTransaction trans = WorldDatabase . BeginTransaction ();
2012-05-30 08:01:02 +02:00
PreparedStatement * stmt = WorldDatabase . GetPreparedStatement ( WORLD_DEL_CREATURE );
2015-02-25 00:13:14 +01:00
stmt -> setUInt64 ( 0 , m_spawnId );
2012-05-30 08:01:02 +02:00
trans -> Append ( stmt );
stmt = WorldDatabase . GetPreparedStatement ( WORLD_DEL_CREATURE_ADDON );
2015-02-25 00:13:14 +01:00
stmt -> setUInt64 ( 0 , m_spawnId );
2012-05-30 08:01:02 +02:00
trans -> Append ( stmt );
stmt = WorldDatabase . GetPreparedStatement ( WORLD_DEL_GAME_EVENT_CREATURE );
2015-02-25 00:13:14 +01:00
stmt -> setUInt64 ( 0 , m_spawnId );
2012-05-30 08:01:02 +02:00
trans -> Append ( stmt );
stmt = WorldDatabase . GetPreparedStatement ( WORLD_DEL_GAME_EVENT_MODEL_EQUIP );
2015-02-25 00:13:14 +01:00
stmt -> setUInt64 ( 0 , m_spawnId );
2012-05-30 08:01:02 +02:00
trans -> Append ( stmt );
2010-08-21 03:19:25 +02:00
WorldDatabase . CommitTransaction ( trans );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-10-13 13:26:27 -04:00
bool Creature :: IsInvisibleDueToDespawn () const
2008-11-06 10:27:58 -06:00
{
2011-10-13 13:26:27 -04:00
if ( Unit :: IsInvisibleDueToDespawn ())
return true ;
2009-10-17 15:51:44 -07:00
2013-12-11 09:38:24 +07:00
if ( IsAlive () || isDying () || m_corpseRemoveTime > time ( NULL ))
2011-10-13 13:26:27 -04:00
return false ;
2009-10-17 15:51:44 -07:00
2011-10-13 13:26:27 -04:00
return true ;
2008-11-06 10:27:58 -06:00
}
2009-10-17 15:51:44 -07:00
2011-10-13 17:01:55 -04:00
bool Creature :: CanAlwaysSee ( WorldObject const * obj ) const
2011-02-10 22:36:35 -03:00
{
if ( IsAIEnabled && AI () -> CanSeeAlways ( obj ))
return true ;
return false ;
}
2013-06-11 21:25:12 -02:30
bool Creature :: CanStartAttack ( Unit const * who , bool force ) const
2008-11-21 10:07:11 -06:00
{
2013-06-11 19:54:27 -02:30
if ( IsCivilian ())
2009-06-15 12:02:55 -05:00
return false ;
2009-10-17 16:20:24 -07:00
2015-06-26 16:52:40 +02:00
// This set of checks is should be done only for creatures
2015-07-05 04:03:36 +02:00
if (( HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_IMMUNE_TO_NPC ) && who -> GetTypeId () != TYPEID_PLAYER ) // flag is valid only for non player characters
2015-06-26 16:52:40 +02:00
|| ( HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_IMMUNE_TO_PC ) && who -> GetTypeId () == TYPEID_PLAYER ) // immune to PC and target is a player, return false
2015-07-04 02:43:43 +01:00
|| ( who -> GetOwner () && who -> GetOwner () -> GetTypeId () == TYPEID_PLAYER && HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_IMMUNE_TO_PC ))) // player pets are immune to pc as well
2015-06-26 16:52:40 +02:00
return false ;
2015-07-05 04:03:36 +02:00
2011-10-01 16:57:10 +01:00
// Do not attack non-combat pets
if ( who -> GetTypeId () == TYPEID_UNIT && who -> GetCreatureType () == CREATURE_TYPE_NON_COMBAT_PET )
return false ;
2011-03-06 12:22:04 +01:00
2012-05-30 08:01:02 +02:00
if ( ! CanFly () && ( GetDistanceZ ( who ) > CREATURE_Z_ATTACK_RANGE + m_CombatDistance ))
2009-06-15 12:02:55 -05:00
//|| who->IsControlledByPlayer() && who->IsFlying()))
// we cannot check flying for other creatures, too much map/vmap calculation
2013-03-08 21:41:30 +01:00
/// @todo should switch to range attack
2008-11-21 10:07:11 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! force )
2009-08-27 13:54:57 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! _IsTargetAcceptable ( who ))
2009-09-29 01:16:08 -07:00
return false ;
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
if ( who -> IsInCombat () && IsWithinDist ( who , ATTACK_DISTANCE ))
2011-06-12 01:47:45 +02:00
if ( Unit * victim = who -> getAttackerForHelper ())
2010-12-23 23:25:44 +01:00
if ( IsWithinDistInMap ( victim , sWorld -> getFloatConfig ( CONFIG_CREATURE_FAMILY_ASSISTANCE_RADIUS )))
2009-08-27 13:54:57 -05:00
force = true ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! force && ( IsNeutralToAll () || ! IsWithinDistInMap ( who , GetAttackDistance ( who ) + m_CombatDistance )))
2009-08-27 13:54:57 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2013-06-11 21:25:12 -02:30
if ( ! CanCreatureAttack ( who , force ))
2008-11-21 10:07:11 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2015-01-26 19:15:06 +00:00
// No aggro from gray creatures
if ( CheckNoGrayAggroConfig ( who -> getLevelForTarget ( this ), getLevelForTarget ( who )))
return false ;
2008-11-21 10:07:11 -06:00
return IsWithinLOSInMap ( who );
}
2009-10-17 15:51:44 -07:00
2015-01-26 19:15:06 +00:00
bool Creature :: CheckNoGrayAggroConfig ( uint32 playerLevel , uint32 creatureLevel ) const
{
if ( Trinity :: XP :: GetColorCode ( playerLevel , creatureLevel ) != XP_GRAY )
return false ;
uint32 notAbove = sWorld -> getIntConfig ( CONFIG_NO_GRAY_AGGRO_ABOVE );
uint32 notBelow = sWorld -> getIntConfig ( CONFIG_NO_GRAY_AGGRO_BELOW );
if ( notAbove == 0 && notBelow == 0 )
return false ;
if ( playerLevel <= notBelow || ( playerLevel >= notAbove && notAbove > 0 ))
return true ;
return false ;
}
2011-11-07 11:18:00 -06:00
float Creature :: GetAttackDistance ( Unit const * player ) const
2008-10-11 14:16:25 -05:00
{
2010-12-23 23:25:44 +01:00
float aggroRate = sWorld -> getRate ( RATE_CREATURE_AGGRO );
2010-04-07 23:25:02 +02:00
if ( aggroRate == 0 )
2008-10-11 14:16:25 -05:00
return 0.0f ;
2009-10-17 15:51:44 -07:00
2011-11-07 11:18:00 -06:00
uint32 playerlevel = player -> getLevelForTarget ( this );
uint32 creaturelevel = getLevelForTarget ( player );
2009-10-17 15:51:44 -07:00
2009-09-18 14:10:37 -07:00
int32 leveldif = int32 ( playerlevel ) - int32 ( creaturelevel );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// "The maximum Aggro Radius has a cap of 25 levels under. Example: A level 30 char has the same Aggro Radius of a level 5 char on a level 60 mob."
2010-04-07 22:59:46 +02:00
if ( leveldif < - 25 )
2008-10-11 14:16:25 -05:00
leveldif = - 25 ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// "The aggro radius of a mob having the same level as the player is roughly 20 yards"
float RetDistance = 20 ;
2009-10-17 15:51:44 -07:00
2008-11-14 16:28:45 -06:00
// "Aggro Radius varies with level difference at a rate of roughly 1 yard/level"
2008-10-11 14:16:25 -05:00
// radius grow if playlevel < creaturelevel
RetDistance -= ( float ) leveldif ;
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
if ( creaturelevel + 5 <= sWorld -> getIntConfig ( CONFIG_MAX_PLAYER_LEVEL ))
2008-10-11 14:16:25 -05:00
{
// detect range auras
RetDistance += GetTotalAuraModifier ( SPELL_AURA_MOD_DETECT_RANGE );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// detected range auras
2011-11-07 11:18:00 -06:00
RetDistance += player -> GetTotalAuraModifier ( SPELL_AURA_MOD_DETECTED_RANGE );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// "Minimum Aggro Radius for a mob seems to be combat range (5 yards)"
2010-04-07 19:14:10 +02:00
if ( RetDistance < 5 )
2008-10-11 14:16:25 -05:00
RetDistance = 5 ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
return ( RetDistance * aggroRate );
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: setDeathState ( DeathState s )
{
2011-06-27 20:22:53 +02:00
Unit :: setDeathState ( s );
if ( s == JUST_DIED )
2008-10-11 14:16:25 -05:00
{
2010-09-15 06:22:54 +02:00
m_corpseRemoveTime = time ( NULL ) + m_corpseDelay ;
m_respawnTime = time ( NULL ) + m_respawnDelay + m_corpseDelay ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// always save boss respawn time at death to prevent crash cheating
2010-12-23 23:25:44 +01:00
if ( sWorld -> getBoolConfig ( CONFIG_SAVE_RESPAWN_TIME_IMMEDIATELY ) || isWorldBoss ())
2008-10-11 14:16:25 -05:00
SaveRespawnTime ();
2009-10-17 15:51:44 -07:00
2016-04-02 02:50:44 +02:00
ReleaseFocus ( nullptr , false ); // remove spellcast focus
2016-01-13 18:38:54 +01:00
SetTarget ( ObjectGuid :: Empty ); // drop target - dead mobs shouldn't ever target things
2015-05-04 12:39:36 +02:00
SetUInt64Value ( UNIT_NPC_FLAGS , UNIT_NPC_FLAG_NONE );
2009-10-17 15:51:44 -07:00
2015-04-19 14:56:12 +02:00
SetUInt32Value ( UNIT_FIELD_MOUNTDISPLAYID , 0 ); // if creature is mounted on a virtual mount, remove it at death
2009-06-03 19:29:55 -05:00
setActive ( false );
2009-10-17 15:51:44 -07:00
2010-01-16 19:35:30 +03:00
if ( HasSearchedAssistance ())
{
SetNoSearchAssistance ( false );
2016-04-16 01:36:32 +02:00
UpdateSpeed ( MOVE_RUN );
2010-01-16 19:35:30 +03:00
}
2009-10-17 16:20:24 -07:00
2009-04-07 10:51:45 +02:00
//Dismiss group if is leader
2010-04-07 19:14:10 +02:00
if ( m_formation && m_formation -> getLeader () == this )
2009-04-07 10:51:45 +02:00
m_formation -> FormationReset ( true );
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
if (( CanFly () || IsFlying ()))
2013-12-12 09:50:52 +01:00
GetMotionMaster () -> MoveFall ();
2009-10-17 15:51:44 -07:00
2009-08-26 19:28:21 -05:00
Unit :: setDeathState ( CORPSE );
2009-04-07 10:51:45 +02:00
}
2012-05-30 08:01:02 +02:00
else if ( s == JUST_RESPAWNED )
2008-10-11 14:16:25 -05:00
{
2016-08-08 11:22:11 +02:00
if ( IsPet ())
SetFullHealth ();
else
SetSpawnHealth ();
2016-04-02 02:50:44 +02:00
SetLootRecipient ( nullptr );
2009-06-11 17:54:59 +02:00
ResetPlayerDamageReq ();
2012-12-27 19:31:22 +01:00
2013-06-07 16:05:39 +02:00
UpdateMovementFlags ();
2012-12-27 19:31:22 +01:00
2013-12-31 14:12:02 +01:00
ClearUnitState ( uint32 ( UNIT_STATE_ALL_STATE & ~ UNIT_STATE_IGNORE_PATHFINDING ));
2016-01-24 01:11:19 +00:00
if ( ! IsPet ())
{
CreatureData const * creatureData = GetCreatureData ();
CreatureTemplate const * cinfo = GetCreatureTemplate ();
uint64 npcflag ;
uint32 unit_flags , dynamicflags ;
ObjectMgr :: ChooseCreatureFlags ( cinfo , npcflag , unit_flags , dynamicflags , creatureData );
SetUInt64Value ( UNIT_NPC_FLAGS , npcflag );
SetUInt32Value ( UNIT_FIELD_FLAGS , unit_flags );
SetUInt32Value ( OBJECT_DYNAMIC_FLAGS , dynamicflags );
RemoveFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_IN_COMBAT );
SetMeleeDamageSchool ( SpellSchools ( cinfo -> dmgschool ));
}
2009-04-06 20:33:52 -05:00
Motion_Initialize ();
2009-08-25 00:56:23 -05:00
Unit :: setDeathState ( ALIVE );
2015-11-10 19:58:13 +01:00
LoadCreaturesAddon ();
2009-04-07 10:51:45 +02:00
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2009-06-17 01:01:39 -05:00
void Creature :: Respawn ( bool force )
2008-10-11 14:16:25 -05:00
{
2009-06-17 01:01:39 -05:00
DestroyForNearbyPlayers ();
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( force )
2009-06-17 01:01:39 -05:00
{
2013-06-11 19:54:27 -02:30
if ( IsAlive ())
2009-06-17 01:01:39 -05:00
setDeathState ( JUST_DIED );
2010-04-07 19:14:10 +02:00
else if ( getDeathState () != CORPSE )
2009-06-17 01:01:39 -05:00
setDeathState ( CORPSE );
}
2009-10-17 15:51:44 -07:00
2010-09-15 06:22:54 +02:00
RemoveCorpse ( false );
2009-10-17 15:51:44 -07:00
2010-04-07 23:25:02 +02:00
if ( getDeathState () == DEAD )
2008-10-11 14:16:25 -05:00
{
2015-02-25 00:13:14 +01:00
if ( m_spawnId )
GetMap () -> RemoveCreatureRespawnTime ( m_spawnId );
2009-10-17 15:51:44 -07:00
2014-09-16 20:47:08 +02:00
TC_LOG_DEBUG ( "entities.unit" , "Respawning creature %s (%s)" ,
GetName (). c_str (), GetGUID (). ToString (). c_str ());
2009-04-14 14:33:48 +02:00
m_respawnTime = 0 ;
2014-07-31 21:41:41 +02:00
ResetPickPocketRefillTimer ();
2014-06-26 22:01:55 +02:00
loot . clear ();
2010-04-07 19:14:10 +02:00
if ( m_originalEntry != GetEntry ())
2009-04-14 14:33:48 +02:00
UpdateEntry ( m_originalEntry );
2009-10-17 15:51:44 -07:00
2014-01-06 20:30:11 +01:00
SelectLevel ();
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
setDeathState ( JUST_RESPAWNED );
2009-10-17 15:51:44 -07:00
2011-05-07 20:09:45 +02:00
uint32 displayID = GetNativeDisplayId ();
2011-09-15 14:08:17 +02:00
CreatureModelInfo const * minfo = sObjectMgr -> GetCreatureModelRandomGender ( & displayID );
2009-09-27 15:14:25 -07:00
if ( minfo ) // Cancel load if no model defined
{
2011-04-28 22:11:45 +02:00
SetDisplayId ( displayID );
SetNativeDisplayId ( displayID );
2014-11-30 19:09:26 +01:00
SetByteValue ( UNIT_FIELD_BYTES_0 , UNIT_BYTES_0_OFFSET_GENDER , minfo -> gender );
2009-09-27 15:14:25 -07:00
}
2009-10-17 15:51:44 -07:00
2010-09-15 06:23:25 +02:00
GetMotionMaster () -> InitDefault ();
2009-04-14 14:33:48 +02:00
//Call AI respawn virtual function
2009-12-06 16:41:05 +01:00
if ( IsAIEnabled )
2013-12-15 21:14:47 +01:00
{
//reset the AI to be sure no dirty or uninitialized values will be used till next tick
AI () -> Reset ();
2016-04-02 02:50:44 +02:00
m_TriggerJustRespawned = true ; //delay event to next tick so all creatures are created on the map before processing
2013-12-15 21:14:47 +01:00
}
2009-10-17 15:51:44 -07:00
2015-02-25 00:13:14 +01:00
uint32 poolid = GetSpawnId () ? sPoolMgr -> IsPartOfAPool < Creature > ( GetSpawnId ()) : 0 ;
2009-04-15 17:06:38 -05:00
if ( poolid )
2015-02-25 00:13:14 +01:00
sPoolMgr -> UpdatePool < Creature > ( poolid , GetSpawnId ());
2010-08-08 19:45:53 +02:00
2010-06-27 03:47:18 +02:00
//Re-initialize reactstate that could be altered by movementgenerators
InitializeReactState ();
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-02-27 15:25:14 +02:00
UpdateObjectVisibility ();
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2016-08-25 23:04:21 +02:00
void Creature :: ForcedDespawn ( uint32 timeMSToDespawn , Seconds const & forceRespawnTimer )
2009-06-18 17:05:01 -05:00
{
2010-01-16 19:51:59 +03:00
if ( timeMSToDespawn )
{
2016-08-25 23:04:21 +02:00
ForcedDespawnDelayEvent * pEvent = new ForcedDespawnDelayEvent ( * this , forceRespawnTimer );
2010-01-16 19:51:59 +03:00
m_Events . AddEvent ( pEvent , m_Events . CalculateTime ( timeMSToDespawn ));
return ;
}
2013-06-11 19:54:27 -02:30
if ( IsAlive ())
2016-08-25 23:04:21 +02:00
{
if ( forceRespawnTimer > Seconds :: zero ())
{
uint32 respawnDelay = m_respawnDelay ;
uint32 corpseDelay = m_corpseDelay ;
m_respawnDelay = forceRespawnTimer . count ();
m_corpseDelay = 0 ;
setDeathState ( JUST_DIED );
m_respawnDelay = respawnDelay ;
m_corpseDelay = corpseDelay ;
}
else
setDeathState ( JUST_DIED );
}
2010-01-16 19:51:59 +03:00
2010-09-15 06:22:54 +02:00
RemoveCorpse ( false );
2009-06-18 17:05:01 -05:00
}
2009-10-17 15:51:44 -07:00
2016-08-25 23:04:21 +02:00
void Creature :: DespawnOrUnsummon ( uint32 msTimeToDespawn /*= 0*/ , Seconds const & forceRespawnTimer /*= 0*/ )
2011-01-02 21:33:37 +01:00
{
if ( TempSummon * summon = this -> ToTempSummon ())
2011-03-07 21:25:34 +01:00
summon -> UnSummon ( msTimeToDespawn );
2011-01-02 21:33:37 +01:00
else
2016-08-25 23:04:21 +02:00
ForcedDespawn ( msTimeToDespawn , forceRespawnTimer );
2011-01-02 21:33:37 +01:00
}
2013-03-20 14:18:26 +01:00
bool Creature :: IsImmunedToSpell ( SpellInfo const * spellInfo ) const
2008-10-11 14:16:25 -05:00
{
if ( ! spellInfo )
return false ;
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
// Creature is immune to main mechanic of the spell
if ( GetCreatureTemplate () -> MechanicImmuneMask & ( 1 << ( spellInfo -> Mechanic - 1 )))
2011-12-14 16:13:10 +01:00
return true ;
2012-05-30 08:01:02 +02:00
// This check must be done instead of 'if (GetCreatureTemplate()->MechanicImmuneMask & (1 << (spellInfo->Mechanic - 1)))' for not break
// the check of mechanic immunity on DB (tested) because GetCreatureTemplate()->MechanicImmuneMask and m_spellImmune[IMMUNITY_MECHANIC] don't have same data.
2011-12-14 16:13:10 +01:00
bool immunedToAllEffects = true ;
2015-01-10 01:35:47 +01:00
for ( SpellEffectInfo const * effect : spellInfo -> GetEffectsForDifficulty ( GetMap () -> GetDifficultyID ()))
2012-06-17 02:21:40 +02:00
{
2014-11-24 18:57:49 +01:00
if ( ! effect || ! effect -> IsEffect ())
2012-06-17 23:17:59 +02:00
continue ;
2014-11-24 18:57:49 +01:00
if ( ! IsImmunedToSpellEffect ( spellInfo , effect -> EffectIndex ))
2011-12-14 16:13:10 +01:00
{
immunedToAllEffects = false ;
break ;
}
2012-06-17 02:21:40 +02:00
}
2011-12-14 16:13:10 +01:00
if ( immunedToAllEffects )
2008-10-11 14:16:25 -05:00
return true ;
2009-10-17 15:51:44 -07:00
2009-01-04 16:45:32 -06:00
return Unit :: IsImmunedToSpell ( spellInfo );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-07-26 23:09:28 +02:00
bool Creature :: IsImmunedToSpellEffect ( SpellInfo const * spellInfo , uint32 index ) const
2008-10-11 14:16:25 -05:00
{
2015-01-24 19:54:41 +01:00
SpellEffectInfo const * effect = spellInfo -> GetEffect ( this , index );
2014-11-24 18:57:49 +01:00
if ( ! effect )
return true ;
if ( GetCreatureTemplate () -> MechanicImmuneMask & ( 1 << ( effect -> Mechanic - 1 )))
2008-10-11 14:16:25 -05:00
return true ;
2009-10-17 15:51:44 -07:00
2014-11-24 18:57:49 +01:00
if ( GetCreatureTemplate () -> type == CREATURE_TYPE_MECHANICAL && effect -> Effect == SPELL_EFFECT_HEAL )
2010-07-28 01:26:18 +02:00
return true ;
2010-01-20 21:10:18 +01:00
return Unit :: IsImmunedToSpellEffect ( spellInfo , index );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2013-03-04 12:41:17 +01:00
bool Creature :: isElite () const
{
2013-06-11 18:39:51 -02:30
if ( IsPet ())
2013-03-04 12:41:17 +01:00
return false ;
uint32 rank = GetCreatureTemplate () -> rank ;
return rank != CREATURE_ELITE_NORMAL && rank != CREATURE_ELITE_RARE ;
}
bool Creature :: isWorldBoss () const
{
2013-06-11 18:39:51 -02:30
if ( IsPet ())
2013-03-04 12:41:17 +01:00
return false ;
2016-05-26 12:56:50 +04:00
return ( GetCreatureTemplate () -> type_flags & CREATURE_TYPE_FLAG_BOSS_MOB ) != 0 ;
2013-03-04 12:41:17 +01:00
}
2012-05-30 08:01:02 +02:00
SpellInfo const * Creature :: reachWithSpellAttack ( Unit * victim )
2008-10-11 14:16:25 -05:00
{
2012-05-30 08:01:02 +02:00
if ( ! victim )
2016-04-02 02:50:44 +02:00
return nullptr ;
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
for ( uint32 i = 0 ; i < MAX_CREATURE_SPELLS ; ++ i )
2008-10-11 14:16:25 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! m_spells [ i ])
2008-10-11 14:16:25 -05:00
continue ;
2011-09-15 14:08:17 +02:00
SpellInfo const * spellInfo = sSpellMgr -> GetSpellInfo ( m_spells [ i ]);
2010-04-07 19:14:10 +02:00
if ( ! spellInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "entities.unit" , "WORLD: unknown spell id %i" , m_spells [ i ]);
2008-10-11 14:16:25 -05:00
continue ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
bool bcontinue = true ;
2015-01-10 01:35:47 +01:00
for ( SpellEffectInfo const * effect : spellInfo -> GetEffectsForDifficulty ( GetMap () -> GetDifficultyID ()))
2008-10-11 14:16:25 -05:00
{
2014-11-24 18:57:49 +01:00
if ( effect && (( effect -> Effect == SPELL_EFFECT_SCHOOL_DAMAGE ) ||
( effect -> Effect == SPELL_EFFECT_INSTAKILL ) ||
( effect -> Effect == SPELL_EFFECT_ENVIRONMENTAL_DAMAGE ) ||
( effect -> Effect == SPELL_EFFECT_HEALTH_LEECH )
))
2008-10-11 14:16:25 -05:00
{
bcontinue = false ;
break ;
}
}
2012-03-17 20:13:37 +08:00
if ( bcontinue )
continue ;
2009-10-17 15:51:44 -07:00
2015-03-08 13:31:57 +01:00
std :: vector < SpellInfo :: CostData > costs = spellInfo -> CalcPowerCost ( this , SpellSchoolMask ( spellInfo -> SchoolMask ));
auto m = std :: find_if ( costs . begin (), costs . end (), []( SpellInfo :: CostData const & cost ) { return cost . Power == POWER_MANA ; });
if ( m != costs . end ())
2015-03-22 13:47:55 +01:00
if ( m -> Amount > GetPower ( POWER_MANA ))
2015-03-08 13:31:57 +01:00
continue ;
2011-07-26 23:09:28 +02:00
float range = spellInfo -> GetMaxRange ( false );
float minrange = spellInfo -> GetMinRange ( false );
2012-05-30 08:01:02 +02:00
float dist = GetDistance ( victim );
2010-04-07 22:59:46 +02:00
if ( dist > range || dist < minrange )
2008-10-11 14:16:25 -05:00
continue ;
2016-06-23 22:19:52 +02:00
if ( spellInfo -> PreventionType & SPELL_PREVENTION_TYPE_SILENCE && HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_SILENCED ))
2009-01-07 11:56:57 -06:00
continue ;
2016-06-23 22:19:52 +02:00
if ( spellInfo -> PreventionType & SPELL_PREVENTION_TYPE_PACIFY && HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_PACIFIED ))
2008-10-11 14:16:25 -05:00
continue ;
return spellInfo ;
}
2016-04-02 02:50:44 +02:00
return nullptr ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
SpellInfo const * Creature :: reachWithSpellCure ( Unit * victim )
2008-10-11 14:16:25 -05:00
{
2012-05-30 08:01:02 +02:00
if ( ! victim )
2016-04-02 02:50:44 +02:00
return nullptr ;
2009-10-17 15:51:44 -07:00
2016-03-24 16:12:53 +01:00
for ( uint32 i = 0 ; i < MAX_CREATURE_SPELLS ; ++ i )
2008-10-11 14:16:25 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! m_spells [ i ])
2008-10-11 14:16:25 -05:00
continue ;
2011-09-15 14:08:17 +02:00
SpellInfo const * spellInfo = sSpellMgr -> GetSpellInfo ( m_spells [ i ]);
2010-04-07 19:14:10 +02:00
if ( ! spellInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "entities.unit" , "WORLD: unknown spell id %i" , m_spells [ i ]);
2008-10-11 14:16:25 -05:00
continue ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
bool bcontinue = true ;
2015-01-10 01:35:47 +01:00
for ( SpellEffectInfo const * effect : spellInfo -> GetEffectsForDifficulty ( GetMap () -> GetDifficultyID ()))
2008-10-11 14:16:25 -05:00
{
2014-11-24 18:57:49 +01:00
if ( effect && ( effect -> Effect == SPELL_EFFECT_HEAL ))
2008-10-11 14:16:25 -05:00
{
bcontinue = false ;
break ;
}
}
2012-05-30 08:01:02 +02:00
if ( bcontinue )
continue ;
2009-10-17 15:51:44 -07:00
2015-03-08 13:31:57 +01:00
std :: vector < SpellInfo :: CostData > costs = spellInfo -> CalcPowerCost ( this , SpellSchoolMask ( spellInfo -> SchoolMask ));
auto m = std :: find_if ( costs . begin (), costs . end (), []( SpellInfo :: CostData const & cost ) { return cost . Power == POWER_MANA ; });
if ( m != costs . end ())
2015-03-22 13:47:55 +01:00
if ( m -> Amount > GetPower ( POWER_MANA ))
2015-03-08 13:31:57 +01:00
continue ;
2011-07-26 23:09:28 +02:00
float range = spellInfo -> GetMaxRange ( true );
float minrange = spellInfo -> GetMinRange ( true );
2012-05-30 08:01:02 +02:00
float dist = GetDistance ( victim );
//if (!isInFront(victim, range) && spellInfo->AttributesEx)
2008-10-11 14:16:25 -05:00
// continue;
2010-04-07 22:59:46 +02:00
if ( dist > range || dist < minrange )
2008-10-11 14:16:25 -05:00
continue ;
2016-06-23 22:19:52 +02:00
if ( spellInfo -> PreventionType & SPELL_PREVENTION_TYPE_SILENCE && HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_SILENCED ))
2009-01-07 11:56:57 -06:00
continue ;
2016-06-23 22:19:52 +02:00
if ( spellInfo -> PreventionType & SPELL_PREVENTION_TYPE_PACIFY && HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_PACIFIED ))
2008-10-11 14:16:25 -05:00
continue ;
return spellInfo ;
}
2016-04-02 02:50:44 +02:00
return nullptr ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-04-25 12:59:27 +02:00
// select nearest hostile unit within the given distance (regardless of threat list).
2013-09-30 10:11:02 +02:00
Unit * Creature :: SelectNearestTarget ( float dist , bool playerOnly /* = false */ ) const
2008-11-20 20:28:17 -06:00
{
2011-10-18 10:53:34 -04:00
CellCoord p ( Trinity :: ComputeCellCoord ( GetPositionX (), GetPositionY ()));
2008-11-20 20:28:17 -06:00
Cell cell ( p );
cell . SetNoCreate ();
2009-10-17 15:51:44 -07:00
2016-04-02 02:50:44 +02:00
Unit * target = nullptr ;
2009-10-17 15:51:44 -07:00
2010-04-25 12:59:27 +02:00
{
2010-11-13 17:18:09 +01:00
if ( dist == 0.0f )
2010-04-25 12:59:27 +02:00
dist = MAX_VISIBILITY_DISTANCE ;
2013-09-30 10:11:02 +02:00
Trinity :: NearestHostileUnitCheck u_check ( this , dist , playerOnly );
2010-04-25 12:59:27 +02:00
Trinity :: UnitLastSearcher < Trinity :: NearestHostileUnitCheck > searcher ( this , target , u_check );
TypeContainerVisitor < Trinity :: UnitLastSearcher < Trinity :: NearestHostileUnitCheck > , WorldTypeMapContainer > world_unit_searcher ( searcher );
TypeContainerVisitor < Trinity :: UnitLastSearcher < Trinity :: NearestHostileUnitCheck > , GridTypeMapContainer > grid_unit_searcher ( searcher );
cell . Visit ( p , world_unit_searcher , * GetMap (), * this , dist );
cell . Visit ( p , grid_unit_searcher , * GetMap (), * this , dist );
}
return target ;
}
// select nearest hostile unit within the given attack distance (i.e. distance is ignored if > than ATTACK_DISTANCE), regardless of threat list.
Unit * Creature :: SelectNearestTargetInAttackDistance ( float dist ) const
{
2011-10-18 10:53:34 -04:00
CellCoord p ( Trinity :: ComputeCellCoord ( GetPositionX (), GetPositionY ()));
2010-04-25 12:59:27 +02:00
Cell cell ( p );
cell . SetNoCreate ();
2016-04-02 02:50:44 +02:00
Unit * target = nullptr ;
2010-04-25 12:59:27 +02:00
2011-06-12 17:13:28 +02:00
if ( dist > MAX_VISIBILITY_DISTANCE )
{
2014-10-22 17:33:55 +02:00
TC_LOG_ERROR ( "entities.unit" , "Creature (%s) SelectNearestTargetInAttackDistance called with dist > MAX_VISIBILITY_DISTANCE. Distance set to ATTACK_DISTANCE." , GetGUID (). ToString (). c_str ());
2011-06-12 17:13:28 +02:00
dist = ATTACK_DISTANCE ;
}
2010-04-25 12:59:27 +02:00
2008-11-20 20:28:17 -06:00
{
Trinity :: NearestHostileUnitInAttackDistanceCheck u_check ( this , dist );
2009-01-31 16:38:50 -06:00
Trinity :: UnitLastSearcher < Trinity :: NearestHostileUnitInAttackDistanceCheck > searcher ( this , target , u_check );
2009-10-17 15:51:44 -07:00
2008-11-20 20:28:17 -06:00
TypeContainerVisitor < Trinity :: UnitLastSearcher < Trinity :: NearestHostileUnitInAttackDistanceCheck > , WorldTypeMapContainer > world_unit_searcher ( searcher );
TypeContainerVisitor < Trinity :: UnitLastSearcher < Trinity :: NearestHostileUnitInAttackDistanceCheck > , GridTypeMapContainer > grid_unit_searcher ( searcher );
2009-10-17 15:51:44 -07:00
2011-06-12 17:13:28 +02:00
cell . Visit ( p , world_unit_searcher , * GetMap (), * this , ATTACK_DISTANCE > dist ? ATTACK_DISTANCE : dist );
cell . Visit ( p , grid_unit_searcher , * GetMap (), * this , ATTACK_DISTANCE > dist ? ATTACK_DISTANCE : dist );
2008-11-21 10:07:11 -06:00
}
2009-10-17 15:51:44 -07:00
2008-11-21 10:07:11 -06:00
return target ;
2008-11-20 20:28:17 -06:00
}
2009-10-17 15:51:44 -07:00
2011-07-11 16:17:21 +02:00
Player * Creature :: SelectNearestPlayer ( float distance ) const
{
2016-04-02 02:50:44 +02:00
Player * target = nullptr ;
2011-07-11 16:17:21 +02:00
Trinity :: NearestPlayerInObjectRangeCheck checker ( this , distance );
Trinity :: PlayerLastSearcher < Trinity :: NearestPlayerInObjectRangeCheck > searcher ( this , target , checker );
VisitNearbyObject ( distance , searcher );
return target ;
}
2009-12-19 17:43:47 +01:00
void Creature :: SendAIReaction ( AiReaction reactionType )
{
2014-11-19 00:30:31 +02:00
WorldPackets :: Combat :: AIReaction packet ;
2009-12-19 17:43:47 +01:00
2014-11-19 00:30:31 +02:00
packet . UnitGUID = GetGUID ();
packet . Reaction = reactionType ;
2009-12-19 17:43:47 +01:00
2014-11-19 21:09:30 +01:00
SendMessageToSet ( packet . Write (), true );
2009-12-19 17:43:47 +01:00
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "WORLD: Sent SMSG_AI_REACTION, type %u." , reactionType );
2009-12-19 17:43:47 +01:00
}
2009-05-21 10:48:00 -05:00
void Creature :: CallAssistance ()
2008-10-11 14:16:25 -05:00
{
2013-06-11 19:54:27 -02:30
if ( ! m_AlreadyCallAssistance && GetVictim () && ! IsPet () && ! IsCharmed ())
2008-10-11 14:16:25 -05:00
{
2008-11-25 16:36:50 -06:00
SetNoCallAssistance ( true );
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
float radius = sWorld -> getFloatConfig ( CONFIG_CREATURE_FAMILY_ASSISTANCE_RADIUS );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( radius > 0 )
2008-10-11 14:16:25 -05:00
{
std :: list < Creature *> assistList ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
{
2011-10-18 10:53:34 -04:00
CellCoord p ( Trinity :: ComputeCellCoord ( GetPositionX (), GetPositionY ()));
2008-10-11 14:16:25 -05:00
Cell cell ( p );
cell . SetNoCreate ();
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
Trinity :: AnyAssistCreatureInRangeCheck u_check ( this , GetVictim (), radius );
2009-12-20 15:20:04 +01:00
Trinity :: CreatureListSearcher < Trinity :: AnyAssistCreatureInRangeCheck > searcher ( this , assistList , u_check );
2009-10-17 15:51:44 -07:00
2008-10-14 11:57:03 -05:00
TypeContainerVisitor < Trinity :: CreatureListSearcher < Trinity :: AnyAssistCreatureInRangeCheck > , GridTypeMapContainer > grid_creature_searcher ( searcher );
2009-10-17 15:51:44 -07:00
2010-01-23 22:24:41 +01:00
cell . Visit ( p , grid_creature_searcher , * GetMap (), * this , radius );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-11-25 16:36:50 -06:00
if ( ! assistList . empty ())
2008-10-11 14:16:25 -05:00
{
2014-04-19 21:48:22 +02:00
AssistDelayEvent * e = new AssistDelayEvent ( EnsureVictim () -> GetGUID (), * this );
2008-11-25 16:36:50 -06:00
while ( ! assistList . empty ())
{
// Pushing guids because in delay can happen some creature gets despawned => invalid pointer
e -> AddAssistant (( * assistList . begin ()) -> GetGUID ());
assistList . pop_front ();
}
2010-12-23 23:25:44 +01:00
m_Events . AddEvent ( e , m_Events . CalculateTime ( sWorld -> getIntConfig ( CONFIG_CREATURE_FAMILY_ASSISTANCE_DELAY )));
2008-10-11 14:16:25 -05:00
}
}
}
}
2009-10-17 15:51:44 -07:00
2011-10-19 17:40:44 +01:00
void Creature :: CallForHelp ( float radius )
2009-06-04 16:10:26 -05:00
{
2013-06-11 19:54:27 -02:30
if ( radius <= 0.0f || ! GetVictim () || IsPet () || IsCharmed ())
2009-06-04 16:10:26 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-10-18 10:53:34 -04:00
CellCoord p ( Trinity :: ComputeCellCoord ( GetPositionX (), GetPositionY ()));
2009-06-04 16:10:26 -05:00
Cell cell ( p );
cell . SetNoCreate ();
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
Trinity :: CallOfHelpCreatureInRangeDo u_do ( this , GetVictim (), radius );
2009-12-20 15:20:04 +01:00
Trinity :: CreatureWorker < Trinity :: CallOfHelpCreatureInRangeDo > worker ( this , u_do );
2009-10-17 15:51:44 -07:00
2009-12-20 15:20:04 +01:00
TypeContainerVisitor < Trinity :: CreatureWorker < Trinity :: CallOfHelpCreatureInRangeDo > , GridTypeMapContainer > grid_creature_searcher ( worker );
2009-10-17 15:51:44 -07:00
2011-10-19 17:40:44 +01:00
cell . Visit ( p , grid_creature_searcher , * GetMap (), * this , radius );
2009-06-04 16:10:26 -05:00
}
2009-10-17 15:51:44 -07:00
2009-05-21 10:15:09 -05:00
bool Creature :: CanAssistTo ( const Unit * u , const Unit * enemy , bool checkfaction /*= true*/ ) const
2008-11-25 16:36:50 -06:00
{
2014-10-13 22:37:44 +02:00
if ( IsInEvadeMode ())
return false ;
2008-12-07 09:21:12 -06:00
// is it true?
2010-04-07 19:14:10 +02:00
if ( ! HasReactState ( REACT_AGGRESSIVE ))
2008-11-25 16:36:50 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2008-11-25 16:36:50 -06:00
// we don't need help from zombies :)
2013-06-11 19:54:27 -02:30
if ( ! IsAlive ())
2008-11-25 16:36:50 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2009-07-24 09:15:52 +08:00
// we don't need help from non-combatant ;)
2013-06-11 19:54:27 -02:30
if ( IsCivilian ())
2009-07-24 09:15:52 +08:00
return false ;
2009-10-17 15:51:44 -07:00
2012-02-03 18:46:05 +01:00
if ( HasFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_NON_ATTACKABLE | UNIT_FLAG_NOT_SELECTABLE | UNIT_FLAG_IMMUNE_TO_NPC ))
2011-03-06 12:22:04 +01:00
return false ;
2008-11-25 16:36:50 -06:00
// skip fighting creature
2013-06-11 19:54:27 -02:30
if ( IsInCombat ())
2008-11-25 16:36:50 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2008-11-25 16:36:50 -06:00
// only free creature
2014-10-21 20:27:00 +02:00
if ( ! GetCharmerOrOwnerGUID (). IsEmpty ())
2008-11-25 16:36:50 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2009-05-21 10:15:09 -05:00
// only from same creature faction
if ( checkfaction )
{
if ( getFaction () != u -> getFaction ())
return false ;
}
else
{
if ( ! IsFriendlyTo ( u ))
return false ;
}
2009-10-17 15:51:44 -07:00
2008-11-25 16:36:50 -06:00
// skip non hostile to caster enemy creatures
2009-05-21 10:15:09 -05:00
if ( ! IsHostileTo ( enemy ))
2008-11-25 16:36:50 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2008-11-25 16:36:50 -06:00
return true ;
}
2009-10-17 15:51:44 -07:00
2009-09-29 15:13:46 -07:00
// use this function to avoid having hostile creatures attack
// friendlies and other mobs they shouldn't attack
2011-06-12 01:47:45 +02:00
bool Creature :: _IsTargetAcceptable ( const Unit * target ) const
2009-09-29 15:13:46 -07:00
{
2010-07-30 20:04:28 +02:00
ASSERT ( target );
2009-10-22 02:19:30 -07:00
2009-10-23 23:26:04 -07:00
// if the target cannot be attacked, the target is not acceptable
2009-10-24 14:53:03 -07:00
if ( IsFriendlyTo ( target )
2011-08-19 14:53:40 +02:00
|| ! target -> isTargetableForAttack ( false )
2009-10-24 14:53:03 -07:00
|| ( m_vehicle && ( IsOnVehicle ( target ) || m_vehicle -> GetBase () -> IsOnVehicle ( target ))))
2009-10-23 20:01:15 -07:00
return false ;
2009-10-17 15:51:44 -07:00
2012-02-03 19:12:41 +02:00
if ( target -> HasUnitState ( UNIT_STATE_DIED ))
2011-02-10 22:36:35 -03:00
{
// guards can detect fake death
2013-06-11 19:54:27 -02:30
if ( IsGuard () && target -> HasFlag ( UNIT_FIELD_FLAGS_2 , UNIT_FLAG2_FEIGN_DEATH ))
2011-02-10 22:36:35 -03:00
return true ;
else
return false ;
}
2011-09-15 14:08:17 +02:00
const Unit * myVictim = getAttackerForHelper ();
2011-06-12 01:47:45 +02:00
const Unit * targetVictim = target -> getAttackerForHelper ();
2009-10-23 23:29:45 -07:00
2009-10-23 23:26:04 -07:00
// if I'm already fighting target, or I'm hostile towards the target, the target is acceptable
if ( myVictim == target || targetVictim == this || IsHostileTo ( target ))
return true ;
2009-10-17 15:51:44 -07:00
2009-10-10 16:26:00 -07:00
// if the target's victim is friendly, and the target is neutral, the target is acceptable
2009-10-24 14:53:03 -07:00
if ( targetVictim && IsFriendlyTo ( targetVictim ))
2009-10-10 16:26:00 -07:00
return true ;
2009-10-17 15:51:44 -07:00
2009-10-10 16:26:00 -07:00
// if the target's victim is not friendly, or the target is friendly, the target is not acceptable
return false ;
2009-09-29 15:13:46 -07:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: SaveRespawnTime ()
{
2015-02-25 00:13:14 +01:00
if ( IsSummon () || ! m_spawnId || ( m_creatureData && ! m_creatureData -> dbData ))
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2015-02-25 00:13:14 +01:00
GetMap () -> SaveCreatureRespawnTime ( m_spawnId , m_respawnTime );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
// this should not be called by petAI or
2013-06-11 21:25:12 -02:30
bool Creature :: CanCreatureAttack ( Unit const * victim , bool /*force*/ ) const
2008-10-11 14:16:25 -05:00
{
2012-05-30 08:01:02 +02:00
if ( ! victim -> IsInMap ( this ))
2009-08-15 14:30:14 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
if ( ! IsValidAttackTarget ( victim ))
2009-08-15 14:30:14 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
if ( ! victim -> isInAccessiblePlaceFor ( this ))
2009-08-15 14:30:14 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2012-05-30 08:01:02 +02:00
if ( IsAIEnabled && ! AI () -> CanAIAttack ( victim ))
2009-08-26 16:54:59 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2014-01-04 18:22:06 +01:00
if ( GetMap () -> IsDungeon ())
2009-08-15 14:30:14 -05:00
return true ;
2009-10-17 15:51:44 -07:00
2016-02-09 22:53:38 +01:00
// if the mob is actively being damaged, do not reset due to distance unless it's a world boss
if ( ! isWorldBoss ())
if ( time ( NULL ) - GetLastDamagedTime () <= MAX_AGGRO_RESET_TIME )
return true ;
2008-11-14 16:28:45 -06:00
//Use AttackDistance in distance check if threat radius is lower. This prevents creature bounce in and out of combat every update tick.
2012-05-30 08:01:02 +02:00
float dist = std :: max ( GetAttackDistance ( victim ), sWorld -> getFloatConfig ( CONFIG_THREAT_RADIUS )) + m_CombatDistance ;
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
if ( Unit * unit = GetCharmerOrOwner ())
2012-05-30 08:01:02 +02:00
return victim -> IsWithinDist ( unit , dist );
2009-08-15 14:30:14 -05:00
else
2012-05-30 08:01:02 +02:00
return victim -> IsInDist ( & m_homePosition , dist );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-28 22:46:40 +02:00
CreatureAddon const * Creature :: GetCreatureAddon () const
2008-10-11 14:16:25 -05:00
{
2015-02-25 00:13:14 +01:00
if ( m_spawnId )
2008-10-11 14:16:25 -05:00
{
2015-02-25 00:13:14 +01:00
if ( CreatureAddon const * addon = sObjectMgr -> GetCreatureAddon ( m_spawnId ))
2008-10-11 14:16:25 -05:00
return addon ;
}
2009-10-17 15:51:44 -07:00
2009-12-17 11:06:50 +01:00
// dependent from difficulty mode entry
2012-05-30 08:01:02 +02:00
return sObjectMgr -> GetCreatureTemplateAddon ( GetCreatureTemplate () -> Entry );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
//creature_addon table
2015-11-10 19:58:13 +01:00
bool Creature :: LoadCreaturesAddon ()
2008-10-11 14:16:25 -05:00
{
2011-09-15 14:08:17 +02:00
CreatureAddon const * cainfo = GetCreatureAddon ();
2010-04-07 19:14:10 +02:00
if ( ! cainfo )
2008-10-11 14:16:25 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
if ( cainfo -> mount != 0 )
Mount ( cainfo -> mount );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
if ( cainfo -> bytes1 != 0 )
2009-05-31 15:58:58 -05:00
{
// 0 StandState
// 1 FreeTalentPoints Pet only, so always 0 for default creature
// 2 StandFlags
// 3 StandMiscFlags
2009-10-17 15:51:44 -07:00
2016-12-13 20:58:22 +01:00
SetByteValue ( UNIT_FIELD_BYTES_1 , UNIT_BYTES_1_OFFSET_STAND_STATE , uint8 ( cainfo -> bytes1 & 0xFF ));
//SetByteValue(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_PET_TALENTS, uint8((cainfo->bytes1 >> 8) & 0xFF));
SetByteValue ( UNIT_FIELD_BYTES_1 , UNIT_BYTES_1_OFFSET_PET_TALENTS , 0 );
SetByteValue ( UNIT_FIELD_BYTES_1 , UNIT_BYTES_1_OFFSET_VIS_FLAG , uint8 (( cainfo -> bytes1 >> 16 ) & 0xFF ));
SetByteValue ( UNIT_FIELD_BYTES_1 , UNIT_BYTES_1_OFFSET_ANIM_TIER , uint8 (( cainfo -> bytes1 >> 24 ) & 0xFF ));
2012-05-30 08:01:02 +02:00
//! Suspected correlation between UNIT_FIELD_BYTES_1, offset 3, value 0x2:
2013-02-22 01:21:49 +01:00
//! If no inhabittype_fly (if no MovementFlag_DisableGravity or MovementFlag_CanFly flag found in sniffs)
//! Check using InhabitType as movement flags are assigned dynamically
//! basing on whether the creature is in air or not
2012-05-30 08:01:02 +02:00
//! Set MovementFlag_Hover. Otherwise do nothing.
2016-12-13 20:58:22 +01:00
if ( GetByteValue ( UNIT_FIELD_BYTES_1 , UNIT_BYTES_1_OFFSET_ANIM_TIER ) & UNIT_BYTE1_FLAG_HOVER && ! ( GetCreatureTemplate () -> InhabitType & INHABIT_AIR ))
2012-05-30 08:01:02 +02:00
AddUnitMovementFlag ( MOVEMENTFLAG_HOVER );
2009-05-31 15:58:58 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
if ( cainfo -> bytes2 != 0 )
2009-05-31 15:58:58 -05:00
{
// 0 SheathState
2016-12-13 20:58:22 +01:00
// 1 PvpFlags
// 2 PetFlags Pet only, so always 0 for default creature
2009-05-31 15:58:58 -05:00
// 3 ShapeshiftForm Must be determined/set by shapeshift spell/aura
2009-10-17 15:51:44 -07:00
2016-12-13 20:58:22 +01:00
SetByteValue ( UNIT_FIELD_BYTES_2 , UNIT_BYTES_2_OFFSET_SHEATH_STATE , uint8 ( cainfo -> bytes2 & 0xFF ));
//SetByteValue(UNIT_FIELD_BYTES_2, UNIT_BYTES_2_OFFSET_PVP_FLAG, uint8((cainfo->bytes2 >> 8) & 0xFF));
//SetByteValue(UNIT_FIELD_BYTES_2, UNIT_BYTES_2_OFFSET_PET_FLAGS, uint8((cainfo->bytes2 >> 16) & 0xFF));
SetByteValue ( UNIT_FIELD_BYTES_2 , UNIT_BYTES_2_OFFSET_PET_FLAGS , 0 );
//SetByteValue(UNIT_FIELD_BYTES_2, UNIT_BYTES_2_OFFSET_SHAPESHIFT_FORM, uint8((cainfo->bytes2 >> 24) & 0xFF));
SetByteValue ( UNIT_FIELD_BYTES_2 , UNIT_BYTES_2_OFFSET_SHAPESHIFT_FORM , 0 );
2009-05-31 15:58:58 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
if ( cainfo -> emote != 0 )
SetUInt32Value ( UNIT_NPC_EMOTESTATE , cainfo -> emote );
2009-10-17 15:51:44 -07:00
2016-08-20 19:23:07 -02:30
SetAIAnimKitId ( cainfo -> aiAnimKit );
SetMovementAnimKitId ( cainfo -> movementAnimKit );
SetMeleeAnimKitId ( cainfo -> meleeAnimKit );
2009-09-19 12:13:08 -07:00
//Load Path
if ( cainfo -> path_id != 0 )
m_path_id = cainfo -> path_id ;
2009-10-17 15:51:44 -07:00
2011-04-28 22:46:40 +02:00
if ( ! cainfo -> auras . empty ())
2008-10-11 14:16:25 -05:00
{
2011-04-28 22:46:40 +02:00
for ( std :: vector < uint32 >:: const_iterator itr = cainfo -> auras . begin (); itr != cainfo -> auras . end (); ++ itr )
2008-10-11 14:16:25 -05:00
{
2011-09-15 14:08:17 +02:00
SpellInfo const * AdditionalSpellInfo = sSpellMgr -> GetSpellInfo ( * itr );
2008-10-11 14:16:25 -05:00
if ( ! AdditionalSpellInfo )
{
2014-10-22 17:33:55 +02:00
TC_LOG_ERROR ( "sql.sql" , "Creature (%s) has wrong spell %u defined in `auras` field." , GetGUID (). ToString (). c_str (), * itr );
2008-10-11 14:16:25 -05:00
continue ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// skip already applied aura
2011-04-28 22:46:40 +02:00
if ( HasAura ( * itr ))
2008-10-11 14:16:25 -05:00
continue ;
2009-10-17 15:51:44 -07:00
2011-04-28 22:46:40 +02:00
AddAura ( * itr , this );
2014-10-22 17:33:55 +02:00
TC_LOG_DEBUG ( "entities.unit" , "Spell: %u added to creature (%s)" , * itr , GetGUID (). ToString (). c_str ());
2008-10-11 14:16:25 -05:00
}
}
2012-05-30 08:01:02 +02:00
2008-10-11 14:16:25 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2008-11-14 16:28:45 -06:00
/// Send a message to LocalDefense channel for players opposition team in the zone
2008-10-11 14:16:25 -05:00
void Creature :: SendZoneUnderAttackMessage ( Player * attacker )
{
uint32 enemy_team = attacker -> GetTeam ();
2015-04-04 19:52:53 +02:00
WorldPackets :: Misc :: ZoneUnderAttack packet ;
2015-04-04 18:03:56 +02:00
packet . AreaID = GetAreaId ();
2016-04-02 02:50:44 +02:00
sWorld -> SendGlobalMessage ( packet . Write (), nullptr , ( enemy_team == ALLIANCE ? HORDE : ALLIANCE ));
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2009-06-27 15:37:33 -05:00
void Creature :: SetInCombatWithZone ()
{
if ( ! CanHaveThreatList ())
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "entities.unit" , "Creature entry %u call SetInCombatWithZone but creature cannot have threat list." , GetEntry ());
2009-06-27 15:37:33 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-10-07 19:45:43 -05:00
Map * map = GetMap ();
2009-10-17 15:51:44 -07:00
2011-10-07 19:45:43 -05:00
if ( ! map -> IsDungeon ())
2009-06-27 15:37:33 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "entities.unit" , "Creature entry %u call SetInCombatWithZone for map (id: %u) that isn't an instance." , GetEntry (), map -> GetId ());
2009-06-27 15:37:33 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-10-07 19:45:43 -05:00
Map :: PlayerList const & PlList = map -> GetPlayers ();
2009-10-17 15:51:44 -07:00
2009-06-27 15:37:33 -05:00
if ( PlList . isEmpty ())
return ;
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for ( Map :: PlayerList :: const_iterator i = PlList . begin (); i != PlList . end (); ++ i )
2009-06-27 15:37:33 -05:00
{
2013-06-11 21:25:12 -02:30
if ( Player * player = i -> GetSource ())
2009-06-27 15:37:33 -05:00
{
2013-06-11 21:25:12 -02:30
if ( player -> IsGameMaster ())
2009-06-27 15:37:33 -05:00
continue ;
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
if ( player -> IsAlive ())
2009-06-27 15:37:33 -05:00
{
2011-10-07 11:08:09 -05:00
this -> SetInCombatWith ( player );
player -> SetInCombatWith ( this );
AddThreat ( player , 0.0f );
2009-06-27 15:37:33 -05:00
}
}
}
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
bool Creature :: HasSpell ( uint32 spellID ) const
{
uint8 i ;
2016-03-24 16:12:53 +01:00
for ( i = 0 ; i < MAX_CREATURE_SPELLS ; ++ i )
2010-04-07 19:14:10 +02:00
if ( spellID == m_spells [ i ])
2008-10-11 14:16:25 -05:00
break ;
2016-03-24 16:12:53 +01:00
return i < MAX_CREATURE_SPELLS ; //broke before end of iteration of known spells
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
time_t Creature :: GetRespawnTimeEx () const
{
2009-12-24 10:20:15 +01:00
time_t now = time ( NULL );
2010-09-15 06:22:54 +02:00
if ( m_respawnTime > now )
2008-10-11 14:16:25 -05:00
return m_respawnTime ;
else
return now ;
}
2009-10-17 15:51:44 -07:00
2011-10-18 10:53:34 -04:00
void Creature :: GetRespawnPosition ( float & x , float & y , float & z , float * ori , float * dist ) const
2008-10-11 14:16:25 -05:00
{
2015-02-25 00:13:14 +01:00
if ( m_spawnId )
2008-10-11 14:16:25 -05:00
{
2015-02-25 00:13:14 +01:00
if ( CreatureData const * data = sObjectMgr -> GetCreatureData ( GetSpawnId ()))
2008-10-11 14:16:25 -05:00
{
x = data -> posX ;
y = data -> posY ;
z = data -> posZ ;
2010-04-07 19:14:10 +02:00
if ( ori )
2008-10-11 14:16:25 -05:00
* ori = data -> orientation ;
2010-04-07 19:14:10 +02:00
if ( dist )
2008-10-11 14:16:25 -05:00
* dist = data -> spawndist ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
return ;
}
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
x = GetPositionX ();
y = GetPositionY ();
z = GetPositionZ ();
2010-04-07 19:14:10 +02:00
if ( ori )
2008-10-11 14:16:25 -05:00
* ori = GetOrientation ();
2010-04-07 19:14:10 +02:00
if ( dist )
2008-10-11 14:16:25 -05:00
* dist = 0 ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
void Creature :: AllLootRemovedFromCorpse ()
{
2014-06-26 22:01:55 +02:00
if ( loot . loot_type != LOOT_SKINNING && ! IsPet () && GetCreatureTemplate () -> SkinLootId && hasLootRecipient ())
if ( LootTemplates_Skinning . HaveLootFor ( GetCreatureTemplate () -> SkinLootId ))
SetFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_SKINNABLE );
2009-10-17 15:51:44 -07:00
2014-06-26 22:01:55 +02:00
time_t now = time ( NULL );
2014-07-12 17:27:26 +02:00
// Do not reset corpse remove time if corpse is already removed
2014-06-26 22:01:55 +02:00
if ( m_corpseRemoveTime <= now )
return ;
2009-10-17 15:51:44 -07:00
2014-06-26 22:01:55 +02:00
float decayRate = sWorld -> getRate ( RATE_CORPSE_DECAY_LOOTED );
2014-06-24 15:21:34 -05:00
2014-06-26 22:01:55 +02:00
// corpse skinnable, but without skinning flag, and then skinned, corpse will despawn next update
if ( loot . loot_type == LOOT_SKINNING )
2014-07-12 17:27:26 +02:00
m_corpseRemoveTime = now ;
2014-06-26 22:01:55 +02:00
else
2014-08-19 00:00:54 +02:00
m_corpseRemoveTime = now + uint32 ( m_corpseDelay * decayRate );
2014-06-26 22:01:55 +02:00
2014-06-28 13:19:00 -05:00
m_respawnTime = m_corpseRemoveTime + m_respawnDelay ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-12-14 22:45:23 +01:00
uint8 Creature :: getLevelForTarget ( WorldObject const * target ) const
2008-10-11 14:16:25 -05:00
{
2010-12-14 22:45:23 +01:00
if ( ! isWorldBoss () || ! target -> ToUnit ())
2008-10-11 14:16:25 -05:00
return Unit :: getLevelForTarget ( target );
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
uint16 level = target -> ToUnit () -> getLevel () + sWorld -> getIntConfig ( CONFIG_WORLD_BOSS_LEVEL_DIFF );
2010-04-07 19:14:10 +02:00
if ( level < 1 )
2008-10-11 14:16:25 -05:00
return 1 ;
2010-04-07 19:14:10 +02:00
if ( level > 255 )
2008-10-11 14:16:25 -05:00
return 255 ;
2010-08-26 01:20:57 +06:00
return uint8 ( level );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2009-04-14 18:31:31 -05:00
std :: string Creature :: GetAIName () const
{
2011-04-28 22:57:08 +02:00
return sObjectMgr -> GetCreatureTemplate ( GetEntry ()) -> AIName ;
2009-04-14 18:31:31 -05:00
}
2009-10-17 15:51:44 -07:00
2009-04-14 18:31:31 -05:00
std :: string Creature :: GetScriptName () const
2008-10-11 14:16:25 -05:00
{
2010-12-22 21:25:23 +01:00
return sObjectMgr -> GetScriptName ( GetScriptId ());
2008-11-20 16:16:57 -06:00
}
2009-10-17 15:51:44 -07:00
2009-04-14 18:31:31 -05:00
uint32 Creature :: GetScriptId () const
2008-11-20 16:16:57 -06:00
{
2017-03-18 11:31:31 +01:00
if ( CreatureData const * creatureData = GetCreatureData ())
return creatureData -> ScriptId ;
2011-04-28 22:57:08 +02:00
return sObjectMgr -> GetCreatureTemplate ( GetEntry ()) -> ScriptID ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
VendorItemData const * Creature :: GetVendorItems () const
{
2010-12-22 21:25:23 +01:00
return sObjectMgr -> GetNpcVendorItemList ( GetEntry ());
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint32 Creature :: GetVendorItemCurrentCount ( VendorItem const * vItem )
{
2010-04-07 19:14:10 +02:00
if ( ! vItem -> maxcount )
2008-10-11 14:16:25 -05:00
return vItem -> maxcount ;
2009-10-17 15:51:44 -07:00
2009-12-24 10:20:15 +01:00
VendorItemCounts :: iterator itr = m_vendorItemCounts . begin ();
for (; itr != m_vendorItemCounts . end (); ++ itr )
2010-04-07 23:25:02 +02:00
if ( itr -> itemId == vItem -> item )
2009-12-24 10:20:15 +01:00
break ;
2010-04-07 19:14:10 +02:00
if ( itr == m_vendorItemCounts . end ())
2008-10-11 14:16:25 -05:00
return vItem -> maxcount ;
2009-10-17 15:51:44 -07:00
2009-12-24 10:20:15 +01:00
VendorItemCount * vCount = &* itr ;
2009-10-17 15:51:44 -07:00
2009-12-24 10:20:15 +01:00
time_t ptime = time ( NULL );
2009-10-17 15:51:44 -07:00
2010-08-23 15:11:06 +02:00
if ( time_t ( vCount -> lastIncrementTime + vItem -> incrtime ) <= ptime )
2013-05-30 13:14:06 +02:00
if ( ItemTemplate const * pProto = sObjectMgr -> GetItemTemplate ( vItem -> item ))
2008-10-11 14:16:25 -05:00
{
2013-05-30 13:14:06 +02:00
uint32 diff = uint32 (( ptime - vCount -> lastIncrementTime ) / vItem -> incrtime );
2014-12-21 13:35:43 +01:00
if (( vCount -> count + diff * pProto -> GetBuyCount ()) >= vItem -> maxcount )
2013-05-30 13:14:06 +02:00
{
m_vendorItemCounts . erase ( itr );
return vItem -> maxcount ;
}
2009-10-17 15:51:44 -07:00
2014-12-21 13:35:43 +01:00
vCount -> count += diff * pProto -> GetBuyCount ();
2013-05-30 13:14:06 +02:00
vCount -> lastIncrementTime = ptime ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
return vCount -> count ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint32 Creature :: UpdateVendorItemCurrentCount ( VendorItem const * vItem , uint32 used_count )
{
2010-04-07 19:14:10 +02:00
if ( ! vItem -> maxcount )
2008-10-11 14:16:25 -05:00
return 0 ;
2009-10-17 15:51:44 -07:00
2009-12-24 10:20:15 +01:00
VendorItemCounts :: iterator itr = m_vendorItemCounts . begin ();
for (; itr != m_vendorItemCounts . end (); ++ itr )
2010-04-07 23:25:02 +02:00
if ( itr -> itemId == vItem -> item )
2009-12-24 10:20:15 +01:00
break ;
2010-04-07 19:14:10 +02:00
if ( itr == m_vendorItemCounts . end ())
2008-10-11 14:16:25 -05:00
{
2010-08-23 15:11:06 +02:00
uint32 new_count = vItem -> maxcount > used_count ? vItem -> maxcount - used_count : 0 ;
2011-04-29 20:47:02 +02:00
m_vendorItemCounts . push_back ( VendorItemCount ( vItem -> item , new_count ));
2008-10-11 14:16:25 -05:00
return new_count ;
}
2009-10-17 15:51:44 -07:00
2009-12-24 10:20:15 +01:00
VendorItemCount * vCount = &* itr ;
2009-10-17 15:51:44 -07:00
2009-12-24 10:20:15 +01:00
time_t ptime = time ( NULL );
2009-10-17 15:51:44 -07:00
2010-08-30 15:25:15 +02:00
if ( time_t ( vCount -> lastIncrementTime + vItem -> incrtime ) <= ptime )
2013-05-30 13:14:06 +02:00
if ( ItemTemplate const * pProto = sObjectMgr -> GetItemTemplate ( vItem -> item ))
{
uint32 diff = uint32 (( ptime - vCount -> lastIncrementTime ) / vItem -> incrtime );
2014-12-21 13:35:43 +01:00
if (( vCount -> count + diff * pProto -> GetBuyCount ()) < vItem -> maxcount )
vCount -> count += diff * pProto -> GetBuyCount ();
2013-05-30 13:14:06 +02:00
else
vCount -> count = vItem -> maxcount ;
}
2009-10-17 15:51:44 -07:00
2009-12-24 10:20:15 +01:00
vCount -> count = vCount -> count > used_count ? vCount -> count - used_count : 0 ;
2008-10-11 14:16:25 -05:00
vCount -> lastIncrementTime = ptime ;
return vCount -> count ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
TrainerSpellData const * Creature :: GetTrainerSpells () const
{
2010-12-22 21:25:23 +01:00
return sObjectMgr -> GetNpcTrainerSpells ( GetEntry ());
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-11-14 17:03:03 -06:00
// overwrite WorldObject function for proper name localization
2012-10-24 13:29:34 +02:00
std :: string const & Creature :: GetNameForLocaleIdx ( LocaleConstant loc_idx ) const
2008-11-14 17:03:03 -06:00
{
2010-09-17 07:04:29 +02:00
if ( loc_idx != DEFAULT_LOCALE )
2008-11-14 17:03:03 -06:00
{
2010-08-23 15:11:06 +02:00
uint8 uloc_idx = uint8 ( loc_idx );
2011-09-15 14:08:17 +02:00
CreatureLocale const * cl = sObjectMgr -> GetCreatureLocale ( GetEntry ());
2008-11-14 17:03:03 -06:00
if ( cl )
{
2010-08-23 15:11:06 +02:00
if ( cl -> Name . size () > uloc_idx && ! cl -> Name [ uloc_idx ]. empty ())
2012-10-24 13:29:34 +02:00
return cl -> Name [ uloc_idx ];
2008-11-14 17:03:03 -06:00
}
}
2009-10-17 15:51:44 -07:00
2008-11-14 17:03:03 -06:00
return GetName ();
}
2009-10-17 15:51:44 -07:00
2013-03-04 12:41:17 +01:00
uint32 Creature :: GetPetAutoSpellOnPos ( uint8 pos ) const
{
if ( pos >= MAX_SPELL_CHARM || m_charmInfo -> GetCharmSpell ( pos ) -> GetType () != ACT_ENABLED )
return 0 ;
else
return m_charmInfo -> GetCharmSpell ( pos ) -> GetAction ();
}
2011-10-10 17:08:01 -04:00
void Creature :: SetPosition ( float x , float y , float z , float o )
{
// prevent crash when a bad coord is sent by the client
if ( ! Trinity :: IsValidMapCoord ( x , y , z , o ))
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "entities.unit" , "Creature::SetPosition(%f, %f, %f) .. bad coordinates!" , x , y , z );
2011-10-10 17:08:01 -04:00
return ;
}
2014-01-04 18:22:06 +01:00
GetMap () -> CreatureRelocation ( this , x , y , z , o );
2011-10-10 17:08:01 -04:00
if ( IsVehicle ())
2012-09-10 15:44:12 +02:00
GetVehicleKit () -> RelocatePassengers ();
2010-10-24 02:55:50 +02:00
}
2011-02-09 16:28:56 +01:00
bool Creature :: IsDungeonBoss () const
{
2011-09-15 14:08:17 +02:00
CreatureTemplate const * cinfo = sObjectMgr -> GetCreatureTemplate ( GetEntry ());
2011-02-09 16:28:56 +01:00
return cinfo && ( cinfo -> flags_extra & CREATURE_FLAG_EXTRA_DUNGEON_BOSS );
}
2012-01-14 15:34:41 +01:00
2012-12-06 09:58:32 -05:00
float Creature :: GetAggroRange ( Unit const * target ) const
{
// Determines the aggro range for creatures (usually pets), used mainly for aggressive pet target selection.
// Based on data from wowwiki due to lack of 3.3.5a data
2013-06-11 18:39:51 -02:30
if ( target && this -> IsPet ())
2012-12-06 09:58:32 -05:00
{
uint32 targetLevel = 0 ;
2012-12-14 01:23:53 +00:00
2012-12-06 09:58:32 -05:00
if ( target -> GetTypeId () == TYPEID_PLAYER )
targetLevel = target -> getLevelForTarget ( this );
else if ( target -> GetTypeId () == TYPEID_UNIT )
targetLevel = target -> ToCreature () -> getLevelForTarget ( this );
uint32 myLevel = getLevelForTarget ( target );
int32 levelDiff = int32 ( targetLevel ) - int32 ( myLevel );
2012-12-14 01:23:53 +00:00
2012-12-06 09:58:32 -05:00
// The maximum Aggro Radius is capped at 45 yards (25 level difference)
if ( levelDiff < - 25 )
levelDiff = - 25 ;
// The base aggro radius for mob of same level
float aggroRadius = 20 ;
// Aggro Radius varies with level difference at a rate of roughly 1 yard/level
aggroRadius -= ( float ) levelDiff ;
// detect range auras
aggroRadius += GetTotalAuraModifier ( SPELL_AURA_MOD_DETECT_RANGE );
// detected range auras
aggroRadius += target -> GetTotalAuraModifier ( SPELL_AURA_MOD_DETECTED_RANGE );
// Just in case, we don't want pets running all over the map
if ( aggroRadius > MAX_AGGRO_RADIUS )
aggroRadius = MAX_AGGRO_RADIUS ;
// Minimum Aggro Radius for a mob seems to be combat range (5 yards)
// hunter pets seem to ignore minimum aggro radius so we'll default it a little higher
if ( aggroRadius < 10 )
aggroRadius = 10 ;
return ( aggroRadius );
}
// Default
return 0.0f ;
}
Unit * Creature :: SelectNearestHostileUnitInAggroRange ( bool useLOS ) const
{
// Selects nearest hostile target within creature's aggro range. Used primarily by
// pets set to aggressive. Will not return neutral or friendly targets.
Unit * target = NULL ;
{
Trinity :: NearestHostileUnitInAggroRangeCheck u_check ( this , useLOS );
Trinity :: UnitSearcher < Trinity :: NearestHostileUnitInAggroRangeCheck > searcher ( this , target , u_check );
VisitNearbyGridObject ( MAX_AGGRO_RADIUS , searcher );
}
return target ;
}
2013-06-07 16:05:39 +02:00
void Creature :: UpdateMovementFlags ()
{
2013-06-22 13:42:13 +02:00
// Do not update movement flags if creature is controlled by a player (charm/vehicle)
2016-08-20 12:43:04 +02:00
if ( m_playerMovingMe )
2013-06-22 13:42:13 +02:00
return ;
2013-06-07 16:05:39 +02:00
// Set the movement flags if the creature is in that mode. (Only fly if actually in air, only swim if in water, etc)
2015-07-28 19:54:49 +02:00
float ground = GetMap () -> GetHeight ( GetPhaseMask (), GetPositionX (), GetPositionY (), GetPositionZMinusOffset ());
2013-06-07 16:05:39 +02:00
2013-06-22 13:42:13 +02:00
bool isInAir = ( G3D :: fuzzyGt ( GetPositionZMinusOffset (), ground + 0.05f ) || G3D :: fuzzyLt ( GetPositionZMinusOffset (), ground - 0.05f )); // Can be underground too, prevent the falling
2013-06-07 16:05:39 +02:00
2013-06-22 13:42:13 +02:00
if ( GetCreatureTemplate () -> InhabitType & INHABIT_AIR && isInAir && ! IsFalling ())
2013-06-07 16:05:39 +02:00
{
if ( GetCreatureTemplate () -> InhabitType & INHABIT_GROUND )
SetCanFly ( true );
else
SetDisableGravity ( true );
}
else
{
SetCanFly ( false );
SetDisableGravity ( false );
}
2013-06-22 13:42:13 +02:00
if ( ! isInAir )
2013-08-10 13:44:08 +02:00
SetFall ( false );
2013-06-22 13:42:13 +02:00
2013-06-18 16:47:12 +02:00
SetSwim ( GetCreatureTemplate () -> InhabitType & INHABIT_WATER && IsInWater ());
2013-06-07 16:05:39 +02:00
}
2013-06-11 17:28:16 +02:00
void Creature :: SetObjectScale ( float scale )
{
Unit :: SetObjectScale ( scale );
if ( CreatureModelInfo const * minfo = sObjectMgr -> GetCreatureModelInfo ( GetDisplayId ()))
{
SetFloatValue ( UNIT_FIELD_BOUNDINGRADIUS , minfo -> bounding_radius * scale );
SetFloatValue ( UNIT_FIELD_COMBATREACH , minfo -> combat_reach * scale );
}
}
void Creature :: SetDisplayId ( uint32 modelId )
{
Unit :: SetDisplayId ( modelId );
if ( CreatureModelInfo const * minfo = sObjectMgr -> GetCreatureModelInfo ( modelId ))
{
2013-08-17 23:46:36 +02:00
SetFloatValue ( UNIT_FIELD_BOUNDINGRADIUS , minfo -> bounding_radius * GetObjectScale ());
SetFloatValue ( UNIT_FIELD_COMBATREACH , minfo -> combat_reach * GetObjectScale ());
2013-06-11 17:28:16 +02:00
}
}
2013-10-03 17:07:25 +02:00
2014-11-23 04:25:47 +01:00
void Creature :: SetTarget ( ObjectGuid const & guid )
2013-10-03 17:07:25 +02:00
{
2016-08-21 11:07:38 +02:00
if ( IsFocusing ( nullptr , true ))
m_suppressedTarget = guid ;
else
2014-09-14 16:14:12 +02:00
SetGuidValue ( UNIT_FIELD_TARGET , guid );
2013-10-03 17:07:25 +02:00
}
2016-08-21 11:07:38 +02:00
void Creature :: FocusTarget ( Spell const * focusSpell , WorldObject const * target )
2013-10-03 17:07:25 +02:00
{
// already focused
2016-04-02 02:50:44 +02:00
if ( m_focusSpell )
2016-08-21 11:07:38 +02:00
return ;
// don't use spell focus for vehicle spells
if ( focusSpell -> GetSpellInfo () -> HasAura ( DIFFICULTY_NONE , SPELL_AURA_CONTROL_VEHICLE ))
return ;
2016-01-13 18:38:54 +01:00
if (( ! target || target == this ) && ! focusSpell -> GetCastTime ()) // instant cast, untargeted (or self-targeted) spell doesn't need any facing updates
2016-08-21 11:07:38 +02:00
return ;
2013-10-03 17:07:25 +02:00
2016-08-21 11:07:38 +02:00
// store pre-cast values for target and orientation (used to later restore)
if ( ! IsFocusing ( nullptr , true ))
{ // only overwrite these fields if we aren't transitioning from one spell focus to another
m_suppressedTarget = GetGuidValue ( UNIT_FIELD_TARGET );
m_suppressedOrientation = GetOrientation ();
}
2013-10-03 17:07:25 +02:00
2016-08-21 11:07:38 +02:00
m_focusSpell = focusSpell ;
2016-01-13 18:38:54 +01:00
// set target, then force send update packet to players if it changed to provide appropriate facing
ObjectGuid newTarget = target ? target -> GetGUID () : ObjectGuid :: Empty ;
if ( GetGuidValue ( UNIT_FIELD_TARGET ) != newTarget )
{
SetGuidValue ( UNIT_FIELD_TARGET , newTarget );
2016-01-24 01:11:19 +00:00
2016-01-13 18:38:54 +01:00
if ( // here we determine if the (relatively expensive) forced update is worth it, or whether we can afford to wait until the scheduled update tick
( // only require instant update for spells that actually have a visual
2016-07-20 13:27:04 +02:00
focusSpell -> GetSpellInfo () -> GetSpellVisual ()
2016-01-24 01:11:19 +00:00
) && (
2016-01-13 18:38:54 +01:00
! focusSpell -> GetCastTime () || // if the spell is instant cast
focusSpell -> GetSpellInfo () -> HasAttribute ( SPELL_ATTR5_DONT_TURN_DURING_CAST ) // client gets confused if we attempt to turn at the regularly scheduled update packet
)
)
{
2016-03-22 23:23:03 +00:00
std :: list < Player *> playersNearby ;
GetPlayerListInGrid ( playersNearby , GetVisibilityRange ());
for ( Player * player : playersNearby )
{
// only update players that are known to the client (have already been created)
if ( player -> HaveAtClient ( this ))
SendUpdateToPlayer ( player );
}
2016-01-13 18:38:54 +01:00
}
}
bool canTurnDuringCast = ! focusSpell -> GetSpellInfo () -> HasAttribute ( SPELL_ATTR5_DONT_TURN_DURING_CAST );
// Face the target - we need to do this before the unit state is modified for no-turn spells
if ( target )
2016-09-20 20:19:15 +02:00
SetFacingToObject ( target );
2016-01-13 18:38:54 +01:00
else if ( ! canTurnDuringCast )
2016-03-16 21:38:01 +01:00
if ( Unit * victim = GetVictim ())
2016-09-20 20:19:15 +02:00
SetFacingToObject ( victim ); // ensure orientation is correct at beginning of cast
2016-01-13 18:38:54 +01:00
if ( ! canTurnDuringCast )
AddUnitState ( UNIT_STATE_CANNOT_TURN );
}
bool Creature :: IsFocusing ( Spell const * focusSpell , bool withDelay )
{
if ( ! IsAlive ()) // dead creatures cannot focus
{
ReleaseFocus ( nullptr , false );
return false ;
}
2016-04-02 02:50:44 +02:00
if ( focusSpell && ( focusSpell != m_focusSpell ))
2016-01-13 18:38:54 +01:00
return false ;
2016-04-02 02:50:44 +02:00
if ( ! m_focusSpell )
2016-01-13 18:38:54 +01:00
{
2016-04-02 02:50:44 +02:00
if ( ! withDelay || ! m_focusDelay )
2016-01-13 18:38:54 +01:00
return false ;
2016-04-02 02:50:44 +02:00
if ( GetMSTimeDiffToNow ( m_focusDelay ) > 1000 ) // @todo figure out if we can get rid of this magic number somehow
2016-01-13 18:38:54 +01:00
{
2016-04-02 02:50:44 +02:00
m_focusDelay = 0 ; // save checks in the future
2016-01-13 18:38:54 +01:00
return false ;
}
}
return true ;
2013-10-03 17:07:25 +02:00
}
2016-01-13 18:38:54 +01:00
void Creature :: ReleaseFocus ( Spell const * focusSpell , bool withDelay )
2013-10-03 17:07:25 +02:00
{
2016-04-02 02:50:44 +02:00
if ( ! m_focusSpell )
2016-01-13 18:38:54 +01:00
return ;
2013-10-03 17:07:25 +02:00
// focused to something else
2016-04-02 02:50:44 +02:00
if ( focusSpell && focusSpell != m_focusSpell )
2013-10-03 17:07:25 +02:00
return ;
2016-04-02 02:50:44 +02:00
if ( IsPet ()) // player pets do not use delay system
2016-08-21 11:07:38 +02:00
{
SetGuidValue ( UNIT_FIELD_TARGET , m_suppressedTarget );
if ( ! m_suppressedTarget . IsEmpty ())
{
if ( WorldObject const * objTarget = ObjectAccessor :: GetWorldObject ( * this , m_suppressedTarget ))
2016-09-20 20:19:15 +02:00
SetFacingToObject ( objTarget );
2016-08-21 11:07:38 +02:00
}
else
SetFacingTo ( m_suppressedOrientation );
}
2016-03-23 16:22:05 +01:00
else
2016-08-21 11:07:38 +02:00
// tell the creature that it should reacquire its actual target after the delay expires (this is handled in ::Update)
2016-04-02 02:50:44 +02:00
// player pets don't need to do this, as they automatically reacquire their target on focus release
MustReacquireTarget ();
2016-01-13 18:38:54 +01:00
2016-04-02 02:50:44 +02:00
if ( m_focusSpell -> GetSpellInfo () -> HasAttribute ( SPELL_ATTR5_DONT_TURN_DURING_CAST ))
2016-01-13 18:38:54 +01:00
ClearUnitState ( UNIT_STATE_CANNOT_TURN );
2013-10-03 17:07:25 +02:00
2016-04-02 02:50:44 +02:00
m_focusSpell = nullptr ;
m_focusDelay = ( ! IsPet () && withDelay ) ? getMSTime () : 0 ; // don't allow re-target right away to prevent visual bugs
2013-10-03 17:07:25 +02:00
}
2014-06-24 22:13:59 -05:00
void Creature :: StartPickPocketRefillTimer ()
{
_pickpocketLootRestore = time ( NULL ) + sWorld -> getIntConfig ( CONFIG_CREATURE_PICKPOCKET_REFILL );
}
2015-06-13 12:27:19 +02:00
void Creature :: SetTextRepeatId ( uint8 textGroup , uint8 id )
{
CreatureTextRepeatIds & repeats = m_textRepeat [ textGroup ];
if ( std :: find ( repeats . begin (), repeats . end (), id ) == repeats . end ())
repeats . push_back ( id );
else
2015-07-03 02:23:58 +01:00
TC_LOG_ERROR ( "sql.sql" , "CreatureTextMgr: TextGroup %u for Creature (%s) % s , id % u already added ", uint32(textGroup), GetName().c_str(), GetGUID().ToString().c_str(), uint32(id)) ;
2015-06-13 12:27:19 +02:00
}
CreatureTextRepeatIds Creature :: GetTextRepeatGroup ( uint8 textGroup )
{
CreatureTextRepeatIds ids ;
CreatureTextRepeatGroup :: const_iterator groupItr = m_textRepeat . find ( textGroup );
if ( groupItr != m_textRepeat . end ())
ids = groupItr -> second ;
return ids ;
}
void Creature :: ClearTextRepeatGroup ( uint8 textGroup )
{
CreatureTextRepeatGroup :: iterator groupItr = m_textRepeat . find ( textGroup );
if ( groupItr != m_textRepeat . end ())
groupItr -> second . clear ();
}