From 47c77826c34b53860bd19977f070cb571c075a2b Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Tue, 6 Aug 2024 22:29:04 +0300 Subject: [PATCH] Visual logger map texts --- .../Analyzers/DangerHitMapAnalyzer.cpp | 32 +++++++++ client/CMakeLists.txt | 4 ++ client/battle/BattleOverlayLogVisualizer.cpp | 33 +++++++++ client/battle/BattleOverlayLogVisualizer.h | 28 ++++++++ client/battle/BattleRenderer.cpp | 27 +------- client/mapView/MapOverlayLogVisualizer.cpp | 68 +++++++++++++++++++ client/mapView/MapOverlayLogVisualizer.h | 33 +++++++++ client/mapView/MapView.cpp | 42 +----------- lib/logging/VisualLogger.cpp | 15 +++- lib/logging/VisualLogger.h | 12 +++- 10 files changed, 228 insertions(+), 66 deletions(-) create mode 100644 client/battle/BattleOverlayLogVisualizer.cpp create mode 100644 client/battle/BattleOverlayLogVisualizer.h create mode 100644 client/mapView/MapOverlayLogVisualizer.cpp create mode 100644 client/mapView/MapOverlayLogVisualizer.h diff --git a/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp b/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp index 6d11d01ba..50ae3fdcc 100644 --- a/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp +++ b/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp @@ -13,6 +13,7 @@ #include "../Engine/Nullkiller.h" #include "../pforeach.h" #include "../../../lib/CRandomGenerator.h" +#include "../../../lib/logging/VisualLogger.h" namespace NKAI { @@ -24,6 +25,35 @@ double HitMapInfo::value() const return danger / std::sqrt(turn / 3.0f + 1); } +void logHitmap(PlayerColor playerID, DangerHitMapAnalyzer & data) +{ +#if NKAI_TRACE_LEVEL >= 1 + logVisual->updateWithLock(playerID.toString() + ".danger.max", [&data](IVisualLogBuilder & b) + { + foreach_tile_pos([&b, &data](const int3 & pos) + { + auto & treat = data.getTileThreat(pos).maximumDanger; + b.addText(pos, std::to_string(treat.danger)); + + if(treat.hero.validAndSet()) + b.addText(pos, treat.hero->getNameTranslated()); + }); + }); + + logVisual->updateWithLock(playerID.toString() + ".danger.fast", [&data](IVisualLogBuilder & b) + { + foreach_tile_pos([&b, &data](const int3 & pos) + { + auto & treat = data.getTileThreat(pos).fastestDanger; + b.addText(pos, std::to_string(treat.danger)); + + if(treat.hero.validAndSet()) + b.addText(pos, treat.hero->getNameTranslated()); + }); + }); +#endif +} + void DangerHitMapAnalyzer::updateHitMap() { if(hitMapUpToDate) @@ -144,6 +174,8 @@ void DangerHitMapAnalyzer::updateHitMap() } logAi->trace("Danger hit map updated in %ld", timeElapsed(start)); + + logHitmap(ai->playerID, *this); } void DangerHitMapAnalyzer::calculateTileOwners() diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 9630a478c..6ab2a19a6 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -27,6 +27,7 @@ set(client_SRCS battle/BattleStacksController.cpp battle/BattleWindow.cpp battle/CreatureAnimation.cpp + battle/BattleOverlayLogVisualizer.cpp eventsSDL/NotificationHandler.cpp eventsSDL/InputHandler.cpp @@ -74,6 +75,7 @@ set(client_SRCS mapView/MapViewController.cpp mapView/MapViewModel.cpp mapView/mapHandler.cpp + mapView/MapOverlayLogVisualizer.cpp media/CAudioBase.cpp media/CMusicHandler.cpp @@ -214,6 +216,7 @@ set(client_HEADERS battle/BattleStacksController.h battle/BattleWindow.h battle/CreatureAnimation.h + battle/BattleOverlayLogVisualizer.h eventsSDL/NotificationHandler.h eventsSDL/InputHandler.h @@ -266,6 +269,7 @@ set(client_HEADERS mapView/MapViewController.h mapView/MapViewModel.h mapView/mapHandler.h + mapView/MapOverlayLogVisualizer.h media/CAudioBase.h media/CEmptyVideoPlayer.h diff --git a/client/battle/BattleOverlayLogVisualizer.cpp b/client/battle/BattleOverlayLogVisualizer.cpp new file mode 100644 index 000000000..c4aee9f50 --- /dev/null +++ b/client/battle/BattleOverlayLogVisualizer.cpp @@ -0,0 +1,33 @@ +/* + * BattleOverlayLogVisualizer.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#include "StdInc.h" +#include "BattleOverlayLogVisualizer.h" +#include "BattleInterface.h" +#include "BattleFieldController.h" + +#include "../render/Canvas.h" +#include "../render/Colors.h" +#include "../render/EFont.h" +#include "../gui/TextAlignment.h" + +BattleOverlayLogVisualizer::BattleOverlayLogVisualizer( + BattleRenderer::RendererRef & target, + BattleInterface & owner) + : target(target), owner(owner) +{ +} + +void BattleOverlayLogVisualizer::drawText(BattleHex hex, std::vector texts) +{ + const Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20); + + target.drawText(offset, EFonts::FONT_TINY, Colors::RED, ETextAlignment::TOPCENTER, texts); +} diff --git a/client/battle/BattleOverlayLogVisualizer.h b/client/battle/BattleOverlayLogVisualizer.h new file mode 100644 index 000000000..c854c6fc4 --- /dev/null +++ b/client/battle/BattleOverlayLogVisualizer.h @@ -0,0 +1,28 @@ +/* + * BattleOverlayLogVisualizer.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ +#pragma once + +#include "../../lib/logging/VisualLogger.h" +#include "BattleRenderer.h" + +class Canvas; +class BattleInterface; + +class BattleOverlayLogVisualizer : public IBattleOverlayLogVisualizer +{ +private: + BattleRenderer::RendererRef & target; + BattleInterface & owner; + +public: + BattleOverlayLogVisualizer(BattleRenderer::RendererRef & target, BattleInterface & owner); + + void drawText(BattleHex hex, std::vector texts) override; +}; diff --git a/client/battle/BattleRenderer.cpp b/client/battle/BattleRenderer.cpp index 6c6e7c5ea..fde76b1c3 100644 --- a/client/battle/BattleRenderer.cpp +++ b/client/battle/BattleRenderer.cpp @@ -16,12 +16,8 @@ #include "BattleWindow.h" #include "BattleSiegeController.h" #include "BattleStacksController.h" -#include "BattleFieldController.h" #include "BattleObstacleController.h" -#include "logging/VisualLogger.h" -#include "../client/render/Canvas.h" -#include "../client/render/Colors.h" -#include "../client/gui/TextAlignment.h" +#include "BattleOverlayLogVisualizer.h" void BattleRenderer::collectObjects() { @@ -73,30 +69,11 @@ void BattleRenderer::insert(EBattleFieldLayer layer, BattleHex tile, BattleRende objects.push_back({functor, layer, tile}); } -class VisualLoggerBattleRenderer : public IBattleOverlayLogVisualizer -{ -private: - Canvas & target; - BattleInterface & owner; - -public: - VisualLoggerBattleRenderer(Canvas & target, BattleInterface & owner) : target(target), owner(owner) - { - } - - virtual void drawText(BattleHex hex, std::vector texts) override - { - const Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20); - - target.drawText(offset, EFonts::FONT_TINY, Colors::RED, ETextAlignment::TOPCENTER, texts); - } -}; - void BattleRenderer::execute(BattleRenderer::RendererRef targetCanvas) { collectObjects(); sortObjects(); renderObjects(targetCanvas); - logVisual->visualize(VisualLoggerBattleRenderer(targetCanvas, owner)); + logVisual->visualize(BattleOverlayLogVisualizer(targetCanvas, owner)); } diff --git a/client/mapView/MapOverlayLogVisualizer.cpp b/client/mapView/MapOverlayLogVisualizer.cpp new file mode 100644 index 000000000..724e8a690 --- /dev/null +++ b/client/mapView/MapOverlayLogVisualizer.cpp @@ -0,0 +1,68 @@ +/* + * MapOverlayLogVisualizer.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#include "StdInc.h" +#include "MapOverlayLogVisualizer.h" +#include "MapViewModel.h" + +#include "../../lib/logging/VisualLogger.h" +#include "../render/Canvas.h" +#include "../render/Colors.h" +#include "../render/EFont.h" +#include "../gui/TextAlignment.h" + + +MapOverlayLogVisualizer::MapOverlayLogVisualizer(Canvas & target, std::shared_ptr model) + : target(target), model(model) +{ +} + +void MapOverlayLogVisualizer::drawLine(int3 start, int3 end) +{ + const Point offset = Point(30, 30); + + auto level = model->getLevel(); + + if(start.z != level || end.z != level) + return; + + auto pStart = model->getTargetTileArea(start).topLeft(); + auto pEnd = model->getTargetTileArea(end).topLeft(); + auto viewPort = target.getRenderArea(); + + pStart.x += 3; + pEnd.x -= 3; + + pStart += offset; + pEnd += offset; + + if(viewPort.isInside(pStart) && viewPort.isInside(pEnd)) + { + target.drawLine(pStart, pEnd, ColorRGBA(255, 255, 0), ColorRGBA(255, 0, 0)); + } +} + +void MapOverlayLogVisualizer::drawText(int3 tile, std::vector texts) +{ + const Point offset = Point(5, 5); + + auto level = model->getLevel(); + + if(tile.z != level) + return; + + auto pStart = offset + model->getTargetTileArea(tile).topLeft(); + auto viewPort = target.getRenderArea(); + + if(viewPort.isInside(pStart)) + { + target.drawText(pStart, EFonts::FONT_TINY, Colors::YELLOW, ETextAlignment::TOPCENTER, texts); + } +} diff --git a/client/mapView/MapOverlayLogVisualizer.h b/client/mapView/MapOverlayLogVisualizer.h new file mode 100644 index 000000000..46e84ea06 --- /dev/null +++ b/client/mapView/MapOverlayLogVisualizer.h @@ -0,0 +1,33 @@ +/* + * MapOverlayLogVisualizer.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ +#pragma once + +#include "../../lib/logging/VisualLogger.h" + +VCMI_LIB_NAMESPACE_BEGIN + +class int3; + +VCMI_LIB_NAMESPACE_END + +class Canvas; +class MapViewModel; + +class MapOverlayLogVisualizer : public IMapOverlayLogVisualizer +{ +private: + Canvas & target; + std::shared_ptr model; + +public: + MapOverlayLogVisualizer(Canvas & target, std::shared_ptr model); + void drawLine(int3 start, int3 end) override; + void drawText(int3 tile, std::vector texts) override; +}; diff --git a/client/mapView/MapView.cpp b/client/mapView/MapView.cpp index 72f60d839..c99a12c81 100644 --- a/client/mapView/MapView.cpp +++ b/client/mapView/MapView.cpp @@ -31,7 +31,8 @@ #include "../../lib/CConfigHandler.h" #include "../../lib/mapObjects/CGHeroInstance.h" -#include "../../lib/logging/VisualLogger.h" + +#include "MapOverlayLogVisualizer.h" BasicMapView::~BasicMapView() = default; @@ -58,50 +59,13 @@ BasicMapView::BasicMapView(const Point & offset, const Point & dimensions) pos.h = dimensions.y; } -class VisualLoggerRenderer : public IMapOverlayLogVisualizer -{ -private: - Canvas & target; - std::shared_ptr model; - -public: - VisualLoggerRenderer(Canvas & target, std::shared_ptr model) : target(target), model(model) - { - } - - virtual void drawLine(int3 start, int3 end) override - { - const Point offset = Point(30, 30); - - auto level = model->getLevel(); - - if(start.z != level || end.z != level) - return; - - auto pStart = model->getTargetTileArea(start).topLeft(); - auto pEnd = model->getTargetTileArea(end).topLeft(); - auto viewPort = target.getRenderArea(); - - pStart.x += 3; - pEnd.x -= 3; - - pStart += offset; - pEnd += offset; - - if(viewPort.isInside(pStart) && viewPort.isInside(pEnd)) - { - target.drawLine(pStart, pEnd, ColorRGBA(255, 255, 0), ColorRGBA(255, 0, 0)); - } - } -}; - void BasicMapView::render(Canvas & target, bool fullUpdate) { Canvas targetClipped(target, pos); tilesCache->update(controller->getContext()); tilesCache->render(controller->getContext(), targetClipped, fullUpdate); - VisualLoggerRenderer r(target, model); + MapOverlayLogVisualizer r(target, model); logVisual->visualize(r); } diff --git a/lib/logging/VisualLogger.cpp b/lib/logging/VisualLogger.cpp index f0d0bbe3f..cd2741707 100644 --- a/lib/logging/VisualLogger.cpp +++ b/lib/logging/VisualLogger.cpp @@ -20,9 +20,10 @@ void VisualLogger::updateWithLock(std::string channel, std::function lock(mutex); mapLines[channel].clear(); + mapTexts[channel].clear(); battleTexts[channel].clear(); - VisualLogBuilder builder(mapLines[channel], battleTexts[channel]); + VisualLogBuilder builder(mapLines[channel], mapTexts[channel], battleTexts[channel]); func(builder); } @@ -35,6 +36,18 @@ void VisualLogger::visualize(IMapOverlayLogVisualizer & visulizer) { visulizer.drawLine(line.start, line.end); } + + std::map> textMap; + + for(auto line : mapTexts[keyToShow]) + { + textMap[line.tile].push_back(line.text); + } + + for(auto & pair : textMap) + { + visulizer.drawText(pair.first, pair.second); + } } void VisualLogger::visualize(IBattleOverlayLogVisualizer & visulizer) diff --git a/lib/logging/VisualLogger.h b/lib/logging/VisualLogger.h index 7743c63ba..176ee9c34 100644 --- a/lib/logging/VisualLogger.h +++ b/lib/logging/VisualLogger.h @@ -19,6 +19,7 @@ class IMapOverlayLogVisualizer { public: virtual void drawLine(int3 start, int3 end) = 0; + virtual void drawText(int3 tile, std::vector texts) = 0; }; class IBattleOverlayLogVisualizer @@ -31,6 +32,7 @@ class IVisualLogBuilder { public: virtual void addLine(int3 start, int3 end) = 0; + virtual void addText(int3 tile, std::string text) = 0; virtual void addText(BattleHex tile, std::string text) = 0; }; @@ -67,12 +69,14 @@ private: private: std::vector> & mapLines; std::vector> & battleTexts; + std::vector> & mapTexts; public: VisualLogBuilder( std::vector> & mapLines, + std::vector> & mapTexts, std::vector> & battleTexts) - :mapLines(mapLines), battleTexts(battleTexts) + :mapLines(mapLines), mapTexts(mapTexts), battleTexts(battleTexts) { } @@ -85,10 +89,16 @@ private: { battleTexts.emplace_back(tile, text); } + + void addText(int3 tile, std::string text) override + { + mapTexts.emplace_back(tile, text); + } }; private: std::map>> mapLines; + std::map>> mapTexts; std::map>> battleTexts; std::mutex mutex; std::string keyToShow;