Skip to content

Commit

Permalink
Remove db error again
Browse files Browse the repository at this point in the history
and simplify a bit
  • Loading branch information
Phatcat committed Sep 14, 2016
1 parent 7e23f3c commit 8dc19ac
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/game/AI/CreatureAISelector.cpp
Expand Up @@ -34,15 +34,11 @@ namespace FactorySelector
{
CreatureAI* selectAI(Creature* creature)
{
CreatureAI* scriptedAI = sScriptMgr.GetCreatureAI(creature);
if (scriptedAI)
if (CreatureAI* scriptedAI = sScriptMgr.GetCreatureAI(creature))
{
// charmed creature may have some script even if its not supposed to be that way (ex: Eye of Acherus)
if (creature->isCharmed())
return scriptedAI;

// Allow scripting AI for normal creatures and not controlled pets (guardians and mini-pets)
if (!creature->IsPet() || !static_cast<Pet*>(creature)->isControlled())
if (creature->isCharmed() || !(creature->IsPet() && static_cast<Pet*>(creature)->isControlled()))
return scriptedAI;
}

Expand All @@ -52,27 +48,21 @@ namespace FactorySelector

std::string ainame = creature->GetAIName();

// select by NPC flags _first_ - otherwise EventAI might be choosen for pets/totems
// select by NPC flags
if (creature->IsPet())
{
if (((Pet*)creature)->isControlled())
if (static_cast<Pet*>(creature)->isControlled())
ai_factory = ai_registry.GetRegistryItem("PetAI");
else
{
else // For guardians and creature pets in general
ai_factory = ai_registry.GetRegistryItem("GuardianAI");
if (!ainame.empty() && (ai_registry.GetRegistryItem(ainame.c_str()) != ai_registry.GetRegistryItem("GuardianAI")))
sLog.outErrorDb("FactorySelector: creature pet / guardian not up-to-date on entry: %u ! it shouldn't have %s - GuardianAI will be used.", creature->GetEntry(), ainame.c_str());
}
}
else if (creature->IsTotem())
ai_factory = ai_registry.GetRegistryItem("TotemAI");
// select by script name
else if (!ainame.empty())
else if (!ainame.empty()) // select by script name
ai_factory = ai_registry.GetRegistryItem(ainame.c_str());
else if (creature->IsGuard())
ai_factory = ai_registry.GetRegistryItem("GuardAI");
// select by permit check
else
else // select by permit check
{
int best_val = PERMIT_BASE_NO;
typedef CreatureAIRegistry::RegistryMapType RMT;
Expand Down

4 comments on commit 8dc19ac

@ssj17vegeta
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one :) Can't get enough of getting rid of those nasty C-style casts ^^

@Laizerox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssj17vegeta since C++11 theres excplicit type cast (Old C Style cast) which does interesting things. Only cast it doednt cover is dynamic_cast

@cyberium
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Laizerox dont try to make us thing that automatic casting method choosen by c++11 compiler will be better than the one we choose ourself in our specific case.
We know that it will be easier...
Its a good practice to do explicit type casting even now, so its recommended to do that for cmangos devs.
Anyway its ugly and we do too much of them mainly because we dont respect enough POO scheme.

@Laizerox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cyberium fair enough, I was just pointing such thing exist. However instead of doing casts everywhere we should probably move towards functions that return converted object?

It has been discussed alot but no one done it yet.
static_cast<Pet*>(object)->method();
object->toPet()->method();
object->convert<Pet*>()->method();

Pick your method :)

Please sign in to comment.