Skip to content

Commit

Permalink
Rework environment switching and allow other entities to switch
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Dec 6, 2024
1 parent 70069db commit de8cc66
Show file tree
Hide file tree
Showing 37 changed files with 774 additions and 543 deletions.
6 changes: 2 additions & 4 deletions include/ClientLib/ClientSessionHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ namespace tsom
void HandlePacket(Packets::EntityPropertyUpdate&& propertyUpdate);
void HandlePacket(Packets::EnvironmentCreate&& envCreate);
void HandlePacket(Packets::EnvironmentDestroy&& envDestroy);
void HandlePacket(Packets::EnvironmentUpdate&& envUpdate);
void HandlePacket(Packets::EnvironmentsUpdateOwner&& envOwnerUpdate);
void HandlePacket(Packets::GameData&& gameData);
void HandlePacket(Packets::NetworkStrings&& networkStrings);
void HandlePacket(Packets::PlayerJoin&& playerJoin);
void HandlePacket(Packets::PlayerLeave&& playerLeave);
void HandlePacket(Packets::PlayerNameUpdate&& playerNameUpdate);
void HandlePacket(Packets::UpdateRootEnvironment&& playerEnv);

void LoadScripts(bool isReloading = false);

Expand All @@ -94,14 +93,14 @@ namespace tsom
private:
inline PlayerInfo* FetchPlayerInfo(PlayerIndex playerIndex);
inline const PlayerInfo* FetchPlayerInfo(PlayerIndex playerIndex) const;
void HandleEntityCreation(Packets::Helper::EntityData&& entityData);
void SetupEntity(entt::handle entity, Packets::Helper::PlayerControlledData&& entityData);

struct EnvironmentData
{
Nz::Bitset<Nz::UInt64> entities;
entt::handle rootEntity;
entt::handle visualRootEntity;
EnvironmentTransform transform;
GravityController* gravityController;
};

Expand All @@ -127,7 +126,6 @@ namespace tsom
ClientBlockLibrary& m_blockLibrary;
Nz::UInt16 m_lastTickIndex;
Nz::UInt16 m_ownPlayerIndex;
Packets::Helper::EnvironmentId m_currentEnvironmentIndex;
ScriptingContext m_scriptingContext;
EntityRegistry m_entityRegistry;
InputIndex m_lastInputIndex;
Expand Down
1 change: 1 addition & 0 deletions include/CommonLib/CharacterController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace tsom
inline const Nz::Quaternionf& GetCharacterRotation() const;
inline const PlayerInputs& GetInputs() const;
inline const Nz::Quaternionf& GetReferenceRotation() const;
inline const std::shared_ptr<ShipController>& GetShipController() const;

inline bool IsFlying() const;

Expand Down
5 changes: 5 additions & 0 deletions include/CommonLib/CharacterController.inl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace tsom
return m_referenceRotation;
}

inline const std::shared_ptr<ShipController>& CharacterController::GetShipController() const
{
return m_shipController;
}

inline bool CharacterController::IsFlying() const
{
return m_isFlying;
Expand Down
3 changes: 1 addition & 2 deletions include/CommonLib/Protocol/PacketList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TSOM_NETWORK_PACKET(EntityProcedureCall)
TSOM_NETWORK_PACKET(EntityPropertyUpdate)
TSOM_NETWORK_PACKET(EnvironmentCreate)
TSOM_NETWORK_PACKET(EnvironmentDestroy)
TSOM_NETWORK_PACKET(EnvironmentUpdate)
TSOM_NETWORK_PACKET(EnvironmentsUpdateOwner)
TSOM_NETWORK_PACKET(ExitShipControl)
TSOM_NETWORK_PACKET(GameData)
TSOM_NETWORK_PACKET(Interact)
Expand All @@ -43,7 +43,6 @@ TSOM_NETWORK_PACKET(PlayerLeave)
TSOM_NETWORK_PACKET(PlayerJoin)
TSOM_NETWORK_PACKET(PlayerNameUpdate)
TSOM_NETWORK_PACKET(SendChatMessage)
TSOM_NETWORK_PACKET(UpdateRootEnvironment)
TSOM_NETWORK_PACKET_LAST(UpdatePlayerInputs)

#undef TSOM_NETWORK_PACKET
Expand Down
46 changes: 24 additions & 22 deletions include/CommonLib/Protocol/Packets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <CommonLib/Protocol/SecuredString.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <NazaraUtils/FixedVector.hpp>
#include <NazaraUtils/Result.hpp>
#include <NazaraUtils/TypeList.hpp>

Expand Down Expand Up @@ -73,13 +74,24 @@ namespace tsom
PlayerIndex controllingPlayerId;
};

