ReagentThreshold.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Content.Shared.Chemistry.Reagent;
  2. using Content.Shared.EntityEffects;
  3. using Content.Shared.FixedPoint;
  4. using Robust.Shared.Prototypes;
  5. namespace Content.Server.EntityEffects.EffectConditions;
  6. /// <summary>
  7. /// Used for implementing reagent effects that require a certain amount of reagent before it should be applied.
  8. /// For instance, overdoses.
  9. ///
  10. /// This can also trigger on -other- reagents, not just the one metabolizing. By default, it uses the
  11. /// one being metabolized.
  12. /// </summary>
  13. public sealed partial class ReagentThreshold : EntityEffectCondition
  14. {
  15. [DataField]
  16. public FixedPoint2 Min = FixedPoint2.Zero;
  17. [DataField]
  18. public FixedPoint2 Max = FixedPoint2.MaxValue;
  19. // TODO use ReagentId
  20. [DataField]
  21. public string? Reagent;
  22. public override bool Condition(EntityEffectBaseArgs args)
  23. {
  24. if (args is EntityEffectReagentArgs reagentArgs)
  25. {
  26. var reagent = Reagent ?? reagentArgs.Reagent?.ID;
  27. if (reagent == null)
  28. return true; // No condition to apply.
  29. var quant = FixedPoint2.Zero;
  30. if (reagentArgs.Source != null)
  31. quant = reagentArgs.Source.GetTotalPrototypeQuantity(reagent);
  32. return quant >= Min && quant <= Max;
  33. }
  34. // TODO: Someone needs to figure out how to do this for non-reagent effects.
  35. throw new NotImplementedException();
  36. }
  37. public override string GuidebookExplanation(IPrototypeManager prototype)
  38. {
  39. ReagentPrototype? reagentProto = null;
  40. if (Reagent is not null)
  41. prototype.TryIndex(Reagent, out reagentProto);
  42. return Loc.GetString("reagent-effect-condition-guidebook-reagent-threshold",
  43. ("reagent", reagentProto?.LocalizedName ?? Loc.GetString("reagent-effect-condition-guidebook-this-reagent")),
  44. ("max", Max == FixedPoint2.MaxValue ? (float) int.MaxValue : Max.Float()),
  45. ("min", Min.Float()));
  46. }
  47. }