From 9101de49d8fbc0b1f5e067ca2cd8c5906f30e84a Mon Sep 17 00:00:00 2001 From: yexo Date: Sun, 26 Apr 2009 16:14:53 +0000 Subject: (svn r16165) -Add [FS#2801] [NoAI]: several functions to AIOrder to check the what kind of order an order is. -Fix: AIOrder::GetOrderDestination and AIOrder::GetOrderFlags didn't work on ORDER_CURRENT when the vehicle was loading/leaving in a station. --- src/ai/api/ai_order.cpp | 69 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 14 deletions(-) (limited to 'src/ai/api/ai_order.cpp') diff --git a/src/ai/api/ai_order.cpp b/src/ai/api/ai_order.cpp index 51ca973ca..fa397e6c0 100644 --- a/src/ai/api/ai_order.cpp +++ b/src/ai/api/ai_order.cpp @@ -46,6 +46,47 @@ static OrderType GetOrderTypeByTile(TileIndex t) return AIVehicle::IsValidVehicle(vehicle_id) && order_position >= 0 && (order_position < ::GetVehicle(vehicle_id)->GetNumOrders() || order_position == ORDER_CURRENT); } +/** + * Get the current order the vehicle is executing. If the current order is in + * the order list, return the order from the orderlist. If the current order + * was a manual order, return the current order. + */ +static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition order_position) +{ + const Vehicle *v = ::GetVehicle(vehicle_id); + if (order_position == AIOrder::ORDER_CURRENT) { + const Order *order = &v->current_order; + if (order->GetType() == OT_GOTO_DEPOT && !(order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS)) return order; + order_position = AIOrder::ResolveOrderPosition(vehicle_id, order_position); + if (order_position == AIOrder::ORDER_INVALID) return NULL; + } + return ::GetVehicleOrder(v, order_position); +} + +/* static */ bool AIOrder::IsGotoStationOrder(VehicleID vehicle_id, OrderPosition order_position) +{ + if (!IsValidVehicleOrder(vehicle_id, order_position)) return false; + + const Order *order = ::ResolveOrder(vehicle_id, order_position); + return order != NULL && order->GetType() == OT_GOTO_STATION; +} + +/* static */ bool AIOrder::IsGotoDepotOrder(VehicleID vehicle_id, OrderPosition order_position) +{ + if (!IsValidVehicleOrder(vehicle_id, order_position)) return false; + + const Order *order = ::ResolveOrder(vehicle_id, order_position); + return order != NULL && order->GetType() == OT_GOTO_DEPOT; +} + +/* static */ bool AIOrder::IsGotoWaypointOrder(VehicleID vehicle_id, OrderPosition order_position) +{ + if (!IsValidVehicleOrder(vehicle_id, order_position)) return false; + + const Order *order = ::ResolveOrder(vehicle_id, order_position); + return order != NULL && order->GetType() == OT_GOTO_WAYPOINT; +} + /* static */ bool AIOrder::IsConditionalOrder(VehicleID vehicle_id, OrderPosition order_position) { if (order_position == ORDER_CURRENT) return false; @@ -55,6 +96,16 @@ static OrderType GetOrderTypeByTile(TileIndex t) return order->GetType() == OT_CONDITIONAL; } +/* static */ bool AIOrder::IsCurrentOrderPartOfOrderList(VehicleID vehicle_id) +{ + if (AIVehicle::IsValidVehicle(vehicle_id)) return false; + if (GetOrderCount(vehicle_id) == 0) return false; + + const Order *order = &::GetVehicle(vehicle_id)->current_order; + if (order->GetType() != OT_GOTO_DEPOT) return true; + return (order->GetDepotOrderType() & ODTFB_PART_OF_ORDERS) != 0; +} + /* static */ AIOrder::OrderPosition AIOrder::ResolveOrderPosition(VehicleID vehicle_id, OrderPosition order_position) { if (!AIVehicle::IsValidVehicle(vehicle_id)) return ORDER_INVALID; @@ -114,14 +165,9 @@ static OrderType GetOrderTypeByTile(TileIndex t) { if (!IsValidVehicleOrder(vehicle_id, order_position)) return INVALID_TILE; - const Order *order; + const Order *order = ::ResolveOrder(vehicle_id, order_position); + if (order == NULL || order->GetType() == OT_CONDITIONAL) return INVALID_TILE; const Vehicle *v = ::GetVehicle(vehicle_id); - if (order_position == ORDER_CURRENT) { - order = &v->current_order; - } else { - order = ::GetVehicleOrder(GetVehicle(vehicle_id), order_position); - if (order->GetType() == OT_CONDITIONAL) return INVALID_TILE; - } switch (order->GetType()) { case OT_GOTO_DEPOT: { @@ -163,13 +209,8 @@ static OrderType GetOrderTypeByTile(TileIndex t) { if (!IsValidVehicleOrder(vehicle_id, order_position)) return AIOF_INVALID; - const Order *order; - if (order_position == ORDER_CURRENT) { - order = &::GetVehicle(vehicle_id)->current_order; - } else { - order = ::GetVehicleOrder(GetVehicle(vehicle_id), order_position); - if (order->GetType() == OT_CONDITIONAL) return AIOF_INVALID; - } + const Order *order = ::ResolveOrder(vehicle_id, order_position); + if (order == NULL || order->GetType() == OT_CONDITIONAL) return AIOF_INVALID; AIOrderFlags order_flags = AIOF_NONE; order_flags |= (AIOrderFlags)order->GetNonStopType(); -- cgit v1.2.3-54-g00ecf