From ffd85f9139119ec56a8a6f3add4ef4c0f047b838 Mon Sep 17 00:00:00 2001 From: Giacomo Pozzoni Date: Thu, 6 Aug 2020 12:58:39 +0000 Subject: Scripts/Comamnds: Improve ".debug objectcount" command (#25208) * Scripts/Comamnds: Improve ".debug objectcount" command Include the top 5 most common creatures in the map * Use C++ features to copy data * Fix build warnings * Update src/server/scripts/Commands/cs_debug.cpp Co-authored-by: Shauren * Update src/server/scripts/Commands/cs_debug.cpp Co-authored-by: Shauren * Update src/server/scripts/Commands/cs_debug.cpp Co-authored-by: Shauren * Move CreatureCountWorker out of function and use template for unhandled cases * Code cleanup Co-authored-by: Shauren (cherry picked from commit bd5e832a648aadf1d1be94f913ea00b67398f249) --- src/server/scripts/Commands/cs_debug.cpp | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/server/scripts/Commands') diff --git a/src/server/scripts/Commands/cs_debug.cpp b/src/server/scripts/Commands/cs_debug.cpp index 86400739bc7..7f1dc590e4c 100644 --- a/src/server/scripts/Commands/cs_debug.cpp +++ b/src/server/scripts/Commands/cs_debug.cpp @@ -1867,12 +1867,57 @@ public: return true; } + class CreatureCountWorker + { + public: + CreatureCountWorker() { } + + void Visit(std::unordered_map& creatureMap) + { + for (auto const& p : creatureMap) + { + uint32& count = creatureIds[p.second->GetEntry()]; + ++count; + } + } + + template + void Visit(std::unordered_map&) { } + + std::vector> GetTopCreatureCount(uint32 count) + { + auto comp = [](std::pair const& a, std::pair const& b) + { + return a.second > b.second; + }; + std::set, decltype(comp)> set(creatureIds.begin(), creatureIds.end(), comp); + + count = std::min(count, uint32(set.size())); + std::vector> result(count); + std::copy_n(set.begin(), count, result.begin()); + + return result; + } + + private: + std::unordered_map creatureIds; + }; + static void HandleDebugObjectCountMap(ChatHandler* handler, Map* map) { handler->PSendSysMessage("Map Id: %u Name: '%s' Instance Id: %u Creatures: " UI64FMTD " GameObjects: " UI64FMTD, map->GetId(), map->GetMapName(), map->GetInstanceId(), uint64(map->GetObjectsStore().Size()), uint64(map->GetObjectsStore().Size())); + + CreatureCountWorker worker; + TypeContainerVisitor visitor(worker); + visitor.Visit(map->GetObjectsStore()); + + handler->PSendSysMessage("Top Creatures count:"); + + for (auto&& p : worker.GetTopCreatureCount(5)) + handler->PSendSysMessage("Entry: %u Count: %u", p.first, p.second); } static bool HandleDebugDummyCommand(ChatHandler* handler, CommandArgs* /*args*/) -- cgit v1.2.3