From 3bf42c6ff3805a0d42bbc661794a95ff31bedc26 Mon Sep 17 00:00:00 2001 From: untodesu Date: Sat, 15 Mar 2025 16:22:09 +0500 Subject: Add whatever I was working on for the last month --- game/server/unloader.cc | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 game/server/unloader.cc (limited to 'game/server/unloader.cc') diff --git a/game/server/unloader.cc b/game/server/unloader.cc new file mode 100644 index 0000000..fd838d0 --- /dev/null +++ b/game/server/unloader.cc @@ -0,0 +1,77 @@ +#include "server/pch.hh" +#include "server/unloader.hh" + +#include "core/config.hh" + +#include "shared/chunk_aabb.hh" +#include "shared/chunk.hh" +#include "shared/dimension.hh" +#include "shared/player.hh" +#include "shared/player.hh" +#include "shared/transform.hh" + +#include "server/game.hh" +#include "server/globals.hh" +#include "server/inhabited.hh" +#include "server/universe.hh" + +static void on_chunk_update(const ChunkUpdateEvent &event) +{ + event.dimension->chunks.emplace_or_replace(event.chunk->get_entity()); +} + +static void on_voxel_set(const VoxelSetEvent &event) +{ + event.dimension->chunks.emplace_or_replace(event.chunk->get_entity()); +} + +void unloader::init(void) +{ + globals::dispatcher.sink().connect<&on_chunk_update>(); + globals::dispatcher.sink().connect<&on_voxel_set>(); +} + +void unloader::init_late(void) +{ + +} + +void unloader::fixed_update_late(Dimension *dimension) +{ + auto group = dimension->entities.group(entt::get); + auto boxes = std::vector(); + + for(const auto [entity, transform] : group.each()) { + ChunkAABB aabb; + aabb.min = transform.chunk - static_cast(server_game::view_distance.get_value()); + aabb.max = transform.chunk + static_cast(server_game::view_distance.get_value()); + boxes.push_back(aabb); + } + + auto view = dimension->chunks.view(); + 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(entity)) { + // Only store inhabited chunks on disk + universe::save_chunk(dimension, chunk.cpos); + } + + dimension->remove_chunk(entity); + } +} -- cgit