SolutionRandomFillSystem.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Content.Server.Chemistry.Components;
  2. using Content.Shared.Chemistry.EntitySystems;
  3. using Content.Shared.Chemistry.Reagent;
  4. using Content.Shared.Random;
  5. using Content.Shared.Random.Helpers;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Random;
  8. namespace Content.Server.Chemistry.EntitySystems;
  9. public sealed class SolutionRandomFillSystem : EntitySystem
  10. {
  11. [Dependency] private readonly SharedSolutionContainerSystem _solutionsSystem = default!;
  12. [Dependency] private readonly IPrototypeManager _proto = default!;
  13. [Dependency] private readonly IRobustRandom _random = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<RandomFillSolutionComponent, MapInitEvent>(OnRandomSolutionFillMapInit);
  18. }
  19. private void OnRandomSolutionFillMapInit(Entity<RandomFillSolutionComponent> entity, ref MapInitEvent args)
  20. {
  21. if (entity.Comp.WeightedRandomId == null)
  22. return;
  23. var pick = _proto.Index<WeightedRandomFillSolutionPrototype>(entity.Comp.WeightedRandomId).Pick(_random);
  24. var reagent = pick.reagent;
  25. var quantity = pick.quantity;
  26. if (!_proto.HasIndex<ReagentPrototype>(reagent))
  27. {
  28. Log.Error($"Tried to add invalid reagent Id {reagent} using SolutionRandomFill.");
  29. return;
  30. }
  31. _solutionsSystem.EnsureSolutionEntity(entity.Owner, entity.Comp.Solution, out var target , pick.quantity);
  32. if(target.HasValue)
  33. _solutionsSystem.TryAddReagent(target.Value, reagent, quantity);
  34. }
  35. }