PKHeX/PKHeX.Core/Legality/RNG/Locks/NPCLock.cs
Kurt f632aedd15
Encounter Templates: Searching and Creating (#3955)
We implement simple state machine iterators to iterate through every split type encounter array, and more finely control the path we iterate through. And, by using generics, we can have the compiler generate optimized code to avoid virtual calls.

In addition to this, we shift away from the big-5 encounter types and not inherit from an abstract class. This allows for creating a PK* of a specific type and directly writing properties (no virtual calls). Plus we can now fine-tune each encounter type to call specific code, and not have to worry about future game encounter types bothering the generation routines.
2023-08-12 16:01:16 -07:00

62 lines
1.6 KiB
C#

namespace PKHeX.Core;
/// <summary>
/// Locks associated to a given NPC PKM that appears before a <see cref="IShadow3"/>.
/// </summary>
public readonly record struct NPCLock
{
private readonly byte Nature;
private readonly byte Gender;
private readonly byte Ratio;
private readonly byte State;
private readonly ushort Species;
public int FramesConsumed => Seen ? 5 : 7;
public bool Seen => State > 1;
public bool Shadow => State != 0;
public (byte Nature, byte Gender) GetLock => (Nature, Gender);
// Not-Shadow
public NPCLock(ushort s, byte n, byte g, byte r)
{
Species = s;
Nature = n;
Gender = g;
Ratio = r;
}
// Shadow
public NPCLock(ushort s, bool seen = false)
{
Species = s;
State = seen ? (byte)2 : (byte)1;
}
public bool MatchesLock(uint PID)
{
if (Shadow && Nature == 0) // Non-locked shadow
return true;
if (Gender != 2 && Gender != ((PID & 0xFF) < Ratio ? 1 : 0))
return false;
if (Nature != PID % 25)
return false;
return true;
}
#if DEBUG
public override string ToString()
{
var sb = new System.Text.StringBuilder(64);
sb.Append((Species)Species);
if (State != 0)
sb.Append(" (Shadow)");
if (Seen)
sb.Append(" [Seen]");
sb.Append(" - ");
sb.Append("Nature: ").Append((Nature)Nature);
if (Gender != 2)
sb.Append(", ").Append("Gender: ").Append(Gender);
return sb.ToString();
}
#endif
}