Core/Units: Fixed crash happening when charm was removed by its own charmed AI during update

This commit is contained in:
Shauren
2018-12-17 15:49:32 +01:00
parent 6658de05db
commit c7445669e8
2 changed files with 9 additions and 3 deletions

View File

@@ -9417,7 +9417,6 @@ void Unit::SetAI(UnitAI* newAI)
void Unit::ScheduleAIChange()
{
ASSERT(!m_aiLocked, "Attempt to schedule AI change during AI update tick");
bool const charmed = IsCharmed();
// if charm is applied, we can't have disabled AI already, and vice versa
if (charmed)
@@ -9427,15 +9426,20 @@ void Unit::ScheduleAIChange()
if (charmed)
i_disabledAI = std::move(i_AI);
else if (m_aiLocked)
{
ASSERT(!i_lockedAILifetimeExtension, "Attempt to schedule multiple charm AI changes during one update");
i_lockedAILifetimeExtension = std::move(i_AI); // AI needs to live just a bit longer to finish its UpdateAI
}
else
i_AI.reset();
}
void Unit::RestoreDisabledAI()
{
ASSERT(!m_aiLocked, "Attempt to restore AI during UpdateAI tick");
ASSERT((GetTypeId() == TYPEID_PLAYER) || i_disabledAI, "Attempt to restore disabled AI on creature without disabled AI");
i_AI = std::move(i_disabledAI);
i_lockedAILifetimeExtension.reset();
}
void Unit::AddToWorld()

View File

@@ -1791,7 +1791,9 @@ class TC_GAME_API Unit : public WorldObject
void UpdateCharmAI();
void RestoreDisabledAI();
std::unique_ptr<UnitAI> i_AI, i_disabledAI;
std::unique_ptr<UnitAI> i_AI;
std::unique_ptr<UnitAI> i_disabledAI;
std::unique_ptr<UnitAI> i_lockedAILifetimeExtension; // yes, this lifetime extension is terrible
bool m_aiLocked;
std::unordered_set<AbstractFollower*> m_followingMe;