3
0

NetworkTime.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Source/NetworkTime/NetworkTime.h>
  9. #include <Multiplayer/IMultiplayer.h>
  10. #include <Multiplayer/Components/NetBindComponent.h>
  11. #include <Multiplayer/Components/NetworkTransformComponent.h>
  12. #include <AzCore/Math/ShapeIntersection.h>
  13. #include <AzFramework/Visibility/IVisibilitySystem.h>
  14. #include <AzFramework/Visibility/EntityBoundsUnionBus.h>
  15. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  16. namespace Multiplayer
  17. {
  18. AZ_CVAR(float, sv_RewindVolumeExtrudeDistance, 50.0f, nullptr, AZ::ConsoleFunctorFlags::Null, "The amount to increase rewind volume checks to account for fast moving entities");
  19. AZ_CVAR(bool, bg_RewindDebugDraw, false, nullptr, AZ::ConsoleFunctorFlags::Null, "If true enables debug draw of rewind operations");
  20. NetworkTime::NetworkTime()
  21. {
  22. AZ::Interface<INetworkTime>::Register(this);
  23. }
  24. NetworkTime::~NetworkTime()
  25. {
  26. AZ::Interface<INetworkTime>::Unregister(this);
  27. }
  28. bool NetworkTime::IsTimeRewound() const
  29. {
  30. return m_rewindingConnectionId != AzNetworking::InvalidConnectionId;
  31. }
  32. HostFrameId NetworkTime::GetHostFrameId() const
  33. {
  34. return m_hostFrameId;
  35. }
  36. HostFrameId NetworkTime::GetUnalteredHostFrameId() const
  37. {
  38. return m_unalteredFrameId;
  39. }
  40. void NetworkTime::IncrementHostFrameId()
  41. {
  42. AZ_Assert(!IsTimeRewound(), "Incrementing the global application frameId is unsupported under a rewound time scope");
  43. ++m_unalteredFrameId;
  44. m_hostFrameId = m_unalteredFrameId;
  45. m_hostTimeMs = AZ::GetElapsedTimeMs();
  46. }
  47. AZ::TimeMs NetworkTime::GetHostTimeMs() const
  48. {
  49. return m_hostTimeMs;
  50. }
  51. float NetworkTime::GetHostBlendFactor() const
  52. {
  53. return m_hostBlendFactor;
  54. }
  55. AzNetworking::ConnectionId NetworkTime::GetRewindingConnectionId() const
  56. {
  57. return m_rewindingConnectionId;
  58. }
  59. void NetworkTime::ForceSetTime(HostFrameId frameId, AZ::TimeMs timeMs)
  60. {
  61. AZ_Assert(!IsTimeRewound(), "Forcibly setting network time is unsupported under a rewound time scope");
  62. m_unalteredFrameId = frameId;
  63. m_hostFrameId = frameId;
  64. m_hostTimeMs = timeMs;
  65. m_rewindingConnectionId = AzNetworking::InvalidConnectionId;
  66. }
  67. void NetworkTime::AlterTime(HostFrameId frameId, AZ::TimeMs timeMs, float blendFactor, AzNetworking::ConnectionId rewindConnectionId)
  68. {
  69. m_hostFrameId = frameId;
  70. m_hostTimeMs = timeMs;
  71. m_hostBlendFactor = blendFactor;
  72. m_rewindingConnectionId = rewindConnectionId;
  73. }
  74. void NetworkTime::SyncEntitiesToRewindState(const AZ::Aabb& rewindVolume)
  75. {
  76. if (!IsTimeRewound())
  77. {
  78. // If we're not inside a rewind scope then reset any rewound state and exit
  79. ClearRewoundEntities();
  80. return;
  81. }
  82. // Since the vis system doesn't support rewound queries, first query with an expanded volume to catch any fast moving entities
  83. const AZ::Aabb expandedVolume = rewindVolume.GetExpanded(AZ::Vector3(sv_RewindVolumeExtrudeDistance));
  84. AzFramework::DebugDisplayRequests* debugDisplay = nullptr;
  85. if (bg_RewindDebugDraw)
  86. {
  87. AzFramework::DebugDisplayRequestBus::BusPtr debugDisplayBus;
  88. AzFramework::DebugDisplayRequestBus::Bind(debugDisplayBus, AzFramework::g_defaultSceneEntityDebugDisplayId);
  89. debugDisplay = AzFramework::DebugDisplayRequestBus::FindFirstHandler(debugDisplayBus);
  90. }
  91. if (debugDisplay)
  92. {
  93. debugDisplay->SetColor(AZ::Colors::Red);
  94. debugDisplay->DrawWireBox(expandedVolume.GetMin(), expandedVolume.GetMax());
  95. }
  96. NetworkEntityTracker* networkEntityTracker = GetNetworkEntityTracker();
  97. AzFramework::IEntityBoundsUnion* entityBoundsUnion = AZ::Interface<AzFramework::IEntityBoundsUnion>::Get();
  98. AZ::Interface<AzFramework::IVisibilitySystem>::Get()->GetDefaultVisibilityScene()->Enumerate(expandedVolume,
  99. [this, debugDisplay, networkEntityTracker, entityBoundsUnion, rewindVolume](const AzFramework::IVisibilityScene::NodeData& nodeData)
  100. {
  101. m_rewoundEntities.reserve(m_rewoundEntities.size() + nodeData.m_entries.size());
  102. for (AzFramework::VisibilityEntry* visEntry : nodeData.m_entries)
  103. {
  104. if (visEntry->m_typeFlags & AzFramework::VisibilityEntry::TypeFlags::TYPE_Entity)
  105. {
  106. AZ::Entity* entity = static_cast<AZ::Entity*>(visEntry->m_userData);
  107. NetworkEntityHandle entityHandle(entity, networkEntityTracker);
  108. if (entityHandle.GetNetBindComponent() != nullptr)
  109. {
  110. const AZ::Aabb currentBounds = entityBoundsUnion->GetEntityWorldBoundsUnion(entity->GetId());
  111. const AZ::Vector3 currentCenter = currentBounds.GetCenter();
  112. NetworkTransformComponent* networkTransform = entity->template FindComponent<NetworkTransformComponent>();
  113. if (debugDisplay)
  114. {
  115. debugDisplay->SetColor(AZ::Colors::White);
  116. debugDisplay->DrawWireBox(currentBounds.GetMin(), currentBounds.GetMax());
  117. }
  118. if (networkTransform != nullptr)
  119. {
  120. // Get the rewound position for target host frame ID plus the one preceding it for potential lerp
  121. AZ::Vector3 rewindCenter = networkTransform->GetTranslation();
  122. const AZ::Vector3 rewindCenterPrevious = networkTransform->GetTranslationPrevious();
  123. const float blendFactor = GetNetworkTime()->GetHostBlendFactor();
  124. if (!AZ::IsClose(blendFactor, 1.0f) && !rewindCenter.IsClose(rewindCenterPrevious))
  125. {
  126. // If we have a blend factor, lerp the translation for accuracy
  127. rewindCenter = rewindCenterPrevious.Lerp(rewindCenter, blendFactor);
  128. }
  129. const AZ::Vector3 rewindOffset = rewindCenter - currentCenter; // Compute offset between rewound and current positions
  130. const AZ::Aabb rewoundAabb = currentBounds.GetTranslated(rewindOffset); // Apply offset to the entity aabb
  131. if (debugDisplay)
  132. {
  133. debugDisplay->SetColor(AZ::Colors::Grey);
  134. debugDisplay->DrawWireBox(rewoundAabb.GetMin(), rewoundAabb.GetMax());
  135. }
  136. if (AZ::ShapeIntersection::Overlaps(rewoundAabb, rewindVolume)) // Validate the rewound aabb intersects our rewind volume
  137. {
  138. m_rewoundEntities.push_back(entityHandle);
  139. entityHandle.GetNetBindComponent()->NotifySyncRewindState();
  140. }
  141. }
  142. }
  143. }
  144. }
  145. });
  146. }
  147. void NetworkTime::ClearRewoundEntities()
  148. {
  149. AZ_Assert(!IsTimeRewound(), "Cannot clear rewound entity state while still within scoped rewind");
  150. for (NetworkEntityHandle entityHandle : m_rewoundEntities)
  151. {
  152. if (NetBindComponent* netBindComponent = entityHandle.GetNetBindComponent())
  153. {
  154. netBindComponent->NotifySyncRewindState();
  155. }
  156. }
  157. m_rewoundEntities.clear();
  158. }
  159. }