1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-27 21:49:10 +02:00

Visual logger map texts

This commit is contained in:
Andrii Danylchenko 2024-08-06 22:29:04 +03:00
parent 9ffd6368d4
commit 47c77826c3
10 changed files with 228 additions and 66 deletions

@ -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()

@ -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

@ -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<std::string> texts)
{
const Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20);
target.drawText(offset, EFonts::FONT_TINY, Colors::RED, ETextAlignment::TOPCENTER, texts);
}

@ -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<std::string> texts) override;
};

@ -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<std::string> 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));
}

@ -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<MapViewModel> 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<std::string> 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);
}
}

@ -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<MapViewModel> model;
public:
MapOverlayLogVisualizer(Canvas & target, std::shared_ptr<MapViewModel> model);
void drawLine(int3 start, int3 end) override;
void drawText(int3 tile, std::vector<std::string> texts) override;
};

@ -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<MapViewModel> model;
public:
VisualLoggerRenderer(Canvas & target, std::shared_ptr<MapViewModel> 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);
}

@ -20,9 +20,10 @@ void VisualLogger::updateWithLock(std::string channel, std::function<void(IVisua
std::lock_guard<std::mutex> 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<int3, std::vector<std::string>> 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)

@ -19,6 +19,7 @@ class IMapOverlayLogVisualizer
{
public:
virtual void drawLine(int3 start, int3 end) = 0;
virtual void drawText(int3 tile, std::vector<std::string> 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<Line<int3>> & mapLines;
std::vector<Text<BattleHex>> & battleTexts;
std::vector<Text<int3>> & mapTexts;
public:
VisualLogBuilder(
std::vector<Line<int3>> & mapLines,
std::vector<Text<int3>> & mapTexts,
std::vector<Text<BattleHex>> & 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<std::string, std::vector<Line<int3>>> mapLines;
std::map<std::string, std::vector<Text<int3>>> mapTexts;
std::map<std::string, std::vector<Text<BattleHex>>> battleTexts;
std::mutex mutex;
std::string keyToShow;