Fhizban

VX Ace Simple Random Shop v1.0

Aug 19th, 2015
442
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #===============================================================================
  2. #                          +++ SIMPLE RANDOM SHOP +++
  3. #===============================================================================
  4. # + Original Script by Fhizban (also known as Malagar)
  5. #
  6. # + Version 1.0
  7. # + Updated August 2015
  8. # + For RPGMaker VX Ace
  9. # + Free for Non-Commercial and Commercial use
  10. # + Requires no other scripts
  11. #===============================================================================
  12. #                                DESCRIPTION
  13. #===============================================================================
  14. #  This script allows you to use a script call to randomly generate a shop. Add
  15. #  notetags to items to specify level requirements and rarity values. The shop
  16. #  inventory will be generated automatically according to the party level and
  17. #  the notetags provided for items. Shops can sell items of specific types,
  18. #  subtypes or custom defined families.
  19. #===============================================================================
  20. #                                INSTALLATION
  21. #===============================================================================
  22. # + Put this script after Material but before main.
  23. # + Add some of the notetags (described below) to the items in the database.
  24. # + Add a new event that represents a shop to your map.
  25. # + Add a script call to the event that will generate a random shop (see below).
  26. #===============================================================================
  27. #                                DOCUMENTATION
  28. #===============================================================================
  29. # You are required to setup two different parts to make this script work:
  30. #   1) Add Notetags to your items in the database.
  31. #   2) Add the script call to your events (chests).
  32. #   3) Optionally you can change the configuration below to your taste.
  33. #
  34. # 1) Editing Notetags:
  35. #    You must specify the following notetags for all ITEMS, WEAPONS and ARMOR that
  36. #    you want to randomly appear in your events (shops). If any items do not have
  37. #    these tags, they will just not appear in a random shops at all.
  38. #
  39. #    <family: x>                    # The group or family the item belongs to
  40. #                                   # You define these families yourself (1-99)
  41. #    <rarity: x>                    # The rarity value (1+ higher is rarer)
  42. #    <level-min: x>                 # Min level of player for item to show (1-99)
  43. #    <level-max: x>                 # Max level of player for item to show (1-99)
  44. #
  45. #    Optional notetag for ARMORS and WEAPONS only:
  46. #
  47. #    <lucky: x>                     # All equipped lucky items of the party are added to
  48. #                                   # calculate the total chance to get rarer items.
  49. #
  50. #    New Optional notetags for all item types:
  51. #
  52. #    <noshop>      # Item does never appear in a random shop (requires Simple Random Shop 1.0+)
  53. #    <noloot>      # Item is never found as random loot (requires Simple Random Loot 1.4+)
  54. #    <nodrop>      # Item is never dropped from an enemy (requires Simple Random Drop 1.0+)
  55. #
  56. # 2) Using the script call:
  57. #    By using this script call, you can randomly populate a shop. Go to your event and add
  58. #    a new command to it, choose script from the very last command tab and insert the
  59. #    following line.
  60. #  
  61. #    EXAMPLE:
  62. #
  63. #            random_shop(0, 1, 10, 50, 1, 1)
  64. #
  65. #    SYNTAX:
  66. #
  67. #            random_shop(type, rarity_min, rarity_max, price=0, amount=0, subtype=0, family=0)
  68. #
  69. #    type                           # The type of item to be found:
  70. #                                   # 0 = All Types, determined randomly
  71. #                                   # 1 = Item
  72. #                                   # 2 = Armor
  73. #                                   # 3 = Weapon
  74. #                                   # 4 = One randomly determined Type
  75. #
  76. #    rarity_min                     # Min and Max Rareness determine the item rarity that
  77. #    rarity_max                     # the script call will obtain when successful. You can
  78. #                                   # state any number that you also use on the notetags
  79. #                                   # of your items. Example:
  80. #                                   # rarity_min=10, rarity_max=50
  81. #                                   # The script call will generate a random number 1-50.
  82. #                                   # An item can only be obtained if it has a rarity
  83. #                                   # EQUAL or LESS than the generated number.
  84. #
  85. #    price                          # OPTIONAL. This will alter the price of all items in
  86. #                                   # the shop by X percent. It is also possible to set
  87. #                                   # this to a negative number, lowering the prices.
  88. #
  89. #    amount                         # OPTIONAL. This limites the shop inventory to X items.
  90. #                                   # If not set, the default maximum amount limit is used.
  91. #
  92. #    subtype                        # OPTIONAL. This only applies to WEAPONS or ARMOR and
  93. #                                   # will be ignored otherwise. Filters the random loot
  94. #                                   # to drop only items of the stated type.
  95. #                                   # (e.g. 1 = General Armour)
  96. #
  97. #    family                         # OPTIONAL. Family can be any number or 0.
  98. #                                   # 0 = drops any item of the stated type/subtype
  99. #                                   # x = drops only items of the stated type/subtype that
  100. #                                   # share the family.
  101. #
  102. # Using a clever combination of type, subtype and families - you can tailor the random
  103. # shop to offer only items of a very specific type. Like only Armors of the Small
  104. # Shield type, or just Weapons of the Sword type and so on.
  105. #===============================================================================
  106. #                                COMPATABILITY
  107. #===============================================================================
  108. # + Should be compatible with most other scripts (like YANFLY engine).
  109. # + Can be combined with my other scripts to form the complete ITEM SUITE:
  110. #   + Simple Random Loot
  111. #   + Simple Random Shop
  112. #   + Simple Random Drop
  113. #   + Simple Random Rarities
  114. #===============================================================================
  115.  
  116. $imported = {} if $imported.nil?
  117. $imported["Random_Shop"] = true
  118.  
  119. module Random_Shop
  120. #===============================================================================
  121. #  CONFIGURATION
  122. #===============================================================================
  123.                                      
  124.   DEFAULT_AMOUNT = 5                        # Maximum number of different, available
  125.                                             # Items if nothing was stated.
  126.  
  127. #===============================================================================
  128. #  END CONFIGURATION
  129. #===============================================================================
  130. end
  131.  
  132. #===============================================================================
  133. module Random_Shop_Notetags
  134.   def loot_rarity
  135.     if @loot_rarity.nil?
  136.       if @note =~ /<rarity: (.*)>/i
  137.         @loot_rarity = $1.to_i
  138.       else
  139.         @loot_rarity = 0
  140.       end
  141.     end
  142.     @loot_rarity
  143.   end
  144.   def loot_level_min
  145.     if @loot_level_min.nil?
  146.       if @note =~ /<level-min: (.*)>/i
  147.         @loot_level_min = $1.to_i
  148.       else
  149.         @loot_level_min = 0
  150.       end
  151.     end
  152.     @loot_level_min
  153.   end
  154.   def loot_level_max
  155.     if @loot_level_max.nil?
  156.       if @note =~ /<level-max: (.*)>/i
  157.         @loot_level_max = $1.to_i
  158.       else
  159.         @loot_level_max = 0
  160.       end
  161.     end
  162.     @loot_level_max
  163.   end
  164.   def loot_lucky
  165.     if @loot_lucky.nil?
  166.       if @note =~ /<lucky: (.*)>/i
  167.         @loot_lucky = $1.to_i
  168.       else
  169.         @loot_lucky = 0
  170.       end
  171.     end
  172.     @loot_lucky
  173.   end
  174.   def loot_family
  175.     if @loot_family.nil?
  176.       if @note =~ /<family: (.*)>/i
  177.         @loot_family = $1.to_i
  178.       else
  179.         @loot_family = 0
  180.       end
  181.     end
  182.     @loot_family
  183.   end
  184.   def loot_noshop
  185.     if @loot_noshop.nil?
  186.       if @note =~ /<noshop>/i
  187.         @loot_noshop = true
  188.       else
  189.         @loot_noshop = false
  190.       end
  191.     end
  192.     @loot_noshop
  193.   end
  194.   def loot_noloot
  195.     if @loot_noloot.nil?
  196.       if @note =~ /<noloot>/i
  197.         @loot_noloot = true
  198.       else
  199.         @loot_noloot = false
  200.       end
  201.     end
  202.     @loot_noloot
  203.   end
  204.   def loot_nodrop
  205.     if @loot_nodrop.nil?
  206.       if @note =~ /<nodrop>/i
  207.         @loot_nodrop = true
  208.       else
  209.         @loot_nodrop = false
  210.       end
  211.     end
  212.     @loot_nodrop
  213.   end
  214.   end # Random_Shop_Notetags
  215. #===============================================================================
  216.  
  217. class RPG::Item
  218.   include Random_Shop_Notetags
  219. end
  220. class RPG::Armor
  221.   include Random_Shop_Notetags
  222. end
  223. class RPG::Weapon
  224.   include Random_Shop_Notetags
  225. end
  226.  
  227. #===============================================================================
  228. class Game_Interpreter
  229.  
  230.   def random_shop(type, rarity_min, rarity_max, price=0, amount=0, subtype=0, family=0)
  231.    
  232.     @goods = []
  233.     @loot = []
  234.    
  235.     #--- Determine Type
  236.     if type == 0
  237.       @loot = $data_items + $data_armors + $data_weapons
  238.       subtype = 0
  239.     elsif type == 1
  240.       @loot = $data_items
  241.       @itemtype = 0
  242.       subtype = 0
  243.     elsif type == 2
  244.       @loot = $data_armors
  245.       @itemtype = 2
  246.     elsif type == 3
  247.       @loot = $data_weapons
  248.       @itemtype = 1
  249.     else
  250.       @loot = $data_items | $data_armors | $data_weapons
  251.       subtype = 0
  252.     end
  253.  
  254.     #--- Preperations
  255.     @loot.shuffle
  256.     rarity_max = rarity_min if rarity_max < rarity_min
  257.     amount = Random_Shop::DEFAULT_AMOUNT if amount < 1
  258.     subtype = 0 if subtype < 0 || subtype > 99
  259.     subtype = 0 if family < 0 || family > 99
  260.  
  261.     #--- Determine Party Luck Bonus
  262.     mem = 0
  263.     eqs = 0
  264.     luck_bonus = 0
  265.     i = 0
  266.     no_equips = $game_party.members.count * $game_party.members[0].equips.count
  267.     $game_party.members.each do |mem|
  268.       mem.equips.each do |eq|
  269.         next if eq.nil?
  270.         luck_bonus += eq.loot_lucky
  271.       end
  272.     end
  273.        
  274.     #--- Determine Rarity Chance
  275.     checked = 0
  276.     inventory = 0
  277.     itemprice = 0
  278.     duplicates = []
  279.     restart = false
  280.     continue = true
  281.     no_items = @loot.count - 1
  282.     rare_chance = rand(rarity_max - rarity_min) + rarity_min + luck_bonus + 1
  283.     random_item = rand(no_items) + 1
  284.     begin_count = rand(no_items) + 1
  285.    
  286.     #--- Determine Random Item
  287.     no_items.times do |i|
  288.       i = i + random_item
  289.       begin_count += 1 if restart
  290.       if i > no_items
  291.         i = i - i + rand(no_items) + 1
  292.         restart = true
  293.       end
  294.    
  295.       #--- Get Random Item
  296.       if family != 0 && @loot[i].loot_family != family
  297.         continue = false
  298.       end
  299.       if subtype != 0 && type == 2
  300.         if @loot[i].atype_id == subtype
  301.           continue = false
  302.         end
  303.       end
  304.       if subtype != 0 && type == 3
  305.         if @loot[i].wtype_id == subtype
  306.           continue = false
  307.         end
  308.       end
  309.  
  310.       #--- Setup Shop Inventory
  311.       if continue
  312.         next if @loot[i].nil?
  313.         next if @loot[i].loot_noshop
  314.         if @loot[i].loot_level_max >= $game_party.leader.level && @loot[i].loot_level_min <= $game_party.leader.level
  315.           if @loot[i].loot_rarity <= rare_chance
  316.             if checked < no_items && inventory < amount
  317.               if !duplicates.include?(@loot[i].id)
  318.                
  319.                 #--- Recheck Item Type
  320.                 if @loot[i].is_a?(RPG::Item)
  321.                   @itemtype = 0
  322.                 elsif @loot[i].is_a?(RPG::Weapon)
  323.                   @itemtype = 1
  324.                 elsif @loot[i].is_a?(RPG::Armor)
  325.                   @itemtype = 2
  326.                 end
  327.                
  328.                 #--- Check Item Price
  329.                 if price != 0
  330.                   itemprice = @loot[i].price + (@loot[i].price * (price/100))
  331.                 end
  332.                 if price <= 0
  333.                   @goods.push([@itemtype, @loot[i].id, 0])
  334.                 else
  335.                   @goods.push([@itemtype, @loot[i].id, 1, itemprice])
  336.                 end
  337.                 duplicates.push(@loot[i].id)
  338.                 inventory += 1
  339.               end
  340.             else
  341.               break
  342.             end
  343.           end
  344.         end
  345.       end
  346.       checked += 1
  347.     end
  348.    
  349.     if @goods.size > 0
  350.       @goods.shuffle
  351.       SceneManager.call(Scene_Shop)
  352.       SceneManager.scene.prepare(@goods, true)
  353.     end
  354.    
  355.   end
  356.    
  357. end # Game_Interpreter
  358.  
  359. #===============================================================================
  360. # END OF SCRIPT
  361. #===============================================================================
RAW Paste Data