From e29928ef4670c03b20bf728bfe36726adfef2c6e Mon Sep 17 00:00:00 2001 From: "R.K" Date: Thu, 16 May 2024 09:47:25 -0700 Subject: [PATCH] [Enhancement]: Stability Improvements to Black Hole Feature (#3132) --- src/backend/looped/world/blackhole.cpp | 54 +++++++++++++++++--------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/backend/looped/world/blackhole.cpp b/src/backend/looped/world/blackhole.cpp index 8d6f4806..26069511 100644 --- a/src/backend/looped/world/blackhole.cpp +++ b/src/backend/looped/world/blackhole.cpp @@ -11,27 +11,41 @@ namespace big { using looped_command::looped_command; + std::vector entity_list; + std::chrono::steady_clock::time_point last_call_time; + virtual void on_tick() override { - for (auto entity : entity::get_entities(g.world.blackhole.include_vehicles, g.world.blackhole.include_peds)) + auto current_time = std::chrono::steady_clock::now(); + auto elapsed_time = std::chrono::duration_cast(current_time - last_call_time).count(); + + if (elapsed_time >= 1000) // Reduce black hole gather spam so it pulses only every second { - if (entity::take_control_of(entity, 0)) + entity_list = entity::get_entities(g.world.blackhole.include_vehicles, g.world.blackhole.include_peds); + last_call_time = current_time; + + for (int i = 0; i < 30 && i < entity_list.size(); i++) // Only yeet up to 30 entities every second to prevent crashes { - auto entity_coord = ENTITY::GET_ENTITY_COORDS(entity, false); - ENTITY::APPLY_FORCE_TO_ENTITY(entity, - 1, - ((g.world.blackhole.pos.x - entity_coord.x) * 9.f), - ((g.world.blackhole.pos.y - entity_coord.y) * 9.f), - ((g.world.blackhole.pos.z - entity_coord.z) * 9.f), - 0.f, - 0.f, - 0.f, - 0, - false, - true, - true, - 0, - 0); + auto entity = entity_list[i]; + + if (entity::take_control_of(entity, 0)) + { + auto entity_coord = ENTITY::GET_ENTITY_COORDS(entity, false); + ENTITY::APPLY_FORCE_TO_ENTITY(entity, + 1, + ((g.world.blackhole.pos.x - entity_coord.x) * 9.f), + ((g.world.blackhole.pos.y - entity_coord.y) * 9.f), + ((g.world.blackhole.pos.z - entity_coord.z) * 9.f), + 0.f, + 0.f, + 0.f, + 0, + false, + true, + true, + 0, + 0); + } } } @@ -64,6 +78,8 @@ namespace big }; blackhole g_blackhole("blackhole", "GUI_TAB_BLACKHOLE", "BACKEND_LOOPED_WORLD_BLACKHOLE_DESC", g.world.blackhole.enable); - bool_command g_blackhole_peds("blackholeincpeds", "PEDS", "BACKEND_LOOPED_WORLD_BLACKHOLE_PEDS_DESC", g.world.blackhole.include_peds); - bool_command g_blackhole_vehicles("blackholeincvehs", "VEHICLES", "BACKEND_LOOPED_WORLD_BLACKHOLE_VEHS_DESC", g.world.blackhole.include_vehicles); + bool_command + g_blackhole_peds("blackholeincpeds", "PEDS", "BACKEND_LOOPED_WORLD_BLACKHOLE_PEDS_DESC", g.world.blackhole.include_peds); + bool_command g_blackhole_vehicles("blackholeincvehs", "VEHICLES", "BACKEND_LOOPED_WORLD_BLACKHOLE_VEHS_DESC", + g.world.blackhole.include_vehicles); } \ No newline at end of file