(svn r10633) [NoAI] -Add: added GetVehicleType and AIVehicle::VehicleType noai
authortruelight
Thu, 19 Jul 2007 23:01:41 +0000
branchnoai
changeset 9684 623970482fb2
parent 9683 a4683b021073
child 9685 d988aad9fc52
(svn r10633) [NoAI] -Add: added GetVehicleType and AIVehicle::VehicleType
[NoAI] -Codechange: use 'vehicle', not 'town' in AIVehicleListValuator ;)
[NoAI] -Fix: don't output pointers in regression as they change on systems and runs
bin/ai/regression/regression.nut
bin/ai/regression/regression.txt
src/ai/ai_squirrel.cpp
src/ai/api/ai_vehicle.cpp
src/ai/api/ai_vehicle.hpp
src/ai/api/ai_vehicle.hpp.sq
src/ai/api/ai_vehiclelist_valuator.cpp
src/ai/api/ai_vehiclelist_valuator.hpp
src/ai/api/ai_vehiclelist_valuator.hpp.sq
--- a/bin/ai/regression/regression.nut	Thu Jul 19 22:41:38 2007 +0000
+++ b/bin/ai/regression/regression.nut	Thu Jul 19 23:01:41 2007 +0000
@@ -155,37 +155,39 @@
 	print("--Event--");
 	AIEventController.Test();
 	local e = AIEventController.GetNextEvent();
-	print("  GetNextEvent:      " + e);
+	print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
 	print("    GetEventType:    " + e.GetEventType());
 	local c = AIEventTest.Convert(e);
-	print("    Convert:         " + c);
+	print("    Convert:         " + (c == null ? "null" : "instance"));
 	print("      GetTest:       " + c.GetTest());
 
 	print("  DisableEvent(1):   done");
 	AIEventController.DisableEvent(1);
 	AIEventController.Test();
 	e = AIEventController.GetNextEvent();
-	print("  GetNextEvent:      " + e);
+	print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
 
 	print("  EnableEvent(1):    done");
 	AIEventController.EnableEvent(1);
 	AIEventController.Test();
 	e = AIEventController.GetNextEvent();
-	print("  GetNextEvent:      " + e);
+	print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
 
 	{
 		print("  DisableAllEvents():done");
 		AIEventController.DisableAllEvents();
 		AIEventController.Test();
 		e = AIEventController.GetNextEvent();
-		print("  GetNextEvent:      " + e);
+		print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
 	}
 
 	print("  EnableEvent(1):    done");
 	AIEventController.EnableEvent(1);
 	AIEventController.Test();
 	e = AIEventController.GetNextEvent();
-	print("  GetNextEvent:      " + e);
+	print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
+	e = AIEventController.GetNextEvent();
+	print("  GetNextEvent:      " + (e == null ? "null" : "instance"));
 }
 
 function Regression::Industry()
@@ -775,6 +777,7 @@
 	print("    GetAgeLeft():        " + vehicle.GetAgeLeft(1024));
 	print("    GetProfitThisYear(): " + vehicle.GetProfitThisYear(1024));
 	print("    GetProfitLastYear(): " + vehicle.GetProfitLastYear(1024));
+	print("    GetVehicleType():    " + vehicle.GetVehicleType(1024));
 
 	print("  BuildVehicle():       " + vehicle.BuildVehicle(32119, 219));
 	print("  IsValidVehicle(1026): " + vehicle.IsValidVehicle(1026));
@@ -824,6 +827,11 @@
 	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
 		print("    " + i + " => " + list.GetValue(i));
 	}
+	list.Valuate(AIVehicleListVehicleType());
+	print("  VehicleType ListDump:");
+	for (local i = list.Begin(); list.HasNext(); i = list.Next()) {
+		print("    " + i + " => " + list.GetValue(i));
+	}
 }
 
 
--- a/bin/ai/regression/regression.txt	Thu Jul 19 22:41:38 2007 +0000
+++ b/bin/ai/regression/regression.txt	Thu Jul 19 23:01:41 2007 +0000
@@ -247,18 +247,19 @@
   GetLoanAmount():             300000
 
 --Event--
-  GetNextEvent:      (instance : 0x0xb431e0)
+  GetNextEvent:      instance
     GetEventType:    1
-    Convert:         (instance : 0x0xb41280)
+    Convert:         instance
       GetTest:       42
   DisableEvent(1):   done
-  GetNextEvent:      (null : 0x00000000)
+  GetNextEvent:      null
   EnableEvent(1):    done
-  GetNextEvent:      (instance : 0x0xb431e0)
+  GetNextEvent:      instance
   DisableAllEvents():done
