def pbGainEXP

return if $game_switches[NPCBATTLE]

return if !@internalbattle

successbegin=true

for i in 0...4 # Not ordered by priority

if !@doublebattle && pbIsDoubleBattler?(i)

@battlers[i].participants=[]

next

end

if pbIsOpposing?(i) && @battlers[i].participants.length>0 &&

(@battlers[i].fainted? || @battlers[i].captured)

haveexpall=(hasConst?(PBItems,:EXPALL) && $PokemonBag.pbHasItem?(:EXPALL))

# First count the number of participants

partic=0

expshare=0

for j in @battlers[i].participants

next if !@party1[j] || !pbIsOwner?(0,j)

partic+=1 if @party1[j].hp>0 && !@party1[j].egg?

end

if !haveexpall

for j in 0...@party1.length

next if !@party1[j] || !pbIsOwner?(0,j)

expshare+=1 if @party1[j].hp>0 && !@party1[j].egg? &&

(isConst?(@party1[j].item,PBItems,:EXPSHARE) ||

isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE))

end

end

# Now calculate EXP for the participants

if partic>0 || expshare>0 || haveexpall

if !@opponent && successbegin && pbAllFainted?(@party2)

@scene.pbWildBattleSuccess

successbegin=false

end

for j in 0...@party1.length

next if !@party1[j] || !pbIsOwner?(0,j)

next if @party1[j].hp<=0 || @party1[j].egg?

haveexpshare=(isConst?(@party1[j].item,PBItems,:EXPSHARE) ||

isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE))

next if !haveexpshare && !@battlers[i].participants.include?(j)

pbGainExpOne(j,@battlers[i],partic,expshare,haveexpall)

end

if haveexpall

showmessage=true

for j in 0...@party1.length

next if !@party1[j] || !pbIsOwner?(0,j)

next if @party1[j].hp<=0 || @party1[j].egg?

next if isConst?(@party1[j].item,PBItems,:EXPSHARE) ||

isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE)

next if @battlers[i].participants.include?(j)

pbDisplayPaused(_INTL("The rest of your team gained Exp. Points thanks to the {1}!",

PBItems.getName(getConst(PBItems,:EXPALL)))) if showmessage

showmessage=false

pbGainExpOne(j,@battlers[i],partic,expshare,haveexpall,false)

end

end

end

# Now clear the participants array

@battlers[i].participants=[]

end

end

end

def pbGainExpOne(index,defeated,partic,expshare,haveexpall,showmessages=true)

thispoke=@party1[index]

# Original species, not current species

level=defeated.level

baseexp=defeated.pokemon.baseExp

evyield=defeated.pokemon.evYield

# Gain effort value points, using RS effort values

totalev=0

for k in 0...6

totalev+=thispoke.ev[k]

end

for k in 0...6

evgain=evyield[k]

evgain*=2 if isConst?(thispoke.item,PBItems,:MACHOBRACE) ||

isConst?(thispoke.itemInitial,PBItems,:MACHOBRACE)

case k

when PBStats::HP

evgain+=4 if isConst?(thispoke.item,PBItems,:POWERWEIGHT) ||

isConst?(thispoke.itemInitial,PBItems,:POWERWEIGHT)

when PBStats::ATTACK

evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBRACER) ||

isConst?(thispoke.itemInitial,PBItems,:POWERBRACER)

when PBStats::DEFENSE

evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBELT) ||

isConst?(thispoke.itemInitial,PBItems,:POWERBELT)

when PBStats:

PATK

evgain+=4 if isConst?(thispoke.item,PBItems,:POWERLENS) ||

isConst?(thispoke.itemInitial,PBItems,:POWERLENS)

when PBStats:

PDEF

evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBAND) ||

isConst?(thispoke.itemInitial,PBItems,:POWERBAND)

when PBStats:

PEED

evgain+=4 if isConst?(thispoke.item,PBItems,:POWERANKLET) ||

isConst?(thispoke.itemInitial,PBItems,:POWERANKLET)

end

evgain*=2 if thispoke.pokerusStage>=1 # Infected or cured

if evgain>0

# Can't exceed overall limit

evgain-=totalev+evgain-PokeBattle_Pokemon::EVLIMIT if totalev+evgain>PokeBattle_Pokemon::EVLIMIT

# Can't exceed stat limit

evgain-=thispoke.ev[k]+evgain-PokeBattle_Pokemon::EVSTATLIMIT if thispoke.ev[k]+evgain>PokeBattle_Pokemon::EVSTATLIMIT

# Add EV gain

thispoke.ev[k]+=evgain

if thispoke.ev[k]>PokeBattle_Pokemon::EVSTATLIMIT

print "Single-stat EV limit #{PokeBattle_Pokemon::EVSTATLIMIT} exceeded.\r\nStat: #{k} EV gain: #{evgain} EVs: #{thispoke.ev.inspect}"

thispoke.ev[k]=PokeBattle_Pokemon::EVSTATLIMIT

