* Calculates the maximum speed of the vehicle under its current conditions. * @return Maximum speed of the vehicle. */
| 380 | * @return Maximum speed of the vehicle. |
| 381 | */ |
| 382 | int 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. */ |
| 430 | void Train::UpdateAcceleration() |
no test coverage detected