(svn r10940) [NoAI] -Add: added AIVehicle::GetName and AIVehicle::SetName to set vehicle names noai
authortruelight
Sun, 19 Aug 2007 13:43:59 +0000
branchnoai
changeset 9699 e1b5f29cc6f9
parent 9698 1d50fe99b7e9
child 9700 e442ce398e83
(svn r10940) [NoAI] -Add: added AIVehicle::GetName and AIVehicle::SetName to set vehicle names
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
src/ai/api/ai_vehicle.cpp
src/ai/api/ai_vehicle.hpp
src/ai/api/ai_vehicle.hpp.sq
--- a/bin/ai/regression/regression.nut	Sun Aug 19 13:31:04 2007 +0000
+++ b/bin/ai/regression/regression.nut	Sun Aug 19 13:43:59 2007 +0000
@@ -826,6 +826,9 @@
 	print("    GetCosts():         " + accounting.GetCosts());
 	print("    Should be:          " + (bank - bank_after));
 
+	print("  GetName():            " + vehicle.GetName(11));
+	print("  SetName():            " + vehicle.SetName(11, "MyVehicleName"));
+	print("  GetName():            " + vehicle.GetName(11));
 	print("  CloneVehicle():       " + vehicle.CloneVehicle(33417, 11, true));
 
 	print("  --VehicleData--");
--- a/bin/ai/regression/regression.txt	Sun Aug 19 13:31:04 2007 +0000
+++ b/bin/ai/regression/regression.txt	Sun Aug 19 13:43:59 2007 +0000
@@ -2496,6 +2496,9 @@
   --Accounting--
     GetCosts():         -5945
     Should be:          -5945
+  GetName():            Road Vehicle 1
+  SetName():            true
+  GetName():            MyVehicleName
   CloneVehicle():       12
   --VehicleData--
     GetLocation():       33417
@@ -2531,19 +2534,19 @@
     13 => 1
     11 => 1
   Age ListDump:
+    15 => 1
+    13 => 1
+    12 => 1
     11 => 1
-    15 => 0
-    13 => 0
-    12 => 0
   MaxAge ListDump:
     15 => 10980
     13 => 10980
     12 => 5490
     11 => 5490
   AgeLeft ListDump:
-    15 => 10980
-    13 => 10980
-    12 => 5490
+    15 => 10979
+    13 => 10979
+    12 => 5489
     11 => 5489
   ProfitThisYear ListDump:
     15 => 0
--- a/src/ai/api/ai_vehicle.cpp	Sun Aug 19 13:31:04 2007 +0000
+++ b/src/ai/api/ai_vehicle.cpp	Sun Aug 19 13:43:59 2007 +0000
@@ -10,6 +10,8 @@
 #include "../../engine.h"
 #include "../../player.h"
 #include "../../aircraft.h"
+#include "../../strings.h"
+#include "table/strings.h"
 
 /* static */ bool AIVehicle::IsValidEngine(EngineID engine_id)
 {
@@ -189,6 +191,15 @@
 	return this->DoCommand(0, vehicle_id, 0, CMD_SKIP_TO_ORDER);
 }
 
+bool AIVehicle::SetName(VehicleID vehicle_id, const char *name)
+{
+	if (!this->IsValidVehicle(vehicle_id)) return false;
+	if (name == NULL) return false;
+
+	_cmd_text = name;
+	return this->DoCommand(0, vehicle_id, 0, CMD_NAME_VEHICLE);
+}
+
 /* static */ TileIndex AIVehicle::GetLocation(VehicleID vehicle_id)
 {
 	if (!AIVehicle::IsValidVehicle(vehicle_id)) return INVALID_TILE;
@@ -210,6 +221,18 @@
 	return ::GetVehicle(vehicle_id)->unitnumber;
 }
 
+/* static */ char *AIVehicle::GetName(VehicleID vehicle_id)
+{
+	if (!AIVehicle::IsValidVehicle(vehicle_id)) return NULL;
+
+	static const int len = 64;
+	char *vehicle_name = MallocT<char>(len);
+
+	SetDParam(0, ::GetVehicle(vehicle_id)->unitnumber);
+	GetString(vehicle_name, ::GetVehicle(vehicle_id)->string_id, &vehicle_name[len - 1]);
+	return vehicle_name;
+}
+
 /* static */ int32 AIVehicle::GetAge(VehicleID vehicle_id)
 {
 	if (!AIVehicle::IsValidVehicle(vehicle_id)) return -1;
--- a/src/ai/api/ai_vehicle.hpp	Sun Aug 19 13:31:04 2007 +0000
+++ b/src/ai/api/ai_vehicle.hpp	Sun Aug 19 13:43:59 2007 +0000
@@ -138,7 +138,6 @@
 	 */
 	bool SellVehicle(VehicleID vehicle_id);
 
-
 	/**
 	 * Sends the given vehicle to a depot.
 	 * @param vehicle_id the vehicle to send to a depot.
@@ -164,6 +163,17 @@
 	bool SkipVehicleOrder(VehicleID vehicle_id);
 
 	/**
+	 * Set the name of a vehicle.
+	 * @param vehicle_id the vehicle to set the name for.
+	 * @param name the name for the vehicle.
+	 * @pre IsValidVehicle(vehicle_id).
+	 * @pre Name has to be unique.
+	 * @pre You have to own the vehicle.
+	 * @return true if and only if the name was changed.
+	 */
+	bool SetName(VehicleID vehicle_id, const char *name);
+
+	/**
 	 * Get the current location of a vehicle.
 	 * @param vehicle_id the vehicle to get the location of.
 	 * @pre IsValidVehicle(vehicle_id).
@@ -188,6 +198,14 @@
 	static int32 GetUnitNumber(VehicleID vehicle_id);
 
 	/**
+	 * Get the name of a vehicle.
+	 * @param vehicle_id the vehicle to get the unitnumber of.
+	 * @pre IsValidVehicle(vehicle_id).
+	 * @return the name the vehicle has.
+	 */
+	static char *GetName(VehicleID vehicle_id);
+
+	/**
 	 * Get the current age of a vehicle.
 	 * @note age is in days.
 	 * @param vehicle_id the vehicle to get the age of.
--- a/src/ai/api/ai_vehicle.hpp.sq	Sun Aug 19 13:31:04 2007 +0000
+++ b/src/ai/api/ai_vehicle.hpp.sq	Sun Aug 19 13:43:59 2007 +0000
@@ -30,6 +30,7 @@
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetLocation,       "GetLocation",       2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetEngineType,     "GetEngineType",     2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetUnitNumber,     "GetUnitNumber",     2, "xi");
+	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetName,           "GetName",           2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetAge,            "GetAge",            2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetMaxAge,         "GetMaxAge",         2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetAgeLeft,        "GetAgeLeft",        2, "xi");
@@ -47,6 +48,7 @@
 	SQAIVehicle.DefSQMethod(engine, &AIVehicle::SendVehicleToDepot,   "SendVehicleToDepot",   2, "xi");
 	SQAIVehicle.DefSQMethod(engine, &AIVehicle::StartStopVehicle,     "StartStopVehicle",     2, "xi");
 	SQAIVehicle.DefSQMethod(engine, &AIVehicle::SkipVehicleOrder,     "SkipVehicleOrder",     2, "xi");
+	SQAIVehicle.DefSQMethod(engine, &AIVehicle::SetName,              "SetName",              3, "xis");
 
 	SQAIVehicle.PostRegister(engine);
 }