-  GetNextEvent:      (null : 0x00000000)
+  GetNextEvent:      null
   EnableEvent(1):    done
-  GetNextEvent:      (instance : 0x0xb431e0)
+  GetNextEvent:      instance
+  GetNextEvent:      null
 
 --Industry--
   GetMaxIndustryID():  71
@@ -2358,6 +2359,7 @@
     GetAgeLeft():        5490
     GetProfitThisYear(): 0
     GetProfitLastYear(): 0
+    GetVehicleType():    1
   BuildVehicle():       1026
   IsValidVehicle(1026): true
 
@@ -2395,6 +2397,10 @@
     1026 => 0
     1025 => 0
     1024 => 0
+  VehicleType ListDump:
+    1026 => 3
+    1025 => 1
+    1024 => 1
 
 --Order--
   GetNumberOfOrders():   0
--- a/src/ai/ai_squirrel.cpp	Thu Jul 19 22:41:38 2007 +0000
+++ b/src/ai/ai_squirrel.cpp	Thu Jul 19 23:01:41 2007 +0000
@@ -272,6 +272,7 @@
 	SQAIVehicleListProfitThisYearRegister(this->engine);
 	SQAIVehicleListRegister(this->engine);
 	SQAIVehicleListUnitNumberRegister(this->engine);
+	SQAIVehicleListVehicleTypeRegister(this->engine);
 	SQAIVehicleRegister(this->engine);
 	SQAIVehicleStationListRegister(this->engine);
 
--- a/src/ai/api/ai_vehicle.cpp	Thu Jul 19 22:41:38 2007 +0000
+++ b/src/ai/api/ai_vehicle.cpp	Thu Jul 19 23:01:41 2007 +0000
@@ -21,7 +21,7 @@
 	return ::IsValidVehicleID(vehicle_id) && ::GetVehicle(vehicle_id)->owner == _current_player;
 }
 
