package me.hugmanrique.pokedata.trainers;

import lombok.Getter;
import lombok.ToString;
import me.hugmanrique.pokedata.Data;
import me.hugmanrique.pokedata.graphics.Imageable;
import me.hugmanrique.pokedata.graphics.Palette;
import me.hugmanrique.pokedata.graphics.ROMImage;
import me.hugmanrique.pokedata.loaders.ROMData;
import me.hugmanrique.pokedata.roms.ROM;
import me.hugmanrique.pokedata.utils.ImageUtils;

/**
 * @author Hugmanrique
 * @since 02/06/2017
 */
@Getter
@ToString
public class Trainer extends Data implements Imageable {
    private static final int ITEM_LIMIT = 4;

    private byte trainerClass;
    private byte gender;
    private byte music;
    private byte sprite;
    private String name;
    private boolean heldsItems;
    private int[] items;
    private boolean doubleBattle;
    private long ai;

    private boolean customAttacks;
    private int partyOffset;
    private TrainerPokemon[] party;

    private int index;

    public Trainer(ROM rom, int index) {
        this.index = index;

        byte flags = rom.readByte();
        customAttacks = (flags & 1) == 1;
        heldsItems = (flags & 2) == 2;

        trainerClass = rom.readByte();

        byte genderMusic = rom.readByte();
        gender = (byte) ((genderMusic & 128) >> 7);
        music = (byte) (genderMusic & 127);

        sprite = rom.readByte();
        name = rom.readPokeText(12);

        items = new int[ITEM_LIMIT];

        for (int i = 0; i < ITEM_LIMIT; i++) {
            items[i] = rom.readWord();
        }

        doubleBattle = rom.readByte() == 1;
        rom.addInternalOffset(3);

        ai = rom.getPointer();

        int partyCount = rom.readByte();
        rom.addInternalOffset(3);
        partyOffset = rom.getPointerAsInt();

        loadParty(rom, partyCount);
    }

    private void loadParty(ROM rom, int count) {
        if (count <= 0 || partyOffset <= 0) {
            return;
        }

        rom.setInternalOffset(partyOffset);

        party = new TrainerPokemon[count];

        for (int i = 0; i < count; i++) {
            party[i] = new TrainerPokemon(rom, this);
        }
    }

    public static Trainer load(ROM rom, ROMData data, int index) {
        int offset = data.getTrainerTable() + (index * 40);
        rom.seek(offset);

        return new Trainer(rom, index);
    }

    @Override
    public ROMImage getImage(ROM rom, ROMData data) {
        rom.setInternalOffset(data.getTrainerPaletteTable() + (index * 8));
        int offset = rom.getPointerAsInt();

        Palette palette = ImageUtils.getPalette(rom, offset);

        rom.setInternalOffset(data.getTrainerImageTable() + (index * 8));
        offset = rom.getPointerAsInt();

        return ImageUtils.getImage(rom, offset, palette, 64, 64);
    }
}

@@ -2451,37 +2451,38 @@
 letteTable() + (
-index
+sprite
  * 8));%0A        
@@ -2642,21 +2642,22 @@
 le() + (
-index
+sprite
  * 8));%0A

Use sprite index instead of Trainer index