From b579ca8a3300512c6407526be39185e64448b2c2 Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Sat, 16 Sep 2023 19:34:50 +0300 Subject: [PATCH] #1912 trap, exception on adding duplicating hero --- lib/mapObjects/CGHeroInstance.cpp | 22 ++++++++++++++++++++++ mapeditor/mainwindow.cpp | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index 5c8952beb..2a7d14149 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -37,6 +37,7 @@ #include "../modding/ModScope.h" #include "../constants/StringConstants.h" #include "../battle/Unit.h" +#include "CConfigHandler.h" VCMI_LIB_NAMESPACE_BEGIN @@ -1504,8 +1505,29 @@ std::string CGHeroInstance::getHeroTypeName() const void CGHeroInstance::afterAddToMap(CMap * map) { + auto existingHero = std::find_if(map->objects.begin(), map->objects.end(), [&](const CGObjectInstance * o) ->bool + { + return (o->ID == Obj::HERO || o->ID == Obj::PRISON) && o->subID == subID && o != this; + }); + + if(existingHero != map->objects.end()) + { + if(settings["session"]["editor"].Bool()) + { + logGlobal->warn("Hero is already on the map at %s", (*existingHero)->visitablePos().toString()); + } + else + { + logGlobal->error("Hero is already on the map at %s", (*existingHero)->visitablePos().toString()); + + throw std::runtime_error("Hero is already on the map"); + } + } + if(ID == Obj::HERO) + { map->heroesOnMap.emplace_back(this); + } } void CGHeroInstance::afterRemoveFromMap(CMap* map) { diff --git a/mapeditor/mainwindow.cpp b/mapeditor/mainwindow.cpp index 680e4337a..aaf5b19f5 100644 --- a/mapeditor/mainwindow.cpp +++ b/mapeditor/mainwindow.cpp @@ -68,6 +68,10 @@ QPixmap pixmapFromJson(const QJsonValue &val) void init() { loadDLLClasses(); + + Settings config = settings.write["session"]["editor"]; + config->Bool() = true; + logGlobal->info("Initializing VCMI_Lib"); }