Advertisement
Guest User

Sardines Pasting Level

a guest
Jan 31st, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 204.56 KB | None | 0 0
  1. # Results of battle:
  2. #    0 - Undecided or aborted
  3. #    1 - Player won
  4. #    2 - Player lost
  5. #    3 - Player or wild Pokémon ran from battle, or player forfeited the match
  6. #    4 - Wild Pokémon was caught
  7. #    5 - Draw
  8. ################################################################################
  9. # Battle Peer.
  10. ################################################################################
  11. class PokeBattle_NullBattlePeer
  12.   def pbStorePokemon(player,pokemon)
  13.     if player.party.length<6
  14.       player.party[player.party.length]=pokemon
  15.       return -1
  16.     else
  17.       return -1
  18.     end
  19.   end
  20.  
  21.   def pbOnEnteringBattle(battle,pokemon)
  22.   end
  23.  
  24.   def pbGetStorageCreator()
  25.     return nil
  26.   end
  27.  
  28.   def pbCurrentBox()
  29.     return -1
  30.   end
  31.  
  32.   def pbBoxName(box)
  33.     return ""
  34.   end
  35. end
  36.  
  37.  
  38.  
  39. class PokeBattle_BattlePeer
  40.   def self.create
  41.     return PokeBattle_NullBattlePeer.new()
  42.   end
  43. end
  44.  
  45.  
  46.  
  47. ################################################################################
  48. # Success State.
  49. ################################################################################
  50. class PokeBattle_SuccessState
  51.   attr_accessor :typemod
  52.   attr_accessor :useState # 0 - not used, 1 - failed, 2 - succeeded
  53.   attr_accessor :protected
  54.   attr_accessor :skill # Used in Battle Arena
  55.  
  56.   def initialize
  57.     clear
  58.   end
  59.  
  60.   def clear
  61.     @typemod=4
  62.     @useState=0
  63.     @protected=false
  64.     @skill=0
  65.   end
  66.  
  67.   def updateSkill
  68.     if @useState==1 && !@protected
  69.       @skill-=2
  70.     elsif @useState==2
  71.       if @typemod>4
  72.         @skill+=2 # "Super effective"
  73.       elsif @typemod>=1 && @typemod<4
  74.         @skill-=1 # "Not very effective"
  75.       elsif @typemod==0
  76.         @skill-=2 # Ineffective
  77.       else
  78.         @skill+=1
  79.       end
  80.     end
  81.     @typemod=4
  82.     @useState=0
  83.     @protected=false
  84.   end
  85. end
  86.  
  87.  
  88.  
  89. ################################################################################
  90. # Catching and storing Pokémon.
  91. ################################################################################
  92. module PokeBattle_BattleCommon
  93.   def pbStorePokemon(pokemon)
  94.     if !(pokemon.isShadow? rescue false)
  95.       if pbDisplayConfirm(_INTL("Would you like to give a nickname to {1}?",pokemon.name))
  96.         species=PBSpecies.getName(pokemon.species)
  97.         nickname=@scene.pbNameEntry(_INTL("{1}'s nickname?",species),pokemon)
  98.         pokemon.name=nickname if nickname!=""
  99.       end
  100.     end
  101.     oldcurbox=@peer.pbCurrentBox()
  102.     storedbox=@peer.pbStorePokemon(self.pbPlayer,pokemon)
  103.     creator=@peer.pbGetStorageCreator()
  104.     return if storedbox<0
  105.     curboxname=@peer.pbBoxName(oldcurbox)
  106.     boxname=@peer.pbBoxName(storedbox)
  107.     if storedbox!=oldcurbox
  108.       if creator
  109.         pbDisplayPaused(_INTL("Box \"{1}\" on {2}'s PC was full.",curboxname,creator))
  110.       else
  111.         pbDisplayPaused(_INTL("Box \"{1}\" on someone's PC was full.",curboxname))
  112.       end
  113.       pbDisplayPaused(_INTL("{1} was transferred to box \"{2}\".",pokemon.name,boxname))
  114.     else
  115.       if creator
  116.         pbDisplayPaused(_INTL("{1} was transferred to {2}'s PC.",pokemon.name,creator))
  117.       else
  118.         pbDisplayPaused(_INTL("{1} was transferred to someone's PC.",pokemon.name))
  119.       end
  120.       pbDisplayPaused(_INTL("It was stored in box \"{1}\".",boxname))
  121.     end
  122.   end
  123.  
  124.   def pbThrowPokeBall(idxPokemon,ball,rareness=nil,showplayer=false)
  125.     itemname=PBItems.getName(ball)
  126.     battler=nil
  127.     if pbIsOpposing?(idxPokemon)
  128.       battler=self.battlers[idxPokemon]
  129.     else
  130.       battler=self.battlers[idxPokemon].pbOppositeOpposing
  131.     end
  132.     if battler.isFainted?
  133.       battler=battler.pbPartner
  134.     end
  135.     pbDisplayBrief(_INTL("{1} threw one {2}!",self.pbPlayer.name,itemname))
  136.     if battler.isFainted?
  137.       pbDisplay(_INTL("But there was no target..."))
  138.       return
  139.     end
  140.     if @opponent && (!pbIsSnagBall?(ball) || !battler.isShadow?)
  141.       @scene.pbThrowAndDeflect(ball,1)
  142.       if $game_switches[290]==false
  143.         pbDisplay(_INTL("The Trainer blocked the Ball!\nDon't be a thief!"))
  144.       else
  145.         pbDisplay(_INTL("The Pokémon knocked the ball away!"))
  146.       end
  147.     else
  148.       if $game_switches[290]==true || $game_variables[975]!=0
  149.         pbDisplay(_INTL("The Pokémon knocked the ball away!"))
  150.         return
  151.       end
  152.       pokemon=battler.pokemon
  153.       species=pokemon.species
  154.       if $DEBUG && Input.press?(Input::CTRL)
  155.         shakes=4
  156.       else
  157.         if !rareness
  158.           dexdata=pbOpenDexData
  159.           pbDexDataOffset(dexdata,species,16)
  160.           rareness=dexdata.fgetb # Get rareness from dexdata file
  161.           dexdata.close
  162.         end
  163.         a=battler.totalhp
  164.         b=battler.hp
  165.         rareness=BallHandlers.modifyCatchRate(ball,rareness,self,battler)
  166.         x=(((a*3-b*2)*rareness)/(a*3)).floor
  167.         if battler.status==PBStatuses::SLEEP || battler.status==PBStatuses::FROZEN
  168.           x=(x*2.5).floor
  169.         elsif battler.status!=0
  170.           x=(x*3/2).floor
  171.         end
  172.         #Critical Capture chances based on caught species'
  173.         c=0
  174.         if $Trainer
  175.           if $Trainer.pokedexOwned>600
  176.             c=(x*2.5/6).floor
  177.           elsif $Trainer.pokedexOwned>450
  178.             c=(x*2/6).floor
  179.           elsif $Trainer.pokedexOwned>300
  180.             c=(x*1.5/6).floor
  181.           elsif $Trainer.pokedexOwned>150
  182.             c=(x*1/6).floor
  183.           elsif $Trainer.pokedexOwned>30
  184.             c=(x*0.5/6).floor
  185.           end
  186.         end
  187.         shakes=0; critical=false; critsuccess=false
  188.         if x>255 || BallHandlers.isUnconditional?(ball,self,battler)
  189.           shakes=4
  190.         else
  191.           x=1 if x==0
  192.            y = ( 65536 / ((255.0/x)**0.1875) ).floor
  193.           if pbRandom(256)<c
  194.             critical=true
  195.             if pbRandom(65536)<y
  196.               critsuccess=true
  197.               shakes=4
  198.             end            
  199.           else          
  200.             shakes+=1 if pbRandom(65536)<y
  201.             shakes+=1 if pbRandom(65536)<y
  202.             shakes+=1 if pbRandom(65536)<y
  203.             shakes+=1 if pbRandom(65536)<y
  204.           end        
  205.         end
  206.       end
  207.        @scene.pbThrow(ball,(critical) ? 1 : shakes,critical,critsuccess,battler.index,showplayer)
  208.       case shakes
  209.         when 0
  210.           pbDisplay(_INTL("Oh no!  The Pokémon broke free!"))
  211.           BallHandlers.onFailCatch(ball,self,pokemon)
  212.         when 1
  213.           pbDisplay(_INTL("Aww... It appeared to be caught!"))
  214.           BallHandlers.onFailCatch(ball,self,pokemon)
  215.         when 2
  216.           pbDisplay(_INTL("Aargh!  Almost had it!"))
  217.           BallHandlers.onFailCatch(ball,self,pokemon)
  218.         when 3
  219.           pbDisplay(_INTL("Shoot!  It was so close, too!"))
  220.           BallHandlers.onFailCatch(ball,self,pokemon)
  221.         when 4
  222.           pbDisplayBrief(_INTL("Gotcha!  {1} was caught!",pokemon.name))
  223.           @scene.pbThrowSuccess
  224.           if pbIsSnagBall?(ball) && @opponent
  225.             pbRemoveFromParty(battler.index,battler.pokemonIndex)
  226.             battler.pbReset
  227.             battler.participants=[]
  228.           else
  229.             @decision=4
  230.           end
  231.           if pbIsSnagBall?(ball)
  232.             pokemon.ot=self.pbPlayer.name
  233.             pokemon.trainerID=self.pbPlayer.id
  234.           end
  235.           BallHandlers.onCatch(ball,self,pokemon)
  236.           pokemon.ballused=pbGetBallType(ball)
  237.           pokemon.pbRecordFirstMoves
  238.           if !self.pbPlayer.owned[species]
  239.             self.pbPlayer.owned[species]=true
  240.             if $Trainer.pokedex
  241.               pbDisplayPaused(_INTL("{1}'s data was added to the Pokédex.",pokemon.name))
  242.               @scene.pbShowPokedex(species)
  243.             end
  244.           end
  245.           @scene.pbHideCaptureBall
  246.           if pbIsSnagBall?(ball) && @opponent
  247.             pokemon.pbUpdateShadowMoves rescue nil
  248.             @snaggedpokemon.push(pokemon)
  249.           else
  250.             pbStorePokemon(pokemon)
  251.           end
  252.       end
  253.     end
  254.   end
  255. end
  256.  
  257.  
  258.  
  259. ################################################################################
  260. # Main battle class.
  261. ################################################################################
  262. class PokeBattle_Battle
  263.   attr_reader(:scene)             # Scene object for this battle
  264.   attr_accessor(:decision)        # Decision: 0=undecided; 1=win; 2=loss; 3=escaped; 4=caught
  265.   attr_accessor(:internalbattle)  # Internal battle flag
  266.   attr_accessor(:doublebattle)    # Double battle flag
  267.   attr_accessor(:cantescape)      # True if player can't escape
  268.   attr_accessor(:shiftStyle)      # Shift/Set "battle style" option
  269.   attr_accessor(:battlescene)     # "Battle scene" option
  270.   attr_accessor(:debug)           # Debug flag
  271.   attr_reader(:player)            # Player trainer
  272.   attr_reader(:opponent)          # Opponent trainer
  273.   attr_reader(:party1)            # Player's Pokémon party
  274.   attr_reader(:party2)            # Foe's Pokémon party
  275.   attr_reader(:partyorder)        # Order of Pokémon in the player's party
  276.   attr_accessor(:fullparty1)      # True if player's party's max size is 6 instead of 3
  277.   attr_accessor(:fullparty2)      # True if opponent's party's max size is 6 instead of 3
  278.   attr_reader(:battlers)          # Currently active Pokémon
  279.   attr_accessor(:items)           # Items held by opponents
  280.   attr_reader(:sides)             # Effects common to each side of a battle
  281.   attr_reader(:field)             # Effects common to the whole of a battle
  282.   attr_accessor(:environment)     # Battle surroundings
  283.   attr_accessor(:weather)         # Current weather, custom methods should use pbWeather instead
  284.   attr_accessor(:weatherduration) # Duration of current weather, or -1 if indefinite
  285.   attr_reader(:switching)         # True if during the switching phase of the round
  286.   attr_reader(:faintswitch)       # True if switching after a faint during endphase  
  287.   attr_reader(:struggle)          # The Struggle move
  288.   attr_accessor(:choices)         # Choices made by each Pokémon this round
  289.   attr_reader(:successStates)     # Success states
  290.   attr_accessor(:lastMoveUsed)    # Last move used
  291.   attr_accessor(:lastMoveUser)    # Last move user
  292.   attr_accessor(:synchronize)     # Synchronize state
  293.   attr_accessor(:megaEvolution)   # Battle index of each trainer's Pokémon to Mega Evolve
  294.   attr_accessor(:amuletcoin)      # Whether Amulet Coin's effect applies
  295.   attr_accessor(:extramoney)      # Money gained in battle by using Pay Day
  296.   attr_accessor(:endspeech)       # Speech by opponent when player wins
  297.   attr_accessor(:endspeech2)      # Speech by opponent when player wins
  298.   attr_accessor(:endspeechwin)    # Speech by opponent when opponent wins
  299.   attr_accessor(:endspeechwin2)   # Speech by opponent when opponent wins
  300.   attr_accessor(:trickroom)
  301. #### KUROTSUNE - 015 - START  
  302.   attr_accessor(:switchedOut)
  303. #### KUROTSUNE - 015 - END  
  304.   attr_accessor(:previousMove)    # Move used directly previously
  305.   attr_accessor(:rules)
  306.   attr_reader(:turncount)
  307.   attr_accessor :controlPlayer
  308.   include PokeBattle_BattleCommon
  309.  
  310.   MAXPARTYSIZE = 6
  311.  
  312.   class BattleAbortedException < Exception; end
  313.  
  314.   def pbAbort
  315.     raise BattleAbortedException.new("Battle aborted")
  316.   end
  317.  
  318.   def pbDebugUpdate
  319.   end
  320.  
  321.   def pbRandom(x)
  322.     return rand(x)
  323.   end
  324.  
  325.   def pbAIRandom(x)
  326.     return rand(x)
  327.   end
  328.  
  329.   def isOnline?
  330.     return false
  331.   end  
  332. ################################################################################
  333. # Initialise battle class.
  334. ################################################################################
  335.   def initialize(scene,p1,p2,player,opponent)
  336.     if p1.length==0
  337.       raise ArgumentError.new(_INTL("Party 1 has no Pokémon."))
  338.       return
  339.     end
  340.     if p2.length==0
  341.       raise ArgumentError.new(_INTL("Party 2 has no Pokémon."))
  342.       return
  343.     end
  344.     if p2.length>2 && !opponent
  345.       raise ArgumentError.new(_INTL("Wild battles with more than two Pokémon are not allowed."))
  346.       return
  347.     end
  348.     @scene           = scene
  349.     @decision        = 0
  350.     @internalbattle  = true
  351.     @doublebattle    = false
  352.     @cantescape      = false
  353.     @shiftStyle      = true
  354.     @battlescene     = true
  355.     @debug           = false
  356.     @debugupdate     = 0
  357.     if opponent && player.is_a?(Array) && player.length==0
  358.       player = player[0]
  359.     end
  360.     if opponent && opponent.is_a?(Array) && opponent.length==0
  361.       opponent = opponent[0]
  362.     end
  363.     @player          = player                # PokeBattle_Trainer object
  364.     @opponent        = opponent              # PokeBattle_Trainer object
  365.     @party1          = p1
  366.     @party2          = p2
  367.     @partyorder      = []
  368.     for i in 0...6; @partyorder.push(i); end
  369.     @fullparty1      = false
  370.     @fullparty2      = false
  371.     @battlers        = []
  372.     @items           = nil
  373.     @sides           = [PokeBattle_ActiveSide.new,   # Player's side
  374.                         PokeBattle_ActiveSide.new]   # Foe's side
  375.     @field           = PokeBattle_ActiveField.new    # Whole field (gravity/rooms)
  376.     @environment     = PBEnvironment::None   # e.g. Tall grass, cave, still water
  377.     @weather         = 0
  378.     @weatherduration = 0
  379.     @switching       = false
  380.     @faintswitch     = false
  381.     @choices         = [ [0,0,nil,-1],[0,0,nil,-1],[0,0,nil,-1],[0,0,nil,-1] ]
  382.     @successStates   = []
  383.     for i in 0...4
  384.       @successStates.push(PokeBattle_SuccessState.new)
  385.     end
  386.     @lastMoveUsed    = -1
  387.     @lastMoveUser    = -1
  388.     @synchronize     = [-1,-1,0]
  389.     @megaEvolution   = []
  390.     if @player.is_a?(Array)
  391.       @megaEvolution[0]=[-1]*@player.length
  392.     else
  393.       @megaEvolution[0]=[-1]
  394.     end
  395.     if @opponent.is_a?(Array)
  396.       @megaEvolution[1]=[-1]*@opponent.length
  397.     else
  398.       @megaEvolution[1]=[-1]
  399.     end
  400.     @amuletcoin      = false
  401. #### KUROTSUNE - 015 - START
  402.     @switchedOut     = []
  403. #### KUROTSUNE - 015 - END    
  404.     @extramoney      = 0
  405.     @endspeech       = ""
  406.     @endspeech2      = ""
  407.     @endspeechwin    = ""
  408.     @endspeechwin2   = ""
  409.     @rules           = {}
  410.     @turncount       = 0
  411.     @peer            = PokeBattle_BattlePeer.create()
  412.     @trickroom       = 0
  413.     @priority        = []
  414.     @usepriority     = false
  415.     @snaggedpokemon  = []
  416.     @runCommand      = 0
  417.     if hasConst?(PBMoves,:STRUGGLE)
  418.       @struggle = PokeBattle_Move.pbFromPBMove(self,PBMove.new(getConst(PBMoves,:STRUGGLE)))
  419.     else
  420.       @struggle = PokeBattle_Struggle.new(self,nil)
  421.     end
  422.     @struggle.pp     = -1
  423.     for i in 0...4
  424.       battlers[i] = PokeBattle_Battler.new(self,i)
  425.     end
  426.     for i in @party1
  427.       next if !i
  428.       i.itemRecycle = 0
  429.       i.itemInitial = i.item
  430.     end
  431.     for i in @party2
  432.       next if !i
  433.       i.itemRecycle = 0
  434.       i.itemInitial = i.item
  435.     end
  436.   end
  437.  
  438. ################################################################################
  439. # Info about battle.
  440. ################################################################################
  441.   def pbIsWild?
  442.     return !@opponent? true : false
  443.   end
  444.  
  445.   def pbDoubleBattleAllowed?
  446.     return true
  447.   end
  448.  
  449.   def pbCheckSideAbility(a,pkmn) #checks to see if your side has a pokemon with a certain ability.
  450.     for i in 0...4 # in order from own first, opposing first, own second, opposing second
  451.       if @battlers[i].hasWorkingAbility(a)
  452.         if @battlers[i]==pkmn || @battlers[i]==pkmn.pbPartner
  453.           return @battlers[i]
  454.         end
  455.       end
  456.     end
  457.     return nil
  458.   end
  459.  
  460.   def pbWeather
  461.     for i in 0...4
  462.       if @battlers[i].hasWorkingAbility(:CLOUDNINE) ||
  463.          @battlers[i].hasWorkingAbility(:AIRLOCK) ||
  464.          $fefieldeffect == 22
  465.         return 0
  466.       end
  467.     end
  468.     return @weather
  469.   end
  470.  
  471. ################################################################################
  472. # Get battler info.
  473. ################################################################################
  474.   def pbIsOpposing?(index)
  475.     return (index%2)==1
  476.   end
  477.  
  478.   def pbOwnedByPlayer?(index)
  479.     return false if pbIsOpposing?(index)
  480.     return false if @player.is_a?(Array) && index==2
  481.     return true
  482.   end
  483.  
  484.   def pbIsDoubleBattler?(index)
  485.     return (index>=2)
  486.   end
  487.  
  488.   def pbThisEx(battlerindex,pokemonindex)
  489.     party=pbParty(battlerindex)
  490.     if pbIsOpposing?(battlerindex)
  491.       if @opponent
  492.         return _INTL("The foe {1}",party[pokemonindex].name)
  493.       else
  494.         return _INTL("The wild {1}",party[pokemonindex].name)
  495.       end
  496.     else
  497.       return _INTL("{1}",party[pokemonindex].name)
  498.     end
  499.   end
  500.  
  501. # Checks whether an item can be removed from a Pokémon.
  502.   def pbIsUnlosableItem(pkmn,item)
  503.     return true if pbIsMail?(item)
  504.     return false if pkmn.effects[PBEffects::Transform]
  505.     if isConst?(pkmn.ability,PBAbilities,:MULTITYPE) &&
  506.        (isConst?(item,PBItems,:FISTPLATE) ||
  507.         isConst?(item,PBItems,:SKYPLATE) ||
  508.         isConst?(item,PBItems,:TOXICPLATE) ||
  509.         isConst?(item,PBItems,:EARTHPLATE) ||
  510.         isConst?(item,PBItems,:STONEPLATE) ||
  511.         isConst?(item,PBItems,:INSECTPLATE) ||
  512.         isConst?(item,PBItems,:SPOOKYPLATE) ||
  513.         isConst?(item,PBItems,:IRONPLATE) ||
  514.         isConst?(item,PBItems,:FLAMEPLATE) ||
  515.         isConst?(item,PBItems,:SPLASHPLATE) ||
  516.         isConst?(item,PBItems,:MEADOWPLATE) ||
  517.         isConst?(item,PBItems,:ZAPPLATE) ||
  518.         isConst?(item,PBItems,:MINDPLATE) ||
  519.         isConst?(item,PBItems,:ICICLEPLATE) ||
  520.         isConst?(item,PBItems,:DRACOPLATE) ||
  521.         isConst?(item,PBItems,:PIXIEPLATE) ||
  522.         isConst?(item,PBItems,:DREADPLATE))
  523.       return true
  524.     end
  525.     if isConst?(pkmn.species,PBSpecies,:GIRATINA) &&
  526.        isConst?(item,PBItems,:GRISEOUSORB)
  527.       return true
  528.     end
  529.     if isConst?(pkmn.species,PBSpecies,:GENESECT) &&
  530.        (isConst?(item,PBItems,:SHOCKDRIVE) ||
  531.         isConst?(item,PBItems,:BURNDRIVE) ||
  532.         isConst?(item,PBItems,:CHILLDRIVE) ||
  533.         isConst?(item,PBItems,:DOUSEDRIVE))
  534.       return true
  535.     end
  536.     if isConst?(pkmn.species,PBSpecies,:VENUSAUR) &&
  537.        isConst?(item,PBItems,:VENUSAURITE)
  538.       return true
  539.     end
  540.     if isConst?(pkmn.species,PBSpecies,:CHARIZARD) &&
  541.        (isConst?(item,PBItems,:CHARIZARDITEX) ||
  542.         isConst?(item,PBItems,:CHARIZARDITEY))
  543.       return true
  544.     end
  545.     if isConst?(pkmn.species,PBSpecies,:BLASTOISE) &&
  546.        isConst?(item,PBItems,:BLASTOISINITE)
  547.       return true
  548.     end
  549.     if isConst?(pkmn.species,PBSpecies,:ABOMBASNOW) &&
  550.        isConst?(item,PBItems,:ABOMASITE)
  551.       return true
  552.     end
  553.     if isConst?(pkmn.species,PBSpecies,:ABSOL) &&
  554.        isConst?(item,PBItems,:ABSOLITE)
  555.       return true
  556.     end
  557.     if isConst?(pkmn.species,PBSpecies,:AERODACTYL) &&
  558.        isConst?(item,PBItems,:AERODACTYLITE)
  559.       return true
  560.     end
  561.     if isConst?(pkmn.species,PBSpecies,:AGGRON) &&
  562.        isConst?(item,PBItems,:AGGRONITE)
  563.       return true
  564.     end
  565.     if isConst?(pkmn.species,PBSpecies,:ALAKAZAM) &&
  566.        isConst?(item,PBItems,:ALAKAZITE)
  567.       return true
  568.     end
  569.     if isConst?(pkmn.species,PBSpecies,:AMPHAROS) &&
  570.        isConst?(item,PBItems,:AMPHAROSITE)
  571.       return true
  572.     end
  573.     if isConst?(pkmn.species,PBSpecies,:BANETTE) &&
  574.        isConst?(item,PBItems,:BANETTITITE)
  575.       return true
  576.     end
  577.     if isConst?(pkmn.species,PBSpecies,:BLAZIKEN) &&
  578.        isConst?(item,PBItems,:BLAZIKENITE)
  579.       return true
  580.     end
  581.     if isConst?(pkmn.species,PBSpecies,:GARCHOMP) &&
  582.        isConst?(item,PBItems,:GARCHOMPITE)
  583.       return true
  584.     end
  585.     if isConst?(pkmn.species,PBSpecies,:GARDEVOIR) &&
  586.        isConst?(item,PBItems,:GARDEVOIRITE)
  587.       return true
  588.     end
  589.     if isConst?(pkmn.species,PBSpecies,:GENGAR) &&
  590.        isConst?(item,PBItems,:GENGARITE)
  591.       return true
  592.     end
  593.     if isConst?(pkmn.species,PBSpecies,:GYARADOS) &&
  594.        isConst?(item,PBItems,:GYARADOSITE)
  595.       return true
  596.     end
  597.     if isConst?(pkmn.species,PBSpecies,:HERACROSS) &&
  598.        isConst?(item,PBItems,:HERACROSSITE)
  599.       return true
  600.     end
  601.     if isConst?(pkmn.species,PBSpecies,:HOUNDOOM) &&
  602.        isConst?(item,PBItems,:HOUNDOOMITE)
  603.       return true
  604.     end
  605.     if isConst?(pkmn.species,PBSpecies,:KANGASKHAN) &&
  606.        isConst?(item,PBItems,:KANGASKHANITE)
  607.       return true
  608.     end
  609.     if isConst?(pkmn.species,PBSpecies,:LUCARIO) &&
  610.        isConst?(item,PBItems,:LUCARIONITE)
  611.       return true
  612.     end
  613.     if isConst?(pkmn.species,PBSpecies,:MANECTRIC) &&
  614.        isConst?(item,PBItems,:MANECTITE)
  615.       return true
  616.     end
  617.     if isConst?(pkmn.species,PBSpecies,:MAWILE) &&
  618.        isConst?(item,PBItems,:MAWILITE)
  619.       return true
  620.     end
  621.     if isConst?(pkmn.species,PBSpecies,:MEDICHAM) &&
  622.        isConst?(item,PBItems,:MEDICHAMITE)
  623.       return true
  624.     end
  625.     if isConst?(pkmn.species,PBSpecies,:MEWTWO) &&
  626.        (isConst?(item,PBItems,:MEWTWONITEX) ||
  627.         isConst?(item,PBItems,:MEWTWONITEY))
  628.       return true
  629.     end
  630.     if isConst?(pkmn.species,PBSpecies,:PINSIR) &&
  631.        isConst?(item,PBItems,:PINSIRITE)
  632.       return true
  633.     end
  634.     if isConst?(pkmn.species,PBSpecies,:SCIZOR) &&
  635.        isConst?(item,PBItems,:SCIZORITE)
  636.       return true
  637.     end
  638.     if isConst?(pkmn.species,PBSpecies,:TYRANITAR) &&
  639.        isConst?(item,PBItems,:TYRANITARITE)
  640.       return true
  641.     end
  642.  if isConst?(pkmn.species,PBSpecies,:BEEDRILL) &&
  643.        isConst?(item,PBItems,:BEEDRILLITE)
  644.       return true
  645.     end
  646.     if isConst?(pkmn.species,PBSpecies,:PIDGEOT) &&
  647.        isConst?(item,PBItems,:PIDGEOTITE)
  648.       return true
  649.     end
  650.     if isConst?(pkmn.species,PBSpecies,:SLOWBRO) &&
  651.        isConst?(item,PBItems,:SLOWBRONITE)
  652.       return true
  653.     end
  654.     if isConst?(pkmn.species,PBSpecies,:STEELIX) &&
  655.        isConst?(item,PBItems,:STEELIXITE)
  656.       return true
  657.     end
  658.     if isConst?(pkmn.species,PBSpecies,:SCEPTILE) &&
  659.        isConst?(item,PBItems,:SCEPTILITE)
  660.       return true
  661.     end
  662.     if isConst?(pkmn.species,PBSpecies,:SWAMPERT) &&
  663.        isConst?(item,PBItems,:SWAMPERTITE)
  664.       return true
  665.     end
  666.     if isConst?(pkmn.species,PBSpecies,:SHARPEDO) &&
  667.        isConst?(item,PBItems,:SHARPEDONITE)
  668.       return true
  669.     end
  670.     if isConst?(pkmn.species,PBSpecies,:SABLEYE) &&
  671.        isConst?(item,PBItems,:SABLENITE)
  672.       return true
  673.     end
  674.     if isConst?(pkmn.species,PBSpecies,:CAMERUPT) &&
  675.        isConst?(item,PBItems,:CAMERUPTITE)
  676.       return true
  677.     end
  678.     if isConst?(pkmn.species,PBSpecies,:ALTARIA) &&
  679.        isConst?(item,PBItems,:ALTARIANITE)
  680.       return true
  681.     end
  682.     if isConst?(pkmn.species,PBSpecies,:GLALIE) &&
  683.        isConst?(item,PBItems,:GLALITITE)
  684.       return true
  685.     end
  686.     if isConst?(pkmn.species,PBSpecies,:SALAMENCE) &&
  687.        isConst?(item,PBItems,:SALAMENCITE)
  688.       return true
  689.     end
  690.     if isConst?(pkmn.species,PBSpecies,:METAGROSS) &&
  691.        isConst?(item,PBItems,:METAGROSSITE)
  692.       return true
  693.     end
  694.     if isConst?(pkmn.species,PBSpecies,:LOPUNNY) &&
  695.        isConst?(item,PBItems,:LOPUNNITE)
  696.       return true
  697.     end
  698.     if isConst?(pkmn.species,PBSpecies,:GALLADE) &&
  699.        isConst?(item,PBItems,:GALLADITE)
  700.       return true
  701.     end
  702.     if isConst?(pkmn.species,PBSpecies,:AUDINO) &&
  703.        isConst?(item,PBItems,:AUDINITE)
  704.       return true
  705.     end
  706.     if isConst?(pkmn.species,PBSpecies,:DIANCIE) &&
  707.        isConst?(item,PBItems,:DIANCITE)
  708.       return true
  709.     end
  710.  if isConst?(pkmn.species,PBSpecies,:GROUDON) &&
  711.        isConst?(item,PBItems,:REDORB)
  712.       return true
  713.     end
  714.     if isConst?(pkmn.species,PBSpecies,:KYOGRE) &&
  715.        isConst?(item,PBItems,:BLUEORB)
  716.       return true
  717.     end
  718.         if isConst?(item,PBItems,:PULSEHOLD)
  719.       return true
  720.     end
  721.     return false
  722.   end
  723.  
  724.  
  725.   def pbCheckGlobalAbility(a)
  726.     for i in 0...4 # in order from own first, opposing first, own second, opposing second
  727.       if @battlers[i].hasWorkingAbility(a)
  728.         return @battlers[i]
  729.       end
  730.     end
  731.     return nil
  732.   end
  733.  
  734. ################################################################################
  735. # Player-related info.
  736. ################################################################################
  737.   def pbPlayer
  738.     if @player.is_a?(Array)
  739.       return @player[0]
  740.     else
  741.       return @player
  742.     end
  743.   end
  744.  
  745.   def pbGetOwnerItems(battlerIndex)
  746.     return [] if !@items
  747.     if pbIsOpposing?(battlerIndex)
  748.       if @opponent.is_a?(Array)
  749.         return (battlerIndex==1) ? @items[0] : @items[1]
  750.       else
  751.         return @items
  752.       end
  753.     else
  754.       return []
  755.     end
  756.   end
  757.  
  758.   def pbSetSeen(pokemon)
  759.     if pokemon && @internalbattle
  760.       self.pbPlayer.seen[pokemon.species]=true
  761.       pbSeenForm(pokemon)
  762.     end
  763.   end
  764.  
  765.    def pbGetMegaRingName(battlerIndex)
  766.     if pbBelongsToPlayer?(battlerIndex)
  767.       ringsA=[:MEGARING,:MEGABRACELET,:MEGACUFF,:MEGACHARM]
  768.       ringsB=[566]                                          # 566 = Mega Ring.
  769.       for i in ringsA
  770.         next if !hasConst?(PBItems,i)
  771.         for k in ringsB
  772.           return PBItems.getName(k) if $PokemonBag.pbQuantity(k)>0
  773.         end        
  774.       end
  775.     end
  776.     # Add your own Mega objects for particular trainer types here
  777. #    if isConst?(pbGetOwner(battlerIndex).trainertype,PBTrainers,:BUGCATCHER)
  778. #      return _INTL("Mega Net")
  779. #    end
  780.     return _INTL("Mega Ring")
  781.   end  
  782.  
  783.   def pbHasMegaRing(battlerIndex)
  784.     return true if !pbBelongsToPlayer?(battlerIndex)
  785.     rings=[:MEGARING,:MEGABRACELET,:MEGACUFF,:MEGACHARM]
  786.     for i in rings
  787.       next if !hasConst?(PBItems,i)
  788.       return true if $PokemonBag.pbQuantity(i)>0
  789.     end
  790.     return false
  791.   end  
  792.  
  793. ################################################################################
  794. # Get party info, manipulate parties.
  795. ################################################################################
  796.   def pbPokemonCount(party)
  797.     count=0
  798.     for i in party
  799.       next if !i
  800.       count+=1 if i.hp>0 && !i.isEgg?
  801.     end
  802.     return count
  803.   end
  804.  
  805.   def pbAllFainted?(party)
  806.     pbPokemonCount(party)==0
  807.   end
  808.  
  809.   def pbMaxLevelFromIndex(index)
  810.     party=pbParty(index)
  811.     owner=(pbIsOpposing?(index)) ? @opponent : @player
  812.     maxlevel=0
  813.     if owner.is_a?(Array)
  814.       start=0
  815.       limit=pbSecondPartyBegin(index)
  816.       start=limit if pbIsDoubleBattler?(index)
  817.       for i in start...start+limit
  818.         next if !party[i]
  819.         maxlevel=party[i].level if maxlevel<party[i].level
  820.       end
  821.     else
  822.       for i in party
  823.         next if !i
  824.         maxlevel=i.level if maxlevel<i.level
  825.       end
  826.     end
  827.     return maxlevel
  828.   end    
  829.  
  830.   def pbMaxLevel(party)
  831.     lv=0
  832.     for i in party
  833.       next if !i
  834.       lv=i.level if lv<i.level
  835.     end
  836.     return lv
  837.   end
  838.  
  839.   def pbParty(index)
  840.     return pbIsOpposing?(index) ? party2 : party1
  841.   end
  842.  
  843.   def pbSecondPartyBegin(battlerIndex)
  844.     if pbIsOpposing?(battlerIndex)
  845.       return @fullparty2 ? 6 : 3
  846.     else
  847.       return @fullparty1 ? 6 : 3
  848.     end
  849.   end
  850.  
  851.   def pbFindNextUnfainted(party,start,finish=-1)
  852.     finish=party.length if finish<0
  853.     for i in start...finish
  854.       next if !party[i]
  855.       return i if party[i].hp>0 && !party[i].isEgg?
  856.     end
  857.     return -1
  858.   end
  859.  
  860.   def pbFindPlayerBattler(pkmnIndex)
  861.     battler=nil
  862.     for k in 0...4
  863.       if !pbIsOpposing?(k) && @battlers[k].pokemonIndex==pkmnIndex
  864.         battler=@battlers[k]
  865.         break
  866.       end
  867.     end
  868.     return battler
  869.   end
  870.  
  871.   def pbIsOwner?(battlerIndex,partyIndex)
  872.     secondParty=pbSecondPartyBegin(battlerIndex)
  873.     if !pbIsOpposing?(battlerIndex)
  874.       return true if !@player || !@player.is_a?(Array)
  875.       return (battlerIndex==0) ? partyIndex<secondParty : partyIndex>=secondParty
  876.     else
  877.       return true if !@opponent || !@opponent.is_a?(Array)
  878.       return (battlerIndex==1) ? partyIndex<secondParty : partyIndex>=secondParty
  879.     end
  880.   end
  881.  
  882.   def pbGetOwner(battlerIndex)
  883.     if pbIsOpposing?(battlerIndex)
  884.       if @opponent.is_a?(Array)
  885.         return (battlerIndex==1) ? @opponent[0] : @opponent[1]
  886.       else
  887.         return @opponent
  888.       end
  889.     else
  890.       if @player.is_a?(Array)
  891.         return (battlerIndex==0) ? @player[0] : @player[1]
  892.       else
  893.         return @player
  894.       end
  895.     end
  896.   end
  897.  
  898.   def pbGetOwnerPartner(battlerIndex)
  899.     if pbIsOpposing?(battlerIndex)
  900.       if @opponent.is_a?(Array)
  901.         return (battlerIndex==1) ? @opponent[1] : @opponent[0]
  902.       else
  903.         return @opponent
  904.       end
  905.     else
  906.       if @player.is_a?(Array)
  907.         return (battlerIndex==0) ? @player[1] : @player[0]
  908.       else
  909.         return @player
  910.       end
  911.     end
  912.   end
  913.  
  914.   def pbGetOwnerIndex(battlerIndex)
  915.     if pbIsOpposing?(battlerIndex)
  916.       return (@opponent.is_a?(Array)) ? ((battlerIndex==1) ? 0 : 1) : 0
  917.     else
  918.       return (@player.is_a?(Array)) ? ((battlerIndex==0) ? 0 : 1) : 0
  919.     end
  920.   end
  921.  
  922.   def pbBelongsToPlayer?(battlerIndex)
  923.     if @player.is_a?(Array) && @player.length>1
  924.       return battlerIndex==0
  925.     else
  926.       return (battlerIndex%2)==0
  927.     end
  928.     return false
  929.   end
  930.  
  931.   def pbPartyGetOwner(battlerIndex,partyIndex)
  932.     secondParty=pbSecondPartyBegin(battlerIndex)
  933.     if !pbIsOpposing?(battlerIndex)
  934.       return @player if !@player || !@player.is_a?(Array)
  935.       return (partyIndex<secondParty) ? @player[0] : @player[1]
  936.     else
  937.       return @opponent if !@opponent || !@opponent.is_a?(Array)
  938.       return (partyIndex<secondParty) ? @opponent[0] : @opponent[1]
  939.     end
  940.   end
  941.  
  942.   def pbAddToPlayerParty(pokemon)
  943.     party=pbParty(0)
  944.     for i in 0...party.length
  945.       party[i]=pokemon if pbIsOwner?(0,i) && !party[i]
  946.     end
  947.   end
  948.  
  949.   def pbRemoveFromParty(battlerIndex,partyIndex)
  950.     party=pbParty(battlerIndex)
  951.     side=(pbIsOpposing?(battlerIndex)) ? @opponent : @player
  952.     party[partyIndex]=nil
  953.     if !side || !side.is_a?(Array) # Wild or single opponent
  954.       party.compact!
  955.       for i in battlerIndex...party.length
  956.         for j in 0..3
  957.           next if !@battlers[j]
  958.           if pbGetOwner(j)==side && @battlers[j].pokemonIndex==i
  959.             @battlers[j].pokemonIndex-=1
  960.             break
  961.           end
  962.         end
  963.       end
  964.     else
  965.       if battlerIndex<pbSecondPartyBegin(battlerIndex)-1
  966.         for i in battlerIndex...pbSecondPartyBegin(battlerIndex)
  967.           if i>=pbSecondPartyBegin(battlerIndex)-1
  968.             party[i]=nil
  969.           else
  970.             party[i]=party[i+1]
  971.           end
  972.         end
  973.       else
  974.         for i in battlerIndex...party.length
  975.           if i>=party.length-1
  976.             party[i]=nil
  977.           else
  978.             party[i]=party[i+1]
  979.           end
  980.         end
  981.       end
  982.     end
  983.   end
  984.  
  985. ################################################################################
  986. # Check whether actions can be taken.
  987. ################################################################################
  988.   def pbCanShowCommands?(idxPokemon)
  989.     thispkmn=@battlers[idxPokemon]
  990.     return false if thispkmn.isFainted?
  991.     return false if thispkmn.effects[PBEffects::TwoTurnAttack]>0
  992.     return false if thispkmn.effects[PBEffects::HyperBeam]>0
  993.     return false if thispkmn.effects[PBEffects::Rollout]>0
  994.     return false if thispkmn.effects[PBEffects::Outrage]>0
  995.     return false if thispkmn.effects[PBEffects::Rage]==true && $fefieldeffect == 24
  996.     return false if thispkmn.effects[PBEffects::Uproar]>0
  997.     return false if thispkmn.effects[PBEffects::Bide]>0
  998. #### KUROTSUNE - 022 - START    
  999.     return false if thispkmn.effects[PBEffects::SkyDrop]    
  1000. #### KUROTSUNE - 022 - END    
  1001.     return true
  1002.   end
  1003.  
  1004. ################################################################################
  1005. # Attacking.
  1006. ################################################################################
  1007.   def pbCanShowFightMenu?(idxPokemon)
  1008.     thispkmn=@battlers[idxPokemon]
  1009.     if !pbCanShowCommands?(idxPokemon)
  1010.       return false
  1011.     end
  1012.     # No moves that can be chosen
  1013.     if !pbCanChooseMove?(idxPokemon,0,false) &&
  1014.        !pbCanChooseMove?(idxPokemon,1,false) &&
  1015.        !pbCanChooseMove?(idxPokemon,2,false) &&
  1016.        !pbCanChooseMove?(idxPokemon,3,false)
  1017.       return false
  1018.     end
  1019.     # Encore
  1020.     return false if thispkmn.effects[PBEffects::Encore]>0
  1021.     return true
  1022.   end
  1023.  
  1024.   def pbCanChooseMove?(idxPokemon,idxMove,showMessages,sleeptalk=false)
  1025.     thispkmn=@battlers[idxPokemon]
  1026.     thismove=thispkmn.moves[idxMove]
  1027.     opp1=thispkmn.pbOpposing1
  1028.     opp2=thispkmn.pbOpposing2
  1029.     if !thismove||thismove.id==0
  1030.       return false
  1031.     end
  1032.     if thismove.pp<=0 && thismove.totalpp>0 && !sleeptalk
  1033.       if showMessages
  1034.         pbDisplayPaused(_INTL("There's no PP left for this move!"))
  1035.       end
  1036.       return false
  1037.     end
  1038.     if thispkmn.effects[PBEffects::ChoiceBand]>=0 &&
  1039.       (thispkmn.hasWorkingItem(:CHOICEBAND) ||
  1040.       thispkmn.hasWorkingItem(:CHOICESPECS) ||
  1041.       thispkmn.hasWorkingItem(:CHOICESCARF))
  1042.       hasmove=false
  1043.       for i in 0...4
  1044.         if thispkmn.moves[i].id==thispkmn.effects[PBEffects::ChoiceBand]
  1045.           hasmove=true
  1046.           break
  1047.         end
  1048.       end
  1049.       if hasmove && thismove.id!=thispkmn.effects[PBEffects::ChoiceBand]
  1050.         if showMessages
  1051.           pbDisplayPaused(_INTL("{1} allows the use of only {2}!",
  1052.              PBItems.getName(thispkmn.item),
  1053.              PBMoves.getName(thispkmn.effects[PBEffects::ChoiceBand])))
  1054.         end
  1055.         return false
  1056.       end
  1057.     end
  1058. #### KUROTSUNE - 018 - START
  1059.  
  1060.     if isConst?(thispkmn.item,PBItems,:ASSAULTVEST) && !(thismove.pbIsPhysical?(thismove.type) || thismove.pbIsSpecial?(thismove.type))
  1061.         if showMessages
  1062.           pbDisplayPaused(_INTL("{1} doesn't allow use of non-attacking moves!",
  1063.              PBItems.getName(thispkmn.item)))
  1064.         end
  1065.         return false
  1066.     end
  1067.  
  1068. #### KUROTSUNE - 018 - END    
  1069.    
  1070.     if opp1.effects[PBEffects::Imprison]
  1071.       if thismove.id==opp1.moves[0].id ||
  1072.          thismove.id==opp1.moves[1].id ||
  1073.          thismove.id==opp1.moves[2].id ||
  1074.          thismove.id==opp1.moves[3].id
  1075.         if showMessages
  1076.           pbDisplayPaused(_INTL("{1} can't use the sealed {2}!",thispkmn.pbThis,thismove.name))
  1077.         end
  1078.        #PBDebug.log("[CanChoose][#{opp1.pbThis} has: #{opp1.moves[0].name}, #{opp1.moves[1].name},#{opp1.moves[2].name},#{opp1.moves[3].name}]") if $INTERNAL
  1079.         return false
  1080.       end
  1081.     end
  1082.     if opp2.effects[PBEffects::Imprison]
  1083.       if thismove.id==opp2.moves[0].id ||
  1084.          thismove.id==opp2.moves[1].id ||
  1085.          thismove.id==opp2.moves[2].id ||
  1086.          thismove.id==opp2.moves[3].id
  1087.         if showMessages
  1088.           pbDisplayPaused(_INTL("{1} can't use the sealed {2}!",thispkmn.pbThis,thismove.name))
  1089.         end
  1090.         #PBDebug.log("[CanChoose][#{opp2.pbThis} has: #{opp2.moves[0].name}, #{opp2.moves[1].name},#{opp2.moves[2].name},#{opp2.moves[3].name}]") if $INTERNAL
  1091.         return false
  1092.       end
  1093.     end
  1094.     if thispkmn.effects[PBEffects::Taunt]>0 && thismove.basedamage==0
  1095.       if showMessages
  1096.         pbDisplayPaused(_INTL("{1} can't use {2} after the Taunt!",thispkmn.pbThis,thismove.name))
  1097.       end
  1098.       return false
  1099.     end
  1100.     if thispkmn.effects[PBEffects::Torment]
  1101.       if thismove.id==thispkmn.lastMoveUsed
  1102.         if showMessages
  1103.           pbDisplayPaused(_INTL("{1} can't use the same move in a row due to the torment!",thispkmn.pbThis))
  1104.         end
  1105.         return false
  1106.       end
  1107.     end
  1108.     if thismove.id==thispkmn.effects[PBEffects::DisableMove] && !sleeptalk
  1109.       if showMessages
  1110.         pbDisplayPaused(_INTL("{1}'s {2} is disabled!",thispkmn.pbThis,thismove.name))
  1111.       end
  1112.       return false
  1113.     end
  1114.     if thispkmn.effects[PBEffects::Encore]>0 && idxMove!=thispkmn.effects[PBEffects::EncoreIndex]
  1115.       return false
  1116.     end
  1117.     return true
  1118.   end
  1119.  
  1120.   def pbAutoChooseMove(idxPokemon,showMessages=true)
  1121.     thispkmn=@battlers[idxPokemon]
  1122.     if thispkmn.isFainted?
  1123.       @choices[idxPokemon][0]=0
  1124.       @choices[idxPokemon][1]=0
  1125.       @choices[idxPokemon][2]=nil
  1126.       return
  1127.     end
  1128.     if thispkmn.effects[PBEffects::Encore]>0 &&
  1129.        pbCanChooseMove?(idxPokemon,thispkmn.effects[PBEffects::EncoreIndex],false)
  1130.       PBDebug.log("[Auto choosing Encore move...]") if $INTERNAL
  1131.       @choices[idxPokemon][0]=1    # "Use move"
  1132.       @choices[idxPokemon][1]=thispkmn.effects[PBEffects::EncoreIndex] # Index of move
  1133.       @choices[idxPokemon][2]=thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]]
  1134.       @choices[idxPokemon][3]=-1   # No target chosen yet
  1135.       if @doublebattle
  1136.         thismove=thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]]
  1137.         target=thispkmn.pbTarget(thismove)
  1138.         if target==PBTargets::SingleNonUser #&& ($fefieldeffect != 33 ||
  1139.           #$fecounter != 4 || (thismove.id != 192 || thismove.id != 214 ||
  1140. #         thismove.id != 218 || thismove.id != 219 || thismove.id != 220 ||
  1141.  #        thismove.id != 445 || thismove.id != 596 || thismove.id != 600))
  1142.   #        print("this message is bad")
  1143.           target=@scene.pbChooseTarget(idxPokemon)
  1144.           pbRegisterTarget(idxPokemon,target) if target>=0
  1145.         elsif target==PBTargets::UserOrPartner
  1146.           target=@scene.pbChooseTarget(idxPokemon)
  1147.           pbRegisterTarget(idxPokemon,target) if target>=0 && (target&1)==(idxPokemon&1)
  1148.         end
  1149.       end
  1150.     else
  1151.       if !pbIsOpposing?(idxPokemon)
  1152.         pbDisplayPaused(_INTL("{1} has no moves left!",thispkmn.name)) if showMessages
  1153.       end
  1154.       @choices[idxPokemon][0]=1           # "Use move"
  1155.       @choices[idxPokemon][1]=-1          # Index of move to be used
  1156.       @choices[idxPokemon][2]=@struggle   # Use Struggle
  1157.       @choices[idxPokemon][3]=-1          # No target chosen yet
  1158.     end
  1159.   end
  1160.  
  1161.   def pbRegisterMove(idxPokemon,idxMove,showMessages=true)
  1162.     thispkmn=@battlers[idxPokemon]
  1163.     thismove=thispkmn.moves[idxMove]
  1164. #### KUROTSUNE - 010 - START
  1165.     thispkmn.selectedMove = thismove.id
  1166. #### KUROTSUNE - 010 - END
  1167.     return false if !pbCanChooseMove?(idxPokemon,idxMove,showMessages)
  1168.     @choices[idxPokemon][0]=1         # "Use move"
  1169.     @choices[idxPokemon][1]=idxMove   # Index of move to be used
  1170.     @choices[idxPokemon][2]=thismove  # PokeBattle_Move object of the move
  1171.     @choices[idxPokemon][3]=-1        # No target chosen yet
  1172.     return true
  1173.   end
  1174.  
  1175.   def pbChoseMove?(i,move)
  1176.     return false if @battlers[i].isFainted?
  1177.     if @choices[i][0]==1 && @choices[i][1]>=0
  1178.       choice=@choices[i][1]
  1179.       return isConst?(@battlers[i].moves[choice].id,PBMoves,move)
  1180.     end
  1181.     return false
  1182.   end
  1183.  
  1184.   def pbChoseMoveFunctionCode?(i,code)
  1185.     return false if @battlers[i].isFainted?
  1186.     if @choices[i][0]==1 && @choices[i][1]>=0
  1187.       choice=@choices[i][1]
  1188.       return @battlers[i].moves[choice].function==code
  1189.     end
  1190.     return false
  1191.   end
  1192.  
  1193.   def pbRegisterTarget(idxPokemon,idxTarget)
  1194.     @choices[idxPokemon][3]=idxTarget   # Set target of move
  1195.     return true
  1196.   end
  1197.  
  1198. # UPDATE 11/23/2013
  1199.   # implementing STALL
  1200. def pbPriority(ignorequickclaw = false)
  1201.     if @usepriority
  1202.       # use stored priority if round isn't over yet
  1203.       return @priority
  1204.     end
  1205.     speeds=[]
  1206.     quickclaw=[]
  1207.     custapberry=[]    
  1208.     stall=[] # <--- Add this here
  1209.     lagtail=[] # <--- This too
  1210.     incense=[] # <--- ... and this
  1211.     priorities=[]
  1212.     temp=[]
  1213.     @priority.clear
  1214.     maxpri=0
  1215.     minpri=0
  1216.     # Calculate each Pokémon's speed
  1217.     ### Simplified below
  1218.     #speeds[0]=@battlers[0].pbSpeed
  1219.     #speeds[1]=@battlers[1].pbSpeed
  1220.     #speeds[2]=@battlers[2].pbSpeed
  1221.     #speeds[3]=@battlers[3].pbSpeed
  1222.     #quickclaw[0]=isConst?(@battlers[0].item,PBItems,:QUICKCLAW)
  1223.     #quickclaw[1]=isConst?(@battlers[1].item,PBItems,:QUICKCLAW)
  1224.     #quickclaw[2]=isConst?(@battlers[2].item,PBItems,:QUICKCLAW)
  1225.     #quickclaw[3]=isConst?(@battlers[3].item,PBItems,:QUICKCLAW)
  1226.     ###    
  1227.     # Find the maximum and minimum priority
  1228.     for i in 0..3
  1229.       ### add these here
  1230.       speeds[i]    = @battlers[i].pbSpeed
  1231.       quickclaw[i] = isConst?(@battlers[i].item, PBItems, :QUICKCLAW)
  1232.       custapberry[i] = @battlers[i].custap
  1233.    #   && !ignorequickclaw && @choices[i][0] == 1
  1234.       stall[i]     = isConst?(@battlers[i].ability, PBAbilities, :STALL)
  1235.       lagtail[i]   = isConst?(@battlers[i].item, PBItems, :LAGGINGTAIL)
  1236.       incense[i]   = isConst?(@battlers[i].item, PBItems, :FULLINCENSE)
  1237.       ###
  1238.       # For this function, switching and using items
  1239.       # is the same as using a move with a priority of 0
  1240.       pri=0
  1241.       if @choices[i][0]==1 # Is a move
  1242.         pri=@choices[i][2].priority
  1243.         pri+=1 if isConst?(@battlers[i].ability,PBAbilities,:PRANKSTER) &&
  1244.                   @choices[i][2].basedamage==0 # Is status move
  1245.         pri+=1 if isConst?(@battlers[i].ability,PBAbilities,:GALEWINGS) && @choices[i][2].type==2
  1246. #        pri-=1 if $fefieldeffect == 6 &&
  1247. #        @battlers[i].effects[PBEffects::TwoTurnAttack] !=0 &&
  1248. #        (@choices[i][2].id==156 || @choices[i][2].id==157)
  1249.        
  1250.       end
  1251.       priorities[i]=pri
  1252.       if i==0
  1253.         maxpri=pri
  1254.         minpri=pri
  1255.       else
  1256.         maxpri=pri if maxpri<pri
  1257.         minpri=pri if minpri>pri
  1258.       end
  1259.     end
  1260.     # Find and order all moves with the same priority
  1261.     curpri=maxpri
  1262.     loop do
  1263.       temp.clear
  1264.       for j in 0...4
  1265.         if priorities[j]==curpri
  1266.           temp[temp.length]=j
  1267.         end
  1268.       end
  1269.       # Sort by speed
  1270.       if temp.length==1
  1271.         @priority[@priority.length]=@battlers[temp[0]]
  1272.       else
  1273.         n=temp.length
  1274.         usequickclaw=(pbRandom(100)<20)
  1275.         for m in 0..n-2
  1276.           for i in 1..n-1
  1277.             if quickclaw[temp[i]] && usequickclaw
  1278.               cmp=(quickclaw[temp[i-1]]) ? 0 : -1 #Rank higher if without Quick Claw, or equal if with it
  1279.             elsif quickclaw[temp[i-1]] && usequickclaw
  1280.               cmp=1 # Rank lower
  1281.             elsif custapberry[temp[i]]
  1282.               cmp=(custapberry[temp[i-1]]) ? 0 : -1 #Rank higher if without Custap Berry, or equal if with it
  1283.             elsif custapberry[temp[i-1]]
  1284.               cmp=1 # Rank lower              
  1285.             # UPDATE 11/23/2013
  1286.             # stall ability
  1287.             # add the following two elsif blocks
  1288.             ####
  1289.             # ignored if we have full incense or lagging tail
  1290.             elsif stall[temp[i]] && !(incense[temp[i]] || lagtail[temp[i]])
  1291.               # if they also have stall
  1292.               if stall[temp[i-1]] && !(incense[temp[i-1]] || lagtail[temp[i-1]])
  1293.                 # higher speed -> lower priority
  1294.                 cmp=speeds[temp[i]] > speeds[temp[i-1]] ? 1 : -1
  1295.               else
  1296.                 cmp=1
  1297.               end
  1298.             elsif lagtail[temp[i-1]] || incense[temp[i-1]]
  1299.               cmp=-1    
  1300.            elsif lagtail[temp[i]] || incense[temp[i]]
  1301.               cmp=1
  1302.             elsif stall[temp[i-1]] && !(incense[temp[i-1]] || lagtail[temp[i-1]])
  1303.               cmp= lagtail[temp[i]] || incense[temp[i]] ? 1 : -1
  1304.            # end of update
  1305.             elsif speeds[temp[i]]!=speeds[temp[i-1]]
  1306.               cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? -1 : 1 #Rank higher to higher-speed battler
  1307.             else
  1308.               cmp=0
  1309.             end
  1310.             # UPDATE implementing Trick Room
  1311.                     if cmp<0 && @trickroom==0
  1312.               # put higher-speed Pokémon first
  1313.               swaptmp=temp[i]
  1314.               temp[i]=temp[i-1]
  1315.               temp[i-1]=swaptmp
  1316.             elsif cmp>0 && @trickroom>0
  1317.                             swaptmp=temp[i]
  1318.               temp[i]=temp[i-1]
  1319.               temp[i-1]=swaptmp
  1320.  
  1321.             elsif cmp==0
  1322.             # END OF UPDATE
  1323.               # swap at random if speeds are equal
  1324.               if pbRandom(2)==0
  1325.                 swaptmp=temp[i]
  1326.                 temp[i]=temp[i-1]
  1327.                 temp[i-1]=swaptmp
  1328.               end
  1329.             end
  1330.           end
  1331.         end
  1332.         #Now add the temp array to priority
  1333.         for i in temp
  1334.           @priority[@priority.length]=@battlers[i]
  1335.         end
  1336.       end
  1337.       curpri-=1
  1338.       break unless curpri>=minpri
  1339.     end
  1340. =begin
  1341.     prioind=[
  1342.        @priority[0].index,
  1343.        @priority[1].index,
  1344.        @priority[2] ? @priority[2].index : -1,
  1345.        @priority[3] ? @priority[3].index : -1
  1346.     ]
  1347.     print("#{speeds.inspect} #{prioind.inspect}")
  1348. =end
  1349.     @usepriority=true
  1350.     return @priority
  1351.   end
  1352.  
  1353. ##### KUROTSUNE - 011 - START
  1354.  # Makes target pokemon move last
  1355.   def pbMoveLast(target)
  1356.     priorityTarget = pbGetPriority(target)
  1357.     priority = @priority
  1358.     case priorityTarget
  1359.     when 0
  1360.       # Opponent has likely already moved
  1361.       return false
  1362.     when 1
  1363.       aux = priority[3]
  1364.       priority[3] = target
  1365.       priority[1] = aux
  1366.       aux = priority[2]
  1367.       priority[2] = priority[1]
  1368.       priority[1] = aux
  1369.       @priority = priority
  1370.       return true
  1371.     when 2
  1372.       aux = priority[2]
  1373.       priority[2] = priority[3]
  1374.       priority[3] = aux
  1375.       @priority = priority
  1376.       return true
  1377.     when 3
  1378.       return false
  1379.     end
  1380.   end
  1381.  
  1382.    
  1383.   # Makes the second pokemon move after the first.
  1384.   def pbMoveAfter(first, second)
  1385.     priorityFirst = pbGetPriority(first)
  1386.     priority = @priority
  1387.     case priorityFirst
  1388.     when 0
  1389.       if second == priority[1]
  1390.         # Nothing to do here
  1391.         return false
  1392.       elsif second == priority[2]
  1393.         aux = priority[1]
  1394.         priority[1] = second
  1395.         priority[2] = aux
  1396.         @priority = priority
  1397.         return true
  1398.       elsif second == priority[3]
  1399.         aux = priority[1]
  1400.         priority[1] = second
  1401.         priority[3] = aux
  1402.         aux = priority[2]
  1403.         priority[2] = priority[3]
  1404.         priority[3] = aux
  1405.         @priority = priority
  1406.         return true
  1407.       end
  1408.     when 1
  1409.       if second == priority[0] || second == priority[2]
  1410.         # Nothing to do here
  1411.         return false
  1412.       elsif second == priority[3]
  1413.         aux = priority[2]
  1414.         priority[2] = priority[3]
  1415.         priority[3] = aux
  1416.         @priority = priority
  1417.         return true
  1418.       end
  1419.     when 2
  1420.       return false
  1421.     when 3
  1422.       return false
  1423.     end
  1424.   end
  1425. ##### KUROTSUNE - 011 - END
  1426.  
  1427. def pbGetPriority(mon)
  1428.     for i in 0..3
  1429.       if @priority[i] == mon
  1430.         return i
  1431.       end
  1432.     end
  1433.     return -1
  1434.   end
  1435.  
  1436.  
  1437.    def pbClearChoices(index)
  1438.     choices[index][0] = -1
  1439.     choices[index][1] = -1
  1440.     choices[index][2] = -1
  1441.     choices[index][3] = -1
  1442.   end
  1443. ################################################################################
  1444. # Switching Pokémon.
  1445. ################################################################################
  1446.   def pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
  1447.     if pkmnidxTo>=0
  1448.       party=pbParty(idxPokemon)
  1449.       if pkmnidxTo>=party.length
  1450.         return false
  1451.       end
  1452.       if !party[pkmnidxTo]
  1453.         return false
  1454.       end
  1455.       if party[pkmnidxTo].isEgg?
  1456.         pbDisplayPaused(_INTL("An Egg can't battle!")) if showMessages
  1457.         return false
  1458.       end
  1459.       if !pbIsOwner?(idxPokemon,pkmnidxTo)
  1460.         owner=pbPartyGetOwner(idxPokemon,pkmnidxTo)
  1461.         pbDisplayPaused(_INTL("You can't switch {1}'s Pokémon with one of yours!",owner.name)) if showMessages
  1462.         return false
  1463.       end
  1464.       if party[pkmnidxTo].hp<=0
  1465.         pbDisplayPaused(_INTL("{1} has no energy left to battle!",party[pkmnidxTo].name)) if showMessages
  1466.         return false
  1467.       end  
  1468.       if @battlers[idxPokemon].pokemonIndex==pkmnidxTo
  1469.         pbDisplayPaused(_INTL("{1} is already in battle!",party[pkmnidxTo].name)) if showMessages
  1470.         return false
  1471.       end
  1472.       if @battlers[idxPokemon].pbPartner.pokemonIndex==pkmnidxTo
  1473.         pbDisplayPaused(_INTL("{1} is already in battle!",party[pkmnidxTo].name)) if showMessages
  1474.         return false
  1475.       end
  1476.     end
  1477.     return true
  1478.   end
  1479.  
  1480.   def pbCanSwitch?(idxPokemon,pkmnidxTo,showMessages)
  1481.     thispkmn=@battlers[idxPokemon]
  1482.     # Multi-Turn Attacks/Mean Look
  1483.     if !pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
  1484.       return false
  1485.     end
  1486.     # UPDATE 11/16/2013
  1487.     # Ghost type can now escape from anything
  1488.     if thispkmn.pbHasType?(:GHOST)
  1489.       return true
  1490.     end
  1491.     isOpposing=pbIsOpposing?(idxPokemon)
  1492.     party=pbParty(idxPokemon)
  1493.     for i in 0...4
  1494.       next if isOpposing!=pbIsOpposing?(i)
  1495.       if choices[i][0]==2 && choices[i][1]==pkmnidxTo
  1496.         pbDisplayPaused(_INTL("{1} has already been selected.",party[pkmnidxTo].name)) if showMessages
  1497.         return false
  1498.       end
  1499.     end
  1500.     if thispkmn.hasWorkingItem(:SHEDSHELL)
  1501.       return true
  1502.     end
  1503.     if thispkmn.effects[PBEffects::MultiTurn]>0 ||
  1504.        thispkmn.effects[PBEffects::MeanLook]>=0 ||
  1505.        field.effects[PBEffects::FairyLock]==1
  1506.       pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1507.       return false
  1508.     end
  1509.     # Ingrain
  1510.     if thispkmn.effects[PBEffects::Ingrain]
  1511.       pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1512.       return false
  1513.     end
  1514.     opp1=thispkmn.pbOpposing1
  1515.     opp2=thispkmn.pbOpposing2
  1516.     opp=nil
  1517.     if thispkmn.pbHasType?(:STEEL)
  1518.       opp=opp1 if opp1.hasWorkingAbility(:MAGNETPULL)
  1519.       opp=opp2 if opp2.hasWorkingAbility(:MAGNETPULL)
  1520.     end
  1521.     if !thispkmn.isAirborne?
  1522.       opp=opp1 if opp1.hasWorkingAbility(:ARENATRAP)
  1523.       opp=opp2 if opp2.hasWorkingAbility(:ARENATRAP)
  1524.     end
  1525.     if !thispkmn.hasWorkingAbility(:SHADOWTAG)
  1526.       opp=opp1 if opp1.hasWorkingAbility(:SHADOWTAG)
  1527.       opp=opp2 if opp2.hasWorkingAbility(:SHADOWTAG)
  1528.     end
  1529.     if opp
  1530.       abilityname=PBAbilities.getName(opp.ability)
  1531.       pbDisplayPaused(_INTL("{1}'s {2} prevents switching!",opp.pbThis,abilityname)) if showMessages
  1532.       # UPDATE 11/16
  1533.       # now displays the proper fleeing message iff you are attempting to flee
  1534.       # Note: not very elegant, but it should work.
  1535.       pbDisplayPaused(_INTL("{1} prevents escaping with {2}!", opp.pbThis, abilityname)) if !showMessages && pkmnidxTo == -1
  1536.       return false
  1537.     end
  1538.     return true
  1539.   end
  1540.  
  1541.   def pbRegisterSwitch(idxPokemon,idxOther)
  1542.     return false if !pbCanSwitch?(idxPokemon,idxOther,false)
  1543.     @choices[idxPokemon][0]=2          # "Switch Pokémon"
  1544.     @choices[idxPokemon][1]=idxOther   # Index of other Pokémon to switch with
  1545.     @choices[idxPokemon][2]=nil
  1546.     side=(pbIsOpposing?(idxPokemon)) ? 1 : 0
  1547.     owner=pbGetOwnerIndex(idxPokemon)
  1548.     if @megaEvolution[side][owner]==idxPokemon
  1549.       @megaEvolution[side][owner]=-1
  1550.     end
  1551.     return true
  1552.   end
  1553.  
  1554.   def pbCanChooseNonActive?(index)
  1555.     party=pbParty(index)
  1556.     for i in 0..party.length-1
  1557.       return true if pbCanSwitchLax?(index,i,false)
  1558.     end
  1559.     return false
  1560.   end
  1561.  
  1562.  def pbJudgeSwitch(favorDraws=false)
  1563.     if !favorDraws
  1564.       return if @decision>0
  1565.       pbJudge()
  1566.       return if @decision>0
  1567.     else
  1568.       return if @decision==5
  1569.       pbJudge()
  1570.       return if @decision>0
  1571.     end
  1572.   end
  1573.  
  1574.   def pbSwitch(favorDraws=false)
  1575.     if !favorDraws
  1576.       return if @decision>0
  1577.       pbJudge()
  1578.       return if @decision>0
  1579.     else
  1580.       return if @decision==5
  1581.       pbJudge()
  1582.       return if @decision>0
  1583.     end
  1584.     firstbattlerhp=@battlers[0].hp
  1585.     switched=[]
  1586.     for index in 0...4
  1587.       next if !@doublebattle && pbIsDoubleBattler?(index)
  1588.       next if @battlers[index] && !@battlers[index].isFainted?
  1589.       next if !pbCanChooseNonActive?(index)
  1590.       if !pbOwnedByPlayer?(index)
  1591.         if !pbIsOpposing?(index) || (@opponent && pbIsOpposing?(index))
  1592.           newenemy=pbSwitchInBetween(index,false,false)
  1593. #### JERICHO - 001 - START
  1594.             if !pbIsOpposing?(index)
  1595.               if isConst?(@party1[newenemy].ability,PBAbilities,:ILLUSION) #ILLUSION
  1596.                 party3=@party1.find_all {|item| item && !item.egg? && item.hp>0 }
  1597.                 if party3[@party1.length-1] != @party1[newenemy]
  1598.                   illusionpoke = party3[party3.length-1]
  1599.                 end
  1600.               end #ILLUSION
  1601.               newname = illusionpoke != nil ? illusionpoke.name : @party1[newenemy].name #ILLUSION
  1602.             else
  1603.               if isConst?(@party2[newenemy].ability,PBAbilities,:ILLUSION) #ILLUSION
  1604.                 party3=@party1.find_all {|item| item && !item.egg? && item.hp>0 }
  1605.                 if party3[@party1.length-1] != @party1[newenemy]
  1606.                   illusionpoke = party3[party3.length-1]
  1607.                 end
  1608.               end #ILLUSION
  1609.               newname = illusionpoke != nil ? illusionpoke.name : PBSpecies.getName(@party2[newenemy].species) #ILLUSION
  1610.             end
  1611. #### JERICHO - 001 - END
  1612.           opponent=pbGetOwner(index)
  1613.           if !@doublebattle && firstbattlerhp>0 && @shiftStyle && @opponent &&
  1614.               @internalbattle && pbCanChooseNonActive?(0) && pbIsOpposing?(index) &&
  1615.               @battlers[0].effects[PBEffects::Outrage]==0
  1616. #### JERICHO - 001 - START  
  1617.               pbDisplayPaused(_INTL("{1} is about to send in {2}.",opponent.fullname,newname)) #ILLUSION              
  1618. #### JERICHO - 001 - END                      
  1619.             if pbDisplayConfirm(_INTL("Will {1} change Pokémon?",self.pbPlayer.name))
  1620.               newpoke=pbSwitchPlayer(0,true,true)
  1621.               if newpoke>=0
  1622.                 pbDisplayBrief(_INTL("{1}, that's enough!  Come back!",@battlers[0].name))
  1623.                 pbRecallAndReplace(0,newpoke)
  1624.                 switched.push(0)
  1625.               end
  1626.             end
  1627.           end
  1628.           pbRecallAndReplace(index,newenemy)
  1629.           switched.push(index)
  1630.         end
  1631.       elsif @opponent
  1632.         newpoke=pbSwitchInBetween(index,true,false)
  1633.         pbRecallAndReplace(index,newpoke)
  1634.         switched.push(index)
  1635.       else
  1636.         switch=false
  1637.         if !pbDisplayConfirm(_INTL("Use next Pokémon?"))
  1638.           switch=(pbRun(index,true)<=0)
  1639.         else
  1640.           switch=true
  1641.         end
  1642.         if switch
  1643.           newpoke=pbSwitchInBetween(index,true,false)
  1644.           pbRecallAndReplace(index,newpoke)
  1645.           switched.push(index)
  1646.         end
  1647.       end
  1648.     end
  1649.     if switched.length>0
  1650.       priority=pbPriority
  1651.       for i in priority
  1652.         i.pbAbilitiesOnSwitchIn(true) if switched.include?(i.index)
  1653.       end
  1654.     end
  1655.   end
  1656.  
  1657. def pbSendOut(index,pokemon)
  1658.     pbSetSeen(pokemon)
  1659.     @peer.pbOnEnteringBattle(self,pokemon)
  1660.     if pbIsOpposing?(index)  
  1661.       #  in-battle text      
  1662.       @scene.pbTrainerSendOut(index,pokemon)
  1663.       # Last Pokemon script; credits to venom12 and HelioAU
  1664.       if !@opponent.is_a?(Array)
  1665.         trainertext = @opponent
  1666.         if pbPokemonCount(@party2)==1
  1667.         # Define any trainers that you want to activate this script below
  1668.         # For each defined trainer, add the BELOW section for them
  1669.           if isConst?(trainertext.trainertype,PBTrainers,:Hotshot)
  1670.             if $game_variables[192] == 0
  1671.               @scene.pbShowOpponent(0)
  1672.               pbDisplayPaused(_INTL("You think you can get away with that?"))
  1673.               @scene.pbHideOpponent
  1674.             elsif $game_variables[192] == 1
  1675.               @scene.pbShowOpponent(0)
  1676.               pbDisplayPaused(_INTL("You don't actually think you're special, do you?"))
  1677.               @scene.pbHideOpponent
  1678.             elsif $game_variables[192] == 2
  1679.               @scene.pbShowOpponent(0)
  1680.               pbDisplayPaused(_INTL("You're really starting to grate on my nerves, and you don't wanna do that."))
  1681.               @scene.pbHideOpponent
  1682.             elsif $game_variables[192] == 4
  1683.               @scene.pbShowOpponent(0)
  1684.               pbDisplayPaused(_INTL("Pfft, picking on you is way too easy."))
  1685.               @scene.pbHideOpponent
  1686.             elsif $game_variables[192] == 5
  1687.               @scene.pbShowOpponent(0)
  1688.               pbDisplayPaused(_INTL("You're a real thorn in my side, you know?"))
  1689.               @scene.pbHideOpponent              
  1690.             end
  1691.           elsif isConst?(trainertext.trainertype,PBTrainers,:Leader)
  1692.             if $game_variables[192] == 0
  1693.               @scene.pbShowOpponent(0)
  1694.               pbDisplayPaused(_INTL("Don'tgoboom,don'tgoboom,don'tgoboom..."))
  1695.               @scene.pbHideOpponent
  1696.             end
  1697.           elsif isConst?(trainertext.trainertype,PBTrainers,:Victoria)
  1698.             if $game_variables[192] ==  1
  1699.               @scene.pbShowOpponent(0)
  1700.               pbDisplayPaused(_INTL("If you can't handle this, there's no way you can get through there safely!"))
  1701.               @scene.pbHideOpponent
  1702.             elsif $game_variables[192] == 3
  1703.               @scene.pbShowOpponent(0)
  1704.               pbDisplayPaused(_INTL("Is there really nothing I can do?"))
  1705.               @scene.pbHideOpponent
  1706.             elsif $game_variables[192] == 4
  1707.               @scene.pbShowOpponent(0)
  1708.               pbDisplayPaused(_INTL("Please, for Kiki's sake..."))
  1709.               @scene.pbHideOpponent
  1710.             elsif $game_variables[192] == 7
  1711.               @scene.pbShowOpponent(0)
  1712.               pbDisplayPaused(_INTL("To minimize the chaos within... Sensei, I'm so sorry."))
  1713.               @scene.pbHideOpponent
  1714.             end
  1715.           elsif isConst?(trainertext.trainertype,PBTrainers,:Cain)
  1716.             if $game_variables[192] == 1
  1717.               @scene.pbShowOpponent(0)
  1718.               pbDisplayPaused(_INTL("Aha, well this is a rough first battle for the little guy. But the first time is always rough, huh?"))
  1719.               @scene.pbHideOpponent
  1720.             elsif $game_variables[192] == 2
  1721.               @scene.pbShowOpponent(0)
  1722.               pbDisplayPaused(_INTL("I am sooo getting bent-over here."))
  1723.               @scene.pbHideOpponent
  1724.             elsif $game_variables[192] == 3
  1725.               @scene.pbShowOpponent(0)
  1726.               pbDisplayPaused(_INTL("Welllll, I thought I could get away with not opening it from a rematch, but-- oops."))
  1727.               @scene.pbHideOpponent
  1728.             elsif $game_variables[192] == 5
  1729.               @scene.pbShowOpponent(0)
  1730.               pbDisplayPaused(_INTL("You always seem to overwhelm me...~"))
  1731.               @scene.pbHideOpponent              
  1732.             end
  1733.           elsif isConst?(trainertext.trainertype,PBTrainers,:Leader_096)
  1734.             if $game_variables[192] == 0
  1735.               @scene.pbShowOpponent(0)
  1736.               pbDisplayPaused(_INTL("Conclusion forthcoming..."))
  1737.               @scene.pbHideOpponent
  1738.             end
  1739.           elsif isConst?(trainertext.trainertype,PBTrainers,:Taka)
  1740.             if $game_variables[192] == 0
  1741.               @scene.pbShowOpponent(0)
  1742.               pbDisplayPaused(_INTL("It's a shame it has to come to this, but..."))
  1743.               @scene.pbHideOpponent
  1744.             elsif $game_variables[192] == 1
  1745.               @scene.pbShowOpponent(0)
  1746.               pbDisplayPaused(_INTL("I guess that means it's time for this, right?"))
  1747.               @scene.pbHideOpponent
  1748.             elsif $game_variables[192] == 2
  1749.               @scene.pbShowOpponent(0)
  1750.               pbDisplayPaused(_INTL("Defeat was never such a relief."))
  1751.               @scene.pbHideOpponent            
  1752.             end
  1753.           elsif isConst?(trainertext.trainertype,PBTrainers,:Corey)
  1754.             if $game_variables[192] == 0
  1755.               @scene.pbShowOpponent(0)
  1756.               pbDisplayPaused(_INTL("Disappointment is a two-way street."))
  1757.               @scene.pbHideOpponent
  1758.             elsif $game_variables[192] == 1
  1759.               @scene.pbShowOpponent(0)
  1760.               pbDisplayPaused(_INTL("All things must end. Even you. Even I."))
  1761.               @scene.pbHideOpponent      
  1762.             end
  1763.           elsif isConst?(trainertext.trainertype,PBTrainers,:Leader_101)
  1764.             if $game_variables[192] == 0
  1765.               @scene.pbShowOpponent(0)
  1766.               pbDisplayPaused(_INTL("Hehe... Failing again..."))
  1767.               @scene.pbHideOpponent
  1768.             end
  1769.           elsif isConst?(trainertext.trainertype,PBTrainers,:Leader_103)
  1770.             if $game_variables[192] == 0
  1771.               @scene.pbShowOpponent(0)
  1772.               pbDisplayPaused(_INTL("..."))
  1773.               @scene.pbHideOpponent
  1774.             end
  1775.           elsif isConst?(trainertext.trainertype,PBTrainers,:ZEL)
  1776.             if $game_variables[192] == 2
  1777.               @scene.pbShowOpponent(0)
  1778.               pbDisplayPaused(_INTL("Zel: Tch, no choice then!"))
  1779.               @scene.pbHideOpponent
  1780.             end
  1781.             if $game_variables[192] == 4
  1782.               @scene.pbShowOpponent(0)
  1783.               pbDisplayPaused(_INTL("zeL: I'm sorry, Magnezone... If we're gonna get through this, we need your help!"))
  1784.               @scene.pbHideOpponent
  1785.             end
  1786.           elsif isConst?(trainertext.trainertype,PBTrainers,:Sensei)
  1787.             if $game_variables[192] == 0
  1788.               @scene.pbShowOpponent(0)
  1789.               pbDisplayPaused(_INTL("To minimize the chaos within... Perhaps it's too late."))
  1790.               @scene.pbHideOpponent
  1791.             end
  1792.           elsif isConst?(trainertext.trainertype,PBTrainers,:WPunk)
  1793.             if $game_variables[192] == 0
  1794.               @scene.pbShowOpponent(0)
  1795.               pbDisplayPaused(_INTL("Just finish it already, why don't you."))
  1796.               @scene.pbHideOpponent
  1797.             end
  1798.           elsif isConst?(trainertext.trainertype,PBTrainers,:Sirius)
  1799.             if $game_variables[192] == 0 || $game_variables[192] == 1
  1800.               @scene.pbShowOpponent(0)
  1801.               pbDisplayPaused(_INTL("Perhaps we've left this threat unchecked for too long."))
  1802.               @scene.pbHideOpponent      
  1803.             end
  1804.           elsif isConst?(trainertext.trainertype,PBTrainers,:DOCTOR)
  1805.             if $game_variables[192] == 0 || $game_variables[192] == 1
  1806.               @scene.pbShowOpponent(0)
  1807.               pbDisplayPaused(_INTL("Your silence does not eclipse your arrogance."))
  1808.               @scene.pbHideOpponent      
  1809.             end
  1810.           elsif isConst?(trainertext.trainertype,PBTrainers,:BENNETT)
  1811.             if $game_variables[192] == 0
  1812.               @scene.pbShowOpponent(0)
  1813.               pbDisplayPaused(_INTL("I see, I see... You are at a highly advanced stage of metamorphosis already!"))
  1814.               @scene.pbHideOpponent
  1815.             elsif $game_variables[192] == 1
  1816.               @scene.pbShowOpponent(0)
  1817.               pbDisplayPaused(_INTL("Tch, she would never... No! I won't give up! I'll make her see!"))
  1818.               @scene.pbHideOpponent      
  1819.             end
  1820.           elsif isConst?(trainertext.trainertype,PBTrainers,:SERRA)
  1821.             if $game_variables[192] == 0
  1822.               @scene.pbShowOpponent(0)
  1823.               pbDisplayPaused(_INTL("Ice, like a mirror, will never be perfect again once it cracks."))
  1824.               @scene.pbHideOpponent
  1825.             end
  1826.           elsif isConst?(trainertext.trainertype,PBTrainers,:Radomus)
  1827.             if $game_variables[192] == 0
  1828.               @scene.pbShowOpponent(0)
  1829.               pbDisplayPaused(_INTL("Well, well, I am impressed."))
  1830.               @scene.pbHideOpponent
  1831.             end
  1832.           elsif isConst?(trainertext.trainertype,PBTrainers,:Noel)
  1833.             if $game_variables[192] == 0
  1834.               @scene.pbShowOpponent(0)
  1835.               pbDisplayPaused(_INTL("It's too bad. They can't always get what they want."))
  1836.               @scene.pbHideOpponent
  1837.             end
  1838.           elsif isConst?(trainertext.trainertype,PBTrainers,:LUNA)
  1839.             if $game_variables[192] == 0
  1840.               @scene.pbShowOpponent(0)
  1841.               pbDisplayPaused(_INTL("Oh? Perhaps I've lost my way after all. How wonderful!"))
  1842.               @scene.pbHideOpponent
  1843.             end
  1844.           elsif isConst?(trainertext.trainertype,PBTrainers,:SAMSON)
  1845.             if $game_variables[192] == 0
  1846.               @scene.pbShowOpponent(0)
  1847.               pbDisplayPaused(_INTL("No sweat. I've been in worse positions."))
  1848.               @scene.pbHideOpponent
  1849.             end
  1850.           elsif isConst?(trainertext.trainertype,PBTrainers,:EclipseDame)
  1851.             if $game_variables[192] == 0
  1852.               @scene.pbShowOpponent(0)
  1853.               pbDisplayPaused(_INTL("ECLIPSE: Seriously, can we stop this already?"))
  1854.               @scene.pbHideOpponent
  1855.             end
  1856.           elsif isConst?(trainertext.trainertype,PBTrainers,:CHARLOTTE)
  1857.             if $game_variables[192] == 0
  1858.               @scene.pbShowOpponent(0)
  1859.               pbDisplayPaused(_INTL("Ever seen a wildfire start from just a spark? ...D'you wanna?"))
  1860.               @scene.pbHideOpponent
  1861.             end
  1862.           elsif isConst?(trainertext.trainertype,PBTrainers,:AsterAce)
  1863.             if $game_variables[192] == 0
  1864.               @scene.pbShowOpponent(0)
  1865.               pbDisplayPaused(_INTL("Eclipse-- I'm sorry."))
  1866.               @scene.pbHideOpponent
  1867.             end
  1868.           elsif isConst?(trainertext.trainertype,PBTrainers,:TERRA)
  1869.             if $game_variables[192] == 0
  1870.               @scene.pbShowOpponent(0)
  1871.               pbDisplayPaused(_INTL("FUCK!!! THE WHAT!!!!!!!"))
  1872.               @scene.pbHideOpponent
  1873.             end
  1874.           elsif isConst?(trainertext.trainertype,PBTrainers,:CIEL)
  1875.             if $game_variables[192] == 0
  1876.               @scene.pbShowOpponent(0)
  1877.               pbDisplayPaused(_INTL("Hold your applause! You've yet to see the final act!"))
  1878.               @scene.pbHideOpponent
  1879.             end
  1880.           elsif isConst?(trainertext.trainertype,PBTrainers,:ARCLIGHT)
  1881.             if $game_variables[192] == 0
  1882.               @scene.pbShowOpponent(0)
  1883.               pbDisplayPaused(_INTL("You've got a good showing. But don't think I'm going off the air just yet!"))
  1884.               @scene.pbHideOpponent
  1885.             end
  1886.           elsif isConst?(trainertext.trainertype,PBTrainers,:ADRIENN)
  1887.             if $game_variables[192] == 0
  1888.               @scene.pbShowOpponent(0)
  1889.               pbDisplayPaused(_INTL("I haven't had a battle this exhilarating in ages-- even by my twisted timeline!"))
  1890.               @scene.pbHideOpponent
  1891.             end
  1892.           elsif isConst?(trainertext.trainertype,PBTrainers,:Exleader)
  1893.             if $game_variables[192] == 0
  1894.               @scene.pbShowOpponent(0)
  1895.               pbDisplayPaused(_INTL("This feels empty."))
  1896.               @scene.pbHideOpponent
  1897.             end
  1898.           elsif isConst?(trainertext.trainertype,PBTrainers,:Techlord)
  1899.             if $game_variables[192] == 2
  1900.               @scene.pbShowOpponent(0)
  1901.               pbBGMPlay("Rayquayza_Theme",100,150)
  1902.               pbDisplayPaused(_INTL("That was merely a warm-up. The real fight begins now."))
  1903.               @scene.pbHideOpponent
  1904.             end
  1905.           end
  1906.         end
  1907.         # For each defined trainer, add the ABOVE section for them
  1908.       end
  1909.     else
  1910.       @scene.pbSendOut(index,pokemon)
  1911.     end
  1912.     @scene.pbResetMoveIndex(index)
  1913.   end
  1914.  
  1915.   def pbReplace(index,newpoke,batonpass=false)
  1916.     party=pbParty(index)
  1917.     if pbOwnedByPlayer?(index)
  1918.       # Reorder the party for this battle
  1919.       bpo=-1; bpn=-1
  1920.       for i in 0...6
  1921.         bpo=i if @partyorder[i]==@battlers[index].pokemonIndex
  1922.         bpn=i if @partyorder[i]==newpoke
  1923.       end
  1924.       poke1=@partyorder[bpo]
  1925.       @partyorder[bpo]=@partyorder[bpn]
  1926.       @partyorder[bpn]=poke1
  1927.       @battlers[index].pbInitialize(party[newpoke],newpoke,batonpass)
  1928.       pbSendOut(index,party[newpoke])
  1929.     else
  1930.       @battlers[index].pbInitialize(party[newpoke],newpoke,batonpass)
  1931.       pbSetSeen(party[newpoke])
  1932.       if pbIsOpposing?(index)
  1933.         pbSendOut(index,party[newpoke])
  1934.       else
  1935.         pbSendOut(index,party[newpoke])
  1936.       end
  1937.     end
  1938.   end
  1939.  
  1940.   def pbRecallAndReplace(index,newpoke,batonpass=false)
  1941.     if @battlers[index].effects[PBEffects::Illusion]
  1942.       @battlers[index].effects[PBEffects::Illusion] = nil
  1943.     end
  1944.     @switchedOut[index] = true
  1945.     pbClearChoices(index)
  1946.     @battlers[index].pbResetForm
  1947.     if !@battlers[index].isFainted?
  1948.       @scene.pbRecall(index)
  1949.     end
  1950.     pbMessagesOnReplace(index,newpoke)
  1951.     pbReplace(index,newpoke,batonpass)
  1952.     @scene.partyBetweenKO2(!pbOwnedByPlayer?(index)) unless @doublebattle
  1953.     return pbOnActiveOne(@battlers[index])
  1954.   end
  1955.  
  1956.   def pbMessagesOnReplace(index,newpoke)
  1957.     party=pbParty(index)
  1958.     if pbOwnedByPlayer?(index)
  1959. #     if !party[newpoke]
  1960. #       p [index,newpoke,party[newpoke],pbAllFainted?(party)]
  1961. #       PBDebug.log([index,newpoke,party[newpoke],"pbMOR"].inspect)
  1962. #       for i in 0...party.length
  1963. #         PBDebug.log([i,party[i].hp].inspect)
  1964. #       end
  1965. #       raise BattleAbortedException.new
  1966. #     end
  1967. #### JERICHO - 001 - start
  1968.       if isConst?(party[newpoke].ability,PBAbilities,:ILLUSION) #ILLUSION
  1969.         party2=party.find_all {|item| item && !item.egg? && item.hp>0 }
  1970.         if party2[party.length-1] != party[newpoke]
  1971.           illusionpoke = party[party.length-1]
  1972.         end
  1973.       end #ILLUSION
  1974.       newname = illusionpoke != nil ? illusionpoke.name : party[newpoke].name      
  1975.       opposing=@battlers[index].pbOppositeOpposing
  1976.       if opposing.hp<=0 || opposing.hp==opposing.totalhp
  1977.         pbDisplayBrief(_INTL("Go! {1}!",newname))
  1978.       elsif opposing.hp>=(opposing.totalhp/2)
  1979.         pbDisplayBrief(_INTL("Do it! {1}!",newname))
  1980.       elsif opposing.hp>=(opposing.totalhp/4)
  1981.         pbDisplayBrief(_INTL("Go for it, {1}!",newname))
  1982.       else
  1983.         pbDisplayBrief(_INTL("Your foe's weak!\nGet 'em, {1}!",newname))
  1984.       end
  1985. #### JERICHO - 001 - END      
  1986.     else
  1987. #     if !party[newpoke]
  1988. #       p [index,newpoke,party[newpoke],pbAllFainted?(party)]
  1989. #       PBDebug.log([index,newpoke,party[newpoke],"pbMOR"].inspect)
  1990. #       for i in 0...party.length
  1991. #         PBDebug.log([i,party[i].hp].inspect)
  1992. #       end
  1993. #       raise BattleAbortedException.new
  1994. #     end
  1995. #### JERICHO - 001 - START    
  1996.       if isConst?(party[newpoke].ability,PBAbilities,:ILLUSION) #ILLUSION
  1997.         party2=party.find_all {|item| item && !item.egg? && item.hp>0 }
  1998.         if party2[party.length-1] != party[newpoke]
  1999.           illusionpoke = party[party.length-1]
  2000.         end
  2001.       end #ILLUSION
  2002.       if pbIsOpposing?(index)
  2003.         newname = illusionpoke != nil ? illusionpoke.name : PBSpecies.getName(party[newpoke].species) #ILLUSION
  2004.       else
  2005.         newname = illusionpoke != nil ? illusionpoke.name : party[newpoke].name #ILLUSION
  2006.       end
  2007.       owner=pbGetOwner(index)      
  2008.       pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",owner.fullname,newname)) #ILLUSION
  2009. #### JERICHO - 001 - END      
  2010.     end
  2011.   end
  2012.  
  2013.   def pbSwitchInBetween(index,lax,cancancel)
  2014.     if !pbOwnedByPlayer?(index)
  2015.       return @scene.pbChooseNewEnemy(index,pbParty(index))
  2016.     else
  2017.       return pbSwitchPlayer(index,lax,cancancel)
  2018.     end
  2019.   end
  2020.  
  2021.   def pbSwitchPlayer(index,lax,cancancel)
  2022.     if @debug
  2023.       return @scene.pbChooseNewEnemy(index,pbParty(index))
  2024.     else
  2025.       return @scene.pbSwitch(index,lax,cancancel)
  2026.     end
  2027.   end
  2028.  
  2029. ################################################################################
  2030. # Using an item.
  2031. ################################################################################
  2032. # Uses an item on a Pokémon in the player's party.
  2033.   def pbUseItemOnPokemon(item,pkmnIndex,userPkmn,scene)
  2034.     pokemon=@party1[pkmnIndex]
  2035.     battler=nil
  2036.     name=pbGetOwner(userPkmn.index).fullname
  2037.     name=pbGetOwner(userPkmn.index).name if pbBelongsToPlayer?(userPkmn.index)
  2038.     pbDisplayBrief(_INTL("{1} used the\r\n{2}.",name,PBItems.getName(item)))
  2039.     PBDebug.log("[Player used #{PBItems.getName(item)}]")
  2040.     ret=false
  2041.     if pokemon.isEgg?
  2042.       pbDisplay(_INTL("But it had no effect!"))
  2043.     else
  2044.       for i in 0...4
  2045.         if !pbIsOpposing?(i) && @battlers[i].pokemonIndex==pkmnIndex
  2046.           battler=@battlers[i]
  2047.         end
  2048.       end
  2049.       ret=ItemHandlers.triggerBattleUseOnPokemon(item,pokemon,battler,scene)
  2050.     end
  2051.     if !ret && pbBelongsToPlayer?(userPkmn.index)
  2052.       if $PokemonBag.pbCanStore?(item)
  2053.         $PokemonBag.pbStoreItem(item)
  2054.       else
  2055.         raise _INTL("Couldn't return unused item to Bag somehow.")
  2056.       end
  2057.     end
  2058.     return ret
  2059.   end
  2060.  
  2061. # Uses an item on an active Pokémon.
  2062.   def pbUseItemOnBattler(item,index,userPkmn,scene)
  2063.     PBDebug.log("[Player used #{PBItems.getName(item)}]")
  2064.     ret=ItemHandlers.triggerBattleUseOnBattler(item,@battlers[index],scene)
  2065.     if !ret && pbBelongsToPlayer?(userPkmn.index)
  2066.       if $PokemonBag.pbCanStore?(item)
  2067.         $PokemonBag.pbStoreItem(item)
  2068.       else
  2069.         raise _INTL("Couldn't return unused item to Bag somehow.")
  2070.       end
  2071.     end
  2072.     return ret
  2073.   end
  2074.  
  2075.   def pbRegisterItem(idxPokemon,idxItem,idxTarget=nil)
  2076.     if ItemHandlers.hasUseInBattle(idxItem)
  2077.       if idxPokemon==0
  2078.         if ItemHandlers.triggerBattleUseOnBattler(idxItem,@battlers[idxPokemon],self)
  2079.           ItemHandlers.triggerUseInBattle(idxItem,@battlers[idxPokemon],self)
  2080.           if @doublebattle
  2081.             @choices[idxPokemon+2][0]=3         # "Use an item"
  2082.             @choices[idxPokemon+2][1]=idxItem   # ID of item to be used
  2083.             @choices[idxPokemon+2][2]=idxTarget # Index of Pokémon to use item on
  2084.           end
  2085.         else
  2086.           return false
  2087.         end
  2088.       else
  2089.         if ItemHandlers.triggerBattleUseOnBattler(idxItem,@battlers[idxPokemon],self)
  2090.           pbDisplay(_INTL("It's impossible to aim without being focused!"))
  2091.         end
  2092.         return false
  2093.       end
  2094.     end
  2095.     @choices[idxPokemon][0]=3         # "Use an item"
  2096.     @choices[idxPokemon][1]=idxItem   # ID of item to be used
  2097.     @choices[idxPokemon][2]=idxTarget # Index of Pokémon to use item on
  2098.     side=(pbIsOpposing?(idxPokemon)) ? 1 : 0
  2099.     owner=pbGetOwnerIndex(idxPokemon)
  2100.     if @megaEvolution[side][owner]==idxPokemon
  2101.       @megaEvolution[side][owner]=-1
  2102.     end
  2103.     return true
  2104.   end
  2105.  
  2106.   def pbEnemyUseItem(item,battler)
  2107.     return 0 if !@internalbattle
  2108.     items=pbGetOwnerItems(battler.index)
  2109.     return if !items
  2110.     opponent=pbGetOwner(battler.index)
  2111.     for i in 0...items.length
  2112.       if items[i]==item
  2113.         items.delete_at(i)
  2114.         break
  2115.       end
  2116.     end
  2117.     itemname=PBItems.getName(item)
  2118.     pbDisplayBrief(_INTL("{1} used the\r\n{2}!",opponent.fullname,itemname))
  2119.     if isConst?(item,PBItems,:POTION)
  2120.       battler.pbRecoverHP(20,true)
  2121.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2122.     elsif isConst?(item,PBItems,:SUPERPOTION)
  2123.       battler.pbRecoverHP(50,true)
  2124.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2125.     elsif isConst?(item,PBItems,:HYPERPOTION)
  2126.       battler.pbRecoverHP(200,true)
  2127.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2128.     elsif isConst?(item,PBItems,:ULTRAPOTION)
  2129.       battler.pbRecoverHP(120,true)
  2130.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2131.     elsif isConst?(item,PBItems,:MOOMOOMILK)
  2132.       battler.pbRecoverHP(110,true)
  2133.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2134.     elsif isConst?(item,PBItems,:STRAWBIC)
  2135.       battler.pbRecoverHP(90,true)
  2136.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2137.     elsif isConst?(item,PBItems,:CHOCOLATEIC)
  2138.       battler.pbRecoverHP(70,true)
  2139.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2140.     elsif isConst?(item,PBItems,:MAXPOTION)
  2141.       battler.pbRecoverHP(battler.totalhp-battler.hp,true)
  2142.       pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2143.     elsif isConst?(item,PBItems,:FULLRESTORE)
  2144.       fullhp=(battler.hp==battler.totalhp)
  2145.       battler.pbRecoverHP(battler.totalhp-battler.hp,true)
  2146.       battler.status=0; battler.statusCount=0
  2147.       battler.effects[PBEffects::Confusion]=0
  2148.       if fullhp
  2149.         pbDisplay(_INTL("{1} became healthy!",battler.pbThis))
  2150.       else
  2151.         pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  2152.       end
  2153.     elsif isConst?(item,PBItems,:FULLHEAL)
  2154.       battler.status=0; battler.statusCount=0
  2155.       battler.effects[PBEffects::Confusion]=0
  2156.       pbDisplay(_INTL("{1} became healthy!",battler.pbThis))
  2157.     elsif isConst?(item,PBItems,:XATTACK)
  2158.       if battler.pbCanIncreaseStatStage?(PBStats::ATTACK)
  2159.         battler.pbIncreaseStat(PBStats::ATTACK,1,true)
  2160.       end
  2161.     elsif isConst?(item,PBItems,:XDEFEND)
  2162.       if battler.pbCanIncreaseStatStage?(PBStats::DEFENSE)
  2163.         battler.pbIncreaseStat(PBStats::DEFENSE,1,true)
  2164.       end
  2165.     elsif isConst?(item,PBItems,:XSPEED)
  2166.       if battler.pbCanIncreaseStatStage?(PBStats::SPEED)
  2167.         battler.pbIncreaseStat(PBStats::SPEED,1,true)
  2168.       end
  2169.     elsif isConst?(item,PBItems,:XSPECIAL)
  2170.       if battler.pbCanIncreaseStatStage?(PBStats::SPATK)
  2171.         battler.pbIncreaseStat(PBStats::SPATK,1,true)
  2172.       end
  2173.     elsif isConst?(item,PBItems,:XSPDEF)
  2174.       if battler.pbCanIncreaseStatStage?(PBStats::SPDEF)
  2175.         battler.pbIncreaseStat(PBStats::SPDEF,1,true)
  2176.       end
  2177.     elsif isConst?(item,PBItems,:XACCURACY)
  2178.       if battler.pbCanIncreaseStatStage?(PBStats::ACCURACY)
  2179.         battler.pbIncreaseStat(PBStats::ACCURACY,1,true)
  2180.       end
  2181.     end
  2182.   end
  2183.  
  2184. ################################################################################
  2185. # Fleeing from battle.
  2186. ################################################################################
  2187.   def pbCanRun?(idxPokemon)
  2188.     return false if @opponent
  2189.     thispkmn=@battlers[idxPokemon]
  2190.     return true if thispkmn.hasWorkingItem(:SMOKEBALL)
  2191.     return true if thispkmn.hasWorkingAbility(:RUNAWAY)
  2192.     return pbCanSwitch?(idxPokemon,-1,false)
  2193.   end
  2194.  
  2195.   def pbRun(idxPokemon,duringBattle=false)
  2196.     thispkmn=@battlers[idxPokemon]
  2197.     if pbIsOpposing?(idxPokemon)
  2198.       return 0 if @opponent
  2199.       @choices[i][0]=5 # run
  2200.       @choices[i][1]=0
  2201.       @choices[i][2]=nil
  2202.       return -1
  2203.     end
  2204.     if @opponent
  2205.       if $DEBUG && Input.press?(Input::CTRL)
  2206.         if pbDisplayConfirm(_INTL("Treat this battle as a win?"))
  2207.           @decision=1
  2208.           return 1
  2209.         elsif pbDisplayConfirm(_INTL("Treat this battle as a loss?"))
  2210.           @decision=2
  2211.           return 1
  2212.         end
  2213.       elsif @internalbattle
  2214.         pbDisplayPaused(_INTL("No!  There's no running from a Trainer battle!"))
  2215.       elsif pbDisplayConfirm(_INTL("Would you like to forfeit the match and quit now?"))
  2216.         pbDisplay(_INTL("{1} forfeited the match!",self.pbPlayer.name))
  2217.         @decision=3
  2218.         return 1
  2219.       end
  2220.       return 0
  2221.     end
  2222.     if $DEBUG && Input.press?(Input::CTRL)
  2223.       pbDisplayPaused(_INTL("Got away safely!"))
  2224.       @decision=3
  2225.       return 1
  2226.     end
  2227.     if @cantescape
  2228.       pbDisplayPaused(_INTL("Can't escape!"))
  2229.       return 0
  2230.     end
  2231.     if thispkmn.pbHasType?(:GHOST)
  2232.       pbDisplayPaused(_INTL("Got away safely!"))
  2233.       @decision=3
  2234.       return 1
  2235.     end
  2236.     if thispkmn.hasWorkingItem(:SMOKEBALL)
  2237.       if duringBattle
  2238.         pbDisplayPaused(_INTL("Got away safely!"))
  2239.       else
  2240.         pbDisplayPaused(_INTL("{1} fled using its {2}!",thispkmn.pbThis,PBItems.getName(thispkmn.item)))
  2241.       end
  2242.       @decision=3
  2243.       return 1
  2244.     end
  2245.     if thispkmn.hasWorkingAbility(:RUNAWAY)
  2246.       if duringBattle
  2247.         pbDisplayPaused(_INTL("Got away safely!"))
  2248.       else
  2249.         pbDisplayPaused(_INTL("{1} fled using Run Away!",thispkmn.pbThis))
  2250.       end
  2251.       @decision=3
  2252.       return 1
  2253.     end
  2254.     if !duringBattle && !pbCanSwitch?(idxPokemon,-1,false) # TODO: Use real messages
  2255.       pbDisplayPaused(_INTL("Can't escape!"))
  2256.       return 0
  2257.     end
  2258.     # Note: not pbSpeed, because using unmodified Speed
  2259.     speedPlayer=@battlers[idxPokemon].speed
  2260.     opposing=@battlers[idxPokemon].pbOppositeOpposing
  2261.     if opposing.isFainted?
  2262.       opposing=opposing.pbPartner
  2263.     end
  2264.     if !opposing.isFainted?
  2265.       speedEnemy=opposing.speed
  2266.       if speedPlayer>speedEnemy
  2267.         rate=256
  2268.       else
  2269.         speedEnemy=1 if speedEnemy<=0
  2270.         rate=speedPlayer*128/speedEnemy
  2271.         rate+=@runCommand*30
  2272.         rate&=0xFF
  2273.       end
  2274.     else
  2275.       rate=256
  2276.     end
  2277.     ret=1
  2278.     if pbAIRandom(256)<rate
  2279.       pbDisplayPaused(_INTL("Got away safely!"))
  2280.       @decision=3
  2281.     else
  2282.       pbDisplayPaused(_INTL("Can't escape!"))
  2283.       ret=-1
  2284.     end
  2285.     if !duringBattle
  2286.       @runCommand+=1
  2287.     end
  2288.     return ret
  2289.   end
  2290.  
  2291. ################################################################################
  2292. # Mega Evolve battler.
  2293. ################################################################################
  2294.   def pbCanMegaEvolve?(index)
  2295.     return false if $game_switches[NO_MEGA_EVOLUTION]
  2296.     return false if !@battlers[index].hasMega?
  2297.     return false if !pbHasMegaRing(index)
  2298.     side=(pbIsOpposing?(index)) ? 1 : 0
  2299.     owner=pbGetOwnerIndex(index)
  2300.     return false if @megaEvolution[side][owner]!=-1
  2301.     return true
  2302.   end
  2303.  
  2304.   def pbRegisterMegaEvolution(index)
  2305.     side=(pbIsOpposing?(index)) ? 1 : 0
  2306.     owner=pbGetOwnerIndex(index)
  2307.     @megaEvolution[side][owner]=index
  2308.   end
  2309.  
  2310.   def pbMegaEvolve(index)
  2311.     return if !@battlers[index] || !@battlers[index].pokemon
  2312.     return if !(@battlers[index].hasMega? rescue false)
  2313.     return if (@battlers[index].isMega? rescue true)
  2314.     ownername=pbGetOwner(index).fullname
  2315.     ownername=pbGetOwner(index).name if pbBelongsToPlayer?(index)
  2316.     if $game_switches[457] && @battlers[index].item==606
  2317.       pbDisplay(_INTL("{1}'s {2} is reacting to the PULSE machine!",
  2318.        @battlers[index].pbThis,
  2319.        PBItems.getName(@battlers[index].item),
  2320.        ownername))
  2321. #### KUROTSUNE - 005 - START
  2322.     elsif isConst?(@battlers[index].species, PBSpecies, :RAYQUAZA)
  2323.       pbDisplay(_INTL("{1}'s fervent wish has reached {2}!",
  2324.        ownername,
  2325.        @battlers[index].pbThis))        
  2326. #### KUROTSUNE - 005 - END
  2327.     else
  2328.       pbDisplay(_INTL("{1}'s {2} is reacting to {3}'s {4}!",
  2329.          @battlers[index].pbThis,PBItems.getName(@battlers[index].item),
  2330.          ownername,pbGetMegaRingName(index)))
  2331.     end    
  2332.     if $game_switches[457] && @battlers[index].item==606
  2333.       pbCommonAnimation("PulseEvolution",@battlers[index],nil)
  2334.     else
  2335.       pbCommonAnimation("MegaEvolution",@battlers[index],nil)
  2336.     end
  2337.     @battlers[index].pokemon.makeMega
  2338.     @battlers[index].form=@battlers[index].pokemon.form
  2339.     @battlers[index].pbUpdate(true)
  2340.     @scene.pbChangePokemon(@battlers[index],@battlers[index].pokemon)
  2341.     meganame=@battlers[index].pokemon.megaName
  2342.     if !meganame || meganame==""
  2343.       meganame=_INTL("Mega {1}",PBSpecies.getName(@battlers[index].pokemon.species))
  2344.     end
  2345.     if $game_switches[457] && @battlers[index].item==606
  2346.       pbDisplay(_INTL("{1} mutated into {2}!",@battlers[index].pbThis,meganame))
  2347.     else
  2348.       pbDisplay(_INTL("{1} Mega Evolved into {2}!",@battlers[index].pbThis,meganame))
  2349.     end    
  2350.     side=(pbIsOpposing?(index)) ? 1 : 0
  2351.     owner=pbGetOwnerIndex(index)
  2352.     @megaEvolution[side][owner]=-2
  2353. #### KUROTSUNE - 006 - START
  2354.     @battlers[index].pbAbilitiesOnSwitchIn(true)
  2355. #### KUROTSUNE - 006 - END
  2356. end
  2357.  
  2358.  
  2359. ################################################################################
  2360. # Call battler.
  2361. ################################################################################
  2362.   def pbCall(index)
  2363.     owner=pbGetOwner(index)
  2364.     pbDisplay(_INTL("{1} called {2}!",owner.name,@battlers[index].name))
  2365.     pbDisplay(_INTL("{1}!",@battlers[index].name))
  2366.     if @battlers[index].isShadow?
  2367.       if @battlers[index].inHyperMode?
  2368.         @battlers[index].pokemon.hypermode=false
  2369.         @battlers[index].pokemon.adjustHeart(-300)
  2370.         pbDisplay(_INTL("{1} came to its senses from the Trainer's call!",@battlers[index].pbThis))
  2371.       else
  2372.         pbDisplay(_INTL("But nothing happened!"))
  2373.       end
  2374.     elsif @battlers[index].status!=PBStatuses::SLEEP &&
  2375.           @battlers[index].pbCanIncreaseStatStage?(PBStats::ACCURACY)
  2376.       @battlers[index].pbIncreaseStat(PBStats::ACCURACY,1,true)
  2377.     else
  2378.       pbDisplay(_INTL("But nothing happened!"))
  2379.     end
  2380.   end
  2381.  
  2382. ################################################################################
  2383. # Gaining Experience.
  2384. ################################################################################
  2385.   def pbGainEXP
  2386.     return if !@internalbattle
  2387.     successbegin=true
  2388.     for i in 0...4 # Not ordered by priority
  2389.       if !@doublebattle && pbIsDoubleBattler?(i)
  2390.         @battlers[i].participants=[]
  2391.         next
  2392.       end
  2393.       if pbIsOpposing?(i) && @battlers[i].participants.length>0 && @battlers[i].isFainted?
  2394.         battlerSpecies=@battlers[i].pokemon.species
  2395.         # Original species, not current species
  2396.         baseexp=@battlers[i].pokemon.baseExp
  2397.         level=@battlers[i].level
  2398.         # First count the number of participants
  2399.         partic=0
  2400.         expshare=0
  2401.         for j in @battlers[i].participants
  2402.           next if !@party1[j] || !pbIsOwner?(0,j)
  2403.           partic+=1 if @party1[j].hp>0 && !@party1[j].isEgg?
  2404.         end
  2405.         for j in 0...@party1.length
  2406.           next if !@party1[j] || !pbIsOwner?(0,j)
  2407.           expshare+=1 if @party1[j].hp>0 && !@party1[j].isEgg? &&
  2408.              (isConst?(@party1[j].item,PBItems,:EXPSHARE) ||
  2409.               isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE))
  2410.         end
  2411.         # Now calculate EXP for the participants
  2412.         if partic>0 || expshare>0
  2413.           if !@opponent && successbegin && pbAllFainted?(@party2)
  2414.             @scene.pbWildBattleSuccess
  2415.             successbegin=false
  2416.           end
  2417.           for j in 0...@party1.length
  2418.             thispoke=@party1[j]
  2419.             next if !@party1[j] || !pbIsOwner?(0,j)
  2420.             ispartic=0
  2421.             haveexpshare=(isConst?(thispoke.item,PBItems,:EXPSHARE) ||
  2422.                           isConst?(thispoke.itemInitial,PBItems,:EXPSHARE)) ? 1 : 0
  2423.             for k in @battlers[i].participants
  2424.               ispartic=1 if k==j
  2425.             end
  2426.             if thispoke.hp>0 && !thispoke.isEgg?
  2427.               exp=0
  2428.               if expshare>0
  2429.                 if partic==0
  2430.                   exp=(level*baseexp).floor
  2431.                   exp=(exp/expshare).floor*haveexpshare
  2432.                 else
  2433.                   exp=(level*baseexp/2).floor
  2434.                   exp=(exp/partic).floor*ispartic + (exp/expshare).floor*haveexpshare
  2435.                 end
  2436.               elsif ispartic==1
  2437.                 exp=(level*baseexp/partic).floor
  2438.               end
  2439.               exp=(exp*3/2).floor if @opponent
  2440.               if USENEWEXPFORMULA   # Use new (Gen 5) Exp. formula
  2441. #### COMMANDER - 003 - START
  2442. #### COMMANDER - 003 - END                                  
  2443.                 exp=(exp/5).floor
  2444.                 leveladjust=(2*level+10.0)/(level+thispoke.level+10.0)
  2445.                 leveladjust=leveladjust**5
  2446.                 leveladjust=Math.sqrt(leveladjust)
  2447.                 exp=(exp*leveladjust).floor
  2448.                 exp+=1 if ispartic>0 || haveexpshare>0
  2449.               else                  # Use old (Gen 1-4) Exp. formula
  2450.                 exp=(exp/7).floor
  2451.               end
  2452. #### COMMANDER - XX1 - START
  2453.                             $game_switches[1218] = false
  2454.               almostLimit = false
  2455.  
  2456.               levelLimits = [20, 25, 32, 35, 40, 42, 45, 50, 55, 60, 62, 65, 70, 72, 75, 80, 85, 88, 90, 95, 100]
  2457.               leadersDefeated = pbPlayer.numbadges
  2458.               if $game_variables[107]>=1
  2459.                 leadersDefeated += 2
  2460.               elsif $game_variables[77]>=1
  2461.                 leadersDefeated += 1
  2462.               end
  2463.                                    
  2464.               if thispoke.level>=levelLimits[leadersDefeated]
  2465.                 exp = 0
  2466.                 $game_switches[1218] = true
  2467.               elsif thispoke.level == levelLimits[leadersDefeated] - 1
  2468.                 almostLimit = true
  2469.               end
  2470. #### COMMANDER - XX1 - END
  2471.              
  2472.   #            print("#{thispoke.trainerID}")
  2473.               isOutsider=((thispoke.trainerID != self.pbPlayer.id &&
  2474.                  thispoke.trainerID != 0) ||
  2475.                  (thispoke.language!=0 && thispoke.language!=self.pbPlayer.language))||$game_variables[975] != 0
  2476.    #           print("#{isOutsider}")
  2477.               if isOutsider
  2478.                 if thispoke.language!=0 && thispoke.language!=self.pbPlayer.language
  2479.                   exp=(exp*17/10).floor
  2480.                 else
  2481.                   exp=(exp*3/2).floor
  2482.                 end
  2483.               end
  2484.               exp=(exp*3/2).floor if isConst?(thispoke.item,PBItems,:LUCKYEGG) ||
  2485.                                      isConst?(thispoke.itemInitial,PBItems,:LUCKYEGG)
  2486.               growthrate=thispoke.growthrate
  2487. #### COMMANDER - XX2 - START    
  2488.               if almostLimit
  2489.                 totalExpNeeded = PBExperience.pbGetStartExperience(levelLimits[leadersDefeated],growthrate)
  2490.                 currExpNeeded = totalExpNeeded - thispoke.exp
  2491.                 if exp > currExpNeeded
  2492.                   exp = currExpNeeded
  2493.                 end
  2494.               end
  2495. #### COMMANDER - XX2 - END
  2496.               newexp=PBExperience.pbAddExperience(thispoke.exp,exp,growthrate)
  2497.               exp=newexp-thispoke.exp
  2498.               if exp > 0 || $game_switches[1218]
  2499. #### KUROTSUNE - 020 - START
  2500.                 if isOutsider || isConst?(thispoke.item,PBItems,:LUCKYEGG)
  2501. #### KUROTSUNE - 020 - END
  2502.                   pbDisplayPaused(_INTL("{1} gained a boosted {2} Exp. Points!",thispoke.name,exp))if exp > 0
  2503.                 elsif exp > 0
  2504.                   pbDisplayPaused(_INTL("{1} gained {2} Exp. Points!",thispoke.name,exp))
  2505.                 end
  2506.                 #Gain effort value points, using RS effort values
  2507.                 totalev=0
  2508.                 for k in 0..5
  2509.                   totalev+=thispoke.ev[k]
  2510.                 end
  2511.                 # Original species, not current species
  2512.                 evyield=@battlers[i].pokemon.evYield
  2513.                 for k in 0..5
  2514.                   evgain=evyield[k]
  2515.                   evgain*=2 if isConst?(thispoke.item,PBItems,:MACHOBRACE) ||
  2516.                                isConst?(thispoke.itemInitial,PBItems,:MACHOBRACE)
  2517.                   case k
  2518.                     when 0
  2519.                       if isConst?(thispoke.item,PBItems,:POWERWEIGHT)
  2520.                         evgain+=4
  2521.                       end
  2522.                     when 1
  2523.                       if isConst?(thispoke.item,PBItems,:POWERBRACER)
  2524.                         evgain+=4
  2525.                       end
  2526.                     when 2
  2527.                       if isConst?(thispoke.item,PBItems,:POWERBELT)
  2528.                         evgain+=4
  2529.                       end
  2530.                     when 3
  2531.                       if isConst?(thispoke.item,PBItems,:POWERANKLET)
  2532.                         evgain+=4
  2533.                       end
  2534.                     when 4
  2535.                       if isConst?(thispoke.item,PBItems,:POWERLENS)
  2536.                         evgain+=4
  2537.                       end
  2538.                     when 5
  2539.                       if isConst?(thispoke.item,PBItems,:POWERBAND)
  2540.                         evgain+=4
  2541.                       end
  2542.                   end
  2543.                   evgain*=2 if thispoke.pokerusStage>=1 # Infected or cured
  2544.                   if evgain>0
  2545.                     # Can't exceed overall limit
  2546.                     if totalev+evgain>510
  2547.                       evgain-=totalev+evgain-510
  2548.                     end
  2549.                     # Can't exceed stat limit
  2550.                     if thispoke.ev[k]+evgain>252
  2551.                       evgain-=thispoke.ev[k]+evgain-252
  2552.                     end
  2553.                     # Add EV gain
  2554.                     thispoke.ev[k]+=evgain
  2555.                     if thispoke.ev[k]>252
  2556.                       print "Single-stat EV limit 252 exceeded.\r\nStat: #{k}  EV gain: #{evgain}  EVs: #{thispoke.ev.inspect}"
  2557.                       thispoke.ev[k]=252
  2558.                     end
  2559.                     totalev+=evgain
  2560.                     if totalev>510
  2561.                       print "EV limit 510 exceeded.\r\nTotal EVs: #{totalev} EV gain: #{evgain}  EVs: #{thispoke.ev.inspect}"
  2562.                     end
  2563.                   end
  2564.                 end
  2565.                 newlevel=PBExperience.pbGetLevelFromExperience(newexp,growthrate)
  2566.                 tempexp=0
  2567.                 curlevel=thispoke.level
  2568.                 thisPokeSpecies=thispoke.species
  2569.                 if newlevel<curlevel
  2570.                   debuginfo="#{thispoke.name}: #{thispoke.level}/#{newlevel} | #{thispoke.exp}/#{newexp} | gain: #{exp}"
  2571.                   raise RuntimeError.new(
  2572.                      _INTL("The new level ({1}) is less than the Pokémon's\r\ncurrent level ({2}), which shouldn't happen.\r\n[Debug: {3}]",
  2573.                      newlevel,curlevel,debuginfo))
  2574.                   return
  2575.                 end
  2576.                 if thispoke.respond_to?("isShadow?") && thispoke.isShadow?
  2577.                   thispoke.exp+=exp
  2578.                 else
  2579.                   tempexp1=thispoke.exp
  2580.                   tempexp2=0
  2581.                   # Find battler
  2582.                   battler=pbFindPlayerBattler(j)
  2583.                   loop do
  2584.                     #EXP Bar animation
  2585.                     startexp=PBExperience.pbGetStartExperience(curlevel,growthrate)
  2586.                     endexp=PBExperience.pbGetStartExperience(curlevel+1,growthrate)
  2587.                     tempexp2=(endexp<newexp) ? endexp : newexp
  2588.                     thispoke.exp=tempexp2
  2589.                     @scene.pbEXPBar(thispoke,battler,startexp,endexp,tempexp1,tempexp2)
  2590.                     tempexp1=tempexp2
  2591.                     curlevel+=1
  2592.                     if curlevel>newlevel
  2593.                       thispoke.calcStats
  2594.                       battler.pbUpdate(false) if battler
  2595.                       @scene.pbRefresh
  2596.                       break
  2597.                     end
  2598.                     oldtotalhp=thispoke.totalhp
  2599.                     oldattack=thispoke.attack
  2600.                     olddefense=thispoke.defense
  2601.                     oldspeed=thispoke.speed
  2602.                     oldspatk=thispoke.spatk
  2603.                     oldspdef=thispoke.spdef
  2604.                     if battler
  2605.                       if battler.pokemon && @internalbattle
  2606.                         battler.pokemon.changeHappiness("level up")
  2607.                       end
  2608.                     end
  2609.                     thispoke.calcStats
  2610.                     battler.pbUpdate(false) if battler
  2611.                     @scene.pbRefresh
  2612.                     pbDisplayPaused(_INTL("{1} grew to Level {2}!",thispoke.name,curlevel))
  2613.                     @scene.pbLevelUp(thispoke,battler,oldtotalhp,oldattack,
  2614.                        olddefense,oldspeed,oldspatk,oldspdef)
  2615.                     # Finding all moves learned at this level
  2616.                     movelist=thispoke.getMoveList
  2617.                     for k in movelist
  2618.                       if k[0]==thispoke.level   # Learned a new move
  2619.                         pbLearnMove(j,k[1])
  2620.                       end
  2621.                     end
  2622.                   end
  2623.                 end
  2624.               end
  2625.             end
  2626.           end
  2627.         end
  2628.         # Now clear the participants array
  2629.         @battlers[i].participants=[]
  2630.       end
  2631.     end
  2632.   end
  2633.  
  2634. ################################################################################
  2635. # Learning a move.
  2636. ################################################################################
  2637.   def pbLearnMove(pkmnIndex,move)
  2638.     pokemon=@party1[pkmnIndex]
  2639.     return if !pokemon
  2640.     pkmnname=pokemon.name
  2641.     battler=pbFindPlayerBattler(pkmnIndex)
  2642.     movename=PBMoves.getName(move)
  2643.     for i in 0...4
  2644.       if pokemon.moves[i].id==move
  2645.         return
  2646.       end
  2647.       if pokemon.moves[i].id==0
  2648.         pokemon.moves[i]=PBMove.new(move)
  2649.         battler.moves[i]=PokeBattle_Move.pbFromPBMove(self,pokemon.moves[i]) if battler
  2650.         pbDisplayPaused(_INTL("{1} learned {2}!",pkmnname,movename))
  2651.         return
  2652.       end
  2653.     end
  2654.     loop do
  2655.       pbDisplayPaused(_INTL("{1} is trying to learn {2}.",pkmnname,movename))
  2656.       pbDisplayPaused(_INTL("But {1} can't learn more than four moves.",pkmnname))
  2657.       if pbDisplayConfirm(_INTL("Delete a move to make room for {1}?",movename))
  2658.         pbDisplayPaused(_INTL("Which move should be forgotten?"))
  2659.         forgetmove=@scene.pbForgetMove(pokemon,move)
  2660.         if forgetmove >=0
  2661.           oldmovename=PBMoves.getName(pokemon.moves[forgetmove].id)
  2662.           pokemon.moves[forgetmove]=PBMove.new(move) # Replaces current/total PP
  2663.           battler.moves[forgetmove]=PokeBattle_Move.pbFromPBMove(self,pokemon.moves[forgetmove]) if battler
  2664.           pbDisplayPaused(_INTL("1,  2, and... ... ..."))
  2665.           pbDisplayPaused(_INTL("Poof!"))
  2666.           pbDisplayPaused(_INTL("{1} forgot {2}.",pkmnname,oldmovename))
  2667.           pbDisplayPaused(_INTL("And..."))
  2668.           pbDisplayPaused(_INTL("{1} learned {2}!",pkmnname,movename))
  2669.           return
  2670.         elsif pbDisplayConfirm(_INTL("Should {1} stop learning {2}?",pkmnname,movename))
  2671.           pbDisplayPaused(_INTL("{1} did not learn {2}.",pkmnname,movename))
  2672.           return
  2673.         end
  2674.       elsif pbDisplayConfirm(_INTL("Should {1} stop learning {2}?",pkmnname,movename))
  2675.         pbDisplayPaused(_INTL("{1} did not learn {2}.",pkmnname,movename))
  2676.         return
  2677.       end
  2678.     end
  2679.   end
  2680.  
  2681. ################################################################################
  2682. # Abilities.
  2683. ################################################################################
  2684.   def pbOnActiveAll
  2685.     for i in 0...4 # Currently unfainted participants will earn EXP even if they faint afterwards
  2686.       @battlers[i].pbUpdateParticipants if pbIsOpposing?(i)
  2687.       @amuletcoin=true if !pbIsOpposing?(i) &&
  2688.                           (isConst?(@battlers[i].item,PBItems,:AMULETCOIN) ||
  2689.                            isConst?(@battlers[i].item,PBItems,:LUCKINCENSE))
  2690.     end
  2691.     for i in 0...4
  2692.       if !@battlers[i].isFainted?
  2693.         if @battlers[i].isShadow? && pbIsOpposing?(i)
  2694.           pbCommonAnimation("Shadow",@battlers[i],nil)
  2695.           pbDisplay(_INTL("Oh!\nA Shadow Pokemon!"))
  2696.         end
  2697.       end
  2698.     end
  2699.     # Weather-inducing abilities, Trace, Imposter, etc.
  2700.     @usepriority=false
  2701.     priority=pbPriority
  2702.     for i in priority
  2703.       i.pbAbilitiesOnSwitchIn(true)
  2704.     end
  2705.     # Check forms are correct
  2706.     for i in 0...4
  2707.       next if @battlers[i].isFainted?
  2708.       @battlers[i].pbCheckForm
  2709.     end
  2710.   end
  2711.  
  2712.   def pbOnActiveOne(pkmn,onlyabilities=false)
  2713.     return false if pkmn.isFainted?
  2714.     if !onlyabilities
  2715.       for i in 0...4 # Currently unfainted participants will earn EXP even if they faint afterwards
  2716.         @battlers[i].pbUpdateParticipants if pbIsOpposing?(i)
  2717.         @amuletcoin=true if !pbIsOpposing?(i) &&
  2718.                             (isConst?(@battlers[i].item,PBItems,:AMULETCOIN) ||
  2719.                              isConst?(@battlers[i].item,PBItems,:LUCKINCENSE))
  2720.       end
  2721.       if pkmn.isShadow? && pbIsOpposing?(pkmn.index)
  2722.         pbCommonAnimation("Shadow",pkmn,nil)
  2723.         pbDisplay(_INTL("Oh!\nA Shadow Pokemon!"))
  2724.       end
  2725.       # Healing Wish
  2726.       if pkmn.effects[PBEffects::HealingWish]
  2727.         pkmn.pbRecoverHP(pkmn.totalhp,true)
  2728.         pkmn.status=0
  2729.         pkmn.statusCount=0
  2730.         pkmn.pbIncreaseStat(PBStats::ATTACK, 1, true) if $fefieldeffect == 31 || $fefieldeffect == 34
  2731.         pkmn.pbIncreaseStat(PBStats::SPATK, 1, true) if $fefieldeffect == 31 || $fefieldeffect == 34
  2732.         pbDisplayPaused(_INTL("The healing wish came true for {1}!",pkmn.pbThis(true)))
  2733.         pkmn.effects[PBEffects::HealingWish]=false
  2734.       end
  2735.       # Lunar Dance
  2736.       if pkmn.effects[PBEffects::LunarDance]
  2737.         pkmn.pbRecoverHP(pkmn.totalhp,true)
  2738.         pkmn.status=0
  2739.         pkmn.statusCount=0
  2740.         pkmn.pbIncreaseStat(PBStats::ATTACK, 1, true) if $fefieldeffect == 35 || $fefieldeffect == 34
  2741.         pkmn.pbIncreaseStat(PBStats::DEFENSE, 1, true) if $fefieldeffect == 35
  2742.         pkmn.pbIncreaseStat(PBStats::SPATK, 1, true) if $fefieldeffect == 35 || $fefieldeffect == 34
  2743.         pkmn.pbIncreaseStat(PBStats::SPDEF, 1, true) if $fefieldeffect == 35
  2744.         pkmn.pbIncreaseStat(PBStats::SPEED, 1, true) if $fefieldeffect == 35
  2745.         pkmn.pbIncreaseStat(PBStats::ACCURACY, 1, true) if $fefieldeffect == 35
  2746.         pkmn.pbIncreaseStat(PBStats::EVASION, 1, true) if $fefieldeffect == 35
  2747.         for i in 0...4
  2748.           pkmn.moves[i].pp=pkmn.moves[i].totalpp
  2749.         end
  2750.         pbDisplayPaused(_INTL("{1} became cloaked in mystical moonlight!",pkmn.pbThis))
  2751.         pkmn.effects[PBEffects::LunarDance]=false
  2752.       end
  2753.       # Spikes
  2754.       pkmn.pbOwnSide.effects[PBEffects::Spikes]=0 if $fefieldeffect == 21 ||
  2755.        $fefieldeffect == 26
  2756.       if pkmn.pbOwnSide.effects[PBEffects::Spikes]>0
  2757.         if !pkmn.isAirborne?
  2758.           if !pkmn.hasWorkingAbility(:MAGICGUARD)
  2759.             spikesdiv=[8,8,6,4][pkmn.pbOwnSide.effects[PBEffects::Spikes]]
  2760.             @scene.pbDamageAnimation(pkmn,0)
  2761.             pkmn.pbReduceHP([(pkmn.totalhp/spikesdiv).floor,1].max)
  2762.             pbDisplay(_INTL("{1} was hurt by spikes!",pkmn.pbThis))
  2763.           end
  2764.         end
  2765.       end
  2766.       pkmn.pbFaint if pkmn.isFainted?
  2767.       # Stealth Rock
  2768.       if pkmn.pbOwnSide.effects[PBEffects::StealthRock]
  2769.         if !pkmn.hasWorkingAbility(:MAGICGUARD)
  2770.           atype=getConst(PBTypes,:ROCK) || 0
  2771.           if $fefieldeffect == 25
  2772.               randtype = pbRandom(4)
  2773.               case randtype
  2774.                 when 0
  2775.                   atype=getConst(PBTypes,:WATER) || 0
  2776.                 when 1
  2777.                   atype=getConst(PBTypes,:GRASS) || 0
  2778.                 when 2
  2779.                   atype=getConst(PBTypes,:FIRE) || 0
  2780.                 when 3
  2781.                   atype=getConst(PBTypes,:PSYCHIC) || 0
  2782.               end
  2783.           end
  2784.           eff=PBTypes.getCombinedEffectiveness(atype,pkmn.type1,pkmn.type2)
  2785.           if eff>0
  2786.             if $fefieldeffect == 14 || $fefieldeffect == 23
  2787.               eff = eff*2
  2788.             end
  2789.             @scene.pbDamageAnimation(pkmn,0)
  2790.             pkmn.pbReduceHP([(pkmn.totalhp*eff/32).floor,1].max)
  2791.             if $fefieldeffect == 25
  2792.               pbDisplay(_INTL("{1} was hurt by the crystalized stealth rocks!",pkmn.pbThis))
  2793.             else
  2794.               pbDisplay(_INTL("{1} was hurt by stealth rocks!",pkmn.pbThis))
  2795.             end
  2796.           end
  2797.         end
  2798.       end
  2799.       pkmn.pbFaint if pkmn.isFainted?
  2800.       # Corrosive Field Entry    
  2801.       if $fefieldeffect == 10      
  2802.         if !pkmn.hasWorkingAbility(:MAGICGUARD) &&        
  2803.          !pkmn.hasWorkingAbility(:POISONHEAL) &&      
  2804.          !pkmn.hasWorkingAbility(:IMMUNITY) &&
  2805.          !pkmn.hasWorkingAbility(:WONDERGUARD) &&
  2806.          !pkmn.hasWorkingAbility(:TOXICBOOST)
  2807.           if !pkmn.isAirborne?
  2808.             if !pkmn.pbHasType?(:POISON) && !pkmn.pbHasType?(:STEEL)
  2809.               atype=getConst(PBTypes,:POISON) || 0
  2810.               eff=PBTypes.getCombinedEffectiveness(atype,pkmn.type1,pkmn.type2)
  2811.               if eff>0
  2812.                 eff=eff*2
  2813.                 @scene.pbDamageAnimation(pkmn,0)
  2814.                 pkmn.pbReduceHP([(pkmn.totalhp*eff/32).floor,1].max)
  2815.                 pbDisplay(_INTL("{1} was seared by the corrosion!",pkmn.pbThis))
  2816.               end
  2817.             end
  2818.           end
  2819.         end
  2820.       end
  2821.       pkmn.pbFaint if pkmn.hp<=0
  2822.       # Sticky Web
  2823.       if pkmn.pbOwnSide.effects[PBEffects::StickyWeb]
  2824.         if !pkmn.isAirborne?
  2825.           if $fefieldeffect == 15
  2826.             #StickyWebMessage
  2827.             pbDisplay(_INTL("{1} was caught in a sticky web!",pkmn.pbThis))
  2828.             pkmn.pbReduceStat(PBStats::SPEED, 2, true)
  2829.           else
  2830.             pbDisplay(_INTL("{1} was caught in a sticky web!",pkmn.pbThis))
  2831.             pkmn.pbReduceStat(PBStats::SPEED, 1, true)
  2832.           end
  2833.         end
  2834.       end
  2835.       # Toxic Spikes
  2836.       pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]=0 if $fefieldeffect == 21 ||
  2837.        $fefieldeffect == 26
  2838.       if pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]>0
  2839.         if !pkmn.isAirborne?
  2840.           if pkmn.pbHasType?(:POISON) && $fefieldeffect != 10
  2841.             pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]=0
  2842.             pbDisplay(_INTL("{1} absorbed the poison spikes!",pkmn.pbThis))          elsif pkmn.pbCanPoisonSpikes?
  2843.             if pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]==2
  2844.               pkmn.pbPoison(pkmn,true)
  2845.               pbDisplay(_INTL("{1} was badly poisoned!",pkmn.pbThis))
  2846.             else
  2847.               pkmn.pbPoison(pkmn)
  2848.               pbDisplay(_INTL("{1} was poisoned!",pkmn.pbThis))
  2849.             end
  2850.           end
  2851.         end
  2852.       end
  2853.     end
  2854.     pkmn.pbAbilityCureCheck
  2855.     if pkmn.isFainted?
  2856.       pbGainEXP
  2857.       pbSwitch if @faintswitch
  2858.       return false
  2859.     end
  2860.     #pkmn.pbAbilitiesOnSwitchIn(true)
  2861.     if !onlyabilities
  2862.       pkmn.pbCheckForm
  2863.       pkmn.pbBerryCureCheck
  2864.     end
  2865.     return true
  2866.   end
  2867.  
  2868. ################################################################################
  2869. # Judging.
  2870. ################################################################################
  2871.   def pbJudgeCheckpoint(attacker,move=0)
  2872.   end
  2873.  
  2874.   def pbDecisionOnTime
  2875.     count1=0
  2876.     count2=0
  2877.     hptotal1=0
  2878.     hptotal2=0
  2879.     for i in @party1
  2880.       next if !i
  2881.       if i.hp>0 && !i.isEgg?
  2882.         count1+=1
  2883.         hptotal1+=i.hp
  2884.       end
  2885.     end
  2886.     for i in @party2
  2887.       next if !i
  2888.       if i.hp>0 && !i.isEgg?
  2889.         count2+=1
  2890.         hptotal2+=i.hp
  2891.       end
  2892.     end
  2893.     return 1 if count1>count2     # win
  2894.     return 2 if count1<count2     # loss
  2895.     return 1 if hptotal1>hptotal2 # win
  2896.     return 2 if hptotal1<hptotal2 # loss
  2897.     return 5                      # draw
  2898.   end
  2899.  
  2900.   def pbDecisionOnTime2
  2901.     count1=0
  2902.     count2=0
  2903.     hptotal1=0
  2904.     hptotal2=0
  2905.     for i in @party1
  2906.       next if !i
  2907.       if i.hp>0 && !i.isEgg?
  2908.         count1+=1
  2909.         hptotal1+=(i.hp*100/i.totalhp)
  2910.       end
  2911.     end
  2912.     hptotal1/=count1 if count1>0
  2913.     for i in @party2
  2914.       next if !i
  2915.       if i.hp>0 && !i.isEgg?
  2916.         count2+=1
  2917.         hptotal2+=(i.hp*100/i.totalhp)
  2918.       end
  2919.     end
  2920.     hptotal2/=count2 if count2>0
  2921.     return 1 if count1>count2     # win
  2922.     return 2 if count1<count2     # loss
  2923.     return 1 if hptotal1>hptotal2 # win
  2924.     return 2 if hptotal1<hptotal2 # loss
  2925.     return 5                      # draw
  2926.   end
  2927.  
  2928.   def pbDecisionOnDraw
  2929.     return 5 # draw
  2930.   end
  2931.  
  2932.   def pbJudge
  2933. #   PBDebug.log("[Counts: #{pbPokemonCount(@party1)}/#{pbPokemonCount(@party2)}]")
  2934.     if pbAllFainted?(@party1) && pbAllFainted?(@party2)
  2935.       @decision=pbDecisionOnDraw() # Draw
  2936.       return
  2937.     end
  2938.     if pbAllFainted?(@party1)
  2939.       @decision=2 # Loss
  2940.       return
  2941.     end
  2942.     if pbAllFainted?(@party2)
  2943.       @decision=1 # Win
  2944.       return
  2945.     end
  2946.   end
  2947.  
  2948. ################################################################################
  2949. # Messages and animations.
  2950. ################################################################################
  2951.   def pbApplySceneBG(sprite,filename)
  2952.     @scene.pbApplyBGSprite(sprite,filename)
  2953.   end
  2954.  
  2955.   def pbDisplay(msg)
  2956.     @scene.pbDisplayMessage(msg)
  2957.   end
  2958.  
  2959.   def pbDisplayPaused(msg)
  2960.     @scene.pbDisplayPausedMessage(msg)
  2961.   end
  2962.  
  2963.   def pbDisplayBrief(msg)
  2964.     @scene.pbDisplayMessage(msg,true)
  2965.   end
  2966.  
  2967.   def pbDisplayConfirm(msg)
  2968.     @scene.pbDisplayConfirmMessage(msg)
  2969.   end
  2970.  
  2971.   def pbShowCommands(msg,commands,cancancel=true)
  2972.     @scene.pbShowCommands(msg,commands,cancancel)
  2973.   end
  2974.  
  2975.   def pbAnimation(move,attacker,opponent,hitnum=0)
  2976.     if @battlescene
  2977.       @scene.pbAnimation(move,attacker,opponent,hitnum)
  2978.     end
  2979.   end
  2980.  
  2981.   def pbCommonAnimation(name,attacker,opponent,hitnum=0)
  2982.     if @battlescene
  2983.       @scene.pbCommonAnimation(name,attacker,opponent,hitnum)
  2984.     end
  2985.   end
  2986.  
  2987. def pbChangeBGSprite
  2988.         case $fefieldeffect
  2989.           when 0 # indoor
  2990.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgIndoorA.png")
  2991.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseIndoorA.png")
  2992.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseIndoorA.png")
  2993.           when 1 # electric
  2994.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgElectric.png")
  2995.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseElectric.png")
  2996.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseElectric.png")
  2997.           when 2 # grassy
  2998.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgGrassy.png")
  2999.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseGrassy.png")
  3000.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseGrassy.png")
  3001.           when 3 # misty
  3002.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgMisty.png")
  3003.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseMisty.png")
  3004.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseMisty.png")
  3005.           when 4 # dark crystal cavern
  3006.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgDarkCrystalCavern.png")
  3007.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseDarkCrystalCavern.png")
  3008.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseDarkCrystalCavern.png")
  3009.           when 5 # chess
  3010.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgChess.png")
  3011.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseChess.png")
  3012.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseChess.png")
  3013.           when 6 # bigtop
  3014.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgBigtop.png")
  3015.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseBigtop.png")
  3016.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseBigtop.png")
  3017.           when 7 # burning
  3018.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgBurning.png")
  3019.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseBurning.png")
  3020.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseBurning.png")
  3021.           when 8 # swamp
  3022.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgSwamp.png")
  3023.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseSwamp.png")
  3024.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseSwamp.png")
  3025.           when 9 # rainbow
  3026.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgRainbow.png")
  3027.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseRainbow.png")
  3028.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseRainbow.png")
  3029.           when 10 # corrosive
  3030.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgCorrosive.png")
  3031.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseCorrosive.png")
  3032.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseCorrosive.png")
  3033.           when 11 # corrosive mist
  3034.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgCorrosiveMist.png")
  3035.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseCorrosiveMist.png")
  3036.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseCorrosiveMist.png")
  3037.           when 12 # desert
  3038.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgDesert.png")
  3039.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseDesert.png")
  3040.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseDesert.png")
  3041.           when 13 # icy
  3042.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgIcy.png")
  3043.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseIcy.png")
  3044.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseIcy.png")
  3045.           when 14 # rocky
  3046.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgRocky.png")
  3047.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseRocky.png")
  3048.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseRocky.png")
  3049.           when 15 # forest
  3050.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgForest.png")
  3051.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseForest.png")
  3052.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseForest.png")
  3053.           when 16 # superheated
  3054.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgSuperheated.png")
  3055.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseSuperheated.png")
  3056.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseSuperheated.png")
  3057.           when 17 # factory
  3058.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgFactory.png")
  3059.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseFactory.png")
  3060.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseFactory.png")
  3061.           when 18 # short-circuit
  3062.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgShortcircuit.png")
  3063.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseShortcircuit.png")
  3064.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseShortcircuit.png")
  3065.           when 19 # wasteland
  3066.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgWasteland.png")
  3067.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseWasteland.png")
  3068.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseWasteland.png")
  3069.           when 20 # ashen beach
  3070.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgAshenBeach.png")
  3071.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseAshenBeach.png")
  3072.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseAshenBeach.png")
  3073.           when 21 # water surface
  3074.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgWaterSurface.png")
  3075.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseWaterSurface.png")
  3076.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseWaterSurface.png")
  3077.           when 22 # underwater
  3078.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgUnderwater.png")
  3079.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseUnderwater.png")
  3080.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseUnderwater.png")
  3081.           when 23 # cave
  3082.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgCave.png")
  3083.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseCave.png")
  3084.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseCave.png")
  3085.           when 24 # glitch
  3086.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgGlitch.png")
  3087.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseGlitch.png")
  3088.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseGlitch.png")
  3089.           when 25 # crystal cavern
  3090.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgCrystalCavern.png")
  3091.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseCrystalCavern.png")
  3092.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseCrystalCavern.png")
  3093.           when 26 # murkwater surface
  3094.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgMurkwaterSurface.png")
  3095.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseMurkwaterSurface.png")
  3096.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseMurkwaterSurface.png")
  3097.           when 27 # mountain
  3098.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgMountain.png")
  3099.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseMountain.png")
  3100.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseMountain.png")
  3101.           when 28 # snowymountain
  3102.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgSnowyMountain.png")
  3103.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseSnowyMountain.png")
  3104.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseSnowyMountain.png")
  3105.           when 29 # holy
  3106.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgHoly.png")
  3107.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseHoly.png")
  3108.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseHoly.png")
  3109.           when 30 # mirror
  3110.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgMirror.png")
  3111.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseMirror.png")
  3112.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseMirror.png")
  3113.           when 31 # fairy tale
  3114.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgFairyTale.png")
  3115.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseFairyTale.png")
  3116.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseFairyTale.png")
  3117.           when 32 # dragons den
  3118.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgDragonsDen.png")
  3119.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseDragonsDen.png")
  3120.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseDragonsDen.png")
  3121.           when 33 # flower garden
  3122.             case $fecounter
  3123.               when 0
  3124.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgFlowerGarden0.png")
  3125.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseFlowerGarden0.png")
  3126.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseFlowerGarden0.png")              
  3127.               when 1
  3128.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgFlowerGarden1.png")
  3129.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseFlowerGarden1.png")
  3130.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseFlowerGarden1.png")              
  3131.               when 2
  3132.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgFlowerGarden2.png")
  3133.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseFlowerGarden2.png")
  3134.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseFlowerGarden2.png")              
  3135.               when 3
  3136.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgFlowerGarden3.png")
  3137.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseFlowerGarden3.png")
  3138.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseFlowerGarden3.png")              
  3139.               when 4
  3140.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgFlowerGarden4.png")
  3141.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseFlowerGarden4.png")
  3142.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseFlowerGarden4.png")              
  3143.             end
  3144.           when 34 # starlight field
  3145.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgStarlight.png")
  3146.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseStarlight.png")
  3147.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseStarlight.png")
  3148.           when 35 # new world
  3149.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgNewWorld.png")
  3150.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseNewWorld.png")
  3151.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseNewWorld.png")
  3152.           when 36 # inverse
  3153.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgInverse.png")
  3154.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseInverse.png")
  3155.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseInverse.png")
  3156.           when 37 # indoorA
  3157.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgIndoorA.png")
  3158.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseIndoorA.png")
  3159.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseIndoorA.png")
  3160.           when 38 # indoorB
  3161.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgIndoorB.png")
  3162.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseIndoorB.png")
  3163.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseIndoorB.png")
  3164.           when 39 # indoorC
  3165.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgIndoorC.png")
  3166.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseIndoorC.png")
  3167.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseIndoorC.png")
  3168.           when 40 # city
  3169.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgCity.png")
  3170.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseCity.png")
  3171.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseCity.png")
  3172.           when 41 # citynew
  3173.                 pbApplySceneBG("battlebg","Graphics/Battlebacks/battlebgCityNew.png")
  3174.                 pbApplySceneBG("playerbase","Graphics/Battlebacks/playerbaseCityNew.png")
  3175.                 pbApplySceneBG("enemybase","Graphics/Battlebacks/enemybaseCityNew.png")
  3176.   end
  3177.  end
  3178.  
  3179. ################################################################################
  3180. # Battle core.
  3181. ################################################################################
  3182.   def pbStartBattle(canlose=false)
  3183.     begin
  3184.       pbStartBattleCore(canlose)
  3185.     rescue BattleAbortedException
  3186.       @decision=0
  3187.       @scene.pbEndBattle(@decision)
  3188.     end
  3189.     return @decision
  3190.   end
  3191.  
  3192.   def pbStartBattleCore(canlose)
  3193.     if !@fullparty1 && @party1.length>MAXPARTYSIZE
  3194.       raise ArgumentError.new(_INTL("Party 1 has more than {1} Pokémon.",MAXPARTYSIZE))
  3195.     end
  3196.     if !@fullparty2 && @party2.length>MAXPARTYSIZE
  3197.       raise ArgumentError.new(_INTL("Party 2 has more than {1} Pokémon.",MAXPARTYSIZE))
  3198.     end
  3199.     if !@opponent
  3200. #========================
  3201. # Initialize wild Pokémon
  3202. #========================
  3203.       if @party2.length==1
  3204.         if @doublebattle
  3205.           raise _INTL("Only two wild Pokémon are allowed in double battles")
  3206.         end
  3207.         wildpoke=@party2[0]
  3208.         @battlers[1].pbInitialize(wildpoke,0,false)
  3209.         @peer.pbOnEnteringBattle(self,wildpoke)
  3210.         pbSetSeen(wildpoke)
  3211.         @scene.pbStartBattle(self)
  3212.         pbDisplayPaused(_INTL("Wild {1} appeared!",wildpoke.name))
  3213.       elsif @party2.length==2
  3214.         if !@doublebattle
  3215.           raise _INTL("Only one wild Pokémon is allowed in single battles")
  3216.         end
  3217.         @battlers[1].pbInitialize(@party2[0],0,false)
  3218.         @battlers[3].pbInitialize(@party2[1],0,false)
  3219.         @peer.pbOnEnteringBattle(self,@party2[0])
  3220.         @peer.pbOnEnteringBattle(self,@party2[1])
  3221.         pbSetSeen(@party2[0])
  3222.         pbSetSeen(@party2[1])
  3223.         @scene.pbStartBattle(self)
  3224.         pbDisplayPaused(_INTL("Wild {1} and\r\n{2} appeared!",
  3225.            @party2[0].name,@party2[1].name))
  3226.       else
  3227.         raise _INTL("Only one or two wild Pokémon are allowed")
  3228.       end
  3229.     elsif @doublebattle
  3230. #=======================================
  3231. # Initialize opponents in double battles
  3232. #=======================================
  3233.       if @opponent.is_a?(Array)
  3234.         if @opponent.length==1
  3235.           @opponent=@opponent[0]
  3236.         elsif @opponent.length!=2
  3237.           raise _INTL("Opponents with zero or more than two people are not allowed")
  3238.         end
  3239.       end
  3240.       if @player.is_a?(Array)
  3241.         if @player.length==1
  3242.           @player=@player[0]
  3243.         elsif @player.length!=2
  3244.           raise _INTL("Player trainers with zero or more than two people are not allowed")
  3245.         end
  3246.       end
  3247.       @scene.pbStartBattle(self)
  3248.       if @opponent.is_a?(Array)
  3249.         pbDisplayBrief(_INTL("{1} and {2} want to battle!",@opponent[0].fullname,@opponent[1].fullname))
  3250.         sendout1=pbFindNextUnfainted(@party2,0,pbSecondPartyBegin(1))
  3251.         raise _INTL("Opponent 1 has no unfainted Pokémon") if sendout1<0
  3252.         sendout2=pbFindNextUnfainted(@party2,pbSecondPartyBegin(1))
  3253.         raise _INTL("Opponent 2 has no unfainted Pokémon") if sendout2<0
  3254.  #       pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent[0].fullname,@party2[sendout1].name))
  3255. #### JERICHO - 001 - START        
  3256.         @battlers[1].pbInitialize(@party2[sendout1],sendout1,false)
  3257.         @battlers[3].pbInitialize(@party2[sendout2],sendout2,false)
  3258.         pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent[0].fullname,PBSpecies.getName(@battlers[1].species))) #ILLUSION
  3259.         pbSendOut(1,@party2[sendout1])
  3260.         pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent[1].fullname,PBSpecies.getName(@battlers[3].species))) #ILLUSION
  3261. #### JERICHO - 001 - END
  3262.         pbSendOut(3,@party2[sendout2])
  3263.       else
  3264.         pbDisplayBrief(_INTL("{1}\r\nwould like to battle!",@opponent.fullname))
  3265.         sendout1=pbFindNextUnfainted(@party2,0)
  3266.         sendout2=pbFindNextUnfainted(@party2,sendout1+1)
  3267.         if sendout1<0 || sendout2<0
  3268.           raise _INTL("Opponent doesn't have two unfainted Pokémon")
  3269.         end
  3270. #### JERICHO - 001 - START        
  3271.         @battlers[1].pbInitialize(@party2[sendout1],sendout1,false) #ILLUSION
  3272.         @battlers[3].pbInitialize(@party2[sendout2],sendout2,false)
  3273.         pbDisplayBrief(_INTL("{1} sent\r\nout {2} and {3}!",
  3274.            @opponent.fullname,PBSpecies.getName(@battlers[1].species),PBSpecies.getName(@battlers[3].species))) #ILLUSION
  3275. #### JERICHO - 001 - END          
  3276.         pbSendOut(1,@party2[sendout1])
  3277.         pbSendOut(3,@party2[sendout2])
  3278.       end
  3279.     else
  3280. #======================================
  3281. # Initialize opponent in single battles
  3282. #======================================
  3283.       sendout=pbFindNextUnfainted(@party2,0)
  3284.       raise _INTL("Trainer has no unfainted Pokémon") if sendout<0
  3285.       if @opponent.is_a?(Array)
  3286.         raise _INTL("Opponent trainer must be only one person in single battles") if @opponent.length!=1
  3287.         @opponent=@opponent[0]
  3288.       end
  3289.       if @player.is_a?(Array)
  3290.         raise _INTL("Player trainer must be only one person in single battles") if @player.length!=1
  3291.         @player=@player[0]
  3292.       end
  3293.       trainerpoke=@party2[sendout]
  3294.       @scene.pbStartBattle(self)
  3295.       pbDisplayBrief(_INTL("{1}\r\nwould like to battle!",@opponent.fullname))
  3296. #### JERICHO - 001 - START      
  3297.       @battlers[1].pbInitialize(trainerpoke,sendout,false) #ILLUSION
  3298.       pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent.fullname,PBSpecies.getName(@battlers[1].species)))
  3299. #### JERICHO - 001 - END    
  3300.       pbSendOut(1,trainerpoke)
  3301.     end
  3302. #=====================================
  3303. # Initialize players in double battles
  3304. #=====================================
  3305.     if @doublebattle
  3306.       if @player.is_a?(Array)
  3307.         sendout1=pbFindNextUnfainted(@party1,0,pbSecondPartyBegin(0))
  3308.         raise _INTL("Player 1 has no unfainted Pokémon") if sendout1<0
  3309.         sendout2=pbFindNextUnfainted(@party1,pbSecondPartyBegin(0))
  3310.         raise _INTL("Player 2 has no unfainted Pokémon") if sendout2<0
  3311. #### JERICHO - 001 - START        
  3312.         @battlers[0].pbInitialize(@party1[sendout1],sendout1,false) #ILLUSION
  3313.         @battlers[2].pbInitialize(@party1[sendout2],sendout2,false)
  3314.         pbDisplayBrief(_INTL("{1} sent\r\nout {2}!  Go! {3}!",
  3315.     @player[1].fullname,@battlers[2].name,@battlers[0].name))#ILLUSION
  3316. #### JERICHO - 001 - END    
  3317.         pbSetSeen(@party1[sendout1])
  3318.         pbSetSeen(@party1[sendout2])
  3319.       else
  3320.         sendout1=pbFindNextUnfainted(@party1,0)
  3321.         sendout2=pbFindNextUnfainted(@party1,sendout1+1)
  3322. #### JERICHO - 001 - START        
  3323.         @battlers[0].pbInitialize(@party1[sendout1],sendout1,false) #ILLUSION
  3324.         @battlers[2].pbInitialize(@party1[sendout2],sendout2,false) unless sendout2==-1
  3325.         if sendout2>-1
  3326.           pbDisplayBrief(_INTL("Go! {1} and {2}!",@battlers[0].name,@battlers[2].name)) #ILLUSION
  3327.         else
  3328.           pbDisplayBrief(_INTL("Go! {1}!",@battlers[0].name)) #ILLUSION  
  3329.         end        
  3330.       end
  3331. #### JERICHO - 001 - END      
  3332.       pbSendOut(0,@party1[sendout1])
  3333.       pbSendOut(2,@party1[sendout2]) unless sendout2==-1
  3334.     else
  3335. #====================================
  3336. # Initialize player in single battles
  3337. #====================================
  3338.       sendout=pbFindNextUnfainted(@party1,0)
  3339.       if sendout<0
  3340.         raise _INTL("Player has no unfainted Pokémon")
  3341.       end
  3342.       playerpoke=@party1[sendout]
  3343. #### JERICHO - 001 - START      
  3344.       @battlers[0].pbInitialize(playerpoke,sendout,false) #Illusion
  3345.       pbDisplayBrief(_INTL("Go! {1}!",@battlers[0].name))
  3346. #### JERICHO - 001 - END      
  3347.       pbSendOut(0,playerpoke)
  3348.     end
  3349. #==================
  3350. # Initialize battle
  3351. #==================
  3352.     if @weather==PBWeather::SUNNYDAY
  3353.       pbCommonAnimation("Sunny",nil,nil)
  3354.       pbDisplay(_INTL("The sunlight is strong."))
  3355.     elsif @weather==PBWeather::RAINDANCE
  3356.       pbCommonAnimation("Rain",nil,nil)
  3357.       pbDisplay(_INTL("It is raining."))
  3358.     elsif @weather==PBWeather::SANDSTORM
  3359.       pbCommonAnimation("Sandstorm",nil,nil)
  3360.       pbDisplay(_INTL("A sandstorm is raging."))
  3361.     elsif @weather==PBWeather::HAIL
  3362.       pbCommonAnimation("Hail",nil,nil)
  3363.       pbDisplay(_INTL("Hail is falling."))
  3364.     elsif @weather==PBWeather::STRONGWINDS
  3365.       pbDisplay(_INTL("The wind is strong."))
  3366.     end
  3367.    # Field Effects BEGIN UPDATE
  3368.    case $fefieldeffect
  3369.      when 1
  3370.       pbDisplay(_INTL("The field is hyper-charged!"))
  3371.      when 2
  3372.       pbDisplay(_INTL("The field is in full bloom."))
  3373.      when 3
  3374.       pbDisplay(_INTL("Mist settles on the field."))
  3375.      when 4
  3376.       pbDisplay(_INTL("Darkness is gathering..."))
  3377.      when 5
  3378.       pbDisplay(_INTL("Opening variation set."))
  3379.      when 6
  3380.       pbDisplay(_INTL("Now presenting...!"))
  3381.      when 7
  3382.       pbDisplay(_INTL("The field is ablaze!"))
  3383.      when 8
  3384.       pbDisplay(_INTL("The field is swamped."))
  3385.      when 9
  3386.       pbDisplay(_INTL("What does it mean?"))
  3387.      when 10
  3388.       pbDisplay(_INTL("The field is corrupted!"))
  3389.      when 11
  3390.       pbDisplay(_INTL("Corrosive mist settles on the field!"))
  3391.      when 12
  3392.       pbDisplay(_INTL("The field is rife with sand."))
  3393.      when 13
  3394.       pbDisplay(_INTL("The field is covered in ice."))
  3395.      when 14
  3396.       pbDisplay(_INTL("The field is littered with rocks."))
  3397.      when 15
  3398.       pbDisplay(_INTL("The field is abound with trees."))
  3399.      when 16
  3400.       pbDisplay(_INTL("The field is super-heated!"))
  3401.      when 17
  3402.       pbDisplay(_INTL("Machines whir in the background."))
  3403.      when 18
  3404.       pbDisplay(_INTL("Bzzt!"))
  3405.      when 19
  3406.       pbDisplay(_INTL("The waste is watching..."))
  3407.      when 20
  3408.       pbDisplay(_INTL("Ash and sand line the field."))
  3409.      when 21
  3410.       pbDisplay(_INTL("The water's surface is calm."))
  3411.      when 22
  3412.       pbDisplay(_INTL("Blub blub..."))
  3413.      when 23
  3414.       pbDisplay(_INTL("The cave echoes dully..."))
  3415.      when 24
  3416.       pbDisplay(_INTL("1n!taliz3 .b//////attl3"))
  3417.      when 25
  3418.       pbDisplay(_INTL("The cave is littered with crystals."))
  3419.      when 26
  3420.       pbDisplay(_INTL("The water is tainted..."))
  3421.      when 27
  3422.       pbDisplay(_INTL("Adieu to disappointment and spleen."))
  3423.      when 28
  3424.       pbDisplay(_INTL("The snow glows white on the mountain..."))
  3425.      when 29
  3426.       pbDisplay(_INTL("Benedictus Sanctus Spiritus..."))
  3427.      when 30
  3428.       pbDisplay(_INTL("Mirror, mirror, on the field,"))
  3429.       pbDisplay(_INTL("Who shalt this fractured power wield?"))
  3430.      when 31
  3431.       pbDisplay(_INTL("Once upon a time..."))
  3432.      when 32
  3433.       pbDisplay(_INTL("If you wish to slay a dragon..."))
  3434.      when 33
  3435.       pbDisplay(_INTL("Seeds line the field."))
  3436.      when 34
  3437.       pbDisplay(_INTL("Starlight fills the battlefield."))
  3438.      when 35
  3439.       pbDisplay(_INTL("From darkness, from stardust,"))
  3440.       pbDisplay(_INTL("From memories of eons past and visions yet to come..."))
  3441.      when 36
  3442.       pbDisplay(_INTL("!trats elttaB"))
  3443.     end
  3444.   # END OF UPDATE
  3445.     pbOnActiveAll   # Abilities
  3446.     @turncount=0
  3447.     loop do   # Now begin the battle loop
  3448.       PBDebug.log("***Round #{@turncount+1}***") if $INTERNAL
  3449.       if @debug && @turncount>=100
  3450.         @decision=pbDecisionOnTime()
  3451.         PBDebug.log("***[Undecided after 100 rounds]")
  3452.         pbAbort
  3453.         break
  3454.       end
  3455.       PBDebug.logonerr{
  3456.          pbCommandPhase
  3457.       }
  3458.       break if @decision>0
  3459.       PBDebug.logonerr{
  3460.          pbAttackPhase
  3461.       }
  3462.       break if @decision>0
  3463.       PBDebug.logonerr{
  3464.          pbEndOfRoundPhase
  3465.       }
  3466.       break if @decision>0
  3467.       @turncount+=1
  3468.     end
  3469.     return pbEndOfBattle(canlose)
  3470.   end
  3471.  
  3472. ################################################################################
  3473. # Command phase.
  3474. ################################################################################
  3475.   def pbCommandMenu(i)
  3476.     return @scene.pbCommandMenu(i)
  3477.   end
  3478.  
  3479.   def pbItemMenu(i)
  3480.     return @scene.pbItemMenu(i)
  3481.   end
  3482.  
  3483.   def pbAutoFightMenu(i)
  3484.     return false
  3485.   end
  3486.  
  3487.   def pbCommandPhase
  3488.     @scene.pbBeginCommandPhase
  3489.     @scene.pbResetCommandIndices
  3490.     for i in 0...4   # Reset choices if commands can be shown
  3491.       if pbCanShowCommands?(i) || @battlers[i].isFainted?
  3492.         @choices[i][0]=0
  3493.         @choices[i][1]=0
  3494.         @choices[i][2]=nil
  3495.         @choices[i][3]=-1
  3496.       else
  3497.         battler=@battlers[i]
  3498.         unless !@doublebattle && pbIsDoubleBattler?(i)
  3499.           PBDebug.log("[reusing commands for #{battler.pbThis(true)}]") if $INTERNAL
  3500.         end
  3501.       end
  3502.     end
  3503. #### KUROTSUNE - 015 - START
  3504.     for i in 0..3
  3505.       @switchedOut[i] = false
  3506.     end
  3507. #### KUROTSUNE - 015 - END
  3508.     # Reset choices to perform Mega Evolution if it wasn't done somehow
  3509.     for i in 0...@megaEvolution[0].length
  3510.       @megaEvolution[0][i]=-1 if @megaEvolution[0][i]>=0
  3511.     end
  3512.     for i in 0...@megaEvolution[1].length
  3513.       @megaEvolution[1][i]=-1 if @megaEvolution[1][i]>=0
  3514.     end
  3515.     for i in 0...4
  3516.       break if @decision!=0
  3517.       next if @choices[i][0]!=0
  3518.       if !pbOwnedByPlayer?(i) || @controlPlayer
  3519.         if !@battlers[i].isFainted? && pbCanShowCommands?(i)
  3520.           @scene.pbChooseEnemyCommand(i)
  3521.         end
  3522.       else
  3523.         commandDone=false
  3524.         commandEnd=false
  3525.         if pbCanShowCommands?(i)
  3526.           loop do
  3527.             cmd=pbCommandMenu(i)
  3528.             if cmd==0 # Fight
  3529.               if pbCanShowFightMenu?(i)
  3530.                 commandDone=true if pbAutoFightMenu(i)
  3531.                 until commandDone
  3532.                   index=@scene.pbFightMenu(i)
  3533.                   if index<0
  3534.                     side=(pbIsOpposing?(i)) ? 1 : 0
  3535.                     owner=pbGetOwnerIndex(i)
  3536.                     if @megaEvolution[side][owner]==i
  3537.                       @megaEvolution[side][owner]=-1
  3538.                     end
  3539.                     break
  3540.                   end
  3541.                   next if !pbRegisterMove(i,index)
  3542.                   if @doublebattle
  3543.                     thismove=@battlers[i].moves[index]
  3544.                     target=@battlers[i].pbTarget(thismove)
  3545.                     if target==PBTargets::SingleNonUser # single non-user
  3546.                       target=@scene.pbChooseTarget(i)
  3547.                       next if target<0
  3548.                       pbRegisterTarget(i,target)
  3549.                     elsif target==PBTargets::UserOrPartner # Acupressure
  3550.                       target=@scene.pbChooseTargetAcupressure(i)
  3551.                       next if target<0 || (target&1)!=(i&1)
  3552.                       pbRegisterTarget(i,target)
  3553.                     end
  3554.                   end
  3555.                   commandDone=true
  3556.                 end
  3557.               else
  3558.                 pbAutoChooseMove(i)
  3559.                 commandDone=true
  3560.               end
  3561.             elsif cmd==1 # Bag
  3562.               if !@internalbattle
  3563.                 if pbOwnedByPlayer?(i)
  3564.                   pbDisplay(_INTL("Items can't be used here."))
  3565.                 end
  3566.               else
  3567.                 item=pbItemMenu(i)
  3568.                 if item[0]>0
  3569.                   if pbRegisterItem(i,item[0],item[1])
  3570.                     commandDone=true
  3571.                   end
  3572.                 end
  3573.               end
  3574.             elsif cmd==2 # Pokémon
  3575.               pkmn=pbSwitchPlayer(i,false,true)
  3576.               if pkmn>=0
  3577.                 commandDone=true if pbRegisterSwitch(i,pkmn)
  3578.               end
  3579.             elsif cmd==3   # Run
  3580.               run=pbRun(i)
  3581.               if run>0
  3582.                 commandDone=true
  3583.                 return
  3584.               elsif run<0
  3585.                 commandDone=true
  3586.                 side=(pbIsOpposing?(i)) ? 1 : 0
  3587.                 owner=pbGetOwnerIndex(i)
  3588.                 if @megaEvolution[side][owner]==i
  3589.                   @megaEvolution[side][owner]=-1
  3590.                 end
  3591.               end
  3592.             elsif cmd==4   # Call
  3593.               thispkmn=@battlers[i]
  3594.               @choices[i][0]=4   # "Call Pokémon"
  3595.               @choices[i][1]=0
  3596.               @choices[i][2]=nil
  3597.               side=(pbIsOpposing?(i)) ? 1 : 0
  3598.               owner=pbGetOwnerIndex(i)
  3599.               if @megaEvolution[side][owner]==i
  3600.                 @megaEvolution[side][owner]=-1
  3601.               end
  3602.               commandDone=true
  3603.             elsif cmd==-1   # Go back to first battler's choice
  3604.               @megaEvolution[0][0]=-1 if @megaEvolution[0][0]>=0
  3605.               @megaEvolution[1][0]=-1 if @megaEvolution[1][0]>=0
  3606.               # Restore the item the player's first Pokémon was due to use
  3607.               if @choices[0][0]==3 && $PokemonBag && $PokemonBag.pbCanStore?(@choices[0][1])
  3608.                 $PokemonBag.pbStoreItem(@choices[0][1])
  3609.               end
  3610.               pbCommandPhase
  3611.               return
  3612.             end
  3613.             break if commandDone
  3614.           end
  3615.         end
  3616.       end
  3617.     end
  3618.   end
  3619.  
  3620. ################################################################################
  3621. # Attack phase.
  3622. ################################################################################
  3623.   def pbAttackPhase
  3624.     @scene.pbBeginAttackPhase
  3625.     for i in 0...4
  3626.       @successStates[i].clear
  3627.       if @choices[i][0]!=1 && @choices[i][0]!=2
  3628.         @battlers[i].effects[PBEffects::DestinyBond]=false
  3629.         @battlers[i].effects[PBEffects::Grudge]=false
  3630.       end
  3631.       @battlers[i].turncount+=1 if !@battlers[i].isFainted?
  3632.       @battlers[i].effects[PBEffects::Rage]=false if !pbChoseMove?(i,:RAGE)
  3633.       @battlers[i].pbCustapBerry
  3634.     end
  3635.     # Calculate priority at this time
  3636.     @usepriority=false
  3637.     priority=pbPriority
  3638.     # Mega Evolution
  3639.     for i in priority
  3640.       next if @choices[i.index][0]!=1
  3641.       side=(pbIsOpposing?(i.index)) ? 1 : 0
  3642.       owner=pbGetOwnerIndex(i.index)
  3643.       if @megaEvolution[side][owner]==i.index
  3644.         pbMegaEvolve(i.index)
  3645.       end
  3646.     end
  3647.     # Call at Pokémon
  3648.     for i in priority
  3649.       if @choices[i.index][0]==4
  3650.         pbCall(i.index)
  3651.       end
  3652.     end
  3653.     # Switch out Pokémon
  3654.     @switching=true
  3655.     switched=[]
  3656.     for i in priority
  3657.       if @choices[i.index][0]==2
  3658.         index=@choices[i.index][1] # party position of Pokémon to switch to
  3659.         self.lastMoveUser=i.index
  3660.         if !pbOwnedByPlayer?(i.index)
  3661.           owner=pbGetOwner(i.index)
  3662.           pbDisplayBrief(_INTL("{1} withdrew {2}!",owner.fullname,PBSpecies.getName(i.species)))
  3663.         else
  3664.           pbDisplayBrief(_INTL("{1}, that's enough!\r\nCome back!",i.name))
  3665.         end
  3666.         for j in priority
  3667.           next if !i.pbIsOpposing?(j.index)
  3668.           # if Pursuit and this target ("i") was chosen
  3669.           if pbChoseMoveFunctionCode?(j.index,0x88) &&
  3670.              !j.effects[PBEffects::Pursuit] &&
  3671.              (@choices[j.index][3]==-1 || @choices[j.index][3]==i.index)
  3672.             if j.status!=PBStatuses::SLEEP &&
  3673.                j.status!=PBStatuses::FROZEN &&
  3674.                (!j.hasWorkingAbility(:TRUANT) || !j.effects[PBEffects::Truant])
  3675.               j.pbUseMove(@choices[j.index])
  3676.               j.effects[PBEffects::Pursuit]=true
  3677.               # UseMove calls pbGainEXP as appropriate
  3678.               @switching=false
  3679.               return if @decision>0
  3680.             end
  3681.           end
  3682.           break if i.isFainted?
  3683.         end
  3684.         if !pbRecallAndReplace(i.index,index)
  3685.           # If a forced switch somehow occurs here in single battles
  3686.           # the attack phase now ends
  3687.           if !@doublebattle
  3688.             @switching=false
  3689.             return
  3690.           end
  3691.         else
  3692.           switched.push(i.index)
  3693.         end
  3694.       end
  3695.     end
  3696.     if switched.length>0
  3697.       for i in priority
  3698.         i.pbAbilitiesOnSwitchIn(true) if switched.include?(i.index)
  3699.       end
  3700.     end
  3701.     @switching=false
  3702.     # Use items
  3703.     for i in priority
  3704.       if pbIsOpposing?(i.index) && @choices[i.index][0]==3
  3705.         pbEnemyUseItem(@choices[i.index][1],i)
  3706.         i.itemUsed = true
  3707.       elsif @choices[i.index][0]==3
  3708.         # Player use item
  3709.         item=@choices[i.index][1]
  3710.         if item>0
  3711.           usetype=$ItemData[item][ITEMBATTLEUSE]
  3712.           if usetype==1 || usetype==3
  3713.             if @choices[i.index][2]>=0
  3714.               pbUseItemOnPokemon(item,@choices[i.index][2],i,@scene)
  3715.               i.itemUsed = true
  3716.             end
  3717.           elsif usetype==2 || usetype==4
  3718.             if !ItemHandlers.hasUseInBattle(item) # Poké Ball/Poké Doll used already
  3719.               pbUseItemOnBattler(item,@choices[i.index][2],i,@scene)
  3720.                i.itemUsed = true
  3721.             end
  3722.           end
  3723.         end
  3724.       end
  3725.     end
  3726. #### KUROTSUNE - 014 - START
  3727.     if @field.effects[PBEffects::WonderRoom] > 0
  3728.       for i in @battlers
  3729.         if !i.wonderroom
  3730.           i.pbSwapDefenses
  3731.         end
  3732.       end
  3733.     end
  3734. #### KUROTSUNE - 014 - END
  3735.     symbiosis = pbSymbiosisCheck(priority)
  3736. # Use Attacks
  3737.     for i in priority
  3738.       if pbChoseMoveFunctionCode?(i.index,0x115) # Focus Punch
  3739.         pbCommonAnimation("FocusPunch",i,nil)
  3740.         pbDisplay(_INTL("{1} is tightening its focus!",i.pbThis))
  3741.       end
  3742.     end
  3743.     for i in priority
  3744.       i.pbProcessTurn(@choices[i.index])
  3745.       if i.effects[PBEffects::Round]
  3746.         i.pbPartner.selectedMove = 297
  3747.       end
  3748.        
  3749.        if symbiosis
  3750.         for s in symbiosis
  3751.           if s.item == 0 && s.pbPartner.item
  3752.             pbDisplay(_INTL("{1} received {2}'s {3} from symbiosis! ",s.pbThis, s.pbPartner.pbThis, PBItems.getName(s.pbPartner.item)))
  3753.             s.item = s.pbPartner.item
  3754.             s.pokemon.itemInitial = s.pbPartner.item
  3755.             s.pbPartner.pokemon.itemInitial = 0
  3756.             s.pbPartner.item=0
  3757.           end
  3758.         end
  3759.       end
  3760.       return if @decision>0
  3761.     end
  3762.     pbWait(20)
  3763.   end
  3764.  
  3765.    # Checks if anyone is eligible to receive an item through symbiosis
  3766.     def pbSymbiosisCheck(battlers)
  3767.       result = Array.new
  3768.       count  = 0
  3769.       for i in battlers
  3770.         if i.item != 0 && i.pokemon.itemInitial != 0 &&
  3771.           i.pbPartner.item != 0 && i.pbPartner.pokemon.itemInitial != 0 &&
  3772.           i.pbPartner.hasWorkingAbility(:SYMBIOSIS)
  3773.             result[count] = i
  3774.             count += 1
  3775.         end
  3776.       end
  3777.       if result.any?
  3778.         return result
  3779.       else
  3780.         return false
  3781.       end
  3782.     end
  3783.  
  3784.    
  3785. ################################################################################
  3786. # End of round.
  3787. ################################################################################
  3788.   def pbEndOfRoundPhase
  3789.     for i in 0...4
  3790.       @battlers[i].forcedSwitchEarlier = false
  3791.       @battlers[i].effects[PBEffects::Roost]=false
  3792.       @battlers[i].effects[PBEffects::Protect]=false
  3793.       @battlers[i].effects[PBEffects::KingsShield]=false # add this line
  3794.       @battlers[i].effects[PBEffects::ProtectNegation]=false
  3795.       @battlers[i].effects[PBEffects::Endure]=false
  3796.       @battlers[i].effects[PBEffects::HyperBeam]-=1 if @battlers[i].effects[PBEffects::HyperBeam]>0
  3797.       @battlers[i].effects[PBEffects::SpikyShield]=false
  3798. #### KUROTSUNE - 023 - START
  3799.       @battlers[i].effects[PBEffects::Powder]  = false
  3800. #### KUROTSUNE - 023 - END
  3801. #### KUROTSUNE - 032 - START
  3802.       @battlers[i].effects[PBEffects::MeFirst] = false
  3803. #### KUROTSUNE - 032 - END      
  3804.       @battlers[i].itemUsed                    = false      
  3805.     end
  3806. #### KUROTSUNE - 013 - START
  3807.     @field.effects[PBEffects::IonDeluge]       = false
  3808. #### KUROTSUNE - 013 - END
  3809.     for i in 0...2
  3810.       sides[i].effects[PBEffects::QuickGuard]=false
  3811.       sides[i].effects[PBEffects::WideGuard]=false
  3812.       sides[i].effects[PBEffects::MatBlock]=false
  3813.     end
  3814.     @usepriority=false  # recalculate priority
  3815.     priority=pbPriority(true) # Ignoring Quick Claw here
  3816.     if @trickroom > 0
  3817.       @trickroom=@trickroom-1
  3818.       if @trickroom == 0
  3819.           Kernel.pbMessage("The twisted dimensions returned to normal!")
  3820.       end
  3821.     end
  3822. #### KUROTSUNE - 014 - START
  3823.     if @field.effects[PBEffects::WonderRoom] > 0
  3824.       @field.effects[PBEffects::WonderRoom] -= 1
  3825.       if @field.effects[PBEffects::WonderRoom] == 0
  3826.         for i in @battlers
  3827.           if i.wonderroom
  3828.            i.pbSwapDefenses
  3829.           end
  3830.         end
  3831.         Kernel.pbMessage("Wonder Room wore off, and the Defense and Sp. Def stats returned to normal!")
  3832.       end
  3833.     end
  3834. #### KUROTSUNE - 014 - END
  3835.     priority=pbPriority(true) # Ignoring Quick Claw here
  3836. #### AME - 003 - START
  3837.     # Field Effects
  3838.     endmessage=false
  3839.     for i in priority  
  3840.       next if i.isFainted?
  3841.       case $fefieldeffect
  3842.         when 2 # Grassy Field
  3843.           next if i.hp<=0  
  3844.           if !i.isAirborne?
  3845.             if i.effects[PBEffects::HealBlock]==0 && i.totalhp != i.hp
  3846.               pbDisplay(_INTL("The grassy terrain healed the Pokemon on the field.",i.pbThis)) if endmessage == false
  3847.               endmessage=true
  3848.               hpgain=(i.totalhp/16).floor
  3849.               hpgain=(hpgain*1.3).floor if isConst?(i.item,PBItems,:BIGROOT)
  3850.               hpgain=i.pbRecoverHP(hpgain,true)
  3851.             end
  3852.           end
  3853.         when 7 # Burning Field
  3854.           next if i.hp<=0
  3855.           if !i.isAirborne?    
  3856.             if isConst?(i.ability,PBAbilities,:FLASHFIRE)
  3857.               if !i.effects[PBEffects::FlashFire]
  3858.                 i.effects[PBEffects::FlashFire]=true
  3859.                 pbDisplay(_INTL("{1}'s {2} raised its Fire power!",
  3860.                 i.pbThis,PBAbilities.getName(i.ability)))
  3861.               end
  3862.             end
  3863.             if !i.pbHasType?(:FIRE) && !i.effects[PBEffects::AquaRing] &&
  3864.              !isConst?(i.ability,PBAbilities,:FLAREBOOST) &&
  3865.              !isConst?(i.ability,PBAbilities,:WATERVEIL) &&
  3866.              !isConst?(i.ability,PBAbilities,:FLASHFIRE) &&
  3867.              !isConst?(i.ability,PBAbilities,:HEATPROOF) &&
  3868.              !isConst?(i.ability,PBAbilities,:MAGMAARMOR) &&
  3869.              !isConst?(i.ability,PBAbilities,:FLAMEBODY) &&
  3870.              !isConst?(i.ability,PBAbilities,:MAGICGUARD) &&
  3871.              ![0xCA,0xCB].include?(PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function) # Dig, Dive
  3872.               atype=getConst(PBTypes,:FIRE) || 0
  3873.               eff=PBTypes.getCombinedEffectiveness(atype,i.type1,i.type2)
  3874.               if eff>0
  3875.                 @scene.pbDamageAnimation(i,0)
  3876.                 if isConst?(i.ability,PBAbilities,:LEAFGUARD) ||
  3877.                  isConst?(i.ability,PBAbilities,:ICEBODY) ||
  3878.                  isConst?(i.ability,PBAbilities,:GRASSPELT)
  3879.                   eff = eff*2
  3880.                 end
  3881.                 pbDisplay(_INTL("The Pokemon were burned by the field!",i.pbThis)) if endmessage == false
  3882.                 endmessage=true
  3883.                 i.pbReduceHP([(i.totalhp*eff/32).floor,1].max)
  3884.                 if i.hp<=0
  3885.                   return if !i.pbFaint
  3886.                 end
  3887.               end
  3888.             end
  3889.           end
  3890.         when 10 # Corrosive Field
  3891.           next if i.hp<=0
  3892.           if i.hasWorkingAbility(:GRASSPELT)        
  3893.             @scene.pbDamageAnimation(i,0)
  3894.             i.pbReduceHP((i.totalhp/8).floor)
  3895.             pbDisplay(_INTL("{1}'s Pelt was corroded!",i.pbThis)) if hpgain>0
  3896.             if i.hp<=0
  3897.               return if !i.pbFaint
  3898.             end
  3899.           end
  3900.           if i.hasWorkingAbility(:POISONHEAL)
  3901.             if !i.isAirborne?    
  3902.               if i.effects[PBEffects::HealBlock]==0
  3903.                 if i.hp<i.totalhp
  3904.                   pbCommonAnimation("Poison",i,nil)
  3905.                   i.pbRecoverHP((i.totalhp/8).floor,true)
  3906.                   pbDisplay(_INTL("{1} was healed by poison!",i.pbThis))
  3907.                 end
  3908.               end
  3909.             end
  3910.           end
  3911.         when 11 # Corrosive Mist Field
  3912.           if i.pbCanPoison?(false)
  3913.             pbDisplay(_INTL("The Pokemon were poisoned by the corroded mist!",i.pbThis))   if endmessage == false
  3914.             endmessage=true
  3915.             i.pbPoison(i)
  3916.           end
  3917.           if isConst?(i.ability,PBAbilities,:POISONHEAL)
  3918.             if i.effects[PBEffects::HealBlock]==0
  3919.               if i.hp<i.totalhp
  3920.                 pbCommonAnimation("Poison",i,nil)
  3921.                 i.pbRecoverHP((i.totalhp/8).floor,true)
  3922.                 pbDisplay(_INTL("{1} was healed by poison!",i.pbThis))
  3923.               end
  3924.             end
  3925.           end
  3926.         when 15 # Forest Field      
  3927.           next if i.hp<=0
  3928.           if i.hasWorkingAbility(:SAPSIPPER) && i.effects[PBEffects::HealBlock]==0        
  3929.             hpgain=(i.totalhp/16).floor
  3930.             hpgain=i.pbRecoverHP(hpgain,true)
  3931.             pbDisplay(_INTL("{1} drank tree sap to recover!",i.pbThis)) if hpgain>0
  3932.           end
  3933.         when 18 # Shortcircuit Field    
  3934.           next if i.hp<=0
  3935.           if i.hasWorkingAbility(:VOLTABSORB) && i.effects[PBEffects::HealBlock]==0      
  3936.             hpgain=(i.totalhp/16).floor
  3937.             hpgain=i.pbRecoverHP(hpgain,true)
  3938.             pbDisplay(_INTL("{1} absorbed stray electricity!",i.pbThis)) if hpgain>0
  3939.           end
  3940.         when 19 # Wasteland
  3941.           if i.hasWorkingAbility(:POISONHEAL)
  3942.             if !i.isAirborne?    
  3943.               if i.effects[PBEffects::HealBlock]==0
  3944.                 if i.hp<i.totalhp
  3945.                   pbCommonAnimation("Poison",i,nil)
  3946.                   i.pbRecoverHP((i.totalhp/8).floor,true)
  3947.                   pbDisplay(_INTL("{1} was healed by poison!",i.pbThis))
  3948.                 end
  3949.               end
  3950.             end
  3951.           end
  3952.         when 21 # Water Surface
  3953.           next if i.hp<=0
  3954.           if i.hasWorkingAbility(:WATERABSORB) && i.effects[PBEffects::HealBlock]==0      
  3955.             hpgain=(i.totalhp/16).floor
  3956.             hpgain=i.pbRecoverHP(hpgain,true)
  3957.             pbDisplay(_INTL("{1} absorbed some of the water!",i.pbThis)) if hpgain>0
  3958.           end
  3959.         when 22# Underwater
  3960.           next if i.hp<=0
  3961.           if !i.pbHasType?(:WATER) &&
  3962.            !i.hasWorkingAbility(:SWIFTSWIM) &&
  3963.            !i.hasWorkingAbility(:MAGICGUARD)
  3964.             atype=getConst(PBTypes,:WATER) || 0
  3965.             eff=PBTypes.getCombinedEffectiveness(atype,i.type1,i.type2)
  3966.             if eff>4
  3967.               @scene.pbDamageAnimation(i,0)
  3968.               if i.hasWorkingAbility(:FLAMEBODY) ||
  3969.                i.hasWorkingAbility(:MAGMAARMOR)
  3970.                 eff = eff*2
  3971.               end
  3972.               i.pbReduceHP([(i.totalhp*eff/32).floor,1].max)
  3973.               pbDisplay(_INTL("{1} struggled in the water!",i.pbThis))
  3974.               if i.hp<=0
  3975.                 return if !i.pbFaint
  3976.               end
  3977.             end
  3978.           end
  3979.         when 26 # Murkwater Surface
  3980.           if !i.pbHasType?(:STEEL) && !i.pbHasType?(:POISON) &&
  3981.            !i.hasWorkingAbility(:POISONHEAL) &&
  3982.            !i.hasWorkingAbility(:MAGICGUARD) &&
  3983.            !i.hasWorkingAbility(:WONDERGUARD) &&
  3984.            !i.hasWorkingAbility(:TOXICBOOST) &&
  3985.            !i.hasWorkingAbility(:IMMUNITY)
  3986.             atype=getConst(PBTypes,:POISON) || 0
  3987.             eff=PBTypes.getCombinedEffectiveness(atype,i.type1,i.type2)
  3988.             if i.hasWorkingAbility(:FLAMEBODY) ||
  3989.              i.hasWorkingAbility(:MAGMAARMOR) ||
  3990.              i.hasWorkingAbility(:DRYSKIN) ||
  3991.              i.hasWorkingAbility(:WATERABSORB)
  3992.               eff = eff*2
  3993.             end
  3994.             if PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function==0xCB # Dive
  3995.               @scene.pbDamageAnimation(i,0)
  3996.               i.pbReduceHP([(i.totalhp*eff/8).floor,1].max)
  3997.               pbDisplay(_INTL("{1} suffocated underneath the toxic water!",i.pbThis))
  3998.             else
  3999.               if !i.isAirborne?
  4000.                 @scene.pbDamageAnimation(i,0)
  4001.                 i.pbReduceHP([(i.totalhp*eff/32).floor,1].max)
  4002.                 pbDisplay(_INTL("{1} was hurt by the toxic water!",i.pbThis))
  4003.               end
  4004.             end
  4005.           end
  4006.           if i.isFainted?
  4007.             return if !i.pbFaint
  4008.           end
  4009.           if i.hasWorkingAbility(:POISONHEAL)
  4010.             if !i.isAirborne?    
  4011.               if i.effects[PBEffects::HealBlock]==0
  4012.                 if i.hp<i.totalhp
  4013.                   pbCommonAnimation("Poison",i,nil)
  4014.                   i.pbRecoverHP((i.totalhp/8).floor,true)
  4015.                   pbDisplay(_INTL("{1} was healed by poisoned water!",i.pbThis))
  4016.                 end
  4017.               end
  4018.             end
  4019.           end
  4020.           if i.pbHasType?(:POISON) && (i.hasWorkingAbility(:DRYSKIN) ||
  4021.            i.hasWorkingAbility(:WATERABSORB))
  4022.             if !i.isAirborne?    
  4023.               if i.effects[PBEffects::HealBlock]==0
  4024.                 if i.hp<i.totalhp
  4025.                   pbCommonAnimation("Poison",i,nil)
  4026.                   i.pbRecoverHP((i.totalhp/8).floor,true)
  4027.                   pbDisplay(_INTL("{1} was healed by the poisoned water!",i.pbThis))
  4028.                 end
  4029.               end
  4030.             end
  4031.           end
  4032.         when 35 # New World
  4033.           $fecounter = 1
  4034.       end
  4035.     end
  4036.   # End Field stuff
  4037. #### AME - 003 - END # Weather    
  4038.     if $fefieldeffect != 22
  4039.       if @weather != PBWeather::HAIL && $fefieldeffect == 27
  4040.         $fecounter = 0
  4041.       end
  4042.     case @weather
  4043.       when PBWeather::SUNNYDAY
  4044.         @weatherduration=@weatherduration-1 if @weatherduration>0
  4045.         if @weatherduration==0
  4046.           pbDisplay(_INTL("The sunlight faded."))
  4047.           @weather=0
  4048.           if $febackup == 34
  4049.             $fefieldeffect = 34
  4050.             pbChangeBGSprite
  4051.             pbDisplay(_INTL("The starry sky shone through!"));
  4052.           end
  4053.         else
  4054.           pbCommonAnimation("Sunny",nil,nil)
  4055.           if $fefieldeffect == 34 # Starlight Arena
  4056.             $fefieldeffect = 0
  4057.             pbChangeBGSprite
  4058.             pbDisplay(_INTL("The sunlight eclipsed the starry sky!"));
  4059.           end
  4060. #          pbDisplay(_INTL("The sunlight is strong."));
  4061.           for i in priority
  4062.             if i.hasWorkingAbility(:SOLARPOWER)
  4063.               pbDisplay(_INTL("{1} was hurt by the sunlight!",i.pbThis))
  4064.               @scene.pbDamageAnimation(i,0)
  4065.               i.pbReduceHP((i.totalhp/8).floor)
  4066.               if i.isFainted?
  4067.                 return if !i.pbFaint
  4068.               end
  4069.             end
  4070.           end
  4071.         end
  4072.       when PBWeather::RAINDANCE
  4073.         @weatherduration=@weatherduration-1 if @weatherduration>0
  4074.         if @weatherduration==0
  4075.           pbDisplay(_INTL("The rain stopped."))
  4076.           @weather=0
  4077.           if $febackup == 34
  4078.             $fefieldeffect = 34
  4079.             pbChangeBGSprite
  4080.             pbDisplay(_INTL("The starry sky shone through!"));
  4081.           end
  4082.         else
  4083.           pbCommonAnimation("Rain",nil,nil)
  4084. #         pbDisplay(_INTL("Rain continues to fall."));
  4085.           if $fefieldeffect == 7 # Burning Field
  4086.             if   $fefieldeffect == $febackup
  4087.               $fefieldeffect = 0
  4088.             else
  4089.               $fefieldeffect = $febackup
  4090.             end
  4091.             pbChangeBGSprite
  4092.             pbDisplay(_INTL("The rain snuffed out the flame!"));
  4093.           end
  4094.           if $fefieldeffect == 34 # Starlight Arena
  4095.             $fefieldeffect = 0
  4096.             pbChangeBGSprite
  4097.             pbDisplay(_INTL("The weather blocked out the starry sky!"));
  4098.           end
  4099.         end
  4100.       when PBWeather::SANDSTORM
  4101.         @weatherduration=@weatherduration-1 if @weatherduration>0
  4102.         if @weatherduration==0
  4103.           pbDisplay(_INTL("The sandstorm subsided."))
  4104.           @weather=0
  4105.           if $febackup == 34
  4106.             $fefieldeffect = 34
  4107.             pbChangeBGSprite
  4108.             pbDisplay(_INTL("The starry sky shone through!"));
  4109.           end
  4110.         else
  4111.           pbCommonAnimation("Sandstorm",nil,nil)
  4112. #         pbDisplay(_INTL("The sandstorm rages."))
  4113.           if $fefieldeffect == 7 # Burning Field
  4114.             if   $fefieldeffect == $febackup
  4115.               $fefieldeffect = 0
  4116.             else
  4117.               $fefieldeffect = $febackup
  4118.             end
  4119.             pbChangeBGSprite
  4120.             pbDisplay(_INTL("The sand snuffed out the flame!"));
  4121.           end
  4122.           if $fefieldeffect == 9 # Rainbow Field
  4123.             if   $fefieldeffect == $febackup
  4124.               $fefieldeffect = 0
  4125.             else
  4126.               $fefieldeffect = $febackup
  4127.             end
  4128.             pbChangeBGSprite
  4129.             pbDisplay(_INTL("The weather blocked out the rainbow!"));
  4130.           end
  4131.           if $fefieldeffect == 34 # Starlight Arena
  4132.             $fefieldeffect = 0
  4133.             pbChangeBGSprite
  4134.             pbDisplay(_INTL("The weather blocked out the starry sky!"));
  4135.           end
  4136.           if pbWeather==PBWeather::SANDSTORM
  4137.             endmessage=false
  4138.             for i in priority
  4139.               next if i.isFainted?
  4140.               if !i.pbHasType?(:GROUND) && !i.pbHasType?(:ROCK) && !i.pbHasType?(:STEEL) &&
  4141.                !i.hasWorkingAbility(:SANDVEIL) &&
  4142.                !i.hasWorkingAbility(:SANDRUSH) &&
  4143.                !i.hasWorkingAbility(:SANDFORCE) &&
  4144.                !i.hasWorkingAbility(:MAGICGUARD) &&
  4145.                !isConst?(i.item,PBItems,:SAFETYGOGGLES) &&
  4146.                !i.hasWorkingAbility(:OVERCOAT) &&
  4147.                ![0xCA,0xCB].include?(PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function) # Dig, Dive
  4148.                 pbDisplay(_INTL("The Pokemon were buffeted by the sandstorm!",i.pbThis)) if endmessage==false
  4149.                 endmessage=true
  4150.                 @scene.pbDamageAnimation(i,0)
  4151.                 i.pbReduceHP((i.totalhp/16).floor)
  4152.                 if i.isFainted?
  4153.                   return if !i.pbFaint
  4154.                 end
  4155.               end
  4156.             end
  4157.           end
  4158.         end
  4159.       when PBWeather::HAIL
  4160.         @weatherduration=@weatherduration-1 if @weatherduration>0
  4161.         if @weatherduration==0
  4162.           pbDisplay(_INTL("The hail stopped."))
  4163.           @weather=0
  4164.           if $febackup == 34
  4165.             $fefieldeffect = 34
  4166.             pbChangeBGSprite
  4167.             pbDisplay(_INTL("The starry sky shone through!"));
  4168.           end
  4169.         elsif $fefieldeffect == 16
  4170.           pbDisplay(_INTL("The hail melted away."))
  4171.           @weather=0
  4172.         else
  4173.           pbCommonAnimation("Hail",nil,nil)
  4174. #         pbDisplay(_INTL("Hail continues to fall."))
  4175.           if $fefieldeffect == 9 # Rainbow ield
  4176.             if   $fefieldeffect == $febackup
  4177.               $fefieldeffect = 0
  4178.             else
  4179.               $fefieldeffect = $febackup
  4180.             end
  4181.             pbChangeBGSprite
  4182.             pbDisplay(_INTL("The weather blocked out the rainbow!"));
  4183.           end
  4184.           if $fefieldeffect == 34 # Starlight Arena
  4185.             $fefieldeffect = 0
  4186.             pbChangeBGSprite
  4187.             pbDisplay(_INTL("The weather blocked out the starry sky!"));
  4188.           end
  4189.           if pbWeather==PBWeather::HAIL
  4190.             endmessage=false
  4191.             for i in priority
  4192.               next if i.isFainted?
  4193.               if !i.pbHasType?(:ICE) &&
  4194.                  !i.hasWorkingAbility(:ICEBODY) &&
  4195.                  !i.hasWorkingAbility(:SNOWCLOAK) &&
  4196.                  !i.hasWorkingAbility(:MAGICGUARD) &&
  4197.                 !isConst?(i.item,PBItems,:SAFETYGOGGLES) &&
  4198.                  !i.hasWorkingAbility(:OVERCOAT) &&
  4199.                  ![0xCA,0xCB].include?(PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function) # Dig, Dive
  4200.                 pbDisplay(_INTL("The Pokemon were buffeted by the hail!",i.pbThis)) if endmessage==false
  4201.                 endmessage=true
  4202.                 @scene.pbDamageAnimation(i,0)
  4203.                 i.pbReduceHP((i.totalhp/16).floor)
  4204.                 if i.isFainted?
  4205.                   return if !i.pbFaint
  4206.                 end
  4207.               end
  4208.             end
  4209.             if $fefieldeffect  == 27
  4210.               $fecounter+=1
  4211.               if $fecounter == 3
  4212.                 $fecounter = 0
  4213.                 $fefieldeffect = 28
  4214.                 pbChangeBGSprite
  4215.                 pbDisplay(_INTL("The mountain was covered in snow!"))
  4216.               end
  4217.             end
  4218.           end
  4219.         end
  4220.       when PBWeather::STRONGWINDS
  4221.           pbCommonAnimation("Wind",nil,nil)
  4222. #         pbDisplay(_INTL("The wind is strong."));
  4223.       end
  4224.     end
  4225.     # Shadow Sky weather
  4226.     if isConst?(@weather,PBWeather,:SHADOWSKY)
  4227.       @weatherduration=@weatherduration-1 if @weatherduration>0
  4228.       if @weatherduration==0
  4229.         pbDisplay(_INTL("The shadow sky faded."))
  4230.         @weather=0
  4231.       else
  4232.         pbCommonAnimation("ShadowSky",nil,nil)
  4233. #        pbDisplay(_INTL("The shadow sky continues."));
  4234.         if isConst?(pbWeather,PBWeather,:SHADOWSKY)
  4235.           for i in priority
  4236.             next if i.isFainted?
  4237.             if !i.isShadow?
  4238.               pbDisplay(_INTL("{1} was hurt by the shadow sky!",i.pbThis))
  4239.               @scene.pbDamageAnimation(i,0)
  4240.               i.pbReduceHP((i.totalhp/16).floor)
  4241.               if i.isFainted?
  4242.                 return if !i.pbFaint
  4243.               end
  4244.             end
  4245.           end
  4246.         end
  4247.       end
  4248.     end
  4249.     # Future Sight/Doom Desire
  4250.     for i in battlers   # not priority
  4251.       next if i.isFainted?
  4252.       if i.effects[PBEffects::FutureSight]>0
  4253.         i.effects[PBEffects::FutureSight]-=1
  4254.         if i.effects[PBEffects::FutureSight]==0
  4255.           if i.effects[PBEffects::FutureSightMove] == 516
  4256.             move=PokeBattle_Move.pbFromPBMove(self,PBMove.new(637))
  4257.           elsif i.effects[PBEffects::FutureSightMove] == 450
  4258.             move=PokeBattle_Move.pbFromPBMove(self,PBMove.new(636))
  4259.           end
  4260.           pbDisplay(_INTL("{1} took the {2} attack!",i.pbThis,move.name))
  4261.           moveuser=@battlers[i.effects[PBEffects::FutureSightUser]]
  4262.           if i.isFainted? || move.pbAccuracyCheck(moveuser,i)
  4263.             i.damagestate.reset
  4264.             damage = nil
  4265.             if i.effects[PBEffects::FutureSightMove] == 450 && !(i.pbHasType?(:DARK))
  4266.               pbCommonAnimation("FutureSight",i,nil)
  4267.             elsif i.effects[PBEffects::FutureSightMove] == 516
  4268.               pbCommonAnimation("DoomDesire",i,nil)
  4269.             end
  4270.             move.pbReduceHPDamage(damage,moveuser,i)
  4271.             move.pbEffectMessages(moveuser,i)
  4272.           else
  4273.             pbDisplay(_INTL("But it failed!"))
  4274.           end
  4275.           i.effects[PBEffects::FutureSight]=0
  4276.           i.effects[PBEffects::FutureSightMove]=0
  4277.           i.effects[PBEffects::FutureSightDamage]=0
  4278.           i.effects[PBEffects::FutureSightUser]=-1
  4279.           if i.isFainted?
  4280.             return if !i.pbFaint
  4281.             next
  4282.           end
  4283.         end
  4284.       end
  4285.     end
  4286.     for i in priority
  4287.       next if i.isFainted?
  4288.       # Rain Dish
  4289.       if pbWeather==PBWeather::RAINDANCE && i.hasWorkingAbility(:RAINDISH)
  4290.         hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  4291.         pbDisplay(_INTL("{1}'s Rain Dish restored its HP a little!",i.pbThis)) if hpgain>0
  4292.       end
  4293.     # Dry Skin
  4294.       if isConst?(i.ability,PBAbilities,:DRYSKIN)
  4295.         if pbWeather==PBWeather::RAINDANCE && i.effects[PBEffects::HealBlock]==0
  4296.           hpgain=i.pbRecoverHP((i.totalhp/8).floor,true)
  4297.           pbDisplay(_INTL("{1}'s Dry Skin was healed by the rain!",i.pbThis)) if hpgain>0
  4298.         elsif pbWeather==PBWeather::SUNNYDAY
  4299.           @scene.pbDamageAnimation(i,0)
  4300.           hploss=i.pbReduceHP((i.totalhp/8).floor)
  4301.           pbDisplay(_INTL("{1}'s Dry Skin was hurt by the sunlight!",i.pbThis)) if hploss>0
  4302.         elsif $fefieldeffect == 11 && !i.pbHasType?(:STEEL)
  4303.           if !i.pbHasType?(:POISON)
  4304.             @scene.pbDamageAnimation(i,0)
  4305.             hploss=i.pbReduceHP((i.totalhp/8).floor)
  4306.             pbDisplay(_INTL("{1}'s Dry Skin absorbed the poison!",i.pbThis)) if hploss>0
  4307.           elsif i.effects[PBEffects::HealBlock]==0
  4308.             hpgain=i.pbRecoverHP((i.totalhp/8).floor,true)
  4309.             pbDisplay(_INTL("{1}'s Dry Skin was healed by the poison!",i.pbThis)) if hpgain>0
  4310.           end
  4311.         elsif $fefieldeffect == 12
  4312.           @scene.pbDamageAnimation(i,0)
  4313.           hploss=i.pbReduceHP((i.totalhp/8).floor)
  4314.           pbDisplay(_INTL("{1}'s Dry Skin was hurt by the desert air!",i.pbThis)) if hploss>0
  4315.         elsif $fefieldeffect == 3 || $fefieldeffect == 8 && # Misty/Swamp Field
  4316.           i.effects[PBEffects::HealBlock]==0    
  4317.           hpgain=(i.totalhp/16).floor
  4318.           hpgain=i.pbRecoverHP(hpgain,true)
  4319.           pbDisplay(_INTL("{1}'s Dry Skin was healed by the mist!",i.pbThis)) if hpgain>0
  4320.         elsif $fefieldeffect == 21 || $fefieldeffect == 22 && #Water fields
  4321.           i.effects[PBEffects::HealBlock]==0    
  4322.           hpgain=(i.totalhp/16).floor
  4323.           hpgain=i.pbRecoverHP(hpgain,true)
  4324.           pbDisplay(_INTL("{1}'s Dry Skin was healed by the water!",i.pbThis)) if hpgain>0
  4325.         end
  4326.       end
  4327.       # Ice Body
  4328.       if (pbWeather==PBWeather::HAIL || $fefieldeffect == 13 ||
  4329.        $fefieldeffefct == 28) &&
  4330.        i.hasWorkingAbility(:ICEBODY) && i.effects[PBEffects::HealBlock]==0
  4331.         hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  4332.         pbDisplay(_INTL("{1}'s Ice Body restored its HP a little!",i.pbThis)) if hpgain>0
  4333.       end
  4334.       if i.isFainted?
  4335.         return if !i.pbFaint
  4336.         next
  4337.       end
  4338.     end
  4339.     # Wish
  4340.     for i in priority
  4341.       next if i.isFainted?
  4342.       if i.effects[PBEffects::Wish]>0
  4343.         i.effects[PBEffects::Wish]-=1
  4344.         if i.effects[PBEffects::Wish]==0
  4345.           hpgain=i.pbRecoverHP(i.effects[PBEffects::WishAmount],true)
  4346.           if hpgain>0
  4347.             wishmaker=pbThisEx(i.index,i.effects[PBEffects::WishMaker])
  4348.             pbDisplay(_INTL("{1}'s wish came true!",wishmaker))
  4349.           end
  4350.         end
  4351.       end
  4352.     end
  4353.     # Fire Pledge + Grass Pledge combination damage - should go here
  4354.     for i in priority
  4355.       next if i.isFainted?
  4356.       # Shed Skin
  4357.       if i.hasWorkingAbility(:SHEDSKIN)
  4358.         if pbRandom(10)<3 && i.status>0
  4359.           case i.status
  4360.             when PBStatuses::SLEEP
  4361.               pbDisplay(_INTL("{1}'s Shed Skin cured its sleep problem!",i.pbThis))
  4362.             when PBStatuses::FROZEN
  4363.               pbDisplay(_INTL("{1}'s Shed Skin cured its ice problem!",i.pbThis))
  4364.             when PBStatuses::BURN
  4365.               pbDisplay(_INTL("{1}'s Shed Skin cured its burn problem!",i.pbThis))
  4366.             when PBStatuses::POISON
  4367.               pbDisplay(_INTL("{1}'s Shed Skin cured its poison problem!",i.pbThis))
  4368.             when PBStatuses::PARALYSIS
  4369.               pbDisplay(_INTL("{1}'s Shed Skin cured its paralysis problem!",i.pbThis))
  4370.           end
  4371.           i.status=0
  4372.           i.statusCount=0
  4373.         end
  4374.       end
  4375.       # Hydration
  4376.       if i.hasWorkingAbility(:HYDRATION) && (pbWeather==PBWeather::RAINDANCE ||
  4377.         $fefieldeffect == 21 || $fefieldeffect == 22)
  4378.         if i.status>0
  4379.           case i.status
  4380.             when PBStatuses::SLEEP
  4381.               pbDisplay(_INTL("{1}'s Hydration cured its sleep problem!",i.pbThis))
  4382.             when PBStatuses::FROZEN
  4383.               pbDisplay(_INTL("{1}'s Hydration cured its ice problem!",i.pbThis))
  4384.             when PBStatuses::BURN
  4385.               pbDisplay(_INTL("{1}'s Hydration cured its burn problem!",i.pbThis))
  4386.             when PBStatuses::POISON
  4387.               pbDisplay(_INTL("{1}'s Hydration cured its poison problem!",i.pbThis))
  4388.             when PBStatuses::PARALYSIS
  4389.               pbDisplay(_INTL("{1}'s Hydration cured its paralysis problem!",i.pbThis))
  4390.           end
  4391.           i.status=0
  4392.           i.statusCount=0
  4393.         end
  4394.       end
  4395.       if i.hasWorkingAbility(:WATERVEIL) && ($fefieldeffect == 21 ||
  4396.         $fefieldeffect == 22)
  4397.         if i.status>0
  4398.               pbDisplay(_INTL("{1}'s Water Veil cured its status problem!",i.pbThis))
  4399.           i.status=0
  4400.           i.statusCount=0
  4401.         end
  4402.       end
  4403.       # Healer
  4404.       if i.hasWorkingAbility(:HEALER)
  4405.         partner=i.pbPartner
  4406.         if partner
  4407.           if pbRandom(10)<3 && partner.status>0
  4408.             case partner.status
  4409.               when PBStatuses::SLEEP
  4410.                 pbDisplay(_INTL("{1}'s Healer cured its partner's sleep problem!",i.pbThis))
  4411.               when PBStatuses::FROZEN
  4412.                 pbDisplay(_INTL("{1}'s Healer cured its partner's ice problem!",i.pbThis))
  4413.               when PBStatuses::BURN
  4414.                 pbDisplay(_INTL("{1}'s Healer cured its partner's burn problem!",i.pbThis))
  4415.               when PBStatuses::POISON
  4416.                 pbDisplay(_INTL("{1}'s Healer cured its partner's poison problem!",i.pbThis))
  4417.               when PBStatuses::PARALYSIS
  4418.                 pbDisplay(_INTL("{1}'s Healer cured its partner's paralysis problem!",i.pbThis))
  4419.             end
  4420.             partner.status=0
  4421.             partner.statusCount=0
  4422.           end
  4423.         end
  4424.       end
  4425.     end
  4426.     # Held berries/Leftovers/Black Sludge
  4427.     for i in priority
  4428.       next if i.isFainted?
  4429.       i.pbBerryCureCheck(true)
  4430.       if i.isFainted?
  4431.         return if !i.pbFaint
  4432.         next
  4433.       end
  4434.     end
  4435.     # Aqua Ring
  4436.     for i in priority
  4437.       next if i.hp<=0
  4438.       if i.effects[PBEffects::AquaRing]
  4439.         if $fefieldeffect == 11 &&
  4440.          !i.pbHasType?(:STEEL) && !i.pbHasType?(:POISON)
  4441.           @scene.pbDamageAnimation(i,0)
  4442.           i.pbReduceHP((i.totalhp/16).floor)
  4443.           pbDisplay(_INTL("{1}'s Aqua Ring absorbed poison!",i.pbThis)) if hpgain>0
  4444.           if i.hp<=0
  4445.             return if !i.pbFaint
  4446.           end
  4447.         elsif i.effects[PBEffects::HealBlock]==0
  4448.           hpgain=(i.totalhp/16).floor
  4449.           hpgain=(hpgain*1.3).floor if isConst?(i.item,PBItems,:BIGROOT)
  4450.           hpgain=(hpgain*2).floor if $fefieldeffect == 3 ||
  4451.            $fefieldeffect == 8 || $fefieldeffect == 21 || $fefieldeffect == 22
  4452.           hpgain=i.pbRecoverHP(hpgain,true)
  4453.           pbDisplay(_INTL("{1}'s Aqua Ring restored its HP a little!",i.pbThis)) if hpgain>0
  4454.         end
  4455.       end
  4456.     end
  4457.     # Ingrain
  4458.     for i in priority
  4459.       next if i.hp<=0
  4460.       if i.effects[PBEffects::Ingrain]
  4461.         if ($fefieldeffect == 8 || $fefieldeffect == 10) &&
  4462.          (!i.pbHasType?(:STEEL) && !i.pbHasType?(:POISON))
  4463.           @scene.pbDamageAnimation(i,0)
  4464.           i.pbReduceHP((i.totalhp/16).floor)
  4465.           pbDisplay(_INTL("{1} absorbed foul nutrients with its roots!",i.pbThis))
  4466.           if i.hp<=0
  4467.             return if !i.pbFaint
  4468.           end
  4469.         else
  4470.           if ($fefieldeffect == 33 && $fecounter >2)
  4471.             hpgain=(i.totalhp/4).floor            
  4472.           elsif ($fefieldeffect == 15 || ($fefieldeffect == 33 && $fecounter >0))
  4473.             hpgain=(i.totalhp/8).floor
  4474.           elsif i.effects[PBEffects::HealBlock]==0
  4475.             hpgain=(i.totalhp/16).floor
  4476.           end
  4477.           if i.effects[PBEffects::HealBlock]==0
  4478.             hpgain=(hpgain*1.3).floor if isConst?(i.item,PBItems,:BIGROOT)
  4479.             hpgain=i.pbRecoverHP(hpgain,true)
  4480.             pbDisplay(_INTL("{1} absorbed nutrients with its roots!",i.pbThis)) if hpgain>0
  4481.           end
  4482.         end
  4483.       end
  4484.     end
  4485.     # Leech Seed
  4486.     for i in priority
  4487.       if i.hasWorkingAbility(:LIQUIDOOZE,true) && i.effects[PBEffects::LeechSeed]>=0
  4488.         recipient=@battlers[i.effects[PBEffects::LeechSeed]]
  4489.         if recipient && !recipient.isFainted?
  4490.           hploss=(i.totalhp/8).floor
  4491.           hploss= hploss * 2 if $fefieldeffect == 19
  4492.           pbCommonAnimation("LeechSeed",recipient,i)
  4493.           i.pbReduceHP(hploss,true)      
  4494.           hploss= hploss / 2 if $fefieldeffect == 19 || $fefieldeffect == 26          
  4495.           recipient.pbReduceHP(hploss,true)
  4496.           pbDisplay(_INTL("{1} sucked up the liquid ooze!",recipient.pbThis))
  4497.           if i.isFainted?          
  4498.             return if !i.pbFaint
  4499.           end
  4500.           if recipient.isFainted?
  4501.             return if !recipient.pbFaint
  4502.           end  
  4503.           next          
  4504.         end
  4505.       end      
  4506.       next if i.isFainted?
  4507.       if i.effects[PBEffects::LeechSeed]>=0
  4508.         recipient=@battlers[i.effects[PBEffects::LeechSeed]]
  4509.         if recipient && !recipient.isFainted?  &&
  4510.           !i.hasWorkingAbility(:MAGICGUARD) # if recipient exists
  4511.           pbCommonAnimation("LeechSeed",recipient,i)
  4512.           hploss=i.pbReduceHP((i.totalhp/8).floor,true)
  4513.           hploss= hploss * 2 if $fefieldeffect == 19
  4514.       #    if i.hasWorkingAbility(:LIQUIDOOZE)
  4515.       #      recipient.pbReduceHP(hploss,true)
  4516.       #      pbDisplay(_INTL("{1} sucked up the liquid ooze!",recipient.pbThis))
  4517.       #      hploss= hploss / 2 if $fefieldeffect == 19 || $fefieldeffect == 26
  4518.           if recipient.effects[PBEffects::HealBlock]==0
  4519.             hploss=(hploss*1.3).floor if recipient.hasWorkingItem(:BIGROOT)
  4520.             recipient.pbRecoverHP(hploss,true)
  4521.             pbDisplay(_INTL("{1}'s health was sapped by Leech Seed!",i.pbThis))
  4522.           end
  4523.           if i.isFainted?          
  4524.             return if !i.pbFaint
  4525.           end
  4526.           if recipient.isFainted?
  4527.             return if !recipient.pbFaint
  4528.           end
  4529.         end
  4530.       end
  4531.     end
  4532.     for i in priority
  4533.       next if i.isFainted?
  4534.       # Poison/Bad poison
  4535.       if i.status==PBStatuses::POISON  &&
  4536.           !i.hasWorkingAbility(:MAGICGUARD)
  4537.         if i.hasWorkingAbility(:POISONHEAL)
  4538.           if i.effects[PBEffects::HealBlock]==0
  4539.             if i.hp<i.totalhp
  4540.               pbCommonAnimation("Poison",i,nil)
  4541.               i.pbRecoverHP((i.totalhp/8).floor,true)
  4542.               pbDisplay(_INTL("{1} is healed by poison!",i.pbThis))
  4543.             end
  4544.             if i.statusCount>0
  4545.               i.effects[PBEffects::Toxic]+=1
  4546.               i.effects[PBEffects::Toxic]=[15,i.effects[PBEffects::Toxic]].min
  4547.             end
  4548.           end
  4549.         else
  4550.           i.pbContinueStatus
  4551.           if i.statusCount==0
  4552.             i.pbReduceHP((i.totalhp/8).floor)
  4553.           else
  4554.             i.effects[PBEffects::Toxic]+=1
  4555.             i.effects[PBEffects::Toxic]=[15,i.effects[PBEffects::Toxic]].min
  4556.             i.pbReduceHP((i.totalhp/16).floor*i.effects[PBEffects::Toxic])
  4557.           end
  4558.         end
  4559.       end
  4560.       # Burn
  4561.       if i.status==PBStatuses::BURN &&
  4562.           !i.hasWorkingAbility(:MAGICGUARD)
  4563.         i.pbContinueStatus
  4564.         if i.hasWorkingAbility(:HEATPROOF) || $fefieldeffect == 13
  4565.           i.pbReduceHP((i.totalhp/16).floor)
  4566.         else
  4567.           i.pbReduceHP((i.totalhp/8).floor)
  4568.         end
  4569.       end
  4570.       # Nightmare
  4571.       if i.effects[PBEffects::Nightmare] &&
  4572.           !i.hasWorkingAbility(:MAGICGUARD) &&
  4573.         $fefieldeffect != 9
  4574.         if i.status==PBStatuses::SLEEP
  4575.           pbCommonAnimation("Nightmare",i,nil)          
  4576.           pbDisplay(_INTL("{1} is locked in a nightmare!",i.pbThis))
  4577.           i.pbReduceHP((i.totalhp/4).floor,true)
  4578.         else
  4579.           i.effects[PBEffects::Nightmare]=false
  4580.         end
  4581.       end
  4582.       if i.isFainted?
  4583.         return if !i.pbFaint
  4584.         next
  4585.       end
  4586.     end
  4587.      # Curse
  4588.     for i in priority
  4589.       next if i.isFainted?
  4590.       if $fefieldeffect == 29 && i.effects[PBEffects::Curse]
  4591.         i.effects[PBEffects::Curse] = false
  4592.         pbDisplay(_INTL("{1}'s curse was lifted!",i.pbThis))
  4593.       end
  4594.       if i.effects[PBEffects::Curse] &&
  4595.           !i.hasWorkingAbility(:MAGICGUARD)
  4596.           pbCommonAnimation("Curse",i,nil)
  4597.         pbDisplay(_INTL("{1} is afflicted by the curse!",i.pbThis))
  4598.         i.pbReduceHP((i.totalhp/4).floor,true)
  4599.       end
  4600.       if i.isFainted?
  4601.         return if !i.pbFaint
  4602.         next
  4603.       end
  4604.     end
  4605.     # Multi-turn attacks (Bind/Clamp/Fire Spin/Magma Storm/Sand Tomb/Whirlpool/Wrap)
  4606.     for i in priority
  4607.       next if i.isFainted?
  4608.       if i.effects[PBEffects::MultiTurn]>0
  4609.         i.effects[PBEffects::MultiTurn]-=1
  4610.         movename=PBMoves.getName(i.effects[PBEffects::MultiTurnAttack])
  4611.         if i.effects[PBEffects::MultiTurn]==0
  4612.           pbDisplay(_INTL("{1} was freed from {2}!",i.pbThis,movename))
  4613.           $bindingband=0
  4614.         else
  4615.           pbDisplay(_INTL("{1} is hurt by {2}!",i.pbThis,movename))
  4616.           if isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:BIND)
  4617.             pbCommonAnimation("Bind",i,nil)
  4618.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:CLAMP)
  4619.             pbCommonAnimation("Clamp",i,nil)
  4620.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:FIRESPIN)
  4621.             pbCommonAnimation("FireSpin",i,nil)
  4622.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:MAGMASTORM)
  4623.             pbCommonAnimation("MagmaStorm",i,nil)
  4624.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:SANDTOMB)
  4625.             pbCommonAnimation("SandTomb",i,nil)
  4626.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:WRAP)
  4627.             pbCommonAnimation("Wrap",i,nil)
  4628.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:INFESTATION)
  4629.             pbCommonAnimation("Infestation",i,nil)
  4630.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:WHIRLPOOL)
  4631.             pbCommonAnimation("Whirlpool",i,nil)              
  4632.           else
  4633.             pbCommonAnimation("Wrap",i,nil)
  4634.           end
  4635.           @scene.pbDamageAnimation(i,0)
  4636.           if $bindingband==1
  4637.             i.pbReduceHP((i.totalhp/6).floor)
  4638.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:MAGMASTORM) &&
  4639.             $fefieldeffect == 32
  4640.             i.pbReduceHP((i.totalhp/6).floor)
  4641.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:SANDTOMB) &&
  4642.             $fefieldeffect == 12
  4643.             i.pbReduceHP((i.totalhp/6).floor)
  4644.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:WHIRLPOOL) &&
  4645.             ($fefieldeffect == 21 || $fefieldeffect == 22)
  4646.             i.pbReduceHP((i.totalhp/6).floor)
  4647.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:INFESTATION) &&
  4648.             $fefieldeffect == 15
  4649.             i.pbReduceHP((i.totalhp/6).floor)
  4650.           elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:INFESTATION) &&
  4651.             $fefieldeffect == 33 && $fecounter > 1
  4652.               case $fecounter
  4653.                 when 2
  4654.                   i.pbReduceHP((i.totalhp/6).floor)
  4655.                 when 3
  4656.                   i.pbReduceHP((i.totalhp/4).floor)
  4657.                 when 4
  4658.                   i.pbReduceHP((i.totalhp/3).floor)
  4659.               end
  4660.           else
  4661.             i.pbReduceHP((i.totalhp/8).floor)
  4662.           end
  4663.           if isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:SANDTOMB) &&
  4664.            $fefieldeffect == 20
  4665.             i.pbCanReduceStatStage?(PBStats::ACCURACY)
  4666.             i.pbReduceStat(PBStats::ACCURACY,1,true)
  4667.           end
  4668.         end
  4669.       end  
  4670.       if i.hp<=0
  4671.         return if !i.pbFaint
  4672.         next
  4673.       end
  4674.     end
  4675.     # Taunt
  4676.     for i in priority
  4677.       next if i.isFainted?
  4678.       if i.effects[PBEffects::Taunt]>0
  4679.         i.effects[PBEffects::Taunt]-=1
  4680.         if i.effects[PBEffects::Taunt]==0
  4681.           pbDisplay(_INTL("{1} recovered from the taunting!",i.pbThis))
  4682.         end
  4683.       end
  4684.     end
  4685.     # Encore
  4686.     for i in priority
  4687.       next if i.isFainted?
  4688.       if i.effects[PBEffects::Encore]>0
  4689.         if i.moves[i.effects[PBEffects::EncoreIndex]].id!=i.effects[PBEffects::EncoreMove]
  4690.           i.effects[PBEffects::Encore]=0
  4691.           i.effects[PBEffects::EncoreIndex]=0
  4692.           i.effects[PBEffects::EncoreMove]=0
  4693.         else
  4694.           i.effects[PBEffects::Encore]-=1
  4695.           if i.effects[PBEffects::Encore]==0 || i.moves[i.effects[PBEffects::EncoreIndex]].pp==0
  4696.             i.effects[PBEffects::Encore]=0
  4697.             pbDisplay(_INTL("{1}'s encore ended!",i.pbThis))
  4698.           end
  4699.         end
  4700.       end
  4701.     end
  4702.     # Disable/Cursed Body
  4703.     for i in priority
  4704.       next if i.isFainted?
  4705.       if i.effects[PBEffects::Disable]>0
  4706.         i.effects[PBEffects::Disable]-=1
  4707.         if i.effects[PBEffects::Disable]==0
  4708.           i.effects[PBEffects::DisableMove]=0
  4709.           pbDisplay(_INTL("{1} is disabled no more!",i.pbThis))
  4710.         end
  4711.       end
  4712.     end
  4713.     # Magnet Rise
  4714.     for i in priority
  4715.       next if i.isFainted?
  4716.       if i.effects[PBEffects::MagnetRise]>0
  4717.         i.effects[PBEffects::MagnetRise]-=1
  4718.         if i.effects[PBEffects::MagnetRise]==0
  4719.           pbDisplay(_INTL("{1} stopped levitating.",i.pbThis))
  4720.         end
  4721.       end
  4722.     end
  4723.     # Telekinesis
  4724.     for i in priority
  4725.       next if i.isFainted?
  4726.       if i.effects[PBEffects::Telekinesis]>0
  4727.         i.effects[PBEffects::Telekinesis]-=1
  4728.         if i.effects[PBEffects::Telekinesis]==0
  4729.           pbDisplay(_INTL("{1} stopped levitating.",i.pbThis))
  4730.         end
  4731.       end
  4732.     end
  4733.     # Heal Block
  4734.     for i in priority
  4735.       next if i.isFainted?
  4736.       if i.effects[PBEffects::HealBlock]>0
  4737.         i.effects[PBEffects::HealBlock]-=1
  4738.         if i.effects[PBEffects::HealBlock]==0
  4739.           pbDisplay(_INTL("The heal block on {1} ended.",i.pbThis))
  4740.         end
  4741.       end
  4742.     end
  4743.     # Embargo
  4744.     for i in priority
  4745.       next if i.isFainted?
  4746.       if i.effects[PBEffects::Embargo]>0
  4747.         i.effects[PBEffects::Embargo]-=1
  4748.         if i.effects[PBEffects::Embargo]==0
  4749.           pbDisplay(_INTL("The embargo on {1} was lifted.",i.pbThis(true)))
  4750.         end
  4751.       end
  4752.     end
  4753.     # Yawn
  4754.     for i in priority
  4755.       next if i.isFainted?
  4756.       if i.effects[PBEffects::Yawn]>0
  4757.         i.effects[PBEffects::Yawn]-=1
  4758.         if i.effects[PBEffects::Yawn]==0 && i.pbCanSleepYawn?
  4759.           i.pbSleep
  4760.           pbDisplay(_INTL("{1} fell asleep!",i.pbThis))
  4761.         end
  4762.       end
  4763.     end
  4764.     # Perish Song
  4765.     perishSongUsers=[]
  4766.     for i in priority
  4767.       next if i.isFainted?
  4768.       if i.effects[PBEffects::PerishSong]>0
  4769.         i.effects[PBEffects::PerishSong]-=1
  4770.         pbDisplay(_INTL("{1}'s Perish count fell to {2}!",i.pbThis,i.effects[PBEffects::PerishSong]))
  4771.         if i.effects[PBEffects::PerishSong]==0
  4772.           perishSongUsers.push(i.effects[PBEffects::PerishSongUser])
  4773.           i.pbReduceHP(i.hp,true)
  4774.         end
  4775.       end
  4776.       if i.isFainted?
  4777.         return if !i.pbFaint
  4778.       end
  4779.     end
  4780.     if perishSongUsers.length>0
  4781.       # If all remaining Pokemon fainted by a Perish Song triggered by a single side
  4782.       if (perishSongUsers.find_all{|item| pbIsOpposing?(item) }.length==perishSongUsers.length) ||
  4783.          (perishSongUsers.find_all{|item| !pbIsOpposing?(item) }.length==perishSongUsers.length)
  4784.         pbJudgeCheckpoint(@battlers[perishSongUsers[0]])
  4785.       end
  4786.     end
  4787.     if @decision>0
  4788.       pbGainEXP
  4789.       return
  4790.     end
  4791.     # Reflect
  4792.     for i in 0...2
  4793.       if sides[i].effects[PBEffects::Reflect]>0
  4794.         sides[i].effects[PBEffects::Reflect]-=1
  4795.         if sides[i].effects[PBEffects::Reflect]==0
  4796.           pbDisplay(_INTL("Your team's Reflect faded!")) if i==0
  4797.           pbDisplay(_INTL("The opposing team's Reflect faded!")) if i==1
  4798.         end
  4799.       end
  4800.     end
  4801.     # Light Screen
  4802.     for i in 0...2
  4803.       if sides[i].effects[PBEffects::LightScreen]>0
  4804.         sides[i].effects[PBEffects::LightScreen]-=1
  4805.         if sides[i].effects[PBEffects::LightScreen]==0
  4806.           pbDisplay(_INTL("Your team's Light Screen faded!")) if i==0
  4807.           pbDisplay(_INTL("The opposing team's Light Screen faded!")) if i==1
  4808.         end
  4809.       end
  4810.     end
  4811.     # Safeguard
  4812.     for i in 0...2
  4813.       if sides[i].effects[PBEffects::Safeguard]>0
  4814.         sides[i].effects[PBEffects::Safeguard]-=1
  4815.         if sides[i].effects[PBEffects::Safeguard]==0
  4816.           pbDisplay(_INTL("Your team is no longer protected by Safeguard!")) if i==0
  4817.           pbDisplay(_INTL("The opposing team is no longer protected by Safeguard!")) if i==1
  4818.         end
  4819.       end
  4820.     end
  4821.     # Mist
  4822.     for i in 0...2
  4823.       if sides[i].effects[PBEffects::Mist]>0
  4824.         sides[i].effects[PBEffects::Mist]-=1
  4825.         if sides[i].effects[PBEffects::Mist]==0
  4826.           pbDisplay(_INTL("Your team's Mist faded!")) if i==0
  4827.           pbDisplay(_INTL("The opposing team's Mist faded!")) if i==1
  4828.         end
  4829.       end
  4830.     end
  4831.     # Tailwind
  4832.     for i in 0...2
  4833.       if sides[i].effects[PBEffects::Tailwind]>0
  4834.         sides[i].effects[PBEffects::Tailwind]-=1
  4835.         if sides[i].effects[PBEffects::Tailwind]==0
  4836.           pbDisplay(_INTL("Your team's tailwind stopped blowing!")) if i==0
  4837.           pbDisplay(_INTL("The opposing team's tailwind stopped blowing!")) if i==1
  4838.         end
  4839.       end
  4840.     end
  4841.     # Lucky Chant
  4842.     for i in 0...2
  4843.       if sides[i].effects[PBEffects::LuckyChant]>0
  4844.         sides[i].effects[PBEffects::LuckyChant]-=1
  4845.         if sides[i].effects[PBEffects::LuckyChant]==0
  4846.           pbDisplay(_INTL("Your team's Lucky Chant faded!")) if i==0
  4847.           pbDisplay(_INTL("The opposing team's Lucky Chant faded!")) if i==1
  4848.         end
  4849.       end
  4850.     end
  4851.     # Mud Sport
  4852.     if @field.effects[PBEffects::MudSport]>0
  4853.       @field.effects[PBEffects::MudSport]-=1
  4854.       if @field.effects[PBEffects::MudSport]==0
  4855.         pbDisplay(_INTL("The effects of Mud Sport faded."))
  4856.       end
  4857.     end
  4858.     # Water Sport
  4859.     if @field.effects[PBEffects::WaterSport]>0
  4860.       @field.effects[PBEffects::WaterSport]-=1
  4861.       if @field.effects[PBEffects::WaterSport]==0
  4862.         pbDisplay(_INTL("The effects of Water Sport faded."))
  4863.       end
  4864.     end
  4865.     # Gravity
  4866.     if @field.effects[PBEffects::Gravity]>0
  4867.       @field.effects[PBEffects::Gravity]-=1
  4868.       if @field.effects[PBEffects::Gravity]==0
  4869.         pbDisplay(_INTL("Gravity returned to normal."))
  4870.         if $febackup == 35 && $fefieldeffect != 35
  4871.           $fefieldeffect = $febackup
  4872.           pbDisplay(_INTL("The world broke apart again!"))
  4873.           pbChangeBGSprite
  4874.         end
  4875.       end
  4876.     end
  4877.     # Terrain
  4878.     if @field.effects[PBEffects::Terrain]>0
  4879.       terrain=[@field.effects[PBEffects::Terrain]].max
  4880.       terrain-=1
  4881.       @field.effects[PBEffects::Terrain] = terrain
  4882.       if terrain==0
  4883.         $fefieldeffect = $febackup
  4884.         pbDisplay(_INTL("The terrain returned to normal."))
  4885.         pbChangeBGSprite
  4886.       end
  4887.     end
  4888.     # Trick Room - should go here
  4889.     # Wonder Room - should go here
  4890.     # Magic Room
  4891.     if @field.effects[PBEffects::MagicRoom]>0
  4892.       @field.effects[PBEffects::MagicRoom]-=1
  4893.       if @field.effects[PBEffects::MagicRoom]==0
  4894.         pbDisplay(_INTL("The area returned to normal."))
  4895.       end
  4896.     end
  4897.     # Fairy Lock
  4898.     if @field.effects[PBEffects::FairyLock]>0
  4899.       @field.effects[PBEffects::FairyLock]-=1
  4900.       if @field.effects[PBEffects::FairyLock]==0
  4901.         # Fairy Lock seems to have no end-of-effect text so I've added some.
  4902.         pbDisplay(_INTL("The Fairy Lock was released."))
  4903.       end
  4904.     end
  4905.     # Uproar
  4906.     for i in priority
  4907.       next if i.isFainted?
  4908.       if i.effects[PBEffects::Uproar]>0
  4909.         for j in priority
  4910.           if !j.isFainted? && j.status==PBStatuses::SLEEP && !j.hasWorkingAbility(:SOUNDPROOF)
  4911.             j.effects[PBEffects::Nightmare]=false
  4912.             j.status=0
  4913.             j.statusCount=0
  4914.             pbDisplay(_INTL("{1} woke up in the uproar!",j.pbThis))
  4915.           end
  4916.         end
  4917.         i.effects[PBEffects::Uproar]-=1
  4918.         if i.effects[PBEffects::Uproar]==0
  4919.           pbDisplay(_INTL("{1} calmed down.",i.pbThis))
  4920.         else
  4921.           pbDisplay(_INTL("{1} is making an uproar!",i.pbThis))
  4922.         end
  4923.       end
  4924.     end
  4925.    
  4926.     #Wasteland hazard interaction
  4927.     if $fefieldeffect == 19
  4928.       for i in priority
  4929.         next if i.isFainted?
  4930.         # SR
  4931.         if i.pbOwnSide.effects[PBEffects::StealthRock]==true
  4932.           pbDisplay(_INTL("The waste swallowed up the pointed stones!"))
  4933.           i.pbOwnSide.effects[PBEffects::StealthRock]=false
  4934.           pbDisplay(_INTL("...Rocks spewed out from the ground below!"))
  4935.           atype=getConst(PBTypes,:ROCK) || 0
  4936.           if !i.isFainted?
  4937.             eff=PBTypes.getCombinedEffectiveness(atype,i.type1,i.type2)
  4938.             if eff>0
  4939.               @scene.pbDamageAnimation(i,0)
  4940.               i.pbReduceHP([(i.totalhp*eff/16).floor,1].max)
  4941.             end
  4942.           end
  4943.           partner=i.pbPartner
  4944.           if partner && !partner.isFainted?
  4945.             eff=PBTypes.getCombinedEffectiveness(atype,partner.type1,partner.type2)
  4946.             if eff>0
  4947.               @scene.pbDamageAnimation(partner,0)
  4948.               partner.pbReduceHP([(partner.totalhp*eff/16).floor,1].max)
  4949.             end
  4950.           end
  4951.         end
  4952.         # Spikes
  4953.         if i.pbOwnSide.effects[PBEffects::Spikes]>0
  4954.           pbDisplay(_INTL("The waste swallowed up the spikes!"))
  4955.           i.pbOwnSide.effects[PBEffects::Spikes]=0
  4956.           pbDisplay(_INTL("...Stalagmites burst up from the ground!"))
  4957.           if !i.isFainted? && !i.isAirborne?
  4958.             @scene.pbDamageAnimation(i,0)
  4959.             i.pbReduceHP([(i.totalhp/3).floor,1].max)
  4960.           end
  4961.           partner=i.pbPartner
  4962.           if partner && !partner.isFainted?
  4963.             if !partner.isAirborne?
  4964.               @scene.pbDamageAnimation(partner,0)
  4965.               partner.pbReduceHP([(partner.totalhp/3).floor,1].max)
  4966.             end
  4967.           end
  4968.         end
  4969.         # TSpikes
  4970.         if i.pbOwnSide.effects[PBEffects::ToxicSpikes]>0
  4971.           pbDisplay(_INTL("The waste swallowed up the toxic spikes!"))
  4972.           i.pbOwnSide.effects[PBEffects::ToxicSpikes]=0
  4973.           pbDisplay(_INTL("...Poison needles shot up from the ground!"))
  4974.           if !i.isFainted? && !i.isAirborne? &&
  4975.            !i.pbHasType?(:STEEL) && !i.pbHasType?(:POISON)
  4976.             @scene.pbDamageAnimation(i,0)
  4977.             i.pbReduceHP([(i.totalhp/8).floor,1].max)
  4978.             if i.status==0 && i.pbCanPoison?(false)
  4979.               i.status=PBStatuses::POISON
  4980.               i.statusCount=1
  4981.               i.effects[PBEffects::Toxic]=0
  4982.               pbCommonAnimation("Poison",i,nil)  
  4983.             end    
  4984.           end
  4985.           partner=i.pbPartner
  4986.           if partner && !partner.isFainted?
  4987.             if !partner.isAirborne? &&
  4988.              !partner.pbHasType?(:STEEL) && !partner.pbHasType?(:POISON)
  4989.               @scene.pbDamageAnimation(partner,0)
  4990.               partner.pbReduceHP([(partner.totalhp/8).floor,1].max)  
  4991.               if partner.status==0 && partner.pbCanPoison?(false)
  4992.                 partner.status=PBStatuses::POISON
  4993.                 partner.statusCount=1
  4994.                 partner.effects[PBEffects::Toxic]=0
  4995.                 pbCommonAnimation("Poison",i,nil)
  4996.               end
  4997.             end
  4998.           end
  4999.         end
  5000.         # StickyWeb
  5001.         if i.pbOwnSide.effects[PBEffects::StickyWeb]
  5002.           pbDisplay(_INTL("The waste swallowed up the sticky web!"))
  5003.           i.pbOwnSide.effects[PBEffects::StickyWeb]=false
  5004.           pbDisplay(_INTL("...Sticky string shot out of the ground!"))
  5005.           if !i.isFainted?
  5006.             if !i.pbTooLow?(PBStats::SPEED)
  5007.               i.pbReduceStatBasic(PBStats::SPEED,4)
  5008.               pbCommonAnimation("StatDown",i,nil)
  5009.               pbDisplay(_INTL("{1}'s Speed was severely lowered!",i.pbThis))
  5010.             end
  5011.           end
  5012.           partner=i.pbPartner
  5013.           if partner && !partner.isFainted?
  5014.             if !partner.pbTooLow?(PBStats::SPEED)
  5015.               partner.pbReduceStatBasic(PBStats::SPEED,4)
  5016.               pbCommonAnimation("StatDown",partner,nil)
  5017.               pbDisplay(_INTL("{1}'s Speed was severely lowered!",partner.pbThis))
  5018.             end
  5019.           end        
  5020.         end
  5021.         if i.hp<=0
  5022.           return if !i.pbFaint
  5023.           next
  5024.         end
  5025.       end
  5026.     end
  5027.     # End Wasteland hazards
  5028.     for i in priority
  5029.       next if i.isFainted?
  5030.       # Speed Boost
  5031.       # A Pokémon's turncount is 0 if it became active after the beginning of a round
  5032.       if i.turncount>0 && i.hasWorkingAbility(:SPEEDBOOST)
  5033.         if !i.pbTooHigh?(PBStats::SPEED)
  5034.           i.pbIncreaseStatBasic(PBStats::SPEED,1)
  5035.           pbCommonAnimation("StatUp",i,nil)
  5036.           pbDisplay(_INTL("{1}'s Speed Boost raised its Speed!",i.pbThis))
  5037.         end
  5038.       end
  5039.       if i.turncount>0 && $fefieldeffect == 8 && # Swamp Field
  5040.        !isConst?(i.ability,PBAbilities,:WHITESMOKE) &&
  5041.        !isConst?(i.ability,PBAbilities,:CLEARBODY) &&
  5042.        !isConst?(i.ability,PBAbilities,:QUICKFEET) &&
  5043.        !isConst?(i.ability,PBAbilities,:SWIFTSWIM)
  5044.         if !i.isAirborne?
  5045.           if !i.pbTooLow?(PBStats::SPEED)
  5046.             i.pbReduceStatBasic(PBStats::SPEED,1)
  5047.             pbCommonAnimation("StatDown",i,nil)
  5048.             pbDisplay(_INTL("{1}'s Speed sank...",i.pbThis))
  5049.           end
  5050.         end
  5051.       end
  5052.      #sleepyswamp
  5053.     if i.status==PBStatuses::SLEEP && !isConst?(i.ability,PBAbilities,:MAGICGUARD)
  5054.       if $fefieldeffect == 8 # Swamp Field
  5055.         hploss=i.pbReduceHP((i.totalhp/16).floor,true)
  5056.         pbDisplay(_INTL("{1}'s strength is sapped by the swamp!",i.pbThis)) if hploss>0
  5057.       end
  5058.     end
  5059.     if i.hp<=0
  5060.       return if !i.pbFaint
  5061.       next
  5062.     end
  5063.     #sleepyrainbow
  5064.     if i.status==PBStatuses::SLEEP
  5065.       if $fefieldeffect == 9 && i.effects[PBEffects::HealBlock]==0#Rainbow Field
  5066.       hpgain=(i.totalhp/16).floor
  5067.       hpgain=(hpgain*1.3).floor if isConst?(i.item,PBItems,:BIGROOT)
  5068.       hpgain=i.pbRecoverHP(hpgain,true)
  5069.       pbDisplay(_INTL("{1} recovered health in its peaceful sleep!",i.pbThis))
  5070.       end
  5071.     end
  5072.     #sleepycorro
  5073.     if i.status==PBStatuses::SLEEP && i.hasWorkingAbility(:MAGICGUARD) &&
  5074.      !i.hasWorkingAbility(:POISONHEAL) && !i.hasWorkingAbility(:TOXICBOOST) &&
  5075.      !i.hasWorkingAbility(:WONDERGUARD) &&
  5076.      !i.pbHasType?(:STEEL) && !i.pbHasType?(:POISON) &&
  5077.      $fefieldeffect == 10 # Corrosive Field
  5078.       hploss=i.pbReduceHP((i.totalhp/16).floor,true)
  5079.       pbDisplay(_INTL("{1}'s is seared by the corrosion!",i.pbThis)) if hploss>0
  5080.     end
  5081.     if i.hp<=0
  5082.       return if !i.pbFaint
  5083.       next
  5084.     end
  5085.     # Bad Dreams
  5086.     if i.status==PBStatuses::SLEEP && !isConst?(i.ability,PBAbilities,:MAGICGUARD) &&
  5087.       $fefieldeffect != 9
  5088.       if i.pbOpposing1.hasWorkingAbility(:BADDREAMS) ||
  5089.          i.pbOpposing2.hasWorkingAbility(:BADDREAMS)
  5090.         hploss=i.pbReduceHP((i.totalhp/8).floor,true)
  5091.         pbDisplay(_INTL("{1} is having a bad dream!",i.pbThis)) if hploss>0
  5092.       end
  5093.     end
  5094.     if i.isFainted?
  5095.       return if !i.pbFaint
  5096.       next
  5097.     end
  5098.     # Harvest
  5099.     if i.hasWorkingAbility(:HARVEST) && i.item<=0 && i.pokemon.itemRecycle>0 #if an item was recycled, check
  5100.       if pbIsBerry?(i.pokemon.itemRecycle) && (rand(100)>50 ||
  5101.        pbWeather==PBWeather::SUNNYDAY || ($fefieldeffect == 33 && $fecounter>0))
  5102.         i.item=i.pokemon.itemRecycle
  5103.         i.pokemon.itemInitial=i.pokemon.itemRecycle
  5104.         i.pokemon.itemRecycle=0
  5105.         firstberryletter=PBItems.getName(i.item).split(//).first
  5106.         if firstberryletter=="A" || firstberryletter=="E" || firstberryletter=="I" ||
  5107.           firstberryletter=="O" || firstberryletter=="U"
  5108.               pbDisplay(_INTL("{1} harvested an {2}!",i.pbThis,PBItems.getName(i.item)))
  5109.         else      
  5110.           pbDisplay(_INTL("{1} harvested a {2}!",i.pbThis,PBItems.getName(i.item)))
  5111.         end
  5112.         i.pbBerryCureCheck(true)
  5113.       end
  5114.     end
  5115.     # Moody
  5116.     if i.hasWorkingAbility(:CLOUDNINE) && $fefieldeffect == 9
  5117.       failsafe=0
  5118.       randoms=[]
  5119.       loop do
  5120.         failsafe+=1
  5121.         break if failsafe==1000        
  5122.         rand=1+self.pbRandom(7)
  5123.         if !i.pbTooHigh?(rand)
  5124.           randoms.push(rand)
  5125.           break
  5126.         end
  5127.       end
  5128.       statnames=[_INTL("Attack"),_INTL("Defense"),_INTL("Speed"),_INTL("Special Attack"),
  5129.                  _INTL("Special Defense"),_INTL("accuracy"),_INTL("evasiveness")]
  5130.       if failsafe!=1000          
  5131.        i.stages[randoms[0]]+=1
  5132.        i.stages[randoms[0]]=6 if i.stages[randoms[0]]>6
  5133.        pbCommonAnimation("StatUp",i,nil)
  5134.        pbDisplay(_INTL("{1}'s Cloud Nine raised its {2}!",i.pbThis,statnames[randoms[0]-1]))
  5135.      end    
  5136.     end
  5137.     if i.hasWorkingAbility(:MOODY)
  5138.       randomup=[]
  5139.       randomdown=[]
  5140.       failsafe1=0
  5141.       failsafe2=0
  5142.       loop do
  5143.         failsafe1+=1
  5144.         break if failsafe1==1000
  5145.         rand=1+self.pbRandom(7)
  5146.         if !i.pbTooHigh?(rand)
  5147.           randomup.push(rand)
  5148.           break
  5149.         end
  5150.       end
  5151.       loop do
  5152.         failsafe2+=1
  5153.         break if failsafe2==1000
  5154.         rand=1+self.pbRandom(7)
  5155.         if !i.pbTooLow?(rand) && rand!=randomup[0]
  5156.           randomdown.push(rand)
  5157.           break
  5158.         end
  5159.       end
  5160.       statnames=[_INTL("Attack"),_INTL("Defense"),_INTL("Speed"),_INTL("Special Attack"),
  5161.                  _INTL("Special Defense"),_INTL("accuracy"),_INTL("evasiveness")]
  5162.       if failsafe1!=1000                
  5163.        i.stages[randomup[0]]+=2
  5164.        i.stages[randomup[0]]=6 if i.stages[randomup[0]]>6
  5165.        pbCommonAnimation("StatUp",i,nil)
  5166.        pbDisplay(_INTL("{1}'s Moody sharply raised its {2}!",i.pbThis,statnames[randomup[0]-1]))
  5167.      end
  5168.      if failsafe2!=1000
  5169.        i.stages[randomdown[0]]-=1
  5170.        pbCommonAnimation("StatDown",i,nil)
  5171.        pbDisplay(_INTL("{1}'s Moody lowered its {2}!",i.pbThis,statnames[randomdown[0]-1]))
  5172.      end    
  5173.     end
  5174.   end
  5175.     for i in priority
  5176.       next if i.isFainted?
  5177.       # Toxic Orb
  5178.       if i.hasWorkingItem(:TOXICORB) && i.status==0 && i.pbCanPoison?(false)
  5179.         i.status=PBStatuses::POISON
  5180.         i.statusCount=1
  5181.         i.effects[PBEffects::Toxic]=0
  5182.         pbCommonAnimation("Poison",i,nil)
  5183.         pbDisplay(_INTL("{1} was poisoned by its {2}!",i.pbThis,PBItems.getName(i.item)))
  5184.       end
  5185.       # Flame Orb
  5186.       if i.hasWorkingItem(:FLAMEORB) && i.status==0 && i.pbCanBurn?(false)
  5187.         i.status=PBStatuses::BURN
  5188.         i.statusCount=0
  5189.         pbCommonAnimation("Burn",i,nil)
  5190.         pbDisplay(_INTL("{1} was burned by its {2}!",i.pbThis,PBItems.getName(i.item)))
  5191.       end
  5192.       # Sticky Barb
  5193.       if i.hasWorkingItem(:STICKYBARB) && !i.hasWorkingAbility(:MAGICGUARD)
  5194.         pbDisplay(_INTL("{1} is hurt by its {2}!",i.pbThis,PBItems.getName(i.item)))
  5195.         @scene.pbDamageAnimation(i,0)
  5196.         i.pbReduceHP((i.totalhp/8).floor)
  5197.       end
  5198.       if i.isFainted?
  5199.         return if !i.pbFaint
  5200.         next
  5201.       end
  5202.     end
  5203.     # Form checks
  5204.     for i in 0...4
  5205.       next if @battlers[i].isFainted?
  5206.       @battlers[i].pbCheckForm
  5207.     end
  5208.     pbGainEXP
  5209. ##### KUROTSUNE - 009 - START
  5210.     # Checks if a pokemon on either side has fainted on this turn
  5211.     # for retaliate
  5212.     player   = priority[0]
  5213.     opponent = priority[1]
  5214.     if player.isFainted? ||
  5215.       (@doublebattle && player.pbPartner.isFainted?)
  5216.       player.pbOwnSide.effects[PBEffects::Retaliate] = true
  5217.     else
  5218.       # No pokemon has fainted in this side this turn
  5219.       player.pbOwnSide.effects[PBEffects::Retaliate] = false
  5220.     end
  5221.    
  5222.     if opponent.isFainted? ||
  5223.       (@doublebattle && player.pbPartner.isFainted?)
  5224.       opponent.pbOwnSide.effects[PBEffects::Retaliate] = true
  5225.     else
  5226.       opponent.pbOwnSide.effects[PBEffects::Retaliate] = false
  5227.     end
  5228. ##### KUROTSUNE - 009 - END
  5229.     @faintswitch = true
  5230.     pbSwitch
  5231.     @faintswitch = false
  5232.     return if @decision>0
  5233.     for i in priority
  5234.       next if i.isFainted?
  5235.       i.pbAbilitiesOnSwitchIn(false)
  5236.     end
  5237.     # Healing Wish/Lunar Dance - should go here
  5238.     # Spikes/Toxic Spikes/Stealth Rock - should go here (in order of their 1st use)
  5239.     for i in 0...4
  5240.       if @battlers[i].turncount>0 && @battlers[i].hasWorkingAbility(:TRUANT)
  5241.         @battlers[i].effects[PBEffects::Truant]=!@battlers[i].effects[PBEffects::Truant]
  5242.       end
  5243.       if @battlers[i].effects[PBEffects::LockOn]>0   # Also Mind Reader
  5244.         @battlers[i].effects[PBEffects::LockOn]-=1
  5245.         @battlers[i].effects[PBEffects::LockOnPos]=-1 if @battlers[i].effects[PBEffects::LockOn]==0
  5246.       end
  5247.       @battlers[i].effects[PBEffects::Flinch]=false
  5248.       @battlers[i].effects[PBEffects::FollowMe]=false
  5249.       @battlers[i].effects[PBEffects::HelpingHand]=false
  5250.       @battlers[i].effects[PBEffects::MagicCoat]=false
  5251.       @battlers[i].effects[PBEffects::Snatch]=false
  5252. #### KUROTSUNE - 024 - START
  5253.       @battlers[i].effects[PBEffects::Electrify]=false
  5254. #### KUROTSUNE - 024 - END
  5255.       @battlers[i].effects[PBEffects::Charge]-=1 if @battlers[i].effects[PBEffects::Charge]>0
  5256.       @battlers[i].lastHPLost=0
  5257.       @battlers[i].lastAttacker=-1
  5258.       @battlers[i].effects[PBEffects::Counter]=-1
  5259.       @battlers[i].effects[PBEffects::CounterTarget]=-1
  5260.       @battlers[i].effects[PBEffects::MirrorCoat]=-1
  5261.       @battlers[i].effects[PBEffects::MirrorCoatTarget]=-1
  5262.     end
  5263.     # invalidate stored priority
  5264.     @usepriority=false
  5265.   end
  5266.  
  5267. ################################################################################
  5268. # End of battle.
  5269. ################################################################################
  5270.   def pbEndOfBattle(canlose=false)
  5271.     case @decision
  5272.     ##### WIN #####
  5273.       when 1
  5274.         if @opponent
  5275.           @scene.pbTrainerBattleSuccess
  5276.           if @opponent.is_a?(Array)
  5277.             pbDisplayPaused(_INTL("{1} defeated {2} and {3}!",self.pbPlayer.name,@opponent[0].fullname,@opponent[1].fullname))
  5278.           else
  5279.             pbDisplayPaused(_INTL("{1} defeated\r\n{2}!",self.pbPlayer.name,@opponent.fullname))
  5280.           end
  5281.           @scene.pbShowOpponent(0)
  5282.           pbDisplayPaused(@endspeech.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  5283.           if @opponent.is_a?(Array)
  5284.             @scene.pbHideOpponent
  5285.             @scene.pbShowOpponent(1)
  5286.             pbDisplayPaused(@endspeech2.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  5287.           end
  5288.           # Calculate money gained for winning
  5289.         if @internalbattle
  5290.           tmoney=0
  5291.           if @opponent.is_a?(Array)   # Double battles
  5292.             maxlevel1=0; maxlevel2=0; limit=pbSecondPartyBegin(1)
  5293.             for i in 0...limit
  5294.               if @party2[i]
  5295.                 maxlevel1=@party2[i].level if maxlevel1<@party2[i].level
  5296.               end
  5297.               if @party2[i+limit]
  5298.                 maxlevel2=@party2[i+limit].level if maxlevel1<@party2[i+limit].level
  5299.               end
  5300.             end
  5301.             tmoney+=maxlevel1*@opponent[0].moneyEarned
  5302.             tmoney+=maxlevel2*@opponent[1].moneyEarned
  5303.           else
  5304.             maxlevel=0
  5305.             for i in @party2
  5306.               next if !i
  5307.               maxlevel=i.level if maxlevel<i.level
  5308.             end
  5309.             tmoney+=maxlevel*@opponent.moneyEarned
  5310.           end
  5311.           # If Amulet Coin/Luck Incense's effect applies, double money earned
  5312.           badgemultiplier = [1,self.pbPlayer.numbadges].max
  5313.           tmoney*=badgemultiplier
  5314.           tmoney*=2 if @amuletcoin
  5315.           oldmoney=self.pbPlayer.money
  5316.           self.pbPlayer.money+=tmoney
  5317.           moneygained=self.pbPlayer.money-oldmoney
  5318.           if moneygained>0
  5319.             pbDisplayPaused(_INTL("{1} got ${2}\r\nfor winning!",self.pbPlayer.name,tmoney))
  5320.           end
  5321.         end
  5322.       end
  5323.       if @internalbattle && @extramoney>0
  5324.         @extramoney*=2 if @amuletcoin
  5325.         oldmoney=self.pbPlayer.money
  5326.         self.pbPlayer.money+=@extramoney
  5327.         moneygained=self.pbPlayer.money-oldmoney
  5328.         if moneygained>0
  5329.           pbDisplayPaused(_INTL("{1} picked up ${2}!",self.pbPlayer.name,@extramoney))
  5330.         end
  5331.       end
  5332.         for pkmn in @snaggedpokemon
  5333.           pbStorePokemon(pkmn)
  5334.           self.pbPlayer.shadowcaught=[] if !self.pbPlayer.shadowcaught
  5335.           self.pbPlayer.shadowcaught[pkmn.species]=true
  5336.         end
  5337.         @snaggedpokemon.clear
  5338.     ##### LOSE, DRAW #####
  5339.       when 2, 5
  5340.         if @internalbattle
  5341.           pbDisplayPaused(_INTL("{1} is out of usable Pokémon!",self.pbPlayer.name))
  5342.           moneylost=pbMaxLevelFromIndex(0)
  5343.           multiplier=[8,16,24,36,48,64,80,100,120,144,178,206,234,266,298,334,370,410,450,500] #Badge no. multiplier for money lost
  5344.           moneylost*=multiplier[[multiplier.length-1,self.pbPlayer.numbadges].min]
  5345.           moneylost=self.pbPlayer.money if moneylost>self.pbPlayer.money
  5346.           moneylost=0 if $game_switches[NO_MONEY_LOSS]
  5347.           self.pbPlayer.money-=moneylost
  5348.           if @opponent
  5349.             if @opponent.is_a?(Array)
  5350.               pbDisplayPaused(_INTL("{1} lost against {2} and {3}!",self.pbPlayer.name,@opponent[0].fullname,@opponent[1].fullname))
  5351.             else
  5352.               pbDisplayPaused(_INTL("{1} lost against\r\n{2}!",self.pbPlayer.name,@opponent.fullname))
  5353.             end
  5354.             if moneylost>0
  5355.               pbDisplayPaused(_INTL("{1} paid ${2}\r\nas the prize money...",self.pbPlayer.name,moneylost))  
  5356.               pbDisplayPaused(_INTL("...")) if !canlose
  5357.             end
  5358.           else
  5359.             if moneylost>0
  5360.               pbDisplayPaused(_INTL("{1} panicked and lost\r\n${2}...",self.pbPlayer.name,moneylost))
  5361.               pbDisplayPaused(_INTL("...")) if !canlose
  5362.             end
  5363.           end
  5364.           pbDisplayPaused(_INTL("{1} blacked out!",self.pbPlayer.name)) if !canlose
  5365.         elsif @decision==2
  5366.           @scene.pbShowOpponent(0)
  5367.           pbDisplayPaused(@endspeechwin.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  5368.           if @opponent.is_a?(Array)
  5369.             @scene.pbHideOpponent
  5370.             @scene.pbShowOpponent(1)
  5371.             pbDisplayPaused(@endspeechwin2.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  5372.           end
  5373.         elsif @decision==5
  5374.           PBDebug.log("***[Draw game]") if $INTERNAL
  5375.         end
  5376.     end
  5377.     # Pass on Pokérus within the party
  5378.     infected=[]
  5379.     for i in 0...$Trainer.party.length
  5380.       if $Trainer.party[i].pokerusStage==1
  5381.         infected.push(i)
  5382.       end
  5383.     end
  5384.     if infected.length>=1
  5385.       for i in infected
  5386.         strain=$Trainer.party[i].pokerus/16
  5387.         if i>0 && $Trainer.party[i-1].pokerusStage==0
  5388.           $Trainer.party[i-1].givePokerus(strain) if rand(3)==0
  5389.         end
  5390.         if i<$Trainer.party.length-1 && $Trainer.party[i+1].pokerusStage==0
  5391.           $Trainer.party[i+1].givePokerus(strain) if rand(3)==0
  5392.         end
  5393.       end
  5394.     end
  5395.     @scene.pbEndBattle(@decision)
  5396.     for i in @battlers
  5397.       i.pbResetForm
  5398.     end
  5399.     for i in $Trainer.party
  5400.       i.setItem(i.itemInitial)
  5401.       i.itemInitial=i.itemRecycle=0
  5402.     end
  5403.     return @decision
  5404.   end
  5405. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement