1
0

PlantChangeStat.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Content.Server.Botany;
  2. using Content.Server.Botany.Components;
  3. using Content.Shared.EntityEffects;
  4. using JetBrains.Annotations;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Random;
  7. namespace Content.Server.EntityEffects.Effects.PlantMetabolism;
  8. [UsedImplicitly]
  9. public sealed partial class PlantChangeStat : EntityEffect
  10. {
  11. [DataField]
  12. public string TargetValue;
  13. [DataField]
  14. public float MinValue;
  15. [DataField]
  16. public float MaxValue;
  17. [DataField]
  18. public int Steps;
  19. public override void Effect(EntityEffectBaseArgs args)
  20. {
  21. var plantHolder = args.EntityManager.GetComponent<PlantHolderComponent>(args.TargetEntity);
  22. if (plantHolder == null || plantHolder.Seed == null)
  23. return;
  24. var member = plantHolder.Seed.GetType().GetField(TargetValue);
  25. var mutationSys = args.EntityManager.System<MutationSystem>();
  26. if (member == null)
  27. {
  28. mutationSys.Log.Error(this.GetType().Name + " Error: Member " + TargetValue + " not found on " + plantHolder.GetType().Name + ". Did you misspell it?");
  29. return;
  30. }
  31. var currentValObj = member.GetValue(plantHolder.Seed);
  32. if (currentValObj == null)
  33. return;
  34. if (member.FieldType == typeof(float))
  35. {
  36. var floatVal = (float)currentValObj;
  37. MutateFloat(ref floatVal, MinValue, MaxValue, Steps);
  38. member.SetValue(plantHolder.Seed, floatVal);
  39. }
  40. else if (member.FieldType == typeof(int))
  41. {
  42. var intVal = (int)currentValObj;
  43. MutateInt(ref intVal, (int)MinValue, (int)MaxValue, Steps);
  44. member.SetValue(plantHolder.Seed, intVal);
  45. }
  46. else if (member.FieldType == typeof(bool))
  47. {
  48. var boolVal = (bool)currentValObj;
  49. boolVal = !boolVal;
  50. member.SetValue(plantHolder.Seed, boolVal);
  51. }
  52. }
  53. // Mutate reference 'val' between 'min' and 'max' by pretending the value
  54. // is representable by a thermometer code with 'bits' number of bits and
  55. // randomly flipping some of them.
  56. private void MutateFloat(ref float val, float min, float max, int bits)
  57. {
  58. if (min == max)
  59. {
  60. val = min;
  61. return;
  62. }
  63. // Starting number of bits that are high, between 0 and bits.
  64. // In other words, it's val mapped linearly from range [min, max] to range [0, bits], and then rounded.
  65. int valInt = (int)MathF.Round((val - min) / (max - min) * bits);
  66. // val may be outside the range of min/max due to starting prototype values, so clamp.
  67. valInt = Math.Clamp(valInt, 0, bits);
  68. // Probability that the bit flip increases n.
  69. // The higher the current value is, the lower the probability of increasing value is, and the higher the probability of decreasive it it.
  70. // In other words, it tends to go to the middle.
  71. float probIncrease = 1 - (float)valInt / bits;
  72. int valIntMutated;
  73. if (Random(probIncrease))
  74. {
  75. valIntMutated = valInt + 1;
  76. }
  77. else
  78. {
  79. valIntMutated = valInt - 1;
  80. }
  81. // Set value based on mutated thermometer code.
  82. float valMutated = Math.Clamp((float)valIntMutated / bits * (max - min) + min, min, max);
  83. val = valMutated;
  84. }
  85. private void MutateInt(ref int val, int min, int max, int bits)
  86. {
  87. if (min == max)
  88. {
  89. val = min;
  90. return;
  91. }
  92. // Starting number of bits that are high, between 0 and bits.
  93. // In other words, it's val mapped linearly from range [min, max] to range [0, bits], and then rounded.
  94. int valInt = (int)MathF.Round((val - min) / (max - min) * bits);
  95. // val may be outside the range of min/max due to starting prototype values, so clamp.
  96. valInt = Math.Clamp(valInt, 0, bits);
  97. // Probability that the bit flip increases n.
  98. // The higher the current value is, the lower the probability of increasing value is, and the higher the probability of decreasing it.
  99. // In other words, it tends to go to the middle.
  100. float probIncrease = 1 - (float)valInt / bits;
  101. int valMutated;
  102. if (Random(probIncrease))
  103. {
  104. valMutated = val + 1;
  105. }
  106. else
  107. {
  108. valMutated = val - 1;
  109. }
  110. valMutated = Math.Clamp(valMutated, min, max);
  111. val = valMutated;
  112. }
  113. private bool Random(float odds)
  114. {
  115. var random = IoCManager.Resolve<IRobustRandom>();
  116. return random.Prob(odds);
  117. }
  118. protected override string? ReagentEffectGuidebookText(IPrototypeManager prototype, IEntitySystemManager entSys)
  119. {
  120. throw new NotImplementedException();
  121. }
  122. }