From e729b9b64c9009653e2e73c99e3f040191c18b70 Mon Sep 17 00:00:00 2001 From: tom-overton Date: Fri, 10 Jun 2022 05:06:06 -0700 Subject: BW2: Fix an issue where PWT trainers could have two Pokemon with the same item, causing an error (Fixes #454) --- src/com/sneed/pkrandom/pokemon/Trainer.java | 16 ++++++++++++++++ .../sneed/pkrandom/romhandlers/AbstractRomHandler.java | 5 +++++ src/com/sneed/pkrandom/romhandlers/Gen5RomHandler.java | 1 + 3 files changed, 22 insertions(+) (limited to 'src/com/sneed/pkrandom') diff --git a/src/com/sneed/pkrandom/pokemon/Trainer.java b/src/com/sneed/pkrandom/pokemon/Trainer.java index a6a6a8a..8e8a76e 100755 --- a/src/com/sneed/pkrandom/pokemon/Trainer.java +++ b/src/com/sneed/pkrandom/pokemon/Trainer.java @@ -40,6 +40,8 @@ public class Trainer implements Comparable { public String fullDisplayName; public MultiBattleStatus multiBattleStatus = MultiBattleStatus.NEVER; public int forceStarterPosition = -1; + // Certain trainers (e.g., trainers in the PWT in BW2) require unique held items for all of their Pokemon to prevent a game crash. + public boolean requiresUniqueHeldItems; public String toString() { StringBuilder sb = new StringBuilder("["); @@ -127,6 +129,20 @@ public class Trainer implements Comparable { return (this.poketype & 1) == 1; } + public boolean pokemonHaveUniqueHeldItems() { + List heldItemsForThisTrainer = new ArrayList<>(); + for (TrainerPokemon poke : this.pokemon) { + if (poke.heldItem > 0) { + if (heldItemsForThisTrainer.contains(poke.heldItem)) { + return false; + } else { + heldItemsForThisTrainer.add(poke.heldItem); + } + } + } + return true; + } + public enum MultiBattleStatus { NEVER, POTENTIAL, ALWAYS } diff --git a/src/com/sneed/pkrandom/romhandlers/AbstractRomHandler.java b/src/com/sneed/pkrandom/romhandlers/AbstractRomHandler.java index 4f289e9..f3471db 100755 --- a/src/com/sneed/pkrandom/romhandlers/AbstractRomHandler.java +++ b/src/com/sneed/pkrandom/romhandlers/AbstractRomHandler.java @@ -1962,6 +1962,11 @@ public abstract class AbstractRomHandler implements RomHandler { } else { for (TrainerPokemon tp : t.pokemon) { randomizeHeldItem(tp, settings, moves, movesets); + if (t.requiresUniqueHeldItems) { + while (!t.pokemonHaveUniqueHeldItems()) { + randomizeHeldItem(tp, settings, moves, movesets); + } + } } } } diff --git a/src/com/sneed/pkrandom/romhandlers/Gen5RomHandler.java b/src/com/sneed/pkrandom/romhandlers/Gen5RomHandler.java index 2743583..26a3aee 100755 --- a/src/com/sneed/pkrandom/romhandlers/Gen5RomHandler.java +++ b/src/com/sneed/pkrandom/romhandlers/Gen5RomHandler.java @@ -1468,6 +1468,7 @@ public class Gen5RomHandler extends AbstractDSRomHandler { tr.poketype = 3; // have held items and custom moves int nameAndClassIndex = Gen5Constants.bw2DriftveilTrainerOffsets.get(trno); tr.fullDisplayName = tclasses.get(Gen5Constants.normalTrainerClassLength + nameAndClassIndex) + " " + tnames.get(Gen5Constants.normalTrainerNameLength + nameAndClassIndex); + tr.requiresUniqueHeldItems = true; int pokemonNum = 6; if (trno < 2) { pokemonNum = 3; -- cgit v1.2.3