using System; using System.Diagnostics; namespace PKHeX.Core { internal sealed class OPowerFlagSet { public readonly OPower6Type Identifier; public readonly int Count; public int Offset { get; set; } public int BaseCount => Math.Min(3, Count); public bool HasOPowerS => Count > 3; public bool HasOPowerMAX => Count == 5; public OPowerFlagSet(int count, OPower6Type ident) { Identifier = ident; Count = count; } public int GetOPowerLevel(ReadOnlySpan data) { for (int i = 0; i < BaseCount; i++) { if (GetFlag(data, i)) continue; Debug.WriteLine($"Fetched {Identifier}: {i}"); return i; } Debug.WriteLine($"Fetched {Identifier}: {BaseCount}"); return BaseCount; } public void SetOPowerLevel(Span data, int value) { Debug.WriteLine($"Setting {Identifier}: {value}"); for (int i = 0; i < BaseCount; i++) SetFlag(data, i, i + 1 <= value); Debug.Assert(value == GetOPowerLevel(data)); } public bool GetOPowerS(ReadOnlySpan data) => HasOPowerS && GetFlag(data, 3); public bool GetOPowerMAX(ReadOnlySpan data) => HasOPowerMAX && GetFlag(data, 4); public void SetOPowerS(Span data, bool value) => SetFlag(data, 3, value); public void SetOPowerMAX(Span data, bool value) => SetFlag(data, 4, value); private bool GetFlag(ReadOnlySpan data, int index) { return data[Offset + index] == (byte)OPowerFlagState.Unlocked; } private void SetFlag(Span data, int index, bool value) { if (index < Count) data[Offset + index] = (byte)(value ? OPowerFlagState.Unlocked : OPowerFlagState.Locked); } } }