(svn r12623) [NoAI] -Add: support for GetLastError in AIOrder. noai
authorrubidium
Tue, 08 Apr 2008 07:19:02 +0000
branchnoai
changeset 10093 b3849a19d137
parent 10091 e4feb2f9fedf
child 10094 e737405b06dd
(svn r12623) [NoAI] -Add: support for GetLastError in AIOrder.
src/ai/api/ai_error.hpp
src/ai/api/ai_error.hpp.sq
src/ai/api/ai_order.cpp
src/ai/api/ai_order.hpp
src/ai/api/ai_order.hpp.sq
--- a/src/ai/api/ai_error.hpp	Mon Apr 07 22:08:28 2008 +0000
+++ b/src/ai/api/ai_error.hpp	Tue Apr 08 07:19:02 2008 +0000
@@ -39,6 +39,7 @@
 		ERR_CAT_TILE,     //!< Error messages related to raising / lowering and demolishing tiles.
 		ERR_CAT_SIGN,     //!< Error messages related to building / removing signs.
 		ERR_CAT_ROAD,     //!< Error messages related to building / maintaining roads.
+		ERR_CAT_ORDER,    //!< Error messages related to managing orders.
 
 		/**
 		 * DO NOT USE! The error bitsize determines how many errors can be stored in
--- a/src/ai/api/ai_error.hpp.sq	Mon Apr 07 22:08:28 2008 +0000
+++ b/src/ai/api/ai_error.hpp.sq	Tue Apr 08 07:19:02 2008 +0000
@@ -29,6 +29,7 @@
 	SQAIError.DefSQConst(engine, AIError::ERR_CAT_TILE,                 "ERR_CAT_TILE");
 	SQAIError.DefSQConst(engine, AIError::ERR_CAT_SIGN,                 "ERR_CAT_SIGN");
 	SQAIError.DefSQConst(engine, AIError::ERR_CAT_ROAD,                 "ERR_CAT_ROAD");
+	SQAIError.DefSQConst(engine, AIError::ERR_CAT_ORDER,                "ERR_CAT_ORDER");
 	SQAIError.DefSQConst(engine, AIError::ERR_CAT_BIT_SIZE,             "ERR_CAT_BIT_SIZE");
 	SQAIError.DefSQConst(engine, AIError::ERR_NONE,                     "ERR_NONE");
 	SQAIError.DefSQConst(engine, AIError::ERR_UNKNOWN,                  "ERR_UNKNOWN");
--- a/src/ai/api/ai_order.cpp	Mon Apr 07 22:08:28 2008 +0000
+++ b/src/ai/api/ai_order.cpp	Tue Apr 08 07:19:02 2008 +0000
@@ -104,15 +104,16 @@
 
 /* static */ bool AIOrder::AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags)
 {
-	if (!AIVehicle::IsValidVehicle(vehicle_id)) return false;
+	EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
 	return InsertOrder(vehicle_id, ::GetVehicle(vehicle_id)->num_orders, destination, order_flags);
 }
 
 /* static */ bool AIOrder::InsertOrder(VehicleID vehicle_id, uint32 order_id, TileIndex destination, AIOrder::AIOrderFlags order_flags)
 {
 	/* IsValidVehicleOrder is not good enough because it does not allow appending. */
-	if (!AIVehicle::IsValidVehicle(vehicle_id) || order_id > ::GetVehicle(vehicle_id)->num_orders ||
-		!AreOrderFlagsValid(destination, order_flags)) return false;
+	EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
+       EnforcePrecondition(false, order_id <= ::GetVehicle(vehicle_id)->num_orders);
+	EnforcePrecondition(false, AreOrderFlagsValid(destination, order_flags));
 
 	Order order;
 	switch (::GetOrderTypeByTile(destination)) {
@@ -142,15 +143,15 @@
 
 /* static */ bool AIOrder::RemoveOrder(VehicleID vehicle_id, uint32 order_id)
 {
-	if (!IsValidVehicleOrder(vehicle_id, order_id)) return false;
+	EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id));
 
 	return AIObject::DoCommand(0, vehicle_id, order_id, CMD_DELETE_ORDER);
 }
 
 /* static */ bool AIOrder::ChangeOrder(VehicleID vehicle_id, uint32 order_id, AIOrder::AIOrderFlags order_flags)
 {
-	if (!IsValidVehicleOrder(vehicle_id, order_id) ||
-		!AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_id), order_flags)) return false;
+	EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id));
+	EnforcePrecondition(false, AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_id), order_flags));
 
 	for (;;) {
 		/* Loop as long as there is a difference between the requested and
@@ -166,29 +167,31 @@
 
 /* static */ bool AIOrder::MoveOrder(VehicleID vehicle_id, uint32 order_id_move, uint32 order_id_target)
 {
-	if (!IsValidVehicleOrder(vehicle_id, order_id_move) || !IsValidVehicleOrder(vehicle_id, order_id_target))
-		return false;
+	EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id_move));
+       EnforcePrecondition(false, IsValidVehicleOrder(vehicle_id, order_id_target));
 
 	return AIObject::DoCommand(0, vehicle_id, order_id_move | (order_id_target << 16), CMD_MOVE_ORDER);
 }
 
 /* static */ bool AIOrder::CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
 {
-	if (!AIVehicle::IsValidVehicle(vehicle_id) || !AIVehicle::IsValidVehicle(main_vehicle_id)) return false;
+	EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
+       EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
 
 	return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_COPY, CMD_CLONE_ORDER);
 }
 
 /* static */ bool AIOrder::ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id)
 {
-	if (!AIVehicle::IsValidVehicle(vehicle_id) || !AIVehicle::IsValidVehicle(main_vehicle_id)) return false;
+	EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
+       EnforcePrecondition(false, AIVehicle::IsValidVehicle(main_vehicle_id));
 
 	return AIObject::DoCommand(0, vehicle_id | (main_vehicle_id << 16), CO_SHARE, CMD_CLONE_ORDER);
 }
 
 /* static */ bool AIOrder::UnshareOrders(VehicleID vehicle_id)
 {
-	if (!AIVehicle::IsValidVehicle(vehicle_id)) return false;
+	EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
 
 	return AIObject::DoCommand(0, vehicle_id, CO_UNSHARE, CMD_CLONE_ORDER);
 }
