PowerSolverShared.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. namespace Content.Server.Power.Pow3r
  2. {
  3. public static class PowerSolverShared
  4. {
  5. public static void UpdateRampPositions(float frameTime, PowerState state)
  6. {
  7. // Update supplies to move their ramp position towards target, if necessary.
  8. foreach (var supply in state.Supplies.Values)
  9. {
  10. if (supply.Paused)
  11. continue;
  12. if (!supply.Enabled)
  13. {
  14. // If disabled, set ramp to 0.
  15. supply.SupplyRampPosition = 0;
  16. continue;
  17. }
  18. var rampDev = supply.SupplyRampTarget - supply.SupplyRampPosition;
  19. if (Math.Abs(rampDev) > 0.001f)
  20. {
  21. float newPos;
  22. if (rampDev > 0)
  23. {
  24. // Position below target, go up.
  25. newPos = Math.Min(
  26. supply.SupplyRampTarget,
  27. supply.SupplyRampPosition + supply.SupplyRampRate * frameTime);
  28. }
  29. else
  30. {
  31. // Other way around, go down
  32. newPos = Math.Max(
  33. supply.SupplyRampTarget,
  34. supply.SupplyRampPosition - supply.SupplyRampRate * frameTime);
  35. }
  36. supply.SupplyRampPosition = Math.Clamp(newPos, 0, supply.MaxSupply);
  37. }
  38. else
  39. {
  40. supply.SupplyRampPosition = supply.SupplyRampTarget;
  41. }
  42. }
  43. // Batteries too.
  44. foreach (var battery in state.Batteries.Values)
  45. {
  46. if (battery.Paused)
  47. continue;
  48. if (!battery.Enabled)
  49. {
  50. // If disabled, set ramp to 0.
  51. battery.SupplyRampPosition = 0;
  52. continue;
  53. }
  54. var rampDev = battery.SupplyRampTarget - battery.SupplyRampPosition;
  55. if (Math.Abs(rampDev) > 0.001f)
  56. {
  57. float newPos;
  58. if (rampDev > 0)
  59. {
  60. // Position below target, go up.
  61. newPos = Math.Min(
  62. battery.SupplyRampTarget,
  63. battery.SupplyRampPosition + battery.SupplyRampRate * frameTime);
  64. }
  65. else
  66. {
  67. // Other way around, go down
  68. newPos = Math.Max(
  69. battery.SupplyRampTarget,
  70. battery.SupplyRampPosition - battery.SupplyRampRate * frameTime);
  71. }
  72. battery.SupplyRampPosition = Math.Clamp(newPos, 0, battery.MaxSupply);
  73. }
  74. else
  75. {
  76. battery.SupplyRampPosition = battery.SupplyRampTarget;
  77. }
  78. }
  79. }
  80. }
  81. }