• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Our friends from the Johto Times are hosting a favorite Pokémon poll - and we'd love for you to participate! Click here for information on how to vote for your favorites!
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

pbBalancedLevel not working right?

  • 217
    Posts
    15
    Years
    • Seen Nov 29, 2021
    As far as I was aware, it's supposed to generate an average of your party levels.
    Yet when I have a party of a level 5, a level 15, and a level 95 Pokemon, it generates a number of 97.
    Likewise, with a party of a level 5 and a level 15, it generates a number of 15.
    With just a level 5, it generates a number of 7.

    Clearly something is off.


    To clarify, I'm using it in the following context:
    Code:
    Events.onWildPokemonCreate+=proc {|sender,e|
       pokemon=e[0]
       if $game_switches[WILD_POKEMON_SCALE_SWITCH]#Switch
         newlevel=pbBalancedLevel($Trainer.party)#-8+rand(5)   # 8-4 levels lower
         newlevel=1 if newlevel<1
    #     newlevel=(PBExperience::MAXLEVEL)-20 if newlevel>(PBExperience::MAXLEVEL)-20
         newlevel=(PBExperience::MAXLEVEL) if newlevel>(PBExperience::MAXLEVEL)
         pokemon.level=newlevel
         pokemon.calcStats
         pokemon.resetMoves
       end
    }
     
    Well, this is the code, as of v16.2.
    Code:
    def pbBalancedLevel(party)
      return 1 if party.length==0
      # Calculate the mean of all levels
      sum=0
      party.each{|p| sum+=p.level }
      return 1 if sum==0
      average=sum.to_f/party.length.to_f
      # Calculate the standard deviation
      varianceTimesN=0
      for i in 0...party.length
        deviation=party[i].level-average
        varianceTimesN+=deviation*deviation
      end
      # Note: This is the "population" standard deviation calculation, since no
      # sample is being taken
      stdev=Math.sqrt(varianceTimesN/party.length)
      mean=0
      weights=[]
      # Skew weights according to standard deviation
      for i in 0...party.length
        weight=party[i].level.to_f/sum.to_f
        if weight<0.5
          weight-=(stdev/PBExperience::MAXLEVEL.to_f)
          weight=0.001 if weight<=0.001
        else
          weight+=(stdev/PBExperience::MAXLEVEL.to_f)
          weight=0.999 if weight>=0.999
        end
        weights.push(weight)
      end
      weightSum=0
      weights.each{|weight| weightSum+=weight }
      # Calculate the weighted mean, assigning each weight to each level's
      # contribution to the sum
      for i in 0...party.length
        mean+=party[i].level*weights[i]
      end
      mean/=weightSum
      # Round to nearest number
      mean=mean.round
      # Adjust level to minimum
      mean=1 if mean<1
      # Add 2 to the mean to challenge the player
      mean+=2
      # Adjust level to maximum
      mean=PBExperience::MAXLEVEL if mean>PBExperience::MAXLEVEL
      return mean
    end
    Besides calculating the average, it also creates a weighting and standard deviation. This basically causes numbers near the average to be more common and that higher levels are worth more than lower ones.

    Me running some numbers through:
    Spoiler:


    You could create a new function that returns just the average, but I wouldn't do that because with such skewed levels the average would be low.
    What if you have a party of a 100 and five 1s? That gives you levels 17 - 18, which is pitifully low.
     
    Back
    Top