Explorar o código

Merge pull request #2847 from vcmi/trap-1912

#1912 trap, exception on adding duplicating hero
Andrii Danylchenko %!s(int64=2) %!d(string=hai) anos
pai
achega
1418e6884e
Modificáronse 2 ficheiros con 26 adicións e 0 borrados
  1. 22 0
      lib/mapObjects/CGHeroInstance.cpp
  2. 4 0
      mapeditor/mainwindow.cpp

+ 22 - 0
lib/mapObjects/CGHeroInstance.cpp

@@ -38,6 +38,7 @@
 #include "../networkPacks/PacksForClientBattle.h"
 #include "../networkPacks/PacksForClientBattle.h"
 #include "../constants/StringConstants.h"
 #include "../constants/StringConstants.h"
 #include "../battle/Unit.h"
 #include "../battle/Unit.h"
+#include "CConfigHandler.h"
 
 
 VCMI_LIB_NAMESPACE_BEGIN
 VCMI_LIB_NAMESPACE_BEGIN
 
 
@@ -1509,8 +1510,29 @@ std::string CGHeroInstance::getHeroTypeName() const
 
 
 void CGHeroInstance::afterAddToMap(CMap * map)
 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)
 	if(ID == Obj::HERO)
+	{		
 		map->heroesOnMap.emplace_back(this);
 		map->heroesOnMap.emplace_back(this);
+	}
 }
 }
 void CGHeroInstance::afterRemoveFromMap(CMap* map)
 void CGHeroInstance::afterRemoveFromMap(CMap* map)
 {
 {

+ 4 - 0
mapeditor/mainwindow.cpp

@@ -68,6 +68,10 @@ QPixmap pixmapFromJson(const QJsonValue &val)
 void init()
 void init()
 {
 {
 	loadDLLClasses();
 	loadDLLClasses();
+
+	Settings config = settings.write["session"]["editor"];
+	config->Bool() = true;
+
 	logGlobal->info("Initializing VCMI_Lib");
 	logGlobal->info("Initializing VCMI_Lib");
 }
 }