Loading

Revision differences

Old revision #pdynurwc5New revision #p3elkxu0z
11{  11{  
12    Axis axis = Extract<Axis, 0, 1>(p1);  12    Axis axis = Extract<Axis, 0, 1>(p1);  
13  13  
14    TileIndex tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));  14    TileIndexDiff diff = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
   15    TileIndex tile2 = tile + diff;
15  16  
16    if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);  17    if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);  
17  18  
  
24    Owner oc1 = HasTileWaterGround(tile) && GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : _current_company;  25    Owner oc1 = HasTileWaterGround(tile) && GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : _current_company;  
25    bool add_cost1 = !IsWaterTile(tile);  26    bool add_cost1 = !IsWaterTile(tile);  
26  27  
27    /* At this point we got a flat tile with no bridge over it. Check for ownership */  27    /* At this point we got a tile with no bridge over it. Check for ownership */
28    if (IsWaterTile(tile) && IsCanal(tile)) {  29    if (IsWaterTile(tile) && IsCanal(tile)) {  
29        ret = EnsureNoVehicleOnGround(tile);  30        ret = EnsureNoVehicleOnGround(tile);  
30        if (ret.Failed()) return ret;  31        if (ret.Failed()) return ret;  
  
45    Owner oc2 = HasTileWaterGround(tile2) && GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : _current_company;  46    Owner oc2 = HasTileWaterGround(tile2) && GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : _current_company;  
46    bool add_cost2 = !IsWaterTile(tile2);  47    bool add_cost2 = !IsWaterTile(tile2);  
47  48  
48    /* At this point we got a flat tile2 with no bridge over it. Check for ownership */  48    /* At this point we got a tile2 with no bridge over it. Check for ownership */
49    if (IsWaterTile(tile2) && IsCanal(tile2)) {  50    if (IsWaterTile(tile2) && IsCanal(tile2)) {  
50        ret = EnsureNoVehicleOnGround(tile2);  51        ret = EnsureNoVehicleOnGround(tile2);  
51        if (ret.Failed()) return ret;  52        if (ret.Failed()) return ret;  
  
62        }  63        }  
63    }  64    }  
64  65  
65    if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {  66    int h_north_corner_n = TileHeight(tile);
66        int z_slope1;  67    int h_north_corner_s = TileHeight(TileAddByDiagDir(tile, AxisToDiagDir(OtherAxis(axis))));
67        Slope slope1 = GetTileSlope(tile, &z_slope1);  68    int h_middle_corner_n = TileHeight(tile2);
68        int z_middle_corner_n = TileHeight(tile2);  69    int h_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
69        int z_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));  70    int h_south_corner_n = TileHeight(tile2 + diff);
70        int z_slope2;  71    int h_south_corner_s = TileHeight(TileAddByDiagDir(tile2 + diff, AxisToDiagDir(OtherAxis(axis))));
71        Slope slope2 = GetTileSlope(tile2, &z_slope2);  72
72  73    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};
73        /* Forbid these combinations. */  74
74        if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  75    int min_h = MAX_TILE_HEIGHT;
75        if (slope1 == SLOPE_NS && slope2 == SLOPE_EW || slope1 == SLOPE_EW && slope2 == SLOPE_NS) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  76    int max_h = 0;
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    for (int i = 0; i < lengthof(corners); i++) {
77        if (slope1 == slope2 && IsInclinedSlope(slope1)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  78        min_h = min(min_h, corners[i]);
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        max_h = max(max_h, corners[i]);
79  80    }
80        /* Terraform these combinations. */  81
81        if (slope1 == SLOPE_FLAT || slope2 == SLOPE_FLAT) {  82//    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)))));
82            /* Mark the tile as already cleared for the terraform command. */  83//    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)))));
83            ClearedObjectArea *coa = MakeClearedObjectArea(slope1 == SLOPE_FLAT ? tile2 : tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);  84
84            /* Hide the tile from the terraforming command. */  85    int best_h;
85            TileIndex tile_flat_before = coa->first_tile;  86    int max_delta = MAX_TILE_HEIGHT;
86            coa->first_tile = INVALID_TILE;  87    for (int h = min_h; h <= max_h; h++) {
87            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);  88        int delta = 0;
88            coa->first_tile = tile_flat_before;  89        for (int c = 0; c < lengthof(corners); c++) {
89            if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  90            delta = max(delta, Delta(h, corners[c]));
90            cost.AddCost(ret);  91        }
91        } else {  92        if (delta < max_delta) {
92            if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {  93            max_delta = delta;
93                /* Mark the tile as already cleared for the terraform command. */  94            best_h = h;
94                ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);  95        }
95                /* Hide the tile from the terraforming command. */  96    }
96                TileIndex tile_3corner1_before = coa->first_tile;  97
97                coa->first_tile = INVALID_TILE;  98//    if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {
98                ret = DoCommand(tile, IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope1) : slope1, IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);  99//        int z_slope1;
99                coa->first_tile = tile_3corner1_before;  100//        Slope slope1 = GetTileSlope(tile, &z_slope1);
100                if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  101//        int z_middle_corner_n = TileHeight(tile2);
101                cost.AddCost(ret);  102//        int z_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
102                if (z_middle_corner_n == z_middle_corner_s) {  103//        int z_slope2;
103                    /* Mark the tile as already cleared for the terraform command. */  104//        Slope slope2 = GetTileSlope(tile2, &z_slope2);
104                    ClearedObjectArea *coa = MakeClearedObjectArea(tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);  105
105                    /* Hide the tile from the terraforming command. */  106//        /* Forbid these combinations. */
106                    TileIndex tile_3corner2_before = coa->first_tile;  107//        if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
107                    coa->first_tile = INVALID_TILE;  108//        if (slope1 == SLOPE_NS && slope2 == SLOPE_EW || slope1 == SLOPE_EW && slope2 == SLOPE_NS) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
108                    ret = DoCommand(tile2, IsSlopeWithThreeCornersRaised(slope2) ? ComplementSlope(slope2) : slope2, IsSlopeWithThreeCornersRaised(slope2) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);  109//        if (z_middle_corner_n != z_middle_corner_s && (IsSlopeWithThreeCornersRaised(slope1) && IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithOneCornerRaised(slope1) && IsSlopeWithThreeCornersRaised(slope2))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
109                    coa->first_tile = tile_3corner2_before;  110//        if (slope1 == slope2 && IsInclinedSlope(slope1)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
110                    if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  111//        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);
111                    cost.AddCost(ret);  112//        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);
112                }  113
113            } else {  114//        /* Terraform these combinations. */
114                if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2))) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)))) {  115//        if (slope1 == SLOPE_FLAT || slope2 == SLOPE_FLAT) {
115                    /* Mark the tile as already cleared for the terraform command. */  116//            /* Mark the tile as already cleared for the terraform command. */
116                    ClearedObjectArea *coa = MakeClearedObjectArea(IsInclinedSlope(slope1) ? tile : tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);  117//            ClearedObjectArea *coa = MakeClearedObjectArea(slope1 == SLOPE_FLAT ? tile2 : tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
117                    /* Hide the tile from the terraforming command. */  118//            /* Hide the tile from the terraforming command. */
118                    TileIndex tile_inclined1_before = coa->first_tile;  119//            TileIndex tile_flat_before = coa->first_tile;
119                    coa->first_tile = INVALID_TILE;  120//            coa->first_tile = INVALID_TILE;
120                    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);  121//            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);
121                    coa->first_tile = tile_inclined1_before;  122//            coa->first_tile = tile_flat_before;
122                    if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  123//            if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
123                    cost.AddCost(ret);  124//            cost.AddCost(ret);
124                } else {  125//        } else {
125                    if ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || (slope2 == SLOPE_NS || slope2 == SLOPE_EW) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {  126//            if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {
126                        /* Mark the tile as already cleared for the terraform command. */  127//                /* Mark the tile as already cleared for the terraform command. */
127                        ClearedObjectArea *coa = MakeClearedObjectArea((slope1 == SLOPE_EW || slope1 == SLOPE_NS), tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);  128//                ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
128                        /* Hide the tile from the terraforming command. */  129//                /* Hide the tile from the terraforming command. */
129                        TileIndex tile_oppositecorners_before = coa->first_tile;  130//                TileIndex tile_3corner1_before = coa->first_tile;
130                        coa->first_tile = INVALID_TILE;  131//                coa->first_tile = INVALID_TILE;
131                        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);  132//                ret = DoCommand(tile, IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope1) : slope1, IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
132                        coa->first_tile = tile_oppositecorners_before;  133//                coa->first_tile = tile_3corner1_before;
133                        if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  134//                if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
134                        cost.AddCost(ret);  135//                cost.AddCost(ret);
135                    } else {  136//                if (z_middle_corner_n == z_middle_corner_s) {
136                        if (slope1 == ComplementSlope(slope2) && IsInclinedSlope(slope1)) {  137//                    /* Mark the tile as already cleared for the terraform command. */
137                            /* Mark the tile as already cleared for the terraform command. */  138//                    ClearedObjectArea *coa = MakeClearedObjectArea(tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
138                            ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);  139//                    /* Hide the tile from the terraforming command. */
139                            /* Hide the tile from the terraforming command. */  140//                    TileIndex tile_3corner2_before = coa->first_tile;
140                            TileIndex tile_complement_before = coa->first_tile;  141//                    coa->first_tile = INVALID_TILE;
141                            coa->first_tile = INVALID_TILE;  142//                    ret = DoCommand(tile2, IsSlopeWithThreeCornersRaised(slope2) ? ComplementSlope(slope2) : slope2, IsSlopeWithThreeCornersRaised(slope2) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
142                            ret = DoCommand(tile, z_slope1 > z_slope2 ? slope2 : slope1, z_slope1 > z_slope2 ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);  143//                    coa->first_tile = tile_3corner2_before;
143                            coa->first_tile = tile_complement_before;  144//                    if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
144                            if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  145//                    cost.AddCost(ret);
145                            cost.AddCost(ret);  146//                }
146                        } else {  147//            } else {
147                            /* Shouldn't reach here. */  148//                if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2))) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)))) {
148                            assert(false);  149//                    /* Mark the tile as already cleared for the terraform command. */
149                        }  150//                    ClearedObjectArea *coa = MakeClearedObjectArea(IsInclinedSlope(slope1) ? tile : tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
150                    }  151//                    /* Hide the tile from the terraforming command. */
151                }  152//                    TileIndex tile_inclined1_before = coa->first_tile;
152            }  153//                    coa->first_tile = INVALID_TILE;
153        }  154//                    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);
154    }  155//                    coa->first_tile = tile_inclined1_before;
   156//                    if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
   157//                    cost.AddCost(ret);
   158//                } else {
   159//                    if ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || (slope2 == SLOPE_NS || slope2 == SLOPE_EW) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {
   160//                        /* Mark the tile as already cleared for the terraform command. */
   161//                        ClearedObjectArea *coa = MakeClearedObjectArea((slope1 == SLOPE_EW || slope1 == SLOPE_NS) ? tile : tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
   162//                        /* Hide the tile from the terraforming command. */
   163//                        TileIndex tile_oppositecorners_before = coa->first_tile;
   164//                        coa->first_tile = INVALID_TILE;
   165//                        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);
   166//                        coa->first_tile = tile_oppositecorners_before;
   167//                        if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
   168//                        cost.AddCost(ret);
   169//                    } else {
   170//                        if (slope1 == ComplementSlope(slope2) && IsInclinedSlope(slope1)) {
   171//                            /* Mark the tile as already cleared for the terraform command. */
   172//                            ClearedObjectArea *coa = MakeClearedObjectArea(tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
   173//                            /* Hide the tile from the terraforming command. */
   174//                            TileIndex tile_complement_before = coa->first_tile;
   175//                            coa->first_tile = INVALID_TILE;
   176//                            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);
   177//                            coa->first_tile = tile_complement_before;
   178//                            if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
   179//                            cost.AddCost(ret);
   180//                        } else {
   181//                            /* Shouldn't reach here. */
   182//                            assert(false);
   183//                        }
   184//                    }
   185//                }
   186//            }
   187//        }
   188//    }
   189
   190        if (!IsTileFlat(tile) || !IsTileFlat(tile2)) return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
