TheoAllen

Theo - Limited Inventory (IND)

Sep 11th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 18.44 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Limited Inventory
  3. # Version : 1.2d
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_LimInventory] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2014.02.11 - Bugfix. Unequip item causes lose the item if inventory is full
  12. # 2013.10.07 - Bugfix. Item doesn't removed when discarded
  13. #            - Bugfix. Inventory amount not refreshed when item is discarded
  14. # 2013.10.04 - Compatibility fix with chest system
  15. # 2013.08.30 - Now unable to discard key item
  16. # 2013.08.22 - Bugfix. Item size notetag isn't working
  17. # 2013.08.19 - Bugfix when gained item
  18. #            - Bugfix when disable display item size in shop menu
  19. # 2013.08.18 - Finished script
  20. # =============================================================================
  21. =begin
  22.  
  23.   ---------------------------------------------------------------------------
  24.   Perkenalan :
  25.   Script ini ngebikin kamu bisa mbatesin inventory dengan jumlah item secara
  26.   keseluruhan daripada item secara individu
  27.  
  28.   ---------------------------------------------------------------------------
  29.   Cara penggunaan :
  30.   Pasang dibawah material namun diatas main
  31.   Kalo kamu make YEA - Shop Option, taruh ini dibawahnya
  32.   Edit konfigurasinya jika perlu
  33.  
  34.   ---------------------------------------------------------------------------
  35.   Notetags :
  36.   Tulis notetag ini di kotak note dalam databasemu
  37.  
  38.   <inv size: n>
  39.   Gunakan notetag ini dimana n adalah angka untuk menentukan berat / ukuran
  40.   item tersebut. Gunakan 0 untuk jumlah yang tak terbatas. Hanya bekerja untuk
  41.   equip seperti weapon dan armor
  42.  
  43.   <inv plus: n>
  44.   Gunakan notetag ini untuk menentukan tambahan slot kosong. Notetag ini
  45.   tersedia untuk Actor, Class, Equip, dan States. Jika dipake buat actor, slot
  46.   kosong akan nambah kalo actor laen masuk party. Kalo equip, slot kosong akan
  47.   nambah kalo actor make equip tertentu. Begitu juga states
  48.  
  49.   <inv minus: n>
  50.   Kebalikan dari <inv plus>. Notetag ini akan ngurangin slot kosong. Wa rasa
  51.   dah cukup jelas
  52.  
  53.   ---------------------------------------------------------------------------
  54.   Script call :
  55.   Kalo kamu pengen nambahin item walo inventory udah penuh, gunakan script call
  56.   kek gini
  57.  
  58.   force_gain_item($data_items[id],amount)
  59.  
  60.   id adalah id item yang ada di database
  61.  
  62.   ---------------------------------------------------------------------------
  63.   Terms of use :
  64.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  65.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Dan btw, kalo dipake
  66.   untuk komersial, bagi ane keuntungannya juga. Ama gratisan gamenya juga
  67.  
  68. =end
  69. # =============================================================================
  70. # Konfigurasi :
  71. # =============================================================================
  72. module THEO
  73.   module LimInv
  74.  
  75.   # --------------------------------------------------------------------------
  76.   # Setting Umum (Tulis aja: true / false)
  77.   # --------------------------------------------------------------------------
  78.  
  79.     DynamicSlot       = true
  80.   # Total slot kosong akan tergantung pada actor, status, party, equip, dll..
  81.  
  82.     Display_ItemSize  = true
  83.   # Nampilin size item dalam menu
  84.  
  85.     Include_Equip     = true
  86.   # Total slot yg digunain juga termasuk equip yang dipake actor
  87.  
  88.     DrawTotal_Size    = true
  89.   # Kalo diset true, menu akan nampilin total size dari sebuah item. Misalnya
  90.   # kamu punya potion 10. Dan setiap potion ukurannya 3. Maka menu akan
  91.   # nampilin 30. Bukan 3
  92.  
  93.   # --------------------------------------------------------------------------
  94.   # Settingan Angka
  95.   # --------------------------------------------------------------------------
  96.  
  97.     Default_FreeSlot  = 100
  98.   # Nilai default tambahan slot kosong per karakter. Tentu aja kamu bisa ganti
  99.   # manual pake notetag. Jika DynamicSlot kamu set false, ini akan jadi nilai
  100.   # patokan buat slot kosong
  101.  
  102.     NearMaxed_Percent = 25
  103.   # Presentase untuk menentukan inventori udah hampir penuh ato belom
  104.  
  105.     NearMaxed_Color   = 21    
  106.   # Jika inventori hampir penuh, inventori akan ditampilin dengan warna yang
  107.   # berbeda. Kode warnanya sama kek \C[n] di message
  108.  
  109.     UseCommand_Size   = 200    
  110.   # Lebar window use item
  111.  
  112.   # --------------------------------------------------------------------------
  113.   # Settingan Vokab (Udah jelas wa rasa)
  114.   # --------------------------------------------------------------------------
  115.  
  116.     InvSlotVocab    = "Inventory: "   # Inventory Vocab
  117.     InvSizeVocab    = "Item Size: "   # Item size / weight
  118.     SlotVocabShort  = "Inv:"          # Singkatan untuk inventory
  119.     UseVocab        = "Use item"      # Use item
  120.     DiscardVocab    = "Discard item"  # Discard Item
  121.     CancelVocab     = "Cancel"        # Cancel
  122.    
  123.   end
  124. end
  125. # ============================================================================
  126. # Jangan setuh apapun setelah ini
  127. # ============================================================================
  128.  
  129. # Udah wa peringatin lho ya :v
  130.  
  131. # ============================================================================
  132. # Altered built in modules and classes
  133. # ============================================================================
  134. class << DataManager
  135.  
  136.   alias theo_limited_item_load_db load_database
  137.   def load_database
  138.     theo_limited_item_load_db
  139.     load_limited_slot
  140.   end
  141.  
  142.   def load_limited_slot
  143.     database = $data_actors + $data_classes + $data_weapons + $data_armors +
  144.       $data_states + $data_items
  145.     database.compact.each do |db|
  146.       db.load_limited_inv
  147.     end
  148.   end
  149. end
  150.  
  151. class RPG::BaseItem
  152.   attr_accessor :inv_size # Item inventory size
  153.   attr_accessor :inv_mod  # Inventory slot modifier
  154.  
  155.   def load_limited_inv
  156.     @inv_size = 1
  157.     @inv_mod = self.is_a?(RPG::Actor) ? THEO::LimInv::Default_FreeSlot : 0
  158.     self.note.split(/[\r\n]+/).each do |line|
  159.       case line
  160.       when /<(?:INV_SIZE|inv size): [ ]*(\d+)>/i
  161.         @inv_size = $1.to_i
  162.       when /<(?:INV_PLUS|inv plus): [ ]*(\d+)>/i
  163.         @inv_mod = $1.to_i
  164.       when /<(?:INV_MINUS|inv minus): [ ]*(\d+)>/i
  165.         @inv_mod = -$1.to_i
  166.       end
  167.     end
  168.   end
  169.  
  170. end
  171. # ============================================================================
  172. # Data structures and workflows goes here
  173. # ============================================================================
  174. class Game_Actor < Game_Battler
  175.  
  176.   def equip_size
  177.     return 0 unless THEO::LimInv::Include_Equip
  178.     equips.compact.inject(0) {|total,equip| total + equip.inv_size}
  179.   end
  180.  
  181.   def inv_max
  182.     result = $data_actors[id].inv_mod
  183.     result += $data_classes[class_id].inv_mod
  184.     result += states.inject(0) {|total,db| total + db.inv_mod}
  185.     result += equips.compact.inject(0) {|total,db| total + db.inv_mod}
  186.     result
  187.   end
  188.   # --------------------------------------------------------------------------
  189.   # Overwrite : Trade item with party
  190.   # --------------------------------------------------------------------------
  191.   def trade_item_with_party(new_item, old_item)
  192.     return false if new_item && !$game_party.has_item?(new_item)
  193.     $game_party.force_gain_item(old_item, 1)
  194.     $game_party.force_gain_item(new_item, -1)
  195.     return true
  196.   end
  197.  
  198. end
  199.  
  200. class Game_Party < Game_Unit
  201.  
  202.   def inv_max
  203.     return THEO::LimInv::Default_FreeSlot unless THEO::LimInv::DynamicSlot
  204.     return members.inject(0) {|total,member| total + member.inv_max}
  205.   end
  206.  
  207.   def inv_maxed?
  208.     inv_max <= total_inv_size
  209.   end
  210.  
  211.   def total_inv_size(print = false)
  212.     result = all_items.inject(0) {|total,item| total +
  213.       (item_number(item) * item.inv_size)}
  214.     puts result if print
  215.     result += members.inject(0) {|total,member| total + member.equip_size}
  216.     puts result if print
  217.     result
  218.   end
  219.  
  220.   alias theo_liminv_max_item max_item_number
  221.   def max_item_number(item)
  222.     $BTEST ? theo_liminv_max_item(item) : inv_max_item(item) + item_number(item)
  223.   end
  224.  
  225.   def inv_max_item(item)
  226.     return 9999999 if item.nil? || item.inv_size == 0
  227.     free_slot / item.inv_size
  228.   end
  229.  
  230.   def free_slot
  231.     inv_max - total_inv_size
  232.   end
  233.  
  234.   alias theo_liminv_item_max? item_max?
  235.   def item_max?(item)
  236.     $BTEST ? theo_liminv_item_max?(item) : inv_maxed?
  237.   end
  238.  
  239.   def near_maxed?
  240.     free_slot.to_f / inv_max <= THEO::LimInv::NearMaxed_Percent/100.0
  241.   end
  242.  
  243.   def item_size(item)
  244.     return 0 unless item
  245.     item.inv_size * item_number(item)
  246.   end
  247.  
  248.   def force_gain_item(item, amount)
  249.     container = item_container(item.class)
  250.     return unless container
  251.     last_number = item_number(item)
  252.     new_number = last_number + amount
  253.     container[item.id] = [new_number, 0].max
  254.     container.delete(item.id) if container[item.id] == 0
  255.     $game_map.need_refresh = true
  256.   end
  257.  
  258. end
  259.  
  260. class Game_Interpreter
  261.   def force_gain_item(item, amount)
  262.     $game_party.force_gain_item(item, amount)
  263.   end
  264. end
  265. # ============================================================================
  266. # Window related class goes here
  267. # ============================================================================
  268. class Window_Base < Window
  269.   def draw_inv_slot(x,y,width = contents.width,align = 2)
  270.     txt = sprintf("%d/%d",$game_party.total_inv_size, $game_party.inv_max)
  271.     color = THEO::LimInv::NearMaxed_Color
  272.     if $game_party.near_maxed?
  273.       change_color(text_color(color))
  274.     else
  275.       change_color(normal_color)
  276.     end
  277.     draw_text(x,y,width,line_height,txt,align)
  278.     change_color(normal_color)
  279.   end
  280.  
  281.   def draw_inv_info(x,y,width = contents.width)
  282.     change_color(system_color)
  283.     draw_text(x,y,width,line_height,THEO::LimInv::InvSlotVocab)
  284.     change_color(normal_color)
  285.     draw_inv_slot(x,y,width)
  286.   end
  287.  
  288.   def draw_item_size(item,x,y,total = true,width = contents.width)
  289.     rect = Rect.new(x,y,width,line_height)
  290.     change_color(system_color)
  291.     draw_text(rect,THEO::LimInv::InvSizeVocab)
  292.     change_color(normal_color)
  293.     number = (THEO::LimInv::DrawTotal_Size && total) ?
  294.       $game_party.item_size(item) : item.nil? ? 0 : item.inv_size
  295.     draw_text(rect,number,2)
  296.   end
  297. end
  298.  
  299. class Window_MenuLimInv < Window_Base
  300.  
  301.   def initialize(width)
  302.     super(0,0,width,fitting_height(1))
  303.     refresh
  304.   end
  305.  
  306.   def refresh
  307.     contents.clear
  308.     change_color(system_color)
  309.     txt = THEO::LimInv::SlotVocabShort
  310.     draw_text(0,0,contents.width,line_height,txt)
  311.     draw_inv_slot(0,0)
  312.   end
  313.  
  314. end
  315.  
  316. class Window_ItemSize < Window_Base
  317.  
  318.   def initialize(x,y,width)
  319.     super(x,y,width,fitting_height(1))
  320.   end
  321.  
  322.   def set_item(item)
  323.     @item = item
  324.     refresh
  325.   end
  326.  
  327.   def refresh
  328.     contents.clear
  329.     draw_item_size(@item,0,0)
  330.   end
  331.  
  332. end
  333.  
  334. class Window_FreeSlot < Window_Base
  335.   def initialize(x,y,width)
  336.     super(x,y,width,fitting_height(1))
  337.     refresh
  338.   end
  339.  
  340.   def refresh
  341.     contents.clear
  342.     draw_inv_info(0,0)
  343.   end
  344. end
  345.  
  346. class Window_ItemUseCommand < Window_Command
  347.   include THEO::LimInv
  348.  
  349.   def initialize
  350.     super(0,0)
  351.     self.openness = 0
  352.   end
  353.  
  354.   def set_item(item)
  355.     @item = item
  356.     refresh
  357.   end
  358.  
  359.   def window_width
  360.     UseCommand_Size
  361.   end
  362.  
  363.   def make_command_list
  364.     add_command(UseVocab, :use, $game_party.usable?(@item))
  365.     add_command(DiscardVocab, :discard, discardable?(@item))
  366.     add_command(CancelVocab, :cancel)
  367.   end
  368.  
  369.   def to_center
  370.     self.x = Graphics.width/2 - width/2
  371.     self.y = Graphics.height/2 - height/2
  372.   end
  373.  
  374.   def discardable?(item)
  375.     return false if item.nil?
  376.     !(item.is_a?(RPG::Item) && item.itype_id == 2)
  377.   end
  378.  
  379. end
  380.  
  381. class Window_DiscardAmount < Window_Base
  382.   attr_accessor :cmn_window
  383.   attr_accessor :itemlist
  384.   attr_accessor :freeslot
  385.  
  386.   def initialize(x,y,width)
  387.     super(x,y,width,fitting_height(1))
  388.     self.openness = 0
  389.     @amount = 0
  390.   end
  391.  
  392.   def set_item(item)
  393.     @item = item
  394.     @amount = 0
  395.     refresh
  396.   end
  397.  
  398.   def refresh
  399.     contents.clear
  400.     return unless @item
  401.     draw_item_name(@item,0,0,true,contents.width)
  402.     txt = sprintf("%d/%d",@amount, $game_party.item_number(@item))
  403.     draw_text(0,0,contents.width,line_height,txt,2)
  404.   end
  405.  
  406.   def draw_item_name(item, x, y, enabled = true, width = 172)
  407.     return unless item
  408.     draw_icon(item.icon_index, x, y, enabled)
  409.     change_color(normal_color, enabled)
  410.     draw_text(x + 24, y, width, line_height, item.name + ":")
  411.   end
  412.  
  413.   def update
  414.     super
  415.     return unless open?
  416.     change_amount(1) if Input.repeat?(:RIGHT)
  417.     change_amount(-1) if Input.repeat?(:LEFT)
  418.     change_amount(10) if Input.repeat?(:UP)
  419.     change_amount(-10) if Input.repeat?(:DOWN)
  420.     lose_item if Input.trigger?(:C)
  421.     close_window if Input.trigger?(:B)
  422.   end
  423.  
  424.   def change_amount(num)
  425.     @amount = [[@amount+num,0].max,$game_party.item_number(@item)].min
  426.     Sound.play_cursor
  427.     refresh
  428.   end
  429.  
  430.   def lose_item
  431.     $game_party.lose_item(@item,@amount)
  432.     @itemlist.redraw_current_item
  433.     @freeslot.refresh
  434.     if $game_party.item_number(@item) == 0
  435.       @itemlist.refresh
  436.     end
  437.     close_window
  438.   end
  439.  
  440.   def close_window
  441.     close
  442.     @cmn_window.activate
  443.     Sound.play_ok
  444.   end
  445.  
  446. end
  447.  
  448. class Window_ItemList < Window_Selectable
  449.   attr_reader :item_size_window
  450.  
  451.   def item_size_window=(window)
  452.     @item_size_window = window
  453.     update_help
  454.   end
  455.  
  456.   alias theo_liminv_update_help update_help
  457.   def update_help
  458.     theo_liminv_update_help
  459.     @item_size_window.set_item(item) if @item_size_window
  460.   end
  461.  
  462.   alias theo_liminv_height= height=
  463.   def height=(height)
  464.     self.theo_liminv_height = height
  465.     refresh
  466.   end
  467.  
  468.   def enable?(item)
  469.     return !item.nil?
  470.   end
  471.  
  472. end
  473.  
  474. class Window_ShopNumber < Window_Selectable
  475.   attr_accessor :mode
  476.  
  477.   alias theo_liminv_init initialize
  478.   def initialize(x, y, height)
  479.     theo_liminv_init(x, y, height)
  480.     @mode = :buy
  481.   end
  482.  
  483.   alias theo_liminv_refresh refresh
  484.   def refresh
  485.     theo_liminv_refresh
  486.     draw_itemsize
  487.   end
  488.  
  489.   def draw_itemsize
  490.     item_size = @number * @item.inv_size
  491.     total_size = $game_party.total_inv_size +
  492.       (@mode == :buy ? item_size : -item_size)
  493.     txt = sprintf("%d/%d",total_size,$game_party.inv_max)
  494.     ypos = item_y + line_height * ($imported["YEA-ShopOptions"] ? 5 : 4)
  495.     rect = Rect.new(4,ypos,contents.width-8,line_height)
  496.     change_color(system_color)
  497.     draw_text(rect,THEO::LimInv::InvSlotVocab)
  498.     change_color(normal_color)
  499.     draw_text(rect,txt,2)
  500.   end
  501.  
  502. end
  503.  
  504. class Window_ShopStatus < Window_Base
  505.  
  506.   if THEO::LimInv::Display_ItemSize
  507.   alias theo_liminv_draw_posses draw_possession
  508.   def draw_possession(x, y)
  509.     theo_liminv_draw_posses(x,y)
  510.     y += line_height
  511.     draw_item_size(@item,x,y,false, contents.width-(x*2))
  512.   end
  513.  
  514.   if $imported["YEA-ShopOptions"]
  515.   def draw_actor_equip_info(dx, dy, actor)
  516.     dy += line_height
  517.     enabled = actor.equippable?(@item)
  518.     change_color(normal_color, enabled)
  519.     draw_text(dx, dy, contents.width, line_height, actor.name)
  520.     item1 = current_equipped_item(actor, @item.etype_id)
  521.     draw_actor_param_change(dx, dy, actor, item1) if enabled
  522.   end
  523.   end # $imported["YEA-ShopOption"]
  524.   end # Display item size
  525.  
  526. end
  527. # ============================================================================
  528. # Scene classes goes here
  529. # ============================================================================
  530. class Scene_Menu < Scene_MenuBase
  531.  
  532.   alias theo_liminv_start start
  533.   def start
  534.     theo_liminv_start
  535.     create_liminv_window
  536.   end
  537.  
  538.   def create_liminv_window
  539.     @lim_inv = Window_MenuLimInv.new(@gold_window.width)
  540.     @lim_inv.x = @command_window.x
  541.     @lim_inv.y = @command_window.height
  542.   end
  543.  
  544. end
  545.  
  546. class Scene_Item < Scene_ItemBase
  547.  
  548.   alias theo_liminv_start start
  549.   def start
  550.     theo_liminv_start
  551.     resize_item_window
  552.     create_freeslot_window
  553.     create_itemsize_window
  554.     create_usecommand_window
  555.     create_discard_amount
  556.   end
  557.  
  558.   def resize_item_window
  559.     @item_window.height -= @item_window.line_height * 2
  560.   end
  561.  
  562.   def create_freeslot_window
  563.     wy = @item_window.y + @item_window.height
  564.     wh = THEO::LimInv::Display_ItemSize ? Graphics.width/2 : Graphics.width
  565.     @freeslot = Window_FreeSlot.new(0,wy,wh)
  566.     @freeslot.viewport = @viewport
  567.   end
  568.  
  569.   def create_itemsize_window
  570.     return unless THEO::LimInv::Display_ItemSize
  571.     wx = @freeslot.width
  572.     wy = @freeslot.y
  573.     ww = wx
  574.     @itemsize = Window_ItemSize.new(wx,wy,ww)
  575.     @itemsize.viewport = @viewport
  576.     @item_window.item_size_window = @itemsize
  577.   end
  578.  
  579.   def create_usecommand_window
  580.     @use_command = Window_ItemUseCommand.new
  581.     @use_command.to_center
  582.     @use_command.set_handler(:use, method(:use_command_ok))
  583.     @use_command.set_handler(:discard, method(:on_discard_ok))
  584.     @use_command.set_handler(:cancel, method(:on_usecmd_cancel))
  585.     @use_command.viewport = @viewport
  586.   end
  587.  
  588.   def create_discard_amount
  589.     wx = @use_command.x
  590.     wy = @use_command.y + @use_command.height
  591.     ww = @use_command.width
  592.     @discard_window = Window_DiscardAmount.new(wx,wy,ww)
  593.     @discard_window.cmn_window = @use_command
  594.     @discard_window.itemlist = @item_window
  595.     @discard_window.freeslot = @freeslot
  596.     @discard_window.viewport = @viewport
  597.   end
  598.  
  599.   alias theo_liminv_item_ok on_item_ok
  600.   def on_item_ok
  601.     @use_command.set_item(item)
  602.     @use_command.open
  603.     @use_command.activate
  604.     @use_command.select(0)
  605.   end
  606.  
  607.   alias theo_liminv_use_item use_item
  608.   def use_item
  609.     @use_command.close
  610.     theo_liminv_use_item
  611.     @freeslot.refresh
  612.   end
  613.  
  614.   def use_command_ok
  615.     theo_liminv_item_ok
  616.     @use_command.close
  617.   end
  618.  
  619.   def on_discard_ok
  620.     @discard_window.set_item(item)
  621.     @discard_window.open
  622.   end
  623.  
  624.   def on_usecmd_cancel
  625.     @item_window.activate
  626.     @use_command.close
  627.     @use_command.deactivate
  628.   end
  629.  
  630. end
  631.  
  632. class Scene_Shop < Scene_MenuBase
  633.   alias theo_liminv_buy_ok on_buy_ok
  634.   def on_buy_ok
  635.     @number_window.mode = :buy
  636.     theo_liminv_buy_ok
  637.   end
  638.  
  639.   alias theo_liminv_sell_ok on_sell_ok
  640.   def on_sell_ok
  641.     @number_window.mode = :sell
  642.     theo_liminv_sell_ok
  643.   end
  644. end
Advertisement
Add Comment
Please, Sign In to add comment