1
0
Fork 0

(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.
release/1.0
yexo 2009-04-26 16:14:53 +00:00
parent 779640b53a
commit 9101de49d8
6 changed files with 184 additions and 83 deletions

View File

@ -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); 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) /* static */ bool AIOrder::IsConditionalOrder(VehicleID vehicle_id, OrderPosition order_position)
{ {
if (order_position == ORDER_CURRENT) return false; if (order_position == ORDER_CURRENT) return false;
@ -55,6 +96,16 @@ static OrderType GetOrderTypeByTile(TileIndex t)
return order->GetType() == OT_CONDITIONAL; 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) /* static */ AIOrder::OrderPosition AIOrder::ResolveOrderPosition(VehicleID vehicle_id, OrderPosition order_position)
{ {
if (!AIVehicle::IsValidVehicle(vehicle_id)) return ORDER_INVALID; 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; 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); 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()) { switch (order->GetType()) {
case OT_GOTO_DEPOT: { case OT_GOTO_DEPOT: {
@ -163,13 +209,8 @@ static OrderType GetOrderTypeByTile(TileIndex t)
{ {
if (!IsValidVehicleOrder(vehicle_id, order_position)) return AIOF_INVALID; if (!IsValidVehicleOrder(vehicle_id, order_position)) return AIOF_INVALID;
const Order *order; const Order *order = ::ResolveOrder(vehicle_id, order_position);
if (order_position == ORDER_CURRENT) { if (order == NULL || order->GetType() == OT_CONDITIONAL) return AIOF_INVALID;
order = &::GetVehicle(vehicle_id)->current_order;
} else {
order = ::GetVehicleOrder(GetVehicle(vehicle_id), order_position);
if (order->GetType() == OT_CONDITIONAL) return AIOF_INVALID;
}
AIOrderFlags order_flags = AIOF_NONE; AIOrderFlags order_flags = AIOF_NONE;
order_flags |= (AIOrderFlags)order->GetNonStopType(); order_flags |= (AIOrderFlags)order->GetNonStopType();

View File

@ -118,6 +118,33 @@ public:
*/ */
static bool IsValidVehicleOrder(VehicleID vehicle_id, OrderPosition order_position); static bool IsValidVehicleOrder(VehicleID vehicle_id, OrderPosition order_position);
/**
* Checks whether the given order is a goto-station order.
* @param vehicle_id The vehicle to check.
* @param order_position The order index to check.
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @return True if and only if the order is a goto-station order.
*/
static bool IsGotoStationOrder(VehicleID vehicle_id, OrderPosition order_position);
/**
* Checks whether the given order is a goto-depot order.
* @param vehicle_id The vehicle to check.
* @param order_position The order index to check.
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @return True if and only if the order is a goto-depot order.
*/
static bool IsGotoDepotOrder(VehicleID vehicle_id, OrderPosition order_position);
/**
* Checks whether the given order is a goto-waypoint order.
* @param vehicle_id The vehicle to check.
* @param order_position The order index to check.
* @pre IsValidVehicleOrder(vehicle_id, order_position).
* @return True if and only if the order is a goto-waypoint order.
*/
static bool IsGotoWaypointOrder(VehicleID vehicle_id, OrderPosition order_position);
/** /**
* Checks whether the given order is a conditional order. * Checks whether the given order is a conditional order.
* @param vehicle_id The vehicle to check. * @param vehicle_id The vehicle to check.
@ -127,6 +154,17 @@ public:
*/ */
static bool IsConditionalOrder(VehicleID vehicle_id, OrderPosition order_position); static bool IsConditionalOrder(VehicleID vehicle_id, OrderPosition order_position);
/**
* Checks whether the current order is part of the orderlist.
* @param vehicle_id The vehicle to check.
* @pre AIVehicle::IsValidVehicle(vehicle_id).
* @return True if and only if the current order is part of the order list.
* @note If the order is a non-'non-stop' order, and the vehicle is currently
* (un)loading at a station that is not the final destination, this function
* will still return true.
*/
static bool IsCurrentOrderPartOfOrderList(VehicleID vehicle_id);
/** /**
* Resolves the given order index to the correct index for the given vehicle. * Resolves the given order index to the correct index for the given vehicle.
* If the order index was ORDER_CURRENT it will be resolved to the index of * If the order index was ORDER_CURRENT it will be resolved to the index of

View File

@ -74,7 +74,11 @@ void SQAIOrder_Register(Squirrel *engine) {
AIError::RegisterErrorMapString(AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION, "ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION"); AIError::RegisterErrorMapString(AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION, "ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsValidVehicleOrder, "IsValidVehicleOrder", 3, ".ii"); SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsValidVehicleOrder, "IsValidVehicleOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsGotoStationOrder, "IsGotoStationOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsGotoDepotOrder, "IsGotoDepotOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsGotoWaypointOrder, "IsGotoWaypointOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsConditionalOrder, "IsConditionalOrder", 3, ".ii"); SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsConditionalOrder, "IsConditionalOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsCurrentOrderPartOfOrderList, "IsCurrentOrderPartOfOrderList", 2, ".i");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::ResolveOrderPosition, "ResolveOrderPosition", 3, ".ii"); SQAIOrder.DefSQStaticMethod(engine, &AIOrder::ResolveOrderPosition, "ResolveOrderPosition", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AreOrderFlagsValid, "AreOrderFlagsValid", 3, ".ii"); SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AreOrderFlagsValid, "AreOrderFlagsValid", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsValidConditionalOrder, "IsValidConditionalOrder", 3, ".ii"); SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsValidConditionalOrder, "IsValidConditionalOrder", 3, ".ii");

View File

@ -160,6 +160,13 @@
return AIObject::DoCommand(0, vehicle_id, 0, GetCmdSendToDepot(::GetVehicle(vehicle_id))); return AIObject::DoCommand(0, vehicle_id, 0, GetCmdSendToDepot(::GetVehicle(vehicle_id)));
} }
/* static */ bool AIVehicle::SendVehicleToDepotForServicing(VehicleID vehicle_id)
{
EnforcePrecondition(false, IsValidVehicle(vehicle_id));
return AIObject::DoCommand(0, vehicle_id, DEPOT_SERVICE, GetCmdSendToDepot(::GetVehicle(vehicle_id)));
}
/* static */ bool AIVehicle::IsInDepot(VehicleID vehicle_id) /* static */ bool AIVehicle::IsInDepot(VehicleID vehicle_id)
{ {
if (!IsValidVehicle(vehicle_id)) return false; if (!IsValidVehicle(vehicle_id)) return false;

View File

@ -431,6 +431,16 @@ public:
*/ */
static bool SendVehicleToDepot(VehicleID vehicle_id); static bool SendVehicleToDepot(VehicleID vehicle_id);
/**
* Sends the given vehicle to a depot for servicing. If the vehicle has
* already been sent to a depot it continues with its normal orders instead.
* @param vehicle_id The vehicle to send to a depot for servicing.
* @pre IsValidVehicle(vehicle_id).
* @exception AIVehicle::ERR_VEHICLE_CANNOT_SEND_TO_DEPOT
* @return True if the current order was changed.
*/
static bool SendVehicleToDepotForServicing(VehicleID vehicle_id);
/** /**
* Starts or stops the given vehicle depending on the current state. * Starts or stops the given vehicle depending on the current state.
* @param vehicle_id The vehicle to start/stop. * @param vehicle_id The vehicle to start/stop.

View File

@ -130,6 +130,7 @@ void SQAIVehicle_Register(Squirrel *engine) {
SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SellWagon, "SellWagon", 3, ".ii"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SellWagon, "SellWagon", 3, ".ii");
SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SellWagonChain, "SellWagonChain", 3, ".ii"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SellWagonChain, "SellWagonChain", 3, ".ii");
SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SendVehicleToDepot, "SendVehicleToDepot", 2, ".i"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SendVehicleToDepot, "SendVehicleToDepot", 2, ".i");
SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SendVehicleToDepotForServicing, "SendVehicleToDepotForServicing", 2, ".i");
SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::StartStopVehicle, "StartStopVehicle", 2, ".i"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::StartStopVehicle, "StartStopVehicle", 2, ".i");
SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SkipToVehicleOrder, "SkipToVehicleOrder", 3, ".ii"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SkipToVehicleOrder, "SkipToVehicleOrder", 3, ".ii");
SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::ReverseVehicle, "ReverseVehicle", 2, ".i"); SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::ReverseVehicle, "ReverseVehicle", 2, ".i");