summaryrefslogtreecommitdiffstats
path: root/game/server/world/unloader.cc
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
committeruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
commit458e0005690ea9d579588a0a12368fc2c2c9a93a (patch)
tree588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /game/server/world/unloader.cc
parentc7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff)
downloadvoxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2
voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip
I hyper-focued on refactoring again
- I put a cool-sounding "we are number one" remix on repeat and straight up grinded the entire repository to a better state until 03:09 AM. I guess I have something wrong in my brain that makes me do this shit
Diffstat (limited to 'game/server/world/unloader.cc')
-rw-r--r--game/server/world/unloader.cc77
1 files changed, 77 insertions, 0 deletions
diff --git a/game/server/world/unloader.cc b/game/server/world/unloader.cc
new file mode 100644
index 0000000..3600ea2
--- /dev/null
+++ b/game/server/world/unloader.cc
@@ -0,0 +1,77 @@
+#include "server/pch.hh"
+
+#include "server/world/unloader.hh"
+
+#include "core/config/number.hh"
+
+#include "shared/entity/player.hh"
+#include "shared/entity/transform.hh"
+#include "shared/world/chunk.hh"
+#include "shared/world/chunk_aabb.hh"
+#include "shared/world/dimension.hh"
+
+#include "server/world/inhabited.hh"
+#include "server/world/universe.hh"
+
+#include "server/game.hh"
+#include "server/globals.hh"
+
+static void on_chunk_update(const world::ChunkUpdateEvent& event)
+{
+ event.dimension->chunks.emplace_or_replace<world::Inhabited>(event.chunk->get_entity());
+}
+
+static void on_voxel_set(const world::VoxelSetEvent& event)
+{
+ event.dimension->chunks.emplace_or_replace<world::Inhabited>(event.chunk->get_entity());
+}
+
+void world::unloader::init(void)
+{
+ globals::dispatcher.sink<world::ChunkUpdateEvent>().connect<&on_chunk_update>();
+ globals::dispatcher.sink<world::VoxelSetEvent>().connect<&on_voxel_set>();
+}
+
+void world::unloader::init_late(void)
+{
+}
+
+void world::unloader::fixed_update_late(Dimension* dimension)
+{
+ auto group = dimension->entities.group(entt::get<entity::Player, entity::Transform>);
+ auto boxes = std::vector<ChunkAABB>();
+
+ for(const auto [entity, transform] : group.each()) {
+ ChunkAABB aabb;
+ aabb.min = transform.chunk - static_cast<chunk_pos::value_type>(server_game::view_distance.get_value());
+ aabb.max = transform.chunk + static_cast<chunk_pos::value_type>(server_game::view_distance.get_value());
+ boxes.push_back(aabb);
+ }
+
+ auto view = dimension->chunks.view<ChunkComponent>();
+ auto chunk_in_view = false;
+
+ for(const auto [entity, chunk] : view.each()) {
+ chunk_in_view = false;
+
+ for(const auto& aabb : boxes) {
+ if(aabb.contains(chunk.cpos)) {
+ chunk_in_view = true;
+ break;
+ }
+ }
+
+ if(chunk_in_view) {
+ // The chunk is within view box of at least
+ // a single player; we shouldn't unload it now
+ continue;
+ }
+
+ if(dimension->chunks.any_of<Inhabited>(entity)) {
+ // Only store inhabited chunks on disk
+ world::universe::save_chunk(dimension, chunk.cpos);
+ }
+
+ dimension->remove_chunk(entity);
+ }
+}