Loading

Paste #p3elkxu0z

  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.  */
  10. CommandCost CmdBuildShipDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
  11. {
  12.     Axis axis = Extract<Axis, 0, 1>(p1);
  13.  
  14.     TileIndexDiff diff = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
  15.     TileIndex tile2 = tile + diff;
  16.  
  17.     if (IsBridgeAbove(tile) || IsBridgeAbove(tile2)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
  18.  
  19.     if (!Depot::CanAllocateItem()) return CMD_ERROR;
  20.  
  21.     CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_DEPOT_SHIP]);
  22.     CommandCost ret;
  23.  
  24.     WaterClass wc1 = IsWaterTile(tile) ? GetWaterClass(tile) : WATER_CLASS_CANAL;
  25.     Owner oc1 = HasTileWaterGround(tile) && GetWaterClass(tile) == WATER_CLASS_CANAL ? GetCanalOwner(tile) : _current_company;
  26.     bool add_cost1 = !IsWaterTile(tile);
  27.  
  28.     /* At this point we got a tile with no bridge over it. Check for ownership */
  29.     if (IsWaterTile(tile) && IsCanal(tile)) {
  30.         ret = EnsureNoVehicleOnGround(tile);
  31.         if (ret.Failed()) return ret;
  32.         if (oc1 != OWNER_NONE) {
  33.             ret = CheckTileOwnership(tile);
  34.             if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;
  35.         }
  36.     } else {
  37.         ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  38.         if (ret.Failed()) return ret;
  39.         if (add_cost1) {
  40.             cost.AddCost(ret);
  41.             if (wc1 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);
  42.         }
  43.     }
  44.  
  45.     WaterClass wc2 = IsWaterTile(tile2) ? GetWaterClass(tile2) : WATER_CLASS_CANAL;
  46.     Owner oc2 = HasTileWaterGround(tile2) && GetWaterClass(tile2) == WATER_CLASS_CANAL ? GetCanalOwner(tile2) : _current_company;
  47.     bool add_cost2 = !IsWaterTile(tile2);
  48.  
  49.     /* At this point we got a tile2 with no bridge over it. Check for ownership */
  50.     if (IsWaterTile(tile2) && IsCanal(tile2)) {
  51.         ret = EnsureNoVehicleOnGround(tile2);
  52.         if (ret.Failed()) return ret;
  53.         if (oc2 != OWNER_NONE) {
  54.             ret = CheckTileOwnership(tile2);
  55.             if (ret.Failed() && !_settings_game.construction.build_on_competitor_canal) return ret;
  56.         }
  57.     } else {
  58.         ret = DoCommand(tile2, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
  59.         if (ret.Failed()) return ret;
  60.         if (add_cost2) {
  61.             cost.AddCost(ret);
  62.             if (wc2 == WATER_CLASS_CANAL) cost.AddCost(_price[PR_BUILD_CANAL]);
  63.         }
  64.     }
  65.  
  66.     int h_north_corner_n = TileHeight(tile);
  67.     int h_north_corner_s = TileHeight(TileAddByDiagDir(tile, AxisToDiagDir(OtherAxis(axis))));
  68.     int h_middle_corner_n = TileHeight(tile2);
  69.     int h_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
  70.     int h_south_corner_n = TileHeight(tile2 + diff);
  71.     int h_south_corner_s = TileHeight(TileAddByDiagDir(tile2 + diff, AxisToDiagDir(OtherAxis(axis))));
  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};
  74.  
  75.     int min_h = MAX_TILE_HEIGHT;
  76.     int max_h = 0;
  77.     for (int i = 0; i < lengthof(corners); i++) {
  78.         min_h = min(min_h, corners[i]);
  79.         max_h = max(max_h, corners[i]);
  80.     }
  81.  
  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)))));
  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)))));
  84.  
  85.     int best_h;
  86.     int max_delta = MAX_TILE_HEIGHT;
  87.     for (int h = min_h; h <= max_h; h++) {
  88.         int delta = 0;
  89.         for (int c = 0; c < lengthof(corners); c++) {
  90.             delta = max(delta, Delta(h, corners[c]));
  91.         }
  92.         if (delta < max_delta) {
  93.             max_delta = delta;
  94.             best_h = h;
  95.         }
  96.     }
  97.  
  98. //  if (!IsTileFlat(tile) || !IsTileFlat(tile2)) {
  99. //      int z_slope1;
  100. //      Slope slope1 = GetTileSlope(tile, &z_slope1);
  101. //      int z_middle_corner_n = TileHeight(tile2);
  102. //      int z_middle_corner_s = TileHeight(TileAddByDiagDir(tile2, AxisToDiagDir(OtherAxis(axis))));
  103. //      int z_slope2;
  104. //      Slope slope2 = GetTileSlope(tile2, &z_slope2);
  105.  
  106. //      /* Forbid these combinations. */
  107. //      if (IsSteepSlope(slope1) || IsSteepSlope(slope2)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  108. //      if (slope1 == SLOPE_NS && slope2 == SLOPE_EW || slope1 == SLOPE_EW && slope2 == SLOPE_NS) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  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);
  110. //      if (slope1 == slope2 && IsInclinedSlope(slope1)) 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);
  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);
  113.  
  114. //      /* Terraform these combinations. */
  115. //      if (slope1 == SLOPE_FLAT || slope2 == SLOPE_FLAT) {
  116. //          /* Mark the tile as already cleared for the terraform command. */
  117. //          ClearedObjectArea *coa = MakeClearedObjectArea(slope1 == SLOPE_FLAT ? tile2 : tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  118. //          /* Hide the tile from the terraforming command. */
  119. //          TileIndex tile_flat_before = coa->first_tile;
  120. //          coa->first_tile = INVALID_TILE;
  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);
  122. //          coa->first_tile = tile_flat_before;
  123. //          if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  124. //          cost.AddCost(ret);
  125. //      } else {
  126. //          if ((IsSlopeWithThreeCornersRaised(slope1) || IsSlopeWithOneCornerRaised(slope1)) && (IsSlopeWithThreeCornersRaised(slope2) || IsSlopeWithOneCornerRaised(slope2))) {
  127. //              /* Mark the tile as already cleared for the terraform command. */
  128. //              ClearedObjectArea *coa = MakeClearedObjectArea(tile, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  129. //              /* Hide the tile from the terraforming command. */
  130. //              TileIndex tile_3corner1_before = coa->first_tile;
  131. //              coa->first_tile = INVALID_TILE;
  132. //              ret = DoCommand(tile, IsSlopeWithThreeCornersRaised(slope1) ? ComplementSlope(slope1) : slope1, IsSlopeWithThreeCornersRaised(slope1) ? 1 : 0, flags | DC_NO_WATER, CMD_TERRAFORM_LAND);
  133. //              coa->first_tile = tile_3corner1_before;
  134. //              if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  135. //              cost.AddCost(ret);
  136. //              if (z_middle_corner_n == z_middle_corner_s) {
  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);
  139. //                  /* Hide the tile from the terraforming command. */
  140. //                  TileIndex tile_3corner2_before = coa->first_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);
  143. //                  coa->first_tile = tile_3corner2_before;
  144. //                  if (ret.Failed()) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
  145. //                  cost.AddCost(ret);
  146. //              }
  147. //          } else {
  148. //              if ((IsInclinedSlope(slope1) && DiagDirToAxis(GetInclinedSlopeDirection(slope1)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope2) || IsSlopeWithThreeCornersRaised(slope2))) || (IsInclinedSlope(slope2) && DiagDirToAxis(GetInclinedSlopeDirection(slope2)) == OtherAxis(axis) && (IsSlopeWithOneCornerRaised(slope1) || IsSlopeWithThreeCornersRaised(slope1)))) {
  149. //                  /* Mark the tile as already cleared for the terraform command. */
  150. //                  ClearedObjectArea *coa = MakeClearedObjectArea(IsInclinedSlope(slope1) ? tile : tile2, tile, axis == AXIS_X ? 2 : 1, axis == AXIS_Y ? 2 : 1);
  151. //                  /* Hide the tile from the terraforming command. */
  152. //                  TileIndex tile_inclined1_before = coa->first_tile;
  153. //                  coa->first_tile = INVALID_TILE;
  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);
  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);
  191.  
  192.     DiagDirection dir_rotate = axis == AXIS_X ? DIAGDIR_SE : DIAGDIR_NE;
  193.     TileIndex tc = tile + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));
  194.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  195.     if (ret.Failed()) return ret;
  196.  
  197.     tc = tile2 + (axis == AXIS_X ? TileDiffXY(0, -1) : TileDiffXY(1, 0));
  198.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  199.     if (ret.Failed()) return ret;
  200.  
  201.     dir_rotate = ReverseDiagDir(dir_rotate);
  202.     tc = tile + (axis == AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(-1, 0));
  203.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  204.     if (ret.Failed()) return ret;
  205.  
  206.     tc = tile2 + (axis == AXIS_X ? TileDiffXY(0, 1) : TileDiffXY(-1, 0));
  207.     ret = EnsureNoShipFromDiagDir(tc, dir_rotate);
  208.     if (ret.Failed()) return ret;
  209.  
  210.     if (flags & DC_EXEC) {
  211.         Depot *depot = new Depot(tile);
  212.         depot->build_date = _date;
  213.  
  214.         if (add_cost1 && wc1 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
  215.         if (add_cost2 && wc2 == WATER_CLASS_CANAL) Company::Get(_current_company)->infrastructure.water++;
  216.         Company::Get(_current_company)->infrastructure.water += 2 * LOCK_DEPOT_TILE_FACTOR;
  217.         DirtyCompanyInfrastructureWindows(_current_company);
  218.  
  219.         MakeShipDepot(tile, _current_company, oc1, depot->index, DEPOT_PART_NORTH, axis, wc1);
  220.         MakeShipDepot(tile2, _current_company, oc2, depot->index, DEPOT_PART_SOUTH, axis, wc2);
  221.         MarkTileDirtyByTile(tile);
  222.         MarkTileDirtyByTile(tile2);
  223.         MakeDefaultName(depot);
  224.     }
  225.  
  226.     return cost;
  227. }
  228.  
  229. void 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.  
  292. static 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]);
  324. }

Version history

Revision # Author Created at
pdynurwc5 Anonymous 01 Dec 2017, 14:56:55 UTC Diff

Comments