Advertisement
Wavescripts

Wave's Limited Supplies

Aug 23rd, 2014
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.45 KB | None | 0 0
  1. ############################################
  2. #                                          #
  3. #        LIMITED SUPPLIES SCRIPT           #
  4. #                                          #
  5. #       v1.0 for RPG Maker VX Ace          #
  6. #                                          #
  7. # Created by Jason "Wavelength" Commander  #
  8. #                                          #
  9. ############################################
  10.  
  11. # "So don't delay.  Act now!  Supplies are running out."
  12. #                                     ~ Smash Mouth
  13.  
  14.  
  15. ############################################
  16. #                                          #
  17. #           ABOUT THIS SCRIPT              #
  18. #                                          #
  19. ############################################
  20.  
  21. # This script allows you to create items for which shops
  22. #      only have a limited supply ("stock"), and to manually
  23. #      change the amount in stock using simple eventing.
  24.  
  25. # Just a few of the possible uses for this script:
  26. #
  27. #      * Limit the availability of healing items, crafting
  28. #        items, etc., for balance purposes.
  29. #
  30. #      * Create one-of-a-kind items (such as a legendary
  31. #        weapon or a quest item) that are available in a
  32. #        shop but can only be bought once.
  33. #
  34. #      * Simulate the rising and falling demand for
  35. #        items in shops, by combining this script with
  36. #        events that take place as game time passes.
  37. #
  38. #      * Temporarily make specific items unavailable in
  39. #        all shops around the world due to an in-game
  40. #        event.
  41. #
  42. #      * Create a sidequest that turns a once rare or
  43. #        unavailable item into a plentiful one that's
  44. #        always available at shops once the quest's complete.
  45. #        (Thanks to Big Rick Cook for this idea!)
  46.  
  47. # Note that the stock of an item available is tracked
  48. #      globally - it cannot be tracked on a per-shop basis.
  49.  
  50. # This script is compatible with most other scripts, even most
  51. #      other shop scripts.  However, it may cause unexpected
  52. #      behavior when combined with scripts that modify the way
  53. #      an item name is written on the Buy or Sell screens.
  54.  
  55.  
  56. ############################################
  57. #                                          #
  58. #              TERMS OF USE                #
  59. #                                          #
  60. ############################################
  61.  
  62. #  Free to use for non-commercial projects - just credit me
  63.  
  64. #  A license is required to use this script in commercial or
  65. #       semi-commercial projects.  Please visit my website
  66. #       wavescripts.wordpress.com for more info about obtaining
  67. #       a license for my scripts.
  68.  
  69. #  You may freely share or modify this script, but you cannot
  70. #       sell it (even if modified) without my written permission
  71.  
  72. #  Please preserve the header (with version #) and terms of use;
  73. #       besides that, feel free to remove any commenting you please
  74.  
  75.  
  76. ############################################
  77. #                                          #
  78. #         HOW TO USE THIS SCRIPT           #
  79. #                                          #
  80. ############################################
  81.  
  82. # Add the Item ID of each Limited Item into the arrays in
  83. #      the "SHOP SUPPLY SETUP" section below.  Items, Weapons,
  84. #      and Armors each have their own array.
  85.  
  86. # Each Limited Supply Item uses a Variable to track its stock.
  87. #      The first item in the Items array will use the variable
  88. #      you have specified in the "First Var Items" option, the
  89. #      next item in that array will use the next variable, and
  90. #      so on.  So if you have 75 Limited Supply Items, be sure
  91. #      to leave a block of 75 consecutive variables that you
  92. #      don't use for other things.  Make sure that the blocks
  93. #      of variables reserved for items, weapons, and armors do
  94. #      not overlap.
  95.  
  96. # A particular item's stock can be manually changed at any
  97. #      time simply by changing its corresponding variable
  98. #      using event commands.  (For example, if Full Potion
  99. #      is the fifth entry in the Limited_items_list array,
  100. #      and your First_var_items is 101, and you want to set
  101. #      the stock of Full Potions to zero, then simply use
  102. #      an event command to set Variable 105 equal to 0.)
  103.  
  104. # This script should be placed in the "Materials" section
  105. #      of the script editor.  If you are using other shop
  106. #      scripts, it's generally better to place this script
  107. #      below those other scripts.
  108.  
  109.  
  110. ############################################
  111. #                                          #
  112. #           SHOP SUPPLY SETUP              #
  113. #                                          #
  114. ############################################
  115.  
  116. module Limited_Items
  117.  
  118. # Use these arrays to define your set of Limited Items.  Simply
  119. #      add the IDs of your Limited Items into the appropriate
  120. #      array (based on whether it's an item, weapon, or armor).
  121. #      The order in which they appear here determines which
  122. #      variable is used to track the stock of each Limited Item,
  123. #      so if you're adding Limited Items as you make your game,
  124. #      it's usually best to add them to the end of the array.
  125. #
  126. #      Example: Limited_items_list = [1, 9, 159, 24, 25, 26, 57, 182, 38]
  127.  
  128. # *** You must set up these arrays for the script to work properly. ***
  129.  
  130.   Limited_items_list = []
  131.   Limited_weapons_list = []
  132.   Limited_armors_list = []
  133.  
  134. # Choose which variable will track the stock of the first
  135. #      Limited Item that you specified in the array above.
  136. #      For example, if you want to use Variable 541 to track
  137. #      the first weapon in the array above, then replace '201'
  138. #      with '541'.  See "How to use this script" above for more info.
  139.  
  140. # *** You must correctly set these variables for the script to work properly. ***
  141.  
  142.   First_var_items = 101
  143.   First_var_weapons = 201
  144.   First_var_armors = 301
  145.  
  146. # If you've done the above two things correctly, this script
  147. #      will work properly.  Everything from here on is entirely
  148. #      optional and is used to customize the system to your liking.
  149.  
  150. # If you want global stock of a Limited Item to increase when
  151. #      that Limited Item is sold to a shop (so that the player
  152. #      can 'buy it back' at the appropriate shop, then leave
  153. #      this option as "true".  If not, change it to "false".
  154.  
  155.   Buyback = true
  156.  
  157. # Normally the Limited Item will read "(Item Name) - (#) Left"
  158. #      if it's in stock.  However, if there are a lot of the
  159. #      the item left in stock, you might want to just say
  160. #      "In Stock".  Set this number to the quantity above which
  161. #      the display will read "(Item Name) - In Stock".  If you
  162. #      always want the number to display no matter how big it is,
  163. #      set this equal to -1.
  164.  
  165.   In_stock_label = 299
  166.  
  167. # You may specify in which color a Limited Items will appear
  168. #      on the shop's Buy screen.  If an item is in stock, it
  169. #      will appear in the Limited Color (or a grayed version
  170. #      of the Limited Color, if you don't have enough money).
  171. #      If it's out of stock, it will appear in a grayed version
  172. #      of the Sold Out Color (regardless of your money).
  173. #      Acceptable values are 0-31 and you can see which number
  174. #      corresponds to which color by going to the Resource
  175. #      Manager, choosing Graphics/System, and viewing "Window".
  176.  
  177.   Limited_Color = 14
  178.   Sold_Out_Color = 8
  179.  
  180. # If you want Limited Items to be shown in the Limited Color
  181. #      on the Sell Item window, leave this option as "true".
  182. #      If you'd rather Limited Items appear just like regular
  183. #      items on the Sell Item window, change it to "false".
  184.  
  185.   Sell_limited_color = true
  186.  
  187.  
  188. ############################################
  189. #                                          #
  190. #               ICKY CODE!                 #
  191. #                                          #
  192. ############################################
  193.  
  194. # Everything from here on represents the inner workings of the script.
  195. #       Please don't alter anything from here on unless you are an
  196. #       advanced scripter yourself (in which case, have at it!)  
  197.  
  198.  
  199.  
  200.   #--------------------------------------------------------------------------
  201.   # This method determines whether the item/weapon/armor is a Limited Item
  202.   #--------------------------------------------------------------------------
  203.   def self.find_presence(item)
  204.     if item.class == RPG::Item && Limited_Items::Limited_items_list.index(item.id) != nil
  205.       return true
  206.     end
  207.     if item.class == RPG::Weapon && Limited_Items::Limited_weapons_list.index(item.id) != nil
  208.       return true
  209.     end
  210.     if item.class == RPG::Armor && Limited_Items::Limited_armors_list.index(item.id) != nil
  211.       return true
  212.     end
  213.     return false
  214.   end
  215.  
  216.   #--------------------------------------------------------------------------
  217.   # This method finds the variable being used to track the Limited Item
  218.   #--------------------------------------------------------------------------
  219.   def self.tracking_vari(item)
  220.     if item.class == RPG::Item
  221.       return Limited_Items::Limited_items_list.index(item.id) + Limited_Items::First_var_items
  222.     end
  223.     if item.class == RPG::Weapon
  224.       return Limited_Items::Limited_weapons_list.index(item.id) + Limited_Items::First_var_weapons
  225.     end
  226.     if item.class == RPG::Armor
  227.       return Limited_Items::Limited_armors_list.index(item.id) + Limited_Items::First_var_armors
  228.     end
  229.   end  
  230.  
  231. end
  232.  
  233.  
  234.  
  235. class Window_ShopBuy < Window_Selectable
  236.  
  237.   #--------------------------------------------------------------------------
  238.   # * Display in Enabled State? - Customized to add functionality to the
  239.   #                               existing "enable?" method
  240.   #--------------------------------------------------------------------------
  241.   alias :enable_limited_items :enable?
  242.   def enable?(item)
  243.     # If it's a Limited Item...
  244.     if Limited_Items.find_presence(item)
  245.       # Determine whether the item is enabled.  Item is disabled if Sold Out.
  246.       if $game_variables[Limited_Items.tracking_vari(item)] <= 0
  247.         return false
  248.       end
  249.     end
  250.     enable_limited_items(item)
  251.   end
  252.  
  253. end
  254.  
  255. class Window_ShopBuy
  256.  
  257.   #--------------------------------------------------------------------------
  258.   # * Draw Limited Item Name - A separate, customized method for drawing
  259.   #                            Limited Item names along with remaining stock
  260.   #
  261.   #     enabled : Enabled flag. When false, draw semi-transparently.
  262.   #--------------------------------------------------------------------------
  263.   def draw_limited_item_name(item, x, y, enabled = true, width = 172)
  264.     return unless item
  265.     draw_icon(item.icon_index, x, y, enabled)
  266.     # Choose a color based on whether the Limited Item is in stock or sold out
  267.     if Limited_Items.find_presence(item)
  268.       if $game_variables[Limited_Items.tracking_vari(item)] > 0
  269.         change_color(text_color(Limited_Items::Limited_Color), enabled)
  270.       else
  271.         change_color(text_color(Limited_Items::Sold_Out_Color), enabled)
  272.       end
  273.     # Combine the item name and number remaining into a single string
  274.       total = $game_variables[Limited_Items.tracking_vari(item)]
  275.       if total > 0
  276.         # If there's more than the "In Stock Label" in stock, print "In Stock"
  277.         if total > Limited_Items::In_stock_label && Limited_Items::In_stock_label != -1
  278.           namestring = item.name + " - In Stock"
  279.         # Otherwise print the quantity of stock that remains
  280.         else
  281.           namestring = item.name + " - " + total.to_s + " Left"
  282.         end
  283.       else
  284.         # If there's none remaining, print "Sold Out!" instead of the number
  285.         namestring = item.name + " - Sold Out!"
  286.       end
  287.     end
  288.     # Draw the item, per usual
  289.     draw_text(x + 24, y, width + 24, line_height, namestring)
  290.   end
  291.  
  292.   #--------------------------------------------------------------------------
  293.   # * Draw Item - Customized to add functionality to the existing
  294.   #               "draw_item" method
  295.   #--------------------------------------------------------------------------
  296.   alias :draw_full_item :draw_item
  297.   def draw_item(index)
  298.     # Either use the regular draw_item_name method or the custom
  299.     #   draw_limited_item_name method, as appropriate
  300.     item = @data[index]
  301.     if !Limited_Items.find_presence(item)
  302.       draw_full_item(index)
  303.     else
  304.       rect = item_rect(index)
  305.       draw_limited_item_name(item, rect.x, rect.y, enable?(item))
  306.       rect.width -= 4
  307.       draw_text(rect, price(item), 2)
  308.     end
  309.   end
  310.  
  311. end
  312.  
  313.  
  314.  
  315. class Window_ShopSell
  316.  
  317.   #--------------------------------------------------------------------------
  318.   # * Draw Item - Taken from Window_ItemList and customized to add
  319.   #               functionality to the existing "draw_item" method
  320.   #--------------------------------------------------------------------------
  321.   alias :draw_full_for_sell :draw_item
  322.   def draw_item(index)
  323.     item = @data[index]
  324.     if item
  325.       # Special logic only occurs if Sell_limited_color option is "true"
  326.       if Limited_Items::Sell_limited_color == true
  327.         # Find the Limited Item ID's position in the array
  328.         if !Limited_Items.find_presence(item)
  329.           draw_full_for_sell(index)
  330.         else
  331.           # If it's in the array, use the Limited Color
  332.           rect = item_rect(index)
  333.           rect.width -= 4
  334.           draw_limited_sell_name(item, rect.x, rect.y, enable?(item))
  335.           draw_item_number(rect, item)
  336.         end
  337.       else
  338.         draw_full_for_sell(index)
  339.       end
  340.     end
  341.   end
  342.  
  343.   #--------------------------------------------------------------------------
  344.   # * Draw Number of Items - Customized to add functionality to the
  345.   #                          existing "draw_item_number" method, but
  346.   #                          it will act like the existing method if
  347.   #                          the Buyback option is "false".
  348.   #--------------------------------------------------------------------------
  349.   alias :draw_number_for_sell :draw_item_number
  350.   def draw_item_number(rect, item)
  351.     # Special logic only occurs if Sell_limited_color option is "true"
  352.     if Limited_Items::Sell_limited_color == true
  353.       # Find the Limited Item ID's position in the array
  354.       if !Limited_Items.find_presence(item)
  355.         draw_number_for_sell(rect, item)
  356.       else
  357.         # If it's in the array, use the Limited Color
  358.         change_color(text_color(Limited_Items::Limited_Color))
  359.         draw_text(rect, sprintf(":%2d", $game_party.item_number(item)), 2)
  360.       end
  361.     else
  362.       draw_number_for_sell(rect, item)
  363.     end
  364.   end
  365.  
  366.   #--------------------------------------------------------------------------
  367.   # * Draw Limited Item Name - A separate, customized method taken from
  368.   #                            based on the "Draw Item Name" method.  It
  369.   #                            draws the item in the Limited Item Color
  370.   #                            if the Sell_limited_color option is "true".
  371.   #
  372.   #     enabled : Enabled flag. When false, draw semi-transparently.
  373.   #--------------------------------------------------------------------------
  374.   def draw_limited_sell_name(item, x, y, enabled = true, width = 172)
  375.     return unless item
  376.     draw_icon(item.icon_index, x, y, enabled)
  377.     change_color(text_color(Limited_Items::Limited_Color), enabled)
  378.     draw_text(x + 24, y, width, line_height, item.name)
  379.   end
  380.  
  381. end
  382.  
  383.  
  384.  
  385. class Scene_Shop
  386.  
  387.   #--------------------------------------------------------------------------
  388.   # * Get Maximum Quantity Buyable - Customized to add functionality to
  389.   #                                  the existing "max_buy" method
  390.   #--------------------------------------------------------------------------
  391.   alias :max_limited_buy :max_buy
  392.   def max_buy
  393.     # If the max is greater than the quantity in stock, set it equal to
  394.     #    the quantity in stock
  395.     if Limited_Items.find_presence(@item)
  396.       [max_limited_buy, $game_variables[Limited_Items.tracking_vari(@item)]].min
  397.     else
  398.       # If not a limited item, simply execute the standard method instead
  399.       max_limited_buy
  400.     end
  401.   end
  402.  
  403.   #--------------------------------------------------------------------------
  404.   # * Execute Purchase - Customized to add functionality to the existing
  405.   #                      "do_buy" method
  406.   #--------------------------------------------------------------------------
  407.   alias :limited_buy :do_buy
  408.   def do_buy(number)
  409.     limited_buy(number)
  410.     # If it's a Limited Item...
  411.     if Limited_Items.find_presence(@item)
  412.       # Reduce the Game Variable that represents the Limited Item by the
  413.       #   quantity that you bought
  414.       $game_variables[Limited_Items.tracking_vari(@item)] -= number
  415.     end
  416.   end
  417.   #--------------------------------------------------------------------------
  418.   # * Execute Sale - Customized to add functionality to the existing
  419.   #                  "do_sell" method, but it will act like the existing
  420.   #                  "do_sell" method if the Buyback option is "false".
  421.   #--------------------------------------------------------------------------
  422.   alias :limited_sell :do_sell
  423.   def do_sell(number)
  424.     limited_sell(number)
  425.     # Special Sell Processing logic only applies if "Buyback" option is "true"
  426.     if Limited_Items::Buyback == true
  427.       # If it's a Limited Item...
  428.       if Limited_Items.find_presence(@item)
  429.         # Reduce the Game Variable that represents the Limited Item by the
  430.         #   quantity that you bought
  431.         $game_variables[Limited_Items.tracking_vari(@item)] += number
  432.       end
  433.     end
  434.   end
  435.  
  436. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement