private void DoSpawn(IEntity owner, IRobustRandom random) { if (Spawn == null) { return; } foreach (var(key, value) in Spawn) { var count = value.Min >= value.Max ? value.Min : random.Next(value.Min, value.Max + 1); if (count == 0) { continue; } if (EntityPrototypeHelpers.HasComponent <StackComponent>(key)) { var spawned = owner.EntityManager.SpawnEntity(key, owner.Transform.Coordinates); var stack = spawned.GetComponent <StackComponent>(); stack.Count = count; spawned.RandomOffset(0.5f); } else { for (var i = 0; i < count; i++) { var spawned = owner.EntityManager.SpawnEntity(key, owner.Transform.Coordinates); spawned.RandomOffset(0.5f); } } } }
public static float NextFloat(this IRobustRandom random) { // This is pretty much the CoreFX implementation. // So credits to that. // Except using float instead of double. return(random.Next() * 4.6566128752458E-10f); }
public static T PickAndTake <T>(this IRobustRandom random, IList <T> list) { var index = random.Next(list.Count); var element = list[index]; list.RemoveAt(index); return(element); }
public void Trigger(IEntity owner, IRobustRandom random, ActSystem actSystem) { Triggered = true; PlaySound(owner); DoSpawn(owner, random); DoActs(owner, actSystem); }
/// <summary> /// Generate a random number from a normal (gaussian) distribution. /// </summary> /// <param name="random">The random object to generate the number from.</param> /// <param name="μ">The average or "center" of the normal distribution.</param> /// <param name="σ">The standard deviation of the normal distribution.</param> public static double NextGaussian(this IRobustRandom random, double μ = 0, double σ = 1) { // https://stackoverflow.com/a/218600 var α = random.NextDouble(); var β = random.NextDouble(); var randStdNormal = Math.Sqrt(-2.0 * Math.Log(α)) * Math.Sin(2.0 * Math.PI * β); return(μ + σ * randStdNormal); }
public void Initialize(PointLightComponent light) { Light = light; RobustRandom = IoCManager.Resolve <IRobustRandom>(); if (Enabled) { Light.Enabled = true; } OnInitialize(); }
private EntityCoordinates FindRandomGrid(IRobustRandom robustRandom) { // Very inefficient (should weight each region by its node count) but better than the old system var reachableSystem = EntitySystem.Get <AiReachableSystem>(); var reachableArgs = ReachableArgs.GetArgs(Owner); var entityRegion = reachableSystem.GetRegion(Owner); var reachableRegions = reachableSystem.GetReachableRegions(reachableArgs, entityRegion); // TODO: When SetupOperators can fail this should be null and fail the setup. if (reachableRegions.Count == 0) { return(default);
public void Initialize(IEntity parent, IRobustRandom random) { _random = random; _parent = parent; if (Enabled && _parent.TryGetComponent(out PointLightComponent? light)) { light.Enabled = true; } OnInitialize(); }
public void Initialize(EntityUid parent, IRobustRandom random, IEntityManager entMan) { _random = random; _entMan = entMan; _parent = parent; if (Enabled && _entMan.TryGetComponent(_parent, out PointLightComponent? light)) { light.Enabled = true; } OnInitialize(); }
public override void Shutdown() { base.Shutdown(); // IOC uninject? _entityManager = null; _robustRandom = null; var componentManager = IoCManager.Resolve <IComponentManager>(); foreach (var overlay in componentManager.EntityQuery <ServerOverlayEffectsComponent>()) { overlay.RemoveOverlay(SharedOverlayID.RadiationPulseOverlay); } }
/// <summary>Picks a random element from a collection.</summary> /// <remarks> /// This is O(n). /// </remarks> public static T Pick <T>(this IRobustRandom random, IReadOnlyCollection <T> collection) { var index = random.Next(collection.Count); var i = 0; foreach (var t in collection) { if (i++ == index) { return(t); } } throw new InvalidOperationException("This should be unreachable!"); }
/// <summary> /// Drops a single cartridge / shell /// Made as a static function just because multiple places need it /// </summary> /// <param name="entity"></param> /// <param name="playSound"></param> /// <param name="robustRandom"></param> /// <param name="prototypeManager"></param> /// <param name="ejectDirections"></param> public static void EjectCasing( IEntity entity, bool playSound = true, IRobustRandom robustRandom = null, IPrototypeManager prototypeManager = null, Direction[] ejectDirections = null) { if (robustRandom == null) { robustRandom = IoCManager.Resolve <IRobustRandom>(); } if (ejectDirections == null) { ejectDirections = new[] { Direction.East, Direction.North, Direction.NorthWest, Direction.South, Direction.SouthEast, Direction.West }; } const float ejectOffset = 1.8f; var ammo = entity.GetComponent <AmmoComponent>(); var offsetPos = ((robustRandom.NextFloat() - 0.5f) * ejectOffset, (robustRandom.NextFloat() - 0.5f) * ejectOffset); entity.Transform.Coordinates = entity.Transform.Coordinates.Offset(offsetPos); entity.Transform.LocalRotation = robustRandom.Pick(ejectDirections).ToAngle(); if (ammo.SoundCollectionEject == null || !playSound) { return; } if (prototypeManager == null) { prototypeManager = IoCManager.Resolve <IPrototypeManager>(); } var soundCollection = prototypeManager.Index <SoundCollectionPrototype>(ammo.SoundCollectionEject); var randomFile = robustRandom.Pick(soundCollection.PickFiles); SoundSystem.Play(Filter.Broadcast(), randomFile, entity.Transform.Coordinates, AudioParams.Default.WithVolume(-1)); }
/// <summary> /// Have a certain chance to return a boolean. /// </summary> /// <param name="random">The random instance to run on.</param> /// <param name="chance">The chance to pass, from 0 to 1.</param> public static bool Prob(this IRobustRandom random, float chance) { return(random.NextDouble() <= chance); }
public static string Pick(this IRobustRandom random, DatasetPrototype prototype) { return(random.Pick(prototype.Values)); }
/// <summary> /// Have a certain chance to return a boolean. /// </summary> /// <param name="random">The random instance to run on.</param> /// <param name="chance">The chance to pass, from 0 to 1.</param> public static bool Prob(this IRobustRandom random, float chance) { DebugTools.Assert(chance <= 1 && chance >= 0, $"Chance must be in the range 0-1. It was {chance}."); return(random.NextDouble() <= chance); }
public ScreamAction() { _random = IoCManager.Resolve <IRobustRandom>(); }
public static T Pick <T>(this IRobustRandom random, IReadOnlyList <T> list) { var index = random.Next(list.Count); return(list[index]); }