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

Function CmdBuildRoadStop

src/station_cmd.cpp:2071–2221  ·  view source on GitHub ↗

* Build a bus or truck stop. * @param flags Operation to perform. * @param tile Northernmost tile of the stop. * @param width Width of the road stop. * @param length Length of the road stop. * @param stop_type Type of road stop (bus/truck). * @param is_drive_through False for normal stops, true for drive-through. * @param ddir Entrance direction (#DiagDirection) for normal stops. Converted

Source from the content-addressed store, hash-verified

2069 * @return The cost of this operation or an error.
2070 */
2071CommandCost CmdBuildRoadStop(DoCommandFlags flags, TileIndex tile, uint8_t width, uint8_t length, RoadStopType stop_type, bool is_drive_through,
2072 DiagDirection ddir, RoadType rt, RoadStopClassID spec_class, uint16_t spec_index, StationID station_to_join, bool adjacent)
2073{
2074 if (!ValParamRoadType(rt) || !IsValidDiagDirection(ddir) || stop_type >= RoadStopType::End) return CMD_ERROR;
2075 bool reuse = (station_to_join != NEW_STATION);
2076 if (!reuse) station_to_join = StationID::Invalid();
2077 bool distant_join = (station_to_join != StationID::Invalid());
2078
2079 /* Check if the given station class is valid */
2080 if (static_cast<uint>(spec_class) >= RoadStopClass::GetClassCount()) return CMD_ERROR;
2081 const RoadStopClass *cls = RoadStopClass::Get(spec_class);
2082 if (IsWaypointClass(*cls)) return CMD_ERROR;
2083 if (spec_index >= cls->GetSpecCount()) return CMD_ERROR;
2084
2085 const RoadStopSpec *roadstopspec = cls->GetSpec(spec_index);
2086 if (roadstopspec != nullptr) {
2087 if (stop_type == RoadStopType::Truck && roadstopspec->stop_type != ROADSTOPTYPE_FREIGHT && roadstopspec->stop_type != ROADSTOPTYPE_ALL) return CMD_ERROR;
2088 if (stop_type == RoadStopType::Bus && roadstopspec->stop_type != ROADSTOPTYPE_PASSENGER && roadstopspec->stop_type != ROADSTOPTYPE_ALL) return CMD_ERROR;
2089 if (!is_drive_through && roadstopspec->flags.Test(RoadStopSpecFlag::DriveThroughOnly)) return CMD_ERROR;
2090 }
2091
2092 /* Check if the requested road stop is too big */
2093 if (width > _settings_game.station.station_spread || length > _settings_game.station.station_spread) return CommandCost(STR_ERROR_STATION_TOO_SPREAD_OUT);
2094 /* Check for incorrect width / length. */
2095 if (width == 0 || length == 0) return CMD_ERROR;
2096 /* Check if the first tile and the last tile are valid */
2097 if (!IsValidTile(tile) || TileAddWrap(tile, width - 1, length - 1) == INVALID_TILE) return CMD_ERROR;
2098
2099 TileArea roadstop_area(tile, width, length);
2100
2101 if (distant_join && (!_settings_game.station.distant_join_stations || !Station::IsValidID(station_to_join))) return CMD_ERROR;
2102
2103 /* Trams only have drive through stops */
2104 if (!is_drive_through && RoadTypeIsTram(rt)) return CMD_ERROR;
2105
2106 Axis axis = DiagDirToAxis(ddir);
2107
2108 CommandCost ret = CheckIfAuthorityAllowsNewStation(tile, flags);
2109 if (ret.Failed()) return ret;
2110
2111 bool is_truck_stop = stop_type != RoadStopType::Bus;
2112
2113 /* Total road stop cost. */
2114 Money unit_cost;
2115 if (roadstopspec != nullptr) {
2116 unit_cost = roadstopspec->GetBuildCost(is_truck_stop ? PR_BUILD_STATION_TRUCK : PR_BUILD_STATION_BUS);
2117 } else {
2118 unit_cost = _price[is_truck_stop ? PR_BUILD_STATION_TRUCK : PR_BUILD_STATION_BUS];
2119 }
2120 StationID est = StationID::Invalid();
2121 CommandCost cost = CalculateRoadStopCost(roadstop_area, flags, is_drive_through, is_truck_stop ? StationType::Truck : StationType::Bus, roadstopspec, axis, ddir, &est, rt, unit_cost);
2122 if (cost.Failed()) return cost;
2123
2124 Station *st = nullptr;
2125 ret = FindJoiningRoadStop(est, station_to_join, adjacent, roadstop_area, &st);
2126 if (ret.Failed()) return ret;
2127
2128 /* Check if this number of road stops can be allocated. */

Callers

nothing calls this directly

Calls 15

ValParamRoadTypeFunction · 0.85
IsValidDiagDirectionFunction · 0.85
InvalidFunction · 0.85
CommandCostClass · 0.85
IsValidTileFunction · 0.85
TileAddWrapFunction · 0.85
RoadTypeIsTramFunction · 0.85
DiagDirToAxisFunction · 0.85
CalculateRoadStopCostFunction · 0.85
FindJoiningRoadStopFunction · 0.85
BuildStationPartFunction · 0.85

Tested by

no test coverage detected