diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index 452368d65..beea5a779 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -49,6 +49,8 @@ VCMI_LIB_NAMESPACE_BEGIN +const ui32 CGHeroInstance::NO_PATROLLING = std::numeric_limits::max(); + void CGHeroPlaceholder::serializeJsonOptions(JsonSerializeFormat & handler) { serializeJsonOwner(handler); @@ -1749,21 +1751,20 @@ void CGHeroInstance::serializeJsonOptions(JsonSerializeFormat & handler) CArmedInstance::serializeJsonOptions(handler); { - static constexpr int NO_PATROLING = -1; - int rawPatrolRadius = NO_PATROLING; + ui32 rawPatrolRadius = NO_PATROLLING; if(handler.saving) { - rawPatrolRadius = patrol.patrolling ? patrol.patrolRadius : NO_PATROLING; + rawPatrolRadius = patrol.patrolling ? patrol.patrolRadius : NO_PATROLLING; } - handler.serializeInt("patrolRadius", rawPatrolRadius, NO_PATROLING); + handler.serializeInt("patrolRadius", rawPatrolRadius, NO_PATROLLING); if(!handler.saving) { - patrol.patrolling = (rawPatrolRadius > NO_PATROLING); + patrol.patrolling = (rawPatrolRadius != NO_PATROLLING); patrol.initialPos = visitablePos(); - patrol.patrolRadius = (rawPatrolRadius > NO_PATROLING) ? rawPatrolRadius : 0; + patrol.patrolRadius = patrol.patrolling ? rawPatrolRadius : 0; } } } diff --git a/lib/mapObjects/CGHeroInstance.h b/lib/mapObjects/CGHeroInstance.h index be3144d94..0b8ddd257 100644 --- a/lib/mapObjects/CGHeroInstance.h +++ b/lib/mapObjects/CGHeroInstance.h @@ -92,6 +92,7 @@ public: static constexpr si32 UNINITIALIZED_MANA = -1; static constexpr ui32 UNINITIALIZED_MOVEMENT = -1; static constexpr auto UNINITIALIZED_EXPERIENCE = std::numeric_limits::max(); + static const ui32 NO_PATROLLING; //std::vector artifacts; //hero's artifacts from bag //std::map artifWorn; //map; positions: 0 - head; 1 - shoulders; 2 - neck; 3 - right hand; 4 - left hand; 5 - torso; 6 - right ring; 7 - left ring; 8 - feet; 9 - misc1; 10 - misc2; 11 - misc3; 12 - misc4; 13 - mach1; 14 - mach2; 15 - mach3; 16 - mach4; 17 - spellbook; 18 - misc5 @@ -99,10 +100,9 @@ public: struct DLL_LINKAGE Patrol { - Patrol(){patrolling=false;initialPos=int3();patrolRadius=-1;}; - bool patrolling; + bool patrolling{false}; int3 initialPos; - ui32 patrolRadius; + ui32 patrolRadius{NO_PATROLLING}; template void serialize(Handler &h) { h & patrolling; diff --git a/mapeditor/inspector/inspector.cpp b/mapeditor/inspector/inspector.cpp index 259470ccb..ef1696d73 100644 --- a/mapeditor/inspector/inspector.cpp +++ b/mapeditor/inspector/inspector.cpp @@ -335,6 +335,15 @@ void Inspector::updateProperties(CGHeroInstance * o) } addProperty("Hero type", o->getHeroTypeID().hasValue() ? o->getHeroType()->getNameTranslated() : "", delegate, false); } + { + const int maxRadius = 60; + auto * patrolDelegate = new InspectorDelegate; + patrolDelegate->options = { {QObject::tr("No patrol"), QVariant::fromValue(CGHeroInstance::NO_PATROLLING)} }; + for(int i = 0; i <= maxRadius; ++i) + patrolDelegate->options.push_back({ QObject::tr("%n tile(s)", "", i), QVariant::fromValue(i)}); + auto patrolRadiusText = o->patrol.patrolling ? QObject::tr("%n tile(s)", "", o->patrol.patrolRadius) : QObject::tr("No patrol"); + addProperty("Patrol radius", patrolRadiusText, patrolDelegate, false); + } } void Inspector::updateProperties(CGTownInstance * o) @@ -711,6 +720,13 @@ void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVari o->randomizeArmy(o->getHeroType()->heroClass->faction); updateProperties(); //updating other properties after change } + + if(key == "Patrol radius") + { + auto radius = value.toInt(); + o->patrol.patrolRadius = radius; + o->patrol.patrolling = radius != CGHeroInstance::NO_PATROLLING; + } } void Inspector::setProperty(CGShipyard * o, const QString & key, const QVariant & value)