diff --git a/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp b/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp index c0dbda0..17957d1 100644 --- a/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp +++ b/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp @@ -313,25 +313,30 @@ namespace NetworkingObjects { switch(command) { case ObjPtrOrderedListCommand_CtoS::INSERT_LIST: { std::vector, NetObjOwnerPtr>> newObjs; + std::vector insertIndices; uint32_t newObjsSize; a(newObjsSize); for(uint32_t i = 0; i < newObjsSize; i++) { - NetworkingObjects::NetObjID idBeforeElement; - a(idBeforeElement); - NetObjOrderedListIterator itToInsertAt; - if(idBeforeElement == NetworkingObjects::NetObjID{0, 0}) - itToInsertAt = data.begin(); - else { - auto idToDataMapIt = idToDataMap.find(idBeforeElement); - if(idToDataMapIt == idToDataMap.end()) - itToInsertAt = data.end(); - else - itToInsertAt = std::next(idToDataMapIt->second); - } - - newObjs.emplace_back(itToInsertAt, l.get_obj_man()->template read_create_message(a, c)); + uint32_t& insertIndex = insertIndices.emplace_back(); + a(insertIndex); + insertIndex -= i; + insertIndex = std::min(insertIndex, data.size()); + newObjs.emplace_back(NetObjOrderedListIterator(), l.get_obj_man()->template read_create_message(a, c)); } + + uint32_t currentIndex = data.size(); + NetObjOrderedListIterator currentIt = data.end(); + for(std::pair, NetObjOwnerPtr>&, uint32_t&> elem : (std::views::zip(newObjs, insertIndices) | std::views::reverse)) { + uint32_t index = elem.second; + auto& it = elem.first.first; + while(currentIndex != index) { + --currentIndex; + --currentIt; + } + it = currentIt; + } + insert_ordered_list(l, c, newObjs); break; } @@ -416,14 +421,6 @@ namespace NetworkingObjects { if(newObjs.empty()) return {}; - std::vector idPositionList; - for(auto& [itToInsertAt, newObj] : newObjs) { - if(itToInsertAt == clientData.begin()) - idPositionList.emplace_back(NetworkingObjects::NetObjID{0, 0}); - else - idPositionList.emplace_back(std::prev(itToInsertAt)->obj.get_net_id()); - } - std::vector> toRet; for(auto& [itToInsertAt, newObj] : newObjs) { auto newObjIt = clientData.emplace(itToInsertAt, std::move(newObj), itToInsertAt == clientData.end() ? clientData.size() : itToInsertAt->pos); // All position values will be wrong except the first one, which is all that matters @@ -431,10 +428,10 @@ namespace NetworkingObjects { toRet.emplace_back(newObjIt); } set_position_obj_info_list(toRet[0], clientData.end()); - l.send_client_update(RELIABLE_COMMAND_CHANNEL, [&toRet, &idPositionList](const NetObjTemporaryPtr>&, cereal::PortableBinaryOutputArchive& a) { + l.send_client_update(RELIABLE_COMMAND_CHANNEL, [&toRet](const NetObjTemporaryPtr>&, cereal::PortableBinaryOutputArchive& a) { a(ObjPtrOrderedListCommand_CtoS::INSERT_LIST, static_cast(toRet.size())); - for(auto [newObjIt, idPosition] : std::views::zip(toRet, idPositionList)) { - a(idPosition); + for(auto newObjIt : toRet) { + a(newObjIt->pos); newObjIt->obj.write_create_message(a); } });