Loading

Revision differences

Old revision #pfvyzywvdNew revision #ppcle1nv7
  1/**  
  2 * Build a ship depot.  
  3 * @param tile tile where ship depot is built  
  4 * @param flags type of operation  
  5 * @param p1 bit 0 depot orientation (Axis)  
  6 * @param p2 unused  
  7 * @param text unused  
  8 * @return the cost of this operation or an error  
  9 */  
  10CommandCost CmdBuildShipDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)  
  11{  
  12    Axis axis = Extract<Axis, 0, 1>(p1);  
  13  
  14    TileIndex tile2 = tile + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));  
  15  
  16    if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);  
  17  
  18    if (!Depot::CanAllocateItem()) return CMD_ERROR;  
  19  
  20    CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_DEPOT_SHIP]);  
  21  
  22    WaterClass wc1 = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;  
  23    Owner oc1 = HasTileWaterGround(tile) && GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : _current_company;  
  24    bool add_cost1 = !IsWaterTile(tile);  
  25  
  26    CommandCost ret;  
  27    /* At this point we got a flat tile with no bridge over it. Check for ownership */  
  28    if (IsWaterTile(tile) && IsCanal(tile)) {  
  29        ret = EnsureNoVehicleOnGround(tile);  
  30        if (ret.Failed()) return ret;  
  31        if (oc1 != OWNER_NONE) {  
  32            ret = CheckTileOwnership(tile);  
  33            if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;  
  34        }  
  35    } else {  
  36        ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);  
  37        if (ret.Failed()) return ret;  
  38        if (add_cost1) {  
  39            cost.AddCost(ret);  
  40            if (wc1 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);  
  41        }  
  42    }  
  43  
  44    WaterClass wc2 = IsWaterTile(tile2) ? GetWaterClass(tile2) : WATER_CLASS_CANAL;  
  45    Owner oc2 = HasTileWaterGround(tile2) && GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : _current_company;  
  46    bool add_cost2 = !IsWaterTile(tile2);  
  47  
  48    /* At this point we got a flat tile2 with no bridge over it. Check for ownership */  
  49    if (IsWaterTile(tile2) && IsCanal(tile2)) {  
  50        ret = EnsureNoVehicleOnGround(tile2);  
  51        if (ret.Failed()) return ret;  
  52        if (oc2 != OWNER_NONE) {  
  53            ret = CheckTileOwnership(tile2);  
  54            if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;  
  55        }  
  56    } else {  
  57        ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);  
  58        if (ret.Failed()) return ret;  
  59        if (add_cost2) {  
  60            cost.AddCost(ret);  
  61            if (wc2 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);  
  62        }  
  63    }  
  64  
1    if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {  65    if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {  
2//        int z_upper_corner_n = TileHeight(tile);    
3//        int z_upper_corner_s = TileHeight(TileAddByDiagDir(tile, AxisToDiagDir(axis == AXIS_X ? AXIS_Y : AXIS_X)));    
4        int z_slope1;  66        int z_slope1;  
5        Slope slope1 = GetTileSlope(tile, &z_slope1);  67        Slope slope1 = GetTileSlope(tile, &z_slope1);  
6        int z_middle_corner_n = TileHeight(tile2);  68        int z_middle_corner_n = TileHeight(tile2);  
7        int z_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(axis == AXIS_X ? AXIS_Y : AXIS_X)));  7        int z_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
8        int z_slope2;  70        int z_slope2;  
9        Slope slope2 = GetTileSlope(tile2, &z_slope2);  71        Slope slope2 = GetTileSlope(tile2, &z_slope2);  
10//        int z_lower_corner_n = TileHeight(tile2 + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)));    
11//        int z_lower_corner_s = TileHeight(TileAddByDiagDir(tile2 + (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)), AxisToDiagDir(axis == AXIS_X ? AXIS_Y : AXIS_X)));    
12  72  
13        /* Forbid these combinations. */  73        /* Forbid these combinations. */  
14        if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  74        if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  
  
