From f2713834c7ee1b188471561736c09aa18ab6e8be Mon Sep 17 00:00:00 2001 From: Ovahlord Date: Fri, 31 Jan 2020 11:27:58 +0100 Subject: [PATCH] Core/AI: cleaned up SAI initialization in order to fix some unexpected behaivior when summoning creatures with sai assigned --- src/server/game/AI/SmartScripts/SmartAI.cpp | 173 ++++++++++---------- src/server/game/AI/SmartScripts/SmartAI.h | 46 +++--- 2 files changed, 108 insertions(+), 111 deletions(-) diff --git a/src/server/game/AI/SmartScripts/SmartAI.cpp b/src/server/game/AI/SmartScripts/SmartAI.cpp index 088ecb35ce..ed8c7f9347 100644 --- a/src/server/game/AI/SmartScripts/SmartAI.cpp +++ b/src/server/game/AI/SmartScripts/SmartAI.cpp @@ -29,35 +29,35 @@ #include "ScriptMgr.h" #include "Vehicle.h" -SmartAI::SmartAI(Creature* creature) : CreatureAI(creature), mIsCharmed(false), mFollowCreditType(0), mFollowArrivedTimer(0), mFollowCredit(0), mFollowArrivedEntry(0), mFollowDist(0.f), mFollowAngle(0.f), +SmartAI::SmartAI(Creature* creature) : CreatureAI(creature), _isCharmed(false), _followCreditType(0), _followArrivedTimer(0), _followCredit(0), _followArrivedEntry(0), _followDist(0.f), _followAngle(0.f), _escortState(SMART_ESCORT_NONE), _escortNPCFlags(0), _escortInvokerCheckTimer(1000), _currentWaypointNode(0), _waypointReached(false), _waypointPauseTimer(0), _waypointPauseForced(false), _repeatWaypointPath(false), - _OOCReached(false), _waypointPathEnded(false), mRun(true), mEvadeDisabled(false), mCanAutoAttack(true), mCanCombatMove(true), mInvincibilityHpLevel(0), mDespawnTime(0), mDespawnState(0), mConditionsTimer(0), + _OOCReached(false), _waypointPathEnded(false), _run(true), _evadeDisabled(false), _canAutoAttack(true), _canCombatMove(true), _invincibilityHpLevel(0), _despawnTime(0), _despawnState(0), _conditionsTimer(0), _gossipReturn(false) { - mHasConditions = sConditionMgr->HasConditionsForNotGroupedEntry(CONDITION_SOURCE_TYPE_CREATURE_TEMPLATE_VEHICLE, creature->GetEntry()); + _hasConditions = sConditionMgr->HasConditionsForNotGroupedEntry(CONDITION_SOURCE_TYPE_CREATURE_TEMPLATE_VEHICLE, creature->GetEntry()); } bool SmartAI::IsAIControlled() const { - return !mIsCharmed; + return !_isCharmed; } void SmartAI::UpdateDespawn(uint32 diff) { - if (mDespawnState <= 1 || mDespawnState > 3) + if (_despawnState <= 1 || _despawnState > 3) return; - if (mDespawnTime < diff) + if (_despawnTime < diff) { - if (mDespawnState == 2) + if (_despawnState == 2) { me->SetVisible(false); - mDespawnTime = 5000; - mDespawnState++; + _despawnTime = 5000; + _despawnState++; } else me->DespawnOrUnsummon(); - } else mDespawnTime -= diff; + } else _despawnTime -= diff; } void SmartAI::StartPath(bool run/* = false*/, uint32 pathId/* = 0*/, bool repeat/* = false*/, Unit* invoker/* = nullptr*/, uint32 nodeId/* = 1*/) @@ -113,7 +113,7 @@ bool SmartAI::LoadPath(uint32 entry) { Trinity::NormalizeMapCoord(waypoint.x); Trinity::NormalizeMapCoord(waypoint.y); - waypoint.moveType = mRun ? WAYPOINT_MOVE_TYPE_RUN : WAYPOINT_MOVE_TYPE_WALK; + waypoint.moveType = _run ? WAYPOINT_MOVE_TYPE_RUN : WAYPOINT_MOVE_TYPE_WALK; } GetScript()->SetPathId(entry); @@ -144,7 +144,7 @@ void SmartAI::PausePath(uint32 delay, bool forced) if (forced) { _waypointPauseForced = forced; - SetRun(mRun); + SetRun(_run); me->PauseMovement(); me->SetHomePosition(me->GetPosition()); } @@ -163,7 +163,7 @@ void SmartAI::StopPath(uint32 DespawnTime, uint32 quest, bool fail) if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == WAYPOINT_MOTION_TYPE) waypointInfo = me->GetCurrentWaypointInfo(); - if (mDespawnState != 2) + if (_despawnState != 2) SetDespawnTime(DespawnTime); me->GetMotionMaster()->MoveIdle(); @@ -175,7 +175,7 @@ void SmartAI::StopPath(uint32 DespawnTime, uint32 quest, bool fail) { if (waypointInfo.first) GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_ENDED, nullptr, waypointInfo.first, waypointInfo.second); - if (mDespawnState == 1) + if (_despawnState == 1) StartDespawn(); } return; @@ -184,7 +184,7 @@ void SmartAI::StopPath(uint32 DespawnTime, uint32 quest, bool fail) if (quest) mEscortQuestID = quest; - if (mDespawnState != 2) + if (_despawnState != 2) SetDespawnTime(DespawnTime); me->GetMotionMaster()->MoveIdle(); @@ -258,12 +258,12 @@ void SmartAI::EndPath(bool fail) if (_repeatWaypointPath) { if (IsAIControlled()) - StartPath(mRun, GetScript()->GetPathId(), _repeatWaypointPath); + StartPath(_run, GetScript()->GetPathId(), _repeatWaypointPath); } else GetScript()->SetPathId(0); - if (mDespawnState == 1) + if (_despawnState == 1) StartDespawn(); } @@ -277,7 +277,7 @@ void SmartAI::ResumePath() _waypointReached = false; _waypointPauseTimer = 0; - SetRun(mRun); + SetRun(_run); me->ResumeMovement(); } @@ -349,20 +349,20 @@ void SmartAI::UpdateAI(uint32 diff) UpdateDespawn(diff); /// @todo move to void - if (mFollowGuid) + if (_followGuid) { - if (mFollowArrivedTimer < diff) + if (_followArrivedTimer < diff) { - if (me->FindNearestCreature(mFollowArrivedEntry, INTERACTION_DISTANCE, true)) + if (me->FindNearestCreature(_followArrivedEntry, INTERACTION_DISTANCE, true)) { StopFollow(true); return; } - mFollowArrivedTimer = 1000; + _followArrivedTimer = 1000; } else - mFollowArrivedTimer -= diff; + _followArrivedTimer -= diff; } if (!IsAIControlled()) @@ -371,7 +371,7 @@ void SmartAI::UpdateAI(uint32 diff) if (!UpdateVictim()) return; - if (mCanAutoAttack) + if (_canAutoAttack) DoMeleeAttackIfReady(); } @@ -454,7 +454,7 @@ void SmartAI::WaypointReached(uint32 nodeId, uint32 pathId) if (_currentWaypointNode == _path.nodes.size()) _waypointPathEnded = true; else - SetRun(mRun); + SetRun(_run); } } @@ -484,7 +484,7 @@ void SmartAI::MovementInform(uint32 type, uint32 id) void SmartAI::EnterEvadeMode(EvadeReason /*why*/) { - if (mEvadeDisabled) + if (_evadeDisabled) { GetScript()->ProcessEventsFor(SMART_EVENT_EVADE); return; @@ -503,7 +503,7 @@ void SmartAI::EnterEvadeMode(EvadeReason /*why*/) GetScript()->ProcessEventsFor(SMART_EVENT_EVADE); // must be after _EnterEvadeMode (spells, auras, ...) - SetRun(mRun); + SetRun(_run); if (Unit* owner = me->GetCharmerOrOwner()) { @@ -515,9 +515,9 @@ void SmartAI::EnterEvadeMode(EvadeReason /*why*/) AddEscortState(SMART_ESCORT_RETURNING); ReturnToLastOOCPos(); } - else if (Unit* target = mFollowGuid ? ObjectAccessor::GetUnit(*me, mFollowGuid) : nullptr) + else if (Unit* target = _followGuid ? ObjectAccessor::GetUnit(*me, _followGuid) : nullptr) { - me->GetMotionMaster()->MoveFollow(target, mFollowDist, mFollowAngle); + me->GetMotionMaster()->MoveFollow(target, _followDist, _followAngle); // evade is not cleared in MoveFollow, so we can't keep it me->ClearUnitState(UNIT_STATE_EVADE); } @@ -591,24 +591,13 @@ bool SmartAI::AssistPlayerInCombatAgainst(Unit* who) void SmartAI::JustAppeared() { - mDespawnTime = 0; - mDespawnState = 0; - _escortState = SMART_ESCORT_NONE; + CreatureAI::JustAppeared(); - me->SetVisible(true); + if (me->isDead()) + return; - if (me->GetFaction() != me->GetCreatureTemplate()->faction) - me->RestoreFaction(); - - GetScript()->OnReset(); GetScript()->ProcessEventsFor(SMART_EVENT_RESPAWN); - mFollowGuid.Clear(); // do not reset follower on Reset(), we need it after combat evade - mFollowDist = 0; - mFollowAngle = 0; - mFollowCredit = 0; - mFollowArrivedTimer = 1000; - mFollowArrivedEntry = 0; - mFollowCreditType = 0; + GetScript()->OnReset(); } void SmartAI::JustReachedHome() @@ -660,18 +649,18 @@ void SmartAI::AttackStart(Unit* who) if (!IsAIControlled()) { if (who) - me->Attack(who, mCanAutoAttack); + me->Attack(who, _canAutoAttack); return; } - if (who && me->Attack(who, mCanAutoAttack)) + if (who && me->Attack(who, _canAutoAttack)) { me->GetMotionMaster()->Clear(MOTION_SLOT_ACTIVE); me->PauseMovement(0, 0, false); - if (mCanCombatMove) + if (_canCombatMove) { - SetRun(mRun); + SetRun(_run); me->GetMotionMaster()->MoveChase(who); } } @@ -694,8 +683,8 @@ void SmartAI::DamageTaken(Unit* doneBy, uint32& damage) if (!IsAIControlled()) // don't allow players to use unkillable units return; - if (mInvincibilityHpLevel && (damage >= me->GetHealth() - mInvincibilityHpLevel)) - damage = me->GetHealth() - mInvincibilityHpLevel; // damage should not be nullified, because of player damage req. + if (_invincibilityHpLevel && (damage >= me->GetHealth() - _invincibilityHpLevel)) + damage = me->GetHealth() - _invincibilityHpLevel; // damage should not be nullified, because of player damage req. } void SmartAI::HealReceived(Unit* doneBy, uint32& addhealth) @@ -737,11 +726,19 @@ void SmartAI::InitializeAI() { GetScript()->OnInitialize(me); - if (!me->isDead()) - { - GetScript()->OnReset(); - GetScript()->ProcessEventsFor(SMART_EVENT_RESPAWN); - } + _despawnTime = 0; + _despawnState = 0; + _escortState = SMART_ESCORT_NONE; + + me->SetVisible(true); + + _followGuid.Clear(); // do not reset follower on Reset(), we need it after combat evade + _followDist = 0; + _followAngle = 0; + _followCredit = 0; + _followArrivedTimer = 1000; + _followArrivedEntry = 0; + _followCreditType = 0; } void SmartAI::OnCharmed(bool apply) @@ -753,14 +750,14 @@ void SmartAI::OnCharmed(bool apply) me->StopMoving(); } - mIsCharmed = apply; + _isCharmed = apply; if (!apply && !me->IsInEvadeMode()) { if (_repeatWaypointPath) - StartPath(mRun, GetScript()->GetPathId(), true); + StartPath(_run, GetScript()->GetPathId(), true); else - me->SetWalk(!mRun); + me->SetWalk(!_run); if (Unit* charmer = me->GetCharmer()) AttackStart(charmer); @@ -793,11 +790,11 @@ ObjectGuid SmartAI::GetGUID(int32 /*id*/) const void SmartAI::SetRun(bool run) { - if (run == mRun) + if (run == _run) return; me->SetWalk(!run); - mRun = run; + _run = run; for (auto& node : _path.nodes) node.moveType = run ? WAYPOINT_MOVE_TYPE_RUN : WAYPOINT_MOVE_TYPE_WALK; } @@ -819,7 +816,7 @@ void SmartAI::SetSwim(bool swim) void SmartAI::SetEvadeDisabled(bool disable) { - mEvadeDisabled = disable; + _evadeDisabled = disable; } bool SmartAI::GossipHello(Player* player) @@ -853,10 +850,10 @@ void SmartAI::QuestReward(Player* player, Quest const* quest, uint32 opt) void SmartAI::SetCombatMove(bool on) { - if (mCanCombatMove == on) + if (_canCombatMove == on) return; - mCanCombatMove = on; + _canCombatMove = on; if (!IsAIControlled()) return; @@ -865,7 +862,7 @@ void SmartAI::SetCombatMove(bool on) { if (on && !me->HasReactState(REACT_PASSIVE) && me->GetVictim() && me->GetMotionMaster()->GetMotionSlotType(MOTION_SLOT_ACTIVE) == MAX_MOTION_TYPE) { - SetRun(mRun); + SetRun(_run); me->GetMotionMaster()->MoveChase(me->GetVictim()); } else if (!on && me->GetMotionMaster()->GetMotionSlotType(MOTION_SLOT_ACTIVE) == CHASE_MOTION_TYPE) @@ -884,38 +881,38 @@ void SmartAI::SetFollow(Unit* target, float dist, float angle, uint32 credit, ui return; } - mFollowGuid = target->GetGUID(); - mFollowDist = dist; - mFollowAngle = angle; - mFollowArrivedTimer = 1000; - mFollowCredit = credit; - mFollowArrivedEntry = end; - mFollowCreditType = creditType; - SetRun(mRun); - me->GetMotionMaster()->MoveFollow(target, mFollowDist, mFollowAngle); + _followGuid = target->GetGUID(); + _followDist = dist; + _followAngle = angle; + _followArrivedTimer = 1000; + _followCredit = credit; + _followArrivedEntry = end; + _followCreditType = creditType; + SetRun(_run); + me->GetMotionMaster()->MoveFollow(target, _followDist, _followAngle); } void SmartAI::StopFollow(bool complete) { - mFollowGuid.Clear(); - mFollowDist = 0; - mFollowAngle = 0; - mFollowCredit = 0; - mFollowArrivedTimer = 1000; - mFollowArrivedEntry = 0; - mFollowCreditType = 0; + _followGuid.Clear(); + _followDist = 0; + _followAngle = 0; + _followCredit = 0; + _followArrivedTimer = 1000; + _followArrivedEntry = 0; + _followCreditType = 0; me->GetMotionMaster()->MoveIdle(); if (!complete) return; - Player* player = ObjectAccessor::GetPlayer(*me, mFollowGuid); + Player* player = ObjectAccessor::GetPlayer(*me, _followGuid); if (player) { - if (!mFollowCreditType) - player->RewardPlayerAndGroupAtEvent(mFollowCredit, me); + if (!_followCreditType) + player->RewardPlayerAndGroupAtEvent(_followCredit, me); else - player->GroupEventHappens(mFollowCredit, me); + player->GroupEventHappens(_followCredit, me); } SetDespawnTime(5000); @@ -945,10 +942,10 @@ void SmartAI::OnSpellClick(Unit* clicker, bool& result) void SmartAI::CheckConditions(uint32 diff) { - if (!mHasConditions) + if (!_hasConditions) return; - if (mConditionsTimer <= diff) + if (_conditionsTimer <= diff) { if (Vehicle* vehicleKit = me->GetVehicleKit()) { @@ -966,10 +963,10 @@ void SmartAI::CheckConditions(uint32 diff) } } - mConditionsTimer = 1000; + _conditionsTimer = 1000; } else - mConditionsTimer -= diff; + _conditionsTimer -= diff; } void SmartGameObjectAI::UpdateAI(uint32 diff) diff --git a/src/server/game/AI/SmartScripts/SmartAI.h b/src/server/game/AI/SmartScripts/SmartAI.h index c97549d730..d5e3bd8f26 100644 --- a/src/server/game/AI/SmartScripts/SmartAI.h +++ b/src/server/game/AI/SmartScripts/SmartAI.h @@ -61,9 +61,9 @@ class TC_GAME_API SmartAI : public CreatureAI bool HasEscortState(uint32 uiEscortState) const { return (_escortState & uiEscortState) != 0; } void AddEscortState(uint32 uiEscortState) { _escortState |= uiEscortState; } void RemoveEscortState(uint32 uiEscortState) { _escortState &= ~uiEscortState; } - void SetAutoAttack(bool on) { mCanAutoAttack = on; } + void SetAutoAttack(bool on) { _canAutoAttack = on; } void SetCombatMove(bool on); - bool CanCombatMove() { return mCanCombatMove; } + bool CanCombatMove() { return _canCombatMove; } void SetFollow(Unit* target, float dist = 0.0f, float angle = 0.0f, uint32 credit = 0, uint32 end = 0, uint32 creditType = 0); void StopFollow(bool complete); bool IsEscortInvokerInRange(); @@ -175,7 +175,7 @@ class TC_GAME_API SmartAI : public CreatureAI void SetEvadeDisabled(bool disable = true); - void SetInvincibilityHpLevel(uint32 level) { mInvincibilityHpLevel = level; } + void SetInvincibilityHpLevel(uint32 level) { _invincibilityHpLevel = level; } bool GossipHello(Player* player) override; bool GossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override; @@ -188,10 +188,10 @@ class TC_GAME_API SmartAI : public CreatureAI void SetDespawnTime (uint32 t) { - mDespawnTime = t; - mDespawnState = t ? 1 : 0; + _despawnTime = t; + _despawnState = t ? 1 : 0; } - void StartDespawn() { mDespawnState = 2; } + void StartDespawn() { _despawnState = 2; } void OnSpellClick(Unit* clicker, bool& result) override; @@ -209,14 +209,14 @@ class TC_GAME_API SmartAI : public CreatureAI SmartScript mScript; - bool mIsCharmed; - uint32 mFollowCreditType; - uint32 mFollowArrivedTimer; - uint32 mFollowCredit; - uint32 mFollowArrivedEntry; - ObjectGuid mFollowGuid; - float mFollowDist; - float mFollowAngle; + bool _isCharmed; + uint32 _followCreditType; + uint32 _followArrivedTimer; + uint32 _followCredit; + uint32 _followArrivedEntry; + ObjectGuid _followGuid; + float _followDist; + float _followAngle; uint32 _escortState; uint32 _escortNPCFlags; @@ -229,18 +229,18 @@ class TC_GAME_API SmartAI : public CreatureAI bool _repeatWaypointPath; bool _OOCReached; bool _waypointPathEnded; - bool mRun; - bool mEvadeDisabled; - bool mCanAutoAttack; - bool mCanCombatMove; - uint32 mInvincibilityHpLevel; + bool _run; + bool _evadeDisabled; + bool _canAutoAttack; + bool _canCombatMove; + uint32 _invincibilityHpLevel; - uint32 mDespawnTime; - uint32 mDespawnState; + uint32 _despawnTime; + uint32 _despawnState; // Vehicle conditions - bool mHasConditions; - uint32 mConditionsTimer; + bool _hasConditions; + uint32 _conditionsTimer; // Gossip bool _gossipReturn;