--- a/src/ai/api/ai_order.hpp	Mon Apr 07 22:08:28 2008 +0000
+++ b/src/ai/api/ai_order.hpp	Tue Apr 08 07:19:02 2008 +0000
@@ -6,6 +6,7 @@
 #define AI_ORDER_HPP
 
 #include "ai_object.hpp"
+#include "ai_error.hpp"
 
 /**
  * Class that handles all order related functions.
@@ -14,6 +15,17 @@
 public:
 	static const char *GetClassName() { return "AIOrder"; }
 
+	enum ErrorMessages {
+		/** Base for all order related errors */
+		ERR_ORDER_BASE = AIError::ERR_CAT_ORDER << AIError::ERR_CAT_BIT_SIZE,
+
+		/** No more space for orders */
+		ERR_ORDER_TOO_MANY,                                  // [STR_8831_NO_MORE_SPACE_FOR_ORDERS]
+
+		/** Destination of new order is to far away from the previous order */
+		ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION,    // [STR_0210_TOO_FAR_FROM_PREVIOUS_DESTINATIO]
+	};
+
 	/**
 	 * Flags that can be used to modify the behaviour of orders.
 	 */
@@ -89,6 +101,9 @@
 	 * @param order_flags The flags given to the order.
 	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
 	 * @pre AreOrderFlagsValid(destination, order_flags).
+	 * @exception AIError::ERR_OWNED_BY_ANOTHER_COMPANY
+	 * @exception AIOrder::ERR_ORDER_NO_MORE_SPACE
+	 * @exception AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION
 	 * @return True if and only if the order was appended.
 	 */
 	static bool AppendOrder(VehicleID vehicle_id, TileIndex destination, AIOrderFlags order_flags);
@@ -101,6 +116,9 @@
 	 * @param order_flags The flags given to the order.
 	 * @pre IsValidVehicleOrder(vehicle_id, order_id).
 	 * @pre AreOrderFlagsValid(destination, order_flags).
+	 * @exception AIError::ERR_OWNED_BY_ANOTHER_COMPANY
+	 * @exception AIOrder::ERR_ORDER_NO_MORE_SPACE
+	 * @exception AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION
 	 * @return True if and only if the order was inserted.
 	 */
 	static bool InsertOrder(VehicleID vehicle_id, uint32 order_id, TileIndex destination, AIOrderFlags order_flags);
@@ -110,6 +128,7 @@
 	 * @param vehicle_id The vehicle to remove the order from.
 	 * @param order_id The order to remove from the order list.
 	 * @pre AIVehicle::IsValidVehicleOrder(vehicle_id, order_id).
+	 * @exception AIError::ERR_OWNED_BY_ANOTHER_COMPANY
 	 * @return True if and only if the order was removed.
 	 */
 	static bool RemoveOrder(VehicleID vehicle_id, uint32 order_id);
@@ -121,6 +140,7 @@
 	 * @param order_flags The new flags given to the order.
 	 * @pre IsValidVehicleOrder(vehicle_id, order_id).
 	 * @pre AreOrderFlagsValid(GetOrderDestination(vehicle_id, order_id), order_flags).
+	 * @exception AIError::ERR_OWNED_BY_ANOTHER_COMPANY
 	 * @return True if and only if the order was changed.
 	 */
 	static bool ChangeOrder(VehicleID vehicle_id, uint32 order_id, AIOrderFlags order_flags);
@@ -132,6 +152,7 @@
 	 * @param order_id_target The target order
 	 * @pre IsValidVehicleOrder(vehicle_id, order_id_move).
 	 * @pre IsValidVehicleOrder(vehicle_id, order_id_target).