155  191  
156    DiagDirection dir_rotate = axis == AXIS_X ? DIAGDIR_SE : DIAGDIR_NE;  192    DiagDirection dir_rotate = axis == AXIS_X ? DIAGDIR_SE : DIAGDIR_NE;  
157    TileIndex tc = tile + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));  193    TileIndex tc = tile + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));  
  
188    }  224    }  
189  225  
190    return cost;  226    return cost;  
  227}  
  228  
  229void MakeWaterKeepingClass(TileIndex tile, Owner o)  
  230{  
  231    WaterClass wc = GetWaterClass(tile);  
  232  
  233    /* Autoslope might turn an originally canal or river tile into land */  
  234    int z;  
  235    Slope slope = GetTileSlope(tile, &z);  
  236  
  237    if (slope != SLOPE_FLAT) {  
  238        if (wc == WATER_CLASS_CANAL) {  
  239            /* If we clear the canal, we have to remove it from the infrastructure count as well. */  
  240            Company *c = Company::GetIfValid(o);  
  241            if (c != NULL) {  
  242                c->infrastructure.water--;  
  243                DirtyCompanyInfrastructureWindows(c->index);  
  244            }  
  245            /* Sloped canals are locks and no natural water remains whatever the slope direction */  
  246            wc = WATER_CLASS_INVALID;  
  247        }  
  248  
  249        /* Only river water should be restored on appropriate slopes. Other water would be invalid on slopes */  
  250        if (wc != WATER_CLASS_RIVER || GetInclinedSlopeDirection(slope) == INVALID_DIAGDIR) {  
  251            wc = WATER_CLASS_INVALID;  
  252        }  
  253    }  
  254  
  255    if (wc == WATER_CLASS_SEA && z > 0) {  
  256        /* Update company infrastructure count. */  
  257        Company *c = Company::GetIfValid(o);  
  258        if (c != NULL) {  
  259            c->infrastructure.water++;  
  260            DirtyCompanyInfrastructureWindows(c->index);  
  261        }  
  262  
  263        wc = WATER_CLASS_CANAL;  
  264    }  
  265  
  266    bool river = HasTileCanalOnRiver(tile);  
  267    /* Zero map array and terminate animation */  
  268    DoClearSquare(tile);  
  269  
  270    /* Maybe change to water */  
  271    switch (wc) {  
  272        case WATER_CLASS_SEA:  
  273            MakeSea(tile);  
  274            break;  
  275  
  276        case WATER_CLASS_CANAL:  
  277            MakeCanal(tile, o, Random());  
  278            if (river) SetCanalOnRiver(tile);  
  279            break;  
  280  
  281        case WATER_CLASS_RIVER:  
  282            MakeRiver(tile, Random());  
  283            break;  
  284  
  285        default:  
  286            break;  
  287    }  
  288  
  289    MarkTileDirtyByTile(tile);  
  290}  
  291  
  292static CommandCost RemoveShipDepot(TileIndex tile, DoCommandFlag flags)  
  293{  
  294    if (!IsShipDepot(tile)) return CMD_ERROR;  
  295  
  296    CommandCost ret = CheckTileOwnership(tile);  
  297    if (ret.Failed()) return ret;  
  298    Owner oc1 = GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : INVALID_OWNER;  
  299  
  300    TileIndex tile2 = GetOtherShipDepotTile(tile);  
  301    Owner oc2 = GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : INVALID_OWNER;  
  302  
  303    /* do not check for ship on tile when company goes bankrupt */  
  304    if (!(flags & DC_BANKRUPT)) {  
  305        CommandCost ret = EnsureNoVehicleOnGround(tile);  
  306        if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile2);  
  307        if (ret.Failed()) return ret;  
  308    }  
  309  
  310    if (flags & DC_EXEC) {  
  311        delete Depot::GetByTile(tile);  
  312  
  313        Company *c = Company::GetIfValid(GetTileOwner(tile));  
  314        if (c != NULL) {  
  315            c->infrastructure.water -= 2 * LOCK_DEPOT_TILE_FACTOR;  
  316            DirtyCompanyInfrastructureWindows(c->index);  
  317        }  
  318  
  319        MakeWaterKeepingClass(tile, oc1);  
  320        MakeWaterKeepingClass(tile2, oc2);  
  321    }  
  322  
  323    return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_SHIP]);  
191} 324}