Core/Spells: Improved spell category cooldown handling

* Category cooldown is stored with the spell that started the cooldown (and only resetting cooldown on that spell will clear cooldowns on entire category - this fully mirrors client behavior)
* This significantly reduces the amount of data saved to database for cooldowns
* Spell casts from items that have a different category specified than on spell will now check for cooldown during the cast

(cherry picked from commit 1efb3f08e278530f59d681f676b031a7fc6db3ac)

Closes #15766
Closes #15137
Closes #14837
This commit is contained in:
Shauren committed 2015-10-29 17:56:18 +01:00
1 parent 3f32307d37
commit 7dcddd90be
12 files changed
+155 -139

No files matched your search

+117 -102
View File
@@ -43,6 +43,8 @@ struct SpellHistory::PersistenceHelper<Player>
cooldownEntry->CooldownEnd = Clock::from_time_t(time_t(fields[2].GetUInt32()));
cooldownEntry->ItemId = fields[1].GetUInt32();
cooldownEntry->CategoryId = fields[3].GetUInt32();
cooldownEntry->CategoryEnd = Clock::from_time_t(time_t(fields[4].GetUInt32()));
return true;
}
@@ -51,6 +53,8 @@ struct SpellHistory::PersistenceHelper<Player>
stmt->setUInt32(index++, cooldown.first);
stmt->setUInt32(index++, cooldown.second.ItemId);
stmt->setUInt32(index++, uint32(Clock::to_time_t(cooldown.second.CooldownEnd)));
stmt->setUInt32(index++, cooldown.second.CategoryId);
stmt->setUInt32(index++, uint32(Clock::to_time_t(cooldown.second.CategoryEnd)));
}
};
@@ -70,6 +74,8 @@ struct SpellHistory::PersistenceHelper<Pet>
cooldownEntry->CooldownEnd = Clock::from_time_t(time_t(fields[1].GetUInt32()));
cooldownEntry->ItemId = 0;
cooldownEntry->CategoryId = fields[2].GetUInt32();
cooldownEntry->CategoryEnd = Clock::from_time_t(time_t(fields[3].GetUInt32()));
return true;
}
@@ -77,6 +83,8 @@ struct SpellHistory::PersistenceHelper<Pet>
{
stmt->setUInt32(index++, cooldown.first);
stmt->setUInt32(index++, uint32(Clock::to_time_t(cooldown.second.CooldownEnd)));
stmt->setUInt32(index++, cooldown.second.CategoryId);
stmt->setUInt32(index++, uint32(Clock::to_time_t(cooldown.second.CategoryEnd)));
}
};
@@ -92,7 +100,11 @@ void SpellHistory::LoadFromDB(PreparedQueryResult cooldownsResult)
uint32 spellId;
CooldownEntry cooldown;
if (StatementInfo::ReadCooldown(cooldownsResult->Fetch(), &spellId, &cooldown))
{
_spellCooldowns[spellId] = cooldown;
if (cooldown.CategoryId)
_categoryCooldowns[cooldown.CategoryId] = &_spellCooldowns[spellId];
}
} while (cooldownsResult->NextRow());
}
@@ -125,40 +137,56 @@ void SpellHistory::Update()
{
SQLTransaction t;
Clock::time_point now = Clock::now();
for (auto itr = _categoryCooldowns.begin(); itr != _categoryCooldowns.end();)
{
if (itr->second->CategoryEnd < now)
itr = _categoryCooldowns.erase(itr);
else
++itr;
}
for (auto itr = _spellCooldowns.begin(); itr != _spellCooldowns.end();)
{
if (itr->second.CooldownEnd < now)
itr = _spellCooldowns.erase(itr);
itr = EraseCooldown(itr);
else
++itr;
}
}
void SpellHistory::HandleCooldowns(SpellInfo const* spellInfo, Item const* item, Spell* spell /*= nullptr*/)
{
HandleCooldowns(spellInfo, item ? item->GetEntry() : 0, spell);
}
void SpellHistory::HandleCooldowns(SpellInfo const* spellInfo, uint32 itemID, Spell* spell /*= nullptr*/)
{
if (Player* player = _owner->ToPlayer())
{
// potions start cooldown until exiting combat
if (item && (item->IsPotion() || spellInfo->IsCooldownStartedOnEvent()))
if (ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(itemID))
{
player->SetLastPotionId(item->GetEntry());
return;
if (itemTemplate->IsPotion() || spellInfo->IsCooldownStartedOnEvent())
{
player->SetLastPotionId(itemID);
return;
}
}
}
if (spellInfo->IsCooldownStartedOnEvent() || spellInfo->IsPassive() || (spell && spell->IsIgnoringCooldowns()))
return;
StartCooldown(spellInfo, item ? item->GetEntry() : 0, spell);
StartCooldown(spellInfo, itemID, spell);
}
bool SpellHistory::IsReady(SpellInfo const* spellInfo) const
bool SpellHistory::IsReady(SpellInfo const* spellInfo, uint32 itemId /*= 0*/) const
{
if (spellInfo->PreventionType == SPELL_PREVENTION_TYPE_SILENCE)
if (IsSchoolLocked(spellInfo->GetSchoolMask()))
return false;
if (HasCooldown(spellInfo->Id))
if (HasCooldown(spellInfo->Id, itemId))
return false;
return true;
@@ -168,42 +196,33 @@ template<>
void SpellHistory::WritePacket<Pet>(WorldPacket& packet) const
{
Clock::time_point now = Clock::now();
uint8 cooldownsCount = _spellCooldowns.size();
packet << uint8(cooldownsCount);
for (auto const& spellCooldown : _spellCooldowns)
{
SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(spellCooldown.first);
if (!spellInfo)
{
packet << uint32(0);
packet << uint16(0);
packet << uint32(0);
packet << uint32(0);
continue;
}
packet << uint32(spellCooldown.first); // spell ID
packet << uint16(spellInfo->GetCategory()); // spell category
packet << uint32(spellCooldown.first); // spell ID
packet << uint16(spellCooldown.second.CategoryId); // spell category
if (!spellCooldown.second.OnHold)
{
uint32 cooldownDuration = spellCooldown.second.CooldownEnd > now ? std::chrono::duration_cast<std::chrono::milliseconds>(spellCooldown.second.CooldownEnd - now).count() : 0;
if (cooldownDuration <= 0)
std::chrono::milliseconds cooldownDuration = std::chrono::duration_cast<std::chrono::milliseconds>(spellCooldown.second.CooldownEnd - now);
if (cooldownDuration.count() <= 0)
{
packet << uint32(0);
packet << uint32(0);
continue;
}
if (spellInfo->GetCategory())
std::chrono::milliseconds categoryDuration = std::chrono::duration_cast<std::chrono::milliseconds>(spellCooldown.second.CategoryEnd - now);
if (categoryDuration.count() > 0)
{
packet << uint32(0);
packet << uint32(cooldownDuration);
packet << uint32(categoryDuration.count());
}
else
{
packet << uint32(cooldownDuration);
packet << uint32(cooldownDuration.count());
packet << uint32(0);
}
}
@@ -216,23 +235,13 @@ void SpellHistory::WritePacket<Player>(WorldPacket& packet) const
Clock::time_point now = Clock::now();
Clock::time_point infTime = now + InfinityCooldownDelayCheck;
uint16 cooldownsCount = _spellCooldowns.size();
size_t dataPos = packet.wpos();
packet << uint16(cooldownsCount);
packet << uint16(_spellCooldowns.size());
for (auto const& spellCooldown : _spellCooldowns)
{
SpellInfo const* sEntry = sSpellMgr->GetSpellInfo(spellCooldown.first);
if (!sEntry)
{
--cooldownsCount;
continue;
}
packet << uint32(spellCooldown.first);
packet << uint16(spellCooldown.second.ItemId); // cast item id
packet << uint16(sEntry->GetCategory()); // spell category
packet << uint16(spellCooldown.second.CategoryId); // spell category
// send infinity cooldown in special format
if (spellCooldown.second.CooldownEnd >= infTime)
@@ -242,21 +251,26 @@ void SpellHistory::WritePacket<Player>(WorldPacket& packet) const
continue;
}
uint32 cooldownDuration = spellCooldown.second.CooldownEnd > now ? std::chrono::duration_cast<std::chrono::milliseconds>(spellCooldown.second.CooldownEnd - now).count() : 0;
std::chrono::milliseconds cooldownDuration = std::chrono::duration_cast<std::chrono::milliseconds>(spellCooldown.second.CooldownEnd - now);
if (cooldownDuration.count() <= 0)
{
packet << uint32(0);
packet << uint32(0);
continue;
}
if (sEntry->GetCategory()) // may be wrong, but anyway better than nothing...
std::chrono::milliseconds categoryDuration = std::chrono::duration_cast<std::chrono::milliseconds>(spellCooldown.second.CategoryEnd - now);
if (categoryDuration.count() >= 0)
{
packet << uint32(0); // cooldown
packet << uint32(cooldownDuration); // category cooldown
packet << uint32(categoryDuration.count()); // category cooldown
}
else
{
packet << uint32(cooldownDuration); // cooldown
packet << uint32(cooldownDuration.count()); // cooldown
packet << uint32(0); // category cooldown
}
}
packet.put<uint16>(dataPos, cooldownsCount);
}
void SpellHistory::StartCooldown(SpellInfo const* spellInfo, uint32 itemId, Spell* spell /*= nullptr*/, bool onHold /*= false*/)
@@ -351,7 +365,7 @@ void SpellHistory::StartCooldown(SpellInfo const* spellInfo, uint32 itemId, Spel
// self spell cooldown
if (recTime != curTime)
{
AddCooldown(spellInfo->Id, itemId, recTime, onHold);
AddCooldown(spellInfo->Id, itemId, recTime, categoryId, catrecTime, onHold);
if (needsCooldownPacket)
{
@@ -363,22 +377,6 @@ void SpellHistory::StartCooldown(SpellInfo const* spellInfo, uint32 itemId, Spel
}
}
}
// category spells
if (categoryId && catrecTime != curTime)
{
SpellCategoryStore::const_iterator i_scstore = sSpellsByCategoryStore.find(categoryId);
if (i_scstore != sSpellsByCategoryStore.end())
{
for (SpellCategorySet::const_iterator i_scset = i_scstore->second.begin(); i_scset != i_scstore->second.end(); ++i_scset)
{
if (*i_scset == spellInfo->Id) // skip main spell, already handled above
continue;
AddCooldown(*i_scset, itemId, catrecTime, onHold);
}
}
}
}
void SpellHistory::SendCooldownEvent(SpellInfo const* spellInfo, uint32 itemId /*= 0*/, Spell* spell /*= nullptr*/, bool startCooldown /*= true*/)
@@ -387,49 +385,27 @@ void SpellHistory::SendCooldownEvent(SpellInfo const* spellInfo, uint32 itemId /
if (startCooldown)
StartCooldown(spellInfo, itemId, spell);
// Send activate cooldown timer (possible 0) at client side
if (Player* player = GetPlayerOwner())
{
// Send activate cooldown timer (possible 0) at client side
WorldPacket data(SMSG_COOLDOWN_EVENT, 4 + 8);
data << uint32(spellInfo->Id);
data << uint64(_owner->GetGUID());
player->SendDirectMessage(&data);
uint32 category = spellInfo->GetCategory();
if (category && spellInfo->CategoryRecoveryTime)
{
SpellCategoryStore::const_iterator ct = sSpellsByCategoryStore.find(category);
if (ct != sSpellsByCategoryStore.end())
{
for (auto const& cooldownPair : _spellCooldowns)
{
uint32 categorySpell = cooldownPair.first;
if (!ct->second.count(categorySpell))
continue;
if (categorySpell == spellInfo->Id) // skip main spell, already handled above
continue;
SpellInfo const* spellInfo2 = sSpellMgr->EnsureSpellInfo(categorySpell);
if (!spellInfo2->IsCooldownStartedOnEvent())
continue;
data.Initialize(SMSG_COOLDOWN_EVENT, 4 + 8);
data << uint32(categorySpell);
data << uint64(_owner->GetGUID());
player->SendDirectMessage(&data);
}
}
}
}
}
void SpellHistory::AddCooldown(uint32 spellId, uint32 itemId, Clock::time_point cooldownEnd, bool onHold /*= false*/)
void SpellHistory::AddCooldown(uint32 spellId, uint32 itemId, Clock::time_point cooldownEnd, uint32 categoryId, Clock::time_point categoryEnd, bool onHold /*= false*/)
{
CooldownEntry& cooldownEntry = _spellCooldowns[spellId];
cooldownEntry.CooldownEnd = cooldownEnd;
cooldownEntry.ItemId = itemId;
cooldownEntry.CategoryId = categoryId;
cooldownEntry.CategoryEnd = categoryEnd;
cooldownEntry.OnHold = onHold;
if (categoryId)
_categoryCooldowns[categoryId] = &cooldownEntry;
}
void SpellHistory::ModifyCooldown(uint32 spellId, int32 cooldownModMs)
@@ -443,7 +419,7 @@ void SpellHistory::ModifyCooldown(uint32 spellId, int32 cooldownModMs)
if (itr->second.CooldownEnd + offset > now)
itr->second.CooldownEnd += offset;
else
_spellCooldowns.erase(itr);
EraseCooldown(itr);
if (Player* playerOwner = GetPlayerOwner())
{
@@ -477,7 +453,7 @@ void SpellHistory::ResetCooldown(CooldownStorageType::iterator& itr, bool update
}
}
itr = _spellCooldowns.erase(itr);
itr = EraseCooldown(itr);
}
void SpellHistory::ResetAllCooldowns()
@@ -492,31 +468,69 @@ void SpellHistory::ResetAllCooldowns()
SendClearCooldowns(cooldowns);
}
_categoryCooldowns.clear();
_spellCooldowns.clear();
}
bool SpellHistory::HasCooldown(uint32 spellId) const
bool SpellHistory::HasCooldown(SpellInfo const* spellInfo, uint32 itemId /*= 0*/) const
{
return _spellCooldowns.count(spellId) != 0;
if (_spellCooldowns.count(spellInfo->Id) != 0)
return true;
uint32 category = 0;
if (ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(itemId))
{
for (uint32 i = 0; i < MAX_ITEM_PROTO_SPELLS; ++i)
{
if (itemTemplate->Spells[i].SpellId == spellInfo->Id)
{
category = itemTemplate->Spells[i].SpellCategory;
break;
}
}
}
if (!category)
category = spellInfo->GetCategory();
if (!category)
return false;
return _categoryCooldowns.count(category) != 0;
}
uint32 SpellHistory::GetRemainingCooldown(uint32 spellId) const
bool SpellHistory::HasCooldown(uint32 spellId, uint32 itemId /*= 0*/) const
{
auto itr = _spellCooldowns.find(spellId);
if (itr == _spellCooldowns.end())
return 0;
return HasCooldown(sSpellMgr->EnsureSpellInfo(spellId), itemId);
}
uint32 SpellHistory::GetRemainingCooldown(SpellInfo const* spellInfo) const
{
Clock::time_point end;
auto itr = _spellCooldowns.find(spellInfo->Id);
if (itr != _spellCooldowns.end())
end = itr->second.CooldownEnd;
else
{
auto catItr = _categoryCooldowns.find(spellInfo->GetCategory());
if (catItr == _categoryCooldowns.end())
return 0;
end = catItr->second->CategoryEnd;
}
Clock::time_point now = Clock::now();
if (itr->second.CooldownEnd < now)
if (end < now)
return 0;
Clock::duration remaining = itr->second.CooldownEnd - now;
Clock::duration remaining = end - now;
return uint32(std::chrono::duration_cast<std::chrono::milliseconds>(remaining).count());
}
void SpellHistory::LockSpellSchool(SpellSchoolMask schoolMask, uint32 lockoutTime)
{
Clock::time_point lockoutEnd = Clock::now() + std::chrono::duration_cast<Clock::duration>(std::chrono::milliseconds(lockoutTime));
Clock::time_point now = Clock::now();
Clock::time_point lockoutEnd = now + std::chrono::duration_cast<Clock::duration>(std::chrono::milliseconds(lockoutTime));
for (uint32 i = 0; i < MAX_SPELL_SCHOOL; ++i)
if (SpellSchoolMask(1 << i) & schoolMask)
_schoolLockouts[i] = lockoutEnd;
@@ -553,10 +567,10 @@ void SpellHistory::LockSpellSchool(SpellSchoolMask schoolMask, uint32 lockoutTim
if (spellInfo->PreventionType != SPELL_PREVENTION_TYPE_SILENCE)
continue;
if ((schoolMask & spellInfo->GetSchoolMask()) && GetRemainingCooldown(spellId) < lockoutTime)
if ((schoolMask & spellInfo->GetSchoolMask()) && GetRemainingCooldown(spellInfo) < lockoutTime)
{
cooldowns[spellId] = lockoutTime;
AddCooldown(spellId, 0, lockoutEnd);
AddCooldown(spellId, 0, lockoutEnd, 0, now);
}
}
@@ -644,6 +658,7 @@ void SpellHistory::SaveCooldownStateBeforeDuel()
void SpellHistory::RestoreCooldownStateAfterDuel()
{
// category cooldows are not preserved.
if (Player* player = _owner->ToPlayer())
{
// add all profession CDs created while in duel (if any)