+	 * @exception AIError::ERR_OWNED_BY_ANOTHER_COMPANY
 	 * @return True if and only if the order was moved.
 	 * @note If the order is moved to a lower place (e.g. from 7 to 2)
 	 *  the target order is moved upwards (e.g. 3). If the order is moved
@@ -147,6 +168,8 @@
 	 * @param main_vehicle_id The vehicle to copy the orders from.
 	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
 	 * @pre AIVehicle::IsValidVehicle(main_vehicle_id).
+	 * @exception AIError::ERR_OWNED_BY_ANOTHER_COMPANY
+	 * @exception AIOrder::ERR_ORDER_NO_MORE_SPACE
 	 * @return True if and only if the copying succeeded.
 	 */
 	static bool CopyOrders(VehicleID vehicle_id, VehicleID main_vehicle_id);
@@ -158,6 +181,7 @@
 	 * @param main_vehicle_id The vehicle to share the orders with.
 	 * @pre AIVehicle::IsValidVehicle(vehicle_id).
 	 * @pre AIVehicle::IsValidVehicle(main_vehicle_id).
+	 * @exception AIError::ERR_OWNED_BY_ANOTHER_COMPANY
 	 * @return True if and only if the sharing succeeded.
 	 */
 	static bool ShareOrders(VehicleID vehicle_id, VehicleID main_vehicle_id);
--- a/src/ai/api/ai_order.hpp.sq	Mon Apr 07 22:08:28 2008 +0000
+++ b/src/ai/api/ai_order.hpp.sq	Tue Apr 08 07:19:02 2008 +0000
@@ -2,6 +2,8 @@
 
 namespace SQConvert {
 	/* Allow enums to be used as Squirrel parameters */
+	template <> AIOrder::ErrorMessages GetParam(ForceType<AIOrder::ErrorMessages>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIOrder::ErrorMessages)tmp; }
+	template <> int Return<AIOrder::ErrorMessages>(HSQUIRRELVM vm, AIOrder::ErrorMessages res) { sq_pushinteger(vm, (int32)res); return 1; }
 	template <> AIOrder::AIOrderFlags GetParam(ForceType<AIOrder::AIOrderFlags>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIOrder::AIOrderFlags)tmp; }
 	template <> int Return<AIOrder::AIOrderFlags>(HSQUIRRELVM vm, AIOrder::AIOrderFlags res) { sq_pushinteger(vm, (int32)res); return 1; }
 
@@ -18,13 +20,22 @@
 	SQAIOrder.PreRegister(engine);
 	SQAIOrder.AddConstructor<void (AIOrder::*)(), 1>(engine, "x");
 
-	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_NONE,              "AIOF_NONE");
-	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_TRANSFER,          "AIOF_TRANSFER");
-	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_UNLOAD,            "AIOF_UNLOAD");
-	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_FULL_LOAD,         "AIOF_FULL_LOAD");
-	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_SERVICE_IF_NEEDED, "AIOF_SERVICE_IF_NEEDED");
-	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_NON_STOP,          "AIOF_NON_STOP");
-	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_INVALID,           "AIOF_INVALID");
+	SQAIOrder.DefSQConst(engine, AIOrder::ERR_ORDER_BASE,                                   "ERR_ORDER_BASE");
+	SQAIOrder.DefSQConst(engine, AIOrder::ERR_ORDER_TOO_MANY,                               "ERR_ORDER_TOO_MANY");
+	SQAIOrder.DefSQConst(engine, AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION, "ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION");
+	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_NONE,                                        "AIOF_NONE");
+	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_TRANSFER,                                    "AIOF_TRANSFER");
+	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_UNLOAD,                                      "AIOF_UNLOAD");
+	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_FULL_LOAD,                                   "AIOF_FULL_LOAD");
+	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_SERVICE_IF_NEEDED,                           "AIOF_SERVICE_IF_NEEDED");
+	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_NON_STOP,                                    "AIOF_NON_STOP");
+	SQAIOrder.DefSQConst(engine, AIOrder::AIOF_INVALID,                                     "AIOF_INVALID");
+
+	AIError::RegisterErrorMap(STR_8831_NO_MORE_SPACE_FOR_ORDERS,         AIOrder::ERR_ORDER_TOO_MANY);
+	AIError::RegisterErrorMap(STR_0210_TOO_FAR_FROM_PREVIOUS_DESTINATIO, AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION);
+
+	AIError::RegisterErrorMapString(AIOrder::ERR_ORDER_TOO_MANY,                               "ERR_ORDER_TOO_MANY");
+	AIError::RegisterErrorMapString(AIOrder::ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION, "ERR_ORDER_TOO_FAR_AWAY_FROM_PREVIOUS_DESTINATION");
 
 	SQAIOrder.DefSQStaticMethod(engine, &AIOrder::GetClassName,        "GetClassName",        1, "x");
 	SQAIOrder.DefSQStaticMethod(engine, &AIOrder::IsValidVehicleOrder, "IsValidVehicleOrder", 3, "xii");