(svn r13726) [NoAI] -Add: AIVehicle::ReverseVehicle (Yexo) noai
authortruebrain
Fri, 18 Jul 2008 10:15:16 +0000
branchnoai
changeset 11168 3842648184cd
parent 11167 9f636bd0ff2d
(svn r13726) [NoAI] -Add: AIVehicle::ReverseVehicle (Yexo)
src/ai/api/ai_vehicle.cpp
src/ai/api/ai_vehicle.hpp
src/ai/api/ai_vehicle.hpp.sq
--- a/src/ai/api/ai_vehicle.cpp	Fri Jul 18 01:05:06 2008 +0000
+++ b/src/ai/api/ai_vehicle.cpp	Fri Jul 18 10:15:16 2008 +0000
@@ -124,6 +124,18 @@
 	return AIObject::DoCommand(0, vehicle_id, order_position, CMD_SKIP_TO_ORDER);
 }
 
+/* static */ bool AIVehicle::ReverseVehicle(VehicleID vehicle_id)
+{
+	EnforcePrecondition(false, IsValidVehicle(vehicle_id));
+	EnforcePrecondition(false, GetVehicleType(vehicle_id) == VEHICLE_ROAD || GetVehicleType(vehicle_id) == VEHICLE_RAIL);
+
+	switch (::GetVehicle(vehicle_id)->type) {
+		case VEH_ROAD: return AIObject::DoCommand(0, vehicle_id, 0, CMD_TURN_ROADVEH);
+		case VEH_TRAIN: return AIObject::DoCommand(0, vehicle_id, 0, CMD_REVERSE_TRAIN_DIRECTION);
+		default: NOT_REACHED();
+	}
+}
+
 /* static */ bool AIVehicle::SetName(VehicleID vehicle_id, const char *name)
 {
 	EnforcePrecondition(false, IsValidVehicle(vehicle_id));
--- a/src/ai/api/ai_vehicle.hpp	Fri Jul 18 01:05:06 2008 +0000
+++ b/src/ai/api/ai_vehicle.hpp	Fri Jul 18 10:15:16 2008 +0000
@@ -335,6 +335,17 @@
 	static bool SkipToVehicleOrder(VehicleID vehicle_id, AIOrder::OrderPosition order_position);
 
 	/**
+	 * Turn the given vehicle so it'll drive the other way.
+	 * @param vehicle_id The vehicle to turn.
+	 * @pre IsValidVehicle(vehicle_id).
+	 * @pre GetVehicleType(vehicle_id) == VEHICLE_ROAD || GetVehicleType(vehicle_id) == VEHICLE_RAIL.
+	 * @return True if and only if the vehicle has started to turn.
+	 * @note Vehicles cannot always be reversed. For example busses and trucks need to be running
+	 *  and not be inside a depot.
+	 */
+	static bool ReverseVehicle(VehicleID vehicle_id);
+
+	/**
 	 * Get the maximum amount of a specific cargo the given vehicle can transport.
 	 * @param vehicle_id The vehicle to get the capacity of.
 	 * @param cargo The cargo to get the capacity for.
--- a/src/ai/api/ai_vehicle.hpp.sq	Fri Jul 18 01:05:06 2008 +0000
+++ b/src/ai/api/ai_vehicle.hpp.sq	Fri Jul 18 10:15:16 2008 +0000
@@ -123,6 +123,7 @@
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SendVehicleToDepot, "SendVehicleToDepot", 2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::StartStopVehicle,   "StartStopVehicle",   2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::SkipToVehicleOrder, "SkipToVehicleOrder", 3, "xii");
+	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::ReverseVehicle,     "ReverseVehicle",     2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetCapacity,        "GetCapacity",        3, "xii");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetCargoLoad,       "GetCargoLoad",       3, "xii");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetGroupID,         "GetGroupID",         2, "xi");