Loading

Paste #pwmdc97kx

  1. /**
  2.  * Helper function
  3.  *
  4.  * @param bridge_tile
  5.  * @param bridge_dir
  6.  * @return true
  7.  * @see GrowTownWithBridge
  8.  */
  9. static bool IsPossibleLockLocationOnTownBridgeTile(const TileIndex bridge_tile, const DiagDirection bridge_dir)
  10. {
  11.     const DiagDirection dir_side = ChangeDiagDir(bridge_dir, DIAGDIRDIFF_90LEFT);
  12.     const int delta_side = TileOffsByDiagDir(dir_side);
  13.  
  14.     if (IsValidTile(bridge_tile) && IsPossibleLockLocationOnDiagDir(bridge_tile, bridge_dir, true) && IsWaterTile(bridge_tile) ||
  15.             IsValidTile(bridge_tile + delta_side) && IsPossibleLockLocationOnDiagDir(bridge_tile + delta_side, dir_side, true) && IsWaterTile(bridge_tile + delta_side) ||
  16.             IsValidTile(bridge_tile - delta_side) && IsPossibleLockLocationOnDiagDir(bridge_tile - delta_side, ReverseDiagDir(dir_side), true) && IsWaterTile(bridge_tile - delta_side)) {
  17.         return true;
  18.     }
  19.  
  20.     return false;
  21. }
  22.  
  23. /**
  24.  * Grows the town with a bridge.
  25.  *  At first we check if a bridge is reasonable.
  26.  *  If so we check if we are able to build it.
  27.  *
  28.  * @param t The current town
  29.  * @param tile The current tile
  30.  * @param bridge_dir The valid direction in which to grow a bridge
  31.  * @return true if a bridge has been build else false
  32.  */
  33. static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDirection bridge_dir)
  34. {
  35.     assert(bridge_dir < DIAGDIR_END);
  36.  
  37.     const Slope slope = GetTileSlope(tile);
  38.  
  39.     /* Make sure the direction is compatible with the slope.
  40.      * Well we check if the slope has an up bit set in the
  41.      * reverse direction. */
  42.     if (slope != SLOPE_FLAT && slope & InclinedSlope(bridge_dir)) return false;
  43.  
  44.     /* Assure that the bridge is connectable to the start side */
  45.     if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false;
  46.  
  47.     /* We are in the right direction */
  48.     uint8 bridge_length = 0;      // This value stores the length of the possible bridge
  49.     TileIndex bridge_tile = tile; // Used to store the other waterside
  50.  
  51.     const int delta = TileOffsByDiagDir(bridge_dir);
  52.     const int delta_side = TileOffsByDiagDir(ChangeDiagDir(bridge_dir, DIAGDIRDIFF_90LEFT));
  53.  
  54.     if (slope == SLOPE_FLAT) {
  55.         /* Bridges starting on flat tiles are only allowed when crossing rivers. */
  56.         do {
  57.             if (bridge_length++ >= 4) {
  58.                 /* Allow to cross rivers, not big lakes. */
  59.                 return false;
  60.             }
  61.             bridge_tile += delta;
  62.             if (IsPossibleLockLocationOnTownBridgeTile(bridge_tile, bridge_dir)) return false;
  63. //          if (IsValidTile(bridge_tile) && IsPossibleLockLocationOnDiagDir(bridge_tile, bridge_dir, true) && IsWaterTile(bridge_tile) ||
  64. //                  IsValidTile(bridge_tile + delta_side) && IsPossibleLockLocationOnDiagDir(bridge_tile + delta_side, ChangeDiagDir(bridge_dir, DIAGDIRDIFF_90LEFT), true) && IsWaterTile(bridge_tile + delta_side) ||
  65. //                  IsValidTile(bridge_tile - delta_side) && IsPossibleLockLocationOnDiagDir(bridge_tile - delta_side, ChangeDiagDir(bridge_dir, DIAGDIRDIFF_90RIGHT), true) && IsWaterTile(bridge_tile - delta_side)) {
  66. //              return false;
  67. //          }
  68.         } while (IsValidTile(bridge_tile) && IsWaterTile(bridge_tile) && !IsSea(bridge_tile));
  69.     } else {
  70.         do {
  71.             if (bridge_length++ >= 11) {
  72.                 /* Max 11 tile long bridges */
  73.                 return false;
  74.             }
  75.             bridge_tile += delta;
  76.             if (IsPossibleLockLocationOnTownBridgeTile(bridge_tile, bridge_dir)) return false;
  77. //          if (IsValidTile(bridge_tile) && IsPossibleLockLocationOnDiagDir(bridge_tile, bridge_dir, true) && IsWaterTile(bridge_tile) ||
  78. //                  IsValidTile(bridge_tile + delta_side) && IsPossibleLockLocationOnDiagDir(bridge_tile + delta_side, ChangeDiagDir(bridge_dir, DIAGDIRDIFF_90LEFT), true) && IsWaterTile(bridge_tile + delta_side) ||
  79. //                  IsValidTile(bridge_tile - delta_side) && IsPossibleLockLocationOnDiagDir(bridge_tile - delta_side, ChangeDiagDir(bridge_dir, DIAGDIRDIFF_90RIGHT), true) && IsWaterTile(bridge_tile - delta_side)) {
  80. //              return false;
  81. //          }
  82.         } while (IsValidTile(bridge_tile) && IsWaterTile(bridge_tile));
  83.     }
  84.  
  85.     /* no water tiles in between? */
  86.     if (bridge_length == 1) return false;
  87.  
  88.     for (uint8 times = 0; times <= 22; times++) {
  89.         byte bridge_type = RandomRange(MAX_BRIDGES - 1);
  90.  
  91.         /* Can we actually build the bridge? */
  92.         if (DoCommand(tile, bridge_tile, bridge_type | ROADTYPES_ROAD << 8 | TRANSPORT_ROAD << 15, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)), CMD_BUILD_BRIDGE).Succeeded()) {
  93.             DoCommand(tile, bridge_tile, bridge_type | ROADTYPES_ROAD << 8 | TRANSPORT_ROAD << 15, DC_EXEC | CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)), CMD_BUILD_BRIDGE);
  94.             _grow_town_result = GROWTH_SUCCEED;
  95.             return true;
  96.         }
  97.     }
  98.     /* Quit if it selecting an appropriate bridge type fails a large number of times. */
  99.     return false;
  100. }

Comments