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

Function CmdBuildRailStation

src/station_cmd.cpp:1438–1654  ·  view source on GitHub ↗

* Build rail station * @param flags operation to perform * @param tile_org northern most position of station dragging/placement * @param rt railtype * @param axis orientation (Axis) * @param numtracks number of tracks * @param plat_len platform length * @param spec_class custom station class * @param spec_index custom station id * @param station_to_join station ID to join (NEW_STATION if

Source from the content-addressed store, hash-verified

1436 * @return the cost of this operation or an error
1437 */
1438CommandCost CmdBuildRailStation(DoCommandFlags flags, TileIndex tile_org, RailType rt, Axis axis, uint8_t numtracks, uint8_t plat_len, StationClassID spec_class, uint16_t spec_index, StationID station_to_join, bool adjacent)
1439{
1440 /* Does the authority allow this? */
1441 CommandCost ret = CheckIfAuthorityAllowsNewStation(tile_org, flags);
1442 if (ret.Failed()) return ret;
1443
1444 if (!ValParamRailType(rt) || !IsValidAxis(axis)) return CMD_ERROR;
1445
1446 /* Check if the given station class is valid */
1447 if (static_cast<uint>(spec_class) >= StationClass::GetClassCount()) return CMD_ERROR;
1448 const StationClass *cls = StationClass::Get(spec_class);
1449 if (IsWaypointClass(*cls)) return CMD_ERROR;
1450 if (spec_index >= cls->GetSpecCount()) return CMD_ERROR;
1451 if (plat_len == 0 || numtracks == 0) return CMD_ERROR;
1452
1453 int w_org, h_org;
1454 if (axis == AXIS_X) {
1455 w_org = plat_len;
1456 h_org = numtracks;
1457 } else {
1458 h_org = plat_len;
1459 w_org = numtracks;
1460 }
1461
1462 /* Check if the first tile and the last tile are valid */
1463 if (!IsValidTile(tile_org) || TileAddWrap(tile_org, w_org - 1, h_org - 1) == INVALID_TILE) return CMD_ERROR;
1464
1465 bool reuse = (station_to_join != NEW_STATION);
1466 if (!reuse) station_to_join = StationID::Invalid();
1467 bool distant_join = (station_to_join != StationID::Invalid());
1468
1469 if (distant_join && (!_settings_game.station.distant_join_stations || !Station::IsValidID(station_to_join))) return CMD_ERROR;
1470
1471 if (h_org > _settings_game.station.station_spread || w_org > _settings_game.station.station_spread) return CMD_ERROR;
1472
1473 /* these values are those that will be stored in train_tile and station_platforms */
1474 TileArea new_location(tile_org, w_org, h_org);
1475
1476 /* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */
1477 StationID est = StationID::Invalid();
1478 std::vector<Train *> affected_vehicles;
1479 /* Add construction and clearing expenses. */
1480 CommandCost cost = CalculateRailStationCost(new_location, flags, axis, &est, rt, affected_vehicles, spec_class, spec_index, plat_len, numtracks);
1481 if (cost.Failed()) return cost;
1482
1483 Station *st = nullptr;
1484 ret = FindJoiningStation(est, station_to_join, adjacent, new_location, &st);
1485 if (ret.Failed()) return ret;
1486
1487 ret = BuildStationPart(&st, flags, reuse, new_location, STATIONNAMING_RAIL);
1488 if (ret.Failed()) return ret;
1489
1490 if (st != nullptr && st->train_station.tile != INVALID_TILE) {
1491 ret = CanExpandRailStation(st, new_location);
1492 if (ret.Failed()) return ret;
1493 }
1494
1495 const StationSpec *statspec = StationClass::Get(spec_class)->GetSpec(spec_index);

Callers

nothing calls this directly

Calls 15

ValParamRailTypeFunction · 0.85
IsValidAxisFunction · 0.85
IsValidTileFunction · 0.85
TileAddWrapFunction · 0.85
InvalidFunction · 0.85
CalculateRailStationCostFunction · 0.85
FindJoiningStationFunction · 0.85
BuildStationPartFunction · 0.85
CanExpandRailStationFunction · 0.85
TileOffsByAxisFunction · 0.85
OtherAxisFunction · 0.85

Tested by

no test coverage detected