Advertisement
pokeminer20

Pokemon Essentials V17.2 PartyPlus V2.1

Aug 23rd, 2020 (edited)
1,417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 464.21 KB | None | 0 0
  1. ################################################################################
  2. #                               Party Plus V2.1                                #
  3. #                                By PokeMiner20                                #
  4. #This script allows for the modification of the number of pokemon a player can #
  5. #bring with them. it provedes 23 different party limits to use, and it all can #
  6. #be done by using one single command. To activate the Script, use the command  #
  7. #$Trainer.partyplus='#' in a script and replace '#' with any of the following  #
  8. #numbers: 1,2,3,4,5,8,9,10,12,14,15,16,18,20,2124,25,26,30,32,35, or 40. any   #
  9. #other number (Save perhaps 6) will result in an error in some sort, not right #
  10. #after changing the limit but in using the party list (No I will not look into #
  11. #why) As an added bonus, this script will also allow opponents to use more than#
  12. #6 pokemon, however they are not limited to the numbers not included above. a  #
  13. #few of the things this doesn't do for you is alter the Hall of Fame, the Joy  #
  14. #Healing Animation, or the continue screen inages, so be aware of that when    #
  15. #using this. Also, because of lazyness you will not be able to see the simple  #
  16. #stats of the pokemon in the Party Menu, this script is already huge as is.    #
  17. #Please give credit if you do, and enjoy this script.                    ~PDM20#
  18. ################################################################################
  19. # Trainer Modification
  20. ################################################################################
  21. class PokeBattle_Trainer
  22.   attr_accessor(:name)
  23.   attr_accessor(:id)
  24.   attr_accessor(:metaID)
  25.   attr_accessor(:trainertype)
  26.   attr_accessor(:outfit)
  27.   attr_accessor(:badges)
  28.   attr_accessor(:money)
  29.   attr_accessor(:seen)
  30.   attr_accessor(:owned)
  31.   attr_accessor(:formseen)
  32.   attr_accessor(:formlastseen)
  33.   attr_accessor(:shadowcaught)
  34.   attr_accessor(:party)
  35.   attr_accessor(:pokedex)    # Whether the Pokédex was obtained
  36.   attr_accessor(:pokegear)   # Whether the Pokégear was obtained
  37.   attr_accessor(:language)
  38.   attr_accessor(:partyplus)
  39.   def trainerTypeName   # Name of this trainer type (localized)
  40.     return PBTrainers.getName(@trainertype) rescue _INTL("PkMn Trainer")
  41.   end
  42.   def fullname
  43.     return _INTL("{1} {2}",self.trainerTypeName,@name)
  44.   end
  45.   def publicID(id=nil)   # Portion of the ID which is visible on the Trainer Card
  46.     return id ? id&0xFFFF : @id&0xFFFF
  47.   end
  48.   def secretID(id=nil)   # Other portion of the ID
  49.     return id ? id>>16 : @id>>16
  50.   end
  51.   def getForeignID   # Random ID other than this Trainer's ID
  52.     fid=0
  53.     loop do
  54.       fid=rand(256)
  55.       fid|=rand(256)<<8
  56.       fid|=rand(256)<<16
  57.       fid|=rand(256)<<24
  58.       break if fid!=@id
  59.     end
  60.     return fid
  61.   end
  62.   def setForeignID(other)
  63.     @id=other.getForeignID
  64.   end
  65.   def metaID
  66.     @metaID=$PokemonGlobal.playerID if !@metaID && $PokemonGlobal
  67.     @metaID=0 if !@metaID
  68.     return @metaID
  69.   end
  70.   def outfit
  71.     @outfit=0 if !@outfit
  72.     return @outfit
  73.   end
  74.   def partyplus
  75.     @partyplus=0 if !@partyplus
  76.     return @partyplus
  77.   end
  78.   def language
  79.     @language=pbGetLanguage() if !@language
  80.     return @language
  81.   end
  82.   def money=(value)
  83.     @money=[[value,MAXMONEY].min,0].max
  84.   end
  85.   def moneyEarned   # Money won when trainer is defeated
  86.     ret=0
  87.     pbRgssOpen("Data/trainertypes.dat","rb"){|f|
  88.        trainertypes=Marshal.load(f)
  89.        return 30 if !trainertypes[@trainertype]
  90.        ret=trainertypes[@trainertype][3]
  91.     }
  92.     return ret
  93.   end
  94.   def skill   # Skill level (for AI)
  95.     ret=0
  96.     pbRgssOpen("Data/trainertypes.dat","rb"){|f|
  97.        trainertypes=Marshal.load(f)
  98.        return 30 if !trainertypes[@trainertype]
  99.        ret=trainertypes[@trainertype][8]
  100.     }
  101.     return ret
  102.   end
  103.   def skillCode
  104.     ret=""
  105.     pbRgssOpen("Data/trainertypes.dat","rb"){|f|
  106.        trainertypes=Marshal.load(f)
  107.        return "" if !trainertypes[@trainertype]
  108.        ret=trainertypes[@trainertype][9]
  109.     }
  110.     return ret
  111.   end
  112.   def hasSkillCode(code)
  113.     c=skillCode
  114.     return true if c!=nil && c!="" && c[/#{code}/]
  115.     return false
  116.   end
  117.   def numbadges   # Number of badges
  118.     ret=0
  119.     for i in 0...@badges.length
  120.       ret+=1 if @badges[i]
  121.     end
  122.     return ret
  123.   end
  124.   def gender
  125.     ret=2   # 2 = gender unknown
  126.     pbRgssOpen("Data/trainertypes.dat","rb"){|f|
  127.        trainertypes=Marshal.load(f)
  128.        if !trainertypes || !trainertypes[trainertype]
  129.          ret=2
  130.        else
  131.          ret=trainertypes[trainertype][7]
  132.          ret=2 if !ret
  133.        end
  134.     }
  135.     return ret
  136.   end
  137.   def isMale?; return self.gender==0; end
  138.   def isFemale?; return self.gender==1; end
  139.   def pokemonParty
  140.     return @party.find_all {|item| item && !item.egg? }
  141.   end
  142.   def ablePokemonParty
  143.     return @party.find_all {|item| item && !item.egg? && item.hp>0 }
  144.   end
  145.   def partyCount
  146.     return @party.length
  147.   end
  148.   def pokemonCount
  149.     ret=0
  150.     for i in 0...@party.length
  151.       ret+=1 if @party[i] && !@party[i].egg?
  152.     end
  153.     return ret
  154.   end
  155.   def ablePokemonCount
  156.     ret=0
  157.     for i in 0...@party.length
  158.       ret+=1 if @party[i] && !@party[i].egg? && @party[i].hp>0
  159.     end
  160.     return ret
  161.   end
  162.   def firstParty
  163.     return nil if @party.length==0
  164.     return @party[0]
  165.   end
  166.   def firstPokemon
  167.     p=self.pokemonParty
  168.     return nil if p.length==0
  169.     return p[0]
  170.   end
  171.   def firstAblePokemon
  172.     p=self.ablePokemonParty
  173.     return nil if p.length==0
  174.     return p[0]
  175.   end
  176.   def lastParty
  177.     return nil if @party.length==0
  178.     return @party[@party.length-1]
  179.   end
  180.   def lastPokemon
  181.     p=self.pokemonParty
  182.     return nil if p.length==0
  183.     return p[p.length-1]
  184.   end
  185.   def lastAblePokemon
  186.     p=self.ablePokemonParty
  187.     return nil if p.length==0
  188.     return p[p.length-1]
  189.   end
  190.   def pokedexSeen(region=-1)   # Number of Pokémon seen
  191.     ret=0
  192.     if region==-1
  193.       for i in 0..PBSpecies.maxValue
  194.         ret+=1 if @seen[i]
  195.       end
  196.     else
  197.       regionlist=pbAllRegionalSpecies(region)
  198.       for i in regionlist
  199.         ret+=1 if @seen[i]
  200.       end
  201.     end
  202.     return ret
  203.   end
  204.   def pokedexOwned(region=-1)   # Number of Pokémon owned
  205.     ret=0
  206.     if region==-1
  207.       for i in 0..PBSpecies.maxValue
  208.         ret+=1 if @owned[i]
  209.       end
  210.     else
  211.       regionlist=pbAllRegionalSpecies(region)
  212.       for i in regionlist
  213.         ret+=1 if @owned[i]
  214.       end
  215.     end
  216.     return ret
  217.   end
  218.   def numFormsSeen(species)
  219.     ret=0
  220.     array=@formseen[species]
  221.     for i in 0...[array[0].length,array[1].length].max
  222.       ret+=1 if array[0][i] || array[1][i]
  223.     end
  224.     return ret
  225.   end
  226.   def hasSeen?(species)
  227.     if species.is_a?(String) || species.is_a?(Symbol)
  228.       species=getID(PBSpecies,species)
  229.     end
  230.     return species>0 ? @seen[species] : false
  231.   end
  232.   def hasOwned?(species)
  233.     if species.is_a?(String) || species.is_a?(Symbol)
  234.       species=getID(PBSpecies,species)
  235.     end
  236.     return species>0 ? @owned[species] : false
  237.   end
  238.   def setSeen(species)
  239.     if species.is_a?(String) || species.is_a?(Symbol)
  240.       species=getID(PBSpecies,species)
  241.     end
  242.     @seen[species]=true if species>0
  243.   end
  244.   def setOwned(species)
  245.     if species.is_a?(String) || species.is_a?(Symbol)
  246.       species=getID(PBSpecies,species)
  247.     end
  248.     @owned[species]=true if species>0
  249.   end
  250.   def clearPokedex
  251.     @seen=[]
  252.     @owned=[]
  253.     @formseen=[]
  254.     @formlastseen=[]
  255.     for i in 1..PBSpecies.maxValue
  256.       @seen[i]=false
  257.       @owned[i]=false
  258.       @formlastseen[i]=[]
  259.       @formseen[i]=[[],[]]
  260.     end
  261.   end
  262.   def initialize(name,trainertype)
  263.     @name=name
  264.     @language=pbGetLanguage()
  265.     @trainertype=trainertype
  266.     @id=rand(256)
  267.     @id|=rand(256)<<8
  268.     @id|=rand(256)<<16
  269.     @id|=rand(256)<<24
  270.     @metaID=0
  271.     @outfit=0
  272.     @partyplus=0
  273.     @pokegear=false
  274.     @pokedex=false
  275.     clearPokedex
  276.     @shadowcaught=[]
  277.     for i in 1..PBSpecies.maxValue
  278.       @shadowcaught[i]=false
  279.     end
  280.     @badges=[]
  281.     for i in 0...8
  282.       @badges[i]=false
  283.     end
  284.     @money=INITIALMONEY
  285.     @party=[]
  286.   end
  287. end
  288. ################################################################################
  289. # Main battle class.
  290. ################################################################################
  291. class PokeBattle_Battle
  292.   attr_reader(:scene)             # Scene object for this battle
  293.   attr_accessor(:decision)        # Decision: 0=undecided; 1=win; 2=loss; 3=escaped; 4=caught
  294.   attr_accessor(:internalbattle)  # Internal battle flag
  295.   attr_accessor(:doublebattle)    # Double battle flag
  296.   attr_accessor(:cantescape)      # True if player can't escape
  297.   attr_accessor(:shiftStyle)      # Shift/Set "battle style" option
  298.   attr_accessor(:battlescene)     # "Battle scene" option
  299.   attr_accessor(:debug)           # Debug flag
  300.   attr_reader(:player)            # Player trainer
  301.   attr_reader(:opponent)          # Opponent trainer
  302.   attr_reader(:party1)            # Player's Pokémon party
  303.   attr_reader(:party2)            # Foe's Pokémon party
  304.   attr_reader(:party1order)       # Order of Pokémon in the player's party
  305.   attr_reader(:party2order)       # Order of Pokémon in the opponent's party
  306.   attr_accessor(:fullparty1)      # True if player's party's max size is 6 instead of 3
  307.   attr_accessor(:fullparty2)      # True if opponent's party's max size is 6 instead of 3
  308.   attr_reader(:battlers)          # Currently active Pokémon
  309.   attr_accessor(:items)           # Items held by opponents
  310.   attr_reader(:sides)             # Effects common to each side of a battle
  311.   attr_reader(:field)             # Effects common to the whole of a battle
  312.   attr_accessor(:environment)     # Battle surroundings
  313.   attr_accessor(:weather)         # Current weather, custom methods should use pbWeather instead
  314.   attr_accessor(:weatherduration) # Duration of current weather, or -1 if indefinite
  315.   attr_reader(:switching)         # True if during the switching phase of the round
  316.   attr_reader(:futuresight)       # True if Future Sight is hitting
  317.   attr_reader(:struggle)          # The Struggle move
  318.   attr_accessor(:choices)         # Choices made by each Pokémon this round
  319.   attr_reader(:successStates)     # Success states
  320.   attr_accessor(:lastMoveUsed)    # Last move used
  321.   attr_accessor(:lastMoveUser)    # Last move user
  322.   attr_accessor(:megaEvolution)   # Battle index of each trainer's Pokémon to Mega Evolve
  323.   attr_accessor(:amuletcoin)      # Whether Amulet Coin's effect applies
  324.   attr_accessor(:extramoney)      # Money gained in battle by using Pay Day
  325.   attr_accessor(:doublemoney)     # Whether Happy Hour's effect applies
  326.   attr_accessor(:endspeech)       # Speech by opponent when player wins
  327.   attr_accessor(:endspeech2)      # Speech by opponent when player wins
  328.   attr_accessor(:endspeechwin)    # Speech by opponent when opponent wins
  329.   attr_accessor(:endspeechwin2)   # Speech by opponent when opponent wins
  330.   attr_accessor(:rules)
  331.   attr_reader(:turncount)
  332.   attr_accessor :controlPlayer
  333.   include PokeBattle_BattleCommon
  334.   MAXPARTYSIZE = 40
  335.   class BattleAbortedException < Exception; end
  336.   def pbAbort
  337.     raise BattleAbortedException.new("Battle aborted")
  338.   end
  339.   def pbDebugUpdate
  340.   end
  341.   def pbRandom(x)
  342.     return rand(x)
  343.   end
  344.   def pbAIRandom(x)
  345.     return rand(x)
  346.   end
  347. ################################################################################
  348. # Initialise battle class.
  349. ################################################################################
  350.   def initialize(scene,p1,p2,player,opponent)
  351.     if p1.length==0
  352.       raise ArgumentError.new(_INTL("Party 1 has no Pokémon."))
  353.       return
  354.     end
  355.     if p2.length==0
  356.       raise ArgumentError.new(_INTL("Party 2 has no Pokémon."))
  357.       return
  358.     end
  359.     if p2.length>2 && !opponent
  360.       raise ArgumentError.new(_INTL("Wild battles with more than two Pokémon are not allowed."))
  361.       return
  362.     end
  363.     @scene           = scene
  364.     @decision        = 0
  365.     @internalbattle  = true
  366.     @doublebattle    = false
  367.     @cantescape      = false
  368.     @shiftStyle      = true
  369.     @battlescene     = true
  370.     @debug           = false
  371.     @debugupdate     = 0
  372.     if opponent && player.is_a?(Array) && player.length==0
  373.       player = player[0]
  374.     end
  375.     if opponent && opponent.is_a?(Array) && opponent.length==0
  376.       opponent = opponent[0]
  377.     end
  378.     @player          = player                # PokeBattle_Trainer object
  379.     @opponent        = opponent              # PokeBattle_Trainer object
  380.     @party1          = p1
  381.     @party2          = p2
  382.     if $Trainer.partyplus==40
  383.     @party1order     = []
  384.     for i in 0...80; @party1order.push(i); end
  385.     elsif $Trainer.partyplus==35
  386.     @party1order     = []
  387.     for i in 0...75; @party1order.push(i); end
  388.     elsif $Trainer.partyplus==32
  389.     @party1order     = []
  390.     for i in 0...72; @party1order.push(i); end
  391.     elsif $Trainer.partyplus==30
  392.     @party1order     = []
  393.     for i in 0...70; @party1order.push(i); end
  394.     elsif $Trainer.partyplus==28
  395.     @party1order     = []
  396.     for i in 0...68; @party1order.push(i); end
  397.     elsif $Trainer.partyplus==25
  398.     @party1order     = []
  399.     for i in 0...65; @party1order.push(i); end
  400.     elsif $Trainer.partyplus==24
  401.     @party1order     = []
  402.     for i in 0...64; @party1order.push(i); end
  403.     elsif $Trainer.partyplus==21
  404.     @party1order     = []
  405.     for i in 0...61; @party1order.push(i); end
  406.     elsif $Trainer.partyplus==20
  407.     @party1order     = []
  408.     for i in 0...60; @party1order.push(i); end
  409.     elsif $Trainer.partyplus==18
  410.     @party1order     = []
  411.     for i in 0...58; @party1order.push(i); end
  412.     elsif $Trainer.partyplus==16
  413.     @party1order     = []
  414.     for i in 0...56; @party1order.push(i); end
  415.     elsif $Trainer.partyplus==15
  416.     @party1order     = []
  417.     for i in 0...55; @party1order.push(i); end
  418.     elsif $Trainer.partyplus==14
  419.     @party1order     = []
  420.     for i in 0...54; @party1order.push(i); end
  421.     elsif $Trainer.partyplus==12
  422.     @party1order     = []
  423.     for i in 0...52; @party1order.push(i); end
  424.     elsif $Trainer.partyplus==10
  425.     @party1order     = []
  426.     for i in 0...50; @party1order.push(i); end
  427.     elsif $Trainer.partyplus==9
  428.     @party1order     = []
  429.     for i in 0...49; @party1order.push(i); end
  430.     elsif $Trainer.partyplus==8
  431.     @party1order     = []
  432.     for i in 0...48; @party1order.push(i); end
  433.     elsif $Trainer.partyplus==5
  434.     @party1order     = []
  435.     for i in 0...45; @party1order.push(i); end
  436.     elsif $Trainer.partyplus==4
  437.     @party1order     = []
  438.     for i in 0...44; @party1order.push(i); end
  439.     elsif $Trainer.partyplus==3
  440.     @party1order     = []
  441.     for i in 0...43; @party1order.push(i); end
  442.     elsif $Trainer.partyplus==2
  443.     @party1order     = []
  444.     for i in 0...42; @party1order.push(i); end
  445.     elsif $Trainer.partyplus==1
  446.     @party1order     = []
  447.     for i in 0...41; @party1order.push(i); end
  448.     else
  449.     @party1order     = []
  450.     for i in 0...46; @party1order.push(i); end
  451.     end
  452.     @party2order     = []
  453.     for i in 0...80; @party2order.push(i); end
  454.     @fullparty1      = false
  455.     @fullparty2      = false
  456.     @battlers        = []
  457.     @items           = nil
  458.     @sides           = [PokeBattle_ActiveSide.new,   # Player's side
  459.                         PokeBattle_ActiveSide.new]   # Foe's side
  460.     @field           = PokeBattle_ActiveField.new    # Whole field (gravity/rooms)
  461.     @environment     = PBEnvironment::None   # e.g. Tall grass, cave, still water
  462.     @weather         = 0
  463.     @weatherduration = 0
  464.     @switching       = false
  465.     @futuresight     = false
  466.     @choices         = [ [0,0,nil,-1],[0,0,nil,-1],[0,0,nil,-1],[0,0,nil,-1] ]
  467.     @successStates   = []
  468.     for i in 0...4
  469.       @successStates.push(PokeBattle_SuccessState.new)
  470.     end
  471.     @lastMoveUsed    = -1
  472.     @lastMoveUser    = -1
  473.     @nextPickupUse   = 0
  474.     @megaEvolution   = []
  475.     if @player.is_a?(Array)
  476.       @megaEvolution[0]=[-1]*@player.length
  477.     else
  478.       @megaEvolution[0]=[-1]
  479.     end
  480.     if @opponent.is_a?(Array)
  481.       @megaEvolution[1]=[-1]*@opponent.length
  482.     else
  483.       @megaEvolution[1]=[-1]
  484.     end
  485.     @amuletcoin      = false
  486.     @extramoney      = 0
  487.     @doublemoney     = false
  488.     @endspeech       = ""
  489.     @endspeech2      = ""
  490.     @endspeechwin    = ""
  491.     @endspeechwin2   = ""
  492.     @rules           = {}
  493.     @turncount       = 0
  494.     @peer            = PokeBattle_BattlePeer.create()
  495.     @priority        = []
  496.     @usepriority     = false
  497.     @snaggedpokemon  = []
  498.     @runCommand      = 0
  499.     if hasConst?(PBMoves,:STRUGGLE)
  500.       @struggle = PokeBattle_Move.pbFromPBMove(self,PBMove.new(getConst(PBMoves,:STRUGGLE)))
  501.     else
  502.       @struggle = PokeBattle_Struggle.new(self,nil)
  503.     end
  504.     @struggle.pp     = -1
  505.     for i in 0...4
  506.       battlers[i] = PokeBattle_Battler.new(self,i)
  507.     end
  508.     for i in @party1
  509.       next if !i
  510.       i.itemRecycle = 0
  511.       i.itemInitial = i.item
  512.       i.belch       = false
  513.     end
  514.     for i in @party2
  515.       next if !i
  516.       i.itemRecycle = 0
  517.       i.itemInitial = i.item
  518.       i.belch       = false
  519.     end
  520.   end
  521. ################################################################################
  522. # Info about battle.
  523. ################################################################################
  524.   def pbDoubleBattleAllowed?
  525.     return false if !@fullparty1 && @party1.length>MAXPARTYSIZE
  526.     return false if !@fullparty2 && @party2.length>MAXPARTYSIZE
  527.     _opponent=@opponent
  528.     _player=@player
  529.     # Wild battle
  530.     if !_opponent
  531.       if @party2.length==1
  532.         return false
  533.       elsif @party2.length==2
  534.         return true
  535.       else
  536.         return false
  537.       end
  538.     # Trainer battle
  539.     else
  540.       if _opponent.is_a?(Array)
  541.         if _opponent.length==1
  542.           _opponent=_opponent[0]
  543.         elsif _opponent.length!=2
  544.           return false
  545.         end
  546.       end
  547.       _player=_player
  548.       if _player.is_a?(Array)
  549.         if _player.length==1
  550.           _player=_player[0]
  551.         elsif _player.length!=2
  552.           return false
  553.         end
  554.       end
  555.       if _opponent.is_a?(Array)
  556.         sendout1=pbFindNextUnfainted(@party2,0,pbSecondPartyBegin(1))
  557.         sendout2=pbFindNextUnfainted(@party2,pbSecondPartyBegin(1))
  558.         return false if sendout1<0 || sendout2<0
  559.       else
  560.         sendout1=pbFindNextUnfainted(@party2,0)
  561.         sendout2=pbFindNextUnfainted(@party2,sendout1+1)
  562.         return false if sendout1<0 || sendout2<0
  563.       end
  564.     end
  565.     if _player.is_a?(Array)
  566.       sendout1=pbFindNextUnfainted(@party1,0,pbSecondPartyBegin(0))
  567.       sendout2=pbFindNextUnfainted(@party1,pbSecondPartyBegin(0))
  568.       return false if sendout1<0 || sendout2<0
  569.     else
  570.       sendout1=pbFindNextUnfainted(@party1,0)
  571.       sendout2=pbFindNextUnfainted(@party1,sendout1+1)
  572.       return false if sendout1<0 || sendout2<0
  573.     end
  574.     return true
  575.   end
  576.   def pbWeather
  577.     for i in 0...4
  578.       if @battlers[i].hasWorkingAbility(:CLOUDNINE) ||
  579.          @battlers[i].hasWorkingAbility(:AIRLOCK)
  580.         return 0
  581.       end
  582.     end
  583.     return @weather
  584.   end
  585. ################################################################################
  586. # Get battler info.
  587. ################################################################################
  588.   def pbIsOpposing?(index)
  589.     return (index%2)==1
  590.   end
  591.   def pbOwnedByPlayer?(index)
  592.     return false if pbIsOpposing?(index)
  593.     return false if @player.is_a?(Array) && index==2
  594.     return true
  595.   end
  596.   def pbIsDoubleBattler?(index)
  597.     return (index>=2)
  598.   end
  599.   # Only used for Wish
  600.   def pbThisEx(battlerindex,pokemonindex)
  601.     party=pbParty(battlerindex)
  602.     if pbIsOpposing?(battlerindex)
  603.       if @opponent
  604.         return _INTL("The foe {1}",party[pokemonindex].name)
  605.       else
  606.         return _INTL("The wild {1}",party[pokemonindex].name)
  607.       end
  608.     else
  609.       return party[pokemonindex].name
  610.     end
  611.   end
  612. # Checks whether an item can be removed from a Pokémon.
  613.   def pbIsUnlosableItem(pkmn,item)
  614.     return true if pbIsMail?(item)
  615.     return false if pkmn.effects[PBEffects::Transform]
  616.     return true if pkmn.pokemon && pkmn.pokemon.getMegaForm(true)!=0 # Mega Stone
  617.     if isConst?(pkmn.ability,PBAbilities,:MULTITYPE)
  618.       plates=[:FISTPLATE,:SKYPLATE,:TOXICPLATE,:EARTHPLATE,:STONEPLATE,
  619.               :INSECTPLATE,:SPOOKYPLATE,:IRONPLATE,:FLAMEPLATE,:SPLASHPLATE,
  620.               :MEADOWPLATE,:ZAPPLATE,:MINDPLATE,:ICICLEPLATE,:DRACOPLATE,
  621.               :DREADPLATE,:PIXIEPLATE]
  622.       for i in plates
  623.         return true if isConst?(item,PBItems,i)
  624.       end
  625.     end
  626.     combos=[[:GIRATINA,:GRISEOUSORB],
  627.             [:GENESECT,:BURNDRIVE],
  628.             [:GENESECT,:CHILLDRIVE],
  629.             [:GENESECT,:DOUSEDRIVE],
  630.             [:GENESECT,:SHOCKDRIVE],
  631.             # Primal Reversion stones
  632.             [:KYOGRE,:BLUEORB],
  633.             [:GROUDON,:REDORB]
  634.            ]
  635.     for i in combos
  636.       if isConst?(pkmn.species,PBSpecies,i[0]) && isConst?(item,PBItems,i[1])
  637.         return true
  638.       end
  639.     end
  640.     return false
  641.   end
  642.   def pbCheckGlobalAbility(a)
  643.     for i in 0...4 # in order from own first, opposing first, own second, opposing second
  644.       if @battlers[i].hasWorkingAbility(a)
  645.         return @battlers[i]
  646.       end
  647.     end
  648.     return nil
  649.   end
  650.   def nextPickupUse
  651.     @nextPickupUse+=1
  652.     return @nextPickupUse
  653.   end
  654. ################################################################################
  655. # Player-related info.
  656. ################################################################################
  657.   def pbPlayer
  658.     if @player.is_a?(Array)
  659.       return @player[0]
  660.     else
  661.       return @player
  662.     end
  663.   end
  664.   def pbGetOwnerItems(battlerIndex)
  665.     return [] if !@items
  666.     if pbIsOpposing?(battlerIndex)
  667.       if @opponent.is_a?(Array)
  668.         return (battlerIndex==1) ? @items[0] : @items[1]
  669.       else
  670.         return @items
  671.       end
  672.     else
  673.       return []
  674.     end
  675.   end
  676.   def pbSetSeen(pokemon)
  677.     if pokemon && @internalbattle
  678.       self.pbPlayer.seen[pokemon.species]=true
  679.       pbSeenForm(pokemon)
  680.     end
  681.   end
  682.   def pbGetMegaRingName(battlerIndex)
  683.     if pbBelongsToPlayer?(battlerIndex)
  684.       for i in MEGARINGS
  685.         next if !hasConst?(PBItems,i)
  686.         return PBItems.getName(getConst(PBItems,i)) if $PokemonBag.pbHasItem?(i)
  687.       end
  688.     end
  689.     # Add your own Mega objects for particular NPC trainers here
  690. #    if isConst?(pbGetOwner(battlerIndex).trainertype,PBTrainers,:BUGCATCHER)
  691. #      return _INTL("Mega Net")
  692. #    end
  693.     return _INTL("Mega Ring")
  694.   end
  695.   def pbHasMegaRing?(battlerIndex)
  696.     return true if !pbBelongsToPlayer?(battlerIndex)
  697.     for i in MEGARINGS
  698.       next if !hasConst?(PBItems,i)
  699.       return true if $PokemonBag.pbHasItem?(i)
  700.     end
  701.     return false
  702.   end
  703. ################################################################################
  704. # Get party info, manipulate parties.
  705. ################################################################################
  706.   def pbPokemonCount(party)
  707.     count=0
  708.     for i in party
  709.       next if !i
  710.       count+=1 if i.hp>0 && !i.egg?
  711.     end
  712.     return count
  713.   end
  714.   def pbAllFainted?(party)
  715.     pbPokemonCount(party)==0
  716.   end
  717.   def pbMaxLevel(party)
  718.     lv=0
  719.     for i in party
  720.       next if !i
  721.       lv=i.level if lv<i.level
  722.     end
  723.     return lv
  724.   end
  725.   def pbMaxLevelFromIndex(index)
  726.     party=pbParty(index)
  727.     owner=(pbIsOpposing?(index)) ? @opponent : @player
  728.     maxlevel=0
  729.     if owner.is_a?(Array)
  730.       start=0
  731.       limit=pbSecondPartyBegin(index)
  732.       start=limit if pbIsDoubleBattler?(index)
  733.       for i in start...start+limit
  734.         next if !party[i]
  735.         maxlevel=party[i].level if maxlevel<party[i].level
  736.       end
  737.     else
  738.       for i in party
  739.         next if !i
  740.         maxlevel=i.level if maxlevel<i.level
  741.       end
  742.     end
  743.     return maxlevel
  744.   end
  745.   def pbParty(index)
  746.     return pbIsOpposing?(index) ? party2 : party1
  747.   end
  748.   def pbOpposingParty(index)
  749.     return pbIsOpposing?(index) ? party1 : party2
  750.   end
  751.   def pbSecondPartyBegin(battlerIndex) #Modify Me if Double Battles are being weird
  752.     if $Trainer.partyplus==40
  753.     if pbIsOpposing?(battlerIndex)
  754.       return @fullparty2 ? 40 : 20
  755.     else
  756.       return @fullparty1 ? 40 : 20
  757.     end
  758.     elsif $Trainer.partyplus==35
  759.     if pbIsOpposing?(battlerIndex)
  760.       return @fullparty2 ? 40 : 20
  761.     else
  762.       return @fullparty1 ? 35 : 35
  763.     end
  764.     elsif $Trainer.partyplus==32
  765.     if pbIsOpposing?(battlerIndex)
  766.       return @fullparty2 ? 40 : 20
  767.     else
  768.       return @fullparty1 ? 32 : 16
  769.     end
  770.     elsif $Trainer.partyplus==30
  771.     if pbIsOpposing?(battlerIndex)
  772.       return @fullparty2 ? 40 : 20
  773.     else
  774.       return @fullparty1 ? 30 : 15
  775.     end
  776.     elsif $Trainer.partyplus==28
  777.     if pbIsOpposing?(battlerIndex)
  778.       return @fullparty2 ? 40 : 20
  779.     else
  780.       return @fullparty1 ? 28 : 14
  781.     end
  782.     elsif $Trainer.partyplus==25
  783.     if pbIsOpposing?(battlerIndex)
  784.       return @fullparty2 ? 40 : 20
  785.     else
  786.       return @fullparty1 ? 25 : 25
  787.     end
  788.     elsif $Trainer.partyplus==24
  789.     if pbIsOpposing?(battlerIndex)
  790.       return @fullparty2 ? 40 : 20
  791.     else
  792.       return @fullparty1 ? 24 : 12
  793.     end
  794.     elsif $Trainer.partyplus==21
  795.     if pbIsOpposing?(battlerIndex)
  796.       return @fullparty2 ? 40 : 20
  797.     else
  798.       return @fullparty1 ? 21 : 21
  799.     end
  800.     elsif $Trainer.partyplus==20
  801.     if pbIsOpposing?(battlerIndex)
  802.       return @fullparty2 ? 40 : 20
  803.     else
  804.       return @fullparty1 ? 20 : 10
  805.     end
  806.     elsif $Trainer.partyplus==18
  807.     if pbIsOpposing?(battlerIndex)
  808.       return @fullparty2 ? 40 : 20
  809.     else
  810.       return @fullparty1 ? 18 : 9
  811.     end
  812.     elsif $Trainer.partyplus==16
  813.     if pbIsOpposing?(battlerIndex)
  814.       return @fullparty2 ? 40 : 20
  815.     else
  816.       return @fullparty1 ? 16 : 8
  817.     end
  818.     elsif $Trainer.partyplus==15
  819.     if pbIsOpposing?(battlerIndex)
  820.       return @fullparty2 ? 40 : 20
  821.     else
  822.       return @fullparty1 ? 15 : 15
  823.     end
  824.     elsif $Trainer.partyplus==14
  825.     if pbIsOpposing?(battlerIndex)
  826.       return @fullparty2 ? 40 : 20
  827.     else
  828.       return @fullparty1 ? 14 : 7
  829.     end
  830.     elsif $Trainer.partyplus==12
  831.     if pbIsOpposing?(battlerIndex)
  832.       return @fullparty2 ? 40 : 20
  833.     else
  834.       return @fullparty1 ? 12 : 6
  835.     end
  836.     elsif $Trainer.partyplus==10
  837.     if pbIsOpposing?(battlerIndex)
  838.       return @fullparty2 ? 40 : 20
  839.     else
  840.       return @fullparty1 ? 10 : 5
  841.     end
  842.     elsif $Trainer.partyplus==9
  843.     if pbIsOpposing?(battlerIndex)
  844.       return @fullparty2 ? 40 : 20
  845.     else
  846.       return @fullparty1 ? 9 : 9
  847.     end
  848.     elsif $Trainer.partyplus==8
  849.     if pbIsOpposing?(battlerIndex)
  850.       return @fullparty2 ? 40 : 20
  851.     else
  852.       return @fullparty1 ? 8 : 4
  853.     end
  854.     elsif $Trainer.partyplus==5
  855.     if pbIsOpposing?(battlerIndex)
  856.       return @fullparty2 ? 40 : 20
  857.     else
  858.       return @fullparty1 ? 5 : 5
  859.     end
  860.     elsif $Trainer.partyplus==4
  861.     if pbIsOpposing?(battlerIndex)
  862.       return @fullparty2 ? 40 : 20
  863.     else
  864.       return @fullparty1 ? 4 : 2
  865.     end
  866.     elsif $Trainer.partyplus==3
  867.     if pbIsOpposing?(battlerIndex)
  868.       return @fullparty2 ? 40 : 20
  869.     else
  870.       return @fullparty1 ? 3 : 3
  871.     end
  872.     elsif $Trainer.partyplus==2
  873.     if pbIsOpposing?(battlerIndex)
  874.       return @fullparty2 ? 40 : 20
  875.     else
  876.       return @fullparty1 ? 2 : 2
  877.     end
  878.     elsif $Trainer.partyplus==1
  879.     if pbIsOpposing?(battlerIndex)
  880.       return @fullparty2 ? 40 : 20
  881.     else
  882.       return @fullparty1 ? 1 : 1
  883.     end
  884.     else
  885.     if pbIsOpposing?(battlerIndex)
  886.       return @fullparty2 ? 6 : 3
  887.     else
  888.       return @fullparty1 ? 6 : 3
  889.     end
  890.     end
  891.   end
  892.   def pbPartyLength(battlerIndex)
  893.     if pbIsOpposing?(battlerIndex)
  894.       return (@opponent.is_a?(Array)) ? pbSecondPartyBegin(battlerIndex) : MAXPARTYSIZE
  895.     else
  896.       return @player.is_a?(Array) ? pbSecondPartyBegin(battlerIndex) : MAXPARTYSIZE
  897.     end
  898.   end
  899.   def pbFindNextUnfainted(party,start,finish=-1)
  900.     finish=party.length if finish<0
  901.     for i in start...finish
  902.       next if !party[i]
  903.       return i if party[i].hp>0 && !party[i].egg?
  904.     end
  905.     return -1
  906.   end
  907.   def pbGetLastPokeInTeam(index)
  908.     party=pbParty(index)
  909.     partyorder=(!pbIsOpposing?(index)) ? @party1order : @party2order
  910.     plength=pbPartyLength(index)
  911.     pstart=pbGetOwnerIndex(index)*plength
  912.     lastpoke=-1
  913.     for i in pstart...pstart+plength
  914.       p=party[partyorder[i]]
  915.       next if !p || p.egg? || p.hp<=0
  916.       lastpoke=partyorder[i]
  917.     end
  918.     return lastpoke
  919.   end
  920.   def pbFindPlayerBattler(pkmnIndex)
  921.     battler=nil
  922.     for k in 0...4
  923.       if !pbIsOpposing?(k) && @battlers[k].pokemonIndex==pkmnIndex
  924.         battler=@battlers[k]
  925.         break
  926.       end
  927.     end
  928.     return battler
  929.   end
  930.   def pbIsOwner?(battlerIndex,partyIndex)
  931.     secondParty=pbSecondPartyBegin(battlerIndex)
  932.     if !pbIsOpposing?(battlerIndex)
  933.       return true if !@player || !@player.is_a?(Array)
  934.       return (battlerIndex==0) ? partyIndex<secondParty : partyIndex>=secondParty
  935.     else
  936.       return true if !@opponent || !@opponent.is_a?(Array)
  937.       return (battlerIndex==1) ? partyIndex<secondParty : partyIndex>=secondParty
  938.     end
  939.   end
  940.   def pbGetOwner(battlerIndex)
  941.     if pbIsOpposing?(battlerIndex)
  942.       if @opponent.is_a?(Array)
  943.         return (battlerIndex==1) ? @opponent[0] : @opponent[1]
  944.       else
  945.         return @opponent
  946.       end
  947.     else
  948.       if @player.is_a?(Array)
  949.         return (battlerIndex==0) ? @player[0] : @player[1]
  950.       else
  951.         return @player
  952.       end
  953.     end
  954.   end
  955.   def pbGetOwnerPartner(battlerIndex)
  956.     if pbIsOpposing?(battlerIndex)
  957.       if @opponent.is_a?(Array)
  958.         return (battlerIndex==1) ? @opponent[1] : @opponent[0]
  959.       else
  960.         return @opponent
  961.       end
  962.     else
  963.       if @player.is_a?(Array)
  964.         return (battlerIndex==0) ? @player[1] : @player[0]
  965.       else
  966.         return @player
  967.       end
  968.     end
  969.   end
  970.   def pbGetOwnerIndex(battlerIndex)
  971.     if pbIsOpposing?(battlerIndex)
  972.       return (@opponent.is_a?(Array)) ? ((battlerIndex==1) ? 0 : 1) : 0
  973.     else
  974.       return (@player.is_a?(Array)) ? ((battlerIndex==0) ? 0 : 1) : 0
  975.     end
  976.   end
  977.   def pbBelongsToPlayer?(battlerIndex)
  978.     if @player.is_a?(Array) && @player.length>1
  979.       return battlerIndex==0
  980.     else
  981.       return (battlerIndex%2)==0
  982.     end
  983.     return false
  984.   end
  985.   def pbPartyGetOwner(battlerIndex,partyIndex)
  986.     secondParty=pbSecondPartyBegin(battlerIndex)
  987.     if !pbIsOpposing?(battlerIndex)
  988.       return @player if !@player || !@player.is_a?(Array)
  989.       return (partyIndex<secondParty) ? @player[0] : @player[1]
  990.     else
  991.       return @opponent if !@opponent || !@opponent.is_a?(Array)
  992.       return (partyIndex<secondParty) ? @opponent[0] : @opponent[1]
  993.     end
  994.   end
  995.   def pbAddToPlayerParty(pokemon)
  996.     party=pbParty(0)
  997.     for i in 0...party.length
  998.       party[i]=pokemon if pbIsOwner?(0,i) && !party[i]
  999.     end
  1000.   end
  1001.   def pbRemoveFromParty(battlerIndex,partyIndex)
  1002.     party=pbParty(battlerIndex)
  1003.     side=(pbIsOpposing?(battlerIndex)) ? @opponent : @player
  1004.     order=(pbIsOpposing?(battlerIndex)) ? @party2order : @party1order
  1005.     secondpartybegin=pbSecondPartyBegin(battlerIndex)
  1006.     party[partyIndex]=nil
  1007.     if !side || !side.is_a?(Array) # Wild or single opponent
  1008.       party.compact!
  1009.       for i in partyIndex...party.length+1
  1010.         for j in 0...4
  1011.           next if !@battlers[j]
  1012.           if pbGetOwner(j)==side && @battlers[j].pokemonIndex==i
  1013.             @battlers[j].pokemonIndex-=1
  1014.             break
  1015.           end
  1016.         end
  1017.       end
  1018.       for i in 0...order.length
  1019.         order[i]=(i==partyIndex) ? order.length-1 : order[i]-1
  1020.       end
  1021.     else
  1022.       if partyIndex<secondpartybegin-1
  1023.         for i in partyIndex...secondpartybegin
  1024.           if i>=secondpartybegin-1
  1025.             party[i]=nil
  1026.           else
  1027.             party[i]=party[i+1]
  1028.           end
  1029.         end
  1030.         for i in 0...order.length
  1031.           next if order[i]>=secondpartybegin
  1032.           order[i]=(i==partyIndex) ? secondpartybegin-1 : order[i]-1
  1033.         end
  1034.       else
  1035.         for i in partyIndex...secondpartybegin+pbPartyLength(battlerIndex)
  1036.           if i>=party.length-1
  1037.             party[i]=nil
  1038.           else
  1039.             party[i]=party[i+1]
  1040.           end
  1041.         end
  1042.         for i in 0...order.length
  1043.           next if order[i]<secondpartybegin
  1044.           order[i]=(i==partyIndex) ? secondpartybegin+pbPartyLength(battlerIndex)-1 : order[i]-1
  1045.         end
  1046.       end
  1047.     end
  1048.   end
  1049. ################################################################################
  1050. # Check whether actions can be taken.
  1051. ################################################################################
  1052.   def pbCanShowCommands?(idxPokemon)
  1053.     thispkmn=@battlers[idxPokemon]
  1054.     return false if thispkmn.fainted?
  1055.     return false if thispkmn.effects[PBEffects::TwoTurnAttack]>0
  1056.     return false if thispkmn.effects[PBEffects::HyperBeam]>0
  1057.     return false if thispkmn.effects[PBEffects::Rollout]>0
  1058.     return false if thispkmn.effects[PBEffects::Outrage]>0
  1059.     return false if thispkmn.effects[PBEffects::Uproar]>0
  1060.     return false if thispkmn.effects[PBEffects::Bide]>0
  1061.     return true
  1062.   end
  1063. ################################################################################
  1064. # Attacking.
  1065. ################################################################################
  1066.   def pbCanShowFightMenu?(idxPokemon)
  1067.     thispkmn=@battlers[idxPokemon]
  1068.     if !pbCanShowCommands?(idxPokemon)
  1069.       return false
  1070.     end
  1071.     # No moves that can be chosen
  1072.     if !pbCanChooseMove?(idxPokemon,0,false) &&
  1073.        !pbCanChooseMove?(idxPokemon,1,false) &&
  1074.        !pbCanChooseMove?(idxPokemon,2,false) &&
  1075.        !pbCanChooseMove?(idxPokemon,3,false)
  1076.       return false
  1077.     end
  1078.     # Encore
  1079.     return false if thispkmn.effects[PBEffects::Encore]>0
  1080.     return true
  1081.   end
  1082.   def pbCanChooseMove?(idxPokemon,idxMove,showMessages,sleeptalk=false)
  1083.     thispkmn=@battlers[idxPokemon]
  1084.     thismove=thispkmn.moves[idxMove]
  1085.     opp1=thispkmn.pbOpposing1
  1086.     opp2=thispkmn.pbOpposing2
  1087.     if !thismove || thismove.id==0
  1088.       return false
  1089.     end
  1090.     if thismove.pp<=0 && thismove.totalpp>0 && !sleeptalk
  1091.       if showMessages
  1092.         pbDisplayPaused(_INTL("There's no PP left for this move!"))
  1093.       end
  1094.       return false
  1095.     end
  1096.     if thispkmn.hasWorkingItem(:ASSAULTVEST) && thismove.pbIsStatus?
  1097.       if showMessages
  1098.         pbDisplayPaused(_INTL("The effects of the {1} prevent status moves from being used!",
  1099.            PBItems.getName(thispkmn.item)))
  1100.       end
  1101.       return false
  1102.     end
  1103.     if thispkmn.effects[PBEffects::ChoiceBand]>=0 &&
  1104.        (thispkmn.hasWorkingItem(:CHOICEBAND) ||
  1105.        thispkmn.hasWorkingItem(:CHOICESPECS) ||
  1106.        thispkmn.hasWorkingItem(:CHOICESCARF))
  1107.       hasmove=false
  1108.       for i in 0...4
  1109.         if thispkmn.moves[i].id==thispkmn.effects[PBEffects::ChoiceBand]
  1110.           hasmove=true; break
  1111.         end
  1112.       end
  1113.       if hasmove && thismove.id!=thispkmn.effects[PBEffects::ChoiceBand]
  1114.         if showMessages
  1115.           pbDisplayPaused(_INTL("{1} allows the use of only {2}!",
  1116.              PBItems.getName(thispkmn.item),
  1117.              PBMoves.getName(thispkmn.effects[PBEffects::ChoiceBand])))
  1118.         end
  1119.         return false
  1120.       end
  1121.     end
  1122.     if opp1.effects[PBEffects::Imprison]
  1123.       if thismove.id==opp1.moves[0].id ||
  1124.          thismove.id==opp1.moves[1].id ||
  1125.          thismove.id==opp1.moves[2].id ||
  1126.          thismove.id==opp1.moves[3].id
  1127.         if showMessages
  1128.           pbDisplayPaused(_INTL("{1} can't use the sealed {2}!",thispkmn.pbThis,thismove.name))
  1129.         end
  1130.         #PBDebug.log("[CanChoose][#{opp1.pbThis} has: #{opp1.moves[0].name}, #{opp1.moves[1].name},#{opp1.moves[2].name},#{opp1.moves[3].name}]")
  1131.         return false
  1132.       end
  1133.     end
  1134.     if opp2.effects[PBEffects::Imprison]
  1135.       if thismove.id==opp2.moves[0].id ||
  1136.          thismove.id==opp2.moves[1].id ||
  1137.          thismove.id==opp2.moves[2].id ||
  1138.          thismove.id==opp2.moves[3].id
  1139.         if showMessages
  1140.           pbDisplayPaused(_INTL("{1} can't use the sealed {2}!",thispkmn.pbThis,thismove.name))
  1141.         end
  1142.         #PBDebug.log("[CanChoose][#{opp2.pbThis} has: #{opp2.moves[0].name}, #{opp2.moves[1].name},#{opp2.moves[2].name},#{opp2.moves[3].name}]")
  1143.         return false
  1144.       end
  1145.     end
  1146.     if thispkmn.effects[PBEffects::Taunt]>0 && thismove.basedamage==0
  1147.       if showMessages
  1148.         pbDisplayPaused(_INTL("{1} can't use {2} after the taunt!",thispkmn.pbThis,thismove.name))
  1149.       end
  1150.       return false
  1151.     end
  1152.     if thispkmn.effects[PBEffects::Torment]
  1153.       if thismove.id==thispkmn.lastMoveUsed
  1154.         if showMessages
  1155.           pbDisplayPaused(_INTL("{1} can't use the same move twice in a row due to the torment!",thispkmn.pbThis))
  1156.         end
  1157.         return false
  1158.       end
  1159.     end
  1160.     if thismove.id==thispkmn.effects[PBEffects::DisableMove] && !sleeptalk
  1161.       if showMessages
  1162.         pbDisplayPaused(_INTL("{1}'s {2} is disabled!",thispkmn.pbThis,thismove.name))
  1163.       end
  1164.       return false
  1165.     end
  1166.     if thismove.function==0x158 && # Belch
  1167.        (!thispkmn.pokemon || !thispkmn.pokemon.belch)
  1168.       if showMessages
  1169.         pbDisplayPaused(_INTL("{1} hasn't eaten any held berry, so it can't possibly belch!",thispkmn.pbThis))
  1170.       end
  1171.       return false
  1172.     end
  1173.     if thispkmn.effects[PBEffects::Encore]>0 && idxMove!=thispkmn.effects[PBEffects::EncoreIndex]
  1174.       return false
  1175.     end
  1176.     return true
  1177.   end
  1178.   def pbAutoChooseMove(idxPokemon,showMessages=true)
  1179.     thispkmn=@battlers[idxPokemon]
  1180.     if thispkmn.fainted?
  1181.       @choices[idxPokemon][0]=0
  1182.       @choices[idxPokemon][1]=0
  1183.       @choices[idxPokemon][2]=nil
  1184.       return
  1185.     end
  1186.     if thispkmn.effects[PBEffects::Encore]>0 &&
  1187.        pbCanChooseMove?(idxPokemon,thispkmn.effects[PBEffects::EncoreIndex],false)
  1188.       PBDebug.log("[Auto choosing Encore move] #{thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]].name}")
  1189.       @choices[idxPokemon][0]=1    # "Use move"
  1190.       @choices[idxPokemon][1]=thispkmn.effects[PBEffects::EncoreIndex] # Index of move
  1191.       @choices[idxPokemon][2]=thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]]
  1192.       @choices[idxPokemon][3]=-1   # No target chosen yet
  1193.       if @doublebattle
  1194.         thismove=thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]]
  1195.         target=thispkmn.pbTarget(thismove)
  1196.         if target==PBTargets::SingleNonUser
  1197.           target=@scene.pbChooseTarget(idxPokemon,target)
  1198.           pbRegisterTarget(idxPokemon,target) if target>=0
  1199.         elsif target==PBTargets::UserOrPartner
  1200.           target=@scene.pbChooseTarget(idxPokemon,target)
  1201.           pbRegisterTarget(idxPokemon,target) if target>=0 && (target&1)==(idxPokemon&1)
  1202.         end
  1203.       end
  1204.     else
  1205.       if !pbIsOpposing?(idxPokemon)
  1206.         pbDisplayPaused(_INTL("{1} has no moves left!",thispkmn.name)) if showMessages
  1207.       end
  1208.       @choices[idxPokemon][0]=1           # "Use move"
  1209.       @choices[idxPokemon][1]=-1          # Index of move to be used
  1210.       @choices[idxPokemon][2]=@struggle   # Use Struggle
  1211.       @choices[idxPokemon][3]=-1          # No target chosen yet
  1212.     end
  1213.   end
  1214.   def pbRegisterMove(idxPokemon,idxMove,showMessages=true)
  1215.     thispkmn=@battlers[idxPokemon]
  1216.     thismove=thispkmn.moves[idxMove]
  1217.     return false if !pbCanChooseMove?(idxPokemon,idxMove,showMessages)
  1218.     @choices[idxPokemon][0]=1         # "Use move"
  1219.     @choices[idxPokemon][1]=idxMove   # Index of move to be used
  1220.     @choices[idxPokemon][2]=thismove  # PokeBattle_Move object of the move
  1221.     @choices[idxPokemon][3]=-1        # No target chosen yet
  1222.     return true
  1223.   end
  1224.   def pbChoseMove?(i,move)
  1225.     return false if @battlers[i].fainted?
  1226.     if @choices[i][0]==1 && @choices[i][1]>=0
  1227.       choice=@choices[i][1]
  1228.       return isConst?(@battlers[i].moves[choice].id,PBMoves,move)
  1229.     end
  1230.     return false
  1231.   end
  1232.   def pbChoseMoveFunctionCode?(i,code)
  1233.     return false if @battlers[i].fainted?
  1234.     if @choices[i][0]==1 && @choices[i][1]>=0
  1235.       choice=@choices[i][1]
  1236.       return @battlers[i].moves[choice].function==code
  1237.     end
  1238.     return false
  1239.   end
  1240.   def pbRegisterTarget(idxPokemon,idxTarget)
  1241.     @choices[idxPokemon][3]=idxTarget   # Set target of move
  1242.     return true
  1243.   end
  1244.   def pbPriority(ignorequickclaw=false,log=false)
  1245.     return @priority if @usepriority # use stored priority if round isn't over yet
  1246.     @priority.clear
  1247.     speeds=[]
  1248.     priorities=[]
  1249.     quickclaw=[]; lagging=[]
  1250.     minpri=0; maxpri=0
  1251.     temp=[]
  1252.     # Calculate each Pokémon's speed
  1253.     for i in 0...4
  1254.       speeds[i]=@battlers[i].pbSpeed
  1255.       quickclaw[i]=false
  1256.       lagging[i]=false
  1257.       if !ignorequickclaw && @choices[i][0]==1 # Chose to use a move
  1258.         if !quickclaw[i] && @battlers[i].hasWorkingItem(:CUSTAPBERRY) &&
  1259.            !@battlers[i].pbOpposing1.hasWorkingAbility(:UNNERVE) &&
  1260.            !@battlers[i].pbOpposing2.hasWorkingAbility(:UNNERVE)
  1261.           if (@battlers[i].hasWorkingAbility(:GLUTTONY) && @battlers[i].hp<=(@battlers[i].totalhp/2).floor) ||
  1262.              @battlers[i].hp<=(@battlers[i].totalhp/4).floor
  1263.             pbCommonAnimation("UseItem",@battlers[i],nil)
  1264.             quickclaw[i]=true
  1265.             pbDisplayBrief(_INTL("{1}'s {2} let it move first!",
  1266.                @battlers[i].pbThis,PBItems.getName(@battlers[i].item)))
  1267.             @battlers[i].pbConsumeItem
  1268.           end
  1269.         end
  1270.         if !quickclaw[i] && @battlers[i].hasWorkingItem(:QUICKCLAW)
  1271.           if pbRandom(10)<2
  1272.             pbCommonAnimation("UseItem",@battlers[i],nil)
  1273.             quickclaw[i]=true
  1274.             pbDisplayBrief(_INTL("{1}'s {2} let it move first!",
  1275.                @battlers[i].pbThis,PBItems.getName(@battlers[i].item)))
  1276.           end
  1277.         end
  1278.         if !quickclaw[i] &&
  1279.            (@battlers[i].hasWorkingAbility(:STALL) ||
  1280.            @battlers[i].hasWorkingItem(:LAGGINGTAIL) ||
  1281.            @battlers[i].hasWorkingItem(:FULLINCENSE))
  1282.           lagging[i]=true
  1283.         end
  1284.       end
  1285.     end
  1286.     # Calculate each Pokémon's priority bracket, and get the min/max priorities
  1287.     for i in 0...4
  1288.       # Assume that doing something other than using a move is priority 0
  1289.       pri=0
  1290.       if @choices[i][0]==1 # Chose to use a move
  1291.         pri=@choices[i][2].priority
  1292.         pri+=1 if @battlers[i].hasWorkingAbility(:PRANKSTER) &&
  1293.                   @choices[i][2].pbIsStatus?
  1294.         pri+=1 if @battlers[i].hasWorkingAbility(:GALEWINGS) &&
  1295.                   isConst?(@choices[i][2].type,PBTypes,:FLYING)
  1296.       end
  1297.       priorities[i]=pri
  1298.       if i==0
  1299.         minpri=pri
  1300.         maxpri=pri
  1301.       else
  1302.         minpri=pri if minpri>pri
  1303.         maxpri=pri if maxpri<pri
  1304.       end
  1305.     end
  1306.     # Find and order all moves with the same priority
  1307.     curpri=maxpri
  1308.     loop do
  1309.       temp.clear
  1310.       for j in 0...4
  1311.         temp.push(j) if priorities[j]==curpri
  1312.       end
  1313.       # Sort by speed
  1314.       if temp.length==1
  1315.         @priority[@priority.length]=@battlers[temp[0]]
  1316.       elsif temp.length>1
  1317.         n=temp.length
  1318.         for m in 0...temp.length-1
  1319.           for i in 1...temp.length
  1320.             # For each pair of battlers, rank the second compared to the first
  1321.             # -1 means rank higher, 0 means rank equal, 1 means rank lower
  1322.             cmp=0
  1323.             if quickclaw[temp[i]]
  1324.               cmp=-1
  1325.               if quickclaw[temp[i-1]]
  1326.                 if speeds[temp[i]]==speeds[temp[i-1]]
  1327.                   cmp=0
  1328.                 else
  1329.                   cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? -1 : 1
  1330.                 end
  1331.               end
  1332.             elsif quickclaw[temp[i-1]]
  1333.               cmp=1
  1334.             elsif lagging[temp[i]]
  1335.               cmp=1
  1336.               if lagging[temp[i-1]]
  1337.                 if speeds[temp[i]]==speeds[temp[i-1]]
  1338.                   cmp=0
  1339.                 else
  1340.                   cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? 1 : -1
  1341.                 end
  1342.               end
  1343.             elsif lagging[temp[i-1]]
  1344.               cmp=-1
  1345.             elsif speeds[temp[i]]!=speeds[temp[i-1]]
  1346.               if @field.effects[PBEffects::TrickRoom]>0
  1347.                 cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? 1 : -1
  1348.               else
  1349.                 cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? -1 : 1
  1350.               end
  1351.             end
  1352.             if cmp<0 || # Swap the pair according to the second battler's rank
  1353.                (cmp==0 && pbRandom(2)==0)
  1354.               swaptmp=temp[i]
  1355.               temp[i]=temp[i-1]
  1356.               temp[i-1]=swaptmp
  1357.             end
  1358.           end
  1359.         end
  1360.         # Battlers in this bracket are properly sorted, so add them to @priority
  1361.         for i in temp
  1362.           @priority[@priority.length]=@battlers[i]
  1363.         end
  1364.       end
  1365.       curpri-=1
  1366.       break if curpri<minpri
  1367.     end
  1368.     # Write the priority order to the debug log
  1369.     if log
  1370.       d="[Priority] "; comma=false
  1371.       for i in 0...4
  1372.         if @priority[i] && !@priority[i].fainted?
  1373.           d+=", " if comma
  1374.           d+="#{@priority[i].pbThis(comma)} (#{@priority[i].index})"; comma=true
  1375.         end
  1376.       end
  1377.       PBDebug.log(d)
  1378.     end
  1379.     @usepriority=true
  1380.     return @priority
  1381.   end
  1382. ################################################################################
  1383. # Switching Pokémon.
  1384. ################################################################################
  1385.   def pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
  1386.     if pkmnidxTo>=0
  1387.       party=pbParty(idxPokemon)
  1388.       return false if pkmnidxTo>=party.length
  1389.       return false if !party[pkmnidxTo]
  1390.       if party[pkmnidxTo].egg?
  1391.         pbDisplayPaused(_INTL("An Egg can't battle!")) if showMessages
  1392.         return false
  1393.       end
  1394.       if !pbIsOwner?(idxPokemon,pkmnidxTo)
  1395.         owner=pbPartyGetOwner(idxPokemon,pkmnidxTo)
  1396.         pbDisplayPaused(_INTL("You can't switch {1}'s Pokémon with one of yours!",owner.name)) if showMessages
  1397.         return false
  1398.       end
  1399.       if party[pkmnidxTo].hp<=0
  1400.         pbDisplayPaused(_INTL("{1} has no energy left to battle!",party[pkmnidxTo].name)) if showMessages
  1401.         return false
  1402.       end
  1403.       if @battlers[idxPokemon].pokemonIndex==pkmnidxTo ||
  1404.          @battlers[idxPokemon].pbPartner.pokemonIndex==pkmnidxTo
  1405.         pbDisplayPaused(_INTL("{1} is already in battle!",party[pkmnidxTo].name)) if showMessages
  1406.         return false
  1407.       end
  1408.     end
  1409.     return true
  1410.   end
  1411.   def pbCanSwitch?(idxPokemon,pkmnidxTo,showMessages,ignoremeanlook=false)
  1412.     thispkmn=@battlers[idxPokemon]
  1413.     # Multi-Turn Attacks/Mean Look
  1414.     return false if !pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
  1415.     isOpposing=pbIsOpposing?(idxPokemon)
  1416.     party=pbParty(idxPokemon)
  1417.     for i in 0...4
  1418.       next if isOpposing!=pbIsOpposing?(i)
  1419.       if choices[i][0]==2 && choices[i][1]==pkmnidxTo
  1420.         pbDisplayPaused(_INTL("{1} has already been selected.",party[pkmnidxTo].name)) if showMessages
  1421.         return false
  1422.       end
  1423.     end
  1424.     return true if thispkmn.hasWorkingItem(:SHEDSHELL)
  1425.     return true if USENEWBATTLEMECHANICS && thispkmn.pbHasType?(:GHOST)
  1426.     if thispkmn.effects[PBEffects::MultiTurn]>0 ||
  1427.        (!ignoremeanlook && thispkmn.effects[PBEffects::MeanLook]>=0)
  1428.       pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1429.       return false
  1430.     end
  1431.     if @field.effects[PBEffects::FairyLock]>0
  1432.       pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1433.       return false
  1434.     end
  1435.     if thispkmn.effects[PBEffects::Ingrain]
  1436.       pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1437.       return false
  1438.     end
  1439.     opp1=thispkmn.pbOpposing1
  1440.     opp2=thispkmn.pbOpposing2
  1441.     opp=nil
  1442.     if thispkmn.pbHasType?(:STEEL)
  1443.       opp=opp1 if opp1.hasWorkingAbility(:MAGNETPULL)
  1444.       opp=opp2 if opp2.hasWorkingAbility(:MAGNETPULL)
  1445.     end
  1446.     if !thispkmn.isAirborne?
  1447.       opp=opp1 if opp1.hasWorkingAbility(:ARENATRAP)
  1448.       opp=opp2 if opp2.hasWorkingAbility(:ARENATRAP)
  1449.     end
  1450.     if !thispkmn.hasWorkingAbility(:SHADOWTAG)
  1451.       opp=opp1 if opp1.hasWorkingAbility(:SHADOWTAG)
  1452.       opp=opp2 if opp2.hasWorkingAbility(:SHADOWTAG)
  1453.     end
  1454.     if opp
  1455.       abilityname=PBAbilities.getName(opp.ability)
  1456.       pbDisplayPaused(_INTL("{1}'s {2} prevents switching!",opp.pbThis,abilityname)) if showMessages
  1457.       return false
  1458.     end
  1459.     return true
  1460.   end
  1461.   def pbRegisterSwitch(idxPokemon,idxOther)
  1462.     return false if !pbCanSwitch?(idxPokemon,idxOther,false)
  1463.     @choices[idxPokemon][0]=2          # "Switch Pokémon"
  1464.     @choices[idxPokemon][1]=idxOther   # Index of other Pokémon to switch with
  1465.     @choices[idxPokemon][2]=nil
  1466.     side=(pbIsOpposing?(idxPokemon)) ? 1 : 0
  1467.     owner=pbGetOwnerIndex(idxPokemon)
  1468.     if @megaEvolution[side][owner]==idxPokemon
  1469.       @megaEvolution[side][owner]=-1
  1470.     end
  1471.     return true
  1472.   end
  1473.   def pbCanChooseNonActive?(index)
  1474.     party=pbParty(index)
  1475.     for i in 0...party.length
  1476.       return true if pbCanSwitchLax?(index,i,false)
  1477.     end
  1478.     return false
  1479.   end
  1480.   def pbSwitch(favorDraws=false)
  1481.     if !favorDraws
  1482.       return if @decision>0
  1483.     else
  1484.       return if @decision==5
  1485.     end
  1486.     pbJudge()
  1487.     return if @decision>0
  1488.     firstbattlerhp=@battlers[0].hp
  1489.     switched=[]
  1490.     for index in 0...4
  1491.       next if !@doublebattle && pbIsDoubleBattler?(index)
  1492.       next if @battlers[index] && !@battlers[index].fainted?
  1493.       next if !pbCanChooseNonActive?(index)
  1494.       if !pbOwnedByPlayer?(index)
  1495.         if !pbIsOpposing?(index) || (@opponent && pbIsOpposing?(index))
  1496.           newenemy=pbSwitchInBetween(index,false,false)
  1497.           newenemyname=newenemy
  1498.           if newenemy>=0 && isConst?(pbParty(index)[newenemy].ability,PBAbilities,:ILLUSION)
  1499.             newenemyname=pbGetLastPokeInTeam(index)
  1500.           end
  1501.           opponent=pbGetOwner(index)
  1502.           if !@doublebattle && firstbattlerhp>0 && @shiftStyle && @opponent &&
  1503.               @internalbattle && pbCanChooseNonActive?(0) && pbIsOpposing?(index) &&
  1504.               @battlers[0].effects[PBEffects::Outrage]==0
  1505.             pbDisplayPaused(_INTL("{1} is about to send in {2}.",opponent.fullname,pbParty(index)[newenemyname].name))
  1506.             if pbDisplayConfirm(_INTL("Will {1} change Pokémon?",self.pbPlayer.name))
  1507.               newpoke=pbSwitchPlayer(0,true,true)
  1508.               if newpoke>=0
  1509.                 newpokename=newpoke
  1510.                 if isConst?(@party1[newpoke].ability,PBAbilities,:ILLUSION)
  1511.                   newpokename=pbGetLastPokeInTeam(0)
  1512.                 end
  1513.                 pbDisplayBrief(_INTL("{1}, that's enough! Come back!",@battlers[0].name))
  1514.                 pbRecallAndReplace(0,newpoke,newpokename)
  1515.                 switched.push(0)
  1516.               end
  1517.             end
  1518.           end
  1519.           pbRecallAndReplace(index,newenemy,newenemyname,false,false)
  1520.           switched.push(index)
  1521.         end
  1522.       elsif @opponent
  1523.         newpoke=pbSwitchInBetween(index,true,false)
  1524.         newpokename=newpoke
  1525.         if isConst?(@party1[newpoke].ability,PBAbilities,:ILLUSION)
  1526.           newpokename=pbGetLastPokeInTeam(index)
  1527.         end
  1528.         pbRecallAndReplace(index,newpoke,newpokename)
  1529.         switched.push(index)
  1530.       else
  1531.         switch=false
  1532.         if !pbDisplayConfirm(_INTL("Use next Pokémon?"))
  1533.           switch=(pbRun(index,true)<=0)
  1534.         else
  1535.           switch=true
  1536.         end
  1537.         if switch
  1538.           newpoke=pbSwitchInBetween(index,true,false)
  1539.           newpokename=newpoke
  1540.           if isConst?(@party1[newpoke].ability,PBAbilities,:ILLUSION)
  1541.             newpokename=pbGetLastPokeInTeam(index)
  1542.           end
  1543.           pbRecallAndReplace(index,newpoke,newpokename)
  1544.           switched.push(index)
  1545.         end
  1546.       end
  1547.     end
  1548.     if switched.length>0
  1549.       priority=pbPriority
  1550.       for i in priority
  1551.         i.pbAbilitiesOnSwitchIn(true) if switched.include?(i.index)
  1552.       end
  1553.     end
  1554.   end
  1555.   def pbSendOut(index,pokemon)
  1556.     pbSetSeen(pokemon)
  1557.     @peer.pbOnEnteringBattle(self,pokemon)
  1558.     if pbIsOpposing?(index)
  1559.       @scene.pbTrainerSendOut(index,pokemon)
  1560.     else
  1561.       @scene.pbSendOut(index,pokemon)
  1562.     end
  1563.     @scene.pbResetMoveIndex(index)
  1564.   end
  1565.   def pbReplace(index,newpoke,batonpass=false)
  1566.     party=pbParty(index)
  1567.     oldpoke=@battlers[index].pokemonIndex
  1568.     # Initialise the new Pokémon
  1569.     @battlers[index].pbInitialize(party[newpoke],newpoke,batonpass)
  1570.     # Reorder the party for this battle
  1571.     partyorder=(!pbIsOpposing?(index)) ? @party1order : @party2order
  1572.     bpo=-1; bpn=-1
  1573.     for i in 0...partyorder.length
  1574.       bpo=i if partyorder[i]==oldpoke
  1575.       bpn=i if partyorder[i]==newpoke
  1576.     end
  1577.     p=partyorder[bpo]; partyorder[bpo]=partyorder[bpn]; partyorder[bpn]=p
  1578.     # Send out the new Pokémon
  1579.     pbSendOut(index,party[newpoke])
  1580.     pbSetSeen(party[newpoke])
  1581.   end
  1582.   def pbRecallAndReplace(index,newpoke,newpokename=-1,batonpass=false,moldbreaker=false)
  1583.     @battlers[index].pbResetForm
  1584.     if !@battlers[index].fainted?
  1585.       @scene.pbRecall(index)
  1586.     end
  1587.     pbMessagesOnReplace(index,newpoke,newpokename)
  1588.     pbReplace(index,newpoke,batonpass)
  1589.     return pbOnActiveOne(@battlers[index],false,moldbreaker)
  1590.   end
  1591.   def pbMessagesOnReplace(index,newpoke,newpokename=-1)
  1592.     newpokename=newpoke if newpokename<0
  1593.     party=pbParty(index)
  1594.     if pbOwnedByPlayer?(index)
  1595. #     if !party[newpoke]
  1596. #       p [index,newpoke,party[newpoke],pbAllFainted?(party)]
  1597. #       PBDebug.log([index,newpoke,party[newpoke],"pbMOR"].inspect)
  1598. #       for i in 0...party.length
  1599. #         PBDebug.log([i,party[i].hp].inspect)
  1600. #       end
  1601. #       raise BattleAbortedException.new
  1602. #     end
  1603.       opposing=@battlers[index].pbOppositeOpposing
  1604.       if opposing.fainted? || opposing.hp==opposing.totalhp
  1605.         pbDisplayBrief(_INTL("Go! {1}!",party[newpokename].name))
  1606.       elsif opposing.hp>=(opposing.totalhp/2)
  1607.         pbDisplayBrief(_INTL("Do it! {1}!",party[newpokename].name))
  1608.       elsif opposing.hp>=(opposing.totalhp/4)
  1609.         pbDisplayBrief(_INTL("Go for it, {1}!",party[newpokename].name))
  1610.       else
  1611.         pbDisplayBrief(_INTL("Your opponent's weak!\nGet 'em, {1}!",party[newpokename].name))
  1612.       end
  1613.       PBDebug.log("[Send out Pokémon] Player sent out #{party[newpokename].name} in position #{index}")
  1614.     else
  1615. #     if !party[newpoke]
  1616. #       p [index,newpoke,party[newpoke],pbAllFainted?(party)]
  1617. #       PBDebug.log([index,newpoke,party[newpoke],"pbMOR"].inspect)
  1618. #       for i in 0...party.length
  1619. #         PBDebug.log([i,party[i].hp].inspect)
  1620. #       end
  1621. #       raise BattleAbortedException.new
  1622. #     end
  1623.       owner=pbGetOwner(index)
  1624.       pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",owner.fullname,party[newpokename].name))
  1625.       PBDebug.log("[Send out Pokémon] Opponent sent out #{party[newpokename].name} in position #{index}")
  1626.     end
  1627.   end
  1628.   def pbSwitchInBetween(index,lax,cancancel)
  1629.     if !pbOwnedByPlayer?(index)
  1630.       return @scene.pbChooseNewEnemy(index,pbParty(index))
  1631.     else
  1632.       return pbSwitchPlayer(index,lax,cancancel)
  1633.     end
  1634.   end
  1635.   def pbSwitchPlayer(index,lax,cancancel)
  1636.     if @debug
  1637.       return @scene.pbChooseNewEnemy(index,pbParty(index))
  1638.     else
  1639.       return @scene.pbSwitch(index,lax,cancancel)
  1640.     end
  1641.   end
  1642. ################################################################################
  1643. # Using an item.
  1644. ################################################################################
  1645. # Uses an item on a Pokémon in the player's party.
  1646.   def pbUseItemOnPokemon(item,pkmnIndex,userPkmn,scene)
  1647.     pokemon=@party1[pkmnIndex]
  1648.     battler=nil
  1649.     name=pbGetOwner(userPkmn.index).fullname
  1650.     name=pbGetOwner(userPkmn.index).name if pbBelongsToPlayer?(userPkmn.index)
  1651.     pbDisplayBrief(_INTL("{1} used the\r\n{2}.",name,PBItems.getName(item)))
  1652.     PBDebug.log("[Use item] Player used #{PBItems.getName(item)} on #{pokemon.name}")
  1653.     ret=false
  1654.     if pokemon.egg?
  1655.       pbDisplay(_INTL("But it had no effect!"))
  1656.     else
  1657.       for i in 0...4
  1658.         if !pbIsOpposing?(i) && @battlers[i].pokemonIndex==pkmnIndex
  1659.           battler=@battlers[i]
  1660.         end
  1661.       end
  1662.       ret=ItemHandlers.triggerBattleUseOnPokemon(item,pokemon,battler,scene)
  1663.     end
  1664.     if !ret && pbBelongsToPlayer?(userPkmn.index)
  1665.       if $PokemonBag.pbCanStore?(item)
  1666.         $PokemonBag.pbStoreItem(item)
  1667.       else
  1668.         raise _INTL("Couldn't return unused item to Bag somehow.")
  1669.       end
  1670.     end
  1671.     return ret
  1672.   end
  1673. # Uses an item on an active Pokémon.
  1674.   def pbUseItemOnBattler(item,index,userPkmn,scene)
  1675.     PBDebug.log("[Use item] Player used #{PBItems.getName(item)} on #{@battlers[index].pbThis(true)}")
  1676.     ret=ItemHandlers.triggerBattleUseOnBattler(item,@battlers[index],scene)
  1677.     if !ret && pbBelongsToPlayer?(userPkmn.index)
  1678.       if $PokemonBag.pbCanStore?(item)
  1679.         $PokemonBag.pbStoreItem(item)
  1680.       else
  1681.         raise _INTL("Couldn't return unused item to Bag somehow.")
  1682.       end
  1683.     end
  1684.     return ret
  1685.   end
  1686.   def pbRegisterItem(idxPokemon,idxItem,idxTarget=nil)
  1687.     if idxTarget!=nil && idxTarget>=0
  1688.       for i in 0...4
  1689.         if !@battlers[i].pbIsOpposing?(idxPokemon) &&
  1690.            @battlers[i].pokemonIndex==idxTarget &&
  1691.            @battlers[i].effects[PBEffects::Embargo]>0
  1692.           pbDisplay(_INTL("Embargo's effect prevents the item's use on {1}!",@battlers[i].pbThis(true)))
  1693.           if pbBelongsToPlayer?(@battlers[i].index)
  1694.             if $PokemonBag.pbCanStore?(idxItem)
  1695.               $PokemonBag.pbStoreItem(idxItem)
  1696.             else
  1697.               raise _INTL("Couldn't return unused item to Bag somehow.")
  1698.             end
  1699.           end
  1700.           return false
  1701.         end
  1702.       end
  1703.     end
  1704.     if ItemHandlers.hasUseInBattle(idxItem)
  1705.       if idxPokemon==0 # Player's first Pokémon
  1706.         if ItemHandlers.triggerBattleUseOnBattler(idxItem,@battlers[idxPokemon],self)
  1707.           # Using Poké Balls or Poké Doll only
  1708.           ItemHandlers.triggerUseInBattle(idxItem,@battlers[idxPokemon],self)
  1709.           if @doublebattle
  1710.             @battlers[idxPokemon].pbPartner.effects[PBEffects::SkipTurn]=true
  1711.           end
  1712.         else
  1713.           if $PokemonBag.pbCanStore?(idxItem)
  1714.             $PokemonBag.pbStoreItem(idxItem)
  1715.           else
  1716.             raise _INTL("Couldn't return unusable item to Bag somehow.")
  1717.           end
  1718.           return false
  1719.         end
  1720.       else
  1721.         if ItemHandlers.triggerBattleUseOnBattler(idxItem,@battlers[idxPokemon],self)
  1722.           pbDisplay(_INTL("It's impossible to aim without being focused!"))
  1723.         end
  1724.         return false
  1725.       end
  1726.     end
  1727.     @choices[idxPokemon][0]=3         # "Use an item"
  1728.     @choices[idxPokemon][1]=idxItem   # ID of item to be used
  1729.     @choices[idxPokemon][2]=idxTarget # Index of Pokémon to use item on
  1730.     side=(pbIsOpposing?(idxPokemon)) ? 1 : 0
  1731.     owner=pbGetOwnerIndex(idxPokemon)
  1732.     if @megaEvolution[side][owner]==idxPokemon
  1733.       @megaEvolution[side][owner]=-1
  1734.     end
  1735.     return true
  1736.   end
  1737.   def pbEnemyUseItem(item,battler)
  1738.     return 0 if !@internalbattle
  1739.     items=pbGetOwnerItems(battler.index)
  1740.     return if !items
  1741.     opponent=pbGetOwner(battler.index)
  1742.     for i in 0...items.length
  1743.       if items[i]==item
  1744.         items.delete_at(i)
  1745.         break
  1746.       end
  1747.     end
  1748.     itemname=PBItems.getName(item)
  1749.     pbDisplayBrief(_INTL("{1} used the\r\n{2}!",opponent.fullname,itemname))
  1750.     PBDebug.log("[Use item] Opponent used #{itemname} on #{battler.pbThis(true)}")
  1751.     if isConst?(item,PBItems,:POTION)
  1752.       battler.pbRecoverHP(20,true)
  1753.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1754.     elsif isConst?(item,PBItems,:SUPERPOTION)
  1755.       battler.pbRecoverHP(50,true)
  1756.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1757.     elsif isConst?(item,PBItems,:HYPERPOTION)
  1758.       battler.pbRecoverHP(200,true)
  1759.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1760.     elsif isConst?(item,PBItems,:MAXPOTION)
  1761.       battler.pbRecoverHP(battler.totalhp-battler.hp,true)
  1762.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1763.     elsif isConst?(item,PBItems,:FULLRESTORE)
  1764.       fullhp=(battler.hp==battler.totalhp)
  1765.       battler.pbRecoverHP(battler.totalhp-battler.hp,true)
  1766.       battler.status=0; battler.statusCount=0
  1767.       battler.effects[PBEffects::Confusion]=0
  1768.       if fullhp
  1769.         pbDisplay(_INTL("{1} became healthy!",battler.pbThis))
  1770.       else
  1771.         pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1772.       end
  1773.     elsif isConst?(item,PBItems,:FULLHEAL)
  1774.       battler.status=0; battler.statusCount=0
  1775.       battler.effects[PBEffects::Confusion]=0
  1776.       pbDisplay(_INTL("{1} became healthy!",battler.pbThis))
  1777.     elsif isConst?(item,PBItems,:XATTACK)
  1778.       if battler.pbCanIncreaseStatStage?(PBStats::ATTACK,battler)
  1779.         battler.pbIncreaseStat(PBStats::ATTACK,1,battler,true)
  1780.       end
  1781.     elsif isConst?(item,PBItems,:XDEFEND)
  1782.       if battler.pbCanIncreaseStatStage?(PBStats::DEFENSE,battler)
  1783.         battler.pbIncreaseStat(PBStats::DEFENSE,1,battler,true)
  1784.       end
  1785.     elsif isConst?(item,PBItems,:XSPEED)
  1786.       if battler.pbCanIncreaseStatStage?(PBStats::SPEED,battler)
  1787.         battler.pbIncreaseStat(PBStats::SPEED,1,battler,true)
  1788.       end
  1789.     elsif isConst?(item,PBItems,:XSPECIAL)
  1790.       if battler.pbCanIncreaseStatStage?(PBStats::SPATK,battler)
  1791.         battler.pbIncreaseStat(PBStats::SPATK,1,battler,true)
  1792.       end
  1793.     elsif isConst?(item,PBItems,:XSPDEF)
  1794.       if battler.pbCanIncreaseStatStage?(PBStats::SPDEF,battler)
  1795.         battler.pbIncreaseStat(PBStats::SPDEF,1,battler,true)
  1796.       end
  1797.     elsif isConst?(item,PBItems,:XACCURACY)
  1798.       if battler.pbCanIncreaseStatStage?(PBStats::ACCURACY,battler)
  1799.         battler.pbIncreaseStat(PBStats::ACCURACY,1,battler,true)
  1800.       end
  1801.     end
  1802.   end
  1803. ################################################################################
  1804. # Fleeing from battle.
  1805. ################################################################################
  1806.   def pbCanRun?(idxPokemon)
  1807.     return false if @opponent
  1808.     return false if @cantescape && !pbIsOpposing?(idxPokemon)
  1809.     thispkmn=@battlers[idxPokemon]
  1810.     return true if thispkmn.pbHasType?(:GHOST) && USENEWBATTLEMECHANICS
  1811.     return true if thispkmn.hasWorkingItem(:SMOKEBALL)
  1812.     return true if thispkmn.hasWorkingAbility(:RUNAWAY)
  1813.     return pbCanSwitch?(idxPokemon,-1,false)
  1814.   end
  1815.   def pbRun(idxPokemon,duringBattle=false)
  1816.     thispkmn=@battlers[idxPokemon]
  1817.     if pbIsOpposing?(idxPokemon)
  1818.       return 0 if @opponent
  1819.       @choices[i][0]=5 # run
  1820.       @choices[i][1]=0
  1821.       @choices[i][2]=nil
  1822.       return -1
  1823.     end
  1824.     if @opponent
  1825.       if $DEBUG && Input.press?(Input::CTRL)
  1826.         if pbDisplayConfirm(_INTL("Treat this battle as a win?"))
  1827.           @decision=1
  1828.           return 1
  1829.         elsif pbDisplayConfirm(_INTL("Treat this battle as a loss?"))
  1830.           @decision=2
  1831.           return 1
  1832.         end
  1833.       elsif @internalbattle
  1834.         pbDisplayPaused(_INTL("No! There's no running from a Trainer battle!"))
  1835.       elsif pbDisplayConfirm(_INTL("Would you like to forfeit the match and quit now?"))
  1836.         pbSEPlay("Battle flee")
  1837.         pbDisplay(_INTL("{1} forfeited the match!",self.pbPlayer.name))
  1838.         @decision=3
  1839.         return 1
  1840.       end
  1841.       return 0
  1842.     end
  1843.     if $DEBUG && Input.press?(Input::CTRL)
  1844.       pbSEPlay("Battle flee")
  1845.       pbDisplayPaused(_INTL("Got away safely!"))
  1846.       @decision=3
  1847.       return 1
  1848.     end
  1849.     if @cantescape
  1850.       pbDisplayPaused(_INTL("Can't escape!"))
  1851.       return 0
  1852.     end
  1853.     if thispkmn.pbHasType?(:GHOST) && USENEWBATTLEMECHANICS
  1854.       pbSEPlay("Battle flee")
  1855.       pbDisplayPaused(_INTL("Got away safely!"))
  1856.       @decision=3
  1857.       return 1
  1858.     end
  1859.     if thispkmn.hasWorkingAbility(:RUNAWAY)
  1860.       pbSEPlay("Battle flee")
  1861.       if duringBattle
  1862.         pbDisplayPaused(_INTL("Got away safely!"))
  1863.       else
  1864.         pbDisplayPaused(_INTL("{1} escaped using Run Away!",thispkmn.pbThis))
  1865.       end
  1866.       @decision=3
  1867.       return 1
  1868.     end
  1869.     if thispkmn.hasWorkingItem(:SMOKEBALL)
  1870.       pbSEPlay("Battle flee")
  1871.       if duringBattle
  1872.         pbDisplayPaused(_INTL("Got away safely!"))
  1873.       else
  1874.         pbDisplayPaused(_INTL("{1} escaped using its {2}!",thispkmn.pbThis,PBItems.getName(thispkmn.item)))
  1875.       end
  1876.       @decision=3
  1877.       return 1
  1878.     end
  1879.     if !duringBattle && !pbCanSwitch?(idxPokemon,-1,false)
  1880.       pbDisplayPaused(_INTL("Can't escape!"))
  1881.       return 0
  1882.     end
  1883.     # Note: not pbSpeed, because using unmodified Speed
  1884.     speedPlayer=@battlers[idxPokemon].speed
  1885.     opposing=@battlers[idxPokemon].pbOppositeOpposing
  1886.     opposing=opposing.pbPartner if opposing.fainted?
  1887.     if !opposing.fainted?
  1888.       speedEnemy=opposing.speed
  1889.       if speedPlayer>speedEnemy
  1890.         rate=256
  1891.       else
  1892.         speedEnemy=1 if speedEnemy<=0
  1893.         rate=speedPlayer*128/speedEnemy
  1894.         rate+=@runCommand*30
  1895.         rate&=0xFF
  1896.       end
  1897.     else
  1898.       rate=256
  1899.     end
  1900.     ret=1
  1901.     if pbAIRandom(256)<rate
  1902.       pbSEPlay("Battle flee")
  1903.       pbDisplayPaused(_INTL("Got away safely!"))
  1904.       @decision=3
  1905.     else
  1906.       pbDisplayPaused(_INTL("Can't escape!"))
  1907.       ret=-1
  1908.     end
  1909.     @runCommand+=1 if !duringBattle
  1910.     return ret
  1911.   end
  1912. ################################################################################
  1913. # Mega Evolve battler.
  1914. ################################################################################
  1915.   def pbCanMegaEvolve?(index)
  1916.     return false if $game_switches[NO_MEGA_EVOLUTION]
  1917.     return false if !@battlers[index].hasMega?
  1918.     return false if pbIsOpposing?(index) && !@opponent
  1919.     return true if $DEBUG && Input.press?(Input::CTRL)
  1920.     return false if !pbHasMegaRing?(index)
  1921.     side=(pbIsOpposing?(index)) ? 1 : 0
  1922.     owner=pbGetOwnerIndex(index)
  1923.     return false if @megaEvolution[side][owner]!=-1
  1924.     return false if @battlers[index].effects[PBEffects::SkyDrop]
  1925.     return true
  1926.   end
  1927.   def pbRegisterMegaEvolution(index)
  1928.     side=(pbIsOpposing?(index)) ? 1 : 0
  1929.     owner=pbGetOwnerIndex(index)
  1930.     @megaEvolution[side][owner]=index
  1931.   end
  1932.   def pbMegaEvolve(index)
  1933.     return if !@battlers[index] || !@battlers[index].pokemon
  1934.     return if !(@battlers[index].hasMega? rescue false)
  1935.     return if (@battlers[index].isMega? rescue true)
  1936.     ownername=pbGetOwner(index).fullname
  1937.     ownername=pbGetOwner(index).name if pbBelongsToPlayer?(index)
  1938.     case (@battlers[index].pokemon.megaMessage rescue 0)
  1939.     when 1 # Rayquaza
  1940.       pbDisplay(_INTL("{1}'s fervent wish has reached {2}!",ownername,@battlers[index].pbThis))
  1941.     else
  1942.       pbDisplay(_INTL("{1}'s {2} is reacting to {3}'s {4}!",
  1943.          @battlers[index].pbThis,PBItems.getName(@battlers[index].item),
  1944.          ownername,pbGetMegaRingName(index)))
  1945.     end
  1946.     pbCommonAnimation("MegaEvolution",@battlers[index],nil)
  1947.     @battlers[index].pokemon.makeMega
  1948.     @battlers[index].form=@battlers[index].pokemon.form
  1949.     @battlers[index].pbUpdate(true)
  1950.     @scene.pbChangePokemon(@battlers[index],@battlers[index].pokemon)
  1951.     pbCommonAnimation("MegaEvolution2",@battlers[index],nil)
  1952.     meganame=(@battlers[index].pokemon.megaName rescue nil)
  1953.     if !meganame || meganame==""
  1954.       meganame=_INTL("Mega {1}",PBSpecies.getName(@battlers[index].pokemon.species))
  1955.     end
  1956.     pbDisplay(_INTL("{1} has Mega Evolved into {2}!",@battlers[index].pbThis,meganame))
  1957.     PBDebug.log("[Mega Evolution] #{@battlers[index].pbThis} became #{meganame}")
  1958.     side=(pbIsOpposing?(index)) ? 1 : 0
  1959.     owner=pbGetOwnerIndex(index)
  1960.     @megaEvolution[side][owner]=-2
  1961.   end
  1962. ################################################################################
  1963. # Primal Revert battler.
  1964. ################################################################################
  1965.   def pbPrimalReversion(index)
  1966.     return if !@battlers[index] || !@battlers[index].pokemon
  1967.     return if !(@battlers[index].hasPrimal? rescue false)
  1968.     return if (@battlers[index].isPrimal? rescue true)
  1969.     if isConst?(@battlers[index].pokemon.species,PBSpecies,:KYOGRE)
  1970.       pbCommonAnimation("PrimalKyogre",@battlers[index],nil)
  1971.     elsif isConst?(@battlers[index].pokemon.species,PBSpecies,:GROUDON)
  1972.       pbCommonAnimation("PrimalGroudon",@battlers[index],nil)
  1973.     end
  1974.     @battlers[index].pokemon.makePrimal
  1975.     @battlers[index].form=@battlers[index].pokemon.form
  1976.     @battlers[index].pbUpdate(true)
  1977.     @scene.pbChangePokemon(@battlers[index],@battlers[index].pokemon)
  1978.     if isConst?(@battlers[index].pokemon.species,PBSpecies,:KYOGRE)
  1979.       pbCommonAnimation("PrimalKyogre2",@battlers[index],nil)
  1980.     elsif isConst?(@battlers[index].pokemon.species,PBSpecies,:GROUDON)
  1981.       pbCommonAnimation("PrimalGroudon2",@battlers[index],nil)
  1982.     end
  1983.     pbDisplay(_INTL("{1}'s Primal Reversion!\nIt reverted to its primal form!",@battlers[index].pbThis))
  1984.     PBDebug.log("[Primal Reversion] #{@battlers[index].pbThis} Primal Reverted")
  1985.   end
  1986. ################################################################################
  1987. # Call battler.
  1988. ################################################################################
  1989.   def pbCall(index)
  1990.     owner=pbGetOwner(index)
  1991.     pbDisplay(_INTL("{1} called {2}!",owner.name,@battlers[index].name))
  1992.     pbDisplay(_INTL("{1}!",@battlers[index].name))
  1993.     PBDebug.log("[Call to Pokémon] #{owner.name} called to #{@battlers[index].pbThis(true)}")
  1994.     if @battlers[index].isShadow?
  1995.       if @battlers[index].inHyperMode?
  1996.         @battlers[index].pokemon.hypermode=false
  1997.         @battlers[index].pokemon.adjustHeart(-300)
  1998.         pbDisplay(_INTL("{1} came to its senses from the Trainer's call!",@battlers[index].pbThis))
  1999.       else
  2000.         pbDisplay(_INTL("But nothing happened!"))
  2001.       end
  2002.     elsif @battlers[index].status!=PBStatuses::SLEEP &&
  2003.           @battlers[index].pbCanIncreaseStatStage?(PBStats::ACCURACY,@battlers[index])
  2004.       @battlers[index].pbIncreaseStat(PBStats::ACCURACY,1,@battlers[index],true)
  2005.     else
  2006.       pbDisplay(_INTL("But nothing happened!"))
  2007.     end
  2008.   end
  2009. ################################################################################
  2010. # Gaining Experience.
  2011. ################################################################################
  2012.   def pbGainEXP
  2013.     return if !@internalbattle
  2014.     successbegin=true
  2015.     for i in 0...4 # Not ordered by priority
  2016.       if !@doublebattle && pbIsDoubleBattler?(i)
  2017.         @battlers[i].participants=[]
  2018.         next
  2019.       end
  2020.       if pbIsOpposing?(i) && @battlers[i].participants.length>0 &&
  2021.          (@battlers[i].fainted? || @battlers[i].captured)
  2022.         haveexpall=(hasConst?(PBItems,:EXPALL) && $PokemonBag.pbHasItem?(:EXPALL))
  2023.         # First count the number of participants
  2024.         partic=0
  2025.         expshare=0
  2026.         for j in @battlers[i].participants
  2027.           next if !@party1[j] || !pbIsOwner?(0,j)
  2028.           partic+=1 if @party1[j].hp>0 && !@party1[j].egg?
  2029.         end
  2030.         if !haveexpall
  2031.           for j in 0...@party1.length
  2032.             next if !@party1[j] || !pbIsOwner?(0,j)
  2033.             expshare+=1 if @party1[j].hp>0 && !@party1[j].egg? &&
  2034.                            (isConst?(@party1[j].item,PBItems,:EXPSHARE) ||
  2035.                            isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE))
  2036.           end
  2037.         end
  2038.         # Now calculate EXP for the participants
  2039.         if partic>0 || expshare>0 || haveexpall
  2040.           if !@opponent && successbegin && pbAllFainted?(@party2)
  2041.             @scene.pbWildBattleSuccess
  2042.             successbegin=false
  2043.           end
  2044.           for j in 0...@party1.length
  2045.             next if !@party1[j] || !pbIsOwner?(0,j)
  2046.             next if @party1[j].hp<=0 || @party1[j].egg?
  2047.             haveexpshare=(isConst?(@party1[j].item,PBItems,:EXPSHARE) ||
  2048.                           isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE))
  2049.             next if !haveexpshare && !@battlers[i].participants.include?(j)
  2050.             pbGainExpOne(j,@battlers[i],partic,expshare,haveexpall)
  2051.           end
  2052.           if haveexpall
  2053.             showmessage=true
  2054.             for j in 0...@party1.length
  2055.               next if !@party1[j] || !pbIsOwner?(0,j)
  2056.               next if @party1[j].hp<=0 || @party1[j].egg?
  2057.               next if isConst?(@party1[j].item,PBItems,:EXPSHARE) ||
  2058.                       isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE)
  2059.               next if @battlers[i].participants.include?(j)
  2060.               pbDisplayPaused(_INTL("The rest of your team gained Exp. Points thanks to the {1}!",
  2061.                  PBItems.getName(getConst(PBItems,:EXPALL)))) if showmessage
  2062.               showmessage=false
  2063.               pbGainExpOne(j,@battlers[i],partic,expshare,haveexpall,false)
  2064.             end
  2065.           end
  2066.         end
  2067.         # Now clear the participants array
  2068.         @battlers[i].participants=[]
  2069.       end
  2070.     end
  2071.   end
  2072.   def pbGainExpOne(index,defeated,partic,expshare,haveexpall,showmessages=true)
  2073.     thispoke=@party1[index]
  2074.     # Original species, not current species
  2075.     level=defeated.level
  2076.     baseexp=defeated.pokemon.baseExp
  2077.     evyield=defeated.pokemon.evYield
  2078.     # Gain effort value points, using RS effort values
  2079.     totalev=0
  2080.     for k in 0...6
  2081.       totalev+=thispoke.ev[k]
  2082.     end
  2083.     for k in 0...6
  2084.       evgain=evyield[k]
  2085.       evgain*=2 if isConst?(thispoke.item,PBItems,:MACHOBRACE) ||
  2086.                    isConst?(thispoke.itemInitial,PBItems,:MACHOBRACE)
  2087.       case k
  2088.       when PBStats::HP
  2089.         evgain+=4 if isConst?(thispoke.item,PBItems,:POWERWEIGHT) ||
  2090.                      isConst?(thispoke.itemInitial,PBItems,:POWERWEIGHT)
  2091.       when PBStats::ATTACK
  2092.         evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBRACER) ||
  2093.                      isConst?(thispoke.itemInitial,PBItems,:POWERBRACER)
  2094.       when PBStats::DEFENSE
  2095.         evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBELT) ||
  2096.                      isConst?(thispoke.itemInitial,PBItems,:POWERBELT)
  2097.       when PBStats::SPATK
  2098.         evgain+=4 if isConst?(thispoke.item,PBItems,:POWERLENS) ||
  2099.                      isConst?(thispoke.itemInitial,PBItems,:POWERLENS)
  2100.       when PBStats::SPDEF
  2101.         evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBAND) ||
  2102.                      isConst?(thispoke.itemInitial,PBItems,:POWERBAND)
  2103.       when PBStats::SPEED
  2104.         evgain+=4 if isConst?(thispoke.item,PBItems,:POWERANKLET) ||
  2105.                      isConst?(thispoke.itemInitial,PBItems,:POWERANKLET)
  2106.       end
  2107.       evgain*=2 if thispoke.pokerusStage>=1 # Infected or cured
  2108.       if evgain>0
  2109.         # Can't exceed overall limit
  2110.         evgain-=totalev+evgain-PokeBattle_Pokemon::EVLIMIT if totalev+evgain>PokeBattle_Pokemon::EVLIMIT
  2111.         # Can't exceed stat limit
  2112.         evgain-=thispoke.ev[k]+evgain-PokeBattle_Pokemon::EVSTATLIMIT if thispoke.ev[k]+evgain>PokeBattle_Pokemon::EVSTATLIMIT
  2113.         # Add EV gain
  2114.         thispoke.ev[k]+=evgain
  2115.         if thispoke.ev[k]>PokeBattle_Pokemon::EVSTATLIMIT
  2116.           print "Single-stat EV limit #{PokeBattle_Pokemon::EVSTATLIMIT} exceeded.\r\nStat: #{k}  EV gain: #{evgain}  EVs: #{thispoke.ev.inspect}"
  2117.           thispoke.ev[k]=PokeBattle_Pokemon::EVSTATLIMIT
  2118.         end
  2119.         totalev+=evgain
  2120.         if totalev>PokeBattle_Pokemon::EVLIMIT
  2121.           print "EV limit #{PokeBattle_Pokemon::EVLIMIT} exceeded.\r\nTotal EVs: #{totalev} EV gain: #{evgain}  EVs: #{thispoke.ev.inspect}"
  2122.         end
  2123.       end
  2124.     end
  2125.     # Gain experience
  2126.     ispartic=0
  2127.     ispartic=1 if defeated.participants.include?(index)
  2128.     haveexpshare=(isConst?(thispoke.item,PBItems,:EXPSHARE) ||
  2129.                   isConst?(thispoke.itemInitial,PBItems,:EXPSHARE)) ? 1 : 0
  2130.     exp=0
  2131.     if expshare>0
  2132.       if partic==0 # No participants, all Exp goes to Exp Share holders
  2133.         exp=(level*baseexp).floor
  2134.         exp=(exp/(NOSPLITEXP ? 1 : expshare)).floor*haveexpshare
  2135.       else
  2136.         if NOSPLITEXP
  2137.           exp=(level*baseexp).floor*ispartic
  2138.           exp=(level*baseexp/2).floor*haveexpshare if ispartic==0
  2139.         else
  2140.           exp=(level*baseexp/2).floor
  2141.           exp=(exp/partic).floor*ispartic + (exp/expshare).floor*haveexpshare
  2142.         end
  2143.       end
  2144.     elsif ispartic==1
  2145.       exp=(level*baseexp/(NOSPLITEXP ? 1 : partic)).floor
  2146.     elsif haveexpall
  2147.       exp=(level*baseexp/2).floor
  2148.     end
  2149.     return if exp<=0
  2150.     exp=(exp*3/2).floor if @opponent
  2151.     if USESCALEDEXPFORMULA
  2152.       exp=(exp/5).floor
  2153.       leveladjust=(2*level+10.0)/(level+thispoke.level+10.0)
  2154.       leveladjust=leveladjust**5
  2155.       leveladjust=Math.sqrt(leveladjust)
  2156.       exp=(exp*leveladjust).floor
  2157.       exp+=1 if ispartic>0 || haveexpshare>0
  2158.     else
  2159.       exp=(exp/7).floor
  2160.     end
  2161.     isOutsider=(thispoke.trainerID!=self.pbPlayer.id ||
  2162.                (thispoke.language!=0 && thispoke.language!=self.pbPlayer.language))
  2163.     if isOutsider
  2164.       if thispoke.language!=0 && thispoke.language!=self.pbPlayer.language
  2165.         exp=(exp*1.7).floor
  2166.       else
  2167.         exp=(exp*3/2).floor
  2168.       end
  2169.     end
  2170.     exp=(exp*3/2).floor if isConst?(thispoke.item,PBItems,:LUCKYEGG) ||
  2171.                            isConst?(thispoke.itemInitial,PBItems,:LUCKYEGG)
  2172.     growthrate=thispoke.growthrate
  2173.     newexp=PBExperience.pbAddExperience(thispoke.exp,exp,growthrate)
  2174.     exp=newexp-thispoke.exp
  2175.     if exp>0
  2176.       if showmessages
  2177.         if isOutsider
  2178.           pbDisplayPaused(_INTL("{1} gained a boosted {2} Exp. Points!",thispoke.name,exp))
  2179.         else
  2180.           pbDisplayPaused(_INTL("{1} gained {2} Exp. Points!",thispoke.name,exp))
  2181.         end
  2182.       end
  2183.       newlevel=PBExperience.pbGetLevelFromExperience(newexp,growthrate)
  2184.       tempexp=0
  2185.       curlevel=thispoke.level
  2186.       if newlevel<curlevel
  2187.         debuginfo="#{thispoke.name}: #{thispoke.level}/#{newlevel} | #{thispoke.exp}/#{newexp} | gain: #{exp}"
  2188.         raise RuntimeError.new(_INTL("The new level ({1}) is less than the Pokémon's\r\ncurrent level ({2}), which shouldn't happen.\r\n[Debug: {3}]",
  2189.                                newlevel,curlevel,debuginfo))
  2190.         return
  2191.       end
  2192.       if thispoke.respond_to?("isShadow?") && thispoke.isShadow?
  2193.         thispoke.exp+=exp
  2194.       else
  2195.         tempexp1=thispoke.exp
  2196.         tempexp2=0
  2197.         # Find battler
  2198.         battler=pbFindPlayerBattler(index)
  2199.         loop do
  2200.           # EXP Bar animation
  2201.           startexp=PBExperience.pbGetStartExperience(curlevel,growthrate)
  2202.           endexp=PBExperience.pbGetStartExperience(curlevel+1,growthrate)
  2203.           tempexp2=(endexp<newexp) ? endexp : newexp
  2204.           thispoke.exp=tempexp2
  2205.           @scene.pbEXPBar(thispoke,battler,startexp,endexp,tempexp1,tempexp2)
  2206.           tempexp1=tempexp2
  2207.           curlevel+=1
  2208.           if curlevel>newlevel
  2209.             thispoke.calcStats
  2210.             battler.pbUpdate(false) if battler
  2211.             @scene.pbRefresh
  2212.             break
  2213.           end
  2214.           oldtotalhp=thispoke.totalhp
  2215.           oldattack=thispoke.attack
  2216.           olddefense=thispoke.defense
  2217.           oldspeed=thispoke.speed
  2218.           oldspatk=thispoke.spatk
  2219.           oldspdef=thispoke.spdef
  2220.           if battler && battler.pokemon && @internalbattle
  2221.             battler.pokemon.changeHappiness("levelup")
  2222.           end
  2223.           thispoke.calcStats
  2224.           battler.pbUpdate(false) if battler
  2225.           @scene.pbRefresh
  2226.           pbDisplayPaused(_INTL("{1} grew to Level {2}!",thispoke.name,curlevel))
  2227.           @scene.pbLevelUp(thispoke,battler,oldtotalhp,oldattack,
  2228.                            olddefense,oldspeed,oldspatk,oldspdef)
  2229.           # Finding all moves learned at this level
  2230.           movelist=thispoke.getMoveList
  2231.           for k in movelist
  2232.             if k[0]==thispoke.level   # Learned a new move
  2233.               pbLearnMove(index,k[1])
  2234.             end
  2235.           end
  2236.         end
  2237.       end
  2238.     end
  2239.   end
  2240. ################################################################################
  2241. # Learning a move.
  2242. ################################################################################
  2243.   def pbLearnMove(pkmnIndex,move)
  2244.     pokemon=@party1[pkmnIndex]
  2245.     return if !pokemon
  2246.     pkmnname=pokemon.name
  2247.     battler=pbFindPlayerBattler(pkmnIndex)
  2248.     movename=PBMoves.getName(move)
  2249.     for i in 0...4
  2250.       return if pokemon.moves[i].id==move
  2251.       if pokemon.moves[i].id==0
  2252.         pokemon.moves[i]=PBMove.new(move)
  2253.         battler.moves[i]=PokeBattle_Move.pbFromPBMove(self,pokemon.moves[i]) if battler
  2254.         pbDisplayPaused(_INTL("{1} learned {2}!",pkmnname,movename))
  2255.         PBDebug.log("[Learn move] #{pkmnname} learned #{movename}")
  2256.         return
  2257.       end
  2258.     end
  2259.     loop do
  2260.       pbDisplayPaused(_INTL("{1} is trying to learn {2}.",pkmnname,movename))
  2261.       pbDisplayPaused(_INTL("But {1} can't learn more than four moves.",pkmnname))
  2262.       if pbDisplayConfirm(_INTL("Delete a move to make room for {1}?",movename))
  2263.         pbDisplayPaused(_INTL("Which move should be forgotten?"))
  2264.         forgetmove=@scene.pbForgetMove(pokemon,move)
  2265.         if forgetmove>=0
  2266.           oldmovename=PBMoves.getName(pokemon.moves[forgetmove].id)
  2267.           pokemon.moves[forgetmove]=PBMove.new(move) # Replaces current/total PP
  2268.           battler.moves[forgetmove]=PokeBattle_Move.pbFromPBMove(self,pokemon.moves[forgetmove]) if battler
  2269.           pbDisplayPaused(_INTL("1,  2, and... ... ..."))
  2270.           pbDisplayPaused(_INTL("Poof!"))
  2271.           pbDisplayPaused(_INTL("{1} forgot {2}.",pkmnname,oldmovename))
  2272.           pbDisplayPaused(_INTL("And..."))
  2273.           pbDisplayPaused(_INTL("{1} learned {2}!",pkmnname,movename))
  2274.           PBDebug.log("[Learn move] #{pkmnname} forgot #{oldmovename} and learned #{movename}")
  2275.           return
  2276.         elsif pbDisplayConfirm(_INTL("Should {1} stop learning {2}?",pkmnname,movename))
  2277.           pbDisplayPaused(_INTL("{1} did not learn {2}.",pkmnname,movename))
  2278.           return
  2279.         end
  2280.       elsif pbDisplayConfirm(_INTL("Should {1} stop learning {2}?",pkmnname,movename))
  2281.         pbDisplayPaused(_INTL("{1} did not learn {2}.",pkmnname,movename))
  2282.         return
  2283.       end
  2284.     end
  2285.   end
  2286. ################################################################################
  2287. # Abilities.
  2288. ################################################################################
  2289.   def pbOnActiveAll
  2290.     for i in 0...4 # Currently unfainted participants will earn EXP even if they faint afterwards
  2291.       @battlers[i].pbUpdateParticipants if pbIsOpposing?(i)
  2292.       @amuletcoin=true if !pbIsOpposing?(i) &&
  2293.                           (isConst?(@battlers[i].item,PBItems,:AMULETCOIN) ||
  2294.                            isConst?(@battlers[i].item,PBItems,:LUCKINCENSE))
  2295.     end
  2296.     for i in 0...4
  2297.       if !@battlers[i].fainted?
  2298.         if @battlers[i].isShadow? && pbIsOpposing?(i)
  2299.           pbCommonAnimation("Shadow",@battlers[i],nil)
  2300.           pbDisplay(_INTL("Oh!\nA Shadow Pokémon!"))
  2301.         end
  2302.       end
  2303.     end
  2304.     # Weather-inducing abilities, Trace, Imposter, etc.
  2305.     @usepriority=false
  2306.     priority=pbPriority
  2307.     for i in priority
  2308.       i.pbAbilitiesOnSwitchIn(true)
  2309.     end
  2310.     # Check forms are correct
  2311.     for i in 0...4
  2312.       next if @battlers[i].fainted?
  2313.       @battlers[i].pbCheckForm
  2314.     end
  2315.   end
  2316.   def pbOnActiveOne(pkmn,onlyabilities=false,moldbreaker=false)
  2317.     return false if pkmn.fainted?
  2318.     if !onlyabilities
  2319.       for i in 0...4 # Currently unfainted participants will earn EXP even if they faint afterwards
  2320.         @battlers[i].pbUpdateParticipants if pbIsOpposing?(i)
  2321.         @amuletcoin=true if !pbIsOpposing?(i) &&
  2322.                             (isConst?(@battlers[i].item,PBItems,:AMULETCOIN) ||
  2323.                              isConst?(@battlers[i].item,PBItems,:LUCKINCENSE))
  2324.       end
  2325.       if pkmn.isShadow? && pbIsOpposing?(pkmn.index)
  2326.         pbCommonAnimation("Shadow",pkmn,nil)
  2327.         pbDisplay(_INTL("Oh!\nA Shadow Pokémon!"))
  2328.       end
  2329.       # Healing Wish
  2330.       if pkmn.effects[PBEffects::HealingWish]
  2331.         PBDebug.log("[Lingering effect triggered] #{pkmn.pbThis}'s Healing Wish")
  2332.         pbCommonAnimation("HealingWish",pkmn,nil)
  2333.         pbDisplayPaused(_INTL("The healing wish came true for {1}!",pkmn.pbThis(true)))
  2334.         pkmn.pbRecoverHP(pkmn.totalhp,true)
  2335.         pkmn.pbCureStatus(false)
  2336.         pkmn.effects[PBEffects::HealingWish]=false
  2337.       end
  2338.       # Lunar Dance
  2339.       if pkmn.effects[PBEffects::LunarDance]
  2340.         PBDebug.log("[Lingering effect triggered] #{pkmn.pbThis}'s Lunar Dance")
  2341.         pbCommonAnimation("LunarDance",pkmn,nil)
  2342.         pbDisplayPaused(_INTL("{1} became cloaked in mystical moonlight!",pkmn.pbThis))
  2343.         pkmn.pbRecoverHP(pkmn.totalhp,true)
  2344.         pkmn.pbCureStatus(false)
  2345.         for i in 0...4
  2346.           pkmn.moves[i].pp=pkmn.moves[i].totalpp
  2347.         end
  2348.         pkmn.effects[PBEffects::LunarDance]=false
  2349.       end
  2350.       # Spikes
  2351.       if pkmn.pbOwnSide.effects[PBEffects::Spikes]>0 && !pkmn.isAirborne?(moldbreaker)
  2352.         if !pkmn.hasWorkingAbility(:MAGICGUARD)
  2353.           PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Spikes")
  2354.           spikesdiv=[8,6,4][pkmn.pbOwnSide.effects[PBEffects::Spikes]-1]
  2355.           @scene.pbDamageAnimation(pkmn,0)
  2356.           pkmn.pbReduceHP((pkmn.totalhp/spikesdiv).floor)
  2357.           pbDisplayPaused(_INTL("{1} is hurt by the spikes!",pkmn.pbThis))
  2358.         end
  2359.       end
  2360.       pkmn.pbFaint if pkmn.fainted?
  2361.       # Stealth Rock
  2362.       if pkmn.pbOwnSide.effects[PBEffects::StealthRock] && !pkmn.fainted?
  2363.         if !pkmn.hasWorkingAbility(:MAGICGUARD)
  2364.           atype=getConst(PBTypes,:ROCK) || 0
  2365.           eff=PBTypes.getCombinedEffectiveness(atype,pkmn.type1,pkmn.type2,pkmn.effects[PBEffects::Type3])
  2366.           if eff>0
  2367.             PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Stealth Rock")
  2368.             @scene.pbDamageAnimation(pkmn,0)
  2369.             pkmn.pbReduceHP(((pkmn.totalhp*eff)/64).floor)
  2370.             pbDisplayPaused(_INTL("Pointed stones dug into {1}!",pkmn.pbThis))
  2371.           end
  2372.         end
  2373.       end
  2374.       pkmn.pbFaint if pkmn.fainted?
  2375.       # Toxic Spikes
  2376.       if pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]>0 && !pkmn.fainted?
  2377.         if !pkmn.isAirborne?(moldbreaker)
  2378.           if pkmn.pbHasType?(:POISON)
  2379.             PBDebug.log("[Entry hazard] #{pkmn.pbThis} absorbed Toxic Spikes")
  2380.             pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]=0
  2381.             pbDisplayPaused(_INTL("{1} absorbed the poison spikes!",pkmn.pbThis))
  2382.           elsif pkmn.pbCanPoisonSpikes?(moldbreaker)
  2383.             PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Toxic Spikes")
  2384.             if pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]==2
  2385.               pkmn.pbPoison(nil,_INTL("{1} was badly poisoned by the poison spikes!",pkmn.pbThis,true))
  2386.             else
  2387.               pkmn.pbPoison(nil,_INTL("{1} was poisoned by the poison spikes!",pkmn.pbThis))
  2388.             end
  2389.           end
  2390.         end
  2391.       end
  2392.       # Sticky Web
  2393.       if pkmn.pbOwnSide.effects[PBEffects::StickyWeb] && !pkmn.fainted? &&
  2394.          !pkmn.isAirborne?(moldbreaker)
  2395.         if pkmn.pbCanReduceStatStage?(PBStats::SPEED,nil,false,nil,moldbreaker)
  2396.           PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Sticky Web")
  2397.           pkmn.pbReduceStat(PBStats::SPEED,1,nil,false,nil,true,moldbreaker)
  2398.           pbDisplayPaused(_INTL("{1} was caught in a sticky web!",pkmn.pbThis))
  2399.         end
  2400.       end
  2401.     end
  2402.     pkmn.pbAbilityCureCheck
  2403.     if pkmn.fainted?
  2404.       pbGainEXP
  2405.       pbJudge #      pbSwitch
  2406.       return false
  2407.     end
  2408. #    pkmn.pbAbilitiesOnSwitchIn(true)
  2409.     if !onlyabilities
  2410.       pkmn.pbCheckForm
  2411.       pkmn.pbBerryCureCheck
  2412.     end
  2413.     return true
  2414.   end
  2415.   def pbPrimordialWeather
  2416.     # End Primordial Sea, Desolate Land, Delta Stream
  2417.     hasabil=false
  2418.     case @weather
  2419.     when PBWeather::HEAVYRAIN
  2420.       for i in 0...4
  2421.         if isConst?(@battlers[i].ability,PBAbilities,:PRIMORDIALSEA) &&
  2422.            !@battlers[i].fainted?
  2423.           hasabil=true; break
  2424.         end
  2425.         if !hasabil
  2426.           @weather=0
  2427.           pbDisplayBrief("The heavy rain has lifted!")
  2428.         end
  2429.       end
  2430.     when PBWeather::HARSHSUN
  2431.       for i in 0...4
  2432.         if isConst?(@battlers[i].ability,PBAbilities,:DESOLATELAND) &&
  2433.            !@battlers[i].fainted?
  2434.           hasabil=true; break
  2435.         end
  2436.         if !hasabil
  2437.           @weather=0
  2438.           pbDisplayBrief("The harsh sunlight faded!")
  2439.         end
  2440.       end
  2441.     when PBWeather::STRONGWINDS
  2442.       for i in 0...4
  2443.         if isConst?(@battlers[i].ability,PBAbilities,:DELTASTREAM) &&
  2444.            !@battlers[i].fainted?
  2445.           hasabil=true; break
  2446.         end
  2447.         if !hasabil
  2448.           @weather=0
  2449.           pbDisplayBrief("The mysterious air current has dissipated!")
  2450.         end
  2451.       end
  2452.     end
  2453.   end
  2454. ################################################################################
  2455. # Judging.
  2456. ################################################################################
  2457.   def pbJudgeCheckpoint(attacker,move=0)
  2458.   end
  2459.   def pbDecisionOnTime
  2460.     count1=0
  2461.     count2=0
  2462.     hptotal1=0
  2463.     hptotal2=0
  2464.     for i in @party1
  2465.       next if !i
  2466.       if i.hp>0 && !i.egg?
  2467.         count1+=1
  2468.         hptotal1+=i.hp
  2469.       end
  2470.     end
  2471.     for i in @party2
  2472.       next if !i
  2473.       if i.hp>0 && !i.egg?
  2474.         count2+=1
  2475.         hptotal2+=i.hp
  2476.       end
  2477.     end
  2478.     return 1 if count1>count2     # win
  2479.     return 2 if count1<count2     # loss
  2480.     return 1 if hptotal1>hptotal2 # win
  2481.     return 2 if hptotal1<hptotal2 # loss
  2482.     return 5                      # draw
  2483.   end
  2484.   def pbDecisionOnTime2
  2485.     count1=0
  2486.     count2=0
  2487.     hptotal1=0
  2488.     hptotal2=0
  2489.     for i in @party1
  2490.       next if !i
  2491.       if i.hp>0 && !i.egg?
  2492.         count1+=1
  2493.         hptotal1+=(i.hp*100/i.totalhp)
  2494.       end
  2495.     end
  2496.     hptotal1/=count1 if count1>0
  2497.     for i in @party2
  2498.       next if !i
  2499.       if i.hp>0 && !i.egg?
  2500.         count2+=1
  2501.         hptotal2+=(i.hp*100/i.totalhp)
  2502.       end
  2503.     end
  2504.     hptotal2/=count2 if count2>0
  2505.     return 1 if count1>count2     # win
  2506.     return 2 if count1<count2     # loss
  2507.     return 1 if hptotal1>hptotal2 # win
  2508.     return 2 if hptotal1<hptotal2 # loss
  2509.     return 5                      # draw
  2510.   end
  2511.   def pbDecisionOnDraw
  2512.     return 5 # draw
  2513.   end
  2514.   def pbJudge
  2515. #   PBDebug.log("[Counts: #{pbPokemonCount(@party1)}/#{pbPokemonCount(@party2)}]")
  2516.     if pbAllFainted?(@party1) && pbAllFainted?(@party2)
  2517.       @decision=pbDecisionOnDraw() # Draw
  2518.       return
  2519.     end
  2520.     if pbAllFainted?(@party1)
  2521.       @decision=2 # Loss
  2522.       return
  2523.     end
  2524.     if pbAllFainted?(@party2)
  2525.       @decision=1 # Win
  2526.       return
  2527.     end
  2528.   end
  2529. ################################################################################
  2530. # Messages and animations.
  2531. ################################################################################
  2532.   def pbDisplay(msg)
  2533.     @scene.pbDisplayMessage(msg)
  2534.   end
  2535.   def pbDisplayPaused(msg)
  2536.     @scene.pbDisplayPausedMessage(msg)
  2537.   end
  2538.   def pbDisplayBrief(msg)
  2539.     @scene.pbDisplayMessage(msg,true)
  2540.   end
  2541.   def pbDisplayConfirm(msg)
  2542.     @scene.pbDisplayConfirmMessage(msg)
  2543.   end
  2544.   def pbShowCommands(msg,commands,cancancel=true)
  2545.     @scene.pbShowCommands(msg,commands,cancancel)
  2546.   end
  2547.   def pbAnimation(move,attacker,opponent,hitnum=0)
  2548.     if @battlescene
  2549.       @scene.pbAnimation(move,attacker,opponent,hitnum)
  2550.     end
  2551.   end
  2552.   def pbCommonAnimation(name,attacker,opponent,hitnum=0)
  2553.     if @battlescene
  2554.       @scene.pbCommonAnimation(name,attacker,opponent,hitnum)
  2555.     end
  2556.   end
  2557. ################################################################################
  2558. # Battle core.
  2559. ################################################################################
  2560.   def pbStartBattle(canlose=false)
  2561.     PBDebug.log("")
  2562.     PBDebug.log("******************************************")
  2563.     begin
  2564.       pbStartBattleCore(canlose)
  2565.     rescue BattleAbortedException
  2566.       @decision=0
  2567.       @scene.pbEndBattle(@decision)
  2568.     end
  2569.     return @decision
  2570.   end
  2571.   def pbStartBattleCore(canlose)
  2572.     if !@fullparty1 && @party1.length>MAXPARTYSIZE
  2573.       raise ArgumentError.new(_INTL("Party 1 has more than {1} Pokémon.",MAXPARTYSIZE))
  2574.     end
  2575.     if !@fullparty2 && @party2.length>MAXPARTYSIZE
  2576.       raise ArgumentError.new(_INTL("Party 2 has more than {1} Pokémon.",MAXPARTYSIZE))
  2577.     end
  2578. #========================
  2579. # Initialize wild Pokémon
  2580. #========================
  2581.     if !@opponent
  2582.       if @party2.length==1
  2583.         if @doublebattle
  2584.           raise _INTL("Only two wild Pokémon are allowed in double battles")
  2585.         end
  2586.         wildpoke=@party2[0]
  2587.         @battlers[1].pbInitialize(wildpoke,0,false)
  2588.         @peer.pbOnEnteringBattle(self,wildpoke)
  2589.         pbSetSeen(wildpoke)
  2590.         @scene.pbStartBattle(self)
  2591.         pbDisplayPaused(_INTL("Wild {1} appeared!",wildpoke.name))
  2592.       elsif @party2.length==2
  2593.         if !@doublebattle
  2594.           raise _INTL("Only one wild Pokémon is allowed in single battles")
  2595.         end
  2596.         @battlers[1].pbInitialize(@party2[0],0,false)
  2597.         @battlers[3].pbInitialize(@party2[1],1,false)
  2598.         @peer.pbOnEnteringBattle(self,@party2[0])
  2599.         @peer.pbOnEnteringBattle(self,@party2[1])
  2600.         pbSetSeen(@party2[0])
  2601.         pbSetSeen(@party2[1])
  2602.         @scene.pbStartBattle(self)
  2603.         pbDisplayPaused(_INTL("Wild {1} and\r\n{2} appeared!",
  2604.            @party2[0].name,@party2[1].name))
  2605.       else
  2606.         raise _INTL("Only one or two wild Pokémon are allowed")
  2607.       end
  2608. #=======================================
  2609. # Initialize opponents in double battles
  2610. #=======================================
  2611.     elsif @doublebattle
  2612.       if @opponent.is_a?(Array)
  2613.         if @opponent.length==1
  2614.           @opponent=@opponent[0]
  2615.         elsif @opponent.length!=2
  2616.           raise _INTL("Opponents with zero or more than two people are not allowed")
  2617.         end
  2618.       end
  2619.       if @player.is_a?(Array)
  2620.         if @player.length==1
  2621.           @player=@player[0]
  2622.         elsif @player.length!=2
  2623.           raise _INTL("Player trainers with zero or more than two people are not allowed")
  2624.         end
  2625.       end
  2626.       @scene.pbStartBattle(self)
  2627.       if @opponent.is_a?(Array)
  2628.         pbDisplayPaused(_INTL("{1} and {2} want to battle!",@opponent[0].fullname,@opponent[1].fullname))
  2629.         sendout1=pbFindNextUnfainted(@party2,0,pbSecondPartyBegin(1))
  2630.         raise _INTL("Opponent 1 has no unfainted Pokémon") if sendout1<0
  2631.         sendout2=pbFindNextUnfainted(@party2,pbSecondPartyBegin(1))
  2632.         raise _INTL("Opponent 2 has no unfainted Pokémon") if sendout2<0
  2633.         @battlers[1].pbInitialize(@party2[sendout1],sendout1,false)
  2634.         pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent[0].fullname,@battlers[1].name))
  2635.         pbSendOut(1,@party2[sendout1])
  2636.         @battlers[3].pbInitialize(@party2[sendout2],sendout2,false)
  2637.         pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent[1].fullname,@battlers[3].name))
  2638.         pbSendOut(3,@party2[sendout2])
  2639.       else
  2640.         pbDisplayPaused(_INTL("{1}\r\nwould like to battle!",@opponent.fullname))
  2641.         sendout1=pbFindNextUnfainted(@party2,0)
  2642.         sendout2=pbFindNextUnfainted(@party2,sendout1+1)
  2643.         if sendout1<0 || sendout2<0
  2644.           raise _INTL("Opponent doesn't have two unfainted Pokémon")
  2645.         end
  2646.         @battlers[1].pbInitialize(@party2[sendout1],sendout1,false)
  2647.         @battlers[3].pbInitialize(@party2[sendout2],sendout2,false)
  2648.         pbDisplayBrief(_INTL("{1} sent\r\nout {2} and {3}!",
  2649.            @opponent.fullname,@battlers[1].name,@battlers[3].name))
  2650.         pbSendOut(1,@party2[sendout1])
  2651.         pbSendOut(3,@party2[sendout2])
  2652.       end
  2653. #======================================
  2654. # Initialize opponent in single battles
  2655. #======================================
  2656.     else
  2657.       sendout=pbFindNextUnfainted(@party2,0)
  2658.       raise _INTL("Trainer has no unfainted Pokémon") if sendout<0
  2659.       if @opponent.is_a?(Array)
  2660.         raise _INTL("Opponent trainer must be only one person in single battles") if @opponent.length!=1
  2661.         @opponent=@opponent[0]
  2662.       end
  2663.       if @player.is_a?(Array)
  2664.         raise _INTL("Player trainer must be only one person in single battles") if @player.length!=1
  2665.         @player=@player[0]
  2666.       end
  2667.       trainerpoke=@party2[sendout]
  2668.       @scene.pbStartBattle(self)
  2669.       pbDisplayPaused(_INTL("{1}\r\nwould like to battle!",@opponent.fullname))
  2670.       @battlers[1].pbInitialize(trainerpoke,sendout,false)
  2671.       pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent.fullname,@battlers[1].name))
  2672.       pbSendOut(1,trainerpoke)
  2673.     end
  2674. #=====================================
  2675. # Initialize players in double battles
  2676. #=====================================
  2677.     if @doublebattle
  2678.       if @player.is_a?(Array)
  2679.         sendout1=pbFindNextUnfainted(@party1,0,pbSecondPartyBegin(0))
  2680.         raise _INTL("Player 1 has no unfainted Pokémon") if sendout1<0
  2681.         sendout2=pbFindNextUnfainted(@party1,pbSecondPartyBegin(0))
  2682.         raise _INTL("Player 2 has no unfainted Pokémon") if sendout2<0
  2683.         @battlers[0].pbInitialize(@party1[sendout1],sendout1,false)
  2684.         @battlers[2].pbInitialize(@party1[sendout2],sendout2,false)
  2685.         pbDisplayBrief(_INTL("{1} sent\r\nout {2}! Go! {3}!",
  2686.            @player[1].fullname,@battlers[2].name,@battlers[0].name))
  2687.         pbSetSeen(@party1[sendout1])
  2688.         pbSetSeen(@party1[sendout2])
  2689.       else
  2690.         sendout1=pbFindNextUnfainted(@party1,0)
  2691.         sendout2=pbFindNextUnfainted(@party1,sendout1+1)
  2692.         if sendout1<0 || sendout2<0
  2693.           raise _INTL("Player doesn't have two unfainted Pokémon")
  2694.         end
  2695.         @battlers[0].pbInitialize(@party1[sendout1],sendout1,false)
  2696.         @battlers[2].pbInitialize(@party1[sendout2],sendout2,false)
  2697.         pbDisplayBrief(_INTL("Go! {1} and {2}!",@battlers[0].name,@battlers[2].name))
  2698.       end
  2699.       pbSendOut(0,@party1[sendout1])
  2700.       pbSendOut(2,@party1[sendout2])
  2701. #====================================
  2702. # Initialize player in single battles
  2703. #====================================
  2704.     else
  2705.       sendout=pbFindNextUnfainted(@party1,0)
  2706.       if sendout<0
  2707.         raise _INTL("Player has no unfainted Pokémon")
  2708.       end
  2709.       @battlers[0].pbInitialize(@party1[sendout],sendout,false)
  2710.       pbDisplayBrief(_INTL("Go! {1}!",@battlers[0].name))
  2711.       pbSendOut(0,@party1[sendout])
  2712.     end
  2713. #==================
  2714. # Initialize battle
  2715. #==================
  2716.     if @weather==PBWeather::SUNNYDAY
  2717.       pbCommonAnimation("Sunny",nil,nil)
  2718.       pbDisplay(_INTL("The sunlight is strong."))
  2719.     elsif @weather==PBWeather::RAINDANCE
  2720.       pbCommonAnimation("Rain",nil,nil)
  2721.       pbDisplay(_INTL("It is raining."))
  2722.     elsif @weather==PBWeather::SANDSTORM
  2723.       pbCommonAnimation("Sandstorm",nil,nil)
  2724.       pbDisplay(_INTL("A sandstorm is raging."))
  2725.     elsif @weather==PBWeather::HAIL
  2726.       pbCommonAnimation("Hail",nil,nil)
  2727.       pbDisplay(_INTL("Hail is falling."))
  2728.     elsif @weather==PBWeather::HEAVYRAIN
  2729.       pbCommonAnimation("HeavyRain",nil,nil)
  2730.       pbDisplay(_INTL("It is raining heavily."))
  2731.     elsif @weather==PBWeather::HARSHSUN
  2732.       pbCommonAnimation("HarshSun",nil,nil)
  2733.       pbDisplay(_INTL("The sunlight is extremely harsh."))
  2734.     elsif @weather==PBWeather::STRONGWINDS
  2735.       pbCommonAnimation("StrongWinds",nil,nil)
  2736.       pbDisplay(_INTL("The wind is strong."))
  2737.     end
  2738.     pbOnActiveAll   # Abilities
  2739.     @turncount=0
  2740.     loop do   # Now begin the battle loop
  2741.       PBDebug.log("")
  2742.       PBDebug.log("***Round #{@turncount+1}***")
  2743.       if @debug && @turncount>=100
  2744.         @decision=pbDecisionOnTime()
  2745.         PBDebug.log("")
  2746.         PBDebug.log("***Undecided after 100 rounds, aborting***")
  2747.         pbAbort
  2748.         break
  2749.       end
  2750.       PBDebug.logonerr{
  2751.          pbCommandPhase
  2752.       }
  2753.       break if @decision>0
  2754.       PBDebug.logonerr{
  2755.          pbAttackPhase
  2756.       }
  2757.       break if @decision>0
  2758.       PBDebug.logonerr{
  2759.          pbEndOfRoundPhase
  2760.       }
  2761.       break if @decision>0
  2762.       @turncount+=1
  2763.     end
  2764.     return pbEndOfBattle(canlose)
  2765.   end
  2766. ################################################################################
  2767. # Command phase.
  2768. ################################################################################
  2769.   def pbCommandMenu(i)
  2770.     return @scene.pbCommandMenu(i)
  2771.   end
  2772.   def pbItemMenu(i)
  2773.     return @scene.pbItemMenu(i)
  2774.   end
  2775.   def pbAutoFightMenu(i)
  2776.     return false
  2777.   end
  2778.   def pbCommandPhase
  2779.     @scene.pbBeginCommandPhase
  2780. #    @scene.pbResetCommandIndices
  2781.     for i in 0...4   # Reset choices if commands can be shown
  2782.       @battlers[i].effects[PBEffects::SkipTurn]=false
  2783.       if pbCanShowCommands?(i) || @battlers[i].fainted?
  2784.         @choices[i][0]=0
  2785.         @choices[i][1]=0
  2786.         @choices[i][2]=nil
  2787.         @choices[i][3]=-1
  2788.       else
  2789.         unless !@doublebattle && pbIsDoubleBattler?(i)
  2790.           PBDebug.log("[Reusing commands] #{@battlers[i].pbThis(true)}")
  2791.         end
  2792.       end
  2793.     end
  2794.     # Reset choices to perform Mega Evolution if it wasn't done somehow
  2795.     for i in 0...2
  2796.       for j in 0...@megaEvolution[i].length
  2797.         @megaEvolution[i][j]=-1 if @megaEvolution[i][j]>=0
  2798.       end
  2799.     end
  2800.     for i in 0...4
  2801.       break if @decision!=0
  2802.       next if @choices[i][0]!=0
  2803.       if !pbOwnedByPlayer?(i) || @controlPlayer
  2804.         if !@battlers[i].fainted? && pbCanShowCommands?(i)
  2805.           @scene.pbChooseEnemyCommand(i)
  2806.         end
  2807.       else
  2808.         commandDone=false
  2809.         commandEnd=false
  2810.         if pbCanShowCommands?(i)
  2811.           loop do
  2812.             cmd=pbCommandMenu(i)
  2813.             if cmd==0 # Fight
  2814.               if pbCanShowFightMenu?(i)
  2815.                 commandDone=true if pbAutoFightMenu(i)
  2816.                 until commandDone
  2817.                   index=@scene.pbFightMenu(i)
  2818.                   if index<0
  2819.                     side=(pbIsOpposing?(i)) ? 1 : 0
  2820.                     owner=pbGetOwnerIndex(i)
  2821.                     if @megaEvolution[side][owner]==i
  2822.                       @megaEvolution[side][owner]=-1
  2823.                     end
  2824.                     break
  2825.                   end
  2826.                   next if !pbRegisterMove(i,index)
  2827.                   if @doublebattle
  2828.                     thismove=@battlers[i].moves[index]
  2829.                     target=@battlers[i].pbTarget(thismove)
  2830.                     if target==PBTargets::SingleNonUser # single non-user
  2831.                       target=@scene.pbChooseTarget(i,target)
  2832.                       next if target<0
  2833.                       pbRegisterTarget(i,target)
  2834.                     elsif target==PBTargets::UserOrPartner # Acupressure
  2835.                       target=@scene.pbChooseTarget(i,target)
  2836.                       next if target<0 || (target&1)==1
  2837.                       pbRegisterTarget(i,target)
  2838.                     end
  2839.                   end
  2840.                   commandDone=true
  2841.                 end
  2842.               else
  2843.                 pbAutoChooseMove(i)
  2844.                 commandDone=true
  2845.               end
  2846.             elsif cmd!=0 && @battlers[i].effects[PBEffects::SkyDrop]
  2847.               pbDisplay(_INTL("Sky Drop won't let {1} go!",@battlers[i].pbThis(true)))
  2848.             elsif cmd==1 # Bag
  2849.               if !@internalbattle
  2850.                 if pbOwnedByPlayer?(i)
  2851.                   pbDisplay(_INTL("Items can't be used here."))
  2852.                 end
  2853.               else
  2854.                 item=pbItemMenu(i)
  2855.                 if item[0]>0
  2856.                   if pbRegisterItem(i,item[0],item[1])
  2857.                     commandDone=true
  2858.                   end
  2859.                 end
  2860.               end
  2861.             elsif cmd==2 # Pokémon
  2862.               pkmn=pbSwitchPlayer(i,false,true)
  2863.               if pkmn>=0
  2864.                 commandDone=true if pbRegisterSwitch(i,pkmn)
  2865.               end
  2866.             elsif cmd==3   # Run
  2867.               run=pbRun(i)
  2868.               if run>0
  2869.                 commandDone=true
  2870.                 return
  2871.               elsif run<0
  2872.                 commandDone=true
  2873.                 side=(pbIsOpposing?(i)) ? 1 : 0
  2874.                 owner=pbGetOwnerIndex(i)
  2875.                 if @megaEvolution[side][owner]==i
  2876.                   @megaEvolution[side][owner]=-1
  2877.                 end
  2878.               end
  2879.             elsif cmd==4   # Call
  2880.               thispkmn=@battlers[i]
  2881.               @choices[i][0]=4   # "Call Pokémon"
  2882.               @choices[i][1]=0
  2883.               @choices[i][2]=nil
  2884.               side=(pbIsOpposing?(i)) ? 1 : 0
  2885.               owner=pbGetOwnerIndex(i)
  2886.               if @megaEvolution[side][owner]==i
  2887.                 @megaEvolution[side][owner]=-1
  2888.               end
  2889.               commandDone=true
  2890.             elsif cmd==-1   # Go back to first battler's choice
  2891.               @megaEvolution[0][0]=-1 if @megaEvolution[0][0]>=0
  2892.               @megaEvolution[1][0]=-1 if @megaEvolution[1][0]>=0
  2893.               # Restore the item the player's first Pokémon was due to use
  2894.               if @choices[0][0]==3 && $PokemonBag && $PokemonBag.pbCanStore?(@choices[0][1])
  2895.                 $PokemonBag.pbStoreItem(@choices[0][1])
  2896.               end
  2897.               pbCommandPhase
  2898.               return
  2899.             end
  2900.             break if commandDone
  2901.           end
  2902.         end
  2903.       end
  2904.     end
  2905.   end
  2906. ################################################################################
  2907. # Attack phase.
  2908. ################################################################################
  2909.   def pbAttackPhase
  2910.     @scene.pbBeginAttackPhase
  2911.     for i in 0...4
  2912.       @successStates[i].clear
  2913.       if @choices[i][0]!=1 && @choices[i][0]!=2
  2914.         @battlers[i].effects[PBEffects::DestinyBond]=false
  2915.         @battlers[i].effects[PBEffects::Grudge]=false
  2916.       end
  2917.       @battlers[i].turncount+=1 if !@battlers[i].fainted?
  2918.       @battlers[i].effects[PBEffects::Rage]=false if !pbChoseMove?(i,:RAGE)
  2919.     end
  2920.     # Calculate priority at this time
  2921.     @usepriority=false
  2922.     priority=pbPriority(false,true)
  2923.     # Mega Evolution
  2924.     megaevolved=[]
  2925.     for i in priority
  2926.       if @choices[i.index][0]==1 && !i.effects[PBEffects::SkipTurn]
  2927.         side=(pbIsOpposing?(i.index)) ? 1 : 0
  2928.         owner=pbGetOwnerIndex(i.index)
  2929.         if @megaEvolution[side][owner]==i.index
  2930.           pbMegaEvolve(i.index)
  2931.           megaevolved.push(i.index)
  2932.         end
  2933.       end
  2934.     end
  2935.     if megaevolved.length>0
  2936.       for i in priority
  2937.         i.pbAbilitiesOnSwitchIn(true) if megaevolved.include?(i.index)
  2938.       end
  2939.     end
  2940.     # Call at Pokémon
  2941.     for i in priority
  2942.       if @choices[i.index][0]==4 && !i.effects[PBEffects::SkipTurn]
  2943.         pbCall(i.index)
  2944.       end
  2945.     end
  2946.     # Switch out Pokémon
  2947.     @switching=true
  2948.     switched=[]
  2949.     for i in priority
  2950.       if @choices[i.index][0]==2 && !i.effects[PBEffects::SkipTurn]
  2951.         index=@choices[i.index][1] # party position of Pokémon to switch to
  2952.         newpokename=index
  2953.         if isConst?(pbParty(i.index)[index].ability,PBAbilities,:ILLUSION)
  2954.           newpokename=pbGetLastPokeInTeam(i.index)
  2955.         end
  2956.         self.lastMoveUser=i.index
  2957.         if !pbOwnedByPlayer?(i.index)
  2958.           owner=pbGetOwner(i.index)
  2959.           pbDisplayBrief(_INTL("{1} withdrew {2}!",owner.fullname,i.name))
  2960.           PBDebug.log("[Withdrew Pokémon] Opponent withdrew #{i.pbThis(true)}")
  2961.         else
  2962.           pbDisplayBrief(_INTL("{1}, that's enough!\r\nCome back!",i.name))
  2963.           PBDebug.log("[Withdrew Pokémon] Player withdrew #{i.pbThis(true)}")
  2964.         end
  2965.         for j in priority
  2966.           next if !i.pbIsOpposing?(j.index)
  2967.           # if Pursuit and this target ("i") was chosen
  2968.           if pbChoseMoveFunctionCode?(j.index,0x88) && # Pursuit
  2969.              !j.hasMovedThisRound?
  2970.             if j.status!=PBStatuses::SLEEP && j.status!=PBStatuses::FROZEN &&
  2971.                !j.effects[PBEffects::SkyDrop] &&
  2972.                (!j.hasWorkingAbility(:TRUANT) || !j.effects[PBEffects::Truant])
  2973.               @choices[j.index][3]=i.index # Make sure to target the switching Pokémon
  2974.               j.pbUseMove(@choices[j.index]) # This calls pbGainEXP as appropriate
  2975.               j.effects[PBEffects::Pursuit]=true
  2976.               @switching=false
  2977.               return if @decision>0
  2978.             end
  2979.           end
  2980.           break if i.fainted?
  2981.         end
  2982.         if !pbRecallAndReplace(i.index,index,newpokename)
  2983.           # If a forced switch somehow occurs here in single battles
  2984.           # the attack phase now ends
  2985.           if !@doublebattle
  2986.             @switching=false
  2987.             return
  2988.           end
  2989.         else
  2990.           switched.push(i.index)
  2991.         end
  2992.       end
  2993.     end
  2994.     if switched.length>0
  2995.       for i in priority
  2996.         i.pbAbilitiesOnSwitchIn(true) if switched.include?(i.index)
  2997.       end
  2998.     end
  2999.     @switching=false
  3000.     # Use items
  3001.     for i in priority
  3002.       if @choices[i.index][0]==3 && !i.effects[PBEffects::SkipTurn]
  3003.         if pbIsOpposing?(i.index)
  3004.           # Opponent use item
  3005.           pbEnemyUseItem(@choices[i.index][1],i)
  3006.         else
  3007.           # Player use item
  3008.           item=@choices[i.index][1]
  3009.           if item>0
  3010.             usetype=$ItemData[item][ITEMBATTLEUSE]
  3011.             if usetype==1 || usetype==3
  3012.               if @choices[i.index][2]>=0
  3013.                 pbUseItemOnPokemon(item,@choices[i.index][2],i,@scene)
  3014.               end
  3015.             elsif usetype==2 || usetype==4
  3016.               if !ItemHandlers.hasUseInBattle(item) # Poké Ball/Poké Doll used already
  3017.                 pbUseItemOnBattler(item,@choices[i.index][2],i,@scene)
  3018.               end
  3019.             end
  3020.           end
  3021.         end
  3022.       end
  3023.     end
  3024.     # Use attacks
  3025.     for i in priority
  3026.       next if i.effects[PBEffects::SkipTurn]
  3027.       if pbChoseMoveFunctionCode?(i.index,0x115) # Focus Punch
  3028.         pbCommonAnimation("FocusPunch",i,nil)
  3029.         pbDisplay(_INTL("{1} is tightening its focus!",i.pbThis))
  3030.       end
  3031.     end
  3032.     10.times do
  3033.       # Forced to go next
  3034.       advance=false
  3035.       for i in priority
  3036.         next if !i.effects[PBEffects::MoveNext]
  3037.         next if i.hasMovedThisRound? || i.effects[PBEffects::SkipTurn]
  3038.         advance=i.pbProcessTurn(@choices[i.index])
  3039.         break if advance
  3040.       end
  3041.       return if @decision>0
  3042.       next if advance
  3043.       # Regular priority order
  3044.       for i in priority
  3045.         next if i.effects[PBEffects::Quash]
  3046.         next if i.hasMovedThisRound? || i.effects[PBEffects::SkipTurn]
  3047.         advance=i.pbProcessTurn(@choices[i.index])
  3048.         break if advance
  3049.       end
  3050.       return if @decision>0
  3051.       next if advance
  3052.       # Quashed
  3053.       for i in priority
  3054.         next if !i.effects[PBEffects::Quash]
  3055.         next if i.hasMovedThisRound? || i.effects[PBEffects::SkipTurn]
  3056.         advance=i.pbProcessTurn(@choices[i.index])
  3057.         break if advance
  3058.       end
  3059.       return if @decision>0
  3060.       next if advance
  3061.       # Check for all done
  3062.       for i in priority
  3063.         advance=true if @choices[i.index][0]==1 && !i.hasMovedThisRound? &&
  3064.                         !i.effects[PBEffects::SkipTurn]
  3065.         break if advance
  3066.       end
  3067.       next if advance
  3068.       break
  3069.     end
  3070.     10.times do
  3071.       @scene.pbGraphicsUpdate
  3072.       @scene.pbInputUpdate
  3073.       @scene.pbFrameUpdate
  3074.     end
  3075.   end
  3076. ################################################################################
  3077. # End of round.
  3078. ################################################################################
  3079.   def pbEndOfRoundPhase
  3080.     PBDebug.log("[End of round]")
  3081.     for i in 0...4
  3082.       @battlers[i].effects[PBEffects::Electrify]=false
  3083.       @battlers[i].effects[PBEffects::Endure]=false
  3084.       @battlers[i].effects[PBEffects::FirstPledge]=0
  3085.       @battlers[i].effects[PBEffects::HyperBeam]-=1 if @battlers[i].effects[PBEffects::HyperBeam]>0
  3086.       @battlers[i].effects[PBEffects::KingsShield]=false
  3087.       @battlers[i].effects[PBEffects::LifeOrb]=false
  3088.       @battlers[i].effects[PBEffects::MoveNext]=false
  3089.       @battlers[i].effects[PBEffects::Powder]=false
  3090.       @battlers[i].effects[PBEffects::Protect]=false
  3091.       @battlers[i].effects[PBEffects::ProtectNegation]=false
  3092.       @battlers[i].effects[PBEffects::Quash]=false
  3093.       @battlers[i].effects[PBEffects::Roost]=false
  3094.       @battlers[i].effects[PBEffects::SpikyShield]=false
  3095.     end
  3096.     @usepriority=false  # recalculate priority
  3097.     priority=pbPriority(true) # Ignoring Quick Claw here
  3098.     # Weather
  3099.     case @weather
  3100.     when PBWeather::SUNNYDAY
  3101.       @weatherduration=@weatherduration-1 if @weatherduration>0
  3102.       if @weatherduration==0
  3103.         pbDisplay(_INTL("The sunlight faded."))
  3104.         @weather=0
  3105.         PBDebug.log("[End of effect] Sunlight weather ended")
  3106.       else
  3107.         pbCommonAnimation("Sunny",nil,nil)
  3108. #        pbDisplay(_INTL("The sunlight is strong."))
  3109.         if pbWeather==PBWeather::SUNNYDAY
  3110.           for i in priority
  3111.             if i.hasWorkingAbility(:SOLARPOWER)
  3112.               PBDebug.log("[Ability triggered] #{i.pbThis}'s Solar Power")
  3113.               @scene.pbDamageAnimation(i,0)
  3114.               i.pbReduceHP((i.totalhp/8).floor)
  3115.               pbDisplay(_INTL("{1} was hurt by the sunlight!",i.pbThis))
  3116.               if i.fainted?
  3117.                 return if !i.pbFaint
  3118.               end
  3119.             end
  3120.           end
  3121.         end
  3122.       end
  3123.     when PBWeather::RAINDANCE
  3124.       @weatherduration=@weatherduration-1 if @weatherduration>0
  3125.       if @weatherduration==0
  3126.         pbDisplay(_INTL("The rain stopped."))
  3127.         @weather=0
  3128.         PBDebug.log("[End of effect] Rain weather ended")
  3129.       else
  3130.         pbCommonAnimation("Rain",nil,nil)
  3131. #        pbDisplay(_INTL("Rain continues to fall."))
  3132.       end
  3133.     when PBWeather::SANDSTORM
  3134.       @weatherduration=@weatherduration-1 if @weatherduration>0
  3135.       if @weatherduration==0
  3136.         pbDisplay(_INTL("The sandstorm subsided."))
  3137.         @weather=0
  3138.         PBDebug.log("[End of effect] Sandstorm weather ended")
  3139.       else
  3140.         pbCommonAnimation("Sandstorm",nil,nil)
  3141. #        pbDisplay(_INTL("The sandstorm rages."))
  3142.         if pbWeather==PBWeather::SANDSTORM
  3143.           PBDebug.log("[Lingering effect triggered] Sandstorm weather damage")
  3144.           for i in priority
  3145.             next if i.fainted?
  3146.             if !i.pbHasType?(:GROUND) && !i.pbHasType?(:ROCK) && !i.pbHasType?(:STEEL) &&
  3147.                !i.hasWorkingAbility(:SANDVEIL) &&
  3148.                !i.hasWorkingAbility(:SANDRUSH) &&
  3149.                !i.hasWorkingAbility(:SANDFORCE) &&
  3150.                !i.hasWorkingAbility(:MAGICGUARD) &&
  3151.                !i.hasWorkingAbility(:OVERCOAT) &&
  3152.                !i.hasWorkingItem(:SAFETYGOGGLES) &&
  3153.                ![0xCA,0xCB].include?(PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function) # Dig, Dive
  3154.               @scene.pbDamageAnimation(i,0)
  3155.               i.pbReduceHP((i.totalhp/16).floor)
  3156.               pbDisplay(_INTL("{1} is buffeted by the sandstorm!",i.pbThis))
  3157.               if i.fainted?
  3158.                 return if !i.pbFaint
  3159.               end
  3160.             end
  3161.           end
  3162.         end
  3163.       end
  3164.     when PBWeather::HAIL
  3165.       @weatherduration=@weatherduration-1 if @weatherduration>0
  3166.       if @weatherduration==0
  3167.         pbDisplay(_INTL("The hail stopped."))
  3168.         @weather=0
  3169.         PBDebug.log("[End of effect] Hail weather ended")
  3170.       else
  3171.         pbCommonAnimation("Hail",nil,nil)
  3172. #        pbDisplay(_INTL("Hail continues to fall."))
  3173.         if pbWeather==PBWeather::HAIL
  3174.           PBDebug.log("[Lingering effect triggered] Hail weather damage")
  3175.           for i in priority
  3176.             next if i.fainted?
  3177.             if !i.pbHasType?(:ICE) &&
  3178.                !i.hasWorkingAbility(:ICEBODY) &&
  3179.                !i.hasWorkingAbility(:SNOWCLOAK) &&
  3180.                !i.hasWorkingAbility(:MAGICGUARD) &&
  3181.                !i.hasWorkingAbility(:OVERCOAT) &&
  3182.                !i.hasWorkingItem(:SAFETYGOGGLES) &&
  3183.                ![0xCA,0xCB].include?(PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function) # Dig, Dive
  3184.               @scene.pbDamageAnimation(i,0)
  3185.               i.pbReduceHP((i.totalhp/16).floor)
  3186.               pbDisplay(_INTL("{1} is buffeted by the hail!",i.pbThis))
  3187.               if i.fainted?
  3188.                 return if !i.pbFaint
  3189.               end
  3190.             end
  3191.           end
  3192.         end
  3193.       end
  3194.     when PBWeather::HEAVYRAIN
  3195.       hasabil=false
  3196.       for i in 0...4
  3197.         if isConst?(@battlers[i].ability,PBAbilities,:PRIMORDIALSEA) && !@battlers[i].fainted?
  3198.           hasabil=true; break
  3199.         end
  3200.       end
  3201.       @weatherduration=0 if !hasabil
  3202.       if @weatherduration==0
  3203.         pbDisplay(_INTL("The heavy rain stopped."))
  3204.         @weather=0
  3205.         PBDebug.log("[End of effect] Primordial Sea's rain weather ended")
  3206.       else
  3207.         pbCommonAnimation("HeavyRain",nil,nil)
  3208. #        pbDisplay(_INTL("It is raining heavily."))
  3209.       end
  3210.     when PBWeather::HARSHSUN
  3211.       hasabil=false
  3212.       for i in 0...4
  3213.         if isConst?(@battlers[i].ability,PBAbilities,:DESOLATELAND) && !@battlers[i].fainted?
  3214.           hasabil=true; break
  3215.         end
  3216.       end
  3217.       @weatherduration=0 if !hasabil
  3218.       if @weatherduration==0
  3219.         pbDisplay(_INTL("The harsh sunlight faded."))
  3220.         @weather=0
  3221.         PBDebug.log("[End of effect] Desolate Land's sunlight weather ended")
  3222.       else
  3223.         pbCommonAnimation("HarshSun",nil,nil)
  3224. #        pbDisplay(_INTL("The sunlight is extremely harsh."))
  3225.         if pbWeather==PBWeather::HARSHSUN
  3226.           for i in priority
  3227.             if i.hasWorkingAbility(:SOLARPOWER)
  3228.               PBDebug.log("[Ability triggered] #{i.pbThis}'s Solar Power")
  3229.               @scene.pbDamageAnimation(i,0)
  3230.               i.pbReduceHP((i.totalhp/8).floor)
  3231.               pbDisplay(_INTL("{1} was hurt by the sunlight!",i.pbThis))
  3232.               if i.fainted?
  3233.                 return if !i.pbFaint
  3234.               end
  3235.             end
  3236.           end
  3237.         end
  3238.       end
  3239.     when PBWeather::STRONGWINDS
  3240.       hasabil=false
  3241.       for i in 0...4
  3242.         if isConst?(@battlers[i].ability,PBAbilities,:DELTASTREAM) && !@battlers[i].fainted?
  3243.           hasabil=true; break
  3244.         end
  3245.       end
  3246.       @weatherduration=0 if !hasabil
  3247.       if @weatherduration==0
  3248.         pbDisplay(_INTL("The air current subsided."))
  3249.         @weather=0
  3250.         PBDebug.log("[End of effect] Delta Stream's wind weather ended")
  3251.       else
  3252.         pbCommonAnimation("StrongWinds",nil,nil)
  3253. #        pbDisplay(_INTL("The wind is strong."))
  3254.       end
  3255.     end
  3256.     # Shadow Sky weather
  3257.     if isConst?(@weather,PBWeather,:SHADOWSKY)
  3258.       @weatherduration=@weatherduration-1 if @weatherduration>0
  3259.       if @weatherduration==0
  3260.         pbDisplay(_INTL("The shadow sky faded."))
  3261.         @weather=0
  3262.         PBDebug.log("[End of effect] Shadow Sky weather ended")
  3263.       else
  3264.         pbCommonAnimation("ShadowSky",nil,nil)
  3265. #        pbDisplay(_INTL("The shadow sky continues."));
  3266.         if isConst?(pbWeather,PBWeather,:SHADOWSKY)
  3267.           PBDebug.log("[Lingering effect triggered] Shadow Sky weather damage")
  3268.           for i in priority
  3269.             next if i.fainted?
  3270.             if !i.isShadow?
  3271.               @scene.pbDamageAnimation(i,0)
  3272.               i.pbReduceHP((i.totalhp/16).floor)
  3273.               pbDisplay(_INTL("{1} was hurt by the shadow sky!",i.pbThis))
  3274.               if i.fainted?
  3275.                 return if !i.pbFaint
  3276.               end
  3277.             end
  3278.           end
  3279.         end
  3280.       end
  3281.     end
  3282.     # Future Sight/Doom Desire
  3283.     for i in battlers   # not priority
  3284.       next if i.fainted?
  3285.       if i.effects[PBEffects::FutureSight]>0
  3286.         i.effects[PBEffects::FutureSight]-=1
  3287.         if i.effects[PBEffects::FutureSight]==0
  3288.           move=i.effects[PBEffects::FutureSightMove]
  3289.           PBDebug.log("[Lingering effect triggered] #{PBMoves.getName(move)} struck #{i.pbThis(true)}")
  3290.           pbDisplay(_INTL("{1} took the {2} attack!",i.pbThis,PBMoves.getName(move)))
  3291.           moveuser=nil
  3292.           for j in battlers
  3293.             next if j.pbIsOpposing?(i.effects[PBEffects::FutureSightUserPos])
  3294.             if j.pokemonIndex==i.effects[PBEffects::FutureSightUser] && !j.fainted?
  3295.               moveuser=j; break
  3296.             end
  3297.           end
  3298.           if !moveuser
  3299.             party=pbParty(i.effects[PBEffects::FutureSightUserPos])
  3300.             if party[i.effects[PBEffects::FutureSightUser]].hp>0
  3301.               moveuser=PokeBattle_Battler.new(self,i.effects[PBEffects::FutureSightUserPos])
  3302.               moveuser.pbInitDummyPokemon(party[i.effects[PBEffects::FutureSightUser]],
  3303.                                           i.effects[PBEffects::FutureSightUser])
  3304.             end
  3305.           end
  3306.           if !moveuser
  3307.             pbDisplay(_INTL("But it failed!"))
  3308.           else
  3309.             @futuresight=true
  3310.             moveuser.pbUseMoveSimple(move,-1,i.index)
  3311.             @futuresight=false
  3312.           end
  3313.           i.effects[PBEffects::FutureSight]=0
  3314.           i.effects[PBEffects::FutureSightMove]=0
  3315.           i.effects[PBEffects::FutureSightUser]=-1
  3316.           i.effects[PBEffects::FutureSightUserPos]=-1
  3317.           if i.fainted?
  3318.             return if !i.pbFaint
  3319.             next
  3320.           end
  3321.         end
  3322.       end
  3323.     end
  3324.     for i in priority
  3325.       next if i.fainted?
  3326.       # Rain Dish
  3327.       if i.hasWorkingAbility(:RAINDISH) && i.effects[PBEffects::HealBlock]==0 &&
  3328.          (pbWeather==PBWeather::RAINDANCE ||
  3329.          pbWeather==PBWeather::HEAVYRAIN)
  3330.         PBDebug.log("[Ability triggered] #{i.pbThis}'s Rain Dish")
  3331.         hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  3332.         pbDisplay(_INTL("{1}'s {2} restored its HP a little!",i.pbThis,PBAbilities.getName(i.ability))) if hpgain>0
  3333.       end
  3334.       # Dry Skin
  3335.       if i.hasWorkingAbility(:DRYSKIN)
  3336.         if (pbWeather==PBWeather::RAINDANCE || pbWeather==PBWeather::HEAVYRAIN) &&
  3337.            i.effects[PBEffects::HealBlock]==0
  3338.           PBDebug.log("[Ability triggered] #{i.pbThis}'s Dry Skin (in rain)")
  3339.           hpgain=i.pbRecoverHP((i.totalhp/8).floor,true)
  3340.           pbDisplay(_INTL("{1}'s {2} was healed by the rain!",i.pbThis,PBAbilities.getName(i.ability))) if hpgain>0
  3341.         elsif pbWeather==PBWeather::SUNNYDAY ||
  3342.               pbWeather==PBWeather::HARSHSUN
  3343.           PBDebug.log("[Ability triggered] #{i.pbThis}'s Dry Skin (in sun)")
  3344.           @scene.pbDamageAnimation(i,0)
  3345.           hploss=i.pbReduceHP((i.totalhp/8).floor)
  3346.           pbDisplay(_INTL("{1}'s {2} was hurt by the sunlight!",i.pbThis,PBAbilities.getName(i.ability))) if hploss>0
  3347.         end
  3348.       end
  3349.       # Ice Body
  3350.       if i.hasWorkingAbility(:ICEBODY) && pbWeather==PBWeather::HAIL &&
  3351.          i.effects[PBEffects::HealBlock]==0
  3352.         PBDebug.log("[Ability triggered] #{i.pbThis}'s Ice Body")
  3353.         hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  3354.         pbDisplay(_INTL("{1}'s {2} restored its HP a little!",i.pbThis,PBAbilities.getName(i.ability))) if hpgain>0
  3355.       end
  3356.       if i.fainted?
  3357.         return if !i.pbFaint
  3358.       end
  3359.     end
  3360.     # Wish
  3361.     for i in priority
  3362.       next if i.fainted?
  3363.       if i.effects[PBEffects::Wish]>0
  3364.         i.effects[PBEffects::Wish]-=1
  3365.         if i.effects[PBEffects::Wish]==0
  3366.           PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Wish")
  3367.           hpgain=i.pbRecoverHP(i.effects[PBEffects::WishAmount],true)
  3368.           if hpgain>0
  3369.             wishmaker=pbThisEx(i.index,i.effects[PBEffects::WishMaker])
  3370.             pbDisplay(_INTL("{1}'s wish came true!",wishmaker))
  3371.           end
  3372.         end
  3373.       end
  3374.     end
  3375.     # Fire Pledge + Grass Pledge combination damage
  3376.     for i in 0...2
  3377.       if sides[i].effects[PBEffects::SeaOfFire]>0 &&
  3378.          pbWeather!=PBWeather::RAINDANCE &&
  3379.          pbWeather!=PBWeather::HEAVYRAIN
  3380.         @battle.pbCommonAnimation("SeaOfFire",nil,nil) if i==0
  3381.         @battle.pbCommonAnimation("SeaOfFireOpp",nil,nil) if i==1
  3382.         for j in priority
  3383.           next if (j.index&1)!=i
  3384.           next if j.pbHasType?(:FIRE) || j.hasWorkingAbility(:MAGICGUARD)
  3385.           @scene.pbDamageAnimation(j,0)
  3386.           hploss=j.pbReduceHP((j.totalhp/8).floor)
  3387.           pbDisplay(_INTL("{1} is hurt by the sea of fire!",j.pbThis)) if hploss>0
  3388.           if j.fainted?
  3389.             return if !j.pbFaint
  3390.           end
  3391.         end
  3392.       end
  3393.     end
  3394.     for i in priority
  3395.       next if i.fainted?
  3396.       # Shed Skin, Hydration
  3397.       if (i.hasWorkingAbility(:SHEDSKIN) && pbRandom(10)<3) ||
  3398.          (i.hasWorkingAbility(:HYDRATION) && (pbWeather==PBWeather::RAINDANCE ||
  3399.                                               pbWeather==PBWeather::HEAVYRAIN))
  3400.         if i.status>0
  3401.           PBDebug.log("[Ability triggered] #{i.pbThis}'s #{PBAbilities.getName(i.ability)}")
  3402.           s=i.status
  3403.           i.pbCureStatus(false)
  3404.           case s
  3405.           when PBStatuses::SLEEP
  3406.             pbDisplay(_INTL("{1}'s {2} cured its sleep problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3407.           when PBStatuses::POISON
  3408.             pbDisplay(_INTL("{1}'s {2} cured its poison problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3409.           when PBStatuses::BURN
  3410.             pbDisplay(_INTL("{1}'s {2} healed its burn!",i.pbThis,PBAbilities.getName(i.ability)))
  3411.           when PBStatuses::PARALYSIS
  3412.             pbDisplay(_INTL("{1}'s {2} cured its paralysis!",i.pbThis,PBAbilities.getName(i.ability)))
  3413.           when PBStatuses::FROZEN
  3414.             pbDisplay(_INTL("{1}'s {2} thawed it out!",i.pbThis,PBAbilities.getName(i.ability)))
  3415.           end
  3416.         end
  3417.       end
  3418.       # Healer
  3419.       if i.hasWorkingAbility(:HEALER) && pbRandom(10)<3
  3420.         partner=i.pbPartner
  3421.         if partner && partner.status>0
  3422.           PBDebug.log("[Ability triggered] #{i.pbThis}'s #{PBAbilities.getName(i.ability)}")
  3423.           s=partner.status
  3424.           partner.pbCureStatus(false)
  3425.           case s
  3426.           when PBStatuses::SLEEP
  3427.             pbDisplay(_INTL("{1}'s {2} cured its partner's sleep problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3428.           when PBStatuses::POISON
  3429.             pbDisplay(_INTL("{1}'s {2} cured its partner's poison problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3430.           when PBStatuses::BURN
  3431.             pbDisplay(_INTL("{1}'s {2} healed its partner's burn!",i.pbThis,PBAbilities.getName(i.ability)))
  3432.           when PBStatuses::PARALYSIS
  3433.             pbDisplay(_INTL("{1}'s {2} cured its partner's paralysis!",i.pbThis,PBAbilities.getName(i.ability)))
  3434.           when PBStatuses::FROZEN
  3435.             pbDisplay(_INTL("{1}'s {2} thawed its partner out!",i.pbThis,PBAbilities.getName(i.ability)))
  3436.           end
  3437.         end
  3438.       end
  3439.     end
  3440.     for i in priority
  3441.       next if i.fainted?
  3442.       # Grassy Terrain (healing)
  3443.       if @field.effects[PBEffects::GrassyTerrain]>0 && !i.isAirborne?
  3444.         if i.effects[PBEffects::HealBlock]==0
  3445.           hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  3446.           pbDisplay(_INTL("{1}'s HP was restored.",i.pbThis)) if hpgain>0
  3447.         end
  3448.       end
  3449.       # Held berries/Leftovers/Black Sludge
  3450.       i.pbBerryCureCheck(true)
  3451.       if i.fainted?
  3452.         return if !i.pbFaint
  3453.       end
  3454.     end
  3455.     # Aqua Ring
  3456.     for i in priority
  3457.       next if i.fainted?
  3458.       if i.effects[PBEffects::AquaRing]
  3459.         PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Aqua Ring")
  3460.         hpgain=(i.totalhp/16).floor
  3461.         hpgain=(hpgain*1.3).floor if i.hasWorkingItem(:BIGROOT)
  3462.         hpgain=i.pbRecoverHP(hpgain,true)
  3463.         pbDisplay(_INTL("Aqua Ring restored {1}'s HP!",i.pbThis)) if hpgain>0
  3464.       end
  3465.     end
  3466.     # Ingrain
  3467.     for i in priority
  3468.       next if i.fainted?
  3469.       if i.effects[PBEffects::Ingrain]
  3470.         PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Ingrain")
  3471.         hpgain=(i.totalhp/16).floor
  3472.         hpgain=(hpgain*1.3).floor if i.hasWorkingItem(:BIGROOT)
  3473.         hpgain=i.pbRecoverHP(hpgain,true)
  3474.         pbDisplay(_INTL("{1} absorbed nutrients with its roots!",i.pbThis)) if hpgain>0
  3475.       end
  3476.     end
  3477.     # Leech Seed
  3478.     for i in priority
  3479.       next if i.fainted?
  3480.       if i.effects[PBEffects::LeechSeed]>=0 && !i.hasWorkingAbility(:MAGICGUARD)
  3481.         recipient=@battlers[i.effects[PBEffects::LeechSeed]]
  3482.         if recipient && !recipient.fainted?
  3483.           PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Leech Seed")
  3484.           pbCommonAnimation("LeechSeed",recipient,i)
  3485.           hploss=i.pbReduceHP((i.totalhp/8).floor,true)
  3486.           if i.hasWorkingAbility(:LIQUIDOOZE)
  3487.             recipient.pbReduceHP(hploss,true)
  3488.             pbDisplay(_INTL("{1} sucked up the liquid ooze!",recipient.pbThis))
  3489.           else
  3490.             if recipient.effects[PBEffects::HealBlock]==0
  3491.               hploss=(hploss*1.3).floor if recipient.hasWorkingItem(:BIGROOT)
  3492.               recipient.pbRecoverHP(hploss,true)
  3493.             end
  3494.             pbDisplay(_INTL("{1}'s health was sapped by Leech Seed!",i.pbThis))
  3495.           end
  3496.           if i.fainted?
  3497.             return if !i.pbFaint
  3498.           end
  3499.           if recipient.fainted?
  3500.             return if !recipient.pbFaint
  3501.           end
  3502.         end
  3503.       end
  3504.     end
  3505.     for i in priority
  3506.       next if i.fainted?
  3507.       # Poison/Bad poison
  3508.       if i.status==PBStatuses::POISON
  3509.         if i.statusCount>0
  3510.           i.effects[PBEffects::Toxic]+=1
  3511.           i.effects[PBEffects::Toxic]=[15,i.effects[PBEffects::Toxic]].min
  3512.         end
  3513.         if i.hasWorkingAbility(:POISONHEAL)
  3514.           pbCommonAnimation("Poison",i,nil)
  3515.           if i.effects[PBEffects::HealBlock]==0 && i.hp<i.totalhp
  3516.             PBDebug.log("[Ability triggered] #{i.pbThis}'s Poison Heal")
  3517.             i.pbRecoverHP((i.totalhp/8).floor,true)
  3518.             pbDisplay(_INTL("{1} is healed by poison!",i.pbThis))
  3519.           end
  3520.         else
  3521.           if !i.hasWorkingAbility(:MAGICGUARD)
  3522.             PBDebug.log("[Status damage] #{i.pbThis} took damage from poison/toxic")
  3523.             if i.statusCount==0
  3524.               i.pbReduceHP((i.totalhp/8).floor)
  3525.             else
  3526.               i.pbReduceHP(((i.totalhp*i.effects[PBEffects::Toxic])/16).floor)
  3527.             end
  3528.             i.pbContinueStatus
  3529.           end
  3530.         end
  3531.       end
  3532.       # Burn
  3533.       if i.status==PBStatuses::BURN
  3534.         if !i.hasWorkingAbility(:MAGICGUARD)
  3535.           PBDebug.log("[Status damage] #{i.pbThis} took damage from burn")
  3536.           if i.hasWorkingAbility(:HEATPROOF)
  3537.             PBDebug.log("[Ability triggered] #{i.pbThis}'s Heatproof")
  3538.             i.pbReduceHP((i.totalhp/16).floor)
  3539.           else
  3540.             i.pbReduceHP((i.totalhp/8).floor)
  3541.           end
  3542.         end
  3543.         i.pbContinueStatus
  3544.       end
  3545.       # Nightmare
  3546.       if i.effects[PBEffects::Nightmare]
  3547.         if i.status==PBStatuses::SLEEP
  3548.           if !i.hasWorkingAbility(:MAGICGUARD)
  3549.             PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s nightmare")
  3550.             i.pbReduceHP((i.totalhp/4).floor,true)
  3551.             pbDisplay(_INTL("{1} is locked in a nightmare!",i.pbThis))
  3552.           end
  3553.         else
  3554.           i.effects[PBEffects::Nightmare]=false
  3555.         end
  3556.       end
  3557.       if i.fainted?
  3558.         return if !i.pbFaint
  3559.         next
  3560.       end
  3561.     end
  3562.     # Curse
  3563.     for i in priority
  3564.       next if i.fainted?
  3565.       if i.effects[PBEffects::Curse] && !i.hasWorkingAbility(:MAGICGUARD)
  3566.         PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s curse")
  3567.         i.pbReduceHP((i.totalhp/4).floor,true)
  3568.         pbDisplay(_INTL("{1} is afflicted by the curse!",i.pbThis))
  3569.       end
  3570.       if i.fainted?
  3571.         return if !i.pbFaint
  3572.         next
  3573.       end
  3574.     end
  3575.     # Multi-turn attacks (Bind/Clamp/Fire Spin/Magma Storm/Sand Tomb/Whirlpool/Wrap)
  3576.     for i in priority
  3577.       next if i.fainted?
  3578.       if i.effects[PBEffects::MultiTurn]>0
  3579.         i.effects[PBEffects::MultiTurn]-=1
  3580.         movename=PBMoves.getName(i.effects[PBEffects::MultiTurnAttack])
  3581.         if i.effects[PBEffects::MultiTurn]==0
  3582.           PBDebug.log("[End of effect] Trapping move #{movename} affecting #{i.pbThis} ended")
  3583.           pbDisplay(_INTL("{1} was freed from {2}!",i.pbThis,movename))
  3584.         else
  3585.           if isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:BIND)
  3586.             pbCommonAnimation("Bind",i,nil)
  3587.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:CLAMP)
  3588.             pbCommonAnimation("Clamp",i,nil)
  3589.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:FIRESPIN)
  3590.             pbCommonAnimation("FireSpin",i,nil)
  3591.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:MAGMASTORM)
  3592.             pbCommonAnimation("MagmaStorm",i,nil)
  3593.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:SANDTOMB)
  3594.             pbCommonAnimation("SandTomb",i,nil)
  3595.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:WRAP)
  3596.             pbCommonAnimation("Wrap",i,nil)
  3597.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:INFESTATION)
  3598.             pbCommonAnimation("Infestation",i,nil)
  3599.           else
  3600.             pbCommonAnimation("Wrap",i,nil)
  3601.           end
  3602.           if !i.hasWorkingAbility(:MAGICGUARD)
  3603.             PBDebug.log("[Lingering effect triggered] #{i.pbThis} took damage from trapping move #{movename}")
  3604.             @scene.pbDamageAnimation(i,0)
  3605.             amt=(USENEWBATTLEMECHANICS) ? (i.totalhp/8).floor : (i.totalhp/16).floor
  3606.             if @battlers[i.effects[PBEffects::MultiTurnUser]].hasWorkingItem(:BINDINGBAND)
  3607.               amt=(USENEWBATTLEMECHANICS) ? (i.totalhp/6).floor : (i.totalhp/8).floor
  3608.             end
  3609.             i.pbReduceHP(amt)
  3610.             pbDisplay(_INTL("{1} is hurt by {2}!",i.pbThis,movename))
  3611.           end
  3612.         end
  3613.       end  
  3614.       if i.fainted?
  3615.         return if !i.pbFaint
  3616.       end
  3617.     end
  3618.     # Taunt
  3619.     for i in priority
  3620.       next if i.fainted?
  3621.       if i.effects[PBEffects::Taunt]>0
  3622.         i.effects[PBEffects::Taunt]-=1
  3623.         if i.effects[PBEffects::Taunt]==0
  3624.           pbDisplay(_INTL("{1}'s taunt wore off!",i.pbThis))
  3625.           PBDebug.log("[End of effect] #{i.pbThis} is no longer taunted")
  3626.         end
  3627.       end
  3628.     end
  3629.     # Encore
  3630.     for i in priority
  3631.       next if i.fainted?
  3632.       if i.effects[PBEffects::Encore]>0
  3633.         if i.moves[i.effects[PBEffects::EncoreIndex]].id!=i.effects[PBEffects::EncoreMove]
  3634.           i.effects[PBEffects::Encore]=0
  3635.           i.effects[PBEffects::EncoreIndex]=0
  3636.           i.effects[PBEffects::EncoreMove]=0
  3637.           PBDebug.log("[End of effect] #{i.pbThis} is no longer encored (encored move was lost)")
  3638.         else
  3639.           i.effects[PBEffects::Encore]-=1
  3640.           if i.effects[PBEffects::Encore]==0 || i.moves[i.effects[PBEffects::EncoreIndex]].pp==0
  3641.             i.effects[PBEffects::Encore]=0
  3642.             pbDisplay(_INTL("{1}'s encore ended!",i.pbThis))
  3643.             PBDebug.log("[End of effect] #{i.pbThis} is no longer encored")
  3644.           end
  3645.         end
  3646.       end
  3647.     end
  3648.     # Disable/Cursed Body
  3649.     for i in priority
  3650.       next if i.fainted?
  3651.       if i.effects[PBEffects::Disable]>0
  3652.         i.effects[PBEffects::Disable]-=1
  3653.         if i.effects[PBEffects::Disable]==0
  3654.           i.effects[PBEffects::DisableMove]=0
  3655.           pbDisplay(_INTL("{1} is no longer disabled!",i.pbThis))
  3656.           PBDebug.log("[End of effect] #{i.pbThis} is no longer disabled")
  3657.         end
  3658.       end
  3659.     end
  3660.     # Magnet Rise
  3661.     for i in priority
  3662.       next if i.fainted?
  3663.       if i.effects[PBEffects::MagnetRise]>0
  3664.         i.effects[PBEffects::MagnetRise]-=1
  3665.         if i.effects[PBEffects::MagnetRise]==0
  3666.           pbDisplay(_INTL("{1} stopped levitating.",i.pbThis))
  3667.           PBDebug.log("[End of effect] #{i.pbThis} is no longer levitating by Magnet Rise")
  3668.         end
  3669.       end
  3670.     end
  3671.     # Telekinesis
  3672.     for i in priority
  3673.       next if i.fainted?
  3674.       if i.effects[PBEffects::Telekinesis]>0
  3675.         i.effects[PBEffects::Telekinesis]-=1
  3676.         if i.effects[PBEffects::Telekinesis]==0
  3677.           pbDisplay(_INTL("{1} stopped levitating.",i.pbThis))
  3678.           PBDebug.log("[End of effect] #{i.pbThis} is no longer levitating by Telekinesis")
  3679.         end
  3680.       end
  3681.     end
  3682.     # Heal Block
  3683.     for i in priority
  3684.       next if i.fainted?
  3685.       if i.effects[PBEffects::HealBlock]>0
  3686.         i.effects[PBEffects::HealBlock]-=1
  3687.         if i.effects[PBEffects::HealBlock]==0
  3688.           pbDisplay(_INTL("{1}'s Heal Block wore off!",i.pbThis))
  3689.           PBDebug.log("[End of effect] #{i.pbThis} is no longer Heal Blocked")
  3690.         end
  3691.       end
  3692.     end
  3693.     # Embargo
  3694.     for i in priority
  3695.       next if i.fainted?
  3696.       if i.effects[PBEffects::Embargo]>0
  3697.         i.effects[PBEffects::Embargo]-=1
  3698.         if i.effects[PBEffects::Embargo]==0
  3699.           pbDisplay(_INTL("{1} can use items again!",i.pbThis(true)))
  3700.           PBDebug.log("[End of effect] #{i.pbThis} is no longer affected by an embargo")
  3701.         end
  3702.       end
  3703.     end
  3704.     # Yawn
  3705.     for i in priority
  3706.       next if i.fainted?
  3707.       if i.effects[PBEffects::Yawn]>0
  3708.         i.effects[PBEffects::Yawn]-=1
  3709.         if i.effects[PBEffects::Yawn]==0 && i.pbCanSleepYawn?
  3710.           PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Yawn")
  3711.           i.pbSleep
  3712.           i.pbBerryCureCheck
  3713.         end
  3714.       end
  3715.     end
  3716.     # Perish Song
  3717.     perishSongUsers=[]
  3718.     for i in priority
  3719.       next if i.fainted?
  3720.       if i.effects[PBEffects::PerishSong]>0
  3721.         i.effects[PBEffects::PerishSong]-=1
  3722.         pbDisplay(_INTL("{1}'s perish count fell to {2}!",i.pbThis,i.effects[PBEffects::PerishSong]))
  3723.         PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Perish Song count dropped to #{i.effects[PBEffects::PerishSong]}")
  3724.         if i.effects[PBEffects::PerishSong]==0
  3725.           perishSongUsers.push(i.effects[PBEffects::PerishSongUser])
  3726.           i.pbReduceHP(i.hp,true)
  3727.         end
  3728.       end
  3729.       if i.fainted?
  3730.         return if !i.pbFaint
  3731.       end
  3732.     end
  3733.     if perishSongUsers.length>0
  3734.       # If all remaining Pokemon fainted by a Perish Song triggered by a single side
  3735.       if (perishSongUsers.find_all{|item| pbIsOpposing?(item) }.length==perishSongUsers.length) ||
  3736.          (perishSongUsers.find_all{|item| !pbIsOpposing?(item) }.length==perishSongUsers.length)
  3737.         pbJudgeCheckpoint(@battlers[perishSongUsers[0]])
  3738.       end
  3739.     end
  3740.     if @decision>0
  3741.       pbGainEXP
  3742.       return
  3743.     end
  3744.     # Reflect
  3745.     for i in 0...2
  3746.       if sides[i].effects[PBEffects::Reflect]>0
  3747.         sides[i].effects[PBEffects::Reflect]-=1
  3748.         if sides[i].effects[PBEffects::Reflect]==0
  3749.           pbDisplay(_INTL("Your team's Reflect faded!")) if i==0
  3750.           pbDisplay(_INTL("The opposing team's Reflect faded!")) if i==1
  3751.           PBDebug.log("[End of effect] Reflect ended on the player's side") if i==0
  3752.           PBDebug.log("[End of effect] Reflect ended on the opponent's side") if i==1
  3753.         end
  3754.       end
  3755.     end
  3756.     # Light Screen
  3757.     for i in 0...2
  3758.       if sides[i].effects[PBEffects::LightScreen]>0
  3759.         sides[i].effects[PBEffects::LightScreen]-=1
  3760.         if sides[i].effects[PBEffects::LightScreen]==0
  3761.           pbDisplay(_INTL("Your team's Light Screen faded!")) if i==0
  3762.           pbDisplay(_INTL("The opposing team's Light Screen faded!")) if i==1
  3763.           PBDebug.log("[End of effect] Light Screen ended on the player's side") if i==0
  3764.           PBDebug.log("[End of effect] Light Screen ended on the opponent's side") if i==1
  3765.         end
  3766.       end
  3767.     end
  3768.     # Safeguard
  3769.     for i in 0...2
  3770.       if sides[i].effects[PBEffects::Safeguard]>0
  3771.         sides[i].effects[PBEffects::Safeguard]-=1
  3772.         if sides[i].effects[PBEffects::Safeguard]==0
  3773.           pbDisplay(_INTL("Your team is no longer protected by Safeguard!")) if i==0
  3774.           pbDisplay(_INTL("The opposing team is no longer protected by Safeguard!")) if i==1
  3775.           PBDebug.log("[End of effect] Safeguard ended on the player's side") if i==0
  3776.           PBDebug.log("[End of effect] Safeguard ended on the opponent's side") if i==1
  3777.         end
  3778.       end
  3779.     end
  3780.     # Mist
  3781.     for i in 0...2
  3782.       if sides[i].effects[PBEffects::Mist]>0
  3783.         sides[i].effects[PBEffects::Mist]-=1
  3784.         if sides[i].effects[PBEffects::Mist]==0
  3785.           pbDisplay(_INTL("Your team's Mist faded!")) if i==0
  3786.           pbDisplay(_INTL("The opposing team's Mist faded!")) if i==1
  3787.           PBDebug.log("[End of effect] Mist ended on the player's side") if i==0
  3788.           PBDebug.log("[End of effect] Mist ended on the opponent's side") if i==1
  3789.         end
  3790.       end
  3791.     end
  3792.     # Tailwind
  3793.     for i in 0...2
  3794.       if sides[i].effects[PBEffects::Tailwind]>0
  3795.         sides[i].effects[PBEffects::Tailwind]-=1
  3796.         if sides[i].effects[PBEffects::Tailwind]==0
  3797.           pbDisplay(_INTL("Your team's Tailwind petered out!")) if i==0
  3798.           pbDisplay(_INTL("The opposing team's Tailwind petered out!")) if i==1
  3799.           PBDebug.log("[End of effect] Tailwind ended on the player's side") if i==0
  3800.           PBDebug.log("[End of effect] Tailwind ended on the opponent's side") if i==1
  3801.         end
  3802.       end
  3803.     end
  3804.     # Lucky Chant
  3805.     for i in 0...2
  3806.       if sides[i].effects[PBEffects::LuckyChant]>0
  3807.         sides[i].effects[PBEffects::LuckyChant]-=1
  3808.         if sides[i].effects[PBEffects::LuckyChant]==0
  3809.           pbDisplay(_INTL("Your team's Lucky Chant faded!")) if i==0
  3810.           pbDisplay(_INTL("The opposing team's Lucky Chant faded!")) if i==1
  3811.           PBDebug.log("[End of effect] Lucky Chant ended on the player's side") if i==0
  3812.           PBDebug.log("[End of effect] Lucky Chant ended on the opponent's side") if i==1
  3813.         end
  3814.       end
  3815.     end
  3816.     # End of Pledge move combinations
  3817.     for i in 0...2
  3818.       if sides[i].effects[PBEffects::Swamp]>0
  3819.         sides[i].effects[PBEffects::Swamp]-=1
  3820.         if sides[i].effects[PBEffects::Swamp]==0
  3821.           pbDisplay(_INTL("The swamp around your team disappeared!")) if i==0
  3822.           pbDisplay(_INTL("The swamp around the opposing team disappeared!")) if i==1
  3823.           PBDebug.log("[End of effect] Grass Pledge's swamp ended on the player's side") if i==0
  3824.           PBDebug.log("[End of effect] Grass Pledge's swamp ended on the opponent's side") if i==1
  3825.         end
  3826.       end
  3827.       if sides[i].effects[PBEffects::SeaOfFire]>0
  3828.         sides[i].effects[PBEffects::SeaOfFire]-=1
  3829.         if sides[i].effects[PBEffects::SeaOfFire]==0
  3830.           pbDisplay(_INTL("The sea of fire around your team disappeared!")) if i==0
  3831.           pbDisplay(_INTL("The sea of fire around the opposing team disappeared!")) if i==1
  3832.           PBDebug.log("[End of effect] Fire Pledge's sea of fire ended on the player's side") if i==0
  3833.           PBDebug.log("[End of effect] Fire Pledge's sea of fire ended on the opponent's side") if i==1
  3834.         end
  3835.       end
  3836.       if sides[i].effects[PBEffects::Rainbow]>0
  3837.         sides[i].effects[PBEffects::Rainbow]-=1
  3838.         if sides[i].effects[PBEffects::Rainbow]==0
  3839.           pbDisplay(_INTL("The rainbow around your team disappeared!")) if i==0
  3840.           pbDisplay(_INTL("The rainbow around the opposing team disappeared!")) if i==1
  3841.           PBDebug.log("[End of effect] Water Pledge's rainbow ended on the player's side") if i==0
  3842.           PBDebug.log("[End of effect] Water Pledge's rainbow ended on the opponent's side") if i==1
  3843.         end
  3844.       end
  3845.     end
  3846.     # Gravity
  3847.     if @field.effects[PBEffects::Gravity]>0
  3848.       @field.effects[PBEffects::Gravity]-=1
  3849.       if @field.effects[PBEffects::Gravity]==0
  3850.         pbDisplay(_INTL("Gravity returned to normal."))
  3851.         PBDebug.log("[End of effect] Strong gravity ended")
  3852.       end
  3853.     end
  3854.     # Trick Room
  3855.     if @field.effects[PBEffects::TrickRoom]>0
  3856.       @field.effects[PBEffects::TrickRoom]-=1
  3857.       if @field.effects[PBEffects::TrickRoom]==0
  3858.         pbDisplay(_INTL("The twisted dimensions returned to normal."))
  3859.         PBDebug.log("[End of effect] Trick Room ended")
  3860.       end
  3861.     end
  3862.     # Wonder Room
  3863.     if @field.effects[PBEffects::WonderRoom]>0
  3864.       @field.effects[PBEffects::WonderRoom]-=1
  3865.       if @field.effects[PBEffects::WonderRoom]==0
  3866.         pbDisplay(_INTL("Wonder Room wore off, and the Defense and Sp. Def stats returned to normal!"))
  3867.         PBDebug.log("[End of effect] Wonder Room ended")
  3868.       end
  3869.     end
  3870.     # Magic Room
  3871.     if @field.effects[PBEffects::MagicRoom]>0
  3872.       @field.effects[PBEffects::MagicRoom]-=1
  3873.       if @field.effects[PBEffects::MagicRoom]==0
  3874.         pbDisplay(_INTL("The area returned to normal."))
  3875.         PBDebug.log("[End of effect] Magic Room ended")
  3876.       end
  3877.     end
  3878.     # Mud Sport
  3879.     if @field.effects[PBEffects::MudSportField]>0
  3880.       @field.effects[PBEffects::MudSportField]-=1
  3881.       if @field.effects[PBEffects::MudSportField]==0
  3882.         pbDisplay(_INTL("The effects of Mud Sport have faded."))
  3883.         PBDebug.log("[End of effect] Mud Sport ended")
  3884.       end
  3885.     end
  3886.     # Water Sport
  3887.     if @field.effects[PBEffects::WaterSportField]>0
  3888.       @field.effects[PBEffects::WaterSportField]-=1
  3889.       if @field.effects[PBEffects::WaterSportField]==0
  3890.         pbDisplay(_INTL("The effects of Water Sport have faded."))
  3891.         PBDebug.log("[End of effect] Water Sport ended")
  3892.       end
  3893.     end
  3894.     # Electric Terrain
  3895.     if @field.effects[PBEffects::ElectricTerrain]>0
  3896.       @field.effects[PBEffects::ElectricTerrain]-=1
  3897.       if @field.effects[PBEffects::ElectricTerrain]==0
  3898.         pbDisplay(_INTL("The electric current disappeared from the battlefield."))
  3899.         PBDebug.log("[End of effect] Electric Terrain ended")
  3900.       end
  3901.     end
  3902.     # Grassy Terrain (counting down)
  3903.     if @field.effects[PBEffects::GrassyTerrain]>0
  3904.       @field.effects[PBEffects::GrassyTerrain]-=1
  3905.       if @field.effects[PBEffects::GrassyTerrain]==0
  3906.         pbDisplay(_INTL("The grass disappeared from the battlefield."))
  3907.         PBDebug.log("[End of effect] Grassy Terrain ended")
  3908.       end
  3909.     end
  3910.     # Misty Terrain
  3911.     if @field.effects[PBEffects::MistyTerrain]>0
  3912.       @field.effects[PBEffects::MistyTerrain]-=1
  3913.       if @field.effects[PBEffects::MistyTerrain]==0
  3914.         pbDisplay(_INTL("The mist disappeared from the battlefield."))
  3915.         PBDebug.log("[End of effect] Misty Terrain ended")
  3916.       end
  3917.     end
  3918.     # Uproar
  3919.     for i in priority
  3920.       next if i.fainted?
  3921.       if i.effects[PBEffects::Uproar]>0
  3922.         for j in priority
  3923.           if !j.fainted? && j.status==PBStatuses::SLEEP && !j.hasWorkingAbility(:SOUNDPROOF)
  3924.             PBDebug.log("[Lingering effect triggered] Uproar woke up #{j.pbThis(true)}")
  3925.             j.pbCureStatus(false)
  3926.             pbDisplay(_INTL("{1} woke up in the uproar!",j.pbThis))
  3927.           end
  3928.         end
  3929.         i.effects[PBEffects::Uproar]-=1
  3930.         if i.effects[PBEffects::Uproar]==0
  3931.           pbDisplay(_INTL("{1} calmed down.",i.pbThis))
  3932.           PBDebug.log("[End of effect] #{i.pbThis} is no longer uproaring")
  3933.         else
  3934.           pbDisplay(_INTL("{1} is making an uproar!",i.pbThis))
  3935.         end
  3936.       end
  3937.     end
  3938.     for i in priority
  3939.       next if i.fainted?
  3940.       # Speed Boost
  3941.       # A Pokémon's turncount is 0 if it became active after the beginning of a round
  3942.       if i.turncount>0 && i.hasWorkingAbility(:SPEEDBOOST)
  3943.         if i.pbIncreaseStatWithCause(PBStats::SPEED,1,i,PBAbilities.getName(i.ability))
  3944.           PBDebug.log("[Ability triggered] #{i.pbThis}'s #{PBAbilities.getName(i.ability)}")
  3945.         end
  3946.       end
  3947.       # Bad Dreams
  3948.       if i.status==PBStatuses::SLEEP && !i.hasWorkingAbility(:MAGICGUARD)
  3949.         if i.pbOpposing1.hasWorkingAbility(:BADDREAMS) ||
  3950.            i.pbOpposing2.hasWorkingAbility(:BADDREAMS)
  3951.           PBDebug.log("[Ability triggered] #{i.pbThis}'s opponent's Bad Dreams")
  3952.           hploss=i.pbReduceHP((i.totalhp/8).floor,true)
  3953.           pbDisplay(_INTL("{1} is having a bad dream!",i.pbThis)) if hploss>0
  3954.         end
  3955.       end
  3956.       if i.fainted?
  3957.         return if !i.pbFaint
  3958.         next
  3959.       end
  3960.       # Pickup
  3961.       if i.hasWorkingAbility(:PICKUP) && i.item<=0
  3962.         item=0; index=-1; use=0
  3963.         for j in 0...4
  3964.           next if j==i.index
  3965.           if @battlers[j].effects[PBEffects::PickupUse]>use
  3966.             item=@battlers[j].effects[PBEffects::PickupItem]
  3967.             index=j
  3968.             use=@battlers[j].effects[PBEffects::PickupUse]
  3969.           end
  3970.         end
  3971.         if item>0
  3972.           i.item=item
  3973.           @battlers[index].effects[PBEffects::PickupItem]=0
  3974.           @battlers[index].effects[PBEffects::PickupUse]=0
  3975.           @battlers[index].pokemon.itemRecycle=0 if @battlers[index].pokemon.itemRecycle==item
  3976.           if !@opponent && # In a wild battle
  3977.              i.pokemon.itemInitial==0 &&
  3978.              @battlers[index].pokemon.itemInitial==item
  3979.             i.pokemon.itemInitial=item
  3980.             @battlers[index].pokemon.itemInitial=0
  3981.           end
  3982.           pbDisplay(_INTL("{1} found one {2}!",i.pbThis,PBItems.getName(item)))
  3983.           i.pbBerryCureCheck(true)
  3984.         end
  3985.       end
  3986.       # Harvest
  3987.       if i.hasWorkingAbility(:HARVEST) && i.item<=0 && i.pokemon.itemRecycle>0
  3988.         if pbIsBerry?(i.pokemon.itemRecycle) &&
  3989.            (pbWeather==PBWeather::SUNNYDAY ||
  3990.            pbWeather==PBWeather::HARSHSUN || pbRandom(10)<5)
  3991.           i.item=i.pokemon.itemRecycle
  3992.           i.pokemon.itemRecycle=0
  3993.           i.pokemon.itemInitial=i.item if i.pokemon.itemInitial==0
  3994.           pbDisplay(_INTL("{1} harvested one {2}!",i.pbThis,PBItems.getName(i.item)))
  3995.           i.pbBerryCureCheck(true)
  3996.         end
  3997.       end
  3998.       # Moody
  3999.       if i.hasWorkingAbility(:MOODY)
  4000.         randomup=[]; randomdown=[]
  4001.         for j in [PBStats::ATTACK,PBStats::DEFENSE,PBStats::SPEED,PBStats::SPATK,
  4002.                   PBStats::SPDEF,PBStats::ACCURACY,PBStats::EVASION]
  4003.           randomup.push(j) if i.pbCanIncreaseStatStage?(j,i)
  4004.           randomdown.push(j) if i.pbCanReduceStatStage?(j,i)
  4005.         end
  4006.         if randomup.length>0
  4007.           PBDebug.log("[Ability triggered] #{i.pbThis}'s Moody (raise stat)")
  4008.           r=pbRandom(randomup.length)
  4009.           i.pbIncreaseStatWithCause(randomup[r],2,i,PBAbilities.getName(i.ability))
  4010.           for j in 0...randomdown.length
  4011.             if randomdown[j]==randomup[r]
  4012.               randomdown[j]=nil; randomdown.compact!
  4013.               break
  4014.             end
  4015.           end
  4016.         end
  4017.         if randomdown.length>0
  4018.           PBDebug.log("[Ability triggered] #{i.pbThis}'s Moody (lower stat)")
  4019.           r=pbRandom(randomdown.length)
  4020.           i.pbReduceStatWithCause(randomdown[r],1,i,PBAbilities.getName(i.ability))
  4021.         end
  4022.       end
  4023.     end
  4024.     for i in priority
  4025.       next if i.fainted?
  4026.       # Toxic Orb
  4027.       if i.hasWorkingItem(:TOXICORB) && i.status==0 && i.pbCanPoison?(nil,false)
  4028.         PBDebug.log("[Item triggered] #{i.pbThis}'s Toxic Orb")
  4029.         i.pbPoison(nil,_INTL("{1} was badly poisoned by its {2}!",i.pbThis,
  4030.            PBItems.getName(i.item)),true)
  4031.       end
  4032.       # Flame Orb
  4033.       if i.hasWorkingItem(:FLAMEORB) && i.status==0 && i.pbCanBurn?(nil,false)
  4034.         PBDebug.log("[Item triggered] #{i.pbThis}'s Flame Orb")
  4035.         i.pbBurn(nil,_INTL("{1} was burned by its {2}!",i.pbThis,PBItems.getName(i.item)))
  4036.       end
  4037.       # Sticky Barb
  4038.       if i.hasWorkingItem(:STICKYBARB) && !i.hasWorkingAbility(:MAGICGUARD)
  4039.         PBDebug.log("[Item triggered] #{i.pbThis}'s Sticky Barb")
  4040.         @scene.pbDamageAnimation(i,0)
  4041.         i.pbReduceHP((i.totalhp/8).floor)
  4042.         pbDisplay(_INTL("{1} is hurt by its {2}!",i.pbThis,PBItems.getName(i.item)))
  4043.       end
  4044.       if i.fainted?
  4045.         return if !i.pbFaint
  4046.       end
  4047.     end
  4048.     # Slow Start's end message
  4049.     if i.hasWorkingAbility(:SLOWSTART) && i.turncount==6
  4050.       pbDisplay(_INTL("{1} finally got its act together!",i.pbThis))
  4051.     end
  4052.     # Form checks
  4053.     for i in 0...4
  4054.       next if @battlers[i].fainted?
  4055.       @battlers[i].pbCheckForm
  4056.     end
  4057.     pbGainEXP
  4058.     pbSwitch
  4059.     return if @decision>0
  4060.     for i in priority
  4061.       next if i.fainted?
  4062.       i.pbAbilitiesOnSwitchIn(false)
  4063.     end
  4064.     # Healing Wish/Lunar Dance - should go here
  4065.     # Spikes/Toxic Spikes/Stealth Rock - should go here (in order of their 1st use)
  4066.     for i in 0...4
  4067.       if @battlers[i].turncount>0 && @battlers[i].hasWorkingAbility(:TRUANT)
  4068.         @battlers[i].effects[PBEffects::Truant]=!@battlers[i].effects[PBEffects::Truant]
  4069.       end
  4070.       if @battlers[i].effects[PBEffects::LockOn]>0   # Also Mind Reader
  4071.         @battlers[i].effects[PBEffects::LockOn]-=1
  4072.         @battlers[i].effects[PBEffects::LockOnPos]=-1 if @battlers[i].effects[PBEffects::LockOn]==0
  4073.       end
  4074.       @battlers[i].effects[PBEffects::Flinch]=false
  4075.       @battlers[i].effects[PBEffects::FollowMe]=0
  4076.       @battlers[i].effects[PBEffects::HelpingHand]=false
  4077.       @battlers[i].effects[PBEffects::MagicCoat]=false
  4078.       @battlers[i].effects[PBEffects::Snatch]=false
  4079.       @battlers[i].effects[PBEffects::Charge]-=1 if @battlers[i].effects[PBEffects::Charge]>0
  4080.       @battlers[i].lastHPLost=0
  4081.       @battlers[i].tookDamage=false
  4082.       @battlers[i].lastAttacker.clear
  4083.       @battlers[i].effects[PBEffects::Counter]=-1
  4084.       @battlers[i].effects[PBEffects::CounterTarget]=-1
  4085.       @battlers[i].effects[PBEffects::MirrorCoat]=-1
  4086.       @battlers[i].effects[PBEffects::MirrorCoatTarget]=-1
  4087.     end
  4088.     for i in 0...2
  4089.       if !@sides[i].effects[PBEffects::EchoedVoiceUsed]
  4090.         @sides[i].effects[PBEffects::EchoedVoiceCounter]=0
  4091.       end
  4092.       @sides[i].effects[PBEffects::EchoedVoiceUsed]=false
  4093.       @sides[i].effects[PBEffects::MatBlock]= false
  4094.       @sides[i].effects[PBEffects::QuickGuard]=false
  4095.       @sides[i].effects[PBEffects::WideGuard]=false
  4096.       @sides[i].effects[PBEffects::CraftyShield]=false
  4097.       @sides[i].effects[PBEffects::Round]=0
  4098.     end
  4099.     @field.effects[PBEffects::FusionBolt]=false
  4100.     @field.effects[PBEffects::FusionFlare]=false
  4101.     @field.effects[PBEffects::IonDeluge]=false
  4102.     @field.effects[PBEffects::FairyLock]-=1 if @field.effects[PBEffects::FairyLock]>0
  4103.     # invalidate stored priority
  4104.     @usepriority=false
  4105.   end
  4106. ################################################################################
  4107. # End of battle.
  4108. ################################################################################
  4109.   def pbEndOfBattle(canlose=false)
  4110.     case @decision
  4111.     ##### WIN #####
  4112.     when 1
  4113.       PBDebug.log("")
  4114.       PBDebug.log("***Player won***")
  4115.       if @opponent
  4116.         @scene.pbTrainerBattleSuccess
  4117.         if @opponent.is_a?(Array)
  4118.           pbDisplayPaused(_INTL("{1} defeated {2} and {3}!",self.pbPlayer.name,@opponent[0].fullname,@opponent[1].fullname))
  4119.         else
  4120.           pbDisplayPaused(_INTL("{1} defeated\r\n{2}!",self.pbPlayer.name,@opponent.fullname))
  4121.         end
  4122.         @scene.pbShowOpponent(0)
  4123.         pbDisplayPaused(@endspeech.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4124.         if @opponent.is_a?(Array)
  4125.           @scene.pbHideOpponent
  4126.           @scene.pbShowOpponent(1)
  4127.           pbDisplayPaused(@endspeech2.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4128.         end
  4129.         # Calculate money gained for winning
  4130.         if @internalbattle
  4131.           tmoney=0
  4132.           if @opponent.is_a?(Array)   # Double battles
  4133.             maxlevel1=0; maxlevel2=0; limit=pbSecondPartyBegin(1)
  4134.             for i in 0...limit
  4135.               if @party2[i]
  4136.                 maxlevel1=@party2[i].level if maxlevel1<@party2[i].level
  4137.               end
  4138.               if @party2[i+limit]
  4139.                 maxlevel2=@party2[i+limit].level if maxlevel1<@party2[i+limit].level
  4140.               end
  4141.             end
  4142.             tmoney+=maxlevel1*@opponent[0].moneyEarned
  4143.             tmoney+=maxlevel2*@opponent[1].moneyEarned
  4144.           else
  4145.             maxlevel=0
  4146.             for i in @party2
  4147.               next if !i
  4148.               maxlevel=i.level if maxlevel<i.level
  4149.             end
  4150.             tmoney+=maxlevel*@opponent.moneyEarned
  4151.           end
  4152.           # If Amulet Coin/Luck Incense's effect applies, double money earned
  4153.           tmoney*=2 if @amuletcoin
  4154.           # If Happy Hour's effect applies, double money earned
  4155.           tmoney*=2 if @doublemoney
  4156.           oldmoney=self.pbPlayer.money
  4157.           self.pbPlayer.money+=tmoney
  4158.           moneygained=self.pbPlayer.money-oldmoney
  4159.           if moneygained>0
  4160.             pbDisplayPaused(_INTL("{1} got ${2}\r\nfor winning!",self.pbPlayer.name,pbCommaNumber(tmoney)))
  4161.           end
  4162.         end
  4163.       end
  4164.       if @internalbattle && @extramoney>0
  4165.         @extramoney*=2 if @amuletcoin
  4166.         @extramoney*=2 if @doublemoney
  4167.         oldmoney=self.pbPlayer.money
  4168.         self.pbPlayer.money+=@extramoney
  4169.         moneygained=self.pbPlayer.money-oldmoney
  4170.         if moneygained>0
  4171.           pbDisplayPaused(_INTL("{1} picked up ${2}!",self.pbPlayer.name,pbCommaNumber(@extramoney)))
  4172.         end
  4173.       end
  4174.       for pkmn in @snaggedpokemon
  4175.         pbStorePokemon(pkmn)
  4176.         self.pbPlayer.shadowcaught=[] if !self.pbPlayer.shadowcaught
  4177.         self.pbPlayer.shadowcaught[pkmn.species]=true
  4178.       end
  4179.       @snaggedpokemon.clear
  4180.     ##### LOSE, DRAW #####
  4181.     when 2, 5
  4182.       PBDebug.log("")
  4183.       PBDebug.log("***Player lost***") if @decision==2
  4184.       PBDebug.log("***Player drew with opponent***") if @decision==5
  4185.       if @internalbattle
  4186.         moneylost=pbMaxLevelFromIndex(0)   # Player's Pokémon only, not partner's
  4187.         multiplier=[8,16,24,36,48,64,80,100,120]
  4188.         moneylost*=multiplier[[multiplier.length-1,self.pbPlayer.numbadges].min]
  4189.         moneylost=self.pbPlayer.money if moneylost>self.pbPlayer.money
  4190.         moneylost=0 if $game_switches[NO_MONEY_LOSS]
  4191.         oldmoney=self.pbPlayer.money
  4192.         self.pbPlayer.money-=moneylost
  4193.         lostmoney=oldmoney-self.pbPlayer.money
  4194.         if @opponent
  4195.           if @opponent.is_a?(Array)
  4196.             pbDisplayPaused(_INTL("You lost against {1} and {2}!",@opponent[0].fullname,@opponent[1].fullname))
  4197.           else
  4198.             pbDisplayPaused(_INTL("You lost against\r\n{1}!",@opponent.fullname))
  4199.           end
  4200.           if moneylost>0
  4201.             pbDisplayPaused(_INTL("You gave ${1} to the winner...",pbCommaNumber(lostmoney)))  
  4202.           end
  4203.         else
  4204.           pbDisplayPaused(_INTL("You have no more Pokémon that can fight!"))
  4205.           if moneylost>0
  4206.             pbDisplayPaused(_INTL("You panicked and dropped\r\n${1}...",pbCommaNumber(lostmoney)))
  4207.           end
  4208.         end
  4209.         pbDisplayPaused(_INTL("You blacked out!")) if !canlose
  4210.       elsif @decision==2
  4211.         @scene.pbShowOpponent(0)
  4212.         pbDisplayPaused(@endspeechwin.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4213.         if @opponent.is_a?(Array)
  4214.           @scene.pbHideOpponent
  4215.           @scene.pbShowOpponent(1)
  4216.           pbDisplayPaused(@endspeechwin2.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4217.         end
  4218.       end
  4219.     end
  4220.     # Pass on Pokérus within the party
  4221.     infected=[]
  4222.     for i in 0...$Trainer.party.length
  4223.       if $Trainer.party[i].pokerusStage==1
  4224.         infected.push(i)
  4225.       end
  4226.     end
  4227.     if infected.length>=1
  4228.       for i in infected
  4229.         strain=$Trainer.party[i].pokerusStrain
  4230.         if i>0 && $Trainer.party[i-1].pokerusStage==0
  4231.           $Trainer.party[i-1].givePokerus(strain) if rand(3)==0
  4232.         end
  4233.         if i<$Trainer.party.length-1 && $Trainer.party[i+1].pokerusStage==0
  4234.           $Trainer.party[i+1].givePokerus(strain) if rand(3)==0
  4235.         end
  4236.       end
  4237.     end
  4238.     @scene.pbEndBattle(@decision)
  4239.     for i in @battlers
  4240.       i.pbResetForm
  4241.       if i.hasWorkingAbility(:NATURALCURE)
  4242.         i.status=0
  4243.       end
  4244.     end
  4245.     for i in $Trainer.party
  4246.       i.setItem(i.itemInitial)
  4247.       i.itemInitial=i.itemRecycle=0
  4248.       i.belch=false
  4249.     end
  4250.     return @decision
  4251.   end
  4252. end
  4253. ################################################################################
  4254. # Pokemon Storing PokeBattle_BattlePeer                                        #
  4255. ################################################################################
  4256. class PokeBattle_NullBattlePeer
  4257.   def pbOnEnteringBattle(battle,pokemon)
  4258.   end
  4259.   def pbStorePokemon(player,pokemon)
  4260.   if $Trainer.partyplus>=1
  4261.     if player.party.length<$Trainer.partyplus
  4262.       player.party[player.party.length]=pokemon
  4263.     end
  4264.   else
  4265.     if player.party.length<6
  4266.       player.party[player.party.length]=pokemon
  4267.     end
  4268.   end
  4269.     return -1
  4270.   end
  4271.   def pbGetStorageCreator()
  4272.     return nil
  4273.   end
  4274.   def pbCurrentBox()
  4275.     return -1
  4276.   end
  4277.   def pbBoxName(box)
  4278.     return ""
  4279.   end
  4280. end
  4281. class PokeBattle_RealBattlePeer
  4282.   def pbStorePokemon(player,pokemon)
  4283.   if $Trainer.partyplus>=1
  4284.     if player.party.length<$Trainer.partyplus
  4285.       player.party[player.party.length]=pokemon
  4286.       return -1
  4287.     else
  4288.       pokemon.heal
  4289.       oldcurbox=$PokemonStorage.currentBox
  4290.       storedbox=$PokemonStorage.pbStoreCaught(pokemon)
  4291.       if storedbox<0
  4292.         pbDisplayPaused(_INTL("Can't catch any more..."))
  4293.         return oldcurbox
  4294.       else
  4295.         return storedbox
  4296.       end
  4297.     end
  4298.   else
  4299.     if player.party.length<6
  4300.       player.party[player.party.length]=pokemon
  4301.       return -1
  4302.     else
  4303.       pokemon.heal
  4304.       oldcurbox=$PokemonStorage.currentBox
  4305.       storedbox=$PokemonStorage.pbStoreCaught(pokemon)
  4306.       if storedbox<0
  4307.         pbDisplayPaused(_INTL("Can't catch any more..."))
  4308.         return oldcurbox
  4309.       else
  4310.         return storedbox
  4311.       end
  4312.     end
  4313.   end
  4314.   end
  4315.   def pbGetStorageCreator()
  4316.     creator=nil
  4317.     if $PokemonGlobal && $PokemonGlobal.seenStorageCreator
  4318.       creator=Kernel.pbGetStorageCreator
  4319.     end
  4320.     return creator
  4321.   end
  4322.   def pbCurrentBox()
  4323.     return $PokemonStorage.currentBox
  4324.   end
  4325.   def pbBoxName(box)
  4326.    return box<0 ? "" : $PokemonStorage[box].name
  4327.   end
  4328. end
  4329. class PokeBattle_BattlePeer
  4330.   def self.create
  4331.     return PokeBattle_RealBattlePeer.new
  4332.   end
  4333. end
  4334. ################################################################################
  4335. # Daycare Methods PField_DayCare                                               #
  4336. ################################################################################
  4337. def pbDayCareWithdraw(index)
  4338.  if $Trainer.partyplus>=1
  4339.   if !$PokemonGlobal.daycare[index][0]
  4340.     raise _INTL("There's no Pokémon here...")
  4341.   elsif $Trainer.party.length>=$Trainer.partyplus
  4342.     raise _INTL("Can't store the Pokémon...")
  4343.   else
  4344.     $Trainer.party[$Trainer.party.length]=$PokemonGlobal.daycare[index][0]
  4345.     $PokemonGlobal.daycare[index][0]=nil
  4346.     $PokemonGlobal.daycare[index][1]=0
  4347.     $PokemonGlobal.daycareEgg=0
  4348.   end  
  4349.  else
  4350.   if !$PokemonGlobal.daycare[index][0]
  4351.     raise _INTL("There's no Pokémon here...")
  4352.   elsif $Trainer.party.length>=6
  4353.     raise _INTL("Can't store the Pokémon...")
  4354.   else
  4355.     $Trainer.party[$Trainer.party.length]=$PokemonGlobal.daycare[index][0]
  4356.     $PokemonGlobal.daycare[index][0]=nil
  4357.     $PokemonGlobal.daycare[index][1]=0
  4358.     $PokemonGlobal.daycareEgg=0
  4359.   end  
  4360.  end
  4361. end
  4362. def pbDayCareGenerateEgg
  4363.   return if pbDayCareDeposited!=2
  4364.   if $Trainer.partyplus>=1
  4365.   if $Trainer.party.length>=$Trainer.partyplus
  4366.     raise _INTL("Can't store the egg")
  4367.   end
  4368.   else
  4369.   if $Trainer.party.length>=6
  4370.     raise _INTL("Can't store the egg")
  4371.   end
  4372.   end
  4373.   pokemon0=$PokemonGlobal.daycare[0][0]
  4374.   pokemon1=$PokemonGlobal.daycare[1][0]
  4375.   mother=nil
  4376.   father=nil
  4377.   babyspecies=0
  4378.   ditto0=pbIsDitto?(pokemon0)
  4379.   ditto1=pbIsDitto?(pokemon1)
  4380.   if pokemon0.isFemale? || ditto0
  4381.     babyspecies=(ditto0) ? pokemon1.species : pokemon0.species
  4382.     mother=pokemon0
  4383.     father=pokemon1
  4384.   else
  4385.     babyspecies=(ditto1) ? pokemon0.species : pokemon1.species
  4386.     mother=pokemon1
  4387.     father=pokemon0
  4388.   end
  4389.   babyspecies=pbGetBabySpecies(babyspecies,mother.item,father.item)
  4390.   if isConst?(babyspecies,PBSpecies,:MANAPHY) && hasConst?(PBSpecies,:PHIONE)
  4391.     babyspecies=getConst(PBSpecies,:PHIONE)
  4392.   elsif (isConst?(babyspecies,PBSpecies,:NIDORANfE) && hasConst?(PBSpecies,:NIDORANmA)) ||
  4393.         (isConst?(babyspecies,PBSpecies,:NIDORANmA) && hasConst?(PBSpecies,:NIDORANfE))
  4394.     babyspecies=[getConst(PBSpecies,:NIDORANmA),
  4395.                  getConst(PBSpecies,:NIDORANfE)][rand(2)]
  4396.   elsif (isConst?(babyspecies,PBSpecies,:VOLBEAT) && hasConst?(PBSpecies,:ILLUMISE)) ||
  4397.         (isConst?(babyspecies,PBSpecies,:ILLUMISE) && hasConst?(PBSpecies,:VOLBEAT))
  4398.     babyspecies=[getConst(PBSpecies,:VOLBEAT),
  4399.                  getConst(PBSpecies,:ILLUMISE)][rand(2)]
  4400.   end
  4401.   # Generate egg
  4402.   egg=PokeBattle_Pokemon.new(babyspecies,EGGINITIALLEVEL,$Trainer)
  4403.   # Randomise personal ID
  4404.   pid=rand(65536)
  4405.   pid|=(rand(65536)<<16)
  4406.   egg.personalID=pid
  4407.   # Inheriting form
  4408.   if isConst?(babyspecies,PBSpecies,:BURMY) ||
  4409.      isConst?(babyspecies,PBSpecies,:SHELLOS) ||
  4410.      isConst?(babyspecies,PBSpecies,:BASCULIN) ||
  4411.      isConst?(babyspecies,PBSpecies,:FLABEBE) ||
  4412.      isConst?(babyspecies,PBSpecies,:PUMPKABOO) ||
  4413.      isConst?(babyspecies,PBSpecies,:ORICORIO) ||
  4414.      isConst?(babyspecies,PBSpecies,:MINIOR)
  4415.     egg.form=mother.form
  4416.   end
  4417.   # Inheriting Moves
  4418.   moves=[]
  4419.   othermoves=[]
  4420.   movefather=father; movemother=mother
  4421.   if pbIsDitto?(movefather) && !mother.isFemale?
  4422.     movefather=mother; movemother=father
  4423.   end
  4424.   # Initial Moves
  4425.   initialmoves=egg.getMoveList
  4426.   for k in initialmoves
  4427.     if k[0]<=EGGINITIALLEVEL
  4428.       moves.push(k[1])
  4429.     else
  4430.       othermoves.push(k[1]) if mother.hasMove?(k[1]) && father.hasMove?(k[1])
  4431.     end
  4432.   end
  4433.   # Inheriting Natural Moves
  4434.   for move in othermoves
  4435.     moves.push(move)
  4436.   end
  4437.   # Inheriting Machine Moves
  4438.   if !USENEWBATTLEMECHANICS
  4439.     for i in 0...$ItemData.length
  4440.       next if !$ItemData[i]
  4441.       atk=$ItemData[i][ITEMMACHINE]
  4442.       next if !atk || atk==0
  4443.       if egg.isCompatibleWithMove?(atk)
  4444.         moves.push(atk) if movefather.hasMove?(atk)
  4445.       end
  4446.     end
  4447.   end
  4448.   # Inheriting Egg Moves
  4449.   if movefather.isMale?
  4450.     pbRgssOpen("Data/eggEmerald.dat","rb"){|f|
  4451.        f.pos=(egg.fSpecies-1)*8
  4452.        offset=f.fgetdw
  4453.        length=f.fgetdw
  4454.        if length>0
  4455.          f.pos=offset
  4456.          i=0; loop do break unless i<length
  4457.            atk=f.fgetw
  4458.            moves.push(atk) if movefather.hasMove?(atk)
  4459.            i+=1
  4460.          end
  4461.        end
  4462.     }
  4463.   end
  4464.   if USENEWBATTLEMECHANICS
  4465.     pbRgssOpen("Data/eggEmerald.dat","rb"){|f|
  4466.        f.pos=(egg.fSpecies-1)*8
  4467.        offset=f.fgetdw
  4468.        length=f.fgetdw
  4469.        if length>0
  4470.          f.pos=offset
  4471.          i=0; loop do break unless i<length
  4472.            atk=f.fgetw
  4473.            moves.push(atk) if movemother.hasMove?(atk)
  4474.            i+=1
  4475.          end
  4476.        end
  4477.     }
  4478.   end
  4479.   # Volt Tackle
  4480.   lightball=false
  4481.   if (isConst?(father.species,PBSpecies,:PIKACHU) ||
  4482.       isConst?(father.species,PBSpecies,:RAICHU)) &&
  4483.       isConst?(father.item,PBItems,:LIGHTBALL)
  4484.     lightball=true
  4485.   end
  4486.   if (isConst?(mother.species,PBSpecies,:PIKACHU) ||
  4487.       isConst?(mother.species,PBSpecies,:RAICHU)) &&
  4488.       isConst?(mother.item,PBItems,:LIGHTBALL)
  4489.     lightball=true
  4490.   end
  4491.   if lightball && isConst?(babyspecies,PBSpecies,:PICHU) &&
  4492.      hasConst?(PBMoves,:VOLTTACKLE)
  4493.     moves.push(getConst(PBMoves,:VOLTTACKLE))
  4494.   end
  4495.   moves|=[] # remove duplicates
  4496.   # Assembling move list
  4497.   finalmoves=[]
  4498.   listend=moves.length-4
  4499.   listend=0 if listend<0
  4500.   j=0
  4501.   for i in listend..listend+3
  4502.     moveid=(i>=moves.length) ? 0 : moves[i]
  4503.     finalmoves[j]=PBMove.new(moveid)
  4504.     j+=1
  4505.   end
  4506.   # Inheriting Individual Values
  4507.   ivs=[]
  4508.   for i in 0...6
  4509.     ivs[i]=rand(32)
  4510.   end
  4511.   ivinherit=[]
  4512.   for i in 0...2
  4513.     parent=[mother,father][i]
  4514.     ivinherit[i]=PBStats::HP if isConst?(parent.item,PBItems,:POWERWEIGHT)
  4515.     ivinherit[i]=PBStats::ATTACK if isConst?(parent.item,PBItems,:POWERBRACER)
  4516.     ivinherit[i]=PBStats::DEFENSE if isConst?(parent.item,PBItems,:POWERBELT)
  4517.     ivinherit[i]=PBStats::SPEED if isConst?(parent.item,PBItems,:POWERANKLET)
  4518.     ivinherit[i]=PBStats::SPATK if isConst?(parent.item,PBItems,:POWERLENS)
  4519.     ivinherit[i]=PBStats::SPDEF if isConst?(parent.item,PBItems,:POWERBAND)
  4520.   end
  4521.   num=0; r=rand(2)
  4522.   for i in 0...2
  4523.     if ivinherit[r]!=nil
  4524.       parent=[mother,father][r]
  4525.       ivs[ivinherit[r]]=parent.iv[ivinherit[r]]
  4526.       num+=1
  4527.       break
  4528.     end
  4529.     r=(r+1)%2
  4530.   end
  4531.   stats=[PBStats::HP,PBStats::ATTACK,PBStats::DEFENSE,
  4532.          PBStats::SPEED,PBStats::SPATK,PBStats::SPDEF]
  4533.   limit=(USENEWBATTLEMECHANICS && (isConst?(mother.item,PBItems,:DESTINYKNOT) ||
  4534.          isConst?(father.item,PBItems,:DESTINYKNOT))) ? 5 : 3
  4535.   loop do
  4536.     freestats=[]
  4537.     for i in stats
  4538.       freestats.push(i) if !ivinherit.include?(i)
  4539.     end
  4540.     break if freestats.length==0
  4541.     r=freestats[rand(freestats.length)]
  4542.     parent=[mother,father][rand(2)]
  4543.     ivs[r]=parent.iv[r]
  4544.     ivinherit.push(r)
  4545.     num+=1
  4546.     break if num>=limit
  4547.   end
  4548.   # Inheriting nature
  4549.   newnatures=[]
  4550.   newnatures.push(mother.nature) if isConst?(mother.item,PBItems,:EVERSTONE)
  4551.   newnatures.push(father.nature) if isConst?(father.item,PBItems,:EVERSTONE)
  4552.   if newnatures.length>0
  4553.     egg.setNature(newnatures[rand(newnatures.length)])
  4554.   end
  4555.   # Masuda method and Shiny Charm
  4556.   shinyretries=0
  4557.   shinyretries+=5 if father.language!=mother.language
  4558.   shinyretries+=2 if hasConst?(PBItems,:SHINYCHARM) &&
  4559.                      $PokemonBag.pbHasItem?(:SHINYCHARM)
  4560.   if shinyretries>0
  4561.     for i in 0...shinyretries
  4562.       break if egg.isShiny?
  4563.       egg.personalID=rand(65536)|(rand(65536)<<16)
  4564.     end
  4565.   end
  4566.   # Inheriting ability from the mother
  4567.   if (!ditto0 && !ditto1)
  4568.     if mother.hasHiddenAbility?
  4569.       egg.setAbility(mother.abilityIndex) if rand(10)<6
  4570.     else
  4571.       if rand(10)<8
  4572.         egg.setAbility(mother.abilityIndex)
  4573.       else
  4574.         egg.setAbility((mother.abilityIndex+1)%2)
  4575.       end
  4576.     end
  4577.   elsif ((!ditto0 && ditto1) || (!ditto1 && ditto0)) && USENEWBATTLEMECHANICS
  4578.     parent=(!ditto0) ? mother : father
  4579.     if parent.hasHiddenAbility?
  4580.       egg.setAbility(parent.abilityIndex) if rand(10)<6
  4581.     end
  4582.   end
  4583.   # Inheriting Poké Ball from the mother
  4584.   if mother.isFemale? &&
  4585.      !isConst?(pbBallTypeToBall(mother.ballused),PBItems,:MASTERBALL) &&
  4586.      !isConst?(pbBallTypeToBall(mother.ballused),PBItems,:CHERISHBALL)
  4587.     egg.ballused=mother.ballused
  4588.   end
  4589.   egg.iv[0]=ivs[0]
  4590.   egg.iv[1]=ivs[1]
  4591.   egg.iv[2]=ivs[2]
  4592.   egg.iv[3]=ivs[3]
  4593.   egg.iv[4]=ivs[4]
  4594.   egg.iv[5]=ivs[5]
  4595.   egg.moves[0]=finalmoves[0]
  4596.   egg.moves[1]=finalmoves[1]
  4597.   egg.moves[2]=finalmoves[2]
  4598.   egg.moves[3]=finalmoves[3]
  4599.   egg.calcStats
  4600.   egg.obtainText=_INTL("Day-Care Couple")
  4601.   egg.name=_INTL("Egg")
  4602.   dexdata=pbOpenDexData
  4603.   pbDexDataOffset(dexdata,babyspecies,21)
  4604.   eggsteps=dexdata.fgetw
  4605.   dexdata.close
  4606.   egg.eggsteps=eggsteps
  4607.   if rand(65536)<POKERUSCHANCE
  4608.     egg.givePokerus
  4609.   end
  4610.   $Trainer.party[$Trainer.party.length]=egg
  4611. end
  4612. ################################################################################
  4613. # Evolution                                                                    #
  4614. ################################################################################
  4615.   def pbEvolutionSuccess
  4616.     # Play cry of evolved species
  4617.     frames = pbCryFrameLength(@newspecies,@pokemon.form)
  4618.     pbBGMStop
  4619.     pbPlayCrySpecies(@newspecies,@pokemon.form)
  4620.     frames.times do
  4621.       Graphics.update
  4622.       pbUpdate
  4623.     end
  4624.     # Success jingle/message
  4625.     pbMEPlay("Evolution success")
  4626.     newspeciesname = PBSpecies.getName(@newspecies)
  4627.     oldspeciesname = PBSpecies.getName(@pokemon.species)
  4628.     Kernel.pbMessageDisplay(@sprites["msgwindow"],
  4629.        _INTL("\\se[]Congratulations! Your {1} evolved into {2}!\\wt[80]",
  4630.        @pokemon.name,newspeciesname)) { pbUpdate }
  4631.     @sprites["msgwindow"].text = ""
  4632.     # Check for consumed item and Pokémon duplication (i.e. Shedinja)
  4633.     removeItem = false
  4634.     createSpecies = pbCheckEvolutionEx(@pokemon){|pokemon,evonib,level,poke|
  4635.        case evonib
  4636.        when PBEvolution::Shedinja
  4637.          next poke if $PokemonBag.pbHasItem?(getConst(PBItems,:POKEBALL))
  4638.        when PBEvolution::TradeItem,PBEvolution::DayHoldItem,PBEvolution::NightHoldItem
  4639.          removeItem = true if poke==@newspecies   # Item is now consumed
  4640.        end
  4641.        next -1
  4642.     }
  4643.     @pokemon.setItem(0) if removeItem
  4644.     # Modify Pokémon to make it evolved
  4645.     @pokemon.species = @newspecies
  4646.     @pokemon.name    = newspeciesname if @pokemon.name==oldspeciesname
  4647.     @pokemon.form    = 0 if isConst?(@pokemon.species,PBSpecies,:MOTHIM)
  4648.     @pokemon.calcStats
  4649.     # See and own evolved species
  4650.     $Trainer.seen[@newspecies]  = true
  4651.     $Trainer.owned[@newspecies] = true
  4652.     pbSeenForm(@pokemon)
  4653.     # Learn moves upon evolution for evolved species
  4654.     movelist = @pokemon.getMoveList
  4655.     for i in movelist
  4656.       if i[0]==0 || i[0]==@pokemon.level          # Learned a new move
  4657.         pbLearnMove(@pokemon,i[1],true) { pbUpdate }
  4658.       end
  4659.     end
  4660.     # Duplicate Pokémon (i.e. Shedinja)
  4661.     if $Trainer.partyplus>=1
  4662.     if createSpecies>0 && $Trainer.party.length<$Trainer.partyplus
  4663.       newpokemon = @pokemon.clone
  4664.       newpokemon.species = createSpecies
  4665.       newpokemon.name    = PBSpecies.getName(createSpecies)
  4666.       newpokemon.iv      = @pokemon.iv.clone
  4667.       newpokemon.ev      = @pokemon.ev.clone
  4668.       newpokemon.markings = 0
  4669.       newpokemon.ballused = 0
  4670.       newpokemon.setItem(0)
  4671.       newpokemon.clearAllRibbons
  4672.       newpokemon.calcStats
  4673.       newpokemon.heal
  4674.       # Add duplicate Pokémon to party
  4675.       $Trainer.party.push(newpokemon)
  4676.       # See and own duplicate Pokémon
  4677.       $Trainer.seen[createSpecies]  = true
  4678.       $Trainer.owned[createSpecies] = true
  4679.       pbSeenForm(newpokemon)
  4680.       # Consume Poké Ball
  4681.       $PokemonBag.pbDeleteItem(getConst(PBItems,:POKEBALL))
  4682.     end
  4683.     else
  4684.     if createSpecies>0 && $Trainer.party.length<6
  4685.       newpokemon = @pokemon.clone
  4686.       newpokemon.species = createSpecies
  4687.       newpokemon.name    = PBSpecies.getName(createSpecies)
  4688.       newpokemon.iv      = @pokemon.iv.clone
  4689.       newpokemon.ev      = @pokemon.ev.clone
  4690.       newpokemon.markings = 0
  4691.       newpokemon.ballused = 0
  4692.       newpokemon.setItem(0)
  4693.       newpokemon.clearAllRibbons
  4694.       newpokemon.calcStats
  4695.       newpokemon.heal
  4696.       # Add duplicate Pokémon to party
  4697.       $Trainer.party.push(newpokemon)
  4698.       # See and own duplicate Pokémon
  4699.       $Trainer.seen[createSpecies]  = true
  4700.       $Trainer.owned[createSpecies] = true
  4701.       pbSeenForm(newpokemon)
  4702.       # Consume Poké Ball
  4703.       $PokemonBag.pbDeleteItem(getConst(PBItems,:POKEBALL))
  4704.     end
  4705.     end
  4706.   end
  4707. ################################################################################
  4708. # Party                                                                        #
  4709. ################################################################################
  4710. #===============================================================================
  4711. # Pokémon party panels
  4712. #===============================================================================
  4713. class PokemonPartyBlankPanel < SpriteWrapper
  4714.   attr_accessor :text
  4715.   def initialize(pokemon,index,viewport=nil)
  4716.     super(viewport)
  4717. if $Trainer.partyplus==40
  4718.     self.x = [0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448][index]
  4719.     self.y = [0,0,0,0,0,0,0,0,64,64,64,64,64,64,64,64,128,128,128,128,128,128,128,128,192,192,192,192,192,192,192,192,256,256,256,256,256,256,256,256][index]
  4720. elsif $Trainer.partyplus==35
  4721.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4722.     self.y = [0,0,0,0,0,0,0,64,64,64,64,64,64,64,128,128,128,128,128,128,128,192,192,192,192,192,192,192,256,256,256,256,256,256,256][index]
  4723. elsif $Trainer.partyplus==32
  4724.     self.x = [0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448][index]
  4725.     self.y = [2,2,2,2,2,2,2,2,82,82,82,82,82,82,82,82,162,162,162,162,162,162,162,162,242,242,242,242,242,242,242,242][index]
  4726. elsif $Trainer.partyplus==30
  4727.     self.x = [2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427][index]
  4728.     self.y = [2,2,2,2,2,2,66,66,66,66,66,66,130,130,130,130,130,130,194,194,194,194,194,194,258,258,258,258,258,258][index]
  4729. elsif $Trainer.partyplus==28
  4730.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4731.     self.y = [2,2,2,2,2,2,2,82,82,82,82,82,82,82,162,162,162,162,162,162,162,242,242,242,242,242,242,242][index]
  4732. elsif $Trainer.partyplus==25
  4733.     self.x = [2,104,206,308,410,2,104,206,308,410,2,104,206,308,410,2,104,206,308,410,2,104,206,308,410][index]
  4734.     self.y = [2,2,2,2,2,66,66,66,66,66,130,130,130,130,130,194,194,194,194,194,258,258,258,258,258][index]
  4735. elsif $Trainer.partyplus==24
  4736.     self.x = [2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427][index]
  4737.     self.y = [2,2,2,2,2,2,82,82,82,82,82,82,162,162,162,162,162,162,242,242,242,242,242,242][index]
  4738. elsif $Trainer.partyplus==21
  4739.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4740.     self.y = [2,2,2,2,2,2,2,108,108,108,108,108,108,108,214,214,214,214,214,214,214][index]
  4741. elsif $Trainer.partyplus==20
  4742.     self.x = [2,104,206,308,410,2,104,206,308,410,2,104,206,308,410,2,104,206,308,410][index]
  4743.     self.y = [2,2,2,2,2,82,82,82,82,82,162,162,162,162,162,242,242,242,242,242][index]
  4744. elsif $Trainer.partyplus==18
  4745.     self.x = [2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427][index]
  4746.     self.y = [2,2,2,2,2,2,108,108,108,108,108,108,214,214,214,214,214,214][index]
  4747. elsif $Trainer.partyplus==16
  4748.     self.x = [2,129,257,384,2,129,257,384,2,129,257,384,2,129,257,384][index]
  4749.     self.y = [2,2,2,2,82,82,82,82,162,162,162,162,242,242,242,242][index]
  4750. elsif $Trainer.partyplus==15
  4751.     self.x = [2,104,206,308,410,2,104,206,308,410,2,104,206,308,410][index]
  4752.     self.y = [2,2,2,2,2,108,108,108,108,108,214,214,214,214,214][index]
  4753. elsif $Trainer.partyplus==14
  4754.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4755.     self.y = [2,2,2,2,2,2,2,161,161,161,161,161,161,161][index]
  4756. elsif $Trainer.partyplus==12
  4757.     self.x = [2,172,342,2,172,342,2,172,342,2,172,342][index]
  4758.     self.y = [2,2,2,82,82,82,162,162,162,242,242,242][index]
  4759. elsif $Trainer.partyplus==10
  4760.     self.x = [2,257,2,257,2,257,2,257,2,257][index]
  4761.     self.y = [2,2,66,66,130,130,194,194,258,258][index]
  4762. elsif $Trainer.partyplus==9
  4763.     self.x = [2,172,342,2,172,342,2,172,342][index]
  4764.     self.y = [2,2,2,108,108,108,214,214,214][index]
  4765. elsif $Trainer.partyplus==8
  4766.     self.x = [2,257,2,257,2,257,2,257][index]
  4767.     self.y = [2,2,82,82,162,162,242,242][index]
  4768. elsif $Trainer.partyplus==5
  4769.     self.x = [2,2,2,2,2,][index]
  4770.     self.y = [2,66,130,194,258][index]
  4771. elsif $Trainer.partyplus==4
  4772.     self.x = [2,258,2,258][index]
  4773.     self.y = [2,2,162,162][index]
  4774. elsif $Trainer.partyplus==3
  4775.     self.x = [2,2,2][index]
  4776.     self.y = [2,108,214][index]
  4777. elsif $Trainer.partyplus==2
  4778.     self.x = [2,257][index]
  4779.     self.y = [2,2][index]
  4780. elsif $Trainer.partyplus==1
  4781.     self.x = [2][index]
  4782.     self.y = [2][index]
  4783. else
  4784.     self.x = [2,257,2,257,2,257][index]
  4785.     self.y = [2,2,108,108,214,214][index]
  4786. end
  4787. if $Trainer.partyplus==40
  4788.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/40 - Party/panel_blank")
  4789. elsif $Trainer.partyplus==35
  4790.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/35 - Party/panel_blank")
  4791. elsif $Trainer.partyplus==32
  4792.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/32 - Party/panel_blank")
  4793. elsif $Trainer.partyplus==30
  4794.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/30 - Party/panel_blank")
  4795. elsif $Trainer.partyplus==28
  4796.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/28 - Party/panel_blank")
  4797. elsif $Trainer.partyplus==25
  4798.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/25 - Party/panel_blank")
  4799. elsif $Trainer.partyplus==24
  4800.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/24 - Party/panel_blank")
  4801. elsif $Trainer.partyplus==21
  4802.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/21 - Party/panel_blank")
  4803. elsif $Trainer.partyplus==20
  4804.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/20 - Party/panel_blank")
  4805. elsif $Trainer.partyplus==18
  4806.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/18 - Party/panel_blank")
  4807. elsif $Trainer.partyplus==16
  4808.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/16 - Party/panel_blank")
  4809. elsif $Trainer.partyplus==15
  4810.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/15 - Party/panel_blank")
  4811. elsif $Trainer.partyplus==14
  4812.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/14 - Party/panel_blank")
  4813. elsif $Trainer.partyplus==12
  4814.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/12 - Party/panel_blank")
  4815. elsif $Trainer.partyplus==10
  4816.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/10 - Party/panel_blank")
  4817. elsif $Trainer.partyplus==9
  4818.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/19 - Party/panel_blank")
  4819. elsif $Trainer.partyplus==8
  4820.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/8 - Party/panel_blank")
  4821. elsif $Trainer.partyplus==5
  4822.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/5 - Party/panel_blank")
  4823. elsif $Trainer.partyplus==4
  4824.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/4 - Party/panel_blank")
  4825. elsif $Trainer.partyplus==3
  4826.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/3 - Party/panel_blank")
  4827. elsif $Trainer.partyplus==2
  4828.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/2 - Party/panel_blank")
  4829. elsif $Trainer.partyplus==1
  4830.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/PartyPlus/1 - Party/panel_blank")
  4831. else
  4832.     @panelbgsprite = AnimatedBitmap.new("Graphics/Pictures/Party/panel_blank")
  4833. end
  4834.     self.bitmap = @panelbgsprite.bitmap
  4835.     @text = nil
  4836.   end
  4837.   def dispose
  4838.     @panelbgsprite.dispose
  4839.     super
  4840.   end
  4841.   def selected; return false; end
  4842.   def selected=(value); end
  4843.   def preselected; return false; end
  4844.   def preselected=(value); end
  4845.   def switching; return false; end
  4846.   def switching=(value); end
  4847.   def refresh; end
  4848. end
  4849. class PokemonPartyPanel < SpriteWrapper
  4850.   attr_reader :pokemon
  4851.   attr_reader :active
  4852.   attr_reader :selected
  4853.   attr_reader :preselected
  4854.   attr_reader :switching
  4855.   attr_accessor :text
  4856.   def initialize(pokemon,index,viewport=nil)
  4857.     super(viewport)
  4858.     @pokemon = pokemon
  4859.     @active = (index==0)   # true = rounded panel, false = rectangular panel
  4860.     @refreshing = true
  4861. if $Trainer.partyplus==40
  4862.     self.x = [0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448][index]
  4863.     self.y = [0,0,0,0,0,0,0,0,64,64,64,64,64,64,64,64,128,128,128,128,128,128,128,128,192,192,192,192,192,192,192,192,256,256,256,256,256,256,256,256][index]
  4864. elsif $Trainer.partyplus==35
  4865.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4866.     self.y = [0,0,0,0,0,0,0,64,64,64,64,64,64,64,128,128,128,128,128,128,128,192,192,192,192,192,192,192,256,256,256,256,256,256,256][index]
  4867. elsif $Trainer.partyplus==32
  4868.     self.x = [0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448,0,64,128,192,256,320,384,448][index]
  4869.     self.y = [2,2,2,2,2,2,2,2,82,82,82,82,82,82,82,82,162,162,162,162,162,162,162,162,242,242,242,242,242,242,242,242][index]
  4870. elsif $Trainer.partyplus==30
  4871.     self.x = [2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427][index]
  4872.     self.y = [2,2,2,2,2,2,66,66,66,66,66,66,130,130,130,130,130,130,194,194,194,194,194,194,258,258,258,258,258,258][index]
  4873. elsif $Trainer.partyplus==28
  4874.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4875.     self.y = [2,2,2,2,2,2,2,82,82,82,82,82,82,82,162,162,162,162,162,162,162,242,242,242,242,242,242,242][index]
  4876. elsif $Trainer.partyplus==25
  4877.     self.x = [2,104,206,308,410,2,104,206,308,410,2,104,206,308,410,2,104,206,308,410,2,104,206,308,410][index]
  4878.     self.y = [2,2,2,2,2,66,66,66,66,66,130,130,130,130,130,194,194,194,194,194,258,258,258,258,258][index]
  4879. elsif $Trainer.partyplus==24
  4880.     self.x = [2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427][index]
  4881.     self.y = [2,2,2,2,2,2,82,82,82,82,82,82,162,162,162,162,162,162,242,242,242,242,242,242][index]
  4882. elsif $Trainer.partyplus==21
  4883.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4884.     self.y = [2,2,2,2,2,2,2,108,108,108,108,108,108,108,214,214,214,214,214,214,214][index]
  4885. elsif $Trainer.partyplus==20
  4886.     self.x = [2,104,206,308,410,2,104,206,308,410,2,104,206,308,410,2,104,206,308,410][index]
  4887.     self.y = [2,2,2,2,2,82,82,82,82,82,162,162,162,162,162,242,242,242,242,242][index]
  4888. elsif $Trainer.partyplus==18
  4889.     self.x = [2,87,172,257,342,427,2,87,172,257,342,427,2,87,172,257,342,427][index]
  4890.     self.y = [2,2,2,2,2,2,108,108,108,108,108,108,214,214,214,214,214,214][index]
  4891. elsif $Trainer.partyplus==16
  4892.     self.x = [2,129,257,384,2,129,257,384,2,129,257,384,2,129,257,384][index]
  4893.     self.y = [2,2,2,2,82,82,82,82,162,162,162,162,242,242,242,242][index]
  4894. elsif $Trainer.partyplus==15
  4895.     self.x = [2,104,206,308,410,2,104,206,308,410,2,104,206,308,410][index]
  4896.     self.y = [2,2,2,2,2,108,108,108,108,108,214,214,214,214,214][index]
  4897. elsif $Trainer.partyplus==14
  4898.     self.x = [2,75,148,221,294,367,440,2,75,148,221,294,367,440][index]
  4899.     self.y = [2,2,2,2,2,2,2,161,161,161,161,161,161,161][index]
  4900. elsif $Trainer.partyplus==12
  4901.     self.x = [2,172,342,2,172,342,2,172,342,2,172,342][index]
  4902.     self.y = [2,2,2,82,82,82,162,162,162,242,242,242][index]
  4903. elsif $Trainer.partyplus==10
  4904.     self.x = [2,257,2,257,2,257,2,257,2,257][index]
  4905.     self.y = [2,2,66,66,130,130,194,194,258,258][index]
  4906. elsif $Trainer.partyplus==9
  4907.     self.x = [2,172,342,2,172,342,2,172,342][index]
  4908.     self.y = [2,2,2,108,108,108,214,214,214][index]
  4909. elsif $Trainer.partyplus==8
  4910.     self.x = [2,257,2,257,2,257,2,257][index]
  4911.     self.y = [2,2,82,82,162,162,242,242][index]
  4912. elsif $Trainer.partyplus==5
  4913.     self.x = [2,2,2,2,2,][index]
  4914.     self.y = [2,66,130,194,258][index]
  4915. elsif $Trainer.partyplus==4
  4916.     self.x = [2,258,2,258][index]
  4917.     self.y = [2,2,162,162][index]
  4918. elsif $Trainer.partyplus==3
  4919.     self.x = [2,2,2][index]
  4920.     self.y = [2,108,214][index]
  4921. elsif $Trainer.partyplus==2
  4922.     self.x = [2,257][index]
  4923.     self.y = [2,2][index]
  4924. elsif $Trainer.partyplus==1
  4925.     self.x = [2][index]
  4926.     self.y = [2][index]
  4927. else
  4928.     self.x = [2,257,2,257,2,257][index]
  4929.     self.y = [2,2,108,108,214,214][index]
  4930. end
  4931.     @panelbgsprite = ChangelingSprite.new(0,0,viewport)
  4932.     @panelbgsprite.z = self.z
  4933. if $Trainer.partyplus==40
  4934.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/40 - Party/panel_rect")
  4935.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/40 - Party/panel_rect_sel")
  4936.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/40 - Party/panel_rect_faint")
  4937.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/40 - Party/panel_rect_faint_sel")
  4938.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/40 - Party/panel_rect_swap")
  4939.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/40 - Party/panel_rect_swap_sel")
  4940.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/40 - Party/panel_rect_swap_sel2")
  4941. elsif $Trainer.partyplus==35
  4942.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/35 - Party/panel_rect")
  4943.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/35 - Party/panel_rect_sel")
  4944.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/35 - Party/panel_rect_faint")
  4945.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/35 - Party/panel_rect_faint_sel")
  4946.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/35 - Party/panel_rect_swap")
  4947.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/35 - Party/panel_rect_swap_sel")
  4948.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/35 - Party/panel_rect_swap_sel2")
  4949. elsif $Trainer.partyplus==32
  4950.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/32 - Party/panel_rect")
  4951.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/32 - Party/panel_rect_sel")
  4952.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/32 - Party/panel_rect_faint")
  4953.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/32 - Party/panel_rect_faint_sel")
  4954.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/32 - Party/panel_rect_swap")
  4955.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/32 - Party/panel_rect_swap_sel")
  4956.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/32 - Party/panel_rect_swap_sel2")
  4957. elsif $Trainer.partyplus==30
  4958.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/30 - Party/panel_rect")
  4959.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/30 - Party/panel_rect_sel")
  4960.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/30 - Party/panel_rect_faint")
  4961.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/30 - Party/panel_rect_faint_sel")
  4962.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/30 - Party/panel_rect_swap")
  4963.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/30 - Party/panel_rect_swap_sel")
  4964.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/30 - Party/panel_rect_swap_sel2")
  4965. elsif $Trainer.partyplus==28
  4966.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/28 - Party/panel_rect")
  4967.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/28 - Party/panel_rect_sel")
  4968.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/28 - Party/panel_rect_faint")
  4969.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/28 - Party/panel_rect_faint_sel")
  4970.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/28 - Party/panel_rect_swap")
  4971.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/28 - Party/panel_rect_swap_sel")
  4972.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/28 - Party/panel_rect_swap_sel2")
  4973. elsif $Trainer.partyplus==25
  4974.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/25 - Party/panel_rect")
  4975.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/25 - Party/panel_rect_sel")
  4976.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/25 - Party/panel_rect_faint")
  4977.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/25 - Party/panel_rect_faint_sel")
  4978.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/25 - Party/panel_rect_swap")
  4979.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/25 - Party/panel_rect_swap_sel")
  4980.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/25 - Party/panel_rect_swap_sel2")
  4981. elsif $Trainer.partyplus==24
  4982.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/24 - Party/panel_rect")
  4983.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/24 - Party/panel_rect_sel")
  4984.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/24 - Party/panel_rect_faint")
  4985.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/24 - Party/panel_rect_faint_sel")
  4986.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/24 - Party/panel_rect_swap")
  4987.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/24 - Party/panel_rect_swap_sel")
  4988.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/24 - Party/panel_rect_swap_sel2")
  4989. elsif $Trainer.partyplus==21
  4990.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/21 - Party/panel_rect")
  4991.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/21 - Party/panel_rect_sel")
  4992.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/21 - Party/panel_rect_faint")
  4993.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/21 - Party/panel_rect_faint_sel")
  4994.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/21 - Party/panel_rect_swap")
  4995.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/21 - Party/panel_rect_swap_sel")
  4996.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/21 - Party/panel_rect_swap_sel2")
  4997. elsif $Trainer.partyplus==20
  4998.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/20 - Party/panel_rect")
  4999.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/20 - Party/panel_rect_sel")
  5000.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/20 - Party/panel_rect_faint")
  5001.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/20 - Party/panel_rect_faint_sel")
  5002.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/20 - Party/panel_rect_swap")
  5003.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/20 - Party/panel_rect_swap_sel")
  5004.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/20 - Party/panel_rect_swap_sel2")
  5005. elsif $Trainer.partyplus==18
  5006.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/18 - Party/panel_rect")
  5007.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/18 - Party/panel_rect_sel")
  5008.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/18 - Party/panel_rect_faint")
  5009.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/18 - Party/panel_rect_faint_sel")
  5010.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/18 - Party/panel_rect_swap")
  5011.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/18 - Party/panel_rect_swap_sel")
  5012.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/18 - Party/panel_rect_swap_sel2")
  5013. elsif $Trainer.partyplus==16
  5014.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/16 - Party/panel_rect")
  5015.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/16 - Party/panel_rect_sel")
  5016.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/16 - Party/panel_rect_faint")
  5017.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/16 - Party/panel_rect_faint_sel")
  5018.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/16 - Party/panel_rect_swap")
  5019.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/16 - Party/panel_rect_swap_sel")
  5020.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/16 - Party/panel_rect_swap_sel2")
  5021. elsif $Trainer.partyplus==15
  5022.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/15 - Party/panel_rect")
  5023.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/15 - Party/panel_rect_sel")
  5024.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/15 - Party/panel_rect_faint")
  5025.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/15 - Party/panel_rect_faint_sel")
  5026.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/15 - Party/panel_rect_swap")
  5027.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/15 - Party/panel_rect_swap_sel")
  5028.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/15 - Party/panel_rect_swap_sel2")
  5029. elsif $Trainer.partyplus==14
  5030.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/14 - Party/panel_rect")
  5031.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/14 - Party/panel_rect_sel")
  5032.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/14 - Party/panel_rect_faint")
  5033.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/14 - Party/panel_rect_faint_sel")
  5034.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/14 - Party/panel_rect_swap")
  5035.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/14 - Party/panel_rect_swap_sel")
  5036.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/14 - Party/panel_rect_swap_sel2")
  5037. elsif $Trainer.partyplus==12
  5038.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/12 - Party/panel_rect")
  5039.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/12 - Party/panel_rect_sel")
  5040.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/12 - Party/panel_rect_faint")
  5041.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/12 - Party/panel_rect_faint_sel")
  5042.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/12 - Party/panel_rect_swap")
  5043.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/12 - Party/panel_rect_swap_sel")
  5044.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/12 - Party/panel_rect_swap_sel2")
  5045. elsif $Trainer.partyplus==10
  5046.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/10 - Party/panel_rect")
  5047.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/10 - Party/panel_rect_sel")
  5048.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/10 - Party/panel_rect_faint")
  5049.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/10 - Party/panel_rect_faint_sel")
  5050.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/10 - Party/panel_rect_swap")
  5051.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/10 - Party/panel_rect_swap_sel")
  5052.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/10 - Party/panel_rect_swap_sel2")
  5053. elsif $Trainer.partyplus==9
  5054.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/9 - Party/panel_rect")
  5055.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/9 - Party/panel_rect_sel")
  5056.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/9 - Party/panel_rect_faint")
  5057.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/9 - Party/panel_rect_faint_sel")
  5058.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/9 - Party/panel_rect_swap")
  5059.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/9 - Party/panel_rect_swap_sel")
  5060.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/9 - Party/panel_rect_swap_sel2")
  5061. elsif $Trainer.partyplus==8
  5062.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/8 - Party/panel_rect")
  5063.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/8 - Party/panel_rect_sel")
  5064.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/8 - Party/panel_rect_faint")
  5065.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/8 - Party/panel_rect_faint_sel")
  5066.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/8 - Party/panel_rect_swap")
  5067.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/8 - Party/panel_rect_swap_sel")
  5068.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/8 - Party/panel_rect_swap_sel2")
  5069. elsif $Trainer.partyplus==5
  5070.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/5 - Party/panel_rect")
  5071.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/5 - Party/panel_rect_sel")
  5072.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/5 - Party/panel_rect_faint")
  5073.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/5 - Party/panel_rect_faint_sel")
  5074.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/5 - Party/panel_rect_swap")
  5075.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/5 - Party/panel_rect_swap_sel")
  5076.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/5 - Party/panel_rect_swap_sel2")
  5077. elsif $Trainer.partyplus==4
  5078.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/4 - Party/panel_rect")
  5079.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/4 - Party/panel_rect_sel")
  5080.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/4 - Party/panel_rect_faint")
  5081.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/4 - Party/panel_rect_faint_sel")
  5082.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/4 - Party/panel_rect_swap")
  5083.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/4 - Party/panel_rect_swap_sel")
  5084.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/4 - Party/panel_rect_swap_sel2")
  5085. elsif $Trainer.partyplus==3
  5086.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/3 - Party/panel_rect")
  5087.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/3 - Party/panel_rect_sel")
  5088.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/3 - Party/panel_rect_faint")
  5089.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/3 - Party/panel_rect_faint_sel")
  5090.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/3 - Party/panel_rect_swap")
  5091.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/3 - Party/panel_rect_swap_sel")
  5092.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/3 - Party/panel_rect_swap_sel2")
  5093. elsif $Trainer.partyplus==2
  5094.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/2 - Party/panel_rect")
  5095.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/2 - Party/panel_rect_sel")
  5096.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/2 - Party/panel_rect_faint")
  5097.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/2 - Party/panel_rect_faint_sel")
  5098.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/2 - Party/panel_rect_swap")
  5099.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/2 - Party/panel_rect_swap_sel")
  5100.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/2 - Party/panel_rect_swap_sel2")
  5101. elsif $Trainer.partyplus==1
  5102.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/PartyPlus/1 - Party/panel_rect")
  5103.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/PartyPlus/1 - Party/panel_rect_sel")
  5104.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/PartyPlus/1 - Party/panel_rect_faint")
  5105.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/PartyPlus/1 - Party/panel_rect_faint_sel")
  5106.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/PartyPlus/1 - Party/panel_rect_swap")
  5107.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/PartyPlus/1 - Party/panel_rect_swap_sel")
  5108.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/PartyPlus/1 - Party/panel_rect_swap_sel2")
  5109. else
  5110.     if @active   # Rounded panel
  5111.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/panel_round")
  5112.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/panel_round_sel")
  5113.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/panel_round_faint")
  5114.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/panel_round_faint_sel")
  5115.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/panel_round_swap")
  5116.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/panel_round_swap_sel")
  5117.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/panel_round_swap_sel2")
  5118.     else   # Rectangular panel
  5119.       @panelbgsprite.addBitmap("able","Graphics/Pictures/Party/panel_rect")
  5120.       @panelbgsprite.addBitmap("ablesel","Graphics/Pictures/Party/panel_rect_sel")
  5121.       @panelbgsprite.addBitmap("fainted","Graphics/Pictures/Party/panel_rect_faint")
  5122.       @panelbgsprite.addBitmap("faintedsel","Graphics/Pictures/Party/panel_rect_faint_sel")
  5123.       @panelbgsprite.addBitmap("swap","Graphics/Pictures/Party/panel_rect_swap")
  5124.       @panelbgsprite.addBitmap("swapsel","Graphics/Pictures/Party/panel_rect_swap_sel")
  5125.       @panelbgsprite.addBitmap("swapsel2","Graphics/Pictures/Party/panel_rect_swap_sel2")
  5126.     end
  5127. end
  5128.     if $Trainer.partyplus>=1
  5129.     else
  5130.     @hpbgsprite = ChangelingSprite.new(0,0,viewport)
  5131.     @hpbgsprite.z = self.z+1
  5132.     @hpbgsprite.addBitmap("able","Graphics/Pictures/Party/overlay_hp_back")
  5133.     @hpbgsprite.addBitmap("fainted","Graphics/Pictures/Party/overlay_hp_back_faint")
  5134.     @hpbgsprite.addBitmap("swap","Graphics/Pictures/Party/overlay_hp_back_swap")
  5135.     @hpbar    = AnimatedBitmap.new("Graphics/Pictures/Party/overlay_hp")
  5136.     end
  5137.     @ballsprite = ChangelingSprite.new(0,0,viewport)
  5138.     @ballsprite.z = self.z+1
  5139.     @ballsprite.addBitmap("desel","Graphics/Pictures/Party/icon_ball")
  5140.     @ballsprite.addBitmap("sel","Graphics/Pictures/Party/icon_ball_sel")
  5141.     @pkmnsprite = PokemonIconSprite.new(pokemon,viewport)
  5142.     @pkmnsprite.active = @active
  5143.     @pkmnsprite.z      = self.z+2
  5144.     @helditemsprite = HeldItemIconSprite.new(0,0,@pokemon,viewport)
  5145.     @helditemsprite.z = self.z+3
  5146.     @overlaysprite = BitmapSprite.new(Graphics.width,Graphics.height,viewport)
  5147.     @overlaysprite.z = self.z+4
  5148.     @statuses = AnimatedBitmap.new(_INTL("Graphics/Pictures/statuses"))
  5149.     @selected      = false
  5150.     @preselected   = false
  5151.     @switching     = false
  5152.     @text          = nil
  5153.     @refreshBitmap = true
  5154.     @refreshing    = false
  5155.     refresh
  5156.   end
  5157.   def dispose
  5158.     @panelbgsprite.dispose
  5159.     if $Trainer.partyplus>=1
  5160.     else
  5161.     @hpbgsprite.dispose
  5162.     @hpbar.dispose
  5163.     end
  5164.     @ballsprite.dispose
  5165.     @pkmnsprite.dispose
  5166.     @helditemsprite.dispose
  5167.     @overlaysprite.bitmap.dispose
  5168.     @overlaysprite.dispose
  5169.     @statuses.dispose
  5170.     super
  5171.   end
  5172.   def x=(value)
  5173.     super
  5174.     refresh
  5175.   end
  5176.   def y=(value)
  5177.     super
  5178.     refresh
  5179.   end
  5180.   def color=(value)
  5181.     super
  5182.     refresh
  5183.   end
  5184.   def text=(value)
  5185.     if @text!=value
  5186.       @text = value
  5187.       @refreshBitmap = true
  5188.       refresh
  5189.     end
  5190.   end
  5191.   def pokemon=(value)
  5192.     @pokemon = value
  5193.     @pkmnsprite.pokemon = value if @pkmnsprite && !@pkmnsprite.disposed?
  5194.     @helditemsprite.pokemon = value if @helditemsprite && !@helditemsprite.disposed?
  5195.     @refreshBitmap = true
  5196.     refresh
  5197.   end
  5198.   def selected=(value)
  5199.     if @selected!=value
  5200.       @selected = value
  5201.       refresh
  5202.     end
  5203.   end
  5204.   def preselected=(value)
  5205.     if @preselected!=value
  5206.       @preselected = value
  5207.       refresh
  5208.     end
  5209.   end
  5210.   def switching=(value)
  5211.     if @switching!=value
  5212.       @switching = value
  5213.       refresh
  5214.     end
  5215.   end
  5216.   def hp; return @pokemon.hp; end
  5217.   def refresh
  5218.     return if disposed?
  5219.     return if @refreshing
  5220.     @refreshing = true
  5221.     if @panelbgsprite && !@panelbgsprite.disposed?
  5222.       if self.selected
  5223.         if self.preselected;     @panelbgsprite.changeBitmap("swapsel2")
  5224.         elsif @switching;        @panelbgsprite.changeBitmap("swapsel")
  5225.         elsif @pokemon.fainted?; @panelbgsprite.changeBitmap("faintedsel")
  5226.         else;                    @panelbgsprite.changeBitmap("ablesel")
  5227.         end
  5228.       else
  5229.         if self.preselected;     @panelbgsprite.changeBitmap("swap")
  5230.         elsif @pokemon.fainted?; @panelbgsprite.changeBitmap("fainted")
  5231.         else;                    @panelbgsprite.changeBitmap("able")
  5232.         end
  5233.       end
  5234.       @panelbgsprite.x     = self.x
  5235.       @panelbgsprite.y     = self.y
  5236.       @panelbgsprite.color = self.color
  5237.     end
  5238.     if $Trainer.partyplus>=1
  5239.     else
  5240.     if @hpbgsprite && !@hpbgsprite.disposed?
  5241.       @hpbgsprite.visible = (!@pokemon.egg? && !(@text && @text.length>0))
  5242.       if @hpbgsprite.visible
  5243.         if self.preselected || (self.selected && @switching); @hpbgsprite.changeBitmap("swap")
  5244.         elsif @pokemon.fainted?;                              @hpbgsprite.changeBitmap("fainted")
  5245.         else;                                                 @hpbgsprite.changeBitmap("able")
  5246.         end
  5247.         @hpbgsprite.x     = self.x+96
  5248.         @hpbgsprite.y     = self.y+50
  5249.         @hpbgsprite.color = self.color
  5250.       end
  5251.     end
  5252.     end
  5253.     if @ballsprite && !@ballsprite.disposed?
  5254.       @ballsprite.changeBitmap((self.selected) ? "sel" : "desel")
  5255.       @ballsprite.x     = self.x+10
  5256.       @ballsprite.y     = self.y+4
  5257.       @ballsprite.color = self.color
  5258.     end
  5259.     if @pkmnsprite && !@pkmnsprite.disposed?
  5260.       @pkmnsprite.x        = self.x
  5261.       @pkmnsprite.y        = self.y
  5262.       @pkmnsprite.color    = self.color
  5263.       @pkmnsprite.selected = self.selected
  5264.     end
  5265.     if @helditemsprite && !@helditemsprite.disposed?
  5266.       if @helditemsprite.visible
  5267.         @helditemsprite.x     = self.x+46
  5268.         @helditemsprite.y     = self.y+46
  5269.         @helditemsprite.color = self.color
  5270.       end
  5271.     end
  5272.     if @overlaysprite && !@overlaysprite.disposed?
  5273.       @overlaysprite.x     = self.x
  5274.       @overlaysprite.y     = self.y
  5275.       @overlaysprite.color = self.color
  5276.     end
  5277.     if @refreshBitmap
  5278.       @refreshBitmap = false
  5279.       @overlaysprite.bitmap.clear if @overlaysprite.bitmap
  5280.       basecolor   = Color.new(248,248,248)
  5281.       shadowcolor = Color.new(40,40,40)
  5282.       pbSetSystemFont(@overlaysprite.bitmap)
  5283.       textpos = []
  5284.       if $Trainer.partyplus>=1
  5285.       else
  5286.       # Draw Pokémon name
  5287.       textpos.push([@pokemon.name,96,16,0,basecolor,shadowcolor])
  5288.       if !@pokemon.egg?
  5289.         if !@text || @text.length==0
  5290.           # Draw HP numbers
  5291.           textpos.push([sprintf("% 3d /% 3d",@pokemon.hp,@pokemon.totalhp),224,60,1,basecolor,shadowcolor])
  5292.           # Draw HP bar
  5293.           if @pokemon.hp>0
  5294.             hpzone = 0
  5295.             hpzone = 1 if @pokemon.hp<=(@pokemon.totalhp/2).floor
  5296.             hpzone = 2 if @pokemon.hp<=(@pokemon.totalhp/4).floor
  5297.             hprect = Rect.new(0,hpzone*8,[@pokemon.hp*96/@pokemon.totalhp,2].max,8)
  5298.             @overlaysprite.bitmap.blt(128,52,@hpbar.bitmap,hprect)
  5299.           end
  5300.           # Draw status
  5301.           status = -1
  5302.           status = 6 if @pokemon.pokerusStage==1
  5303.           status = @pokemon.status-1 if @pokemon.status>0
  5304.           status = 5 if @pokemon.hp<=0
  5305.           if status>=0
  5306.             statusrect = Rect.new(0,16*status,44,16)
  5307.             @overlaysprite.bitmap.blt(78,68,@statuses.bitmap,statusrect)
  5308.           end
  5309.         end
  5310.         # Draw gender icon
  5311.         if @pokemon.isMale?
  5312.           textpos.push([_INTL("♂"),224,16,0,Color.new(0,112,248),Color.new(120,184,232)])
  5313.         elsif @pokemon.isFemale?
  5314.           textpos.push([_INTL("♀"),224,16,0,Color.new(232,32,16),Color.new(248,168,184)])
  5315.         end
  5316.       end
  5317.       pbDrawTextPositions(@overlaysprite.bitmap,textpos)
  5318.       # Draw level text
  5319.       if !@pokemon.egg?
  5320.         pbDrawImagePositions(@overlaysprite.bitmap,[[
  5321.            "Graphics/Pictures/Party/overlay_lv",20,70,0,0,22,14]])
  5322.         pbSetSmallFont(@overlaysprite.bitmap)
  5323.         pbDrawTextPositions(@overlaysprite.bitmap,[
  5324.            [@pokemon.level.to_s,42,62,0,basecolor,shadowcolor]
  5325.         ])
  5326.       end
  5327.       # Draw annotation text
  5328.       end
  5329.       if @text && @text.length>0
  5330.         pbSetSystemFont(@overlaysprite.bitmap)
  5331.         pbDrawTextPositions(@overlaysprite.bitmap,[
  5332.            [@text,96,58,0,basecolor,shadowcolor]
  5333.         ])
  5334.       end
  5335.     end
  5336.     @refreshing = false
  5337.   end
  5338.   def update
  5339.     super
  5340.     @panelbgsprite.update if @panelbgsprite && !@panelbgsprite.disposed?
  5341.     if $Trainer.partyplus>=1
  5342.     else
  5343.     @hpbgsprite.update if @hpbgsprite && !@hpbgsprite.disposed?
  5344.     end
  5345.     @ballsprite.update if @ballsprite && !@ballsprite.disposed?
  5346.     @pkmnsprite.update if @pkmnsprite && !@pkmnsprite.disposed?
  5347.     @helditemsprite.update if @helditemsprite && !@helditemsprite.disposed?
  5348.   end
  5349. end
  5350. #===============================================================================
  5351. # Pokémon party visuals
  5352. #===============================================================================
  5353. class PokemonParty_Scene
  5354.   def pbStartScene(party,starthelptext,annotations=nil,multiselect=false)
  5355.     @sprites = {}
  5356.     @party = party
  5357.     @viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  5358.     @viewport.z = 99999
  5359.     @multiselect = multiselect
  5360.     if $Trainer.partyplus==40
  5361.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/40 - Party/bg",@viewport)
  5362.     elsif $Trainer.partyplus==35
  5363.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/35 - Party/bg",@viewport)
  5364.     elsif $Trainer.partyplus==32
  5365.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/32 - Party/bg",@viewport)
  5366.     elsif $Trainer.partyplus==30
  5367.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/30 - Party/bg",@viewport)
  5368.     elsif $Trainer.partyplus==28
  5369.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/28 - Party/bg",@viewport)
  5370.     elsif $Trainer.partyplus==25
  5371.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/25 - Party/bg",@viewport)
  5372.     elsif $Trainer.partyplus==24
  5373.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/24 - Party/bg",@viewport)
  5374.     elsif $Trainer.partyplus==21
  5375.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/21 - Party/bg",@viewport)
  5376.     elsif $Trainer.partyplus==20
  5377.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/20 - Party/bg",@viewport)
  5378.     elsif $Trainer.partyplus==18
  5379.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/18 - Party/bg",@viewport)
  5380.     elsif $Trainer.partyplus==16
  5381.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/16 - Party/bg",@viewport)
  5382.     elsif $Trainer.partyplus==15
  5383.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/15 - Party/bg",@viewport)
  5384.     elsif $Trainer.partyplus==14
  5385.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/14 - Party/bg",@viewport)
  5386.     elsif $Trainer.partyplus==12
  5387.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/12 - Party/bg",@viewport)
  5388.     elsif $Trainer.partyplus==10
  5389.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/10 - Party/bg",@viewport)
  5390.     elsif $Trainer.partyplus==9
  5391.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/9 - Party/bg",@viewport)
  5392.     elsif $Trainer.partyplus==8
  5393.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/8 - Party/bg",@viewport)
  5394.     elsif $Trainer.partyplus==5
  5395.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/5 - Party/bg",@viewport)
  5396.     elsif $Trainer.partyplus==4
  5397.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/4 - Party/bg",@viewport)
  5398.     elsif $Trainer.partyplus==3
  5399.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/3 - Party/bg",@viewport)
  5400.     elsif $Trainer.partyplus==2
  5401.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/2 - Party/bg",@viewport)
  5402.     elsif $Trainer.partyplus==1
  5403.     addBackgroundPlane(@sprites,"partybg","Party/PartyPlus/1 - Party/bg",@viewport)
  5404.     else
  5405.     addBackgroundPlane(@sprites,"partybg","Party/bg",@viewport)
  5406.     end
  5407.     @sprites["messagebox"] = Window_AdvancedTextPokemon.new("")
  5408.     @sprites["messagebox"].viewport       = @viewport
  5409.     @sprites["messagebox"].visible        = false
  5410.     @sprites["messagebox"].letterbyletter = true
  5411.     pbBottomLeftLines(@sprites["messagebox"],2)
  5412.     @sprites["helpwindow"] = Window_UnformattedTextPokemon.new(starthelptext)
  5413.     @sprites["helpwindow"].viewport = @viewport
  5414.     @sprites["helpwindow"].visible  = true
  5415.     pbBottomLeftLines(@sprites["helpwindow"],1)
  5416.     pbSetHelpText(starthelptext)
  5417.     # Add party Pokémon sprites
  5418.     if $Trainer.partyplus>=1
  5419.     for i in 0...$Trainer.partyplus
  5420.       if @party[i]
  5421.         @sprites["pokemon#{i}"] = PokemonPartyPanel.new(@party[i],i,@viewport)
  5422.       else
  5423.         @sprites["pokemon#{i}"] = PokemonPartyBlankPanel.new(@party[i],i,@viewport)
  5424.       end
  5425.       @sprites["pokemon#{i}"].text = annotations[i] if annotations
  5426.     end
  5427.     else
  5428.     for i in 0...6
  5429.       if @party[i]
  5430.         @sprites["pokemon#{i}"] = PokemonPartyPanel.new(@party[i],i,@viewport)
  5431.       else
  5432.         @sprites["pokemon#{i}"] = PokemonPartyBlankPanel.new(@party[i],i,@viewport)
  5433.       end
  5434.       @sprites["pokemon#{i}"].text = annotations[i] if annotations
  5435.     end
  5436.     end
  5437.     if $Trainer.partyplus==40
  5438.     if @multiselect
  5439.       @sprites["pokemon40"] = PokemonPartyConfirmSprite.new(@viewport)
  5440.       @sprites["pokemon41"] = PokemonPartyCancelSprite2.new(@viewport)
  5441.     else
  5442.       @sprites["pokemon40"] = PokemonPartyCancelSprite.new(@viewport)
  5443.     end
  5444.     elsif $Trainer.partyplus==35
  5445.     if @multiselect
  5446.       @sprites["pokemon35"] = PokemonPartyConfirmSprite.new(@viewport)
  5447.       @sprites["pokemon36"] = PokemonPartyCancelSprite2.new(@viewport)
  5448.     else
  5449.       @sprites["pokemon35"] = PokemonPartyCancelSprite.new(@viewport)
  5450.     end
  5451.     elsif $Trainer.partyplus==32
  5452.     if @multiselect
  5453.       @sprites["pokemon32"] = PokemonPartyConfirmSprite.new(@viewport)
  5454.       @sprites["pokemon33"] = PokemonPartyCancelSprite2.new(@viewport)
  5455.     else
  5456.       @sprites["pokemon32"] = PokemonPartyCancelSprite.new(@viewport)
  5457.     end
  5458.     elsif $Trainer.partyplus==30
  5459.     if @multiselect
  5460.       @sprites["pokemon30"] = PokemonPartyConfirmSprite.new(@viewport)
  5461.       @sprites["pokemon31"] = PokemonPartyCancelSprite2.new(@viewport)
  5462.     else
  5463.       @sprites["pokemon30"] = PokemonPartyCancelSprite.new(@viewport)
  5464.     end
  5465.     elsif $Trainer.partyplus==28
  5466.     if @multiselect
  5467.       @sprites["pokemon28"] = PokemonPartyConfirmSprite.new(@viewport)
  5468.       @sprites["pokemon29"] = PokemonPartyCancelSprite2.new(@viewport)
  5469.     else
  5470.       @sprites["pokemon28"] = PokemonPartyCancelSprite.new(@viewport)
  5471.     end
  5472.     elsif $Trainer.partyplus==25
  5473.     if @multiselect
  5474.       @sprites["pokemon25"] = PokemonPartyConfirmSprite.new(@viewport)
  5475.       @sprites["pokemon26"] = PokemonPartyCancelSprite2.new(@viewport)
  5476.     else
  5477.       @sprites["pokemon25"] = PokemonPartyCancelSprite.new(@viewport)
  5478.     end
  5479.     elsif $Trainer.partyplus==24
  5480.     if @multiselect
  5481.       @sprites["pokemon24"] = PokemonPartyConfirmSprite.new(@viewport)
  5482.       @sprites["pokemon25"] = PokemonPartyCancelSprite2.new(@viewport)
  5483.     else
  5484.       @sprites["pokemon24"] = PokemonPartyCancelSprite.new(@viewport)
  5485.     end
  5486.     elsif $Trainer.partyplus==21
  5487.     if @multiselect
  5488.       @sprites["pokemon21"] = PokemonPartyConfirmSprite.new(@viewport)
  5489.       @sprites["pokemon22"] = PokemonPartyCancelSprite2.new(@viewport)
  5490.     else
  5491.       @sprites["pokemon21"] = PokemonPartyCancelSprite.new(@viewport)
  5492.     end
  5493.     elsif $Trainer.partyplus==20
  5494.     if @multiselect
  5495.       @sprites["pokemon20"] = PokemonPartyConfirmSprite.new(@viewport)
  5496.       @sprites["pokemon21"] = PokemonPartyCancelSprite2.new(@viewport)
  5497.     else
  5498.       @sprites["pokemon20"] = PokemonPartyCancelSprite.new(@viewport)
  5499.     end
  5500.     elsif $Trainer.partyplus==18
  5501.     if @multiselect
  5502.       @sprites["pokemon18"] = PokemonPartyConfirmSprite.new(@viewport)
  5503.       @sprites["pokemon19"] = PokemonPartyCancelSprite2.new(@viewport)
  5504.     else
  5505.       @sprites["pokemon18"] = PokemonPartyCancelSprite.new(@viewport)
  5506.     end
  5507.     elsif $Trainer.partyplus==16
  5508.     if @multiselect
  5509.       @sprites["pokemon16"] = PokemonPartyConfirmSprite.new(@viewport)
  5510.       @sprites["pokemon17"] = PokemonPartyCancelSprite2.new(@viewport)
  5511.     else
  5512.       @sprites["pokemon16"] = PokemonPartyCancelSprite.new(@viewport)
  5513.     end
  5514.     elsif $Trainer.partyplus==15
  5515.     if @multiselect
  5516.       @sprites["pokemon15"] = PokemonPartyConfirmSprite.new(@viewport)
  5517.       @sprites["pokemon16"] = PokemonPartyCancelSprite2.new(@viewport)
  5518.     else
  5519.       @sprites["pokemon15"] = PokemonPartyCancelSprite.new(@viewport)
  5520.     end
  5521.     elsif $Trainer.partyplus==14
  5522.     if @multiselect
  5523.       @sprites["pokemon14"] = PokemonPartyConfirmSprite.new(@viewport)
  5524.       @sprites["pokemon15"] = PokemonPartyCancelSprite2.new(@viewport)
  5525.     else
  5526.       @sprites["pokemon14"] = PokemonPartyCancelSprite.new(@viewport)
  5527.     end
  5528.     elsif $Trainer.partyplus==12
  5529.     if @multiselect
  5530.       @sprites["pokemon12"] = PokemonPartyConfirmSprite.new(@viewport)
  5531.       @sprites["pokemon13"] = PokemonPartyCancelSprite2.new(@viewport)
  5532.     else
  5533.       @sprites["pokemon12"] = PokemonPartyCancelSprite.new(@viewport)
  5534.     end
  5535.     elsif $Trainer.partyplus==10
  5536.     if @multiselect
  5537.       @sprites["pokemon10"] = PokemonPartyConfirmSprite.new(@viewport)
  5538.       @sprites["pokemon11"] = PokemonPartyCancelSprite2.new(@viewport)
  5539.     else
  5540.       @sprites["pokemon10"] = PokemonPartyCancelSprite.new(@viewport)
  5541.     end
  5542.     elsif $Trainer.partyplus==9
  5543.     if @multiselect
  5544.       @sprites["pokemon9"] = PokemonPartyConfirmSprite.new(@viewport)
  5545.       @sprites["pokemon10"] = PokemonPartyCancelSprite2.new(@viewport)
  5546.     else
  5547.       @sprites["pokemon9"] = PokemonPartyCancelSprite.new(@viewport)
  5548.     end
  5549.     elsif $Trainer.partyplus==8
  5550.     if @multiselect
  5551.       @sprites["pokemon8"] = PokemonPartyConfirmSprite.new(@viewport)
  5552.       @sprites["pokemon9"] = PokemonPartyCancelSprite2.new(@viewport)
  5553.     else
  5554.       @sprites["pokemon8"] = PokemonPartyCancelSprite.new(@viewport)
  5555.     end
  5556.     elsif $Trainer.partyplus==5
  5557.     if @multiselect
  5558.       @sprites["pokemon5"] = PokemonPartyConfirmSprite.new(@viewport)
  5559.       @sprites["pokemon6"] = PokemonPartyCancelSprite2.new(@viewport)
  5560.     else
  5561.       @sprites["pokemon5"] = PokemonPartyCancelSprite.new(@viewport)
  5562.     end
  5563.     elsif $Trainer.partyplus==4
  5564.     if @multiselect
  5565.       @sprites["pokemon4"] = PokemonPartyConfirmSprite.new(@viewport)
  5566.       @sprites["pokemon5"] = PokemonPartyCancelSprite2.new(@viewport)
  5567.     else
  5568.       @sprites["pokemon4"] = PokemonPartyCancelSprite.new(@viewport)
  5569.     end
  5570.     elsif $Trainer.partyplus==3
  5571.     if @multiselect
  5572.       @sprites["pokemon3"] = PokemonPartyConfirmSprite.new(@viewport)
  5573.       @sprites["pokemon4"] = PokemonPartyCancelSprite2.new(@viewport)
  5574.     else
  5575.       @sprites["pokemon3"] = PokemonPartyCancelSprite.new(@viewport)
  5576.     end
  5577.     elsif $Trainer.partyplus==2
  5578.     if @multiselect
  5579.       @sprites["pokemon2"] = PokemonPartyConfirmSprite.new(@viewport)
  5580.       @sprites["pokemon3"] = PokemonPartyCancelSprite2.new(@viewport)
  5581.     else
  5582.       @sprites["pokemon2"] = PokemonPartyCancelSprite.new(@viewport)
  5583.     end
  5584.     elsif $Trainer.partyplus==1
  5585.     if @multiselect
  5586.       @sprites["pokemon1"] = PokemonPartyConfirmSprite.new(@viewport)
  5587.       @sprites["pokemon2"] = PokemonPartyCancelSprite2.new(@viewport)
  5588.     else
  5589.       @sprites["pokemon1"] = PokemonPartyCancelSprite.new(@viewport)
  5590.     end
  5591.     else
  5592.     if @multiselect
  5593.       @sprites["pokemon6"] = PokemonPartyConfirmSprite.new(@viewport)
  5594.       @sprites["pokemon7"] = PokemonPartyCancelSprite2.new(@viewport)
  5595.     else
  5596.       @sprites["pokemon6"] = PokemonPartyCancelSprite.new(@viewport)
  5597.     end
  5598.     end
  5599.     # Select first Pokémon
  5600.     @activecmd = 0
  5601.     @sprites["pokemon0"].selected = true
  5602.     pbFadeInAndShow(@sprites) { update }
  5603.   end
  5604.   def pbEndScene
  5605.     pbFadeOutAndHide(@sprites) { update }
  5606.     pbDisposeSpriteHash(@sprites)
  5607.     @viewport.dispose
  5608.   end
  5609.   def pbDisplay(text)
  5610.     @sprites["messagebox"].text    = text
  5611.     @sprites["messagebox"].visible = true
  5612.     @sprites["helpwindow"].visible = false
  5613.     pbPlayDecisionSE
  5614.     loop do
  5615.       Graphics.update
  5616.       Input.update
  5617.       self.update
  5618.       if @sprites["messagebox"].busy?
  5619.         if Input.trigger?(Input::C)
  5620.           pbPlayDecisionSE if @sprites["messagebox"].pausing?
  5621.           @sprites["messagebox"].resume
  5622.         end
  5623.       else
  5624.         if Input.trigger?(Input::B) || Input.trigger?(Input::C)
  5625.           break
  5626.         end
  5627.       end
  5628.     end
  5629.     @sprites["messagebox"].visible = false
  5630.     @sprites["helpwindow"].visible = true
  5631.   end
  5632.   def pbDisplayConfirm(text)
  5633.     ret = -1
  5634.     @sprites["messagebox"].text    = text
  5635.     @sprites["messagebox"].visible = true
  5636.     @sprites["helpwindow"].visible = false
  5637.     using(cmdwindow = Window_CommandPokemon.new([_INTL("Yes"),_INTL("No")])) {
  5638.       cmdwindow.visible = false
  5639.       pbBottomRight(cmdwindow)
  5640.       cmdwindow.y -= @sprites["messagebox"].height
  5641.       cmdwindow.z = @viewport.z+1
  5642.       loop do
  5643.         Graphics.update
  5644.         Input.update
  5645.         cmdwindow.visible = true if !@sprites["messagebox"].busy?
  5646.         cmdwindow.update
  5647.         self.update
  5648.         if !@sprites["messagebox"].busy?
  5649.           if Input.trigger?(Input::B)
  5650.             ret = false
  5651.             break
  5652.           elsif Input.trigger?(Input::C) && @sprites["messagebox"].resume
  5653.             ret = (cmdwindow.index==0)
  5654.             break
  5655.           end
  5656.         end
  5657.       end
  5658.     }
  5659.     @sprites["messagebox"].visible = false
  5660.     @sprites["helpwindow"].visible = true
  5661.     return ret
  5662.   end
  5663.   def pbShowCommands(helptext,commands,index=0)
  5664.     ret = -1
  5665.     helpwindow = @sprites["helpwindow"]
  5666.     helpwindow.visible = true
  5667.     using(cmdwindow = Window_CommandPokemonColor.new(commands)) {
  5668.       cmdwindow.z     = @viewport.z+1
  5669.       cmdwindow.index = index
  5670.       pbBottomRight(cmdwindow)
  5671.       helpwindow.resizeHeightToFit(helptext,Graphics.width-cmdwindow.width)
  5672.       helpwindow.text = helptext
  5673.       pbBottomLeft(helpwindow)
  5674.       loop do
  5675.         Graphics.update
  5676.         Input.update
  5677.         cmdwindow.update
  5678.         self.update
  5679.         if Input.trigger?(Input::B)
  5680.           pbPlayCancelSE
  5681.           ret = -1
  5682.           break
  5683.         elsif Input.trigger?(Input::C)
  5684.           pbPlayDecisionSE
  5685.           ret = cmdwindow.index
  5686.           break
  5687.         end
  5688.       end
  5689.     }
  5690.     return ret
  5691.   end
  5692.   def pbMessageFreeText(text,startMsg,maxlength)   # Unused
  5693.     return Kernel.pbMessageFreeText(
  5694.        _INTL("Please enter a message (max. {1} characters).",maxlength),
  5695.        startMsg,false,maxlength,Graphics.width) { update }
  5696.   end
  5697.   def pbSetHelpText(helptext)
  5698.     helpwindow = @sprites["helpwindow"]
  5699.     pbBottomLeftLines(helpwindow,1)
  5700.     helpwindow.text = helptext
  5701.     helpwindow.width = 398
  5702.     helpwindow.visible = true
  5703.   end
  5704.   def pbAnnotate(annot)
  5705.     if $Trainer.partyplus>=1
  5706.     for i in 0...$Trainer.partyplus
  5707.       @sprites["pokemon#{i}"].text = (annot) ? annot[i] : nil
  5708.     end
  5709.     else
  5710.     for i in 0...6
  5711.       @sprites["pokemon#{i}"].text = (annot) ? annot[i] : nil
  5712.     end
  5713.     end
  5714.   end
  5715.   def pbSelect(item)
  5716.     @activecmd = item
  5717.     if $Trainer.partyplus==40
  5718.     numsprites = (@multiselect) ? 42 : 41
  5719.     elsif $Trainer.partyplus==35
  5720.     numsprites = (@multiselect) ? 37 : 36
  5721.     elsif $Trainer.partyplus==32
  5722.     numsprites = (@multiselect) ? 33 : 32
  5723.     elsif $Trainer.partyplus==30
  5724.     numsprites = (@multiselect) ? 32 : 31
  5725.     elsif $Trainer.partyplus==28
  5726.     numsprites = (@multiselect) ? 30 : 29
  5727.     elsif $Trainer.partyplus==25
  5728.     numsprites = (@multiselect) ? 27 : 26
  5729.     elsif $Trainer.partyplus==24
  5730.     numsprites = (@multiselect) ? 26 : 25
  5731.     elsif $Trainer.partyplus==21
  5732.     numsprites = (@multiselect) ? 23 : 22
  5733.     elsif $Trainer.partyplus==20
  5734.     numsprites = (@multiselect) ? 22 : 21
  5735.     elsif $Trainer.partyplus==18
  5736.     numsprites = (@multiselect) ? 20 : 19
  5737.     elsif $Trainer.partyplus==16
  5738.     numsprites = (@multiselect) ? 18 : 17
  5739.     elsif $Trainer.partyplus==15
  5740.     numsprites = (@multiselect) ? 17 : 16
  5741.     elsif $Trainer.partyplus==14
  5742.     numsprites = (@multiselect) ? 16 : 15
  5743.     elsif $Trainer.partyplus==12
  5744.     numsprites = (@multiselect) ? 14 : 13
  5745.     elsif $Trainer.partyplus==10
  5746.     numsprites = (@multiselect) ? 12 : 11
  5747.     elsif $Trainer.partyplus==9
  5748.     numsprites = (@multiselect) ? 11 : 10
  5749.     elsif $Trainer.partyplus==8
  5750.     numsprites = (@multiselect) ? 10 : 9
  5751.     elsif $Trainer.partyplus==5
  5752.     numsprites = (@multiselect) ? 7 : 6
  5753.     elsif $Trainer.partyplus==4
  5754.     numsprites = (@multiselect) ? 6 : 5
  5755.     elsif $Trainer.partyplus==3
  5756.     numsprites = (@multiselect) ? 5 : 4
  5757.     elsif $Trainer.partyplus==2
  5758.     numsprites = (@multiselect) ? 4 : 3
  5759.     elsif $Trainer.partyplus==1
  5760.     numsprites = (@multiselect) ? 3 : 2
  5761.     else
  5762.     numsprites = (@multiselect) ? 8 : 7
  5763.     end
  5764.     for i in 0...numsprites
  5765.       @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5766.     end
  5767.   end
  5768.   def pbPreSelect(item)
  5769.     @activecmd = item
  5770.   end
  5771.   def pbSwitchBegin(oldid,newid)
  5772.     oldsprite = @sprites["pokemon#{oldid}"]
  5773.     newsprite = @sprites["pokemon#{newid}"]
  5774.     16.times do
  5775.       oldsprite.x += (oldid&1)==0 ? -16 : 16
  5776.       newsprite.x += (newid&1)==0 ? -16 : 16
  5777.       Graphics.update
  5778.       Input.update
  5779.       self.update
  5780.     end
  5781.   end
  5782.   def pbSwitchEnd(oldid,newid)
  5783.     oldsprite = @sprites["pokemon#{oldid}"]
  5784.     newsprite = @sprites["pokemon#{newid}"]
  5785.     oldsprite.pokemon = @party[oldid]
  5786.     newsprite.pokemon = @party[newid]
  5787.     16.times do
  5788.       oldsprite.x -= (oldid&1)==0 ? -16 : 16
  5789.       newsprite.x -= (newid&1)==0 ? -16 : 16
  5790.       Graphics.update
  5791.       Input.update
  5792.       self.update
  5793.     end
  5794.     if $Trainer.partyplus>=1
  5795.     for i in 0...$Trainer.partyplus
  5796.       @sprites["pokemon#{i}"].preselected = false
  5797.       @sprites["pokemon#{i}"].switching   = false
  5798.     end
  5799.     else
  5800.     for i in 0...6
  5801.       @sprites["pokemon#{i}"].preselected = false
  5802.       @sprites["pokemon#{i}"].switching   = false
  5803.     end
  5804.     end
  5805.     pbRefresh
  5806.   end
  5807.   def pbClearSwitching
  5808.     if $Trainer.partyplus>=1
  5809.     for i in 0...$Trainer.partyplus
  5810.       @sprites["pokemon#{i}"].preselected = false
  5811.       @sprites["pokemon#{i}"].switching   = false
  5812.     end
  5813.     else
  5814.     for i in 0...6
  5815.       @sprites["pokemon#{i}"].preselected = false
  5816.       @sprites["pokemon#{i}"].switching   = false
  5817.     end
  5818. end
  5819.   end
  5820.   def pbSummary(pkmnid)
  5821.     oldsprites = pbFadeOutAndHide(@sprites)
  5822.     scene = PokemonSummary_Scene.new
  5823.     screen = PokemonSummaryScreen.new(scene)
  5824.     screen.pbStartScreen(@party,pkmnid)
  5825.     pbFadeInAndShow(@sprites,oldsprites)
  5826.   end
  5827.   def pbChooseItem(bag)
  5828.     ret = 0
  5829.     pbFadeOutIn(99999){
  5830.       scene = PokemonBag_Scene.new
  5831.       screen = PokemonBagScreen.new(scene,bag)
  5832.       ret = screen.pbChooseItemScreen(Proc.new{|item| pbCanHoldItem?(item) })
  5833.     }
  5834.     return ret
  5835.   end
  5836.   def pbUseItem(bag,pokemon)
  5837.     ret = 0
  5838.     pbFadeOutIn(99999){
  5839.       scene = PokemonBag_Scene.new
  5840.       screen = PokemonBagScreen.new(scene,bag)
  5841.       ret = screen.pbChooseItemScreen(Proc.new{|item|
  5842.         next false if !pbCanUseOnPokemon?(item)
  5843.         if pbIsMachine?(item)
  5844.           move = pbGetMachine(item)
  5845.           next false if pokemon.hasMove?(move) || !pokemon.isCompatibleWithMove?(move)
  5846.         end
  5847.         next true
  5848.       })
  5849.     }
  5850.     return ret
  5851.   end
  5852.   def pbChoosePokemon(switching=false,initialsel=-1,canswitch=0)
  5853.     if $Trainer.partyplus>=1
  5854.     for i in 0...$Trainer.partyplus
  5855.       @sprites["pokemon#{i}"].preselected = (switching && i==@activecmd)
  5856.       @sprites["pokemon#{i}"].switching   = switching
  5857.     end
  5858.     else
  5859.     for i in 0...6
  5860.       @sprites["pokemon#{i}"].preselected = (switching && i==@activecmd)
  5861.       @sprites["pokemon#{i}"].switching   = switching
  5862.     end
  5863.     end
  5864.     @activecmd = initialsel if initialsel>=0
  5865.     pbRefresh
  5866.     loop do
  5867.       Graphics.update
  5868.       Input.update
  5869.       self.update
  5870.       oldsel = @activecmd
  5871.       key = -1
  5872.       key = Input::DOWN if Input.repeat?(Input::DOWN)
  5873.       key = Input::RIGHT if Input.repeat?(Input::RIGHT)
  5874.       key = Input::LEFT if Input.repeat?(Input::LEFT)
  5875.       key = Input::UP if Input.repeat?(Input::UP)
  5876.       if key>=0
  5877.         @activecmd = pbChangeSelection(key,@activecmd)
  5878.       end
  5879.     if $Trainer.partyplus==40
  5880.       if @activecmd!=oldsel # Changing selection
  5881.         pbPlayCursorSE
  5882.         numsprites = (@multiselect) ? 42 : 41
  5883.         for i in 0...numsprites
  5884.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5885.         end
  5886.       end
  5887.       cancelsprite = (@multiselect) ? 41 : 40
  5888.     elsif $Trainer.partyplus==35
  5889.       if @activecmd!=oldsel # Changing selection
  5890.         pbPlayCursorSE
  5891.         numsprites = (@multiselect) ? 37 : 36
  5892.         for i in 0...numsprites
  5893.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5894.         end
  5895.       end
  5896.       cancelsprite = (@multiselect) ? 36 : 35
  5897.     elsif $Trainer.partyplus==32
  5898.       if @activecmd!=oldsel # Changing selection
  5899.         pbPlayCursorSE
  5900.         numsprites = (@multiselect) ? 34 : 33
  5901.         for i in 0...numsprites
  5902.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5903.         end
  5904.       end
  5905.       cancelsprite = (@multiselect) ? 33 : 32
  5906.     elsif $Trainer.partyplus==30
  5907.       if @activecmd!=oldsel # Changing selection
  5908.         pbPlayCursorSE
  5909.         numsprites = (@multiselect) ? 32 : 31
  5910.         for i in 0...numsprites
  5911.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5912.         end
  5913.       end
  5914.       cancelsprite = (@multiselect) ? 31 : 30
  5915.     elsif $Trainer.partyplus==28
  5916.       if @activecmd!=oldsel # Changing selection
  5917.         pbPlayCursorSE
  5918.         numsprites = (@multiselect) ? 30 : 29
  5919.         for i in 0...numsprites
  5920.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5921.         end
  5922.       end
  5923.       cancelsprite = (@multiselect) ? 29 : 28
  5924.     elsif $Trainer.partyplus==25
  5925.       if @activecmd!=oldsel # Changing selection
  5926.         pbPlayCursorSE
  5927.         numsprites = (@multiselect) ? 27 : 26
  5928.         for i in 0...numsprites
  5929.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5930.         end
  5931.       end
  5932.       cancelsprite = (@multiselect) ? 26 : 25
  5933.     elsif $Trainer.partyplus==24
  5934.       if @activecmd!=oldsel # Changing selection
  5935.         pbPlayCursorSE
  5936.         numsprites = (@multiselect) ? 26 : 25
  5937.         for i in 0...numsprites
  5938.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5939.         end
  5940.       end
  5941.       cancelsprite = (@multiselect) ? 25 : 24
  5942.     elsif $Trainer.partyplus==21
  5943.       if @activecmd!=oldsel # Changing selection
  5944.         pbPlayCursorSE
  5945.         numsprites = (@multiselect) ? 23 : 22
  5946.         for i in 0...numsprites
  5947.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5948.         end
  5949.       end
  5950.       cancelsprite = (@multiselect) ? 22 : 21
  5951.     elsif $Trainer.partyplus==20
  5952.       if @activecmd!=oldsel # Changing selection
  5953.         pbPlayCursorSE
  5954.         numsprites = (@multiselect) ? 22 : 21
  5955.         for i in 0...numsprites
  5956.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5957.         end
  5958.       end
  5959.       cancelsprite = (@multiselect) ? 21 : 20
  5960.     elsif $Trainer.partyplus==18
  5961.       if @activecmd!=oldsel # Changing selection
  5962.         pbPlayCursorSE
  5963.         numsprites = (@multiselect) ? 20 : 19
  5964.         for i in 0...numsprites
  5965.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5966.         end
  5967.       end
  5968.       cancelsprite = (@multiselect) ? 19 : 18
  5969.     elsif $Trainer.partyplus==16
  5970.       if @activecmd!=oldsel # Changing selection
  5971.         pbPlayCursorSE
  5972.         numsprites = (@multiselect) ? 18 : 17
  5973.         for i in 0...numsprites
  5974.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5975.         end
  5976.       end
  5977.       cancelsprite = (@multiselect) ? 17 : 16
  5978.     elsif $Trainer.partyplus==15
  5979.       if @activecmd!=oldsel # Changing selection
  5980.         pbPlayCursorSE
  5981.         numsprites = (@multiselect) ? 17 : 16
  5982.         for i in 0...numsprites
  5983.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5984.         end
  5985.       end
  5986.       cancelsprite = (@multiselect) ? 16 : 15
  5987.     elsif $Trainer.partyplus==14
  5988.       if @activecmd!=oldsel # Changing selection
  5989.         pbPlayCursorSE
  5990.         numsprites = (@multiselect) ? 16 : 15
  5991.         for i in 0...numsprites
  5992.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  5993.         end
  5994.       end
  5995.       cancelsprite = (@multiselect) ? 15 : 14
  5996.     elsif $Trainer.partyplus==12
  5997.       if @activecmd!=oldsel # Changing selection
  5998.         pbPlayCursorSE
  5999.         numsprites = (@multiselect) ? 14 : 13
  6000.         for i in 0...numsprites
  6001.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6002.         end
  6003.       end
  6004.       cancelsprite = (@multiselect) ? 13 : 12
  6005.     elsif $Trainer.partyplus==10
  6006.       if @activecmd!=oldsel # Changing selection
  6007.         pbPlayCursorSE
  6008.         numsprites = (@multiselect) ? 12 : 11
  6009.         for i in 0...numsprites
  6010.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6011.         end
  6012.       end
  6013.       cancelsprite = (@multiselect) ? 11 : 10
  6014.     elsif $Trainer.partyplus==9
  6015.       if @activecmd!=oldsel # Changing selection
  6016.         pbPlayCursorSE
  6017.         numsprites = (@multiselect) ? 11 : 10
  6018.         for i in 0...numsprites
  6019.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6020.         end
  6021.       end
  6022.       cancelsprite = (@multiselect) ? 10 : 9
  6023.     elsif $Trainer.partyplus==8
  6024.       if @activecmd!=oldsel # Changing selection
  6025.         pbPlayCursorSE
  6026.         numsprites = (@multiselect) ? 10 : 9
  6027.         for i in 0...numsprites
  6028.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6029.         end
  6030.       end
  6031.       cancelsprite = (@multiselect) ? 9 : 8
  6032.     elsif $Trainer.partyplus==5
  6033.       if @activecmd!=oldsel # Changing selection
  6034.         pbPlayCursorSE
  6035.         numsprites = (@multiselect) ? 7 : 6
  6036.         for i in 0...numsprites
  6037.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6038.         end
  6039.       end
  6040.       cancelsprite = (@multiselect) ? 6 : 5
  6041.     elsif $Trainer.partyplus==4
  6042.       if @activecmd!=oldsel # Changing selection
  6043.         pbPlayCursorSE
  6044.         numsprites = (@multiselect) ? 6 : 5
  6045.         for i in 0...numsprites
  6046.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6047.         end
  6048.       end
  6049.       cancelsprite = (@multiselect) ? 5 : 4
  6050.     elsif $Trainer.partyplus==3
  6051.       if @activecmd!=oldsel # Changing selection
  6052.         pbPlayCursorSE
  6053.         numsprites = (@multiselect) ? 5 : 4
  6054.         for i in 0...numsprites
  6055.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6056.         end
  6057.       end
  6058.       cancelsprite = (@multiselect) ? 4 : 3
  6059.     elsif $Trainer.partyplus==2
  6060.       if @activecmd!=oldsel # Changing selection
  6061.         pbPlayCursorSE
  6062.         numsprites = (@multiselect) ? 4 : 3
  6063.         for i in 0...numsprites
  6064.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6065.         end
  6066.       end
  6067.       cancelsprite = (@multiselect) ? 3 : 2
  6068.     elsif $Trainer.partyplus==1
  6069.       if @activecmd!=oldsel # Changing selection
  6070.         pbPlayCursorSE
  6071.         numsprites = (@multiselect) ? 3 : 2
  6072.         for i in 0...numsprites
  6073.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6074.         end
  6075.       end
  6076.       cancelsprite = (@multiselect) ? 2 : 1
  6077.     else
  6078.       if @activecmd!=oldsel # Changing selection
  6079.         pbPlayCursorSE
  6080.         numsprites = (@multiselect) ? 8 : 7
  6081.         for i in 0...numsprites
  6082.           @sprites["pokemon#{i}"].selected = (i==@activecmd)
  6083.         end
  6084.       end
  6085.       cancelsprite = (@multiselect) ? 7 : 6
  6086.     end
  6087.       if Input.trigger?(Input::A) && canswitch==1 && @activecmd!=cancelsprite
  6088.         pbPlayDecisionSE
  6089.         return [1,@activecmd]
  6090.       elsif Input.trigger?(Input::A) && canswitch==2
  6091.         return -1
  6092.       elsif Input.trigger?(Input::B)
  6093.         return -1
  6094.       elsif Input.trigger?(Input::C)
  6095.         pbPlayDecisionSE
  6096.         return (@activecmd==cancelsprite) ? -1 : @activecmd
  6097.       end
  6098.     end
  6099.   end
  6100.   def pbChangeSelection(key,currentsel)
  6101. if $Trainer.partyplus==40
  6102.     numsprites = (@multiselect) ? 42 : 41
  6103.     case key
  6104.     when Input::LEFT
  6105.       begin
  6106.         currentsel -= 1
  6107.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6108.       if currentsel>=@party.length && currentsel<40
  6109.         currentsel = @party.length-1
  6110.       end
  6111.       currentsel = numsprites-1 if currentsel<0
  6112.     when Input::RIGHT
  6113.       begin
  6114.         currentsel += 1
  6115.       end while currentsel<@party.length && !@party[currentsel]
  6116.       if currentsel==@party.length
  6117.         currentsel = 40
  6118.       elsif currentsel==numsprites
  6119.         currentsel = 0
  6120.       end
  6121.     when Input::UP
  6122.       if currentsel>=40
  6123.         begin
  6124.           currentsel -= 1
  6125.         end while currentsel>0 && !@party[currentsel]
  6126.       else
  6127.         begin
  6128.           currentsel -= 8
  6129.         end while currentsel>0 && !@party[currentsel]
  6130.       end
  6131.       if currentsel>=@party.length && currentsel<40
  6132.         currentsel = @party.length-1
  6133.       end
  6134.       currentsel = numsprites-1 if currentsel<0
  6135.     when Input::DOWN
  6136.       if currentsel>=33
  6137.         currentsel = 40
  6138.       else
  6139.         currentsel += 8
  6140.         currentsel = 40 if currentsel<40 && !@party[currentsel]
  6141.       end
  6142.       if currentsel>=@party.length && currentsel<40
  6143.         currentsel = 40
  6144.       elsif currentsel>=numsprites
  6145.         currentsel = 0
  6146.       end
  6147.     end
  6148. elsif $Trainer.partyplus==35
  6149.     numsprites = (@multiselect) ? 37 : 36
  6150.     case key
  6151.     when Input::LEFT
  6152.       begin
  6153.         currentsel -= 1
  6154.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6155.       if currentsel>=@party.length && currentsel<35
  6156.         currentsel = @party.length-1
  6157.       end
  6158.       currentsel = numsprites-1 if currentsel<0
  6159.     when Input::RIGHT
  6160.       begin
  6161.         currentsel += 1
  6162.       end while currentsel<@party.length && !@party[currentsel]
  6163.       if currentsel==@party.length
  6164.         currentsel = 35
  6165.       elsif currentsel==numsprites
  6166.         currentsel = 0
  6167.       end
  6168.     when Input::UP
  6169.       if currentsel>=35
  6170.         begin
  6171.           currentsel -= 1
  6172.         end while currentsel>0 && !@party[currentsel]
  6173.       else
  6174.         begin
  6175.           currentsel -= 7
  6176.         end while currentsel>0 && !@party[currentsel]
  6177.       end
  6178.       if currentsel>=@party.length && currentsel<35
  6179.         currentsel = @party.length-1
  6180.       end
  6181.       currentsel = numsprites-1 if currentsel<0
  6182.     when Input::DOWN
  6183.       if currentsel>=28
  6184.         currentsel = 35
  6185.       else
  6186.         currentsel += 7
  6187.         currentsel = 35 if currentsel<35 && !@party[currentsel]
  6188.       end
  6189.       if currentsel>=@party.length && currentsel<35
  6190.         currentsel = 35
  6191.       elsif currentsel>=numsprites
  6192.         currentsel = 0
  6193.       end
  6194.     end
  6195. elsif $Trainer.partyplus==32
  6196.     numsprites = (@multiselect) ? 34 : 33
  6197.     case key
  6198.     when Input::LEFT
  6199.       begin
  6200.         currentsel -= 1
  6201.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6202.       if currentsel>=@party.length && currentsel<32
  6203.         currentsel = @party.length-1
  6204.       end
  6205.       currentsel = numsprites-1 if currentsel<0
  6206.     when Input::RIGHT
  6207.       begin
  6208.         currentsel += 1
  6209.       end while currentsel<@party.length && !@party[currentsel]
  6210.       if currentsel==@party.length
  6211.         currentsel = 32
  6212.       elsif currentsel==numsprites
  6213.         currentsel = 0
  6214.       end
  6215.     when Input::UP
  6216.       if currentsel>=32
  6217.         begin
  6218.           currentsel -= 1
  6219.         end while currentsel>0 && !@party[currentsel]
  6220.       else
  6221.         begin
  6222.           currentsel -= 6
  6223.         end while currentsel>0 && !@party[currentsel]
  6224.       end
  6225.       if currentsel>=@party.length && currentsel<32
  6226.         currentsel = @party.length-1
  6227.       end
  6228.       currentsel = numsprites-1 if currentsel<0
  6229.     when Input::DOWN
  6230.       if currentsel>=25
  6231.         currentsel = 32
  6232.       else
  6233.         currentsel += 6
  6234.         currentsel = 32 if currentsel<32 && !@party[currentsel]
  6235.       end
  6236.       if currentsel>=@party.length && currentsel<32
  6237.         currentsel = 32
  6238.       elsif currentsel>=numsprites
  6239.         currentsel = 0
  6240.       end
  6241.     end
  6242. elsif $Trainer.partyplus==30
  6243.     numsprites = (@multiselect) ? 32 : 31
  6244.     case key
  6245.     when Input::LEFT
  6246.       begin
  6247.         currentsel -= 1
  6248.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6249.       if currentsel>=@party.length && currentsel<30
  6250.         currentsel = @party.length-1
  6251.       end
  6252.       currentsel = numsprites-1 if currentsel<0
  6253.     when Input::RIGHT
  6254.       begin
  6255.         currentsel += 1
  6256.       end while currentsel<@party.length && !@party[currentsel]
  6257.       if currentsel==@party.length
  6258.         currentsel = 30
  6259.       elsif currentsel==numsprites
  6260.         currentsel = 0
  6261.       end
  6262.     when Input::UP
  6263.       if currentsel>=30
  6264.         begin
  6265.           currentsel -= 1
  6266.         end while currentsel>0 && !@party[currentsel]
  6267.       else
  6268.         begin
  6269.           currentsel -= 6
  6270.         end while currentsel>0 && !@party[currentsel]
  6271.       end
  6272.       if currentsel>=@party.length && currentsel<30
  6273.         currentsel = @party.length-1
  6274.       end
  6275.       currentsel = numsprites-1 if currentsel<0
  6276.     when Input::DOWN
  6277.       if currentsel>=25
  6278.         currentsel = 30
  6279.       else
  6280.         currentsel += 6
  6281.         currentsel = 30 if currentsel<30 && !@party[currentsel]
  6282.       end
  6283.       if currentsel>=@party.length && currentsel<30
  6284.         currentsel = 30
  6285.       elsif currentsel>=numsprites
  6286.         currentsel = 0
  6287.       end
  6288.     end
  6289. elsif $Trainer.partyplus==28
  6290.     numsprites = (@multiselect) ? 30 : 29
  6291.     case key
  6292.     when Input::LEFT
  6293.       begin
  6294.         currentsel -= 1
  6295.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6296.       if currentsel>=@party.length && currentsel<28
  6297.         currentsel = @party.length-1
  6298.       end
  6299.       currentsel = numsprites-1 if currentsel<0
  6300.     when Input::RIGHT
  6301.       begin
  6302.         currentsel += 1
  6303.       end while currentsel<@party.length && !@party[currentsel]
  6304.       if currentsel==@party.length
  6305.         currentsel = 28
  6306.       elsif currentsel==numsprites
  6307.         currentsel = 0
  6308.       end
  6309.     when Input::UP
  6310.       if currentsel>=28
  6311.         begin
  6312.           currentsel -= 1
  6313.         end while currentsel>0 && !@party[currentsel]
  6314.       else
  6315.         begin
  6316.           currentsel -= 7
  6317.         end while currentsel>0 && !@party[currentsel]
  6318.       end
  6319.       if currentsel>=@party.length && currentsel<28
  6320.         currentsel = @party.length-1
  6321.       end
  6322.       currentsel = numsprites-1 if currentsel<0
  6323.     when Input::DOWN
  6324.       if currentsel>=22
  6325.         currentsel = 28
  6326.       else
  6327.         currentsel += 7
  6328.         currentsel = 28 if currentsel<28 && !@party[currentsel]
  6329.       end
  6330.       if currentsel>=@party.length && currentsel<28
  6331.         currentsel = 28
  6332.       elsif currentsel>=numsprites
  6333.         currentsel = 0
  6334.       end
  6335.     end
  6336. elsif $Trainer.partyplus==25
  6337.     numsprites = (@multiselect) ? 27 : 26
  6338.     case key
  6339.     when Input::LEFT
  6340.       begin
  6341.         currentsel -= 1
  6342.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6343.       if currentsel>=@party.length && currentsel<25
  6344.         currentsel = @party.length-1
  6345.       end
  6346.       currentsel = numsprites-1 if currentsel<0
  6347.     when Input::RIGHT
  6348.       begin
  6349.         currentsel += 1
  6350.       end while currentsel<@party.length && !@party[currentsel]
  6351.       if currentsel==@party.length
  6352.         currentsel = 25
  6353.       elsif currentsel==numsprites
  6354.         currentsel = 0
  6355.       end
  6356.     when Input::UP
  6357.       if currentsel>=25
  6358.         begin
  6359.           currentsel -= 1
  6360.         end while currentsel>0 && !@party[currentsel]
  6361.       else
  6362.         begin
  6363.           currentsel -= 5
  6364.         end while currentsel>0 && !@party[currentsel]
  6365.       end
  6366.       if currentsel>=@party.length && currentsel<25
  6367.         currentsel = @party.length-1
  6368.       end
  6369.       currentsel = numsprites-1 if currentsel<0
  6370.     when Input::DOWN
  6371.       if currentsel>=21
  6372.         currentsel = 25
  6373.       else
  6374.         currentsel += 5
  6375.         currentsel = 25 if currentsel<25 && !@party[currentsel]
  6376.       end
  6377.       if currentsel>=@party.length && currentsel<25
  6378.         currentsel = 25
  6379.       elsif currentsel>=numsprites
  6380.         currentsel = 0
  6381.       end
  6382.     end
  6383. elsif $Trainer.partyplus==24
  6384.     numsprites = (@multiselect) ? 26 : 25
  6385.     case key
  6386.     when Input::LEFT
  6387.       begin
  6388.         currentsel -= 1
  6389.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6390.       if currentsel>=@party.length && currentsel<24
  6391.         currentsel = @party.length-1
  6392.       end
  6393.       currentsel = numsprites-1 if currentsel<0
  6394.     when Input::RIGHT
  6395.       begin
  6396.         currentsel += 1
  6397.       end while currentsel<@party.length && !@party[currentsel]
  6398.       if currentsel==@party.length
  6399.         currentsel = 24
  6400.       elsif currentsel==numsprites
  6401.         currentsel = 0
  6402.       end
  6403.     when Input::UP
  6404.       if currentsel>=24
  6405.         begin
  6406.           currentsel -= 1
  6407.         end while currentsel>0 && !@party[currentsel]
  6408.       else
  6409.         begin
  6410.           currentsel -= 6
  6411.         end while currentsel>0 && !@party[currentsel]
  6412.       end
  6413.       if currentsel>=@party.length && currentsel<24
  6414.         currentsel = @party.length-1
  6415.       end
  6416.       currentsel = numsprites-1 if currentsel<0
  6417.     when Input::DOWN
  6418.       if currentsel>=19
  6419.         currentsel = 24
  6420.       else
  6421.         currentsel += 6
  6422.         currentsel = 24 if currentsel<24 && !@party[currentsel]
  6423.       end
  6424.       if currentsel>=@party.length && currentsel<24
  6425.         currentsel = 24
  6426.       elsif currentsel>=numsprites
  6427.         currentsel = 0
  6428.       end
  6429.     end
  6430. elsif $Trainer.partyplus==21
  6431.     numsprites = (@multiselect) ? 23 : 22
  6432.     case key
  6433.     when Input::LEFT
  6434.       begin
  6435.         currentsel -= 1
  6436.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6437.       if currentsel>=@party.length && currentsel<21
  6438.         currentsel = @party.length-1
  6439.       end
  6440.       currentsel = numsprites-1 if currentsel<0
  6441.     when Input::RIGHT
  6442.       begin
  6443.         currentsel += 1
  6444.       end while currentsel<@party.length && !@party[currentsel]
  6445.       if currentsel==@party.length
  6446.         currentsel = 21
  6447.       elsif currentsel==numsprites
  6448.         currentsel = 0
  6449.       end
  6450.     when Input::UP
  6451.       if currentsel>=21
  6452.         begin
  6453.           currentsel -= 1
  6454.         end while currentsel>0 && !@party[currentsel]
  6455.       else
  6456.         begin
  6457.           currentsel -= 7
  6458.         end while currentsel>0 && !@party[currentsel]
  6459.       end
  6460.       if currentsel>=@party.length && currentsel<21
  6461.         currentsel = @party.length-1
  6462.       end
  6463.       currentsel = numsprites-1 if currentsel<0
  6464.     when Input::DOWN
  6465.       if currentsel>=15
  6466.         currentsel = 21
  6467.       else
  6468.         currentsel += 7
  6469.         currentsel = 21 if currentsel<21 && !@party[currentsel]
  6470.       end
  6471.       if currentsel>=@party.length && currentsel<21
  6472.         currentsel = 21
  6473.       elsif currentsel>=numsprites
  6474.         currentsel = 0
  6475.       end
  6476.     end
  6477. elsif $Trainer.partyplus==20
  6478.     numsprites = (@multiselect) ? 22 : 21
  6479.     case key
  6480.     when Input::LEFT
  6481.       begin
  6482.         currentsel -= 1
  6483.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6484.       if currentsel>=@party.length && currentsel<20
  6485.         currentsel = @party.length-1
  6486.       end
  6487.       currentsel = numsprites-1 if currentsel<0
  6488.     when Input::RIGHT
  6489.       begin
  6490.         currentsel += 1
  6491.       end while currentsel<@party.length && !@party[currentsel]
  6492.       if currentsel==@party.length
  6493.         currentsel = 20
  6494.       elsif currentsel==numsprites
  6495.         currentsel = 0
  6496.       end
  6497.     when Input::UP
  6498.       if currentsel>=20
  6499.         begin
  6500.           currentsel -= 1
  6501.         end while currentsel>0 && !@party[currentsel]
  6502.       else
  6503.         begin
  6504.           currentsel -= 5
  6505.         end while currentsel>0 && !@party[currentsel]
  6506.       end
  6507.       if currentsel>=@party.length && currentsel<20
  6508.         currentsel = @party.length-1
  6509.       end
  6510.       currentsel = numsprites-1 if currentsel<0
  6511.     when Input::DOWN
  6512.       if currentsel>=16
  6513.         currentsel = 20
  6514.       else
  6515.         currentsel += 5
  6516.         currentsel = 20 if currentsel<20 && !@party[currentsel]
  6517.       end
  6518.       if currentsel>=@party.length && currentsel<20
  6519.         currentsel = 20
  6520.       elsif currentsel>=numsprites
  6521.         currentsel = 0
  6522.       end
  6523.     end
  6524. elsif $Trainer.partyplus==18
  6525.     numsprites = (@multiselect) ? 20 : 19
  6526.     case key
  6527.     when Input::LEFT
  6528.       begin
  6529.         currentsel -= 1
  6530.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6531.       if currentsel>=@party.length && currentsel<18
  6532.         currentsel = @party.length-1
  6533.       end
  6534.       currentsel = numsprites-1 if currentsel<0
  6535.     when Input::RIGHT
  6536.       begin
  6537.         currentsel += 1
  6538.       end while currentsel<@party.length && !@party[currentsel]
  6539.       if currentsel==@party.length
  6540.         currentsel = 18
  6541.       elsif currentsel==numsprites
  6542.         currentsel = 0
  6543.       end
  6544.     when Input::UP
  6545.       if currentsel>=18
  6546.         begin
  6547.           currentsel -= 1
  6548.         end while currentsel>0 && !@party[currentsel]
  6549.       else
  6550.         begin
  6551.           currentsel -= 6
  6552.         end while currentsel>0 && !@party[currentsel]
  6553.       end
  6554.       if currentsel>=@party.length && currentsel<18
  6555.         currentsel = @party.length-1
  6556.       end
  6557.       currentsel = numsprites-1 if currentsel<0
  6558.     when Input::DOWN
  6559.       if currentsel>=13
  6560.         currentsel = 18
  6561.       else
  6562.         currentsel += 6
  6563.         currentsel = 18 if currentsel<18 && !@party[currentsel]
  6564.       end
  6565.       if currentsel>=@party.length && currentsel<18
  6566.         currentsel = 18
  6567.       elsif currentsel>=numsprites
  6568.         currentsel = 0
  6569.       end
  6570.     end
  6571. elsif $Trainer.partyplus==16
  6572.     numsprites = (@multiselect) ? 18 : 17
  6573.     case key
  6574.     when Input::LEFT
  6575.       begin
  6576.         currentsel -= 1
  6577.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6578.       if currentsel>=@party.length && currentsel<16
  6579.         currentsel = @party.length-1
  6580.       end
  6581.       currentsel = numsprites-1 if currentsel<0
  6582.     when Input::RIGHT
  6583.       begin
  6584.         currentsel += 1
  6585.       end while currentsel<@party.length && !@party[currentsel]
  6586.       if currentsel==@party.length
  6587.         currentsel = 16
  6588.       elsif currentsel==numsprites
  6589.         currentsel = 0
  6590.       end
  6591.     when Input::UP
  6592.       if currentsel>=16
  6593.         begin
  6594.           currentsel -= 1
  6595.         end while currentsel>0 && !@party[currentsel]
  6596.       else
  6597.         begin
  6598.           currentsel -= 4
  6599.         end while currentsel>0 && !@party[currentsel]
  6600.       end
  6601.       if currentsel>=@party.length && currentsel<16
  6602.         currentsel = @party.length-1
  6603.       end
  6604.       currentsel = numsprites-1 if currentsel<0
  6605.     when Input::DOWN
  6606.       if currentsel>=13
  6607.         currentsel = 16
  6608.       else
  6609.         currentsel += 4
  6610.         currentsel = 16 if currentsel<16 && !@party[currentsel]
  6611.       end
  6612.       if currentsel>=@party.length && currentsel<16
  6613.         currentsel = 16
  6614.       elsif currentsel>=numsprites
  6615.         currentsel = 0
  6616.       end
  6617.     end
  6618. elsif $Trainer.partyplus==15
  6619.     numsprites = (@multiselect) ? 17 : 16
  6620.     case key
  6621.     when Input::LEFT
  6622.       begin
  6623.         currentsel -= 1
  6624.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6625.       if currentsel>=@party.length && currentsel<15
  6626.         currentsel = @party.length-1
  6627.       end
  6628.       currentsel = numsprites-1 if currentsel<0
  6629.     when Input::RIGHT
  6630.       begin
  6631.         currentsel += 1
  6632.       end while currentsel<@party.length && !@party[currentsel]
  6633.       if currentsel==@party.length
  6634.         currentsel = 15
  6635.       elsif currentsel==numsprites
  6636.         currentsel = 0
  6637.       end
  6638.     when Input::UP
  6639.       if currentsel>=15
  6640.         begin
  6641.           currentsel -= 1
  6642.         end while currentsel>0 && !@party[currentsel]
  6643.       else
  6644.         begin
  6645.           currentsel -= 5
  6646.         end while currentsel>0 && !@party[currentsel]
  6647.       end
  6648.       if currentsel>=@party.length && currentsel<15
  6649.         currentsel = @party.length-1
  6650.       end
  6651.       currentsel = numsprites-1 if currentsel<0
  6652.     when Input::DOWN
  6653.       if currentsel>=11
  6654.         currentsel = 15
  6655.       else
  6656.         currentsel += 5
  6657.         currentsel = 15 if currentsel<15 && !@party[currentsel]
  6658.       end
  6659.       if currentsel>=@party.length && currentsel<15
  6660.         currentsel = 15
  6661.       elsif currentsel>=numsprites
  6662.         currentsel = 0
  6663.       end
  6664.     end
  6665. elsif $Trainer.partyplus==14
  6666.     numsprites = (@multiselect) ? 16 : 15
  6667.     case key
  6668.     when Input::LEFT
  6669.       begin
  6670.         currentsel -= 1
  6671.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6672.       if currentsel>=@party.length && currentsel<14
  6673.         currentsel = @party.length-1
  6674.       end
  6675.       currentsel = numsprites-1 if currentsel<0
  6676.     when Input::RIGHT
  6677.       begin
  6678.         currentsel += 1
  6679.       end while currentsel<@party.length && !@party[currentsel]
  6680.       if currentsel==@party.length
  6681.         currentsel = 14
  6682.       elsif currentsel==numsprites
  6683.         currentsel = 0
  6684.       end
  6685.     when Input::UP
  6686.       if currentsel>=14
  6687.         begin
  6688.           currentsel -= 1
  6689.         end while currentsel>0 && !@party[currentsel]
  6690.       else
  6691.         begin
  6692.           currentsel -= 7
  6693.         end while currentsel>0 && !@party[currentsel]
  6694.       end
  6695.       if currentsel>=@party.length && currentsel<14
  6696.         currentsel = @party.length-1
  6697.       end
  6698.       currentsel = numsprites-1 if currentsel<0
  6699.     when Input::DOWN
  6700.       if currentsel>=8
  6701.         currentsel += 1
  6702.       else
  6703.         currentsel += 7
  6704.         currentsel = 14 if currentsel<14 && !@party[currentsel]
  6705.       end
  6706.       if currentsel>=@party.length && currentsel<14
  6707.         currentsel = 14
  6708.       elsif currentsel>=numsprites
  6709.         currentsel = 0
  6710.       end
  6711.     end
  6712. elsif $Trainer.partyplus==12
  6713.     numsprites = (@multiselect) ? 14 : 13
  6714.     case key
  6715.     when Input::LEFT
  6716.       begin
  6717.         currentsel -= 1
  6718.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6719.       if currentsel>=@party.length && currentsel<12
  6720.         currentsel = @party.length-1
  6721.       end
  6722.       currentsel = numsprites-1 if currentsel<0
  6723.     when Input::RIGHT
  6724.       begin
  6725.         currentsel += 1
  6726.       end while currentsel<@party.length && !@party[currentsel]
  6727.       if currentsel==@party.length
  6728.         currentsel = 12
  6729.       elsif currentsel==numsprites
  6730.         currentsel = 0
  6731.       end
  6732.     when Input::UP
  6733.       if currentsel>=12
  6734.         begin
  6735.           currentsel -= 1
  6736.         end while currentsel>0 && !@party[currentsel]
  6737.       else
  6738.         begin
  6739.           currentsel -= 3
  6740.         end while currentsel>0 && !@party[currentsel]
  6741.       end
  6742.       if currentsel>=@party.length && currentsel<12
  6743.         currentsel = @party.length-1
  6744.       end
  6745.       currentsel = numsprites-1 if currentsel<0
  6746.     when Input::DOWN
  6747.       if currentsel>=10
  6748.         currentsel += 1
  6749.       else
  6750.         currentsel += 3
  6751.         currentsel = 12 if currentsel<12 && !@party[currentsel]
  6752.       end
  6753.       if currentsel>=@party.length && currentsel<12
  6754.         currentsel = 12
  6755.       elsif currentsel>=numsprites
  6756.         currentsel = 0
  6757.       end
  6758.     end
  6759. elsif $Trainer.partyplus==10
  6760.     numsprites = (@multiselect) ? 12 : 11
  6761.     case key
  6762.     when Input::LEFT
  6763.       begin
  6764.         currentsel -= 1
  6765.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6766.       if currentsel>=@party.length && currentsel<10
  6767.         currentsel = @party.length-1
  6768.       end
  6769.       currentsel = numsprites-1 if currentsel<0
  6770.     when Input::RIGHT
  6771.       begin
  6772.         currentsel += 1
  6773.       end while currentsel<@party.length && !@party[currentsel]
  6774.       if currentsel==@party.length
  6775.         currentsel = 10
  6776.       elsif currentsel==numsprites
  6777.         currentsel = 0
  6778.       end
  6779.     when Input::UP
  6780.       if currentsel>=10
  6781.         begin
  6782.           currentsel -= 1
  6783.         end while currentsel>0 && !@party[currentsel]
  6784.       else
  6785.         begin
  6786.           currentsel -= 2
  6787.         end while currentsel>0 && !@party[currentsel]
  6788.       end
  6789.       if currentsel>=@party.length && currentsel<10
  6790.         currentsel = @party.length-1
  6791.       end
  6792.       currentsel = numsprites-1 if currentsel<0
  6793.     when Input::DOWN
  6794.       if currentsel>=9
  6795.         currentsel += 1
  6796.       else
  6797.         currentsel += 2
  6798.         currentsel = 10 if currentsel<10 && !@party[currentsel]
  6799.       end
  6800.       if currentsel>=@party.length && currentsel<10
  6801.         currentsel = 10
  6802.       elsif currentsel>=numsprites
  6803.         currentsel = 0
  6804.       end
  6805.     end
  6806. elsif $Trainer.partyplus==9
  6807.     numsprites = (@multiselect) ? 11 : 10
  6808.     case key
  6809.     when Input::LEFT
  6810.       begin
  6811.         currentsel -= 1
  6812.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6813.       if currentsel>=@party.length && currentsel<9
  6814.         currentsel = @party.length-1
  6815.       end
  6816.       currentsel = numsprites-1 if currentsel<0
  6817.     when Input::RIGHT
  6818.       begin
  6819.         currentsel += 1
  6820.       end while currentsel<@party.length && !@party[currentsel]
  6821.       if currentsel==@party.length
  6822.         currentsel = 9
  6823.       elsif currentsel==numsprites
  6824.         currentsel = 0
  6825.       end
  6826.     when Input::UP
  6827.       if currentsel>=9
  6828.         begin
  6829.           currentsel -= 1
  6830.         end while currentsel>0 && !@party[currentsel]
  6831.       else
  6832.         begin
  6833.           currentsel -= 3
  6834.         end while currentsel>0 && !@party[currentsel]
  6835.       end
  6836.       if currentsel>=@party.length && currentsel<9
  6837.         currentsel = @party.length-1
  6838.       end
  6839.       currentsel = numsprites-1 if currentsel<0
  6840.     when Input::DOWN
  6841.       if currentsel>=7
  6842.         currentsel += 1
  6843.       else
  6844.         currentsel += 3
  6845.         currentsel = 9 if currentsel<9 && !@party[currentsel]
  6846.       end
  6847.       if currentsel>=@party.length && currentsel<9
  6848.         currentsel = 9
  6849.       elsif currentsel>=numsprites
  6850.         currentsel = 0
  6851.       end
  6852.     end
  6853. elsif $Trainer.partyplus==8
  6854.     numsprites = (@multiselect) ? 10 : 9
  6855.     case key
  6856.     when Input::LEFT
  6857.       begin
  6858.         currentsel -= 1
  6859.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6860.       if currentsel>=@party.length && currentsel<8
  6861.         currentsel = @party.length-1
  6862.       end
  6863.       currentsel = numsprites-1 if currentsel<0
  6864.     when Input::RIGHT
  6865.       begin
  6866.         currentsel += 1
  6867.       end while currentsel<@party.length && !@party[currentsel]
  6868.       if currentsel==@party.length
  6869.         currentsel = 8
  6870.       elsif currentsel==numsprites
  6871.         currentsel = 0
  6872.       end
  6873.     when Input::UP
  6874.       if currentsel>=8
  6875.         begin
  6876.           currentsel -= 1
  6877.         end while currentsel>0 && !@party[currentsel]
  6878.       else
  6879.         begin
  6880.           currentsel -= 2
  6881.         end while currentsel>0 && !@party[currentsel]
  6882.       end
  6883.       if currentsel>=@party.length && currentsel<8
  6884.         currentsel = @party.length-1
  6885.       end
  6886.       currentsel = numsprites-1 if currentsel<0
  6887.     when Input::DOWN
  6888.       if currentsel>=7
  6889.         currentsel += 1
  6890.       else
  6891.         currentsel += 2
  6892.         currentsel = 8 if currentsel<8 && !@party[currentsel]
  6893.       end
  6894.       if currentsel>=@party.length && currentsel<8
  6895.         currentsel = 8
  6896.       elsif currentsel>=numsprites
  6897.         currentsel = 0
  6898.       end
  6899.     end
  6900. elsif $Trainer.partyplus==5
  6901.     numsprites = (@multiselect) ? 7 : 6
  6902.     case key
  6903.     when Input::LEFT
  6904.       begin
  6905.         currentsel -= 1
  6906.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6907.       if currentsel>=@party.length && currentsel<5
  6908.         currentsel = @party.length-1
  6909.       end
  6910.       currentsel = numsprites-1 if currentsel<0
  6911.     when Input::RIGHT
  6912.       begin
  6913.         currentsel += 1
  6914.       end while currentsel<@party.length && !@party[currentsel]
  6915.       if currentsel==@party.length
  6916.         currentsel = 5
  6917.       elsif currentsel==numsprites
  6918.         currentsel = 0
  6919.       end
  6920.     when Input::UP
  6921.       if currentsel>=5
  6922.         begin
  6923.           currentsel -= 1
  6924.         end while currentsel>0 && !@party[currentsel]
  6925.       else
  6926.         begin
  6927.           currentsel -= 1
  6928.         end while currentsel>0 && !@party[currentsel]
  6929.       end
  6930.       if currentsel>=@party.length && currentsel<5
  6931.         currentsel = @party.length-1
  6932.       end
  6933.       currentsel = numsprites-1 if currentsel<0
  6934.     when Input::DOWN
  6935.       if currentsel>=4
  6936.         currentsel += 1
  6937.       else
  6938.         currentsel += 1
  6939.         currentsel = 5 if currentsel<5 && !@party[currentsel]
  6940.       end
  6941.       if currentsel>=@party.length && currentsel<5
  6942.         currentsel = 5
  6943.       elsif currentsel>=numsprites
  6944.         currentsel = 0
  6945.       end
  6946.     end
  6947. elsif $Trainer.partyplus==4
  6948.     numsprites = (@multiselect) ? 6 : 5
  6949.     case key
  6950.     when Input::LEFT
  6951.       begin
  6952.         currentsel -= 1
  6953.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  6954.       if currentsel>=@party.length && currentsel<4
  6955.         currentsel = @party.length-1
  6956.       end
  6957.       currentsel = numsprites-1 if currentsel<0
  6958.     when Input::RIGHT
  6959.       begin
  6960.         currentsel += 1
  6961.       end while currentsel<@party.length && !@party[currentsel]
  6962.       if currentsel==@party.length
  6963.         currentsel = 4
  6964.       elsif currentsel==numsprites
  6965.         currentsel = 0
  6966.       end
  6967.     when Input::UP
  6968.       if currentsel>=4
  6969.         begin
  6970.           currentsel -= 1
  6971.         end while currentsel>0 && !@party[currentsel]
  6972.       else
  6973.         begin
  6974.           currentsel -= 2
  6975.         end while currentsel>0 && !@party[currentsel]
  6976.       end
  6977.       if currentsel>=@party.length && currentsel<4
  6978.         currentsel = @party.length-1
  6979.       end
  6980.       currentsel = numsprites-1 if currentsel<0
  6981.     when Input::DOWN
  6982.       if currentsel>=3
  6983.         currentsel += 1
  6984.       else
  6985.         currentsel += 2
  6986.         currentsel = 4 if currentsel<4 && !@party[currentsel]
  6987.       end
  6988.       if currentsel>=@party.length && currentsel<4
  6989.         currentsel = 4
  6990.       elsif currentsel>=numsprites
  6991.         currentsel = 0
  6992.       end
  6993.     end
  6994. elsif $Trainer.partyplus==3
  6995.     numsprites = (@multiselect) ? 5 : 4
  6996.     case key
  6997.     when Input::LEFT
  6998.       begin
  6999.         currentsel -= 1
  7000.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  7001.       if currentsel>=@party.length && currentsel<3
  7002.         currentsel = @party.length-1
  7003.       end
  7004.       currentsel = numsprites-1 if currentsel<0
  7005.     when Input::RIGHT
  7006.       begin
  7007.         currentsel += 1
  7008.       end while currentsel<@party.length && !@party[currentsel]
  7009.       if currentsel==@party.length
  7010.         currentsel = 3
  7011.       elsif currentsel==numsprites
  7012.         currentsel = 0
  7013.       end
  7014.     when Input::UP
  7015.       if currentsel>=3
  7016.         begin
  7017.           currentsel -= 1
  7018.         end while currentsel>0 && !@party[currentsel]
  7019.       else
  7020.         begin
  7021.           currentsel -= 1
  7022.         end while currentsel>0 && !@party[currentsel]
  7023.       end
  7024.       if currentsel>=@party.length && currentsel<3
  7025.         currentsel = @party.length-1
  7026.       end
  7027.       currentsel = numsprites-1 if currentsel<0
  7028.     when Input::DOWN
  7029.       if currentsel>=3
  7030.         currentsel += 1
  7031.       else
  7032.         currentsel += 1
  7033.         currentsel = 3 if currentsel<3 && !@party[currentsel]
  7034.       end
  7035.       if currentsel>=@party.length && currentsel<3
  7036.         currentsel = 3
  7037.       elsif currentsel>=numsprites
  7038.         currentsel = 0
  7039.       end
  7040.     end
  7041. elsif $Trainer.partyplus==2
  7042.     numsprites = (@multiselect) ? 4 : 3
  7043.     case key
  7044.     when Input::LEFT
  7045.       begin
  7046.         currentsel -= 1
  7047.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  7048.       if currentsel>=@party.length && currentsel<2
  7049.         currentsel = @party.length-1
  7050.       end
  7051.       currentsel = numsprites-1 if currentsel<0
  7052.     when Input::RIGHT
  7053.       begin
  7054.         currentsel += 1
  7055.       end while currentsel<@party.length && !@party[currentsel]
  7056.       if currentsel==@party.length
  7057.         currentsel = 2
  7058.       elsif currentsel==numsprites
  7059.         currentsel = 0
  7060.       end
  7061.     when Input::UP
  7062.       if currentsel>=2
  7063.         begin
  7064.           currentsel -= 1
  7065.         end while currentsel>0 && !@party[currentsel]
  7066.       else
  7067.         begin
  7068.           currentsel -= 2
  7069.         end while currentsel>0 && !@party[currentsel]
  7070.       end
  7071.       if currentsel>=@party.length && currentsel<2
  7072.         currentsel = @party.length-1
  7073.       end
  7074.       currentsel = numsprites-1 if currentsel<0
  7075.     when Input::DOWN
  7076.       if currentsel>=2
  7077.         currentsel += 1
  7078.       else
  7079.         currentsel += 2
  7080.         currentsel = 2 if currentsel<2 && !@party[currentsel]
  7081.       end
  7082.       if currentsel>=@party.length && currentsel<2
  7083.         currentsel = 2
  7084.       elsif currentsel>=numsprites
  7085.         currentsel = 0
  7086.       end
  7087.     end
  7088. elsif $Trainer.partyplus==1
  7089.     numsprites = (@multiselect) ? 3 : 2
  7090.     case key
  7091.     when Input::LEFT
  7092.       begin
  7093.         currentsel -= 1
  7094.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  7095.       if currentsel>=@party.length && currentsel<1
  7096.         currentsel = @party.length-1
  7097.       end
  7098.       currentsel = numsprites-1 if currentsel<0
  7099.     when Input::RIGHT
  7100.       begin
  7101.         currentsel += 1
  7102.       end while currentsel<@party.length && !@party[currentsel]
  7103.       if currentsel==@party.length
  7104.         currentsel = 1
  7105.       elsif currentsel==numsprites
  7106.         currentsel = 0
  7107.       end
  7108.     when Input::UP
  7109.       if currentsel>=1
  7110.         begin
  7111.           currentsel -= 1
  7112.         end while currentsel>0 && !@party[currentsel]
  7113.       else
  7114.         begin
  7115.           currentsel -= 1
  7116.         end while currentsel>0 && !@party[currentsel]
  7117.       end
  7118.       if currentsel>=@party.length && currentsel<1
  7119.         currentsel = @party.length-1
  7120.       end
  7121.       currentsel = numsprites-1 if currentsel<0
  7122.     when Input::DOWN
  7123.       if currentsel>=1
  7124.         currentsel += 1
  7125.       else
  7126.         currentsel += 1
  7127.         currentsel = 1 if currentsel<1 && !@party[currentsel]
  7128.       end
  7129.       if currentsel>=@party.length && currentsel<1
  7130.         currentsel = 1
  7131.       elsif currentsel>=numsprites
  7132.         currentsel = 0
  7133.       end
  7134.     end
  7135. else
  7136.     numsprites = (@multiselect) ? 8 : 7
  7137.     case key
  7138.     when Input::LEFT
  7139.       begin
  7140.         currentsel -= 1
  7141.       end while currentsel>0 && currentsel<@party.length && !@party[currentsel]
  7142.       if currentsel>=@party.length && currentsel<6
  7143.         currentsel = @party.length-1
  7144.       end
  7145.       currentsel = numsprites-1 if currentsel<0
  7146.     when Input::RIGHT
  7147.       begin
  7148.         currentsel += 1
  7149.       end while currentsel<@party.length && !@party[currentsel]
  7150.       if currentsel==@party.length
  7151.         currentsel = 6
  7152.       elsif currentsel==numsprites
  7153.         currentsel = 0
  7154.       end
  7155.     when Input::UP
  7156.       if currentsel>=6
  7157.         begin
  7158.           currentsel -= 1
  7159.         end while currentsel>0 && !@party[currentsel]
  7160.       else
  7161.         begin
  7162.           currentsel -= 2
  7163.         end while currentsel>0 && !@party[currentsel]
  7164.       end
  7165.       if currentsel>=@party.length && currentsel<6
  7166.         currentsel = @party.length-1
  7167.       end
  7168.       currentsel = numsprites-1 if currentsel<0
  7169.     when Input::DOWN
  7170.       if currentsel>=5
  7171.         currentsel += 1
  7172.       else
  7173.         currentsel += 2
  7174.         currentsel = 6 if currentsel<6 && !@party[currentsel]
  7175.       end
  7176.       if currentsel>=@party.length && currentsel<6
  7177.         currentsel = 6
  7178.       elsif currentsel>=numsprites
  7179.         currentsel = 0
  7180.       end
  7181.     end
  7182. end
  7183.     return currentsel
  7184.   end
  7185.   def pbHardRefresh
  7186.   if $Trainer.partyplus>=1
  7187.     oldtext = []
  7188.     lastselected = -1
  7189.     for i in 0...$Trainer.partyplus
  7190.       oldtext.push(@sprites["pokemon#{i}"].text)
  7191.       lastselected = i if @sprites["pokemon#{i}"].selected
  7192.       @sprites["pokemon#{i}"].dispose
  7193.     end
  7194.     lastselected = @party.length-1 if lastselected>=@party.length
  7195.     lastselected = 0 if lastselected<0
  7196.     for i in 0...$Trainer.partyplus
  7197.       if @party[i]
  7198.         @sprites["pokemon#{i}"] = PokemonPartyPanel.new(@party[i],i,@viewport)
  7199.       else
  7200.         @sprites["pokemon#{i}"] = PokemonPartyBlankPanel.new(@party[i],i,@viewport)
  7201.       end
  7202.       @sprites["pokemon#{i}"].text = oldtext[i]
  7203.     end
  7204.     pbSelect(lastselected)
  7205.   else
  7206.     oldtext = []
  7207.     lastselected = -1
  7208.     for i in 0...6
  7209.       oldtext.push(@sprites["pokemon#{i}"].text)
  7210.       lastselected = i if @sprites["pokemon#{i}"].selected
  7211.       @sprites["pokemon#{i}"].dispose
  7212.     end
  7213.     lastselected = @party.length-1 if lastselected>=@party.length
  7214.     lastselected = 0 if lastselected<0
  7215.     for i in 0...6
  7216.       if @party[i]
  7217.         @sprites["pokemon#{i}"] = PokemonPartyPanel.new(@party[i],i,@viewport)
  7218.       else
  7219.         @sprites["pokemon#{i}"] = PokemonPartyBlankPanel.new(@party[i],i,@viewport)
  7220.       end
  7221.       @sprites["pokemon#{i}"].text = oldtext[i]
  7222.     end
  7223.     pbSelect(lastselected)
  7224.   end
  7225.   end
  7226.   def pbRefresh
  7227.   if $Trainer.partyplus>=1
  7228.     for i in 0...$Trainer.partyplus
  7229.       sprite = @sprites["pokemon#{i}"]
  7230.       if sprite
  7231.         if sprite.is_a?(PokemonPartyPanel)
  7232.           sprite.pokemon = sprite.pokemon
  7233.         else
  7234.           sprite.refresh
  7235.         end
  7236.       end
  7237.     end
  7238.   else
  7239.     for i in 0...6
  7240.       sprite = @sprites["pokemon#{i}"]
  7241.       if sprite
  7242.         if sprite.is_a?(PokemonPartyPanel)
  7243.           sprite.pokemon = sprite.pokemon
  7244.         else
  7245.           sprite.refresh
  7246.         end
  7247.       end
  7248.     end
  7249.   end
  7250.   end
  7251.   def pbRefreshSingle(i)
  7252.     sprite = @sprites["pokemon#{i}"]
  7253.     if sprite
  7254.       if sprite.is_a?(PokemonPartyPanel)
  7255.         sprite.pokemon = sprite.pokemon
  7256.       else
  7257.         sprite.refresh
  7258.       end
  7259.     end
  7260.   end
  7261.   def update
  7262.     pbUpdateSpriteHash(@sprites)
  7263.   end
  7264. end
  7265. ################################################################################
  7266. # Storage Scene                                                                #
  7267. ################################################################################
  7268. #===============================================================================
  7269. # Party pop-up panel
  7270. #===============================================================================
  7271. class PokemonBoxPartySprite < SpriteWrapper
  7272.   def initialize(party,viewport=nil)
  7273.     super(viewport)
  7274.     @party = party
  7275. if $Trainer.partyplus==40
  7276.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_40_party")
  7277. elsif $Trainer.partyplus==35
  7278.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_35_party")
  7279. elsif $Trainer.partyplus==32
  7280.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_32_party")
  7281. elsif $Trainer.partyplus==30
  7282.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_30_party")
  7283. elsif $Trainer.partyplus==28
  7284.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_28_party")
  7285. elsif $Trainer.partyplus==25
  7286.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_25_party")
  7287. elsif $Trainer.partyplus==24
  7288.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_24_party")
  7289. elsif $Trainer.partyplus==21
  7290.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_21_party")
  7291. elsif $Trainer.partyplus==20
  7292.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_20_party")
  7293. elsif $Trainer.partyplus==18
  7294.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_18_party")
  7295. elsif $Trainer.partyplus==16
  7296.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_16_party")
  7297. elsif $Trainer.partyplus==15
  7298.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_15_party")
  7299. elsif $Trainer.partyplus==14
  7300.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_14_party")
  7301. elsif $Trainer.partyplus==12
  7302.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_12_party")
  7303. elsif $Trainer.partyplus==10
  7304.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_10_party")
  7305. elsif $Trainer.partyplus==9
  7306.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_9_party")
  7307. elsif $Trainer.partyplus==8
  7308.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_8_party")
  7309. elsif $Trainer.partyplus==5
  7310.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_5_party")
  7311. elsif $Trainer.partyplus==4
  7312.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_4_party")
  7313. elsif $Trainer.partyplus==3
  7314.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_3_party")
  7315. elsif $Trainer.partyplus==2
  7316.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_2_party")
  7317. elsif $Trainer.partyplus==1
  7318.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_1_party")
  7319. else
  7320.     @boxbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/overlay_party")
  7321. end
  7322.     @pokemonsprites = []
  7323.     if $Trainer.partyplus>=1
  7324.     for i in 0...$Trainer.partyplus
  7325.       @pokemonsprites[i] = nil
  7326.       pokemon = @party[i]
  7327.       if pokemon
  7328.         @pokemonsprites[i] = PokemonBoxIcon.new(pokemon,viewport)
  7329.       end
  7330.     end
  7331.     else
  7332.     for i in 0...6
  7333.       @pokemonsprites[i] = nil
  7334.       pokemon = @party[i]
  7335.       if pokemon
  7336.         @pokemonsprites[i] = PokemonBoxIcon.new(pokemon,viewport)
  7337.       end
  7338.     end
  7339.     end
  7340. if $Trainer.partyplus==40
  7341.     @contents = BitmapWrapper.new(508,356)
  7342.     self.bitmap = @contents
  7343.     self.x = 512-508
  7344.     self.y = Graphics.height-356
  7345. elsif $Trainer.partyplus==35
  7346.     @contents = BitmapWrapper.new(476,350)
  7347.     self.bitmap = @contents
  7348.     self.x = 512-476
  7349.     self.y = Graphics.height-350
  7350. elsif $Trainer.partyplus==32
  7351.     @contents = BitmapWrapper.new(508,306)
  7352.     self.bitmap = @contents
  7353.     self.x = 512-508
  7354.     self.y = Graphics.height-306
  7355. elsif $Trainer.partyplus==30
  7356.     @contents = BitmapWrapper.new(412,350)
  7357.     self.bitmap = @contents
  7358.     self.x = 512-412
  7359.     self.y = Graphics.height-350
  7360. elsif $Trainer.partyplus==28
  7361.     @contents = BitmapWrapper.new(476,302)
  7362.     self.bitmap = @contents
  7363.     self.x = 512-476
  7364.     self.y = Graphics.height-302
  7365. elsif $Trainer.partyplus==25
  7366.     @contents = BitmapWrapper.new(348,350)
  7367.     self.bitmap = @contents
  7368.     self.x = 512-348
  7369.     self.y = Graphics.height-350
  7370. elsif $Trainer.partyplus==24
  7371.     @contents = BitmapWrapper.new(412,302)
  7372.     self.bitmap = @contents
  7373.     self.x = 512-412
  7374.     self.y = Graphics.height-302
  7375. elsif $Trainer.partyplus==21
  7376.     @contents = BitmapWrapper.new(476,254)
  7377.     self.bitmap = @contents
  7378.     self.x = 512-476
  7379.     self.y = Graphics.height-254
  7380. elsif $Trainer.partyplus==20
  7381.     @contents = BitmapWrapper.new(348,302)
  7382.     self.bitmap = @contents
  7383.     self.x = 512-348
  7384.     self.y = Graphics.height-302
  7385. elsif $Trainer.partyplus==18
  7386.     @contents = BitmapWrapper.new(412,254)
  7387.     self.bitmap = @contents
  7388.     self.x = 512-412
  7389.     self.y = Graphics.height-254
  7390. elsif $Trainer.partyplus==16
  7391.     @contents = BitmapWrapper.new(284,302)
  7392.     self.bitmap = @contents
  7393.     self.x = 512-284
  7394.     self.y = Graphics.height-302
  7395. elsif $Trainer.partyplus==15
  7396.     @contents = BitmapWrapper.new(348,254)
  7397.     self.bitmap = @contents
  7398.     self.x = 512-348
  7399.     self.y = Graphics.height-254
  7400. elsif $Trainer.partyplus==14
  7401.     @contents = BitmapWrapper.new(476,206)
  7402.     self.bitmap = @contents
  7403.     self.x = 512-476
  7404.     self.y = Graphics.height-206
  7405. elsif $Trainer.partyplus==12
  7406.     @contents = BitmapWrapper.new(220,302)
  7407.     self.bitmap = @contents
  7408.     self.x = 512-220
  7409.     self.y = Graphics.height-302
  7410. elsif $Trainer.partyplus==10
  7411.     @contents = BitmapWrapper.new(172,350)
  7412.     self.bitmap = @contents
  7413.     self.x = 512-172
  7414.     self.y = Graphics.height-350
  7415. elsif $Trainer.partyplus==9
  7416.     @contents = BitmapWrapper.new(222,254)
  7417.     self.bitmap = @contents
  7418.     self.x = 512-222
  7419.     self.y = Graphics.height-254
  7420. elsif $Trainer.partyplus==8
  7421.     @contents = BitmapWrapper.new(172,302)
  7422.     self.bitmap = @contents
  7423.     self.x = 512-172
  7424.     self.y = Graphics.height-302
  7425. elsif $Trainer.partyplus==5
  7426.     @contents = BitmapWrapper.new(172,334)
  7427.     self.bitmap = @contents
  7428.     self.x = 512-172
  7429.     self.y = Graphics.height-334
  7430. elsif $Trainer.partyplus==4
  7431.     @contents = BitmapWrapper.new(172,206)
  7432.     self.bitmap = @contents
  7433.     self.x = 512-172
  7434.     self.y = Graphics.height-206
  7435. elsif $Trainer.partyplus==3
  7436.     @contents = BitmapWrapper.new(172,238)
  7437.     self.bitmap = @contents
  7438.     self.x = 512-172
  7439.     self.y = Graphics.height-238
  7440. elsif $Trainer.partyplus==2
  7441.     @contents = BitmapWrapper.new(172,158)
  7442.     self.bitmap = @contents
  7443.     self.x = 512-172
  7444.     self.y = Graphics.height-158
  7445. elsif $Trainer.partyplus==1
  7446.     @contents = BitmapWrapper.new(172,141)
  7447.     self.bitmap = @contents
  7448.     self.x = 512-172
  7449.     self.y = Graphics.height-141
  7450. else
  7451.     @contents = BitmapWrapper.new(172,352)
  7452.     self.bitmap = @contents
  7453.     self.x = 182
  7454.     self.y = Graphics.height-352
  7455. end
  7456.     pbSetSystemFont(self.bitmap)
  7457.     refresh
  7458.   end
  7459.   def dispose
  7460.   if $Trainer.partyplus>=1
  7461.     for i in 0...$Trainer.partyplus
  7462.       @pokemonsprites[i].dispose if @pokemonsprites[i]
  7463.     end
  7464.   else
  7465.     for i in 0...6
  7466.       @pokemonsprites[i].dispose if @pokemonsprites[i]
  7467.     end
  7468.   end
  7469.     @boxbitmap.dispose
  7470.     @contents.dispose
  7471.     super
  7472.   end
  7473.   def x=(value)
  7474.     super
  7475.     refresh
  7476.   end
  7477.   def y=(value)
  7478.     super
  7479.     refresh
  7480.   end
  7481.   def color=(value)
  7482.     super
  7483.     if $Trainer.partyplus>=1
  7484.     for i in 0...$Trainer.partyplus
  7485.       if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
  7486.         @pokemonsprites[i].color = pbSrcOver(@pokemonsprites[i].color,value)
  7487.       end
  7488.     end
  7489.     else
  7490.     for i in 0...6
  7491.       if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
  7492.         @pokemonsprites[i].color = pbSrcOver(@pokemonsprites[i].color,value)
  7493.       end
  7494.     end
  7495.     end
  7496.   end
  7497.   def visible=(value)
  7498.     super
  7499.     if $Trainer.partyplus>=1
  7500.     for i in 0...$Trainer.partyplus
  7501.       if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
  7502.         @pokemonsprites[i].visible = value
  7503.       end
  7504.     end
  7505.     else
  7506.     for i in 0...6
  7507.       if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
  7508.         @pokemonsprites[i].visible = value
  7509.       end
  7510.     end
  7511.     end
  7512.   end
  7513.   def getPokemon(index)
  7514.     return @pokemonsprites[index]
  7515.   end
  7516.   def setPokemon(index,sprite)
  7517.     @pokemonsprites[index] = sprite
  7518.     @pokemonsprites.compact!
  7519.     refresh
  7520.   end
  7521.   def grabPokemon(index,arrow)
  7522.     sprite = @pokemonsprites[index]
  7523.     if sprite
  7524.       arrow.grab(sprite)
  7525.       @pokemonsprites[index] = nil
  7526.       @pokemonsprites.compact!
  7527.       refresh
  7528.     end
  7529.   end
  7530.   def deletePokemon(index)
  7531.     @pokemonsprites[index].dispose
  7532.     @pokemonsprites[index] = nil
  7533.     @pokemonsprites.compact!
  7534.     refresh
  7535.   end
  7536.   def refresh
  7537. if $Trainer.partyplus==40
  7538.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,508,356))
  7539.     pbDrawTextPositions(self.bitmap,[
  7540.        [_INTL("Back"),254,312,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7541.     ])
  7542.    
  7543.     xvalues = [12,72,132,192,252,312,372,432,12,72,132,192,252,312,372,432,12,72,132,192,252,312,372,432,12,72,132,192,252,312,372,432,12,72,132,192,252,312,372,432]
  7544.     yvalues = [18,32,18,32,18,32,18,32,68,82,68,82,68,82,68,82,118,132,118,132,118,132,118,132,168,182,168,182,168,182,168,182,218,232,218,232,218,232,218,232]
  7545. elsif $Trainer.partyplus==35
  7546.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,476,350))
  7547.     pbDrawTextPositions(self.bitmap,[
  7548.        [_INTL("Back"),238,306,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7549.     ])
  7550.    
  7551.     xvalues = [14,78,142,206,270,334,398,14,78,142,206,270,334,398,14,78,142,206,270,334,398,14,78,142,206,270,334,398,14,78,142,206,270,334,398,]
  7552.     yvalues = [34,18,34,18,34,18,34,82,66,82,66,82,66,82,130,114,130,114,130,114,114,162,178,162,178,162,178,162,210,226,210,226,210,226,210]
  7553. elsif $Trainer.partyplus==32
  7554.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,508,306))
  7555.     pbDrawTextPositions(self.bitmap,[
  7556.        [_INTL("Back"),254,262,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7557.     ])
  7558.    
  7559.     xvalues = [12,72,132,192,252,312,372,432,12,72,132,192,252,312,372,432,12,72,132,192,252,312,372,432,12,72,132,192,252,312,372,432]
  7560.     yvalues = [18,32,18,32,18,32,18,32,68,82,68,82,68,82,68,82,118,132,118,132,118,132,118,132,168,182,168,182,168,182,168,282]
  7561. elsif $Trainer.partyplus==30
  7562.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,412,350))
  7563.     pbDrawTextPositions(self.bitmap,[
  7564.        [_INTL("Back"),206,306,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7565.     ])
  7566.    
  7567.     xvalues = [14,78,142,206,270,334,14,78,142,206,270,334,14,78,142,206,270,334,14,78,142,206,270,334,14,78,142,206,270,334]
  7568.     yvalues = [18,34,18,34,18,34,66,82,66,82,66,82,114,130,114,130,114,130,178,162,178,162,178,162,226,210,226,210,226,210]
  7569. elsif $Trainer.partyplus==28
  7570.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,476,302))
  7571.     pbDrawTextPositions(self.bitmap,[
  7572.        [_INTL("Back"),238,258,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7573.     ])
  7574.    
  7575.     xvalues = [14,78,142,206,270,334,398,14,78,142,206,270,334,398,14,78,142,206,270,334,398,14,78,142,206,270,334,398]
  7576.     yvalues = [18,34,18,34,18,34,18,66,82,66,82,66,82,66,114,130,114,130,114,130,114,178,162,178,162,178,162,178]
  7577. elsif $Trainer.partyplus==25
  7578.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,348,350))
  7579.     pbDrawTextPositions(self.bitmap,[
  7580.        [_INTL("Back"),174,306,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7581.     ])
  7582.    
  7583.     xvalues = [14,78,142,206,270,14,78,142,206,270,14,78,142,206,270,14,78,142,206,270,14,78,142,206,270]
  7584.     yvalues = [34,18,34,18,34,82,66,82,66,82,130,114,130,114,130,162,178,162,178,162,210,226,210,226,210]
  7585. elsif $Trainer.partyplus==24
  7586.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,412,302))
  7587.     pbDrawTextPositions(self.bitmap,[
  7588.        [_INTL("Back"),206,258,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7589.     ])
  7590.    
  7591.     xvalues = [14,78,142,206,270,334,14,78,142,206,270,334,14,78,142,206,270,334,14,78,142,206,270,334]
  7592.     yvalues = [18,34,18,34,18,34,66,82,66,82,66,82,114,130,114,130,114,130,162,178,162,178,162,178]
  7593. elsif $Trainer.partyplus==21
  7594.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,476,254))
  7595.     pbDrawTextPositions(self.bitmap,[
  7596.        [_INTL("Back"),238,210,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7597.     ])
  7598.    
  7599.     xvalues = [14,78,142,206,270,334,398,14,78,142,206,270,334,398,14,78,142,206,270,334,398]
  7600.     yvalues = [34,18,34,18,34,18,34,82,66,82,66,82,66,82,130,114,130,114,130,114,130]
  7601. elsif $Trainer.partyplus==20
  7602.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,348,302))
  7603.     pbDrawTextPositions(self.bitmap,[
  7604.        [_INTL("Back"),174,258,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7605.     ])
  7606.    
  7607.     xvalues = [14,78,142,206,270,14,78,142,206,270,14,78,142,206,270,14,78,142,206,270]
  7608.     yvalues = [34,18,34,18,34,82,66,82,66,82,130,114,130,114,130,162,178,162,178,162]
  7609. elsif $Trainer.partyplus==18
  7610.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,412,254))
  7611.     pbDrawTextPositions(self.bitmap,[
  7612.        [_INTL("Back"),206,210,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7613.     ])
  7614.    
  7615.     xvalues = [14,78,142,206,270,334,14,78,142,206,270,334,14,78,142,206,270,334]
  7616.     yvalues = [18,34,18,34,18,34,66,82,66,82,66,82,114,130,114,130,114,130]
  7617. elsif $Trainer.partyplus==16
  7618.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,284,302))
  7619.     pbDrawTextPositions(self.bitmap,[
  7620.        [_INTL("Back"),142,258,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7621.     ])
  7622.    
  7623.     xvalues = [14,78,142,206,14,78,142,206,14,78,142,206,14,78,142,206,]
  7624.     yvalues = [18,34,18,34,66,82,66,82,114,130,114,130,162,178,162,178]
  7625. elsif $Trainer.partyplus==15
  7626.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,348,254))
  7627.     pbDrawTextPositions(self.bitmap,[
  7628.        [_INTL("Back"),174,210,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7629.     ])
  7630.    
  7631.     xvalues = [14,78,142,206,270,14,78,142,206,270,14,78,142,206,270]
  7632.     yvalues = [34,18,34,18,34,82,66,82,66,82,130,114,130,114,130]
  7633. elsif $Trainer.partyplus==14
  7634.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,476,206))
  7635.     pbDrawTextPositions(self.bitmap,[
  7636.        [_INTL("Back"),238,162,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7637.     ])
  7638.    
  7639.     xvalues = [14,78,142,206,270,334,398,14,78,142,206,270,334,398]
  7640.     yvalues = [34,18,34,18,34,18,34,82,66,82,66,82,66,82]
  7641. elsif $Trainer.partyplus==12
  7642.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,220,302))
  7643.     pbDrawTextPositions(self.bitmap,[
  7644.        [_INTL("Back"),110,258,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7645.     ])
  7646.    
  7647.     xvalues = [14,78,142,14,78,142,14,78,142,14,78,142]
  7648.     yvalues = [34,18,34,82,66,82,130,114,130,178,162,178]
  7649. elsif $Trainer.partyplus==10
  7650.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,350))
  7651.     pbDrawTextPositions(self.bitmap,[
  7652.        [_INTL("Back"),86,306,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7653.     ])
  7654.    
  7655.     xvalues = [22,86,22,86,22,86,22,86,22,86]
  7656.     yvalues = [18,34,66,82,114,130,162,178,210,226]
  7657. elsif $Trainer.partyplus==9
  7658.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,222,254))
  7659.     pbDrawTextPositions(self.bitmap,[
  7660.        [_INTL("Back"),110,210,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7661.     ])
  7662.    
  7663.     xvalues = [14,78,142,14,78,142,14,78,142]
  7664.     yvalues = [34,18,34,82,66,82,130,114,130]
  7665. elsif $Trainer.partyplus==8
  7666.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,302))
  7667.     pbDrawTextPositions(self.bitmap,[
  7668.        [_INTL("Back"),86,258,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7669.     ])
  7670.    
  7671.     xvalues = [22,86,22,86,22,86,22,86]
  7672.     yvalues = [18,34,66,82,114,130,162,178]
  7673. elsif $Trainer.partyplus==5
  7674.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,334))
  7675.     pbDrawTextPositions(self.bitmap,[
  7676.        [_INTL("Back"),86,290,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7677.     ])
  7678.    
  7679.     xvalues = [54,54,54,54,54]
  7680.     yvalues = [18,66,114,162,210]
  7681. elsif $Trainer.partyplus==4
  7682.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,206))
  7683.     pbDrawTextPositions(self.bitmap,[
  7684.        [_INTL("Back"),86,162,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7685.     ])
  7686.    
  7687.     xvalues = [22,86,22,86]
  7688.     yvalues = [18,34,66,82]
  7689. elsif $Trainer.partyplus==3
  7690.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,238))
  7691.     pbDrawTextPositions(self.bitmap,[
  7692.        [_INTL("Back"),86,194,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7693.     ])
  7694.    
  7695.     xvalues = [54,54,54]
  7696.     yvalues = [18,66,114]
  7697. elsif $Trainer.partyplus==2
  7698.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,158))
  7699.     pbDrawTextPositions(self.bitmap,[
  7700.        [_INTL("Back"),86,114,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7701.     ])
  7702.    
  7703.     xvalues = [22,86]
  7704.     yvalues = [18,34]
  7705. elsif $Trainer.partyplus==1
  7706.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,141))
  7707.     pbDrawTextPositions(self.bitmap,[
  7708.        [_INTL("Back"),86,97,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7709.     ])
  7710.    
  7711.     xvalues = [54]
  7712.     yvalues = [18]
  7713. else
  7714.     @contents.blt(0,0,@boxbitmap.bitmap,Rect.new(0,0,172,352))
  7715.     pbDrawTextPositions(self.bitmap,[
  7716.        [_INTL("Back"),86,242,2,Color.new(248,248,248),Color.new(80,80,80),1]
  7717.     ])
  7718.    
  7719.     xvalues = [18,90,18,90,18,90]
  7720.     yvalues = [2,18,66,82,130,146]
  7721. end
  7722.     if $Trainer.partyplus>=1
  7723.     for j in 0...$Trainer.partyplus
  7724.       @pokemonsprites[j] = nil if @pokemonsprites[j] && @pokemonsprites[j].disposed?
  7725.     end
  7726.     @pokemonsprites.compact!
  7727.     for j in 0...$Trainer.partyplus
  7728.       sprite = @pokemonsprites[j]
  7729.       if sprite && !sprite.disposed?
  7730.         sprite.viewport = self.viewport
  7731.         sprite.x = self.x+xvalues[j]
  7732.         sprite.y = self.y+yvalues[j]
  7733.         sprite.z = 0
  7734.       end
  7735.     end
  7736.     else
  7737.     for j in 0...6
  7738.       @pokemonsprites[j] = nil if @pokemonsprites[j] && @pokemonsprites[j].disposed?
  7739.     end
  7740.     @pokemonsprites.compact!
  7741.     for j in 0...6
  7742.       sprite = @pokemonsprites[j]
  7743.       if sprite && !sprite.disposed?
  7744.         sprite.viewport = self.viewport
  7745.         sprite.x = self.x+xvalues[j]
  7746.         sprite.y = self.y+yvalues[j]
  7747.         sprite.z = 0
  7748.       end
  7749.     end
  7750.     end
  7751.   end
  7752.   def update
  7753.     super
  7754.     if $Trainer.partyplus>=1
  7755.     for i in 0...$Trainer.partyplus
  7756.       @pokemonsprites[i].update if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
  7757.     end
  7758.     else
  7759.     for i in 0...6
  7760.       @pokemonsprites[i].update if @pokemonsprites[i] && !@pokemonsprites[i].disposed?
  7761.     end
  7762.     end
  7763.   end
  7764. end
  7765. #===============================================================================
  7766. # Pokémon storage visuals
  7767. #===============================================================================
  7768. class PokemonStorageScene
  7769.   attr_reader :quickswap
  7770.   def initialize
  7771.     @command = 1
  7772.   end
  7773.   def pbStartBox(screen,command)
  7774.     @screen = screen
  7775.     @storage = screen.storage
  7776.     @bgviewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  7777.     @bgviewport.z = 99999
  7778.     @boxviewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  7779.     @boxviewport.z = 99999
  7780.     @boxsidesviewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  7781.     @boxsidesviewport.z = 99999
  7782.     @arrowviewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  7783.     @arrowviewport.z = 99999
  7784.     @viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  7785.     @viewport.z = 99999
  7786.     @selection = 0
  7787.     @quickswap = false
  7788.     @sprites = {}
  7789.     @choseFromParty = false
  7790.     @command = command
  7791.     addBackgroundPlane(@sprites,"background","Storage/bg",@bgviewport)
  7792.     @sprites["box"] = PokemonBoxSprite.new(@storage,@storage.currentBox,@boxviewport)
  7793.     @sprites["boxsides"] = IconSprite.new(0,0,@boxsidesviewport)
  7794.     @sprites["boxsides"].setBitmap("Graphics/Pictures/Storage/overlay_main")
  7795.     @sprites["overlay"] = BitmapSprite.new(Graphics.width,Graphics.height,@boxsidesviewport)
  7796.     pbSetSystemFont(@sprites["overlay"].bitmap)
  7797.     @sprites["pokemon"] = AutoMosaicPokemonSprite.new(@boxsidesviewport)
  7798.     @sprites["pokemon"].setOffset(PictureOrigin::Center)
  7799.     @sprites["pokemon"].x = 90
  7800.     @sprites["pokemon"].y = 134
  7801.     @sprites["boxparty"] = PokemonBoxPartySprite.new(@storage.party,@boxsidesviewport)
  7802.     if command!=2 # Drop down tab only on Deposit
  7803. if $Trainer.partyplus==40
  7804.       @sprites["boxparty"].x = 512-508
  7805. elsif $Trainer.partyplus==35
  7806.       @sprites["boxparty"].x = 512-476
  7807. elsif $Trainer.partyplus==32
  7808.       @sprites["boxparty"].x = 512-508
  7809. elsif $Trainer.partyplus==30
  7810.       @sprites["boxparty"].x = 512-412
  7811. elsif $Trainer.partyplus==28
  7812.       @sprites["boxparty"].x = 512-476
  7813. elsif $Trainer.partyplus==25
  7814.       @sprites["boxparty"].x = 512-348
  7815. elsif $Trainer.partyplus==24
  7816.       @sprites["boxparty"].x = 512-412
  7817. elsif $Trainer.partyplus==21
  7818.       @sprites["boxparty"].x = 512-476
  7819. elsif $Trainer.partyplus==20
  7820.       @sprites["boxparty"].x = 512-348
  7821. elsif $Trainer.partyplus==18
  7822.       @sprites["boxparty"].x = 512-412
  7823. elsif $Trainer.partyplus==16
  7824.       @sprites["boxparty"].x = 512-284
  7825. elsif $Trainer.partyplus==15
  7826.       @sprites["boxparty"].x = 512-348
  7827. elsif $Trainer.partyplus==14
  7828.       @sprites["boxparty"].x = 512-476
  7829. elsif $Trainer.partyplus==12
  7830.       @sprites["boxparty"].x = 512-220
  7831. elsif $Trainer.partyplus==10
  7832.       @sprites["boxparty"].x = 512-172
  7833. elsif $Trainer.partyplus==9
  7834.       @sprites["boxparty"].x = 512-222
  7835. elsif $Trainer.partyplus==8
  7836.       @sprites["boxparty"].x = 512-172
  7837. elsif $Trainer.partyplus==5
  7838.       @sprites["boxparty"].x = 512-172
  7839. elsif $Trainer.partyplus==4
  7840.       @sprites["boxparty"].x = 512-172
  7841. elsif $Trainer.partyplus==3
  7842.       @sprites["boxparty"].x = 512-172
  7843. elsif $Trainer.partyplus==2
  7844.       @sprites["boxparty"].x = 512-172
  7845. elsif $Trainer.partyplus==1
  7846.       @sprites["boxparty"].x = 512-172
  7847. else
  7848.       @sprites["boxparty"].x = 182
  7849. end
  7850.       @sprites["boxparty"].y = Graphics.height
  7851.     end
  7852.     @markingbitmap = AnimatedBitmap.new("Graphics/Pictures/Storage/markings")
  7853.     @sprites["markingbg"] = IconSprite.new(292,68,@boxsidesviewport)
  7854.     @sprites["markingbg"].setBitmap("Graphics/Pictures/Storage/overlay_marking")
  7855.     @sprites["markingbg"].visible = false
  7856.     @sprites["markingoverlay"] = BitmapSprite.new(Graphics.width,Graphics.height,@boxsidesviewport)
  7857.     @sprites["markingoverlay"].visible = false
  7858.     pbSetSystemFont(@sprites["markingoverlay"].bitmap)
  7859.     @sprites["arrow"] = PokemonBoxArrow.new(@arrowviewport)
  7860.     @sprites["arrow"].z += 1
  7861.     if command!=2
  7862.       pbSetArrow(@sprites["arrow"],@selection)
  7863.       pbUpdateOverlay(@selection)
  7864.       pbSetMosaic(@selection)
  7865.     else
  7866.       pbPartySetArrow(@sprites["arrow"],@selection)
  7867.       pbUpdateOverlay(@selection,@storage.party)
  7868.       pbSetMosaic(@selection)
  7869.     end
  7870.     pbFadeInAndShow(@sprites)
  7871.   end
  7872.   def pbCloseBox
  7873.     pbFadeOutAndHide(@sprites)  
  7874.     pbDisposeSpriteHash(@sprites)
  7875.     @markingbitmap.dispose if @markingbitmap
  7876.     @boxviewport.dispose
  7877.     @boxsidesviewport.dispose
  7878.     @arrowviewport.dispose
  7879.   end
  7880.   def pbDisplay(message)
  7881.     msgwindow = Window_UnformattedTextPokemon.newWithSize("",180,0,Graphics.width-180,32)
  7882.     msgwindow.viewport       = @viewport
  7883.     msgwindow.visible        = true
  7884.     msgwindow.letterbyletter = false
  7885.     msgwindow.resizeHeightToFit(message,Graphics.width-180)
  7886.     msgwindow.text           = message
  7887.     pbBottomRight(msgwindow)
  7888.     loop do
  7889.       Graphics.update
  7890.       Input.update
  7891.       if Input.trigger?(Input::B) || Input.trigger?(Input::C)
  7892.         break
  7893.       end
  7894.       msgwindow.update
  7895.       self.update
  7896.     end
  7897.     msgwindow.dispose
  7898.     Input.update
  7899.   end
  7900.   def pbShowCommands(message,commands,index=0)
  7901.     ret = 0
  7902.     msgwindow = Window_UnformattedTextPokemon.newWithSize("",180,0,Graphics.width-180,32)
  7903.     msgwindow.viewport       = @viewport
  7904.     msgwindow.visible        = true
  7905.     msgwindow.letterbyletter = false
  7906.     msgwindow.text           = message
  7907.     msgwindow.resizeHeightToFit(message,Graphics.width-180)
  7908.     pbBottomRight(msgwindow)
  7909.     cmdwindow = Window_CommandPokemon.new(commands)
  7910.     cmdwindow.viewport = @viewport
  7911.     cmdwindow.visible  = true
  7912.     cmdwindow.resizeToFit(cmdwindow.commands)
  7913.     cmdwindow.height   = Graphics.height-msgwindow.height if cmdwindow.height>Graphics.height-msgwindow.height
  7914.     pbBottomRight(cmdwindow)
  7915.     cmdwindow.y        -= msgwindow.height
  7916.     cmdwindow.index    = index
  7917.     loop do
  7918.       Graphics.update
  7919.       Input.update
  7920.       msgwindow.update
  7921.       cmdwindow.update
  7922.       if Input.trigger?(Input::B)
  7923.         ret = -1
  7924.         break
  7925.       elsif Input.trigger?(Input::C)
  7926.         ret = cmdwindow.index
  7927.         break
  7928.       end
  7929.       self.update
  7930.     end
  7931.     msgwindow.dispose
  7932.     cmdwindow.dispose
  7933.     Input.update
  7934.     return ret
  7935.   end
  7936.   def pbSetArrow(arrow,selection)
  7937.     case selection
  7938.     when -1, -4, -5 # Box name, move left, move right
  7939.       arrow.x = 157*2
  7940.       arrow.y = -12*2
  7941.     when -2 # Party Pokémon
  7942.       arrow.x = 119*2
  7943.       arrow.y = 139*2
  7944.     when -3 # Close Box
  7945.       arrow.x = 207*2
  7946.       arrow.y = 139*2
  7947.     else
  7948.       arrow.x = (97+24*(selection%6))*2
  7949.       arrow.y = (8+24*(selection/6))*2
  7950.     end
  7951.   end
  7952.   def pbChangeSelection(key,selection)
  7953.     case key
  7954.     when Input::UP
  7955.       if selection==-1 # Box name
  7956.         selection = -2
  7957.       elsif selection==-2 # Party
  7958.         selection = 25
  7959.       elsif selection==-3 # Close Box
  7960.         selection = 28
  7961.       else
  7962.         selection -= 6
  7963.         selection = -1 if selection<0
  7964.       end
  7965.     when Input::DOWN
  7966.       if selection==-1 # Box name
  7967.         selection = 2
  7968.       elsif selection==-2 # Party
  7969.         selection = -1
  7970.       elsif selection==-3 # Close Box
  7971.         selection = -1
  7972.       else
  7973.         selection += 6
  7974.         selection = -2 if selection==30 || selection==31 || selection==32
  7975.         selection = -3 if selection==33 || selection==34 || selection==35
  7976.       end
  7977.     when Input::LEFT
  7978.       if selection==-1 # Box name
  7979.         selection = -4 # Move to previous box
  7980.       elsif selection==-2
  7981.         selection = -3
  7982.       elsif selection==-3
  7983.         selection = -2
  7984.       else
  7985.         selection -= 1
  7986.         selection += 6 if selection==-1 || selection%6==5
  7987.       end
  7988.     when Input::RIGHT
  7989.       if selection==-1 # Box name
  7990.         selection = -5 # Move to next box
  7991.       elsif selection==-2
  7992.         selection = -3
  7993.       elsif selection==-3
  7994.         selection = -2
  7995.       else
  7996.         selection += 1
  7997.         selection -= 6 if selection%6==0
  7998.       end
  7999.     end
  8000.     return selection
  8001.   end
  8002.   def pbPartySetArrow(arrow,selection)
  8003.     if selection>=0
  8004. if $Trainer.partyplus==40
  8005.     xvalues = [16,76,136,196,256,316,376,436,16,76,136,196,256,316,376,436,16,76,136,196,256,316,376,436,16,76,136,196,256,316,376,436,16,76,136,196,256,316,376,436,226]
  8006.     yvalues = [14,28,14,28,14,28,14,28,64,78,64,78,64,78,64,78,114,128,114,128,114,128,114,128,164,178,164,178,164,178,164,178,214,228,214,228,214,228,214,228,280]
  8007. elsif $Trainer.partyplus==35
  8008.     xvalues = [50,114,178,242,306,370,434,50,114,178,242,306,370,434,50,114,178,242,306,370,434,50,114,178,242,306,370,434,50,114,178,242,306,370,434,242]
  8009.     yvalues = [36,20,36,20,36,20,36,84,68,84,68,84,68,84,132,116,132,116,132,116,132,164,180,164,180,164,180,164,228,212,228,212,228,212,228,280]
  8010. elsif $Trainer.partyplus==32
  8011.     xvalues = [16,76,136,196,256,316,376,436,16,76,136,196,256,316,376,436,16,76,136,196,256,316,376,436,16,76,136,196,256,316,376,436,226]
  8012.     yvalues = [64,78,64,78,64,78,64,78,114,128,114,128,114,128,114,128,164,178,164,178,164,178,164,178,214,228,214,228,214,228,214,228,280]
  8013. elsif $Trainer.partyplus==30
  8014.     xvalues = [114,178,242,306,370,434,114,178,242,306,370,434,114,178,242,306,370,434,114,178,242,306,370,434,114,178,242,306,370,434,279]
  8015.     yvalues = [20,36,20,36,20,36,68,84,68,84,68,84,116,132,116,132,116,132,164,180,164,180,164,180,212,228,212,228,212,228,280]
  8016. elsif $Trainer.partyplus==28
  8017.     xvalues = [50,114,178,242,306,370,434,50,114,178,242,306,370,434,50,114,178,242,306,370,434,50,114,178,242,306,370,434,242]
  8018.     yvalues = [68,84,68,84,68,84,68,116,132,116,132,116,132,116,164,180,164,180,164,180,164,212,228,212,228,212,228,212,280]
  8019. elsif $Trainer.partyplus==25
  8020.     xvalues = [178,242,306,370,434,178,242,306,370,434,178,242,306,370,434,178,242,306,370,434,178,242,306,370,434,308]
  8021.     yvalues = [36,20,36,20,36,84,68,84,68,84,132,116,132,116,132,180,164,180,164,180,228,212,228,212,228,280]
  8022. elsif $Trainer.partyplus==24
  8023.     xvalues = [114,178,242,306,370,434,114,178,242,306,370,434,114,178,242,306,370,434,114,178,242,306,370,434,274]
  8024.     yvalues = [68,84,68,84,68,84,116,132,116,132,116,132,164,180,164,180,164,180,212,228,212,228,212,228,280]
  8025. elsif $Trainer.partyplus==21
  8026.     xvalues = [50,114,178,242,306,370,434,50,114,178,242,306,370,434,50,114,178,242,306,370,434,242]
  8027.     yvalues = [132,116,132,116,132,116,132,180,164,180,164,180,164,180,228,212,228,212,228,212,228,280]
  8028. elsif $Trainer.partyplus==20
  8029.     xvalues = [178,242,306,370,434,178,242,306,370,434,178,242,306,370,434,178,242,306,370,434,308]
  8030.     yvalues = [84,68,84,68,84,132,116,132,116,132,180,164,180,164,180,228,212,228,212,228,280]
  8031. elsif $Trainer.partyplus==18
  8032.     xvalues = [114,178,242,306,370,434,114,178,242,306,370,434,114,178,242,306,370,434,274]
  8033.     yvalues = [116,132,116,132,116,132,164,180,164,180,164,180,212,228,212,228,212,228,280]
  8034. elsif $Trainer.partyplus==16
  8035.     xvalues = [242,306,370,434,242,306,370,434,242,306,370,434,242,306,370,434,338]
  8036.     yvalues = [68,84,68,84,116,132,116,132,164,180,164,180,212,228,212,228,280]
  8037. elsif $Trainer.partyplus==15
  8038.     xvalues = [178,242,306,370,434,178,242,306,370,434,178,242,306,370,434,308]
  8039.     yvalues = [132,116,132,116,132,180,164,180,164,180,228,212,228,212,228,280]
  8040. elsif $Trainer.partyplus==14
  8041.     xvalues = [50,114,178,242,306,370,434,50,114,178,242,306,370,434,242]
  8042.     yvalues = [180,164,180,164,180,164,180,228,212,228,212,228,212,228,280]
  8043. elsif $Trainer.partyplus==12
  8044.     xvalues = [306,370,434,306,370,434,306,370,434,306,370,434,370]
  8045.     yvalues = [84,68,84,132,116,132,180,164,180,228,212,228,280]
  8046. elsif $Trainer.partyplus==10
  8047.     xvalues = [362,426,362,426,362,426,362,426,362,426,394]
  8048.     yvalues = [20,36,68,84,116,132,164,180,212,228,280]
  8049. elsif $Trainer.partyplus==9
  8050.     xvalues = [304,368,432,304,368,432,304,368,432,370]
  8051.     yvalues = [132,116,132,180,164,180,228,212,228,280]
  8052. elsif $Trainer.partyplus==8
  8053.     xvalues = [362,426,362,426,362,426,362,426,394]
  8054.     yvalues = [68,84,116,132,164,180,212,228,280]
  8055. elsif $Trainer.partyplus==5
  8056.     xvalues = [394,394,394,394,394,394]
  8057.     yvalues = [36,84,132,180,228,280]
  8058. elsif $Trainer.partyplus==4
  8059.     xvalues = [362,426,362,426,394]
  8060.     yvalues = [164,180,212,228,280]
  8061. elsif $Trainer.partyplus==3
  8062.     xvalues = [394,394,394,394]
  8063.     yvalues = [132,180,228,280]
  8064. elsif $Trainer.partyplus==2
  8065.     xvalues = [362,426,394]
  8066.     yvalues = [212,228,280]
  8067. elsif $Trainer.partyplus==1
  8068.     xvalues = [394,394]
  8069.     yvalues = [228,280]
  8070. else
  8071.       xvalues = [200,272,200,272,200,272,236]
  8072.       yvalues = [2,18,66,82,130,146,220]
  8073. end
  8074.       arrow.angle = 0
  8075.       arrow.mirror = false
  8076.       arrow.ox = 0
  8077.       arrow.oy = 0
  8078.       arrow.x = xvalues[selection]
  8079.       arrow.y = yvalues[selection]
  8080.     end
  8081.   end
  8082.   def pbPartyChangeSelection(key,selection)
  8083. if $Trainer.partyplus==40
  8084.     case key
  8085.     when Input::LEFT
  8086.       selection -= 1
  8087.       selection = 40 if selection<0
  8088.     when Input::RIGHT
  8089.       selection += 1
  8090.       selection = 0 if selection>40
  8091.     when Input::UP
  8092.       if selection==40
  8093.         selection = 39
  8094.       else
  8095.         selection -= 8
  8096.         selection = 40 if selection<0
  8097.       end
  8098.     when Input::DOWN
  8099.       if selection==40
  8100.         selection = 0
  8101.       else
  8102.         selection += 8
  8103.         selection = 40 if selection>40
  8104.       end
  8105.     end
  8106. elsif $Trainer.partyplus==35
  8107.     case key
  8108.     when Input::LEFT
  8109.       selection -= 1
  8110.       selection = 35 if selection<0
  8111.     when Input::RIGHT
  8112.       selection += 1
  8113.       selection = 0 if selection>35
  8114.     when Input::UP
  8115.       if selection==35
  8116.         selection = 34
  8117.       else
  8118.         selection -= 7
  8119.         selection = 35 if selection<0
  8120.       end
  8121.     when Input::DOWN
  8122.       if selection==35
  8123.         selection = 0
  8124.       else
  8125.         selection += 7
  8126.         selection = 35 if selection>35
  8127.       end
  8128.     end
  8129. elsif $Trainer.partyplus==32
  8130.     case key
  8131.     when Input::LEFT
  8132.       selection -= 1
  8133.       selection = 32 if selection<0
  8134.     when Input::RIGHT
  8135.       selection += 1
  8136.       selection = 0 if selection>32
  8137.     when Input::UP
  8138.       if selection==32
  8139.         selection = 31
  8140.       else
  8141.         selection -= 8
  8142.         selection = 32 if selection<0
  8143.       end
  8144.     when Input::DOWN
  8145.       if selection==32
  8146.         selection = 0
  8147.       else
  8148.         selection += 8
  8149.         selection = 32 if selection>32
  8150.       end
  8151.     end
  8152. elsif $Trainer.partyplus==30
  8153.     case key
  8154.     when Input::LEFT
  8155.       selection -= 1
  8156.       selection = 30 if selection<0
  8157.     when Input::RIGHT
  8158.       selection += 1
  8159.       selection = 0 if selection>30
  8160.     when Input::UP
  8161.       if selection==30
  8162.         selection = 29
  8163.       else
  8164.         selection -= 6
  8165.         selection = 30 if selection<0
  8166.       end
  8167.     when Input::DOWN
  8168.       if selection==30
  8169.         selection = 0
  8170.       else
  8171.         selection += 6
  8172.         selection = 30 if selection>30
  8173.       end
  8174.     end
  8175. elsif $Trainer.partyplus==28
  8176.     case key
  8177.     when Input::LEFT
  8178.       selection -= 1
  8179.       selection = 28 if selection<0
  8180.     when Input::RIGHT
  8181.       selection += 1
  8182.       selection = 0 if selection>28
  8183.     when Input::UP
  8184.       if selection==28
  8185.         selection = 27
  8186.       else
  8187.         selection -= 7
  8188.         selection = 28 if selection<0
  8189.       end
  8190.     when Input::DOWN
  8191.       if selection==28
  8192.         selection = 0
  8193.       else
  8194.         selection += 7
  8195.         selection = 28 if selection>28
  8196.       end
  8197.     end
  8198. elsif $Trainer.partyplus==25
  8199.     case key
  8200.     when Input::LEFT
  8201.       selection -= 1
  8202.       selection = 25 if selection<0
  8203.     when Input::RIGHT
  8204.       selection += 1
  8205.       selection = 0 if selection>25
  8206.     when Input::UP
  8207.       if selection==25
  8208.         selection = 24
  8209.       else
  8210.         selection -= 5
  8211.         selection = 25 if selection<0
  8212.       end
  8213.     when Input::DOWN
  8214.       if selection==25
  8215.         selection = 0
  8216.       else
  8217.         selection += 5
  8218.         selection = 25 if selection>25
  8219.       end
  8220.     end
  8221. elsif $Trainer.partyplus==24
  8222.     case key
  8223.     when Input::LEFT
  8224.       selection -= 1
  8225.       selection = 24 if selection<0
  8226.     when Input::RIGHT
  8227.       selection += 1
  8228.       selection = 0 if selection>24
  8229.     when Input::UP
  8230.       if selection==24
  8231.         selection = 23
  8232.       else
  8233.         selection -= 6
  8234.         selection = 24 if selection<0
  8235.       end
  8236.     when Input::DOWN
  8237.       if selection==24
  8238.         selection = 0
  8239.       else
  8240.         selection += 6
  8241.         selection = 24 if selection>24
  8242.       end
  8243.     end
  8244. elsif $Trainer.partyplus==21
  8245.     case key
  8246.     when Input::LEFT
  8247.       selection -= 1
  8248.       selection = 21 if selection<0
  8249.     when Input::RIGHT
  8250.       selection += 1
  8251.       selection = 0 if selection>21
  8252.     when Input::UP
  8253.       if selection==21
  8254.         selection = 20
  8255.       else
  8256.         selection -= 7
  8257.         selection = 21 if selection<0
  8258.       end
  8259.     when Input::DOWN
  8260.       if selection==21
  8261.         selection = 0
  8262.       else
  8263.         selection += 7
  8264.         selection = 21 if selection>21
  8265.       end
  8266.     end
  8267. elsif $Trainer.partyplus==20
  8268.     case key
  8269.     when Input::LEFT
  8270.       selection -= 1
  8271.       selection = 20 if selection<0
  8272.     when Input::RIGHT
  8273.       selection += 1
  8274.       selection = 0 if selection>20
  8275.     when Input::UP
  8276.       if selection==20
  8277.         selection = 19
  8278.       else
  8279.         selection -= 5
  8280.         selection = 20 if selection<0
  8281.       end
  8282.     when Input::DOWN
  8283.       if selection==20
  8284.         selection = 0
  8285.       else
  8286.         selection += 5
  8287.         selection = 20 if selection>20
  8288.       end
  8289.     end
  8290. elsif $Trainer.partyplus==18
  8291.     case key
  8292.     when Input::LEFT
  8293.       selection -= 1
  8294.       selection = 18 if selection<0
  8295.     when Input::RIGHT
  8296.       selection += 1
  8297.       selection = 0 if selection>18
  8298.     when Input::UP
  8299.       if selection==18
  8300.         selection = 17
  8301.       else
  8302.         selection -= 6
  8303.         selection = 18 if selection<0
  8304.       end
  8305.     when Input::DOWN
  8306.       if selection==18
  8307.         selection = 0
  8308.       else
  8309.         selection += 6
  8310.         selection = 18 if selection>18
  8311.       end
  8312.     end
  8313. elsif $Trainer.partyplus==16
  8314.     case key
  8315.     when Input::LEFT
  8316.       selection -= 1
  8317.       selection = 16 if selection<0
  8318.     when Input::RIGHT
  8319.       selection += 1
  8320.       selection = 0 if selection>16
  8321.     when Input::UP
  8322.       if selection==16
  8323.         selection = 15
  8324.       else
  8325.         selection -= 4
  8326.         selection = 16 if selection<0
  8327.       end
  8328.     when Input::DOWN
  8329.       if selection==16
  8330.         selection = 0
  8331.       else
  8332.         selection += 4
  8333.         selection = 16 if selection>16
  8334.       end
  8335.     end
  8336. elsif $Trainer.partyplus==15
  8337.     case key
  8338.     when Input::LEFT
  8339.       selection -= 1
  8340.       selection = 15 if selection<0
  8341.     when Input::RIGHT
  8342.       selection += 1
  8343.       selection = 0 if selection>15
  8344.     when Input::UP
  8345.       if selection==15
  8346.         selection = 14
  8347.       else
  8348.         selection -= 5
  8349.         selection = 15 if selection<0
  8350.       end
  8351.     when Input::DOWN
  8352.       if selection==15
  8353.         selection = 0
  8354.       else
  8355.         selection += 5
  8356.         selection = 15 if selection>15
  8357.       end
  8358.     end
  8359. elsif $Trainer.partyplus==14
  8360.     case key
  8361.     when Input::LEFT
  8362.       selection -= 1
  8363.       selection = 14 if selection<0
  8364.     when Input::RIGHT
  8365.       selection += 1
  8366.       selection = 0 if selection>14
  8367.     when Input::UP
  8368.       if selection==14
  8369.         selection = 13
  8370.       else
  8371.         selection -= 7
  8372.         selection = 14 if selection<0
  8373.       end
  8374.     when Input::DOWN
  8375.       if selection==14
  8376.         selection = 0
  8377.       else
  8378.         selection += 7
  8379.         selection = 14 if selection>14
  8380.       end
  8381.     end
  8382. elsif $Trainer.partyplus==12
  8383.     case key
  8384.     when Input::LEFT
  8385.       selection -= 1
  8386.       selection = 12 if selection<0
  8387.     when Input::RIGHT
  8388.       selection += 1
  8389.       selection = 0 if selection>12
  8390.     when Input::UP
  8391.       if selection==12
  8392.         selection = 11
  8393.       else
  8394.         selection -= 3
  8395.         selection = 12 if selection<0
  8396.       end
  8397.     when Input::DOWN
  8398.       if selection==12
  8399.         selection = 0
  8400.       else
  8401.         selection += 3
  8402.         selection = 12 if selection>12
  8403.       end
  8404.     end
  8405. elsif $Trainer.partyplus==10
  8406.     case key
  8407.     when Input::LEFT
  8408.       selection -= 1
  8409.       selection = 10 if selection<0
  8410.     when Input::RIGHT
  8411.       selection += 1
  8412.       selection = 0 if selection>10
  8413.     when Input::UP
  8414.       if selection==10
  8415.         selection = 9
  8416.       else
  8417.         selection -= 2
  8418.         selection = 10 if selection<0
  8419.       end
  8420.     when Input::DOWN
  8421.       if selection==10
  8422.         selection = 0
  8423.       else
  8424.         selection += 2
  8425.         selection = 10 if selection>10
  8426.       end
  8427.     end
  8428. elsif $Trainer.partyplus==9
  8429.     case key
  8430.     when Input::LEFT
  8431.       selection -= 1
  8432.       selection = 19 if selection<0
  8433.     when Input::RIGHT
  8434.       selection += 1
  8435.       selection = 0 if selection>9
  8436.     when Input::UP
  8437.       if selection==9
  8438.         selection = 8
  8439.       else
  8440.         selection -= 3
  8441.         selection = 9 if selection<0
  8442.       end
  8443.     when Input::DOWN
  8444.       if selection==9
  8445.         selection = 0
  8446.       else
  8447.         selection += 3
  8448.         selection = 9 if selection>9
  8449.       end
  8450.     end
  8451. elsif $Trainer.partyplus==8
  8452.     case key
  8453.     when Input::LEFT
  8454.       selection -= 1
  8455.       selection = 8 if selection<0
  8456.     when Input::RIGHT
  8457.       selection += 1
  8458.       selection = 0 if selection>8
  8459.     when Input::UP
  8460.       if selection==8
  8461.         selection = 7
  8462.       else
  8463.         selection -= 2
  8464.         selection = 8 if selection<0
  8465.       end
  8466.     when Input::DOWN
  8467.       if selection==8
  8468.         selection = 0
  8469.       else
  8470.         selection += 2
  8471.         selection = 8 if selection>8
  8472.       end
  8473.     end
  8474. elsif $Trainer.partyplus==5
  8475.     case key
  8476.     when Input::LEFT
  8477.       selection -= 1
  8478.       selection = 5 if selection<0
  8479.     when Input::RIGHT
  8480.       selection += 1
  8481.       selection = 0 if selection>5
  8482.     when Input::UP
  8483.       if selection==5
  8484.         selection = 4
  8485.       else
  8486.         selection -= 1
  8487.         selection = 5 if selection<0
  8488.       end
  8489.     when Input::DOWN
  8490.       if selection==5
  8491.         selection = 0
  8492.       else
  8493.         selection += 1
  8494.         selection = 5 if selection>5
  8495.       end
  8496.     end
  8497. elsif $Trainer.partyplus==4
  8498.     case key
  8499.     when Input::LEFT
  8500.       selection -= 1
  8501.       selection = 4 if selection<0
  8502.     when Input::RIGHT
  8503.       selection += 1
  8504.       selection = 0 if selection>4
  8505.     when Input::UP
  8506.       if selection==4
  8507.         selection = 3
  8508.       else
  8509.         selection -= 2
  8510.         selection = 4 if selection<0
  8511.       end
  8512.     when Input::DOWN
  8513.       if selection==4
  8514.         selection = 0
  8515.       else
  8516.         selection += 2
  8517.         selection = 4 if selection>4
  8518.       end
  8519.     end
  8520. elsif $Trainer.partyplus==3
  8521.     case key
  8522.     when Input::LEFT
  8523.       selection -= 1
  8524.       selection = 3 if selection<0
  8525.     when Input::RIGHT
  8526.       selection += 1
  8527.       selection = 0 if selection>3
  8528.     when Input::UP
  8529.       if selection==3
  8530.         selection = 2
  8531.       else
  8532.         selection -= 1
  8533.         selection = 3 if selection<0
  8534.       end
  8535.     when Input::DOWN
  8536.       if selection==3
  8537.         selection = 0
  8538.       else
  8539.         selection += 1
  8540.         selection = 3 if selection>3
  8541.       end
  8542.     end
  8543. elsif $Trainer.partyplus==2
  8544.     case key
  8545.     when Input::LEFT
  8546.       selection -= 1
  8547.       selection = 2 if selection<0
  8548.     when Input::RIGHT
  8549.       selection += 1
  8550.       selection = 0 if selection>2
  8551.     when Input::UP
  8552.       if selection==2
  8553.         selection = 1
  8554.       else
  8555.         selection -= 2
  8556.         selection = 2 if selection<0
  8557.       end
  8558.     when Input::DOWN
  8559.       if selection==2
  8560.         selection = 0
  8561.       else
  8562.         selection += 2
  8563.         selection = 2 if selection>2
  8564.       end
  8565.     end
  8566. elsif $Trainer.partyplus==1
  8567.     case key
  8568.     when Input::LEFT
  8569.       selection -= 1
  8570.       selection = 1 if selection<0
  8571.     when Input::RIGHT
  8572.       selection += 1
  8573.       selection = 0 if selection>1
  8574.     when Input::UP
  8575.       if selection==1
  8576.         selection = 0
  8577.       else
  8578.         selection -= 1
  8579.         selection = 1 if selection<0
  8580.       end
  8581.     when Input::DOWN
  8582.       if selection==1
  8583.         selection = 0
  8584.       else
  8585.         selection += 1
  8586.         selection = 1 if selection>1
  8587.       end
  8588.     end
  8589. else
  8590.     case key
  8591.     when Input::LEFT
  8592.       selection -= 1
  8593.       selection = 6 if selection<0
  8594.     when Input::RIGHT
  8595.       selection += 1
  8596.       selection = 0 if selection>6
  8597.     when Input::UP
  8598.       if selection==6
  8599.         selection = 5
  8600.       else
  8601.         selection -= 2
  8602.         selection = 6 if selection<0
  8603.       end
  8604.     when Input::DOWN
  8605.       if selection==6
  8606.         selection = 0
  8607.       else
  8608.         selection += 2
  8609.         selection = 6 if selection>6
  8610.       end
  8611.     end
  8612. end
  8613.     return selection
  8614.   end
  8615.   def pbSelectBoxInternal(party)
  8616.     selection = @selection
  8617.     pbSetArrow(@sprites["arrow"],selection)
  8618.     pbUpdateOverlay(selection)
  8619.     pbSetMosaic(selection)
  8620.     loop do
  8621.       Graphics.update
  8622.       Input.update
  8623.       key = -1
  8624.       key = Input::DOWN if Input.repeat?(Input::DOWN)
  8625.       key = Input::RIGHT if Input.repeat?(Input::RIGHT)
  8626.       key = Input::LEFT if Input.repeat?(Input::LEFT)
  8627.       key = Input::UP if Input.repeat?(Input::UP)
  8628.       if key>=0
  8629.         pbPlayCursorSE
  8630.         selection = pbChangeSelection(key,selection)
  8631.         pbSetArrow(@sprites["arrow"],selection)
  8632.         if selection==-4
  8633.           nextbox = (@storage.currentBox+@storage.maxBoxes-1)%@storage.maxBoxes
  8634.           pbSwitchBoxToLeft(nextbox)
  8635.           @storage.currentBox = nextbox
  8636.         elsif selection==-5
  8637.           nextbox = (@storage.currentBox+1)%@storage.maxBoxes
  8638.           pbSwitchBoxToRight(nextbox)
  8639.           @storage.currentBox = nextbox
  8640.         end
  8641.         selection = -1 if selection==-4 || selection==-5
  8642.         pbUpdateOverlay(selection)
  8643.         pbSetMosaic(selection)
  8644.       end
  8645.       self.update
  8646.       if Input.trigger?(Input::L)
  8647.         pbPlayCursorSE
  8648.         nextbox = (@storage.currentBox+@storage.maxBoxes-1)%@storage.maxBoxes
  8649.         pbSwitchBoxToLeft(nextbox)
  8650.         @storage.currentBox = nextbox
  8651.         pbUpdateOverlay(selection)
  8652.         pbSetMosaic(selection)
  8653.       elsif Input.trigger?(Input::R)
  8654.         pbPlayCursorSE
  8655.         nextbox = (@storage.currentBox+1)%@storage.maxBoxes
  8656.         pbSwitchBoxToRight(nextbox)
  8657.         @storage.currentBox = nextbox
  8658.         pbUpdateOverlay(selection)
  8659.         pbSetMosaic(selection)
  8660.       elsif Input.trigger?(Input::F5)   # Jump to box name
  8661.         if selection!=-1
  8662.           pbPlayCursorSE
  8663.           selection = -1
  8664.           pbSetArrow(@sprites["arrow"],selection)
  8665.           pbUpdateOverlay(selection)
  8666.           pbSetMosaic(selection)
  8667.         end
  8668.       elsif Input.trigger?(Input::A) && @command==0   # Organize only
  8669.         pbPlayDecisionSE
  8670.         pbSetQuickSwap(!@quickswap)
  8671.       elsif Input.trigger?(Input::B)
  8672.         @selection = selection
  8673.         return nil
  8674.       elsif Input.trigger?(Input::C)
  8675.         @selection = selection
  8676.         if selection>=0
  8677.           return [@storage.currentBox,selection]
  8678.         elsif selection==-1 # Box name
  8679.           return [-4,-1]
  8680.         elsif selection==-2 # Party Pokémon
  8681.           return [-2,-1]
  8682.         elsif selection==-3 # Close Box
  8683.           return [-3,-1]
  8684.         end
  8685.       end
  8686.     end
  8687.   end
  8688.   def pbSelectBox(party)
  8689.     return pbSelectBoxInternal(party) if @command==1 # Withdraw
  8690.     ret = nil
  8691.     loop do
  8692.       if !@choseFromParty
  8693.         ret = pbSelectBoxInternal(party)
  8694.       end
  8695.       if @choseFromParty || (ret && ret[0]==-2) # Party Pokémon
  8696.         if !@choseFromParty
  8697.           pbShowPartyTab
  8698.           @selection = 0
  8699.         end
  8700.         ret = pbSelectPartyInternal(party,false)
  8701.         if ret<0
  8702.           pbHidePartyTab
  8703.           @selection = 0
  8704.           @choseFromParty = false
  8705.         else
  8706.           @choseFromParty = true
  8707.           return [-1,ret]
  8708.         end
  8709.       else
  8710.         @choseFromParty = false
  8711.         return ret
  8712.       end
  8713.     end
  8714.   end
  8715.   def pbSelectPartyInternal(party,depositing)
  8716.     selection = @selection
  8717.     pbPartySetArrow(@sprites["arrow"],selection)
  8718.     pbUpdateOverlay(selection,party)
  8719.     pbSetMosaic(selection)
  8720.     lastsel = 1
  8721.     loop do
  8722.       Graphics.update
  8723.       Input.update
  8724.       key = -1
  8725.       key = Input::DOWN if Input.repeat?(Input::DOWN)
  8726.       key = Input::RIGHT if Input.repeat?(Input::RIGHT)
  8727.       key = Input::LEFT if Input.repeat?(Input::LEFT)
  8728.       key = Input::UP if Input.repeat?(Input::UP)
  8729.       if key>=0
  8730.         pbPlayCursorSE
  8731.         newselection = pbPartyChangeSelection(key,selection)
  8732.         if newselection==-1
  8733.           return -1 if !depositing
  8734.         elsif newselection==-2
  8735.           selection = lastsel
  8736.         else
  8737.           selection = newselection
  8738.         end
  8739.         pbPartySetArrow(@sprites["arrow"],selection)
  8740.         lastsel = selection if selection>0
  8741.         pbUpdateOverlay(selection,party)
  8742.         pbSetMosaic(selection)
  8743.       end
  8744.       self.update
  8745.       if Input.trigger?(Input::A) && @command==0   # Organize only
  8746.         pbPlayDecisionSE
  8747.         pbSetQuickSwap(!@quickswap)
  8748.       elsif Input.trigger?(Input::B)
  8749.         @selection = selection
  8750.         return -1
  8751.       elsif Input.trigger?(Input::C)
  8752.       if $Trainer.partyplus>=1
  8753.         if selection>=0 && selection<$Trainer.partyplus
  8754.           @selection = selection
  8755.           return selection
  8756.         elsif selection==$Trainer.partyplus    # Close Box
  8757.           @selection = selection
  8758.           return (depositing) ? -3 : -1
  8759.         end
  8760.       else
  8761.         if selection>=0 && selection<6
  8762.           @selection = selection
  8763.           return selection
  8764.         elsif selection==6   # Close Box
  8765.           @selection = selection
  8766.           return (depositing) ? -3 : -1
  8767.         end
  8768.       end
  8769.       end
  8770.     end
  8771.   end
  8772.   def pbSelectParty(party)
  8773.     return pbSelectPartyInternal(party,true)
  8774.   end
  8775.   def pbChangeBackground(wp)
  8776.     @sprites["box"].refreshSprites = false
  8777.     alpha = 0
  8778.     Graphics.update
  8779.     self.update
  8780.     16.times do
  8781.       alpha += 16
  8782.       Graphics.update
  8783.       Input.update
  8784.       @sprites["box"].color = Color.new(248,248,248,alpha)
  8785.       self.update
  8786.     end
  8787.     @sprites["box"].refreshBox = true
  8788.     @storage[@storage.currentBox].background = wp
  8789.     4.times do
  8790.       Graphics.update
  8791.       Input.update
  8792.       self.update
  8793.     end
  8794.     16.times do
  8795.       alpha -= 16
  8796.       Graphics.update
  8797.       Input.update
  8798.       @sprites["box"].color = Color.new(248,248,248,alpha)
  8799.       self.update
  8800.     end
  8801.     @sprites["box"].refreshSprites = true
  8802.   end
  8803.   def pbSwitchBoxToRight(newbox)
  8804.     newbox = PokemonBoxSprite.new(@storage,newbox,@boxviewport)
  8805.     newbox.x = 520
  8806.     Graphics.frame_reset
  8807.     begin
  8808.       Graphics.update
  8809.       Input.update
  8810.       @sprites["box"].x -= 32
  8811.       newbox.x -= 32
  8812.       self.update
  8813.     end until newbox.x<=184
  8814.     diff = newbox.x-184
  8815.     newbox.x = 184; @sprites["box"].x -= diff
  8816.     @sprites["box"].dispose
  8817.     @sprites["box"] = newbox
  8818.   end
  8819.   def pbSwitchBoxToLeft(newbox)
  8820.     newbox = PokemonBoxSprite.new(@storage,newbox,@boxviewport)
  8821.     newbox.x = -152
  8822.     Graphics.frame_reset
  8823.     begin
  8824.       Graphics.update
  8825.       Input.update
  8826.       @sprites["box"].x += 32
  8827.       newbox.x += 32
  8828.       self.update
  8829.     end until newbox.x>=184
  8830.     diff = newbox.x-184
  8831.     newbox.x = 184; @sprites["box"].x -= diff
  8832.     @sprites["box"].dispose
  8833.     @sprites["box"] = newbox
  8834.   end
  8835.   def pbJumpToBox(newbox)
  8836.     if @storage.currentBox!=newbox
  8837.       if newbox>@storage.currentBox
  8838.         pbSwitchBoxToRight(newbox)
  8839.       else
  8840.         pbSwitchBoxToLeft(newbox)
  8841.       end
  8842.       @storage.currentBox = newbox
  8843.     end
  8844.   end
  8845.   def pbSetMosaic(selection)
  8846.     if !@screen.pbHeldPokemon
  8847.       if @boxForMosaic!=@storage.currentBox || @selectionForMosaic!=selection
  8848.         @sprites["pokemon"].mosaic = 10
  8849.         @boxForMosaic = @storage.currentBox
  8850.         @selectionForMosaic = selection
  8851.       end
  8852.     end
  8853.   end
  8854.   def pbSetQuickSwap(value)
  8855.     @quickswap = value
  8856.     @sprites["arrow"].quickswap = value
  8857.   end
  8858.   def pbShowPartyTab
  8859. if $Trainer.partyplus==40
  8860.     begin
  8861.       Graphics.update
  8862.       Input.update
  8863.       @sprites["boxparty"].y -= 24
  8864.       self.update
  8865.     end until @sprites["boxparty"].y<=Graphics.height-356
  8866.     @sprites["boxparty"].y = Graphics.height-356
  8867. elsif $Trainer.partyplus==35
  8868.     begin
  8869.       Graphics.update
  8870.       Input.update
  8871.       @sprites["boxparty"].y -= 24
  8872.       self.update
  8873.     end until @sprites["boxparty"].y<=Graphics.height-350
  8874.     @sprites["boxparty"].y = Graphics.height-350
  8875. elsif $Trainer.partyplus==32
  8876.     begin
  8877.       Graphics.update
  8878.       Input.update
  8879.       @sprites["boxparty"].y -= 24
  8880.       self.update
  8881.     end until @sprites["boxparty"].y<=Graphics.height-306
  8882.     @sprites["boxparty"].y = Graphics.height-306
  8883. elsif $Trainer.partyplus==30
  8884.     begin
  8885.       Graphics.update
  8886.       Input.update
  8887.       @sprites["boxparty"].y -= 24
  8888.       self.update
  8889.     end until @sprites["boxparty"].y<=Graphics.height-350
  8890.     @sprites["boxparty"].y = Graphics.height-350
  8891. elsif $Trainer.partyplus==28
  8892.     begin
  8893.       Graphics.update
  8894.       Input.update
  8895.       @sprites["boxparty"].y -= 24
  8896.       self.update
  8897.     end until @sprites["boxparty"].y<=Graphics.height-302
  8898.     @sprites["boxparty"].y = Graphics.height-302
  8899. elsif $Trainer.partyplus==25
  8900.     begin
  8901.       Graphics.update
  8902.       Input.update
  8903.       @sprites["boxparty"].y -= 24
  8904.       self.update
  8905.     end until @sprites["boxparty"].y<=Graphics.height-350
  8906.     @sprites["boxparty"].y = Graphics.height-350
  8907. elsif $Trainer.partyplus==24
  8908.     begin
  8909.       Graphics.update
  8910.       Input.update
  8911.       @sprites["boxparty"].y -= 24
  8912.       self.update
  8913.     end until @sprites["boxparty"].y<=Graphics.height-302
  8914.     @sprites["boxparty"].y = Graphics.height-302
  8915. elsif $Trainer.partyplus==21
  8916.     begin
  8917.       Graphics.update
  8918.       Input.update
  8919.       @sprites["boxparty"].y -= 24
  8920.       self.update
  8921.     end until @sprites["boxparty"].y<=Graphics.height-254
  8922.     @sprites["boxparty"].y = Graphics.height-254
  8923. elsif $Trainer.partyplus==20
  8924.     begin
  8925.       Graphics.update
  8926.       Input.update
  8927.       @sprites["boxparty"].y -= 24
  8928.       self.update
  8929.     end until @sprites["boxparty"].y<=Graphics.height-302
  8930.     @sprites["boxparty"].y = Graphics.height-302
  8931. elsif $Trainer.partyplus==18
  8932.     begin
  8933.       Graphics.update
  8934.       Input.update
  8935.       @sprites["boxparty"].y -= 24
  8936.       self.update
  8937.     end until @sprites["boxparty"].y<=Graphics.height-254
  8938.     @sprites["boxparty"].y = Graphics.height-254
  8939. elsif $Trainer.partyplus==16
  8940.     begin
  8941.       Graphics.update
  8942.       Input.update
  8943.       @sprites["boxparty"].y -= 24
  8944.       self.update
  8945.     end until @sprites["boxparty"].y<=Graphics.height-302
  8946.     @sprites["boxparty"].y = Graphics.height-302
  8947. elsif $Trainer.partyplus==15
  8948.     begin
  8949.       Graphics.update
  8950.       Input.update
  8951.       @sprites["boxparty"].y -= 24
  8952.       self.update
  8953.     end until @sprites["boxparty"].y<=Graphics.height-254
  8954.     @sprites["boxparty"].y = Graphics.height-254
  8955. elsif $Trainer.partyplus==14
  8956.     begin
  8957.       Graphics.update
  8958.       Input.update
  8959.       @sprites["boxparty"].y -= 24
  8960.       self.update
  8961.     end until @sprites["boxparty"].y<=Graphics.height-206
  8962.     @sprites["boxparty"].y = Graphics.height-206
  8963. elsif $Trainer.partyplus==12
  8964.     begin
  8965.       Graphics.update
  8966.       Input.update
  8967.       @sprites["boxparty"].y -= 24
  8968.       self.update
  8969.     end until @sprites["boxparty"].y<=Graphics.height-302
  8970.     @sprites["boxparty"].y = Graphics.height-302
  8971. elsif $Trainer.partyplus==10
  8972.     begin
  8973.       Graphics.update
  8974.       Input.update
  8975.       @sprites["boxparty"].y -= 24
  8976.       self.update
  8977.     end until @sprites["boxparty"].y<=Graphics.height-350
  8978.     @sprites["boxparty"].y = Graphics.height-350
  8979. elsif $Trainer.partyplus==9
  8980.     begin
  8981.       Graphics.update
  8982.       Input.update
  8983.       @sprites["boxparty"].y -= 24
  8984.       self.update
  8985.     end until @sprites["boxparty"].y<=Graphics.height-254
  8986.     @sprites["boxparty"].y = Graphics.height-254
  8987. elsif $Trainer.partyplus==8
  8988.     begin
  8989.       Graphics.update
  8990.       Input.update
  8991.       @sprites["boxparty"].y -= 24
  8992.       self.update
  8993.     end until @sprites["boxparty"].y<=Graphics.height-302
  8994.     @sprites["boxparty"].y = Graphics.height-302
  8995. elsif $Trainer.partyplus==5
  8996.     begin
  8997.       Graphics.update
  8998.       Input.update
  8999.       @sprites["boxparty"].y -= 24
  9000.       self.update
  9001.     end until @sprites["boxparty"].y<=Graphics.height-334
  9002.     @sprites["boxparty"].y = Graphics.height-334
  9003. elsif $Trainer.partyplus==4
  9004.     begin
  9005.       Graphics.update
  9006.       Input.update
  9007.       @sprites["boxparty"].y -= 24
  9008.       self.update
  9009.     end until @sprites["boxparty"].y<=Graphics.height-206
  9010.     @sprites["boxparty"].y = Graphics.height-206
  9011. elsif $Trainer.partyplus==3
  9012.     begin
  9013.       Graphics.update
  9014.       Input.update
  9015.       @sprites["boxparty"].y -= 24
  9016.       self.update
  9017.     end until @sprites["boxparty"].y<=Graphics.height-238
  9018.     @sprites["boxparty"].y = Graphics.height-238
  9019. elsif $Trainer.partyplus==2
  9020.     begin
  9021.       Graphics.update
  9022.       Input.update
  9023.       @sprites["boxparty"].y -= 24
  9024.       self.update
  9025.     end until @sprites["boxparty"].y<=Graphics.height-158
  9026.     @sprites["boxparty"].y = Graphics.height-158
  9027. elsif $Trainer.partyplus==1
  9028.     begin
  9029.       Graphics.update
  9030.       Input.update
  9031.       @sprites["boxparty"].y -= 24
  9032.       self.update
  9033.     end until @sprites["boxparty"].y<=Graphics.height-141
  9034.     @sprites["boxparty"].y = Graphics.height-141
  9035. else
  9036.     begin
  9037.       Graphics.update
  9038.       Input.update
  9039.       @sprites["boxparty"].y -= 24
  9040.       self.update
  9041.     end until @sprites["boxparty"].y<=Graphics.height-352
  9042.     @sprites["boxparty"].y = Graphics.height-352
  9043. end
  9044.   end
  9045.   def pbHidePartyTab
  9046.     begin
  9047.       Graphics.update
  9048.       Input.update
  9049.       @sprites["boxparty"].y += 24
  9050.       self.update
  9051.     end until @sprites["boxparty"].y>=Graphics.height
  9052.     @sprites["boxparty"].y = Graphics.height
  9053.   end
  9054.   def pbHold(selected)
  9055.     if selected[0]==-1
  9056.       @sprites["boxparty"].grabPokemon(selected[1],@sprites["arrow"])
  9057.     else
  9058.       @sprites["box"].grabPokemon(selected[1],@sprites["arrow"])
  9059.     end
  9060.     while @sprites["arrow"].grabbing?
  9061.       Graphics.update
  9062.       Input.update
  9063.       self.update
  9064.     end
  9065.   end
  9066.   def pbSwap(selected,heldpoke)
  9067.     heldpokesprite = @sprites["arrow"].heldPokemon
  9068.     boxpokesprite = nil
  9069.     if selected[0]==-1
  9070.       boxpokesprite = @sprites["boxparty"].getPokemon(selected[1])
  9071.     else
  9072.       boxpokesprite = @sprites["box"].getPokemon(selected[1])
  9073.     end
  9074.     if selected[0]==-1
  9075.       @sprites["boxparty"].setPokemon(selected[1],heldpokesprite)
  9076.     else
  9077.       @sprites["box"].setPokemon(selected[1],heldpokesprite)
  9078.     end
  9079.     @sprites["arrow"].setSprite(boxpokesprite)
  9080.     @sprites["pokemon"].mosaic = 10
  9081.     @boxForMosaic = @storage.currentBox
  9082.     @selectionForMosaic = selected[1]
  9083.   end
  9084.   def pbPlace(selected,heldpoke)
  9085.     heldpokesprite = @sprites["arrow"].heldPokemon
  9086.     @sprites["arrow"].place
  9087.     while @sprites["arrow"].placing?
  9088.       Graphics.update
  9089.       Input.update
  9090.       self.update
  9091.     end
  9092.     if selected[0]==-1
  9093.       @sprites["boxparty"].setPokemon(selected[1],heldpokesprite)
  9094.     else
  9095.       @sprites["box"].setPokemon(selected[1],heldpokesprite)
  9096.     end
  9097.     @boxForMosaic = @storage.currentBox
  9098.     @selectionForMosaic = selected[1]
  9099.   end
  9100.   def pbWithdraw(selected,heldpoke,partyindex)
  9101.     pbHold(selected) if !heldpoke
  9102.     pbShowPartyTab
  9103.     pbPartySetArrow(@sprites["arrow"],partyindex)
  9104.     pbPlace([-1,partyindex],heldpoke)
  9105.     pbHidePartyTab
  9106.   end
  9107.   def pbStore(selected,heldpoke,destbox,firstfree)
  9108.     if heldpoke
  9109.       if destbox==@storage.currentBox
  9110.         heldpokesprite = @sprites["arrow"].heldPokemon
  9111.         @sprites["box"].setPokemon(firstfree,heldpokesprite)
  9112.         @sprites["arrow"].setSprite(nil)
  9113.       else
  9114.         @sprites["arrow"].deleteSprite
  9115.       end
  9116.     else
  9117.       sprite = @sprites["boxparty"].getPokemon(selected[1])
  9118.       if destbox==@storage.currentBox
  9119.         @sprites["box"].setPokemon(firstfree,sprite)
  9120.         @sprites["boxparty"].setPokemon(selected[1],nil)
  9121.       else
  9122.         @sprites["boxparty"].deletePokemon(selected[1])
  9123.       end
  9124.     end
  9125.   end
  9126.   def pbRelease(selected,heldpoke)
  9127.     box = selected[0]
  9128.     index = selected[1]
  9129.     if heldpoke
  9130.       sprite = @sprites["arrow"].heldPokemon
  9131.     elsif box==-1
  9132.       sprite = @sprites["boxparty"].getPokemon(index)
  9133.     else
  9134.       sprite = @sprites["box"].getPokemon(index)
  9135.     end
  9136.     if sprite
  9137.       sprite.release
  9138.       while sprite.releasing?
  9139.         Graphics.update
  9140.         sprite.update
  9141.         self.update
  9142.       end
  9143.     end
  9144.   end
  9145.   def pbChooseBox(msg)
  9146.     commands = []
  9147.     for i in 0...@storage.maxBoxes
  9148.       box = @storage[i]
  9149.       if box
  9150.         commands.push(_INTL("{1} ({2}/{3})",box.name,box.nitems,box.length))
  9151.       end
  9152.     end
  9153.     return pbShowCommands(msg,commands,@storage.currentBox)
  9154.   end
  9155.   def pbBoxName(helptext,minchars,maxchars)
  9156.     oldsprites = pbFadeOutAndHide(@sprites)
  9157.     ret = pbEnterBoxName(helptext,minchars,maxchars)
  9158.     if ret.length>0
  9159.       @storage[@storage.currentBox].name = ret
  9160.     end
  9161.     @sprites["box"].refreshBox = true
  9162.     pbRefresh
  9163.     pbFadeInAndShow(@sprites,oldsprites)
  9164.   end
  9165.   def pbChooseItem(bag)
  9166.     ret = 0
  9167.     pbFadeOutIn(99999){
  9168.       scene = PokemonBag_Scene.new
  9169.       screen = PokemonBagScreen.new(scene,bag)
  9170.       ret = screen.pbChooseItemScreen(Proc.new{|item| pbCanHoldItem?(item) })
  9171.     }
  9172.     return ret
  9173.   end
  9174.   def pbSummary(selected,heldpoke)
  9175.     oldsprites = pbFadeOutAndHide(@sprites)
  9176.     scene = PokemonSummary_Scene.new
  9177.     screen = PokemonSummaryScreen.new(scene)
  9178.     if heldpoke
  9179.       screen.pbStartScreen([heldpoke],0)
  9180.     elsif selected[0]==-1
  9181.       @selection = screen.pbStartScreen(@storage.party,selected[1])
  9182.       pbPartySetArrow(@sprites["arrow"],@selection)
  9183.       pbUpdateOverlay(@selection,@storage.party)
  9184.     else
  9185.       @selection = screen.pbStartScreen(@storage.boxes[selected[0]],selected[1])
  9186.       pbSetArrow(@sprites["arrow"],@selection)
  9187.       pbUpdateOverlay(@selection)
  9188.     end
  9189.     pbFadeInAndShow(@sprites,oldsprites)
  9190.   end
  9191.   def pbMarkingSetArrow(arrow,selection)
  9192.     if selection>=0
  9193.       xvalues = [162,191,220,162,191,220,184,184]
  9194.       yvalues = [24,24,24,49,49,49,77,109]
  9195.       arrow.angle = 0
  9196.       arrow.mirror = false
  9197.       arrow.ox = 0
  9198.       arrow.oy = 0
  9199.       arrow.x = xvalues[selection]*2
  9200.       arrow.y = yvalues[selection]*2
  9201.     end
  9202.   end
  9203.   def pbMarkingChangeSelection(key,selection)
  9204.     case key
  9205.     when Input::LEFT
  9206.       if selection<6
  9207.         selection -= 1
  9208.         selection += 3 if selection%3==2
  9209.       end
  9210.     when Input::RIGHT
  9211.       if selection<6
  9212.         selection += 1
  9213.         selection -= 3 if selection%3==0
  9214.       end
  9215.     when Input::UP
  9216.       if selection==7; selection = 6
  9217.       elsif selection==6; selection = 4
  9218.       elsif selection<3; selection = 7
  9219.       else; selection -= 3
  9220.       end
  9221.     when Input::DOWN
  9222.       if selection==7; selection = 1
  9223.       elsif selection==6; selection = 7
  9224.       elsif selection>=3; selection = 6
  9225.       else; selection += 3
  9226.       end
  9227.     end
  9228.     return selection
  9229.   end
  9230.   def pbMark(selected,heldpoke)
  9231.     ret = 0
  9232.     @sprites["markingbg"].visible      = true
  9233.     @sprites["markingoverlay"].visible = true
  9234.     msg = _INTL("Mark your Pokémon.")
  9235.     msgwindow = Window_UnformattedTextPokemon.newWithSize("",180,0,Graphics.width-180,32)
  9236.     msgwindow.viewport       = @viewport
  9237.     msgwindow.visible        = true
  9238.     msgwindow.letterbyletter = false
  9239.     msgwindow.text           = msg
  9240.     msgwindow.resizeHeightToFit(msg,Graphics.width-180)
  9241.     pbBottomRight(msgwindow)
  9242.     base   = Color.new(248,248,248)
  9243.     shadow = Color.new(80,80,80)
  9244.     pokemon = heldpoke
  9245.     if heldpoke
  9246.       pokemon = heldpoke
  9247.     elsif selected[0]==-1
  9248.       pokemon = @storage.party[selected[1]]
  9249.     else
  9250.       pokemon = @storage.boxes[selected[0]][selected[1]]
  9251.     end
  9252.     markings = pokemon.markings
  9253.     index = 0
  9254.     redraw = true
  9255.     markrect = Rect.new(0,0,16,16)
  9256.     loop do
  9257.       # Redraw the markings and text
  9258.       if redraw
  9259.         @sprites["markingoverlay"].bitmap.clear
  9260.         for i in 0...6
  9261.           markrect.x = i*16
  9262.           markrect.y = (markings&(1<<i)!=0) ? 16 : 0
  9263.           @sprites["markingoverlay"].bitmap.blt(336+58*(i%3),106+50*(i/3),@markingbitmap.bitmap,markrect)
  9264.         end
  9265.         textpos = [
  9266.            [_INTL("OK"),402,210,2,base,shadow,1],
  9267.            [_INTL("Cancel"),402,274,2,base,shadow,1]
  9268.         ]
  9269.         pbDrawTextPositions(@sprites["markingoverlay"].bitmap,textpos)
  9270.         pbMarkingSetArrow(@sprites["arrow"],index)
  9271.         redraw = false
  9272.       end
  9273.       Graphics.update
  9274.       Input.update
  9275.       key = -1
  9276.       key = Input::DOWN if Input.repeat?(Input::DOWN)
  9277.       key = Input::RIGHT if Input.repeat?(Input::RIGHT)
  9278.       key = Input::LEFT if Input.repeat?(Input::LEFT)
  9279.       key = Input::UP if Input.repeat?(Input::UP)
  9280.       if key>=0
  9281.         oldindex = index
  9282.         index = pbMarkingChangeSelection(key,index)
  9283.         pbPlayCursorSE if index!=oldindex
  9284.         pbMarkingSetArrow(@sprites["arrow"],index)
  9285.       end
  9286.       self.update
  9287.       if Input.trigger?(Input::B)
  9288.         pbPlayCancelSE
  9289.         break
  9290.       elsif Input.trigger?(Input::C)
  9291.         pbPlayDecisionSE
  9292.         if index==6 # OK
  9293.           pokemon.markings = markings
  9294.           break
  9295.         elsif index==7 # Cancel
  9296.           break
  9297.         else
  9298.           mask = (1<<index)
  9299.           if (markings&mask)==0
  9300.             markings |= mask
  9301.           else
  9302.             markings &= ~mask
  9303.           end
  9304.           redraw = true
  9305.         end
  9306.       end
  9307.     end
  9308.     @sprites["markingbg"].visible      = false
  9309.     @sprites["markingoverlay"].visible = false
  9310.     msgwindow.dispose
  9311.   end
  9312.   def pbRefresh
  9313.     @sprites["box"].refresh
  9314.     @sprites["boxparty"].refresh
  9315.   end
  9316.   def pbHardRefresh
  9317.     oldPartyY = @sprites["boxparty"].y
  9318.     @sprites["box"].dispose
  9319.     @sprites["box"] = PokemonBoxSprite.new(@storage,@storage.currentBox,@boxviewport)
  9320.     @sprites["boxparty"].dispose
  9321.     @sprites["boxparty"] = PokemonBoxPartySprite.new(@storage.party,@boxsidesviewport)
  9322.     @sprites["boxparty"].y = oldPartyY
  9323.   end
  9324.   def drawMarkings(bitmap,x,y,width,height,markings)
  9325.     markrect = Rect.new(0,0,16,16)
  9326.     for i in 0...8
  9327.       markrect.x = i*16
  9328.       markrect.y = (markings&(1<<i)!=0) ? 16 : 0
  9329.       bitmap.blt(x+i*16,y,@markingbitmap.bitmap,markrect)
  9330.     end
  9331.   end
  9332.   def pbUpdateOverlay(selection,party=nil)
  9333.     overlay = @sprites["overlay"].bitmap
  9334.     overlay.clear
  9335.     buttonbase = Color.new(248,248,248)
  9336.     buttonshadow = Color.new(80,80,80)
  9337.     pbDrawTextPositions(overlay,[
  9338.        [_INTL("Party: {1}",(@storage.party.length rescue 0)),270,328,2,buttonbase,buttonshadow,1],
  9339.        [_INTL("Exit"),446,328,2,buttonbase,buttonshadow,1],
  9340.     ])
  9341.     pokemon = nil
  9342.     if @screen.pbHeldPokemon
  9343.       pokemon = @screen.pbHeldPokemon
  9344.     elsif selection>=0
  9345.       pokemon = (party) ? party[selection] : @storage[@storage.currentBox,selection]
  9346.     end
  9347.     if !pokemon
  9348.       @sprites["pokemon"].visible = false
  9349.       return
  9350.     end
  9351.     @sprites["pokemon"].visible = true
  9352.     base   = Color.new(88,88,80)
  9353.     shadow = Color.new(168,184,184)
  9354.     nonbase   = Color.new(208,208,208)
  9355.     nonshadow = Color.new(224,224,224)
  9356.     pokename = pokemon.name
  9357.     textstrings = [
  9358.        [pokename,10,8,false,base,shadow]
  9359.     ]
  9360.     if !pokemon.egg?
  9361.       imagepos = []
  9362.       if pokemon.isMale?
  9363.         textstrings.push([_INTL("♂"),148,8,false,Color.new(24,112,216),Color.new(136,168,208)])
  9364.       elsif pokemon.isFemale?
  9365.         textstrings.push([_INTL("♀"),148,8,false,Color.new(248,56,32),Color.new(224,152,144)])
  9366.       end
  9367.       imagepos.push(["Graphics/Pictures/Storage/overlay_lv",6,246,0,0,-1,-1])
  9368.       textstrings.push([pokemon.level.to_s,28,234,false,base,shadow])
  9369.       if pokemon.ability>0
  9370.         textstrings.push([PBAbilities.getName(pokemon.ability),86,306,2,base,shadow])
  9371.       else
  9372.         textstrings.push([_INTL("No ability"),86,306,2,nonbase,nonshadow])
  9373.       end
  9374.       if pokemon.item>0
  9375.         textstrings.push([PBItems.getName(pokemon.item),86,342,2,base,shadow])
  9376.       else
  9377.         textstrings.push([_INTL("No item"),86,342,2,nonbase,nonshadow])
  9378.       end
  9379.       if pokemon.isShiny?
  9380.         imagepos.push(["Graphics/Pictures/shiny",156,198,0,0,-1,-1])
  9381.       end
  9382.       typebitmap = AnimatedBitmap.new(_INTL("Graphics/Pictures/types"))
  9383.       type1rect = Rect.new(0,pokemon.type1*28,64,28)
  9384.       type2rect = Rect.new(0,pokemon.type2*28,64,28)
  9385.       if pokemon.type1==pokemon.type2
  9386.         overlay.blt(52,272,typebitmap.bitmap,type1rect)
  9387.       else
  9388.         overlay.blt(18,272,typebitmap.bitmap,type1rect)
  9389.         overlay.blt(88,272,typebitmap.bitmap,type2rect)
  9390.       end
  9391.       drawMarkings(overlay,70,240,128,20,pokemon.markings)
  9392.       pbDrawImagePositions(overlay,imagepos)
  9393.     end
  9394.     pbDrawTextPositions(overlay,textstrings)
  9395.     @sprites["pokemon"].setPokemonBitmap(pokemon)
  9396.   end
  9397.   def update
  9398.     pbUpdateSpriteHash(@sprites)
  9399.   end
  9400. end
  9401. ################################################################################
  9402. # Pc                                                                           #
  9403. ################################################################################
  9404. #===============================================================================
  9405. # PC menus
  9406. #===============================================================================
  9407. def pbPCItemStorage
  9408.   loop do
  9409.     command=Kernel.pbShowCommandsWithHelp(nil,
  9410.        [_INTL("Withdraw Item"),
  9411.        _INTL("Deposit Item"),
  9412.        _INTL("Toss Item"),
  9413.        _INTL("Exit")],
  9414.        [_INTL("Take out items from the PC."),
  9415.        _INTL("Store items in the PC."),
  9416.        _INTL("Throw away items stored in the PC."),
  9417.        _INTL("Go back to the previous menu.")],-1
  9418.     )
  9419.     if command==0 # Withdraw Item
  9420.       if !$PokemonGlobal.pcItemStorage
  9421.         $PokemonGlobal.pcItemStorage=PCItemStorage.new
  9422.       end
  9423.       if $PokemonGlobal.pcItemStorage.empty?
  9424.         Kernel.pbMessage(_INTL("There are no items."))
  9425.       else
  9426.         pbFadeOutIn(99999){
  9427.           scene = WithdrawItemScene.new
  9428.           screen = PokemonBagScreen.new(scene,$PokemonBag)
  9429.           ret = screen.pbWithdrawItemScreen
  9430.         }
  9431.       end
  9432.     elsif command==1 # Deposit Item
  9433.       pbFadeOutIn(99999){
  9434.         scene = PokemonBag_Scene.new
  9435.         screen = PokemonBagScreen.new(scene,$PokemonBag)
  9436.         ret = screen.pbDepositItemScreen
  9437.       }
  9438.     elsif command==2 # Toss Item
  9439.       if !$PokemonGlobal.pcItemStorage
  9440.         $PokemonGlobal.pcItemStorage=PCItemStorage.new
  9441.       end
  9442.       if $PokemonGlobal.pcItemStorage.empty?
  9443.         Kernel.pbMessage(_INTL("There are no items."))
  9444.       else
  9445.         pbFadeOutIn(99999){
  9446.           scene = TossItemScene.new
  9447.           screen = PokemonBagScreen.new(scene,$PokemonBag)
  9448.           ret = screen.pbTossItemScreen
  9449.         }
  9450.       end
  9451.     else
  9452.       break
  9453.     end
  9454.   end
  9455. end
  9456. def pbPCMailbox
  9457.   if !$PokemonGlobal.mailbox || $PokemonGlobal.mailbox.length==0
  9458.     Kernel.pbMessage(_INTL("There's no Mail here."))
  9459.   else
  9460.     loop do
  9461.       commands=[]
  9462.       for mail in $PokemonGlobal.mailbox
  9463.         commands.push(mail.sender)
  9464.       end
  9465.       commands.push(_INTL("Cancel"))
  9466.       command=Kernel.pbShowCommands(nil,commands,-1)
  9467.       if command>=0 && command<$PokemonGlobal.mailbox.length
  9468.         mailIndex=command
  9469.         command=Kernel.pbMessage(_INTL("What do you want to do with {1}'s Mail?",
  9470.            $PokemonGlobal.mailbox[mailIndex].sender),[
  9471.            _INTL("Read"),
  9472.            _INTL("Move to Bag"),
  9473.            _INTL("Give"),
  9474.            _INTL("Cancel")
  9475.            ],-1)
  9476.         if command==0 # Read
  9477.           pbFadeOutIn(99999){
  9478.             pbDisplayMail($PokemonGlobal.mailbox[mailIndex])
  9479.           }
  9480.         elsif command==1 # Move to Bag
  9481.           if Kernel.pbConfirmMessage(_INTL("The message will be lost. Is that OK?"))
  9482.             if $PokemonBag.pbStoreItem($PokemonGlobal.mailbox[mailIndex].item)
  9483.               Kernel.pbMessage(_INTL("The Mail was returned to the Bag with its message erased."))
  9484.               $PokemonGlobal.mailbox.delete_at(mailIndex)
  9485.             else
  9486.               Kernel.pbMessage(_INTL("The Bag is full."))
  9487.             end
  9488.           end
  9489.         elsif command==2 # Give
  9490.           pbFadeOutIn(99999){
  9491.             sscene = PokemonParty_Scene.new
  9492.             sscreen = PokemonPartyScreen.new(sscene,$Trainer.party)
  9493.             sscreen.pbPokemonGiveMailScreen(mailIndex)
  9494.           }
  9495.         end
  9496.       else
  9497.         break
  9498.       end
  9499.     end
  9500.   end
  9501. end
  9502. def pbTrainerPCMenu
  9503.   loop do
  9504.     command=Kernel.pbMessage(_INTL("What do you want to do?"),[
  9505.        _INTL("Item Storage"),
  9506.        _INTL("Mailbox"),
  9507.        _INTL("Turn Off")
  9508.        ],-1)
  9509.     if command==0
  9510.       pbPCItemStorage
  9511.     elsif command==1
  9512.       pbPCMailbox
  9513.     else
  9514.       break
  9515.     end
  9516.   end
  9517. end
  9518. class TrainerPC
  9519.   def shouldShow?
  9520.     return true
  9521.   end
  9522.   def name
  9523.     return _INTL("{1}'s PC",$Trainer.name)
  9524.   end
  9525.   def access
  9526.     Kernel.pbMessage(_INTL("\\se[PC access]Accessed {1}'s PC.",$Trainer.name))
  9527.     pbTrainerPCMenu
  9528.   end
  9529. end
  9530. def Kernel.pbGetStorageCreator
  9531.   creator=pbStorageCreator
  9532.   creator=_INTL("Bill") if !creator || creator==""
  9533.   return creator
  9534. end
  9535. class StorageSystemPC
  9536.   def shouldShow?
  9537.     return true
  9538.   end
  9539.   def name
  9540.     if $PokemonGlobal.seenStorageCreator
  9541.       return _INTL("{1}'s PC",Kernel.pbGetStorageCreator)
  9542.     else
  9543.       return _INTL("Someone's PC")
  9544.     end
  9545.   end
  9546.   def access
  9547.     Kernel.pbMessage(_INTL("\\se[PC access]The Pokémon Storage System was opened."))
  9548.     loop do
  9549.       command=Kernel.pbShowCommandsWithHelp(nil,
  9550.          [_INTL("Organize Boxes"),
  9551.          _INTL("Withdraw Pokémon"),
  9552.          _INTL("Deposit Pokémon"),
  9553.          _INTL("See ya!")],
  9554.          [_INTL("Organize the Pokémon in Boxes and in your party."),
  9555.          _INTL("Move Pokémon stored in Boxes to your party."),
  9556.          _INTL("Store Pokémon in your party in Boxes."),
  9557.          _INTL("Return to the previous menu.")],-1
  9558.       )
  9559.       if command>=0 && command<3
  9560.         if command==1   # Withdraw
  9561.         if $Trainer.partyplus>=1
  9562.           if $PokemonStorage.party.length>=$Trainer.partyplus
  9563.             Kernel.pbMessage(_INTL("Your party is full!"))
  9564.             next
  9565.           end
  9566.         else
  9567.           if $PokemonStorage.party.length>=6
  9568.             Kernel.pbMessage(_INTL("Your party is full!"))
  9569.             next
  9570.           end
  9571.         end
  9572.         elsif command==2   # Deposit
  9573.           count=0
  9574.           for p in $PokemonStorage.party
  9575.             count+=1 if p && !p.egg? && p.hp>0
  9576.           end
  9577.           if count<=1
  9578.             Kernel.pbMessage(_INTL("Can't deposit the last Pokémon!"))
  9579.             next
  9580.           end
  9581.         end
  9582.         pbFadeOutIn(99999){
  9583.           scene = PokemonStorageScene.new
  9584.           screen = PokemonStorageScreen.new(scene,$PokemonStorage)
  9585.           screen.pbStartScreen(command)
  9586.         }
  9587.       else
  9588.         break
  9589.       end
  9590.     end
  9591.   end
  9592. end
  9593. def pbTrainerPC
  9594.   Kernel.pbMessage(_INTL("\\se[PC open]{1} booted up the PC.",$Trainer.name))
  9595.   pbTrainerPCMenu
  9596.   pbSEPlay("PC close")
  9597. end
  9598. def pbPokeCenterPC
  9599.   Kernel.pbMessage(_INTL("\\se[PC open]{1} booted up the PC.",$Trainer.name))
  9600.   loop do
  9601.     commands=PokemonPCList.getCommandList
  9602.     command=Kernel.pbMessage(_INTL("Which PC should be accessed?"),commands,commands.length)
  9603.     break if !PokemonPCList.callCommand(command)
  9604.   end
  9605.   pbSEPlay("PC close")
  9606. end
  9607. module PokemonPCList
  9608.   @@pclist=[]
  9609.  
  9610.   def self.registerPC(pc)
  9611.     @@pclist.push(pc)
  9612.   end
  9613.  
  9614.   def self.getCommandList()
  9615.     commands=[]
  9616.     for pc in @@pclist
  9617.       if pc.shouldShow?
  9618.         commands.push(pc.name)
  9619.       end
  9620.     end
  9621.     commands.push(_INTL("Log Off"))
  9622.     return commands
  9623.   end
  9624.  
  9625.   def self.callCommand(cmd)
  9626.     if cmd<0 || cmd>=@@pclist.length
  9627.       return false
  9628.     end
  9629.     i=0
  9630.     for pc in @@pclist
  9631.       if pc.shouldShow?
  9632.         if i==cmd
  9633.            pc.access()
  9634.            return true
  9635.         end
  9636.         i+=1
  9637.       end
  9638.     end
  9639.     return false
  9640.   end
  9641. end
  9642. PokemonPCList.registerPC(StorageSystemPC.new)
  9643. PokemonPCList.registerPC(TrainerPC.new)
  9644. ################################################################################
  9645. # OrgBattle                                                                    #
  9646. ################################################################################
  9647. def pbBattleFactoryPokemon(rule,numwins,numswaps,rentals)
  9648.   table=nil
  9649.   btpokemon=pbGetBTPokemon(pbBattleChallenge.currentChallenge)
  9650.   ivtable=[
  9651.      0,6,3,6,
  9652.      7,13,6,9,
  9653.      14,20,9,12,
  9654.      21,27,12,15,
  9655.      28,34,15,21,
  9656.      35,41,21,31,
  9657.      42,-1,31,31
  9658.   ]
  9659.   groups=[
  9660.      1,14,6,0,
  9661.      15,21,5,1,
  9662.      22,28,4,2,
  9663.      29,35,3,3,
  9664.      36,42,2,4,
  9665.      43,-1,1,5
  9666.   ]
  9667.   if rule.ruleset.suggestedLevel!=100
  9668.     table=[
  9669.        0,6,110,199,
  9670.        7,13,162,266,
  9671.        14,20,267,371,
  9672.        21,27,372,467,
  9673.        28,34,468,563,
  9674.        35,41,564,659,
  9675.        42,48,660,755,
  9676.        49,-1,372,849
  9677.     ]
  9678.   else # Open Level (Level 100)
  9679.     table=[
  9680.        0,6,372,467,
  9681.        7,13,468,563,
  9682.        14,20,564,659,
  9683.        21,27,660,755,
  9684.        28,34,372,881,
  9685.        35,41,372,881,
  9686.        42,48,372,881,
  9687.        49,-1,372,881
  9688.     ]
  9689.   end
  9690.   pokemonNumbers=[0,0]
  9691.   ivs=[0,0]
  9692.   ivgroups=[6,0]
  9693.   for i in 0...table.length/4
  9694.     if table[i*4]<=numwins
  9695.       if (table[i*4+1]<0 || table[i*4+1]>=numwins)
  9696.         pokemonNumbers=[
  9697.            table[i*4+2]*btpokemon.length/882,
  9698.              table[i*4+3]*btpokemon.length/882
  9699.           ]
  9700.       end
  9701.     end
  9702.   end
  9703.   for i in 0...ivtable.length/4
  9704.     if ivtable[i*4]<=numwins
  9705.       if (ivtable[i*4+1]<0 || ivtable[i*4+1]>=numwins)
  9706.         ivs=[ivtable[i*4+2],ivtable[i*4+3]]
  9707.       end
  9708.     end
  9709.   end
  9710.   for i in 0...groups.length/4
  9711.     if groups[i*4]<=numswaps
  9712.       if (groups[i*4+1]<0 || groups[i*4+1]>=numswaps)
  9713.         ivgroups=[groups[i*4+2],groups[i*4+3]]
  9714.       end
  9715.     end
  9716.   end
  9717.   party=[]
  9718.   begin
  9719.     party.clear
  9720.     if $Trainer.partyplus>=1
  9721.     while party.length<$Trainer.partyplus
  9722.       rnd=pokemonNumbers[0]+rand(pokemonNumbers[1]-pokemonNumbers[0]+1)
  9723.       rndpoke=btpokemon[rnd]
  9724.       indvalue=(party.length<ivgroups[0]) ? ivs[0] : ivs[1]
  9725.       party.push(rndpoke.createPokemon(rule.ruleset.suggestedLevel,indvalue,nil))
  9726.     end
  9727.     else
  9728.     while party.length<6
  9729.       rnd=pokemonNumbers[0]+rand(pokemonNumbers[1]-pokemonNumbers[0]+1)
  9730.       rndpoke=btpokemon[rnd]
  9731.       indvalue=(party.length<ivgroups[0]) ? ivs[0] : ivs[1]
  9732.       party.push(rndpoke.createPokemon(rule.ruleset.suggestedLevel,indvalue,nil))
  9733.     end
  9734.     end
  9735.   end until rule.ruleset.isValid?(party)
  9736.   return party
  9737. end
  9738. ################################################################################
  9739. # PokemonUtilities                                                             #
  9740. ################################################################################
  9741. #===============================================================================
  9742. # Nicknaming and storing Pokémon
  9743. #===============================================================================
  9744. def pbBoxesFull?
  9745. if $Trainer.partyplus>=1
  9746.   return !$Trainer || ($Trainer.party.length==$Trainer.partyplus && $PokemonStorage.full?)
  9747. else
  9748.   return !$Trainer || ($Trainer.party.length==6 && $PokemonStorage.full?)
  9749. end
  9750. end
  9751. def pbStorePokemon(pokemon)
  9752.   if pbBoxesFull?
  9753.     Kernel.pbMessage(_INTL("There's no more room for Pokémon!\1"))
  9754.     Kernel.pbMessage(_INTL("The Pokémon Boxes are full and can't accept any more!"))
  9755.     return
  9756.   end
  9757.   pokemon.pbRecordFirstMoves
  9758. if $Trainer.partyplus>=1
  9759.   if $Trainer.party.length<$Trainer.partyplus
  9760.     $Trainer.party[$Trainer.party.length] = pokemon
  9761.   else
  9762.     oldcurbox = $PokemonStorage.currentBox
  9763.     storedbox = $PokemonStorage.pbStoreCaught(pokemon)
  9764.     curboxname = $PokemonStorage[oldcurbox].name
  9765.     boxname = $PokemonStorage[storedbox].name
  9766.     creator = nil
  9767.     creator = Kernel.pbGetStorageCreator if $PokemonGlobal.seenStorageCreator
  9768.     if storedbox!=oldcurbox
  9769.       if creator
  9770.         Kernel.pbMessage(_INTL("Box \"{1}\" on {2}'s PC was full.\1",curboxname,creator))
  9771.       else
  9772.         Kernel.pbMessage(_INTL("Box \"{1}\" on someone's PC was full.\1",curboxname))
  9773.       end
  9774.       Kernel.pbMessage(_INTL("{1} was transferred to box \"{2}.\"",pokemon.name,boxname))
  9775.     else
  9776.       if creator
  9777.         Kernel.pbMessage(_INTL("{1} was transferred to {2}'s PC.\1",pokemon.name,creator))
  9778.       else
  9779.         Kernel.pbMessage(_INTL("{1} was transferred to someone's PC.\1",pokemon.name))
  9780.       end
  9781.       Kernel.pbMessage(_INTL("It was stored in box \"{1}.\"",boxname))
  9782.     end
  9783.   end
  9784. else
  9785.   if $Trainer.party.length<6
  9786.     $Trainer.party[$Trainer.party.length] = pokemon
  9787.   else
  9788.     oldcurbox = $PokemonStorage.currentBox
  9789.     storedbox = $PokemonStorage.pbStoreCaught(pokemon)
  9790.     curboxname = $PokemonStorage[oldcurbox].name
  9791.     boxname = $PokemonStorage[storedbox].name
  9792.     creator = nil
  9793.     creator = Kernel.pbGetStorageCreator if $PokemonGlobal.seenStorageCreator
  9794.     if storedbox!=oldcurbox
  9795.       if creator
  9796.         Kernel.pbMessage(_INTL("Box \"{1}\" on {2}'s PC was full.\1",curboxname,creator))
  9797.       else
  9798.         Kernel.pbMessage(_INTL("Box \"{1}\" on someone's PC was full.\1",curboxname))
  9799.       end
  9800.       Kernel.pbMessage(_INTL("{1} was transferred to box \"{2}.\"",pokemon.name,boxname))
  9801.     else
  9802.       if creator
  9803.         Kernel.pbMessage(_INTL("{1} was transferred to {2}'s PC.\1",pokemon.name,creator))
  9804.       else
  9805.         Kernel.pbMessage(_INTL("{1} was transferred to someone's PC.\1",pokemon.name))
  9806.       end
  9807.       Kernel.pbMessage(_INTL("It was stored in box \"{1}.\"",boxname))
  9808.     end
  9809.   end
  9810. end
  9811. end
  9812. #===============================================================================
  9813. # Giving Pokémon to the player (will send to storage if party is full)
  9814. #===============================================================================
  9815. def pbAddPokemonSilent(pokemon,level=nil,seeform=true)
  9816.   return false if !pokemon || pbBoxesFull? || !$Trainer
  9817.   if pokemon.is_a?(String) || pokemon.is_a?(Symbol)
  9818.     pokemon = getID(PBSpecies,pokemon)
  9819.   end
  9820.   if pokemon.is_a?(Integer) && level.is_a?(Integer)
  9821.     pokemon = PokeBattle_Pokemon.new(pokemon,level,$Trainer)
  9822.   end
  9823.   $Trainer.seen[pokemon.species]  = true
  9824.   $Trainer.owned[pokemon.species] = true
  9825.   pbSeenForm(pokemon) if seeform
  9826.   pokemon.pbRecordFirstMoves
  9827. if $Trainer.partyplus>=1
  9828.   if $Trainer.party.length<$Trainer.partyplus
  9829.     $Trainer.party[$Trainer.party.length] = pokemon
  9830.   else
  9831.     $PokemonStorage.pbStoreCaught(pokemon)
  9832.   end
  9833. else
  9834.   if $Trainer.party.length<6
  9835.     $Trainer.party[$Trainer.party.length] = pokemon
  9836.   else
  9837.     $PokemonStorage.pbStoreCaught(pokemon)
  9838.   end
  9839. end
  9840.   return true
  9841. end
  9842. #===============================================================================
  9843. # Giving Pokémon/eggs to the player (can only add to party)
  9844. #===============================================================================
  9845. def pbAddToParty(pokemon,level=nil,seeform=true)
  9846. if $Trainer.partyplus>=1
  9847.   return false if !pokemon || !$Trainer || $Trainer.party.length>=$Trainer.partyplus
  9848. else
  9849.   return false if !pokemon || !$Trainer || $Trainer.party.length>=6
  9850. end
  9851.   if pokemon.is_a?(String) || pokemon.is_a?(Symbol)
  9852.     pokemon = getID(PBSpecies,pokemon)
  9853.   end
  9854.   if pokemon.is_a?(Integer) && level.is_a?(Integer)
  9855.     pokemon = PokeBattle_Pokemon.new(pokemon,level,$Trainer)
  9856.   end
  9857.   speciesname = PBSpecies.getName(pokemon.species)
  9858.   Kernel.pbMessage(_INTL("\\me[Pkmn get]{1} obtained {2}!\1",$Trainer.name,speciesname))
  9859.   pbNicknameAndStore(pokemon)
  9860.   pbSeenForm(pokemon) if seeform
  9861.   return true
  9862. end
  9863. def pbAddToPartySilent(pokemon,level=nil,seeform=true)
  9864. if $Trainer.partyplus>=1
  9865.   return false if !pokemon || !$Trainer || $Trainer.party.length>=$Trainer.partyplus
  9866. else
  9867.   return false if !pokemon || !$Trainer || $Trainer.party.length>=6
  9868. end
  9869.   if pokemon.is_a?(String) || pokemon.is_a?(Symbol)
  9870.     pokemon = getID(PBSpecies,pokemon)
  9871.   end
  9872.   if pokemon.is_a?(Integer) && level.is_a?(Integer)
  9873.     pokemon = PokeBattle_Pokemon.new(pokemon,level,$Trainer)
  9874.   end
  9875.   $Trainer.seen[pokemon.species]  = true
  9876.   $Trainer.owned[pokemon.species] = true
  9877.   pbSeenForm(pokemon) if seeform
  9878.   pokemon.pbRecordFirstMoves
  9879.   $Trainer.party[$Trainer.party.length] = pokemon
  9880.   return true
  9881. end
  9882. def pbAddForeignPokemon(pokemon,level=nil,ownerName=nil,nickname=nil,ownerGender=0,seeform=true)
  9883. if $Trainer.partyplus>=1
  9884.   return false if !pokemon || !$Trainer || $Trainer.party.length>=$Trainer.partyplus
  9885. else
  9886.   return false if !pokemon || !$Trainer || $Trainer.party.length>=6
  9887. end
  9888.   if pokemon.is_a?(String) || pokemon.is_a?(Symbol)
  9889.     pokemon = getID(PBSpecies,pokemon)
  9890.   end
  9891.   if pokemon.is_a?(Integer) && level.is_a?(Integer)
  9892.     pokemon = PokeBattle_Pokemon.new(pokemon,level,$Trainer)
  9893.   end
  9894.   # Set original trainer to a foreign one (if ID isn't already foreign)
  9895.   if pokemon.trainerID==$Trainer.id
  9896.     pokemon.trainerID = $Trainer.getForeignID
  9897.     pokemon.ot        = ownerName if ownerName && ownerName!=""
  9898.     pokemon.otgender  = ownerGender
  9899.   end
  9900.   # Set nickname
  9901.   pokemon.name = nickname[0,PokeBattle_Pokemon::NAMELIMIT] if nickname && nickname!=""
  9902.   # Recalculate stats
  9903.   pokemon.calcStats
  9904.   if ownerName
  9905.     Kernel.pbMessage(_INTL("\\me[Pkmn get]{1} received a Pokémon from {2}.\1",$Trainer.name,ownerName))
  9906.   else
  9907.     Kernel.pbMessage(_INTL("\\me[Pkmn get]{1} received a Pokémon.\1",$Trainer.name))
  9908.   end
  9909.   pbStorePokemon(pokemon)
  9910.   $Trainer.seen[pokemon.species]  = true
  9911.   $Trainer.owned[pokemon.species] = true
  9912.   pbSeenForm(pokemon) if seeform
  9913.   return true
  9914. end
  9915. def pbGenerateEgg(pokemon,text="")
  9916. if $Trainer.partyplus>=1
  9917.   return false if !pokemon || !$Trainer || $Trainer.party.length>=$Trainer.partyplus
  9918. else
  9919.   return false if !pokemon || !$Trainer || $Trainer.party.length>=6
  9920. end
  9921.   if pokemon.is_a?(String) || pokemon.is_a?(Symbol)
  9922.     pokemon = getID(PBSpecies,pokemon)
  9923.   end
  9924.   if pokemon.is_a?(Integer)
  9925.     pokemon = PokeBattle_Pokemon.new(pokemon,EGGINITIALLEVEL,$Trainer)
  9926.   end
  9927.   # Get egg steps
  9928.   dexdata = pbOpenDexData
  9929.   pbDexDataOffset(dexdata,pokemon.fSpecies,21)
  9930.   eggsteps = dexdata.fgetw
  9931.   dexdata.close
  9932.   # Set egg's details
  9933.   pokemon.name       = _INTL("Egg")
  9934.   pokemon.eggsteps   = eggsteps
  9935.   pokemon.obtainText = text
  9936.   pokemon.calcStats
  9937.   # Add egg to party
  9938.   $Trainer.party[$Trainer.party.length] = pokemon
  9939.   return true
  9940. end
  9941. ################################################################################
  9942. # Utilities                                                                    #
  9943. ################################################################################
  9944. #===============================================================================
  9945. # Other utilities
  9946. #===============================================================================
  9947. def pbMoveTutorAnnotations(move,movelist=nil)
  9948.   ret = []
  9949.   if $Trainer.partyplus>=1
  9950.   for i in 0...$Trainer.partyplus
  9951.     ret[i] = nil
  9952.     next if i>=$Trainer.party.length
  9953.     found = false
  9954.     for j in 0...4
  9955.       if !$Trainer.party[i].egg? && $Trainer.party[i].moves[j].id==move
  9956.         ret[i] = _INTL("LEARNED")
  9957.         found = true
  9958.       end
  9959.     end
  9960.     next if found
  9961.     species = $Trainer.party[i].species
  9962.     if !$Trainer.party[i].egg? && movelist && movelist.any?{|j| j==species }
  9963.       # Checked data from movelist
  9964.       ret[i] = _INTL("ABLE")
  9965.     elsif !$Trainer.party[i].egg? && $Trainer.party[i].isCompatibleWithMove?(move)
  9966.       # Checked data from PBS/tm.txt
  9967.       ret[i] = _INTL("ABLE")
  9968.     else
  9969.       ret[i] = _INTL("NOT ABLE")
  9970.     end
  9971.   end
  9972.   else
  9973.   for i in 0...6
  9974.     ret[i] = nil
  9975.     next if i>=$Trainer.party.length
  9976.     found = false
  9977.     for j in 0...4
  9978.       if !$Trainer.party[i].egg? && $Trainer.party[i].moves[j].id==move
  9979.         ret[i] = _INTL("LEARNED")
  9980.         found = true
  9981.       end
  9982.     end
  9983.     next if found
  9984.     species = $Trainer.party[i].species
  9985.     if !$Trainer.party[i].egg? && movelist && movelist.any?{|j| j==species }
  9986.       # Checked data from movelist
  9987.       ret[i] = _INTL("ABLE")
  9988.     elsif !$Trainer.party[i].egg? && $Trainer.party[i].isCompatibleWithMove?(move)
  9989.       # Checked data from PBS/tm.txt
  9990.       ret[i] = _INTL("ABLE")
  9991.     else
  9992.       ret[i] = _INTL("NOT ABLE")
  9993.     end
  9994.   end
  9995.   end
  9996.   return ret
  9997. end
  9998. ################################################################################
  9999. # Debug                                                                        #
  10000. ################################################################################
  10001. def pbDebugDayCare
  10002.   commands = [_INTL("Withdraw Pokémon 1"),
  10003.               _INTL("Withdraw Pokémon 2"),
  10004.               _INTL("Deposit Pokémon"),
  10005.               _INTL("Generate egg"),
  10006.               _INTL("Collect egg")]
  10007.   viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  10008.   viewport.z = 99999
  10009.   sprites = {}
  10010.   addBackgroundPlane(sprites,"background","hatchbg",viewport)
  10011.   sprites["overlay"] = BitmapSprite.new(Graphics.width,Graphics.height,viewport)
  10012.   pbSetSystemFont(sprites["overlay"].bitmap)
  10013.   sprites["cmdwindow"] = Window_CommandPokemonEx.new(commands)
  10014.   cmdwindow = sprites["cmdwindow"]
  10015.   cmdwindow.x        = 0
  10016.   cmdwindow.y        = Graphics.height-128
  10017.   cmdwindow.width    = Graphics.width
  10018.   cmdwindow.height   = 128
  10019.   cmdwindow.viewport = viewport
  10020.   cmdwindow.columns = 2
  10021.   base   = Color.new(248,248,248)
  10022.   shadow = Color.new(104,104,104)
  10023.   refresh = true
  10024.   loop do
  10025.     if refresh
  10026.       if pbEggGenerated?
  10027.         commands[3] = _INTL("Discard egg")
  10028.       else
  10029.         commands[3] = _INTL("Generate egg")
  10030.       end
  10031.       cmdwindow.commands = commands
  10032.       sprites["overlay"].bitmap.clear
  10033.       textpos = []
  10034.       for i in 0...2
  10035.         textpos.push([_INTL("Pokémon {1}",i+1),Graphics.width/4+i*Graphics.width/2,8,2,base,shadow])
  10036.       end
  10037.       for i in 0...pbDayCareDeposited
  10038.         next if !$PokemonGlobal.daycare[i][0]
  10039.         y = 40
  10040.         pkmn      = $PokemonGlobal.daycare[i][0]
  10041.         initlevel = $PokemonGlobal.daycare[i][1]
  10042.         leveldiff = pkmn.level-initlevel
  10043.         textpos.push([pkmn.name+" ("+PBSpecies.getName(pkmn.species)+")",8+i*Graphics.width/2,y,0,base,shadow])
  10044.         y += 32
  10045.         if pkmn.isMale?
  10046.           textpos.push([_INTL("Male ♂"),8+i*Graphics.width/2,y,0,Color.new(128,192,248),shadow])
  10047.         elsif pkmn.isFemale?
  10048.           textpos.push([_INTL("Female ♀"),8+i*Graphics.width/2,y,0,Color.new(248,96,96),shadow])
  10049.         else
  10050.           textpos.push([_INTL("Genderless"),8+i*Graphics.width/2,y,0,base,shadow])
  10051.         end
  10052.         y += 32
  10053.         if initlevel>=PBExperience::MAXLEVEL
  10054.           textpos.push(["Lv. #{initlevel} (max)",8+i*Graphics.width/2,y,0,base,shadow])
  10055.         elsif leveldiff>0
  10056.           textpos.push(["Lv. #{initlevel} -> #{pkmn.level} (+#{leveldiff})",
  10057.              8+i*Graphics.width/2,y,0,base,shadow])
  10058.         else
  10059.           textpos.push(["Lv. #{initlevel} (no change)",8+i*Graphics.width/2,y,0,base,shadow])
  10060.         end
  10061.         y += 32
  10062.         if pkmn.level<PBExperience::MAXLEVEL
  10063.           endexp   = PBExperience.pbGetStartExperience(pkmn.level+1,pkmn.growthrate)
  10064.           textpos.push(["To next Lv.: #{endexp-pkmn.exp}",8+i*Graphics.width/2,y,0,base,shadow])
  10065.           y += 32
  10066.         end
  10067.         textpos.push(["Cost: $#{$game_variables[costVariable]}",8+i*Graphics.width/2,y,0,base,shadow])
  10068.       end
  10069.       if pbEggGenerated?
  10070.         textpos.push(["Egg waiting for collection",Graphics.width/2,216,2,Color.new(248,248,0),shadow])
  10071.       elsif pbDayCareDeposited==2
  10072.         if pbDayCareGetCompat==0
  10073.           textpos.push(["Pokémon cannot breed",Graphics.width/2,216,2,Color.new(248,96,96),shadow])
  10074.         else
  10075.           textpos.push(["Pokémon can breed",Graphics.width/2,216,2,Color.new(64,248,64),shadow])
  10076.         end
  10077.       end
  10078.       pbDrawTextPositions(sprites["overlay"].bitmap,textpos)
  10079.       refresh = false
  10080.     end
  10081.     pbUpdateSpriteHash(sprites)
  10082.     Graphics.update
  10083.     Input.update
  10084.     if Input.trigger?(Input::B)
  10085.       break
  10086.     elsif Input.trigger?(Input::C)
  10087.       ret = cmdwindow.index
  10088.       if $Trainer.partyplus>=1
  10089.       case cmdwindow.index
  10090.       when 0 # Withdraw Pokémon 1
  10091.         if !$PokemonGlobal.daycare[0][0]
  10092.           pbPlayBuzzerSE
  10093.         elsif $Trainer.party.length>=$Trainer.partyplus
  10094.           pbPlayBuzzerSE
  10095.           Kernel.pbMessage(_INTL("Party is full, can't withdraw Pokémon."))
  10096.         else
  10097.           pbPlayDecisionSE
  10098.           pbDayCareGetDeposited(0,3,4)
  10099.           pbDayCareWithdraw(0)
  10100.           refresh = true
  10101.         end
  10102.       when 1 # Withdraw Pokémon 2
  10103.         if !$PokemonGlobal.daycare[1][0]
  10104.           pbPlayBuzzerSE
  10105.         elsif $Trainer.party.length>=$Trainer.partyplus
  10106.           pbPlayBuzzerSE
  10107.           Kernel.pbMessage(_INTL("Party is full, can't withdraw Pokémon."))
  10108.         else
  10109.           pbPlayDecisionSE
  10110.           pbDayCareGetDeposited(1,3,4)
  10111.           pbDayCareWithdraw(1)
  10112.           refresh = true
  10113.         end
  10114.       when 2 # Deposit Pokémon
  10115.         if pbDayCareDeposited==2
  10116.           pbPlayBuzzerSE
  10117.         elsif $Trainer.party.length==0
  10118.           pbPlayBuzzerSE
  10119.           Kernel.pbMessage(_INTL("Party is empty, can't deposit Pokémon."))
  10120.         else
  10121.           pbPlayDecisionSE
  10122.           pbChooseNonEggPokemon(1,3)
  10123.           if pbGet(1)>=0
  10124.             pbDayCareDeposit(pbGet(1))
  10125.             refresh = true
  10126.           end
  10127.         end
  10128.       when 3 # Generate/discard egg
  10129.         if pbEggGenerated?
  10130.           pbPlayDecisionSE
  10131.           $PokemonGlobal.daycareEgg      = 0
  10132.           $PokemonGlobal.daycareEggSteps = 0
  10133.           refresh = true
  10134.         else
  10135.           if pbDayCareDeposited!=2 || pbDayCareGetCompat==0
  10136.             pbPlayBuzzerSE
  10137.           else
  10138.             pbPlayDecisionSE
  10139.             $PokemonGlobal.daycareEgg = 1
  10140.             refresh = true
  10141.           end
  10142.         end
  10143.       when 4 # Collect egg
  10144.         if $PokemonGlobal.daycareEgg!=1
  10145.           pbPlayBuzzerSE
  10146.         elsif $Trainer.party.length>=$Trainer.partyplus
  10147.           pbPlayBuzzerSE
  10148.           Kernel.pbMessage(_INTL("Party is full, can't collect the egg."))
  10149.         else
  10150.           pbPlayDecisionSE
  10151.           pbDayCareGenerateEgg
  10152.           $PokemonGlobal.daycareEgg      = 0
  10153.           $PokemonGlobal.daycareEggSteps = 0
  10154.           Kernel.pbMessage(_INTL("Collected the {1} egg.",
  10155.              PBSpecies.getName($Trainer.lastParty.species)))
  10156.           refresh = true
  10157.         end
  10158.       end
  10159.       else
  10160.       case cmdwindow.index
  10161.       when 0 # Withdraw Pokémon 1
  10162.         if !$PokemonGlobal.daycare[0][0]
  10163.           pbPlayBuzzerSE
  10164.         elsif $Trainer.party.length>=6
  10165.           pbPlayBuzzerSE
  10166.           Kernel.pbMessage(_INTL("Party is full, can't withdraw Pokémon."))
  10167.         else
  10168.           pbPlayDecisionSE
  10169.           pbDayCareGetDeposited(0,3,4)
  10170.           pbDayCareWithdraw(0)
  10171.           refresh = true
  10172.         end
  10173.       when 1 # Withdraw Pokémon 2
  10174.         if !$PokemonGlobal.daycare[1][0]
  10175.           pbPlayBuzzerSE
  10176.         elsif $Trainer.party.length>=6
  10177.           pbPlayBuzzerSE
  10178.           Kernel.pbMessage(_INTL("Party is full, can't withdraw Pokémon."))
  10179.         else
  10180.           pbPlayDecisionSE
  10181.           pbDayCareGetDeposited(1,3,4)
  10182.           pbDayCareWithdraw(1)
  10183.           refresh = true
  10184.         end
  10185.       when 2 # Deposit Pokémon
  10186.         if pbDayCareDeposited==2
  10187.           pbPlayBuzzerSE
  10188.         elsif $Trainer.party.length==0
  10189.           pbPlayBuzzerSE
  10190.           Kernel.pbMessage(_INTL("Party is empty, can't deposit Pokémon."))
  10191.         else
  10192.           pbPlayDecisionSE
  10193.           pbChooseNonEggPokemon(1,3)
  10194.           if pbGet(1)>=0
  10195.             pbDayCareDeposit(pbGet(1))
  10196.             refresh = true
  10197.           end
  10198.         end
  10199.       when 3 # Generate/discard egg
  10200.         if pbEggGenerated?
  10201.           pbPlayDecisionSE
  10202.           $PokemonGlobal.daycareEgg      = 0
  10203.           $PokemonGlobal.daycareEggSteps = 0
  10204.           refresh = true
  10205.         else
  10206.           if pbDayCareDeposited!=2 || pbDayCareGetCompat==0
  10207.             pbPlayBuzzerSE
  10208.           else
  10209.             pbPlayDecisionSE
  10210.             $PokemonGlobal.daycareEgg = 1
  10211.             refresh = true
  10212.           end
  10213.         end
  10214.       when 4 # Collect egg
  10215.         if $PokemonGlobal.daycareEgg!=1
  10216.           pbPlayBuzzerSE
  10217.         elsif $Trainer.party.length>=6
  10218.           pbPlayBuzzerSE
  10219.           Kernel.pbMessage(_INTL("Party is full, can't collect the egg."))
  10220.         else
  10221.           pbPlayDecisionSE
  10222.           pbDayCareGenerateEgg
  10223.           $PokemonGlobal.daycareEgg      = 0
  10224.           $PokemonGlobal.daycareEggSteps = 0
  10225.           Kernel.pbMessage(_INTL("Collected the {1} egg.",
  10226.              PBSpecies.getName($Trainer.lastParty.species)))
  10227.           refresh = true
  10228.         end
  10229.       end
  10230.       end
  10231.     end
  10232.   end
  10233.   pbDisposeSpriteHash(sprites)
  10234.   viewport.dispose
  10235. end
  10236. #===============================================================================
  10237. # Individual trainer editor
  10238. #===============================================================================
  10239. module TrainerBattleProperty
  10240.   def self.set(settingname,oldsetting)
  10241.     return oldsetting if !oldsetting
  10242.     properties = [
  10243.        [_INTL("Trainer Type"),TrainerTypeProperty,_INTL("Name of the trainer type for this Trainer.")],
  10244.        [_INTL("Trainer Name"),StringProperty,_INTL("Name of the Trainer.")],
  10245.        [_INTL("Battle ID"),LimitProperty.new(255),_INTL("ID used to distinguish Trainers with the same name and trainer type.")],
  10246.        [_INTL("Pokémon 1"),TrainerPokemonProperty,_INTL("1st Pokémon.")],
  10247.        [_INTL("Pokémon 2"),TrainerPokemonProperty,_INTL("2nd Pokémon.")],
  10248.        [_INTL("Pokémon 3"),TrainerPokemonProperty,_INTL("3rd Pokémon.")],
  10249.        [_INTL("Pokémon 4"),TrainerPokemonProperty,_INTL("4th Pokémon.")],
  10250.        [_INTL("Pokémon 5"),TrainerPokemonProperty,_INTL("5th Pokémon.")],
  10251.        [_INTL("Pokémon 6"),TrainerPokemonProperty,_INTL("6th Pokémon.")],
  10252.        [_INTL("Pokémon 7"),TrainerPokemonProperty,_INTL("7th Pokémon.")],
  10253.        [_INTL("Pokémon 8"),TrainerPokemonProperty,_INTL("8th Pokémon.")],
  10254.        [_INTL("Pokémon 9"),TrainerPokemonProperty,_INTL("9th Pokémon.")],
  10255.        [_INTL("Pokémon 10"),TrainerPokemonProperty,_INTL("10th Pokémon.")],
  10256.        [_INTL("Pokémon 11"),TrainerPokemonProperty,_INTL("11th Pokémon.")],
  10257.        [_INTL("Pokémon 12"),TrainerPokemonProperty,_INTL("12th Pokémon.")],
  10258.        [_INTL("Pokémon 13"),TrainerPokemonProperty,_INTL("13th Pokémon.")],
  10259.        [_INTL("Pokémon 14"),TrainerPokemonProperty,_INTL("14th Pokémon.")],
  10260.        [_INTL("Pokémon 15"),TrainerPokemonProperty,_INTL("15th Pokémon.")],
  10261.        [_INTL("Pokémon 16"),TrainerPokemonProperty,_INTL("16th Pokémon.")],
  10262.        [_INTL("Pokémon 17"),TrainerPokemonProperty,_INTL("17th Pokémon.")],
  10263.        [_INTL("Pokémon 18"),TrainerPokemonProperty,_INTL("18th Pokémon.")],
  10264.        [_INTL("Pokémon 19"),TrainerPokemonProperty,_INTL("19th Pokémon.")],
  10265.        [_INTL("Pokémon 20"),TrainerPokemonProperty,_INTL("20th Pokémon.")],
  10266.        [_INTL("Pokémon 21"),TrainerPokemonProperty,_INTL("21th Pokémon.")],
  10267.        [_INTL("Pokémon 22"),TrainerPokemonProperty,_INTL("22th Pokémon.")],
  10268.        [_INTL("Pokémon 23"),TrainerPokemonProperty,_INTL("23th Pokémon.")],
  10269.        [_INTL("Pokémon 24"),TrainerPokemonProperty,_INTL("14th Pokémon.")],
  10270.        [_INTL("Pokémon 25"),TrainerPokemonProperty,_INTL("25th Pokémon.")],
  10271.        [_INTL("Pokémon 26"),TrainerPokemonProperty,_INTL("26th Pokémon.")],
  10272.        [_INTL("Pokémon 27"),TrainerPokemonProperty,_INTL("27th Pokémon.")],
  10273.        [_INTL("Pokémon 28"),TrainerPokemonProperty,_INTL("28th Pokémon.")],
  10274.        [_INTL("Pokémon 29"),TrainerPokemonProperty,_INTL("29th Pokémon.")],
  10275.        [_INTL("Pokémon 30"),TrainerPokemonProperty,_INTL("30th Pokémon.")],
  10276.        [_INTL("Pokémon 31"),TrainerPokemonProperty,_INTL("31th Pokémon.")],
  10277.        [_INTL("Pokémon 32"),TrainerPokemonProperty,_INTL("32th Pokémon.")],
  10278.        [_INTL("Pokémon 33"),TrainerPokemonProperty,_INTL("33th Pokémon.")],
  10279.        [_INTL("Pokémon 34"),TrainerPokemonProperty,_INTL("34th Pokémon.")],
  10280.        [_INTL("Pokémon 35"),TrainerPokemonProperty,_INTL("35th Pokémon.")],
  10281.        [_INTL("Pokémon 36"),TrainerPokemonProperty,_INTL("36th Pokémon.")],
  10282.        [_INTL("Pokémon 37"),TrainerPokemonProperty,_INTL("37th Pokémon.")],
  10283.        [_INTL("Pokémon 38"),TrainerPokemonProperty,_INTL("38th Pokémon.")],
  10284.        [_INTL("Pokémon 39"),TrainerPokemonProperty,_INTL("39th Pokémon.")],
  10285.        [_INTL("Pokémon 40"),TrainerPokemonProperty,_INTL("40th Pokémon.")],
  10286.        [_INTL("Item 1"),ItemProperty,_INTL("Item used by the trainer during battle.")],
  10287.        [_INTL("Item 2"),ItemProperty,_INTL("Item used by the trainer during battle.")],
  10288.        [_INTL("Item 3"),ItemProperty,_INTL("Item used by the trainer during battle.")],
  10289.        [_INTL("Item 4"),ItemProperty,_INTL("Item used by the trainer during battle.")],
  10290.        [_INTL("Item 5"),ItemProperty,_INTL("Item used by the trainer during battle.")],
  10291.        [_INTL("Item 6"),ItemProperty,_INTL("Item used by the trainer during battle.")],
  10292.        [_INTL("Item 7"),ItemProperty,_INTL("Item used by the trainer during battle.")],
  10293.        [_INTL("Item 8"),ItemProperty,_INTL("Item used by the trainer during battle.")]
  10294.     ]
  10295.     if !pbPropertyList(settingname,oldsetting,properties,true)
  10296.       oldsetting = nil
  10297.     else
  10298.       oldsetting = nil if !oldsetting[0] || oldsetting[0]==0
  10299.     end
  10300.     return oldsetting
  10301.   end
  10302.  
  10303.   def self.format(value)
  10304.     return value.inspect
  10305.   end
  10306. end
  10307. def pbTrainerBattleEditor
  10308.   selection = 0
  10309.   trainers     = load_data("Data/trainers.dat")
  10310.   trainertypes = load_data("Data/trainertypes.dat")
  10311.   modified = false
  10312.   for trainer in trainers
  10313.     trtype = trainer[0]
  10314.     if !trainertypes || !trainertypes[trtype]
  10315.       trainer[0] = 0
  10316.       modified = true
  10317.     end
  10318.   end
  10319.   if modified
  10320.     save_data(trainers,"Data/trainers.dat")
  10321.     pbConvertTrainerData
  10322.   end
  10323.   pbListScreenBlock(_INTL("Trainer Battles"),TrainerBattleLister.new(selection,true)){|button,trtype|
  10324.     if trtype
  10325.       index = trtype[0]
  10326.       trainerdata = trtype[1]
  10327.       if button==Input::A
  10328.         if index>=0
  10329.           if Kernel.pbConfirmMessageSerious("Delete this trainer battle?")
  10330.             data = load_data("Data/trainers.dat")
  10331.             data.delete_at(index)
  10332.             save_data(data,"Data/trainers.dat")
  10333.             pbConvertTrainerData
  10334.             Kernel.pbMessage(_INTL("The Trainer battle was deleted."))
  10335.           end
  10336.         end
  10337.       elsif button==Input::C
  10338.         selection = index
  10339.         if selection<0
  10340.           ret=Kernel.pbMessage(_INTL("First, define the type of trainer."),[
  10341.              _INTL("Use existing type"),
  10342.              _INTL("Use new type"),
  10343.              _INTL("Cancel")],3)
  10344.           trainertype = -1
  10345.           trainername = ""
  10346.           if ret==0
  10347.             trainertype = pbListScreen(_INTL("TRAINER TYPE"),TrainerTypeLister.new(0,false))
  10348.             next if !trainertype
  10349.             trainertype = trainertype[0]
  10350.             next if trainertype<0
  10351.           elsif ret==1
  10352.             trainertype = pbTrainerTypeEditorNew(nil)
  10353.             next if trainertype<0
  10354.           else
  10355.             next
  10356.           end
  10357.           trainername = Kernel.pbMessageFreeText(_INTL("Now enter the trainer's name."),"",false,32)
  10358.           next if trainername==""
  10359.           trainerparty = pbGetFreeTrainerParty(trainertype,trainername)
  10360.           if trainerparty<0
  10361.             Kernel.pbMessage(_INTL("There is no room to create a trainer of that type and name."))
  10362.             next
  10363.           end
  10364.           ###############
  10365.           pbNewTrainer(trainertype,trainername,trainerparty)
  10366.         else
  10367.           data = [
  10368.              trainerdata[0],      # Trainer type - 0
  10369.              trainerdata[1],      # Trainer name - 1
  10370.              trainerdata[4],      # ID - 2
  10371.              trainerdata[3][0],   # Pokémon 1 - 3
  10372.              trainerdata[3][1],   # Pokémon 2 - 4
  10373.              trainerdata[3][2],   # Pokémon 3 - 5
  10374.              trainerdata[3][3],   # Pokémon 4 - 6
  10375.              trainerdata[3][4],   # Pokémon 5 - 7
  10376.              trainerdata[3][5],   # Pokémon 6 - 8
  10377.              trainerdata[3][6],   # Pokémon 7 - 9
  10378.              trainerdata[3][7],   # Pokémon 8 - 10
  10379.              trainerdata[3][8],   # Pokémon 9 - 11
  10380.              trainerdata[3][9],   # Pokémon 10
  10381.              trainerdata[3][10],  # Pokémon 11
  10382.              trainerdata[3][11],  # Pokémon 12
  10383.              trainerdata[3][12],  # Pokémon 13
  10384.              trainerdata[3][13],  # Pokémon 14
  10385.              trainerdata[3][14],  # Pokémon 15
  10386.              trainerdata[3][15],  # Pokémon 16
  10387.              trainerdata[3][16],  # Pokémon 17
  10388.              trainerdata[3][17],  # Pokémon 18
  10389.              trainerdata[3][18],  # Pokémon 19
  10390.              trainerdata[3][19],  # Pokémon 20
  10391.              trainerdata[3][20],  # Pokémon 21
  10392.              trainerdata[3][21],  # Pokémon 22
  10393.              trainerdata[3][22],  # Pokémon 23
  10394.              trainerdata[3][23],  # Pokémon 24
  10395.              trainerdata[3][24],  # Pokémon 25
  10396.              trainerdata[3][25],  # Pokémon 26
  10397.              trainerdata[3][26],  # Pokémon 27
  10398.              trainerdata[3][27],  # Pokémon 28
  10399.              trainerdata[3][28],  # Pokémon 29
  10400.              trainerdata[3][29],  # Pokémon 30
  10401.              trainerdata[3][30],  # Pokémon 31
  10402.              trainerdata[3][31],  # Pokémon 32
  10403.              trainerdata[3][32],  # Pokémon 33
  10404.              trainerdata[3][33],  # Pokémon 34
  10405.              trainerdata[3][34],  # Pokémon 35
  10406.              trainerdata[3][35],  # Pokémon 36
  10407.              trainerdata[3][36],  # Pokémon 37
  10408.              trainerdata[3][37],  # Pokémon 38
  10409.              trainerdata[3][38],  # Pokémon 39
  10410.              trainerdata[3][39],  # Pokémon 40
  10411.              trainerdata[2][0],   # Item 1
  10412.              trainerdata[2][1],   # Item 2
  10413.              trainerdata[2][2],   # Item 3
  10414.              trainerdata[2][3],   # Item 4
  10415.              trainerdata[2][4],   # Item 5
  10416.              trainerdata[2][5],   # Item 6
  10417.              trainerdata[2][6],   # Item 7
  10418.              trainerdata[2][7]    # Item 8
  10419.           ]
  10420.           save = false
  10421.           while true
  10422.             data = TrainerBattleProperty.set(trainerdata[1],data)
  10423.             if data
  10424.               trainerdata = [
  10425.                  data[0],
  10426.                  data[1],
  10427.                  [data[43],data[44],data[45],data[46],data[47],data[48],data[49],data[50]].find_all {|i| i && i!=0 },   # Item list
  10428.                  [data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10],data[11],data[12],data[13],data[14],data[15],data[16],data[17],data[18],data[19],data[20],data[21],data[22],data[23],data[24],data[25],data[26],data[27],data[28],data[29],data[30],data[31],data[32],data[33],data[34],data[35],data[36],data[37],data[38],data[39],data[40],data[41],data[42]].find_all {|i| i && i[TPSPECIES]!=0 },   # Pokémon list
  10429.                  data[2]
  10430.               ]
  10431.               if trainerdata[3].length==0
  10432.                 Kernel.pbMessage(_INTL("Can't save. The Pokémon list is empty."))
  10433.               elsif !trainerdata[1] || trainerdata[1].length==0
  10434.                 Kernel.pbMessage(_INTL("Can't save. No name was entered."))
  10435.               else
  10436.                 save = true
  10437.                 break
  10438.               end
  10439.             else
  10440.               break
  10441.             end
  10442.           end
  10443.           if save
  10444.             data = load_data("Data/trainers.dat")
  10445.             data[index] = trainerdata
  10446.             save_data(data,"Data/trainers.dat")
  10447.             pbConvertTrainerData
  10448.           end
  10449.         end
  10450.       end
  10451.     end
  10452.   }
  10453. end
  10454. ################################################################################
  10455. # Storage Inprovements                                                         #
  10456. ################################################################################
  10457. class PokemonStorage
  10458.   attr_reader :boxes
  10459.   attr_accessor :currentBox
  10460.   attr_writer :unlockedWallpapers
  10461.   BASICWALLPAPERQTY = 16
  10462.   def initialize(maxBoxes=STORAGEBOXES,maxPokemon=30)
  10463.     @boxes = []
  10464.     for i in 0...maxBoxes
  10465.       @boxes[i] = PokemonBox.new(_INTL("Box {1}",i+1),maxPokemon)
  10466.       @boxes[i].background = i%BASICWALLPAPERQTY
  10467.     end
  10468.     @currentBox = 0
  10469.     @boxmode = -1
  10470.     @unlockedWallpapers = []
  10471.     for i in 0...allWallpapers.length
  10472.       @unlockedWallpapers[i] = false
  10473.     end
  10474.   end
  10475.   def allWallpapers
  10476.     return [
  10477.        # Basic wallpapers
  10478.        _INTL("Forest"),_INTL("City"),_INTL("Desert"),_INTL("Savanna"),
  10479.        _INTL("Crag"),_INTL("Volcano"),_INTL("Snow"),_INTL("Cave"),
  10480.        _INTL("Beach"),_INTL("Seafloor"),_INTL("River"),_INTL("Sky"),
  10481.        _INTL("Poké Center"),_INTL("Machine"),_INTL("Checks"),_INTL("Simple"),
  10482.        # Special wallpapers
  10483.        _INTL("Space"),_INTL("Backyard"),_INTL("Nostalgic 1"),_INTL("Torchic"),
  10484.        _INTL("Trio 1"),_INTL("PikaPika 1"),_INTL("Legend 1"),_INTL("Team Galactic 1"),
  10485.        _INTL("Distortion"),_INTL("Contest"),_INTL("Nostalgic 2"),_INTL("Croagunk"),
  10486.        _INTL("Trio 2"),_INTL("PikaPika 2"),_INTL("Legend 2"),_INTL("Team Galactic 2"),
  10487.        _INTL("Heart"),_INTL("Soul"),_INTL("Big Brother"),_INTL("Pokéathlon"),
  10488.        _INTL("Trio 3"),_INTL("Spiky Pika"),_INTL("Kimono Girl"),_INTL("Revival")
  10489.     ]
  10490.   end
  10491.   def unlockedWallpapers
  10492.     @unlockedWallpapers = [] if !@unlockedWallpapers
  10493.     return @unlockedWallpapers
  10494.   end
  10495.   def availableWallpapers
  10496.     ret = [[],[]]   # Names, IDs
  10497.     papers = allWallpapers
  10498.     @unlockedWallpapers = [] if !@unlockedWallpapers
  10499.     for i in 0...papers.length
  10500.       next if !isAvailableWallpaper(i)
  10501.       ret[0].push(papers[i]); ret[1].push(i)
  10502.     end
  10503.     return ret
  10504.   end
  10505.   def isAvailableWallpaper(i)
  10506.     @unlockedWallpapers = [] if !@unlockedWallpapers
  10507.     return true if i<BASICWALLPAPERQTY
  10508.     return true if @unlockedWallpapers[i]
  10509.     return false
  10510.   end
  10511.   def party
  10512.     $Trainer.party
  10513.   end
  10514.   def party=(value)
  10515.     raise ArgumentError.new("Not supported")
  10516.   end
  10517.   def maxBoxes
  10518.     return @boxes.length
  10519.   end
  10520.   def maxPokemon(box)
  10521.     return 0 if box>=self.maxBoxes
  10522.     return (box<0) ? 6 : self[box].length
  10523.   end
  10524.   def full?
  10525.     for i in 0...self.maxBoxes
  10526.       return false if !@boxes[i].full?
  10527.     end
  10528.     return true
  10529.   end
  10530.   def pbFirstFreePos(box)
  10531.     if box==-1
  10532.       ret = self.party.nitems
  10533.       return (ret==6) ? -1 : ret
  10534.     else
  10535.       for i in 0...maxPokemon(box)
  10536.         return i if !self[box,i]
  10537.       end
  10538.       return -1
  10539.     end
  10540.   end
  10541.   def [](x,y=nil)
  10542.     if y==nil
  10543.       return (x==-1) ? self.party : @boxes[x]
  10544.     else
  10545.       for i in @boxes
  10546.         raise "Box is a Pokémon, not a box" if i.is_a?(PokeBattle_Pokemon)
  10547.       end
  10548.       return (x==-1) ? self.party[y] : @boxes[x][y]
  10549.     end
  10550.   end
  10551.   def []=(x,y,value)
  10552.     if x==-1
  10553.       self.party[y] = value
  10554.     else
  10555.       @boxes[x][y] = value
  10556.     end
  10557.   end
  10558.   def pbCopy(boxDst,indexDst,boxSrc,indexSrc)
  10559.     if indexDst<0 && boxDst<self.maxBoxes
  10560.       found = false
  10561.       for i in 0...maxPokemon(boxDst)
  10562.         if !self[boxDst,i]
  10563.           found = true
  10564.           indexDst = i
  10565.           break
  10566.         end
  10567.       end
  10568.       return false if !found
  10569.     end
  10570.     if boxDst==-1
  10571.     if $Trainer.partyplus>=1
  10572.       return false if self.party.nitems>=$Trainer.partyplus
  10573.     else
  10574.       return false if self.party.nitems>=6
  10575.     end
  10576.       self.party[self.party.length] = self[boxSrc,indexSrc]
  10577.       self.party.compact!
  10578.     else
  10579.       pkmn = self[boxSrc,indexSrc]
  10580.       if !pkmn
  10581.         raise "Trying to copy nil to storage"
  10582.       end
  10583.       pkmn.heal
  10584.       pkmn.formTime = nil if pkmn.respond_to?("formTime") && pkmn.formTime
  10585.       self[boxDst,indexDst] = pkmn
  10586.     end
  10587.     return true
  10588.   end
  10589.   def pbMove(boxDst,indexDst,boxSrc,indexSrc)
  10590.     return false if !pbCopy(boxDst,indexDst,boxSrc,indexSrc)
  10591.     pbDelete(boxSrc,indexSrc)
  10592.     return true
  10593.   end
  10594.   def pbMoveCaughtToParty(pkmn)
  10595.   if $Trainer.partyplus>=1
  10596.     return false if self.party.nitems>=$Trainer.partyplus
  10597.   else
  10598.     return false if self.party.nitems>=6
  10599.   end
  10600.     self.party[self.party.length] = pkmn
  10601.   end
  10602.   def pbMoveCaughtToBox(pkmn,box)
  10603.     for i in 0...maxPokemon(box)
  10604.       if self[box,i]==nil
  10605.         if box>=0
  10606.           pkmn.heal
  10607.           pkmn.formTime = nil if pkmn.respond_to?("formTime") && pkmn.formTime
  10608.         end
  10609.         self[box,i] = pkmn
  10610.         return true
  10611.       end
  10612.     end
  10613.     return false
  10614.   end
  10615.   def pbStoreCaught(pkmn)
  10616.     for i in 0...maxPokemon(@currentBox)
  10617.       if self[@currentBox,i]==nil
  10618.         self[@currentBox,i] = pkmn
  10619.         return @currentBox
  10620.       end
  10621.     end
  10622.     for j in 0...self.maxBoxes
  10623.       for i in 0...maxPokemon(j)
  10624.         if self[j,i]==nil
  10625.           self[j,i] = pkmn
  10626.           @currentBox = j
  10627.           return @currentBox
  10628.         end
  10629.       end
  10630.     end
  10631.     return -1
  10632.   end
  10633.   def pbDelete(box,index)
  10634.     if self[box,index]
  10635.       self[box,index] = nil
  10636.       self.party.compact! if box==-1
  10637.     end
  10638.   end
  10639.   def clear
  10640.     for i in 0...self.maxBoxes
  10641.       @boxes[i].clear
  10642.     end
  10643.   end
  10644. end
  10645. ################################################################################
  10646. # Battles                                                                      #
  10647. ################################################################################
  10648. #===============================================================================
  10649. # Start a double wild battle
  10650. #===============================================================================
  10651. def pbDoubleWildBattle(species1,level1,species2,level2,variable=nil,canescape=true,canlose=false)
  10652.   if (Input.press?(Input::CTRL) && $DEBUG) || $Trainer.pokemonCount==0
  10653.     if $Trainer.pokemonCount>0
  10654.       Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
  10655.     end
  10656.     pbSet(variable,1)
  10657.     $PokemonGlobal.nextBattleBGM  = nil
  10658.     $PokemonGlobal.nextBattleME   = nil
  10659.     $PokemonGlobal.nextBattleBack = nil
  10660.     return true
  10661.   end
  10662.   if species1.is_a?(String) || species1.is_a?(Symbol)
  10663.     species1 = getID(PBSpecies,species1)
  10664.   end
  10665.   if species2.is_a?(String) || species2.is_a?(Symbol)
  10666.     species2 = getID(PBSpecies,species2)
  10667.   end
  10668.   currentlevels = []
  10669.   for i in $Trainer.party
  10670.     currentlevels.push(i.level)
  10671.   end
  10672.   genwildpoke  = pbGenerateWildPokemon(species1,level1)
  10673.   genwildpoke2 = pbGenerateWildPokemon(species2,level2)
  10674.   Events.onStartBattle.trigger(nil,genwildpoke)
  10675.   scene = pbNewBattleScene
  10676.   if $PokemonGlobal.partner
  10677.     othertrainer = PokeBattle_Trainer.new($PokemonGlobal.partner[1],$PokemonGlobal.partner[0])
  10678.     othertrainer.id    = $PokemonGlobal.partner[2]
  10679.     othertrainer.party = $PokemonGlobal.partner[3]
  10680.     combinedParty = []
  10681.     for i in 0...$Trainer.party.length
  10682.       combinedParty[i] = $Trainer.party[i]
  10683.     end
  10684.     for i in 0...othertrainer.party.length
  10685.       combinedParty[40+i] = othertrainer.party[i]
  10686.     end
  10687.     battle = PokeBattle_Battle.new(scene,combinedParty,[genwildpoke,genwildpoke2],[$Trainer,othertrainer],nil)
  10688.     battle.fullparty1 = true
  10689.   else
  10690.     battle = PokeBattle_Battle.new(scene,$Trainer.party,[genwildpoke,genwildpoke2],$Trainer,nil)
  10691.     battle.fullparty1 = false
  10692.   end
  10693.   battle.internalbattle = true
  10694.   battle.doublebattle   = battle.pbDoubleBattleAllowed?()
  10695.   battle.cantescape     = !canescape
  10696.   pbPrepareBattle(battle)
  10697.   decision = 0
  10698.   pbBattleAnimation(pbGetWildBattleBGM(species1),2,[genwildpoke,genwildpoke2]) {
  10699.     pbSceneStandby {
  10700.       decision = battle.pbStartBattle(canlose)
  10701.     }
  10702.     pbAfterBattle(decision,canlose)
  10703.   }
  10704.   Input.update
  10705.   pbSet(variable,decision)
  10706.   return (decision!=2 && decision!=5)
  10707. end
  10708. #===============================================================================
  10709. # Start a trainer battle against one trainer
  10710. #===============================================================================
  10711. def pbTrainerBattle(trainerid,trainername,endspeech,
  10712.                     doublebattle=false,trainerparty=0,canlose=false,variable=nil)
  10713.   if $Trainer.pokemonCount==0
  10714.     Kernel.pbMessage(_INTL("SKIPPING BATTLE...")) if $DEBUG
  10715.     return false
  10716.   end
  10717.   if !$PokemonTemp.waitingTrainer && pbMapInterpreterRunning? &&
  10718.      ($Trainer.ablePokemonCount>1 || $Trainer.ablePokemonCount>0 && $PokemonGlobal.partner)
  10719.     thisEvent = pbMapInterpreter.get_character(0)
  10720.     triggeredEvents = $game_player.pbTriggeredTrainerEvents([2],false)
  10721.     otherEvent = []
  10722.     for i in triggeredEvents
  10723.       if i.id!=thisEvent.id && !$game_self_switches[[$game_map.map_id,i.id,"A"]]
  10724.         otherEvent.push(i)
  10725.       end
  10726.     end
  10727.     if otherEvent.length==1
  10728.       trainer = pbLoadTrainer(trainerid,trainername,trainerparty)
  10729.       Events.onTrainerPartyLoad.trigger(nil,trainer)
  10730.       if !trainer
  10731.         pbMissingTrainer(trainerid,trainername,trainerparty)
  10732.         return false
  10733.       end
  10734.       if trainer[2].length<=40
  10735.         $PokemonTemp.waitingTrainer=[trainer,thisEvent.id,endspeech]
  10736.         return false
  10737.       end
  10738.     end
  10739.   end
  10740.   trainer = pbLoadTrainer(trainerid,trainername,trainerparty)
  10741.   Events.onTrainerPartyLoad.trigger(nil,trainer)
  10742.   if !trainer
  10743.     pbMissingTrainer(trainerid,trainername,trainerparty)
  10744.     return false
  10745.   end
  10746.   if $PokemonGlobal.partner && ($PokemonTemp.waitingTrainer || doublebattle)
  10747.     othertrainer = PokeBattle_Trainer.new($PokemonGlobal.partner[1],$PokemonGlobal.partner[0])
  10748.     othertrainer.id    = $PokemonGlobal.partner[2]
  10749.     othertrainer.party = $PokemonGlobal.partner[3]
  10750.     playerparty = []
  10751.     for i in 0...$Trainer.party.length
  10752.       playerparty[i] = $Trainer.party[i]
  10753.     end
  10754.     for i in 0...othertrainer.party.length
  10755.       playerparty[40+i] = othertrainer.party[i]
  10756.     end
  10757.     playertrainer = [$Trainer,othertrainer]
  10758.     fullparty1    = true
  10759.     doublebattle  = true
  10760.   else
  10761.     playerparty   = $Trainer.party
  10762.     playertrainer = $Trainer
  10763.     fullparty1    = false
  10764.   end
  10765.   if $PokemonTemp.waitingTrainer
  10766.     combinedParty = []
  10767.     fullparty2 = false
  10768.     if $PokemonTemp.waitingTrainer[0][2].length>3 || trainer[2].length>3
  10769.       for i in 0...$PokemonTemp.waitingTrainer[0][2].length
  10770.         combinedParty[i] = $PokemonTemp.waitingTrainer[0][2][i]
  10771.       end
  10772.       for i in 0...trainer[2].length
  10773.         combinedParty[40+i] = trainer[2][i]
  10774.       end
  10775.       fullparty2 = true
  10776.     else
  10777.       for i in 0...$PokemonTemp.waitingTrainer[0][2].length
  10778.         combinedParty[i] = $PokemonTemp.waitingTrainer[0][2][i]
  10779.       end
  10780.       for i in 0...trainer[2].length
  10781.         combinedParty[20+i] = trainer[2][i]
  10782.       end
  10783.     end
  10784.     scene = pbNewBattleScene
  10785.     battle = PokeBattle_Battle.new(scene,playerparty,combinedParty,playertrainer,
  10786.                                    [$PokemonTemp.waitingTrainer[0][0],trainer[0]])
  10787.     battle.fullparty1   = fullparty1
  10788.     battle.fullparty2   = fullparty2
  10789.     battle.doublebattle = battle.pbDoubleBattleAllowed?
  10790.     battle.endspeech    = $PokemonTemp.waitingTrainer[2]
  10791.     battle.endspeech2   = endspeech
  10792.     battle.items        = [$PokemonTemp.waitingTrainer[0][1],trainer[1]]
  10793.     trainerbgm = pbGetTrainerBattleBGM([$PokemonTemp.waitingTrainer[0][0],trainer[0]])
  10794.   else
  10795.     scene = pbNewBattleScene
  10796.     battle = PokeBattle_Battle.new(scene,playerparty,trainer[2],playertrainer,trainer[0])
  10797.     battle.fullparty1   = fullparty1
  10798.     battle.doublebattle = (doublebattle) ? battle.pbDoubleBattleAllowed? : false
  10799.     battle.endspeech    = endspeech
  10800.     battle.items        = trainer[1]
  10801.     trainerbgm = pbGetTrainerBattleBGM(trainer[0])
  10802.   end
  10803.   if Input.press?(Input::CTRL) && $DEBUG
  10804.     Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
  10805.     Kernel.pbMessage(_INTL("AFTER LOSING..."))
  10806.     Kernel.pbMessage(battle.endspeech)
  10807.     Kernel.pbMessage(battle.endspeech2) if battle.endspeech2
  10808.     if $PokemonTemp.waitingTrainer
  10809.       pbMapInterpreter.pbSetSelfSwitch($PokemonTemp.waitingTrainer[1],"A",true)
  10810.       $PokemonTemp.waitingTrainer = nil
  10811.     end
  10812.     return true
  10813.   end
  10814.   Events.onStartBattle.trigger(nil,nil)
  10815.   battle.internalbattle = true
  10816.   pbPrepareBattle(battle)
  10817.   restorebgm = true
  10818.   decision = 0
  10819.   Audio.me_stop
  10820.   tr = [trainer]; tr.push($PokemonTemp.waitingTrainer[0]) if $PokemonTemp.waitingTrainer
  10821.   pbBattleAnimation(trainerbgm,(battle.doublebattle) ? 3 : 1,tr) {
  10822.     pbSceneStandby {
  10823.       decision = battle.pbStartBattle(canlose)
  10824.     }
  10825.     pbAfterBattle(decision,canlose)
  10826.     if decision==1
  10827.       if $PokemonTemp.waitingTrainer
  10828.         pbMapInterpreter.pbSetSelfSwitch($PokemonTemp.waitingTrainer[1],"A",true)
  10829.       end
  10830.     end
  10831.   }
  10832.   Input.update
  10833.   pbSet(variable,decision)
  10834.   $PokemonTemp.waitingTrainer = nil
  10835.   return (decision==1)
  10836. end
  10837. #===============================================================================
  10838. # Start a trainer battle against two trainers
  10839. #===============================================================================
  10840. def pbDoubleTrainerBattle(trainerid1, trainername1, trainerparty1, endspeech1,
  10841.                           trainerid2, trainername2, trainerparty2, endspeech2,
  10842.                           canlose=false,variable=nil)
  10843.   trainer1 = pbLoadTrainer(trainerid1,trainername1,trainerparty1)
  10844.   Events.onTrainerPartyLoad.trigger(nil,trainer1)
  10845.   if !trainer1
  10846.     pbMissingTrainer(trainerid1,trainername1,trainerparty1)
  10847.   end
  10848.   trainer2 = pbLoadTrainer(trainerid2,trainername2,trainerparty2)
  10849.   Events.onTrainerPartyLoad.trigger(nil,trainer2)
  10850.   if !trainer2
  10851.     pbMissingTrainer(trainerid2,trainername2,trainerparty2)
  10852.   end
  10853.   if !trainer1 || !trainer2
  10854.     return false
  10855.   end
  10856.   if $PokemonGlobal.partner
  10857.     othertrainer = PokeBattle_Trainer.new($PokemonGlobal.partner[1],$PokemonGlobal.partner[0])
  10858.     othertrainer.id    = $PokemonGlobal.partner[2]
  10859.     othertrainer.party = $PokemonGlobal.partner[3]
  10860.     playerparty = []
  10861.     for i in 0...$Trainer.party.length
  10862.       playerparty[i] = $Trainer.party[i]
  10863.     end
  10864.     for i in 0...othertrainer.party.length
  10865.       playerparty[40+i] = othertrainer.party[i]
  10866.     end
  10867.     playertrainer = [$Trainer,othertrainer]
  10868.     fullparty1    = true
  10869.   else
  10870.     playerparty   = $Trainer.party
  10871.     playertrainer = $Trainer
  10872.     fullparty1    = false
  10873.   end
  10874.   combinedParty = []
  10875.   for i in 0...trainer1[2].length
  10876.     combinedParty[i] = trainer1[2][i]
  10877.   end
  10878.   for i in 0...trainer2[2].length
  10879.     combinedParty[40+i] = trainer2[2][i]
  10880.   end
  10881.   scene = pbNewBattleScene
  10882.   battle = PokeBattle_Battle.new(scene,playerparty,combinedParty,playertrainer,
  10883.                                  [trainer1[0],trainer2[0]])
  10884.   battle.fullparty1   = fullparty1
  10885.   battle.fullparty2   = true
  10886.   battle.doublebattle = battle.pbDoubleBattleAllowed?()
  10887.   battle.endspeech    = endspeech1
  10888.   battle.endspeech2   = endspeech2
  10889.   battle.items        = [trainer1[1],trainer2[1]]
  10890.   trainerbgm = pbGetTrainerBattleBGM([trainer1[0],trainer2[0]])
  10891.   if Input.press?(Input::CTRL) && $DEBUG
  10892.     Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
  10893.     Kernel.pbMessage(_INTL("AFTER LOSING..."))
  10894.     Kernel.pbMessage(battle.endspeech)
  10895.     Kernel.pbMessage(battle.endspeech2) if battle.endspeech2 && battle.endspeech2!=""
  10896.     return true
  10897.   end
  10898.   Events.onStartBattle.trigger(nil,nil)
  10899.   battle.internalbattle = true
  10900.   pbPrepareBattle(battle)
  10901.   restorebgm = true
  10902.   decision = 0
  10903.   pbBattleAnimation(trainerbgm,(battle.doublebattle) ? 3 : 1,[trainer1,trainer2]) {
  10904.     pbSceneStandby {
  10905.        decision = battle.pbStartBattle(canlose)
  10906.     }
  10907.     pbAfterBattle(decision,canlose)
  10908.   }
  10909.   Input.update
  10910.   pbSet(variable,decision)
  10911.   return (decision==1)
  10912. end
  10913. ################################################################################
  10914. # Item Reworking                                                               #
  10915. ################################################################################
  10916. ItemHandlers::UseOnPokemon.add(:DNASPLICERS,proc{|item,pokemon,scene|
  10917.    if isConst?(pokemon.species,PBSpecies,:KYUREM)
  10918.      if pokemon.hp>0
  10919.        if pokemon.fused!=nil
  10920.        if $Trainer.partyplus>=1
  10921.          if $Trainer.party.length>=$Trainer.partyplus
  10922.            scene.pbDisplay(_INTL("You have no room to separate the Pokémon."))
  10923.            next false
  10924.          else
  10925.            $Trainer.party[$Trainer.party.length]=pokemon.fused
  10926.            pokemon.fused=nil
  10927.            pokemon.form=0
  10928.            scene.pbHardRefresh
  10929.            scene.pbDisplay(_INTL("{1} changed Forme!",pokemon.name))
  10930.            next true
  10931.          end
  10932.        else
  10933.          if $Trainer.party.length>=6
  10934.            scene.pbDisplay(_INTL("You have no room to separate the Pokémon."))
  10935.            next false
  10936.          else
  10937.            $Trainer.party[$Trainer.party.length]=pokemon.fused
  10938.            pokemon.fused=nil
  10939.            pokemon.form=0
  10940.            scene.pbHardRefresh
  10941.            scene.pbDisplay(_INTL("{1} changed Forme!",pokemon.name))
  10942.            next true
  10943.          end
  10944.        end
  10945.        else
  10946.          chosen=scene.pbChoosePokemon(_INTL("Fuse with which Pokémon?"))
  10947.          if chosen>=0
  10948.            poke2=$Trainer.party[chosen]
  10949.            if (isConst?(poke2.species,PBSpecies,:RESHIRAM) ||
  10950.               isConst?(poke2.species,PBSpecies,:ZEKROM)) && poke2.hp>0 && !poke2.egg?
  10951.              pokemon.form=1 if isConst?(poke2.species,PBSpecies,:RESHIRAM)
  10952.              pokemon.form=2 if isConst?(poke2.species,PBSpecies,:ZEKROM)
  10953.              pokemon.fused=poke2
  10954.              pbRemovePokemonAt(chosen)
  10955.              scene.pbHardRefresh
  10956.              scene.pbDisplay(_INTL("{1} changed Forme!",pokemon.name))
  10957.              next true
  10958.            elsif poke2.egg?
  10959.              scene.pbDisplay(_INTL("It cannot be fused with an Egg."))
  10960.            elsif poke2.hp<=0
  10961.              scene.pbDisplay(_INTL("It cannot be fused with that fainted Pokémon."))
  10962.            elsif pokemon==poke2
  10963.              scene.pbDisplay(_INTL("It cannot be fused with itself."))
  10964.            else
  10965.              scene.pbDisplay(_INTL("It cannot be fused with that Pokémon."))
  10966.            end
  10967.          else
  10968.            next false
  10969.          end
  10970.        end
  10971.      else
  10972.        scene.pbDisplay(_INTL("This can't be used on the fainted Pokémon."))
  10973.      end
  10974.    else
  10975.      scene.pbDisplay(_INTL("It had no effect."))
  10976.      next false
  10977.    end
  10978. })
  10979. ItemHandlers::BattleUseOnBattler.addIf(proc{|item|
  10980.                 pbIsPokeBall?(item)},proc{|item,battler,scene|  # Any Poké Ball
  10981.    battle=battler.battle
  10982.    if !battler.pbOpposing1.fainted? && !battler.pbOpposing2.fainted?
  10983.      if !pbIsSnagBall?(item)
  10984.        scene.pbDisplay(_INTL("It's no good! It's impossible to aim when there are two Pokémon!"))
  10985.        return false
  10986.      end
  10987.    end
  10988.    if $Trainer.partyplus>=1
  10989.    if battle.pbPlayer.party.length>=$Trainer.partyplus && $PokemonStorage.full?
  10990.      scene.pbDisplay(_INTL("There is no room left in the PC!"))
  10991.      return false
  10992.    end
  10993.    else
  10994.    if battle.pbPlayer.party.length>=6 && $PokemonStorage.full?
  10995.      scene.pbDisplay(_INTL("There is no room left in the PC!"))
  10996.      return false
  10997.    end
  10998.    end
  10999.    return true
  11000. })
  11001. #===============================================================================
  11002. # Battle scene (the visuals of the battle)
  11003. #===============================================================================
  11004. class PokeBattle_Scene
  11005.   attr_accessor :abortable
  11006.   attr_reader :viewport
  11007.   attr_reader :sprites
  11008.   BLANK      = 0
  11009.   MESSAGEBOX = 1
  11010.   COMMANDBOX = 2
  11011.   FIGHTBOX   = 3
  11012.   def initialize
  11013.     @battle=nil
  11014.     @lastcmd=[0,0,0,0]
  11015.     @lastmove=[0,0,0,0]
  11016.     @pkmnwindows=[nil,nil,nil,nil]
  11017.     @sprites={}
  11018.     @battlestart=true
  11019.     @messagemode=false
  11020.     @abortable=false
  11021.     @aborted=false
  11022.   end
  11023.   def pbUpdate
  11024.     partyAnimationUpdate
  11025.     @sprites["battlebg"].update if @sprites["battlebg"].respond_to?("update")
  11026.   end
  11027.   def pbGraphicsUpdate
  11028.     partyAnimationUpdate
  11029.     @sprites["battlebg"].update if @sprites["battlebg"].respond_to?("update")
  11030.     Graphics.update
  11031.   end
  11032.   def pbInputUpdate
  11033.     Input.update
  11034.     if Input.trigger?(Input::B) && @abortable && !@aborted
  11035.       @aborted=true
  11036.       @battle.pbAbort
  11037.     end
  11038.   end
  11039.   def pbShowWindow(windowtype)
  11040.     @sprites["messagebox"].visible = (windowtype==MESSAGEBOX ||
  11041.                                       windowtype==COMMANDBOX ||
  11042.                                       windowtype==FIGHTBOX ||
  11043.                                       windowtype==BLANK )
  11044.     @sprites["messagewindow"].visible = (windowtype==MESSAGEBOX)
  11045.     @sprites["commandwindow"].visible = (windowtype==COMMANDBOX)
  11046.     @sprites["fightwindow"].visible = (windowtype==FIGHTBOX)
  11047.   end
  11048.   def pbSetMessageMode(mode)
  11049.     @messagemode=mode
  11050.     msgwindow=@sprites["messagewindow"]
  11051.     if mode # Within Pokémon command
  11052.       msgwindow.baseColor=PokeBattle_SceneConstants::MENUBASECOLOR
  11053.       msgwindow.shadowColor=PokeBattle_SceneConstants::MENUSHADOWCOLOR
  11054.       msgwindow.opacity=255
  11055.       msgwindow.x=16
  11056.       msgwindow.width=Graphics.width
  11057.       msgwindow.height=96
  11058.       msgwindow.y=Graphics.height-msgwindow.height+2
  11059.     else
  11060.       msgwindow.baseColor=PokeBattle_SceneConstants::MESSAGEBASECOLOR
  11061.       msgwindow.shadowColor=PokeBattle_SceneConstants::MESSAGESHADOWCOLOR
  11062.       msgwindow.opacity=0
  11063.       msgwindow.x=16
  11064.       msgwindow.width=Graphics.width-32
  11065.       msgwindow.height=96
  11066.       msgwindow.y=Graphics.height-msgwindow.height+2
  11067.     end
  11068.   end
  11069.   def pbWaitMessage
  11070.     if @briefmessage
  11071.       pbShowWindow(MESSAGEBOX)
  11072.       cw=@sprites["messagewindow"]
  11073.       40.times do
  11074.         pbGraphicsUpdate
  11075.         pbInputUpdate
  11076.         pbFrameUpdate(cw)
  11077.       end
  11078.       cw.text=""
  11079.       cw.visible=false
  11080.       @briefmessage=false
  11081.     end
  11082.   end
  11083.   def pbDisplay(msg,brief=false)
  11084.     pbDisplayMessage(msg,brief)
  11085.   end
  11086.   def pbDisplayMessage(msg,brief=false)
  11087.     pbWaitMessage
  11088.     pbRefresh
  11089.     pbShowWindow(MESSAGEBOX)
  11090.     cw=@sprites["messagewindow"]
  11091.     cw.text=msg
  11092.     i=0
  11093.     loop do
  11094.       pbGraphicsUpdate
  11095.       pbInputUpdate
  11096.       pbFrameUpdate(cw)
  11097.       if i==40
  11098.         cw.text=""
  11099.         cw.visible=false
  11100.         return
  11101.       end
  11102.       if Input.trigger?(Input::C) || @abortable
  11103.         if cw.pausing?
  11104.           pbPlayDecisionSE() if !@abortable
  11105.           cw.resume
  11106.         end
  11107.       end
  11108.       if !cw.busy?
  11109.         if brief
  11110.           @briefmessage=true
  11111.           return
  11112.         end
  11113.         i+=1
  11114.       end
  11115.     end
  11116.   end
  11117.   def pbDisplayPausedMessage(msg)
  11118.     pbWaitMessage
  11119.     pbRefresh
  11120.     pbShowWindow(MESSAGEBOX)
  11121.     if @messagemode
  11122.       @switchscreen.pbDisplay(msg)
  11123.       return
  11124.     end
  11125.     cw=@sprites["messagewindow"]
  11126.     cw.text=_INTL("{1}\1",msg)
  11127.     loop do
  11128.       pbGraphicsUpdate
  11129.       pbInputUpdate
  11130.       pbFrameUpdate(cw)
  11131.       if Input.trigger?(Input::C) || @abortable
  11132.         if cw.busy?
  11133.           pbPlayDecisionSE() if cw.pausing? && !@abortable
  11134.           cw.resume
  11135.         elsif !inPartyAnimation?
  11136.           cw.text=""
  11137.           pbPlayDecisionSE()
  11138.           cw.visible=false if @messagemode
  11139.           return
  11140.         end
  11141.       end
  11142.       cw.update
  11143.     end
  11144.   end
  11145.   def pbDisplayConfirmMessage(msg)
  11146.     return pbShowCommands(msg,[_INTL("Yes"),_INTL("No")],1)==0
  11147.   end
  11148.   def pbShowCommands(msg,commands,defaultValue)
  11149.     pbWaitMessage
  11150.     pbRefresh
  11151.     pbShowWindow(MESSAGEBOX)
  11152.     dw=@sprites["messagewindow"]
  11153.     dw.text=msg
  11154.     cw = Window_CommandPokemon.new(commands)
  11155.     cw.x=Graphics.width-cw.width
  11156.     cw.y=Graphics.height-cw.height-dw.height
  11157.     cw.index=0
  11158.     cw.viewport=@viewport
  11159.     pbRefresh
  11160.     loop do
  11161.       cw.visible=!dw.busy?
  11162.       pbGraphicsUpdate
  11163.       pbInputUpdate
  11164.       pbFrameUpdate(cw)
  11165.       dw.update
  11166.       if Input.trigger?(Input::B) && defaultValue>=0
  11167.         if dw.busy?
  11168.           pbPlayDecisionSE() if dw.pausing?
  11169.           dw.resume
  11170.         else
  11171.           cw.dispose
  11172.           dw.text=""
  11173.           return defaultValue
  11174.         end
  11175.       end
  11176.       if Input.trigger?(Input::C)
  11177.         if dw.busy?
  11178.           pbPlayDecisionSE() if dw.pausing?
  11179.           dw.resume
  11180.         else
  11181.           cw.dispose
  11182.           dw.text=""
  11183.           return cw.index
  11184.         end
  11185.       end
  11186.     end
  11187.   end
  11188.   def pbFrameUpdate(cw=nil)
  11189.     cw.update if cw
  11190.     for i in 0...4
  11191.       if @sprites["battlebox#{i}"]
  11192.         @sprites["battlebox#{i}"].update
  11193.       end
  11194.       if @sprites["pokemon#{i}"]
  11195.         @sprites["pokemon#{i}"].update
  11196.       end
  11197.     end
  11198.   end
  11199.   def pbRefresh
  11200.     for i in 0...4
  11201.       if @sprites["battlebox#{i}"]
  11202.         @sprites["battlebox#{i}"].refresh
  11203.       end
  11204.     end
  11205.   end
  11206.   def pbAddSprite(id,x,y,filename,viewport)
  11207.     sprite=IconSprite.new(x,y,viewport)
  11208.     if filename
  11209.       sprite.setBitmap(filename) rescue nil
  11210.     end
  11211.     @sprites[id]=sprite
  11212.     return sprite
  11213.   end
  11214.   def pbAddPlane(id,filename,viewport)
  11215.     sprite=AnimatedPlane.new(viewport)
  11216.     if filename
  11217.       sprite.setBitmap(filename)
  11218.     end
  11219.     @sprites[id]=sprite
  11220.     return sprite
  11221.   end
  11222.   def pbDisposeSprites
  11223.     pbDisposeSpriteHash(@sprites)
  11224.   end
  11225.   def pbBeginCommandPhase
  11226.     # Called whenever a new round begins.
  11227.     @battlestart=false
  11228.   end
  11229.   def pbShowOpponent(index)
  11230.     if @battle.opponent
  11231.       if @battle.opponent.is_a?(Array)
  11232.         trainerfile=pbTrainerSpriteFile(@battle.opponent[index].trainertype)
  11233.       else
  11234.         trainerfile=pbTrainerSpriteFile(@battle.opponent.trainertype)
  11235.       end
  11236.     else
  11237.       trainerfile="Graphics/Characters/trfront"
  11238.     end
  11239.     pbAddSprite("trainer",Graphics.width,PokeBattle_SceneConstants::FOETRAINER_Y,
  11240.        trainerfile,@viewport)
  11241.     if @sprites["trainer"].bitmap
  11242.       @sprites["trainer"].y-=@sprites["trainer"].bitmap.height
  11243.       @sprites["trainer"].z=8
  11244.     end
  11245.     20.times do
  11246.       pbGraphicsUpdate
  11247.       pbInputUpdate
  11248.       pbFrameUpdate
  11249.       @sprites["trainer"].x-=6
  11250.     end
  11251.   end
  11252.   def pbHideOpponent
  11253.     20.times do
  11254.       pbGraphicsUpdate
  11255.       pbInputUpdate
  11256.       pbFrameUpdate
  11257.       @sprites["trainer"].x+=6
  11258.     end
  11259.   end
  11260.   def pbShowHelp(text)
  11261.     @sprites["helpwindow"].resizeToFit(text,Graphics.width)
  11262.     @sprites["helpwindow"].y=0
  11263.     @sprites["helpwindow"].x=0
  11264.     @sprites["helpwindow"].text=text
  11265.     @sprites["helpwindow"].visible=true
  11266.   end
  11267.   def pbHideHelp
  11268.     @sprites["helpwindow"].visible=false
  11269.   end
  11270.   def pbBackdrop
  11271.     environ=@battle.environment
  11272.     # Choose backdrop
  11273.     backdrop="Field"
  11274.     if environ==PBEnvironment::Cave
  11275.       backdrop="Cave"
  11276.     elsif environ==PBEnvironment::MovingWater || environ==PBEnvironment::StillWater
  11277.       backdrop="Water"
  11278.     elsif environ==PBEnvironment::Underwater
  11279.       backdrop="Underwater"
  11280.     elsif environ==PBEnvironment::Rock
  11281.       backdrop="Mountain"
  11282.     else
  11283.       if !$game_map || !pbGetMetadata($game_map.map_id,MetadataOutdoor)
  11284.         backdrop="IndoorA"
  11285.       end
  11286.     end
  11287.     if $game_map
  11288.       back=pbGetMetadata($game_map.map_id,MetadataBattleBack)
  11289.       if back && back!=""
  11290.         backdrop=back
  11291.       end
  11292.     end
  11293.     if $PokemonGlobal && $PokemonGlobal.nextBattleBack
  11294.       backdrop=$PokemonGlobal.nextBattleBack
  11295.     end
  11296.     # Choose bases
  11297.     base=""
  11298.     trialname=""
  11299.     if environ==PBEnvironment::Grass || environ==PBEnvironment::TallGrass
  11300.       trialname="Grass"
  11301.     elsif environ==PBEnvironment::Sand
  11302.       trialname="Sand"
  11303.     elsif $PokemonGlobal.surfing
  11304.       trialname="Water"
  11305.     end
  11306.     if pbResolveBitmap(sprintf("Graphics/Battlebacks/playerbase"+backdrop+trialname))
  11307.       base=trialname
  11308.     end
  11309.     # Choose time of day
  11310.     time=""
  11311.     if ENABLESHADING
  11312.       trialname=""
  11313.       timenow=pbGetTimeNow
  11314.       if PBDayNight.isNight?(timenow)
  11315.         trialname="Night"
  11316.       elsif PBDayNight.isEvening?(timenow)
  11317.         trialname="Eve"
  11318.       end
  11319.       if pbResolveBitmap(sprintf("Graphics/Battlebacks/battlebg"+backdrop+trialname))
  11320.         time=trialname
  11321.       end
  11322.     end
  11323.     # Apply graphics
  11324.     battlebg="Graphics/Battlebacks/battlebg"+backdrop+time
  11325.     enemybase="Graphics/Battlebacks/enemybase"+backdrop+base+time
  11326.     playerbase="Graphics/Battlebacks/playerbase"+backdrop+base+time
  11327.     pbAddPlane("battlebg",battlebg,@viewport)
  11328.     pbAddSprite("playerbase",
  11329.        PokeBattle_SceneConstants::PLAYERBASEX,
  11330.        PokeBattle_SceneConstants::PLAYERBASEY,playerbase,@viewport)
  11331.     @sprites["playerbase"].x-=@sprites["playerbase"].bitmap.width/2 if @sprites["playerbase"].bitmap!=nil
  11332.     @sprites["playerbase"].y-=@sprites["playerbase"].bitmap.height if @sprites["playerbase"].bitmap!=nil
  11333.     pbAddSprite("enemybase",
  11334.        PokeBattle_SceneConstants::FOEBASEX,
  11335.        PokeBattle_SceneConstants::FOEBASEY,enemybase,@viewport)
  11336.     @sprites["enemybase"].x-=@sprites["enemybase"].bitmap.width/2 if @sprites["enemybase"].bitmap!=nil
  11337.     @sprites["enemybase"].y-=@sprites["enemybase"].bitmap.height/2 if @sprites["enemybase"].bitmap!=nil
  11338.     @sprites["battlebg"].z=0
  11339.     @sprites["playerbase"].z=1
  11340.     @sprites["enemybase"].z=1
  11341.   end
  11342.   # Returns whether the party line-ups are currently appearing on-screen
  11343.   def inPartyAnimation?
  11344.     return @enablePartyAnim && @partyAnimPhase<3
  11345.   end
  11346.   # Shows the party line-ups appearing on-screen
  11347.   def partyAnimationUpdate
  11348.     return if !inPartyAnimation?
  11349.     ballmovedist=16 # How far a ball moves each frame
  11350.     # Bar slides on
  11351.     if @partyAnimPhase==0
  11352.       @sprites["partybarfoe"].x+=16
  11353.       @sprites["partybarplayer"].x-=16
  11354.       if @sprites["partybarfoe"].x+@sprites["partybarfoe"].bitmap.width>=PokeBattle_SceneConstants::FOEPARTYBAR_X
  11355.         @sprites["partybarfoe"].x=PokeBattle_SceneConstants::FOEPARTYBAR_X-@sprites["partybarfoe"].bitmap.width
  11356.         @sprites["partybarplayer"].x=PokeBattle_SceneConstants::PLAYERPARTYBAR_X
  11357.         @partyAnimPhase=1
  11358.       end
  11359.       return
  11360.     end
  11361.     # Set up all balls ready to slide on
  11362.     if @partyAnimPhase==1
  11363.       @xposplayer=PokeBattle_SceneConstants::PLAYERPARTYBALL1_X
  11364.       counter=0
  11365.       # Make sure the ball starts off-screen
  11366.       while @xposplayer<Graphics.width
  11367.         counter+=1; @xposplayer+=ballmovedist
  11368.       end
  11369.       @xposenemy=PokeBattle_SceneConstants::FOEPARTYBALL1_X-counter*ballmovedist
  11370.       for i in 0...6
  11371.         # Choose the ball's graphic (player's side)
  11372.         ballgraphic="Graphics/Pictures/Battle/icon_ball_empty"
  11373.         if i<@battle.party1.length && @battle.party1[i]
  11374.           if @battle.party1[i].hp<=0 || @battle.party1[i].egg?
  11375.             ballgraphic="Graphics/Pictures/Battle/icon_ball_faint"
  11376.           elsif @battle.party1[i].status>0
  11377.             ballgraphic="Graphics/Pictures/Battle/icon_ball_status"
  11378.           else
  11379.             ballgraphic="Graphics/Pictures/Battle/icon_ball"
  11380.           end
  11381.         end
  11382.         pbAddSprite("player#{i}",
  11383.            @xposplayer+i*ballmovedist*6,PokeBattle_SceneConstants::PLAYERPARTYBALL1_Y,
  11384.            ballgraphic,@viewport)
  11385.         @sprites["player#{i}"].z=41
  11386.         # Choose the ball's graphic (opponent's side)
  11387.         ballgraphic="Graphics/Pictures/Battle/icon_ball_empty"
  11388.         enemyindex=i
  11389.         if @battle.doublebattle && i>=3
  11390.           enemyindex=(i%3)+@battle.pbSecondPartyBegin(1)
  11391.         end
  11392.         if enemyindex<@battle.party2.length && @battle.party2[enemyindex]
  11393.           if @battle.party2[enemyindex].hp<=0 || @battle.party2[enemyindex].egg?
  11394.             ballgraphic="Graphics/Pictures/Battle/icon_ball_faint"
  11395.           elsif @battle.party2[enemyindex].status>0
  11396.             ballgraphic="Graphics/Pictures/Battle/icon_ball_status"
  11397.           else
  11398.             ballgraphic="Graphics/Pictures/Battle/icon_ball"
  11399.           end
  11400.         end
  11401.         pbAddSprite("enemy#{i}",
  11402.            @xposenemy-i*ballmovedist*6,PokeBattle_SceneConstants::FOEPARTYBALL1_Y,
  11403.            ballgraphic,@viewport)
  11404.         @sprites["enemy#{i}"].z=41
  11405.       end
  11406.       @partyAnimPhase=2
  11407.     end
  11408.     # Balls slide on
  11409.     if @partyAnimPhase==2
  11410.       for i in 0...6
  11411.         if @sprites["enemy#{i}"].x<PokeBattle_SceneConstants::FOEPARTYBALL1_X-i*PokeBattle_SceneConstants::FOEPARTYBALL_GAP
  11412.           @sprites["enemy#{i}"].x+=ballmovedist
  11413.           @sprites["player#{i}"].x-=ballmovedist
  11414.           if @sprites["enemy#{i}"].x>=PokeBattle_SceneConstants::FOEPARTYBALL1_X-i*PokeBattle_SceneConstants::FOEPARTYBALL_GAP
  11415.             @sprites["enemy#{i}"].x=PokeBattle_SceneConstants::FOEPARTYBALL1_X-i*PokeBattle_SceneConstants::FOEPARTYBALL_GAP
  11416.             @sprites["player#{i}"].x=PokeBattle_SceneConstants::PLAYERPARTYBALL1_X+i*PokeBattle_SceneConstants::PLAYERPARTYBALL_GAP
  11417.             if i==5
  11418.               @partyAnimPhase=3
  11419.             end
  11420.           end
  11421.         end
  11422.       end
  11423.     end
  11424.   end
  11425.   def pbStartBattle(battle)
  11426.     # Called whenever the battle begins
  11427.     @battle=battle
  11428.     @lastcmd=[0,0,0,0]
  11429.     @lastmove=[0,0,0,0]
  11430.     @showingplayer=true
  11431.     @showingenemy=true
  11432.     @sprites.clear
  11433.     @viewport=Viewport.new(0,Graphics.height/2,Graphics.width,0)
  11434.     @viewport.z=99999
  11435.     @traineryoffset=(Graphics.height-320) # Adjust player's side for screen size
  11436.     @foeyoffset=(@traineryoffset*3/4).floor  # Adjust foe's side for screen size
  11437.     pbBackdrop
  11438.     pbAddSprite("partybarfoe",
  11439.        PokeBattle_SceneConstants::FOEPARTYBAR_X,
  11440.        PokeBattle_SceneConstants::FOEPARTYBAR_Y,
  11441.        "Graphics/Pictures/Battle/overlay_lineup",@viewport)
  11442.     pbAddSprite("partybarplayer",
  11443.        PokeBattle_SceneConstants::PLAYERPARTYBAR_X,
  11444.        PokeBattle_SceneConstants::PLAYERPARTYBAR_Y,
  11445.        "Graphics/Pictures/Battle/overlay_lineup",@viewport)
  11446.     @sprites["partybarfoe"].x-=@sprites["partybarfoe"].bitmap.width
  11447.     @sprites["partybarplayer"].mirror=true
  11448.     @sprites["partybarfoe"].z=40
  11449.     @sprites["partybarplayer"].z=40
  11450.     @sprites["partybarfoe"].visible=false
  11451.     @sprites["partybarplayer"].visible=false
  11452.     if @battle.player.is_a?(Array)
  11453.       trainerfile=pbPlayerSpriteBackFile(@battle.player[0].trainertype)
  11454.       pbAddSprite("player",
  11455.            PokeBattle_SceneConstants::PLAYERTRAINERD1_X,
  11456.            PokeBattle_SceneConstants::PLAYERTRAINERD1_Y,trainerfile,@viewport)
  11457.       trainerfile=pbTrainerSpriteBackFile(@battle.player[1].trainertype)
  11458.       pbAddSprite("playerB",
  11459.            PokeBattle_SceneConstants::PLAYERTRAINERD2_X,
  11460.            PokeBattle_SceneConstants::PLAYERTRAINERD2_Y,trainerfile,@viewport)
  11461.       if @sprites["player"].bitmap
  11462.         if @sprites["player"].bitmap.width>@sprites["player"].bitmap.height
  11463.           @sprites["player"].src_rect.x=0
  11464.           @sprites["player"].src_rect.width=@sprites["player"].bitmap.width/5
  11465.         end
  11466.         @sprites["player"].x-=(@sprites["player"].src_rect.width/2)
  11467.         @sprites["player"].y-=@sprites["player"].bitmap.height
  11468.         @sprites["player"].z=30
  11469.       end
  11470.       if @sprites["playerB"].bitmap
  11471.         if @sprites["playerB"].bitmap.width>@sprites["playerB"].bitmap.height
  11472.           @sprites["playerB"].src_rect.x=0
  11473.           @sprites["playerB"].src_rect.width=@sprites["playerB"].bitmap.width/5
  11474.         end
  11475.         @sprites["playerB"].x-=(@sprites["playerB"].src_rect.width/2)
  11476.         @sprites["playerB"].y-=@sprites["playerB"].bitmap.height
  11477.         @sprites["playerB"].z=31
  11478.       end
  11479.     else
  11480.       trainerfile=pbPlayerSpriteBackFile(@battle.player.trainertype)
  11481.       pbAddSprite("player",
  11482.            PokeBattle_SceneConstants::PLAYERTRAINER_X,
  11483.            PokeBattle_SceneConstants::PLAYERTRAINER_Y,trainerfile,@viewport)
  11484.       if @sprites["player"].bitmap
  11485.         if @sprites["player"].bitmap.width>@sprites["player"].bitmap.height
  11486.           @sprites["player"].src_rect.x=0
  11487.           @sprites["player"].src_rect.width=@sprites["player"].bitmap.width/5
  11488.         end
  11489.         @sprites["player"].x-=(@sprites["player"].src_rect.width/2)
  11490.         @sprites["player"].y-=@sprites["player"].bitmap.height
  11491.         @sprites["player"].z=30
  11492.       end
  11493.     end
  11494.     if @battle.opponent
  11495.       if @battle.opponent.is_a?(Array)
  11496.         trainerfile=pbTrainerSpriteFile(@battle.opponent[1].trainertype)
  11497.         pbAddSprite("trainer2",
  11498.            PokeBattle_SceneConstants::FOETRAINERD2_X,
  11499.            PokeBattle_SceneConstants::FOETRAINERD2_Y,trainerfile,@viewport)
  11500.         trainerfile=pbTrainerSpriteFile(@battle.opponent[0].trainertype)
  11501.         pbAddSprite("trainer",
  11502.            PokeBattle_SceneConstants::FOETRAINERD1_X,
  11503.            PokeBattle_SceneConstants::FOETRAINERD1_Y,trainerfile,@viewport)
  11504.       else
  11505.         trainerfile=pbTrainerSpriteFile(@battle.opponent.trainertype)
  11506.         pbAddSprite("trainer",
  11507.            PokeBattle_SceneConstants::FOETRAINER_X,
  11508.            PokeBattle_SceneConstants::FOETRAINER_Y,trainerfile,@viewport)
  11509.       end
  11510.     else
  11511.       trainerfile="Graphics/Characters/trfront"
  11512.       pbAddSprite("trainer",
  11513.            PokeBattle_SceneConstants::FOETRAINER_X,
  11514.            PokeBattle_SceneConstants::FOETRAINER_Y,trainerfile,@viewport)
  11515.     end
  11516.     if @sprites["trainer"].bitmap
  11517.       @sprites["trainer"].x-=(@sprites["trainer"].bitmap.width/2)
  11518.       @sprites["trainer"].y-=@sprites["trainer"].bitmap.height
  11519.       @sprites["trainer"].z=8
  11520.     end
  11521.     if @sprites["trainer2"] && @sprites["trainer2"].bitmap
  11522.       @sprites["trainer2"].x-=(@sprites["trainer2"].bitmap.width/2)
  11523.       @sprites["trainer2"].y-=@sprites["trainer2"].bitmap.height
  11524.       @sprites["trainer2"].z=7
  11525.     end
  11526.     @sprites["shadow0"]=IconSprite.new(0,0,@viewport)
  11527.     @sprites["shadow0"].z=3
  11528.     pbAddSprite("shadow1",0,0,"Graphics/Pictures/Battle/object_shadow",@viewport)
  11529.     @sprites["shadow1"].z=3
  11530.     @sprites["shadow1"].visible=false
  11531.     @sprites["pokemon0"]=PokemonBattlerSprite.new(battle.doublebattle,0,@viewport)
  11532.     @sprites["pokemon0"].z=21
  11533.     @sprites["pokemon1"]=PokemonBattlerSprite.new(battle.doublebattle,1,@viewport)
  11534.     @sprites["pokemon1"].z=16
  11535.     if battle.doublebattle
  11536.       @sprites["shadow2"]=IconSprite.new(0,0,@viewport)
  11537.       @sprites["shadow2"].z=3
  11538.       pbAddSprite("shadow3",0,0,"Graphics/Pictures/Battle/object_shadow",@viewport)
  11539.       @sprites["shadow3"].z=3
  11540.       @sprites["shadow3"].visible=false
  11541.       @sprites["pokemon2"]=PokemonBattlerSprite.new(battle.doublebattle,2,@viewport)
  11542.       @sprites["pokemon2"].z=26
  11543.       @sprites["pokemon3"]=PokemonBattlerSprite.new(battle.doublebattle,3,@viewport)
  11544.       @sprites["pokemon3"].z=11
  11545.     end
  11546.     @sprites["battlebox0"]=PokemonDataBox.new(battle.battlers[0],battle.doublebattle,@viewport)
  11547.     @sprites["battlebox1"]=PokemonDataBox.new(battle.battlers[1],battle.doublebattle,@viewport)
  11548.     if battle.doublebattle
  11549.       @sprites["battlebox2"]=PokemonDataBox.new(battle.battlers[2],battle.doublebattle,@viewport)
  11550.       @sprites["battlebox3"]=PokemonDataBox.new(battle.battlers[3],battle.doublebattle,@viewport)
  11551.     end
  11552.     pbAddSprite("messagebox",0,Graphics.height-96,"Graphics/Pictures/Battle/overlay_message",@viewport)
  11553.     @sprites["messagebox"].z=90
  11554.     @sprites["helpwindow"]=Window_UnformattedTextPokemon.newWithSize("",0,0,32,32,@viewport)
  11555.     @sprites["helpwindow"].visible=false
  11556.     @sprites["helpwindow"].z=90
  11557.     @sprites["messagewindow"]=Window_AdvancedTextPokemon.new("")
  11558.     @sprites["messagewindow"].letterbyletter=true
  11559.     @sprites["messagewindow"].viewport=@viewport
  11560.     @sprites["messagewindow"].z=100
  11561.     @sprites["commandwindow"]=CommandMenuDisplay.new(@viewport)
  11562.     @sprites["commandwindow"].z=100
  11563.     @sprites["fightwindow"]=FightMenuDisplay.new(nil,@viewport)
  11564.     @sprites["fightwindow"].z=100
  11565.     pbShowWindow(MESSAGEBOX)
  11566.     pbSetMessageMode(false)
  11567.     trainersprite1=@sprites["trainer"]
  11568.     trainersprite2=@sprites["trainer2"]
  11569.     if !@battle.opponent
  11570.       @sprites["trainer"].visible=false
  11571.       if @battle.party2.length>=1
  11572.         if @battle.party2.length==1
  11573.           species=@battle.party2[0].species
  11574.           @sprites["pokemon1"].setPokemonBitmap(@battle.party2[0],false)
  11575.           @sprites["pokemon1"].tone=Tone.new(-128,-128,-128,-128)
  11576.           @sprites["pokemon1"].x=PokeBattle_SceneConstants::FOEBATTLER_X
  11577.           @sprites["pokemon1"].x-=@sprites["pokemon1"].width/2
  11578.           @sprites["pokemon1"].y=PokeBattle_SceneConstants::FOEBATTLER_Y
  11579.           @sprites["pokemon1"].y+=adjustBattleSpriteY(@sprites["pokemon1"],species,1)
  11580.           @sprites["pokemon1"].visible=true
  11581.           @sprites["shadow1"].x=PokeBattle_SceneConstants::FOEBATTLER_X
  11582.           @sprites["shadow1"].y=PokeBattle_SceneConstants::FOEBATTLER_Y
  11583.           @sprites["shadow1"].x-=@sprites["shadow1"].bitmap.width/2 if @sprites["shadow1"].bitmap!=nil
  11584.           @sprites["shadow1"].y-=@sprites["shadow1"].bitmap.height/2 if @sprites["shadow1"].bitmap!=nil
  11585.           @sprites["shadow1"].visible=showShadow?(species)
  11586.           trainersprite1=@sprites["pokemon1"]
  11587.         elsif @battle.party2.length==2
  11588.           species=@battle.party2[0].species
  11589.           @sprites["pokemon1"].setPokemonBitmap(@battle.party2[0],false)
  11590.           @sprites["pokemon1"].tone=Tone.new(-128,-128,-128,-128)
  11591.           @sprites["pokemon1"].x=PokeBattle_SceneConstants::FOEBATTLERD1_X
  11592.           @sprites["pokemon1"].x-=@sprites["pokemon1"].width/2
  11593.           @sprites["pokemon1"].y=PokeBattle_SceneConstants::FOEBATTLERD1_Y
  11594.           @sprites["pokemon1"].y+=adjustBattleSpriteY(@sprites["pokemon1"],species,1)
  11595.           @sprites["pokemon1"].visible=true
  11596.           @sprites["shadow1"].x=PokeBattle_SceneConstants::FOEBATTLERD1_X
  11597.           @sprites["shadow1"].y=PokeBattle_SceneConstants::FOEBATTLERD1_Y
  11598.           @sprites["shadow1"].x-=@sprites["shadow1"].bitmap.width/2 if @sprites["shadow1"].bitmap!=nil
  11599.           @sprites["shadow1"].y-=@sprites["shadow1"].bitmap.height/2 if @sprites["shadow1"].bitmap!=nil
  11600.           @sprites["shadow1"].visible=showShadow?(species)
  11601.           trainersprite1=@sprites["pokemon1"]
  11602.           species=@battle.party2[1].species
  11603.           @sprites["pokemon3"].setPokemonBitmap(@battle.party2[1],false)
  11604.           @sprites["pokemon3"].tone=Tone.new(-128,-128,-128,-128)
  11605.           @sprites["pokemon3"].x=PokeBattle_SceneConstants::FOEBATTLERD2_X
  11606.           @sprites["pokemon3"].x-=@sprites["pokemon3"].width/2
  11607.           @sprites["pokemon3"].y=PokeBattle_SceneConstants::FOEBATTLERD2_Y
  11608.           @sprites["pokemon3"].y+=adjustBattleSpriteY(@sprites["pokemon3"],species,3)
  11609.           @sprites["pokemon3"].visible=true
  11610.           @sprites["shadow3"].x=PokeBattle_SceneConstants::FOEBATTLERD2_X
  11611.           @sprites["shadow3"].y=PokeBattle_SceneConstants::FOEBATTLERD2_Y
  11612.           @sprites["shadow3"].x-=@sprites["shadow3"].bitmap.width/2 if @sprites["shadow3"].bitmap!=nil
  11613.           @sprites["shadow3"].y-=@sprites["shadow3"].bitmap.height/2 if @sprites["shadow3"].bitmap!=nil
  11614.           @sprites["shadow3"].visible=showShadow?(species)
  11615.           trainersprite2=@sprites["pokemon3"]
  11616.         end
  11617.       end
  11618.     end
  11619.     #################
  11620.     # Move trainers/bases/etc. off-screen
  11621.     oldx=[]
  11622.     oldx[0]=@sprites["playerbase"].x; @sprites["playerbase"].x+=Graphics.width
  11623.     oldx[1]=@sprites["player"].x; @sprites["player"].x+=Graphics.width
  11624.     if @sprites["playerB"]
  11625.       oldx[2]=@sprites["playerB"].x; @sprites["playerB"].x+=Graphics.width
  11626.     end
  11627.     oldx[3]=@sprites["enemybase"].x; @sprites["enemybase"].x-=Graphics.width
  11628.     oldx[4]=trainersprite1.x; trainersprite1.x-=Graphics.width
  11629.     if trainersprite2
  11630.       oldx[5]=trainersprite2.x; trainersprite2.x-=Graphics.width
  11631.     end
  11632.     oldx[6]=@sprites["shadow1"].x; @sprites["shadow1"].x-=Graphics.width
  11633.     if @sprites["shadow3"]
  11634.       oldx[7]=@sprites["shadow3"].x; @sprites["shadow3"].x-=Graphics.width
  11635.     end
  11636.     @sprites["partybarfoe"].x-=PokeBattle_SceneConstants::FOEPARTYBAR_X
  11637.     @sprites["partybarplayer"].x+=Graphics.width-PokeBattle_SceneConstants::PLAYERPARTYBAR_X
  11638.     #################
  11639.     appearspeed=12
  11640.     (1+Graphics.width/appearspeed).times do
  11641.       tobreak=true
  11642.       if @viewport.rect.y>0
  11643.         @viewport.rect.y-=appearspeed/2
  11644.         @viewport.rect.y=0 if @viewport.rect.y<0
  11645.         @viewport.rect.height+=appearspeed
  11646.         @viewport.rect.height=Graphics.height if @viewport.rect.height>Graphics.height
  11647.         tobreak=false
  11648.       end
  11649.       if !tobreak
  11650.         for i in @sprites
  11651.           i[1].ox=@viewport.rect.x
  11652.           i[1].oy=@viewport.rect.y
  11653.         end
  11654.       end
  11655.       if @sprites["playerbase"].x>oldx[0]
  11656.         @sprites["playerbase"].x-=appearspeed; tobreak=false
  11657.         @sprites["playerbase"].x=oldx[0] if @sprites["playerbase"].x<oldx[0]
  11658.       end
  11659.       if @sprites["player"].x>oldx[1]
  11660.         @sprites["player"].x-=appearspeed; tobreak=false
  11661.         @sprites["player"].x=oldx[1] if @sprites["player"].x<oldx[1]
  11662.       end
  11663.       if @sprites["playerB"] && @sprites["playerB"].x>oldx[2]
  11664.         @sprites["playerB"].x-=appearspeed; tobreak=false
  11665.         @sprites["playerB"].x=oldx[2] if @sprites["playerB"].x<oldx[2]
  11666.       end
  11667.       if @sprites["enemybase"].x<oldx[3]
  11668.         @sprites["enemybase"].x+=appearspeed; tobreak=false
  11669.         @sprites["enemybase"].x=oldx[3] if @sprites["enemybase"].x>oldx[3]
  11670.       end
  11671.       if trainersprite1.x<oldx[4]
  11672.         trainersprite1.x+=appearspeed; tobreak=false
  11673.         trainersprite1.x=oldx[4] if trainersprite1.x>oldx[4]
  11674.       end
  11675.       if trainersprite2 && trainersprite2.x<oldx[5]
  11676.         trainersprite2.x+=appearspeed; tobreak=false
  11677.         trainersprite2.x=oldx[5] if trainersprite2.x>oldx[5]
  11678.       end
  11679.       if @sprites["shadow1"].x<oldx[6]
  11680.         @sprites["shadow1"].x+=appearspeed; tobreak=false
  11681.         @sprites["shadow1"].x=oldx[6] if @sprites["shadow1"].x>oldx[6]
  11682.       end
  11683.       if @sprites["shadow3"] && @sprites["shadow3"].x<oldx[7]
  11684.         @sprites["shadow3"].x+=appearspeed; tobreak=false
  11685.         @sprites["shadow3"].x=oldx[7] if @sprites["shadow3"].x>oldx[7]
  11686.       end
  11687.       pbGraphicsUpdate
  11688.       pbInputUpdate
  11689.       pbFrameUpdate
  11690.       break if tobreak
  11691.     end
  11692.     #################
  11693.     if @battle.opponent
  11694.       @enablePartyAnim=true
  11695.       @partyAnimPhase=0
  11696.       @sprites["partybarfoe"].visible=true
  11697.       @sprites["partybarplayer"].visible=true
  11698.     else
  11699.       pbPlayCry(@battle.party2[0])   # Play cry for wild Pokémon
  11700.       @sprites["battlebox1"].appear
  11701.       @sprites["battlebox3"].appear if @battle.party2.length==2
  11702.       appearing=true
  11703.       begin
  11704.         pbGraphicsUpdate
  11705.         pbInputUpdate
  11706.         pbFrameUpdate
  11707.         @sprites["pokemon1"].tone.red+=8 if @sprites["pokemon1"].tone.red<0
  11708.         @sprites["pokemon1"].tone.blue+=8 if @sprites["pokemon1"].tone.blue<0
  11709.         @sprites["pokemon1"].tone.green+=8 if @sprites["pokemon1"].tone.green<0
  11710.         @sprites["pokemon1"].tone.gray+=8 if @sprites["pokemon1"].tone.gray<0
  11711.         appearing=@sprites["battlebox1"].appearing
  11712.         if @battle.party2.length==2
  11713.           @sprites["pokemon3"].tone.red+=8 if @sprites["pokemon3"].tone.red<0
  11714.           @sprites["pokemon3"].tone.blue+=8 if @sprites["pokemon3"].tone.blue<0
  11715.           @sprites["pokemon3"].tone.green+=8 if @sprites["pokemon3"].tone.green<0
  11716.           @sprites["pokemon3"].tone.gray+=8 if @sprites["pokemon3"].tone.gray<0
  11717.           appearing=(appearing || @sprites["battlebox3"].appearing)
  11718.         end
  11719.       end while appearing
  11720.       # Show shiny animation for wild Pokémon
  11721.       if @battle.battlers[1].isShiny? && @battle.battlescene
  11722.         pbCommonAnimation("Shiny",@battle.battlers[1],nil)
  11723.       end
  11724.       if @battle.party2.length==2
  11725.         if @battle.battlers[3].isShiny? && @battle.battlescene
  11726.           pbCommonAnimation("Shiny",@battle.battlers[3],nil)
  11727.         end
  11728.       end
  11729.     end
  11730.   end
  11731.   def pbEndBattle(result)
  11732.     @abortable=false
  11733.     pbShowWindow(BLANK)
  11734.     # Fade out all sprites
  11735.     pbBGMFade(1.0)
  11736.     pbFadeOutAndHide(@sprites)
  11737.     pbDisposeSprites
  11738.   end
  11739.   def pbRecall(battlerindex)
  11740.     @briefmessage=false
  11741.     if @battle.pbIsOpposing?(battlerindex)
  11742.       origin=PokeBattle_SceneConstants::FOEBATTLER_Y
  11743.       if @battle.doublebattle
  11744.         origin=PokeBattle_SceneConstants::FOEBATTLERD1_Y if battlerindex==1
  11745.         origin=PokeBattle_SceneConstants::FOEBATTLERD2_Y if battlerindex==3
  11746.       end
  11747.       @sprites["shadow#{battlerindex}"].visible=false
  11748.     else
  11749.       origin=PokeBattle_SceneConstants::PLAYERBATTLER_Y
  11750.       if @battle.doublebattle
  11751.         origin=PokeBattle_SceneConstants::PLAYERBATTLERD1_Y if battlerindex==0
  11752.         origin=PokeBattle_SceneConstants::PLAYERBATTLERD2_Y if battlerindex==2
  11753.       end
  11754.     end
  11755.     spritePoke=@sprites["pokemon#{battlerindex}"]
  11756.     picturePoke=PictureEx.new(spritePoke.z)
  11757.     dims=[spritePoke.x,spritePoke.y]
  11758.     center=getSpriteCenter(spritePoke)
  11759.     # starting positions
  11760.     picturePoke.moveVisible(1,true)
  11761.     picturePoke.moveOrigin(1,PictureOrigin::Center)
  11762.     picturePoke.moveXY(0,1,center[0],center[1])
  11763.     # directives
  11764.     picturePoke.moveTone(10,1,Tone.new(248,248,248,248))
  11765.     delay=picturePoke.totalDuration
  11766.     picturePoke.moveSE(delay,"Audio/SE/Battle recall")
  11767.     picturePoke.moveZoom(15,delay,0)
  11768.     picturePoke.moveXY(15,delay,center[0],origin)
  11769.     picturePoke.moveVisible(picturePoke.totalDuration,false)
  11770.     picturePoke.moveTone(0,picturePoke.totalDuration,Tone.new(0,0,0,0))
  11771.     picturePoke.moveOrigin(picturePoke.totalDuration,PictureOrigin::TopLeft)
  11772.     loop do
  11773.       picturePoke.update
  11774.       setPictureSprite(spritePoke,picturePoke)
  11775.       pbGraphicsUpdate
  11776.       pbInputUpdate
  11777.       pbFrameUpdate
  11778.       break if !picturePoke.running?
  11779.     end
  11780.   end
  11781.   def pbTrainerSendOut(battlerindex,pkmn)
  11782.     illusionpoke=@battle.battlers[battlerindex].effects[PBEffects::Illusion]
  11783.     @briefmessage=false
  11784.     fadeanim=nil
  11785.     while inPartyAnimation?; end
  11786.     if @showingenemy
  11787.       fadeanim=TrainerFadeAnimation.new(@sprites)
  11788.     end
  11789.     frame=0
  11790.     @sprites["pokemon#{battlerindex}"].setPokemonBitmap(pkmn,false)
  11791.     if illusionpoke
  11792.       @sprites["pokemon#{battlerindex}"].setPokemonBitmap(illusionpoke,false)
  11793.     end
  11794.     sendout=PokeballSendOutAnimation.new(@sprites["pokemon#{battlerindex}"],
  11795.        @sprites,@battle.battlers[battlerindex],illusionpoke,@battle.doublebattle)
  11796.     loop do
  11797.       fadeanim.update if fadeanim
  11798.       frame+=1    
  11799.       if frame==1
  11800.         @sprites["battlebox#{battlerindex}"].appear
  11801.       end
  11802.       if frame>=10
  11803.         sendout.update
  11804.       end
  11805.       pbGraphicsUpdate
  11806.       pbInputUpdate
  11807.       pbFrameUpdate
  11808.       break if (!fadeanim || fadeanim.animdone?) && sendout.animdone? &&
  11809.          !@sprites["battlebox#{battlerindex}"].appearing
  11810.     end
  11811.     if @battle.battlers[battlerindex].isShiny? && @battle.battlescene
  11812.       pbCommonAnimation("Shiny",@battle.battlers[battlerindex],nil)
  11813.     end
  11814.     sendout.dispose
  11815.     if @showingenemy
  11816.       @showingenemy=false
  11817.       pbDisposeSprite(@sprites,"trainer")
  11818.       pbDisposeSprite(@sprites,"partybarfoe")
  11819.       for i in 0...6
  11820.         pbDisposeSprite(@sprites,"enemy#{i}")
  11821.       end
  11822.     end
  11823.     pbRefresh
  11824.   end
  11825.   def pbSendOut(battlerindex,pkmn) # Player sending out Pokémon
  11826.     while inPartyAnimation?; end
  11827.     illusionpoke=@battle.battlers[battlerindex].effects[PBEffects::Illusion]
  11828.     balltype=pkmn.ballused
  11829.     balltype=illusionpoke.ballused if illusionpoke
  11830.     ballbitmap=sprintf("Graphics/Pictures/Battle/ball_%02d",balltype)
  11831.     pictureBall=PictureEx.new(32)
  11832.     delay=1
  11833.     pictureBall.moveVisible(delay,true)
  11834.     pictureBall.moveName(delay,ballbitmap)
  11835.     pictureBall.moveOrigin(delay,PictureOrigin::Center)
  11836.     # Setting the ball's movement path
  11837.     path=[[0,   146], [10,  134], [21,  122], [30,  112],
  11838.           [39,  104], [46,   99], [53,   95], [61,   93],
  11839.           [68,   93], [75,   96], [82,  102], [89,  111],
  11840.           [94,  121], [100, 134], [106, 150], [111, 166],
  11841.           [116, 183], [120, 199], [124, 216], [127, 238]]
  11842.     spriteBall=IconSprite.new(0,0,@viewport)
  11843.     spriteBall.visible=false
  11844.     angle=0
  11845.     multiplier=1.0
  11846.     if @battle.doublebattle
  11847.       multiplier=(battlerindex==0) ? 0.7 : 1.3
  11848.     end
  11849.     for coord in path
  11850.     # delay=pictureBall.totalDuration
  11851.     # pictureBall.moveAngle(0,delay,angle)
  11852.     # pictureBall.moveXY(1,delay,coord[0]*multiplier,coord[1])
  11853.     # angle+=40
  11854.     # angle%=360
  11855.     end
  11856.     pictureBall.adjustPosition(0,@traineryoffset)
  11857.     @sprites["battlebox#{battlerindex}"].visible=false
  11858.     @briefmessage=false
  11859.     fadeanim=nil
  11860.     if @showingplayer
  11861.       fadeanim=PlayerFadeAnimation.new(@sprites)
  11862.     end
  11863.     frame=0
  11864.     @sprites["pokemon#{battlerindex}"].setPokemonBitmap(pkmn,true)
  11865.     if illusionpoke
  11866.       @sprites["pokemon#{battlerindex}"].setPokemonBitmap(illusionpoke,true)
  11867.     end
  11868.     sendout=PokeballPlayerSendOutAnimation.new(@sprites["pokemon#{battlerindex}"],
  11869.        @sprites,@battle.battlers[battlerindex],illusionpoke,@battle.doublebattle)
  11870.     loop do
  11871.       fadeanim.update if fadeanim
  11872.       frame+=1
  11873.       if frame>1 && !pictureBall.running? && !@sprites["battlebox#{battlerindex}"].appearing
  11874.         @sprites["battlebox#{battlerindex}"].appear
  11875.       end
  11876.       if frame>=3 && !pictureBall.running?
  11877.         sendout.update
  11878.       end
  11879.       if (frame>=10 || !fadeanim) && pictureBall.running?
  11880.         pictureBall.update
  11881.         setPictureIconSprite(spriteBall,pictureBall)
  11882.       end
  11883.       pbGraphicsUpdate
  11884.       pbInputUpdate
  11885.       pbFrameUpdate
  11886.       break if (!fadeanim || fadeanim.animdone?) && sendout.animdone? &&
  11887.          !@sprites["battlebox#{battlerindex}"].appearing
  11888.     end
  11889.     spriteBall.dispose
  11890.     sendout.dispose
  11891.     if @battle.battlers[battlerindex].isShiny? && @battle.battlescene
  11892.       pbCommonAnimation("Shiny",@battle.battlers[battlerindex],nil)
  11893.     end
  11894.     if @showingplayer
  11895.       @showingplayer=false
  11896.       pbDisposeSprite(@sprites,"player")
  11897.       pbDisposeSprite(@sprites,"partybarplayer")
  11898.       for i in 0...6
  11899.         pbDisposeSprite(@sprites,"player#{i}")
  11900.       end
  11901.     end
  11902.     pbRefresh
  11903.   end
  11904.   def pbTrainerWithdraw(battle,pkmn)
  11905.     pbRefresh
  11906.   end
  11907.   def pbWithdraw(battle,pkmn)
  11908.     pbRefresh
  11909.   end
  11910.   def pbMoveString(move)
  11911.     ret=move.name
  11912.     typename=PBTypes.getName(move.type)
  11913.     if move.id>0
  11914.       ret+=_INTL(" ({1}) PP: {2}/{3}",typename,move.pp,move.totalpp)
  11915.     end
  11916.     return ret
  11917.   end
  11918.   def pbBeginAttackPhase
  11919.     pbSelectBattler(-1)
  11920.     pbGraphicsUpdate
  11921.   end
  11922.   def pbSafariStart
  11923.     @briefmessage=false
  11924.     @sprites["battlebox0"]=SafariDataBox.new(@battle,@viewport)
  11925.     @sprites["battlebox0"].appear
  11926.     loop do
  11927.       @sprites["battlebox0"].update
  11928.       pbGraphicsUpdate
  11929.       pbInputUpdate
  11930.       pbFrameUpdate
  11931.       break if !@sprites["battlebox0"].appearing
  11932.     end
  11933.     pbRefresh
  11934.   end
  11935.   def pbResetCommandIndices
  11936.     @lastcmd=[0,0,0,0]
  11937.   end
  11938.   def pbResetMoveIndex(index)
  11939.     @lastmove[index]=0
  11940.   end
  11941.   def pbSafariCommandMenu(index)
  11942.     pbCommandMenuEx(index,[
  11943.        _INTL("What will\n{1} throw?",@battle.pbPlayer.name),
  11944.        _INTL("Ball"),
  11945.        _INTL("Bait"),
  11946.        _INTL("Rock"),
  11947.        _INTL("Run")
  11948.     ],2)
  11949.   end
  11950. # Use this method to display the list of commands.
  11951. # Return values: 0=Fight, 1=Bag, 2=Pokémon, 3=Run, 4=Call
  11952.   def pbCommandMenu(index)
  11953.     shadowTrainer=(hasConst?(PBTypes,:SHADOW) && @battle.opponent)
  11954.     ret=pbCommandMenuEx(index,[
  11955.        _INTL("What will\n{1} do?",@battle.battlers[index].name),
  11956.        _INTL("Fight"),
  11957.        _INTL("Bag"),
  11958.        _INTL("Pokémon"),
  11959.        shadowTrainer ? _INTL("Call") : _INTL("Run")
  11960.     ],(shadowTrainer ? 1 : 0))
  11961.     ret=4 if ret==3 && shadowTrainer   # Convert "Run" to "Call"
  11962.     return ret
  11963.   end
  11964.   def pbCommandMenuEx(index,texts,mode=0)      # Mode: 0 - regular battle
  11965.     pbShowWindow(COMMANDBOX)                   #       1 - Shadow Pokémon battle
  11966.     cw=@sprites["commandwindow"]               #       2 - Safari Zone
  11967.     cw.setTexts(texts)                         #       3 - Bug Catching Contest
  11968.     cw.index=@lastcmd[index]
  11969.     cw.mode=mode
  11970.     pbSelectBattler(index)
  11971.     pbRefresh
  11972.     loop do
  11973.       pbGraphicsUpdate
  11974.       pbInputUpdate
  11975.       pbFrameUpdate(cw)
  11976.       # Update selected command
  11977.       if Input.trigger?(Input::LEFT) && (cw.index&1)==1
  11978.         pbPlayCursorSE
  11979.         cw.index-=1
  11980.       elsif Input.trigger?(Input::RIGHT) &&  (cw.index&1)==0
  11981.         pbPlayCursorSE
  11982.         cw.index+=1
  11983.       elsif Input.trigger?(Input::UP) &&  (cw.index&2)==2
  11984.         pbPlayCursorSE
  11985.         cw.index-=2
  11986.       elsif Input.trigger?(Input::DOWN) &&  (cw.index&2)==0
  11987.         pbPlayCursorSE
  11988.         cw.index+=2
  11989.       end
  11990.       if Input.trigger?(Input::C)   # Confirm choice
  11991.         pbPlayDecisionSE
  11992.         ret=cw.index
  11993.         @lastcmd[index]=ret
  11994.         return ret
  11995.       elsif Input.trigger?(Input::B) && index==2 && @lastcmd[0]!=2 # Cancel
  11996.         pbPlayDecisionSE
  11997.         return -1
  11998.       end
  11999.     end
  12000.   end
  12001. # Use this method to display the list of moves for a Pokémon
  12002.   def pbFightMenu(index)
  12003.     pbShowWindow(FIGHTBOX)
  12004.     cw = @sprites["fightwindow"]
  12005.     battler=@battle.battlers[index]
  12006.     cw.battler=battler
  12007.     lastIndex=@lastmove[index]
  12008.     if battler.moves[lastIndex].id!=0
  12009.       cw.setIndex(lastIndex)
  12010.     else
  12011.       cw.setIndex(0)
  12012.     end
  12013.     cw.megaButton=0
  12014.     cw.megaButton=1 if @battle.pbCanMegaEvolve?(index)
  12015.     pbSelectBattler(index)
  12016.     pbRefresh
  12017.     loop do
  12018.       pbGraphicsUpdate
  12019.       pbInputUpdate
  12020.       pbFrameUpdate(cw)
  12021.       # Update selected command
  12022.       if Input.trigger?(Input::LEFT) && (cw.index&1)==1
  12023.         pbPlayCursorSE if cw.setIndex(cw.index-1)
  12024.       elsif Input.trigger?(Input::RIGHT) &&  (cw.index&1)==0
  12025.         pbPlayCursorSE if cw.setIndex(cw.index+1)
  12026.       elsif Input.trigger?(Input::UP) &&  (cw.index&2)==2
  12027.         pbPlayCursorSE if cw.setIndex(cw.index-2)
  12028.       elsif Input.trigger?(Input::DOWN) &&  (cw.index&2)==0
  12029.         pbPlayCursorSE if cw.setIndex(cw.index+2)
  12030.       end
  12031.       if Input.trigger?(Input::C)   # Confirm choice
  12032.         ret=cw.index
  12033.         pbPlayDecisionSE
  12034.         @lastmove[index]=ret
  12035.         return ret
  12036.       elsif Input.trigger?(Input::A)   # Use Mega Evolution
  12037.         if @battle.pbCanMegaEvolve?(index)
  12038.           @battle.pbRegisterMegaEvolution(index)
  12039.           cw.megaButton=2
  12040.           pbPlayDecisionSE
  12041.         end
  12042.       elsif Input.trigger?(Input::B)   # Cancel fight menu
  12043.         @lastmove[index]=cw.index
  12044.         pbPlayCancelSE
  12045.         return -1
  12046.       end
  12047.     end
  12048.   end
  12049. # Use this method to display the inventory
  12050. # The return value is the item chosen, or 0 if the choice was canceled.
  12051.   def pbItemMenu(index)
  12052.     ret=0
  12053.     retindex=-1
  12054.     pkmnid=-1
  12055.     endscene=true
  12056.     oldsprites=pbFadeOutAndHide(@sprites)
  12057.     oldlastpocket = $PokemonBag.lastpocket
  12058.     oldchoices = $PokemonBag.getAllChoices
  12059.     $PokemonBag.lastpocket = @baglastpocket if @baglastpocket!=nil
  12060.     $PokemonBag.setAllChoices(@bagchoices) if @bagchoices!=nil
  12061.     itemscene=PokemonBag_Scene.new
  12062.     itemscene.pbStartScene($PokemonBag,true,Proc.new{|item|
  12063.        $ItemData[item][ITEMBATTLEUSE]>0 || pbIsPokeBall?(item)
  12064.        },false)
  12065.     loop do
  12066.       item=itemscene.pbChooseItem
  12067.       break if item==0
  12068.       usetype=$ItemData[item][ITEMBATTLEUSE]
  12069.       cmdUse=-1
  12070.       commands=[]
  12071.       if usetype==0
  12072.         commands[commands.length]=_INTL("Cancel")
  12073.       else
  12074.         commands[cmdUse=commands.length]=_INTL("Use")
  12075.         commands[commands.length]=_INTL("Cancel")
  12076.       end
  12077.       itemname=PBItems.getName(item)
  12078.       command=itemscene.pbShowCommands(_INTL("{1} is selected.",itemname),commands)
  12079.       if cmdUse>=0 && command==cmdUse
  12080.         if usetype==1 || usetype==3
  12081.           modparty=[]
  12082.           if $Trainer.partyplus>=1
  12083.           for i in 0...$Trainer.partyplus
  12084.             modparty.push(@battle.party1[@battle.party1order[i]])
  12085.           end
  12086.           else
  12087.           for i in 0...6
  12088.             modparty.push(@battle.party1[@battle.party1order[i]])
  12089.           end
  12090.           end
  12091.           pkmnlist=PokemonParty_Scene.new
  12092.           pkmnscreen=PokemonPartyScreen.new(pkmnlist,modparty)
  12093.           itemscene.pbEndScene
  12094.           pkmnscreen.pbStartScene(_INTL("Use on which Pokémon?"),@battle.doublebattle)
  12095.           activecmd=pkmnscreen.pbChoosePokemon
  12096.           pkmnid=@battle.party1order[activecmd]
  12097.           if activecmd>=0 && pkmnid>=0 && ItemHandlers.hasBattleUseOnPokemon(item)
  12098.             pkmnlist.pbEndScene
  12099.             ret=item
  12100.             retindex=pkmnid
  12101.             endscene=false
  12102.             break
  12103.           end
  12104.           pkmnlist.pbEndScene
  12105.           itemscene.pbStartScene($PokemonBag)
  12106.         elsif usetype==2 || usetype==4
  12107.           if ItemHandlers.hasBattleUseOnBattler(item)
  12108.             ret=item
  12109.             retindex=index
  12110.             break
  12111.           end
  12112.         end
  12113.       end
  12114.     end
  12115.     pbConsumeItemInBattle($PokemonBag,ret) if ret>0
  12116.     itemscene.pbEndScene if endscene
  12117.     @baglastpocket = $PokemonBag.lastpocket
  12118.     @bagchoices = $PokemonBag.getAllChoices
  12119.     $PokemonBag.lastpocket = oldlastpocket
  12120.     $PokemonBag.setAllChoices(oldchoices)
  12121.     pbFadeInAndShow(@sprites,oldsprites)
  12122.     return [ret,retindex]
  12123.   end
  12124. # Called whenever a Pokémon should forget a move.  It should return -1 if the
  12125. # selection is canceled, or 0 to 3 to indicate the move to forget.  The function
  12126. # should not allow HM moves to be forgotten.
  12127.   def pbForgetMove(pokemon,moveToLearn)
  12128.     ret=-1
  12129.     pbFadeOutIn(99999){
  12130.        scene=PokemonSummary_Scene.new
  12131.        screen=PokemonSummaryScreen.new(scene)
  12132.        ret=screen.pbStartForgetScreen([pokemon],0,moveToLearn)
  12133.     }
  12134.     return ret
  12135.   end
  12136. # Called whenever a Pokémon needs one of its moves chosen. Used for Ether.
  12137.   def pbChooseMove(pokemon,message)
  12138.     ret=-1
  12139.     pbFadeOutIn(99999){
  12140.        scene=PokemonSummary_Scene.new
  12141.        screen=PokemonSummaryScreen.new(scene)
  12142.        ret=screen.pbStartChooseMoveScreen([pokemon],0,message)
  12143.     }
  12144.     return ret
  12145.   end
  12146.   def pbNameEntry(helptext,pokemon)
  12147.     return pbEnterPokemonName(helptext,0,PokeBattle_Pokemon::NAMELIMIT,"",pokemon)
  12148.   end
  12149.   def pbSelectBattler(index,selectmode=1)
  12150.     numwindows=@battle.doublebattle ? 4 : 2
  12151.     for i in 0...numwindows
  12152.       sprite=@sprites["battlebox#{i}"]
  12153.       sprite.selected=(i==index) ? selectmode : 0
  12154.       sprite=@sprites["pokemon#{i}"]
  12155.       sprite.selected=(i==index) ? selectmode : 0
  12156.     end
  12157.   end
  12158.   def pbFirstTarget(index,targettype)
  12159.     case targettype
  12160.     when PBTargets::SingleNonUser
  12161.       for i in 0...4
  12162.         if i!=index && !@battle.battlers[i].fainted? &&
  12163.            @battle.battlers[index].pbIsOpposing?(i)
  12164.           return i
  12165.         end  
  12166.       end
  12167.     when PBTargets::UserOrPartner
  12168.       return index
  12169.     end
  12170.     return -1
  12171.   end
  12172.   def pbUpdateSelected(index)
  12173.     numwindows=@battle.doublebattle ? 4 : 2
  12174.     for i in 0...numwindows
  12175.       if i==index
  12176.         @sprites["battlebox#{i}"].selected=2
  12177.         @sprites["pokemon#{i}"].selected=2
  12178.       else
  12179.         @sprites["battlebox#{i}"].selected=0
  12180.         @sprites["pokemon#{i}"].selected=0
  12181.       end
  12182.     end
  12183.     pbFrameUpdate
  12184.   end
  12185. # Use this method to make the player choose a target
  12186. # for certain moves in double battles.
  12187.   def pbChooseTarget(index,targettype)
  12188.     pbShowWindow(FIGHTBOX)
  12189.     cw = @sprites["fightwindow"]
  12190.     battler=@battle.battlers[index]
  12191.     cw.battler=battler
  12192.     lastIndex=@lastmove[index]
  12193.     if battler.moves[lastIndex].id!=0
  12194.       cw.setIndex(lastIndex)
  12195.     else
  12196.       cw.setIndex(0)
  12197.     end
  12198.     curwindow=pbFirstTarget(index,targettype)
  12199.     if curwindow==-1
  12200.       raise RuntimeError.new(_INTL("No targets somehow..."))
  12201.     end
  12202.     loop do
  12203.       pbGraphicsUpdate
  12204.       pbInputUpdate
  12205.       pbUpdateSelected(curwindow)
  12206.       if Input.trigger?(Input::B)
  12207.         pbUpdateSelected(-1)
  12208.         return -1
  12209.       elsif Input.trigger?(Input::C)
  12210.         pbUpdateSelected(-1)
  12211.         return curwindow
  12212.       end
  12213.       if curwindow>=0
  12214.         if Input.trigger?(Input::RIGHT) || Input.trigger?(Input::DOWN)
  12215.           loop do
  12216.             case targettype
  12217.             when PBTargets::SingleNonUser
  12218.               case curwindow
  12219.               when 0; newcurwindow=2
  12220.               when 1; newcurwindow=0
  12221.               when 2; newcurwindow=3
  12222.               when 3; newcurwindow=1
  12223.               end
  12224.             when PBTargets::UserOrPartner
  12225.               newcurwindow=(curwindow+2)%4
  12226.             end
  12227.             curwindow=newcurwindow
  12228.             next if targettype==PBTargets::SingleNonUser && curwindow==index
  12229.             break if !@battle.battlers[curwindow].fainted?
  12230.           end
  12231.         elsif Input.trigger?(Input::LEFT) || Input.trigger?(Input::UP)
  12232.           loop do
  12233.             case targettype
  12234.             when PBTargets::SingleNonUser
  12235.               case curwindow
  12236.               when 0; newcurwindow=1
  12237.               when 1; newcurwindow=3
  12238.               when 2; newcurwindow=0
  12239.               when 3; newcurwindow=2
  12240.               end
  12241.             when PBTargets::UserOrPartner
  12242.               newcurwindow=(curwindow+2)%4
  12243.             end
  12244.             curwindow=newcurwindow
  12245.             next if targettype==PBTargets::SingleNonUser && curwindow==index
  12246.             break if !@battle.battlers[curwindow].fainted?
  12247.           end
  12248.         end
  12249.       end
  12250.     end
  12251.   end
  12252.   def pbSwitch(index,lax,cancancel)
  12253.     party=@battle.pbParty(index)
  12254.     partypos=@battle.party1order
  12255.     ret=-1
  12256.     # Fade out and hide all sprites
  12257.     visiblesprites=pbFadeOutAndHide(@sprites)
  12258.     pbShowWindow(BLANK)
  12259.     pbSetMessageMode(true)
  12260.     modparty=[]
  12261.     if $Trainer.partyplus>=1
  12262.     for i in 0...$Trainer.partyplus
  12263.       modparty.push(party[partypos[i]])
  12264.     end
  12265.     else
  12266.     for i in 0...6
  12267.       modparty.push(party[partypos[i]])
  12268.     end
  12269.     end
  12270.     scene=PokemonParty_Scene.new
  12271.     @switchscreen=PokemonPartyScreen.new(scene,modparty)
  12272.     @switchscreen.pbStartScene(_INTL("Choose a Pokémon."),
  12273.        @battle.doublebattle && !@battle.fullparty1)
  12274.     loop do
  12275.       scene.pbSetHelpText(_INTL("Choose a Pokémon."))
  12276.       activecmd=@switchscreen.pbChoosePokemon
  12277.       if cancancel && activecmd==-1
  12278.         ret=-1
  12279.         break
  12280.       end
  12281.       if activecmd>=0
  12282.         commands=[]
  12283.         cmdShift=-1
  12284.         cmdSummary=-1
  12285.         pkmnindex=partypos[activecmd]
  12286.         commands[cmdShift=commands.length]=_INTL("Switch In") if !party[pkmnindex].egg?
  12287.         commands[cmdSummary=commands.length]=_INTL("Summary")
  12288.         commands[commands.length]=_INTL("Cancel")
  12289.         command=scene.pbShowCommands(_INTL("Do what with {1}?",party[pkmnindex].name),commands)
  12290.         if cmdShift>=0 && command==cmdShift
  12291.           canswitch=lax ? @battle.pbCanSwitchLax?(index,pkmnindex,true) :
  12292.              @battle.pbCanSwitch?(index,pkmnindex,true)
  12293.           if canswitch
  12294.             ret=pkmnindex
  12295.             break
  12296.           end
  12297.         elsif cmdSummary>=0 && command==cmdSummary
  12298.           scene.pbSummary(activecmd)
  12299.         end
  12300.       end
  12301.     end
  12302.     @switchscreen.pbEndScene
  12303.     @switchscreen=nil
  12304.     pbShowWindow(BLANK)
  12305.     pbSetMessageMode(false)
  12306.     # back to main battle screen
  12307.     pbFadeInAndShow(@sprites,visiblesprites)
  12308.     return ret
  12309.   end
  12310.   def pbDamageAnimation(pkmn,effectiveness)
  12311.     pkmnsprite=@sprites["pokemon#{pkmn.index}"]
  12312.     shadowsprite=@sprites["shadow#{pkmn.index}"]
  12313.     sprite=@sprites["battlebox#{pkmn.index}"]
  12314.     oldshadowvisible=shadowsprite.visible
  12315.     oldvisible=sprite.visible
  12316.     sprite.selected=3
  12317.     @briefmessage=false
  12318.     6.times do
  12319.       pbGraphicsUpdate
  12320.       pbInputUpdate
  12321.       pbFrameUpdate
  12322.     end
  12323.     case effectiveness
  12324.     when 0
  12325.       pbSEPlay("Battle damage normal")
  12326.     when 1
  12327.       pbSEPlay("Battle damage weak")
  12328.     when 2
  12329.       pbSEPlay("Battle damage super")
  12330.     end
  12331.     8.times do
  12332.       pkmnsprite.visible=!pkmnsprite.visible
  12333.       if oldshadowvisible
  12334.         shadowsprite.visible=!shadowsprite.visible
  12335.       end
  12336.       4.times do
  12337.         pbGraphicsUpdate
  12338.         pbInputUpdate
  12339.         pbFrameUpdate
  12340.         sprite.update
  12341.       end
  12342.     end
  12343.     sprite.selected=0
  12344.     sprite.visible=oldvisible
  12345.   end
  12346. # This method is called whenever a Pokémon's HP changes.
  12347. # Used to animate the HP bar.
  12348.   def pbHPChanged(pkmn,oldhp,anim=false)
  12349.     @briefmessage=false
  12350.     hpchange=pkmn.hp-oldhp
  12351.     if hpchange<0
  12352.       hpchange=-hpchange
  12353.       PBDebug.log("[HP change] #{pkmn.pbThis} lost #{hpchange} HP (#{oldhp}=>#{pkmn.hp})")
  12354.     else
  12355.       PBDebug.log("[HP change] #{pkmn.pbThis} gained #{hpchange} HP (#{oldhp}=>#{pkmn.hp})")
  12356.     end
  12357.     if anim && @battle.battlescene
  12358.       if pkmn.hp>oldhp
  12359.         pbCommonAnimation("HealthUp",pkmn,nil)
  12360.       elsif pkmn.hp<oldhp
  12361.         pbCommonAnimation("HealthDown",pkmn,nil)
  12362.       end
  12363.     end
  12364.     sprite=@sprites["battlebox#{pkmn.index}"]
  12365.     sprite.animateHP(oldhp,pkmn.hp)
  12366.     while sprite.animatingHP
  12367.       pbGraphicsUpdate
  12368.       pbInputUpdate
  12369.       pbFrameUpdate
  12370.       sprite.update
  12371.     end
  12372.   end
  12373. # This method is called whenever a Pokémon faints.
  12374.   def pbFainted(pkmn)
  12375.     frames=pbCryFrameLength(pkmn.pokemon)
  12376.     pbPlayCry(pkmn.pokemon)
  12377.     frames.times do
  12378.       pbGraphicsUpdate
  12379.       pbInputUpdate
  12380.       pbFrameUpdate
  12381.     end
  12382.     @sprites["shadow#{pkmn.index}"].visible=false
  12383.     pkmnsprite=@sprites["pokemon#{pkmn.index}"]
  12384.     ycoord=0
  12385.     if @battle.doublebattle
  12386.       ycoord=PokeBattle_SceneConstants::PLAYERBATTLERD1_Y if pkmn.index==0
  12387.       ycoord=PokeBattle_SceneConstants::FOEBATTLERD1_Y if pkmn.index==1
  12388.       ycoord=PokeBattle_SceneConstants::PLAYERBATTLERD2_Y if pkmn.index==2
  12389.       ycoord=PokeBattle_SceneConstants::FOEBATTLERD2_Y if pkmn.index==3
  12390.     else
  12391.       if @battle.pbIsOpposing?(pkmn.index)
  12392.         ycoord=PokeBattle_SceneConstants::FOEBATTLER_Y
  12393.       else
  12394.         ycoord=PokeBattle_SceneConstants::PLAYERBATTLER_Y
  12395.       end
  12396.     end
  12397.     pbSEPlay("Pkmn faint")
  12398.     loop do
  12399.       pkmnsprite.y+=8
  12400.       if pkmnsprite.y-pkmnsprite.oy+pkmnsprite.src_rect.height>=ycoord
  12401.         pkmnsprite.src_rect.height=ycoord-pkmnsprite.y+pkmnsprite.oy
  12402.       end
  12403.       pbGraphicsUpdate
  12404.       pbInputUpdate
  12405.       pbFrameUpdate
  12406.       break if pkmnsprite.y>=ycoord
  12407.     end
  12408.     pkmnsprite.visible=false
  12409.     8.times do
  12410.       @sprites["battlebox#{pkmn.index}"].opacity-=32
  12411.       pbGraphicsUpdate
  12412.       pbInputUpdate
  12413.       pbFrameUpdate
  12414.     end
  12415.     @sprites["battlebox#{pkmn.index}"].visible=false
  12416.     pkmn.pbResetForm
  12417.   end
  12418. # Use this method to choose a command for the enemy.
  12419.   def pbChooseEnemyCommand(index)
  12420.     @battle.pbDefaultChooseEnemyCommand(index)
  12421.   end
  12422. # Use this method to choose a new Pokémon for the enemy
  12423. # The enemy's party is guaranteed to have at least one choosable member.
  12424.   def pbChooseNewEnemy(index,party)
  12425.     @battle.pbDefaultChooseNewEnemy(index,party)
  12426.   end
  12427. # This method is called when the player wins a wild Pokémon battle.
  12428. # This method can change the battle's music for example.
  12429.   def pbWildBattleSuccess
  12430.     pbBGMPlay(pbGetWildVictoryME())
  12431.   end
  12432. # This method is called when the player wins a Trainer battle.
  12433. # This method can change the battle's music for example.
  12434.   def pbTrainerBattleSuccess
  12435.     pbBGMPlay(pbGetTrainerVictoryME(@battle.opponent))
  12436.   end
  12437.   def pbEXPBar(pokemon,battler,startexp,endexp,tempexp1,tempexp2)
  12438.     if battler
  12439.       @sprites["battlebox#{battler.index}"].refreshExpLevel
  12440.       exprange=(endexp-startexp)
  12441.       startexplevel=0
  12442.       endexplevel=0
  12443.       if exprange!=0
  12444.         startexplevel=(tempexp1-startexp)*192/exprange
  12445.         endexplevel=(tempexp2-startexp)*192/exprange
  12446.       end
  12447.       @sprites["battlebox#{battler.index}"].animateEXP(startexplevel,endexplevel)
  12448.       while @sprites["battlebox#{battler.index}"].animatingEXP
  12449.         pbGraphicsUpdate
  12450.         pbInputUpdate
  12451.         pbFrameUpdate
  12452.         @sprites["battlebox#{battler.index}"].update
  12453.       end
  12454.     end
  12455.   end
  12456.   def pbShowPokedex(species)
  12457.     pbFadeOutIn(99999){
  12458.       scene = PokemonPokedexInfo_Scene.new
  12459.       screen = PokemonPokedexInfoScreen.new(scene)
  12460.       screen.pbDexEntry(species)
  12461.     }
  12462.   end
  12463.   def pbChangeSpecies(attacker,species)
  12464.     pkmn=@sprites["pokemon#{attacker.index}"]
  12465.     shadow=@sprites["shadow#{attacker.index}"]
  12466.     back=!@battle.pbIsOpposing?(attacker.index)
  12467.     pkmn.setPokemonBitmapSpecies(attacker.pokemon,species,back)
  12468.     pkmn.x=-pkmn.bitmap.width/2
  12469.     pkmn.y=adjustBattleSpriteY(pkmn,species,attacker.index)
  12470.     if @battle.doublebattle
  12471.       case attacker.index
  12472.       when 0
  12473.         pkmn.x+=PokeBattle_SceneConstants::PLAYERBATTLERD1_X
  12474.         pkmn.y+=PokeBattle_SceneConstants::PLAYERBATTLERD1_Y
  12475.       when 1
  12476.         pkmn.x+=PokeBattle_SceneConstants::FOEBATTLERD1_X
  12477.         pkmn.y+=PokeBattle_SceneConstants::FOEBATTLERD1_Y
  12478.       when 2
  12479.         pkmn.x+=PokeBattle_SceneConstants::PLAYERBATTLERD2_X
  12480.         pkmn.y+=PokeBattle_SceneConstants::PLAYERBATTLERD2_Y
  12481.       when 3
  12482.         pkmn.x+=PokeBattle_SceneConstants::FOEBATTLERD2_X
  12483.         pkmn.y+=PokeBattle_SceneConstants::FOEBATTLERD2_Y
  12484.       end
  12485.     else
  12486.       pkmn.x+=PokeBattle_SceneConstants::PLAYERBATTLER_X if attacker.index==0
  12487.       pkmn.y+=PokeBattle_SceneConstants::PLAYERBATTLER_Y if attacker.index==0
  12488.       pkmn.x+=PokeBattle_SceneConstants::FOEBATTLER_X if attacker.index==1
  12489.       pkmn.y+=PokeBattle_SceneConstants::FOEBATTLER_Y if attacker.index==1
  12490.     end
  12491.     if shadow && !back
  12492.       shadow.visible=showShadow?(species)
  12493.     end
  12494.   end
  12495.   def pbChangePokemon(attacker,pokemon)
  12496.     pkmn=@sprites["pokemon#{attacker.index}"]
  12497.     shadow=@sprites["shadow#{attacker.index}"]
  12498.     back=!@battle.pbIsOpposing?(attacker.index)
  12499.     pkmn.setPokemonBitmap(pokemon,back)
  12500.     pkmn.x=-pkmn.bitmap.width/2
  12501.     pkmn.y=adjustBattleSpriteY(pkmn,pokemon.species,attacker.index)
  12502.     if @battle.doublebattle
  12503.       case attacker.index
  12504.       when 0
  12505.         pkmn.x+=PokeBattle_SceneConstants::PLAYERBATTLERD1_X
  12506.         pkmn.y+=PokeBattle_SceneConstants::PLAYERBATTLERD1_Y
  12507.       when 1
  12508.         pkmn.x+=PokeBattle_SceneConstants::FOEBATTLERD1_X
  12509.         pkmn.y+=PokeBattle_SceneConstants::FOEBATTLERD1_Y
  12510.       when 2
  12511.         pkmn.x+=PokeBattle_SceneConstants::PLAYERBATTLERD2_X
  12512.         pkmn.y+=PokeBattle_SceneConstants::PLAYERBATTLERD2_Y
  12513.       when 3
  12514.         pkmn.x+=PokeBattle_SceneConstants::FOEBATTLERD2_X
  12515.         pkmn.y+=PokeBattle_SceneConstants::FOEBATTLERD2_Y
  12516.       end
  12517.     else
  12518.       pkmn.x+=PokeBattle_SceneConstants::PLAYERBATTLER_X if attacker.index==0
  12519.       pkmn.y+=PokeBattle_SceneConstants::PLAYERBATTLER_Y if attacker.index==0
  12520.       pkmn.x+=PokeBattle_SceneConstants::FOEBATTLER_X if attacker.index==1
  12521.       pkmn.y+=PokeBattle_SceneConstants::FOEBATTLER_Y if attacker.index==1
  12522.     end
  12523.     if shadow && !back
  12524.       shadow.visible=showShadow?(pokemon.species)
  12525.     end
  12526.   end
  12527.   def pbSaveShadows
  12528.     shadows=[]
  12529.     for i in 0...4
  12530.       s=@sprites["shadow#{i}"]
  12531.       shadows[i]=s ? s.visible : false
  12532.       s.visible=false if s
  12533.     end
  12534.     yield
  12535.     for i in 0...4
  12536.       s=@sprites["shadow#{i}"]
  12537.       s.visible=shadows[i] if s
  12538.     end
  12539.   end
  12540.   def pbFindAnimation(moveid,userIndex,hitnum)
  12541.     begin
  12542.       move2anim=load_data("Data/move2anim.dat")
  12543.       noflip=false
  12544.       if (userIndex&1)==0   # On player's side
  12545.         anim=move2anim[0][moveid]
  12546.       else                  # On opposing side
  12547.         anim=move2anim[1][moveid]
  12548.         noflip=true if anim
  12549.         anim=move2anim[0][moveid] if !anim
  12550.       end
  12551.       return [anim+hitnum,noflip] if anim
  12552.       # Actual animation not found, get the default animation for the move's type
  12553.       move=PBMoveData.new(moveid)
  12554.       type=move.type
  12555.       typedefaultanim=[[:NORMAL,:TACKLE],
  12556.                        [:FIGHTING,:COMETPUNCH],
  12557.                        [:FLYING,:GUST],
  12558.                        [:POISON,:SLUDGE],
  12559.                        [:GROUND,:MUDSLAP],
  12560.                        [:ROCK,:ROCKTHROW],
  12561.                        [:BUG,:TWINEEDLE],
  12562.                        [:GHOST,:NIGHTSHADE],
  12563.                        [:STEEL,:GYROBALL],
  12564.                        [:FIRE,:EMBER],
  12565.                        [:WATER,:WATERGUN],
  12566.                        [:GRASS,:RAZORLEAF],
  12567.                        [:ELECTRIC,:THUNDERSHOCK],
  12568.                        [:PSYCHIC,:CONFUSION],
  12569.                        [:ICE,:ICEBALL],
  12570.                        [:DRAGON,:DRAGONRAGE],
  12571.                        [:DARK,:PURSUIT],
  12572.                        [:FAIRY,:FAIRYWIND]]
  12573.       for i in typedefaultanim
  12574.         if isConst?(type,PBTypes,i[0]) && hasConst?(PBMoves,i[1])
  12575.           noflip=false
  12576.           if (userIndex&1)==0   # On player's side
  12577.             anim=move2anim[0][getConst(PBMoves,i[1])]
  12578.           else                  # On opposing side
  12579.             anim=move2anim[1][getConst(PBMoves,i[1])]
  12580.             noflip=true if anim
  12581.             anim=move2anim[0][getConst(PBMoves,i[1])] if !anim
  12582.           end
  12583.           return [anim,noflip] if anim
  12584.           break
  12585.         end
  12586.       end
  12587.       # Default animation for the move's type not found, use Tackle's animation
  12588.       if hasConst?(PBMoves,:TACKLE)
  12589.         anim=move2anim[0][getConst(PBMoves,:TACKLE)]
  12590.         return [anim,false] if anim
  12591.       end
  12592.     rescue
  12593.       return nil
  12594.     end
  12595.     return nil
  12596.   end
  12597.   def pbCommonAnimation(animname,user,target,hitnum=0)
  12598.     animations=load_data("Data/PkmnAnimations.rxdata")
  12599.     for i in 0...animations.length
  12600.       if animations[i] && animations[i].name=="Common:"+animname
  12601.         pbAnimationCore(animations[i],user,(target!=nil) ? target : user)
  12602.         return
  12603.       end
  12604.     end
  12605.   end
  12606.   def pbAnimation(moveid,user,target,hitnum=0)
  12607.     animid=pbFindAnimation(moveid,user.index,hitnum)
  12608.     return if !animid
  12609.     anim=animid[0]
  12610.     animations=load_data("Data/PkmnAnimations.rxdata")
  12611.     pbSaveShadows {
  12612.        if animid[1] # On opposing side and using OppMove animation
  12613.          pbAnimationCore(animations[anim],target,user,true)
  12614.        else         # On player's side, and/or using Move animation
  12615.          pbAnimationCore(animations[anim],user,target)
  12616.        end
  12617.     }
  12618.     if PBMoveData.new(moveid).function==0x69 && user && target # Transform
  12619.       # Change form to transformed version
  12620.       pbChangePokemon(user,target.pokemon)
  12621.     end
  12622.   end
  12623.   def pbAnimationCore(animation,user,target,oppmove=false)
  12624.     return if !animation
  12625.     @briefmessage=false
  12626.     usersprite=(user) ? @sprites["pokemon#{user.index}"] : nil
  12627.     targetsprite=(target) ? @sprites["pokemon#{target.index}"] : nil
  12628.     olduserx=usersprite ? usersprite.x : 0
  12629.     oldusery=usersprite ? usersprite.y : 0
  12630.     oldtargetx=targetsprite ? targetsprite.x : 0
  12631.     oldtargety=targetsprite ? targetsprite.y : 0
  12632.     if !targetsprite
  12633.       target=user if !target
  12634.       animplayer=PBAnimationPlayerX.new(animation,user,target,self,oppmove)
  12635.       userwidth=(!usersprite || !usersprite.bitmap || usersprite.bitmap.disposed?) ? 128 : usersprite.bitmap.width
  12636.       userheight=(!usersprite || !usersprite.bitmap || usersprite.bitmap.disposed?) ? 128 : usersprite.bitmap.height
  12637.       animplayer.setLineTransform(
  12638.          PokeBattle_SceneConstants::FOCUSUSER_X,PokeBattle_SceneConstants::FOCUSUSER_Y,
  12639.          PokeBattle_SceneConstants::FOCUSTARGET_X,PokeBattle_SceneConstants::FOCUSTARGET_Y,
  12640.          olduserx+(userwidth/2),oldusery+(userheight/2),
  12641.          olduserx+(userwidth/2),oldusery+(userheight/2))
  12642.     else
  12643.       animplayer=PBAnimationPlayerX.new(animation,user,target,self,oppmove)
  12644.       userwidth=(!usersprite || !usersprite.bitmap || usersprite.bitmap.disposed?) ? 128 : usersprite.bitmap.width
  12645.       userheight=(!usersprite || !usersprite.bitmap || usersprite.bitmap.disposed?) ? 128 : usersprite.bitmap.height
  12646.       targetwidth=(!targetsprite.bitmap || targetsprite.bitmap.disposed?) ? 128 : targetsprite.bitmap.width
  12647.       targetheight=(!targetsprite.bitmap || targetsprite.bitmap.disposed?) ? 128 : targetsprite.bitmap.height
  12648.       animplayer.setLineTransform(
  12649.          PokeBattle_SceneConstants::FOCUSUSER_X,PokeBattle_SceneConstants::FOCUSUSER_Y,
  12650.          PokeBattle_SceneConstants::FOCUSTARGET_X,PokeBattle_SceneConstants::FOCUSTARGET_Y,
  12651.          olduserx+(userwidth/2),oldusery+(userheight/2),
  12652.          oldtargetx+(targetwidth/2),oldtargety+(targetheight/2))
  12653.     end
  12654.     animplayer.start
  12655.     while animplayer.playing?
  12656.       animplayer.update
  12657.       pbGraphicsUpdate
  12658.       pbInputUpdate
  12659.       pbFrameUpdate
  12660.     end
  12661.     usersprite.ox=0 if usersprite
  12662.     usersprite.oy=0 if usersprite
  12663.     usersprite.x=olduserx if usersprite
  12664.     usersprite.y=oldusery if usersprite
  12665.     targetsprite.ox=0 if targetsprite
  12666.     targetsprite.oy=0 if targetsprite
  12667.     targetsprite.x=oldtargetx if targetsprite
  12668.     targetsprite.y=oldtargety if targetsprite
  12669.     animplayer.dispose
  12670.   end
  12671.   def pbLevelUp(pokemon,battler,oldtotalhp,oldattack,olddefense,oldspeed,
  12672.                 oldspatk,oldspdef)
  12673.     pbTopRightWindow(_INTL("Max. HP<r>+{1}\r\nAttack<r>+{2}\r\nDefense<r>+{3}\r\nSp. Atk<r>+{4}\r\nSp. Def<r>+{5}\r\nSpeed<r>+{6}",
  12674.        pokemon.totalhp-oldtotalhp,
  12675.        pokemon.attack-oldattack,
  12676.        pokemon.defense-olddefense,
  12677.        pokemon.spatk-oldspatk,
  12678.        pokemon.spdef-oldspdef,
  12679.        pokemon.speed-oldspeed))
  12680.     pbTopRightWindow(_INTL("Max. HP<r>{1}\r\nAttack<r>{2}\r\nDefense<r>{3}\r\nSp. Atk<r>{4}\r\nSp. Def<r>{5}\r\nSpeed<r>{6}",
  12681.        pokemon.totalhp,pokemon.attack,pokemon.defense,pokemon.spatk,pokemon.spdef,pokemon.speed))
  12682.   end
  12683.   def pbThrowAndDeflect(ball,targetBattler)
  12684.     @briefmessage=false
  12685.     balltype=pbGetBallType(ball)
  12686.     ball=sprintf("Graphics/Pictures/Battle/ball_%02d",balltype)
  12687.     # sprite
  12688.     spriteBall=IconSprite.new(0,0,@viewport)
  12689.     spriteBall.visible=false
  12690.     # picture
  12691.     pictureBall=PictureEx.new(@sprites["pokemon#{targetBattler}"].z+1)
  12692.     center=getSpriteCenter(@sprites["pokemon#{targetBattler}"])
  12693.     # starting positions
  12694.     pictureBall.moveVisible(1,true)
  12695.     pictureBall.moveName(1,ball)
  12696.     pictureBall.moveOrigin(1,PictureOrigin::Center)
  12697.     pictureBall.moveXY(0,1,10,180)
  12698.     # directives
  12699.     pictureBall.moveSE(1,"Audio/SE/Battle throw")
  12700.     pictureBall.moveCurve(30,1,150,70,30+Graphics.width/2,10,center[0],center[1])
  12701.     pictureBall.moveAngle(30,1,-1080)
  12702.     pictureBall.moveAngle(0,pictureBall.totalDuration,0)
  12703.     delay=pictureBall.totalDuration
  12704.     pictureBall.moveSE(delay,"Audio/SE/Battle ball drop")
  12705.     pictureBall.moveXY(20,delay,0,Graphics.height)
  12706.     loop do
  12707.       pictureBall.update
  12708.       setPictureIconSprite(spriteBall,pictureBall)
  12709.       pbGraphicsUpdate
  12710.       pbInputUpdate
  12711.       pbFrameUpdate
  12712.       break if !pictureBall.running?
  12713.     end
  12714.     spriteBall.dispose
  12715.   end
  12716.   def pbThrow(ball,shakes,critical,targetBattler,showplayer=false)
  12717.     @briefmessage=false
  12718.     burst=-1
  12719.     animations=load_data("Data/PkmnAnimations.rxdata")
  12720.     for i in 0...2
  12721.       t=(i==0) ? ball : 0
  12722.       for j in 0...animations.length
  12723.         if animations[j]
  12724.           if animations[j].name=="Common:BallBurst#{t}"
  12725.             burst=t if burst<0
  12726.             break
  12727.           end
  12728.         end
  12729.       end
  12730.       break if burst>=0
  12731.     end
  12732.     pokeballThrow(ball,shakes,critical,targetBattler,self,@battle.battlers[targetBattler],burst,showplayer)
  12733.   end
  12734.   def pbThrowSuccess
  12735.     if !@battle.opponent
  12736.       @briefmessage=false
  12737.       pbMEPlay("Battle capture success")
  12738.       frames=(3.5*Graphics.frame_rate).to_i
  12739.       frames.times do
  12740.         pbGraphicsUpdate
  12741.         pbInputUpdate
  12742.         pbFrameUpdate
  12743.       end
  12744.     end
  12745.   end
  12746.   def pbHideCaptureBall
  12747.     if @sprites["capture"]
  12748.       loop do
  12749.         break if @sprites["capture"].opacity<=0
  12750.         @sprites["capture"].opacity-=12
  12751.         pbGraphicsUpdate
  12752.         pbInputUpdate
  12753.         pbFrameUpdate
  12754.       end
  12755.     end
  12756.   end
  12757.   def pbThrowBait
  12758.     @briefmessage=false
  12759.     ball=sprintf("Graphics/Pictures/Battle/object_bait")
  12760.     armanim=false
  12761.     if @sprites["player"].bitmap.width>@sprites["player"].bitmap.height
  12762.       armanim=true
  12763.     end
  12764.     # sprites
  12765.     spritePoke=@sprites["pokemon1"]
  12766.     spritePlayer=@sprites["player"]
  12767.     spriteBall=IconSprite.new(0,0,@viewport)
  12768.     spriteBall.visible=false
  12769.     # pictures
  12770.     pictureBall=PictureEx.new(spritePoke.z+1)
  12771.     picturePoke=PictureEx.new(spritePoke.z)
  12772.     picturePlayer=PictureEx.new(spritePoke.z+2)
  12773.     dims=[spritePoke.x,spritePoke.y]
  12774.     pokecenter=getSpriteCenter(@sprites["pokemon1"])
  12775.     playerpos=[@sprites["player"].x,@sprites["player"].y]
  12776.     ballendy=PokeBattle_SceneConstants::FOEBATTLER_Y-4
  12777.     # starting positions
  12778.     pictureBall.moveVisible(1,true)
  12779.     pictureBall.moveName(1,ball)
  12780.     pictureBall.moveOrigin(1,PictureOrigin::Center)
  12781.     pictureBall.moveXY(0,1,64,256)
  12782.     picturePoke.moveVisible(1,true)
  12783.     picturePoke.moveOrigin(1,PictureOrigin::Center)
  12784.     picturePoke.moveXY(0,1,pokecenter[0],pokecenter[1])
  12785.     picturePlayer.moveVisible(1,true)
  12786.     picturePlayer.moveName(1,@sprites["player"].name)
  12787.     picturePlayer.moveOrigin(1,PictureOrigin::TopLeft)
  12788.     picturePlayer.moveXY(0,1,playerpos[0],playerpos[1])
  12789.     # directives
  12790.     picturePoke.moveSE(1,"Audio/SE/Battle throw")
  12791.     pictureBall.moveCurve(30,1,64,256,Graphics.width/2,48,
  12792.                           PokeBattle_SceneConstants::FOEBATTLER_X-48,
  12793.                           PokeBattle_SceneConstants::FOEBATTLER_Y)
  12794.     pictureBall.moveAngle(30,1,-720)
  12795.     pictureBall.moveAngle(0,pictureBall.totalDuration,0)
  12796.     if armanim
  12797.       picturePlayer.moveSrc(1,@sprites["player"].bitmap.height,0)
  12798.       picturePlayer.moveXY(0,1,playerpos[0]-14,playerpos[1])
  12799.       picturePlayer.moveSrc(4,@sprites["player"].bitmap.height*2,0)
  12800.       picturePlayer.moveXY(0,4,playerpos[0]-12,playerpos[1])
  12801.       picturePlayer.moveSrc(8,@sprites["player"].bitmap.height*3,0)
  12802.       picturePlayer.moveXY(0,8,playerpos[0]+20,playerpos[1])
  12803.       picturePlayer.moveSrc(16,@sprites["player"].bitmap.height*4,0)
  12804.       picturePlayer.moveXY(0,16,playerpos[0]+16,playerpos[1])
  12805.       picturePlayer.moveSrc(40,0,0)
  12806.       picturePlayer.moveXY(0,40,playerpos[0],playerpos[1])
  12807.     end
  12808.     # Show Pokémon jumping before eating the bait
  12809.     picturePoke.moveSE(50,"Audio/SE/Player jump")
  12810.     picturePoke.moveXY(8,50,pokecenter[0],pokecenter[1]-8)
  12811.     picturePoke.moveXY(8,58,pokecenter[0],pokecenter[1])
  12812.     pictureBall.moveVisible(66,false)
  12813.     picturePoke.moveSE(66,"Audio/SE/Player jump")
  12814.     picturePoke.moveXY(8,66,pokecenter[0],pokecenter[1]-8)
  12815.     picturePoke.moveXY(8,74,pokecenter[0],pokecenter[1])
  12816.     # TODO: Show Pokémon eating the bait (pivots at the bottom right corner)
  12817.     picturePoke.moveOrigin(picturePoke.totalDuration,PictureOrigin::TopLeft)
  12818.     picturePoke.moveXY(0,picturePoke.totalDuration,dims[0],dims[1])
  12819.     loop do
  12820.       pictureBall.update
  12821.       picturePoke.update
  12822.       picturePlayer.update
  12823.       setPictureIconSprite(spriteBall,pictureBall)
  12824.       setPictureSprite(spritePoke,picturePoke)
  12825.       setPictureIconSprite(spritePlayer,picturePlayer)
  12826.       pbGraphicsUpdate
  12827.       pbInputUpdate
  12828.       pbFrameUpdate
  12829.       break if !pictureBall.running? && !picturePoke.running? && !picturePlayer.running?
  12830.     end
  12831.     spriteBall.dispose
  12832.   end
  12833.   def pbThrowRock
  12834.     @briefmessage=false
  12835.     ball=sprintf("Graphics/Pictures/Battle/object_rock")
  12836.     anger=sprintf("Graphics/Pictures/Battle/object_anger")
  12837.     armanim=false
  12838.     if @sprites["player"].bitmap.width>@sprites["player"].bitmap.height
  12839.       armanim=true
  12840.     end
  12841.     # sprites
  12842.     spritePoke=@sprites["pokemon1"]
  12843.     spritePlayer=@sprites["player"]
  12844.     spriteBall=IconSprite.new(0,0,@viewport)
  12845.     spriteBall.visible=false
  12846.     spriteAnger=IconSprite.new(0,0,@viewport)
  12847.     spriteAnger.visible=false
  12848.     # pictures
  12849.     pictureBall=PictureEx.new(spritePoke.z+1)
  12850.     picturePoke=PictureEx.new(spritePoke.z)
  12851.     picturePlayer=PictureEx.new(spritePoke.z+2)
  12852.     pictureAnger=PictureEx.new(spritePoke.z+1)
  12853.     dims=[spritePoke.x,spritePoke.y]
  12854.     pokecenter=getSpriteCenter(@sprites["pokemon1"])
  12855.     playerpos=[@sprites["player"].x,@sprites["player"].y]
  12856.     ballendy=PokeBattle_SceneConstants::FOEBATTLER_Y-4
  12857.     # starting positions
  12858.     pictureBall.moveVisible(1,true)
  12859.     pictureBall.moveName(1,ball)
  12860.     pictureBall.moveOrigin(1,PictureOrigin::Center)
  12861.     pictureBall.moveXY(0,1,64,256)
  12862.     picturePoke.moveVisible(1,true)
  12863.     picturePoke.moveOrigin(1,PictureOrigin::Center)
  12864.     picturePoke.moveXY(0,1,pokecenter[0],pokecenter[1])
  12865.     picturePlayer.moveVisible(1,true)
  12866.     picturePlayer.moveName(1,@sprites["player"].name)
  12867.     picturePlayer.moveOrigin(1,PictureOrigin::TopLeft)
  12868.     picturePlayer.moveXY(0,1,playerpos[0],playerpos[1])
  12869.     pictureAnger.moveVisible(1,false)
  12870.     pictureAnger.moveName(1,anger)
  12871.     pictureAnger.moveXY(0,1,pokecenter[0]-56,pokecenter[1]-48)
  12872.     pictureAnger.moveOrigin(1,PictureOrigin::Center)
  12873.     pictureAnger.moveZoom(0,1,100)
  12874.     # directives
  12875.     picturePoke.moveSE(1,"Audio/SE/Battle throw")
  12876.     pictureBall.moveCurve(30,1,64,256,Graphics.width/2,48,pokecenter[0],pokecenter[1])
  12877.     pictureBall.moveAngle(30,1,-720)
  12878.     pictureBall.moveAngle(0,pictureBall.totalDuration,0)
  12879.     pictureBall.moveSE(30,"Audio/SE/Battle damage weak")
  12880.     if armanim
  12881.       picturePlayer.moveSrc(1,@sprites["player"].bitmap.height,0)
  12882.       picturePlayer.moveXY(0,1,playerpos[0]-14,playerpos[1])
  12883.       picturePlayer.moveSrc(4,@sprites["player"].bitmap.height*2,0)
  12884.       picturePlayer.moveXY(0,4,playerpos[0]-12,playerpos[1])
  12885.       picturePlayer.moveSrc(8,@sprites["player"].bitmap.height*3,0)
  12886.       picturePlayer.moveXY(0,8,playerpos[0]+20,playerpos[1])
  12887.       picturePlayer.moveSrc(16,@sprites["player"].bitmap.height*4,0)
  12888.       picturePlayer.moveXY(0,16,playerpos[0]+16,playerpos[1])
  12889.       picturePlayer.moveSrc(40,0,0)
  12890.       picturePlayer.moveXY(0,40,playerpos[0],playerpos[1])
  12891.     end
  12892.     pictureBall.moveVisible(40,false)
  12893.     # Show Pokémon being angry
  12894.     pictureAnger.moveSE(48,"Audio/SE/Player jump")
  12895.     pictureAnger.moveVisible(48,true)
  12896.     pictureAnger.moveZoom(8,48,130)
  12897.     pictureAnger.moveZoom(8,56,100)
  12898.     pictureAnger.moveXY(0,64,pokecenter[0]+56,pokecenter[1]-64)
  12899.     pictureAnger.moveSE(64,"Audio/SE/Player jump")
  12900.     pictureAnger.moveZoom(8,64,130)
  12901.     pictureAnger.moveZoom(8,72,100)
  12902.     pictureAnger.moveVisible(80,false)
  12903.     picturePoke.moveOrigin(picturePoke.totalDuration,PictureOrigin::TopLeft)
  12904.     picturePoke.moveXY(0,picturePoke.totalDuration,dims[0],dims[1])
  12905.     loop do
  12906.       pictureBall.update
  12907.       picturePoke.update
  12908.       picturePlayer.update
  12909.       pictureAnger.update
  12910.       setPictureIconSprite(spriteBall,pictureBall)
  12911.       setPictureSprite(spritePoke,picturePoke)
  12912.       setPictureIconSprite(spritePlayer,picturePlayer)
  12913.       setPictureIconSprite(spriteAnger,pictureAnger)
  12914.       pbGraphicsUpdate
  12915.       pbInputUpdate
  12916.       pbFrameUpdate
  12917.       break if !pictureBall.running? && !picturePoke.running? &&
  12918.                !picturePlayer.running? && !pictureAnger.running?
  12919.     end
  12920.     spriteBall.dispose
  12921.   end
  12922. end
  12923.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement