• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Random Battle Generator

1,748
Posts
14
Years
Just another "maybe" useful script that someone might be looking for, basically it generates random battles.

UPDATES: I Added the ability to randomly generate trainers on a map, but this however is only limited to overwriting already existing events... (To do this simply call: BattleGenerator.generateMapTrainers(amount of trainers to generate, difficulty(0-4 or -1 to calculate), minimum party size, maximum party size))

Code:
################################################################################
# Random Trainer/Pokemon Generator
# By Rei
# Credits Required
################################################################################
# A Module with some handy Generation tools for Trainers and Wild Pokemon
################################################################################

module BattleGenerator
  
  # The maximum level for each difficulty
  EasyPokemon = 25
  MediumPokemon = 40
  HardPokemon = 60
  ExtremePokemon = 100
  
  # Pokemon that cannot, no matter what be generated.
  BLACK_LIST = [
    PBSpecies::MEWTWO,
    PBSpecies::MEW,
    PBSpecies::HOOH,
    PBSpecies::LUGIA,
    PBSpecies::CELEBI,
    PBSpecies::KYOGRE,
    PBSpecies::GROUDON,
    PBSpecies::RAYQUAZA,
    PBSpecies::DEOXYS,
    PBSpecies::JIRACHI,
    PBSpecies::DIALGA,
    PBSpecies::PALKIA,
    PBSpecies::GIRATINA,
    PBSpecies::HEATRAN,
    PBSpecies::DARKRAI,
    PBSpecies::SHAYMIN,
    PBSpecies::ARCEUS,
    PBSpecies::ZEKROM,
    PBSpecies::RESHIRAM,
    PBSpecies::KYUREM,
    PBSpecies::LANDORUS,
    PBSpecies::MELOETTA,
    PBSpecies::KELDEO,
    PBSpecies::GENESECT
  ]
  
  # Calculates the difficulty based on your party pokemon's level
  def self.calcDifficulty
    lv = [EasyPokemon, MediumPokemon, HardPokemon, ExtremePokemon]
     pparty = pbBalancedLevel($Trainer.party)
     for i in 0...lv.length
       if pparty <= lv[i]
         break
       end
     end
     return i
   end
   
   # calculates the maximum level via difficulty
   def self.calcMaxLevel(difficulty)
     maxl = 100
    if difficulty < 1
      maxl = EasyPokemon
    elsif difficulty == 1
      maxl = MediumPokemon
    elsif difficulty == 2
      maxl = HardPokemon
    elsif difficulty >= 3
      maxl = ExtremePokemon
    end
    return maxl
   end
  
  # Sets an event's graphic to the generated trainer's graphic
  def self.setTrainerGraphic(trainer, eventID)
    name = ""
    name += "0" if trainer[0].trainertype < 10
    name += "0" if trainer[0].trainertype < 100
    name += trainer[0].trainertype.to_s
    $game_map.events[eventID].character_name = "trchar" + name
  end
  
  def self.getTrainerTypeGender(trainertype)
    ret=2   # 2 = gender unknown
    pbRgssOpen("Data/trainertypes.dat","rb"){|f|
       trainertypes=Marshal.load(f)
       if !trainertypes[trainertype]
         ret=2
       else
         ret=trainertypes[trainertype][7]
         ret=2 if !ret
       end
    }
    return ret
  end
  
  #### Generates amount amount of trainers into random trainers!
  #### You can also set difficulty and party size
  def self.generateMapTrainers(amount, difficulty, min_size = 3, max_size = -1)
    max_size = min_size if max_size < min_size
    available_events = $game_map.events.clone
    for key in available_events.keys
      if !available_events[key].name.include?("Trainer(")
        available_events.delete(key)
      end
    end
    amount = available_events.keys.length if amount > available_events.keys.length
    for i in 0...amount
      key = available_events.keys[rand(available_events.keys.length)]
      event = available_events[key]
      trainer = generateTrainer(difficulty, rand(max_size - min_size - 1) + min_size)
      setTrainerGraphic(trainer, key)
      event.trainer = trainer
      event.setTriggerToEvent
      pbPushBranch(event.list, "$game_map.events[#{key}].tsOn?('battle')")
      pbPushText(event.list, trainer[2], 1)
      pbPushExit(event.list,1)
      pbPushBranchEnd(event.list)
      pbPushScript(event.list, "pbTrainerIntro(:FISHERMAN)")
      pbPushScript(event.list, "Kernel.pbNoticePlayer(get_character(0))")
      pbPushBranch(event.list, "BattleGenerator.generateBattle($game_map.events[#{key}].trainer) == 1")
      pbPushScript(event.list, "$game_map.events[#{key}].setTempSwitchOn('battle')", 1)
      pbPushScript(event.list, "$game_map.events[#{key}].setTriggerToAction", 1)
      pbPushBranchEnd(event.list)
      pbPushScript(event.list, "pbTrainerEnd")
    end
  end
  
  # Generates a battle, the parameter difficulty can be trainer data
  # if you already generated a trainer
  def self.generateBattle(difficulty)
    trainer = difficulty
    trainer = generateTrainer(difficulty) if !difficulty.is_a?(Array)
    Kernel.pbMessage(_INTL(trainer[1]))
    scene=pbNewBattleScene
    battle=PokeBattle_Battle.new(scene,$Trainer.party,
    trainer[0].party,$Trainer,trainer[0])
    battle.fullparty1=trainer[0].party
    battle.doublebattle=false
    battle.endspeech = trainer[2]
    trainerbgm=pbGetTrainerBattleBGM(trainer[0])
    pbPrepareBattle(battle)

    restorebgm=true
    decision=0
    pbMEFade
    pbBattleAnimation(trainerbgm,trainer[0].trainertype,trainer[0].name) { 
     pbSceneStandby {
        decision=battle.pbStartBattle(false)
     }
    }
    return decision
  end
  
  # Generates a trainer based on difficulty (-1 means it will calculate difficulty
  # based on your party's level)
  def self.generateTrainer(difficulty, size = 3, forcedTypes = nil)
    if difficulty < 0
       difficulty = calcDifficulty
    end
    trainers = pbGetBTTrainers(0) # only normal battle tower trainers
    trainer = trainers[rand(trainers.length)]
    gender = getTrainerTypeGender(trainer[0])
    # Get a new random trainer type
    if forcedTypes
      while (forcedTypes.length > 0)
        type = forcedTypes[rand(forcedTypes.length)]
        cgender = getTrainerTypeGender(type)
        if gender == cgender
          trainer[0] = type
          break
        end
      end
    end
    
    tr = PokeBattle_Trainer.new(trainer[1], trainer[0])
    tr.party = generateParty(difficulty, size)
    
    return [tr, trainer[2], trainer[4]]
  end
  
  # Generates a party of size pokemon
  def self.generateParty(difficulty, size = 3)
    maxl = calcMaxLevel(difficulty)
    party = []
    for i in 0...size
      # Base the level off the trainer party but keep it within bounds
      l = pbBalancedLevel($Trainer.party)
      if difficulty == 0
        l -= 3
      else
        l -= 4 + rand(8)
      end
      if l > maxl
        l = maxl
      end
      pk = generatePokemon(l)
      pkmn = PokeBattle_Pokemon.new(pk, l + 1)
      party.push(pkmn)
    end
    return party
  end
  
  # Generates a wild pokemon based on difficulty (-1 means it will calculate
  # difficulty on your party's level)
  def self.generateWildPokemonData(difficulty = -1)
    if difficulty < 0
       difficulty = calcDifficulty(difficulty)
     end
     maxl = calcMaxLevel
     l = pbBalancedLevel($Trainer.party) - 4 + rand(8)
    if l > maxl
      l = maxl
    end
    species = generatePokemon(level)
    return [species, l]
  end
  
  
  # A Nice little generator for pokemon, it finds a random pokemon then get's
  # it's first evolution and then through a small checksum evolves it if some
  # conditions are met (pokemon can evolve up to 5 levels under the actual
  # evolution data)
  def self.generatePokemon(level)
    species = rand(PBSpecies.maxValue) + 1
    while BLACK_LIST.include?(species)
      rand(PBSpecies.maxValue) + 1
    end
    species = pbGetBabySpecies(species) # revert to the first evolution
    item = 0
    loop do
      nl = level + 5
      nl = MAXIMUMLEVEL if nl > MAXIMUMLEVEL
      pkmn = PokeBattle_Pokemon.new(species, nl)
      cevo = Kernel.pbCheckEvolution(pkmn)
      evo = pbGetEvolvedFormData(species)
      if evo
        evo = evo[rand(evo.length - 1)]
        # evolve the species if we can't evolve and there is an evolution
        # and a bit of randomness passes as well as the evolution type cannot
        # be by level
        if evo && cevo < 1 && rand(MAXIMUMLEVEL) <= level
          species = evo[2] if evo[0] != 4 && rand(MAXIMUMLEVEL) <= level
        end
      end
      if cevo == -1 || (rand(MAXIMUMLEVEL) > level && level < 60)
        # ^ Only break the loop if there is no more evolutions or some randomness
        # applies and the level is under 60 
        break
      else
        species = evo[2]
      end
    end
     return species
  end
