• 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.
  • Ever thought it'd be cool to have your art, writing, or challenge runs featured on PokéCommunity? Click here for info - we'd love to spotlight your work!
  • Our weekly protagonist poll is now up! Vote for your favorite Conquest protagonist in the poll by clicking here.
  • 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.

Random Item/Pokémon Helper

FL

Pokémon Island Creator
  • 2,545
    Posts
    14
    Years
    • Online now
    Code:
    #===============================================================================
    # * Random Item/Pokémon Helper - by FL (Credits will be apreciated)
    #===============================================================================
    #
    # This script is for Pokémon Essentials. It creates a class who helps scripters
    # to do random operations.
    #
    #== INSTALLATION ===============================================================
    #
    # To this script works, put it above main OR convert into a plugin.
    #
    #== HOW TO USE =================================================================
    #
    # Example usage:
    #  
    #  helper = PokemonRandomHelper.new
    #  helper.add(3, :RATTATA)
    #  helper.add(1, :SPEAROW)
    #  helper.add(1, :EKANS)
    #  pbAddPokemon(helper.get, 10)
    #  
    # In this example, player have 60% of receiving a Rattata, 20% for Spearow and
    # 20% for Ekans. If a single pokémon name is misspeled, the helper throws an
    # error, so you don't need to test all results.
    # 
    # Another example:
    #  
    #  helper = ItemRandomHelper.new
    #  # Throws an error if total isn't 100
    #  helper.require_weight = 100
    #  helper.allow_nil = true
    #  helper.allow_repeat = true
    #  helper.add(10.5, :POTION)
    #  # 3% for total. 1% for each item
    #  helper.add_range(3, [:AWAKENING,:ANTIDOTE,:PARLYZEHEAL)
    #  # 2% for each item. 6% for total. 
    #  helper.add_copying_weight(2, [:POKEBALL,:GREATBALL,:ULTRABALL])
    #  helper.add(80.5, nil)
    #  item = helper.get($game_variables[99]/100.0)
    #  if item
    #    pbItemBall(item)
    #  else
    #    pbMessage("Nothing found.")
    #  end
    #
    #== NOTES ======================================================================
    #
    # helper.get also accept an seed (same seed, same results). You can use
    # helper.get_percentage(value) for getting a value percentage change. For
    # items, you can user helper.average_sell_price for average sell price.
    #
    #===============================================================================
    
    if !PluginManager.installed?("Random Item/Pokémon Helper")
      PluginManager.register({                                                 
        :name    => "Random Item/Pokémon Helper",                                        
        :version => "1.1",                                                     
        :link    => "https://www.pokecommunity.com/showthread.php?p=10384370",             
        :credits => "FL"
      })
    end
    
    # Base ruby class. This isn't Essentials/RPG Maker XP dependent.
    # This class can be used for any value.
    class RandomHelper
      attr_accessor :require_weight
      attr_accessor :allow_repeat
      attr_accessor :allow_nil
    
      def initialize
        @allow_repeat = false
        @allow_nil = false
        @total_weight_dirty = true
        @weights_values = []
      end
    
      def add(weight, value)
        if !@allow_nil && value==nil
          raise ArgumentError, "RandomHelper received nil value."
        end
        if !@allow_repeat && @weights_values.find_index{|wv| wv[1] == value }
          raise ArgumentError, "RandomHelper #{value} already included."
        end
        @weights_values.push([weight, value])
        @total_weight_dirty = true
      end
    
      def get(seed = nil)
        seed = Kernel.oldRand if !seed
        raise "RandomHelper is empty." if total_weight == 0
        if !fulfill_require_weight?
          raise "RandomHelper total_height is #{total_weight}, expected #{@require_weight}."
        end
        if seed > 1
          raise RangeError, "Seed is #{seed}. Seeds should be between 0 and 1 (inclusive)."
        end
        seed-=0.000001 if seed==1 # small fix for 1 limit
        count = 0
        for weight_value in @weights_values
          count += weight_value[0]/total_weight.to_f
          return weight_value[1] if seed < count
        end
        raise "Unexpected result!"
      end
    
      def total_weight
        if @total_weight_dirty
          @cached_total_weight = 0
          for weight_value in @weights_values
            @cached_total_weight+=weight_value[0]
          end
          @total_weight_dirty = false
        end
        return @cached_total_weight
      end
    
      def fulfill_require_weight?
        return (
          !@require_weight || 
          @require_weight<=0 || 
          ((total_weight - @require_weight).abs < 0.001)
        )
      end
    
      def get_percentage(value)
        counted_weight=0
        for weight_value in @weights_values
          counted_weight += weight_value[0] if weight_value[1]==value
        end
        return 100.0*counted_weight/total_weight
      end
      
      # Same as calling "add" for each array item.
      def add_copying_weight(weight, array)
        for value in array
          add(weight, value)
        end
      end
      
      # Same as add_copying_weight, but weight is divided by array size.
      def add_range(weight, array)
        add_copying_weight(weight/array.size.to_f, array)
      end
      
      class << Kernel
        if !method_defined?(:oldRand)
          def oldRand
            return rand
          end
        end
      end
    end
    
    class GameDataRandomHelper < RandomHelper
      def add(weight, value)
        raise "@game_data isn't defined!" if !@game_data
        if value && !@game_data.exists?(value)
          raise TypeError, "#{value} isn't a #{@game_data}"
        end
        super(weight, value)
      end
    end
    
    class ItemRandomHelper < GameDataRandomHelper
      def initialize
        @game_data = GameData::Item
        super
      end
    
      def average_buy_price
        counted_price=0
        for weight_value in @weights_values
          next if !weight_value[1]
          counted_price += GameData::Item.get(weight_value[1]).price*weight_value[0]
        end
        return counted_price/total_weight.to_f
      end
      
      def average_sell_price
        return average_buy_price/2.0
      end
    end
    
    class PokemonRandomHelper < GameDataRandomHelper
      def initialize
        @game_data = GameData::Pokemon
        super
      end
    end
    Tested on v19.1. Doesn't work on Essentials versions older than v19. If this script isn't working on latest Essentials version, please inform on this thread.
     
    Last edited:
    This is Nice, but I have no idea how to use it, and I'm thinking that i'm doing it all wrong
    so my question is

    how do I get to work? can you send me an example of how to use it?

    can I have multiple pools?

    common pool, uncommon pool, or rare pool
    for the items

    can you give players money by this?


    Regards
    A.I.Berger
     
    how do I get to work?
    Put the script above main script section OR convert into a plugin. After this, use a event command "script" to call it.

    can you send me an example of how to use it?
    The script has two examples on it header, but I can send you more if you wish.

    can I have multiple pools?

    common pool, uncommon pool, or rare pool
    for the items
    Do you mean several pool for a single draw, right? This script hadn't a support for pools, so I update it and now it has! The method for pools is add_range:

    Code:
      helper = ItemRandomHelper.new
      helper.add_range(60, [:POTION, :POKEBALL,:REPEL])
      helper.add_range(30, [:SUPERPOTION,:GREATBALL,:SUPERREPEL])
      helper.add_range(10, [:HYPERPOTION,:ULTRABALL,:MAXREPEL])
      pbItemBall(helper.get)
    In this example, you have 60% of getting one of common pool items, 30% of uncommon pool items and 10% of rare pool items.

    can you give players money by this?
    You can use for anything, but it has more features (by default) for pokémon and items. Example for a random number (for money):

    Code:
      helper = RandomHelper.new
      helper.add(60, 1000)
      helper.add(30, 3000)
      helper.add(10, 10000)
      money_won = helper.get
      $Trainer.money += money_won
      pbMessage(_INTL("You won ${1}!",money_won))
    In this example you have 60% of obtaining $1000, 30% of $3000 and 10% of $10000.
     
    Back
    Top