From cc8c222d18d4fe4bebfbbb1547189f1e91d970ca Mon Sep 17 00:00:00 2001 From: David Cernat Date: Tue, 22 Mar 2022 10:42:19 +0200 Subject: [PATCH] [General] Combine CellStore's exact object searches --- apps/openmw/mwmp/ObjectList.cpp | 21 ++------ apps/openmw/mwworld/cellstore.cpp | 64 ++++-------------------- apps/openmw/mwworld/cellstore.hpp | 16 ++---- components/openmw-mp/Base/BaseObject.hpp | 2 +- 4 files changed, 16 insertions(+), 87 deletions(-) diff --git a/apps/openmw/mwmp/ObjectList.cpp b/apps/openmw/mwmp/ObjectList.cpp index 29c0f512b..daf2e0768 100644 --- a/apps/openmw/mwmp/ObjectList.cpp +++ b/apps/openmw/mwmp/ObjectList.cpp @@ -365,14 +365,7 @@ void ObjectList::activateObjects(MWWorld::CellStore* cellStore) else { LOG_APPEND(TimedLog::LOG_VERBOSE, "-- Activated object is %s %i-%i", baseObject.refId.c_str(), baseObject.refNum, baseObject.mpNum); - if (baseObject.refId.empty()) - { - ptrFound = cellStore->searchExact(baseObject.refNum, baseObject.mpNum); - } - else - { - ptrFound = cellStore->searchExactPlus(baseObject.refId, baseObject.refNum, baseObject.mpNum); - } + ptrFound = cellStore->searchExact(baseObject.refNum, baseObject.mpNum, baseObject.refId); } if (ptrFound) @@ -1014,16 +1007,8 @@ void ObjectList::makeDialogueChoices(MWWorld::CellStore* cellStore) for (const auto& baseObject : baseObjects) { LOG_APPEND(TimedLog::LOG_VERBOSE, "- cellRef: %s %i-%i", baseObject.refId.c_str(), baseObject.refNum, baseObject.mpNum); - MWWorld::Ptr ptrFound; - - if (baseObject.refId.empty()) - { - ptrFound = cellStore->searchExact(baseObject.refNum, baseObject.mpNum); - } - else - { - ptrFound = cellStore->searchExactPlus(baseObject.refId, baseObject.refNum, baseObject.mpNum); - } + + MWWorld::Ptr ptrFound = cellStore->searchExact(baseObject.refNum, baseObject.mpNum, baseObject.refId); if (ptrFound) { diff --git a/apps/openmw/mwworld/cellstore.cpp b/apps/openmw/mwworld/cellstore.cpp index 46a1dc7a7..27b26ed5d 100644 --- a/apps/openmw/mwworld/cellstore.cpp +++ b/apps/openmw/mwworld/cellstore.cpp @@ -605,47 +605,21 @@ namespace MWWorld { const unsigned int mRefNumToFind; const unsigned int mMpNumToFind; - public: - SearchExactVisitor(const unsigned int refNum, const unsigned int mpNum) : mRefNumToFind(refNum), mMpNumToFind(mpNum) {} - - Ptr mFound; - - bool operator()(const Ptr& ptr) - { - if (ptr.getCellRef().getRefNum().mIndex == mRefNumToFind && ptr.getCellRef().getMpNum() == mMpNumToFind) - { - mFound = ptr; - return false; - } - return true; - } - }; - /* - End of tes3mp addition - */ - - /* - Start of tes3mp addition - - A custom type of search visitor used to find objects by their reference numbers - while ensuring they have a certain refId - */ - class SearchExactPlusVisitor - { const std::string mRefIdToFind; - const unsigned int mRefNumToFind; - const unsigned int mMpNumToFind; public: - SearchExactPlusVisitor(const std::string refId, const unsigned int refNum, const unsigned int mpNum) : mRefIdToFind(refId), mRefNumToFind(refNum), mMpNumToFind(mpNum) {} + SearchExactVisitor(const unsigned int refNum, const unsigned int mpNum, const std::string refId) : mRefNumToFind(refNum), mMpNumToFind(mpNum), mRefIdToFind(refId) {} Ptr mFound; bool operator()(const Ptr& ptr) { - if (ptr.getCellRef().getRefNum().mIndex == mRefNumToFind && ptr.getCellRef().getMpNum() == mMpNumToFind && Misc::StringUtils::ciEqual(ptr.getCellRef().getRefId(), mRefIdToFind)) + if (ptr.getCellRef().getRefNum().mIndex == mRefNumToFind && ptr.getCellRef().getMpNum() == mMpNumToFind) { - mFound = ptr; - return false; + if (mRefIdToFind.empty() || Misc::StringUtils::ciEqual(ptr.getCellRef().getRefId(), mRefIdToFind)) + { + mFound = ptr; + return false; + } } return true; } @@ -659,33 +633,13 @@ namespace MWWorld Allow the searching of objects by their reference numbers */ - Ptr CellStore::searchExact (const unsigned int refNum, const unsigned int mpNum) - { - // Ensure that all objects searched for have a valid reference number - if (refNum == 0 && mpNum == 0) - return 0; - - SearchExactVisitor searchVisitor(refNum, mpNum); - forEach(searchVisitor); - return searchVisitor.mFound; - } - /* - End of tes3mp addition - */ - - /* - Start of tes3mp addition - - Allow the searching of objects by their reference numbers while ensuring - they have a certain refId - */ - Ptr CellStore::searchExactPlus(const std::string refId, const unsigned int refNum, const unsigned int mpNum) + Ptr CellStore::searchExact (const unsigned int refNum, const unsigned int mpNum, const std::string refId) { // Ensure that all objects searched for have a valid reference number if (refNum == 0 && mpNum == 0) return 0; - SearchExactPlusVisitor searchVisitor(refId, refNum, mpNum); + SearchExactVisitor searchVisitor(refNum, mpNum, refId); forEach(searchVisitor); return searchVisitor.mFound; } diff --git a/apps/openmw/mwworld/cellstore.hpp b/apps/openmw/mwworld/cellstore.hpp index ba0a78972..b5179e676 100644 --- a/apps/openmw/mwworld/cellstore.hpp +++ b/apps/openmw/mwworld/cellstore.hpp @@ -262,20 +262,10 @@ namespace MWWorld /* Start of tes3mp addition - Allow the searching of objects by their reference numbers + Allow the searching of objects by their reference numbers and, optionally, + their refIds */ - Ptr searchExact (unsigned int refNum, unsigned int mpNum); - /* - End of tes3mp addition - */ - - /* - Start of tes3mp addition - - Allow the searching of objects by their reference numbers while ensuring - they have a certain refId - */ - Ptr searchExactPlus (std::string refId, unsigned int refNum, unsigned int mpNum); + Ptr searchExact (unsigned int refNum, unsigned int mpNum, std::string refId = ""); /* End of tes3mp addition */ diff --git a/components/openmw-mp/Base/BaseObject.hpp b/components/openmw-mp/Base/BaseObject.hpp index f3d004923..94175a63d 100644 --- a/components/openmw-mp/Base/BaseObject.hpp +++ b/components/openmw-mp/Base/BaseObject.hpp @@ -26,7 +26,7 @@ namespace mwmp struct BaseObject { - std::string refId; + std::string refId = ""; unsigned int refNum; unsigned int mpNum; int count;