diff --git a/Gems/Multiplayer/Code/Source/AutoGen/LocalPredictionPlayerInputComponent.AutoComponent.xml b/Gems/Multiplayer/Code/Source/AutoGen/LocalPredictionPlayerInputComponent.AutoComponent.xml index 45dfc43e49..65e2a8884e 100644 --- a/Gems/Multiplayer/Code/Source/AutoGen/LocalPredictionPlayerInputComponent.AutoComponent.xml +++ b/Gems/Multiplayer/Code/Source/AutoGen/LocalPredictionPlayerInputComponent.AutoComponent.xml @@ -12,14 +12,15 @@ + - + - + @@ -30,6 +31,6 @@ - + diff --git a/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.cpp b/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.cpp index 56192c96d7..5a8fbfbe33 100644 --- a/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.cpp +++ b/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.cpp @@ -106,7 +106,7 @@ namespace Multiplayer void LocalPredictionPlayerInputComponentController::HandleSendClientInput ( AzNetworking::IConnection* invokingConnection, - const Multiplayer::NetworkInputVector& inputArray, + const Multiplayer::NetworkInputArray& inputArray, const AZ::HashValue64& stateHash, [[maybe_unused]] const AzNetworking::PacketEncodingBuffer& clientState ) @@ -133,7 +133,7 @@ namespace Multiplayer // Figure out which index from the input array we want // we start at the oldest input that has not been processed int32_t inputArrayIndex = -1; - for (int32_t i = NetworkInputVector::MaxElements - 1; i >= 0; --i) + for (int32_t i = NetworkInputArray::MaxElements - 1; i >= 0; --i) { // Find an input that is newer than the last one we processed if (m_lastInputReceived[i].GetClientInputId() > GetLastInputId()) @@ -286,7 +286,7 @@ namespace Multiplayer void LocalPredictionPlayerInputComponentController::HandleSendMigrateClientInput ( AzNetworking::IConnection* invokingConnection, - const Multiplayer::MigrateNetworkInputVector& inputArray + const Multiplayer::NetworkInputMigrationVector& inputArray ) { if (!m_allowMigrateClientInput) @@ -308,7 +308,7 @@ namespace Multiplayer const float clientInputRateSec = static_cast(static_cast(cl_InputRateMs)) / 1000.0; // Copy array so we can modify input ids - MigrateNetworkInputVector inputArrayCopy = inputArray; + NetworkInputMigrationVector inputArrayCopy = inputArray; for (uint32_t i = 0; i < inputArrayCopy.GetSize(); ++i) { @@ -447,7 +447,7 @@ namespace Multiplayer void LocalPredictionPlayerInputComponentController::OnMigrateEnd() { - MigrateNetworkInputVector inputArray; + NetworkInputMigrationVector inputArray; // Roll up all inputs that the new server doesn't have and send them now for (AZStd::size_t i = 0; i < m_inputHistory.Size(); ++i) @@ -499,7 +499,7 @@ namespace Multiplayer m_moveAccumulator -= inputRate; ++m_clientInputId; - NetworkInputVector inputArray(GetEntityHandle()); + NetworkInputArray inputArray(GetEntityHandle()); NetworkInput& input = inputArray[0]; input.SetClientInputId(m_clientInputId); @@ -544,7 +544,7 @@ namespace Multiplayer // Form the rest of the input array using the n most recent elements in the history buffer // NOTE: inputArray[0] has already been initialized hence start at i = 1 - for (uint32_t i = 1; i < NetworkInputVector::MaxElements; ++i) + for (uint32_t i = 1; i < NetworkInputArray::MaxElements; ++i) { if (i < inputHistorySize) { diff --git a/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.h b/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.h index 0a4e574d65..feb79c0fa2 100644 --- a/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.h +++ b/Gems/Multiplayer/Code/Source/Components/LocalPredictionPlayerInputComponent.h @@ -44,7 +44,7 @@ namespace Multiplayer void HandleSendClientInput ( AzNetworking::IConnection* invokingConnection, - const Multiplayer::NetworkInputVector& inputArray, + const Multiplayer::NetworkInputArray& inputArray, const AZ::HashValue64& stateHash, const AzNetworking::PacketEncodingBuffer& clientState ) override; @@ -52,7 +52,7 @@ namespace Multiplayer void HandleSendMigrateClientInput ( AzNetworking::IConnection* invokingConnection, - const Multiplayer::MigrateNetworkInputVector& inputArray + const Multiplayer::NetworkInputMigrationVector& inputArray ) override; void HandleSendClientInputCorrection @@ -87,7 +87,7 @@ namespace Multiplayer NetworkInputHistory m_inputHistory; // Anti-cheat accumulator for clients who purposely mess with their clock rate - NetworkInputVector m_lastInputReceived; + NetworkInputArray m_lastInputReceived; AZ::ScheduledEvent m_autonomousUpdateEvent; // Drives autonomous input collection AZ::ScheduledEvent m_updateBankedTimeEvent; // Drives authority bank time updates diff --git a/Gems/Multiplayer/Code/Source/Components/NetBindComponent.h b/Gems/Multiplayer/Code/Source/Components/NetBindComponent.h index 4a4c4d0549..0885e44aa4 100644 --- a/Gems/Multiplayer/Code/Source/Components/NetBindComponent.h +++ b/Gems/Multiplayer/Code/Source/Components/NetBindComponent.h @@ -126,7 +126,7 @@ namespace Multiplayer ReplicationRecord m_currentRecord = NetEntityRole::InvalidRole; ReplicationRecord m_totalRecord = NetEntityRole::InvalidRole; - ReplicationRecord m_predictableRecord = NetEntityRole::InvalidRole; + ReplicationRecord m_predictableRecord = NetEntityRole::Autonomous; ReplicationRecord m_localNotificationRecord = NetEntityRole::InvalidRole; PrefabEntityId m_prefabEntityId; AZStd::unordered_map m_multiplayerComponentMap; diff --git a/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp index 506581b5e4..aec8d8520e 100644 --- a/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp +++ b/Gems/Multiplayer/Code/Source/Debug/MultiplayerDebugSystemComponent.cpp @@ -235,7 +235,7 @@ namespace Multiplayer if (m_displayMultiplayerStats) { - if (ImGui::Begin("Multiplayer Stats", &m_displayMultiplayerStats, ImGuiWindowFlags_HorizontalScrollbar)) + if (ImGui::Begin("Multiplayer Stats", &m_displayMultiplayerStats, ImGuiWindowFlags_None)) { IMultiplayer* multiplayer = AZ::Interface::Get(); const Multiplayer::MultiplayerStats& stats = multiplayer->GetStats(); @@ -254,7 +254,7 @@ namespace Multiplayer if (ImGui::BeginTable("", 5, flags)) { // The first column will use the default _WidthStretch when ScrollX is Off and _WidthFixed when ScrollX is On - ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_NoHide, TEXT_BASE_WIDTH * 36.0f); + ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthStretch); ImGui::TableSetupColumn("Total Calls", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 12.0f); ImGui::TableSetupColumn("Total Bytes", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 12.0f); ImGui::TableSetupColumn("Calls/Sec", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 12.0f); diff --git a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInput.h b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInput.h index 2c33d1ccf0..b2b0fa12c6 100644 --- a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInput.h +++ b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInput.h @@ -29,8 +29,8 @@ namespace Multiplayer public: //! Intentionally restrict instancing of these objects to associated containers classes only //! This is a mechanism used to restrict calling autonomous client predicted setter functions to the ProcessInput call chain only - friend class NetworkInputVector; - friend class MigrateNetworkInputVector; + friend class NetworkInputArray; + friend class NetworkInputMigrationVector; friend class NetworkInputHistory; friend class NetworkInputChild; diff --git a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputVector.cpp b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputArray.cpp similarity index 54% rename from Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputVector.cpp rename to Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputArray.cpp index 6d284a6835..0f5a0d7c0c 100644 --- a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputVector.cpp +++ b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputArray.cpp @@ -10,21 +10,21 @@ * */ -#include +#include #include #include #include namespace Multiplayer { - NetworkInputVector::NetworkInputVector() + NetworkInputArray::NetworkInputArray() : m_owner() , m_inputs() { ; } - NetworkInputVector::NetworkInputVector(const ConstNetworkEntityHandle& entityHandle) + NetworkInputArray::NetworkInputArray(const ConstNetworkEntityHandle& entityHandle) : m_owner(entityHandle) , m_inputs() { @@ -38,27 +38,27 @@ namespace Multiplayer } } - NetworkInput& NetworkInputVector::operator[](uint32_t index) + NetworkInput& NetworkInputArray::operator[](uint32_t index) { return m_inputs[index].m_networkInput; } - const NetworkInput& NetworkInputVector::operator[](uint32_t index) const + const NetworkInput& NetworkInputArray::operator[](uint32_t index) const { return m_inputs[index].m_networkInput; } - void NetworkInputVector::SetPreviousInputId(ClientInputId previousInputId) + void NetworkInputArray::SetPreviousInputId(ClientInputId previousInputId) { m_previousInputId = previousInputId; } - ClientInputId NetworkInputVector::GetPreviousInputId() const + ClientInputId NetworkInputArray::GetPreviousInputId() const { return m_previousInputId; } - bool NetworkInputVector::Serialize(AzNetworking::ISerializer& serializer) + bool NetworkInputArray::Serialize(AzNetworking::ISerializer& serializer) { // Always serialize the full first element if (!m_inputs[0].m_networkInput.Serialize(serializer)) @@ -105,67 +105,4 @@ namespace Multiplayer serializer.Serialize(m_previousInputId, "PreviousInputId"); return true; } - - - MigrateNetworkInputVector::MigrateNetworkInputVector() - : m_owner() - { - ; - } - - MigrateNetworkInputVector::MigrateNetworkInputVector(const ConstNetworkEntityHandle& entityHandle) - : m_owner(entityHandle) - { - ; - } - - uint32_t MigrateNetworkInputVector::GetSize() const - { - return aznumeric_cast(m_inputs.size()); - } - - NetworkInput& MigrateNetworkInputVector::operator[](uint32_t index) - { - return m_inputs[index].m_networkInput; - } - - const NetworkInput& MigrateNetworkInputVector::operator[](uint32_t index) const - { - return m_inputs[index].m_networkInput; - } - - bool MigrateNetworkInputVector::PushBack(const NetworkInput& networkInput) - { - if (m_inputs.size() < m_inputs.capacity()) - { - m_inputs.push_back(networkInput); - return true; - } - return false; - } - - bool MigrateNetworkInputVector::Serialize(AzNetworking::ISerializer& serializer) - { - NetEntityId ownerId = m_owner.GetNetEntityId(); - serializer.Serialize(ownerId, "OwnerId"); - - uint32_t inputCount = aznumeric_cast(m_inputs.size()); - serializer.Serialize(inputCount, "InputCount"); - - if (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject) - { - // make sure all the possible NetworkInputs get attached prior to serialization, this double sends the size, but this message is only sent on server migration - m_inputs.resize(inputCount); - m_owner = GetNetworkEntityManager()->GetEntity(ownerId); - NetBindComponent* netBindComponent = m_owner.GetNetBindComponent(); - if (netBindComponent) - { - for (uint32_t i = 0; i < m_inputs.size(); ++i) - { - m_inputs[i].m_networkInput.AttachNetBindComponent(netBindComponent); - } - } - } - return serializer.Serialize(m_inputs, "Inputs"); - } } diff --git a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputArray.h b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputArray.h new file mode 100644 index 0000000000..504992fecb --- /dev/null +++ b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputArray.h @@ -0,0 +1,54 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#pragma once + +#include +#include +#include +#include + +namespace Multiplayer +{ + //! @class NetworkInputArray + //! @brief An array of network inputs. Used to mitigate loss of input packets on the server. Compresses subsequent elements. + class NetworkInputArray final + { + public: + static constexpr uint32_t MaxElements = 8; // Never try to replicate a list larger than this amount + + NetworkInputArray(); + NetworkInputArray(const ConstNetworkEntityHandle& entityHandle); + ~NetworkInputArray() = default; + + NetworkInput& operator[](uint32_t index); + const NetworkInput& operator[](uint32_t index) const; + + void SetPreviousInputId(ClientInputId previousInputId); + ClientInputId GetPreviousInputId() const; + + bool Serialize(AzNetworking::ISerializer& serializer); + + private: + + struct Wrapper // Strictly a workaround to deal with the private constructor of NetworkInput + { + Wrapper() : m_networkInput() {} + Wrapper(const NetworkInput& networkInput) : m_networkInput(networkInput) {} + NetworkInput m_networkInput; + }; + + ConstNetworkEntityHandle m_owner; + AZStd::array m_inputs; + ClientInputId m_previousInputId; + }; +} diff --git a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputMigrationVector.cpp b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputMigrationVector.cpp new file mode 100644 index 0000000000..dee72156ed --- /dev/null +++ b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputMigrationVector.cpp @@ -0,0 +1,80 @@ +/* +* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or +* its licensors. +* +* For complete copyright and license terms please see the LICENSE at the root of this +* distribution (the "License"). All use of this software is governed by the License, +* or, if provided, by the license below or the license accompanying this file. Do not +* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +*/ + +#include +#include +#include + +namespace Multiplayer +{ + NetworkInputMigrationVector::NetworkInputMigrationVector() + : m_owner() + { + ; + } + + NetworkInputMigrationVector::NetworkInputMigrationVector(const ConstNetworkEntityHandle& entityHandle) + : m_owner(entityHandle) + { + ; + } + + uint32_t NetworkInputMigrationVector::GetSize() const + { + return aznumeric_cast(m_inputs.size()); + } + + NetworkInput& NetworkInputMigrationVector::operator[](uint32_t index) + { + return m_inputs[index].m_networkInput; + } + + const NetworkInput& NetworkInputMigrationVector::operator[](uint32_t index) const + { + return m_inputs[index].m_networkInput; + } + + bool NetworkInputMigrationVector::PushBack(const NetworkInput& networkInput) + { + if (m_inputs.size() < m_inputs.capacity()) + { + m_inputs.push_back(networkInput); + return true; + } + return false; + } + + bool NetworkInputMigrationVector::Serialize(AzNetworking::ISerializer& serializer) + { + NetEntityId ownerId = m_owner.GetNetEntityId(); + serializer.Serialize(ownerId, "OwnerId"); + + uint32_t inputCount = aznumeric_cast(m_inputs.size()); + serializer.Serialize(inputCount, "InputCount"); + + if (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject) + { + // make sure all the possible NetworkInputs get attached prior to serialization, this double sends the size, but this message is only sent on server migration + m_inputs.resize(inputCount); + m_owner = GetNetworkEntityManager()->GetEntity(ownerId); + NetBindComponent* netBindComponent = m_owner.GetNetBindComponent(); + if (netBindComponent) + { + for (uint32_t i = 0; i < m_inputs.size(); ++i) + { + m_inputs[i].m_networkInput.AttachNetBindComponent(netBindComponent); + } + } + } + return serializer.Serialize(m_inputs, "Inputs"); + } +} diff --git a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputVector.h b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputMigrationVector.h similarity index 54% rename from Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputVector.h rename to Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputMigrationVector.h index 91970040c5..c6ea425fec 100644 --- a/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputVector.h +++ b/Gems/Multiplayer/Code/Source/NetworkInput/NetworkInputMigrationVector.h @@ -14,53 +14,21 @@ #include #include +#include #include namespace Multiplayer { - //! @class NetworkInputVector - //! @brief An array of network inputs. Used to mitigate loss of input packets on the server. Compresses subsequent elements. - class NetworkInputVector final - { - public: - static constexpr uint32_t MaxElements = 8; // Never try to replicate a list larger than this amount - - NetworkInputVector(); - NetworkInputVector(const ConstNetworkEntityHandle& entityHandle); - ~NetworkInputVector() = default; - - NetworkInput& operator[](uint32_t index); - const NetworkInput& operator[](uint32_t index) const; - - void SetPreviousInputId(ClientInputId previousInputId); - ClientInputId GetPreviousInputId() const; - - bool Serialize(AzNetworking::ISerializer& serializer); - - private: - - struct Wrapper // Strictly a workaround to deal with the private constructor of NetworkInput - { - Wrapper() : m_networkInput() {} - Wrapper(const NetworkInput& networkInput) : m_networkInput(networkInput) {} - NetworkInput m_networkInput; - }; - - ConstNetworkEntityHandle m_owner; - AZStd::fixed_vector m_inputs; - ClientInputId m_previousInputId; - }; - - //! @class MigrateNetworkInputVector + //! @class NetworkInputMigrationVector //! @brief A variable sized array of input commands, used specifically when migrate a clients inputs. - class MigrateNetworkInputVector final + class NetworkInputMigrationVector final { public: static constexpr uint32_t MaxElements = 90; // Never try to migrate a list larger than this amount, bumped up to handle DTLS connection time - MigrateNetworkInputVector(); - MigrateNetworkInputVector(const ConstNetworkEntityHandle& entityHandle); - virtual ~MigrateNetworkInputVector() = default; + NetworkInputMigrationVector(); + NetworkInputMigrationVector(const ConstNetworkEntityHandle& entityHandle); + virtual ~NetworkInputMigrationVector() = default; uint32_t GetSize() const; NetworkInput& operator[](uint32_t index); diff --git a/Gems/Multiplayer/Code/Source/ReplicationWindows/ServerToClientReplicationWindow.cpp b/Gems/Multiplayer/Code/Source/ReplicationWindows/ServerToClientReplicationWindow.cpp index 22f777a149..abcb1e62ab 100644 --- a/Gems/Multiplayer/Code/Source/ReplicationWindows/ServerToClientReplicationWindow.cpp +++ b/Gems/Multiplayer/Code/Source/ReplicationWindows/ServerToClientReplicationWindow.cpp @@ -157,14 +157,15 @@ namespace Multiplayer //} // We want to find the closest extent to the player and prioritize using that distance - const AZ::Vector3 supportNormal = controlledEntityPosition - visEntry->m_boundingVolume.GetCenter(); - const AZ::Vector3 closestPosition = visEntry->m_boundingVolume.GetSupport(supportNormal); - const float gatherDistanceSquared = controlledEntityPosition.GetDistanceSq(closestPosition); - const float priority = (gatherDistanceSquared > 0.0f) ? 1.0f / gatherDistanceSquared : 0.0f; AZ::Entity* entity = static_cast(visEntry->m_userData); NetBindComponent* entryNetBindComponent = entity->template FindComponent(); if (entryNetBindComponent != nullptr) { + const AZ::Vector3 supportNormal = controlledEntityPosition - visEntry->m_boundingVolume.GetCenter(); + const AZ::Vector3 closestPosition = visEntry->m_boundingVolume.GetSupport(supportNormal); + const float gatherDistanceSquared = controlledEntityPosition.GetDistanceSq(closestPosition); + const float priority = (gatherDistanceSquared > 0.0f) ? 1.0f / gatherDistanceSquared : 0.0f; + NetworkEntityHandle entityHandle(entryNetBindComponent, networkEntityTracker); AddEntityToReplicationSet(entityHandle, priority, gatherDistanceSquared); } diff --git a/Gems/Multiplayer/Code/multiplayer_files.cmake b/Gems/Multiplayer/Code/multiplayer_files.cmake index b340913e47..1f4e57ae43 100644 --- a/Gems/Multiplayer/Code/multiplayer_files.cmake +++ b/Gems/Multiplayer/Code/multiplayer_files.cmake @@ -81,12 +81,14 @@ set(FILES Source/NetworkEntity/NetworkEntityUpdateMessage.h Source/NetworkInput/NetworkInput.cpp Source/NetworkInput/NetworkInput.h + Source/NetworkInput/NetworkInputArray.cpp + Source/NetworkInput/NetworkInputArray.h Source/NetworkInput/NetworkInputChild.cpp Source/NetworkInput/NetworkInputChild.h Source/NetworkInput/NetworkInputHistory.cpp Source/NetworkInput/NetworkInputHistory.h - Source/NetworkInput/NetworkInputVector.cpp - Source/NetworkInput/NetworkInputVector.h + Source/NetworkInput/NetworkInputMigrationVector.cpp + Source/NetworkInput/NetworkInputMigrationVector.h Source/NetworkTime/NetworkTime.cpp Source/NetworkTime/NetworkTime.h Source/NetworkTime/RewindableObject.h