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
@@ -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);