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
|
|
|
using System;
|
2021-08-15 00:16:53 +00:00
|
|
|
using System.Drawing;
|
2017-01-12 01:55:42 +00:00
|
|
|
using PKHeX.Core;
|
2021-11-27 23:48:08 +00:00
|
|
|
using PKHeX.Drawing.PokeSprite.Properties;
|
2017-01-12 01:55:42 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
namespace PKHeX.Drawing.PokeSprite;
|
|
|
|
|
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
|
|
|
/// <summary>
|
|
|
|
/// Singleton that builds sprite images.
|
|
|
|
/// </summary>
|
2021-12-10 08:15:04 +00:00
|
|
|
public static class SpriteUtil
|
2017-01-12 01:55:42 +00:00
|
|
|
{
|
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
|
|
|
/// <summary>Square sprite builder instance</summary>
|
2022-02-05 22:54:01 +00:00
|
|
|
public static readonly SpriteBuilder5668s SB8s = new();
|
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
|
|
|
/// <summary>Circle sprite builder instance (used in Legends: Arceus)</summary>
|
2022-02-05 22:54:01 +00:00
|
|
|
public static readonly SpriteBuilder5668c SB8c = new();
|
2022-11-25 01:42:17 +00:00
|
|
|
/// <summary>Circle sprite builder instance (used in Brilliant Diamond, Shining Pearl, Scarlet, and Violet)</summary>
|
|
|
|
public static readonly SpriteBuilder5668a SB8a = new();
|
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
|
|
|
|
|
|
|
/// <summary>Current sprite builder reference used to build sprites.</summary>
|
2022-02-05 22:54:01 +00:00
|
|
|
public static SpriteBuilder Spriter { get; private set; } = SB8s;
|
|
|
|
|
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
|
|
|
/// <summary>
|
|
|
|
/// Changes the builder mode to the requested mode.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mode">Requested sprite builder mode</param>
|
|
|
|
/// <remarks>If an out of bounds value is provided, will not change.</remarks>
|
2022-02-05 22:54:01 +00:00
|
|
|
public static void ChangeMode(SpriteBuilderMode mode) => Spriter = mode switch
|
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
SpriteBuilderMode.SpritesArtwork5668 => SB8a,
|
2022-02-05 22:54:01 +00:00
|
|
|
SpriteBuilderMode.CircleMugshot5668 => SB8c,
|
|
|
|
SpriteBuilderMode.SpritesClassic5668 => SB8s,
|
|
|
|
_ => Spriter,
|
|
|
|
};
|
2021-12-10 08:15:04 +00:00
|
|
|
|
|
|
|
private const int MaxSlotCount = 30; // slots in a box
|
|
|
|
private static int SpriteWidth => Spriter.Width;
|
|
|
|
private static int SpriteHeight => Spriter.Height;
|
|
|
|
private static int PartyMarkShiftX => SpriteWidth - 16;
|
|
|
|
private static int SlotLockShiftX => SpriteWidth - 14;
|
|
|
|
private static int SlotTeamShiftX => SpriteWidth - 19;
|
|
|
|
private static int FlagIllegalShiftY => SpriteHeight - 16;
|
|
|
|
|
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
|
|
|
/// <summary>
|
|
|
|
/// Sets up the sprite builder to behave with the input <see cref="sav"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sav">Save File to be generating sprites for.</param>
|
2022-02-05 22:54:01 +00:00
|
|
|
public static void Initialize(SaveFile sav)
|
|
|
|
{
|
|
|
|
ChangeMode(SpriteBuilderUtil.GetSuggestedMode(sav));
|
|
|
|
Spriter.Initialize(sav);
|
|
|
|
}
|
2021-12-10 08:15:04 +00:00
|
|
|
|
|
|
|
public static Image GetBallSprite(int ball)
|
2017-01-12 01:55:42 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
string resource = SpriteName.GetResourceStringBall(ball);
|
|
|
|
return (Bitmap?)Resources.ResourceManager.GetObject(resource) ?? Resources._ball4; // Poké Ball (default)
|
|
|
|
}
|
2017-07-16 21:05:29 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
public static Image? GetItemSprite(int item) => Resources.ResourceManager.GetObject($"item_{item}") as Image;
|
2021-11-27 23:48:08 +00:00
|
|
|
|
2023-02-07 01:36:01 +00:00
|
|
|
public static Image GetSprite(ushort species, byte form, int gender, uint formarg, int item, bool isegg, Shiny shiny, EntityContext context = EntityContext.None)
|
2021-12-10 08:15:04 +00:00
|
|
|
{
|
2023-02-07 01:36:01 +00:00
|
|
|
return Spriter.GetSprite(species, form, gender, formarg, item, isegg, shiny, context);
|
2021-12-10 08:15:04 +00:00
|
|
|
}
|
2021-11-27 23:48:08 +00:00
|
|
|
|
2023-02-07 01:36:01 +00:00
|
|
|
private static Image GetSprite(PKM pk)
|
2021-12-10 08:15:04 +00:00
|
|
|
{
|
|
|
|
var formarg = pk is IFormArgument f ? f.FormArgument : 0;
|
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
|
|
|
var shiny = !pk.IsShiny ? Shiny.Never : (ShinyExtensions.IsSquareShinyExist(pk) ? Shiny.AlwaysSquare : Shiny.AlwaysStar);
|
|
|
|
|
2023-02-07 01:36:01 +00:00
|
|
|
var img = GetSprite(pk.Species, pk.Form, pk.Gender, formarg, pk.SpriteItem, pk.IsEgg, shiny, pk.Context);
|
2022-11-25 01:42:17 +00:00
|
|
|
if (pk is IShadowCapture {IsShadow: true})
|
2017-01-12 01:55:42 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
const int Lugia = (int)Species.Lugia;
|
|
|
|
if (pk.Species == Lugia) // show XD shadow sprite
|
2023-02-07 01:36:01 +00:00
|
|
|
img = Spriter.GetSprite(Spriter.ShadowLugia, Lugia, pk.SpriteItem, pk.IsEgg, shiny, pk.Context);
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
GetSpriteGlow(pk, 75, 0, 130, out var pixels, out var baseSprite, true);
|
|
|
|
var glowImg = ImageUtil.GetBitmap(pixels, baseSprite.Width, baseSprite.Height, baseSprite.PixelFormat);
|
|
|
|
return ImageUtil.LayerImage(glowImg, img, 0, 0);
|
|
|
|
}
|
2022-11-25 01:42:17 +00:00
|
|
|
if (pk is IGigantamaxReadOnly { CanGigantamax: true})
|
2017-01-12 01:55:42 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
var gm = Resources.dyna;
|
|
|
|
return ImageUtil.LayerImage(img, gm, (img.Width - gm.Width) / 2, 0);
|
2017-01-12 01:55:42 +00:00
|
|
|
}
|
2022-02-05 01:35:54 +00:00
|
|
|
if (pk is IAlpha {IsAlpha: true})
|
|
|
|
{
|
2023-02-07 02:04:25 +00:00
|
|
|
var alpha = Resources.alpha_alt;
|
2022-02-05 01:35:54 +00:00
|
|
|
return ImageUtil.LayerImage(img, alpha, SlotTeamShiftX, 0);
|
|
|
|
}
|
2021-12-10 08:15:04 +00:00
|
|
|
return img;
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
private static Image GetSprite(PKM pk, SaveFile sav, int box, int slot, bool flagIllegal = false)
|
|
|
|
{
|
|
|
|
bool inBox = (uint)slot < MaxSlotCount;
|
|
|
|
bool empty = pk.Species == 0;
|
2023-02-07 01:36:01 +00:00
|
|
|
var sprite = empty ? Spriter.None : pk.Sprite();
|
2021-12-10 08:15:04 +00:00
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
if (!empty)
|
2017-03-08 16:46:25 +00:00
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
if (SpriteBuilder.ShowTeraType != SpriteBackgroundType.None && pk is ITeraType t)
|
|
|
|
{
|
|
|
|
var type = t.TeraType;
|
|
|
|
sprite = ApplyTeraColor((byte)type, sprite, SpriteBuilder.ShowTeraType);
|
|
|
|
}
|
|
|
|
if (flagIllegal)
|
|
|
|
{
|
|
|
|
var la = new LegalityAnalysis(pk, sav.Personal, box != -1 ? SlotOrigin.Box : SlotOrigin.Party);
|
|
|
|
if (!la.Valid)
|
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.warn, 0, FlagIllegalShiftY);
|
|
|
|
else if (pk.Format >= 8 && MoveInfo.IsDummiedMoveAny(pk))
|
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.hint, 0, FlagIllegalShiftY);
|
|
|
|
|
|
|
|
if (SpriteBuilder.ShowEncounterColorPKM != SpriteBackgroundType.None)
|
|
|
|
sprite = ApplyEncounterColor(la.EncounterOriginal, sprite, SpriteBuilder.ShowEncounterColorPKM);
|
|
|
|
|
|
|
|
if (SpriteBuilder.ShowExperiencePercent)
|
|
|
|
sprite = ApplyExperience(pk, sprite, la.EncounterMatch);
|
|
|
|
}
|
2017-03-08 16:46:25 +00:00
|
|
|
}
|
2021-12-10 08:15:04 +00:00
|
|
|
if (inBox) // in box
|
2017-02-05 04:39:42 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
var flags = sav.GetSlotFlags(box, slot);
|
|
|
|
|
|
|
|
// Indicate any battle box teams & according locked state.
|
|
|
|
int team = flags.IsBattleTeam();
|
|
|
|
if (team >= 0)
|
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.team, SlotTeamShiftX, 0);
|
2023-01-22 04:02:33 +00:00
|
|
|
if (flags.HasFlag(StorageSlotSource.Locked))
|
2021-12-10 08:15:04 +00:00
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.locked, SlotLockShiftX, 0);
|
|
|
|
|
|
|
|
// Some games store Party directly in the list of pokemon data (LGP/E). Indicate accordingly.
|
|
|
|
int party = flags.IsParty();
|
|
|
|
if (party >= 0)
|
|
|
|
sprite = ImageUtil.LayerImage(sprite, PartyMarks[party], PartyMarkShiftX, 0);
|
2023-01-22 04:02:33 +00:00
|
|
|
if (flags.HasFlag(StorageSlotSource.Starter))
|
2021-12-10 08:15:04 +00:00
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.starter, 0, 0);
|
2017-02-05 04:39:42 +00:00
|
|
|
}
|
2017-01-12 06:28:35 +00:00
|
|
|
|
2022-03-08 05:39:48 +00:00
|
|
|
if (SpriteBuilder.ShowExperiencePercent && !flagIllegal)
|
2021-12-10 08:15:04 +00:00
|
|
|
sprite = ApplyExperience(pk, sprite);
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
private static Image ApplyTeraColor(byte elementalType, Image img, SpriteBackgroundType type)
|
|
|
|
{
|
|
|
|
var color = TypeColor.GetTypeSpriteColor(elementalType);
|
|
|
|
var thk = SpriteBuilder.ShowTeraThicknessStripe;
|
|
|
|
var op = SpriteBuilder.ShowTeraOpacityStripe;
|
|
|
|
var bg = SpriteBuilder.ShowTeraOpacityBackground;
|
|
|
|
return ApplyColor(img, type, color, thk, op, bg);
|
|
|
|
}
|
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
public static Image ApplyEncounterColor(IEncounterTemplate enc, Image img, SpriteBackgroundType type)
|
|
|
|
{
|
|
|
|
var index = (enc.GetType().Name.GetHashCode() * 0x43FD43FD);
|
|
|
|
var color = Color.FromArgb(index);
|
2022-11-25 01:42:17 +00:00
|
|
|
var thk = SpriteBuilder.ShowEncounterThicknessStripe;
|
|
|
|
var op = SpriteBuilder.ShowEncounterOpacityStripe;
|
|
|
|
var bg = SpriteBuilder.ShowEncounterOpacityBackground;
|
|
|
|
return ApplyColor(img, type, color, thk, op, bg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Image ApplyColor(Image img, SpriteBackgroundType type, Color color, int thick, byte opacStripe, byte opacBack)
|
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
if (type == SpriteBackgroundType.BottomStripe)
|
2021-08-24 23:11:48 +00:00
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
int stripeHeight = thick; // from bottom
|
2022-06-08 06:59:01 +00:00
|
|
|
if ((uint)stripeHeight > img.Height) // clamp negative & too-high values back to height.
|
|
|
|
stripeHeight = img.Height;
|
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
return ImageUtil.BlendTransparentTo(img, color, opacStripe, img.Width * 4 * (img.Height - stripeHeight));
|
2021-08-24 23:11:48 +00:00
|
|
|
}
|
2022-11-25 01:42:17 +00:00
|
|
|
if (type == SpriteBackgroundType.TopStripe)
|
2021-11-07 22:48:59 +00:00
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
int stripeHeight = thick; // from top
|
|
|
|
if ((uint)stripeHeight > img.Height) // clamp negative & too-high values back to height.
|
|
|
|
stripeHeight = img.Height;
|
|
|
|
|
|
|
|
return ImageUtil.BlendTransparentTo(img, color, opacStripe, 0, (img.Width * 4 * stripeHeight) - 4);
|
2021-11-07 22:48:59 +00:00
|
|
|
}
|
2022-11-25 01:42:17 +00:00
|
|
|
if (type == SpriteBackgroundType.FullBackground) // full background
|
|
|
|
{
|
2022-11-27 18:56:16 +00:00
|
|
|
return ImageUtil.ChangeTransparentTo(img, color, opacBack);
|
2022-11-25 01:42:17 +00:00
|
|
|
}
|
|
|
|
return img;
|
2021-12-10 08:15:04 +00:00
|
|
|
}
|
2021-11-07 22:48:59 +00:00
|
|
|
|
2022-03-08 05:39:48 +00:00
|
|
|
private static Image ApplyExperience(PKM pk, Image img, IEncounterTemplate? enc = null)
|
2021-12-10 08:15:04 +00:00
|
|
|
{
|
|
|
|
const int bpp = 4;
|
|
|
|
int start = bpp * SpriteWidth * (SpriteHeight - 1);
|
|
|
|
var level = pk.CurrentLevel;
|
|
|
|
if (level == 100)
|
|
|
|
return ImageUtil.WritePixels(img, Color.Lime, start, start + (SpriteWidth * bpp));
|
|
|
|
|
|
|
|
var pct = Experience.GetEXPToLevelUpPercentage(level, pk.EXP, pk.PersonalInfo.EXPGrowth);
|
2022-03-08 05:39:48 +00:00
|
|
|
if (pct is not 0)
|
|
|
|
return ImageUtil.WritePixels(img, Color.DodgerBlue, start, start + (int)(SpriteWidth * pct * bpp));
|
|
|
|
|
|
|
|
var encLevel = enc is { EggEncounter: true } x ? x.LevelMin : pk.Met_Level;
|
|
|
|
var color = level != encLevel && pk.HasOriginalMetLocation ? Color.DarkOrange : Color.Yellow;
|
|
|
|
return ImageUtil.WritePixels(img, color, start, start + (SpriteWidth * bpp));
|
2021-12-10 08:15:04 +00:00
|
|
|
}
|
2018-12-10 07:18:37 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
private static readonly Bitmap[] PartyMarks =
|
|
|
|
{
|
|
|
|
Resources.party1, Resources.party2, Resources.party3, Resources.party4, Resources.party5, Resources.party6,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static void GetSpriteGlow(PKM pk, byte blue, byte green, byte red, out byte[] pixels, out Image baseSprite, bool forceHollow = false)
|
|
|
|
{
|
|
|
|
bool egg = pk.IsEgg;
|
|
|
|
var formarg = pk is IFormArgument f ? f.FormArgument : 0;
|
2022-12-18 08:16:29 +00:00
|
|
|
baseSprite = GetSprite(pk.Species, pk.Form, pk.Gender, formarg, 0, egg, Shiny.Never, pk.Context);
|
2021-12-10 08:15:04 +00:00
|
|
|
GetSpriteGlow(baseSprite, blue, green, red, out pixels, forceHollow || egg);
|
|
|
|
}
|
2018-07-28 02:59:14 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
public static void GetSpriteGlow(Image baseSprite, byte blue, byte green, byte red, out byte[] pixels, bool forceHollow = false)
|
|
|
|
{
|
|
|
|
pixels = ImageUtil.GetPixelData((Bitmap)baseSprite);
|
|
|
|
if (!forceHollow)
|
2018-07-28 02:59:14 +00:00
|
|
|
{
|
2019-04-23 05:24:29 +00:00
|
|
|
ImageUtil.GlowEdges(pixels, blue, green, red, baseSprite.Width);
|
2021-12-10 08:15:04 +00:00
|
|
|
return;
|
2018-07-28 02:59:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
// If the image has any transparency, any derived background will bleed into it.
|
|
|
|
// Need to undo any transparency values if any present.
|
|
|
|
// Remove opaque pixels from original image, leaving only the glow effect pixels.
|
|
|
|
var original = (byte[])pixels.Clone();
|
|
|
|
ImageUtil.SetAllUsedPixelsOpaque(pixels);
|
|
|
|
ImageUtil.GlowEdges(pixels, blue, green, red, baseSprite.Width);
|
|
|
|
ImageUtil.RemovePixels(pixels, original);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Image GetLegalIndicator(bool valid) => valid ? Resources.valid : Resources.warn;
|
2019-09-29 16:47:06 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
// Extension Methods
|
2023-02-07 01:36:01 +00:00
|
|
|
public static Image Sprite(this PKM pk) => GetSprite(pk);
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
public static Image Sprite(this IEncounterTemplate enc)
|
|
|
|
{
|
|
|
|
if (enc is MysteryGift g)
|
|
|
|
return GetMysteryGiftPreviewPoke(g);
|
|
|
|
var gender = GetDisplayGender(enc);
|
2022-12-18 08:16:29 +00:00
|
|
|
var img = GetSprite(enc.Species, enc.Form, gender, 0, 0, enc.EggEncounter, enc.IsShiny ? Shiny.Always : Shiny.Never, enc.Context);
|
2021-12-10 08:15:04 +00:00
|
|
|
if (SpriteBuilder.ShowEncounterBall && enc is IFixedBall {FixedBall: not Ball.None} b)
|
2021-08-15 00:16:53 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
var ballSprite = GetBallSprite((int)b.FixedBall);
|
|
|
|
img = ImageUtil.LayerImage(img, ballSprite, 0, img.Height - ballSprite.Height);
|
2021-08-15 00:16:53 +00:00
|
|
|
}
|
2022-11-25 01:42:17 +00:00
|
|
|
if (enc is IGigantamaxReadOnly {CanGigantamax: true})
|
2021-08-15 00:16:53 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
var gm = Resources.dyna;
|
|
|
|
img = ImageUtil.LayerImage(img, gm, (img.Width - gm.Width) / 2, 0);
|
|
|
|
}
|
2022-11-25 01:42:17 +00:00
|
|
|
if (enc is IAlphaReadOnly { IsAlpha: true })
|
2022-02-05 01:35:54 +00:00
|
|
|
{
|
2023-02-07 02:04:25 +00:00
|
|
|
var alpha = Resources.alpha_alt;
|
2022-02-05 01:35:54 +00:00
|
|
|
img = ImageUtil.LayerImage(img, alpha, SlotTeamShiftX, 0);
|
|
|
|
}
|
2021-12-10 08:15:04 +00:00
|
|
|
if (SpriteBuilder.ShowEncounterColor != SpriteBackgroundType.None)
|
|
|
|
img = ApplyEncounterColor(enc, img, SpriteBuilder.ShowEncounterColor);
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int GetDisplayGender(IEncounterTemplate enc) => enc switch
|
|
|
|
{
|
2023-08-12 23:01:16 +00:00
|
|
|
IFixedGender { IsFixedGender: true } s => Math.Max(0, (int)s.Gender),
|
|
|
|
IPogoSlot g => (int)g.Gender & 1,
|
2021-12-10 08:15:04 +00:00
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static Image Sprite(this PKM pk, SaveFile sav, int box, int slot, bool flagIllegal = false)
|
|
|
|
=> GetSprite(pk, sav, box, slot, flagIllegal);
|
2021-08-15 00:16:53 +00:00
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
public static Image GetMysteryGiftPreviewPoke(MysteryGift gift)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (gift is { IsEgg: true, Species: (int)Species.Manaphy }) // Manaphy Egg
|
2022-12-18 08:16:29 +00:00
|
|
|
return GetSprite((int)Species.Manaphy, 0, 2, 0, 0, true, Shiny.Never, gift.Context);
|
2021-12-10 08:15:04 +00:00
|
|
|
|
|
|
|
var gender = Math.Max(0, gift.Gender);
|
2022-12-18 08:16:29 +00:00
|
|
|
var img = GetSprite(gift.Species, gift.Form, gender, 0, gift.HeldItem, gift.IsEgg, gift.IsShiny ? Shiny.Always : Shiny.Never, gift.Context);
|
2021-12-10 08:15:04 +00:00
|
|
|
|
|
|
|
if (SpriteBuilder.ShowEncounterBall && gift is IFixedBall { FixedBall: not Ball.None } b)
|
|
|
|
{
|
|
|
|
var ballSprite = GetBallSprite((int)b.FixedBall);
|
|
|
|
img = ImageUtil.LayerImage(img, ballSprite, 0, img.Height - ballSprite.Height);
|
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
if (gift is IGigantamaxReadOnly { CanGigantamax: true })
|
2021-11-27 23:48:08 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
var gm = Resources.dyna;
|
|
|
|
img = ImageUtil.LayerImage(img, gm, (img.Width - gm.Width) / 2, 0);
|
2021-11-27 23:48:08 +00:00
|
|
|
}
|
2021-12-10 08:15:04 +00:00
|
|
|
return img;
|
2017-01-12 01:55:42 +00:00
|
|
|
}
|
|
|
|
}
|