From c8e3458dfb0debdd9b00a94faf82e283dcdb4da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zieli=C5=84ski?= Date: Sat, 23 Nov 2024 10:07:34 +0100 Subject: [PATCH 1/2] Handle connections by unique id --- lib/rmg/CRmgTemplate.cpp | 26 ++++++++++++++++++++++---- lib/rmg/CRmgTemplate.h | 5 ++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/rmg/CRmgTemplate.cpp b/lib/rmg/CRmgTemplate.cpp index 8e94e2c2e..f10f0c3f5 100644 --- a/lib/rmg/CRmgTemplate.cpp +++ b/lib/rmg/CRmgTemplate.cpp @@ -420,6 +420,7 @@ void ZoneOptions::serializeJson(JsonSerializeFormat & handler) } ZoneConnection::ZoneConnection(): + id(-1), zoneA(-1), zoneB(-1), guardStrength(0), @@ -429,6 +430,16 @@ ZoneConnection::ZoneConnection(): } +int ZoneConnection::getId() const +{ + return id; +} + +void ZoneConnection::setId(int id) +{ + this->id = id; +} + TRmgTemplateZoneId ZoneConnection::getZoneA() const { return zoneA; @@ -472,7 +483,7 @@ rmg::ERoadOption ZoneConnection::getRoadOption() const bool operator==(const ZoneConnection & l, const ZoneConnection & r) { - return l.zoneA == r.zoneA && l.zoneB == r.zoneB && l.guardStrength == r.guardStrength; + return l.id == r.id; } void ZoneConnection::serializeJson(JsonSerializeFormat & handler) @@ -591,7 +602,7 @@ const CRmgTemplate::Zones & CRmgTemplate::getZones() const const std::vector & CRmgTemplate::getConnectedZoneIds() const { - return connectedZoneIds; + return connections; } void CRmgTemplate::validate() const @@ -720,7 +731,14 @@ void CRmgTemplate::serializeJson(JsonSerializeFormat & handler) { auto connectionsData = handler.enterArray("connections"); - connectionsData.serializeStruct(connectedZoneIds); + connectionsData.serializeStruct(connections); + if(!handler.saving) + { + for(size_t i = 0; i < connections.size(); ++i) + { + connections[i].setId(i); + } + } } { @@ -842,7 +860,7 @@ void CRmgTemplate::afterLoad() } } - for(const auto & connection : connectedZoneIds) + for(const auto & connection : connections) { auto id1 = connection.getZoneA(); auto id2 = connection.getZoneB(); diff --git a/lib/rmg/CRmgTemplate.h b/lib/rmg/CRmgTemplate.h index 297f62387..f420e0acb 100644 --- a/lib/rmg/CRmgTemplate.h +++ b/lib/rmg/CRmgTemplate.h @@ -96,6 +96,8 @@ public: ZoneConnection(); + int getId() const; + void setId(int id); TRmgTemplateZoneId getZoneA() const; TRmgTemplateZoneId getZoneB() const; TRmgTemplateZoneId getOtherZoneId(TRmgTemplateZoneId id) const; @@ -107,6 +109,7 @@ public: friend bool operator==(const ZoneConnection &, const ZoneConnection &); private: + int id; TRmgTemplateZoneId zoneA; TRmgTemplateZoneId zoneB; int guardStrength; @@ -293,7 +296,7 @@ private: CPlayerCountRange players; CPlayerCountRange humanPlayers; Zones zones; - std::vector connectedZoneIds; + std::vector connections; std::set allowedWaterContent; std::unique_ptr mapSettings; From e5b151991b69a15b48d6dc0feb76334a5462f667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zieli=C5=84ski?= Date: Sat, 23 Nov 2024 10:43:14 +0100 Subject: [PATCH 2/2] Fix duplicated offroad connections --- lib/rmg/modificators/ConnectionsPlacer.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/rmg/modificators/ConnectionsPlacer.cpp b/lib/rmg/modificators/ConnectionsPlacer.cpp index 79c4ff438..37f158855 100644 --- a/lib/rmg/modificators/ConnectionsPlacer.cpp +++ b/lib/rmg/modificators/ConnectionsPlacer.cpp @@ -143,7 +143,7 @@ void ConnectionsPlacer::forcePortalConnection(const rmg::ZoneConnection & connec void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & connection) { bool success = false; - auto otherZoneId = (connection.getZoneA() == zone.getId() ? connection.getZoneB() : connection.getZoneA()); + auto otherZoneId = connection.getOtherZoneId(zone.getId()); auto & otherZone = map.getZones().at(otherZoneId); bool createRoad = shouldGenerateRoad(connection); @@ -327,10 +327,9 @@ void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & con assert(otherZone->getModificator()); otherZone->getModificator()->addRoadNode(roadNode); - - assert(otherZone->getModificator()); - otherZone->getModificator()->otherSideConnection(connection); } + assert(otherZone->getModificator()); + otherZone->getModificator()->otherSideConnection(connection); success = true; }