1
0

AdjustReagent.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Content.Shared.Body.Prototypes;
  2. using Content.Shared.Chemistry.Reagent;
  3. using Content.Shared.EntityEffects;
  4. using Content.Shared.FixedPoint;
  5. using JetBrains.Annotations;
  6. using Robust.Shared.Prototypes;
  7. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
  8. namespace Content.Server.EntityEffects.Effects
  9. {
  10. [UsedImplicitly]
  11. public sealed partial class AdjustReagent : EntityEffect
  12. {
  13. /// <summary>
  14. /// The reagent ID to remove. Only one of this and <see cref="Group"/> should be active.
  15. /// </summary>
  16. [DataField(customTypeSerializer: typeof(PrototypeIdSerializer<ReagentPrototype>))]
  17. public string? Reagent = null;
  18. // TODO use ReagentId
  19. /// <summary>
  20. /// The metabolism group to remove, if the reagent satisfies any.
  21. /// Only one of this and <see cref="Reagent"/> should be active.
  22. /// </summary>
  23. [DataField(customTypeSerializer: typeof(PrototypeIdSerializer<MetabolismGroupPrototype>))]
  24. public string? Group = null;
  25. [DataField(required: true)]
  26. public FixedPoint2 Amount = default!;
  27. public override void Effect(EntityEffectBaseArgs args)
  28. {
  29. if (args is EntityEffectReagentArgs reagentArgs)
  30. {
  31. if (reagentArgs.Source == null)
  32. return;
  33. var amount = Amount;
  34. amount *= reagentArgs.Scale;
  35. if (Reagent != null)
  36. {
  37. if (amount < 0 && reagentArgs.Source.ContainsPrototype(Reagent))
  38. reagentArgs.Source.RemoveReagent(Reagent, -amount);
  39. if (amount > 0)
  40. reagentArgs.Source.AddReagent(Reagent, amount);
  41. }
  42. else if (Group != null)
  43. {
  44. var prototypeMan = IoCManager.Resolve<IPrototypeManager>();
  45. foreach (var quant in reagentArgs.Source.Contents.ToArray())
  46. {
  47. var proto = prototypeMan.Index<ReagentPrototype>(quant.Reagent.Prototype);
  48. if (proto.Metabolisms != null && proto.Metabolisms.ContainsKey(Group))
  49. {
  50. if (amount < 0)
  51. reagentArgs.Source.RemoveReagent(quant.Reagent, amount);
  52. if (amount > 0)
  53. reagentArgs.Source.AddReagent(quant.Reagent, amount);
  54. }
  55. }
  56. }
  57. return;
  58. }
  59. // TODO: Someone needs to figure out how to do this for non-reagent effects.
  60. throw new NotImplementedException();
  61. }
  62. protected override string? ReagentEffectGuidebookText(IPrototypeManager prototype, IEntitySystemManager entSys)
  63. {
  64. if (Reagent is not null && prototype.TryIndex(Reagent, out ReagentPrototype? reagentProto))
  65. {
  66. return Loc.GetString("reagent-effect-guidebook-adjust-reagent-reagent",
  67. ("chance", Probability),
  68. ("deltasign", MathF.Sign(Amount.Float())),
  69. ("reagent", reagentProto.LocalizedName),
  70. ("amount", MathF.Abs(Amount.Float())));
  71. }
  72. else if (Group is not null && prototype.TryIndex(Group, out MetabolismGroupPrototype? groupProto))
  73. {
  74. return Loc.GetString("reagent-effect-guidebook-adjust-reagent-group",
  75. ("chance", Probability),
  76. ("deltasign", MathF.Sign(Amount.Float())),
  77. ("group", groupProto.LocalizedName),
  78. ("amount", MathF.Abs(Amount.Float())));
  79. }
  80. throw new NotImplementedException();
  81. }
  82. }
  83. }