| 128 | } |
| 129 | |
| 130 | TrackElement* trackGetPreviousBlock(CoordsXYZ& location, TileElement* tileElement) |
| 131 | { |
| 132 | CoordsXYZ startLocation = location; |
| 133 | TrackBeginEnd trackBeginEnd, slowTrackBeginEnd; |
| 134 | TileElement slowTileElement = *tileElement; |
| 135 | bool counter = true; |
| 136 | CoordsXY slowLocation = location; |
| 137 | do |
| 138 | { |
| 139 | if (!trackBlockGetPrevious({ location, tileElement }, &trackBeginEnd)) |
| 140 | { |
| 141 | return nullptr; |
| 142 | } |
| 143 | if (trackBeginEnd.begin_x == startLocation.x && trackBeginEnd.begin_y == startLocation.y |
| 144 | && tileElement == trackBeginEnd.begin_element) |
| 145 | { |
| 146 | return nullptr; |
| 147 | } |
| 148 | |
| 149 | location.x = trackBeginEnd.end_x; |
| 150 | location.y = trackBeginEnd.end_y; |
| 151 | location.z = trackBeginEnd.begin_z; |
| 152 | tileElement = trackBeginEnd.begin_element; |
| 153 | |
| 154 | // #2081: prevent infinite loop |
| 155 | counter = !counter; |
| 156 | if (counter) |
| 157 | { |
| 158 | trackBlockGetPrevious({ slowLocation, &slowTileElement }, &slowTrackBeginEnd); |
| 159 | slowLocation.x = slowTrackBeginEnd.end_x; |
| 160 | slowLocation.y = slowTrackBeginEnd.end_y; |
| 161 | slowTileElement = *(slowTrackBeginEnd.begin_element); |
| 162 | if (slowLocation == location && slowTileElement.getBaseZ() == tileElement->getBaseZ() |
| 163 | && slowTileElement.getType() == tileElement->getType() |
| 164 | && slowTileElement.getDirection() == tileElement->getDirection()) |
| 165 | { |
| 166 | return nullptr; |
| 167 | } |
| 168 | } |
| 169 | } while (!(trackBeginEnd.begin_element->asTrack()->IsBlockStart())); |
| 170 | |
| 171 | // Get the start of the track block instead of the end |
| 172 | location = { trackBeginEnd.begin_x, trackBeginEnd.begin_y, trackBeginEnd.begin_z }; |
| 173 | auto trackOrigin = OpenRCT2::MapGetTrackElementAtOfTypeSeq( |
| 174 | location, trackBeginEnd.begin_element->asTrack()->GetTrackType(), 0); |
| 175 | if (trackOrigin == nullptr) |
| 176 | { |
| 177 | return nullptr; |
| 178 | } |
| 179 | |
| 180 | return trackOrigin->asTrack(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * |
no test coverage detected