AtmosAlarmThreshold.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using Robust.Shared.Prototypes;
  2. using Robust.Shared.Serialization;
  3. namespace Content.Shared.Atmos.Monitor;
  4. [Prototype("alarmThreshold")]
  5. public sealed partial class AtmosAlarmThresholdPrototype : IPrototype
  6. {
  7. [IdDataField]
  8. public string ID { get; private set; } = default!;
  9. [DataField("ignore")]
  10. public bool Ignore;
  11. [DataField("upperBound")]
  12. public AlarmThresholdSetting UpperBound = AlarmThresholdSetting.Disabled;
  13. [DataField("lowerBound")]
  14. public AlarmThresholdSetting LowerBound = AlarmThresholdSetting.Disabled;
  15. [DataField("upperWarnAround")]
  16. public AlarmThresholdSetting UpperWarningPercentage = AlarmThresholdSetting.Disabled;
  17. [DataField("lowerWarnAround")]
  18. public AlarmThresholdSetting LowerWarningPercentage = AlarmThresholdSetting.Disabled;
  19. }
  20. [Serializable, NetSerializable, DataDefinition]
  21. public sealed partial class AtmosAlarmThreshold
  22. {
  23. [DataField("ignore")]
  24. public bool Ignore;
  25. [DataField("upperBound")]
  26. private AlarmThresholdSetting _upperBound = AlarmThresholdSetting.Disabled;
  27. [DataField("lowerBound")]
  28. private AlarmThresholdSetting _lowerBound = AlarmThresholdSetting.Disabled;
  29. [DataField("upperWarnAround")]
  30. public AlarmThresholdSetting UpperWarningPercentage = AlarmThresholdSetting.Disabled;
  31. [DataField("lowerWarnAround")]
  32. public AlarmThresholdSetting LowerWarningPercentage = AlarmThresholdSetting.Disabled;
  33. public AlarmThresholdSetting UpperBound
  34. {
  35. get => _upperBound;
  36. set
  37. {
  38. // Because the warnings are stored as percentages of the bounds,
  39. // Make a copy of the calculated bounds, so that the real warning amount
  40. // doesn't change value when user changes the bounds
  41. var oldWarning = UpperWarningBound;
  42. _upperBound = value;
  43. UpperWarningBound = oldWarning;
  44. }
  45. }
  46. public AlarmThresholdSetting LowerBound
  47. {
  48. get => _lowerBound;
  49. set
  50. {
  51. // Because the warnings are stored as percentages of the bounds,
  52. // Make a copy of the calculated bounds, so that the real warning amount
  53. // doesn't change value when user changes the bounds
  54. var oldWarning = LowerWarningBound;
  55. _lowerBound = value;
  56. LowerWarningBound = oldWarning;
  57. }
  58. }
  59. [ViewVariables]
  60. public AlarmThresholdSetting UpperWarningBound
  61. {
  62. get => CalculateWarningBound(AtmosMonitorThresholdBound.Upper);
  63. set => UpperWarningPercentage = CalculateWarningPercentage(AtmosMonitorThresholdBound.Upper, value);
  64. }
  65. [ViewVariables]
  66. public AlarmThresholdSetting LowerWarningBound
  67. {
  68. get => CalculateWarningBound(AtmosMonitorThresholdBound.Lower);
  69. set => LowerWarningPercentage = CalculateWarningPercentage(AtmosMonitorThresholdBound.Lower, value);
  70. }
  71. public AtmosAlarmThreshold()
  72. {
  73. }
  74. public AtmosAlarmThreshold(AtmosAlarmThreshold other)
  75. {
  76. Ignore = other.Ignore;
  77. UpperBound = other.UpperBound;
  78. LowerBound = other.LowerBound;
  79. UpperWarningPercentage = other.UpperWarningPercentage;
  80. LowerWarningPercentage = other.LowerWarningPercentage;
  81. }
  82. public AtmosAlarmThreshold(AtmosAlarmThresholdPrototype proto)
  83. {
  84. Ignore = proto.Ignore;
  85. UpperBound = proto.UpperBound;
  86. LowerBound = proto.LowerBound;
  87. UpperWarningPercentage = proto.UpperWarningPercentage;
  88. LowerWarningPercentage = proto.LowerWarningPercentage;
  89. }
  90. // utility function to check a threshold against some calculated value
  91. public bool CheckThreshold(float value, out AtmosAlarmType state)
  92. {
  93. return CheckThreshold(value, out state, out AtmosMonitorThresholdBound _);
  94. }
  95. // utility function to check a threshold against some calculated value. If the output state
  96. // is normal, whichFailed should not be used..
  97. public bool CheckThreshold(float value, out AtmosAlarmType state, out AtmosMonitorThresholdBound whichFailed)
  98. {
  99. state = AtmosAlarmType.Normal;
  100. whichFailed = AtmosMonitorThresholdBound.Upper;
  101. if (Ignore)
  102. {
  103. return false;
  104. }
  105. if (value >= UpperBound)
  106. {
  107. state = AtmosAlarmType.Danger;
  108. whichFailed = AtmosMonitorThresholdBound.Upper;
  109. return true;
  110. }
  111. if(value <= LowerBound)
  112. {
  113. state = AtmosAlarmType.Danger;
  114. whichFailed = AtmosMonitorThresholdBound.Lower;
  115. return true;
  116. }
  117. if (value >= UpperWarningBound)
  118. {
  119. state = AtmosAlarmType.Warning;
  120. whichFailed = AtmosMonitorThresholdBound.Upper;
  121. return true;
  122. }
  123. if (value <= LowerWarningBound)
  124. {
  125. state = AtmosAlarmType.Warning;
  126. whichFailed = AtmosMonitorThresholdBound.Lower;
  127. return true;
  128. }
  129. return false;
  130. }
  131. /// Warnings are stored in prototypes as a percentage, for ease of content
  132. /// maintainers. This recalculates a new "real" value of the warning
  133. /// threshold, for use in the actual atmosphereic checks.
  134. public AlarmThresholdSetting CalculateWarningBound(AtmosMonitorThresholdBound bound)
  135. {
  136. switch (bound)
  137. {
  138. case AtmosMonitorThresholdBound.Upper:
  139. return new AlarmThresholdSetting {
  140. Enabled = UpperWarningPercentage.Enabled,
  141. Value = UpperBound.Value * UpperWarningPercentage.Value};
  142. case AtmosMonitorThresholdBound.Lower:
  143. return new AlarmThresholdSetting {
  144. Enabled = LowerWarningPercentage.Enabled,
  145. Value = LowerBound.Value * LowerWarningPercentage.Value};
  146. default:
  147. // Unreachable.
  148. return new AlarmThresholdSetting();
  149. }
  150. }
  151. public AlarmThresholdSetting CalculateWarningPercentage(AtmosMonitorThresholdBound bound, AlarmThresholdSetting warningBound)
  152. {
  153. switch (bound)
  154. {
  155. case AtmosMonitorThresholdBound.Upper:
  156. return new AlarmThresholdSetting {
  157. Enabled = UpperWarningPercentage.Enabled,
  158. Value = UpperBound.Value == 0 ? 0 : warningBound.Value / UpperBound.Value};
  159. case AtmosMonitorThresholdBound.Lower:
  160. return new AlarmThresholdSetting {
  161. Enabled = LowerWarningPercentage.Enabled,
  162. Value = LowerBound.Value == 0 ? 0 : warningBound.Value / LowerBound.Value };
  163. default:
  164. // Unreachable.
  165. return new AlarmThresholdSetting();
  166. }
  167. }
  168. // Enable or disable a single threshold setting
  169. public void SetEnabled(AtmosMonitorLimitType whichLimit, bool isEnabled)
  170. {
  171. switch(whichLimit)
  172. {
  173. case AtmosMonitorLimitType.LowerDanger:
  174. LowerBound = LowerBound.WithEnabled(isEnabled);
  175. break;
  176. case AtmosMonitorLimitType.LowerWarning:
  177. LowerWarningPercentage = LowerWarningPercentage.WithEnabled(isEnabled);
  178. break;
  179. case AtmosMonitorLimitType.UpperWarning:
  180. UpperWarningPercentage = UpperWarningPercentage.WithEnabled(isEnabled);
  181. break;
  182. case AtmosMonitorLimitType.UpperDanger:
  183. UpperBound = UpperBound.WithEnabled(isEnabled);
  184. break;
  185. }
  186. }
  187. // Set the limit for a threshold. Will clamp other limits appropriately to
  188. // enforce that LowerBound <= LowerWarningBound <= UpperWarningBound <= UpperBound
  189. public void SetLimit(AtmosMonitorLimitType whichLimit, float limit)
  190. {
  191. if (limit <= 0)
  192. {
  193. // Unit tests expect that setting value of 0 or less should not change the limit.
  194. // Feels a bit strange, but does avoid a bug where the warning data (stored as a
  195. // percentage of danger bounds) is lost when setting the danger threshold to zero
  196. return;
  197. }
  198. switch (whichLimit)
  199. {
  200. case AtmosMonitorLimitType.LowerDanger:
  201. LowerBound = LowerBound.WithThreshold(limit);
  202. LowerWarningBound = LowerWarningBound.WithThreshold(Math.Max(limit, LowerWarningBound.Value));
  203. UpperWarningBound = UpperWarningBound.WithThreshold(Math.Max(limit, UpperWarningBound.Value));
  204. UpperBound = UpperBound.WithThreshold(Math.Max(limit, UpperBound.Value));
  205. break;
  206. case AtmosMonitorLimitType.LowerWarning:
  207. LowerBound = LowerBound.WithThreshold(Math.Min(LowerBound.Value, limit));
  208. LowerWarningBound = LowerWarningBound.WithThreshold(limit);
  209. UpperWarningBound = UpperWarningBound.WithThreshold(Math.Max(limit, UpperWarningBound.Value));
  210. UpperBound = UpperBound.WithThreshold(Math.Max(limit, UpperBound.Value));
  211. break;
  212. case AtmosMonitorLimitType.UpperWarning:
  213. LowerBound = LowerBound.WithThreshold(Math.Min(LowerBound.Value, limit));
  214. LowerWarningBound = LowerWarningBound.WithThreshold(Math.Min(LowerWarningBound.Value, limit));
  215. UpperWarningBound = UpperWarningBound.WithThreshold(limit);
  216. UpperBound = UpperBound.WithThreshold(Math.Max(limit, UpperBound.Value));
  217. break;
  218. case AtmosMonitorLimitType.UpperDanger:
  219. LowerBound = LowerBound.WithThreshold(Math.Min(LowerBound.Value, limit));
  220. LowerWarningBound = LowerWarningBound.WithThreshold(Math.Min(LowerWarningBound.Value, limit));
  221. UpperWarningBound = UpperWarningBound.WithThreshold(Math.Min(UpperWarningBound.Value, limit));
  222. UpperBound = UpperBound.WithThreshold(limit);
  223. break;
  224. }
  225. }
  226. /// <summary>
  227. /// Iterates through the changes that these threshold settings would make from a
  228. /// previous instance. Basically, diffs the two settings.
  229. /// </summary>
  230. public IEnumerable<AtmosAlarmThresholdChange> GetChanges(AtmosAlarmThreshold previous)
  231. {
  232. if (LowerBound != previous.LowerBound)
  233. yield return new AtmosAlarmThresholdChange(AtmosMonitorLimitType.LowerDanger, previous.LowerBound, LowerBound);
  234. if (LowerWarningBound != previous.LowerWarningBound)
  235. yield return new AtmosAlarmThresholdChange(AtmosMonitorLimitType.LowerWarning, previous.LowerWarningBound, LowerWarningBound);
  236. if (UpperBound != previous.UpperBound)
  237. yield return new AtmosAlarmThresholdChange(AtmosMonitorLimitType.UpperDanger, previous.UpperBound, UpperBound);
  238. if (UpperWarningBound != previous.UpperWarningBound)
  239. yield return new AtmosAlarmThresholdChange(AtmosMonitorLimitType.UpperWarning, previous.UpperWarningBound, UpperWarningBound);
  240. }
  241. }
  242. /// <summary>
  243. /// A change of a single value between two AtmosAlarmThreshold, for a given AtmosMonitorLimitType
  244. /// </summary>
  245. public readonly struct AtmosAlarmThresholdChange
  246. {
  247. /// <summary>
  248. /// The type of change between the two threshold sets
  249. /// </summary>
  250. public readonly AtmosMonitorLimitType Type;
  251. /// <summary>
  252. /// The value in the old threshold set
  253. /// </summary>
  254. public readonly AlarmThresholdSetting? Previous;
  255. /// <summary>
  256. /// The value in the new threshold set
  257. /// </summary>
  258. public readonly AlarmThresholdSetting Current;
  259. public AtmosAlarmThresholdChange(AtmosMonitorLimitType type, AlarmThresholdSetting? previous, AlarmThresholdSetting current)
  260. {
  261. Type = type;
  262. Previous = previous;
  263. Current = current;
  264. }
  265. }
  266. [DataDefinition, Serializable]
  267. public readonly partial struct AlarmThresholdSetting: IEquatable<AlarmThresholdSetting>
  268. {
  269. [DataField("enabled")]
  270. public bool Enabled { get; init; } = true;
  271. [DataField("threshold")]
  272. public float Value { get; init; } = 1;
  273. public static AlarmThresholdSetting Disabled = new() {Enabled = false, Value = 0};
  274. public AlarmThresholdSetting()
  275. {
  276. }
  277. public static bool operator <=(float a, AlarmThresholdSetting b)
  278. {
  279. return b.Enabled && a <= b.Value;
  280. }
  281. public static bool operator >=(float a, AlarmThresholdSetting b)
  282. {
  283. return b.Enabled && a >= b.Value;
  284. }
  285. public AlarmThresholdSetting WithThreshold(float threshold)
  286. {
  287. return this with {Value = threshold};
  288. }
  289. public AlarmThresholdSetting WithEnabled(bool enabled)
  290. {
  291. return this with {Enabled = enabled};
  292. }
  293. public bool Equals(AlarmThresholdSetting other)
  294. {
  295. if (Enabled != other.Enabled)
  296. return false;
  297. if (Value != other.Value)
  298. return false;
  299. return true;
  300. }
  301. public static bool operator ==(AlarmThresholdSetting lhs, AlarmThresholdSetting rhs)
  302. {
  303. return lhs.Equals(rhs);
  304. }
  305. public static bool operator !=(AlarmThresholdSetting lhs, AlarmThresholdSetting rhs)
  306. {
  307. return !lhs.Equals(rhs);
  308. }
  309. public override int GetHashCode()
  310. {
  311. return HashCode.Combine(Enabled, Value);
  312. }
  313. }
  314. public enum AtmosMonitorThresholdBound
  315. {
  316. Upper,
  317. Lower
  318. }
  319. public enum AtmosMonitorLimitType //<todo.eoin Very similar to the above...
  320. {
  321. LowerDanger,
  322. LowerWarning,
  323. UpperWarning,
  324. UpperDanger,
  325. }
  326. // not really used in the prototype but in code,
  327. // to differentiate between the different
  328. // fields you can find this prototype in
  329. public enum AtmosMonitorThresholdType
  330. {
  331. Temperature,
  332. Pressure,
  333. Gas
  334. }
  335. [Serializable, NetSerializable]
  336. public enum AtmosMonitorVisuals : byte
  337. {
  338. AlarmType,
  339. }