end

class Game_Event
  attr_accessor :trainer
  
  def setTriggerToAction
    @trigger = 0
  end
  
  def setTriggerToEvent
    @trigger = 2
  end
end
For those wondering how to setup an event for this:
nd4MB8v.png


For those wondering how to setup a Random Trainer Generator Event:
QthxLhK.png


And lastly for those wondering how to setup a Random Trainer Spawn Point:
Basically you need an event which it's name must be "Trainer(amount of tiles it can see)" that is activated by event touch.
Q8n88eY.png
 
Last edited:

Daruda

Grinder
33
Posts
10
Years
  • Age 38
  • Seen Aug 24, 2014
This is really nice, I really don't want to make every trainer in a game, just the ones more important, and it would be nice to make these re-fightable to have every time differents battles.
Only question, If I make a overworld with someone, like a gym leader, I want random pokemon for him, but the Graphic must be of that person, so I can add a switch to make sure is that.
But if I want to make this for all my trainers It don't seems really neat.
There is already something I overlooked? or can you suggest me some method?
 
1,748
Posts
14
Years
This is really nice, I really don't want to make every trainer in a game, just the ones more important, and it would be nice to make these re-fightable to have every time differents battles.
Only question, If I make a overworld with someone, like a gym leader, I want random pokemon for him, but the Graphic must be of that person, so I can add a switch to make sure is that.
But if I want to make this for all my trainers It don't seems really neat.
There is already something I overlooked? or can you suggest me some method?

