From 0d86ef2b04854e2d2bfa50260d74a91d23d72b70 Mon Sep 17 00:00:00 2001 From: Zach Toogood Date: Tue, 21 Nov 2023 11:22:14 +0000 Subject: [PATCH] [core] Remove usage of "*std::find_if" pattern (#4645) --- src/map/utils/petutils.cpp | 30 +++++++++++++++++++++++++++--- src/map/utils/trustutils.cpp | 17 +++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/map/utils/petutils.cpp b/src/map/utils/petutils.cpp index d0f04384534..bb0e90c9ed9 100644 --- a/src/map/utils/petutils.cpp +++ b/src/map/utils/petutils.cpp @@ -740,12 +740,20 @@ namespace petutils uint32 petID = PPet->m_PetID; // clang-format off - Pet_t* PPetData = *std::find_if(g_PPetList.begin(), g_PPetList.end(), [petID](Pet_t* t) + auto maybePetData = std::find_if(g_PPetList.begin(), g_PPetList.end(), [petID](Pet_t* t) { return t->PetID == petID; }); // clang-format on + if (maybePetData == g_PPetList.end()) + { + ShowError(fmt::format("Could not look up pet data for id: {}", petID)); + return; + } + + auto* PPetData = *maybePetData; + uint8 mLvl = PMaster->GetMLevel(); if (PMaster->GetMJob() == JOB_SMN) @@ -920,12 +928,20 @@ namespace petutils uint32 petID = PPet->m_PetID; // clang-format off - Pet_t* PPetData = *std::find_if(g_PPetList.begin(), g_PPetList.end(), [petID](Pet_t* t) + auto maybePetData = std::find_if(g_PPetList.begin(), g_PPetList.end(), [petID](Pet_t* t) { return t->PetID == petID; }); // clang-format on + if (maybePetData == g_PPetList.end()) + { + ShowError(fmt::format("Could not look up pet data for id: {}", petID)); + return; + } + + auto* PPetData = *maybePetData; + static_cast(PPet->m_Weapons[SLOT_MAIN])->setDelay((uint16)(floor(1000.0f * (240.0f / 60.0f)))); static_cast(PPet->m_Weapons[SLOT_MAIN])->setBaseDelay((uint16)(floor(1000.0f * (240.0f / 60.0f)))); // Get the Jug pet cap level @@ -1581,12 +1597,20 @@ namespace petutils } // clang-format off - Pet_t* PPetData = *std::find_if(g_PPetList.begin(), g_PPetList.end(), [PetID](Pet_t* t) + auto maybePetData = std::find_if(g_PPetList.begin(), g_PPetList.end(), [PetID](Pet_t* t) { return t->PetID == PetID; }); // clang-format on + if (maybePetData == g_PPetList.end()) + { + ShowError(fmt::format("Could not look up pet data for id: {}", PetID)); + return; + } + + auto* PPetData = *maybePetData; + if (PMaster->GetMJob() != JOB_DRG && PetID == PETID_WYVERN) { return; diff --git a/src/map/utils/trustutils.cpp b/src/map/utils/trustutils.cpp index 7d4aaaaed5e..24d8f755c60 100644 --- a/src/map/utils/trustutils.cpp +++ b/src/map/utils/trustutils.cpp @@ -341,12 +341,17 @@ namespace trustutils CTrustEntity* SpawnTrust(CCharEntity* PMaster, uint32 TrustID) { + CTrustEntity* PTrust = LoadTrust(PMaster, TrustID); + if (PTrust == nullptr) + { + return nullptr; + } + if (PMaster->PParty == nullptr) { PMaster->PParty = new CParty(PMaster); } - CTrustEntity* PTrust = LoadTrust(PMaster, TrustID); PMaster->PTrusts.insert(PMaster->PTrusts.end(), PTrust); PMaster->StatusEffectContainer->CopyConfrontationEffect(PTrust); PTrust->setBattleID(PMaster->getBattleID()); @@ -374,12 +379,20 @@ namespace trustutils auto* PTrust = new CTrustEntity(PMaster); // clang-format off - auto* trustData = *std::find_if(g_PTrustList.begin(), g_PTrustList.end(), [TrustID](Trust_t* t) + auto maybeTrustData = std::find_if(g_PTrustList.begin(), g_PTrustList.end(), [TrustID](Trust_t* t) { return t->trustID == TrustID; }); // clang-format on + if (maybeTrustData == g_PTrustList.end()) + { + ShowError(fmt::format("Could not look up trust data for id: {}", TrustID)); + return PTrust; + } + + auto* trustData = *maybeTrustData; + PTrust->loc = PMaster->loc; PTrust->m_OwnerID.id = PMaster->id; PTrust->m_OwnerID.targid = PMaster->targid;