struct EntityData
{
EnvironmentId environmentId;
EntityId entityId;
EntityState initialStates;
std::optional<PlayerControlledData> playerControlled;
std::vector<EntityProperty> properties;
CompressedUnsigned<Nz::UInt32> entityClass;
};

struct VoxelLocation
{
Nz::UInt8 x;
Nz::UInt8 y;
Nz::UInt8 z;
};

TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EntityData& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EntityState& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EnvironmentTransform& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, PlayerControlledData& data);
Expand Down Expand Up @@ -174,18 +186,8 @@ namespace tsom

struct EntitiesCreation
{
struct EntityData
{
Helper::EnvironmentId environmentId;
Helper::EntityId entityId;
Helper::EntityState initialStates;
std::optional<Helper::PlayerControlledData> playerControlled;
std::vector<EntityProperty> properties;
CompressedUnsigned<Nz::UInt32> entityClass;
};

Nz::UInt16 tickIndex;
std::vector<EntityData> entities;
std::vector<Helper::EntityData> entities;
};

struct EntitiesDelete
Expand Down Expand Up @@ -242,7 +244,8 @@ namespace tsom
{
Nz::UInt16 tickIndex;
Helper::EnvironmentId id;
EnvironmentTransform transform;
Helper::EntityId ownerEntity;
std::vector<Helper::EntityData> entities;
};

struct EnvironmentDestroy
Expand All @@ -251,11 +254,16 @@ namespace tsom
Helper::EnvironmentId id;
};

struct EnvironmentUpdate
struct EnvironmentsUpdateOwner
{
struct OwnerUpdate
{
Helper::EnvironmentId environment;
Helper::EntityId newOwner;
};

Nz::UInt16 tickIndex;
Helper::EnvironmentId id;
EnvironmentTransform transform;
Nz::HybridVector<OwnerUpdate, 3> ownerUpdates;
};

struct ExitShipControl
Expand Down Expand Up @@ -322,11 +330,6 @@ namespace tsom
SecuredString<Constants::ChatMaxPlayerMessageLength> message;
};

struct UpdateRootEnvironment
{
Helper::EnvironmentId newRootEnv;
};

struct UpdatePlayerInputs
{
PlayerInputs inputs;
Expand All @@ -348,7 +351,7 @@ namespace tsom
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EntityPropertyUpdate& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EnvironmentCreate& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EnvironmentDestroy& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EnvironmentUpdate& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, EnvironmentsUpdateOwner& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, ExitShipControl& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, GameData& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, Interact& data);
Expand All @@ -359,7 +362,6 @@ namespace tsom
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, PlayerJoin& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, PlayerNameUpdate& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, SendChatMessage& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, UpdateRootEnvironment& data);
TSOM_COMMONLIB_API void Serialize(PacketSerializer& serializer, UpdatePlayerInputs& data);
}
}
Expand Down
5 changes: 4 additions & 1 deletion include/CommonLib/ShipController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ namespace tsom
~ShipController() = default;

inline const Nz::Quaternionf& GetReferenceRotation() const;
inline entt::handle GetShipEntity() const;

void PostSimulate(CharacterController& characterOwner, float elapsedTime);
void PostSimulate(CharacterController& character, float elapsedTime);
void PreSimulate(CharacterController& character, float elapsedTime);

inline void UpdateShipEntity(entt::handle entity);

ShipController& operator=(const ShipController&) = delete;
ShipController& operator=(ShipController&&) = delete;

Expand Down
10 changes: 10 additions & 0 deletions include/CommonLib/ShipController.inl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ namespace tsom
{
return m_rotation;
}

inline entt::handle tsom::ShipController::GetShipEntity() const
{
return m_entity;
}

inline void ShipController::UpdateShipEntity(entt::handle entity)
{
m_entity = entity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace tsom
std::shared_ptr<Nz::Collider3D> entryTrigger;
Nz::Boxf aabb; //< in trigger space, serves as a cheap test before testing entryTrigger
ServerEnvironment* targetEnvironment;
bool updateRoot = false;
};
}

Expand Down
3 changes: 1 addition & 2 deletions include/ServerLib/Components/EnvironmentProxyComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ namespace tsom

struct EnvironmentProxyComponent
{
ServerEnvironment* fromEnv;
ServerEnvironment* toEnv;
ServerEnvironment* targetEnvironment;
};
}

