IoC'd random (#302)

* Implemented RobustRandom

* update submodule

* update submodule

* Fix benchmark
This commit is contained in:
DamianX
2019-08-17 21:09:09 +02:00
committed by Acruid
parent 534af65f7c
commit 4dcbf28714
17 changed files with 71 additions and 47 deletions

View File

@@ -5,7 +5,10 @@ using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#endif
using BenchmarkDotNet.Attributes;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using SysVector4 = System.Numerics.Vector4;
namespace Content.Benchmarks

View File

@@ -5,6 +5,7 @@ using Robust.Client.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.Timers;
@@ -16,7 +17,9 @@ namespace Content.Client.GameObjects.Components.Sound
{
private readonly List<ScheduledSound> _schedules = new List<ScheduledSound>();
private AudioSystem _audioSystem;
private Random Random;
#pragma warning disable 649
[Dependency] private readonly IRobustRandom _random;
#pragma warning restore 649
public override void StopAllSounds()
{
@@ -46,9 +49,8 @@ namespace Content.Client.GameObjects.Components.Sound
public void Play(ScheduledSound schedule)
{
if (!schedule.Play) return;
if (Random == null) Random = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
Timer.Spawn((int) schedule.Delay + (Random.Next((int) schedule.RandomDelay)),() =>
Timer.Spawn((int) schedule.Delay + (_random.Next((int) schedule.RandomDelay)),() =>
{
if (!schedule.Play) return; // We make sure this hasn't changed.
if (_audioSystem == null) _audioSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>();

View File

@@ -10,6 +10,7 @@ using Robust.Client.Utility;
using Robust.Shared.Interfaces.Log;
using Robust.Shared.Maths;
using Robust.Shared.Noise;
using Robust.Shared.Random;
using SixLabors.ImageSharp.Advanced;
using BlendFactor = Robust.Shared.Maths.Color.BlendFactor;

View File

@@ -10,6 +10,7 @@ using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
using static Content.Shared.Construction.ConstructionStepMaterial;
@@ -29,7 +30,9 @@ namespace Content.Server.GameObjects.Components.Construction
SpriteComponent Sprite;
ITransformComponent Transform;
Random random;
#pragma warning disable 649
[Dependency] private IRobustRandom _random;
#pragma warning restore 649
public override void Initialize()
{
@@ -38,7 +41,6 @@ namespace Content.Server.GameObjects.Components.Construction
Sprite = Owner.GetComponent<SpriteComponent>();
Transform = Owner.GetComponent<ITransformComponent>();
var systemman = IoCManager.Resolve<IEntitySystemManager>();
random = new Random();
}
public bool AttackBy(AttackByEventArgs eventArgs)
@@ -127,7 +129,7 @@ namespace Content.Server.GameObjects.Components.Construction
case ToolType.Welder:
if (slapped.TryGetComponent(out WelderComponent welder) && welder.TryUse(toolStep.Amount))
{
if (random.NextDouble() > 0.5)
if (_random.NextDouble() > 0.5)
sound.Play("/Audio/items/welder.ogg", Transform.GridPosition);
else
sound.Play("/Audio/items/welder2.ogg", Transform.GridPosition);
@@ -144,7 +146,7 @@ namespace Content.Server.GameObjects.Components.Construction
case ToolType.Screwdriver:
if (slapped.HasComponent<ScrewdriverComponent>())
{
if (random.NextDouble() > 0.5)
if (_random.NextDouble() > 0.5)
sound.Play("/Audio/items/screwdriver.ogg", Transform.GridPosition);
else
sound.Play("/Audio/items/screwdriver2.ogg", Transform.GridPosition);

View File

@@ -5,8 +5,10 @@ using Content.Server.Interfaces;
using Content.Shared.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Damage
@@ -59,7 +61,7 @@ namespace Content.Server.GameObjects.Components.Damage
public void OnExplosion(ExplosionEventArgs eventArgs)
{
var prob = new Random();
var prob = IoCManager.Resolve<IRobustRandom>();
switch (eventArgs.Severity)
{
case ExplosionSeverity.Destruction:

View File

@@ -5,8 +5,10 @@ using Content.Server.Interfaces;
using Content.Shared.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -74,7 +76,7 @@ namespace Content.Server.GameObjects.Components.Destructible
void IExAct.OnExplosion(ExplosionEventArgs eventArgs)
{
var prob = new Random();
var prob = IoCManager.Resolve<IRobustRandom>();
switch (eventArgs.Severity)
{
case ExplosionSeverity.Destruction:
@@ -84,7 +86,7 @@ namespace Content.Server.GameObjects.Components.Destructible
_actSystem.HandleDestruction(Owner, true);
break;
case ExplosionSeverity.Light:
if (RandomExtensions.Prob(prob, 40))
if (prob.Prob(40))
_actSystem.HandleDestruction(Owner, true);
break;
}

View File

@@ -10,10 +10,12 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.EntitySystemMessages;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Explosive
@@ -26,6 +28,7 @@ namespace Content.Server.GameObjects.Components.Explosive
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IServerEntityManager _serverEntityManager;
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
[Dependency] private readonly IRobustRandom _robustRandom;
#pragma warning restore 649
public override string Name => "Explosive";
@@ -96,7 +99,7 @@ namespace Content.Server.GameObjects.Components.Explosive
mapGrid.SetTile(tileLoc, new Tile(_tileDefinitionManager["space"].TileId));
if (distanceFromTile < HeavyImpactRange)
{
if (new Random().Prob(80))
if (_robustRandom.Prob(80))
{
mapGrid.SetTile(tileLoc, new Tile(_tileDefinitionManager[tileDef.SubFloor].TileId));
}
@@ -107,7 +110,7 @@ namespace Content.Server.GameObjects.Components.Explosive
}
if (distanceFromTile < LightImpactRange)
{
if (new Random().Prob(50))
if (_robustRandom.Prob(50))
{
mapGrid.SetTile(tileLoc, new Tile(_tileDefinitionManager[tileDef.SubFloor].TileId));
}

View File

@@ -5,9 +5,11 @@ using Content.Shared.Audio;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -19,11 +21,11 @@ namespace Content.Server.GameObjects.Components.Items
{
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _random;
#pragma warning restore 649
public override string Name => "Dice";
private Random _random;
private int _step = 1;
private int _sides = 20;
private int _currentSide = 20;
@@ -45,12 +47,6 @@ namespace Content.Server.GameObjects.Components.Items
_currentSide = _sides;
}
public override void OnAdd()
{
base.OnAdd();
_random = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
}
public void Roll()
{
_currentSide = _random.Next(1, (_sides/_step)+1) * _step;

View File

@@ -2,8 +2,10 @@ using System;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
namespace Content.Server.GameObjects.Components.Items.Storage.Fill
{
@@ -19,7 +21,7 @@ namespace Content.Server.GameObjects.Components.Items.Storage.Fill
void IMapInit.MapInit()
{
var storage = Owner.GetComponent<IStorageComponent>();
var random = new Random(DateTime.Now.GetHashCode() ^ Owner.Uid.GetHashCode());
var random = IoCManager.Resolve<IRobustRandom>();
void Spawn(string prototype)
{

View File

@@ -2,8 +2,10 @@ using System;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
namespace Content.Server.GameObjects.Components.Items.Storage.Fill
{
@@ -19,7 +21,7 @@ namespace Content.Server.GameObjects.Components.Items.Storage.Fill
void IMapInit.MapInit()
{
var storage = Owner.GetComponent<IStorageComponent>();
var random = new Random(DateTime.Now.GetHashCode() ^ Owner.Uid.GetHashCode());
var random = IoCManager.Resolve<IRobustRandom>();
void Spawn(string prototype)
{

View File

@@ -7,7 +7,10 @@ using Robust.Server.GameObjects;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects
@@ -20,6 +23,10 @@ namespace Content.Server.GameObjects
public override uint? NetID => ContentNetIDs.ITEM;
public override Type StateType => typeof(ItemComponentState);
#pragma warning disable 649
[Dependency] private readonly IRobustRandom _robustRandom;
#pragma warning restore 649
private string _equippedPrefix;
public string EquippedPrefix
@@ -115,7 +122,7 @@ namespace Content.Server.GameObjects
float RandomOffset()
{
var size = 15.0F;
return (new Random().NextFloat() * size) - size / 2;
return (_robustRandom.NextFloat() * size) - size / 2;
}
}
}

View File

@@ -8,6 +8,7 @@ using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
@@ -24,6 +25,7 @@ namespace Content.Server.GameObjects.Components.Movement
#pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IServerEntityManager _serverEntityManager;
[Dependency] private readonly IRobustRandom _spreadRandom;
#pragma warning restore 649
// TODO: Look at MapManager.Map for Beacons to get all entities on grid
public ItemTeleporterState State => _state;
@@ -44,8 +46,6 @@ namespace Content.Server.GameObjects.Components.Movement
private AppearanceComponent _appearanceComponent;
private Random _spreadRandom;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
@@ -149,7 +149,6 @@ namespace Content.Server.GameObjects.Components.Movement
public override void Initialize()
{
_appearanceComponent = Owner.GetComponent<AppearanceComponent>();
_spreadRandom = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
_state = ItemTeleporterState.Off;
base.Initialize();
}

View File

@@ -2,9 +2,11 @@
using Content.Shared.Audio;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Sound
@@ -17,10 +19,10 @@ namespace Content.Server.GameObjects.Components.Sound
{
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _footstepRandom;
#pragma warning restore 649
/// <inheritdoc />
///
private Random _footstepRandom;
public override string Name => "FootstepModifier";
@@ -32,12 +34,6 @@ namespace Content.Server.GameObjects.Components.Sound
serializer.DataField(ref _soundCollectionName, "footstepSoundCollection", "");
}
public override void Initialize()
{
base.Initialize();
_footstepRandom = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
}
public void PlayFootstep()
{
if (!string.IsNullOrWhiteSpace(_soundCollectionName))

View File

@@ -10,7 +10,10 @@ using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -34,8 +37,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
[ViewVariables]
private IEntity Magazine => _magazineSlot.ContainedEntity;
[ViewVariables]
private Random _bulletDropRandom;
#pragma warning disable 649
[Dependency] private readonly IRobustRandom _bulletDropRandom;
#pragma warning restore 649
[ViewVariables]
private string _magInSound;
[ViewVariables]
@@ -74,7 +78,6 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
base.Initialize();
_appearance = Owner.GetComponent<AppearanceComponent>();
_bulletDropRandom = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
}
public override void Startup()

View File

@@ -8,10 +8,12 @@ using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -23,7 +25,9 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
private float _spreadStdDev = 3;
private bool _spread = true;
private Random _spreadRandom;
#pragma warning disable 649
[Dependency] private IRobustRandom _spreadRandom;
#pragma warning restore 649
[ViewVariables(VVAccess.ReadWrite)]
public bool Spread
@@ -45,8 +49,6 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
var rangedWeapon = Owner.GetComponent<RangedWeaponComponent>();
rangedWeapon.FireHandler = Fire;
_spreadRandom = new Random(Owner.Uid.GetHashCode() ^ DateTime.Now.GetHashCode());
}
public override void ExposeData(ObjectSerializer serializer)

View File

@@ -23,7 +23,9 @@ using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Content.Server.GameObjects.Components.Sound;
using Content.Shared.GameObjects.Components.Inventory;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.Log;
using Robust.Shared.Random;
namespace Content.Server.GameObjects.EntitySystems
{
@@ -35,10 +37,10 @@ namespace Content.Server.GameObjects.EntitySystems
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager;
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IRobustRandom _robustRandom;
#pragma warning restore 649
private AudioSystem _audioSystem;
private Random _footstepRandom;
private const float StepSoundMoveDistanceRunning = 2;
private const float StepSoundMoveDistanceWalking = 1.5f;
@@ -47,7 +49,7 @@ namespace Content.Server.GameObjects.EntitySystems
public override void Initialize()
{
EntityQuery = new TypeEntityQuery(typeof(IMoverComponent));
var moveUpCmdHandler = InputCmdHandler.FromDelegate(
session => HandleDirChange(session, Direction.North, true),
session => HandleDirChange(session, Direction.North, false));
@@ -75,7 +77,6 @@ namespace Content.Server.GameObjects.EntitySystems
SubscribeEvent<PlayerAttachSystemMessage>(PlayerAttached);
SubscribeEvent<PlayerDetachedSystemMessage>(PlayerDetached);
_footstepRandom = new Random();
_audioSystem = EntitySystemManager.GetEntitySystem<AudioSystem>();
}
@@ -153,7 +154,7 @@ namespace Content.Server.GameObjects.EntitySystems
{
mover.StepSoundDistance = 0;
if (mover.Owner.TryGetComponent<InventoryComponent>(out var inventory)
&& inventory.TryGetSlotItem<ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item)
&& inventory.TryGetSlotItem<ItemComponent>(EquipmentSlotDefines.Slots.SHOES, out var item)
&& item.Owner.TryGetComponent<FootstepModifierComponent>(out var modifier))
{
modifier.PlayFootstep();
@@ -186,7 +187,7 @@ namespace Content.Server.GameObjects.EntitySystems
where T: Component
{
component = default;
var ent = session.AttachedEntity;
if (ent == null || !ent.IsValid())
@@ -238,7 +239,7 @@ namespace Content.Server.GameObjects.EntitySystems
try
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
var file = _footstepRandom.Pick(soundCollection.PickFiles);
var file = _robustRandom.Pick(soundCollection.PickFiles);
_audioSystem.Play(file, coordinates);
}
catch (UnknownPrototypeException)

View File

@@ -21,12 +21,14 @@ using Robust.Shared.Interfaces.Configuration;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Random;
using Robust.Shared.Timers;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
@@ -76,8 +78,6 @@ namespace Content.Server.GameTicking
[ViewVariables] private bool _roundStartCountdownHasNotStartedYetDueToNoPlayers;
private DateTime _roundStartTimeUtc;
private readonly Random _spawnRandom = new Random();
[ViewVariables] private readonly List<GameRule> _gameRules = new List<GameRule>();
[ViewVariables] private Type _presetType;
@@ -92,6 +92,7 @@ namespace Content.Server.GameTicking
[Dependency] private IChatManager _chatManager;
[Dependency] private IServerNetManager _netManager;
[Dependency] private IDynamicTypeFactory _dynamicTypeFactory;
[Dependency] private readonly IRobustRandom _robustRandom;
#pragma warning restore 649
public void Initialize()
@@ -293,7 +294,7 @@ namespace Content.Server.GameTicking
if (possiblePoints.Count != 0)
{
location = _spawnRandom.Pick(possiblePoints);
location = _robustRandom.Pick(possiblePoints);
}
return location;