namespace GamePort.Game; /// 42A920 global energy and low-warning latch. Retained across ordinary /// level loads; the original TSK stores the scalar, not this warning latch. public sealed class PlayerEnergy { private float value = 20; public float Value { get => value; set { if (!float.IsFinite(value)) throw new ArgumentOutOfRangeException(nameof(value)); this.value = value; } } public bool LowWarning { get; private set; } public void Adjust(float delta, string level, Action? sound = null) { if (!float.IsFinite(delta)) throw new ArgumentOutOfRangeException(nameof(delta)); ArgumentNullException.ThrowIfNull(level); if (IsTutorial(level)) return; //The first x87 sum is compared before a float store. Sound80 precedes //the write; the helper then reloads the live global for the actual add. if (value > 0 && (double)value + delta <= 0) sound?.Invoke(80); var sum = (double)value + delta; value = (float)sum; if (sum > 100) { value = 100; LowWarning = false; return; } if (value < 0) value = 0; if (value < 19) { if (!LowWarning) { sound?.Invoke(182); LowWarning = true; } } else if (value > 20) LowWarning = false; } public static bool IsTutorial(string level) { ArgumentNullException.ThrowIfNull(level); if (level.Length != 4) return false; var code = ((uint)level[0] << 24) | ((uint)level[1] << 16) | ((uint)level[2] << 8) | level[3]; return code is >= 0x56523031 and <= 0x56523038; } }