fix cast spell stuck and lagging

This commit is contained in:
Yunfan Li
2023-05-27 23:59:07 +08:00
parent 33ef77c4fa
commit a59bb24d7c

View File

@@ -63,7 +63,9 @@ void PacketHandlingHelper::AddPacket(WorldPacket const& packet)
{ {
if (packet.empty()) if (packet.empty())
return; return;
// assert(handlers);
// assert(packet);
// assert(packet.GetOpcode());
if (handlers.find(packet.GetOpcode()) != handlers.end()) if (handlers.find(packet.GetOpcode()) != handlers.end())
queue.push(WorldPacket(packet)); queue.push(WorldPacket(packet));
} }
@@ -177,6 +179,10 @@ void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)
nextAICheckDelay = 0; nextAICheckDelay = 0;
// cancel logout in combat // cancel logout in combat
if (!bot->GetSession()) {
return;
}
if (bot->GetSession()->isLogingOut()) if (bot->GetSession()->isLogingOut())
{ {
if (bot->IsInCombat() || (master && master->IsInCombat() && sServerFacade->GetDistance2d(bot, master) < 30.0f)) if (bot->IsInCombat() || (master && master->IsInCombat() && sServerFacade->GetDistance2d(bot, master) < 30.0f))
@@ -187,26 +193,26 @@ void PlayerbotAI::UpdateAI(uint32 elapsed, bool minimal)
} }
} }
// wake up if in combat // wake up if in combat
if (bot->IsInCombat()) // if (bot->IsInCombat())
{ // {
if (!inCombat) // if (!inCombat)
nextAICheckDelay = 0; // nextAICheckDelay = 0;
else if (!AllowActivity()) // else if (!AllowActivity())
{ // {
if (AllowActivity(ALL_ACTIVITY, true)) // if (AllowActivity(ALL_ACTIVITY, true))
nextAICheckDelay = 0; // nextAICheckDelay = 0;
} // }
inCombat = true; // inCombat = true;
} // }
else // else
{ // {
if (inCombat) // if (inCombat)
nextAICheckDelay = 0; // nextAICheckDelay = 0;
inCombat = false; // inCombat = false;
} // }
// force stop if moving but should not // force stop if moving but should not
if (bot->isMoving() && !CanMove() && !bot->m_movementInfo.HasMovementFlag(MOVEMENTFLAG_FALLING)) if (bot->isMoving() && !CanMove() && !bot->m_movementInfo.HasMovementFlag(MOVEMENTFLAG_FALLING))
@@ -1653,8 +1659,13 @@ bool PlayerbotAI::CanCastSpell(uint32 spellid, Unit* target, bool checkHasSpell,
return false; return false;
uint32 CastingTime = !spellInfo->IsChanneled() ? spellInfo->CalcCastTime(bot) : spellInfo->GetDuration(); uint32 CastingTime = !spellInfo->IsChanneled() ? spellInfo->CalcCastTime(bot) : spellInfo->GetDuration();
if (CastingTime && bot->isMoving()) if (CastingTime && bot->isMoving()) {
if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
LOG_DEBUG("playerbots", "Casting time and bot is moving - target name: {}, spellid: {}, bot name: {}",
target->GetName(), spellid, bot->GetName());
}
return false; return false;
}
if (!itemTarget) if (!itemTarget)
{ {
@@ -1665,30 +1676,45 @@ bool PlayerbotAI::CanCastSpell(uint32 spellid, Unit* target, bool checkHasSpell,
// if (!positiveSpell && bot->IsFriendlyTo(target)) // if (!positiveSpell && bot->IsFriendlyTo(target))
// return false; // return false;
bool damage = false; // bool damage = false;
for (uint8 i = EFFECT_0; i <= EFFECT_2; i++) // for (uint8 i = EFFECT_0; i <= EFFECT_2; i++)
{ // {
if (spellInfo->Effects[i].Effect == SPELL_EFFECT_SCHOOL_DAMAGE) // if (spellInfo->Effects[i].Effect == SPELL_EFFECT_SCHOOL_DAMAGE)
{ // {
damage = true; // damage = true;
break; // break;
// }
// }
// if (target->IsImmunedToSpell(spellInfo)) {
// if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
// LOG_DEBUG("playerbots", "target is immuned to spell - target name: {}, spellid: {}, bot name: {}",
// target->GetName(), spellid, bot->GetName());
// }
// return false;
// }
// if (!damage)
// {
// for (uint8 i = EFFECT_0; i <= EFFECT_2; i++)
// {
// if (target->IsImmunedToSpellEffect(spellInfo, i)) {
// if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
// LOG_DEBUG("playerbots", "target is immuned to spell effect - target name: {}, spellid: {}, bot name: {}",
// target->GetName(), spellid, bot->GetName());
// }
// return false;
// }
// }
// }
if (bot != target && sServerFacade->GetDistance2d(bot, target) > sPlayerbotAIConfig->sightDistance) {
if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
LOG_DEBUG("playerbots", "target is out of sight distance - target name: {}, spellid: {}, bot name: {}",
target->GetName(), spellid, bot->GetName());
} }
}
if (target->IsImmunedToSpell(spellInfo))
return false; return false;
if (!damage)
{
for (uint8 i = EFFECT_0; i <= EFFECT_2; i++)
{
if (target->IsImmunedToSpellEffect(spellInfo, i))
return false;
}
} }
if (bot != target && sServerFacade->GetDistance2d(bot, target) > sPlayerbotAIConfig->sightDistance)
return false;
} }
ObjectGuid oldSel = bot->GetTarget(); ObjectGuid oldSel = bot->GetTarget();
@@ -1703,6 +1729,13 @@ bool PlayerbotAI::CanCastSpell(uint32 spellid, Unit* target, bool checkHasSpell,
SpellCastResult result = spell->CheckCast(true); SpellCastResult result = spell->CheckCast(true);
delete spell; delete spell;
if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
if (result != SPELL_FAILED_NOT_READY && result != SPELL_CAST_OK) {
LOG_DEBUG("playerbots", "CanCastSpell - target name: {}, spellid: {}, bot name: {}, result: {}",
target->GetName(), spellid, bot->GetName(), result);
}
}
if (oldSel) if (oldSel)
bot->SetTarget(oldSel); bot->SetTarget(oldSel);
@@ -1849,8 +1882,9 @@ bool PlayerbotAI::CastSpell(std::string const name, Unit* target, Item* itemTarg
bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget) bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget)
{ {
if (!spellId) if (!spellId) {
return false; return false;
}
if (!target) if (!target)
target = bot; target = bot;
@@ -1880,8 +1914,13 @@ bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget)
aiObjectContext->GetValue<LastMovement&>("last movement")->Get().Set(nullptr); aiObjectContext->GetValue<LastMovement&>("last movement")->Get().Set(nullptr);
aiObjectContext->GetValue<time_t>("stay time")->Set(0); aiObjectContext->GetValue<time_t>("stay time")->Set(0);
if (bot->IsFlying() || bot->HasUnitState(UNIT_STATE_IN_FLIGHT)) if (bot->IsFlying() || bot->HasUnitState(UNIT_STATE_IN_FLIGHT)) {
if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
LOG_DEBUG("playerbots", "Spell cast is flying - target name: {}, spellid: {}, bot name: {}}",
target->GetName(), spellId, bot->GetName());
}
return false; return false;
}
bot->ClearUnitState(UNIT_STATE_CHASE); bot->ClearUnitState(UNIT_STATE_CHASE);
bot->ClearUnitState(UNIT_STATE_FOLLOW); bot->ClearUnitState(UNIT_STATE_FOLLOW);
@@ -1906,6 +1945,10 @@ bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget)
if (failWithDelay) if (failWithDelay)
{ {
SetNextCheckDelay(sPlayerbotAIConfig->globalCoolDown); SetNextCheckDelay(sPlayerbotAIConfig->globalCoolDown);
if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
LOG_DEBUG("playerbots", "Spell cast fail with delay - target name: {}, spellid: {}, bot name: {}",
target->GetName(), spellId, bot->GetName());
}
return false; return false;
} }
@@ -1921,6 +1964,10 @@ bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget)
{ {
bot->GetTradeData()->SetSpell(spellId); bot->GetTradeData()->SetSpell(spellId);
delete spell; delete spell;
if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
LOG_DEBUG("playerbots", "Spell cast no item - target name: {}, spellid: {}, bot name: {}}",
target->GetName(), spellId, bot->GetName());
}
return true; return true;
} }
} }
@@ -1969,8 +2016,16 @@ bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget)
// return false; // return false;
} }
// spell->m_targets.SetUnitTarget(target);
// SpellCastResult spellSuccess = spell->CheckCast(true);
// if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
// LOG_DEBUG("playerbots", "Spell cast result - target name: {}, spellid: {}, bot name: {}, result: {}",
// target->GetName(), spellId, bot->GetName(), spellSuccess);
// }
// if (spellSuccess != SPELL_CAST_OK)
// return false;
spell->prepare(&targets); spell->prepare(&targets);
SpellCastResult spellSuccess = spell->CheckCast(true);
if (spellInfo->Effects[0].Effect == SPELL_EFFECT_OPEN_LOCK || spellInfo->Effects[0].Effect == SPELL_EFFECT_SKINNING) if (spellInfo->Effects[0].Effect == SPELL_EFFECT_OPEN_LOCK || spellInfo->Effects[0].Effect == SPELL_EFFECT_SKINNING)
{ {
@@ -1978,14 +2033,15 @@ bool PlayerbotAI::CastSpell(uint32 spellId, Unit* target, Item* itemTarget)
if (!loot.IsLootPossible(bot)) if (!loot.IsLootPossible(bot))
{ {
spell->cancel(); spell->cancel();
//delete spell; delete spell;
if (!sPlayerbotAIConfig->logInGroupOnly || bot->GetGroup()) {
LOG_DEBUG("playerbots", "Spell cast loot - target name: {}, spellid: {}, bot name: {}",
target->GetName(), spellId, bot->GetName());
}
return false; return false;
} }
} }
if (spellSuccess != SPELL_CAST_OK)
return false;
WaitForSpellCast(spell); WaitForSpellCast(spell);
if (spell->GetCastTime()) if (spell->GetCastTime())
aiObjectContext->GetValue<LastSpellCast&>("last spell cast")->Get().Set(spellId, target->GetGUID(), time(nullptr)); aiObjectContext->GetValue<LastSpellCast&>("last spell cast")->Get().Set(spellId, target->GetGUID(), time(nullptr));