2017-11-26 06:28:38 +00:00
|
|
|
|
using System.Linq;
|
2021-08-05 22:18:43 +00:00
|
|
|
|
using static PKHeX.Core.SlotType;
|
2017-11-26 06:28:38 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
2021-03-14 18:28:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// RNG Encounter Slot Ranges to convert the [0,100) value into a slot index.
|
|
|
|
|
/// </summary>
|
2017-05-15 06:21:34 +00:00
|
|
|
|
public static class SlotRange
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static readonly Range[] H_OldRod = GetRanges(70, 30);
|
|
|
|
|
private static readonly Range[] H_GoodRod = GetRanges(60, 20, 20);
|
2017-11-29 04:56:21 +00:00
|
|
|
|
private static readonly Range[] H_SuperRod = GetRanges(40, 40, 15, 4, 1);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static readonly Range[] H_Surf = GetRanges(60, 30, 5, 4, 1);
|
|
|
|
|
private static readonly Range[] H_Regular = GetRanges(20, 20, 10, 10, 10, 10, 5, 5, 4, 4, 1, 1);
|
2017-05-15 06:21:34 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static readonly Range[] J_SuperRod = GetRanges(40, 40, 15, 4, 1);
|
2017-11-29 04:56:21 +00:00
|
|
|
|
private static readonly Range[] K_SuperRod = GetRanges(40, 30, 15, 10, 5);
|
2017-11-26 06:28:38 +00:00
|
|
|
|
private static readonly Range[] K_BCC = GetRanges(5,5,5,5, 10,10,10,10, 20,20).Reverse().ToArray();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static readonly Range[] K_Headbutt = GetRanges(50, 15, 15, 10, 5, 5);
|
2017-05-15 06:21:34 +00:00
|
|
|
|
|
2021-08-05 22:18:43 +00:00
|
|
|
|
private const int Invalid = -1; // all slots are [0,X], unsigned. This will always result in a non-match.
|
|
|
|
|
|
2021-03-14 18:28:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the <see cref="INumberedSlot.SlotNumber"/> from the raw 16bit <see cref="rand"/> seed half.
|
|
|
|
|
/// </summary>
|
2021-01-02 01:08:49 +00:00
|
|
|
|
public static int GetSlot(SlotType type, uint rand, FrameType t) => t switch
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
2021-01-02 01:08:49 +00:00
|
|
|
|
FrameType.MethodH => HSlot(type, rand),
|
|
|
|
|
FrameType.MethodJ => JSlot(type, rand),
|
|
|
|
|
FrameType.MethodK => KSlot(type, rand),
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => Invalid,
|
2021-01-02 01:08:49 +00:00
|
|
|
|
};
|
2017-05-15 06:21:34 +00:00
|
|
|
|
|
2021-03-14 18:28:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the <see cref="INumberedSlot.SlotNumber"/> from the raw 16bit <see cref="rand"/> seed half.
|
|
|
|
|
/// </summary>
|
2017-05-15 06:21:34 +00:00
|
|
|
|
private static int HSlot(SlotType type, uint rand)
|
|
|
|
|
{
|
|
|
|
|
var ESV = rand % 100;
|
2021-08-05 22:18:43 +00:00
|
|
|
|
if ((type & Swarm) != 0)
|
|
|
|
|
return ESV < 50 ? 0 : Invalid;
|
2021-02-02 07:43:52 +00:00
|
|
|
|
|
2019-10-08 01:40:09 +00:00
|
|
|
|
return type switch
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
2021-08-05 22:18:43 +00:00
|
|
|
|
Old_Rod => CalcSlot(ESV, H_OldRod),
|
|
|
|
|
Good_Rod => CalcSlot(ESV, H_GoodRod),
|
|
|
|
|
Super_Rod => CalcSlot(ESV, H_SuperRod),
|
|
|
|
|
Rock_Smash => CalcSlot(ESV, H_Surf),
|
|
|
|
|
Surf => CalcSlot(ESV, H_Surf),
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => CalcSlot(ESV, H_Regular),
|
2019-10-08 01:40:09 +00:00
|
|
|
|
};
|
2017-05-15 06:21:34 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-14 18:28:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the <see cref="INumberedSlot.SlotNumber"/> from the raw 16bit <see cref="rand"/> seed half.
|
|
|
|
|
/// </summary>
|
2017-12-02 00:33:03 +00:00
|
|
|
|
private static int KSlot(SlotType type, uint rand)
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
|
|
|
|
var ESV = rand % 100;
|
2020-12-25 18:58:33 +00:00
|
|
|
|
return type switch
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
2021-08-05 22:18:43 +00:00
|
|
|
|
Rock_Smash or Surf => CalcSlot(ESV, H_Surf),
|
|
|
|
|
Old_Rod or Good_Rod or Super_Rod => CalcSlot(ESV, K_SuperRod),
|
|
|
|
|
BugContest => CalcSlot(ESV, K_BCC),
|
|
|
|
|
Headbutt or (Headbutt | Special) => CalcSlot(ESV, K_Headbutt),
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => CalcSlot(ESV, H_Regular),
|
2020-12-25 18:58:33 +00:00
|
|
|
|
};
|
2017-05-15 06:21:34 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-14 18:28:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the <see cref="INumberedSlot.SlotNumber"/> from the raw 16bit <see cref="rand"/> seed half.
|
|
|
|
|
/// </summary>
|
2017-12-02 00:33:03 +00:00
|
|
|
|
private static int JSlot(SlotType type, uint rand)
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
|
|
|
|
uint ESV = rand / 656;
|
2020-12-25 18:58:33 +00:00
|
|
|
|
return type switch
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
2021-08-05 22:18:43 +00:00
|
|
|
|
Old_Rod or Rock_Smash or Surf => CalcSlot(ESV, H_Surf),
|
|
|
|
|
Good_Rod or Super_Rod => CalcSlot(ESV, J_SuperRod),
|
|
|
|
|
HoneyTree => 0,
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => CalcSlot(ESV, H_Regular),
|
2020-12-25 18:58:33 +00:00
|
|
|
|
};
|
2017-05-15 06:21:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 02:32:35 +00:00
|
|
|
|
private readonly record struct Range(uint Min, uint Max);
|
2017-05-15 06:21:34 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static Range[] GetRanges(params uint[] rates)
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
|
|
|
|
var len = rates.Length;
|
|
|
|
|
var arr = new Range[len];
|
|
|
|
|
uint sum = 0;
|
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
|
arr[i] = new Range(sum, (sum += rates[i]) - 1);
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int CalcSlot(uint esv, Range[] ranges)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < ranges.Length; ++i)
|
2018-10-27 15:53:09 +00:00
|
|
|
|
{
|
2021-12-10 03:30:12 +00:00
|
|
|
|
var (min, max) = ranges[i];
|
|
|
|
|
if (esv >= min && esv <= max)
|
2017-05-15 06:21:34 +00:00
|
|
|
|
return i;
|
2018-10-27 15:53:09 +00:00
|
|
|
|
}
|
2021-08-05 22:18:43 +00:00
|
|
|
|
return Invalid;
|
2017-05-15 06:21:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-30 07:20:49 +00:00
|
|
|
|
public static int GetLevel(EncounterSlot slot, LeadRequired lead, uint lvlrand)
|
2017-05-15 06:21:34 +00:00
|
|
|
|
{
|
2017-11-30 07:20:49 +00:00
|
|
|
|
if (lead == LeadRequired.PressureHustleSpirit)
|
2017-11-30 05:31:52 +00:00
|
|
|
|
return slot.LevelMax;
|
2021-08-05 22:18:43 +00:00
|
|
|
|
if (slot.IsFixedLevel)
|
2017-11-30 05:31:52 +00:00
|
|
|
|
return slot.LevelMin;
|
|
|
|
|
int delta = slot.LevelMax - slot.LevelMin + 1;
|
|
|
|
|
var adjust = (int)(lvlrand % delta);
|
|
|
|
|
|
2017-11-30 07:20:49 +00:00
|
|
|
|
return slot.LevelMin + adjust;
|
2017-11-30 05:31:52 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2020-05-26 23:59:47 +00:00
|
|
|
|
#pragma warning disable IDE0060, RCS1163 // Unused parameter.
|
2021-07-30 21:05:23 +00:00
|
|
|
|
public static bool GetIsEncounterable<T>(T slot, FrameType frameType, int rand, LeadRequired lead) where T : ISlotRNGType
|
2020-05-26 23:59:47 +00:00
|
|
|
|
#pragma warning restore IDE0060, RCS1163 // Unused parameter.
|
2017-11-30 05:31:52 +00:00
|
|
|
|
{
|
2021-07-30 21:05:23 +00:00
|
|
|
|
if (slot.Type.IsSweetScentType())
|
2017-11-30 07:20:49 +00:00
|
|
|
|
return true;
|
2017-11-30 05:31:52 +00:00
|
|
|
|
return true; // todo
|
2018-03-11 03:40:57 +00:00
|
|
|
|
//return GetCanEncounter(slot, frameType, rand, lead);
|
2017-11-30 07:20:49 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2020-05-26 23:59:47 +00:00
|
|
|
|
// ReSharper disable once UnusedMember.Global
|
2021-07-30 21:05:23 +00:00
|
|
|
|
public static bool GetCanEncounter<T>(T slot, FrameType frameType, int rand, LeadRequired lead) where T : ISlotRNGType
|
2017-11-30 07:20:49 +00:00
|
|
|
|
{
|
|
|
|
|
int proc = frameType == FrameType.MethodJ ? rand / 656 : rand % 100;
|
2021-07-30 21:05:23 +00:00
|
|
|
|
var stype = slot.Type;
|
2021-08-05 22:18:43 +00:00
|
|
|
|
if (stype == Rock_Smash)
|
2017-11-30 07:20:49 +00:00
|
|
|
|
return proc < 60;
|
|
|
|
|
if (frameType == FrameType.MethodH)
|
|
|
|
|
return true; // fishing encounters are disjointed by the hooked message.
|
2021-03-14 23:16:55 +00:00
|
|
|
|
return GetCanEncounterFish(lead, stype, proc);
|
|
|
|
|
}
|
2017-11-30 07:20:49 +00:00
|
|
|
|
|
2021-03-14 23:16:55 +00:00
|
|
|
|
private static bool GetCanEncounterFish(LeadRequired lead, SlotType stype, int proc) => stype switch
|
|
|
|
|
{
|
|
|
|
|
// Lead:None => can be suction cups
|
2021-08-05 22:18:43 +00:00
|
|
|
|
Old_Rod => proc switch
|
2017-11-30 07:20:49 +00:00
|
|
|
|
{
|
2021-03-14 23:16:55 +00:00
|
|
|
|
< 25 => true,
|
|
|
|
|
< 50 => lead == LeadRequired.None,
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => false,
|
2021-03-14 23:16:55 +00:00
|
|
|
|
},
|
2021-08-05 22:18:43 +00:00
|
|
|
|
Good_Rod => proc switch
|
2017-11-30 07:20:49 +00:00
|
|
|
|
{
|
2021-03-14 23:16:55 +00:00
|
|
|
|
< 50 => true,
|
|
|
|
|
< 75 => lead == LeadRequired.None,
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => false,
|
2021-03-14 23:16:55 +00:00
|
|
|
|
},
|
2021-08-05 22:18:43 +00:00
|
|
|
|
Super_Rod => proc < 75 || lead == LeadRequired.None,
|
2021-03-14 23:16:55 +00:00
|
|
|
|
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => false,
|
2021-03-14 23:16:55 +00:00
|
|
|
|
};
|
2017-12-02 00:33:03 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks both Static and Magnet Pull ability type selection encounters to see if the encounter can be selected.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="slot">Slot Data</param>
|
2021-08-05 22:18:43 +00:00
|
|
|
|
/// <param name="esv">Rand16 value for the call</param>
|
2017-12-02 00:33:03 +00:00
|
|
|
|
/// <returns>Slot number from the slot data if the slot is selected on this frame, else an invalid slot value.</returns>
|
2021-08-05 22:18:43 +00:00
|
|
|
|
internal static int GetSlotStaticMagnet<T>(T slot, uint esv) where T : EncounterSlot, IMagnetStatic, INumberedSlot
|
2017-12-02 00:33:03 +00:00
|
|
|
|
{
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (slot.StaticCount > 0 && slot.StaticIndex >= 0)
|
2017-12-02 00:33:03 +00:00
|
|
|
|
{
|
2021-08-05 22:18:43 +00:00
|
|
|
|
var index = esv % slot.StaticCount;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (index == slot.StaticIndex)
|
2017-12-02 00:33:03 +00:00
|
|
|
|
return slot.SlotNumber;
|
|
|
|
|
}
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (slot.MagnetPullCount > 0 && slot.MagnetPullIndex >= 0)
|
2017-12-02 00:33:03 +00:00
|
|
|
|
{
|
2021-08-05 22:18:43 +00:00
|
|
|
|
var index = esv % slot.MagnetPullCount;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (index == slot.MagnetPullIndex)
|
2017-12-02 00:33:03 +00:00
|
|
|
|
return slot.SlotNumber;
|
|
|
|
|
}
|
2021-08-05 22:18:43 +00:00
|
|
|
|
return Invalid;
|
2017-12-02 00:33:03 +00:00
|
|
|
|
}
|
2017-05-15 06:21:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|