/**
* 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);
TileIndexDiff diff = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
TileIndex tile2 = tile + diff;
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 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 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]);
}
}
int h_north_corner_n = TileHeight(tile);
int h_north_corner_s = TileHeight(TileAddByDiagDir(tile, AxisToDiagDir(OtherAxis(axis))));
int h_middle_corner_n = TileHeight(tile2);
int h_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
int h_south_corner_n = TileHeight(tile2 + diff);
int h_south_corner_s = TileHeight(TileAddByDiagDir(tile2 + diff, AxisToDiagDir(OtherAxis(axis))));
int corners[] = {h_north_corner_n, h_north_corner_s, h_middle_corner_n, h_middle_corner_s, h_south_corner_n, h_south_corner_s};
int min_h = MAX_TILE_HEIGHT;
int max_h = 0;
for (int i = 0; i < lengthof(corners); i++) {
min_h = min(min_h, corners[i]);
max_h = max(max_h, corners[i]);
}
// int min_h = min(h_north_corner_n, min(h_north_corner_s, min(h_middle_corner_n, min(h_middle_corner_s, min(h_south_corner_n, h_south_corner_s)))));
// int max_h = max(h_north_corner_n, max(h_north_corner_s, max(h_middle_corner_n, max(h_middle_corner_s, max(h_south_corner_n, h_south_corner_s)))));
int best_h;
int max_delta = MAX_TILE_HEIGHT;
for (int h = min_h; h <= max_h; h++) {
int delta = 0;
for (int c = 0; c < lengthof(corners); c++) {
delta = max(delta, Delta(h, corners[c]));
}
if (delta < max_delta) {
max_delta = delta;
best_h = h;
}
}
// 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)) && IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == axis) || ((IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)) && IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == axis)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
// if (((slope2 == SLOPE_EW || slope2 == SLOPE_NS) && IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis)) || ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(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 : tile2, 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(tile2, 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(tile2, z_middle_corner_n > z_slope1 ? slope2 : slope1, z_middle_corner_n > z_slope1 ? 0 : 1, 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);
// }
// }
// }
// }
// }
// }
if (!IsTileFlat(tile) || !IsTileFlat(tile2)) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
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;
}
void MakeWaterKeepingClass(TileIndex tile, Owner o)
{
WaterClass wc = GetWaterClass(tile);
/* Autoslope might turn an originally canal or river tile into land */
int z;
Slope slope = GetTileSlope(tile, &z);
if (slope != SLOPE_FLAT) {
if (wc == WATER_CLASS_CANAL) {
/* If we clear the canal, we have to remove it from the infrastructure count as well. */
Company *c = Company::GetIfValid(o);
if (c != NULL) {
c->infrastructure.water--;
DirtyCompanyInfrastructureWindows(c->index);
}
/* Sloped canals are locks and no natural water remains whatever the slope direction */
wc = WATER_CLASS_INVALID;
}
/* Only river water should be restored on appropriate slopes. Other water would be invalid on slopes */
if (wc != WATER_CLASS_RIVER || GetInclinedSlopeDirection(slope) == INVALID_DIAGDIR) {
wc = WATER_CLASS_INVALID;
}
}
if (wc == WATER_CLASS_SEA && z > 0) {
/* Update company infrastructure count. */
Company *c = Company::GetIfValid(o);
if (c != NULL) {
c->infrastructure.water++;
DirtyCompanyInfrastructureWindows(c->index);
}
wc = WATER_CLASS_CANAL;
}
bool river = HasTileCanalOnRiver(tile);
/* Zero map array and terminate animation */
DoClearSquare(tile);
/* Maybe change to water */
switch (wc) {
case WATER_CLASS_SEA:
MakeSea(tile);
break;
case WATER_CLASS_CANAL:
MakeCanal(tile, o, Random());
if (river) SetCanalOnRiver(tile);
break;
case WATER_CLASS_RIVER:
MakeRiver(tile, Random());
break;
default:
break;
}
MarkTileDirtyByTile(tile);
}
static CommandCost RemoveShipDepot(TileIndex tile, DoCommandFlag flags)
{
if (!IsShipDepot(tile)) return CMD_ERROR;
CommandCost ret = CheckTileOwnership(tile);
if (ret.Failed()) return ret;
Owner oc1 = GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : INVALID_OWNER;
TileIndex tile2 = GetOtherShipDepotTile(tile);
Owner oc2 = GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : INVALID_OWNER;
/* do not check for ship on tile when company goes bankrupt */
if (!(flags & DC_BANKRUPT)) {
CommandCost ret = EnsureNoVehicleOnGround(tile);
if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile2);
if (ret.Failed()) return ret;
}
if (flags & DC_EXEC) {
delete Depot::GetByTile(tile);
Company *c = Company::GetIfValid(GetTileOwner(tile));
if (c != NULL) {
c->infrastructure.water -= 2 * LOCK_DEPOT_TILE_FACTOR;
DirtyCompanyInfrastructureWindows(c->index);
}
MakeWaterKeepingClass(tile, oc1);
MakeWaterKeepingClass(tile2, oc2);
}
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_SHIP]);
}