NetworkRandomComponent.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <Source/Components/NetworkRandomComponent.h>
  8. namespace ${SanitizedCppName}
  9. {
  10. uint64_t NetworkRandomComponentController::GetRandomUint64()
  11. {
  12. // Reimplements SimpleLcgRandom's rand int with a synchronized seed
  13. uint64_t& seed = ModifySeed();
  14. seed = (GetSeed() * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1);
  15. return seed;
  16. }
  17. int NetworkRandomComponentController::GetRandomInt()
  18. {
  19. // Reimplements SimpleLcgRandom's rand int with a synchronized seed
  20. return static_cast<unsigned int>(GetRandomUint64() >> 16);
  21. }
  22. float NetworkRandomComponentController::GetRandomFloat()
  23. {
  24. // Reimplements SimpleLcgRandom's rand float with a synchronized seed
  25. unsigned int r = GetRandomInt();
  26. r &= 0x007fffff; //sets mantissa to random bits
  27. r |= 0x3f800000; //result is in [1,2), uniformly distributed
  28. union
  29. {
  30. float f;
  31. unsigned int i;
  32. } u;
  33. u.i = r;
  34. return u.f - 1.0f;
  35. }
  36. NetworkRandomComponentController::NetworkRandomComponentController(NetworkRandomComponent& parent)
  37. : NetworkRandomComponentControllerBase(parent)
  38. {
  39. #if AZ_TRAIT_SERVER
  40. if (IsNetEntityRoleAuthority())
  41. {
  42. // Setup seed on authority for proxies to pull
  43. AZ::BetterPseudoRandom seedGenerator;
  44. uint64_t seed = 0;
  45. seedGenerator.GetRandom(seed);
  46. SetSeed(seed);
  47. };
  48. #endif
  49. }
  50. void NetworkRandomComponentController::OnActivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  51. {
  52. ;
  53. }
  54. void NetworkRandomComponentController::OnDeactivate([[maybe_unused]] Multiplayer::EntityIsMigrating entityIsMigrating)
  55. {
  56. ;
  57. }
  58. }