-EngineID AIVehicle::FindBestVehicle(CargoID cargo, uint8 min_reliability, VehicleType veh_type, uint max_cost)
+EngineID AIVehicle::FindBestVehicle(CargoID cargo, uint8 min_reliability, AIVehicle::VehicleType veh_type, uint max_cost)
 {
 	if (!AICargo::IsValidCargo(cargo) || min_reliability > 100) return INVALID_ENGINE;
 
@@ -37,7 +37,7 @@
 			uint cspeed, ccargo;
 			/* Can this vehicle handle the cargo requested? */
 			switch (veh_type) {
-				case VEH_ROAD: {
+				case VEHICLE_ROAD: {
 					const RoadVehicleInfo *vi = RoadVehInfo(engine_id);
 					if (vi->cargo_type != cargo && !CanRefitTo(engine_id, cargo)) continue;
 					if ((_price.roadveh_base >> 3) * vi->base_cost >> 5 > max_cost) continue;
@@ -45,7 +45,7 @@
 					cspeed = vi->max_speed;
 				} break;
 
-				case VEH_TRAIN: {
+				case VEHICLE_RAIL: {
 					const RailVehicleInfo *vi = RailVehInfo(engine_id);
 					if (vi->cargo_type != cargo && !CanRefitTo(engine_id, cargo)) continue;
 					if ((_price.build_railvehicle >> 3) * vi->base_cost >> 5 > max_cost) continue;
@@ -53,7 +53,7 @@
 					cspeed = vi->max_speed;
 				} break;
 
-				case VEH_SHIP: {
+				case VEHICLE_WATER: {
 					const ShipVehicleInfo *vi = ShipVehInfo(engine_id);
 					if (vi->cargo_type != cargo && !CanRefitTo(engine_id, cargo)) continue;
 					if ((_price.ship_base >> 3) * vi->base_cost >> 5 > max_cost) continue;
@@ -61,7 +61,7 @@
 					cspeed = vi->max_speed;
 				} break;
 
-				case VEH_AIRCRAFT: {
+				case VEHICLE_AIR: {
 					const AircraftVehicleInfo *vi = AircraftVehInfo(engine_id);
 					if (CT_PASSENGERS != cargo && CT_MAIL != cargo && !CanRefitTo(engine_id, cargo)) continue;
 					if ((_price.aircraft_base >> 3) * vi->base_cost >> 5 > max_cost) continue;
@@ -86,12 +86,12 @@
 
 EngineID AIVehicle::FindBestRoadVehicle(CargoID cargo, uint8 min_reliability, uint max_cost)
 {
-	return this->FindBestVehicle(cargo, min_reliability, VEH_ROAD, max_cost);
+	return this->FindBestVehicle(cargo, min_reliability, VEHICLE_ROAD, max_cost);
 }
 
 EngineID AIVehicle::FindBestAircraftVehicle(CargoID cargo, uint8 min_reliability, uint max_cost)
 {
-	return this->FindBestVehicle(cargo, min_reliability, VEH_AIRCRAFT, max_cost);
+	return this->FindBestVehicle(cargo, min_reliability, VEHICLE_AIR, max_cost);
 }
 
 VehicleID AIVehicle::BuildVehicle(TileIndex depot, EngineID engine_id)
@@ -239,3 +239,16 @@
 
 	return ::GetVehicle(vehicle_id)->profit_last_year;
 }
+
+/* static */ AIVehicle::VehicleType AIVehicle::GetVehicleType(VehicleID vehicle_id)
+{
+	if (!AIVehicle::IsValidVehicle(vehicle_id)) return VEHICLE_INVALID;
+
+	switch (GetVehicle(vehicle_id)->type) {
+		case VEH_ROAD:     return VEHICLE_ROAD;
+		case VEH_TRAIN:    return VEHICLE_RAIL;
+		case VEH_SHIP:     return VEHICLE_WATER;
+		case VEH_AIRCRAFT: return VEHICLE_AIR;
+		default:           return VEHICLE_INVALID;
+	}
+}
--- a/src/ai/api/ai_vehicle.hpp	Thu Jul 19 22:41:38 2007 +0000
+++ b/src/ai/api/ai_vehicle.hpp	Thu Jul 19 23:01:41 2007 +0000
@@ -13,6 +13,15 @@
  */
 class AIVehicle : public AIObject {
 public:
+	enum VehicleType {
+		/* Order IS important, as it now matches the internal state of the game for vehicle type */
+		VEHICLE_RAIL,
+		VEHICLE_ROAD,
+		VEHICLE_WATER,
+		VEHICLE_AIR,
+		VEHICLE_INVALID = 0xFF,
+	};
+
 	/**
 	 * The name of the class, needed by several sub-processes.
 	 */
@@ -203,8 +212,16 @@
 	 */
 	static int32 GetProfitLastYear(VehicleID vehicle_id);
 
+	/**
+	 * Get the type of vehicle.
+	 * @param vehicle_id the vehicle to get the type of.
+	 * @pre IsValidVehicle(vehicle_id).
+	 * @return the vehicle type.
+	 */
+	static AIVehicle::VehicleType GetVehicleType(VehicleID vehicle_id);
+
 private:
-	EngineID FindBestVehicle(CargoID cargo, uint8 min_reliability, VehicleType veh_type, uint max_cost);
+	EngineID FindBestVehicle(CargoID cargo, uint8 min_reliability, AIVehicle::VehicleType veh_type, uint max_cost);
 };
 
 #endif /* AI_VEHICLE_HPP */
--- a/src/ai/api/ai_vehicle.hpp.sq	Thu Jul 19 22:41:38 2007 +0000
+++ b/src/ai/api/ai_vehicle.hpp.sq	Thu Jul 19 23:01:41 2007 +0000
@@ -1,6 +1,10 @@
 #include "ai_vehicle.hpp"
 
 namespace SQConvert {
+	/* Allow enums to be used as Squirrel parameters */
+	template <> AIVehicle::VehicleType GetParam(ForceType<AIVehicle::VehicleType>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIVehicle::VehicleType)tmp; }
+	template <> int Return<AIVehicle::VehicleType>(HSQUIRRELVM vm, AIVehicle::VehicleType res) { sq_pushinteger(vm, (int32)res); return 1; }
+
 	/* Allow AIVehicle to be used as Squirrel parameter */
 	template <> AIVehicle *GetParam(ForceType<AIVehicle *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicle *)instance; }
 	template <> AIVehicle &GetParam(ForceType<AIVehicle &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicle *)instance; }
@@ -14,6 +18,12 @@
 	SQAIVehicle.PreRegister(engine);
 	SQAIVehicle.AddConstructor<void (AIVehicle::*)(), 1>(engine, "x");
 
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_INVALID, "VEHICLE_INVALID");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_RAIL,    "VEHICLE_RAIL");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_ROAD,    "VEHICLE_ROAD");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_WATER,   "VEHICLE_WATER");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_AIR,     "VEHICLE_AIR");
+
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetClassName,      "GetClassName",      1, "x");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::IsValidEngine,     "IsValidEngine",     2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::IsValidVehicle,    "IsValidVehicle",    2, "xi");
@@ -25,6 +35,7 @@
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetAgeLeft,        "GetAgeLeft",        2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetProfitThisYear, "GetProfitThisYear", 2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetProfitLastYear, "GetProfitLastYear", 2, "xi");
+	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetVehicleType,    "GetVehicleType",    2, "xi");
 
 	SQAIVehicle.DefSQMethod(engine, &AIVehicle::FindBestRoadVehicle,     "FindBestRoadVehicle",     4, "xiii");
 	SQAIVehicle.DefSQMethod(engine, &AIVehicle::FindBestAircraftVehicle, "FindBestAircraftVehicle", 4, "xiii");
