(svn r12555) [NoAI] -Add: support for GetLastError for AICompany. noai
authorrubidium
Thu, 03 Apr 2008 23:01:54 +0000
branchnoai
changeset 9865 f241472f09dc
parent 9864 4a9b7b610b13
child 9866 efc38e1f559a
(svn r12555) [NoAI] -Add: support for GetLastError for AICompany.
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
src/ai/api/ai_company.cpp
src/ai/api/ai_company.hpp
src/ai/api/ai_error.hpp
src/ai/api/ai_error.hpp.sq
--- a/bin/ai/regression/regression.nut	Thu Apr 03 22:32:28 2008 +0000
+++ b/bin/ai/regression/regression.nut	Thu Apr 03 23:01:54 2008 +0000
@@ -209,6 +209,7 @@
 			local exec = AIExecMode();
 			print("  SetCompanyName():     " + AICompany.SetCompanyName("Regression"));
 			print("  SetCompanyName():     " + AICompany.SetCompanyName("Regression"));
+			print("  GetLastErrorString(): " + AIError.GetLastErrorString());
 		}
 	}
 
@@ -225,6 +226,7 @@
 	print("  SetLoanAmount(1):            " + AICompany.SetLoanAmount(1));
 	print("  SetLoanAmount(100):          " + AICompany.SetLoanAmount(100));
 	print("  SetLoanAmount(10000):        " + AICompany.SetLoanAmount(10000));
+	print("  GetLastErrorString():        " + AIError.GetLastErrorString());
 	print("  GetBankBalance():            " + AICompany.GetBankBalance(AICompany.MY_COMPANY));
 	print("  GetLoanAmount():             " + AICompany.GetLoanAmount());
 	print("  SetMinimumLoanAmount(31337): " + AICompany.SetMinimumLoanAmount(31337));
@@ -238,6 +240,8 @@
 	print("  GetCompanyHQ():              " + AICompany.GetCompanyHQ(AICompany.MY_COMPANY));
 	print("  BuildCompanyHQ():            " + AICompany.BuildCompanyHQ(AIMap.GetTileIndex(129, 129)));
 	print("  GetCompanyHQ():              " + AICompany.GetCompanyHQ(AICompany.MY_COMPANY));
+	print("  BuildCompanyHQ():            " + AICompany.BuildCompanyHQ(AIMap.GetTileIndex(129, 128)));
+	print("  GetLastErrorString():        " + AIError.GetLastErrorString());
 }
 
 function Regression::Engine()
--- a/bin/ai/regression/regression.txt	Thu Apr 03 22:32:28 2008 +0000
+++ b/bin/ai/regression/regression.txt	Thu Apr 03 23:01:54 2008 +0000
@@ -807,6 +807,7 @@
   SetCompanyName():     true
   SetCompanyName():     true
   SetCompanyName():     false
+  GetLastErrorString(): ERR_NAME_IS_NOT_UNIQUE
   GetCompanyName():            Regression
   GetPresidentName():          I. Campbell
   SetPresidentName():          true
@@ -820,6 +821,7 @@
   SetLoanAmount(1):            false
   SetLoanAmount(100):          false
   SetLoanAmount(10000):        false
+  GetLastErrorString():        ERR_PRECONDITION_FAILED
   GetBankBalance():            82534
   GetLoanAmount():             100000
   SetMinimumLoanAmount(31337): true
@@ -833,6 +835,8 @@
   GetCompanyHQ():              33151
   BuildCompanyHQ():            true
   GetCompanyHQ():              33153
+  BuildCompanyHQ():            false
+  GetLastErrorString():        ERR_AREA_NOT_CLEAR
 
 --Engine--
   Engine -1
--- a/src/ai/api/ai_company.cpp	Thu Apr 03 22:32:28 2008 +0000
+++ b/src/ai/api/ai_company.cpp	Thu Apr 03 23:01:54 2008 +0000
@@ -3,6 +3,7 @@
 /** @file ai_company.cpp Implementation of AICompany. */
 
 #include "ai_company.hpp"
+#include "ai_error.hpp"
 #include "../../command_func.h"
 #include "../../player_func.h"
 #include "../../player_base.h"
@@ -11,6 +12,7 @@
 #include "../../tile_map.h"
 #include "../../variables.h"
 #include "../../core/alloc_func.hpp"
+#include "../../string_func.h"
 #include "table/strings.h"
 
 /* static */ AICompany::CompanyIndex AICompany::ResolveCompanyIndex(AICompany::CompanyIndex company)
@@ -22,7 +24,10 @@
 
 /* static */ bool AICompany::SetCompanyName(const char *name)
 {
-	if (name == NULL) return false;
+	if (::StrEmpty(name)) {
+		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
+		return false;
+	}
 
 	_cmd_text = name;
 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_COMPANY_NAME);
