mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Core/AI: Fix crashes caused by charmed Creatures having null AI for 1 map update tick (#27434)
Implement using ScheduledChangeAI instead of nullptr to signal a required AI change
This commit is contained in:
21
src/server/game/AI/CoreAI/ScheduledChangeAI.cpp
Normal file
21
src/server/game/AI/CoreAI/ScheduledChangeAI.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* 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 MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ScheduledChangeAI.h"
|
||||
|
||||
ScheduledChangeAI::ScheduledChangeAI(Creature* creature): CreatureAI(creature)
|
||||
{ }
|
||||
40
src/server/game/AI/CoreAI/ScheduledChangeAI.h
Normal file
40
src/server/game/AI/CoreAI/ScheduledChangeAI.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* 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 MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the 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, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TRINITY_SCHEDULEDCHANGEAI_H
|
||||
#define TRINITY_SCHEDULEDCHANGEAI_H
|
||||
|
||||
#include "CreatureAI.h"
|
||||
|
||||
class TC_GAME_API ScheduledChangeAI final : public CreatureAI
|
||||
{
|
||||
public:
|
||||
explicit ScheduledChangeAI(Creature* creature);
|
||||
|
||||
void MoveInLineOfSight(Unit*) override { }
|
||||
void AttackStart(Unit*) override { }
|
||||
void JustStartedThreateningMe(Unit*) override { }
|
||||
void JustEnteredCombat(Unit*) override { }
|
||||
void UpdateAI(uint32) override { }
|
||||
void JustAppeared() override { }
|
||||
void EnterEvadeMode(EvadeReason /*why*/) override { }
|
||||
void OnCharmed(bool /*isNew*/) override { }
|
||||
|
||||
static int Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; }
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "PassiveAI.h"
|
||||
#include "PetAI.h"
|
||||
#include "ReactorAI.h"
|
||||
#include "ScheduledChangeAI.h"
|
||||
#include "SmartAI.h"
|
||||
#include "TotemAI.h"
|
||||
|
||||
@@ -46,6 +47,7 @@ namespace AIRegistry
|
||||
(new CreatureAIFactory<TurretAI>("TurretAI"))->RegisterSelf();
|
||||
(new CreatureAIFactory<VehicleAI>("VehicleAI"))->RegisterSelf();
|
||||
(new CreatureAIFactory<SmartAI>("SmartAI"))->RegisterSelf();
|
||||
(new CreatureAIFactory<ScheduledChangeAI, false>("ScheduledChangeAI"))->RegisterSelf();
|
||||
|
||||
(new GameObjectAIFactory<NullGameObjectAI>("NullGameObjectAI"))->RegisterSelf();
|
||||
(new GameObjectAIFactory<GameObjectAI>("GameObjectAI"))->RegisterSelf();
|
||||
|
||||
@@ -60,6 +60,7 @@
|
||||
#include "PlayerAI.h"
|
||||
#include "QuestDef.h"
|
||||
#include "ReputationMgr.h"
|
||||
#include "ScheduledChangeAI.h"
|
||||
#include "SpellAuraEffects.h"
|
||||
#include "SpellAuras.h"
|
||||
#include "Spell.h"
|
||||
@@ -491,7 +492,7 @@ void Unit::Update(uint32 p_time)
|
||||
// All position info based actions have been executed, reset info
|
||||
_positionUpdateInfo.Reset();
|
||||
|
||||
if (!GetAI() && (GetTypeId() != TYPEID_PLAYER || (IsCharmed() && GetCharmerGUID().IsCreature())))
|
||||
if (HasScheduledAIChange() && (GetTypeId() != TYPEID_PLAYER || (IsCharmed() && GetCharmerGUID().IsCreature())))
|
||||
UpdateCharmAI();
|
||||
RefreshAI();
|
||||
}
|
||||
@@ -9524,11 +9525,11 @@ void Unit::ScheduleAIChange()
|
||||
bool const charmed = IsCharmed();
|
||||
|
||||
if (charmed)
|
||||
PushAI(nullptr);
|
||||
PushAI(GetScheduledChangeAI());
|
||||
else
|
||||
{
|
||||
RestoreDisabledAI();
|
||||
PushAI(nullptr); //This could actually be PopAI() to get the previous AI but it's required atm to trigger UpdateCharmAI()
|
||||
PushAI(GetScheduledChangeAI()); //This could actually be PopAI() to get the previous AI but it's required atm to trigger UpdateCharmAI()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9536,10 +9537,26 @@ void Unit::RestoreDisabledAI()
|
||||
{
|
||||
// Keep popping the stack until we either reach the bottom or find a valid AI
|
||||
while (PopAI())
|
||||
if (GetTopAI())
|
||||
if (GetTopAI() && dynamic_cast<ScheduledChangeAI*>(GetTopAI()) == nullptr)
|
||||
return;
|
||||
}
|
||||
|
||||
UnitAI* Unit::GetScheduledChangeAI()
|
||||
{
|
||||
if (Creature* creature = ToCreature())
|
||||
return new ScheduledChangeAI(creature);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool Unit::HasScheduledAIChange() const
|
||||
{
|
||||
if (UnitAI* ai = GetAI())
|
||||
return dynamic_cast<ScheduledChangeAI*>(ai) != nullptr;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
void Unit::AddToWorld()
|
||||
{
|
||||
if (IsInWorld())
|
||||
|
||||
@@ -804,6 +804,8 @@ class TC_GAME_API Unit : public WorldObject
|
||||
void SetAI(UnitAI* newAI);
|
||||
UnitAI* GetTopAI() const { return i_AIs.empty() ? nullptr : i_AIs.top().get(); }
|
||||
void RefreshAI();
|
||||
UnitAI* GetScheduledChangeAI();
|
||||
bool HasScheduledAIChange() const;
|
||||
public:
|
||||
|
||||
void AddToWorld() override;
|
||||
|
||||
Reference in New Issue
Block a user