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

Merge pull request #4964 from vcmi/connections_rework

Connections rework
This commit is contained in:
Ivan Savenko 2024-12-02 13:21:32 +02:00 committed by GitHub
commit fcb4dfa985
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 9 deletions

View File

@ -421,6 +421,7 @@ void ZoneOptions::serializeJson(JsonSerializeFormat & handler)
} }
ZoneConnection::ZoneConnection(): ZoneConnection::ZoneConnection():
id(-1),
zoneA(-1), zoneA(-1),
zoneB(-1), zoneB(-1),
guardStrength(0), guardStrength(0),
@ -430,6 +431,16 @@ ZoneConnection::ZoneConnection():
} }
int ZoneConnection::getId() const
{
return id;
}
void ZoneConnection::setId(int id)
{
this->id = id;
}
TRmgTemplateZoneId ZoneConnection::getZoneA() const TRmgTemplateZoneId ZoneConnection::getZoneA() const
{ {
return zoneA; return zoneA;
@ -473,7 +484,7 @@ rmg::ERoadOption ZoneConnection::getRoadOption() const
bool operator==(const ZoneConnection & l, const ZoneConnection & r) 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) void ZoneConnection::serializeJson(JsonSerializeFormat & handler)
@ -592,7 +603,7 @@ const CRmgTemplate::Zones & CRmgTemplate::getZones() const
const std::vector<ZoneConnection> & CRmgTemplate::getConnectedZoneIds() const const std::vector<ZoneConnection> & CRmgTemplate::getConnectedZoneIds() const
{ {
return connectedZoneIds; return connections;
} }
void CRmgTemplate::validate() const void CRmgTemplate::validate() const
@ -721,7 +732,14 @@ void CRmgTemplate::serializeJson(JsonSerializeFormat & handler)
{ {
auto connectionsData = handler.enterArray("connections"); 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);
}
}
} }
{ {
@ -843,7 +861,7 @@ void CRmgTemplate::afterLoad()
} }
} }
for(const auto & connection : connectedZoneIds) for(const auto & connection : connections)
{ {
auto id1 = connection.getZoneA(); auto id1 = connection.getZoneA();
auto id2 = connection.getZoneB(); auto id2 = connection.getZoneB();

View File

@ -97,6 +97,8 @@ public:
ZoneConnection(); ZoneConnection();
int getId() const;
void setId(int id);
TRmgTemplateZoneId getZoneA() const; TRmgTemplateZoneId getZoneA() const;
TRmgTemplateZoneId getZoneB() const; TRmgTemplateZoneId getZoneB() const;
TRmgTemplateZoneId getOtherZoneId(TRmgTemplateZoneId id) const; TRmgTemplateZoneId getOtherZoneId(TRmgTemplateZoneId id) const;
@ -108,6 +110,7 @@ public:
friend bool operator==(const ZoneConnection &, const ZoneConnection &); friend bool operator==(const ZoneConnection &, const ZoneConnection &);
private: private:
int id;
TRmgTemplateZoneId zoneA; TRmgTemplateZoneId zoneA;
TRmgTemplateZoneId zoneB; TRmgTemplateZoneId zoneB;
int guardStrength; int guardStrength;
@ -294,7 +297,7 @@ private:
CPlayerCountRange players; CPlayerCountRange players;
CPlayerCountRange humanPlayers; CPlayerCountRange humanPlayers;
Zones zones; Zones zones;
std::vector<rmg::ZoneConnection> connectedZoneIds; std::vector<rmg::ZoneConnection> connections;
std::set<EWaterContent::EWaterContent> allowedWaterContent; std::set<EWaterContent::EWaterContent> allowedWaterContent;
std::unique_ptr<JsonNode> mapSettings; std::unique_ptr<JsonNode> mapSettings;

View File

@ -143,7 +143,7 @@ void ConnectionsPlacer::forcePortalConnection(const rmg::ZoneConnection & connec
void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & connection) void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & connection)
{ {
bool success = false; 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); auto & otherZone = map.getZones().at(otherZoneId);
bool createRoad = shouldGenerateRoad(connection); bool createRoad = shouldGenerateRoad(connection);
@ -327,10 +327,9 @@ void ConnectionsPlacer::selfSideDirectConnection(const rmg::ZoneConnection & con
assert(otherZone->getModificator<RoadPlacer>()); assert(otherZone->getModificator<RoadPlacer>());
otherZone->getModificator<RoadPlacer>()->addRoadNode(roadNode); otherZone->getModificator<RoadPlacer>()->addRoadNode(roadNode);
assert(otherZone->getModificator<ConnectionsPlacer>());
otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
} }
assert(otherZone->getModificator<ConnectionsPlacer>());
otherZone->getModificator<ConnectionsPlacer>()->otherSideConnection(connection);
success = true; success = true;
} }