浏览代码

Visual logger map texts

Andrii Danylchenko 1 年之前
父节点
当前提交
47c77826c3

+ 32 - 0
AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp

@@ -13,6 +13,7 @@
 #include "../Engine/Nullkiller.h"
 #include "../Engine/Nullkiller.h"
 #include "../pforeach.h"
 #include "../pforeach.h"
 #include "../../../lib/CRandomGenerator.h"
 #include "../../../lib/CRandomGenerator.h"
+#include "../../../lib/logging/VisualLogger.h"
 
 
 namespace NKAI
 namespace NKAI
 {
 {
@@ -24,6 +25,35 @@ double HitMapInfo::value() const
 	return danger / std::sqrt(turn / 3.0f + 1);
 	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()
 void DangerHitMapAnalyzer::updateHitMap()
 {
 {
 	if(hitMapUpToDate)
 	if(hitMapUpToDate)
@@ -144,6 +174,8 @@ void DangerHitMapAnalyzer::updateHitMap()
 	}
 	}
 
 
 	logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
 	logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
+
+	logHitmap(ai->playerID, *this);
 }
 }
 
 
 void DangerHitMapAnalyzer::calculateTileOwners()
 void DangerHitMapAnalyzer::calculateTileOwners()

+ 4 - 0
client/CMakeLists.txt

@@ -27,6 +27,7 @@ set(client_SRCS
 	battle/BattleStacksController.cpp
 	battle/BattleStacksController.cpp
 	battle/BattleWindow.cpp
 	battle/BattleWindow.cpp
 	battle/CreatureAnimation.cpp
 	battle/CreatureAnimation.cpp
+	battle/BattleOverlayLogVisualizer.cpp
 
 
 	eventsSDL/NotificationHandler.cpp
 	eventsSDL/NotificationHandler.cpp
 	eventsSDL/InputHandler.cpp
 	eventsSDL/InputHandler.cpp
@@ -74,6 +75,7 @@ set(client_SRCS
 	mapView/MapViewController.cpp
 	mapView/MapViewController.cpp
 	mapView/MapViewModel.cpp
 	mapView/MapViewModel.cpp
 	mapView/mapHandler.cpp
 	mapView/mapHandler.cpp
+	mapView/MapOverlayLogVisualizer.cpp
 
 
 	media/CAudioBase.cpp
 	media/CAudioBase.cpp
 	media/CMusicHandler.cpp
 	media/CMusicHandler.cpp
@@ -214,6 +216,7 @@ set(client_HEADERS
 	battle/BattleStacksController.h
 	battle/BattleStacksController.h
 	battle/BattleWindow.h
 	battle/BattleWindow.h
 	battle/CreatureAnimation.h
 	battle/CreatureAnimation.h
+	battle/BattleOverlayLogVisualizer.h
 
 
 	eventsSDL/NotificationHandler.h
 	eventsSDL/NotificationHandler.h
 	eventsSDL/InputHandler.h
 	eventsSDL/InputHandler.h
@@ -266,6 +269,7 @@ set(client_HEADERS
 	mapView/MapViewController.h
 	mapView/MapViewController.h
 	mapView/MapViewModel.h
 	mapView/MapViewModel.h
 	mapView/mapHandler.h
 	mapView/mapHandler.h
+	mapView/MapOverlayLogVisualizer.h
 
 
 	media/CAudioBase.h
 	media/CAudioBase.h
 	media/CEmptyVideoPlayer.h
 	media/CEmptyVideoPlayer.h

+ 33 - 0
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<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);
+}

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

+ 2 - 25
client/battle/BattleRenderer.cpp

@@ -16,12 +16,8 @@
 #include "BattleWindow.h"
 #include "BattleWindow.h"
 #include "BattleSiegeController.h"
 #include "BattleSiegeController.h"
 #include "BattleStacksController.h"
 #include "BattleStacksController.h"
-#include "BattleFieldController.h"
 #include "BattleObstacleController.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()
 void BattleRenderer::collectObjects()
 {
 {
@@ -73,30 +69,11 @@ void BattleRenderer::insert(EBattleFieldLayer layer, BattleHex tile, BattleRende
 	objects.push_back({functor, layer, tile});
 	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)
 void BattleRenderer::execute(BattleRenderer::RendererRef targetCanvas)
 {
 {
 	collectObjects();
 	collectObjects();
 	sortObjects();
 	sortObjects();
 	renderObjects(targetCanvas);
 	renderObjects(targetCanvas);
 
 
-	logVisual->visualize(VisualLoggerBattleRenderer(targetCanvas, owner));
+	logVisual->visualize(BattleOverlayLogVisualizer(targetCanvas, owner));
 }
 }

+ 68 - 0
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<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);
+	}
+}

+ 33 - 0
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<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;
+};

