diff --git a/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp b/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp index f9c12e7..d2ec166 100644 --- a/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp +++ b/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp @@ -15,8 +15,10 @@ #ifdef ENABLE_NETOBJ_ORDERED_LIST_VERBOSE_DEBUG #define NETOBJ_ORDERED_LIST_VERBOSE_DEBUG(data, idMap) netobj_ordered_list_debug_check(data, idMap) + #define NETOBJ_CHECK_INSERT_ORDERED(data, newObjs) netobj_check_insert_is_ordered(data, newObjs) #else #define NETOBJ_ORDERED_LIST_VERBOSE_DEBUG(data, idMap) + #define NETOBJ_CHECK_INSERT_ORDERED(data, newObjs) #endif namespace NetworkingObjects { @@ -78,6 +80,28 @@ namespace NetworkingObjects { } std::cout << "[netobj_ordered_list_debug_check] Debug print end" << std::endl; } + + template void netobj_check_insert_is_ordered(const std::list>& l, std::vector, NetObjOwnerPtr>>& newObjs) { + std::cout << "[netobj_check_insert_is_ordered] Insert list order check" << std::endl; + uint32_t lastPos = 0; + bool isEnd = false; + for(auto& [it, obj] : newObjs) { + if(it == l.end()) { + std::cout << "[netobj_check_insert_is_ordered] Insert at: " << l.size() << " (end)" << std::endl; + isEnd = true; + } + else { + std::cout << "[netobj_check_insert_is_ordered] Insert at: " << it->pos << std::endl; + if(isEnd) + throw std::runtime_error("[netobj_check_insert_is_ordered] Insert list not ordered! (Failure at end)"); + if(lastPos > it->pos) + throw std::runtime_error("[netobj_check_insert_is_ordered] Insert list not ordered!"); + lastPos = it->pos; + isEnd = false; + } + } + std::cout << "[netobj_check_insert_is_ordered] Insert list is ordered" << std::endl; + } #endif template void refresh_positions_complete(std::list>& l) { @@ -311,6 +335,8 @@ namespace NetworkingObjects { if(newObjs.empty()) return {}; + NETOBJ_CHECK_INSERT_ORDERED(data, newObjs); + std::vector idPositionList; for(auto& [itToInsertAt, newObj] : newObjs) { if(itToInsertAt == data.begin()) @@ -465,6 +491,8 @@ namespace NetworkingObjects { if(newObjs.empty()) return {}; + NETOBJ_CHECK_INSERT_ORDERED(clientData, newObjs); + 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 @@ -648,8 +676,11 @@ namespace NetworkingObjects { if(!elemData.foundInClient) continue; if(firstInsertedIt == clientData.end()) { - firstInsertedActualPos = elemData.clientInsertPosIt->pos; firstInsertedIt = elemData.clientObjectIt; + if(elemData.clientInsertPosIt != clientData.end()) + firstInsertedActualPos = elemData.clientInsertPosIt->pos; + else + firstInsertedActualPos = clientData.size(); } clientData.splice(elemData.clientInsertPosIt, referredToObjectData, elemData.clientObjectIt); clientIdToDataMap.emplace(elemData.clientObjectIt->obj.get_net_id(), elemData.clientObjectIt); diff --git a/src/World.cpp b/src/World.cpp index 70e792d..fe67c08 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -507,34 +507,38 @@ void World::scale_up(const WorldScalar& scaleUpAmount) { #ifdef ENABLE_ORDERED_LIST_TEST void World::list_debug_test_update() { if(std::chrono::steady_clock::now() < (listDebugTestTimeStart + std::chrono::minutes(1))) { - bool isInsert; - if(listDebugTest->size() > 70) - isInsert = false; - else if(listDebugTest->size() < 20) - isInsert = true; - else - isInsert = Random::get().real_range(0.0f, 1.0f) > 0.5f; + if(nextSendTime < std::chrono::steady_clock::now() - std::chrono::milliseconds(300)) { + nextSendTime = std::chrono::steady_clock::now(); - using namespace NetworkingObjects; - if(isInsert) { - std::vector, NetObjOwnerPtr>> toInsert; - std::vector randomIndices; - uint32_t insertAmount = Random::get().int_range(5, 25); - for(uint32_t i = 0; i < insertAmount; i++) - randomIndices.emplace_back(Random::get().int_range(0, listDebugTest->size() + 10)); - randomIndices[1] = randomIndices[0]; - std::sort(randomIndices.begin(), randomIndices.end()); - for(uint32_t index : randomIndices) - toInsert.emplace_back(listDebugTest->at(index), netObjMan.make_obj_direct(Random::get().int_range(10, 100))); - listDebugTest->insert_ordered_list_and_send_create(listDebugTest, toInsert); - } - else { - std::vector> toErase; - for(uint32_t i = 0; i < listDebugTest->size(); i++) { - if(Random::get().real_range(0.0f, 1.0f) > 0.8f) - toErase.emplace_back(listDebugTest->at(i)); + bool isInsert; + if(listDebugTest->size() > 70) + isInsert = false; + else if(listDebugTest->size() < 20) + isInsert = true; + else + isInsert = Random::get().real_range(0.0f, 1.0f) > 0.5f; + + using namespace NetworkingObjects; + if(isInsert) { + std::vector, NetObjOwnerPtr>> toInsert; + std::vector randomIndices; + uint32_t insertAmount = Random::get().int_range(5, 25); + for(uint32_t i = 0; i < insertAmount; i++) + randomIndices.emplace_back(Random::get().int_range(0, listDebugTest->size() + 10)); + randomIndices[1] = randomIndices[0]; + std::sort(randomIndices.begin(), randomIndices.end()); + for(uint32_t index : randomIndices) + toInsert.emplace_back(listDebugTest->at(index), netObjMan.make_obj_direct(Random::get().int_range(10, 100))); + listDebugTest->insert_ordered_list_and_send_create(listDebugTest, toInsert); + } + else { + std::vector> toErase; + for(uint32_t i = 0; i < listDebugTest->size(); i++) { + if(Random::get().real_range(0.0f, 1.0f) > 0.8f) + toErase.emplace_back(listDebugTest->at(i)); + } + listDebugTest->erase_list(listDebugTest, toErase); } - listDebugTest->erase_list(listDebugTest, toErase); } } if(netServer && netServer->get_client_list().empty()) diff --git a/src/World.hpp b/src/World.hpp index ee58b1b..0cd27e4 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -120,5 +120,6 @@ class World { NetworkingObjects::NetObjOwnerPtr> listDebugTest; void list_debug_test_update(); std::chrono::steady_clock::time_point listDebugTestTimeStart = std::chrono::steady_clock::now(); + std::chrono::steady_clock::time_point nextSendTime = std::chrono::steady_clock::now(); #endif };