2008-10-11 14:16:25 -05:00
|
|
|
/*
|
2009-02-04 12:42:26 +01:00
|
|
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
2008-10-14 11:57:03 -05:00
|
|
|
*
|
2010-01-16 20:19:18 +03:00
|
|
|
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
|
2008-10-11 14:16:25 -05:00
|
|
|
*
|
|
|
|
|
* 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
|
2008-11-20 16:16:57 -06:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-10-11 14:16:25 -05:00
|
|
|
* 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, write to the Free Software
|
2008-11-20 16:16:57 -06:00
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2008-10-11 14:16:25 -05:00
|
|
|
*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
#include "Creature.h"
|
|
|
|
|
#include "CreatureAISelector.h"
|
2010-06-08 13:05:47 -06:00
|
|
|
#include "PassiveAI.h"
|
2010-06-25 00:18:01 +02:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
#include "MovementGenerator.h"
|
|
|
|
|
#include "Pet.h"
|
2009-03-19 14:41:30 -06:00
|
|
|
#include "TemporarySummon.h"
|
2009-05-10 16:07:49 -05:00
|
|
|
#include "CreatureAIFactory.h"
|
2010-01-19 11:36:05 +01:00
|
|
|
#include "ScriptMgr.h"
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
namespace FactorySelector
|
|
|
|
|
{
|
|
|
|
|
CreatureAI* selectAI(Creature *creature)
|
|
|
|
|
{
|
2009-04-11 15:59:27 -05:00
|
|
|
const CreatureAICreator *ai_factory = NULL;
|
2010-06-25 00:18:01 +02:00
|
|
|
CreatureAIRegistry& ai_registry(*CreatureAIRepository::instance());
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-04-07 19:14:10 +02:00
|
|
|
if (creature->isPet())
|
2009-04-11 15:59:27 -05:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("PetAI");
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2009-04-11 15:59:27 -05:00
|
|
|
//scriptname in db
|
2010-04-07 19:14:10 +02:00
|
|
|
if (!ai_factory)
|
|
|
|
|
if (CreatureAI* scriptedAI = sScriptMgr.GetAI(creature))
|
2008-10-11 14:16:25 -05:00
|
|
|
return scriptedAI;
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2009-04-14 18:31:31 -05:00
|
|
|
// AIname in db
|
|
|
|
|
std::string ainame=creature->GetAIName();
|
2010-04-07 19:14:10 +02:00
|
|
|
if (!ai_factory && !ainame.empty())
|
2010-04-07 22:59:46 +02:00
|
|
|
ai_factory = ai_registry.GetRegistryItem(ainame.c_str());
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
// select by NPC flags
|
2010-04-07 19:14:10 +02:00
|
|
|
if (!ai_factory)
|
2008-10-11 14:16:25 -05:00
|
|
|
{
|
2010-04-29 21:22:25 +02:00
|
|
|
if (creature->IsVehicle())
|
|
|
|
|
ai_factory = ai_registry.GetRegistryItem("VehicleAI");
|
|
|
|
|
else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN) && ((Guardian*)creature)->GetOwner()->GetTypeId() == TYPEID_PLAYER)
|
2009-06-05 22:43:02 -05:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("PetAI");
|
2010-04-29 21:22:25 +02:00
|
|
|
else if (creature->HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_SPELLCLICK))
|
2009-04-21 14:08:49 -05:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
|
2010-04-07 19:14:10 +02:00
|
|
|
else if (creature->isGuard())
|
2009-02-04 10:33:18 -06:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("GuardAI");
|
2010-04-07 19:14:10 +02:00
|
|
|
else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN))
|
2008-10-11 14:16:25 -05:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("PetAI");
|
2010-04-07 19:14:10 +02:00
|
|
|
else if (creature->isTotem())
|
2008-10-11 14:16:25 -05:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("TotemAI");
|
2010-04-07 19:14:10 +02:00
|
|
|
else if (creature->isTrigger())
|
2009-06-09 16:18:08 -05:00
|
|
|
{
|
2010-04-07 19:14:10 +02:00
|
|
|
if (creature->m_spells[0])
|
2009-06-09 16:18:08 -05:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("TriggerAI");
|
|
|
|
|
else
|
|
|
|
|
ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
|
|
|
|
|
}
|
2010-04-07 19:14:10 +02:00
|
|
|
else if (creature->GetCreatureType() == CREATURE_TYPE_CRITTER && !creature->HasUnitTypeMask(UNIT_MASK_GUARDIAN))
|
2009-01-28 20:16:11 -06:00
|
|
|
ai_factory = ai_registry.GetRegistryItem("CritterAI");
|
2008-10-11 14:16:25 -05:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
// select by permit check
|
2010-04-07 19:14:10 +02:00
|
|
|
if (!ai_factory)
|
2008-10-11 14:16:25 -05:00
|
|
|
{
|
|
|
|
|
int best_val = -1;
|
|
|
|
|
typedef CreatureAIRegistry::RegistryMapType RMT;
|
|
|
|
|
RMT const &l = ai_registry.GetRegisteredItems();
|
2009-10-17 16:20:24 -07:00
|
|
|
for (RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
|
2008-10-11 14:16:25 -05:00
|
|
|
{
|
|
|
|
|
const CreatureAICreator *factory = iter->second;
|
|
|
|
|
const SelectableAI *p = dynamic_cast<const SelectableAI *>(factory);
|
2010-07-30 20:04:28 +02:00
|
|
|
ASSERT(p != NULL);
|
2008-10-11 14:16:25 -05:00
|
|
|
int val = p->Permit(creature);
|
2010-04-07 22:59:46 +02:00
|
|
|
if (val > best_val)
|
2008-10-11 14:16:25 -05:00
|
|
|
{
|
|
|
|
|
best_val = val;
|
|
|
|
|
ai_factory = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
// select NullCreatureAI if not another cases
|
|
|
|
|
ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key();
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-04-07 22:59:46 +02:00
|
|
|
DEBUG_LOG("Creature %u used AI is %s.", creature->GetGUIDLow(), ainame.c_str());
|
|
|
|
|
return (ai_factory == NULL ? new NullCreatureAI(creature) : ai_factory->Create(creature));
|
2008-10-11 14:16:25 -05:00
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
MovementGenerator* selectMovementGenerator(Creature *creature)
|
|
|
|
|
{
|
2010-06-25 00:18:01 +02:00
|
|
|
MovementGeneratorRegistry& mv_registry(*MovementGeneratorRepository::instance());
|
2010-07-30 20:04:28 +02:00
|
|
|
ASSERT(creature->GetCreatureInfo() != NULL);
|
2010-04-07 22:59:46 +02:00
|
|
|
const MovementGeneratorCreator *mv_factory = mv_registry.GetRegistryItem(creature->GetDefaultMovementType());
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-04-07 22:59:46 +02:00
|
|
|
/* if (mv_factory == NULL)
|
2008-10-11 14:16:25 -05:00
|
|
|
{
|
|
|
|
|
int best_val = -1;
|
|
|
|
|
std::vector<std::string> l;
|
|
|
|
|
mv_registry.GetRegisteredItems(l);
|
2009-10-17 16:20:24 -07:00
|
|
|
for (std::vector<std::string>::iterator iter = l.begin(); iter != l.end(); ++iter)
|
2008-10-11 14:16:25 -05:00
|
|
|
{
|
|
|
|
|
const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
|
|
|
|
|
const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
|
2010-07-30 20:04:28 +02:00
|
|
|
ASSERT(p != NULL);
|
2008-10-11 14:16:25 -05:00
|
|
|
int val = p->Permit(creature);
|
2010-04-07 22:59:46 +02:00
|
|
|
if (val > best_val)
|
2008-10-11 14:16:25 -05:00
|
|
|
{
|
|
|
|
|
best_val = val;
|
|
|
|
|
mv_factory = p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-04-07 22:59:46 +02:00
|
|
|
return (mv_factory == NULL ? NULL : mv_factory->Create(creature));
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-11 14:16:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
2009-02-17 20:07:49 -06:00
|
|
|
|