254 uint bits; |
254 uint bits; |
255 int i; |
255 int i; |
256 RememberData rd; |
256 RememberData rd; |
257 TileIndex tile_org = tile; |
257 TileIndex tile_org = tile; |
258 |
258 |
259 // check if the old tile can be left at that direction |
259 if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
260 if (tpf->tracktype == TRANSPORT_ROAD) { |
260 if (IsTunnel(tile)) { |
261 // road stops and depots now have a track (r4419) |
261 if (GetTunnelDirection(tile) != direction || |
262 // don't enter road stop from the back |
262 GetTunnelTransportType(tile) != tpf->tracktype) { |
263 if (IsRoadStopTile(tile) && GetRoadStopDir(tile) != direction) return; |
263 return; |
264 // don't enter road depot from the back |
264 } |
265 if (IsTileDepotType(tile, TRANSPORT_ROAD) && GetRoadDepotDirection(tile) != direction) return; |
265 tile = SkipToEndOfTunnel(tpf, tile, direction); |
266 } |
266 } else { |
267 |
267 TileIndex tile_end; |
268 if (IsTunnelTile(tile)) { |
268 if (GetBridgeRampDirection(tile) != direction || |
269 if (GetTunnelDirection(tile) != direction || |
269 GetBridgeTransportType(tile) != tpf->tracktype) { |
270 GetTunnelTransportType(tile) != tpf->tracktype) { |
270 return; |
271 return; |
271 } |
272 } |
272 //fprintf(stderr, "%s: Planning over bridge\n", __func__); |
273 tile = SkipToEndOfTunnel(tpf, tile, direction); |
273 // TODO doesn't work - WHAT doesn't work? |
|
274 TPFSetTileBit(tpf, tile, 14); |
|
275 tile_end = GetOtherBridgeEnd(tile); |
|
276 tpf->rd.cur_length += DistanceManhattan(tile, tile_end); |
|
277 tile = tile_end; |
|
278 TPFSetTileBit(tpf, tile, 14); |
|
279 } |
274 } |
280 } |
275 tile += TileOffsByDir(direction); |
281 tile += TileOffsByDir(direction); |
276 |
282 |
277 /* Check in case of rail if the owner is the same */ |
283 /* Check in case of rail if the owner is the same */ |
278 if (tpf->tracktype == TRANSPORT_RAIL) { |
284 if (tpf->tracktype == TRANSPORT_RAIL) { |
279 // don't enter train depot from the back |
285 // don't enter train depot from the back |
280 if (IsTileDepotType(tile, TRANSPORT_RAIL) && GetRailDepotDirection(tile) == direction) return; |
286 if (IsTileDepotType(tile, TRANSPORT_RAIL) && GetRailDepotDirection(tile) == direction) return; |
281 |
287 |
282 if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNELBRIDGE)) |
288 if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNELBRIDGE)) |
283 if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) |
289 if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) |
284 /* Check if we are on a bridge (middle parts don't have an owner */ |
290 if (GetTileOwner(tile_org) != GetTileOwner(tile)) return; |
285 if (!IsBridgeTile(tile) || !IsBridgeMiddle(tile)) |
|
286 if (!IsBridgeTile(tile_org) || !IsBridgeMiddle(tile_org)) |
|
287 if (GetTileOwner(tile_org) != GetTileOwner(tile)) |
|
288 return; |
|
289 } |
291 } |
290 |
292 |
291 // check if the new tile can be entered from that direction |
293 // check if the new tile can be entered from that direction |
292 if (tpf->tracktype == TRANSPORT_ROAD) { |
294 if (tpf->tracktype == TRANSPORT_ROAD) { |
293 // road stops and depots now have a track (r4419) |
295 // road stops and depots now have a track (r4419) |
709 direction = _tpf_new_direction[si.track]; |
710 direction = _tpf_new_direction[si.track]; |
710 |
711 |
711 start_at: |
712 start_at: |
712 // If the tile is the entry tile of a tunnel, and we're not going out of the tunnel, |
713 // If the tile is the entry tile of a tunnel, and we're not going out of the tunnel, |
713 // need to find the exit of the tunnel. |
714 // need to find the exit of the tunnel. |
714 if (IsTunnelTile(tile) && |
715 if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
715 GetTunnelDirection(tile) != ReverseDiagDir(direction)) { |
716 if (IsTunnel(tile)) { |
716 /* We are not just driving out of the tunnel */ |
717 if (GetTunnelDirection(tile) != ReverseDiagDir(direction)) { |
717 if (GetTunnelDirection(tile) != direction || |
718 FindLengthOfTunnelResult flotr; |
718 GetTunnelTransportType(tile) != tpf->tracktype) { |
719 |
719 // We are not driving into the tunnel, or it is an invalid tunnel |
720 /* We are not just driving out of the tunnel */ |
720 continue; |
721 if (GetTunnelDirection(tile) != direction || |
721 } |
722 GetTunnelTransportType(tile) != tpf->tracktype) { |
722 if (!HASBIT(tpf->railtypes, GetRailType(tile))) { |
723 // We are not driving into the tunnel, or it is an invalid tunnel |
723 bits = 0; |
724 continue; |
724 break; |
725 } |
725 } |
726 if (!HASBIT(tpf->railtypes, GetRailType(tile))) { |
726 flotr = FindLengthOfTunnel(tile, direction); |
727 bits = 0; |
727 si.cur_length += flotr.length * DIAG_FACTOR; |
728 break; |
728 tile = flotr.tile; |
729 } |
729 // tile now points to the exit tile of the tunnel |
730 flotr = FindLengthOfTunnel(tile, direction); |
|
731 si.cur_length += flotr.length * DIAG_FACTOR; |
|
732 tile = flotr.tile; |
|
733 // tile now points to the exit tile of the tunnel |
|
734 } |
|
735 } else { |
|
736 TileIndex tile_end; |
|
737 if (GetBridgeRampDirection(tile) != ReverseDiagDir(direction)) { |
|
738 // We are not just leaving the bridge |
|
739 if (GetBridgeRampDirection(tile) != direction || |
|
740 GetBridgeTransportType(tile) != tpf->tracktype) { |
|
741 // Not entering the bridge or not compatible |
|
742 continue; |
|
743 } |
|
744 } |
|
745 tile_end = GetOtherBridgeEnd(tile); |
|
746 si.cur_length += DistanceManhattan(tile, tile_end) * DIAG_FACTOR; |
|
747 tile = tile_end; |
|
748 } |
730 } |
749 } |
731 |
750 |
732 // This is a special loop used to go through |
751 // This is a special loop used to go through |
733 // a rail net and find the first intersection |
752 // a rail net and find the first intersection |
734 tile_org = tile; |
753 tile_org = tile; |
752 bits = (bits | (bits >> 8)) & 0x3F; |
771 bits = (bits | (bits >> 8)) & 0x3F; |
753 |
772 |
754 // Check that the tile contains exactly one track |
773 // Check that the tile contains exactly one track |
755 if (bits == 0 || KILL_FIRST_BIT(bits) != 0) break; |
774 if (bits == 0 || KILL_FIRST_BIT(bits) != 0) break; |
756 |
775 |
757 /* Check the rail type only if the train is *NOT* on top of |
776 if (IsTileType(tile, MP_STREET) ? !HASBIT(tpf->railtypes, GetRailTypeCrossing(tile)) : !HASBIT(tpf->railtypes, GetRailType(tile))) { |
758 * a bridge. */ |
777 bits = 0; |
759 if (!(IsBridgeTile(tile) && IsBridgeMiddle(tile) && GetBridgeAxis(tile) == DiagDirToAxis(direction))) { |
778 break; |
760 if (IsTileType(tile, MP_STREET) ? !HASBIT(tpf->railtypes, GetRailTypeCrossing(tile)) : !HASBIT(tpf->railtypes, GetRailType(tile))) { |
|
761 bits = 0; |
|
762 break; |
|
763 } |
|
764 } |
779 } |
765 |
780 |
766 /////////////////// |
781 /////////////////// |
767 // If we reach here, the tile has exactly one track. |
782 // If we reach here, the tile has exactly one track. |
768 // tile - index to a tile that is not rail tile, but still straight (with optional signals) |
783 // tile - index to a tile that is not rail tile, but still straight (with optional signals) |