Loading

Paste #ppcle1nv7

  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.  
  22.     WaterClass wc1 = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;
  23.     Owner oc1 = HasTileWaterGround(tile) && GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : _current_company;
  24.     bool add_cost1 = !IsWaterTile(tile);
  25.  
  26.     CommandCost ret;
  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 || slope1 == ComplementSlope(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.              * Do this for all tiles (like trees), not only objects. */
  84.             ClearedObjectArea *coa = FindClearedObject(slope1 == SLOPE_FLAT ? tile2 : tile);
  85.             if (coa == NULL) {
  86.                 coa = _cleared_object_areas.Append();
  87.                 coa->first_tile = slope1 == SLOPE_FLAT ? tile2 : tile;
  88.                 coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  89.             }
  90.             /* Hide the tile from the terraforming command */
  91.             TileIndex tile_flat_before = coa->first_tile;
  92.             coa->first_tile = INVALID_TILE;
  93.             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);
  94.             coa->first_tile = tile_flat_before;
  95.             if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  96.             cost.AddCost(ret);
  97.         } else {
  98.             if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {
  99.                 /* Mark the tile as already cleared for the terraform command.
  100.                  * Do this for all tiles (like trees), not only objects. */
  101.                 ClearedObjectArea *coa = FindClearedObject(tile);
  102.                 if (coa == NULL) {
  103.                     coa = _cleared_object_areas.Append();
  104.                     coa->first_tile = tile;
  105.                     coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  106.                 }
  107.                 /* Hide the tile from the terraforming command */
  108.                 TileIndex tile_3corner1_before = coa->first_tile;
  109.                 coa->first_tile = INVALID_TILE;
  110.                 ret = DoCommand(tile, IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope1) : slope1, IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  111.                 coa->first_tile = tile_3corner1_before;
  112.                 if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  113.                 cost.AddCost(ret);
  114.                 if (z_middle_corner_n == z_middle_corner_s) {
  115.                     /* Mark the tile as already cleared for the terraform command.
  116.                      * Do this for all tiles (like trees), not only objects. */
  117.                     ClearedObjectArea *coa = FindClearedObject(tile2);
  118.                     if (coa == NULL) {
  119.                         coa = _cleared_object_areas.Append();
  120.                         coa->first_tile = tile2;
  121.                         coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  122.                     }
  123.                     /* Hide the tile from the terraforming command */
  124.                     TileIndex tile_3corner2_before = coa->first_tile;
  125.                     coa->first_tile = INVALID_TILE;
  126.                     ret = DoCommand(tile2, IsSlopeWithThreeCornersRaised(slope2) ? ComplementSlope(slope2) : slope2, IsSlopeWithThreeCornersRaised(slope2) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  127.                     coa->first_tile = tile_3corner2_before;
  128.                     if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  129.                     cost.AddCost(ret);
  130.                 }
  131.             } else {
  132.                 if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2))) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)))) {
  133.                     /* Mark the tile as already cleared for the terraform command.
  134.                      * Do this for all tiles (like trees), not only objects. */
  135.                     ClearedObjectArea *coa = FindClearedObject(IsInclinedSlope(slope1) ? tile : tile2);
  136.                     if (coa == NULL) {
  137.                         coa = _cleared_object_areas.Append();
  138.                         coa->first_tile = IsInclinedSlope(slope1) ? tile : tile2;
  139.                         coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  140.                     }
  141.                     /* Hide the tile from the terraforming command */
  142.                     TileIndex tile_inclined1_before = coa->first_tile;
  143.                     coa->first_tile = INVALID_TILE;
  144.                     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);
  145.                     coa->first_tile = tile_inclined1_before;
  146.                     if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  147.                     cost.AddCost(ret);
  148.                 } else {
  149.                     if ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || (slope2 == SLOPE_NS || slope2 == SLOPE_EW) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {
  150.                         /* Mark the tile as already cleared for the terraform command.
  151.                          * Do this for all tiles (like trees), not only objects. */
  152.                         ClearedObjectArea *coa = FindClearedObject((slope1 == SLOPE_EW || slope1 == SLOPE_NS) ? tile : tile2);
  153.                         if (coa == NULL) {
  154.                             coa = _cleared_object_areas.Append();
  155.                             coa->first_tile = (slope1 == SLOPE_EW || slope1 == SLOPE_NS) ? tile : tile2;
  156.                             coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  157.                         }
  158.                         /* Hide the tile from the terraforming command */
  159.                         TileIndex tile_oppositecorners_before = coa->first_tile;
  160.                         coa->first_tile = INVALID_TILE;
  161.                         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);
  162.                         coa->first_tile = tile_oppositecorners_before;
  163.                         if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  164.                         cost.AddCost(ret);
  165.                     } else {
  166.                         /* Shouldn't reach here. */
  167.                         assert(false);
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.     }
  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. }

Version history

Revision # Author Created at
pfvyzywvd Anonymous 24 Sep 2016, 12:35:51 UTC Diff

Comments