|
@@ -524,765 +524,765 @@ void OrderList::DeleteOrderAt(int index)
|
|
|
if (!to_remove->IsType(OT_IMPLICIT)) --this->num_manual_orders;
|
|
|
this->timetable_duration -= (to_remove->GetTimetabledWait() + to_remove->GetTimetabledTravel());
|
|
|
this->total_duration -= (to_remove->GetWaitTime() + to_remove->GetTravelTime());
|
|
|
delete to_remove;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Move an order to another position within the order list.
|
|
|
* @param from is the zero-based position of the order to move.
|
|
|
* @param to is the zero-based position where the order is moved to.
|
|
|
*/
|
|
|
void OrderList::MoveOrder(int from, int to)
|
|
|
{
|
|
|
if (from >= this->num_orders || to >= this->num_orders || from == to) return;
|
|
|
|
|
|
Order *moving_one;
|
|
|
|
|
|
/* Take the moving order out of the pointer-chain */
|
|
|
if (from == 0) {
|
|
|
moving_one = this->first;
|
|
|
this->first = moving_one->next;
|
|
|
} else {
|
|
|
Order *one_before = GetOrderAt(from - 1);
|
|
|
moving_one = one_before->next;
|
|
|
one_before->next = moving_one->next;
|
|
|
}
|
|
|
|
|
|
/* Insert the moving_order again in the pointer-chain */
|
|
|
if (to == 0) {
|
|
|
moving_one->next = this->first;
|
|
|
this->first = moving_one;
|
|
|
} else {
|
|
|
Order *one_before = GetOrderAt(to - 1);
|
|
|
moving_one->next = one_before->next;
|
|
|
one_before->next = moving_one;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Removes the vehicle from the shared order list.
|
|
|
* @note This is supposed to be called when the vehicle is still in the chain
|
|
|
* @param v vehicle to remove from the list
|
|
|
*/
|
|
|
void OrderList::RemoveVehicle(Vehicle *v)
|
|
|
{
|
|
|
--this->num_vehicles;
|
|
|
if (v == this->first_shared) this->first_shared = v->NextShared();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Checks whether a vehicle is part of the shared vehicle chain.
|
|
|
* @param v is the vehicle to search in the shared vehicle chain.
|
|
|
*/
|
|
|
bool OrderList::IsVehicleInSharedOrdersList(const Vehicle *v) const
|
|
|
{
|
|
|
for (const Vehicle *v_shared = this->first_shared; v_shared != nullptr; v_shared = v_shared->NextShared()) {
|
|
|
if (v_shared == v) return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Gets the position of the given vehicle within the shared order vehicle list.
|
|
|
* @param v is the vehicle of which to get the position
|
|
|
* @return position of v within the shared vehicle chain.
|
|
|
*/
|
|
|
int OrderList::GetPositionInSharedOrderList(const Vehicle *v) const
|
|
|
{
|
|
|
int count = 0;
|
|
|
for (const Vehicle *v_shared = v->PreviousShared(); v_shared != nullptr; v_shared = v_shared->PreviousShared()) count++;
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Checks whether all orders of the list have a filled timetable.
|
|
|
* @return whether all orders have a filled timetable.
|
|
|
*/
|
|
|
bool OrderList::IsCompleteTimetable() const
|
|
|
{
|
|
|
for (Order *o = this->first; o != nullptr; o = o->next) {
|
|
|
/* Implicit orders are, by definition, not timetabled. */
|
|
|
if (o->IsType(OT_IMPLICIT)) continue;
|
|
|
if (!o->IsCompletelyTimetabled()) return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Checks for internal consistency of order list. Triggers assertion if something is wrong.
|
|
|
*/
|
|
|
void OrderList::DebugCheckSanity() const
|
|
|
{
|
|
|
VehicleOrderID check_num_orders = 0;
|
|
|
VehicleOrderID check_num_manual_orders = 0;
|
|
|
uint check_num_vehicles = 0;
|
|
|
Ticks check_timetable_duration = 0;
|
|
|
Ticks check_total_duration = 0;
|
|
|
|
|
|
DEBUG(misc, 6, "Checking OrderList %hu for sanity...", this->index);
|
|
|
|
|
|
for (const Order *o = this->first; o != nullptr; o = o->next) {
|
|
|
++check_num_orders;
|
|
|
if (!o->IsType(OT_IMPLICIT)) ++check_num_manual_orders;
|
|
|
check_timetable_duration += o->GetTimetabledWait() + o->GetTimetabledTravel();
|
|
|
check_total_duration += o->GetWaitTime() + o->GetTravelTime();
|
|
|
}
|
|
|
assert(this->num_orders == check_num_orders);
|
|
|
assert(this->num_manual_orders == check_num_manual_orders);
|
|
|
assert(this->timetable_duration == check_timetable_duration);
|
|
|
assert(this->total_duration == check_total_duration);
|
|
|
|
|
|
for (const Vehicle *v = this->first_shared; v != nullptr; v = v->NextShared()) {
|
|
|
++check_num_vehicles;
|
|
|
assert(v->orders.list == this);
|
|
|
}
|
|
|
assert(this->num_vehicles == check_num_vehicles);
|
|
|
DEBUG(misc, 6, "... detected %u orders (%u manual), %u vehicles, %i timetabled, %i total",
|
|
|
(uint)this->num_orders, (uint)this->num_manual_orders,
|
|
|
this->num_vehicles, this->timetable_duration, this->total_duration);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Checks whether the order goes to a station or not, i.e. whether the
|
|
|
* destination is a station
|
|
|
* @param v the vehicle to check for
|
|
|
* @param o the order to check
|
|
|
* @return true if the destination is a station
|
|
|
*/
|
|
|
static inline bool OrderGoesToStation(const Vehicle *v, const Order *o)
|
|
|
{
|
|
|
return o->IsType(OT_GOTO_STATION) ||
|
|
|
(v->type == VEH_AIRCRAFT && o->IsType(OT_GOTO_DEPOT) && !(o->GetDepotActionType() & ODATFB_NEAREST_DEPOT));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Delete all news items regarding defective orders about a vehicle
|
|
|
* This could kill still valid warnings (for example about void order when just
|
|
|
* another order gets added), but assume the company will notice the problems,
|
|
|
* when (s)he's changing the orders.
|
|
|
*/
|
|
|
static void DeleteOrderWarnings(const Vehicle *v)
|
|
|
{
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_TOO_FEW_ORDERS);
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_VOID_ORDER);
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_DUPLICATE_ENTRY);
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_VEHICLE_HAS_INVALID_ENTRY);
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_PLANE_USES_TOO_SHORT_RUNWAY);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Returns a tile somewhat representing the order destination (not suitable for pathfinding).
|
|
|
* @param v The vehicle to get the location for.
|
|
|
* @param airport Get the airport tile and not the station location for aircraft.
|
|
|
* @return destination of order, or INVALID_TILE if none.
|
|
|
*/
|
|
|
TileIndex Order::GetLocation(const Vehicle *v, bool airport) const
|
|
|
{
|
|
|
switch (this->GetType()) {
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
case OT_GOTO_STATION:
|
|
|
case OT_IMPLICIT:
|
|
|
if (airport && v->type == VEH_AIRCRAFT) return Station::Get(this->GetDestination())->airport.tile;
|
|
|
return BaseStation::Get(this->GetDestination())->xy;
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
if ((this->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) return INVALID_TILE;
|
|
|
return (v->type == VEH_AIRCRAFT) ? Station::Get(this->GetDestination())->xy : Depot::Get(this->GetDestination())->xy;
|
|
|
|
|
|
default:
|
|
|
return INVALID_TILE;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Get the distance between two orders of a vehicle. Conditional orders are resolved
|
|
|
* and the bigger distance of the two order branches is returned.
|
|
|
* @param prev Origin order.
|
|
|
* @param cur Destination order.
|
|
|
* @param v The vehicle to get the distance for.
|
|
|
* @param conditional_depth Internal param for resolving conditional orders.
|
|
|
* @return Maximum distance between the two orders.
|
|
|
*/
|
|
|
uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle *v, int conditional_depth)
|
|
|
{
|
|
|
if (cur->IsType(OT_CONDITIONAL)) {
|
|
|
if (conditional_depth > v->GetNumOrders()) return 0;
|
|
|
|
|
|
conditional_depth++;
|
|
|
|
|
|
int dist1 = GetOrderDistance(prev, v->GetOrder(cur->GetConditionSkipToOrder()), v, conditional_depth);
|
|
|
int dist2 = GetOrderDistance(prev, cur->next == nullptr ? v->orders.list->GetFirstOrder() : cur->next, v, conditional_depth);
|
|
|
return max(dist1, dist2);
|
|
|
return std::max(dist1, dist2);
|
|
|
}
|
|
|
|
|
|
TileIndex prev_tile = prev->GetLocation(v, true);
|
|
|
TileIndex cur_tile = cur->GetLocation(v, true);
|
|
|
if (prev_tile == INVALID_TILE || cur_tile == INVALID_TILE) return 0;
|
|
|
return v->type == VEH_AIRCRAFT ? DistanceSquare(prev_tile, cur_tile) : DistanceManhattan(prev_tile, cur_tile);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Add an order to the orderlist of a vehicle.
|
|
|
* @param tile unused
|
|
|
* @param flags operation to perform
|
|
|
* @param p1 various bitstuffed elements
|
|
|
* - p1 = (bit 0 - 19) - ID of the vehicle
|
|
|
* - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
|
|
|
* the order will be inserted before that one
|
|
|
* the maximum vehicle order id is 254.
|
|
|
* @param p2 packed order to insert
|
|
|
* @param text unused
|
|
|
* @return the cost of this operation or an error
|
|
|
*/
|
|
|
CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
{
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
|
|
VehicleOrderID sel_ord = GB(p1, 20, 8);
|
|
|
Order new_order(p2);
|
|
|
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
|
|
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
/* Check if the inserted order is to the correct destination (owner, type),
|
|
|
* and has the correct flags if any */
|
|
|
switch (new_order.GetType()) {
|
|
|
case OT_GOTO_STATION: {
|
|
|
const Station *st = Station::GetIfValid(new_order.GetDestination());
|
|
|
if (st == nullptr) return CMD_ERROR;
|
|
|
|
|
|
if (st->owner != OWNER_NONE) {
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
}
|
|
|
|
|
|
if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
|
|
|
for (Vehicle *u = v->FirstShared(); u != nullptr; u = u->NextShared()) {
|
|
|
if (!CanVehicleUseStation(u, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER_SHARED);
|
|
|
}
|
|
|
|
|
|
/* Non stop only allowed for ground vehicles. */
|
|
|
if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
|
|
|
|
|
|
/* Filter invalid load/unload types. */
|
|
|
switch (new_order.GetLoadType()) {
|
|
|
case OLF_LOAD_IF_POSSIBLE: case OLFB_FULL_LOAD: case OLF_FULL_LOAD_ANY: case OLFB_NO_LOAD: break;
|
|
|
default: return CMD_ERROR;
|
|
|
}
|
|
|
switch (new_order.GetUnloadType()) {
|
|
|
case OUF_UNLOAD_IF_POSSIBLE: case OUFB_UNLOAD: case OUFB_TRANSFER: case OUFB_NO_UNLOAD: break;
|
|
|
default: return CMD_ERROR;
|
|
|
}
|
|
|
|
|
|
/* Filter invalid stop locations */
|
|
|
switch (new_order.GetStopLocation()) {
|
|
|
case OSL_PLATFORM_NEAR_END:
|
|
|
case OSL_PLATFORM_MIDDLE:
|
|
|
if (v->type != VEH_TRAIN) return CMD_ERROR;
|
|
|
FALLTHROUGH;
|
|
|
|
|
|
case OSL_PLATFORM_FAR_END:
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
return CMD_ERROR;
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case OT_GOTO_DEPOT: {
|
|
|
if ((new_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) == 0) {
|
|
|
if (v->type == VEH_AIRCRAFT) {
|
|
|
const Station *st = Station::GetIfValid(new_order.GetDestination());
|
|
|
|
|
|
if (st == nullptr) return CMD_ERROR;
|
|
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
if (!CanVehicleUseStation(v, st) || !st->airport.HasHangar()) {
|
|
|
return CMD_ERROR;
|
|
|
}
|
|
|
} else {
|
|
|
const Depot *dp = Depot::GetIfValid(new_order.GetDestination());
|
|
|
|
|
|
if (dp == nullptr) return CMD_ERROR;
|
|
|
|
|
|
CommandCost ret = CheckOwnership(GetTileOwner(dp->xy));
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
switch (v->type) {
|
|
|
case VEH_TRAIN:
|
|
|
if (!IsRailDepotTile(dp->xy)) return CMD_ERROR;
|
|
|
break;
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
if (!IsRoadDepotTile(dp->xy)) return CMD_ERROR;
|
|
|
break;
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
if (!IsShipDepotTile(dp->xy)) return CMD_ERROR;
|
|
|
break;
|
|
|
|
|
|
default: return CMD_ERROR;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && !v->IsGroundVehicle()) return CMD_ERROR;
|
|
|
if (new_order.GetDepotOrderType() & ~(ODTFB_PART_OF_ORDERS | ((new_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0 ? ODTFB_SERVICE : 0))) return CMD_ERROR;
|
|
|
if (new_order.GetDepotActionType() & ~(ODATFB_HALT | ODATFB_NEAREST_DEPOT)) return CMD_ERROR;
|
|
|
if ((new_order.GetDepotOrderType() & ODTFB_SERVICE) && (new_order.GetDepotActionType() & ODATFB_HALT)) return CMD_ERROR;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case OT_GOTO_WAYPOINT: {
|
|
|
const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination());
|
|
|
if (wp == nullptr) return CMD_ERROR;
|
|
|
|
|
|
switch (v->type) {
|
|
|
default: return CMD_ERROR;
|
|
|
|
|
|
case VEH_TRAIN: {
|
|
|
if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
|
|
|
|
|
|
CommandCost ret = CheckOwnership(wp->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER);
|
|
|
if (wp->owner != OWNER_NONE) {
|
|
|
CommandCost ret = CheckOwnership(wp->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
/* Order flags can be any of the following for waypoints:
|
|
|
* [non-stop]
|
|
|
* non-stop orders (if any) are only valid for trains */
|
|
|
if (new_order.GetNonStopType() != ONSF_STOP_EVERYWHERE && v->type != VEH_TRAIN) return CMD_ERROR;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case OT_CONDITIONAL: {
|
|
|
VehicleOrderID skip_to = new_order.GetConditionSkipToOrder();
|
|
|
if (skip_to != 0 && skip_to >= v->GetNumOrders()) return CMD_ERROR; // Always allow jumping to the first (even when there is no order).
|
|
|
if (new_order.GetConditionVariable() >= OCV_END) return CMD_ERROR;
|
|
|
|
|
|
OrderConditionComparator occ = new_order.GetConditionComparator();
|
|
|
if (occ >= OCC_END) return CMD_ERROR;
|
|
|
switch (new_order.GetConditionVariable()) {
|
|
|
case OCV_REQUIRES_SERVICE:
|
|
|
if (occ != OCC_IS_TRUE && occ != OCC_IS_FALSE) return CMD_ERROR;
|
|
|
break;
|
|
|
|
|
|
case OCV_UNCONDITIONALLY:
|
|
|
if (occ != OCC_EQUALS) return CMD_ERROR;
|
|
|
if (new_order.GetConditionValue() != 0) return CMD_ERROR;
|
|
|
break;
|
|
|
|
|
|
case OCV_LOAD_PERCENTAGE:
|
|
|
case OCV_RELIABILITY:
|
|
|
if (new_order.GetConditionValue() > 100) return CMD_ERROR;
|
|
|
FALLTHROUGH;
|
|
|
|
|
|
default:
|
|
|
if (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) return CMD_ERROR;
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
default: return CMD_ERROR;
|
|
|
}
|
|
|
|
|
|
if (sel_ord > v->GetNumOrders()) return CMD_ERROR;
|
|
|
|
|
|
if (v->GetNumOrders() >= MAX_VEH_ORDER_ID) return_cmd_error(STR_ERROR_TOO_MANY_ORDERS);
|
|
|
if (!Order::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
|
|
|
if (v->orders.list == nullptr && !OrderList::CanAllocateItem()) return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
Order *new_o = new Order();
|
|
|
new_o->AssignOrder(new_order);
|
|
|
InsertOrder(v, new_o, sel_ord);
|
|
|
}
|
|
|
|
|
|
return CommandCost();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 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.
|
|
|
*/
|
|
|
void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord)
|
|
|
{
|
|
|
/* Create new order and link in list */
|
|
|
if (v->orders.list == nullptr) {
|
|
|
v->orders.list = new OrderList(new_o, v);
|
|
|
} else {
|
|
|
v->orders.list->InsertOrderAt(new_o, sel_ord);
|
|
|
}
|
|
|
|
|
|
Vehicle *u = v->FirstShared();
|
|
|
DeleteOrderWarnings(u);
|
|
|
for (; u != nullptr; u = u->NextShared()) {
|
|
|
assert(v->orders.list == u->orders.list);
|
|
|
|
|
|
/* If there is added an order before the current one, we need
|
|
|
* to update the selected order. We do not change implicit/real order indices though.
|
|
|
* If the new order is between the current implicit order and real order, the implicit order will
|
|
|
* later skip the inserted order. */
|
|
|
if (sel_ord <= u->cur_real_order_index) {
|
|
|
uint cur = u->cur_real_order_index + 1;
|
|
|
/* Check if we don't go out of bound */
|
|
|
if (cur < u->GetNumOrders()) {
|
|
|
u->cur_real_order_index = cur;
|
|
|
}
|
|
|
}
|
|
|
if (sel_ord == u->cur_implicit_order_index && u->IsGroundVehicle()) {
|
|
|
/* We are inserting an order just before the current implicit order.
|
|
|
* We do not know whether we will reach current implicit or the newly inserted order first.
|
|
|
* So, disable creation of implicit orders until we are on track again. */
|
|
|
uint16 &gv_flags = u->GetGroundVehicleFlags();
|
|
|
SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
|
|
|
}
|
|
|
if (sel_ord <= u->cur_implicit_order_index) {
|
|
|
uint cur = u->cur_implicit_order_index + 1;
|
|
|
/* Check if we don't go out of bound */
|
|
|
if (cur < u->GetNumOrders()) {
|
|
|
u->cur_implicit_order_index = cur;
|
|
|
}
|
|
|
}
|
|
|
/* Update any possible open window of the vehicle */
|
|
|
InvalidateVehicleOrder(u, INVALID_VEH_ORDER_ID | (sel_ord << 8));
|
|
|
}
|
|
|
|
|
|
/* As we insert an order, the order to skip to will be 'wrong'. */
|
|
|
VehicleOrderID cur_order_id = 0;
|
|
|
for (Order *order : v->Orders()) {
|
|
|
if (order->IsType(OT_CONDITIONAL)) {
|
|
|
VehicleOrderID order_id = order->GetConditionSkipToOrder();
|
|
|
if (order_id >= sel_ord) {
|
|
|
order->SetConditionSkipToOrder(order_id + 1);
|
|
|
}
|
|
|
if (order_id == cur_order_id) {
|
|
|
order->SetConditionSkipToOrder((order_id + 1) % v->GetNumOrders());
|
|
|
}
|
|
|
}
|
|
|
cur_order_id++;
|
|
|
}
|
|
|
|
|
|
/* Make sure to rebuild the whole list */
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Declone an order-list
|
|
|
* @param *dst delete the orders of this vehicle
|
|
|
* @param flags execution flags
|
|
|
*/
|
|
|
static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags)
|
|
|
{
|
|
|
if (flags & DC_EXEC) {
|
|
|
DeleteVehicleOrders(dst);
|
|
|
InvalidateVehicleOrder(dst, VIWD_REMOVE_ALL_ORDERS);
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(dst->type), 0);
|
|
|
}
|
|
|
return CommandCost();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Delete an order from the orderlist of a vehicle.
|
|
|
* @param tile unused
|
|
|
* @param flags operation to perform
|
|
|
* @param p1 the ID of the vehicle
|
|
|
* @param p2 the order to delete (max 255)
|
|
|
* @param text unused
|
|
|
* @return the cost of this operation or an error
|
|
|
*/
|
|
|
CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
{
|
|
|
VehicleID veh_id = GB(p1, 0, 20);
|
|
|
VehicleOrderID sel_ord = GB(p2, 0, 8);
|
|
|
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh_id);
|
|
|
|
|
|
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
/* If we did not select an order, we maybe want to de-clone the orders */
|
|
|
if (sel_ord >= v->GetNumOrders()) return DecloneOrder(v, flags);
|
|
|
|
|
|
if (v->GetOrder(sel_ord) == nullptr) return CMD_ERROR;
|
|
|
|
|
|
if (flags & DC_EXEC) DeleteOrder(v, sel_ord);
|
|
|
return CommandCost();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Cancel the current loading order of the vehicle as the order was deleted.
|
|
|
* @param v the vehicle
|
|
|
*/
|
|
|
static void CancelLoadingDueToDeletedOrder(Vehicle *v)
|
|
|
{
|
|
|
assert(v->current_order.IsType(OT_LOADING));
|
|
|
/* NON-stop flag is misused to see if a train is in a station that is
|
|
|
* on his order list or not */
|
|
|
v->current_order.SetNonStopType(ONSF_STOP_EVERYWHERE);
|
|
|
/* When full loading, "cancel" that order so the vehicle doesn't
|
|
|
* stay indefinitely at this station anymore. */
|
|
|
if (v->current_order.GetLoadType() & OLFB_FULL_LOAD) v->current_order.SetLoadType(OLF_LOAD_IF_POSSIBLE);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Delete an order but skip the parameter validation.
|
|
|
* @param v The vehicle to delete the order from.
|
|
|
* @param sel_ord The id of the order to be deleted.
|
|
|
*/
|
|
|
void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord)
|
|
|
{
|
|
|
v->orders.list->DeleteOrderAt(sel_ord);
|
|
|
|
|
|
Vehicle *u = v->FirstShared();
|
|
|
DeleteOrderWarnings(u);
|
|
|
for (; u != nullptr; u = u->NextShared()) {
|
|
|
assert(v->orders.list == u->orders.list);
|
|
|
|
|
|
if (sel_ord == u->cur_real_order_index && u->current_order.IsType(OT_LOADING)) {
|
|
|
CancelLoadingDueToDeletedOrder(u);
|
|
|
}
|
|
|
|
|
|
if (sel_ord < u->cur_real_order_index) {
|
|
|
u->cur_real_order_index--;
|
|
|
} else if (sel_ord == u->cur_real_order_index) {
|
|
|
u->UpdateRealOrderIndex();
|
|
|
}
|
|
|
|
|
|
if (sel_ord < u->cur_implicit_order_index) {
|
|
|
u->cur_implicit_order_index--;
|
|
|
} else if (sel_ord == u->cur_implicit_order_index) {
|
|
|
/* Make sure the index is valid */
|
|
|
if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
|
|
|
|
|
|
/* Skip non-implicit orders for the implicit-order-index (e.g. if the current implicit order was deleted */
|
|
|
while (u->cur_implicit_order_index != u->cur_real_order_index && !u->GetOrder(u->cur_implicit_order_index)->IsType(OT_IMPLICIT)) {
|
|
|
u->cur_implicit_order_index++;
|
|
|
if (u->cur_implicit_order_index >= u->GetNumOrders()) u->cur_implicit_order_index = 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Update any possible open window of the vehicle */
|
|
|
InvalidateVehicleOrder(u, sel_ord | (INVALID_VEH_ORDER_ID << 8));
|
|
|
}
|
|
|
|
|
|
/* As we delete an order, the order to skip to will be 'wrong'. */
|
|
|
VehicleOrderID cur_order_id = 0;
|
|
|
for (Order *order : v->Orders()) {
|
|
|
if (order->IsType(OT_CONDITIONAL)) {
|
|
|
VehicleOrderID order_id = order->GetConditionSkipToOrder();
|
|
|
if (order_id >= sel_ord) {
|
|
|
order_id = max(order_id - 1, 0);
|
|
|
order_id = std::max(order_id - 1, 0);
|
|
|
}
|
|
|
if (order_id == cur_order_id) {
|
|
|
order_id = (order_id + 1) % v->GetNumOrders();
|
|
|
}
|
|
|
order->SetConditionSkipToOrder(order_id);
|
|
|
}
|
|
|
cur_order_id++;
|
|
|
}
|
|
|
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Goto order of order-list.
|
|
|
* @param tile unused
|
|
|
* @param flags operation to perform
|
|
|
* @param p1 The ID of the vehicle which order is skipped
|
|
|
* @param p2 the selected order to which we want to skip
|
|
|
* @param text unused
|
|
|
* @return the cost of this operation or an error
|
|
|
*/
|
|
|
CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
{
|
|
|
VehicleID veh_id = GB(p1, 0, 20);
|
|
|
VehicleOrderID sel_ord = GB(p2, 0, 8);
|
|
|
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh_id);
|
|
|
|
|
|
if (v == nullptr || !v->IsPrimaryVehicle() || sel_ord == v->cur_implicit_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR;
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
|
|
|
|
|
|
v->cur_implicit_order_index = v->cur_real_order_index = sel_ord;
|
|
|
v->UpdateRealOrderIndex();
|
|
|
|
|
|
InvalidateVehicleOrder(v, VIWD_MODIFY_ORDERS);
|
|
|
}
|
|
|
|
|
|
/* We have an aircraft/ship, they have a mini-schedule, so update them all */
|
|
|
if (v->type == VEH_AIRCRAFT) SetWindowClassesDirty(WC_AIRCRAFT_LIST);
|
|
|
if (v->type == VEH_SHIP) SetWindowClassesDirty(WC_SHIPS_LIST);
|
|
|
|
|
|
return CommandCost();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Move an order inside the orderlist
|
|
|
* @param tile unused
|
|
|
* @param flags operation to perform
|
|
|
* @param p1 the ID of the vehicle
|
|
|
* @param p2 order to move and target
|
|
|
* bit 0-15 : the order to move
|
|
|
* bit 16-31 : the target order
|
|
|
* @param text unused
|
|
|
* @return the cost of this operation or an error
|
|
|
* @note The target order will move one place down in the orderlist
|
|
|
* if you move the order upwards else it'll move it one place down
|
|
|
*/
|
|
|
CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
{
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
|
|
VehicleOrderID moving_order = GB(p2, 0, 16);
|
|
|
VehicleOrderID target_order = GB(p2, 16, 16);
|
|
|
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
|
|
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
/* Don't make senseless movements */
|
|
|
if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() ||
|
|
|
moving_order == target_order || v->GetNumOrders() <= 1) return CMD_ERROR;
|
|
|
|
|
|
Order *moving_one = v->GetOrder(moving_order);
|
|
|
/* Don't move an empty order */
|
|
|
if (moving_one == nullptr) return CMD_ERROR;
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
v->orders.list->MoveOrder(moving_order, target_order);
|
|
|
|
|
|
/* Update shared list */
|
|
|
Vehicle *u = v->FirstShared();
|
|
|
|
|
|
DeleteOrderWarnings(u);
|
|
|
|
|
|
for (; u != nullptr; u = u->NextShared()) {
|
|
|
/* Update the current order.
|
|
|
* There are multiple ways to move orders, which result in cur_implicit_order_index
|
|
|
* and cur_real_order_index to not longer make any sense. E.g. moving another
|
|
|
* real order between them.
|
|
|
*
|
|
|
* Basically one could choose to preserve either of them, but not both.
|
|
|
* While both ways are suitable in this or that case from a human point of view, neither
|
|
|
* of them makes really sense.
|
|
|
* However, from an AI point of view, preserving cur_real_order_index is the most
|
|
|
* predictable and transparent behaviour.
|
|
|
*
|
|
|
* With that decision it basically does not matter what we do to cur_implicit_order_index.
|
|
|
* If we change orders between the implicit- and real-index, the implicit orders are mostly likely
|
|
|
* completely out-dated anyway. So, keep it simple and just keep cur_implicit_order_index as well.
|
|
|
* The worst which can happen is that a lot of implicit orders are removed when reaching current_order.
|
|
|
*/
|
|
|
if (u->cur_real_order_index == moving_order) {
|
|
|
u->cur_real_order_index = target_order;
|
|
|
} else if (u->cur_real_order_index > moving_order && u->cur_real_order_index <= target_order) {
|
|
|
u->cur_real_order_index--;
|
|
|
} else if (u->cur_real_order_index < moving_order && u->cur_real_order_index >= target_order) {
|
|
|
u->cur_real_order_index++;
|
|
|
}
|
|
|
|
|
|
if (u->cur_implicit_order_index == moving_order) {
|
|
|
u->cur_implicit_order_index = target_order;
|
|
|
} else if (u->cur_implicit_order_index > moving_order && u->cur_implicit_order_index <= target_order) {
|
|
|
u->cur_implicit_order_index--;
|
|
|
} else if (u->cur_implicit_order_index < moving_order && u->cur_implicit_order_index >= target_order) {
|
|
|
u->cur_implicit_order_index++;
|
|
|
}
|
|
|
|
|
|
assert(v->orders.list == u->orders.list);
|
|
|
/* Update any possible open window of the vehicle */
|
|
|
InvalidateVehicleOrder(u, moving_order | (target_order << 8));
|
|
|
}
|
|
|
|
|
|
/* As we move an order, the order to skip to will be 'wrong'. */
|
|
|
for (Order *order : v->Orders()) {
|
|
|
if (order->IsType(OT_CONDITIONAL)) {
|
|
|
VehicleOrderID order_id = order->GetConditionSkipToOrder();
|
|
|
if (order_id == moving_order) {
|
|
|
order_id = target_order;
|
|
|
} else if (order_id > moving_order && order_id <= target_order) {
|
|
|
order_id--;
|
|
|
} else if (order_id < moving_order && order_id >= target_order) {
|
|
|
order_id++;
|
|
|
}
|
|
|
order->SetConditionSkipToOrder(order_id);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Make sure to rebuild the whole list */
|
|
|
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 0);
|
|
|
}
|
|
|
|
|
|
return CommandCost();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Modify an order in the orderlist of a vehicle.
|
|
|
* @param tile unused
|
|
|
* @param flags operation to perform
|
|
|
* @param p1 various bitstuffed elements
|
|
|
* - p1 = (bit 0 - 19) - ID of the vehicle
|
|
|
* - p1 = (bit 24 - 31) - the selected order (if any). If the last order is given,
|
|
|
* the order will be inserted before that one
|
|
|
* the maximum vehicle order id is 254.
|
|
|
* @param p2 various bitstuffed elements
|
|
|
* - p2 = (bit 0 - 3) - what data to modify (@see ModifyOrderFlags)
|
|
|
* - p2 = (bit 4 - 15) - the data to modify
|
|
|
* @param text unused
|
|
|
* @return the cost of this operation or an error
|
|
|
*/
|
|
|
CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
{
|
|
|
VehicleOrderID sel_ord = GB(p1, 20, 8);
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
|
|
ModifyOrderFlags mof = Extract<ModifyOrderFlags, 0, 4>(p2);
|
|
|
uint16 data = GB(p2, 4, 11);
|
|
|
|
|
|
if (mof >= MOF_END) return CMD_ERROR;
|
|
|
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
|
|
if (v == nullptr || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
/* Is it a valid order? */
|
|
|
if (sel_ord >= v->GetNumOrders()) return CMD_ERROR;
|
|
|
|
|
|
Order *order = v->GetOrder(sel_ord);
|
|
|
switch (order->GetType()) {
|
|
|
case OT_GOTO_STATION:
|
|
|
if (mof != MOF_NON_STOP && mof != MOF_STOP_LOCATION && mof != MOF_UNLOAD && mof != MOF_LOAD) return CMD_ERROR;
|
|
|
break;
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
if (mof != MOF_NON_STOP && mof != MOF_DEPOT_ACTION) return CMD_ERROR;
|
|
|
break;
|
|
@@ -1796,385 +1796,385 @@ void CheckOrders(const Vehicle *v)
|
|
|
|
|
|
/**
|
|
|
* Removes an order from all vehicles. Triggers when, say, a station is removed.
|
|
|
* @param type The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
|
|
|
* @param destination The destination. Can be a StationID, DepotID or WaypointID.
|
|
|
* @param hangar Only used for airports in the destination.
|
|
|
* When false, remove airport and hangar orders.
|
|
|
* When true, remove either airport or hangar order.
|
|
|
*/
|
|
|
void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination, bool hangar)
|
|
|
{
|
|
|
/* Aircraft have StationIDs for depot orders and never use DepotIDs
|
|
|
* This fact is handled specially below
|
|
|
*/
|
|
|
|
|
|
/* Go through all vehicles */
|
|
|
for (Vehicle *v : Vehicle::Iterate()) {
|
|
|
Order *order;
|
|
|
|
|
|
order = &v->current_order;
|
|
|
if ((v->type == VEH_AIRCRAFT && order->IsType(OT_GOTO_DEPOT) && !hangar ? OT_GOTO_STATION : order->GetType()) == type &&
|
|
|
(!hangar || v->type == VEH_AIRCRAFT) && v->current_order.GetDestination() == destination) {
|
|
|
order->MakeDummy();
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
|
|
}
|
|
|
|
|
|
/* Clear the order from the order-list */
|
|
|
int id = -1;
|
|
|
for (Order *order : v->Orders()) {
|
|
|
id++;
|
|
|
restart:
|
|
|
|
|
|
OrderType ot = order->GetType();
|
|
|
if (ot == OT_GOTO_DEPOT && (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) != 0) continue;
|
|
|
if (ot == OT_GOTO_DEPOT && hangar && v->type != VEH_AIRCRAFT) continue; // Not an aircraft? Can't have a hangar order.
|
|
|
if (ot == OT_IMPLICIT || (v->type == VEH_AIRCRAFT && ot == OT_GOTO_DEPOT && !hangar)) ot = OT_GOTO_STATION;
|
|
|
if (ot == type && order->GetDestination() == destination) {
|
|
|
/* We want to clear implicit orders, but we don't want to make them
|
|
|
* dummy orders. They should just vanish. Also check the actual order
|
|
|
* type as ot is currently OT_GOTO_STATION. */
|
|
|
if (order->IsType(OT_IMPLICIT)) {
|
|
|
order = order->next; // DeleteOrder() invalidates current order
|
|
|
DeleteOrder(v, id);
|
|
|
if (order != nullptr) goto restart;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
/* Clear wait time */
|
|
|
v->orders.list->UpdateTotalDuration(-order->GetWaitTime());
|
|
|
if (order->IsWaitTimetabled()) {
|
|
|
v->orders.list->UpdateTimetableDuration(-order->GetTimetabledWait());
|
|
|
order->SetWaitTimetabled(false);
|
|
|
}
|
|
|
order->SetWaitTime(0);
|
|
|
|
|
|
/* Clear order, preserving travel time */
|
|
|
bool travel_timetabled = order->IsTravelTimetabled();
|
|
|
order->MakeDummy();
|
|
|
order->SetTravelTimetabled(travel_timetabled);
|
|
|
|
|
|
for (const Vehicle *w = v->FirstShared(); w != nullptr; w = w->NextShared()) {
|
|
|
/* In GUI, simulate by removing the order and adding it back */
|
|
|
InvalidateVehicleOrder(w, id | (INVALID_VEH_ORDER_ID << 8));
|
|
|
InvalidateVehicleOrder(w, (INVALID_VEH_ORDER_ID << 8) | id);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
OrderBackup::RemoveOrder(type, destination, hangar);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Checks if a vehicle has a depot in its order list.
|
|
|
* @return True iff at least one order is a depot order.
|
|
|
*/
|
|
|
bool Vehicle::HasDepotOrder() const
|
|
|
{
|
|
|
for (const Order *order : this->Orders()) {
|
|
|
if (order->IsType(OT_GOTO_DEPOT)) return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Delete all orders from a vehicle
|
|
|
* @param v Vehicle whose orders to reset
|
|
|
* @param keep_orderlist If true, do not free the order list, only empty it.
|
|
|
* @param reset_order_indices If true, reset cur_implicit_order_index and cur_real_order_index
|
|
|
* and cancel the current full load order (if the vehicle is loading).
|
|
|
* If false, _you_ have to make sure the order indices are valid after
|
|
|
* your messing with them!
|
|
|
*/
|
|
|
void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
|
|
|
{
|
|
|
DeleteOrderWarnings(v);
|
|
|
|
|
|
if (v->IsOrderListShared()) {
|
|
|
/* Remove ourself from the shared order list. */
|
|
|
v->RemoveFromShared();
|
|
|
v->orders.list = nullptr;
|
|
|
} else if (v->orders.list != nullptr) {
|
|
|
/* Remove the orders */
|
|
|
v->orders.list->FreeChain(keep_orderlist);
|
|
|
if (!keep_orderlist) v->orders.list = nullptr;
|
|
|
}
|
|
|
|
|
|
if (reset_order_indices) {
|
|
|
v->cur_implicit_order_index = v->cur_real_order_index = 0;
|
|
|
if (v->current_order.IsType(OT_LOADING)) {
|
|
|
CancelLoadingDueToDeletedOrder(v);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Clamp the service interval to the correct min/max. The actual min/max values
|
|
|
* depend on whether it's in percent or days.
|
|
|
* @param interval proposed service interval
|
|
|
* @return Clamped service interval
|
|
|
*/
|
|
|
uint16 GetServiceIntervalClamped(uint interval, bool ispercent)
|
|
|
{
|
|
|
return ispercent ? Clamp(interval, MIN_SERVINT_PERCENT, MAX_SERVINT_PERCENT) : Clamp(interval, MIN_SERVINT_DAYS, MAX_SERVINT_DAYS);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* Check if a vehicle has any valid orders
|
|
|
*
|
|
|
* @return false if there are no valid orders
|
|
|
* @note Conditional orders are not considered valid destination orders
|
|
|
*
|
|
|
*/
|
|
|
static bool CheckForValidOrders(const Vehicle *v)
|
|
|
{
|
|
|
for (const Order *order : v->Orders()) {
|
|
|
switch (order->GetType()) {
|
|
|
case OT_GOTO_STATION:
|
|
|
case OT_GOTO_DEPOT:
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
return true;
|
|
|
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Compare the variable and value based on the given comparator.
|
|
|
*/
|
|
|
static bool OrderConditionCompare(OrderConditionComparator occ, int variable, int value)
|
|
|
{
|
|
|
switch (occ) {
|
|
|
case OCC_EQUALS: return variable == value;
|
|
|
case OCC_NOT_EQUALS: return variable != value;
|
|
|
case OCC_LESS_THAN: return variable < value;
|
|
|
case OCC_LESS_EQUALS: return variable <= value;
|
|
|
case OCC_MORE_THAN: return variable > value;
|
|
|
case OCC_MORE_EQUALS: return variable >= value;
|
|
|
case OCC_IS_TRUE: return variable != 0;
|
|
|
case OCC_IS_FALSE: return variable == 0;
|
|
|
default: NOT_REACHED();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Process a conditional order and determine the next order.
|
|
|
* @param order the order the vehicle currently has
|
|
|
* @param v the vehicle to update
|
|
|
* @return index of next order to jump to, or INVALID_VEH_ORDER_ID to use the next order
|
|
|
*/
|
|
|
VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
|
|
|
{
|
|
|
if (order->GetType() != OT_CONDITIONAL) return INVALID_VEH_ORDER_ID;
|
|
|
|
|
|
bool skip_order = false;
|
|
|
OrderConditionComparator occ = order->GetConditionComparator();
|
|
|
uint16 value = order->GetConditionValue();
|
|
|
|
|
|
switch (order->GetConditionVariable()) {
|
|
|
case OCV_LOAD_PERCENTAGE: skip_order = OrderConditionCompare(occ, CalcPercentVehicleFilled(v, nullptr), value); break;
|
|
|
case OCV_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->reliability), value); break;
|
|
|
case OCV_MAX_RELIABILITY: skip_order = OrderConditionCompare(occ, ToPercent16(v->GetEngine()->reliability), value); break;
|
|
|
case OCV_MAX_SPEED: skip_order = OrderConditionCompare(occ, v->GetDisplayMaxSpeed() * 10 / 16, value); break;
|
|
|
case OCV_AGE: skip_order = OrderConditionCompare(occ, v->age / DAYS_IN_LEAP_YEAR, value); break;
|
|
|
case OCV_REQUIRES_SERVICE: skip_order = OrderConditionCompare(occ, v->NeedsServicing(), value); break;
|
|
|
case OCV_UNCONDITIONALLY: skip_order = true; break;
|
|
|
case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, max(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1, 0) / DAYS_IN_LEAP_YEAR, value); break;
|
|
|
case OCV_REMAINING_LIFETIME: skip_order = OrderConditionCompare(occ, std::max(v->max_age - v->age + DAYS_IN_LEAP_YEAR - 1, 0) / DAYS_IN_LEAP_YEAR, value); break;
|
|
|
default: NOT_REACHED();
|
|
|
}
|
|
|
|
|
|
return skip_order ? order->GetConditionSkipToOrder() : (VehicleOrderID)INVALID_VEH_ORDER_ID;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Update the vehicle's destination tile from an order.
|
|
|
* @param order the order the vehicle currently has
|
|
|
* @param v the vehicle to update
|
|
|
* @param conditional_depth the depth (amount of steps) to go with conditional orders. This to prevent infinite loops.
|
|
|
* @param pbs_look_ahead Whether we are forecasting orders for pbs reservations in advance. If true, the order indices must not be modified.
|
|
|
*/
|
|
|
bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool pbs_look_ahead)
|
|
|
{
|
|
|
if (conditional_depth > v->GetNumOrders()) {
|
|
|
v->current_order.Free();
|
|
|
v->SetDestTile(0);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
switch (order->GetType()) {
|
|
|
case OT_GOTO_STATION:
|
|
|
v->SetDestTile(v->GetOrderStationLocation(order->GetDestination()));
|
|
|
return true;
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !v->NeedsServicing()) {
|
|
|
assert(!pbs_look_ahead);
|
|
|
UpdateVehicleTimetable(v, true);
|
|
|
v->IncrementRealOrderIndex();
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
|
|
|
/* We need to search for the nearest depot (hangar). */
|
|
|
TileIndex location;
|
|
|
DestinationID destination;
|
|
|
bool reverse;
|
|
|
|
|
|
if (v->FindClosestDepot(&location, &destination, &reverse)) {
|
|
|
/* PBS reservations cannot reverse */
|
|
|
if (pbs_look_ahead && reverse) return false;
|
|
|
|
|
|
v->SetDestTile(location);
|
|
|
v->current_order.MakeGoToDepot(destination, v->current_order.GetDepotOrderType(), v->current_order.GetNonStopType(), (OrderDepotActionFlags)(v->current_order.GetDepotActionType() & ~ODATFB_NEAREST_DEPOT), v->current_order.GetRefitCargo());
|
|
|
|
|
|
/* If there is no depot in front, reverse automatically (trains only) */
|
|
|
if (v->type == VEH_TRAIN && reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
|
|
|
|
|
|
if (v->type == VEH_AIRCRAFT) {
|
|
|
Aircraft *a = Aircraft::From(v);
|
|
|
if (a->state == FLYING && a->targetairport != destination) {
|
|
|
/* The aircraft is now heading for a different hangar than the next in the orders */
|
|
|
extern void AircraftNextAirportPos_and_Order(Aircraft *a);
|
|
|
AircraftNextAirportPos_and_Order(a);
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/* If there is no depot, we cannot help PBS either. */
|
|
|
if (pbs_look_ahead) return false;
|
|
|
|
|
|
UpdateVehicleTimetable(v, true);
|
|
|
v->IncrementRealOrderIndex();
|
|
|
} else {
|
|
|
if (v->type != VEH_AIRCRAFT) {
|
|
|
v->SetDestTile(Depot::Get(order->GetDestination())->xy);
|
|
|
} else {
|
|
|
Aircraft *a = Aircraft::From(v);
|
|
|
DestinationID destination = a->current_order.GetDestination();
|
|
|
if (a->targetairport != destination) {
|
|
|
/* The aircraft is now heading for a different hangar than the next in the orders */
|
|
|
a->SetDestTile(a->GetOrderStationLocation(destination));
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
v->SetDestTile(Waypoint::Get(order->GetDestination())->xy);
|
|
|
return true;
|
|
|
|
|
|
case OT_CONDITIONAL: {
|
|
|
assert(!pbs_look_ahead);
|
|
|
VehicleOrderID next_order = ProcessConditionalOrder(order, v);
|
|
|
if (next_order != INVALID_VEH_ORDER_ID) {
|
|
|
/* Jump to next_order. cur_implicit_order_index becomes exactly that order,
|
|
|
* cur_real_order_index might come after next_order. */
|
|
|
UpdateVehicleTimetable(v, false);
|
|
|
v->cur_implicit_order_index = v->cur_real_order_index = next_order;
|
|
|
v->UpdateRealOrderIndex();
|
|
|
v->current_order_time += v->GetOrder(v->cur_real_order_index)->GetTimetabledTravel();
|
|
|
|
|
|
/* Disable creation of implicit orders.
|
|
|
* When inserting them we do not know that we would have to make the conditional orders point to them. */
|
|
|
if (v->IsGroundVehicle()) {
|
|
|
uint16 &gv_flags = v->GetGroundVehicleFlags();
|
|
|
SetBit(gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
|
|
|
}
|
|
|
} else {
|
|
|
UpdateVehicleTimetable(v, true);
|
|
|
v->IncrementRealOrderIndex();
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
v->SetDestTile(0);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
assert(v->cur_implicit_order_index < v->GetNumOrders());
|
|
|
assert(v->cur_real_order_index < v->GetNumOrders());
|
|
|
|
|
|
/* Get the current order */
|
|
|
order = v->GetOrder(v->cur_real_order_index);
|
|
|
if (order != nullptr && order->IsType(OT_IMPLICIT)) {
|
|
|
assert(v->GetNumManualOrders() == 0);
|
|
|
order = nullptr;
|
|
|
}
|
|
|
|
|
|
if (order == nullptr) {
|
|
|
v->current_order.Free();
|
|
|
v->SetDestTile(0);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
v->current_order = *order;
|
|
|
return UpdateOrderDest(v, order, conditional_depth + 1, pbs_look_ahead);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Handle the orders of a vehicle and determine the next place
|
|
|
* to go to if needed.
|
|
|
* @param v the vehicle to do this for.
|
|
|
* @return true *if* the vehicle is eligible for reversing
|
|
|
* (basically only when leaving a station).
|
|
|
*/
|
|
|
bool ProcessOrders(Vehicle *v)
|
|
|
{
|
|
|
switch (v->current_order.GetType()) {
|
|
|
case OT_GOTO_DEPOT:
|
|
|
/* Let a depot order in the orderlist interrupt. */
|
|
|
if (!(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return false;
|
|
|
break;
|
|
|
|
|
|
case OT_LOADING:
|
|
|
return false;
|
|
|
|
|
|
case OT_LEAVESTATION:
|
|
|
if (v->type != VEH_AIRCRAFT) return false;
|
|
|
break;
|
|
|
|
|
|
default: break;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Reversing because of order change is allowed only just after leaving a
|
|
|
* station (and the difficulty setting to allowed, of course)
|
|
|
* this can be detected because only after OT_LEAVESTATION, current_order
|
|
|
* will be reset to nothing. (That also happens if no order, but in that case
|
|
|
* it won't hit the point in code where may_reverse is checked)
|
|
|
*/
|
|
|
bool may_reverse = v->current_order.IsType(OT_NOTHING);
|
|
|
|
|
|
/* Check if we've reached a 'via' destination. */
|
|
|
if (((v->current_order.IsType(OT_GOTO_STATION) && (v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) || v->current_order.IsType(OT_GOTO_WAYPOINT)) &&
|
|
|
IsTileType(v->tile, MP_STATION) &&
|
|
|
v->current_order.GetDestination() == GetStationIndex(v->tile)) {
|
|
|
v->DeleteUnreachedImplicitOrders();
|
|
|
/* We set the last visited station here because we do not want
|
|
|
* the train to stop at this 'via' station if the next order
|
|
|
* is a no-non-stop order; in that case not setting the last
|
|
|
* visited station will cause the vehicle to still stop. */
|
|
|
v->last_station_visited = v->current_order.GetDestination();
|
|
|
UpdateVehicleTimetable(v, true);
|
|
|
v->IncrementImplicitOrderIndex();
|
|
|
}
|
|
|
|
|
|
/* Get the current order */
|
|
|
assert(v->cur_implicit_order_index == 0 || v->cur_implicit_order_index < v->GetNumOrders());
|
|
|
v->UpdateRealOrderIndex();
|
|
|
|
|
|
const Order *order = v->GetOrder(v->cur_real_order_index);
|
|
|
if (order != nullptr && order->IsType(OT_IMPLICIT)) {
|
|
|
assert(v->GetNumManualOrders() == 0);
|
|
|
order = nullptr;
|
|
|
}
|
|
|
|