2018-06-10 18:28:20 +00:00
|
|
|
using System;
|
2018-06-10 04:04:34 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
{
|
2020-09-07 20:51:13 +00:00
|
|
|
internal sealed class OPowerFlagSet
|
2018-06-10 04:04:34 +00:00
|
|
|
{
|
|
|
|
public readonly OPower6Type Identifier;
|
|
|
|
public readonly int Count;
|
2022-01-03 05:35:59 +00:00
|
|
|
public int Offset { get; set; }
|
2018-06-10 18:28:20 +00:00
|
|
|
public int BaseCount => Math.Min(3, Count);
|
|
|
|
public bool HasOPowerS => Count > 3;
|
|
|
|
public bool HasOPowerMAX => Count == 5;
|
2018-06-10 04:04:34 +00:00
|
|
|
|
|
|
|
public OPowerFlagSet(int count, OPower6Type ident)
|
|
|
|
{
|
|
|
|
Identifier = ident;
|
|
|
|
Count = count;
|
|
|
|
}
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
public int GetOPowerLevel(ReadOnlySpan<byte> data)
|
2018-06-10 04:04:34 +00:00
|
|
|
{
|
2018-06-10 18:28:20 +00:00
|
|
|
for (int i = 0; i < BaseCount; i++)
|
2018-06-10 04:04:34 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
if (GetFlag(data, i))
|
2018-06-10 04:04:34 +00:00
|
|
|
continue;
|
|
|
|
Debug.WriteLine($"Fetched {Identifier}: {i}");
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2018-06-10 18:28:20 +00:00
|
|
|
Debug.WriteLine($"Fetched {Identifier}: {BaseCount}");
|
|
|
|
return BaseCount;
|
2018-06-10 04:04:34 +00:00
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
public void SetOPowerLevel(Span<byte> data, int value)
|
2018-06-10 04:04:34 +00:00
|
|
|
{
|
|
|
|
Debug.WriteLine($"Setting {Identifier}: {value}");
|
2018-06-10 18:28:20 +00:00
|
|
|
for (int i = 0; i < BaseCount; i++)
|
2022-01-03 05:35:59 +00:00
|
|
|
SetFlag(data, i, i + 1 <= value);
|
|
|
|
Debug.Assert(value == GetOPowerLevel(data));
|
2018-06-10 04:04:34 +00:00
|
|
|
}
|
2018-06-10 18:28:20 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
public bool GetOPowerS(ReadOnlySpan<byte> data) => HasOPowerS && GetFlag(data, 3);
|
|
|
|
public bool GetOPowerMAX(ReadOnlySpan<byte> data) => HasOPowerMAX && GetFlag(data, 4);
|
|
|
|
public void SetOPowerS(Span<byte> data, bool value) => SetFlag(data, 3, value);
|
|
|
|
public void SetOPowerMAX(Span<byte> data, bool value) => SetFlag(data, 4, value);
|
2018-06-10 18:28:20 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
private bool GetFlag(ReadOnlySpan<byte> data, int index)
|
2018-06-10 18:28:20 +00:00
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
return data[Offset + index] == (byte)OPowerFlagState.Unlocked;
|
2018-06-10 18:28:20 +00:00
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
private void SetFlag(Span<byte> data, int index, bool value)
|
2018-06-10 18:28:20 +00:00
|
|
|
{
|
|
|
|
if (index < Count)
|
2022-01-03 05:35:59 +00:00
|
|
|
data[Offset + index] = (byte)(value ? OPowerFlagState.Unlocked : OPowerFlagState.Locked);
|
2018-06-10 18:28:20 +00:00
|
|
|
}
|
2018-06-10 04:04:34 +00:00
|
|
|
}
|
|
|
|
}
|