+ 3 - 39
client/mapView/MapView.cpp

@@ -31,7 +31,8 @@
 
 
 #include "../../lib/CConfigHandler.h"
 #include "../../lib/CConfigHandler.h"
 #include "../../lib/mapObjects/CGHeroInstance.h"
 #include "../../lib/mapObjects/CGHeroInstance.h"
-#include "../../lib/logging/VisualLogger.h"
+
+#include "MapOverlayLogVisualizer.h"
 
 
 BasicMapView::~BasicMapView() = default;
 BasicMapView::~BasicMapView() = default;
 
 
@@ -58,50 +59,13 @@ BasicMapView::BasicMapView(const Point & offset, const Point & dimensions)
 	pos.h = dimensions.y;
 	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)
 void BasicMapView::render(Canvas & target, bool fullUpdate)
 {
 {
 	Canvas targetClipped(target, pos);
 	Canvas targetClipped(target, pos);
 	tilesCache->update(controller->getContext());
 	tilesCache->update(controller->getContext());
 	tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
 	tilesCache->render(controller->getContext(), targetClipped, fullUpdate);
 
 
-	VisualLoggerRenderer r(target, model);
+	MapOverlayLogVisualizer r(target, model);
 	logVisual->visualize(r);
 	logVisual->visualize(r);
 }
 }
 
 

+ 14 - 1
lib/logging/VisualLogger.cpp

@@ -20,9 +20,10 @@ void VisualLogger::updateWithLock(std::string channel, std::function<void(IVisua
 	std::lock_guard<std::mutex> lock(mutex);
 	std::lock_guard<std::mutex> lock(mutex);
 
 
 	mapLines[channel].clear();
 	mapLines[channel].clear();
+	mapTexts[channel].clear();
 	battleTexts[channel].clear();
 	battleTexts[channel].clear();
 
 
-	VisualLogBuilder builder(mapLines[channel], battleTexts[channel]);
+	VisualLogBuilder builder(mapLines[channel], mapTexts[channel], battleTexts[channel]);
 	
 	
 	func(builder);
 	func(builder);
 }
 }
@@ -35,6 +36,18 @@ void VisualLogger::visualize(IMapOverlayLogVisualizer & visulizer)
 	{
 	{
 		visulizer.drawLine(line.start, line.end);
 		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)
 void VisualLogger::visualize(IBattleOverlayLogVisualizer & visulizer)

+ 11 - 1
lib/logging/VisualLogger.h

@@ -19,6 +19,7 @@ class IMapOverlayLogVisualizer
 {
 {
 public:
 public:
 	virtual void drawLine(int3 start, int3 end) = 0;
 	virtual void drawLine(int3 start, int3 end) = 0;
+	virtual void drawText(int3 tile, std::vector<std::string> texts) = 0;
 };
 };
 
 
 class IBattleOverlayLogVisualizer
 class IBattleOverlayLogVisualizer
@@ -31,6 +32,7 @@ class IVisualLogBuilder
 {
 {
 public:
 public:
 	virtual void addLine(int3 start, int3 end) = 0;
 	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;
 	virtual void addText(BattleHex tile, std::string text) = 0;
 };
 };
 
 
@@ -67,12 +69,14 @@ private:
 	private:
 	private:
 		std::vector<Line<int3>> & mapLines;
 		std::vector<Line<int3>> & mapLines;
 		std::vector<Text<BattleHex>> & battleTexts;
 		std::vector<Text<BattleHex>> & battleTexts;
+		std::vector<Text<int3>> & mapTexts;
 
 
 	public:
 	public:
 		VisualLogBuilder(
 		VisualLogBuilder(
 			std::vector<Line<int3>> & mapLines,
 			std::vector<Line<int3>> & mapLines,
+			std::vector<Text<int3>> & mapTexts,
 			std::vector<Text<BattleHex>> & battleTexts)
 			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);
 			battleTexts.emplace_back(tile, text);
 		}
 		}
+
+		void addText(int3 tile, std::string text) override
+		{
+			mapTexts.emplace_back(tile, text);
+		}
 	};
 	};
 
 
 private:
 private:
 	std::map<std::string, std::vector<Line<int3>>> mapLines;
 	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::map<std::string, std::vector<Text<BattleHex>>> battleTexts;
 	std::mutex mutex;
 	std::mutex mutex;
 	std::string keyToShow;
 	std::string keyToShow;