diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 44e3b580a6..f90565ffaa 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1942,13 +1942,12 @@ void Vehicle::BeginLoading() } else { /* We weren't scheduled to stop here. Insert an implicit order - * to show that we are stopping here, but only do that if the order - * list isn't empty. + * to show that we are stopping here. * While only groundvehicles have implicit orders, e.g. aircraft might still enter * the 'wrong' terminal when skipping orders etc. */ Order *in_list = this->GetOrder(this->cur_implicit_order_index); - if (this->IsGroundVehicle() && in_list != NULL && - (!in_list->IsType(OT_IMPLICIT) || + if (this->IsGroundVehicle() && + (in_list == NULL || !in_list->IsType(OT_IMPLICIT) || in_list->GetDestination() != this->last_station_visited)) { bool suppress_implicit_orders = HasBit(this->GetGroundVehicleFlags(), GVF_SUPPRESS_IMPLICIT_ORDERS); /* Do not create consecutive duplicates of implicit orders */ @@ -1961,15 +1960,16 @@ void Vehicle::BeginLoading() * so test whether the right order follows later */ int target_index = this->cur_implicit_order_index; bool found = false; - while (target_index != this->cur_real_order_index) { + while (target_index != this->cur_real_order_index || !this->HasExplicitOrders()) { const Order *order = this->GetOrder(target_index); + if (order == NULL) break; // No orders. if (order->IsType(OT_IMPLICIT) && order->GetDestination() == this->last_station_visited) { found = true; break; } target_index++; if (target_index >= this->orders.list->GetNumOrders()) target_index = 0; - assert(target_index != this->cur_implicit_order_index); // infinite loop? + if (target_index == this->cur_implicit_order_index) break; // Avoid infinite loop. } if (found) { @@ -1999,7 +1999,10 @@ void Vehicle::BeginLoading() assert(order != NULL); } } - } else if (!suppress_implicit_orders && this->orders.list->GetNumOrders() < MAX_VEH_ORDER_ID && Order::CanAllocateItem()) { + } else if (!suppress_implicit_orders && + ((this->orders.list == NULL && OrderList::CanAllocateItem()) || + this->orders.list->GetNumOrders() < MAX_VEH_ORDER_ID) + && Order::CanAllocateItem()) { /* Insert new implicit order */ Order *implicit_order = new Order(); implicit_order->MakeImplicit(this->last_station_visited); @@ -2402,7 +2405,7 @@ CommandCost Vehicle::SendToDepot(DoCommandFlag flags, DepotCommand command) if (flags & DC_EXEC) { if (this->current_order.IsType(OT_LOADING)) this->LeaveStation(); - if (this->IsGroundVehicle()) { + if (this->IsGroundVehicle() && this->HasExplicitOrders()) { uint16 &gv_flags = this->GetGroundVehicleFlags(); SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS); } diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 4eecda6ccd..a752096066 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -779,6 +779,18 @@ struct Vehicle : VehiclePool::PoolItem<&_vehicle_pool>, BaseVehicle, BaseConsist } } + /** + * A vehicle has explicit orders if the current real order index points to + * such an order. As soon as there is one explicit order the index will + * point to that one and it will never change to an implicit one. + * @return If vehicle has explicit orders + */ + bool HasExplicitOrders() const + { + const Order *cur = this->GetOrder(this->cur_real_order_index); + return cur != NULL && !cur->IsType(OT_IMPLICIT); + } + /** * Returns order 'index' of a vehicle or NULL when it doesn't exists * @param index the order to fetch