Loading

Paste #pbfwpiqdi

  1. /**
  2.  * Check whether growing on a half-tile coast tile ends up blocking a water connection
  3.  *
  4.  * @param tile The target tile
  5.  * @return true if building here blocks a water connection
  6.  */
  7. static bool NewGrowingBlocksWaterConnection(TileIndex tile)
  8. {
  9.     if (!IsValidTile(tile) || !IsCoastTile(tile)) return false;
  10.  
  11.     Slope slope = GetTileSlope(tile);
  12.  
  13.     /* Is this a coast tile with one corner raised ? */
  14.     if (!IsSlopeWithOneCornerRaised(slope)) return false;
  15.  
  16.     Corner corner = GetHighestSlopeCorner(slope);
  17.  
  18.     /* Test opposite tile is valid */
  19.     static const Direction corner_to_direction[] = { DIR_W, DIR_S, DIR_E, DIR_N };
  20.     TileIndex opposite_tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir(corner_to_direction[OppositeCorner(corner)]));
  21.     if (!IsValidTile(opposite_tile)) return false;
  22.  
  23.     /* Test adjacent tiles are traversible. */
  24.     Track track = TrackBitsToTrack(TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0)));
  25.     Trackdir main_trackdir = TrackToTrackdir(track);
  26.  
  27.     DiagDirection main_dir_1 = TrackdirToExitdir(main_trackdir);
  28.     DiagDirection main_dir_2 = TrackdirToExitdir(ReverseTrackdir(main_trackdir));
  29.  
  30.     TrackBits trackbits_1 = TrackStatusToTrackBits(GetTileTrackStatus(AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(main_dir_1)), TRANSPORT_WATER, 0));
  31.     TrackBits trackbits_2 = TrackStatusToTrackBits(GetTileTrackStatus(AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(main_dir_2)), TRANSPORT_WATER, 0));
  32.  
  33.     TrackBits main_track_1 = trackbits_1 & DiagdirReachesTracks(main_dir_1);
  34.     TrackBits main_track_2 = trackbits_2 & DiagdirReachesTracks(main_dir_2);
  35.  
  36.     /* Is main tile connected to adjacent tiles? */
  37.     if (!main_track_1 || !main_track_2) return false;
  38.  
  39.     Track opposite_track = TrackToOppositeTrack(track);
  40.  
  41.     Trackdir oppo_trackdir = TrackToTrackdir(opposite_track);
  42.  
  43.     TrackBits oppo_track_1 = trackbits_1 & DiagdirReachesTracks(TrackdirToExitdir(oppo_trackdir));
  44.     TrackBits oppo_track_2 = trackbits_2 & DiagdirReachesTracks(TrackdirToExitdir(ReverseTrackdir(oppo_trackdir)));
  45.  
  46.     TrackBits mirror = TRACK_BIT_CROSS | (corner & 1 ? TRACK_BIT_HORZ : TRACK_BIT_VERT);
  47.  
  48.     /* Is there a connection between tile_1 and tile_2 via opposite_tile? */
  49.     if (HasTrack(TrackStatusToTrackBits(GetTileTrackStatus(opposite_tile, TRANSPORT_WATER, 0)), opposite_track) &&
  50.             oppo_track_1 & (main_track_1 ^ mirror) && oppo_track_2 & (main_track_2 ^ mirror)) return false;
  51.  
  52.     /* There is either no track or an incomplete connection via opposite_tile. */
  53.     return true;
  54. }

Comments