From 9c986f44ace08b267be26f2aa7b8d1296b17e342 Mon Sep 17 00:00:00 2001
From: Flashfyre <flashfireex@gmail.com>
Date: Thu, 22 Feb 2024 00:34:54 -0500
Subject: [PATCH] Item stealing items prioritize rarer items

Item stealing items prioritize rarer items; reward premium voucher for subsequent classic clears
---
 src/modifier/modifier-type.ts | 14 ++++++++++++++
 src/modifier/modifier.ts      | 11 +++++++++--
 src/phases.ts                 |  5 ++++-
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/modifier/modifier-type.ts b/src/modifier/modifier-type.ts
index 9c361f2ec22..5bfc4a823a8 100644
--- a/src/modifier/modifier-type.ts
+++ b/src/modifier/modifier-type.ts
@@ -59,6 +59,20 @@ export class ModifierType {
     this.tier = tier;
   }
 
+  getOrInferTier(): ModifierTier {
+    if (this.tier)
+      return this.tier;
+    if (!this.id)
+      return null;
+    for (let tier of Utils.getEnumValues(ModifierTier)) {
+      if (!modifierPool.hasOwnProperty(tier))
+        continue;
+      if (modifierPool[tier].find(m => (m as WeightedModifierType).modifierType.id === (this.generatorId || this.id)))
+        return (this.tier = tier);
+    }
+    return null;
+  }
+
   withIdFromFunc(func: ModifierTypeFunc): ModifierType {
     this.id = Object.keys(modifierTypes).find(k => modifierTypes[k] === func);
     return this;
diff --git a/src/modifier/modifier.ts b/src/modifier/modifier.ts
index 8438ea012b7..8f5861437c5 100644
--- a/src/modifier/modifier.ts
+++ b/src/modifier/modifier.ts
@@ -18,6 +18,7 @@ import { MoneyAchv, achvs } from '../system/achv';
 import { VoucherType } from '../system/voucher';
 import { PreventBerryUseAbAttr, applyAbAttrs } from '../data/ability';
 import { FormChangeItem, SpeciesFormChangeItemTrigger } from '../data/pokemon-forms';
+import { ModifierTier } from './modifier-tier';
 
 type ModifierType = ModifierTypes.ModifierType;
 export type ModifierPredicate = (modifier: Modifier) => boolean;
@@ -1648,12 +1649,18 @@ export abstract class HeldItemTransferModifier extends PokemonHeldItemModifier {
     const transferredModifierTypes: ModifierTypes.ModifierType[] = [];
     const itemModifiers = pokemon.scene.findModifiers(m => m instanceof PokemonHeldItemModifier
         && (m as PokemonHeldItemModifier).pokemonId === targetPokemon.id && m.getTransferrable(withinParty), targetPokemon.isPlayer()) as PokemonHeldItemModifier[];
+    let highestItemTier = itemModifiers.map(m => m.type.getOrInferTier()).reduce((highestTier, tier) => Math.max(tier, highestTier), 0);
+    let tierItemModifiers = itemModifiers.filter(m => m.type.getOrInferTier() === highestItemTier);
 
     let heldItemTransferPromises: Promise<void>[] = [];
     
     for (let i = 0; i < transferredItemCount; i++) {
-      if (!itemModifiers.length)
-        break;
+      if (!tierItemModifiers.length) {
+        while (highestItemTier-- && !tierItemModifiers.length)
+          tierItemModifiers = itemModifiers.filter(m => m.type.tier === highestItemTier);
+        if (!tierItemModifiers.length)
+          break;
+      }
       const randItemIndex = pokemon.randSeedInt(itemModifiers.length);
       const randItem = itemModifiers[randItemIndex];
       heldItemTransferPromises.push(pokemon.scene.tryTransferHeldItemModifier(randItem, pokemon, false, false, true).then(success => {
diff --git a/src/phases.ts b/src/phases.ts
index c71882b6742..a9b1c70bd9c 100644
--- a/src/phases.ts
+++ b/src/phases.ts
@@ -2885,8 +2885,9 @@ export class GameOverPhase extends BattlePhase {
 
     this.scene.gameData.clearSession().then(() => {
       this.scene.time.delayedCall(1000, () => {
+        let firstClear = false;
         if (this.victory) {
-          this.scene.validateAchv(achvs.CLASSIC_VICTORY);
+          firstClear = this.scene.validateAchv(achvs.CLASSIC_VICTORY);
           this.scene.gameData.gameStats.sessionsWon++;
         }
         this.scene.gameData.saveSystem();
@@ -2896,6 +2897,8 @@ export class GameOverPhase extends BattlePhase {
           this.scene.clearPhaseQueue();
           this.scene.ui.clearText();
           this.handleUnlocks(this.scene.getParty());
+          if (!firstClear)
+            this.scene.unshiftPhase(new ModifierRewardPhase(this.scene, modifierTypes.VOUCHER_PREMIUM));
           this.scene.reset();
           this.scene.unshiftPhase(new CheckLoadPhase(this.scene));
           this.end();