@@ -43,7 +48,10 @@
 
 /* static */ bool AICompany::SetPresidentName(const char *name)
 {
-	if (name == NULL) return false;
+	if (::StrEmpty(name)) {
+		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
+		return false;
+	}
 
 	_cmd_text = name;
 	return AIObject::DoCommand(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME);
@@ -102,6 +110,7 @@
 			(loan % GetLoanInterval()) != 0 ||
 			loan > GetMaxLoanAmount() ||
 			(loan - GetLoanAmount() + GetBankBalance(MY_COMPANY)) < 0) {
+		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
 		return false;
 	}
 
@@ -114,12 +123,18 @@
 
 /* static */ bool AICompany::SetMinimumLoanAmount(int32 loan)
 {
-	if (loan < 0) return false;
+	if (loan < 0) {
+		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
+		return false;
+	}
 
 	int32 over_interval = loan % GetLoanInterval();
 	if (over_interval != 0) loan += GetLoanInterval() - over_interval;
 
-	if (loan > GetMaxLoanAmount()) return false;
+	if (loan > GetMaxLoanAmount()) {
+		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
+		return false;
+	}
 
 	SetLoanAmount(loan);
 
@@ -128,7 +143,10 @@
 
 /* static */ bool AICompany::BuildCompanyHQ(TileIndex tile)
 {
-	if (!::IsValidTile(tile)) return false;
+	if (!::IsValidTile(tile)) {
+		AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
+		return false;
+	}
 
 	return AIObject::DoCommand(tile, 0, 0, CMD_BUILD_COMPANY_HQ);
 }
--- a/src/ai/api/ai_company.hpp	Thu Apr 03 22:32:28 2008 +0000
+++ b/src/ai/api/ai_company.hpp	Thu Apr 03 23:01:54 2008 +0000
@@ -35,6 +35,8 @@
 	/**
 	 * Set the name of your company.
 	 * @param name The new name of the company.
+	 * @pre 'name' must have at least one character.
+	 * @exception AIError::ERR_NAME_IS_NOT_UNIQUE
 	 * @return True if the name was changed.
 	 */
 	static bool SetCompanyName(const char *name);
@@ -50,6 +52,8 @@
 	/**
 	 * Set the name of your president.
 	 * @param name The new name of the president.
+	 * @pre 'name' must have at least one character.
+	 * @exception AIError::ERR_NAME_IS_NOT_UNIQUE
 	 * @return True if the name was changed.
 	 */
 	static bool SetPresidentName(const char *name);
@@ -125,6 +129,7 @@
 	 * Build your company's HQ on the given tile.
 	 * @param tile The tile to build your HQ on, this tile is the most nothern tile of your HQ.
 	 * @pre AIMap::IsValidTile(tile).
+	 * @exception AIError::ERR_AREA_NOT_CLEAR
 	 * @return True if the HQ could be build.
 	 * @note An HQ can not be removed, only by water or rebuilding; If an HQ is
 	 *  build again, the old one is removed.
--- a/src/ai/api/ai_error.hpp	Thu Apr 03 22:32:28 2008 +0000
+++ b/src/ai/api/ai_error.hpp	Thu Apr 03 23:01:54 2008 +0000
@@ -54,10 +54,16 @@
 		ERR_ALREADY_BUILT,                            // [STR_1007_ALREADY_BUILT]
 
 		/** Area isn't clear, try to demolish the building on it */
-		ERR_AREA_NOT_CLEAR,                           // [STR_2004_BUILDING_MUST_BE_DEMOLISHED]
+		ERR_AREA_NOT_CLEAR,                           // [STR_2004_BUILDING_MUST_BE_DEMOLISHED, STR_5007_MUST_DEMOLISH_BRIDGE_FIRST, STR_300B_MUST_DEMOLISH_RAILROAD, STR_300E_MUST_DEMOLISH_AIRPORT_FIRST, STR_MUST_DEMOLISH_CARGO_TRAM_STATION, STR_3047_MUST_DEMOLISH_TRUCK_STATION, STR_MUST_DEMOLISH_PASSENGER_TRAM_STATION, STR_3046_MUST_DEMOLISH_BUS_STATION, STR_306A_BUOY_IN_THE_WAY, STR_304D_MUST_DEMOLISH_DOCK_FIRST, STR_4800_IN_THE_WAY, STR_5804_COMPANY_HEADQUARTERS_IN, STR_5800_OBJECT_IN_THE_WAY, STR_1801_MUST_REMOVE_ROAD_FIRST, STR_1008_MUST_REMOVE_RAILROAD_TRACK, STR_5007_MUST_DEMOLISH_BRIDGE_FIRST, STR_5006_MUST_DEMOLISH_TUNNEL_FIRST]
 
 		/** Area is owned by another company */
 		ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY,         // [STR_1024_AREA_IS_OWNED_BY_ANOTHER]
+
+		/** The name given is not unique for the object type */
+		ERR_NAME_IS_NOT_UNIQUE,                       // [STR_NAME_MUST_BE_UNIQUE]
+
+		/** The building you want to build requires flat land */
+		ERR_FLAT_LAND_REQUIRED,                       // [STR_0007_FLAT_LAND_REQUIRED]
 	};
 
 	/**
--- a/src/ai/api/ai_error.hpp.sq	Thu Apr 03 22:32:28 2008 +0000
+++ b/src/ai/api/ai_error.hpp.sq	Thu Apr 03 23:01:54 2008 +0000
@@ -33,12 +33,32 @@
 	SQAIError.DefSQConst(engine, AIError::ERR_ALREADY_BUILT,                    "ERR_ALREADY_BUILT");
 	SQAIError.DefSQConst(engine, AIError::ERR_AREA_NOT_CLEAR,                   "ERR_AREA_NOT_CLEAR");
 	SQAIError.DefSQConst(engine, AIError::ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY, "ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY");
+	SQAIError.DefSQConst(engine, AIError::ERR_NAME_IS_NOT_UNIQUE,               "ERR_NAME_IS_NOT_UNIQUE");
+	SQAIError.DefSQConst(engine, AIError::ERR_FLAT_LAND_REQUIRED,               "ERR_FLAT_LAND_REQUIRED");
 
-	AIError::RegisterErrorMap(STR_0003_NOT_ENOUGH_CASH_REQUIRES,    AIError::ERR_NOT_ENOUGH_CASH);
-	AIError::RegisterErrorMap(STR_2009_LOCAL_AUTHORITY_REFUSES,     AIError::ERR_LOCAL_AUTHORITY_REFUSES);
-	AIError::RegisterErrorMap(STR_1007_ALREADY_BUILT,               AIError::ERR_ALREADY_BUILT);
-	AIError::RegisterErrorMap(STR_2004_BUILDING_MUST_BE_DEMOLISHED, AIError::ERR_AREA_NOT_CLEAR);
-	AIError::RegisterErrorMap(STR_1024_AREA_IS_OWNED_BY_ANOTHER,    AIError::ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY);
+	AIError::RegisterErrorMap(STR_0003_NOT_ENOUGH_CASH_REQUIRES,        AIError::ERR_NOT_ENOUGH_CASH);
+	AIError::RegisterErrorMap(STR_2009_LOCAL_AUTHORITY_REFUSES,         AIError::ERR_LOCAL_AUTHORITY_REFUSES);
+	AIError::RegisterErrorMap(STR_1007_ALREADY_BUILT,                   AIError::ERR_ALREADY_BUILT);
+	AIError::RegisterErrorMap(STR_2004_BUILDING_MUST_BE_DEMOLISHED,     AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST,      AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_300B_MUST_DEMOLISH_RAILROAD,          AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_300E_MUST_DEMOLISH_AIRPORT_FIRST,     AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_MUST_DEMOLISH_CARGO_TRAM_STATION,     AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_3047_MUST_DEMOLISH_TRUCK_STATION,     AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_MUST_DEMOLISH_PASSENGER_TRAM_STATION, AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_3046_MUST_DEMOLISH_BUS_STATION,       AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_306A_BUOY_IN_THE_WAY,                 AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_304D_MUST_DEMOLISH_DOCK_FIRST,        AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_4800_IN_THE_WAY,                      AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_5804_COMPANY_HEADQUARTERS_IN,         AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_5800_OBJECT_IN_THE_WAY,               AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_1801_MUST_REMOVE_ROAD_FIRST,          AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_1008_MUST_REMOVE_RAILROAD_TRACK,      AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST,      AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST,      AIError::ERR_AREA_NOT_CLEAR);
+	AIError::RegisterErrorMap(STR_1024_AREA_IS_OWNED_BY_ANOTHER,        AIError::ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY);
+	AIError::RegisterErrorMap(STR_NAME_MUST_BE_UNIQUE,                  AIError::ERR_NAME_IS_NOT_UNIQUE);
+	AIError::RegisterErrorMap(STR_0007_FLAT_LAND_REQUIRED,              AIError::ERR_FLAT_LAND_REQUIRED);
 
 	AIError::RegisterErrorMapString(AIError::ERR_NONE,                             "ERR_NONE");
 	AIError::RegisterErrorMapString(AIError::ERR_UNKNOWN,                          "ERR_UNKNOWN");
@@ -48,6 +68,8 @@
 	AIError::RegisterErrorMapString(AIError::ERR_ALREADY_BUILT,                    "ERR_ALREADY_BUILT");
 	AIError::RegisterErrorMapString(AIError::ERR_AREA_NOT_CLEAR,                   "ERR_AREA_NOT_CLEAR");
 	AIError::RegisterErrorMapString(AIError::ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY, "ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY");
+	AIError::RegisterErrorMapString(AIError::ERR_NAME_IS_NOT_UNIQUE,               "ERR_NAME_IS_NOT_UNIQUE");
+	AIError::RegisterErrorMapString(AIError::ERR_FLAT_LAND_REQUIRED,               "ERR_FLAT_LAND_REQUIRED");
 
 	SQAIError.DefSQStaticMethod(engine, &AIError::GetClassName,           "GetClassName",           1, "x");
 	SQAIError.DefSQStaticMethod(engine, &AIError::GetErrorCategory,       "GetErrorCategory",       1, "x");