Files
TCPlayerbotCore/src/server/game/AI/ScriptedAI/ScriptedCreature.cpp
T

710 lines
20 KiB
C++
Raw Normal View History

2010-01-19 16:37:49 +01:00
/* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
2008-10-14 11:57:03 -05:00
*
* Thanks to the original authors: ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
*
* This program is free software licensed under GPL version 2
* Please see the included DOCS/LICENSE.TXT for more information */
2009-10-17 15:51:44 -07:00
#include "ScriptPCH.h"
#include "Item.h"
#include "Spell.h"
#include "ObjectMgr.h"
2009-05-11 11:46:48 -05:00
#include "TemporarySummon.h"
2009-10-17 15:51:44 -07:00
// Spell summary for ScriptedAI::SelectSpell
2009-08-14 19:45:06 +02:00
struct TSpellSummary
{
uint8 Targets; // set of enum SelectTarget
uint8 Effects; // set of enum SelectEffect
} extern *SpellSummary;
2009-10-17 15:51:44 -07:00
2009-05-13 10:54:11 -05:00
void SummonList::DoZoneInCombat(uint32 entry)
{
2009-11-20 19:11:28 -08:00
for (iterator i = begin(); i != end();)
2009-05-13 10:54:11 -05:00
{
2010-04-14 23:07:41 +02:00
Creature *summon = Unit::GetCreature(*me, *i);
2009-05-13 10:54:11 -05:00
++i;
2010-04-07 19:14:10 +02:00
if (summon && summon->IsAIEnabled
2009-05-13 10:54:11 -05:00
&& (!entry || summon->GetEntry() == entry))
summon->AI()->DoZoneInCombat();
}
}
2009-10-17 15:51:44 -07:00
void SummonList::DoAction(uint32 entry, int32 info)
{
2009-11-20 19:11:28 -08:00
for (iterator i = begin(); i != end();)
{
2010-04-14 23:07:41 +02:00
Creature *summon = Unit::GetCreature(*me, *i);
++i;
2010-04-07 19:14:10 +02:00
if (summon && summon->IsAIEnabled
2009-05-13 10:54:11 -05:00
&& (!entry || summon->GetEntry() == entry))
summon->AI()->DoAction(info);
}
}
2009-10-17 15:51:44 -07:00
void SummonList::DespawnEntry(uint32 entry)
{
2009-11-20 19:11:28 -08:00
for (iterator i = begin(); i != end();)
{
2010-04-14 23:07:41 +02:00
Creature *summon = Unit::GetCreature(*me, *i);
2010-04-07 19:14:10 +02:00
if (!summon)
erase(i++);
2010-04-07 19:14:10 +02:00
else if (summon->GetEntry() == entry)
{
erase(i++);
summon->setDeathState(JUST_DIED);
summon->RemoveCorpse();
}
else
++i;
}
}
2009-10-17 15:51:44 -07:00
void SummonList::DespawnAll()
{
2010-04-07 19:13:19 +02:00
while (!empty())
{
2010-04-14 23:07:41 +02:00
Creature *summon = Unit::GetCreature(*me, *begin());
2010-04-07 19:14:10 +02:00
if (!summon)
erase(begin());
else
{
erase(begin());
2011-05-25 15:55:14 +02:00
if (TempSummon* summ = summon->ToTempSummon())
{
summon->DestroyForNearbyPlayers();
2011-05-25 15:55:14 +02:00
summ->UnSummon();
}
2009-05-11 11:46:48 -05:00
else
summon->DisappearAndDie();
}
}
}
2009-10-17 15:51:44 -07:00
void SummonList::RemoveNotExisting()
{
for (iterator i = begin(); i != end();)
{
if (Unit::GetCreature(*me, *i))
++i;
else
erase(i++);
}
}
bool SummonList::HasEntry(uint32 entry)
{
for (iterator i = begin(); i != end();)
{
Creature* summon = Unit::GetCreature(*me, *i);
if (!summon)
erase(i++);
else if (summon->GetEntry() == entry)
return true;
else
++i;
}
return false;
}
2009-08-16 21:46:31 +02:00
ScriptedAI::ScriptedAI(Creature* pCreature) : CreatureAI(pCreature),
2010-04-14 23:07:41 +02:00
me(pCreature),
2009-08-16 21:46:31 +02:00
IsFleeing(false),
2011-05-09 03:37:31 +02:00
_evadeCheckCooldown(2500),
_isCombatMovementAllowed(true)
2009-04-29 00:15:18 -05:00
{
_isHeroic = me->GetMap()->IsHeroic();
_difficulty = Difficulty(me->GetMap()->GetSpawnMode());
2009-04-29 00:15:18 -05:00
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::AttackStartNoMove(Unit* pWho)
{
2009-08-14 19:45:06 +02:00
if (!pWho)
return;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->Attack(pWho, false))
2009-08-14 19:45:06 +02:00
DoStartNoMovement(pWho);
}
2009-10-17 15:51:44 -07:00
void ScriptedAI::UpdateAI(uint32 const /*diff*/)
{
//Check if we have a current target
2009-08-14 19:45:06 +02:00
if (!UpdateVictim())
return;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->isAttackReady())
{
2009-08-14 19:45:06 +02:00
//If we are within range melee the target
2010-04-14 23:07:41 +02:00
if (me->IsWithinMeleeRange(me->getVictim()))
{
2010-04-14 23:07:41 +02:00
me->AttackerStateUpdate(me->getVictim());
me->resetAttackTimer();
}
}
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::DoStartMovement(Unit* pVictim, float fDistance, float fAngle)
{
2009-08-14 19:45:06 +02:00
if (pVictim)
2010-04-14 23:07:41 +02:00
me->GetMotionMaster()->MoveChase(pVictim, fDistance, fAngle);
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::DoStartNoMovement(Unit* pVictim)
{
2009-08-14 19:45:06 +02:00
if (!pVictim)
return;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
me->GetMotionMaster()->MoveIdle();
}
2009-10-17 15:51:44 -07:00
void ScriptedAI::DoStopAttack()
{
2010-04-14 23:07:41 +02:00
if (me->getVictim())
me->AttackStop();
}
2009-10-17 15:51:44 -07:00
void ScriptedAI::DoCastSpell(Unit* pTarget, SpellInfo const* pSpellInfo, bool bTriggered)
{
2010-04-14 23:07:41 +02:00
if (!pTarget || me->IsNonMeleeSpellCasted(false))
return;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
me->StopMoving();
me->CastSpell(pTarget, pSpellInfo, bTriggered ? TRIGGERED_FULL_MASK : TRIGGERED_NONE);
}
2009-10-17 15:51:44 -07:00
2009-08-06 17:36:32 +02:00
void ScriptedAI::DoPlaySoundToSet(WorldObject* pSource, uint32 uiSoundId)
{
2009-04-09 23:07:32 +02:00
if (!pSource)
return;
2009-10-17 15:51:44 -07:00
2009-04-09 23:07:32 +02:00
if (!GetSoundEntriesStore()->LookupEntry(uiSoundId))
{
sLog->outError("TSCR: Invalid soundId %u used in DoPlaySoundToSet (Source: TypeId %u, GUID %u)", uiSoundId, pSource->GetTypeId(), pSource->GetGUIDLow());
return;
}
2009-10-17 15:51:44 -07:00
2009-04-09 23:07:32 +02:00
pSource->PlayDirectSound(uiSoundId);
}
2009-10-17 15:51:44 -07:00
Creature* ScriptedAI::DoSpawnCreature(uint32 entry, float offsetX, float offsetY, float offsetZ, float angle, uint32 type, uint32 despawntime)
{
return me->SummonCreature(entry, me->GetPositionX() + offsetX, me->GetPositionY() + offsetY, me->GetPositionZ() + offsetZ, angle, TempSummonType(type), despawntime);
}
2009-10-17 15:51:44 -07:00
SpellInfo const* ScriptedAI::SelectSpell(Unit* target, uint32 school, uint32 mechanic, SelectTargetType targets, uint32 powerCostMin, uint32 powerCostMax, float rangeMin, float rangeMax, SelectEffect effects)
{
2009-10-27 21:39:53 -07:00
//No target so we can't cast
if (!target)
return false;
2009-10-17 15:51:44 -07:00
//Silenced so we can't cast
2010-04-14 23:07:41 +02:00
if (me->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SILENCED))
return false;
2009-10-17 15:51:44 -07:00
//Using the extended script system we first create a list of viable spells
SpellInfo const* apSpell[CREATURE_MAX_SPELLS];
memset(apSpell, 0, CREATURE_MAX_SPELLS * sizeof(SpellInfo*));
2009-10-17 15:51:44 -07:00
uint32 spellCount = 0;
2009-10-17 15:51:44 -07:00
SpellInfo const* tempSpell = NULL;
2009-10-17 15:51:44 -07:00
//Check if each spell is viable(set it to null if not)
for (uint32 i = 0; i < CREATURE_MAX_SPELLS; i++)
{
tempSpell = sSpellMgr->GetSpellInfo(me->m_spells[i]);
2009-10-17 15:51:44 -07:00
//This spell doesn't exist
if (!tempSpell)
continue;
2009-10-17 15:51:44 -07:00
// Targets and Effects checked first as most used restrictions
//Check the spell targets if specified
if (targets && !(SpellSummary[me->m_spells[i]].Targets & (1 << (targets-1))))
continue;
2009-10-17 15:51:44 -07:00
//Check the type of spell if we are looking for a specific spell type
if (effects && !(SpellSummary[me->m_spells[i]].Effects & (1 << (effects-1))))
continue;
2009-10-17 15:51:44 -07:00
//Check for school if specified
if (school && (tempSpell->SchoolMask & school) == 0)
continue;
2009-10-17 15:51:44 -07:00
//Check for spell mechanic if specified
if (mechanic && tempSpell->Mechanic != mechanic)
continue;
2009-10-17 15:51:44 -07:00
//Make sure that the spell uses the requested amount of power
if (powerCostMin && tempSpell->ManaCost < powerCostMin)
continue;
2009-10-17 15:51:44 -07:00
if (powerCostMax && tempSpell->ManaCost > powerCostMax)
continue;
2009-10-17 15:51:44 -07:00
//Continue if we don't have the mana to actually cast this spell
if (tempSpell->ManaCost > me->GetPower(Powers(tempSpell->PowerType)))
continue;
2009-10-17 15:51:44 -07:00
//Check if the spell meets our range requirements
if (rangeMin && me->GetSpellMinRangeForTarget(target, tempSpell) < rangeMin)
continue;
if (rangeMax && me->GetSpellMaxRangeForTarget(target, tempSpell) > rangeMax)
continue;
2009-10-17 15:51:44 -07:00
2009-10-27 21:39:53 -07:00
//Check if our target is in range
if (me->IsWithinDistInMap(target, float(me->GetSpellMinRangeForTarget(target, tempSpell))) || !me->IsWithinDistInMap(target, float(me->GetSpellMaxRangeForTarget(target, tempSpell))))
continue;
2009-10-17 15:51:44 -07:00
//All good so lets add it to the spell list
apSpell[spellCount] = tempSpell;
++spellCount;
}
2009-10-17 15:51:44 -07:00
//We got our usable spells so now lets randomly pick one
if (!spellCount)
return NULL;
2009-10-17 15:51:44 -07:00
return apSpell[urand(0, spellCount - 1)];
}
2009-10-17 15:51:44 -07:00
bool ScriptedAI::CanCast(Unit* target, SpellInfo const* spell, bool triggered /*= false*/)
{
2009-10-27 21:39:53 -07:00
//No target so we can't cast
if (!target || !spell)
return false;
2009-10-17 15:51:44 -07:00
//Silenced so we can't cast
if (!triggered && me->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SILENCED))
return false;
2009-10-17 15:51:44 -07:00
//Check for power
if (!triggered && me->GetPower(Powers(spell->PowerType)) < spell->ManaCost)
return false;
2009-10-17 15:51:44 -07:00
//Unit is out of range of this spell
if (me->IsInRange(target, float(me->GetSpellMinRangeForTarget(target, spell)), float(me->GetSpellMaxRangeForTarget(target, spell))))
return false;
2009-10-17 15:51:44 -07:00
return true;
}
2009-10-17 15:51:44 -07:00
void ScriptedAI::DoResetThreat()
{
2010-04-14 23:07:41 +02:00
if (!me->CanHaveThreatList() || me->getThreatManager().isThreatListEmpty())
{
sLog->outError("TSCR: DoResetThreat called for creature that either cannot have threat list or has empty threat list (me entry = %d)", me->GetEntry());
return;
}
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
std::list<HostileReference*>& threatlist = me->getThreatManager().getThreatList();
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
for (std::list<HostileReference*>::iterator itr = threatlist.begin(); itr != threatlist.end(); ++itr)
{
2010-04-14 23:07:41 +02:00
Unit* pUnit = Unit::GetUnit((*me), (*itr)->getUnitGuid());
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if (pUnit && DoGetThreat(pUnit))
DoModifyThreatPercent(pUnit, -100);
}
}
2009-10-17 15:51:44 -07:00
float ScriptedAI::DoGetThreat(Unit* pUnit)
{
2010-04-07 19:14:10 +02:00
if (!pUnit) return 0.0f;
2010-04-14 23:07:41 +02:00
return me->getThreatManager().getThreat(pUnit);
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::DoModifyThreatPercent(Unit* pUnit, int32 pct)
{
2010-04-07 19:14:10 +02:00
if (!pUnit) return;
2010-04-14 23:07:41 +02:00
me->getThreatManager().modifyThreatPercent(pUnit, pct);
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::DoTeleportTo(float fX, float fY, float fZ, uint32 uiTime)
2009-02-23 13:14:04 -06:00
{
2010-04-14 23:07:41 +02:00
me->Relocate(fX, fY, fZ);
me->SendMonsterMove(fX, fY, fZ, uiTime);
2009-02-23 13:14:04 -06:00
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::DoTeleportTo(const float fPos[4])
2009-05-13 10:54:11 -05:00
{
2009-08-14 19:45:06 +02:00
me->NearTeleportTo(fPos[0], fPos[1], fPos[2], fPos[3]);
2009-05-13 10:54:11 -05:00
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::DoTeleportPlayer(Unit* pUnit, float fX, float fY, float fZ, float fO)
{
2010-04-07 19:14:10 +02:00
if (!pUnit || pUnit->GetTypeId() != TYPEID_PLAYER)
{
2010-04-07 19:14:10 +02:00
if (pUnit)
sLog->outError("TSCR: Creature " UI64FMTD " (Entry: %u) Tried to teleport non-player unit (Type: %u GUID: " UI64FMTD ") to x: %f y:%f z: %f o: %f. Aborted.", me->GetGUID(), me->GetEntry(), pUnit->GetTypeId(), pUnit->GetGUID(), fX, fY, fZ, fO);
return;
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
CAST_PLR(pUnit)->TeleportTo(pUnit->GetMapId(), fX, fY, fZ, fO, TELE_TO_NOT_LEAVE_COMBAT);
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
void ScriptedAI::DoTeleportAll(float fX, float fY, float fZ, float fO)
{
2010-04-14 23:07:41 +02:00
Map *map = me->GetMap();
if (!map->IsDungeon())
return;
2009-10-17 15:51:44 -07:00
Map::PlayerList const &PlayerList = map->GetPlayers();
2009-10-17 16:20:24 -07:00
for (Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
if (Player* i_pl = i->getSource())
if (i_pl->isAlive())
2010-04-14 23:07:41 +02:00
i_pl->TeleportTo(me->GetMapId(), fX, fY, fZ, fO, TELE_TO_NOT_LEAVE_COMBAT);
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
Unit* ScriptedAI::DoSelectLowestHpFriendly(float fRange, uint32 uiMinHPDiff)
{
Unit* pUnit = NULL;
2010-04-14 23:07:41 +02:00
Trinity::MostHPMissingInRange u_check(me, fRange, uiMinHPDiff);
Trinity::UnitLastSearcher<Trinity::MostHPMissingInRange> searcher(me, pUnit, u_check);
me->VisitNearbyObject(fRange, searcher);
2009-10-17 15:51:44 -07:00
return pUnit;
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
std::list<Creature*> ScriptedAI::DoFindFriendlyCC(float fRange)
{
std::list<Creature*> pList;
2010-04-14 23:07:41 +02:00
Trinity::FriendlyCCedInRange u_check(me, fRange);
Trinity::CreatureListSearcher<Trinity::FriendlyCCedInRange> searcher(me, pList, u_check);
me->VisitNearbyObject(fRange, searcher);
return pList;
}
2009-10-17 15:51:44 -07:00
2009-08-14 19:45:06 +02:00
std::list<Creature*> ScriptedAI::DoFindFriendlyMissingBuff(float fRange, uint32 uiSpellid)
{
std::list<Creature*> pList;
2010-04-14 23:07:41 +02:00
Trinity::FriendlyMissingBuffInRange u_check(me, fRange, uiSpellid);
Trinity::CreatureListSearcher<Trinity::FriendlyMissingBuffInRange> searcher(me, pList, u_check);
me->VisitNearbyObject(fRange, searcher);
return pList;
}
2009-10-17 15:51:44 -07:00
2009-07-24 05:27:49 +02:00
Player* ScriptedAI::GetPlayerAtMinimumRange(float fMinimumRange)
{
Player* pPlayer = NULL;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
CellPair pair(Trinity::ComputeCellPair(me->GetPositionX(), me->GetPositionY()));
2009-07-24 05:27:49 +02:00
Cell cell(pair);
cell.data.Part.reserved = ALL_DISTRICT;
cell.SetNoCreate();
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
Trinity::PlayerAtMinimumRangeAway check(me, fMinimumRange);
Trinity::PlayerSearcher<Trinity::PlayerAtMinimumRangeAway> searcher(me, pPlayer, check);
2009-07-24 05:27:49 +02:00
TypeContainerVisitor<Trinity::PlayerSearcher<Trinity::PlayerAtMinimumRangeAway>, GridTypeMapContainer> visitor(searcher);
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
cell.Visit(pair, visitor, *(me->GetMap()));
2009-10-17 15:51:44 -07:00
2009-07-24 05:27:49 +02:00
return pPlayer;
}
2009-10-17 15:51:44 -07:00
void ScriptedAI::SetEquipmentSlots(bool loadDefault, int32 mainHand /*= EQUIP_NO_CHANGE*/, int32 offHand /*= EQUIP_NO_CHANGE*/, int32 ranged /*= EQUIP_NO_CHANGE*/)
2009-04-19 21:16:11 +02:00
{
if (loadDefault)
2009-04-19 21:16:11 +02:00
{
2011-04-28 22:57:08 +02:00
if (CreatureTemplate const* creatureInfo = sObjectMgr->GetCreatureTemplate(me->GetEntry()))
me->LoadEquipment(creatureInfo->equipmentId, true);
2009-10-17 15:51:44 -07:00
2009-04-19 21:16:11 +02:00
return;
}
2009-10-17 15:51:44 -07:00
if (mainHand >= 0)
me->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + 0, uint32(mainHand));
2009-10-17 15:51:44 -07:00
if (offHand >= 0)
me->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + 1, uint32(offHand));
2009-10-17 15:51:44 -07:00
if (ranged >= 0)
me->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_ID + 2, uint32(ranged));
2009-04-19 21:16:11 +02:00
}
2009-10-17 15:51:44 -07:00
void ScriptedAI::SetCombatMovement(bool allowMovement)
2009-05-19 21:03:43 +02:00
{
_isCombatMovementAllowed = allowMovement;
2009-05-19 21:03:43 +02:00
}
2009-10-17 15:51:44 -07:00
2009-10-17 07:42:48 -07:00
enum eNPCs
2009-08-14 19:45:06 +02:00
{
NPC_BROODLORD = 12017,
NPC_VOID_REAVER = 19516,
NPC_JAN_ALAI = 23578,
NPC_SARTHARION = 28860
};
2009-10-17 15:51:44 -07:00
2009-08-08 21:29:12 +02:00
// Hacklike storage used for misc creatures that are expected to evade of outside of a certain area.
2010-02-05 00:31:28 +01:00
// It is assumed the information is found elswehere and can be handled by the core. So far no luck finding such information/way to extract it.
bool ScriptedAI::EnterEvadeIfOutOfCombatArea(uint32 const diff)
2009-08-08 21:29:12 +02:00
{
if (_evadeCheckCooldown <= diff)
_evadeCheckCooldown = 2500;
2009-08-08 21:29:12 +02:00
else
{
_evadeCheckCooldown -= diff;
2009-08-08 21:29:12 +02:00
return false;
}
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
if (me->IsInEvadeMode() || !me->getVictim())
2009-08-08 21:29:12 +02:00
return false;
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
float fX = me->GetPositionX();
float fY = me->GetPositionY();
float fZ = me->GetPositionZ();
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
switch(me->GetEntry())
2009-08-08 21:29:12 +02:00
{
2009-08-14 19:45:06 +02:00
case NPC_BROODLORD: // broodlord (not move down stairs)
2009-08-08 21:29:12 +02:00
if (fZ > 448.60f)
return false;
break;
2009-08-14 19:45:06 +02:00
case NPC_VOID_REAVER: // void reaver (calculate from center of room)
2010-04-14 23:07:41 +02:00
if (me->GetDistance2d(432.59f, 371.93f) < 105.0f)
2009-08-08 21:29:12 +02:00
return false;
break;
2009-08-14 19:45:06 +02:00
case NPC_JAN_ALAI: // jan'alai (calculate by Z)
2009-08-08 21:29:12 +02:00
if (fZ > 12.0f)
return false;
break;
2009-08-14 19:45:06 +02:00
case NPC_SARTHARION: // sartharion (calculate box)
2009-08-12 23:34:19 +02:00
if (fX > 3218.86f && fX < 3275.69f && fY < 572.40f && fY > 484.68f)
2009-08-08 21:29:12 +02:00
return false;
break;
default:
sLog->outError("TSCR: EnterEvadeIfOutOfCombatArea used for creature entry %u, but does not have any definition.", me->GetEntry());
2009-08-08 21:29:12 +02:00
return false;
}
2009-10-17 15:51:44 -07:00
2009-08-08 21:29:12 +02:00
EnterEvadeMode();
return true;
}
2009-10-17 15:51:44 -07:00
void Scripted_NoMovementAI::AttackStart(Unit* target)
{
if (!target)
return;
2009-10-17 15:51:44 -07:00
if (me->Attack(target, true))
DoStartNoMovement(target);
}
2009-10-17 15:51:44 -07:00
// BossAI - for instanced bosses
2011-05-09 03:37:31 +02:00
BossAI::BossAI(Creature* creature, uint32 bossId) : ScriptedAI(creature),
instance(creature->GetInstanceScript()),
summons(creature),
_boundary(instance ? instance->GetBossBoundary(bossId) : NULL),
_bossId(bossId)
{
}
2009-10-17 15:51:44 -07:00
2009-05-10 22:48:22 -05:00
void BossAI::_Reset()
{
2010-04-07 19:14:10 +02:00
if (!me->isAlive())
return;
2009-10-17 15:51:44 -07:00
me->ResetLootMode();
2009-05-10 22:48:22 -05:00
events.Reset();
summons.DespawnAll();
2010-04-07 19:14:10 +02:00
if (instance)
instance->SetBossState(_bossId, NOT_STARTED);
2009-05-10 22:48:22 -05:00
}
2009-10-17 15:51:44 -07:00
2009-05-10 22:48:22 -05:00
void BossAI::_JustDied()
{
events.Reset();
summons.DespawnAll();
2010-04-07 19:14:10 +02:00
if (instance)
{
instance->SetBossState(_bossId, DONE);
instance->SaveToDB();
}
2009-05-10 22:48:22 -05:00
}
2009-10-17 15:51:44 -07:00
2009-05-10 22:48:22 -05:00
void BossAI::_EnterCombat()
{
me->setActive(true);
2009-05-10 22:48:22 -05:00
DoZoneInCombat();
2010-04-07 19:14:10 +02:00
if (instance)
{
// bosses do not respawn, check only on enter combat
if (!instance->CheckRequiredBosses(_bossId))
{
EnterEvadeMode();
return;
}
instance->SetBossState(_bossId, IN_PROGRESS);
}
2009-05-10 22:48:22 -05:00
}
2009-10-17 15:51:44 -07:00
void BossAI::TeleportCheaters()
{
float x, y, z;
me->GetPosition(x, y, z);
std::list<HostileReference*>& threatList = me->getThreatManager().getThreatList();
for (std::list<HostileReference*>::iterator itr = threatList.begin(); itr != threatList.end(); ++itr)
if (Unit* target = (*itr)->getTarget())
if (target->GetTypeId() == TYPEID_PLAYER && !CheckBoundary(target))
target->NearTeleportTo(x, y, z, 0);
}
2009-10-17 15:51:44 -07:00
bool BossAI::CheckBoundary(Unit* who)
{
if (!GetBoundary() || !who)
return true;
2009-10-17 15:51:44 -07:00
for (BossBoundaryMap::const_iterator itr = GetBoundary()->begin(); itr != GetBoundary()->end(); ++itr)
{
switch (itr->first)
{
case BOUNDARY_N:
if (me->GetPositionX() > itr->second)
return false;
break;
case BOUNDARY_S:
if (me->GetPositionX() < itr->second)
return false;
break;
case BOUNDARY_E:
if (me->GetPositionY() < itr->second)
return false;
break;
case BOUNDARY_W:
if (me->GetPositionY() > itr->second)
return false;
break;
case BOUNDARY_NW:
if (me->GetPositionX() + me->GetPositionY() > itr->second)
return false;
break;
case BOUNDARY_SE:
if (me->GetPositionX() + me->GetPositionY() < itr->second)
return false;
break;
case BOUNDARY_NE:
if (me->GetPositionX() - me->GetPositionY() > itr->second)
return false;
break;
case BOUNDARY_SW:
if (me->GetPositionX() - me->GetPositionY() < itr->second)
return false;
break;
default:
break;
}
}
2009-10-17 15:51:44 -07:00
return true;
}
2009-10-17 15:51:44 -07:00
void BossAI::JustSummoned(Creature* summon)
2009-05-10 22:48:22 -05:00
{
summons.Summon(summon);
2010-04-07 19:14:10 +02:00
if (me->isInCombat())
2009-05-13 10:54:11 -05:00
DoZoneInCombat(summon);
2009-05-10 22:48:22 -05:00
}
2009-10-17 15:51:44 -07:00
void BossAI::SummonedCreatureDespawn(Creature* summon)
2009-05-10 22:48:22 -05:00
{
summons.Despawn(summon);
}
2009-10-17 15:51:44 -07:00
void BossAI::UpdateAI(uint32 const diff)
{
if (!UpdateVictim())
return;
events.Update(diff);
if (me->HasUnitState(UNIT_STAT_CASTING))
return;
while (uint32 eventId = events.ExecuteEvent())
ExecuteEvent(eventId);
DoMeleeAttackIfReady();
}
// WorldBossAI - for non-instanced bosses
WorldBossAI::WorldBossAI(Creature* creature) :
ScriptedAI(creature),
summons(creature)
{
}
void WorldBossAI::_Reset()
{
if (!me->isAlive())
return;
events.Reset();
summons.DespawnAll();
}
void WorldBossAI::_JustDied()
{
events.Reset();
summons.DespawnAll();
}
void WorldBossAI::_EnterCombat()
{
2011-05-28 02:14:47 +02:00
Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 0.0f, true);
if (target)
AttackStart(target);
}
void WorldBossAI::JustSummoned(Creature* summon)
{
summons.Summon(summon);
2011-05-28 02:14:47 +02:00
Unit* target = SelectTarget(SELECT_TARGET_RANDOM, 0, 0.0f, true);
2011-06-12 02:30:32 +02:00
if (target)
summon->AI()->AttackStart(target);
}
void WorldBossAI::SummonedCreatureDespawn(Creature* summon)
{
summons.Despawn(summon);
}
void WorldBossAI::UpdateAI(uint32 const diff)
{
if (!UpdateVictim())
return;
events.Update(diff);
if (me->HasUnitState(UNIT_STAT_CASTING))
return;
while (uint32 eventId = events.ExecuteEvent())
ExecuteEvent(eventId);
DoMeleeAttackIfReady();
}
// SD2 grid searchers.
Creature* GetClosestCreatureWithEntry(WorldObject* source, uint32 entry, float maxSearchRange, bool alive /*= true*/)
2009-11-21 05:01:03 -08:00
{
return source->FindNearestCreature(entry, maxSearchRange, alive);
2009-11-21 05:01:03 -08:00
}
GameObject* GetClosestGameObjectWithEntry(WorldObject* source, uint32 entry, float maxSearchRange)
2009-07-12 01:17:04 +02:00
{
return source->FindNearestGameObject(entry, maxSearchRange);
2009-07-12 01:17:04 +02:00
}
void GetCreatureListWithEntryInGrid(std::list<Creature*>& list, WorldObject* source, uint32 entry, float maxSearchRange)
2009-11-21 05:01:03 -08:00
{
source->GetCreatureListWithEntryInGrid(list, entry, maxSearchRange);
2009-11-21 05:01:03 -08:00
}
void GetGameObjectListWithEntryInGrid(std::list<GameObject*>& list, WorldObject* source, uint32 entry, float maxSearchRange)
2009-11-21 05:01:03 -08:00
{
source->GetGameObjectListWithEntryInGrid(list, entry, maxSearchRange);
2009-11-21 05:01:03 -08:00
}