17        if ((slope1 == slope2 || slope1 == ComplementSlope(slope2)) && IsInclinedSlope(slope1)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  77        if ((slope1 == slope2 || slope1 == ComplementSlope(slope2)) && IsInclinedSlope(slope1)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  
18        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);  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);  
19  79  
20//        int corners_tile1 = IsSlopeWithOneCornerRaised(slope1) ? 1 : IsSlopeWithThreeCornersRaised(slope1) ? 3 : 2;  20        /* Terraform these combinations. */
21//        int tee = TileHeight(tile);    
22//        int z_tile1_min = GetTileZ(tile);    
23//        int z_tile1_max = GetTileMaxZ(tile);    
24//        int z_tile2_min = GetTileZ(tile2);    
25//        int z_tile2_max = GetTileMaxZ(tile2);    
26//        bool z = Delta(z_tile_lower, z_tile) == 0;    
27        if (slope1 == SLOPE_FLAT || slope2 == SLOPE_FLAT) {  81        if (slope1 == SLOPE_FLAT || slope2 == SLOPE_FLAT) {  
28            /* Mark the tile as already cleared for the terraform command.  82            /* Mark the tile as already cleared for the terraform command.  
29             * Do this for all tiles (like trees), not only objects. */  83             * Do this for all tiles (like trees), not only objects. */  
  
42            cost.AddCost(ret);  96            cost.AddCost(ret);  
43        } else {  97        } else {  
44            if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {  98            if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {  
45//                int z_middle_corner1 = TileHeight(tile2);    
46//                int z_middle_corner2 = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(axis == AXIS_X ? AXIS_Y : AXIS_X)));    
47                   
48                /* Mark the tile as already cleared for the terraform command.  99                /* Mark the tile as already cleared for the terraform command.  
49                 * Do this for all tiles (like trees), not only objects. */  100                 * Do this for all tiles (like trees), not only objects. */  
50                ClearedObjectArea *coa = FindClearedObject(tile);  101                ClearedObjectArea *coa = FindClearedObject(tile);  
  
60                coa->first_tile = tile_3corner1_before;  111                coa->first_tile = tile_3corner1_before;  
61                if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  112                if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  
62                cost.AddCost(ret);  113                cost.AddCost(ret);  
63    
64                if (z_middle_corner_n == z_middle_corner_s) {  114                if (z_middle_corner_n == z_middle_corner_s) {  
65                    /* Mark the tile as already cleared for the terraform command.  115                    /* Mark the tile as already cleared for the terraform command.  
66                     * Do this for all tiles (like trees), not only objects. */  116                     * Do this for all tiles (like trees), not only objects. */  
  
79                    cost.AddCost(ret);  129                    cost.AddCost(ret);  
80                }  130                }  
81            } else {  131            } else {  
82                if (IsInclinedSlope(slope1) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || IsInclinedSlope(slope2) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {  82                if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2))) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)))) {
83                    if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis)) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis))) {  83                    /* Mark the tile as already cleared for the terraform command.
84                        if (IsInclinedSlope(slope1)) {  84                     * Do this for all tiles (like trees), not only objects. */
85                            /* Mark the tile as already cleared for the terraform command.  85                    ClearedObjectArea *coa = FindClearedObject(IsInclinedSlope(slope1) ? tile : tile2);
86                             * Do this for all tiles (like trees), not only objects. */  86                    if (coa == NULL) {
87                            ClearedObjectArea *coa = FindClearedObject(tile);  87                        coa = _cleared_object_areas.Append();
88                            if (coa == NULL) {  88                        coa->first_tile = IsInclinedSlope(slope1) ? tile : tile2;
89                                coa = _cleared_object_areas.Append();  89                        coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
90                                coa->first_tile = tile;    
91                                coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);    
92                            }    
93                            /* Hide the tile from the terraforming command */    
94                            TileIndex tile_inclined1_before = coa->first_tile;    
95                            coa->first_tile = INVALID_TILE;    
96                            ret = DoCommand(tile, IsSlopeWithThreeCornersRaised(slope2) ? ComplementSlope(slope1) : slope1, IsSlopeWithThreeCornersRaised(slope2) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);    
97                            coa->first_tile = tile_inclined1_before;    
98                            if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);    
99                            cost.AddCost(ret);    
100                        } else {    
101                            /* Mark the tile as already cleared for the terraform command.    
102                             * Do this for all tiles (like trees), not only objects. */    
103                            ClearedObjectArea *coa = FindClearedObject(tile2);    
104                            if (coa == NULL) {    
105                                coa = _cleared_object_areas.Append();    
106                                coa->first_tile = tile2;    
107                                coa->area = TileArea(tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);    
108                            }    
109                            /* Hide the tile from the terraforming command */    
110                            TileIndex tile_inclined2_before = coa->first_tile;    
111                            coa->first_tile = INVALID_TILE;    
112                            ret = DoCommand(tile2, IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope2) : slope2, IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);    
113                            coa->first_tile = tile_inclined2_before;    
114                            if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);    
115                            cost.AddCost(ret);    
116                        }    
117                    }  140                    }  
  141                    /* Hide the tile from the terraforming command */  
  142                    TileIndex tile_inclined1_before = coa->first_tile;  
  143                    coa->first_tile = INVALID_TILE;  
  144                    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);  
  145                    coa->first_tile = tile_inclined1_before;  
  146                    if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);  
  147                    cost.AddCost(ret);  
118                } else {  148                } else {  
119                    if ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || (slope2 == SLOPE_NS || slope2 == SLOPE_EW) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {  149                    if ((slope1 == SLOPE_EW || slope1 == SLOPE_NS) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2)) || (slope2 == SLOPE_NS || slope2 == SLOPE_EW) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1))) {  
120                        /* Mark the tile as already cleared for the terraform command.  150                        /* Mark the tile as already cleared for the terraform command.  
  
139                }  169                }  
140            }  170            }  
141        }  171        }  
142    } 172    }
   173
   174    if (flags & DC_EXEC) {
   175        Depot *depot = new Depot(tile);
   176        depot->build_date = _date;
   177
   178        if (add_cost1 && wc1 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
   179        if (add_cost2 && wc2 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
   180        Company::Get(_current_company)->infrastructure.water += 2 * LOCK_DEPOT_TILE_FACTOR;
   181        DirtyCompanyInfrastructureWindows(_current_company);
   182
   183        MakeShipDepot(tile, _current_company, oc1, depot->index, DEPOT_PART_NORTH, axis, wc1);
   184        MakeShipDepot(tile2, _current_company, oc2, depot->index, DEPOT_PART_SOUTH, axis, wc2);
   185        MarkTileDirtyByTile(tile);
   186        MarkTileDirtyByTile(tile2);
   187        MakeDefaultName(depot);
   188    }
   189
   190    return cost;
   191}