Expand Down
2 changes: 2 additions & 0 deletions include/ServerLib/Components/NetworkedComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace tsom
NetworkedComponent(NetworkedComponent&&) = default;
~NetworkedComponent() = default;

inline void DontSendCreationSignal();

inline bool ShouldSignalCreation() const;

NetworkedComponent& operator=(const NetworkedComponent&) = delete;
Expand Down
5 changes: 5 additions & 0 deletions include/ServerLib/Components/NetworkedComponent.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace tsom
{
}

inline void NetworkedComponent::DontSendCreationSignal()
{
m_shouldSignalCreation = false;
}

inline bool NetworkedComponent::ShouldSignalCreation() const
{
return m_shouldSignalCreation;
Expand Down
26 changes: 26 additions & 0 deletions include/ServerLib/Components/ServerEnvironmentSwitchComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq ([email protected])
// 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_SERVERLIB_COMPONENTS_SERVERENVIRONMENTSWITCHCOMPONENT_HPP
#define TSOM_SERVERLIB_COMPONENTS_SERVERENVIRONMENTSWITCHCOMPONENT_HPP

#include <CommonLib/EnvironmentTransform.hpp>
#include <entt/fwd.hpp>
#include <functional>

namespace tsom
{
class ServerEnvironment;

struct ServerEnvironmentSwitchComponent
{
std::function<void(entt::handle entity, ServerEnvironment* newEnvironment, const EnvironmentTransform& /*environmentTransform*/)> handleEnvironmentSwitch;

NazaraSignal(OnEntitySwitchedEnvironment, entt::handle /*previousEntity*/, entt::handle /*newEntity*/, ServerEnvironment* /*newEnvironment*/, const EnvironmentTransform& /*environmentTransform*/);
};
}

#endif // TSOM_SERVERLIB_COMPONENTS_SERVERENVIRONMENTSWITCHCOMPONENT_HPP
24 changes: 24 additions & 0 deletions include/ServerLib/Components/ShipExteriorComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq ([email protected])
// 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_SERVERLIB_COMPONENTS_SHIPEXTERIORCOMPONENT_HPP
#define TSOM_SERVERLIB_COMPONENTS_SHIPEXTERIORCOMPONENT_HPP

#include <ServerLib/ServerShipEnvironment.hpp>

namespace tsom
{
class ServerShipEnvironment;

struct ShipExteriorComponent
{
ServerShipEnvironment* ownerShip;

NazaraSlot(ServerShipEnvironment, OnInteriorColliderUpdated, onInteriorColliderUpdated);
};
}

#endif // TSOM_SERVERLIB_COMPONENTS_SHIPEXTERIORCOMPONENT_HPP
44 changes: 44 additions & 0 deletions include/ServerLib/Entities/ServerClassLibrary.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq ([email protected])
// 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_SERVERLIB_ENTITIES_SERVERCLASSLIBRARY_HPP
#define TSOM_SERVERLIB_ENTITIES_SERVERCLASSLIBRARY_HPP

#include <ServerLib/Export.hpp>
#include <CommonLib/Entities/EntityClassLibrary.hpp>

namespace Nz
{
class ApplicationBase;
}

namespace tsom
{
class BlockLibrary;
class ChunkContainer;
class ChunkEntities;

class TSOM_SERVERLIB_API ServerClassLibrary : public EntityClassLibrary
{
public:
inline ServerClassLibrary(Nz::ApplicationBase& app);
ServerClassLibrary(const ServerClassLibrary&) = delete;
ServerClassLibrary(ServerClassLibrary&&) = delete;
~ServerClassLibrary() = default;

void Register(EntityRegistry& registry) override;

ServerClassLibrary& operator=(const ServerClassLibrary&) = delete;
ServerClassLibrary& operator=(ServerClassLibrary&&) = delete;

protected:
Nz::ApplicationBase& m_app;
};
}

#include <ServerLib/Entities/ServerClassLibrary.inl>

#endif // TSOM_SERVERLIB_ENTITIES_SERVERCLASSLIBRARY_HPP
11 changes: 11 additions & 0 deletions include/ServerLib/Entities/ServerClassLibrary.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq ([email protected])
// This file is part of the "This Space Of Mine" project
// For conditions of distribution and use, see copyright notice in LICENSE

namespace tsom
{
inline ServerClassLibrary::ServerClassLibrary(Nz::ApplicationBase& app) :
m_app(app)
{
}
}
Loading

0 comments on commit de8cc66

Please sign in to comment.