From 5d3ccae6c5bd3f328ad0d8e68ae7c69451b9ca48 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 13 Feb 2019 22:48:46 +0000 Subject: Add: AI function to get current usage of a group. --- src/script/api/ai/ai_group.hpp.sq | 1 + src/script/api/script_group.cpp | 21 +++++++++++++++++++++ src/script/api/script_group.hpp | 8 ++++++++ 3 files changed, 30 insertions(+) (limited to 'src/script') diff --git a/src/script/api/ai/ai_group.hpp.sq b/src/script/api/ai/ai_group.hpp.sq index d721ddfbf..cae6cb3ac 100644 --- a/src/script/api/ai/ai_group.hpp.sq +++ b/src/script/api/ai/ai_group.hpp.sq @@ -44,6 +44,7 @@ void SQAIGroup_Register(Squirrel *engine) SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::StopAutoReplace, "StopAutoReplace", 3, ".ii"); SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetProfitThisYear, "GetProfitThisYear", 2, ".i"); SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetProfitLastYear, "GetProfitLastYear", 2, ".i"); + SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetCurrentUsage, "GetCurrentUsage", 2, ".i"); SQAIGroup.PostRegister(engine); } diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp index 023f5534c..0e89c3c60 100644 --- a/src/script/api/script_group.cpp +++ b/src/script/api/script_group.cpp @@ -173,3 +173,24 @@ return ::Group::Get(group_id)->statistics.profit_last_year; } + +/* static */ uint32 ScriptGroup::GetCurrentUsage(GroupID group_id) +{ + if (!IsValidGroup(group_id)) return -1; + + uint32 occupancy = 0; + uint32 vehicle_count = 0; + + const Vehicle *v; + FOR_ALL_VEHICLES(v) { + if (v->group_id != group_id) continue; + if (!v->IsPrimaryVehicle()) continue; + + occupancy += v->trip_occupancy; + vehicle_count++; + } + + if (vehicle_count == 0) return -1; + + return occupancy / vehicle_count; +} diff --git a/src/script/api/script_group.hpp b/src/script/api/script_group.hpp index c5f41cdb6..a41196718 100644 --- a/src/script/api/script_group.hpp +++ b/src/script/api/script_group.hpp @@ -205,6 +205,14 @@ public: * @return The current profit the group had last year. */ static Money GetProfitLastYear(GroupID group_id); + + /** + * Get the current vehicle usage of a group. + * @param group_id The group to get the current usage of. + * @pre IsValidGroup(group_id). + * @return The current usage of the group. + */ + static uint32 GetCurrentUsage(GroupID group_id); }; #endif /* SCRIPT_GROUP_HPP */ -- cgit v1.2.3-70-g09d2