CatStation/Content.Server/Silicons/Laws/IonStormSystem.cs
n1-nbt 3ba046fed5
Admin log silicon laws (#44238)
* Admin log silicon laws

* Remove duplicate IonStorm lawchange logging and other stuff ig

* Update SharedSiliconLawSystem.cs

* Update IonStormSystem.cs

* Update SiliconLawSystem.cs

* Maybe I shouldve just used an IDE from the start
2026-07-18 02:40:21 -04:00

111 lines
3.6 KiB
C#

using Content.Shared.FixedPoint;
using Content.Shared.Silicons.Laws;
using Content.Shared.Silicons.Laws.Components;
using Robust.Shared.Random;
using Content.Shared.Random;
using Content.Shared.Random.Helpers;
namespace Content.Server.Silicons.Laws;
public sealed partial class IonStormSystem : EntitySystem
{
[Dependency] private SiliconLawSystem _siliconLaw = default!;
[Dependency] private IRobustRandom _robustRandom = default!;
[Dependency] private IonLawSystem _ionLaw = default!;
/// <summary>
/// Randomly alters the laws of an individual silicon.
/// </summary>
public void IonStormTarget(Entity<SiliconLawBoundComponent, IonStormTargetComponent> ent)
{
var lawBound = ent.Comp1;
var target = ent.Comp2;
if (!_robustRandom.Prob(target.Chance))
return;
var laws = _siliconLaw.GetLaws(ent, lawBound);
if (laws.Laws.Count == 0)
return;
// try to swap it out with a random lawset
if (_robustRandom.Prob(target.RandomLawsetChance))
{
var lawsets = ProtoMan.Index<WeightedRandomPrototype>(target.RandomLawsets);
var lawset = lawsets.Pick(_robustRandom);
laws = _siliconLaw.GetLawset(lawset);
}
// clone it so not modifying stations lawset
laws = laws.Clone();
// shuffle them all
if (_robustRandom.Prob(target.ShuffleChance))
{
// hopefully work with existing glitched laws if there are multiple ion storms
var baseOrder = FixedPoint2.New(1);
foreach (var law in laws.Laws)
{
if (law.Order < baseOrder)
baseOrder = law.Order;
}
_robustRandom.Shuffle(laws.Laws);
// change order based on shuffled position
for (int i = 0; i < laws.Laws.Count; i++)
{
laws.Laws[i].Order = baseOrder + i;
}
}
// see if we can remove a random law
if (laws.Laws.Count > 0 && _robustRandom.Prob(target.RemoveChance))
{
var i = _robustRandom.Next(laws.Laws.Count);
laws.Laws.RemoveAt(i);
}
// generate a new law...
var newLaw = _ionLaw.GetIonLaw();
if (string.IsNullOrEmpty(newLaw))
return;
// see if the law we add will replace a random existing law or be a new glitched order one
if (laws.Laws.Count > 0 && _robustRandom.Prob(target.ReplaceChance))
{
var i = _robustRandom.Next(laws.Laws.Count);
laws.Laws[i] = new SiliconLaw()
{
LawString = newLaw,
Order = laws.Laws[i].Order
};
}
else
{
var glitchedLaw = new SiliconLaw
{
LawString = newLaw,
Order = -1,
LawIdentifierOverride = Loc.GetString(
"ion-storm-law-scrambled-number",
(
"length",
_robustRandom.Next(
SharedSiliconLawSystem.IonStormIdentifierMinLength,
SharedSiliconLawSystem.IonStormIdentifierMaxLength
)
)
),
Corrupted = true
};
laws.Laws.Insert(0, glitchedLaw);
}
SiliconLawSystem.RankLaws(laws.Laws);
// laws unique to this silicon, dont use station laws anymore
EnsureComp<SiliconLawProviderComponent>(ent);
var ev = new IonStormLawsEvent(laws);
RaiseLocalEvent(ent, ref ev);
}
}