Add EncounterTime interface for Gen2 encounters

This commit is contained in:
Kurt 2024-05-24 11:55:46 -05:00
parent 225e2e3ce5
commit a79581e965
5 changed files with 30 additions and 15 deletions

View file

@ -40,4 +40,11 @@ public sealed class EncounterSuggestionData : ISpeciesForm, IRelearn, ILevelRang
public int GetSuggestedMetLevel(PKM pk) => EncounterSuggestion.GetSuggestedMetLevel(pk, LevelMin);
public GroundTileType GetSuggestedGroundTile() => Encounter is IGroundTypeTile t ? t.GroundTile.GetIndex() : 0;
public bool HasGroundTile(byte format) => Encounter is IGroundTypeTile t && t.HasGroundTile(format);
public int GetSuggestedMetTimeOfDay()
{
if (Encounter is IEncounterTime time)
return time.GetRandomTime();
return 0;
}
}

View file

@ -10,7 +10,7 @@ namespace PKHeX.Core;
/// Referenced Area object contains Time data which is used for <see cref="GameVersion.C"/> origin data.
/// </remarks>
public sealed record EncounterSlot2(EncounterArea2 Parent, ushort Species, byte Form, byte LevelMin, byte LevelMax, byte SlotNumber)
: IEncounterable, IEncounterMatch, IEncounterConvertible<PK2>, INumberedSlot, IEncounterFormRandom
: IEncounterable, IEncounterMatch, IEncounterConvertible<PK2>, INumberedSlot, IEncounterFormRandom, IEncounterTime
{
public byte Generation => 2;
public EntityContext Context => EntityContext.Gen2;
@ -123,6 +123,7 @@ public sealed record EncounterSlot2(EncounterArea2 Parent, ushort Species, byte
}
public int GetRandomTime() => Parent.Time.RandomValidTime();
public EncounterTime EncounterTime => Parent.Time;
#endregion

View file

@ -4,7 +4,7 @@ namespace PKHeX.Core;
/// Generation 2 Static Encounter
/// </summary>
public sealed record EncounterStatic2(ushort Species, byte Level, GameVersion Version)
: IEncounterable, IEncounterMatch, IEncounterConvertible<PK2>, IHatchCycle, IFixedGender, IMoveset, IFixedIVSet
: IEncounterable, IEncounterMatch, IEncounterConvertible<PK2>, IHatchCycle, IFixedGender, IMoveset, IFixedIVSet, IEncounterTime
{
public byte Generation => 2;
public EntityContext Context => EntityContext.Gen2;
@ -65,7 +65,7 @@ public sealed record EncounterStatic2(ushort Species, byte Level, GameVersion Ve
pk.OriginalTrainerGender = tr.Gender;
pk.MetLevel = LevelMin;
pk.MetLocation = Location;
pk.MetTimeOfDay = EncounterTime.Any.RandomValidTime();
pk.MetTimeOfDay = GetRandomTime();
}
if (Moves.HasMoves)
@ -213,4 +213,13 @@ public sealed record EncounterStatic2(ushort Species, byte Level, GameVersion Ve
}
#endregion
public EncounterTime EncounterTime => EncounterTime.Any;
public int GetRandomTime()
{
if (IsEgg)
return 0;
return EncounterTime.RandomValidTime();
}
}

View file

@ -1,4 +1,4 @@
using System;
using System;
namespace PKHeX.Core;
@ -6,7 +6,7 @@ namespace PKHeX.Core;
/// Generation 2 Time of Encounter enum
/// </summary>
[Flags]
internal enum EncounterTime : byte
public enum EncounterTime : byte
{
Any = 0,
Morning = 1 << 1,
@ -14,7 +14,13 @@ internal enum EncounterTime : byte
Night = 1 << 3,
}
internal static class EncounterTimeExtension
public interface IEncounterTime
{
EncounterTime EncounterTime { get; }
public int GetRandomTime();
}
public static class EncounterTimeExtension
{
internal static bool Contains(this EncounterTime t1, int t2) => t1 == EncounterTime.Any || (t1 & (EncounterTime)(1 << t2)) != 0;

View file

@ -875,15 +875,7 @@ public sealed partial class PKMEditor : UserControl, IMainEditor
Entity.MetLocation = location;
TB_MetLevel.Text = encounter.GetSuggestedMetLevel(Entity).ToString();
CB_MetLocation.SelectedValue = (int)location;
var timeIndex = 0;
if (encounter.Encounter is { } enc && location is < 253 and not 0)
{
if (enc is EncounterSlot2 s2)
timeIndex = s2.GetRandomTime();
else
timeIndex = Util.Rand.Next(1, 4);
}
CB_MetTimeOfDay.SelectedIndex = timeIndex;
CB_MetTimeOfDay.SelectedIndex = location == 0 ? 0 : encounter.GetSuggestedMetTimeOfDay();
}
if (Entity.CurrentLevel < minLevel)