diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 92441cd9a5..8879218d74 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -827,7 +827,7 @@ static void LimitSpeedFromLookAhead(int &max_speed, const TrainDecelerationStats } static void ApplyLookAheadItem(const Train *v, const TrainReservationLookAheadItem &item, int &max_speed, int &advisory_max_speed, - VehicleOrderID ¤t_order_index, const TrainDecelerationStats &stats, int current_position) + VehicleOrderID ¤t_order_index, const Order *&order, const TrainDecelerationStats &stats, int current_position) { auto limit_speed = [&](int position, int end_speed, int z) { LimitSpeedFromLookAhead(max_speed, stats, current_position, position, end_speed, z - stats.z_pos); @@ -839,19 +839,17 @@ static void ApplyLookAheadItem(const Train *v, const TrainReservationLookAheadIt switch (item.type) { case TRLIT_STATION: { - if (current_order_index < v->GetNumOrders()) { - const Order *order = v->GetOrder(current_order_index); - if (order->ShouldStopAtStation(nullptr, item.data_id, Waypoint::GetIfValid(item.data_id) != nullptr)) { - limit_advisory_speed(item.start + PredictStationStoppingLocation(v, order, item.end - item.start, item.data_id), 0, item.z_pos); - } else if (order->IsType(OT_GOTO_WAYPOINT) && order->GetDestination() == item.data_id && (order->GetWaypointFlags() & OWF_REVERSE)) { - limit_advisory_speed(item.start + v->gcache.cached_total_length, 0, item.z_pos); - } - if (order->IsBaseStationOrder() && order->GetDestination() == item.data_id) { - current_order_index++; - AdvanceOrderIndex(v, current_order_index); - uint16 max_speed = v->GetOrder(current_order_index)->GetMaxSpeed(); - if (max_speed < UINT16_MAX) limit_advisory_speed(item.start, max_speed, item.z_pos); - } + if (order->ShouldStopAtStation(nullptr, item.data_id, Waypoint::GetIfValid(item.data_id) != nullptr)) { + limit_advisory_speed(item.start + PredictStationStoppingLocation(v, order, item.end - item.start, item.data_id), 0, item.z_pos); + } else if (order->IsType(OT_GOTO_WAYPOINT) && order->GetDestination() == item.data_id && (order->GetWaypointFlags() & OWF_REVERSE)) { + limit_advisory_speed(item.start + v->gcache.cached_total_length, 0, item.z_pos); + } + if (order->IsBaseStationOrder() && order->GetDestination() == item.data_id && v->GetNumOrders() > 0) { + current_order_index++; + AdvanceOrderIndex(v, current_order_index); + order = v->GetOrder(current_order_index); + uint16 max_speed = order->GetMaxSpeed(); + if (max_speed < UINT16_MAX) limit_advisory_speed(item.start, max_speed, item.z_pos); } break; } @@ -1003,8 +1001,9 @@ Train::MaxSpeedInfo Train::GetCurrentMaxSpeedInfoInternal(bool update_state) con LimitSpeedFromLookAhead(max_speed, stats, this->lookahead->current_position, this->lookahead->reservation_end_position, 0, this->lookahead->reservation_end_z - stats.z_pos); } VehicleOrderID current_order_index = this->cur_real_order_index; + const Order *order = &(this->current_order); for (const TrainReservationLookAheadItem &item : this->lookahead->items) { - ApplyLookAheadItem(this, item, max_speed, advisory_max_speed, current_order_index, stats, this->lookahead->current_position); + ApplyLookAheadItem(this, item, max_speed, advisory_max_speed, current_order_index, order, stats, this->lookahead->current_position); } if (HasBit(this->lookahead->flags, TRLF_APPLY_ADVISORY)) { max_speed = std::min(max_speed, advisory_max_speed);