/** * 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(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]); } } if (!IsTileFlat(tile) || !IsTileFlat(tile2)) { Slope slope1 = GetTileSlope(tile); Slope slope2 = GetTileSlope(tile2); if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE); 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 best_h = INT_MAX; int second_best_h = INT_MAX; int best_sum = INT_MAX; int second_best_sum = INT_MAX; for (int h = min_h; h <= max_h; h++) { int sum = 0; for (int c = 0; c < lengthof(corners); c++) { sum += abs(h - corners[c]); } if (sum <= best_sum) { second_best_sum = best_sum; best_sum = sum; second_best_h = best_h; best_h = h; } } if (best_sum != second_best_sum) { int command1p2 = -1; Slope tileh1 = SLOPE_FLAT; if (h_north_corner_n > best_h) { command1p2 = 0; tileh1 |= SLOPE_N; } else if (h_north_corner_n < best_h) { command1p2 = 1; tileh1 |= SLOPE_N; } if (h_north_corner_s > best_h) { command1p2 = 0; tileh1 |= axis == AXIS_X ? SLOPE_E : SLOPE_W; } else if (h_north_corner_s < best_h) { command1p2 = 1; tileh1 |= axis == AXIS_X ? SLOPE_E : SLOPE_W; } int command2p2 = -1; Slope tileh2 = SLOPE_FLAT; if (h_south_corner_n > best_h) { command2p2 = 0; tileh2 |= axis == AXIS_X ? SLOPE_W : SLOPE_E; } else if (h_south_corner_n < best_h) { command2p2 = 1; tileh2 |= axis == AXIS_X ? SLOPE_W : SLOPE_E; } if (h_south_corner_s > best_h) { command2p2 = 0; tileh2 |= SLOPE_S; } else if (h_south_corner_s < best_h) { command2p2 = 1; tileh2 |= SLOPE_S; } if (h_middle_corner_n > best_h) { if (tileh1 != SLOPE_FLAT) { command1p2 = 0; tileh1 |= axis == AXIS_X ? SLOPE_W : SLOPE_E; } else { command2p2 = 0; tileh2 |= SLOPE_N; } } else if (h_middle_corner_n < best_h) { if (tileh1 != SLOPE_FLAT) { command1p2 = 1; tileh1 |= axis == AXIS_X ? SLOPE_W : SLOPE_E; } else { command2p2 = 1; tileh2 |= SLOPE_N; } } if (h_middle_corner_s > best_h) { if (tileh1 != SLOPE_FLAT) { command1p2 = 0; tileh1 |= SLOPE_S; } else { command2p2 = 0; tileh2 |= axis == AXIS_X ? SLOPE_E : SLOPE_W; } } else if (h_middle_corner_s < best_h) { if (tileh1 != SLOPE_FLAT) { command1p2 = 1; tileh1 |= SLOPE_S; } else { command2p2 = 1; tileh2 |= axis == AXIS_X ? SLOPE_E : SLOPE_W; } } if (tileh1 != SLOPE_FLAT && command1p2 != -1) { /* 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 tile1_before = coa->first_tile; coa->first_tile = INVALID_TILE; ret = DoCommand(tile, tileh1, command1p2, flags | DC_NO_WATER, CMD_TERRAFORM_LAND); coa->first_tile = tile1_before; if (ret.Failed()) return ret; cost.AddCost(ret); } if (tileh2 != SLOPE_FLAT && command2p2 != -1) { /* 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 tile2_before = coa->first_tile; coa->first_tile = INVALID_TILE; ret = DoCommand(tile2, tileh2, command2p2, flags | DC_NO_WATER, CMD_TERRAFORM_LAND); coa->first_tile = tile2_before; if (ret.Failed()) return ret; cost.AddCost(ret); } } else { return_cmd_error(STR_ERROR_SITE_UNSUITABLE); } } // 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; // int c_count = 0; // for (int c = 0; c < lengthof(corners); c++) { // if (corners[c] == h) { // c_count++; // } // } // 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. */ /** * 3 * / \ * 2 2 * / \ / * 1 1 * \ / * 0 */ // if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE); /** * 1 0 * / \ / \ * 0 0 1 1 * / \ / / \ / * 1 1 0 0 * \ / \ / * 0 1 */ // if (slope1 == SLOPE_NS && slope2 == SLOPE_EW || slope1 == SLOPE_EW && slope2 == SLOPE_NS) return_cmd_error(STR_ERROR_SITE_UNSUITABLE); /** * 1 * / \ * 0 1 * / \ / * 0 1 * \ / * 0 */ // if (z_middle_corner_n != z_middle_corner_s && (IsSlopeWithThreeCornersRaised(slope1) && IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithOneCornerRaised(slope1) && IsSlopeWithThreeCornersRaised(slope2))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE); /** * 2 1 * / \ / \ * 1 2 1 0 * / \ / / \ / * 0 1 1 0 * \ / \ / * 0 0 */ // if (slope1 == slope2 && IsInclinedSlope(slope1)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE); /** * 1 2 0 * / \ / \ / \ * 0 1 1 2 1 0 * / \ / / \ / / \ / * 0 0 1 1 1 1 * \ / \ / \ / * 1 0 2 */ // 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); /** * 0 * / \ * 0 1 * / \ / * 1 1 * \ / * 0 */ // 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; }