/**
* 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 (!HasTileWaterGround(tile) || !HasTileWaterGround(tile2)) {
return_cmd_error(STR_ERROR_MUST_BE_BUILT_ON_WATER);
}
if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {
/* Prevent depots on rapids */
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
}
if (!Depot::CanAllocateItem()) return CMD_ERROR;
CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_DEPOT_SHIP]);
WaterClass wc1 = GetWaterClass(tile);
WaterClass wc2 = GetWaterClass(tile2);
bool river1 = HasTileCanalOnRiver(tile);
bool river2 = HasTileCanalOnRiver(tile2);
Owner oc1 = wc1 == WATER_CLASS_CANAL ? GetCanalOwner(tile) : INVALID_OWNER;
Owner oc2 = wc2 == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : INVALID_OWNER;
bool add_cost;
CommandCost ret;
/* At this point we got a flat tile with water at the ground and 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 {
add_cost = !IsWaterTile(tile);
ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
if (ret.Failed()) return ret;
if (add_cost) {
cost.AddCost(ret);
}
}
/* At this point we got a flat tile2 with water at the ground and 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 {
add_cost = !IsWaterTile(tile2);
ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
if (ret.Failed()) return ret;
if (add_cost) {
cost.AddCost(ret);
}
}
if (flags & DC_EXEC) {
Depot *depot = new Depot(tile);
depot->build_date = _date;
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);
if (river1) SetCanalOnRiver(tile);
if (river2) SetCanalOnRiver(tile2);
MarkTileDirtyByTile(tile);
MarkTileDirtyByTile(tile2);
MakeDefaultName(depot);
}
return cost;
}