summaryrefslogtreecommitdiff
path: root/src/ai/api
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2010-02-04 23:18:19 +0000
committeryexo <yexo@openttd.org>2010-02-04 23:18:19 +0000
commit091d88f5143601a456c3688ff9305f144a5ed6e2 (patch)
tree54471dd005dd9a741cd3692537eda46f80274530 /src/ai/api
parentd858264ca64c09802792457888e6f3506fada46d (diff)
downloadopenttd-091d88f5143601a456c3688ff9305f144a5ed6e2.tar.xz
(svn r19014) -Add: [NoAI] AIOrder::[G|S]etStopLocation to get/set the stop location of trains in a rail station
Diffstat (limited to 'src/ai/api')
-rw-r--r--src/ai/api/ai_changelog.hpp2
-rw-r--r--src/ai/api/ai_order.cpp22
-rw-r--r--src/ai/api/ai_order.hpp30
-rw-r--r--src/ai/api/ai_order.hpp.sq8
4 files changed, 62 insertions, 0 deletions
diff --git a/src/ai/api/ai_changelog.hpp b/src/ai/api/ai_changelog.hpp
index fc8b697c8..813ca1669 100644
--- a/src/ai/api/ai_changelog.hpp
+++ b/src/ai/api/ai_changelog.hpp
@@ -26,6 +26,8 @@
* \li AIInfo::AICONFIG_INGAME
* \li AIMarine::GetBuildCost
* \li AIOrder::AIOF_GOTO_NEAREST_DEPOT
+ * \li AIOrder::GetStopLocation
+ * \li AIOrder::SetStopLocation
* \li AIRail::RemoveRailStationTileRectangle
* \li AIRail::RemoveRailWaypointTileRectangle
* \li AIRail::GetBuildCost
diff --git a/src/ai/api/ai_order.cpp b/src/ai/api/ai_order.cpp
index 3e7ff11af..cdc56ccf8 100644
--- a/src/ai/api/ai_order.cpp
+++ b/src/ai/api/ai_order.cpp
@@ -285,6 +285,16 @@ static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition or
return value;
}
+/* static */ AIOrder::StopLocation AIOrder::GetStopLocation(VehicleID vehicle_id, OrderPosition order_position)
+{
+ if (!IsValidVehicleOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
+ if (AIVehicle::GetVehicleType(vehicle_id) != AIVehicle::VT_RAIL) return STOPLOCATION_INVALID;
+ if (!IsGotoStationOrder(vehicle_id, order_position)) return STOPLOCATION_INVALID;
+
+ const Order *order = Vehicle::Get(vehicle_id)->GetOrder(order_position);
+ return (AIOrder::StopLocation)order->GetStopLocation();
+}
+
/* static */ bool AIOrder::SetOrderJumpTo(VehicleID vehicle_id, OrderPosition order_position, OrderPosition jump_to)
{
EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
@@ -322,6 +332,18 @@ static const Order *ResolveOrder(VehicleID vehicle_id, AIOrder::OrderPosition or
return AIObject::DoCommand(0, vehicle_id | (order_position << 16), MOF_COND_VALUE | (value << 4), CMD_MODIFY_ORDER);
}
+/* static */ bool AIOrder::SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location)
+{
+ EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_position));
+ EnforcePrecondition(false, AIVehicle::GetVehicleType(vehicle_id) == AIVehicle::VT_RAIL);
+ EnforcePrecondition(false, IsGotoStationOrder(vehicle_id, order_position));
+ EnforcePrecondition(false, stop_location >= STOPLOCATION_NEAR && stop_location <= STOPLOCATION_FAR);
+
+ uint32 p1 = vehicle_id | (order_position << 16);
+ uint32 p2 = MOF_STOP_LOCATION | (stop_location << 4);
+ return AIObject::DoCommand(0, p1, p2, CMD_MODIFY_ORDER);
+}
+
/* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
{
EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
diff --git a/src/ai/api/ai_order.hpp b/src/ai/api/ai_order.hpp
index 53ce0d261..fe0dcd826 100644
--- a/src/ai/api/ai_order.hpp
+++ b/src/ai/api/ai_order.hpp
@@ -118,6 +118,14 @@ public:
ORDER_INVALID = -1, //!< An invalid order.
};
+ /** Where to stop trains in a station that's longer then the train */
+ enum StopLocation {
+ STOPLOCATION_NEAR, //!< Stop the train as soon as it's completely in the station
+ STOPLOCATION_MIDDLE, //!< Stop the train in the middle of the station
+ STOPLOCATION_FAR, //!< Stop the train at the far end of the station
+ STOPLOCATION_INVALID = -1, //!< An invalid stop location
+ };
+
/**
* Checks whether the given order id is valid for the given vehicle.
* @param vehicle_id The vehicle to check the order index for.
@@ -282,6 +290,17 @@ public:
static int32 GetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position);
/**
+ * Gets the stoplocation of the given order for the given train.
+ * @param vehicle_id The vehicle to get the value for.
+ * @param order_position The order to get the value for.
+ * @pre IsValidVehicleOrder(vehicle_id, order_position).
+ * @pre AIVehicle::GetVehicleType(vehicle_id) == AIVehicle::VT_RAIL.
+ * @pre IsGotoStationOrder(vehicle_id, order_position).
+ * @return The relative position where the train will stop inside a station.
+ */
+ static StopLocation GetStopLocation(VehicleID vehicle_id, OrderPosition order_position);
+
+ /**
* Sets the OrderPosition to jump to if the check succeeds of the given order for the given vehicle.
* @param vehicle_id The vehicle to set the OrderPosition for.
* @param order_position The order to set the OrderPosition for.
@@ -330,6 +349,17 @@ public:
static bool SetOrderCompareValue(VehicleID vehicle_id, OrderPosition order_position, int32 value);
/**
+ * Sets the stoplocation of the given order for the given train.
+ * @param vehicle_id The vehicle to get the value for.
+ * @param order_position The order to get the value for.
+ * @pre IsValidVehicleOrder(vehicle_id, order_position).
+ * @pre AIVehicle::GetVehicleType(vehicle_id) == AIVehicle::VT_RAIL.
+ * @pre IsGotoStationOrder(vehicle_id, order_position).
+ * @return Whether the order has been/can be changed.
+ */
+ static bool SetStopLocation(VehicleID vehicle_id, OrderPosition order_position, StopLocation stop_location);
+
+ /**
* Appends an order to the end of the vehicle's order list.
* @param vehicle_id The vehicle to append the order to.
* @param destination The destination of the order.
diff --git a/src/ai/api/ai_order.hpp.sq b/src/ai/api/ai_order.hpp.sq
index 53fa61e4e..c6aa1c873 100644
--- a/src/ai/api/ai_order.hpp.sq
+++ b/src/ai/api/ai_order.hpp.sq
@@ -23,6 +23,8 @@ namespace SQConvert {
template <> int Return<AIOrder::CompareFunction>(HSQUIRRELVM vm, AIOrder::CompareFunction res) { sq_pushinteger(vm, (int32)res); return 1; }
template <> AIOrder::OrderPosition GetParam(ForceType<AIOrder::OrderPosition>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIOrder::OrderPosition)tmp; }
template <> int Return<AIOrder::OrderPosition>(HSQUIRRELVM vm, AIOrder::OrderPosition res) { sq_pushinteger(vm, (int32)res); return 1; }
+ template <> AIOrder::StopLocation GetParam(ForceType<AIOrder::StopLocation>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIOrder::StopLocation)tmp; }
+ template <> int Return<AIOrder::StopLocation>(HSQUIRRELVM vm, AIOrder::StopLocation res) { sq_pushinteger(vm, (int32)res); return 1; }
/* Allow AIOrder to be used as Squirrel parameter */
template <> AIOrder *GetParam(ForceType<AIOrder *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIOrder *)instance; }
@@ -76,6 +78,10 @@ void SQAIOrder_Register(Squirrel *engine)
SQAIOrder.DefSQConst(engine, AIOrder::CF_INVALID, "CF_INVALID");
SQAIOrder.DefSQConst(engine, AIOrder::ORDER_CURRENT, "ORDER_CURRENT");
SQAIOrder.DefSQConst(engine, AIOrder::ORDER_INVALID, "ORDER_INVALID");
+ SQAIOrder.DefSQConst(engine, AIOrder::STOPLOCATION_NEAR, "STOPLOCATION_NEAR");
+ SQAIOrder.DefSQConst(engine, AIOrder::STOPLOCATION_MIDDLE, "STOPLOCATION_MIDDLE");
+ SQAIOrder.DefSQConst(engine, AIOrder::STOPLOCATION_FAR, "STOPLOCATION_FAR");
+ SQAIOrder.DefSQConst(engine, AIOrder::STOPLOCATION_INVALID, "STOPLOCATION_INVALID");
AIError::RegisterErrorMap(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS, AIOrder::ERR_ORDER_TOO_MANY);
AIError::RegisterErrorMap(STR_ERROR_TOO_FAR_FROM_PREVIOUS_DESTINATION, AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION);
@@ -99,10 +105,12 @@ void SQAIOrder_Register(Squirrel *engine)
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetOrderCondition, "GetOrderCondition", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetOrderCompareFunction, "GetOrderCompareFunction", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetOrderCompareValue, "GetOrderCompareValue", 3, ".ii");
+ SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetStopLocation, "GetStopLocation", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderJumpTo, "SetOrderJumpTo", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderCondition, "SetOrderCondition", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderCompareFunction, "SetOrderCompareFunction", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetOrderCompareValue, "SetOrderCompareValue", 4, ".iii");
+ SQAIOrder.DefSQStaticMethod(engine, &AIOrder::SetStopLocation, "SetStopLocation", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AppendOrder, "AppendOrder", 4, ".iii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::AppendConditionalOrder, "AppendConditionalOrder", 3, ".ii");
SQAIOrder.DefSQStaticMethod(engine, &AIOrder::InsertOrder, "InsertOrder", 5, ".iiii");