2017-05-28 04:17:53 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using static PKHeX . Core . LegalityCheckStrings ;
using static PKHeX . Core . LegalityAnalysis ;
namespace PKHeX.Core
{
2017-10-24 06:12:58 +00:00
/// <summary>
/// Logic to verify the current <see cref="PKM.RelearnMoves"/>.
/// </summary>
2017-05-28 04:17:53 +00:00
public static class VerifyRelearnMoves
{
2018-12-28 04:24:24 +00:00
private static readonly int [ ] RelearnEmpty = new int [ 4 ] ;
2017-06-18 01:37:19 +00:00
public static CheckResult [ ] VerifyRelearn ( PKM pkm , LegalInfo info )
2017-05-28 04:17:53 +00:00
{
2017-09-04 02:51:29 +00:00
if ( info . Generation < 6 | | pkm . VC1 )
2017-06-18 01:37:19 +00:00
return VerifyRelearnNone ( pkm , info ) ;
2017-05-28 04:17:53 +00:00
2018-01-26 17:19:20 +00:00
switch ( info . EncounterMatch )
{
2018-12-28 04:24:24 +00:00
case EncounterLink l when l . RelearnMoves . Length > 0 :
2018-01-26 17:19:20 +00:00
return VerifyRelearnSpecifiedMoveset ( pkm , info , l . RelearnMoves ) ;
case MysteryGift g :
return VerifyRelearnSpecifiedMoveset ( pkm , info , g . RelearnMoves ) ;
2018-08-03 03:11:42 +00:00
case EncounterStatic s when s . Relearn . Length > 0 :
2018-01-26 17:19:20 +00:00
return VerifyRelearnSpecifiedMoveset ( pkm , info , s . Relearn ) ;
case EncounterEgg e :
return VerifyRelearnEggBase ( pkm , info , e ) ;
2018-03-11 03:51:09 +00:00
case EncounterSlot z when pkm . RelearnMove1 ! = 0 & & z . Permissions . DexNav & & EncounterSlotGenerator . IsDexNavValid ( pkm ) :
2018-01-26 17:19:20 +00:00
return VerifyRelearnDexNav ( pkm , info ) ;
}
2017-06-18 01:37:19 +00:00
return VerifyRelearnNone ( pkm , info ) ;
2017-05-28 04:17:53 +00:00
}
2017-06-18 01:37:19 +00:00
private static CheckResult [ ] VerifyRelearnSpecifiedMoveset ( PKM pkm , LegalInfo info , int [ ] moves )
2017-05-28 04:17:53 +00:00
{
CheckResult [ ] res = new CheckResult [ 4 ] ;
int [ ] RelearnMoves = pkm . RelearnMoves ;
for ( int i = 0 ; i < 4 ; i + + )
2018-08-03 03:11:42 +00:00
{
2017-05-28 04:17:53 +00:00
res [ i ] = moves [ i ] ! = RelearnMoves [ i ]
2018-09-01 21:11:12 +00:00
? new CheckResult ( Severity . Invalid , string . Format ( LMoveFExpect_0 , MoveStrings [ moves [ i ] ] ) , CheckIdentifier . RelearnMove )
2017-05-28 04:17:53 +00:00
: new CheckResult ( CheckIdentifier . RelearnMove ) ;
2018-08-03 03:11:42 +00:00
}
2017-05-28 04:17:53 +00:00
info . RelearnBase = moves ;
return res ;
}
2018-08-03 03:11:42 +00:00
2017-06-18 01:37:19 +00:00
private static CheckResult [ ] VerifyRelearnDexNav ( PKM pkm , LegalInfo info )
2017-05-28 04:17:53 +00:00
{
2018-12-28 04:24:24 +00:00
var result = new CheckResult [ 4 ] ;
2017-05-28 04:17:53 +00:00
int [ ] RelearnMoves = pkm . RelearnMoves ;
// DexNav Pokémon can have 1 random egg move as a relearn move.
2018-12-28 04:24:24 +00:00
result [ 0 ] = ! Legal . GetValidRelearn ( pkm , Legal . GetBaseEggSpecies ( pkm ) , true ) . Contains ( RelearnMoves [ 0 ] )
2018-09-01 21:11:12 +00:00
? new CheckResult ( Severity . Invalid , LMoveRelearnDexNav , CheckIdentifier . RelearnMove )
2017-05-28 04:17:53 +00:00
: new CheckResult ( CheckIdentifier . RelearnMove ) ;
// All other relearn moves must be empty.
for ( int i = 1 ; i < 4 ; i + + )
2018-08-03 03:11:42 +00:00
{
2018-12-28 04:24:24 +00:00
result [ i ] = RelearnMoves [ i ] ! = 0
2018-09-01 21:11:12 +00:00
? new CheckResult ( Severity . Invalid , LMoveRelearnNone , CheckIdentifier . RelearnMove )
2017-05-28 04:17:53 +00:00
: new CheckResult ( CheckIdentifier . RelearnMove ) ;
2018-08-03 03:11:42 +00:00
}
2017-05-28 04:17:53 +00:00
// Update the relearn base moves if the first relearn move is okay.
2018-12-28 04:24:24 +00:00
info . RelearnBase = result [ 0 ] . Valid
2017-05-28 04:17:53 +00:00
? RelearnMoves
2018-12-28 04:24:24 +00:00
: RelearnEmpty ;
2017-05-28 04:17:53 +00:00
2018-12-28 04:24:24 +00:00
return result ;
2017-05-28 04:17:53 +00:00
}
2018-08-03 03:11:42 +00:00
2017-06-18 01:37:19 +00:00
private static CheckResult [ ] VerifyRelearnNone ( PKM pkm , LegalInfo info )
2017-05-28 04:17:53 +00:00
{
2018-12-28 04:24:24 +00:00
var result = new CheckResult [ 4 ] ;
2017-05-28 04:17:53 +00:00
int [ ] RelearnMoves = pkm . RelearnMoves ;
// No relearn moves should be present.
for ( int i = 0 ; i < 4 ; i + + )
2018-08-03 03:11:42 +00:00
{
2018-12-28 04:24:24 +00:00
result [ i ] = RelearnMoves [ i ] ! = 0
2018-09-01 21:11:12 +00:00
? new CheckResult ( Severity . Invalid , LMoveRelearnNone , CheckIdentifier . RelearnMove )
2017-05-28 04:17:53 +00:00
: new CheckResult ( CheckIdentifier . RelearnMove ) ;
2018-08-03 03:11:42 +00:00
}
2017-05-28 04:17:53 +00:00
2018-12-28 04:24:24 +00:00
info . RelearnBase = RelearnEmpty ;
return result ;
2017-05-28 04:17:53 +00:00
}
2018-08-03 03:11:42 +00:00
2017-06-18 01:37:19 +00:00
private static CheckResult [ ] VerifyRelearnEggBase ( PKM pkm , LegalInfo info , EncounterEgg e )
2017-05-28 04:17:53 +00:00
{
int [ ] RelearnMoves = pkm . RelearnMoves ;
2018-12-28 04:24:24 +00:00
var result = new CheckResult [ 4 ] ;
2017-08-01 00:09:16 +00:00
// Level up moves cannot be inherited if Ditto is the parent
2018-07-04 16:15:20 +00:00
// that means genderless species and male only species except Nidoran and Volbeat (they breed with female nidoran and illumise) could not have level up moves as an egg
2018-06-19 04:56:30 +00:00
bool inheritLvlMoves = Legal . GetCanInheritMoves ( e . Species ) ;
2017-05-28 04:17:53 +00:00
// Obtain level1 moves
2018-03-31 05:11:24 +00:00
var baseMoves = Legal . GetBaseEggMoves ( pkm , e . Species , e . Version , 1 ) ;
2017-08-01 21:55:10 +00:00
int baseCt = Math . Min ( 4 , baseMoves . Length ) ;
2017-05-28 04:17:53 +00:00
// Obtain Inherited moves
2018-03-31 05:11:24 +00:00
var inheritMoves = Legal . GetValidRelearn ( pkm , e . Species , inheritLvlMoves , e . Version ) . ToList ( ) ;
2017-08-01 00:09:16 +00:00
int reqBase = GetRequiredBaseMoves ( RelearnMoves , baseMoves , baseCt , inheritMoves ) ;
2017-05-28 04:17:53 +00:00
// Check if the required amount of Base Egg Moves are present.
2018-12-28 04:24:24 +00:00
FlagBaseEggMoves ( result , reqBase , baseMoves , RelearnMoves ) ;
2017-05-28 04:17:53 +00:00
// Non-Base moves that can magically appear in the regular movepool
if ( Legal . LightBall . Contains ( pkm . Species ) )
2017-08-01 00:09:16 +00:00
inheritMoves . Add ( 344 ) ; // Volt Tackle
2017-05-28 04:17:53 +00:00
// If any splitbreed moves are invalid, flag accordingly
2018-09-02 02:55:08 +00:00
var splitMoves = e is EncounterEggSplit s
? Legal . GetValidRelearn ( pkm , s . OtherSpecies , inheritLvlMoves , e . Version ) . ToList ( )
: ( IReadOnlyList < int > ) Array . Empty < int > ( ) ;
2017-05-28 04:17:53 +00:00
// Inherited moves appear after the required base moves.
2017-08-01 00:09:16 +00:00
// If the pkm is capable of split-species breeding and any inherited move is from the other split scenario, flag accordingly.
2018-12-28 04:24:24 +00:00
bool splitInvalid = FlagInvalidInheritedMoves ( result , reqBase , RelearnMoves , inheritMoves , splitMoves ) ;
2017-08-01 00:09:16 +00:00
if ( splitInvalid )
2018-12-28 04:24:24 +00:00
FlagSplitbreedMoves ( result , reqBase , e , pkm ) ;
2017-08-01 00:09:16 +00:00
info . RelearnBase = baseMoves ;
2018-12-28 04:24:24 +00:00
return result ;
2017-08-01 00:09:16 +00:00
}
2018-08-03 03:11:42 +00:00
2018-12-28 04:24:24 +00:00
private static void FlagBaseEggMoves ( CheckResult [ ] result , int required , IReadOnlyList < int > baseMoves , IReadOnlyList < int > RelearnMoves )
2017-08-01 00:09:16 +00:00
{
for ( int i = 0 ; i < required ; i + + )
{
if ( ! baseMoves . Contains ( RelearnMoves [ i ] ) )
{
2018-12-28 04:24:24 +00:00
FlagRelearnMovesMissing ( result , required , baseMoves , i ) ;
2017-08-01 00:09:16 +00:00
return ;
}
2018-12-28 04:24:24 +00:00
result [ i ] = new CheckResult ( Severity . Valid , LMoveRelearnEgg , CheckIdentifier . RelearnMove ) ;
2017-08-01 00:09:16 +00:00
}
}
2018-08-03 03:11:42 +00:00
2018-12-28 04:24:24 +00:00
private static void FlagRelearnMovesMissing ( CheckResult [ ] result , int required , IReadOnlyList < int > baseMoves , int start )
2017-08-01 00:09:16 +00:00
{
for ( int z = start ; z < required ; z + + )
2018-12-28 04:24:24 +00:00
result [ z ] = new CheckResult ( Severity . Invalid , LMoveRelearnEggMissing , CheckIdentifier . RelearnMove ) ;
2017-08-01 00:09:16 +00:00
// provide the list of suggested base moves for the last required slot
2018-05-12 15:41:23 +00:00
string em = string . Join ( ", " , GetMoveNames ( baseMoves ) ) ;
2018-12-28 04:24:24 +00:00
result [ required - 1 ] . Comment + = string . Format ( Environment . NewLine + LMoveRelearnFExpect_0 , em ) ;
2017-08-01 00:09:16 +00:00
}
2018-08-03 03:11:42 +00:00
2018-12-28 04:24:24 +00:00
private static bool FlagInvalidInheritedMoves ( CheckResult [ ] result , int required , IReadOnlyList < int > RelearnMoves , IReadOnlyList < int > inheritMoves , IReadOnlyList < int > splitMoves )
2017-08-01 00:09:16 +00:00
{
bool splitInvalid = false ;
2018-07-04 16:15:20 +00:00
bool isSplit = splitMoves . Count > 0 ;
2017-08-01 00:09:16 +00:00
for ( int i = required ; i < 4 ; i + + )
2017-05-28 04:17:53 +00:00
{
if ( RelearnMoves [ i ] = = 0 ) // empty
2018-12-28 04:24:24 +00:00
result [ i ] = new CheckResult ( Severity . Valid , LMoveSourceEmpty , CheckIdentifier . RelearnMove ) ;
2017-05-28 04:17:53 +00:00
else if ( inheritMoves . Contains ( RelearnMoves [ i ] ) ) // inherited
2018-12-28 04:24:24 +00:00
result [ i ] = new CheckResult ( Severity . Valid , LMoveSourceRelearn , CheckIdentifier . RelearnMove ) ;
2018-07-04 16:15:20 +00:00
else if ( isSplit & & splitMoves . Contains ( RelearnMoves [ i ] ) ) // inherited
2017-05-28 04:17:53 +00:00
splitInvalid = true ;
else // not inheritable, flag
2018-12-28 04:24:24 +00:00
result [ i ] = new CheckResult ( Severity . Invalid , LMoveRelearnInvalid , CheckIdentifier . RelearnMove ) ;
2017-05-28 04:17:53 +00:00
}
2017-08-01 00:09:16 +00:00
return splitInvalid ;
}
2018-08-03 03:11:42 +00:00
2017-08-01 00:09:16 +00:00
private static void FlagSplitbreedMoves ( CheckResult [ ] res , int required , EncounterEgg e , PKM pkm )
{
2018-07-04 16:15:20 +00:00
var other = e is EncounterEggSplit x ? x . OtherSpecies : Legal . GetBaseEggSpecies ( pkm , 1 ) ;
2017-08-01 00:09:16 +00:00
for ( int i = required ; i < 4 ; i + + )
2017-05-28 04:17:53 +00:00
{
2017-08-01 00:09:16 +00:00
if ( res [ i ] ! = null )
continue ;
2018-09-01 21:11:12 +00:00
string message = string . Format ( LMoveEggFIncompatible0_1 , SpeciesStrings [ other ] , SpeciesStrings [ e . Species ] ) ;
2017-08-01 00:09:16 +00:00
res [ i ] = new CheckResult ( Severity . Invalid , message , CheckIdentifier . RelearnMove ) ;
2017-05-28 04:17:53 +00:00
}
2017-08-01 00:09:16 +00:00
}
2017-05-28 04:17:53 +00:00
2017-08-01 00:09:16 +00:00
private static int GetRequiredBaseMoves ( int [ ] RelearnMoves , IReadOnlyList < int > baseMoves , int baseCt , IReadOnlyList < int > inheritMoves )
{
2018-06-10 07:26:33 +00:00
var inherited = RelearnMoves . Where ( m = > m ! = 0 & & ( ! baseMoves . Contains ( m ) | | inheritMoves . Contains ( m ) ) ) . ToList ( ) ;
2017-08-01 00:09:16 +00:00
int inheritCt = inherited . Count ;
// Get required amount of base moves
2017-10-07 04:03:23 +00:00
int unique = baseMoves . Union ( inherited ) . Count ( ) ;
2017-08-01 00:09:16 +00:00
int reqBase = inheritCt = = 4 | | baseCt + inheritCt > 4 ? 4 - inheritCt : baseCt ;
2017-10-07 04:03:23 +00:00
if ( RelearnMoves . Count ( m = > m ! = 0 ) < Math . Min ( 4 , baseMoves . Count ) )
2017-08-01 00:09:16 +00:00
reqBase = Math . Min ( 4 , unique ) ;
return reqBase ;
2017-05-28 04:17:53 +00:00
}
}
}