Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.27 KB | None | 0 0
  1.   def pbGainExp
  2.     return if !@internalBattle
  3.     expAll = (hasConst?(PBItems,:EXPALL) && $PokemonBag.pbHasItem?(:EXPALL))
  4.     successBegin = true
  5.     p1 = pbParty(0)
  6.     @battlers.each do |b|
  7.       next unless b && opposes?(b)   # Can only gain Exp from fainted foes
  8.       next if b.participants.length==0
  9.       next unless b.fainted? || b.captured
  10.       # Count the number of participants
  11.       numPartic = 0
  12.       b.participants.each do |partic|
  13.         next unless p1[partic] && p1[partic].able? && pbIsOwner?(0,partic)
  14.         numPartic += 1
  15.       end
  16.       # Find which Pokémon have an Exp Share
  17.       expShare = []
  18.       if !expAll
  19.         eachInTeam(0,0) do |pkmn,i|
  20.           next if !pkmn.able?
  21.           next if !isConst?(pkmn.item,PBItems,:EXPSHARE) &&
  22.                   !isConst?(@initialItems[0][i],PBItems,:EXPSHARE)
  23.           expShare.push(i)
  24.         end
  25.       end
  26.       # Calculate EV and Exp gains for the participants
  27.       if numPartic>0 || expShare.length>0 || expAll
  28.         # Play wild victory music if it's the end of the battle
  29.         # TODO: Shouldn't this go somewhere else?
  30.         if wildBattle? && successBegin && pbAllFainted?(pbParty(1))
  31.           @scene.pbWildBattleSuccess
  32.           successBegin = false
  33.         end
  34.         # Gain EVs and Exp for participants
  35.         eachInTeam(0,0) do |pkmn,i|
  36.           next if !pkmn.able?
  37.           next unless b.participants.include?(i) || expShare.include?(i)
  38.           pbGainEVsOne(i,b)
  39.           pbGainExpOne(i,b,numPartic,expShare,expAll)
  40.         end
  41.         # Gain EVs and Exp for all other Pokémon because of Exp All
  42.         if expAll
  43.           showMessage = true
  44.           eachInTeam(0,0) do |pkmn,i|
  45.             next if !pkmn.able?
  46.             next if b.participants.include?(i) || expShare.include?(i)
  47.             pbDisplayPaused(_INTL("Your party Pokémon in waiting also got Exp. Points!")) if showMessage
  48.             showMessage = false
  49.             pbGainEVsOne(i,b)
  50.             pbGainExpOne(i,b,numPartic,expShare,expAll,false)
  51.           end
  52.         end
  53.       end
  54.       # Clear the participants array
  55.       b.participants = []
  56.     end
  57.   end
  58.  
  59.   def pbGainEVsOne(idxParty,defeatedBattler)
  60.     pkmn = pbParty(0)[idxParty]   # The Pokémon gaining EVs from defeatedBattler
  61.     evYield = defeatedBattler.pokemon.evYield
  62.     # Num of effort points pkmn already has
  63.     evTotal = 0
  64.     PBStats.eachStat { |s| evTotal += pkmn.ev[s] }
  65.     # Modify EV yield based on pkmn's held item
  66.     if !BattleHandlers.triggerEVGainModifierItem(pkmn.item,pkmn,evYield)
  67.       BattleHandlers.triggerEVGainModifierItem(@initialItems[0][idxParty],pkmn,evYield)
  68.     end
  69.     # Double EV gain because of Pokérus
  70.     if pkmn.pokerusStage>=1   # Infected or cured
  71.       evYield.collect! { |a| a*2 }
  72.     end
  73.     # Gain EVs for each stat in turn
  74.     PBStats.eachStat do |s|
  75.       evGain = evYield[s]
  76.       # Can't exceed overall limit
  77.       if evTotal+evGain>PokeBattle_Pokemon::EV_LIMIT
  78.         evGain = PokeBattle_Pokemon::EV_LIMIT-evTotal
  79.       end
  80.       # Can't exceed individual stat limit
  81.       if pkmn.ev[s]+evGain>PokeBattle_Pokemon::EV_STAT_LIMIT
  82.         evGain = PokeBattle_Pokemon::EV_STAT_LIMIT-pkmn.ev[s]
  83.       end
  84.       # Add EV gain
  85.       pkmn.ev[s] += evGain
  86.       evTotal += evGain
  87.     end
  88.   end
  89.  
  90.   def pbGainExpOne(idxParty,defeatedBattler,numPartic,expShare,expAll,showMessages=true)
  91.     pkmn = pbParty(0)[idxParty]   # The Pokémon gaining EVs from defeatedBattler
  92.     growthRate = pkmn.growthrate
  93.     # Don't bother calculating if gainer is already at max Exp
  94.     return if pkmn.exp>=PBExperience.pbGetMaxExperience(growthRate)
  95.     isPartic    = defeatedBattler.participants.include?(idxParty)
  96.     hasExpShare = expShare.include?(idxParty)
  97.     level = defeatedBattler.level
  98.     # Main Exp calculation
  99.     exp = 0
  100.     a = level*defeatedBattler.pokemon.baseExp
  101.     if expShare.length>0 && (isPartic || hasExpShare)
  102.       if numPartic==0   # No participants, all Exp goes to Exp Share holders
  103.         exp = a/(SPLIT_EXP_BETWEEN_GAINERS ? expShare.length : 1)
  104.       elsif SPLIT_EXP_BETWEEN_GAINERS   # Gain from participating and/or Exp Share
  105.         exp = a/(2*numPartic) if isPartic
  106.         exp += a/(2*expShare.length) if hasExpShare
  107.       else   # Gain from participating and/or Exp Share (Exp not split)
  108.         exp = (isPartic) ? a : a/2
  109.       end
  110.     elsif isPartic   # Participated in battle, no Exp Shares held by anyone
  111.       exp = a/(SPLIT_EXP_BETWEEN_GAINERS ? numPartic : 1)
  112.     elsif expAll   # Didn't participate in battle, gaining Exp due to Exp All
  113.       # NOTE: Exp All works like the Exp Share from Gen 6+, not like the Exp All
  114.       #       from Gen 1, i.e. Exp isn't split between all Pokémon gaining it.
  115.       exp = a/2
  116.     end
  117.     return if exp<=0
  118.     # Pokémon gain more Exp from trainer battles
  119.     exp = (exp*1.5).floor if trainerBattle?
  120.     # Scale the gained Exp based on the gainer's level (or not)
  121.     if SCALED_EXP_FORMULA
  122.       exp /= 5
  123.       levelAdjust = (2*level+10.0)/(pkmn.level+level+10.0)
  124.       levelAdjust = levelAdjust**5
  125.       levelAdjust = Math.sqrt(levelAdjust)
  126.       exp *= levelAdjust
  127.       exp = exp.floor
  128.       exp += 1 if isPartic || hasExpShare
  129.     else
  130.       exp /= 7
  131.     end
  132.     # Foreign Pokémon gain more Exp
  133.     isOutsider = (pkmn.trainerID!=pbPlayer.id ||
  134.                  (pkmn.language!=0 && pkmn.language!=pbPlayer.language))
  135.     if isOutsider
  136.       if pkmn.language!=0 && pkmn.language!=pbPlayer.language
  137.         exp = (exp*1.7).floor
  138.       else
  139.         exp = (exp*1.5).floor
  140.       end
  141.     end
  142.     # Modify Exp gain based on pkmn's held item
  143.     i = BattleHandlers.triggerExpGainModifierItem(pkmn.item,pkmn,exp)
  144.     if i<0
  145.       i = BattleHandlers.triggerExpGainModifierItem(@initialItems[0][idxParty],pkmn,exp)
  146.     end
  147.     exp = i if i>=0
  148.     # Make sure Exp doesn't exceed the maximum
  149.     expFinal = PBExperience.pbAddExperience(pkmn.exp,exp,growthRate)
  150.     expGained = expFinal-pkmn.exp
  151.     return if expGained<=0
  152.     # "Exp gained" message
  153.     if showMessages
  154.       if isOutsider
  155.         pbDisplayPaused(_INTL("{1} got a boosted {2} Exp. Points!",pkmn.name,expGained))
  156.       else
  157.         pbDisplayPaused(_INTL("{1} got {2} Exp. Points!",pkmn.name,expGained))
  158.       end
  159.     end
  160.     curLevel = pkmn.level
  161.     newLevel = PBExperience.pbGetLevelFromExperience(expFinal,growthRate)
  162.     if newLevel<curLevel
  163.       debugInfo = "Levels: #{curLevel}->#{newLevel} | Exp: #{pkmn.exp}->#{expFinal} | gain: #{expGained}"
  164.       raise RuntimeError.new(
  165.          _INTL("{1}'s new level is less than its\r\ncurrent level, which shouldn't happen.\r\n[Debug: {2}]",
  166.          pkmn.name,debugInfo))
  167.       return
  168.     end
  169.     # Give Exp
  170.     if pkmn.shadowPokemon?
  171.       pkmn.exp += expGained
  172.       return
  173.     end
  174.     tempExp1 = pkmn.exp
  175.     battler = pbFindBattler(idxParty)
  176.     loop do   # For each level gained in turn...
  177.       # EXP Bar animation
  178.       levelMinExp = PBExperience.pbGetStartExperience(curLevel,growthRate)
  179.       levelMaxExp = PBExperience.pbGetStartExperience(curLevel+1,growthRate)
  180.       tempExp2 = (levelMaxExp<expFinal) ? levelMaxExp : expFinal
  181.       pkmn.exp = tempExp2
  182.       @scene.pbEXPBar(battler,levelMinExp,levelMaxExp,tempExp1,tempExp2)
  183.       tempExp1 = tempExp2
  184.       curLevel += 1
  185.       if curLevel>newLevel
  186.         # Gained all the Exp now, end the animation
  187.         pkmn.calcStats
  188.         battler.pbUpdate(false) if battler
  189.         @scene.pbRefresh
  190.         break
  191.       end
  192.       # Levelled up
  193.       # TODO: There should be a Common animation for levelling up here.
  194.       oldTotalHP = pkmn.totalhp
  195.       oldAttack  = pkmn.attack
  196.       oldDefense = pkmn.defense
  197.       oldSpAtk   = pkmn.spatk
  198.       oldSpDef   = pkmn.spdef
  199.       oldSpeed   = pkmn.speed
  200.       if battler && battler.pokemon
  201.         battler.pokemon.changeHappiness("levelup")
  202.       end
  203.       pkmn.calcStats
  204.       battler.pbUpdate(false) if battler
  205.       @scene.pbRefresh
  206.       pbDisplayPaused(_INTL("{1} grew to Lv. {2}!",pkmn.name,curLevel))
  207.       @scene.pbLevelUp(pkmn,battler,oldTotalHP,oldAttack,oldDefense,
  208.                                     oldSpAtk,oldSpDef,oldSpeed)
  209.       # Learn all moves learned at this level
  210.       moveList = pkmn.getMoveList
  211.       moveList.each { |m| pbLearnMove(idxParty,m[1]) if m[0]==curLevel }
  212.     end
  213.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement