Add synthesis for GameDataPK9->GameDataPK8

Code duplication -> identified the root pattern. Looks quite simple now; hopefully this is the eventual pattern.
This commit is contained in:
Kurt 2023-05-09 20:00:13 -07:00
parent e265c18fa2
commit 709da45449
5 changed files with 59 additions and 28 deletions

View file

@ -109,6 +109,8 @@ public sealed class GameDataPA8 : HomeOptional1, IGameDataSide, IScaledSizeAbsol
return GameDataPB7.Create<GameDataPA8>(x);
if (pkh.DataPB8 is { } b)
return GameDataPB8.Create<GameDataPA8>(b);
if (pkh.DataPK9 is { } g9)
return GameDataPK9.Create<GameDataPA8>(g9);
if (pkh.DataPK8 is { } c)
return new GameDataPA8 { Ball = c.Met_Location == Locations.HOME_SWLA ? (int)Core.Ball.LAPoke : c.Ball, Met_Location = c.Met_Location, Egg_Location = c.Egg_Location };
return null;

View file

@ -128,6 +128,11 @@ public sealed class GameDataPB7 : HomeOptional1, IGameDataSide, IScaledSizeAbsol
met = z.Met_Location;
ball = z.Ball;
}
else if (pkh.DataPK9 is { } g9)
{
met = g9.Met_Location;
ball = g9.Ball;
}
if (met == 0)
return null;

View file

@ -81,6 +81,8 @@ public sealed class GameDataPB8 : HomeOptional1, IGameDataSide
return Create(b);
if (pkh.DataPA8 is { } a)
return Create(a);
if (pkh.DataPK9 is { } g9)
return Create(g9);
return null;
}

View file

@ -97,27 +97,33 @@ public sealed class GameDataPK8 : HomeOptional1, IGameDataSide, IGigantamax, IDy
if (pkh.DataPB7 is { } x)
return GameDataPB7.Create<GameDataPK8>(x);
if (pkh.DataPB8 is { } b)
{
if (pkh.Version is (int)GameVersion.SW or (int)GameVersion.SH && b.Met_Location is not (Locations.HOME_SWLA or Locations.HOME_SWBD or Locations.HOME_SHSP))
return new GameDataPK8 { Ball = b.Ball, Met_Location = b.Met_Location, Egg_Location = b.Egg_Location is Locations.Default8bNone ? 0 : b.Egg_Location };
var side = pkh.DataPB8 as IGameDataSide
?? pkh.DataPA8 as IGameDataSide
?? pkh.DataPK9;
if (side is null)
return null;
var ball = b.Ball > (int)Core.Ball.Beast ? 4 : b.Ball;
var ver = pkh.Version;
var loc = Locations.GetMetSWSH((ushort)b.Met_Location, ver);
return new GameDataPK8 { Ball = ball, Met_Location = loc, Egg_Location = loc != b.Met_Location ? Locations.HOME_SWSHBDSPEgg : b.Egg_Location };
}
if (pkh.DataPA8 is { } a)
{
if (pkh.Version is (int)GameVersion.SW or (int)GameVersion.SH && a.Met_Location is not (Locations.HOME_SWLA or Locations.HOME_SWBD or Locations.HOME_SHSP))
return new GameDataPK8 { Ball = a.Ball > (int)Core.Ball.Beast ? 4 : a.Ball, Met_Location = a.Met_Location, Egg_Location = a.Egg_Location is Locations.Default8bNone ? 0 : a.Egg_Location };
var ball = a.Ball > (int)Core.Ball.Beast ? 4 : a.Ball;
var ver = pkh.Version;
var loc = Locations.GetMetSWSH((ushort)a.Met_Location, ver);
return new GameDataPK8 { Ball = ball, Met_Location = loc, Egg_Location = loc != a.Met_Location ? Locations.HOME_SWSHBDSPEgg : a.Egg_Location };
}
return null;
var ver = pkh.Version;
var met = side.Met_Location;
var ball = GetBall(side.Ball);
var egg = GetEggLocation(side.Egg_Location);
if (!IsOriginallySWSH(ver, met))
RemapMetEgg(ver, ref met, ref egg);
return new GameDataPK8 { Ball = ball, Met_Location = met, Egg_Location = egg };
}
private static void RemapMetEgg(int ver, ref int met, ref int egg)
{
var remap = Locations.GetMetSWSH((ushort)met, ver);
if (remap == met)
return;
met = remap;
egg = Locations.HOME_SWSHBDSPEgg;
}
private static bool IsOriginallySWSH(int ver, int loc) => ver is (int)GameVersion.SW or (int)GameVersion.SH && !IsFakeMetLocation(loc);
private static bool IsFakeMetLocation(int met) => met is Locations.HOME_SWLA or Locations.HOME_SWBD or Locations.HOME_SHSP;
private static int GetBall(int ball) => ball > (int)Core.Ball.Beast ? 4 : ball;
private static int GetEggLocation(int egg) => egg == Locations.Default8bNone ? 0 : egg;
}

View file

@ -97,15 +97,31 @@ public sealed class GameDataPK9 : HomeOptional1, IGameDataSide
/// <summary> Reconstructive logic to best apply suggested values. </summary>
public static GameDataPK9? TryCreate(PKH pkh)
{
var orig = pkh.LatestGameData;
if (orig is GameDataPK9 pk9)
return pk9;
if (!PersonalTable.SV.IsPresentInGame(pkh.Species, pkh.Form))
return null;
var result = new GameDataPK9();
orig.CopyTo(result);
return result;
if (pkh.DataPB7 is { } x)
return Create(x);
if (pkh.DataPK8 is { } b)
return Create(b);
if (pkh.DataPA8 is { } a)
return Create(a);
if (pkh.DataPB8 is { } bd)
return Create(bd);
return null;
}
public static T Create<T>(GameDataPK9 data) where T : IGameDataSide, new() => new()
{
Ball = data.Ball,
Met_Location = data.Met_Location == Locations.Default8bNone ? 0 : data.Met_Location,
Egg_Location = data.Egg_Location == Locations.Default8bNone ? 0 : data.Egg_Location,
};
public static GameDataPK9 Create(IGameDataSide data) => new()
{
Ball = data.Ball,
Met_Location = data.Met_Location == 0 ? Locations.Default8bNone : data.Met_Location,
Egg_Location = data.Egg_Location == 0 ? Locations.Default8bNone : data.Egg_Location,
};
}