From 4430863a1e9a787a3a702b58adaf4c7bb639fa29 Mon Sep 17 00:00:00 2001 From: Ovahlord Date: Mon, 24 May 2021 00:09:57 +0200 Subject: [PATCH] Core/Spells: Implement spell queue As of patch 4.1 spell casts can now be queued up 400ms before a ongoing cast finishes or the global cooldown expires to deliver a smooth transitions between casts though emulators do not really benefit from it. closes #224 --- src/server/game/Entities/Player/Player.cpp | 10 +- src/server/game/Entities/Player/Player.h | 2 +- src/server/game/Entities/Unit/Unit.cpp | 298 +++++++++++++++++- src/server/game/Entities/Unit/Unit.h | 34 ++ src/server/game/Handlers/SpellHandler.cpp | 228 ++------------ .../game/Server/Packets/SpellPackets.cpp | 33 ++ src/server/game/Server/Packets/SpellPackets.h | 68 ++-- .../game/Server/Packets/SpellPacketsCommon.h | 75 +++++ src/server/game/Server/Protocol/Opcodes.cpp | 2 +- src/server/game/Server/WorldSession.h | 5 +- src/server/game/Spells/Spell.h | 1 + src/server/game/Spells/SpellHistory.cpp | 16 + src/server/game/Spells/SpellHistory.h | 1 + 13 files changed, 513 insertions(+), 260 deletions(-) create mode 100644 src/server/game/Server/Packets/SpellPacketsCommon.h diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index ca2c4ac0ab..5af7c8a3c6 100644 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -7940,7 +7940,7 @@ void Player::CastItemCombatSpell(DamageInfo const& damageInfo, Item* item, ItemT } } -void Player::CastItemUseSpell(Item* item, SpellCastTargets const& targets, uint8 cast_count, uint32 glyphIndex) +void Player::CastItemUseSpell(Item* item, SpellCastTargets const& targets, uint8 castId, uint32 glyphIndex) { ItemTemplate const* proto = item->GetTemplate(); // special learning case @@ -7959,7 +7959,7 @@ void Player::CastItemUseSpell(Item* item, SpellCastTargets const& targets, uint8 Spell* spell = new Spell(this, spellInfo, TRIGGERED_NONE); spell->m_CastItem = item; - spell->m_cast_count = cast_count; //set count of casts + spell->m_cast_count = castId; spell->SetSpellValue(SPELLVALUE_BASE_POINT0, learning_spell_id); spell->prepare(targets); return; @@ -7984,8 +7984,8 @@ void Player::CastItemUseSpell(Item* item, SpellCastTargets const& targets, uint8 Spell* spell = new Spell(this, spellInfo, TRIGGERED_NONE); spell->m_CastItem = item; - spell->m_cast_count = cast_count; // set count of casts - spell->m_glyphIndex = glyphIndex; // glyph index + spell->m_cast_count = castId; + spell->m_glyphIndex = glyphIndex; spell->prepare(targets); return; } @@ -8014,7 +8014,7 @@ void Player::CastItemUseSpell(Item* item, SpellCastTargets const& targets, uint8 Spell* spell = new Spell(this, spellInfo, TRIGGERED_NONE); spell->m_CastItem = item; - spell->m_cast_count = cast_count; // set count of casts + spell->m_cast_count = castId; // set count of casts spell->m_glyphIndex = glyphIndex; // glyph index spell->prepare(targets); return; diff --git a/src/server/game/Entities/Player/Player.h b/src/server/game/Entities/Player/Player.h index df4d476f76..a92d70ab9b 100644 --- a/src/server/game/Entities/Player/Player.h +++ b/src/server/game/Entities/Player/Player.h @@ -2046,7 +2046,7 @@ class TC_GAME_API Player : public Unit, public GridObject void UpdateEquipSpellsAtFormChange(); void CastItemCombatSpell(DamageInfo const& damageInfo); void CastItemCombatSpell(DamageInfo const& damageInfo, Item* item, ItemTemplate const* proto); - void CastItemUseSpell(Item* item, SpellCastTargets const& targets, uint8 cast_count, uint32 glyphIndex); + void CastItemUseSpell(Item* item, SpellCastTargets const& targets, uint8 castId, uint32 glyphIndex); void SendEquipmentSetList(); void SetEquipmentSet(EquipmentSetInfo::EquipmentSetData const& eqset); diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index 8998d3a37c..a3a9a1f2cf 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -17,6 +17,7 @@ #include "Unit.h" #include "AbstractPursuer.h" +#include "Archaeology.h" #include "Battlefield.h" #include "BattlefieldMgr.h" #include "Battleground.h" @@ -485,6 +486,7 @@ void Unit::Update(uint32 p_time) } _UpdateSpells(p_time); + ProcessPendingSpellCastRequest(); // If this is set during update SetCantProc(false) call is missing somewhere in the code // Having this would prevent spells from being proced, so let's crash @@ -3031,11 +3033,11 @@ void Unit::SetCurrentCastSpell(Spell* pSpell) void Unit::InterruptSpell(CurrentSpellTypes spellType, bool withDelayed, bool withInstant, Spell* interruptingSpell /* = nullptr */) { - //TC_LOG_DEBUG("entities.unit", "Interrupt spell for unit %u.", GetEntry()); Spell* spell = m_currentSpells[spellType]; - if (spell - && (withDelayed || spell->getState() != SPELL_STATE_DELAYED) - && (withInstant || spell->GetCastTime() > 0 || spell->getState() == SPELL_STATE_CASTING)) + if (!spell) + return; + + if ((withDelayed || spell->getState() != SPELL_STATE_DELAYED) && (withInstant || spell->GetCastTime() > 0 || spell->getState() == SPELL_STATE_CASTING)) { // for example, do not let self-stun aura interrupt itself if (!spell->IsInterruptable()) @@ -9094,6 +9096,7 @@ void Unit::setDeathState(DeathState s) // remove aurastates allowing special moves ClearAllReactives(); ClearDiminishings(); + CancelPendingCastRequest(); if (IsInWorld()) { // Only clear MotionMaster for entities that exists in world @@ -14579,3 +14582,290 @@ SpellInfo const* Unit::GetCastSpellInfo(SpellInfo const* spellInfo) const return spellInfo; } + +bool Unit::CanExecutePendingSpellCastRequest(SpellInfo const* spellInfo) const +{ + // Generic and melee spells have to wait, channeled spells can be processed immediately. + if (!GetCurrentSpell(CURRENT_CHANNELED_SPELL) && HasUnitState(UNIT_STATE_CASTING)) + return false; + + // Waiting for the global cooldown to expire + if (GetSpellHistory()->GetRemainingGlobalCooldown(spellInfo) > 0) + return false; + + return true; +} + +void Unit::RequestSpellCast(PendingSpellCastRequest castRequest, SpellInfo const* spellInfo) +{ + _pendingSpellCastRequest = castRequest; + + // If we can process the cast request right now, do it. + if (CanExecutePendingSpellCastRequest(spellInfo)) + ProcessPendingSpellCastRequest(); +} + +void Unit::CancelPendingCastRequest() +{ + if (!_pendingSpellCastRequest.has_value()) + return; + + // We have to inform the client that the cast has been canceled to avoid the cast button to stay highlightened + if (IsPlayer()) + { + // @todo: convert to packet class + WorldPacket data(SMSG_CAST_FAILED, 1 + 4 + 1); + data << uint8(_pendingSpellCastRequest->CastRequest.CastID); + data << uint32(_pendingSpellCastRequest->CastRequest.SpellID); + data << uint8(SPELL_FAILED_DONT_REPORT); + ToPlayer()->SendDirectMessage(&data); + } + + _pendingSpellCastRequest.reset(); +} + +// A spell can be queued up within 400 milliseconds before global cooldown expires or the cast finishes +static constexpr uint16 const SPELL_QUEUE_TIME_WINDOW = 400; + +bool Unit::CanRequestSpellCast(SpellInfo const* spellInfo) const +{ + if (GetSpellHistory()->GetRemainingGlobalCooldown(spellInfo) > SPELL_QUEUE_TIME_WINDOW) + return false; + + for (CurrentSpellTypes spellSlot : { CURRENT_MELEE_SPELL, CURRENT_GENERIC_SPELL }) + if (Spell const* spell = GetCurrentSpell(spellSlot)) + if (spell->GetRemainingCastTime() > SPELL_QUEUE_TIME_WINDOW) + return false; + + return true; +} + +void Unit::ProcessPendingSpellCastRequest() +{ + if (!_pendingSpellCastRequest.has_value()) + return; + + // Sanity check. If the player requested an invalid spell cast, just skip the request. + SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(_pendingSpellCastRequest->CastRequest.SpellID); + if (!spellInfo) + { + _pendingSpellCastRequest.reset(); + return; + } + + // Waiting for ongoing casts to finish and the global cooldown to expire. + if (!CanExecutePendingSpellCastRequest(spellInfo)) + return; + + // Spell can be executed now. Check data and prepare cast. + Unit* caster = this; + + // Process packet provided targets + SpellCastTargets targets(caster, _pendingSpellCastRequest->CastRequest); + + // The spell cast has been requested by using an item. Handle the cast accordingly. + if (_pendingSpellCastRequest->CastItemData.has_value()) + { + ProcessItemCast(*_pendingSpellCastRequest, targets); + _pendingSpellCastRequest.reset(); + return; + } + + if (caster->IsCreature() && !caster->ToCreature()->HasSpell(spellInfo->Id)) + { + // If the vehicle creature does not have the spell but it allows the passenger to cast own spells + // change caster to player and let him cast + + Unit* charmer = caster->GetCharmer(); + if (charmer && (!charmer->IsOnVehicle(this) || spellInfo->CheckVehicle(charmer) != SPELL_CAST_OK)) + return; + + caster = charmer; + } + + if (caster->GetTypeId() == TYPEID_PLAYER && !caster->ToPlayer()->HasActiveSpell(spellInfo->Id) && !spellInfo->IsRaidMarker() && + !caster->ToPlayer()->HasArchProject(static_cast(spellInfo->ResearchProjectId))) + { + bool allow = false; + + // allow casting of unknown spells for special lock cases + if (GameObject *go = targets.GetGOTarget()) + if (go->GetSpellForLock(caster->ToPlayer()) == spellInfo) + allow = true; + + // TODO: Preparation for #23204 + // allow casting of spells triggered by clientside periodic trigger auras + /* + if (caster->HasAuraTypeWithTriggerSpell(SPELL_AURA_PERIODIC_TRIGGER_SPELL_FROM_CLIENT, spellId)) + { + allow = true; + triggerFlag = TRIGGERED_FULL_MASK; + } + */ + + if (!allow) + { + _pendingSpellCastRequest.reset(); + return; + } + } + + // Check possible spell cast overrides + spellInfo = caster->GetCastSpellInfo(spellInfo); + + // can't use our own spells when we're in possession of another unit, + if (caster->isPossessing()) + { + _pendingSpellCastRequest.reset(); + return; + } + + // Client is resending autoshot cast opcode when other spell is cast during shoot rotation + // Skip it to prevent "interrupt" message + // Also check targets! target may have changed and we need to interrupt current spell + if (spellInfo->IsAutoRepeatRangedSpell()) + { + if (Spell* spell = caster->GetCurrentSpell(CURRENT_AUTOREPEAT_SPELL)) + { + if (spell->m_spellInfo == spellInfo && spell->m_targets.GetUnitTargetGUID() == targets.GetUnitTargetGUID()) + { + _pendingSpellCastRequest.reset(); + return; + } + } + } + + // auto-selection buff level base at target level (in spellInfo) + // TODO: is this even necessary? client already seems to send correct rank for "standard" buffs + if (spellInfo->IsPositive()) + { + if (Unit* target = targets.GetUnitTarget()) + { + SpellInfo const* actualSpellInfo = spellInfo->GetAuraRankForLevel(target->getLevel()); + + // if rank not found then function return NULL but in explicit cast case original spell can be cast and later failed with appropriate error message + if (actualSpellInfo) + spellInfo = actualSpellInfo; + } + } + + if (!_pendingSpellCastRequest->CastRequest.Weight.empty()) + { + ArchData archaeologyCastData; + for (WorldPackets::Spells::SpellWeight const& weight : _pendingSpellCastRequest->CastRequest.Weight) + { + switch (weight.Type) + { + case 1: // Currency + archaeologyCastData.FragId = weight.ID; + archaeologyCastData.FragCount = weight.Quantity; + break; + case 2: // Item + archaeologyCastData.KeyId = weight.ID; + archaeologyCastData.KeyCount = weight.Quantity; + break; + default: + break; + } + } + + if (Player* player = caster->ToPlayer()) + player->SetArchData(archaeologyCastData); + } + + Spell* spell = new Spell(caster, spellInfo, TRIGGERED_NONE, ObjectGuid::Empty, false); + spell->m_cast_count = _pendingSpellCastRequest->CastRequest.CastID; // set count of casts + spell->m_glyphIndex = _pendingSpellCastRequest->CastRequest.Misc; + spell->prepare(targets); + + _pendingSpellCastRequest.reset(); +} + +void Unit::ProcessItemCast(PendingSpellCastRequest const& castRequest, SpellCastTargets const& targets) +{ + Player* player = ToPlayer(); + if (!player || !castRequest.CastItemData.has_value()) + return; + + Item* item = player->GetUseableItemByPos(castRequest.CastItemData->BagSlot, castRequest.CastItemData->Slot); + if (!item) + { + player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, nullptr, nullptr); + return; + } + + if (item->GetGUID() != castRequest.CastItemData->CastItem) + { + player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, nullptr, nullptr); + return; + } + + ItemTemplate const* proto = item->GetTemplate(); + if (!proto) + { + player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, item, nullptr); + return; + } + + // some item classes can be used only in equipped state + if (proto->GetInventoryType() != INVTYPE_NON_EQUIP && !item->IsEquipped()) + { + player->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, item, nullptr); + return; + } + + InventoryResult msg = player->CanUseItem(item); + if (msg != EQUIP_ERR_OK) + { + player->SendEquipError(msg, item, nullptr); + return; + } + + // only allow conjured consumable, bandage, poisons (all should have the 2^21 item flag set in DB) + if (proto->GetClass() == ITEM_CLASS_CONSUMABLE && !(proto->GetFlags() & ITEM_FLAG_IGNORE_DEFAULT_ARENA_RESTRICTIONS) && player->InArena()) + { + player->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH, item, nullptr); + return; + } + + // don't allow items banned in arena + if ((proto->GetFlags() & ITEM_FLAG_NOT_USEABLE_IN_ARENA) && player->InArena()) + { + player->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH, item, nullptr); + return; + } + + if (player->IsInCombat()) + { + for (ItemEffect const& effect : proto->Effects) + { + if (SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(effect.SpellID)) + { + if (!spellInfo->CanBeUsedInCombat()) + { + player->SendEquipError(EQUIP_ERR_NOT_IN_COMBAT, item, nullptr); + return; + } + } + } + } + + // check also BIND_ON_ACQUIRE and BIND_QUEST for .additem or .additemset case by GM (not binded at adding to inventory) + if (item->GetTemplate()->GetBonding() == BIND_ON_USE || item->GetTemplate()->GetBonding() == BIND_ON_ACQUIRE || item->GetTemplate()->GetBonding() == BIND_QUEST) + { + if (!item->IsSoulBound()) + { + item->SetState(ITEM_CHANGED, player); + item->SetBinding(true); + } + } + + player->RemoveAurasWithInterruptFlags(SpellAuraInterruptFlags::ItemUse); + + // Note: If script stop casting it must send appropriate data to client to prevent stuck item in gray state. + if (!sScriptMgr->OnItemUse(player, item, targets)) + { + // no script or script not process request by self + player->CastItemUseSpell(item, targets, castRequest.CastRequest.CastID, castRequest.CastRequest.Misc); + } +} diff --git a/src/server/game/Entities/Unit/Unit.h b/src/server/game/Entities/Unit/Unit.h index 11e9bd7d83..1a7afa6488 100644 --- a/src/server/game/Entities/Unit/Unit.h +++ b/src/server/game/Entities/Unit/Unit.h @@ -25,6 +25,7 @@ #include "SpellDefines.h" #include "ThreatManager.h" #include "Timer.h" +#include "SpellPacketsCommon.h" #include "UnitDefines.h" #include "Util.h" #include @@ -730,6 +731,29 @@ struct PositionUpdateInfo bool Turned = false; }; +struct SpellCastRequestItemData +{ + SpellCastRequestItemData(uint8 bagSlot, uint8 slot, ObjectGuid castItem) : + BagSlot(bagSlot), Slot(slot), CastItem(castItem) + { + } + + uint8 BagSlot = 0; + uint8 Slot = 0; + ObjectGuid CastItem; +}; + +struct PendingSpellCastRequest +{ + PendingSpellCastRequest(WorldPackets::Spells::SpellCastRequest&& castRequest, Optional castItemData = {}) : + CastRequest(castRequest), CastItemData(castItemData) + { + } + + WorldPackets::Spells::SpellCastRequest CastRequest; + Optional CastItemData; +}; + // delay time next attack to prevent client attack animation problems #define ATTACK_DISPLAY_DELAY 200 #define MAX_PLAYER_STEALTH_DETECT_RANGE 30.0f // max distance for detection targets by player @@ -1736,6 +1760,11 @@ class TC_GAME_API Unit : public WorldObject // enables/disables combat permission of this unit void SetIgnoringCombat(bool apply) { _isIgnoringCombat = apply; } + // Queues up a spell cast request that has been received via packet and processes it whenever possible. + void RequestSpellCast(PendingSpellCastRequest castRequest, SpellInfo const* spellInfo); + void CancelPendingCastRequest(); + bool CanRequestSpellCast(SpellInfo const* spell) const; + protected: explicit Unit (bool isWorldObject); @@ -1880,6 +1909,11 @@ class TC_GAME_API Unit : public WorldObject std::array, AsUnderlyingType(PursuingType::Max)> _unitsPursuingMe; bool _isIgnoringCombat; + + Optional _pendingSpellCastRequest; + void ProcessPendingSpellCastRequest(); + void ProcessItemCast(PendingSpellCastRequest const& castRequest, SpellCastTargets const& targets); + bool CanExecutePendingSpellCastRequest(SpellInfo const* spellInfo) const; }; namespace Trinity diff --git a/src/server/game/Handlers/SpellHandler.cpp b/src/server/game/Handlers/SpellHandler.cpp index 340bf75689..4d8e871831 100644 --- a/src/server/game/Handlers/SpellHandler.cpp +++ b/src/server/game/Handlers/SpellHandler.cpp @@ -88,116 +88,20 @@ void WorldSession::HandleClientCastFlags(WorldPacket& recvPacket, uint8 castFlag } } -void WorldSession::HandleUseItemOpcode(WorldPacket& recvPacket) +void WorldSession::HandleUseItemOpcode(WorldPackets::Spells::UseItem& packet) { - /// @todo add targets.read() check - Player* pUser = _player; + Player* user = _player; // ignore for remote control state - if (pUser->m_unitMovedByMe != pUser) + if (user->m_unitMovedByMe != user) return; - uint8 bagIndex, slot, castFlags; - uint8 castCount; // next cast if exists (single or not) - ObjectGuid itemGUID; - uint32 glyphIndex; // something to do with glyphs? - uint32 spellId; // cast spell id - - recvPacket >> bagIndex >> slot >> castCount >> spellId >> itemGUID >> glyphIndex >> castFlags; - - if (glyphIndex >= MAX_GLYPH_SLOT_INDEX) - { - pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, nullptr, nullptr); + SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(packet.Cast.SpellID); + if (!spellInfo) return; - } - Item* pItem = pUser->GetUseableItemByPos(bagIndex, slot); - if (!pItem) - { - pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, nullptr, nullptr); - return; - } - - if (pItem->GetGUID() != itemGUID) - { - pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, nullptr, nullptr); - return; - } - - TC_LOG_DEBUG("network", "WORLD: CMSG_USE_ITEM packet, bagIndex: %u, slot: %u, castCount: %u, spellId: %u, Item: %u, glyphIndex: %u, data length = %i", bagIndex, slot, castCount, spellId, pItem->GetEntry(), glyphIndex, (uint32)recvPacket.size()); - - ItemTemplate const* proto = pItem->GetTemplate(); - if (!proto) - { - pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, nullptr); - return; - } - - // some item classes can be used only in equipped state - if (proto->GetInventoryType() != INVTYPE_NON_EQUIP && !pItem->IsEquipped()) - { - pUser->SendEquipError(EQUIP_ERR_ITEM_NOT_FOUND, pItem, nullptr); - return; - } - - InventoryResult msg = pUser->CanUseItem(pItem); - if (msg != EQUIP_ERR_OK) - { - pUser->SendEquipError(msg, pItem, nullptr); - return; - } - - // only allow conjured consumable, bandage, poisons (all should have the 2^21 item flag set in DB) - if (proto->GetClass() == ITEM_CLASS_CONSUMABLE && !(proto->GetFlags() & ITEM_FLAG_IGNORE_DEFAULT_ARENA_RESTRICTIONS) && pUser->InArena()) - { - pUser->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH, pItem, nullptr); - return; - } - - // don't allow items banned in arena - if ((proto->GetFlags() & ITEM_FLAG_NOT_USEABLE_IN_ARENA) && pUser->InArena()) - { - pUser->SendEquipError(EQUIP_ERR_NOT_DURING_ARENA_MATCH, pItem, nullptr); - return; - } - - if (pUser->IsInCombat()) - { - for (ItemEffect const& effect : proto->Effects) - { - if (SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(effect.SpellID)) - { - if (!spellInfo->CanBeUsedInCombat()) - { - pUser->SendEquipError(EQUIP_ERR_NOT_IN_COMBAT, pItem, nullptr); - return; - } - } - } - } - - // check also BIND_ON_ACQUIRE and BIND_QUEST for .additem or .additemset case by GM (not binded at adding to inventory) - if (pItem->GetTemplate()->GetBonding() == BIND_ON_USE || pItem->GetTemplate()->GetBonding() == BIND_ON_ACQUIRE || pItem->GetTemplate()->GetBonding() == BIND_QUEST) - { - if (!pItem->IsSoulBound()) - { - pItem->SetState(ITEM_CHANGED, pUser); - pItem->SetBinding(true); - } - } - - pUser->RemoveAurasWithInterruptFlags(SpellAuraInterruptFlags::ItemUse); - - SpellCastTargets targets; - targets.Read(recvPacket, pUser); - HandleClientCastFlags(recvPacket, castFlags, targets); - - // Note: If script stop casting it must send appropriate data to client to prevent stuck item in gray state. - if (!sScriptMgr->OnItemUse(pUser, pItem, targets)) - { - // no script or script not process request by self - pUser->CastItemUseSpell(pItem, targets, castCount, glyphIndex); - } + if (user->CanRequestSpellCast(spellInfo)) + user->RequestSpellCast(PendingSpellCastRequest(std::move(packet.Cast), SpellCastRequestItemData(packet.PackSlot, packet.Slot, packet.CastItem)), spellInfo); } void WorldSession::HandleOpenItemOpcode(WorldPacket& recvPacket) @@ -360,120 +264,31 @@ void WorldSession::HandleCastSpellOpcode(WorldPackets::Spells::CastSpell& cast) { // ignore for remote control state (for player case) Unit* mover = _player->m_unitMovedByMe; - if (mover != _player && mover->GetTypeId() == TYPEID_PLAYER) + if (mover != _player && mover->IsPlayer()) return; SpellInfo const* spellInfo = sSpellMgr->GetSpellInfo(cast.Cast.SpellID); - if (!spellInfo) + if (!spellInfo || spellInfo->IsPassive()) return; - if (spellInfo->IsPassive()) - return; - - Unit* caster = mover; - if (caster->GetTypeId() == TYPEID_UNIT && !caster->ToCreature()->HasSpell(spellInfo->Id)) - { - // If the vehicle creature does not have the spell but it allows the passenger to cast own spells - // change caster to player and let him cast - if (!_player->IsOnVehicle(caster) || spellInfo->CheckVehicle(_player) != SPELL_CAST_OK) - return; - - caster = _player; - } - - // client provided targets - SpellCastTargets targets(caster, cast.Cast); - - if (caster->GetTypeId() == TYPEID_PLAYER && !caster->ToPlayer()->HasActiveSpell(spellInfo->Id) && !spellInfo->IsRaidMarker() && - !caster->ToPlayer()->HasArchProject(static_cast(spellInfo->ResearchProjectId))) - { - bool allow = false; - - // allow casting of unknown spells for special lock cases - if (GameObject *go = targets.GetGOTarget()) - if (go->GetSpellForLock(caster->ToPlayer()) == spellInfo) - allow = true; - - // TODO: Preparation for #23204 - // allow casting of spells triggered by clientside periodic trigger auras - /* - if (caster->HasAuraTypeWithTriggerSpell(SPELL_AURA_PERIODIC_TRIGGER_SPELL_FROM_CLIENT, spellId)) - { - allow = true; - triggerFlag = TRIGGERED_FULL_MASK; - } - */ - - if (!allow) - return; - } - - // Check possible spell cast overrides - spellInfo = caster->GetCastSpellInfo(spellInfo); - - // can't use our own spells when we're in possession of another unit, - if (_player->isPossessing()) - return; - - // Client is resending autoshot cast opcode when other spell is cast during shoot rotation - // Skip it to prevent "interrupt" message - // Also check targets! target may have changed and we need to interrupt current spell - if (spellInfo->IsAutoRepeatRangedSpell()) - if (Spell* spell = caster->GetCurrentSpell(CURRENT_AUTOREPEAT_SPELL)) - if (spell->m_spellInfo == spellInfo && spell->m_targets.GetUnitTargetGUID() == targets.GetUnitTargetGUID()) - return; - - // auto-selection buff level base at target level (in spellInfo) - // TODO: is this even necessary? client already seems to send correct rank for "standard" buffs - if (spellInfo->IsPositive()) - if (Unit* target = targets.GetUnitTarget()) - { - SpellInfo const* actualSpellInfo = spellInfo->GetAuraRankForLevel(target->getLevel()); - - // if rank not found then function return NULL but in explicit cast case original spell can be cast and later failed with appropriate error message - if (actualSpellInfo) - spellInfo = actualSpellInfo; - } - - if (!cast.Cast.Weight.empty()) - { - ArchData archaeologyCastData; - for (WorldPackets::Spells::SpellWeight& weight : cast.Cast.Weight) - { - switch (weight.Type) - { - case 1: // Currency - archaeologyCastData.FragId = weight.ID; - archaeologyCastData.FragCount = weight.Quantity; - break; - case 2: // Item - archaeologyCastData.KeyId = weight.ID; - archaeologyCastData.KeyCount = weight.Quantity; - break; - default: - break; - } - } - - if (Player* player = GetPlayer()) - player->SetArchData(archaeologyCastData); - } - - Spell* spell = new Spell(caster, spellInfo, TRIGGERED_NONE, ObjectGuid::Empty, false); - spell->m_cast_count = cast.Cast.CastID; // set count of casts - spell->m_glyphIndex = cast.Cast.Misc; - spell->prepare(targets); + if (mover->CanRequestSpellCast(spellInfo)) + mover->RequestSpellCast(PendingSpellCastRequest(std::move(cast.Cast)), spellInfo); + else if (mover->IsPlayer()) + Spell::SendCastResult(mover->ToPlayer(), spellInfo, cast.Cast.CastID, SPELL_FAILED_DONT_REPORT); } void WorldSession::HandleCancelCastOpcode(WorldPacket& recvPacket) { uint32 spellId = 0; + uint8 castId = 0; - recvPacket.read_skip(); // counter, increments with every CANCEL packet, don't use for now + recvPacket >> castId; recvPacket >> spellId; if (_player->IsNonMeleeSpellCast(false)) _player->InterruptNonMeleeSpells(false, spellId, false); + + _player->CancelPendingCastRequest(); } void WorldSession::HandleCancelAuraOpcode(WorldPacket& recvPacket) @@ -783,3 +598,12 @@ void WorldSession::HandleRequestCategoryCooldowns(WorldPacket& /*recvPacket*/) { _player->SendSpellCategoryCooldowns(); } + +void WorldSession::HandleCancelQueuedSpellOpcode(WorldPackets::Spells::CancelQueuedSpell& /*cancelQueuedSpell*/) +{ + Unit* mover = _player->m_unitMovedByMe; + if (mover != _player && mover->IsPlayer()) + return; + + mover->CancelPendingCastRequest(); +} diff --git a/src/server/game/Server/Packets/SpellPackets.cpp b/src/server/game/Server/Packets/SpellPackets.cpp index faaa7e393b..ffa29913c0 100644 --- a/src/server/game/Server/Packets/SpellPackets.cpp +++ b/src/server/game/Server/Packets/SpellPackets.cpp @@ -788,3 +788,36 @@ void WorldPackets::Spells::CastSpell::Read() { _worldPacket >> Cast; } + +void WorldPackets::Spells::UseItem::Read() +{ + _worldPacket >> PackSlot; + _worldPacket >> Slot; + _worldPacket >> Cast.CastID; + _worldPacket >> Cast.SpellID; + _worldPacket >> CastItem; + _worldPacket >> Cast.Misc; + _worldPacket >> Cast.SendCastFlags; + _worldPacket >> Cast.Target; + + if (Cast.SendCastFlags & CAST_FLAG_HAS_TRAJECTORY) + { + _worldPacket >> Cast.MissileTrajectory; + bool hasMovementData = _worldPacket.read(); + if (hasMovementData) + _worldPacket >> Cast.MoveUpdate; + } + + if (Cast.SendCastFlags & CAST_FLAG_HAS_WEIGHT) + { + uint32 weightCount = _worldPacket.read(); + Cast.Weight.resize(weightCount); + + for (WorldPackets::Spells::SpellWeight& weight : Cast.Weight) + { + _worldPacket >> weight.Type; + _worldPacket >> weight.ID; + _worldPacket >> weight.Quantity; + } + } +} diff --git a/src/server/game/Server/Packets/SpellPackets.h b/src/server/game/Server/Packets/SpellPackets.h index f9780fe93d..8a2ca65d5f 100644 --- a/src/server/game/Server/Packets/SpellPackets.h +++ b/src/server/game/Server/Packets/SpellPackets.h @@ -19,11 +19,7 @@ #define SpellPackets_h__ #include "Packet.h" -#include "MovementInfo.h" -#include "ObjectGuid.h" -#include "Optional.h" -#include "Position.h" -#include "SharedDefines.h" +#include "SpellPacketsCommon.h" #include @@ -66,27 +62,11 @@ namespace WorldPackets uint32 Value = 0; }; - struct TargetLocation - { - ObjectGuid Transport; - Position Location; - }; - struct ProjectileVisual { std::array Id = { }; }; - struct SpellTargetData - { - uint32 Flags = 0; - Optional Unit; - Optional Item; - Optional SrcLocation; - Optional DstLocation; - Optional Name; - }; - struct SpellHealPrediction { Optional BeaconGUID; @@ -479,31 +459,6 @@ namespace WorldPackets int32 ActualDelay = 0; }; - struct MissileTrajectoryRequest - { - float Pitch = 0.0f; - float Speed = 0.0f; - }; - - struct SpellWeight - { - uint8 Type = 0; - int32 ID = 0; - uint32 Quantity = 0; - }; - - struct SpellCastRequest - { - uint8 CastID = 0; - uint8 SendCastFlags = 0; - int32 SpellID = 0; - int32 Misc = 0; - SpellTargetData Target; - MissileTrajectoryRequest MissileTrajectory; - Optional MoveUpdate; - std::vector Weight; - }; - class CastSpell final : public ClientPacket { public: @@ -513,6 +468,27 @@ namespace WorldPackets SpellCastRequest Cast; }; + + class CancelQueuedSpell final : public ClientPacket + { + public: + CancelQueuedSpell(WorldPacket&& packet) : ClientPacket(CMSG_CANCEL_QUEUED_SPELL, std::move(packet)) { } + + void Read() override { } + }; + + class UseItem final : public ClientPacket + { + public: + UseItem(WorldPacket&& packet) : ClientPacket(CMSG_USE_ITEM, std::move(packet)) { } + + void Read() override; + + uint8 PackSlot = 0; + uint8 Slot = 0; + ObjectGuid CastItem; + SpellCastRequest Cast; + }; } } diff --git a/src/server/game/Server/Packets/SpellPacketsCommon.h b/src/server/game/Server/Packets/SpellPacketsCommon.h new file mode 100644 index 0000000000..928fc437cd --- /dev/null +++ b/src/server/game/Server/Packets/SpellPacketsCommon.h @@ -0,0 +1,75 @@ +/* +* 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 . +*/ + +#ifndef SpellPacketsCommon_h__ +#define SpellPacketsCommon_h__ + +#include "ObjectGuid.h" +#include "Optional.h" +#include "MovementInfo.h" +#include "Position.h" +#include "SharedDefines.h" +#include + +namespace WorldPackets +{ + namespace Spells + { + struct TargetLocation + { + ObjectGuid Transport; + Position Location; + }; + + struct SpellTargetData + { + uint32 Flags = 0; + Optional Unit; + Optional Item; + Optional SrcLocation; + Optional DstLocation; + Optional Name; + }; + + struct MissileTrajectoryRequest + { + float Pitch = 0.0f; + float Speed = 0.0f; + }; + + struct SpellWeight + { + uint8 Type = 0; + int32 ID = 0; + uint32 Quantity = 0; + }; + + struct SpellCastRequest + { + uint8 CastID = 0; + uint8 SendCastFlags = 0; + int32 SpellID = 0; + int32 Misc = 0; + SpellTargetData Target; + MissileTrajectoryRequest MissileTrajectory; + Optional MoveUpdate; + std::vector Weight; + }; + } +} + +#endif diff --git a/src/server/game/Server/Protocol/Opcodes.cpp b/src/server/game/Server/Protocol/Opcodes.cpp index fd1a42a204..034a1e59ee 100644 --- a/src/server/game/Server/Protocol/Opcodes.cpp +++ b/src/server/game/Server/Protocol/Opcodes.cpp @@ -226,7 +226,7 @@ void OpcodeTable::Initialize() DEFINE_HANDLER(CMSG_CANCEL_CHANNELLING, STATUS_LOGGEDIN, PROCESS_INPLACE, &WorldSession::HandleCancelChanneling ); DEFINE_HANDLER(CMSG_CANCEL_GROWTH_AURA, STATUS_LOGGEDIN, PROCESS_THREADUNSAFE, &WorldSession::HandleCancelGrowthAuraOpcode ); DEFINE_HANDLER(CMSG_CANCEL_MOUNT_AURA, STATUS_LOGGEDIN, PROCESS_INPLACE, &WorldSession::HandleCancelMountAuraOpcode ); - DEFINE_HANDLER(CMSG_CANCEL_QUEUED_SPELL, STATUS_UNHANDLED, PROCESS_INPLACE, &WorldSession::Handle_NULL ); + DEFINE_HANDLER(CMSG_CANCEL_QUEUED_SPELL, STATUS_LOGGEDIN, PROCESS_INPLACE, &WorldSession::HandleCancelQueuedSpellOpcode ); DEFINE_HANDLER(CMSG_CANCEL_TEMP_ENCHANTMENT, STATUS_LOGGEDIN, PROCESS_INPLACE, &WorldSession::HandleCancelTempEnchantmentOpcode); DEFINE_HANDLER(CMSG_CANCEL_TRADE, STATUS_LOGGEDIN_OR_RECENTLY_LOGGOUT, PROCESS_THREADUNSAFE, &WorldSession::HandleCancelTradeOpcode); DEFINE_HANDLER(CMSG_CAST_SPELL, STATUS_LOGGEDIN, PROCESS_THREADSAFE, &WorldSession::HandleCastSpellOpcode ); diff --git a/src/server/game/Server/WorldSession.h b/src/server/game/Server/WorldSession.h index 91b2138897..74d5d49501 100644 --- a/src/server/game/Server/WorldSession.h +++ b/src/server/game/Server/WorldSession.h @@ -198,6 +198,8 @@ namespace WorldPackets namespace Spells { class CastSpell; + class CancelQueuedSpell; + class UseItem; } namespace Who @@ -921,11 +923,12 @@ class TC_GAME_API WorldSession void HandleAttackStopOpcode(WorldPacket& recvPacket); void HandleSetSheathedOpcode(WorldPacket& recvPacket); - void HandleUseItemOpcode(WorldPacket& recvPacket); + void HandleUseItemOpcode(WorldPackets::Spells::UseItem& packet); void HandleOpenItemOpcode(WorldPacket& recvPacket); void HandleOpenWrappedItemCallback(uint16 pos, ObjectGuid itemGuid, PreparedQueryResult result); void HandleCastSpellOpcode(WorldPackets::Spells::CastSpell& castRequest); void HandleCancelCastOpcode(WorldPacket& recvPacket); + void HandleCancelQueuedSpellOpcode(WorldPackets::Spells::CancelQueuedSpell& cancelQueuedSpell); void HandleCancelAuraOpcode(WorldPacket& recvPacket); void HandleCancelGrowthAuraOpcode(WorldPacket& recvPacket); void HandleCancelAutoRepeatSpellOpcode(WorldPacket& recvPacket); diff --git a/src/server/game/Spells/Spell.h b/src/server/game/Spells/Spell.h index 3283f85333..8b6f3ebfb4 100644 --- a/src/server/game/Spells/Spell.h +++ b/src/server/game/Spells/Spell.h @@ -534,6 +534,7 @@ class TC_GAME_API Spell UsedSpellMods m_appliedMods; int32 GetCastTime() const { return m_casttime; } + int32 GetRemainingCastTime() const { return m_timer; } bool IsAutoRepeat() const { return m_autoRepeat; } void SetAutoRepeat(bool rep) { m_autoRepeat = rep; } void ReSetTimer() { m_timer = m_casttime > 0 ? m_casttime : 0; } diff --git a/src/server/game/Spells/SpellHistory.cpp b/src/server/game/Spells/SpellHistory.cpp index 78dab009b9..f264acc066 100644 --- a/src/server/game/Spells/SpellHistory.cpp +++ b/src/server/game/Spells/SpellHistory.cpp @@ -639,6 +639,22 @@ void SpellHistory::CancelGlobalCooldown(SpellInfo const* spellInfo) _globalCooldowns[spellInfo->StartRecoveryCategory] = Clock::time_point(Clock::duration(0)); } +uint32 SpellHistory::GetRemainingGlobalCooldown(SpellInfo const* spellInfo) const +{ + Clock::time_point end; + auto cdItr = _globalCooldowns.find(spellInfo->StartRecoveryCategory); + if (cdItr == _globalCooldowns.end()) + return 0; + + end = cdItr->second; + Clock::time_point now = GameTime::GetGameTimeSystemPoint(); + if (end < now) + return 0; + + Clock::duration remaining = end - now; + return uint32(std::chrono::duration_cast(remaining).count()); +} + Player* SpellHistory::GetPlayerOwner() const { return _owner->GetCharmerOrOwnerPlayerOrPlayerItself(); diff --git a/src/server/game/Spells/SpellHistory.h b/src/server/game/Spells/SpellHistory.h index 8d016f303e..c9a4974394 100644 --- a/src/server/game/Spells/SpellHistory.h +++ b/src/server/game/Spells/SpellHistory.h @@ -137,6 +137,7 @@ public: bool HasGlobalCooldown(SpellInfo const* spellInfo) const; void AddGlobalCooldown(SpellInfo const* spellInfo, uint32 duration); void CancelGlobalCooldown(SpellInfo const* spellInfo); + uint32 GetRemainingGlobalCooldown(SpellInfo const* spellInfo) const; void BuildCooldownPacket(WorldPacket& data, uint8 flags, uint32 spellId, uint32 cooldown) const;