/**
* Build a ship depot.
* @param tile tile where ship depot is built
* @param flags type of operation
* @param p1 bit 0 depot orientation (Axis)
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdBuildShipDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
Axis axis = Extract<Axis, 0, 1>(p1);
TileIndex tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
if (!Depot::CanAllocateItem()) return CMD_ERROR;
CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_DEPOT_SHIP]);
CommandCost ret;
WaterClass wc1 = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;
Owner oc1 = HasTileWaterGround(tile) && GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : _current_company;
bool add_cost1 = !IsWaterTile(tile);
/* At this point we got a flat tile with no bridge over it. Check for ownership */
if (IsWaterTile(tile) && IsCanal(tile)) {
ret = EnsureNoVehicleOnGround(tile);
if (ret.Failed()) return ret;
if (oc1 != OWNER_NONE) {
ret = CheckTileOwnership(tile);
if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;
}
} else {
ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
if (ret.Failed()) return ret;
if (add_cost1) {
cost.AddCost(ret);
if (wc1 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);
}
}
WaterClass wc2 = IsWaterTile(tile2) ? GetWaterClass(tile2) : WATER_CLASS_CANAL;
Owner oc2 = HasTileWaterGround(tile2) && GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : _current_company;
bool add_cost2 = !IsWaterTile(tile2);
/* At this point we got a flat tile2 with no bridge over it. Check for ownership */
if (IsWaterTile(tile2) && IsCanal(tile2)) {
ret = EnsureNoVehicleOnGround(tile2);
if (ret.Failed()) return ret;
if (oc2 != OWNER_NONE) {
ret = CheckTileOwnership(tile2);
if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;
}
} else {
ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
if (ret.Failed()) return ret;
if (add_cost2) {
cost.AddCost(ret);
if (wc2 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);
}
}
if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {
int z_slope1;
Slope slope1 = GetTileSlope(tile, &z_slope1);
int z_middle_corner_n = TileHeight(tile2);
int z_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
int z_slope2;
Slope slope2 = GetTileSlope(tile2, &z_slope2);
/* Forbid these combinations. */
if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
if (slope1 == SLOPE_NS && slope2 == SLOPE_EW || slope1 == SLOPE_EW && slope2 == SLOPE_NS) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
if (z_middle_corner_n != z_middle_corner_s && (IsSlopeWithThreeCornersRaised(slope1) && IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithOneCornerRaised(slope1) && IsSlopeWithThreeCornersRaised(slope2))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
if (slope1 == slope2 && IsInclinedSlope(slope1)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
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);
/* Terraform these combinations. */
if (slope1 == SLOPE_FLAT || slope2 == SLOPE_FLAT) {
/* Mark the tile as already cleared for the terraform command. */
ClearedObjectArea *coa = MakeClearedObjectArea(slope1 == SLOPE_FLAT ? tile2 : tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
/* Hide the tile from the terraforming command. */
TileIndex tile_flat_before = coa->first_tile;
coa->first_tile = INVALID_TILE;
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);
coa->first_tile = tile_flat_before;
if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
cost.AddCost(ret);
} else {
if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {
/* Mark the tile as already cleared for the terraform command. */
ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
/* Hide the tile from the terraforming command. */
TileIndex tile_3corner1_before = coa->first_tile;
coa->first_tile = INVALID_TILE;
ret = DoCommand(tile, IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope1) : slope1, IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
coa->first_tile = tile_3corner1_before;
if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
cost.AddCost(ret);
if (z_middle_corner_n == z_middle_corner_s) {
/* Mark the tile as already cleared for the terraform command. */
ClearedObjectArea *coa = MakeClearedObjectArea(tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
/* Hide the tile from the terraforming command. */
TileIndex tile_3corner2_before = coa->first_tile;
coa->first_tile = INVALID_TILE;
ret = DoCommand(tile2, IsSlopeWithThreeCornersRaised(slope2) ? ComplementSlope(slope2) : slope2, IsSlopeWithThreeCornersRaised(slope2) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
coa->first_tile = tile_3corner2_before;
if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
cost.AddCost(ret);
}
} else {
if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2))) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)))) {
/* Mark the tile as already cleared for the terraform command. */
ClearedObjectArea *coa = MakeClearedObjectArea(IsInclinedSlope(slope1) ? tile : tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
/* Hide the tile from the terraforming command. */
TileIndex tile_inclined1_before = coa->first_tile;
coa->first_tile = INVALID_TILE;
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);
coa->first_tile = tile_inclined1_before;
if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
cost.AddCost(ret);
} else {
if ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || (slope2 == SLOPE_NS || slope2 == SLOPE_EW) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {
/* Mark the tile as already cleared for the terraform command. */
ClearedObjectArea *coa = MakeClearedObjectArea((slope1 == SLOPE_EW || slope1 == SLOPE_NS), tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
/* Hide the tile from the terraforming command. */
TileIndex tile_oppositecorners_before = coa->first_tile;
coa->first_tile = INVALID_TILE;
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);
coa->first_tile = tile_oppositecorners_before;
if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
cost.AddCost(ret);
} else {
if (slope1 == ComplementSlope(slope2) && IsInclinedSlope(slope1)) {
/* Mark the tile as already cleared for the terraform command. */
ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
/* Hide the tile from the terraforming command. */
TileIndex tile_complement_before = coa->first_tile;
coa->first_tile = INVALID_TILE;
ret = DoCommand(tile, z_slope1 > z_slope2 ? slope2 : slope1, z_slope1 > z_slope2 ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
coa->first_tile = tile_complement_before;
if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
cost.AddCost(ret);
} else {
/* Shouldn't reach here. */
assert(false);
}
}
}
}
}
}
DiagDirection dir_rotate = axis == AXIS_X ? DIAGDIR_SE : DIAGDIR_NE;
TileIndex tc = tile + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));
ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
if (ret.Failed()) return ret;
tc = tile2 + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));
ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
if (ret.Failed()) return ret;
dir_rotate = ReverseDiagDir(dir_rotate);
tc = tile + (axis == AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(-1, 0));
ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
if (ret.Failed()) return ret;
tc = tile2 + (axis == AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(-1, 0));
ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
if (ret.Failed()) return ret;
if (flags & DC_EXEC) {
Depot *depot = new Depot(tile);
depot->build_date = _date;
if (add_cost1 && wc1 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
if (add_cost2 && wc2 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
Company::Get(_current_company)->infrastructure.water += 2 * LOCK_DEPOT_TILE_FACTOR;
DirtyCompanyInfrastructureWindows(_current_company);
MakeShipDepot(tile, _current_company, oc1, depot->index, DEPOT_PART_NORTH, axis, wc1);
MakeShipDepot(tile2, _current_company, oc2, depot->index, DEPOT_PART_SOUTH, axis, wc2);
MarkTileDirtyByTile(tile);
MarkTileDirtyByTile(tile2);
MakeDefaultName(depot);
}
return cost;
}