Advertisement
gerkrt

RPGXP - ENG - Multiusable items

Sep 14th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.77 KB | None | 0 0
  1. #==============================================================================
  2. # Multiusable items
  3. # By gerkrt/gerrtunk
  4. # Version: 1.5
  5. # License: GPL, credits
  6. #==============================================================================
  7. =begin
  8.  
  9. --------Instructions-----------
  10.  
  11. Just add new items in the variable Multiusable_items. You have to do this to
  12. add a new item every time.
  13.  
  14. Ex: If you wanted to add the item number 5 of the database with 3 uses:
  15.  
  16. Multiusable_items = {1=>3, 3=>2, 5=>3}
  17.  
  18. First goes the item id, then the uses. Note that you can remove the two examples
  19. i have add:
  20.  
  21. Multiusable_items = {5=>3}
  22.  
  23. This works like in old Rpgmakers. Internally it will be used the item with less
  24. number of uses left.
  25.  
  26. ----------Options-------------
  27.  
  28. Show_uses: If you turn this option on it will add to every multiusable descriptions
  29. the number of uses left, example:
  30.  
  31. Super Potion
  32. Description: 2/2 uses. Recovers 300 HP.
  33.  
  34. Uses_text: You can modify here the text that is add before the uses ratio.
  35.  
  36.  
  37. ----------Compatibality-------------
  38.  
  39. The show uses option modifies the shop and item menu, if you have some script
  40. that changue that it can give you problems. Turn this option off if something goes
  41. wrong.
  42.  
  43. =end
  44.  
  45. module Wep
  46.     # By uses. {Item_id=> number of uses}
  47.     Multiusable_items = {1=>3, 3=>2}
  48.     Show_uses = true
  49.     Uses_text = ' uses. '
  50. end
  51.  
  52. class Game_Party
  53.    attr_reader :multiusable_items
  54.    alias wep_gm_par_init initialize
  55.    def initialize
  56.      wep_gm_par_init
  57.      @multiusable_items = []
  58.   end
  59.  
  60.   #--------------------------------------------------------------------------
  61.   # * Gain Items (or lose)
  62.   #     item_id : item ID
  63.   #     n       : quantity
  64.   #--------------------------------------------------------------------------
  65.   def gain_item(item_id, n)
  66.     # Update quantity data in the hash.
  67.     if item_id > 0
  68.      # Check if multiusable
  69.      if multiusable?(item_id) and n > 0
  70.        for i in 0...n
  71.         # Push a new item with uses and item id
  72.         uses = Wep::Multiusable_items[item_id]
  73.         @multiusable_items.push([item_id,uses])
  74.        end
  75.       end
  76.       @items[item_id] = [[item_number(item_id) + n, 0].max, 99].min
  77.     end
  78.   end
  79.  
  80.   #--------------------------------------------------------------------------
  81.   # * Lose Items
  82.   #     item_id : item ID
  83.   #     n       : quantity
  84.   #--------------------------------------------------------------------------
  85.   def lose_item(item_id, n)
  86.      if multiusable?(item_id) and have_multiusables?(item_id) and not $scene.is_a?(Scene_Shop)
  87.        # Sort by uses
  88.        @multiusable_items.sort! {|a,b|a[1]<=> b[1]}
  89.        # Iterate over all items in search of what have the lowest uses
  90.        i=0
  91.        for item in @multiusable_items
  92.         if item[0] == item_id
  93.           @multiusable_items[i][1]-=1
  94.           # If have no more uses, delete it
  95.           if @multiusable_items[i][1] == 0
  96.            @multiusable_items.delete(item)
  97.            @items[item_id] = [[item_number(item_id) -1, 0].max, 99].min
  98.          end
  99.          break
  100.        end
  101.        i+=1
  102.        end
  103.       elsif $scene.is_a?(Scene_Shop) and multiusable?(item_id)
  104.         i=0
  105.         to_lose = n
  106.         @multiusable_items.sort! {|a,b|a[1]<=> b[1]}
  107.         for item in @multiusable_items
  108.           if to_lose == 0
  109.             break
  110.           end
  111.           if item[0] == item_id
  112.              @multiusable_items.delete_at(i)
  113.              to_lose-=1
  114.           end
  115.           i+=1
  116.         end
  117.         @items[item_id] = [[item_number(item_id) -n, 0].max, 99].min
  118.        else
  119.        # Reverse the numerical value and call it gain_item
  120.         gain_item(item_id, -n)
  121.       end
  122.     end
  123.    
  124.   #--------------------------------------------------------------------------
  125.   # * Have Multiusables?
  126.   #--------------------------------------------------------------------------
  127.   def have_multiusables?(item_id)
  128.        for item in @multiusable_items
  129.          if item[0] == item_id
  130.            return true
  131.          end
  132.        end
  133.       return false
  134.   end
  135.    
  136.   #--------------------------------------------------------------------------
  137.   # * Multiusables?
  138.   #--------------------------------------------------------------------------
  139.   def multiusable?(item_id)
  140.     return Wep::Multiusable_items[item_id]
  141.   end
  142. end
  143.  
  144. if Wep::Show_uses
  145.   class Window_Item < Window_Selectable
  146.     #--------------------------------------------------------------------------
  147.     # * Help Text Update
  148.     #--------------------------------------------------------------------------
  149.     def update_help
  150.       if Wep::Multiusable_items[self.item.id]
  151.         for item in $game_party.multiusable_items
  152.          if item[0] == self.item.id
  153.            uses=item[1]
  154.            break
  155.          end
  156.        end
  157.         description = uses.to_s+'/'+Wep::Multiusable_items[self.item.id].to_s+Wep::Uses_text+self.item.description
  158.         @help_window.set_text(description)
  159.       else
  160.         @help_window.set_text(self.item == nil ? "" : self.item.description)
  161.       end
  162.     end
  163.   end
  164.  
  165.   class Window_ShopSell < Window_Selectable
  166.     #--------------------------------------------------------------------------
  167.     # * Help Text Update
  168.     #--------------------------------------------------------------------------
  169.     def update_help
  170.       if Wep::Multiusable_items[self.item.id]
  171.         for item in $game_party.multiusable_items
  172.          if item[0] == self.item.id
  173.            uses=item[1]
  174.            break
  175.          end
  176.        end
  177.         description = uses.to_s+'/'+Wep::Multiusable_items[self.item.id].to_s+Wep::Uses_text+self.item.description
  178.         @help_window.set_text(description)
  179.       else
  180.         @help_window.set_text(self.item == nil ? "" : self.item.description)
  181.       end
  182.     end
  183.   end  
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement