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

Method GetCurrentMaxSpeed

src/train_cmd.cpp:382–427  ·  view source on GitHub ↗

* Calculates the maximum speed of the vehicle under its current conditions. * @return Maximum speed of the vehicle. */

Source from the content-addressed store, hash-verified

380 * @return Maximum speed of the vehicle.
381 */
382int Train::GetCurrentMaxSpeed() const
383{
384 int max_speed = _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL ?
385 this->gcache.cached_max_track_speed :
386 this->tcache.cached_max_curve_speed;
387
388 if (_settings_game.vehicle.train_acceleration_model == AM_REALISTIC && IsRailStationTile(this->tile)) {
389 StationID sid = GetStationIndex(this->tile);
390 if (this->current_order.ShouldStopAtStation(this, sid)) {
391 int station_ahead;
392 int station_length;
393 int stop_at = GetTrainStopLocation(sid, this->tile, this, &station_ahead, &station_length);
394
395 /* The distance to go is whatever is still ahead of the train minus the
396 * distance from the train's stop location to the end of the platform */
397 int distance_to_go = station_ahead / TILE_SIZE - (station_length - stop_at) / TILE_SIZE;
398
399 if (distance_to_go > 0) {
400 int st_max_speed = 120;
401
402 int delta_v = this->cur_speed / (distance_to_go + 1);
403 if (max_speed > (this->cur_speed - delta_v)) {
404 st_max_speed = this->cur_speed - (delta_v / 10);
405 }
406
407 st_max_speed = std::max(st_max_speed, 25 * distance_to_go);
408 max_speed = std::min(max_speed, st_max_speed);
409 }
410 }
411 }
412
413 for (const Train *u = this; u != nullptr; u = u->Next()) {
414 if (_settings_game.vehicle.train_acceleration_model == AM_REALISTIC && u->track == TRACK_BIT_DEPOT) {
415 max_speed = std::min(max_speed, 61);
416 break;
417 }
418
419 /* Vehicle is on the middle part of a bridge. */
420 if (u->track == TRACK_BIT_WORMHOLE && !u->vehstatus.Test(VehState::Hidden)) {
421 max_speed = std::min<int>(max_speed, GetBridgeSpec(GetBridgeType(u->tile))->speed);
422 }
423 }
424
425 max_speed = std::min<int>(max_speed, this->current_order.GetMaxSpeed());
426 return std::min<int>(max_speed, this->gcache.cached_max_track_speed);
427}
428
429/** Update acceleration of the train from the cached power and weight. */
430void Train::UpdateAcceleration()

Callers 1

UpdateSpeedMethod · 0.95

Calls 9

IsRailStationTileFunction · 0.85
GetStationIndexFunction · 0.85
GetTrainStopLocationFunction · 0.85
GetBridgeSpecFunction · 0.85
GetBridgeTypeFunction · 0.85
ShouldStopAtStationMethod · 0.80
TestMethod · 0.80
NextMethod · 0.45
GetMaxSpeedMethod · 0.45

Tested by

no test coverage detected