2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2017-05-29 07:48:25 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 3 Mystery Gift Template File
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This is fabricated data built to emulate the future generation Mystery Gift objects.
|
|
|
|
/// Data here is not stored in any save file and cannot be naturally exported.
|
|
|
|
/// </remarks>
|
2023-12-04 04:13:20 +00:00
|
|
|
public sealed class WC3(bool Fateful = false)
|
|
|
|
: MysteryGift, IRibbonSetEvent3, ILangNicknamedTemplate, IRandomCorrelation
|
2017-05-29 07:48:25 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public override MysteryGift Clone() => (WC3)MemberwiseClone();
|
|
|
|
|
2017-10-24 06:12:58 +00:00
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// Matched <see cref="PIDIV"/> Type
|
2017-10-24 06:12:58 +00:00
|
|
|
/// </summary>
|
2023-01-22 04:02:33 +00:00
|
|
|
public PIDType Method { get; init; }
|
2023-08-12 23:01:16 +00:00
|
|
|
public PIDType GetSuggestedCorrelation() => Method;
|
|
|
|
public bool IsCompatible(PIDType type, PKM pk)
|
|
|
|
{
|
|
|
|
if (type == Method)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// forced shiny eggs, when hatched, can lose their detectable correlation.
|
|
|
|
if (!IsEgg || pk.IsEgg)
|
|
|
|
return false;
|
|
|
|
return type is PIDType.BACD_R_S or PIDType.BACD_U_S;
|
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
|
|
|
|
private const ushort UnspecifiedID = ushort.MaxValue;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
public override string OT_Name { get; set; } = string.Empty;
|
|
|
|
public int OT_Gender { get; init; } = 3;
|
2023-01-22 04:02:33 +00:00
|
|
|
public override uint ID32 { get => (uint)(SID16 << 16 | TID16); set => (SID16, TID16) = ((ushort)(value >> 16), (ushort)value); }
|
|
|
|
public override ushort TID16 { get; set; } = UnspecifiedID;
|
|
|
|
public override ushort SID16 { get; set; } = UnspecifiedID;
|
2022-06-18 18:04:24 +00:00
|
|
|
public override int Location { get; set; } = 255;
|
|
|
|
public override int EggLocation { get => 0; set {} }
|
|
|
|
public override GameVersion Version { get; set; }
|
|
|
|
public int Language { get; init; } = -1;
|
2022-08-27 06:43:36 +00:00
|
|
|
public override ushort Species { get; set; }
|
2022-06-18 18:04:24 +00:00
|
|
|
public override bool IsEgg { get; set; }
|
2022-08-22 00:34:32 +00:00
|
|
|
public override Moveset Moves { get; set; }
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool NotDistributed { get; init; }
|
|
|
|
public override Shiny Shiny { get; init; }
|
2023-12-04 04:13:20 +00:00
|
|
|
public override bool FatefulEncounter { get; } = Fateful; // Obedience Flag
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Mystery Gift Properties
|
|
|
|
public override int Generation => 3;
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
public override EntityContext Context => EntityContext.Gen3;
|
2022-06-18 18:04:24 +00:00
|
|
|
public override byte Level { get; set; }
|
|
|
|
public override int Ball { get; set; } = 4;
|
|
|
|
public override bool IsShiny => Shiny == Shiny.Always;
|
|
|
|
public override bool HasFixedIVs => false;
|
|
|
|
public bool RibbonEarth { get; set; }
|
|
|
|
public bool RibbonNational { get; set; }
|
|
|
|
public bool RibbonCountry { get; set; }
|
|
|
|
public bool RibbonChampionBattle { get; set; }
|
|
|
|
public bool RibbonChampionRegional { get; set; }
|
|
|
|
public bool RibbonChampionNational { get; set; }
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public string? Nickname { get; init; }
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Description
|
|
|
|
public override string CardTitle { get; set; } = "Generation 3 Event";
|
|
|
|
public override string CardHeader => CardTitle;
|
|
|
|
|
|
|
|
// Unused
|
|
|
|
public override bool GiftUsed { get; set; }
|
|
|
|
public override int CardID { get; set; }
|
|
|
|
public override bool IsItem { get; set; }
|
|
|
|
public override int ItemID { get; set; }
|
|
|
|
public override bool IsEntity { get; set; } = true;
|
|
|
|
public override bool Empty => false;
|
|
|
|
public override int Gender { get; set; }
|
2022-08-27 06:43:36 +00:00
|
|
|
public override byte Form { get; set; }
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Synthetic
|
|
|
|
private readonly int? _metLevel;
|
|
|
|
|
|
|
|
public int Met_Level
|
2017-05-29 07:48:25 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
get => _metLevel ?? (IsEgg ? 0 : Level);
|
|
|
|
init => _metLevel = value;
|
|
|
|
}
|
2017-06-30 02:32:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override AbilityPermission Ability => AbilityPermission.Any12;
|
2021-12-09 08:46:59 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public override PK3 ConvertToPKM(ITrainerInfo tr, EncounterCriteria criteria)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
PK3 pk = new()
|
2017-05-29 07:48:25 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Species = Species,
|
|
|
|
Met_Level = Met_Level,
|
|
|
|
Met_Location = Location,
|
|
|
|
Ball = 4,
|
|
|
|
|
|
|
|
// Ribbons
|
|
|
|
RibbonCountry = RibbonCountry,
|
|
|
|
RibbonNational = RibbonNational,
|
|
|
|
RibbonEarth = RibbonEarth,
|
|
|
|
RibbonChampionBattle = RibbonChampionBattle,
|
|
|
|
RibbonChampionRegional = RibbonChampionRegional,
|
|
|
|
RibbonChampionNational = RibbonChampionNational,
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
FatefulEncounter = FatefulEncounter,
|
2022-06-18 18:04:24 +00:00
|
|
|
Version = GetVersion(tr),
|
|
|
|
};
|
|
|
|
pk.EXP = Experience.GetEXP(Level, pk.PersonalInfo.EXPGrowth);
|
|
|
|
SetMoves(pk);
|
2017-07-16 01:36:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
bool hatchedEgg = IsEgg && tr.Generation != 3;
|
|
|
|
if (hatchedEgg)
|
|
|
|
{
|
|
|
|
SetForceHatchDetails(pk, tr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pk.OT_Gender = OT_Gender != 3 ? OT_Gender & 1 : tr.Gender;
|
2023-01-22 04:02:33 +00:00
|
|
|
pk.TID16 = TID16;
|
|
|
|
pk.SID16 = SID16;
|
2019-02-09 19:37:20 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.Language = (int)GetSafeLanguage((LanguageID)tr.Language);
|
|
|
|
pk.OT_Name = !string.IsNullOrWhiteSpace(OT_Name) ? OT_Name : tr.OT;
|
|
|
|
if (IsEgg)
|
2023-08-28 06:05:50 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.IsEgg = true; // lang should be set to japanese already
|
2023-08-28 06:05:50 +00:00
|
|
|
if (pk.OT_Trash[0] == 0xFF)
|
|
|
|
pk.OT_Name = "ゲーフリ";
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
pk.Nickname = Nickname ?? SpeciesName.GetSpeciesNameGeneration(Species, pk.Language, 3); // will be set to Egg nickname if appropriate by PK3 setter
|
2017-07-16 01:36:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var pi = pk.PersonalInfo;
|
|
|
|
pk.OT_Friendship = pk.IsEgg ? pi.HatchCycles : pi.BaseFriendship;
|
2019-02-09 19:37:20 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Generate PIDIV
|
|
|
|
SetPINGA(pk, criteria);
|
|
|
|
pk.HeldItem = 0; // clear, only random for Jirachi (?), no loss
|
2018-05-27 17:11:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Version == GameVersion.XD)
|
|
|
|
pk.FatefulEncounter = true; // pk3 is already converted from xk3
|
2020-10-04 15:59:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.RefreshChecksum();
|
|
|
|
return pk;
|
|
|
|
}
|
2019-02-09 19:37:20 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void SetForceHatchDetails(PK3 pk, ITrainerInfo sav)
|
|
|
|
{
|
|
|
|
pk.Language = (int)GetSafeLanguageNotEgg((LanguageID)sav.Language);
|
|
|
|
pk.OT_Name = sav.OT;
|
|
|
|
// ugly workaround for character table interactions
|
|
|
|
if (string.IsNullOrWhiteSpace(pk.OT_Name))
|
2019-02-09 19:37:20 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.Language = (int)LanguageID.English;
|
|
|
|
pk.OT_Name = "PKHeX";
|
2019-02-09 19:37:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.OT_Gender = sav.Gender;
|
2023-01-22 04:02:33 +00:00
|
|
|
pk.TID16 = sav.TID16;
|
|
|
|
pk.SID16 = sav.SID16;
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.Met_Location = pk.FRLG ? 146 /* Four Island */ : 32; // Route 117
|
|
|
|
pk.FatefulEncounter &= pk.FRLG; // clear flag for RSE
|
|
|
|
pk.Met_Level = 0; // hatched
|
|
|
|
}
|
2017-07-16 06:35:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private int GetVersion(ITrainerInfo sav)
|
|
|
|
{
|
|
|
|
if (Version != 0)
|
|
|
|
return (int) GetRandomVersion(Version);
|
|
|
|
bool gen3 = sav.Game <= 15 && GameVersion.Gen3.Contains((GameVersion)sav.Game);
|
|
|
|
return gen3 ? sav.Game : (int)GameVersion.R;
|
|
|
|
}
|
2018-12-30 06:24:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void SetMoves(PK3 pk)
|
|
|
|
{
|
2022-08-22 00:34:32 +00:00
|
|
|
if (!Moves.HasMoves) // not completely defined
|
2018-12-30 06:24:34 +00:00
|
|
|
{
|
2023-04-21 04:23:15 +00:00
|
|
|
var ver = (GameVersion)pk.Version;
|
|
|
|
ILearnSource ls = ver switch
|
|
|
|
{
|
|
|
|
GameVersion.R or GameVersion.S => LearnSource3RS.Instance,
|
|
|
|
GameVersion.FR => LearnSource3FR.Instance,
|
|
|
|
GameVersion.LG => LearnSource3LG.Instance,
|
|
|
|
_ => LearnSource3E.Instance,
|
|
|
|
};
|
|
|
|
|
|
|
|
var learn = ls.GetLearnset(Species, Form);
|
2022-08-27 06:43:36 +00:00
|
|
|
Span<ushort> moves = stackalloc ushort[4];
|
2023-04-21 04:23:15 +00:00
|
|
|
learn.SetEncounterMoves(Level, moves);
|
2022-08-27 06:43:36 +00:00
|
|
|
Moves = new(moves[0], moves[1], moves[2], moves[3]);
|
2018-12-30 06:24:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 00:34:32 +00:00
|
|
|
pk.SetMoves(Moves);
|
|
|
|
pk.SetMaximumPPCurrent(Moves);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2017-07-01 23:50:45 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void SetPINGA(PK3 pk, EncounterCriteria _)
|
|
|
|
{
|
|
|
|
var seed = Util.Rand32();
|
2023-01-22 04:02:33 +00:00
|
|
|
seed = TID16 == 06930 ? MystryMew.GetSeed(seed, Method) : GetSaneSeed(seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
PIDGenerator.SetValuesFromSeed(pk, Method, seed);
|
2023-08-28 06:05:50 +00:00
|
|
|
pk.RefreshAbility((int)(pk.EncryptionConstant & 1));
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-10-04 15:59:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private uint GetSaneSeed(uint seed) => Method switch
|
|
|
|
{
|
|
|
|
PIDType.BACD_R => seed & 0x0000FFFF, // u16
|
|
|
|
PIDType.BACD_R_S => seed & 0x000000FF, // u8
|
|
|
|
_ => seed,
|
|
|
|
};
|
2018-08-10 04:53:39 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private LanguageID GetSafeLanguage(LanguageID hatchLang)
|
|
|
|
{
|
|
|
|
if (IsEgg)
|
|
|
|
return LanguageID.Japanese;
|
|
|
|
return GetSafeLanguageNotEgg(hatchLang);
|
|
|
|
}
|
2017-07-16 06:35:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private LanguageID GetSafeLanguageNotEgg(LanguageID hatchLang)
|
|
|
|
{
|
|
|
|
if (Language != -1)
|
|
|
|
return (LanguageID) Language;
|
|
|
|
if (hatchLang < LanguageID.Korean && hatchLang != LanguageID.Hacked)
|
|
|
|
return hatchLang;
|
|
|
|
return LanguageID.English; // fallback
|
|
|
|
}
|
2017-07-16 06:35:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static GameVersion GetRandomVersion(GameVersion version)
|
|
|
|
{
|
|
|
|
if (version is <= GameVersion.CXD and > 0) // single game
|
|
|
|
return version;
|
|
|
|
|
|
|
|
return version switch
|
2018-12-27 09:00:08 +00:00
|
|
|
{
|
2022-09-25 02:23:23 +00:00
|
|
|
GameVersion.FRLG => Util.Rand.Next(2) == 0 ? GameVersion.FR : GameVersion.LG,
|
|
|
|
GameVersion.RS or GameVersion.RSE => Util.Rand.Next(2) == 0 ? GameVersion.R : GameVersion.S,
|
2022-06-18 18:04:24 +00:00
|
|
|
GameVersion.COLO or GameVersion.XD => GameVersion.CXD,
|
|
|
|
_ => throw new Exception($"Unknown GameVersion: {version}"),
|
|
|
|
};
|
|
|
|
}
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override bool IsMatchExact(PKM pk, EvoCriteria evo)
|
|
|
|
{
|
|
|
|
// Gen3 Version MUST match.
|
|
|
|
if (Version != 0 && !Version.Contains((GameVersion)pk.Version))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool hatchedEgg = IsEgg && !pk.IsEgg;
|
|
|
|
if (!hatchedEgg)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (SID16 != UnspecifiedID && SID16 != pk.SID16) return false;
|
|
|
|
if (TID16 != UnspecifiedID && TID16 != pk.TID16) return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (OT_Gender < 3 && OT_Gender != pk.OT_Gender) return false;
|
|
|
|
var wcOT = OT_Name;
|
|
|
|
if (!string.IsNullOrEmpty(wcOT))
|
2018-12-27 09:00:08 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (wcOT.Length > 7) // Colosseum MATTLE Ho-Oh
|
2018-12-27 09:00:08 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!GetIsValidOTMattleHoOh(wcOT, pk.OT_Name, pk is CK3))
|
2018-12-27 09:00:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (wcOT != pk.OT_Name)
|
|
|
|
{
|
2018-12-27 09:00:08 +00:00
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-12-27 09:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 07:28:47 +00:00
|
|
|
if (Form != evo.Form && !FormInfo.IsFormChangeable(Species, Form, pk.Form, Context, pk.Context))
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Language != -1 && Language != pk.Language) return false;
|
|
|
|
if (Ball != pk.Ball) return false;
|
2023-01-22 04:02:33 +00:00
|
|
|
if (FatefulEncounter != pk.FatefulEncounter)
|
2023-08-12 23:01:16 +00:00
|
|
|
return false;
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.IsNative)
|
|
|
|
{
|
|
|
|
if (hatchedEgg)
|
|
|
|
return true; // defer egg specific checks to later.
|
|
|
|
if (Met_Level != pk.Met_Level)
|
|
|
|
return false;
|
|
|
|
if (Location != pk.Met_Location)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pk.IsEgg)
|
|
|
|
return false;
|
|
|
|
if (Level > pk.Met_Level)
|
|
|
|
return false;
|
2023-07-06 04:14:09 +00:00
|
|
|
if (pk.Egg_Location != LocationEdits.GetNoneLocation(pk.Context))
|
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-01 02:57:52 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static bool GetIsValidOTMattleHoOh(ReadOnlySpan<char> wc, ReadOnlySpan<char> ot, bool ck3)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (ck3) // match original if still ck3, otherwise must be truncated 7char
|
2023-07-06 04:14:09 +00:00
|
|
|
return wc.SequenceEqual(ot);
|
2022-06-18 18:04:24 +00:00
|
|
|
return ot.Length == 7 && wc.StartsWith(ot, StringComparison.Ordinal);
|
2017-05-29 07:48:25 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-08-12 23:01:16 +00:00
|
|
|
protected override bool IsMatchDeferred(PKM pk) => false;
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchPartial(PKM pk) => false;
|
|
|
|
|
|
|
|
public string GetNickname(int language) => Nickname ?? string.Empty;
|
|
|
|
public bool GetIsNicknamed(int language) => Nickname != null;
|
|
|
|
public bool CanBeAnyLanguage() => false;
|
|
|
|
public bool CanHaveLanguage(int language) => Language == language;
|
|
|
|
public bool CanHandleOT(int language) => IsEgg;
|
2017-05-29 07:48:25 +00:00
|
|
|
}
|