Fix crash when checking for train reverse on custom bridge heads with YAPF

This commit is contained in:
Jonathan G Rennison 2020-01-01 12:48:38 +00:00
parent 796a878b90
commit b048f824dc

View File

@ -680,30 +680,32 @@ bool YapfTrainCheckReverse(const Train *v)
/* front in tunnel / on bridge */ /* front in tunnel / on bridge */
DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile); DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile);
if (TrackdirToExitdir(td) == dir_into_wormhole) tile = GetOtherTunnelBridgeEnd(tile);
/* Now 'tile' is the tunnel entry/bridge ramp the train will reach when driving forward */
/* Current position of the train in the wormhole */ /* Current position of the train in the wormhole */
TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos); TileIndex cur_tile = TileVirtXY(v->x_pos, v->y_pos);
/* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path /* Add distance to drive in the wormhole as penalty for the forward path, i.e. bonus for the reverse path
* Note: Negative penalties are ok for the start tile. */ * Note: Negative penalties are ok for the start tile. */
if (TrackdirToExitdir(td) == dir_into_wormhole) {
reverse_penalty += DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
} else {
reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH; reverse_penalty -= DistanceManhattan(cur_tile, tile) * YAPF_TILE_LENGTH;
} }
}
if (last_veh->track & TRACK_BIT_WORMHOLE) { if (last_veh->track & TRACK_BIT_WORMHOLE) {
/* back in tunnel / on bridge */ /* back in tunnel / on bridge */
DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev); DiagDirection dir_into_wormhole = GetTunnelBridgeDirection(tile_rev);
if (TrackdirToExitdir(td_rev) == dir_into_wormhole) tile_rev = GetOtherTunnelBridgeEnd(tile_rev);
/* Now 'tile_rev' is the tunnel entry/bridge ramp the train will reach when reversing */
/* Current position of the last wagon in the wormhole */ /* Current position of the last wagon in the wormhole */
TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos); TileIndex cur_tile = TileVirtXY(last_veh->x_pos, last_veh->y_pos);
/* Add distance to drive in the wormhole as penalty for the revere path. */ /* Add distance to drive in the wormhole as penalty for the revere path. */
if (TrackdirToExitdir(td_rev) == dir_into_wormhole) {
reverse_penalty -= DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
} else {
reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH; reverse_penalty += DistanceManhattan(cur_tile, tile_rev) * YAPF_TILE_LENGTH;
} }
}
typedef bool (*PfnCheckReverseTrain)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int); typedef bool (*PfnCheckReverseTrain)(const Train*, TileIndex, Trackdir, TileIndex, Trackdir, int);
PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain; PfnCheckReverseTrain pfnCheckReverseTrain = CYapfRail1::stCheckReverseTrain;