(svn r13553) [NoAI] -Add: function to determine the state of a vehicle (running, manually stopped, broken down, crashed etc.). noai
authorrubidium
Tue, 17 Jun 2008 21:35:32 +0000
branchnoai
changeset 10999 f238f608f45c
parent 10993 203b90795f80
child 11000 51305152af09
(svn r13553) [NoAI] -Add: function to determine the state of a vehicle (running, manually stopped, broken down, crashed etc.).
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	Tue Jun 17 13:06:32 2008 +0000
+++ b/src/ai/api/ai_vehicle.cpp	Tue Jun 17 21:35:32 2008 +0000
@@ -192,6 +192,21 @@
 	return ::GetVehicle(vehicle_id)->GetDisplaySpeed();
 }
 
+/* static */ AIVehicle::VehicleState AIVehicle::GetState(VehicleID vehicle_id)
+{
+	if (!IsValidVehicle(vehicle_id)) return AIVehicle::VS_INVALID;
+
+	const Vehicle *v = ::GetVehicle(vehicle_id);
+	byte vehstatus = v->vehstatus;
+
+	if (vehstatus & ::VS_CRASHED) return AIVehicle::VS_CRASHED;
+	if (vehstatus & ::VS_BROKEN) return AIVehicle::VS_BROKEN;
+	if (v->IsStoppedInDepot()) return AIVehicle::VS_IN_DEPOT;
+	if (vehstatus & ::VS_STOPPED) return AIVehicle::VS_STOPPED;
+	if (v->current_order.IsType(OT_LOADING)) return AIVehicle::VS_AT_STATION;
+	return AIVehicle::VS_RUNNING;
+}
+
 /* static */ Money AIVehicle::GetRunningCost(VehicleID vehicle_id)
 {
 	if (!IsValidVehicle(vehicle_id)) return -1;
--- a/src/ai/api/ai_vehicle.hpp	Tue Jun 17 13:06:32 2008 +0000
+++ b/src/ai/api/ai_vehicle.hpp	Tue Jun 17 21:35:32 2008 +0000
@@ -60,6 +60,7 @@
 		ERR_VEHCILE_NO_POWER,                   // [STR_TRAIN_START_NO_CATENARY]
 
 	};
+
 	/**
 	 * The type of a vehicle available in the game. Trams for example are
 	 *  road vehicles, as maglev is a rail vehicle.
@@ -74,6 +75,20 @@
 	};
 
 	/**
+	 * The different states a vehicle can be in.
+	 */
+	enum VehicleState {
+		VS_RUNNING         //!< The vehicle is currently running.
+		VS_STOPPED,        //!< The vehicle is stopped manually.
+		VS_IN_DEPOT,       //!< The vehicle is stopped in the depot.
+		VS_AT_STATION,     //!< The vehicle is stopped at a station and is currently loading or unloading.
+		VS_BROKEN,         //!< The vehicle has broken down and will start running again in a while.
+		VS_CRASHED,        //!< The vehicle is crashed (and will never run again).
+
+		VS_INVALID = 0xFF, //!< An invalid vehicle state.
+	};
+
+	/**
 	 * Checks whether the given vehicle is valid and owned by you.
 	 * @param vehicle_id The vehicle to check.
 	 * @return True if and only if the vehicle is valid.
@@ -160,6 +175,14 @@
 	static int32 GetCurrentSpeed(VehicleID vehicle_id);
 
 	/**
+	 * Get the current state of a vehicle.
+	 * @param vehicle_id The vehicle to get the state of.
+	 * @pre IsValidVehicle(vehicle_id).
+	 * @return The current state of the vehicle.
+	 */
+	static VehicleState GetState(VehicleID vehicle_id);
+
+	/**
 	 * Get the running cost of this vehicle.
 	 * @param vehicle_id The vehicle to get the age of.
 	 * @pre IsValidVehicle(vehicle_id).
--- a/src/ai/api/ai_vehicle.hpp.sq	Tue Jun 17 13:06:32 2008 +0000
+++ b/src/ai/api/ai_vehicle.hpp.sq	Tue Jun 17 21:35:32 2008 +0000
@@ -9,6 +9,8 @@
 	template <> int Return<AIVehicle::ErrorMessages>(HSQUIRRELVM vm, AIVehicle::ErrorMessages res) { sq_pushinteger(vm, (int32)res); return 1; }
 	template <> AIVehicle::VehicleType GetParam(ForceType<AIVehicle::VehicleType>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { 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; }
+	template <> AIVehicle::VehicleState GetParam(ForceType<AIVehicle::VehicleState>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIVehicle::VehicleState)tmp; }
+	template <> int Return<AIVehicle::VehicleState>(HSQUIRRELVM vm, AIVehicle::VehicleState 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, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AIVehicle *)instance; }
@@ -41,6 +43,13 @@
 	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_WATER,                    "VEHICLE_WATER");
 	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_AIR,                      "VEHICLE_AIR");
 	SQAIVehicle.DefSQConst(engine, AIVehicle::VEHICLE_INVALID,                  "VEHICLE_INVALID");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VS_RUNNING,                       "VS_RUNNING");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VS_STOPPED,                       "VS_STOPPED");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VS_IN_DEPOT,                      "VS_IN_DEPOT");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VS_AT_STATION,                    "VS_AT_STATION");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VS_BROKEN,                        "VS_BROKEN");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VS_CRASHED,                       "VS_CRASHED");
+	SQAIVehicle.DefSQConst(engine, AIVehicle::VS_INVALID,                       "VS_INVALID");
 
 	AIError::RegisterErrorMap(STR_00E1_TOO_MANY_VEHICLES_IN_GAME,     AIVehicle::ERR_VEHICLE_TOO_MANY);
 	AIError::RegisterErrorMap(STR_AIRCRAFT_NOT_AVAILABLE,             AIVehicle::ERR_VEHICLE_NOT_AVAILABLE);
@@ -99,6 +108,7 @@
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetMaxAge,          "GetMaxAge",          2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetAgeLeft,         "GetAgeLeft",         2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetCurrentSpeed,    "GetCurrentSpeed",    2, "xi");
+	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetState,           "GetState",           2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetRunningCost,     "GetRunningCost",     2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetProfitThisYear,  "GetProfitThisYear",  2, "xi");
 	SQAIVehicle.DefSQStaticMethod(engine, &AIVehicle::GetProfitLastYear,  "GetProfitLastYear",  2, "xi");