* Insert a new order but skip the validation. * @param v The vehicle to insert the order to. * @param new_o The new order. * @param sel_ord The position the order should be inserted at. */
| 845 | * @param sel_ord The position the order should be inserted at. |
| 846 | */ |
| 847 | void InsertOrder(Vehicle *v, Order &&new_o, VehicleOrderID sel_ord) |
| 848 | { |
| 849 | /* Create new order and link in list */ |
| 850 | if (v->orders == nullptr) { |
| 851 | v->orders = new OrderList(std::move(new_o), v); |
| 852 | } else { |
| 853 | v->orders->InsertOrderAt(std::move(new_o), sel_ord); |
| 854 | } |
| 855 | |
| 856 | Vehicle *u = v->FirstShared(); |
| 857 | DeleteOrderWarnings(u); |
| 858 | for (; u != nullptr; u = u->NextShared()) { |
| 859 | assert(v->orders == u->orders); |
| 860 | |
| 861 | /* If there is added an order before the current one, we need |
| 862 | * to update the selected order. We do not change implicit/real order indices though. |
| 863 | * If the new order is between the current implicit order and real order, the implicit order will |
| 864 | * later skip the inserted order. */ |
| 865 | if (sel_ord <= u->cur_real_order_index) { |
| 866 | uint cur = u->cur_real_order_index + 1; |
| 867 | /* Check if we don't go out of bound */ |
| 868 | if (cur < u->GetNumOrders()) { |
| 869 | u->cur_real_order_index = cur; |
| 870 | } |
| 871 | } |
| 872 | if (sel_ord == u->cur_implicit_order_index && u->IsGroundVehicle()) { |
| 873 | /* We are inserting an order just before the current implicit order. |
| 874 | * We do not know whether we will reach current implicit or the newly inserted order first. |
| 875 | * So, disable creation of implicit orders until we are on track again. */ |
| 876 | uint16_t &gv_flags = u->GetGroundVehicleFlags(); |
| 877 | SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS); |
| 878 | } |
| 879 | if (sel_ord <= u->cur_implicit_order_index) { |
| 880 | uint cur = u->cur_implicit_order_index + 1; |
| 881 | /* Check if we don't go out of bound */ |
| 882 | if (cur < u->GetNumOrders()) { |
| 883 | u->cur_implicit_order_index = cur; |
| 884 | } |
| 885 | } |
| 886 | /* Unbunching data is no longer valid. */ |
| 887 | u->ResetDepotUnbunching(); |
| 888 | |
| 889 | /* Update any possible open window of the vehicle */ |
| 890 | InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8)); |
| 891 | } |
| 892 | |
| 893 | /* As we insert an order, the order to skip to will be 'wrong'. */ |
| 894 | VehicleOrderID cur_order_id = 0; |
| 895 | for (Order &order : v->Orders()) { |
| 896 | if (order.IsType(OT_CONDITIONAL)) { |
| 897 | VehicleOrderID order_id = order.GetConditionSkipToOrder(); |
| 898 | if (order_id >= sel_ord) { |
| 899 | order.SetConditionSkipToOrder(order_id + 1); |
| 900 | } |
| 901 | if (order_id == cur_order_id) { |
| 902 | order.SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders()); |
| 903 | } |
| 904 | } |
no test coverage detected