summaryrefslogtreecommitdiff
path: root/src/ai/api/ai_order.cpp
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2009-04-26 16:14:53 +0000
committeryexo <yexo@openttd.org>2009-04-26 16:14:53 +0000
commit9101de49d8fbc0b1f5e067ca2cd8c5906f30e84a (patch)
treeb441111de37f81dab27a535c5c3fe41360ba5fc6 /src/ai/api/ai_order.cpp
parent779640b53a212091e929a2d7603474ab7b2710f8 (diff)
downloadopenttd-9101de49d8fbc0b1f5e067ca2cd8c5906f30e84a.tar.xz
(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.
Diffstat (limited to 'src/ai/api/ai_order.cpp')
-rw-r--r--src/ai/api/ai_order.cpp69
1 files changed, 55 insertions, 14 deletions
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();