Loading

Paste #pdynurwc5

  1. /**
  2.  * Build a ship depot.
  3.  * @param tile tile where ship depot is built
  4.  * @param flags type of operation
  5.  * @param p1 bit 0 depot orientation (Axis)
  6.  * @param p2 unused
  7.  * @param text unused
  8.  * @return the cost of this operation or an error
  9.  */
  10. CommandCost CmdBuildShipDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
  11. {
  12.     Axis axis = Extract<Axis, 0, 1>(p1);
  13.  
  14.     TileIndex tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
  15.  
  16.     if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
  17.  
  18.     if (!Depot::CanAllocateItem()) return CMD_ERROR;
  19.  
  20.     CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_DEPOT_SHIP]);
  21.     CommandCost ret;
  22.  
  23.     WaterClass wc1 = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;
  24.     Owner oc1 = HasTileWaterGround(tile) && GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : _current_company;
  25.     bool add_cost1 = !IsWaterTile(tile);
  26.  
  27.     /* At this point we got a flat tile with no bridge over it. Check for ownership */
  28.     if (IsWaterTile(tile) && IsCanal(tile)) {
  29.         ret = EnsureNoVehicleOnGround(tile);
  30.         if (ret.Failed()) return ret;
  31.         if (oc1 != OWNER_NONE) {
  32.             ret = CheckTileOwnership(tile);
  33.             if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;
  34.         }
  35.     } else {
  36.         ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  37.         if (ret.Failed()) return ret;
  38.         if (add_cost1) {
  39.             cost.AddCost(ret);
  40.             if (wc1 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);
  41.         }
  42.     }
  43.  
  44.     WaterClass wc2 = IsWaterTile(tile2) ? GetWaterClass(tile2) : WATER_CLASS_CANAL;
  45.     Owner oc2 = HasTileWaterGround(tile2) && GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : _current_company;
  46.     bool add_cost2 = !IsWaterTile(tile2);
  47.  
  48.     /* At this point we got a flat tile2 with no bridge over it. Check for ownership */
  49.     if (IsWaterTile(tile2) && IsCanal(tile2)) {
  50.         ret = EnsureNoVehicleOnGround(tile2);
  51.         if (ret.Failed()) return ret;
  52.         if (oc2 != OWNER_NONE) {
  53.             ret = CheckTileOwnership(tile2);
  54.             if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;
  55.         }
  56.     } else {
  57.         ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  58.         if (ret.Failed()) return ret;
  59.         if (add_cost2) {
  60.             cost.AddCost(ret);
  61.             if (wc2 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);
  62.         }
  63.     }
  64.  
  65.     if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {
  66.         int z_slope1;
  67.         Slope slope1 = GetTileSlope(tile, &z_slope1);
  68.         int z_middle_corner_n = TileHeight(tile2);
  69.         int z_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
  70.         int z_slope2;
  71.         Slope slope2 = GetTileSlope(tile2, &z_slope2);
  72.  
  73.         /* Forbid these combinations. */
  74.         if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  75.         if (slope1 == SLOPE_NS && slope2 == SLOPE_EW || slope1 == SLOPE_EW && slope2 == SLOPE_NS) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  76.         if (z_middle_corner_n != z_middle_corner_s && (IsSlopeWithThreeCornersRaised(slope1) && IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithOneCornerRaised(slope1) && IsSlopeWithThreeCornersRaised(slope2))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  77.         if (slope1 == slope2 && IsInclinedSlope(slope1)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  78.         if (((IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2) || slope2 == SLOPE_EW || slope2 == SLOPE_NS) && IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == axis) || ((IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1) || slope1 == SLOPE_EW || slope1 == SLOPE_NS) && IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == axis)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  79.  
  80.         /* Terraform these combinations. */
  81.         if (slope1 == SLOPE_FLAT || slope2 == SLOPE_FLAT) {
  82.             /* Mark the tile as already cleared for the terraform command. */
  83.             ClearedObjectArea *coa = MakeClearedObjectArea(slope1 == SLOPE_FLAT ? tile2 : tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  84.             /* Hide the tile from the terraforming command. */
  85.             TileIndex tile_flat_before = coa->first_tile;
  86.             coa->first_tile = INVALID_TILE;
  87.             ret = DoCommand(slope1 == SLOPE_FLAT ? tile2 : tile, slope1 == SLOPE_FLAT ? z_slope1 == z_slope2 ? slope2: ComplementSlope(slope2) : z_slope1 == z_slope2 ? slope1 : ComplementSlope(slope1), z_slope1 == z_slope2 ? 0 : 1, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  88.             coa->first_tile = tile_flat_before;
  89.             if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  90.             cost.AddCost(ret);
  91.         } else {
  92.             if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {
  93.                 /* Mark the tile as already cleared for the terraform command. */
  94.                 ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  95.                 /* Hide the tile from the terraforming command. */
  96.                 TileIndex tile_3corner1_before = coa->first_tile;
  97.                 coa->first_tile = INVALID_TILE;
  98.                 ret = DoCommand(tile, IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope1) : slope1, IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  99.                 coa->first_tile = tile_3corner1_before;
  100.                 if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  101.                 cost.AddCost(ret);
  102.                 if (z_middle_corner_n == z_middle_corner_s) {
  103.                     /* Mark the tile as already cleared for the terraform command. */
  104.                     ClearedObjectArea *coa = MakeClearedObjectArea(tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  105.                     /* Hide the tile from the terraforming command. */
  106.                     TileIndex tile_3corner2_before = coa->first_tile;
  107.                     coa->first_tile = INVALID_TILE;
  108.                     ret = DoCommand(tile2, IsSlopeWithThreeCornersRaised(slope2) ? ComplementSlope(slope2) : slope2, IsSlopeWithThreeCornersRaised(slope2) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  109.                     coa->first_tile = tile_3corner2_before;
  110.                     if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  111.                     cost.AddCost(ret);
  112.                 }
  113.             } else {
  114.                 if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2))) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)))) {
  115.                     /* Mark the tile as already cleared for the terraform command. */
  116.                     ClearedObjectArea *coa = MakeClearedObjectArea(IsInclinedSlope(slope1) ? tile : tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  117.                     /* Hide the tile from the terraforming command. */
  118.                     TileIndex tile_inclined1_before = coa->first_tile;
  119.                     coa->first_tile = INVALID_TILE;
  120.                     ret = DoCommand(IsInclinedSlope(slope1) ? tile : tile2, IsInclinedSlope(slope1) ? IsSlopeWithThreeCornersRaised(slope2) ? ComplementSlope(slope1) : slope1 : IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope2) : slope2, IsInclinedSlope(slope1) ? IsSlopeWithThreeCornersRaised(slope2) ? 1 : 0 : IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  121.                     coa->first_tile = tile_inclined1_before;
  122.                     if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  123.                     cost.AddCost(ret);
  124.                 } else {
  125.                     if ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || (slope2 == SLOPE_NS || slope2 == SLOPE_EW) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {
  126.                         /* Mark the tile as already cleared for the terraform command. */
  127.                         ClearedObjectArea *coa = MakeClearedObjectArea((slope1 == SLOPE_EW || slope1 == SLOPE_NS), tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  128.                         /* Hide the tile from the terraforming command. */
  129.                         TileIndex tile_oppositecorners_before = coa->first_tile;
  130.                         coa->first_tile = INVALID_TILE;
  131.                         ret = DoCommand((slope1 == SLOPE_EW || slope1 == SLOPE_NS) ? tile : tile2, (slope1 == SLOPE_EW || slope1 == SLOPE_NS) ? IsSlopeWithOneCornerRaised(slope2) ? slope1 : ComplementSlope(slope1) : IsSlopeWithOneCornerRaised(slope1) ? slope2 : ComplementSlope(slope2), (slope1 == SLOPE_EW || slope1 == SLOPE_NS) ? IsSlopeWithOneCornerRaised(slope2) ? 0 : 1 : IsSlopeWithOneCornerRaised(slope1) ? 0 : 1, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  132.                         coa->first_tile = tile_oppositecorners_before;
  133.                         if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  134.                         cost.AddCost(ret);
  135.                     } else {
  136.                         if (slope1 == ComplementSlope(slope2) && IsInclinedSlope(slope1)) {
  137.                             /* Mark the tile as already cleared for the terraform command. */
  138.                             ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  139.                             /* Hide the tile from the terraforming command. */
  140.                             TileIndex tile_complement_before = coa->first_tile;
  141.                             coa->first_tile = INVALID_TILE;
  142.                             ret = DoCommand(tile, z_slope1 > z_slope2 ? slope2 : slope1, z_slope1 > z_slope2 ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  143.                             coa->first_tile = tile_complement_before;
  144.                             if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  145.                             cost.AddCost(ret);
  146.                         } else {
  147.                             /* Shouldn't reach here. */
  148.                             assert(false);
  149.                         }
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.     }
  155.  
  156.     DiagDirection dir_rotate = axis == AXIS_X ? DIAGDIR_SE : DIAGDIR_NE;
  157.     TileIndex tc = tile + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));
  158.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  159.     if (ret.Failed()) return ret;
  160.  
  161.     tc = tile2 + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));
  162.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  163.     if (ret.Failed()) return ret;
  164.  
  165.     dir_rotate = ReverseDiagDir(dir_rotate);
  166.     tc = tile + (axis == AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(-1, 0));
  167.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  168.     if (ret.Failed()) return ret;
  169.  
  170.     tc = tile2 + (axis == AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(-1, 0));
  171.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  172.     if (ret.Failed()) return ret;
  173.  
  174.     if (flags & DC_EXEC) {
  175.         Depot *depot = new Depot(tile);
  176.         depot->build_date = _date;
  177.  
  178.         if (add_cost1 && wc1 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
  179.         if (add_cost2 && wc2 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
  180.         Company::Get(_current_company)->infrastructure.water += 2 * LOCK_DEPOT_TILE_FACTOR;
  181.         DirtyCompanyInfrastructureWindows(_current_company);
  182.  
  183.         MakeShipDepot(tile, _current_company, oc1, depot->index, DEPOT_PART_NORTH, axis, wc1);
  184.         MakeShipDepot(tile2, _current_company, oc2, depot->index, DEPOT_PART_SOUTH, axis, wc2);
  185.         MarkTileDirtyByTile(tile);
  186.         MarkTileDirtyByTile(tile2);
  187.         MakeDefaultName(depot);
  188.     }
  189.  
  190.     return cost;
  191. }

Comments