From 21a33b01ad7849697ad09bb3853b9e0f09baba1b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 2 May 2020 22:09:27 -0400 Subject: [PATCH] CTeamAiMgr: Collapse two loops into count_if Same behavior, less code. Ranges in C++20 will allow us to further simplify this in the future. --- Runtime/World/CTeamAiMgr.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Runtime/World/CTeamAiMgr.cpp b/Runtime/World/CTeamAiMgr.cpp index 23c9b33b6..ca76e990f 100644 --- a/Runtime/World/CTeamAiMgr.cpp +++ b/Runtime/World/CTeamAiMgr.cpp @@ -1,5 +1,7 @@ #include "Runtime/World/CTeamAiMgr.hpp" +#include + #include "Runtime/CStateManager.hpp" #include "Runtime/World/CPlayer.hpp" @@ -301,20 +303,15 @@ void CTeamAiMgr::ClearTeamAiRole(TUniqueId aiId) { } s32 CTeamAiMgr::GetNumAssignedOfRole(CTeamAiRole::ETeamAiRole testRole) const { - s32 ret = 0; - for (const auto& role : x58_roles) - if (role.GetTeamAiRole() == testRole) - ++ret; - return ret; + return static_cast(std::count_if(x58_roles.cbegin(), x58_roles.cend(), + [testRole](const auto& role) { return role.GetTeamAiRole() == testRole; })); } s32 CTeamAiMgr::GetNumAssignedAiRoles() const { - s32 ret = 0; - for (const auto& role : x58_roles) - if (role.GetTeamAiRole() > CTeamAiRole::ETeamAiRole::Initial && - role.GetTeamAiRole() <= CTeamAiRole::ETeamAiRole::Unassigned) - ++ret; - return ret; + return static_cast(std::count_if(x58_roles.cbegin(), x58_roles.cend(), [](const auto& role) { + const auto aiRole = role.GetTeamAiRole(); + return aiRole > CTeamAiRole::ETeamAiRole::Initial && aiRole <= CTeamAiRole::ETeamAiRole::Unassigned; + })); } CTeamAiRole* CTeamAiMgr::GetTeamAiRole(CStateManager& mgr, TUniqueId mgrId, TUniqueId aiId) {