Fhizban

VX ACE Galv's Random Loot 1.4

Jul 1st, 2014
2,629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.70 KB | None | 0 0
  1. #===============================================================================
  2. #                          +++ GALV'S RANDOM LOOT +++
  3. #===============================================================================
  4. # + Original Script by Galv
  5. # + Extended by Fhizban (also known as Malagar)
  6. # + Version 1.4
  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 have a script call to gain random loot from chests.
  15. #  Add notetags to items to specify level requirements and rarity values.
  16. #  A notetag can also be added to items that you want to increase the chance to
  17. #  find rarer loot. Random loot can be limited to certain types of items too.
  18. #===============================================================================
  19. #                                INSTALLATION
  20. #===============================================================================
  21. # + Put this script after Material but before main.
  22. # + Add some of the notetags (described below) to the items in the database.
  23. # + Add a new event that represents a chest or other container to your map.
  24. # + Add a script call to the event that will generate random loot (see below).
  25. #===============================================================================
  26. #                                DOCUMENTATION
  27. #===============================================================================
  28. # You are required to setup two different parts to make this script work:
  29. #   1) Add Notetags to your items in the database.
  30. #   2) Add the script call to your events (chests).
  31. #   3) Optionally you can change the configuration below to your taste.
  32. #
  33. # 1) Editing Notetags:
  34. #    You must specify the following notetags for all ITEMS, WEAPONS and ARMOR that
  35. #    you want to randomly appear in your events (chests). If any items do not have
  36. #    these tags, they will just not appear in a random chest at all.
  37. #
  38. #    <family: x>                    # The group or family the item belongs to
  39. #                                   # You define these families yourself (1-99)
  40. #    <rarity: x>                    # The rarity value (1+ higher is rarer)
  41. #    <level-min: x>                 # Min level of player for item to show (1-99)
  42. #    <level-max: x>                 # Max level of player for item to show (1-99)
  43. #
  44. #    Optional notetag for ARMORS and WEAPONS only:
  45. #
  46. #    <lucky: x>                     # All equipped lucky items of the party are added to
  47. #                                   # calculate the total chance to get rarer items.
  48. #
  49. # 2) Using the script call:
  50. #    By using this script call, you can randomly obtain an item. Go to your event and add
  51. #    a new command to it, choose script from the very last command tab and insert the
  52. #    following line.
  53. #  
  54. #    EXAMPLE:
  55. #
  56. #            random_item(0, 1, 10, 50, 1, 1)
  57. #
  58. #    SYNTAX:
  59. #
  60. #            random_item(type, rarity_min, rarity_max, subtype=0, monster_id=0, family=0)
  61. #
  62. #    type                           # The type of item to be found:
  63. #                                   # 0 = Random/any type (you can also use numbers 4+)
  64. #                                   # 1 = Item
  65. #                                   # 2 = Armor
  66. #                                   # 3 = Weapon
  67. #
  68. #    rarity_min                     # Min and Max Rareness determine the item rarity that
  69. #    rarity_max                     # the script call will obtain when successful. You can
  70. #                                   # state any number that you also use on the notetags
  71. #                                   # of your items. Example:
  72. #                                   # rarity_min=10, rarity_max=50
  73. #                                   # The script call will generate a random number 1-50.
  74. #                                   # An item can only be obtained if it has a rarity
  75. #                                   # EQUAL or LESS than the generated number.
  76. #
  77. #    subtype                        # OPTIONAL. This only applies to WEAPONS or ARMOR and
  78. #                                   # will be ignored otherwise. Filters the random loot
  79. #                                   # to drop only items of the stated type.
  80. #                                   # (e.g. 1 = General Armour)
  81. #
  82. #    monster_id                     # OPTIONAL. This ID is used to decide what TROPP id
  83. #                                   # will be used when a monster-in-a-box is encountered.
  84. #                                   # Otherwise the standard troop will be used. Ignored
  85. #                                   # if encounter chance is set to zero.
  86. #
  87. #    family                         # OPTIONAL. Family can be any number or 0.
  88. #                                   # 0 = drops any item of the stated type/subtype
  89. #                                   # x = drops only items of the stated type/subtype that
  90. #                                   # share the family.
  91. #
  92. # Using a clever combination of type, subtype and families - you can tailor the random
  93. # loot spawn to create only items of a very specific type. Like only Armors of the Small
  94. # Shield type, or just Weapons of the Sword type and so on.
  95. #===============================================================================
  96.  
  97. $imported = {} if $imported.nil?
  98. $imported["Random_Loot"] = true
  99.  
  100. module Random_Loot
  101. #===============================================================================
  102. #  CONFIGURATION
  103. #===============================================================================
  104.   GET_MESSAGE = "Found "                    # Text before item name.
  105.   GET_MESSAGE_AFTER = "!"                   # Text after item name.
  106.  
  107.   FAIL_MESSAGE = "It's empty!"              # Text when random item comes up nothing.
  108.    
  109.   SOUND_EFFECT = ["Coin", 90, 100]          # Sound effect of gaining an item.
  110.                                             # ["SE Name", volume, pitch]
  111.                                             # Make "SE Name" = "" for no sound.
  112.                                      
  113.   DEFAULT_MONSTER = 1                       # Default Monster ID if none stated.
  114.  
  115.   MONSTER_CHANCE = 10                       # % Chance that if no item is found, a
  116.                                             # battle will happen instead.
  117.   ESCAPE_MONSTER = true                     # Can you escape it? true or false
  118.    
  119.   MONSTER_MESSAGE = "Monster in a box!"     # Message when a monster appears.
  120.                                             # Make it "" to disable.
  121. #===============================================================================
  122. #  END CONFIGURATION
  123. #===============================================================================
  124. end
  125.  
  126. #===============================================================================
  127. module Random_Loot_Notetags
  128.   def loot_rarity
  129.     if @loot_rarity.nil?
  130.       if @note =~ /<rarity: (.*)>/i
  131.         @loot_rarity = $1.to_i
  132.       else
  133.         @loot_rarity = 0
  134.       end
  135.     end
  136.     @loot_rarity
  137.   end
  138.   def loot_level_min
  139.     if @loot_level_min.nil?
  140.       if @note =~ /<level-min: (.*)>/i
  141.         @loot_level_min = $1.to_i
  142.       else
  143.         @loot_level_min = 0
  144.       end
  145.     end
  146.     @loot_level_min
  147.   end
  148.   def loot_level_max
  149.     if @loot_level_max.nil?
  150.       if @note =~ /<level-max: (.*)>/i
  151.         @loot_level_max = $1.to_i
  152.       else
  153.         @loot_level_max = 0
  154.       end
  155.     end
  156.     @loot_level_max
  157.   end
  158.   def loot_lucky
  159.     if @loot_lucky.nil?
  160.       if @note =~ /<lucky: (.*)>/i
  161.         @loot_lucky = $1.to_i
  162.       else
  163.         @loot_lucky = 0
  164.       end
  165.     end
  166.     @loot_lucky
  167.   end
  168.   def loot_family
  169.     if @loot_family.nil?
  170.       if @note =~ /<family: (.*)>/i
  171.         @loot_family = $1.to_i
  172.       else
  173.         @loot_family = 0
  174.       end
  175.     end
  176.     @loot_family
  177.   end
  178.   end # Random_Loot_Notetags
  179. #===============================================================================
  180.  
  181. class RPG::Item
  182.   include Random_Loot_Notetags
  183. end
  184. class RPG::Armor
  185.   include Random_Loot_Notetags
  186. end
  187. class RPG::Weapon
  188.   include Random_Loot_Notetags
  189. end
  190.  
  191. #===============================================================================
  192. class Game_Interpreter
  193.  
  194.   def random_item(type, rarity_min, rarity_max, subtype=0, monster_id=0, family=0)
  195.    
  196.     #--- Determine Type
  197.     if type == 1
  198.       @loot = $data_items
  199.       subtype = 0
  200.     elsif type == 2
  201.       @loot = $data_armors
  202.     elsif type == 3
  203.       @loot = $data_weapons
  204.     else
  205.       @loot = $data_items | $data_armors | $data_weapons
  206.       subtype = 0
  207.     end
  208.  
  209.     #--- Preperations
  210.     @loot.shuffle
  211.     if rarity_max < rarity_min
  212.       rarity_max = rarity_min
  213.     end
  214.     if subtype < 0 || subtype > 99
  215.       subtype = 0
  216.     end
  217.     if family < 0 || family > 99
  218.       subtype = 0
  219.     end
  220.     if monster_id < 1
  221.       monster_id = Random_Loot::DEFAULT_MONSTER
  222.     end
  223.    
  224.     #--- Determine Party Luck Bonus
  225.     mem = 0
  226.     eqs = 0
  227.     luck_bonus = 0
  228.     i = 0
  229.     no_equips = $game_party.members.count * $game_party.members[0].equips.count
  230.     $game_party.members.each do |mem|
  231.       mem.equips.each do |eq|
  232.         next if eq.nil?
  233.         luck_bonus += eq.loot_lucky
  234.       end
  235.     end
  236.    
  237.     #--- Determine Rarity Chance
  238.     checked = 0
  239.     restart = false
  240.     continue = true
  241.     no_items = @loot.count - 1
  242.     rare_chance = rand(rarity_max - rarity_min) + rarity_min + luck_bonus + 1
  243.     random_item = rand(no_items) + 1
  244.     begin_count = rand(no_items) + 1
  245.    
  246.     #--- Determine Random Item
  247.     no_items.times do |i|
  248.       i = i + random_item
  249.       begin_count += 1 if restart
  250.       if i > no_items
  251.         i = i - i + rand(no_items) + 1
  252.         restart = true
  253.       end
  254.    
  255.       #--- Get Random Item
  256.       if family != 0 && @loot[i].loot_family != family
  257.         continue = false
  258.       end
  259.       if subtype != 0 && type == 2
  260.         if @loot[i].atype_id == subtype
  261.           continue = false
  262.         end
  263.       end
  264.       if subtype != 0 && type == 3
  265.         if @loot[i].wtype_id == subtype
  266.           continue = false
  267.         end
  268.       end
  269.  
  270.       if continue
  271.         if @loot[i].loot_level_max >= $game_party.leader.level && @loot[i].loot_level_min <= $game_party.leader.level
  272.           if @loot[i].loot_rarity <= rare_chance
  273.             if rand(1) <= 0 #--- Dirty Hack to skip some items
  274.               $game_party.gain_item(@loot[i], 1)
  275.               RPG::SE.new(Random_Loot::SOUND_EFFECT[0], Random_Loot::SOUND_EFFECT[1], Random_Loot::SOUND_EFFECT[2]).play
  276.               $game_message.add(Random_Loot::GET_MESSAGE + "\\I[" + @loot[i].icon_index.to_s + "]" + @loot[i].name.to_s + Random_Loot::GET_MESSAGE_AFTER)
  277.               wait_for_message
  278.               return
  279.             end
  280.           end
  281.         end
  282.       end
  283.       checked += 1
  284.      
  285.       #--- Trigger Monster-in-a-Box Battle
  286.       if checked == no_items
  287.         monster_chance = rand(100) + 1
  288.         if monster_chance <= Random_Loot::MONSTER_CHANCE
  289.           if Random_Loot::MONSTER_MESSAGE != ""
  290.             $game_message.add("\\>" + Random_Loot::MONSTER_MESSAGE + "\\.\\.\\.\\^")
  291.             wait_for_message
  292.           end
  293.           BattleManager.setup(monster_id, Random_Loot::ESCAPE_MONSTER, false)
  294.           SceneManager.call(Scene_Battle)
  295.         else
  296.           $game_message.add(Random_Loot::FAIL_MESSAGE)
  297.           wait_for_message
  298.         end
  299.       end
  300.     end
  301.   end
  302.    
  303. end # Game_Interpreter
  304.  
  305. #===============================================================================
  306. # END OF SCRIPT
  307. #===============================================================================
Advertisement
Add Comment
Please, Sign In to add comment