Local prediction player controller is now functional

This commit is contained in:
karlberg
2021-05-06 17:23:03 -07:00
parent 751d13dd7b
commit e7f0bc9ee2
12 changed files with 176 additions and 133 deletions
@@ -12,14 +12,15 @@
<Include File="Include/MultiplayerTypes.h"/>
<Include File="Source/NetworkInput/NetworkInput.h"/>
<Include File="Source/NetworkInput/NetworkInputArray.h"/>
<Include File="Source/NetworkInput/NetworkInputHistory.h"/>
<Include File="Source/NetworkInput/NetworkInputVector.h"/>
<Include File="Source/NetworkInput/NetworkInputMigrationVector.h"/>
<Include File="AzNetworking/DataStructures/ByteBuffer.h"/>
<NetworkProperty Type="Multiplayer::ClientInputId" Name="LastInputId" Init="Multiplayer::ClientInputId{ 0 }" ReplicateFrom="Authority" ReplicateTo="Authority" IsRewindable="false" IsPredictable="false" IsPublic="false" Container="Object" ExposeToEditor="false" GenerateEventBindings="false" />
<RemoteProcedure Name="SendClientInput" InvokeFrom="Autonomous" HandleOn="Authority" IsPublic="true" IsReliable="false" Description="Client to server move / input RPC">
<Param Type="Multiplayer::NetworkInputVector" Name="inputArray" />
<Param Type="Multiplayer::NetworkInputArray" Name="inputArray" />
<Param Type="AZ::HashValue64" Name="stateHash" />
<Param Type="AzNetworking::PacketEncodingBuffer" Name="clientState" Description="This is for debugging desyncs only; release games should not populate this parameter" />
</RemoteProcedure>
@@ -30,6 +31,6 @@
</RemoteProcedure>
<RemoteProcedure Name="SendMigrateClientInput" InvokeFrom="Autonomous" HandleOn="Authority" IsPublic="true" IsReliable="false" Description="Client to server migrate move / input RPC">
<Param Type="Multiplayer::MigrateNetworkInputVector" Name="inputArray" />
<Param Type="Multiplayer::NetworkInputMigrationVector" Name="inputArray" />
</RemoteProcedure>
</Component>
@@ -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<float>(static_cast<AZ::TimeMs>(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)
{
@@ -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
@@ -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<NetComponentId, MultiplayerComponent*> m_multiplayerComponentMap;
@@ -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<IMultiplayer>::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);
@@ -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;
@@ -10,21 +10,21 @@
*
*/
#include <Source/NetworkInput/NetworkInputVector.h>
#include <Source/NetworkInput/NetworkInputArray.h>
#include <Include/INetworkEntityManager.h>
#include <AzNetworking/Serialization/ISerializer.h>
#include <AzNetworking/Serialization/DeltaSerializer.h>
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<uint32_t>(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<uint32_t>(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");
}
}
@@ -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 <Source/NetworkInput/NetworkInput.h>
#include <Include/NetworkEntityHandle.h>
#include <AzCore/std/containers/array.h>
#include <AzCore/std/containers/fixed_vector.h>
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<Wrapper, MaxElements> m_inputs;
ClientInputId m_previousInputId;
};
}
@@ -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 <Source/NetworkInput/NetworkInputMigrationVector.h>
#include <Include/INetworkEntityManager.h>
#include <AzNetworking/Serialization/ISerializer.h>
namespace Multiplayer
{
NetworkInputMigrationVector::NetworkInputMigrationVector()
: m_owner()
{
;
}
NetworkInputMigrationVector::NetworkInputMigrationVector(const ConstNetworkEntityHandle& entityHandle)
: m_owner(entityHandle)
{
;
}
uint32_t NetworkInputMigrationVector::GetSize() const
{
return aznumeric_cast<uint32_t>(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<uint32_t>(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");
}
}
@@ -14,53 +14,21 @@
#include <Source/NetworkInput/NetworkInput.h>
#include <Include/NetworkEntityHandle.h>
#include <AzCore/std/containers/array.h>
#include <AzCore/std/containers/fixed_vector.h>
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<Wrapper, MaxElements> 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);
@@ -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<AZ::Entity*>(visEntry->m_userData);
NetBindComponent* entryNetBindComponent = entity->template FindComponent<NetBindComponent>();
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);
}
@@ -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