1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Merge pull request #2847 from vcmi/trap-1912

#1912 trap, exception on adding duplicating hero
This commit is contained in:
Andrii Danylchenko 2023-10-29 19:24:47 +02:00 committed by GitHub
commit 1418e6884e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -38,6 +38,7 @@
#include "../networkPacks/PacksForClientBattle.h"
#include "../constants/StringConstants.h"
#include "../battle/Unit.h"
#include "CConfigHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -1509,8 +1510,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)
{

View File

@ -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");
}