From d0e138b4c792cce3a9d0a7267922986bab16ed22 Mon Sep 17 00:00:00 2001 From: Grey-Echo Date: Sat, 22 Apr 2017 14:35:51 +0200 Subject: [PATCH] Implement GROUP:CountInZone() and SET_GROUP:CountInZone() --- Moose Development/Moose/Core/Set.lua | 46 +++++++++++++++++++++++ Moose Development/Moose/Wrapper/Group.lua | 18 +++++++++ 2 files changed, 64 insertions(+) diff --git a/Moose Development/Moose/Core/Set.lua b/Moose Development/Moose/Core/Set.lua index c81b6ef66..893b28471 100644 --- a/Moose Development/Moose/Core/Set.lua +++ b/Moose Development/Moose/Core/Set.lua @@ -1117,6 +1117,52 @@ function SET_GROUP:NoneInZone(Zone) return true end +--- Iterate the SET_GROUP and count how many GROUPs are completely in the Zone +-- That could easily be done with SET_GROUP:ForEachGroupCompletelyInZone(), but this function +-- provides an easy to use shortcut... +-- @param #SET_GROUP self +-- @param Core.Zone#ZONE ZoneObject The Zone to be tested for. +-- @return #number the number of GROUPs completely in the Zone +-- @usage +-- local MyZone = ZONE:New("Zone1") +-- local MySetGroup = SET_GROUP:New() +-- MySetGroup:AddGroupsByName({"Group1", "Group2"}) +-- +-- MESSAGE:New("There are " .. MySetGroup:CountInZone(MyZone) .. " GROUPs in the Zone !", 10):ToAll() +function SET_GROUP:CountInZone(Zone) + self:F2(Zone) + local Count = 0 + local Set = self:GetSet() + for GroupID, GroupData in pairs(Set) do -- For each GROUP in SET_GROUP + if GroupData:IsCompletelyInZone(Zone) then + Count = Count + 1 + end + end + return Count +end + +--- Iterate the SET_GROUP and count how many UNITs are completely in the Zone +-- @param #SET_GROUP self +-- @param Core.Zone#ZONE ZoneObject The Zone to be tested for. +-- @return #number the number of GROUPs completely in the Zone +-- @usage +-- local MyZone = ZONE:New("Zone1") +-- local MySetGroup = SET_GROUP:New() +-- MySetGroup:AddGroupsByName({"Group1", "Group2"}) +-- +-- MESSAGE:New("There are " .. MySetGroup:CountUnitInZone(MyZone) .. " UNITs in the Zone !", 10):ToAll() +function SET_GROUP:CountUnitInZone(Zone) + self:F2(Zone) + local Count = 0 + local Set = self:GetSet() + for GroupID, GroupData in pairs(Set) do -- For each GROUP in SET_GROUP + if GroupData:IsCompletelyInZone(Zone) then + Count = Count + 1 + end + end + return Count +end + ----- Iterate the SET_GROUP and call an interator function for each **alive** player, providing the Group of the player and optional parameters. ---- @param #SET_GROUP self ---- @param #function IteratorFunction The function that will be called when there is an alive player in the SET_GROUP. The function needs to accept a GROUP parameter. diff --git a/Moose Development/Moose/Wrapper/Group.lua b/Moose Development/Moose/Wrapper/Group.lua index 3e9e6b5f9..8dae1d5b8 100644 --- a/Moose Development/Moose/Wrapper/Group.lua +++ b/Moose Development/Moose/Wrapper/Group.lua @@ -591,6 +591,24 @@ function GROUP:IsNotInZone( Zone ) return true end +--- Returns the number of UNITs that are in the @{Zone} +-- @param #GROUP self +-- @param Core.Zone#ZONE_BASE Zone The zone to test. +-- @return #number The number of UNITs that are in the @{Zone} +function GROUP:CountInZone( Zone ) + self:F2( {slef.GroupName, Zone} ) + local Count = 0 + + for UnitID, UnitData in pairs( self:GetUnits() ) do + local Unit = UnitData -- Wrapper.Unit#UNIT + if Zone:IsVec3InZone( Unit:GetVec3() ) then + Count = Count + 1 + end + end + + return Count +end + --- Returns if the group is of an air category. -- If the group is a helicopter or a plane, then this method will return true, otherwise false. -- @param #GROUP self