MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / CmdBuildDock

Function CmdBuildDock

src/station_cmd.cpp:2892–2978  ·  view source on GitHub ↗

* Build a dock/haven. * @param flags operation to perform * @param tile tile where dock will be built * @param station_to_join station ID to join (NEW_STATION if build new one) * @param adjacent allow docks directly adjacent to other docks. * @return the cost of this operation or an error */

Source from the content-addressed store, hash-verified

2890 * @return the cost of this operation or an error
2891 */
2892CommandCost CmdBuildDock(DoCommandFlags flags, TileIndex tile, StationID station_to_join, bool adjacent)
2893{
2894 bool reuse = (station_to_join != NEW_STATION);
2895 if (!reuse) station_to_join = StationID::Invalid();
2896 bool distant_join = (station_to_join != StationID::Invalid());
2897
2898 if (distant_join && (!_settings_game.station.distant_join_stations || !Station::IsValidID(station_to_join))) return CMD_ERROR;
2899
2900 DiagDirection direction = GetInclinedSlopeDirection(GetTileSlope(tile));
2901 if (direction == INVALID_DIAGDIR) return CommandCost(STR_ERROR_SITE_UNSUITABLE);
2902 direction = ReverseDiagDir(direction);
2903
2904 /* Docks cannot be placed on rapids */
2905 if (HasTileWaterGround(tile)) return CommandCost(STR_ERROR_SITE_UNSUITABLE);
2906
2907 CommandCost ret = CheckIfAuthorityAllowsNewStation(tile, flags);
2908 if (ret.Failed()) return ret;
2909
2910 ret = IsDockBridgeAboveOk(tile, to_underlying(direction));
2911 if (ret.Failed()) return ret;
2912
2913 CommandCost cost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_STATION_DOCK]);
2914 ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile);
2915 if (ret.Failed()) return ret;
2916 cost.AddCost(ret.GetCost());
2917
2918 TileIndex tile_cur = tile + TileOffsByDiagDir(direction);
2919
2920 if (!HasTileWaterGround(tile_cur) || !IsTileFlat(tile_cur)) {
2921 return CommandCost(STR_ERROR_SITE_UNSUITABLE);
2922 }
2923
2924 ret = IsDockBridgeAboveOk(tile_cur, GFX_DOCK_BASE_WATER_PART + to_underlying(DiagDirToAxis(direction)));
2925 if (ret.Failed()) return ret;
2926
2927 /* Get the water class of the water tile before it is cleared.*/
2928 WaterClass wc = GetWaterClass(tile_cur);
2929
2930 bool add_cost = !IsWaterTile(tile_cur);
2931 ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile_cur);
2932 if (ret.Failed()) return ret;
2933 if (add_cost) cost.AddCost(ret.GetCost());
2934
2935 tile_cur += TileOffsByDiagDir(direction);
2936 if (!IsTileType(tile_cur, MP_WATER) || !IsTileFlat(tile_cur)) {
2937 return CommandCost(STR_ERROR_SITE_UNSUITABLE);
2938 }
2939
2940 TileArea dock_area = TileArea(tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]),
2941 _dock_w_chk[direction], _dock_h_chk[direction]);
2942
2943 /* middle */
2944 Station *st = nullptr;
2945 ret = FindJoiningStation(StationID::Invalid(), station_to_join, adjacent, dock_area, &st);
2946 if (ret.Failed()) return ret;
2947
2948 /* Distant join */
2949 if (st == nullptr && distant_join) st = Station::GetIfValid(station_to_join);

Callers

nothing calls this directly

Calls 15

InvalidFunction · 0.85
GetTileSlopeFunction · 0.85
CommandCostClass · 0.85
ReverseDiagDirFunction · 0.85
HasTileWaterGroundFunction · 0.85
IsDockBridgeAboveOkFunction · 0.85
to_underlyingFunction · 0.85
TileOffsByDiagDirFunction · 0.85
IsTileFlatFunction · 0.85
DiagDirToAxisFunction · 0.85

Tested by

no test coverage detected