Go Back   The PokéCommunity Forums > Creative Discussions > Game Development > Scripts & Tutorials
Reload this Page [Essentials script] Random Battle Generator

Notices
For all updates, view the main page.

Scripts & Tutorials This forum is for scripts and code, as well as all kinds of tutorials, software, tools and so forth. Remember to give credit!
The thread revival limit does not apply here.



Reply
 
Thread Tools
  #1    
Old January 25th, 2014 (06:50 PM). Edited January 28th, 2014 by Umbreon.
Umbreon's Avatar
Umbreon Umbreon is offline
Gold Tier
 
Join Date: Aug 2009
Location: Nearby my feet.
Age: 18
Gender: Male
Nature: Calm
Posts: 1,586
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 Hansiec
# 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:


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


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.
Reply With Quote
  #2    
Old January 26th, 2014 (01:41 PM).
Daruda's Avatar
Daruda Daruda is offline
Grinder
 
Join Date: Jan 2014
Gender: Male
Posts: 33
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?
Reply With Quote
  #3    
Old January 26th, 2014 (06:41 PM).
Umbreon's Avatar
Umbreon Umbreon is offline
Gold Tier
 
Join Date: Aug 2009
Location: Nearby my feet.
Age: 18
Gender: Male
Nature: Calm
Posts: 1,586
Quote originally posted by Daruda:
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
Reply With Quote
  #4    
Old January 28th, 2014 (06:50 PM).
Umbreon's Avatar
Umbreon Umbreon is offline
Gold Tier
 
Join Date: Aug 2009
Location: Nearby my feet.
Age: 18
Gender: Male
Nature: Calm
Posts: 1,586
(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!
Reply With Quote
Reply
Quick Reply

Sponsored Links
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are UTC -8. The time now is 12:30 PM.