Advertisement
khanhdu

Menu chế tạo

Apr 26th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 18.24 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Hệ Thống Chế Tạo
  3. # Version : 1.1b
  4. # Blogger : https://rpgmakervn.blogspot.com/
  5. # (Bản Này ko do tôi viết ra mà do tác gia theolizid viết ra )
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_Crafting] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.08.25 - Bugfix disable req text isn't working
  12. # 2013.08.22 - Bugfix at amount of item crafting
  13. #            - Added show condition
  14. #            - Added custom name
  15. # 2013.08.20 - Finished script
  16. # 2013.08.19 - Started script
  17. # =============================================================================
  18. =begin
  19.  
  20.   ---------------------------------------------------------------------------
  21.   Yêu Càu khi làm :
  22.   Am hiểu về biết gắn scirpt nhé
  23.  
  24.   ---------------------------------------------------------------------------
  25.   các khởi động :
  26.   ---------------------------------------------------------------------------
  27.   đây là bản tiếng việt 100%
  28.   vì vậy gọi lệnh khỏi động bằng cách dùng lệnh này  :
  29.  
  30.   enter_crafting
  31.  
  32.   ---------------------------------------------------------------------------
  33.   Khả năng làm việt vs (UntukItems/Armors/Weapons):
  34.   ---------------------------------------------------------------------------
  35.   -----------------------------------
  36.   Phần yêu cầu vật phẩm chế tạo ( bỏ vào note nhé )
  37.   -----------------------------------
  38.   <cong thuc vat pham: id vật phẩm,số lượng vật phẩm>
  39.   <cong thuc trang bi: id trang bị,số lượng trang bị>
  40.   <cong thuc vu khi: id vũ khi,số lượng vật phẩm>
  41.   ^
  42.   công thức yêu cầu
  43.  
  44.   <ten che tao: text>
  45.   ^
  46.   ten sau khi vat pham che tao
  47.  
  48.   -----------------------------------
  49.   Level : Medium - Hard
  50.   (kalo ga paham ga usah coba2 make)
  51.   -----------------------------------
  52.   <them lenh>
  53.   script
  54.   </them lenh>
  55.   ^
  56.   là khi bạn gắn cái này vào sẽ có yêu cầu các lệnh ( khuyên nên xem trên trang blogger
  57.   chung tôi cho dễ hiểu
  58.  
  59.   <nhap van ban>
  60.   nhap điền thông tin nhé
  61.   </nhap van ban>
  62.   ^
  63.   cho phép hiện thông tin craft
  64.  
  65.   <them lenh hoc>
  66.   script
  67.   </them lenh hoc>
  68.   ^
  69.  cái này cho phép bạn chèn cái scirpt vào vào giống như bạn phải chạy qua lệnh đó r
  70.  mới chế tạo đc
  71. =end
  72. # =============================================================================
  73. # Konfigurasi :
  74. # =============================================================================
  75. module THEO
  76.   module Craft
  77.  
  78.   # --------------------------------------------------------------------------
  79.   # Settingan umum
  80.   # --------------------------------------------------------------------------
  81.     MainMenu        = true
  82.   # Set true kalo kamu pengen dalam main menu bisa akses crafting
  83.  
  84.     DisplayReqText  = true
  85.   # Set false kalo kamu pengen ngilangin tulisan requirement sbelum resep
  86.   # di window status sebelah kanan
  87.  
  88.   # --------------------------------------------------------------------------
  89.   # Settingan vokab
  90.   # --------------------------------------------------------------------------
  91.     VocabMenu      = "Chế Tạo"          # Command buat main menu (kalo kamu make)
  92.     VocabNoReq     = "- Trống -"       # Kalo ngga ada requirement khusus
  93.     VocabRequire   = "Thông Tin:"  # Vokab requirement
  94.     VocabRecipes   = "Yêu Cầu : "     # Vokab resep
  95.     VocabInventory = "Đã Chế Được: "    # Vokab buat inventory
  96.    
  97.   # --------------------------------------------------------------------------
  98.   # Lainnya
  99.   # --------------------------------------------------------------------------
  100.     Text_Buffer_x = 12
  101.   # Jarak tambahan text dari samping kiri
  102.  
  103.     AmountSize = 250
  104.   # Lebar window amount
  105.  
  106.   end
  107. end
  108. # =============================================================================
  109. # Akhir dari konfig :
  110. # =============================================================================
  111. module THEO
  112.   module Craft
  113.     def self.items
  114.       items = $data_items + $data_armors + $data_weapons
  115.       items.compact!
  116.       items.select {|item| item.can_be_crafted? }
  117.     end
  118.   end
  119. end
  120.  
  121. class RPG::BaseItem
  122.   attr_accessor :item_recipes
  123.   attr_accessor :armor_recipes
  124.   attr_accessor :weapon_recipes
  125.   attr_accessor :enable_cond
  126.   attr_accessor :enable_text
  127.   attr_accessor :show_cond
  128.   attr_accessor :craft_name
  129.   attr_accessor :craft_key  # For future planned feature
  130.  
  131.   def load_recipes
  132.     @item_recipes = {}
  133.     @armor_recipes = {}
  134.     @weapon_recipes = {}
  135.     @show_cond = @enable_cond = "true"
  136.     @enable_text = []
  137.     @craft_name = ""
  138.     @craft_key = ""
  139.     read_craft_cond = read_craft_txt = read_craft_show = false
  140.     self.note.split(/[\r\n]+/).each do |line|
  141.       case line
  142.       when /<(?:ADD_ITEM_RECIPE|cong thuc vat pham): (.*)>/i
  143.         str = $1.scan(/\d+/)
  144.         @item_recipes[str[0].to_i] = str[1].to_i
  145.       when /<(?:ADD_ARMOR_RECIPE|cong thuc trang bi): (.*)>/i
  146.         str = $1.scan(/\d+/)
  147.         @armor_recipes[str[0].to_i] = str[1].to_i
  148.       when /<(?:ADD_WEAPON_RECIPE|cong thuc vu khi): (.*)>/i
  149.         str = $1.scan(/\d+/)
  150.         @weapon_recipes[str[0].to_i] = str[1].to_i
  151.       when /<(?:CRAFT_REQUIRE|Them lenh)>/i
  152.         read_craft_cond = true
  153.         @enable_cond = ""
  154.       when /<\/(?:CRAFT_REQUIRE|them lenh)>/i
  155.         read_craft_cond = false
  156.       when /<(?:CRAFT_REQ_TEXT|nhap van ban)>/i
  157.         read_craft_txt = true
  158.       when /<\/(?:CRAFT_REQ_TEXT|nhap van ban)>/i
  159.         read_craft_txt = false
  160.       when /<(?:CRAFT_SHOW_EVAL|them lenh hoc)>/i
  161.         read_craft_show = true
  162.         @show_cond = ""
  163.       when /<\/(?:CRAFT_SHOW_EVAL|them lenh hoc)>/i
  164.         read_craft_show = false
  165.       when /<(?:CRAFT_NAME|ten che tao): (.*)>/i
  166.         @craft_name = $1.to_s
  167.       when /<(?:CRAFT_KEY|craft key): [ ]*(.*)>/i
  168.         @craft_key = $1.to_s
  169.       else
  170.         if read_craft_cond
  171.           @enable_cond += line
  172.         end
  173.         if read_craft_txt
  174.           @enable_text.push(line)
  175.         end
  176.         if read_craft_show
  177.           @show_cond += line
  178.         end
  179.       end
  180.     end
  181.   end
  182.  
  183.   def recipes
  184.     [@item_recipes, @armor_recipes, @weapon_recipes]
  185.   end
  186.  
  187.   # ---------------------------------------------------------------------------
  188.   # If the error is directed you to this line. It's possible that you wrote
  189.   # craft requirement formula in a wrong way. Not this script mistake
  190.   # ---------------------------------------------------------------------------
  191.   def craft_possible?
  192.     return eval(@enable_cond) && can_be_crafted?
  193.   end
  194.  
  195.   def show_possible?
  196.     return eval(@show_cond)
  197.   end
  198.   # ---------------------------------------------------------------------------
  199.   # Determine if item can be crafted
  200.   # ---------------------------------------------------------------------------
  201.   def can_be_crafted?
  202.     recipes.any? {|recipe| !recipe.empty?} && show_possible?
  203.   end
  204.  
  205. end
  206.  
  207. class << DataManager
  208.  
  209.   alias theo_recipe_craft_load_db load_database
  210.   def load_database
  211.     theo_recipe_craft_load_db
  212.     load_item_recipes
  213.   end
  214.  
  215.   def load_item_recipes
  216.     ($data_items+$data_armors+$data_weapons).compact.each do |db|
  217.       db.load_recipes
  218.     end
  219.   end
  220.  
  221. end
  222.  
  223. class Window_CraftList < Window_ItemList
  224.  
  225.   def initialize(x,y,w,h,key)
  226.     @key = key
  227.     super(x,y,w,h)
  228.     refresh
  229.   end
  230.  
  231.   def col_max
  232.     return 1
  233.   end
  234.  
  235.   def status_window=(window)
  236.     @status_window = window
  237.     update_help
  238.   end
  239.  
  240.   def item_number_window=(window)
  241.     @inum_window = window
  242.     update_help
  243.   end
  244.  
  245.   def update_help
  246.     super
  247.     @status_window.set_item(item) if @status_window
  248.     @inum_window.set_item(item) if @inum_window
  249.   end
  250.  
  251.   def make_item_list
  252.     @data = []
  253.     items = THEO::Craft.items
  254.     items.each do |item|
  255.       @data.push(item) if @key.empty? || item.craft_key == @key
  256.     end
  257.   end
  258.  
  259.   def enable?(item)
  260.     item.craft_possible? && has_recipes?(item) && has_free_slot?(item)
  261.   end
  262.  
  263.   def has_free_slot?(item)
  264.     $game_party.item_number(item) < $game_party.max_item_number(item)
  265.   end
  266.  
  267.   def has_recipes?(item)
  268.     result1 = check_recipe(item.recipes[0], $data_items)
  269.     result2 = check_recipe(item.recipes[1], $data_armors)
  270.     result3 = check_recipe(item.recipes[2], $data_weapons)
  271.     return result1 && result2 && result3
  272.   end
  273.  
  274.   def check_recipe(recipes, data)
  275.     return true if recipes.empty?
  276.     return recipes.all? {|recipe|
  277.       $game_party.item_number(data[recipe[0]]) >= recipe[1]}
  278.   end
  279.  
  280.   def draw_item_name(item, x, y, enabled = true, width = 172)
  281.     return unless item
  282.     draw_icon(item.icon_index, x, y, enabled)
  283.     change_color(normal_color, enabled)
  284.     name = item.craft_name.empty? ? item.name : item.craft_name
  285.     draw_text(x + 24, y, width, line_height, name)
  286.   end
  287.  
  288.   def draw_item_number(rect, item)
  289.     return unless $imported[:Theo_LimInventory]
  290.     return unless THEO::LimInv::Display_ItemSize
  291.     draw_text(rect, sprintf(":%2d", item.inv_size), 2)
  292.   end
  293.  
  294. end
  295.  
  296. class Window_CraftStatus < Window_Base
  297.  
  298.   def set_item(item)
  299.     @item = item
  300.     refresh
  301.   end
  302.  
  303.   def refresh
  304.     contents.clear
  305.     return unless @item
  306.     @line_count = 0
  307.     draw_requirement
  308.     draw_recipes
  309.   end
  310.  
  311.   def draw_requirement
  312.     return unless THEO::Craft::DisplayReqText
  313.     change_color(system_color)
  314.     draw_text(line_rect(4),THEO::Craft::VocabRequire)
  315.     increase_line_count
  316.     if !@item.enable_text.empty?
  317.       change_color(normal_color, @item.craft_possible?)
  318.       @item.enable_text.each do |text|
  319.         draw_text(line_rect(buff_x),text)
  320.         increase_line_count
  321.       end
  322.       increase_line_count
  323.     else
  324.       change_color(normal_color)
  325.       draw_text(line_rect(buff_x),THEO::Craft::VocabNoReq)
  326.       2.times { increase_line_count }
  327.     end
  328.   end
  329.  
  330.   def draw_recipes
  331.     change_color(system_color)
  332.     draw_text(line_rect(4),THEO::Craft::VocabRecipes)
  333.     change_color(normal_color)
  334.     increase_line_count
  335.     draw_recipe(@item.recipes[0], $data_items)
  336.     draw_recipe(@item.recipes[1], $data_armors)
  337.     draw_recipe(@item.recipes[2], $data_weapons)
  338.   end
  339.  
  340.   def draw_recipe(hash, items)
  341.     hash.each do |id,amount|
  342.       item_num = $game_party.item_number(items[id])
  343.       enable = item_num >= amount
  344.       change_color(normal_color, enable)
  345.       rect = line_rect(buff_x + 24)
  346.       text = sprintf("%s (%d/%d)",items[id].name, item_num, amount)
  347.       draw_text(rect, text)
  348.       draw_icon(items[id].icon_index, buff_x, @line_count, enable)
  349.       increase_line_count
  350.     end
  351.   end  
  352.  
  353.   def line_rect(xpos = 0)
  354.     Rect.new(xpos,@line_count,contents.width-xpos,line_height)
  355.   end
  356.  
  357.   def increase_line_count
  358.     @line_count += line_height
  359.   end
  360.  
  361.   def buff_x
  362.     THEO::Craft::Text_Buffer_x
  363.   end
  364.  
  365. end
  366.  
  367. class Window_CraftAmount < Window_Base
  368.   attr_accessor :inv_window
  369.   attr_accessor :ok_handler
  370.   attr_accessor :cancel_handler
  371.   attr_reader :amount
  372.  
  373.   def initialize
  374.     super(0,0,window_width,fitting_height(1))
  375.     self.openness = 0
  376.     to_center
  377.     @amount = 0
  378.     @max = 0
  379.     @item = nil
  380.   end
  381.  
  382.   def window_width
  383.     return THEO::Craft::AmountSize
  384.   end
  385.  
  386.   def to_center
  387.     self.x = (Graphics.width - width)/2
  388.     self.y = (Graphics.height - height)/2
  389.   end
  390.  
  391.   def set(item, max)
  392.     change_amount(0,true)
  393.     @item = item
  394.     @max = max
  395.     open
  396.     refresh
  397.   end
  398.  
  399.   def refresh
  400.     contents.clear
  401.     return unless @item
  402.     draw_item_name(@item,0,0,true,contents.width)
  403.     draw_amount
  404.   end
  405.  
  406.   def draw_amount
  407.     text = sprintf("%d/%d",@amount,@max)
  408.     draw_text(0,0,contents.width,line_height,text,2)
  409.   end
  410.  
  411.   def update
  412.     super
  413.     if open?
  414.       change_amount(1) if Input.repeat?(:RIGHT)
  415.       change_amount(-1) if Input.repeat?(:LEFT)
  416.       change_amount(10) if Input.repeat?(:UP)
  417.       change_amount(-10) if Input.repeat?(:DOWN)
  418.       call_ok if Input.trigger?(:C)
  419.       call_cancel if Input.trigger?(:B)
  420.     end
  421.   end
  422.  
  423.   def change_amount(number, instant = false)
  424.     if instant
  425.       @amount = number
  426.     else
  427.       Sound.play_cursor
  428.       @amount = [[@amount + number,0].max,@max].min
  429.     end
  430.     refresh
  431.     @inv_window.amount = @amount if @inv_window
  432.   end
  433.  
  434.   def call_ok
  435.     Sound.play_ok
  436.     ok_handler.call
  437.     change_amount(0,true)
  438.     close
  439.   end
  440.  
  441.   def call_cancel
  442.     Sound.play_cancel
  443.     cancel_handler.call
  444.     change_amount(0,true)
  445.     close
  446.   end
  447.  
  448. end
  449.  
  450. class Window_CraftFooter < Window_Base
  451.  
  452.   def initialize
  453.     super(0,0,Graphics.width,fitting_height(1))
  454.     self.y = Graphics.height - height
  455.     @amount = 0
  456.     @item = nil
  457.     refresh
  458.   end
  459.  
  460.   def amount=(amount)
  461.     @amount = amount
  462.     refresh
  463.   end
  464.  
  465.   def refresh
  466.     contents.clear
  467.     change_color(system_color)
  468.     draw_text(0,0,contents.width,line_height,THEO::Craft::VocabInventory)
  469.     change_color(normal_color)
  470.     return unless @item
  471.     if $imported[:Theo_LimInventory]
  472.       draw_inv_slot(0,0)
  473.     else
  474.       draw_item_number
  475.     end
  476.   end
  477.  
  478.   def draw_inv_slot(x,y,width = contents.width,align = 2)
  479.     total = $game_party.total_inv_size + @amount*@item.inv_size
  480.     total -= calculate_lose_item
  481.     txt = sprintf("%d/%d", total, $game_party.inv_max)
  482.     color = THEO::LimInv::NearMaxed_Color
  483.     near_max = total.to_f/$game_party.inv_max >=
  484.       (100 - THEO::LimInv::NearMaxed_Percent).to_f / 100
  485.     if near_max
  486.       change_color(text_color(color))
  487.     else
  488.       change_color(normal_color)
  489.     end
  490.     draw_text(x,y,width,line_height,txt,align)
  491.     change_color(normal_color)
  492.   end
  493.  
  494.   def draw_item_number
  495.     txt = sprintf("%d/%d",@amount + $game_party.item_number(@item),
  496.       $game_party.max_item_number(@item))
  497.     draw_text(0,0,contents.width,line_height,txt,2)
  498.   end
  499.  
  500.   def set_item(item)
  501.     @item = item
  502.     refresh
  503.   end
  504.  
  505.   def calculate_lose_item
  506.     result = 0
  507.     @item.recipes[0].each do |id,amount|
  508.       data = $data_items[id]
  509.       amount *= @amount
  510.       result += amount
  511.     end
  512.     @item.recipes[1].each do |id,amount|
  513.       data = $data_armors[id]
  514.       amount *= @amount
  515.       result += amount
  516.     end
  517.     @item.recipes[2].each do |id,amount|
  518.       data = $data_weapons[id]
  519.       amount *= @amount
  520.       result += amount
  521.     end
  522.     return result
  523.   end
  524.  
  525. end
  526.  
  527. class Game_Interpreter
  528.  
  529.   # ----------------------------------------------
  530.   # key is just for my future planned feature
  531.   # ----------------------------------------------
  532.   def enter_crafting(key = "")
  533.     SceneManager.call(Scene_ItemCrafting)
  534.     SceneManager.scene.prepare(key)
  535.     Fiber.yield
  536.   end
  537.  
  538. end
  539.  
  540. class Window_MenuCommand < Window_Command
  541.  
  542.   alias theo_craft_ori_cmd add_original_commands
  543.   def add_original_commands
  544.     theo_craft_ori_cmd
  545.     return unless THEO::Craft::MainMenu
  546.     add_command(THEO::Craft::VocabMenu, :craft)
  547.   end
  548.  
  549. end
  550.  
  551. class Scene_Menu < Scene_MenuBase
  552.  
  553.   alias theo_craft_cmnd_window create_command_window
  554.   def create_command_window
  555.     theo_craft_cmnd_window
  556.     @command_window.set_handler(:craft, method(:enter_crafting))
  557.   end
  558.  
  559.   def enter_crafting
  560.     SceneManager.call(Scene_ItemCrafting)
  561.     SceneManager.scene.prepare("")
  562.   end
  563.  
  564. end
  565.  
  566. class Scene_ItemCrafting < Scene_MenuBase
  567.  
  568.   def prepare(key)
  569.     @key = key
  570.   end
  571.  
  572.   def start
  573.     super
  574.     create_help_window
  575.     create_craftfooter_window
  576.     create_craftlist_window
  577.     create_craftstatus_window
  578.     create_craftamount_window
  579.   end
  580.  
  581.   def create_craftfooter_window
  582.     @craft_footer = Window_CraftFooter.new
  583.   end
  584.  
  585.   def create_craftlist_window
  586.     wy = @help_window.height
  587.     ww = Graphics.width/2
  588.     wh = Graphics.height - wy - @craft_footer.height
  589.     @craft_list = Window_CraftList.new(0,wy,ww,wh,@key)
  590.     @craft_list.set_handler(:ok, method(:on_craft_ok))
  591.     @craft_list.set_handler(:cancel, method(:return_scene))
  592.     @craft_list.help_window = @help_window
  593.     @craft_list.item_number_window = @craft_footer
  594.     @craft_list.activate
  595.     @craft_list.select(0)
  596.   end
  597.  
  598.   def create_craftstatus_window
  599.     wx = @craft_list.width
  600.     wy = @craft_list.y
  601.     ww = @craft_list.width
  602.     wh = @craft_list.height
  603.     @craft_status = Window_CraftStatus.new(wx,wy,ww,wh)
  604.     @craft_list.status_window = @craft_status
  605.   end
  606.  
  607.   def create_craftamount_window
  608.     @craft_amount = Window_CraftAmount.new
  609.     @craft_amount.ok_handler = method(:on_amount_ok)
  610.     @craft_amount.cancel_handler = method(:on_amount_cancel)
  611.     @craft_amount.inv_window = @craft_footer
  612.   end
  613.  
  614.   def on_craft_ok
  615.     @craft_amount.set(item, item_max)
  616.   end
  617.  
  618.   def on_amount_ok
  619.     $game_party.gain_item(item, @craft_amount.amount)
  620.     lose_recipes
  621.     @craft_status.refresh
  622.     @craft_list.refresh
  623.     @craft_list.activate
  624.   end
  625.  
  626.   def on_amount_cancel
  627.     @craft_list.activate
  628.   end
  629.  
  630.   def item
  631.     @craft_list.item
  632.   end
  633.  
  634.   def item_max
  635.     ary = []
  636.     item.recipes[0].each do |id,amount|
  637.       data = $data_items[id]
  638.       ary.push($game_party.item_number(data)/amount)
  639.     end
  640.     item.recipes[1].each do |id,amount|
  641.       data = $data_armors[id]
  642.       ary.push($game_party.item_number(data)/amount)
  643.     end
  644.     item.recipes[2].each do |id,amount|
  645.       data = $data_weapons[id]
  646.       ary.push($game_party.item_number(data)/amount)
  647.     end
  648.     max_item = $game_party.max_item_number(item)
  649.     inum = $game_party.item_number(item)
  650.     return [ary.min , max_item - inum].min
  651.   end
  652.  
  653.   def lose_recipes
  654.     item.recipes[0].each do |id,amount|
  655.       data = $data_items[id]
  656.       amount *= @craft_amount.amount
  657.       $game_party.lose_item(data,amount)
  658.     end
  659.     item.recipes[1].each do |id,amount|
  660.       data = $data_armors[id]
  661.       amount *= @craft_amount.amount
  662.       $game_party.lose_item(data,amount)
  663.     end
  664.     item.recipes[2].each do |id,amount|
  665.       data = $data_weapons[id]
  666.       amount *= @craft_amount.amount
  667.       $game_party.lose_item(data,amount)
  668.     end
  669.   end
  670.  
  671. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement