Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions database/server/202610051332_planets_positions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE "planets" ADD COLUMN "position_x" REAL NOT NULL DEFAULT 0.0;
ALTER TABLE "planets" ADD COLUMN "position_y" REAL NOT NULL DEFAULT 0.0;
ALTER TABLE "planets" ADD COLUMN "position_z" REAL NOT NULL DEFAULT 0.0;
DROP TABLE "planet_links";
22 changes: 20 additions & 2 deletions include/ClientLib/ClientSessionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ namespace tsom
inline entt::handle GetControlledEntity() const;
inline EntityRegistry& GetEntityRegistry();
inline const EntityRegistry& GetEntityRegistry() const;
const Nz::Node* GetEnvironmentNode(std::size_t environmentIndex) const;
inline const GravityController* GetGravityController(std::size_t environmentIndex) const;
const Nz::Node* GetEnvironmentNode(Packets::Helper::EnvironmentId environmentIndex) const;
inline const GravityController* GetGravityController(Packets::Helper::EnvironmentId environmentIndex) const;
inline Packets::Helper::EnvironmentId GetParentEnvironmentId(Packets::Helper::EnvironmentId environmentId) const;
inline Packets::Helper::EnvironmentId GetRootEnvironmentId() const;
inline ScriptingContext& GetScriptingContext();

void HandlePacket(Packets::S_AuthResponse&& authResponse);
Expand Down Expand Up @@ -97,6 +99,7 @@ namespace tsom
NazaraSignal(OnPlayerListUpdated);

static constexpr Packets::Helper::EntityId InvalidEntity = Nz::MaxValue();
static constexpr Packets::Helper::EnvironmentId InvalidEnvironment = Nz::MaxValue();

struct PlayerInfo
{
Expand All @@ -108,8 +111,10 @@ namespace tsom
private:
inline PlayerInfo* FetchPlayerInfo(PlayerIndex playerIndex);
inline const PlayerInfo* FetchPlayerInfo(PlayerIndex playerIndex) const;
void HandleControlledEntityEnvironmentSwitch(Packets::Helper::EnvironmentId newEnvironmentId);
void HandleEntityCreation(Packets::Helper::EntityData&& entityData);
void SetupEntity(entt::handle entity, Packets::Helper::PlayerControlledData&& entityData);
void UpdateRootEnvironment(Packets::Helper::EnvironmentId environmentId);

struct DebugDrawLines
{
Expand All @@ -125,9 +130,20 @@ namespace tsom

struct EnvironmentData
{
struct ChildData
{
Packets::Helper::EnvironmentId parentEnvironment;
};

struct RootData
{
Nz::Vector3i32 indices;
};

Nz::Bitset<Nz::UInt64> entities;
entt::handle rootEntity;
GravityController* gravityController;
std::variant<ChildData, RootData> specData;
};

struct EntityData
Expand All @@ -148,6 +164,7 @@ namespace tsom
std::vector<std::optional<EnvironmentData>> m_environments; //< FIXME: Nz::SparseVector
std::vector<std::optional<PlayerInfo>> m_players; //< FIXME: Nz::SparseVector
tsl::hopscotch_map<Packets::Helper::ChunkId, Packets::S_ChunkReset> m_pendingChunkReset;
tsl::hopscotch_map<Packets::Helper::EntityId, Packets::Helper::EnvironmentId> m_entityToOwnedEnvironment;
Nz::ApplicationBase& m_app;
Nz::EnttWorld& m_world;
ClientBlockLibrary& m_blockLibrary;
Expand All @@ -157,6 +174,7 @@ namespace tsom
ScriptingContext m_scriptingContext;
EntityRegistry m_entityRegistry;
InputIndex m_lastInputIndex;
Packets::Helper::EnvironmentId m_rootEnvironmentId;
};
}

Expand Down
7 changes: 6 additions & 1 deletion include/ClientLib/ClientSessionHandler.inl
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ namespace tsom
return m_entityRegistry;
}

inline const GravityController* ClientSessionHandler::GetGravityController(std::size_t environmentIndex) const
inline const GravityController* ClientSessionHandler::GetGravityController(Packets::Helper::EnvironmentId environmentIndex) const
{
if (environmentIndex > m_environments.size() || !m_environments[environmentIndex])
return nullptr;

return m_environments[environmentIndex]->gravityController;
}

inline Packets::Helper::EnvironmentId ClientSessionHandler::GetRootEnvironmentId() const
{
return m_rootEnvironmentId;
}

inline ScriptingContext& ClientSessionHandler::GetScriptingContext()
{
return m_scriptingContext;
Expand Down
3 changes: 0 additions & 3 deletions include/CommonLib/BlockIndex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#define TSOM_COMMONLIB_BLOCKINDEX_HPP

#include <NazaraUtils/Constants.hpp>
#include <NazaraUtils/Prerequisites.hpp>

namespace tsom
{
Expand All @@ -18,6 +17,4 @@ namespace tsom
constexpr BlockIndex InvalidBlockIndex = Nz::MaxValue();
}

#include <CommonLib/BlockIndex.inl>

#endif // TSOM_COMMONLIB_BLOCKINDEX_HPP
3 changes: 3 additions & 0 deletions include/CommonLib/InternalConstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ namespace tsom::Constants

// Serialization constants
constexpr Nz::UInt32 ChunkBinaryVersion = 1;

// Space constants
constexpr float SpaceCellSize = 8192.0f; //< [-4096;4096]
}

#endif // TSOM_COMMONLIB_INTERNALCONSTANTS_HPP
12 changes: 11 additions & 1 deletion include/CommonLib/Protocol/Packets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,19 @@ namespace tsom

struct S_EnvironmentCreate
{
struct ChildEnvironment
{
Helper::EntityId ownerEntity;
};

struct RootEnvironment
{
Nz::Vector3i32 indices;
};

Nz::UInt16 tickIndex;
Helper::EnvironmentId id;
Helper::EntityId ownerEntity;
std::variant<ChildEnvironment, RootEnvironment> environmentData;
std::vector<Helper::EntityData> entities;
};

Expand Down
2 changes: 1 addition & 1 deletion include/CommonLib/SurfaceNetsChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace tsom
void BuildMesh(std::size_t layerIndex, std::vector<Nz::UInt32>& indices, const Nz::Vector3f& center, const Nz::FunctionRef<VertexAttributes(const Nz::Vector3ui& blockIndices, Direction direction)>& addFace) const override;

Nz::EnumArray<Nz::BoxCorner, Nz::Vector3f> ComputeBlockCorners(const Nz::Vector3ui& indices) const override;
std::optional<Nz::Vector3ui> ComputeCoordinates(const Nz::Vector3f& position) const;
std::optional<Nz::Vector3ui> ComputeCoordinates(const Nz::Vector3f& position) const override;
std::optional<HitBlock> ComputeHitCoordinates(const Nz::Vector3f& hitPos, const Nz::Vector3f& hitNormal, const Nz::Collider3D& collider, std::uint32_t hitSubshapeId) const override;

SurfaceNetsChunk& operator=(const SurfaceNetsChunk&) = delete;
Expand Down
23 changes: 23 additions & 0 deletions include/CommonLib/Utility/SpaceTransformations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2026 Jérôme "SirLynix" Leclercq (lynix680@gmail.com)
// This file is part of the "This Space Of Mine" project
// For conditions of distribution and use, see copyright notice in LICENSE

#pragma once

#ifndef TSOM_COMMONLIB_UTILITY_SPACETRANSFORMATIONS_HPP
#define TSOM_COMMONLIB_UTILITY_SPACETRANSFORMATIONS_HPP

#include <CommonLib/Export.hpp>
#include <CommonLib/InternalConstants.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <utility>

namespace tsom::SpaceTransformations
{
inline Nz::Vector3d ToGlobalCoordinates(const Nz::Vector3i32& cellCoordinates, const Nz::Vector3f& localCoordinates);
inline std::pair<Nz::Vector3i32, Nz::Vector3f> ToLocalCoordinates(const Nz::Vector3d& globalCoords);
}

#include <CommonLib/Utility/SpaceTransformations.inl>

#endif // TSOM_COMMONLIB_UTILITY_SPACETRANSFORMATIONS_HPP
30 changes: 30 additions & 0 deletions include/CommonLib/Utility/SpaceTransformations.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2026 Jérôme "SirLynix" Leclercq (lynix680@gmail.com)
// This file is part of the "This Space Of Mine" project
// For conditions of distribution and use, see copyright notice in LICENSE

#include <cmath>

namespace tsom::SpaceTransformations
{
inline Nz::Vector3d ToGlobalCoordinates(const Nz::Vector3i32& cellCoordinates, const Nz::Vector3f& localCoordinates)
{
constexpr double SpaceCellSize_d = Constants::SpaceCellSize;

return Nz::Vector3d(cellCoordinates) * SpaceCellSize_d + Nz::Vector3d(localCoordinates);
}

inline std::pair<Nz::Vector3i32, Nz::Vector3f> ToLocalCoordinates(const Nz::Vector3d& globalCoords)
{
constexpr double SpaceCellSize_d = Constants::SpaceCellSize;

// Positive when value is positive on this axis
Nz::Vector3d offset = Nz::Vector3d(SpaceCellSize_d * 0.5);
offset *= Nz::Vector3d::Apply(globalCoords, [](double value) { return std::copysign(1.0, value); });

Nz::Vector3d offsetCoords = globalCoords + offset;
Nz::Vector3d cellCoordinates = offsetCoords / Nz::Vector3d(SpaceCellSize_d);
Nz::Vector3d localCoordinates = offsetCoords % Nz::Vector3d(SpaceCellSize_d) - offset;

return { Nz::Vector3i32(cellCoordinates), Nz::Vector3f(localCoordinates) };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace tsom
Nz::Boxf aabb; //< in trigger space, serves as a cheap test before testing entryTrigger
ServerEnvironment* targetEnvironment;
bool enabled = true;
bool updateRoot = false;
};
}

Expand Down
8 changes: 1 addition & 7 deletions include/ServerLib/Database/Schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace tsom::Database
Nz::UInt32 id;
std::string_view generatorName;
std::string_view type = "round_cube";
Nz::Vector3d position = Nz::Vector3d::Zero();
Nz::Vector3ui32 chunkCount;
nlohmann::json properties = nlohmann::json::object();
};
Expand Down Expand Up @@ -66,13 +67,6 @@ namespace tsom::Database
Nz::UInt32 chunkDataVersion;
std::span<const Nz::UInt8> chunkData;
};

struct PlanetLink
{
Nz::UInt32 sourcePlanet;
Nz::UInt32 destinationPlanet;
Nz::Vector3f position;
};
}

#include <ServerLib/Database/Schema.inl>
Expand Down
6 changes: 0 additions & 6 deletions include/ServerLib/Database/ServerDatabase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ namespace tsom
void GetAllConfigs(Nz::FunctionRef<bool(Database::Config&& /*config*/)> callback) const;
void GetAllPlanets(Nz::FunctionRef<bool(Database::Planet&& /*planet*/)> callback) const;
void GetAllPlanetEntities(Nz::UInt32 planetEntityId, Nz::FunctionRef<bool(Database::PlanetEntity&& /*planetEntities*/)> callback) const;
void GetAllPlanetLinks(Nz::FunctionRef<bool(Database::PlanetLink&& /*planetLink*/)> callback) const;
void GetAllPlanetChunks(Nz::UInt32 planetId, Nz::FunctionRef<bool(Database::PlanetChunk&& /*planetChunk*/)> callback) const;
bool GetPlanetChunk(Nz::UInt32 planetId, const Nz::Vector3i32& chunkIndices, Nz::FunctionRef<void(Database::PlanetChunk&& /*planetChunk*/)> callback) const;

void StorePlanet(const Database::Planet& planetChunk);
void StorePlanetChunk(const Database::PlanetChunk& planetChunk);
void StorePlanetLink(const Database::PlanetLink& planetChunk);

void Transaction(Nz::FunctionRef<bool(ServerDatabase& database)> callback);

Expand Down Expand Up @@ -80,10 +78,6 @@ namespace tsom
SQLite::Statement deletePlanetEntityQuery;
SQLite::Statement getAllPlanetEntitiesQuery;
SQLite::Statement partialUpdatePlanetEntityQuery;

// planet links
SQLite::Statement getAllPlanetLinkQuery;
SQLite::Statement storePlanetLinkQuery;
};

SQLite::Database m_database;
Expand Down
7 changes: 4 additions & 3 deletions include/ServerLib/ServerEnvironment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace tsom
enum class ServerEnvironmentType
{
Planet,
Ship
Ship,
Space
};

class TSOM_SERVERLIB_API ServerEnvironment
Expand All @@ -51,8 +52,8 @@ namespace tsom
template<typename F> void ForEachPlayer(F&& callback) const;

virtual ServerAtmosphere* GetAtmosphereAtPosition(const Nz::Vector3f& position);
virtual ChunkContainer& GetChunkContainer() = 0;
virtual const ChunkContainer& GetChunkContainer() const = 0;
virtual ChunkContainer* GetChunkContainer() = 0;
virtual const ChunkContainer* GetChunkContainer() const = 0;
virtual const GravityController* GetGravityController() const = 0;
inline ServerInstance& GetServerInstance();
inline ServerEnvironmentType GetType() const;
Expand Down
6 changes: 5 additions & 1 deletion include/ServerLib/ServerInstance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace tsom
{
class ServerPlanetEnvironment;
class ServerShipEnvironment;
class ServerSpaceEnvironment;

class TSOM_SERVERLIB_API ServerInstance
{
Expand Down Expand Up @@ -66,9 +67,12 @@ namespace tsom
template<typename F> void ForEachPlayer(F&& functor);
template<typename F> void ForEachPlayer(F&& functor) const;

template<typename F> void ForEachSpaceCell(F&& functor);

inline Nz::ApplicationBase& GetApplication();
inline const BlockLibrary& GetBlockLibrary() const;
inline const Config& GetConfig() const;
ServerEnvironment* GetOrCreateSpaceEnvironment(const Nz::Vector3i32& coordinates);
inline const Spawnpoint& GetDefaultSpawnpoint() const;
inline EntityRegistry& GetEntityRegistry();
inline const EntityRegistry& GetEntityRegistry() const;
Expand All @@ -78,7 +82,6 @@ namespace tsom
inline Nz::Time GetTickDuration() const;
inline Nz::TimerManager& GetTickedTimerManager();

void LinkDatabaseEnvironments(Nz::UInt32 sourceDatabaseId, Nz::UInt32 destinationDatabaseId, const Nz::Vector3f& position);
void LoadFromDatabase();
void LoadScripts(bool isReloading = false);

Expand Down Expand Up @@ -133,6 +136,7 @@ namespace tsom
std::vector<std::function<void()>> m_scheduledTickFunctions;
std::vector<std::function<void()>> m_nextScheduledTickFunctions;
tsl::hopscotch_map<Nz::UInt32 /*databaseId*/, std::unique_ptr<ServerEnvironment>> m_databaseEnvironments;
tsl::hopscotch_map<Nz::Vector3i32 /*coordinates*/, std::unique_ptr<ServerSpaceEnvironment>> m_spaceCellEnvironments;
Nz::Bitset<> m_disconnectedPlayers;
Nz::Bitset<> m_newPlayers;
Nz::MemoryPool<ServerPlayer> m_players;
Expand Down
13 changes: 11 additions & 2 deletions include/ServerLib/ServerInstance.inl
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,27 @@ namespace tsom
return nullptr;
}

template<typename F> void ServerInstance::ForEachPlayer(F&& functor)
template<typename F>
void ServerInstance::ForEachPlayer(F&& functor)
{
for (ServerPlayer& serverPlayer : m_players)
functor(serverPlayer);
}

template<typename F> void ServerInstance::ForEachPlayer(F&& functor) const
template<typename F>
void ServerInstance::ForEachPlayer(F&& functor) const
{
for (const ServerPlayer& serverPlayer : m_players)
functor(serverPlayer);
}

template<typename F>
void ServerInstance::ForEachSpaceCell(F&& functor)
{
for (auto&& [indices, environmentPtr] : m_spaceCellEnvironments)
functor(indices, environmentPtr.get());
}

inline Nz::ApplicationBase& ServerInstance::GetApplication()
{
return m_application;
Expand Down
11 changes: 8 additions & 3 deletions include/ServerLib/ServerPlanetEnvironment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace tsom
class TSOM_SERVERLIB_API ServerPlanetEnvironment final : public ServerEnvironment
{
public:
ServerPlanetEnvironment(ServerInstance& serverInstance, std::optional<Nz::UInt32> databaseId, std::string generatorName, const Nz::Vector3ui& chunkCount, std::string_view planetType, std::vector<EntityProperty>&& properties);
ServerPlanetEnvironment(ServerInstance& serverInstance, std::optional<Nz::UInt32> databaseId, std::string generatorName, const Nz::Vector3d& position, const Nz::Vector3ui& chunkCount, std::string_view planetType, std::vector<EntityProperty>&& properties);
ServerPlanetEnvironment(const ServerPlanetEnvironment&) = delete;
ServerPlanetEnvironment(ServerPlanetEnvironment&&) = delete;
~ServerPlanetEnvironment();
Expand All @@ -41,15 +41,17 @@ namespace tsom
void ForEachAtmosphere(Nz::FunctionRef<void(ServerAtmosphere*)> callback) override;
void ForEachAtmosphere(Nz::FunctionRef<void(const ServerAtmosphere*)> callback) const override;

ChunkContainer& GetChunkContainer() override;
const ChunkContainer& GetChunkContainer() const override;
ChunkContainer* GetChunkContainer() override;
const ChunkContainer* GetChunkContainer() const override;
inline std::optional<Nz::UInt32> GetDatabaseId() const;
const GravityController* GetGravityController() const override;
Planet& GetPlanet();
const Planet& GetPlanet() const;
inline entt::handle GetPlanetEntity() const;
inline const Nz::Vector3d& GetPlanetPosition() const;

void OnSave() override;
void OnTick(Nz::Time elapsedTime) override;

void Update() override;

Expand All @@ -60,6 +62,8 @@ namespace tsom
ServerAtmosphere* GetFallbackAtmosphereAtPosition(const Nz::Vector3f& position) override;
void LoadChunksFromDatabase();
void LoadEntitiesFromDatabase();
void UpdateChunkLoading();
void UpdateEnvironmentSwitch();

struct ChunkLoadingData
{
Expand All @@ -83,6 +87,7 @@ namespace tsom
std::unordered_set<ChunkIndices /*chunkIndex*/> m_dirtyChunks;
std::unordered_map<std::string, EntityProperty> m_planetProperties;
entt::handle m_planetEntity;
Nz::Vector3d m_planetPosition;
Nz::Vector3ui m_chunkCount;
};
}
Expand Down
Loading
Loading