mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-02-02 10:33:46 +00:00
refactor(Core/AI): factory functions cleanup (#11779)
This commit is contained in:
committed by
GitHub
parent
02fa20b638
commit
cc52712ac1
@@ -15,132 +15,102 @@
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "CreatureAISelector.h"
|
||||
#include "Creature.h"
|
||||
#include "CreatureAISelector.h"
|
||||
#include "CreatureAIFactory.h"
|
||||
|
||||
#include "MovementGenerator.h"
|
||||
#include "PassiveAI.h"
|
||||
#include "Pet.h"
|
||||
|
||||
#include "GameObject.h"
|
||||
#include "GameObjectAIFactory.h"
|
||||
|
||||
#include "ScriptMgr.h"
|
||||
#include "TemporarySummon.h"
|
||||
|
||||
namespace FactorySelector
|
||||
{
|
||||
CreatureAI* selectAI(Creature* creature)
|
||||
template <class T>
|
||||
struct PermissibleOrderPred
|
||||
{
|
||||
const CreatureAICreator* ai_factory = nullptr;
|
||||
CreatureAIRegistry& ai_registry(*CreatureAIRegistry::instance());
|
||||
public:
|
||||
PermissibleOrderPred(T const* obj) : _obj(obj) { }
|
||||
|
||||
// xinef: if we have controlable guardian, define petai for players as they can steer him, otherwise db / normal ai
|
||||
// xinef: dont remember why i changed this qq commented out as may break some quests
|
||||
if (creature->IsPet()/* || (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN) && ((Guardian*)creature)->GetOwner()->GetTypeId() == TYPEID_PLAYER)*/)
|
||||
ai_factory = ai_registry.GetRegistryItem("PetAI");
|
||||
template <class Value>
|
||||
bool operator()(Value const& left, Value const& right) const
|
||||
{
|
||||
Permissible<T> const* leftPermit = ASSERT_NOTNULL(dynamic_cast<Permissible<T> const*>(left.second.get()));
|
||||
Permissible<T> const* rightPermit = ASSERT_NOTNULL(dynamic_cast<Permissible<T> const*>(right.second.get()));
|
||||
|
||||
//scriptname in db
|
||||
return leftPermit->Permit(_obj) < rightPermit->Permit(_obj);
|
||||
}
|
||||
|
||||
private:
|
||||
T const* const _obj;
|
||||
};
|
||||
|
||||
CreatureAI* SelectAI(Creature* creature)
|
||||
{
|
||||
CreatureAICreator const* ai_factory = nullptr;
|
||||
|
||||
// scriptname in db
|
||||
if (!ai_factory)
|
||||
if (CreatureAI* scriptedAI = sScriptMgr->GetCreatureAI(creature))
|
||||
return scriptedAI;
|
||||
|
||||
// AIname in db
|
||||
std::string ainame = creature->GetAIName();
|
||||
if (!ai_factory && !ainame.empty())
|
||||
ai_factory = ai_registry.GetRegistryItem(ainame);
|
||||
|
||||
// select by NPC flags
|
||||
if (!ai_factory)
|
||||
{
|
||||
if (creature->IsVehicle())
|
||||
ai_factory = ai_registry.GetRegistryItem("VehicleAI");
|
||||
else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN) && ((Guardian*)creature)->GetOwner()->GetTypeId() == TYPEID_PLAYER)
|
||||
ai_factory = ai_registry.GetRegistryItem("PetAI");
|
||||
else if (creature->HasNpcFlag(UNIT_NPC_FLAG_SPELLCLICK))
|
||||
ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
|
||||
else if (creature->IsGuard())
|
||||
ai_factory = ai_registry.GetRegistryItem("GuardAI");
|
||||
else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN))
|
||||
ai_factory = ai_registry.GetRegistryItem("PetAI");
|
||||
else if (creature->IsTotem())
|
||||
ai_factory = ai_registry.GetRegistryItem("TotemAI");
|
||||
else if (creature->IsTrigger())
|
||||
{
|
||||
if (creature->m_spells[0])
|
||||
ai_factory = ai_registry.GetRegistryItem("TriggerAI");
|
||||
else
|
||||
ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
|
||||
}
|
||||
else if (creature->IsCritter() && !creature->HasUnitTypeMask(UNIT_MASK_GUARDIAN))
|
||||
ai_factory = ai_registry.GetRegistryItem("CritterAI");
|
||||
}
|
||||
std::string const& aiName = creature->GetAIName();
|
||||
if (!ai_factory && !aiName.empty())
|
||||
ai_factory = sCreatureAIRegistry->GetRegistryItem(aiName);
|
||||
|
||||
// select by permit check
|
||||
if (!ai_factory)
|
||||
{
|
||||
int best_val = -1;
|
||||
typedef CreatureAIRegistry::RegistryMapType RMT;
|
||||
RMT const& l = ai_registry.GetRegisteredItems();
|
||||
for (RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
|
||||
{
|
||||
const CreatureAICreator* factory = iter->second;
|
||||
const SelectableAI* p = dynamic_cast<const SelectableAI*>(factory);
|
||||
ASSERT(p);
|
||||
int val = p->Permit(creature);
|
||||
if (val > best_val)
|
||||
{
|
||||
best_val = val;
|
||||
ai_factory = p;
|
||||
}
|
||||
}
|
||||
CreatureAIRegistry::RegistryMapType const& items = sCreatureAIRegistry->GetRegisteredItems();
|
||||
auto itr = std::max_element(items.begin(), items.end(), PermissibleOrderPred<Creature>(creature));
|
||||
if (itr != items.end())
|
||||
ai_factory = itr->second.get();
|
||||
}
|
||||
|
||||
// select NullCreatureAI if not another cases
|
||||
ainame = (!ai_factory) ? "NullCreatureAI" : ai_factory->key();
|
||||
LOG_DEBUG("scripts.ai", "Creature {} used AI is {}.", creature->GetGUID().ToString(), ainame);
|
||||
return (!ai_factory ? new NullCreatureAI(creature) : ai_factory->Create(creature));
|
||||
if (!ai_factory)
|
||||
ai_factory = sCreatureAIRegistry->GetRegistryItem("NullCreatureAI");
|
||||
|
||||
return ASSERT_NOTNULL(ai_factory)->Create(creature);
|
||||
}
|
||||
|
||||
MovementGenerator* selectMovementGenerator(Creature* creature)
|
||||
MovementGenerator* SelectMovementGenerator(Unit* unit)
|
||||
{
|
||||
MovementGeneratorRegistry& mv_registry(*MovementGeneratorRegistry::instance());
|
||||
ASSERT(creature->GetCreatureTemplate());
|
||||
const MovementGeneratorCreator* mv_factory = mv_registry.GetRegistryItem(creature->GetDefaultMovementType());
|
||||
MovementGeneratorType type = IDLE_MOTION_TYPE;
|
||||
if (unit->GetTypeId() == TYPEID_UNIT)
|
||||
type = unit->ToCreature()->GetDefaultMovementType();
|
||||
|
||||
/* if (mv_factory == nullptr)
|
||||
{
|
||||
int best_val = -1;
|
||||
std::vector<std::string> l;
|
||||
mv_registry.GetRegisteredItems(l);
|
||||
for (std::vector<std::string>::iterator iter = l.begin(); iter != l.end(); ++iter)
|
||||
{
|
||||
const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
|
||||
const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
|
||||
ASSERT(p != nullptr);
|
||||
int val = p->Permit(creature);
|
||||
if (val > best_val)
|
||||
{
|
||||
best_val = val;
|
||||
mv_factory = p;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return (!mv_factory ? nullptr : mv_factory->Create(creature));
|
||||
MovementGeneratorCreator const* mv_factory = sMovementGeneratorRegistry->GetRegistryItem(type);
|
||||
return ASSERT_NOTNULL(mv_factory)->Create(unit);
|
||||
}
|
||||
|
||||
GameObjectAI* SelectGameObjectAI(GameObject* go)
|
||||
{
|
||||
const GameObjectAICreator* ai_factory = nullptr;
|
||||
GameObjectAIRegistry& ai_registry(*GameObjectAIRegistry::instance());
|
||||
GameObjectAICreator const* ai_factory = nullptr;
|
||||
|
||||
// scriptname in db
|
||||
if (GameObjectAI* scriptedAI = sScriptMgr->GetGameObjectAI(go))
|
||||
return scriptedAI;
|
||||
|
||||
ai_factory = ai_registry.GetRegistryItem(go->GetAIName());
|
||||
// AIname in db
|
||||
std::string const& aiName = go->GetAIName();
|
||||
if (!ai_factory && !aiName.empty())
|
||||
ai_factory = sGameObjectAIRegistry->GetRegistryItem(aiName);
|
||||
|
||||
//future goAI types go here
|
||||
// select by permit check
|
||||
if (!ai_factory)
|
||||
{
|
||||
GameObjectAIRegistry::RegistryMapType const& items = sGameObjectAIRegistry->GetRegisteredItems();
|
||||
auto itr = std::max_element(items.begin(), items.end(), PermissibleOrderPred<GameObject>(go));
|
||||
if (itr != items.end())
|
||||
ai_factory = itr->second.get();
|
||||
}
|
||||
|
||||
std::string ainame = (!ai_factory || go->GetScriptId()) ? "NullGameObjectAI" : ai_factory->key();
|
||||
LOG_DEBUG("scripts.ai", "GameObject {} used AI is {}.", go->GetGUID().ToString(), ainame);
|
||||
if (!ai_factory)
|
||||
ai_factory = sGameObjectAIRegistry->GetRegistryItem("NullGameObjectAI");
|
||||
|
||||
return (!ai_factory ? new NullGameObjectAI(go) : ai_factory->Create(go));
|
||||
return ASSERT_NOTNULL(ai_factory)->Create(go);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user