Skip to content

Commit

Permalink
Merge pull request #293 from divisionbyz0rro/triggers-and-shields
Browse files Browse the repository at this point in the history
Cache the list of trigger receivers before enumerating
  • Loading branch information
IngoHHacks authored Dec 14, 2023
2 parents 240a730 + a74bf8b commit 89cc0d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
10 changes: 6 additions & 4 deletions InscryptionAPI/Triggers/CustomTriggerFinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using DiskCardGame;
using InscryptionAPI.Card;
using System.Collections;
using Sirenix.Serialization.Utilities;

namespace InscryptionAPI.Triggers;

Expand Down Expand Up @@ -419,8 +420,9 @@ public static IEnumerable<T> FindTriggersOnBoard<T>()
public static IEnumerable<T> FindTriggersOnBoard<T>(bool findFacedown)
{
List<PlayableCard> cardsOnBoardCache = new(BoardManager.Instance.CardsOnBoard);
return GlobalTriggerHandler.Instance.nonCardReceivers.Where(x => x.TriggerBeforeCards).OfType<T>().Concat(cardsOnBoardCache.Where(x => x != null && x.OnBoard && (!x.FaceDown || findFacedown)).SelectMany(FindTriggersOnCard<T>))
.Concat(GlobalTriggerHandler.Instance.nonCardReceivers.Where(x => !x.TriggerBeforeCards).OfType<T>())
List<NonCardTriggerReceiver> triggerReceiverCache = new(GlobalTriggerHandler.Instance.nonCardReceivers);
return triggerReceiverCache.Where(x => !x.SafeIsUnityNull()).Where(x => x.TriggerBeforeCards).OfType<T>().Concat(cardsOnBoardCache.Where(x => x != null && x.OnBoard && (!x.FaceDown || findFacedown)).SelectMany(FindTriggersOnCard<T>))
.Concat(triggerReceiverCache.Where(x => !x.SafeIsUnityNull()).Where(x => !x.TriggerBeforeCards).OfType<T>())
.Concat(cardsOnBoardCache.Where(x => x != null && x.OnBoard && x.FaceDown && !findFacedown).SelectMany(FindTriggersOnCard<T>)
.Where(x => x is IActivateWhenFacedown && (x as IActivateWhenFacedown).ShouldTriggerCustomWhenFaceDown(typeof(T))));
}
Expand Down Expand Up @@ -471,5 +473,5 @@ public static IEnumerable<T> FindTriggersOnCard<T>(this CardTriggerHandler card)
/// <typeparam name="T">The type of reciever to search for</typeparam>
/// <returns>All trigger recievers of type T in the opponent's queue.</returns>
public static IEnumerable<T> FindTriggersInQueue<T>() =>
TurnManager.Instance?.Opponent?.Queue.Where(card => card.TryGetComponent(out T _)).Select(card => card.GetComponent<T>()) ?? new T[]{};
TurnManager.Instance?.Opponent?.Queue.Where(card => card.TryGetComponent(out T _)).Select(card => card.GetComponent<T>()) ?? new T[] { };
}
39 changes: 21 additions & 18 deletions InscryptionCommunityPatch/Card/StackAbilityIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static void ClearStackableIcons(ref List<Ability> __result, CardInfo inf

private static Vector2Int FindMatchingOnesDigit(Texture2D searchTex, bool normalSize = true)
{
Texture2D onesTexture = normalSize ? NUMBER_TEXTURES[0] : MEDIUM_NUMBER_TEXTURES[0];
Texture2D onesTexture = normalSize ? NUMBER_TEXTURES[1] : MEDIUM_NUMBER_TEXTURES[1];
Color[] onesColor = onesTexture.GetPixels();
return FindMatchingTexture(searchTex, onesTexture.width, onesTexture.height, onesColor);
}
Expand All @@ -134,7 +134,7 @@ private static Vector2Int FindMatchingTexture(Texture2D searchTex, int width, in
{
int j = nX + (nY * width);
int i = sX + nX + (sY + nY) * searchTex.width;
if ((matchPixels != null && searchPixels[i] != matchPixels[j]) || searchPixels[i].a > 0)
if (matchPixels != null && (searchPixels[i].a > 0) != (matchPixels[j].a > 0))
{
failed = true;
break;
Expand Down Expand Up @@ -201,27 +201,30 @@ private static Tuple<Vector2Int, int> FindNextBestLocation(Texture2D texture)
return new Tuple<Vector2Int, int>(lowerRight, FORCED);
}

private static Tuple<Vector2Int, int> GetPatchLocationForAbility(Ability ability, Texture2D abilityTexture)
private static Tuple<Vector2Int, int> GetPatchLocationForAbility(Ability ability, Texture2D abilityTexture, int stackAmount)
{
if (patchLocations.ContainsKey(ability))
return patchLocations[ability];

// We have to calculate the location

// First, see if the texture has the 'one' icon on it. If so, we will replace at that location
Vector2Int oneLoc = FindMatchingOnesDigit(abilityTexture, true);
if (oneLoc.x != -1)
if (stackAmount < 10)
{
patchLocations.Add(ability, new Tuple<Vector2Int, int>(oneLoc, NORMAL));
return patchLocations[ability];
}
Vector2Int oneLoc = FindMatchingOnesDigit(abilityTexture, true);
if (oneLoc.x != -1)
{
patchLocations.Add(ability, new Tuple<Vector2Int, int>(oneLoc, NORMAL));
return patchLocations[ability];
}

// Let's try the medium-sized digit
oneLoc = FindMatchingOnesDigit(abilityTexture, false);
if (oneLoc.x != -1)
{
patchLocations.Add(ability, new Tuple<Vector2Int, int>(oneLoc, MEDIUM));
return patchLocations[ability];
// Let's try the medium-sized digit
oneLoc = FindMatchingOnesDigit(abilityTexture, false);
if (oneLoc.x != -1)
{
patchLocations.Add(ability, new Tuple<Vector2Int, int>(oneLoc, MEDIUM));
return patchLocations[ability];
}
}

// Simplified the logic for next best to prevent issues - can be easily reverted if more issues arise
Expand Down Expand Up @@ -249,7 +252,7 @@ private static Texture2D PatchTexture(Ability ability, int count)
Texture2D newTexture = TextureHelper.DuplicateTexture(AbilitiesUtil.LoadAbilityIcon(ability.ToString(), false, false) as Texture2D);
newTexture.name = textureName;

Tuple<Vector2Int, int> patchTuple = GetPatchLocationForAbility(ability, newTexture);
Tuple<Vector2Int, int> patchTuple = GetPatchLocationForAbility(ability, newTexture, count);
Vector2Int patchLocation = patchTuple.Item1;
int textureType = patchTuple.Item2; // This means that it's on the lower-right and needs a one-pixel border

Expand Down Expand Up @@ -320,7 +323,7 @@ private static void AddIconNumber(Ability ability, CardInfo info, PlayableCard c
}

int count = baseAbilities.Where(ab => ab == ability).Count();

if (count > 1) // We need to add an override
__instance.SetIcon(PatchTexture(ability, count));
}
Expand Down Expand Up @@ -430,7 +433,7 @@ private static Sprite OverridePixelSprite(AbilityInfo abilityInfo, CardInfo card
int turnsInPlay = card?.GetComponentInChildren<Evolve>()?.numTurnsInPlay ?? 0;
int turnsToEvolve = Mathf.Max(1, (cardInfo.evolveParams == null ? 1 : cardInfo.evolveParams.turnsToEvolve) - turnsInPlay);
int pngIndex = turnsToEvolve > 3 ? 0 : turnsToEvolve;

if (pngIndex == 0)
return abilityInfo.pixelIcon;

Expand All @@ -439,7 +442,7 @@ private static Sprite OverridePixelSprite(AbilityInfo abilityInfo, CardInfo card
texture = TextureHelper.GetImageAsTexture($"pixel_evolve_{pngIndex}.png", typeof(StackAbilityIcons).Assembly);
else
texture = TextureHelper.GetImageAsTexture($"pixel_transformer_{pngIndex}.png", typeof(StackAbilityIcons).Assembly);

return TextureHelper.ConvertTexture(texture, TextureHelper.SpriteType.PixelAbilityIcon);
}

Expand Down

0 comments on commit 89cc0d1

Please sign in to comment.