summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSamuXarick <43006711+SamuXarick@users.noreply.github.com>2021-08-12 20:29:03 +0100
committerGitHub <noreply@github.com>2021-08-12 21:29:03 +0200
commit120d216b0b7e3efe01cbc180e96e1ab11257c304 (patch)
treecc0daa78def41388e79a44cd6997f4d076ff865c /src
parent26f7f592cd0d93ec79aa2bbfd0a435c4465b95cc (diff)
downloadopenttd-120d216b0b7e3efe01cbc180e96e1ab11257c304.tar.xz
Add: [AI] Get the number of vehicles in a given group (#9462)
Diffstat (limited to 'src')
-rw-r--r--src/script/api/ai_changelog.hpp1
-rw-r--r--src/script/api/script_group.cpp9
-rw-r--r--src/script/api/script_group.hpp14
3 files changed, 24 insertions, 0 deletions
diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp
index 15e171027..6e3283820 100644
--- a/src/script/api/ai_changelog.hpp
+++ b/src/script/api/ai_changelog.hpp
@@ -20,6 +20,7 @@
* API additions:
* \li AINewGRF
* \li AINewGRFList
+ * \li AIGroup::GetNumVehicles
*
* \b 1.11.0
*
diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp
index 0295c67aa..a6e2fdee2 100644
--- a/src/script/api/script_group.cpp
+++ b/src/script/api/script_group.cpp
@@ -106,6 +106,15 @@
return GetGroupNumEngines(ScriptObject::GetCompany(), group_id, engine_id);
}
+/* static */ int32 ScriptGroup::GetNumVehicles(GroupID group_id, ScriptVehicle::VehicleType vehicle_type)
+{
+ bool valid_group = IsValidGroup(group_id);
+ if (!valid_group && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
+ if (!valid_group && (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR)) return -1;
+
+ return GetGroupNumVehicle(ScriptObject::GetCompany(), group_id, valid_group ? ::Group::Get(group_id)->vehicle_type : (::VehicleType)vehicle_type);
+}
+
/* static */ bool ScriptGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id)
{
EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT);
diff --git a/src/script/api/script_group.hpp b/src/script/api/script_group.hpp
index 2c27b81c6..384fea7a2 100644
--- a/src/script/api/script_group.hpp
+++ b/src/script/api/script_group.hpp
@@ -129,6 +129,20 @@ public:
static int32 GetNumEngines(GroupID group_id, EngineID engine_id);
/**
+ * Get the total number of vehicles in a given group and its sub-groups.
+ * @param group_id The group to get the number of vehicles in.
+ * @param vehicle_type The type of vehicle of the group.
+ * @pre IsValidGroup(group_id) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
+ * @pre IsValidGroup(group_id) || vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL ||
+ * vehicle_type == ScriptVehicle::VT_WATER || vehicle_type == ScriptVehicle::VT_AIR
+ * @return The total number of vehicles in the group with id group_id and it's sub-groups.
+ * @note If the group is valid (neither GROUP_ALL nor GROUP_DEFAULT), the value of
+ * vehicle_type is retrieved from the group itself and not from the input value.
+ * But if the group is GROUP_ALL or GROUP_DEFAULT, then vehicle_type must be valid.
+ */
+ static int32 GetNumVehicles(GroupID group_id, ScriptVehicle::VehicleType vehicle_type);
+
+ /**
* Move a vehicle to a group.
* @param group_id The group to move the vehicle to.
* @param vehicle_id The vehicle to move to the group.