From fbc0150855b8b8712fe0dc7121503cf220505c52 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Mon, 17 Feb 2025 13:38:29 -0800 Subject: [PATCH] Added 12V tracks. --- common/lc_traintrack.cpp | 72 +++++++++++++++++------------ common/lc_traintrack.h | 9 +++- resources/traintrack.json | 95 ++++++++++++++++++++++++++++++--------- 3 files changed, 124 insertions(+), 52 deletions(-) diff --git a/common/lc_traintrack.cpp b/common/lc_traintrack.cpp index 174b460b..15d9c1fc 100644 --- a/common/lc_traintrack.cpp +++ b/common/lc_traintrack.cpp @@ -28,41 +28,46 @@ void lcTrainTrackInit(lcPiecesLibrary* Library) for (QJsonObject::const_iterator PiecesIt = JsonPieces.constBegin(); PiecesIt != JsonPieces.constEnd(); ++PiecesIt) { - PieceInfo* Info = Library->FindPiece(PiecesIt.key().toLatin1(), nullptr, false, false); - - if (!Info) - continue; - - lcTrainTrackInfo* TrainTrackInfo = new lcTrainTrackInfo(); - - Info->SetTrainTrackInfo(TrainTrackInfo); - QJsonObject JsonPiece = PiecesIt->toObject(); - QJsonArray JsonConnections = JsonPiece["Connections"].toArray(); + QJsonArray JsonParts = JsonPiece["Parts"].toArray(); - for (QJsonArray::const_iterator ConnectionIt = JsonConnections.constBegin(); ConnectionIt != JsonConnections.constEnd(); ++ConnectionIt) + for (QJsonArray::const_iterator PartsIt = JsonParts.constBegin(); PartsIt != JsonParts.constEnd(); ++PartsIt) { - QJsonObject JsonConnection = ConnectionIt->toObject(); + PieceInfo* Info = Library->FindPiece(PartsIt->toString().toLatin1(), nullptr, false, false); - QJsonArray JsonPosition = JsonConnection["Position"].toArray(); - lcVector3 Position(JsonPosition[0].toDouble(), JsonPosition[1].toDouble(), JsonPosition[2].toDouble()); + if (!Info) + continue; - float Rotation = JsonConnection["Rotation"].toDouble() * LC_DTOR; - QString ConnectionGroup = JsonConnection["Type"].toString(); - int ConnectionDirection = 0; + lcTrainTrackInfo* TrainTrackInfo = new lcTrainTrackInfo(); - if (ConnectionGroup.startsWith('+')) + Info->SetTrainTrackInfo(TrainTrackInfo); + + QJsonArray JsonConnections = JsonPiece["Connections"].toArray(); + + for (QJsonArray::const_iterator ConnectionIt = JsonConnections.constBegin(); ConnectionIt != JsonConnections.constEnd(); ++ConnectionIt) { - ConnectionDirection = 1; - ConnectionGroup = ConnectionGroup.mid(1); - } - else if (ConnectionGroup.startsWith('-')) - { - ConnectionDirection = -1; - ConnectionGroup = ConnectionGroup.mid(1); - } + QJsonObject JsonConnection = ConnectionIt->toObject(); - TrainTrackInfo->AddConnection(lcMatrix44(lcMatrix33RotationZ(Rotation), Position), { qHash(ConnectionGroup), ConnectionDirection } ); + QJsonArray JsonPosition = JsonConnection["Position"].toArray(); + lcVector3 Position(JsonPosition[0].toDouble(), JsonPosition[1].toDouble(), JsonPosition[2].toDouble()); + + float Rotation = JsonConnection["Rotation"].toDouble() * LC_DTOR; + QString ConnectionGroup = JsonConnection["Type"].toString(); + int ConnectionDirection = 0; + + if (ConnectionGroup.startsWith('+')) + { + ConnectionDirection = 1; + ConnectionGroup = ConnectionGroup.mid(1); + } + else if (ConnectionGroup.startsWith('-')) + { + ConnectionDirection = -1; + ConnectionGroup = ConnectionGroup.mid(1); + } + + TrainTrackInfo->AddConnection(lcMatrix44(lcMatrix33RotationZ(Rotation), Position), { qHash(ConnectionGroup), ConnectionDirection } ); + } } } } @@ -120,7 +125,18 @@ std::optional lcTrainTrackInfo::GetPieceInsertTransform(lcPiece* Cur return std::nullopt; } - return GetConnectionTransform(CurrentPiece, ConnectionIndex, Info, ConnectionIndex ? 0 : 1); + const lcTrainTrackConnectionType& CurrentConnectionType = CurrentTrackInfo->GetConnections()[ConnectionIndex].Type; + const std::vector& NewConnections = Info->GetTrainTrackInfo()->GetConnections(); + quint32 NewConnectionIndex;// = ConnectionIndex ? 0 : 1; + + for (NewConnectionIndex = 0; NewConnectionIndex < NewConnections.size(); NewConnectionIndex++) + if (AreConnectionsCompatible(CurrentConnectionType, NewConnections[NewConnectionIndex].Type)) + break; + + if (NewConnectionIndex == NewConnections.size()) + return std::nullopt; + + return GetConnectionTransform(CurrentPiece, ConnectionIndex, Info, NewConnectionIndex); } std::optional lcTrainTrackInfo::GetConnectionTransform(lcPiece* CurrentPiece, quint32 CurrentConnectionIndex, PieceInfo* Info, quint32 NewConnectionIndex) diff --git a/common/lc_traintrack.h b/common/lc_traintrack.h index f37094a9..45e86992 100644 --- a/common/lc_traintrack.h +++ b/common/lc_traintrack.h @@ -37,16 +37,21 @@ public: return mConnections; } - bool CanConnectTo(lcTrainTrackConnectionType ConnectionType) const + bool CanConnectTo(const lcTrainTrackConnectionType& ConnectionType) const { for (const lcTrainTrackConnection& Connection : mConnections) - if (Connection.Type.Group == ConnectionType.Group && Connection.Type.Format + ConnectionType.Format == 0) + if (AreConnectionsCompatible(Connection.Type, ConnectionType)) return true; return false; } protected: + static bool AreConnectionsCompatible(const lcTrainTrackConnectionType& a, const lcTrainTrackConnectionType& b) + { + return a.Group == b.Group && a.Format + b.Format == 0; + } + std::vector mConnections; }; diff --git a/resources/traintrack.json b/resources/traintrack.json index 4eb40129..3672525c 100644 --- a/resources/traintrack.json +++ b/resources/traintrack.json @@ -2,9 +2,9 @@ "Version": 1, "Pieces": { - "2859c04.dat": + "Train Track 9V Point Right": { - "Description": "Train Track 9V Point Right Branching (Complete)", + "Parts": [ "2859c03.dat", "2859c04.dat" ], "Connections": [ { @@ -24,9 +24,9 @@ } ] }, - "2861c04.dat": + "Train Track 9V Point Left": { - "Description": "Train Track 9V Point Left Branching (Complete)", + "Parts": [ "2861c03.dat", "2861c04.dat" ], "Connections": [ { @@ -46,9 +46,9 @@ } ] }, - "32087.dat": + "Train Track 9V Crossing": { - "Description": "Train Track 9V Crossing", + "Parts": [ "32087.dat" ], "Connections": [ { @@ -73,9 +73,9 @@ } ] }, - "74746.dat": + "Train Track 9V Straight": { - "Description": "Train Track 9V Straight", + "Parts": [ "74746.dat" ], "Connections": [ { @@ -90,9 +90,9 @@ } ] }, - "74747.dat": + "Train Track 9V Curved": { - "Description": "Train Track 9V Curved", + "Parts": [ "74747.dat" ], "Connections": [ { @@ -107,9 +107,9 @@ } ] }, - "3241ac01.dat": + "Train Track 12V Slotted Curved": { - "Description": "Train Track 12V Slotted Curved (Complete 1 Segment)", + "Parts": [ "3241ac01.dat" ], "Connections": [ { @@ -124,9 +124,9 @@ } ] }, - "4166a.dat": + "Train Track Sleeper Plate 2 x 8": { - "Description": "Train Track Sleeper Plate 2 x 8 without Cable Grooves", + "Parts": [ "4166a.dat" ], "Connections": [ { @@ -141,9 +141,26 @@ } ] }, - "3240bc02.dat": + "Train Track 12V Slotted Straight With End Sleepers": { - "Description": "Train Track 12V Slotted Straight with Conductive Centre Rail with Sockets (Complete without End Sleepers)", + "Parts": [ "3240ac01.dat", "3240bc01.dat" ], + "Connections": + [ + { + "Position": [ 160.0, 0.0, 0.0 ], + "Rotation": 0.0, + "Type": "+12V" + }, + { + "Position": [ -160.0, 0.0, 0.0 ], + "Rotation": -180.0, + "Type": "-12V" + } + ] + }, + "Train Track 12V Slotted Straight Without End Sleepers": + { + "Parts": [ "3240ac02.dat", "3240bc02.dat" ], "Connections": [ { @@ -158,9 +175,43 @@ } ] }, - "73697ac03.dat": + "Train Track 12V Slotted Straight With Signal": { - "Description": "Train Track 12V Slotted Point Left Electric (Straight)", + "Parts": [ "862ac02.dat" ], + "Connections": + [ + { + "Position": [ 160.0, 0.0, 0.0 ], + "Rotation": 0.0, + "Type": "+12V" + }, + { + "Position": [ -160.0, 0.0, 0.0 ], + "Rotation": -180.0, + "Type": "+12V" + } + ] + }, + "Train Track 12V Slotted Decoupler": + { + "Parts": [ "u9230c01.dat", "u9230c02.dat" ], + "Connections": + [ + { + "Position": [ 160.0, 0.0, 0.0 ], + "Rotation": 0.0, + "Type": "+12V" + }, + { + "Position": [ -160.0, 0.0, 0.0 ], + "Rotation": -180.0, + "Type": "+12V" + } + ] + }, + "Train Track 12V Slotted Point Left": + { + "Parts": [ "73697ac01.dat", "73697ac02.dat", "73697ac03.dat", "73697ac04.dat" ], "Connections": [ { @@ -180,9 +231,9 @@ } ] }, - "73696ac04.dat": + "Train Track 12V Slotted Point Right": { - "Description": "Train Track 12V Slotted Point Right Electric (Branching)", + "Parts": [ "73696ac01.dat", "73696ac02.dat", "73696ac03.dat", "73696ac04.dat" ], "Connections": [ { @@ -202,9 +253,9 @@ } ] }, - "73698.dat": + "Train Track 12V Slotted Crossing": { - "Description": "Train Track 12V Slotted Crossing", + "Parts": [ "73698.dat" ], "Connections": [ {