Skip to content

Commit c6d875a

Browse files
committed
[simulator] fix crash on addTrain() when too long
1 parent 11683a1 commit c6d875a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

shared/src/traintastic/simulator/simulator.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,8 +2823,6 @@ bool Simulator::addTrain(const std::string_view& name, DecoderProtocol proto, ui
28232823

28242824
train->length += item.vehicle->length + (empty ? 0.0f : data.trainCouplingLength);
28252825
empty = false;
2826-
2827-
train->vehicles.push_back(item);
28282826
}
28292827

28302828
if(empty)
@@ -2981,6 +2979,15 @@ bool Simulator::addTrain(const std::string_view& name, DecoderProtocol proto, ui
29812979
}
29822980
};
29832981

2982+
// Add wagons to train only when sure train will be added
2983+
for(const auto& item : vehicles)
2984+
{
2985+
if(!item.vehicle || item.vehicle->activeTrain)
2986+
continue;
2987+
2988+
train->vehicles.push_back(item);
2989+
}
2990+
29842991
// Place wagons in segments
29852992
bool first = true;
29862993
for(auto& item : train->vehicles | std::views::reverse)

0 commit comments

Comments
 (0)