end

totalev+=evgain

if totalev>PokeBattle_Pokemon::EVLIMIT

print "EV limit #{PokeBattle_Pokemon::EVLIMIT} exceeded.\r\nTotal EVs: #{totalev} EV gain: #{evgain} EVs: #{thispoke.ev.inspect}"

end

end

end

# Gain experience

ispartic=0

ispartic=1 if defeated.participants.include?(index)

haveexpshare=(isConst?(thispoke.item,PBItems,:EXPSHARE) ||

isConst?(thispoke.itemInitial,PBItems,:EXPSHARE)) ? 1 : 0

exp=0

if expshare>0

if partic==0 # No participants, all Exp goes to Exp Share holders

exp=(level*baseexp).floor

exp=(exp/(NOSPLITEXP ? 1 : expshare)).floor*haveexpshare

else

if NOSPLITEXP

exp=(level*baseexp).floor*ispartic

exp=(level*baseexp/2).floor*haveexpshare if ispartic==0

else

exp=(level*baseexp/2).floor

exp=(exp/partic).floor*ispartic + (exp/expshare).floor*haveexpshare

end

end

elsif ispartic==1

exp=(level*baseexp/(NOSPLITEXP ? 1 : partic)).floor

elsif haveexpall

exp=(level*baseexp/2).floor

end

return if exp<=0

exp=(exp*3/2).floor if @opponent

if USESCALEDEXPFORMULA

exp=(exp/5).floor

leveladjust=(2*level+10.0)/(level+thispoke.level+10.0)

leveladjust=leveladjust**5

leveladjust=Math.sqrt(leveladjust)

exp=(exp*leveladjust).floor

exp+=1 if ispartic>0 || haveexpshare>0

else

exp=(exp/7).floor

end

isOutsider=(thispoke.trainerID!=self.pbPlayer.id ||

(thispoke.language!=0 && thispoke.language!=self.pbPlayer.language))

if isOutsider

if thispoke.language!=0 && thispoke.language!=self.pbPlayer.language

exp=(exp*1.7).floor

else

exp=(exp*3/2).floor

end

end

exp=(exp*3/2).floor if isConst?(thispoke.item,PBItems,:LUCKYEGG) ||

isConst?(thispoke.itemInitial,PBItems,:LUCKYEGG)

growthrate=thispoke.growthrate

newexp=PBExperience.pbAddExperience(thispoke.exp,exp,growthrate)

exp=newexp-thispoke.exp

if exp>0

if showmessages

if isOutsider

pbDisplayPaused(_INTL("{1} gained a boosted {2} Exp. Points!",thispoke.name,exp))

else

pbDisplayPaused(_INTL("{1} gained {2} Exp. Points!",thispoke.name,exp))

end

end

newlevel=PBExperience.pbGetLevelFromExperience(newexp,growthrate)

tempexp=0

curlevel=thispoke.level

if newlevel<curlevel

debuginfo="#{thispoke.name}: #{thispoke.level}/#{newlevel} | #{thispoke.exp}/#{newexp} | gain: #{exp}"

raise RuntimeError.new(_INTL("The new level ({1}) is less than the Pokémon's\r\ncurrent level ({2}), which shouldn't happen.\r\n[Debug: {3}]",

newlevel,curlevel,debuginfo))

return

end

if thispoke.respond_to?("isShadow?") && thispoke.isShadow?

thispoke.exp+=exp

else

tempexp1=thispoke.exp

tempexp2=0

# Find battler

battler=pbFindPlayerBattler(index)

loop do

# EXP Bar animation

startexp=PBExperience.pbGetStartExperience(curlevel,growthrate)

endexp=PBExperience.pbGetStartExperience(curlevel+1,growthrate)

tempexp2=(endexp<newexp) ? endexp : newexp

thispoke.exp=tempexp2

@scene.pbEXPBar(thispoke,battler,startexp,endexp,tempexp1,tempexp2)

tempexp1=tempexp2

curlevel+=1

if curlevel>newlevel

thispoke.calcStats

battler.pbUpdate(false) if battler

@scene.pbRefresh

break

end

oldtotalhp=thispoke.totalhp

oldattack=thispoke.attack

olddefense=thispoke.defense

oldspeed=thispoke.speed

oldspatk=thispoke.spatk

oldspdef=thispoke.spdef

if battler && battler.pokemon && @internalbattle

battler.pokemon.changeHappiness("levelup")

end

thispoke.calcStats

battler.pbUpdate(false) if battler

@scene.pbRefresh

pbDisplayPaused(_INTL("{1} grew to Level {2}!",thispoke.name,curlevel))

@scene.pbLevelUp(thispoke,battler,oldtotalhp,oldattack,

olddefense,oldspeed,oldspatk,oldspdef)

# Finding all moves learned at this level

movelist=thispoke.getMoveList

for k in movelist

if k[0]==thispoke.level # Learned a new move

pbLearnMove(index,k[1])

end

end

end

end

end

end