NetworkStressTestComponent.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of
  3. * this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Source/Components/NetworkStressTestComponent.h>
  9. #include <Source/Components/NetworkAiComponent.h>
  10. #include <Source/Components/NetworkPlayerMovementComponent.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <Multiplayer/IMultiplayer.h>
  14. #include <Multiplayer/Components/NetBindComponent.h>
  15. #include <Multiplayer/ConnectionData/IConnectionData.h>
  16. #include <Multiplayer/ReplicationWindows/IReplicationWindow.h>
  17. namespace MultiplayerSample
  18. {
  19. void NetworkStressTestComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  22. if (serializeContext)
  23. {
  24. serializeContext->Class<NetworkStressTestComponent, NetworkStressTestComponentBase>()
  25. ->Version(1);
  26. }
  27. NetworkStressTestComponentBase::Reflect(context);
  28. }
  29. void NetworkStressTestComponent::OnInit()
  30. {
  31. }
  32. NetworkStressTestComponentController::NetworkStressTestComponentController(NetworkStressTestComponent& owner)
  33. : NetworkStressTestComponentControllerBase(owner)
  34. #if AZ_TRAIT_SERVER
  35. , m_autoSpawnTimer([this]() { HandleSpawnAiEntity(); }, AZ::Name("StressTestSpawner Event"))
  36. #endif
  37. {
  38. ;
  39. }
  40. void NetworkStressTestComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  41. {
  42. #ifdef IMGUI_ENABLED
  43. ImGui::ImGuiUpdateListenerBus::Handler::BusConnect();
  44. #endif
  45. auto agentType = AZ::Interface<Multiplayer::IMultiplayer>::Get()->GetAgentType();
  46. switch (agentType)
  47. {
  48. case Multiplayer::MultiplayerAgentType::DedicatedServer:
  49. case Multiplayer::MultiplayerAgentType::ClientServer:
  50. #ifdef IMGUI_ENABLED
  51. m_isServer = true;
  52. #endif
  53. break;
  54. default:
  55. break;
  56. }
  57. #if AZ_TRAIT_SERVER
  58. if (GetAutoSpawnIntervalMs() > AZ::Time::ZeroTimeMs)
  59. {
  60. m_autoSpawnTimer.Enqueue(GetAutoSpawnIntervalMs(), true);
  61. }
  62. #endif
  63. }
  64. void NetworkStressTestComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  65. {
  66. #ifdef IMGUI_ENABLED
  67. ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect();
  68. #endif
  69. }
  70. #if AZ_TRAIT_SERVER
  71. void NetworkStressTestComponentController::HandleSpawnAiEntity()
  72. {
  73. HandleSpawnAIEntity(nullptr, m_fireIntervalMinMs, m_fireIntervalMaxMs, m_actionIntervalMinMs, m_actionIntervalMaxMs, m_teamID);
  74. }
  75. #endif
  76. #if defined(IMGUI_ENABLED)
  77. void NetworkStressTestComponentController::OnImGuiMainMenuUpdate()
  78. {
  79. if (ImGui::BeginMenu("Multiplayer Sample"))
  80. {
  81. ImGui::Checkbox("Entity Spawner", &m_displayEntitySpawner);
  82. ImGui::EndMenu();
  83. }
  84. }
  85. void NetworkStressTestComponentController::OnImGuiUpdate()
  86. {
  87. if (m_displayEntitySpawner)
  88. {
  89. if (ImGui::Begin("Entity Spawner", &m_displayEntitySpawner, ImGuiWindowFlags_AlwaysAutoResize))
  90. {
  91. DrawEntitySpawner();
  92. }
  93. }
  94. }
  95. void NetworkStressTestComponentController::DrawEntitySpawner()
  96. {
  97. ImGui::SliderInt("Quantity", &m_quantity, 1, 100);
  98. ImGui::SliderInt("Team ID", &m_teamID, 0, 3);
  99. ImGui::InputFloat("Fire Interval Min (ms)", &m_fireIntervalMinMs, 0.f, 100000.f);
  100. ImGui::InputFloat("Fire Interval Max (ms)", &m_fireIntervalMaxMs, 0.f, 100000.f);
  101. ImGui::InputFloat("Action Interval Min (ms)", &m_actionIntervalMinMs, 0.f, 100000.f);
  102. ImGui::InputFloat("Action Interval Max (ms)", &m_actionIntervalMaxMs, 0.f, 100000.f);
  103. if (ImGui::Button("Spawn AI Entity"))
  104. {
  105. for (int i = 0; i != m_quantity; ++i)
  106. {
  107. if (m_isServer)
  108. {
  109. #if AZ_TRAIT_SERVER
  110. HandleSpawnAIEntity(
  111. nullptr,
  112. m_fireIntervalMinMs,
  113. m_fireIntervalMaxMs,
  114. m_actionIntervalMinMs,
  115. m_actionIntervalMaxMs,
  116. m_teamID);
  117. #endif
  118. }
  119. else
  120. {
  121. #if AZ_TRAIT_CLIENT
  122. SpawnAIEntity(
  123. m_fireIntervalMinMs,
  124. m_fireIntervalMaxMs,
  125. m_actionIntervalMinMs,
  126. m_actionIntervalMaxMs,
  127. m_teamID);
  128. #endif
  129. }
  130. }
  131. }
  132. }
  133. #endif // defined(IMGUI_ENABLED)
  134. void NetworkStressTestComponent::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  135. {
  136. }
  137. void NetworkStressTestComponent::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  138. {
  139. }
  140. #if AZ_TRAIT_SERVER
  141. void NetworkStressTestComponentController::HandleSpawnAIEntity(
  142. AzNetworking::IConnection* invokingConnection,
  143. const float& fireIntervalMinMs,
  144. const float& fireIntervalMaxMs,
  145. const float& actionIntervalMinMs,
  146. const float& actionIntervalMaxMs,
  147. [[maybe_unused]] const int& teamId)
  148. {
  149. if (GetSpawnCount() > GetMaxSpawns())
  150. {
  151. return;
  152. }
  153. ModifySpawnCount()++;
  154. static Multiplayer::PrefabEntityId prefabId(AZ::Name{ "prefabs/player.network.spawnable" });
  155. Multiplayer::INetworkEntityManager::EntityList entityList =
  156. AZ::Interface<Multiplayer::IMultiplayer>::Get()->GetNetworkEntityManager()->CreateEntitiesImmediate(
  157. prefabId, Multiplayer::NetEntityRole::Authority, AZ::Transform::CreateIdentity(), Multiplayer::AutoActivate::DoNotActivate);
  158. for (const Multiplayer::NetworkEntityHandle& entityItem : entityList)
  159. {
  160. entityItem.GetNetBindComponent()->EnablePlayerHostAutonomy(true);
  161. }
  162. if (entityList.empty())
  163. {
  164. AZ_Error("NetworkStressTestComponentController", false, "No AI entity to spawn");
  165. return;
  166. }
  167. Multiplayer::NetworkEntityHandle createdEntity = entityList[0];
  168. // Drive inputs from AI instead of user inputs and disable camera following
  169. NetworkAiComponentController* networkAiController = createdEntity.FindController<NetworkAiComponentController>();
  170. networkAiController->ConfigureAi(fireIntervalMinMs, fireIntervalMaxMs, actionIntervalMinMs, actionIntervalMaxMs);
  171. networkAiController->SetEnabled(true);
  172. if (invokingConnection)
  173. {
  174. createdEntity.GetNetBindComponent()->SetOwningConnectionId(invokingConnection->GetConnectionId());
  175. }
  176. createdEntity.Activate();
  177. }
  178. #endif
  179. } // namespace MultiplayerSample