diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h index 8745cd4abe..c1d2aa8fbf 100644 --- a/src/common/Utilities/Containers.h +++ b/src/common/Utilities/Containers.h @@ -272,6 +272,77 @@ namespace Trinity ++itr; } } + + namespace Impl + { + template + void EraseIfMoveAssignable(Container& c, Predicate p) + { + auto wpos = c.begin(); + for (auto rpos = c.begin(), end = c.end(); rpos != end; ++rpos) + { + if (!p(*rpos)) + { + if (rpos != wpos) + std::swap(*rpos, *wpos); + ++wpos; + } + } + c.erase(wpos, c.end()); + } + + template + void EraseIfNotMoveAssignable(Container& c, Predicate p) + { + for (auto it = c.begin(); it != c.end();) + { + if (p(*it)) + it = c.erase(it); + else + ++it; + } + } + } + + template + void EraseIf(Container& c, Predicate p) + { + if constexpr (std::is_move_assignable_v) + Impl::EraseIfMoveAssignable(c, std::ref(p)); + else + Impl::EraseIfNotMoveAssignable(c, std::ref(p)); + } + + /** + * Returns a mutable reference to element at index i + * Will resize vector if neccessary to ensure element at i can be safely written + * + * This exists as separate overload instead of one function with default argument to allow using + * with vectors of non-default-constructible classes + */ + template + inline decltype(auto) EnsureWritableVectorIndex(std::vector& vec, typename std::vector::size_type i) + { + if (i >= vec.size()) + vec.resize(i + 1); + + return vec[i]; + } + + /** + * Returns a mutable reference to element at index i + * Will resize vector if neccessary to ensure element at i can be safely written + * + * This overload allows specifying what value to pad vector with during .resize + */ + template + inline decltype(auto) EnsureWritableVectorIndex(std::vector& vec, typename std::vector::size_type i, T const& resizeDefault) + { + if (i >= vec.size()) + vec.resize(i + 1, resizeDefault); + + return vec[i]; + } } //! namespace Containers } diff --git a/src/server/game/OutdoorPvP/OutdoorPvP.cpp b/src/server/game/OutdoorPvP/OutdoorPvP.cpp index 3e13b7de31..07052034c9 100644 --- a/src/server/game/OutdoorPvP/OutdoorPvP.cpp +++ b/src/server/game/OutdoorPvP/OutdoorPvP.cpp @@ -434,6 +434,26 @@ void OutdoorPvP::BroadcastPacket(WorldPacket &data) const player->SendDirectMessage(&data); } +void OutdoorPvP::AddCapturePoint(OPvPCapturePoint* cp) +{ + OPvPCapturePointMap::iterator i = m_capturePoints.find(cp->m_capturePointSpawnId); + if (i != m_capturePoints.end()) + { + TC_LOG_ERROR("outdoorpvp", "OutdoorPvP::AddCapturePoint: CapturePoint {} already exists!", cp->m_capturePointSpawnId); + if (i->second.get() == cp) + return; + } + m_capturePoints[cp->m_capturePointSpawnId].reset(cp); +} + +OPvPCapturePoint* OutdoorPvP::GetCapturePoint(ObjectGuid::LowType guid) const +{ + OutdoorPvP::OPvPCapturePointMap::const_iterator itr = m_capturePoints.find(guid); + if (itr != m_capturePoints.end()) + return itr->second.get(); + return nullptr; +} + void OutdoorPvP::RegisterZone(uint32 zoneId) { sOutdoorPvPMgr->AddZone(zoneId, this); diff --git a/src/server/game/OutdoorPvP/OutdoorPvP.h b/src/server/game/OutdoorPvP/OutdoorPvP.h index 45fc50d470..e2c0c850e0 100644 --- a/src/server/game/OutdoorPvP/OutdoorPvP.h +++ b/src/server/game/OutdoorPvP/OutdoorPvP.h @@ -23,6 +23,7 @@ #include "SharedDefines.h" #include "ZoneScript.h" #include +#include namespace WorldPackets { @@ -169,7 +170,8 @@ class TC_GAME_API OutdoorPvP : public ZoneScript // dtor virtual ~OutdoorPvP(); - typedef std::map OPvPCapturePointMap; + typedef std::map> OPvPCapturePointMap; + typedef std::pair GoScriptPair; typedef std::pair CreatureScriptPair; @@ -251,19 +253,9 @@ class TC_GAME_API OutdoorPvP : public ZoneScript virtual void HandlePlayerResurrects(Player* player, uint32 zone); - void AddCapturePoint(OPvPCapturePoint* cp) - { - m_capturePoints[cp->m_capturePointSpawnId] = cp; - } + void AddCapturePoint(OPvPCapturePoint* cp); - OPvPCapturePoint* GetCapturePoint(ObjectGuid::LowType spawnId) const - { - OutdoorPvP::OPvPCapturePointMap::const_iterator itr = m_capturePoints.find(spawnId); - if (itr != m_capturePoints.end()) - return itr->second; - - return nullptr; - } + OPvPCapturePoint* GetCapturePoint(ObjectGuid::LowType spawnId) const; void RegisterZone(uint32 zoneid); diff --git a/src/server/game/OutdoorPvP/OutdoorPvPMgr.cpp b/src/server/game/OutdoorPvP/OutdoorPvPMgr.cpp index 3372b04449..9729254100 100644 --- a/src/server/game/OutdoorPvP/OutdoorPvPMgr.cpp +++ b/src/server/game/OutdoorPvP/OutdoorPvPMgr.cpp @@ -16,6 +16,7 @@ */ #include "OutdoorPvPMgr.h" +#include "Containers.h" #include "DatabaseEnv.h" #include "DisableMgr.h" #include "Log.h" @@ -118,6 +119,10 @@ void OutdoorPvPMgr::CreateOutdoorPvPForMap(Map* map) void OutdoorPvPMgr::DestroyOutdoorPvPForMap(Map* map) { + Trinity::Containers::EraseIf(m_OutdoorPvPMap, [map](OutdoorPvPMap::value_type const& pair) + { + return pair.first.first == map; + }); m_OutdoorPvPByMap.erase(map); }