PKHeX/PKHeX.Core/Legality/Verifiers/MarkVerifier.cs

219 lines
8.4 KiB
C#
Raw Normal View History

using System.Linq;
using static PKHeX.Core.LegalityCheckStrings;
2020-04-06 23:32:23 +00:00
namespace PKHeX.Core
{
/// <summary>
/// Verifies the <see cref="RibbonIndex"/> values for markings.
/// </summary>
public sealed class MarkVerifier : Verifier
{
protected override CheckIdentifier Identifier => CheckIdentifier.RibbonMark;
public override void Verify(LegalityAnalysis data)
{
var pkm = data.pkm;
if (pkm is not IRibbonIndex m)
2020-04-06 23:32:23 +00:00
return;
if (data.Info.Generation != 8 || (pkm.Species == (int)Species.Shedinja && data.EncounterOriginal.Species is not (int)Species.Shedinja)) // Shedinja doesn't copy Ribbons or Marks
2020-04-06 23:32:23 +00:00
VerifyNoMarksPresent(data, m);
else
VerifyMarksPresent(data, m);
VerifyAffixedRibbonMark(data, m);
2020-04-06 23:32:23 +00:00
}
private void VerifyNoMarksPresent(LegalityAnalysis data, IRibbonIndex m)
{
for (var x = RibbonIndex.MarkLunchtime; x <= RibbonIndex.MarkSlump; x++)
{
if (m.GetRibbon((int)x))
data.AddLine(GetInvalid(string.Format(LRibbonMarkingFInvalid_0, x)));
}
2020-04-06 23:32:23 +00:00
}
private void VerifyMarksPresent(LegalityAnalysis data, IRibbonIndex m)
{
bool hasOne = false;
for (var mark = RibbonIndex.MarkLunchtime; mark <= RibbonIndex.MarkSlump; mark++)
{
Enforce weather legality for SWSH (#3221) * Add weather types by location Creates a dictionary of possible weather types for each SWSH location. Unlisted locations may only have Normal weather or do not have any encounter slots. * Prune unused weather from static encounters Some encounters were too permissive with weather, e.g. Sandstorm in Fields of Honor and Challenge Beach; Snowstorm at Dyna Tree Hill. A few crossover areas that would have limited the possible weathers were marked with a "(c)". An example is Nidorina from Giant's Bed crossing to Frostpoint Field, where Raining and Thunderstorm do not occur. This additionally organizes encounters by location. * Move location-weather dictionary to EncounterArea8 * Verify weather marks on encounterslot/static encounters * Adds some static encounters available through weather bleed Weathers aren't normally available, but these static encounters are close enough to the boundary that the weather in an adjacent area can be used to spawn them. - Frostpoint Field Snorlax with Raining/Thunderstorm from Giant's Bed - Snowslide Slope Amaura with Raining/Thunderstorm from Giant's Bed - Frigid Sea Carracosta with Intense_Sun from Three-Point Pass - Frigid Sea Magmortar with Intense_Sun from Three-Point Pass - Ballimere Lake Corviknight with Snowstorm from Giant's Bed - Ballimere Lake Cryogonal with Snowstorm from Giant's Bed - Ballimere Lake Tyrunt with all weather from Giant's Bed - Lakeside Cave Ferrothorn with all weather from Ballimere Lake - Tunnel to the Top Golbat with all weather from Path to the Peak * Defer weather marks if incompatible, enforce encounterslot weather This uses the area weather to check fishing slots and tentatively adds some tables for weather bleed encounterslots. * Warm-Up Tunnel gets weather bleed from Training Lowlands * Update for base PKHeX * Handle weather bleed for SWSH encounter slots Co-authored-by: Skadiv <62726360+Skadiv@users.noreply.github.com> * Minor clean Having duplicate weather marks is illegal, so just auto-partial match them instead of checking the other marks. * Rearrange slot weather check logic * Claim reserved byte in SWSH pkl * No need for two variable names now * Valid weather marks on tree/fishing should return with main weather * Fix tree/fishing deferral and add another surf slot weather bleed * Disallow tree/fishing encounters from using bleed tables None are currently known at this time, and only hidden grass encounters have a weather bleed table * Condense bleed expression, combine tree/fish flag check * Move weather-bleed check into EncounterArea8 Makes the dictionaries private instead of internal. Co-authored-by: Skadiv <62726360+Skadiv@users.noreply.github.com>
2021-07-26 21:28:05 +00:00
bool has = m.GetRibbon((int)mark);
2020-04-06 23:32:23 +00:00
if (!has)
continue;
if (hasOne)
{
data.AddLine(GetInvalid(string.Format(LRibbonMarkingFInvalid_0, mark)));
return;
}
2020-11-04 20:58:56 +00:00
bool result = IsMarkValid(mark, data.pkm, data.EncounterMatch);
2020-04-06 23:32:23 +00:00
if (!result)
{
data.AddLine(GetInvalid(string.Format(LRibbonMarkingFInvalid_0, mark)));
return;
}
hasOne = true;
}
}
public static bool IsMarkValid(RibbonIndex mark, PKM pk, IEncounterTemplate enc)
2020-04-06 23:32:23 +00:00
{
2020-11-04 20:58:56 +00:00
return IsMarkAllowedAny(enc) && IsMarkAllowedSpecific(mark, pk, enc);
}
public static bool IsMarkAllowedSpecific(RibbonIndex mark, PKM pk, IEncounterTemplate x) => mark switch
2020-11-04 20:58:56 +00:00
{
RibbonIndex.MarkCurry when !IsMarkAllowedCurry(pk, x) => false,
2021-02-15 06:25:59 +00:00
RibbonIndex.MarkFishing when !IsMarkAllowedFishing(x) => false,
2021-08-06 21:54:32 +00:00
RibbonIndex.MarkMisty when pk.Met_Level < EncounterArea8.BoostLevel && EncounterArea8.IsBoostedArea60Fog(pk.Met_Location) => false,
RibbonIndex.MarkDestiny => false,
Enforce weather legality for SWSH (#3221) * Add weather types by location Creates a dictionary of possible weather types for each SWSH location. Unlisted locations may only have Normal weather or do not have any encounter slots. * Prune unused weather from static encounters Some encounters were too permissive with weather, e.g. Sandstorm in Fields of Honor and Challenge Beach; Snowstorm at Dyna Tree Hill. A few crossover areas that would have limited the possible weathers were marked with a "(c)". An example is Nidorina from Giant's Bed crossing to Frostpoint Field, where Raining and Thunderstorm do not occur. This additionally organizes encounters by location. * Move location-weather dictionary to EncounterArea8 * Verify weather marks on encounterslot/static encounters * Adds some static encounters available through weather bleed Weathers aren't normally available, but these static encounters are close enough to the boundary that the weather in an adjacent area can be used to spawn them. - Frostpoint Field Snorlax with Raining/Thunderstorm from Giant's Bed - Snowslide Slope Amaura with Raining/Thunderstorm from Giant's Bed - Frigid Sea Carracosta with Intense_Sun from Three-Point Pass - Frigid Sea Magmortar with Intense_Sun from Three-Point Pass - Ballimere Lake Corviknight with Snowstorm from Giant's Bed - Ballimere Lake Cryogonal with Snowstorm from Giant's Bed - Ballimere Lake Tyrunt with all weather from Giant's Bed - Lakeside Cave Ferrothorn with all weather from Ballimere Lake - Tunnel to the Top Golbat with all weather from Path to the Peak * Defer weather marks if incompatible, enforce encounterslot weather This uses the area weather to check fishing slots and tentatively adds some tables for weather bleed encounterslots. * Warm-Up Tunnel gets weather bleed from Training Lowlands * Update for base PKHeX * Handle weather bleed for SWSH encounter slots Co-authored-by: Skadiv <62726360+Skadiv@users.noreply.github.com> * Minor clean Having duplicate weather marks is illegal, so just auto-partial match them instead of checking the other marks. * Rearrange slot weather check logic * Claim reserved byte in SWSH pkl * No need for two variable names now * Valid weather marks on tree/fishing should return with main weather * Fix tree/fishing deferral and add another surf slot weather bleed * Disallow tree/fishing encounters from using bleed tables None are currently known at this time, and only hidden grass encounters have a weather bleed table * Condense bleed expression, combine tree/fish flag check * Move weather-bleed check into EncounterArea8 Makes the dictionaries private instead of internal. Co-authored-by: Skadiv <62726360+Skadiv@users.noreply.github.com>
2021-07-26 21:28:05 +00:00
>= RibbonIndex.MarkCloudy and <= RibbonIndex.MarkMisty => IsWeatherPermitted(mark, x),
2021-08-20 20:49:20 +00:00
_ => true,
};
2020-11-04 20:58:56 +00:00
Enforce weather legality for SWSH (#3221) * Add weather types by location Creates a dictionary of possible weather types for each SWSH location. Unlisted locations may only have Normal weather or do not have any encounter slots. * Prune unused weather from static encounters Some encounters were too permissive with weather, e.g. Sandstorm in Fields of Honor and Challenge Beach; Snowstorm at Dyna Tree Hill. A few crossover areas that would have limited the possible weathers were marked with a "(c)". An example is Nidorina from Giant's Bed crossing to Frostpoint Field, where Raining and Thunderstorm do not occur. This additionally organizes encounters by location. * Move location-weather dictionary to EncounterArea8 * Verify weather marks on encounterslot/static encounters * Adds some static encounters available through weather bleed Weathers aren't normally available, but these static encounters are close enough to the boundary that the weather in an adjacent area can be used to spawn them. - Frostpoint Field Snorlax with Raining/Thunderstorm from Giant's Bed - Snowslide Slope Amaura with Raining/Thunderstorm from Giant's Bed - Frigid Sea Carracosta with Intense_Sun from Three-Point Pass - Frigid Sea Magmortar with Intense_Sun from Three-Point Pass - Ballimere Lake Corviknight with Snowstorm from Giant's Bed - Ballimere Lake Cryogonal with Snowstorm from Giant's Bed - Ballimere Lake Tyrunt with all weather from Giant's Bed - Lakeside Cave Ferrothorn with all weather from Ballimere Lake - Tunnel to the Top Golbat with all weather from Path to the Peak * Defer weather marks if incompatible, enforce encounterslot weather This uses the area weather to check fishing slots and tentatively adds some tables for weather bleed encounterslots. * Warm-Up Tunnel gets weather bleed from Training Lowlands * Update for base PKHeX * Handle weather bleed for SWSH encounter slots Co-authored-by: Skadiv <62726360+Skadiv@users.noreply.github.com> * Minor clean Having duplicate weather marks is illegal, so just auto-partial match them instead of checking the other marks. * Rearrange slot weather check logic * Claim reserved byte in SWSH pkl * No need for two variable names now * Valid weather marks on tree/fishing should return with main weather * Fix tree/fishing deferral and add another surf slot weather bleed * Disallow tree/fishing encounters from using bleed tables None are currently known at this time, and only hidden grass encounters have a weather bleed table * Condense bleed expression, combine tree/fish flag check * Move weather-bleed check into EncounterArea8 Makes the dictionaries private instead of internal. Co-authored-by: Skadiv <62726360+Skadiv@users.noreply.github.com>
2021-07-26 21:28:05 +00:00
private static bool IsWeatherPermitted(RibbonIndex mark, IEncounterTemplate enc)
{
var permit = mark.GetWeather8();
// Encounter slots check location weather, while static encounters check weather per encounter.
return enc switch
{
EncounterSlot8 w => IsSlotWeatherPermitted(permit, w),
EncounterStatic8 s => s.Weather.HasFlag(permit),
_ => false,
};
}
private static bool IsSlotWeatherPermitted(AreaWeather8 permit, EncounterSlot8 s)
{
var location = s.Location;
// If it's not in the main table, it can only have Normal weather.
if (!EncounterArea8.WeatherbyArea.TryGetValue(location, out var weather))
weather = AreaWeather8.Normal;
if (weather.HasFlag(permit))
return true;
// Valid tree/fishing weathers should have returned with main area weather.
if ((s.Weather & (AreaWeather8.Shaking_Trees | AreaWeather8.Fishing)) != 0)
return false;
// Check bleed conditions otherwise.
return EncounterArea8.IsWeatherBleedPossible(s.SlotType, permit, location);
}
public static bool IsMarkAllowedAny(IEncounterTemplate enc) => enc.Generation == 8 && enc switch
2020-11-04 20:58:56 +00:00
{
2021-02-02 04:34:04 +00:00
// Gen 8
WC8 or EncounterEgg or EncounterTrade or EncounterSlot8GO
or EncounterStatic8U or EncounterStatic8N or EncounterStatic8ND or EncounterStatic8NC
or EncounterStatic8 {Gift: true}
or EncounterStatic8 {ScriptedNoMarks: true}
=> false,
_ => true,
};
2020-11-04 20:58:56 +00:00
public static bool IsMarkAllowedCurry(PKM pkm, IEncounterTemplate enc)
2020-11-04 20:58:56 +00:00
{
2021-02-15 06:25:59 +00:00
// Curry are only encounter slots, from the hidden table (not symbol). Slots taken from area's current weather(?).
2021-08-03 06:54:44 +00:00
if (enc is not EncounterSlot8 s || !s.SlotType.CanEncounterViaCurry())
2021-02-15 06:25:59 +00:00
return false;
var weather = s.Weather;
if ((weather & AreaWeather8.All) == 0)
return false;
if (EncounterArea8.IsWildArea(s.Location))
2020-11-04 20:58:56 +00:00
return false;
var ball = pkm.Ball;
return (uint)(ball - 2) <= 2;
2020-04-06 23:32:23 +00:00
}
public static bool IsMarkAllowedFishing(IEncounterTemplate enc)
2021-02-15 06:25:59 +00:00
{
// Fishing are only encounter slots, from the hidden table (not symbol).
if (enc is not EncounterSlot8 s)
return false;
2021-08-03 06:54:44 +00:00
return s.SlotType.CanEncounterViaFishing(s.Weather);
2021-02-15 06:25:59 +00:00
}
private void VerifyAffixedRibbonMark(LegalityAnalysis data, IRibbonIndex m)
{
if (m is not PK8 pk8)
return;
var affix = pk8.AffixedRibbon;
if (affix == -1) // None
return;
if ((byte)affix > (int)RibbonIndex.MarkSlump)
{
data.AddLine(GetInvalid(string.Format(LRibbonMarkingAffixedF_0, affix)));
return;
}
if (pk8.Species == (int)Species.Shedinja && data.EncounterOriginal.Species is not (int)Species.Shedinja)
{
VerifyShedinjaAffixed(data, affix, pk8);
return;
}
EnsureHasRibbon(data, pk8, affix);
}
private void VerifyShedinjaAffixed(LegalityAnalysis data, sbyte affix, PK8 pk8)
{
// Does not copy ribbons or marks, but retains the Affixed Ribbon value.
// Try re-verifying to see if it could have had the Ribbon/Mark.
2021-04-22 02:12:50 +00:00
var enc = data.EncounterOriginal;
if ((byte) affix >= (int) RibbonIndex.MarkLunchtime)
{
2021-04-22 02:12:50 +00:00
if (!IsMarkValid((RibbonIndex)affix, pk8, enc))
data.AddLine(GetInvalid(string.Format(LRibbonMarkingAffixedF_0, (RibbonIndex) affix)));
return;
}
if (enc.Generation <= 4 && (pk8.Ball != (int)Ball.Poke || IsMoveSetEvolvedShedinja(pk8)))
{
// Evolved in a prior generation.
EnsureHasRibbon(data, pk8, affix);
return;
}
var clone = pk8.Clone();
clone.Species = (int) Species.Nincada;
((IRibbonIndex) clone).SetRibbon(affix);
var parse = RibbonVerifier.GetRibbonResults(clone, data.Info.EvoChainsAllGens, enc);
var expect = $"Ribbon{(RibbonIndex) affix}";
var name = RibbonStrings.GetName(expect);
bool invalid = parse.FirstOrDefault(z => z.Name == name)?.Invalid == true;
var severity = invalid ? Severity.Invalid : Severity.Fishy;
data.AddLine(Get(string.Format(LRibbonMarkingAffixedF_0, affix), severity));
}
private static bool IsMoveSetEvolvedShedinja(PK8 pk8)
{
2021-04-22 02:12:50 +00:00
// Check for gen3/4 exclusive moves that are Ninjask glitch only.
if (pk8.HasMove((int) Move.Screech))
return true;
if (pk8.HasMove((int) Move.SwordsDance))
return true;
if (pk8.HasMove((int) Move.Slash))
return true;
if (pk8.HasMove((int) Move.BatonPass))
return true;
2021-04-22 02:12:50 +00:00
return pk8.HasMove((int)Move.Agility) && !pk8.GetMoveRecordFlag(12); // TR12 (Agility)
}
2021-04-22 02:12:50 +00:00
private void EnsureHasRibbon(LegalityAnalysis data, IRibbonIndex pk8, sbyte affix)
{
var hasRibbon = pk8.GetRibbonIndex((RibbonIndex) affix);
if (!hasRibbon)
data.AddLine(GetInvalid(string.Format(LRibbonMarkingAffixedF_0, (RibbonIndex) affix)));
}
2020-04-06 23:32:23 +00:00
}
}