--- a/src/ai/api/ai_vehiclelist_valuator.cpp	Thu Jul 19 22:41:38 2007 +0000
+++ b/src/ai/api/ai_vehiclelist_valuator.cpp	Thu Jul 19 23:01:41 2007 +0000
@@ -40,3 +40,8 @@
 {
 	return AIVehicle::GetProfitLastYear(vehicle);
 }
+
+int32 AIVehicleListVehicleType::Valuate(int32 vehicle) const
+{
+	return AIVehicle::GetVehicleType(vehicle);
+}
--- a/src/ai/api/ai_vehiclelist_valuator.hpp	Thu Jul 19 22:41:38 2007 +0000
+++ b/src/ai/api/ai_vehiclelist_valuator.hpp	Thu Jul 19 23:01:41 2007 +0000
@@ -20,7 +20,7 @@
 	static const char *GetClassName() { return "AIVehicleListGetLocation"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 /**
@@ -36,7 +36,7 @@
 	static const char *GetClassName() { return "AIVehicleListEngineType"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 /**
@@ -52,7 +52,7 @@
 	static const char *GetClassName() { return "AIVehicleListUnitNumber"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 /**
@@ -68,7 +68,7 @@
 	static const char *GetClassName() { return "AIVehicleListAge"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 /**
@@ -84,7 +84,7 @@
 	static const char *GetClassName() { return "AIVehicleListMaxAge"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 /**
@@ -100,7 +100,7 @@
 	static const char *GetClassName() { return "AIVehicleListAgeLeft"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 /**
@@ -116,7 +116,7 @@
 	static const char *GetClassName() { return "AIVehicleListProfitThisYear"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 /**
@@ -132,7 +132,23 @@
 	static const char *GetClassName() { return "AIVehicleListProfitLastYear"; }
 
 private:
-	int32 Valuate(int32 town) const;
+	int32 Valuate(int32 vehicle_id) const;
+};
+
+/**
+ * Get the ype of the vehicle for entries in an AIVehicleList instance.
+ * @note resulting items are of the type AIVehicle::VehicleType
+ * @note the input items are of the type VehicleID
+ */
+class AIVehicleListVehicleType : public AIAbstractList::Valuator {
+public:
+	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIVehicleListVehicleType"; }
+
+private:
+	int32 Valuate(int32 vehicle_id) const;
 };
 
 
--- a/src/ai/api/ai_vehiclelist_valuator.hpp.sq	Thu Jul 19 22:41:38 2007 +0000
+++ b/src/ai/api/ai_vehiclelist_valuator.hpp.sq	Thu Jul 19 23:01:41 2007 +0000
@@ -151,3 +151,22 @@
 
 	SQAIVehicleListProfitLastYear.PostRegister(engine);
 }
+
+namespace SQConvert {
+	/* Allow AIVehicleListVehicleType to be used as Squirrel parameter */
+	template <> AIVehicleListVehicleType *GetParam(ForceType<AIVehicleListVehicleType *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleListVehicleType *)instance; }
+	template <> AIVehicleListVehicleType &GetParam(ForceType<AIVehicleListVehicleType &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleListVehicleType *)instance; }
+	template <> const AIVehicleListVehicleType *GetParam(ForceType<const AIVehicleListVehicleType *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicleListVehicleType *)instance; }
+	template <> const AIVehicleListVehicleType &GetParam(ForceType<const AIVehicleListVehicleType &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIVehicleListVehicleType *)instance; }
+	template <> int Return<AIVehicleListVehicleType *>(HSQUIRRELVM vm, AIVehicleListVehicleType *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "AIVehicleListVehicleType", res, NULL, DefSQDestructorCallback<AIVehicleListVehicleType>); return 1; }
+}; // namespace SQConvert
+
+void SQAIVehicleListVehicleTypeRegister(Squirrel *engine) {
+	DefSQClass <AIVehicleListVehicleType> SQAIVehicleListVehicleType("AIVehicleListVehicleType");
+	SQAIVehicleListVehicleType.PreRegister(engine);
+	SQAIVehicleListVehicleType.AddConstructor<void (AIVehicleListVehicleType::*)(), 1>(engine, "x");
+
+	SQAIVehicleListVehicleType.DefSQStaticMethod(engine, &AIVehicleListVehicleType::GetClassName, "GetClassName", 1, "x");
+
+	SQAIVehicleListVehicleType.PostRegister(engine);
+}