Well, you could generate a trainer and store that inside a global variable, then use the method "BattleGenerator.setTrainerGraphic(trainer data, event ID)" and it will automatically set the graphic to the corresponding trainer graphic. I don't really know if you mean something else though
 
1,748
Posts
14
Years
(Sorry in advanced if double posting isn't allowed for notifying updates but)

I Made some new changes to the script, well I added a feature to be exact (and hopefully the images attached should provide enough information to get it setup) Basically you can now generate random trainers on the map without much hassle!
 
2
Posts
8
Years
  • Age 29
  • Seen Jan 31, 2016
Hi Umbra, I have tried using your script on Pokemon Essentials v16.1 but I get this error whenever I attempt to launch the game. "Script 'AutoTRAINERS' line 83: SyntaxError Occured. Is this because this script is not compatible with the Pokemon Essentials version I'm using?
 
31
Posts
8
Years
Well I have no errors showing up and I copied from Thread Tool - Show Printable Version
But I still don't know how to make it work in v16.1
The images are missing on how to set it up right...
 
2
Posts
8
Years
  • Age 29
  • Seen Jan 31, 2016
wow lmao after copying from the printable version I get no error anymore, such a silly fix lol. I guess we just need to wait until something gets posted on how to use in v16.1 :(

Using wayback machine I have managed to find all the pictures on how to use the script :)
 
Last edited by a moderator:
8
Posts
7
Years
  • Age 37
  • Seen Jun 25, 2018
Hello! i think this script can be used to create the function to check the enemie pokemon and evolve it if needed, but i dont know about scripts. ¿it is really possible?

and i also idont know how to call this part of script to generate a wild pokemon.

# A Nice little generator for pokemon, it finds a random pokemon then get's
# it's first evolution and then through a small checksum evolves it if some
# conditions are met (pokemon can evolve up to 5 levels under the actual
# evolution data)
def self.generatePokemon(level)
species = rand(PBSpecies.maxValue) + 1
while BLACK_LIST.include?(species)
rand(PBSpecies.maxValue) + 1
end
species = pbGetBabySpecies(species) # revert to the first evolution
item = 0
loop do
nl = level + 5
nl = MAXIMUMLEVEL if nl > MAXIMUMLEVEL
pkmn = PokeBattle_Pokemon.new(species, nl)
cevo = Kernel.pbCheckEvolution(pkmn)
evo = pbGetEvolvedFormData(species)
if evo
evo = evo[rand(evo.length - 1)]
# evolve the species if we can't evolve and there is an evolution
# and a bit of randomness passes as well as the evolution type cannot
# be by level
if evo && cevo < 1 && rand(MAXIMUMLEVEL) <= level
species = evo[2] if evo[0] != 4 && rand(MAXIMUMLEVEL) <= level
end
end
if cevo == -1 || (rand(MAXIMUMLEVEL) > level && level < 60)
# ^ Only break the loop if there is no more evolutions or some randomness
# applies and the level is under 60
break
else
species = evo[2]
end
end
return species
end
end

class Game_Event
attr_accessor :trainer

def setTriggerToAction
@trigger = 0
end

def setTriggerToEvent
@trigger = 2
end
end
 
Last edited:
129
Posts
14
Years
  • Age 24
  • Seen Sep 4, 2023
For anyone wanting to use this script, there is a typo that will cause the script to loop indefinitely whenever a blacklisted pokémon is selected randomly.
To correct this all you have to do is change
Code:
while BLACK_LIST.include?(species)
      rand(PBSpecies.maxValue) + 1
    end
to
Code:
while BLACK_LIST.include?(species)
     [S-HIGHLIGHT]species = [/S-HIGHLIGHT] rand(PBSpecies.maxValue) + 1
    end
 
Back
Top