* Build a train depot * @param flags operation to perform * @param tile position of the train depot * @param railtype rail type * @param dir entrance direction * @return the cost of this operation or an error * * @todo When checking for the tile slope, * distinguish between "Flat land required" and "land sloped in wrong direction" */
| 959 | * distinguish between "Flat land required" and "land sloped in wrong direction" |
| 960 | */ |
| 961 | CommandCost CmdBuildTrainDepot(DoCommandFlags flags, TileIndex tile, RailType railtype, DiagDirection dir) |
| 962 | { |
| 963 | /* check railtype and valid direction for depot (0 through 3), 4 in total */ |
| 964 | if (!ValParamRailType(railtype) || !IsValidDiagDirection(dir)) return CMD_ERROR; |
| 965 | |
| 966 | Slope tileh = GetTileSlope(tile); |
| 967 | |
| 968 | CommandCost cost(EXPENSES_CONSTRUCTION); |
| 969 | |
| 970 | /* Prohibit construction if |
| 971 | * The tile is non-flat AND |
| 972 | * 1) build-on-slopes is disabled |
| 973 | * 2) the tile is steep i.e. spans two height levels |
| 974 | * 3) the exit points in the wrong direction |
| 975 | */ |
| 976 | |
| 977 | if (tileh != SLOPE_FLAT) { |
| 978 | if (!_settings_game.construction.build_on_slopes || !CanBuildDepotByTileh(dir, tileh)) { |
| 979 | return CommandCost(STR_ERROR_FLAT_LAND_REQUIRED); |
| 980 | } |
| 981 | cost.AddCost(_price[PR_BUILD_FOUNDATION]); |
| 982 | } |
| 983 | |
| 984 | /* Allow the user to rotate the depot instead of having to destroy it and build it again */ |
| 985 | bool rotate_existing_depot = false; |
| 986 | if (IsRailDepotTile(tile) && railtype == GetRailType(tile)) { |
| 987 | CommandCost ret = CheckTileOwnership(tile); |
| 988 | if (ret.Failed()) return ret; |
| 989 | |
| 990 | if (dir == GetRailDepotDirection(tile)) return CommandCost(); |
| 991 | |
| 992 | ret = EnsureNoVehicleOnGround(tile); |
| 993 | if (ret.Failed()) return ret; |
| 994 | |
| 995 | rotate_existing_depot = true; |
| 996 | } |
| 997 | |
| 998 | if (!rotate_existing_depot) { |
| 999 | cost.AddCost(Command<CMD_LANDSCAPE_CLEAR>::Do(flags, tile)); |
| 1000 | if (cost.Failed()) return cost; |
| 1001 | |
| 1002 | if (IsBridgeAbove(tile)) return CommandCost(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST); |
| 1003 | |
| 1004 | if (!Depot::CanAllocateItem()) return CMD_ERROR; |
| 1005 | } |
| 1006 | |
| 1007 | if (flags.Test(DoCommandFlag::Execute)) { |
| 1008 | if (rotate_existing_depot) { |
| 1009 | SetRailDepotExitDirection(tile, dir); |
| 1010 | } else { |
| 1011 | Depot *d = new Depot(tile); |
| 1012 | |
| 1013 | MakeRailDepot(tile, _current_company, d->index, dir, railtype); |
| 1014 | MakeDefaultName(d); |
| 1015 | |
| 1016 | Company::Get(_current_company)->infrastructure.rail[railtype]++; |
| 1017 | DirtyCompanyInfrastructureWindows(_current_company); |
| 1018 | } |
nothing calls this directly
no test coverage detected