# HG changeset patch # User frosch # Date 2011-04-16 17:20:08 # Node ID 692f8d6f04d726af5dfb1a4a20cd96b8ea6a2a24 # Parent e215b17d8fbff0a536a190ed287e054f4922b7a1 (svn r22333) -Change: Prefer deleting automatic orders instead of inserting new ones. diff --git a/src/vehicle.cpp b/src/vehicle.cpp --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1870,7 +1870,49 @@ void Vehicle::BeginLoading() (!prev_order->IsType(OT_AUTOMATIC) && !prev_order->IsType(OT_GOTO_STATION)) || prev_order->GetDestination() != this->last_station_visited) { - if (!suppress_automatic_orders && this->orders.list->GetNumOrders() < MAX_VEH_ORDER_ID && Order::CanAllocateItem()) { + /* Prefer deleting automatic orders instead of inserting new ones, + * so test whether the right order follows later */ + int target_index = this->cur_auto_order_index; + bool found = false; + while (target_index != this->cur_real_order_index) { + const Order *order = this->GetOrder(target_index); + if (order->IsType(OT_AUTOMATIC) && 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_auto_order_index); // infinite loop? + } + + if (found) { + if (suppress_automatic_orders) { + /* Skip to the found order */ + this->cur_auto_order_index = target_index; + InvalidateVehicleOrder(this, 0); + } else { + /* Delete all automatic orders up to the station we just reached */ + const Order *order = this->GetOrder(this->cur_auto_order_index); + while (!order->IsType(OT_AUTOMATIC) || order->GetDestination() != this->last_station_visited) { + if (order->IsType(OT_AUTOMATIC)) { + /* Delete order effectively deletes order, so get the next before deleting it. */ + order = order->next; + DeleteOrder(this, this->cur_auto_order_index); + } else { + /* Skip non-automatic orders, e.g. service-orders */ + order = order->next; + this->cur_auto_order_index++; + } + + /* Wrap around */ + if (order == NULL) { + order = this->GetOrder(0); + this->cur_auto_order_index = 0; + } + assert(order != NULL); + } + } + } else if (!suppress_automatic_orders && this->orders.list->GetNumOrders() < MAX_VEH_ORDER_ID && Order::CanAllocateItem()) { /* Insert new automatic order */ Order *auto_order = new Order(); auto_order->MakeAutomatic(this->last_station_visited);