Scinaya

Item_Exchange Shop (Aero GP's Toolbox)

Apr 28th, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.72 KB | None | 0 0
  1. #==============================================================================
  2. # ** ExCommand_BarterShop ~ Item Version of Exchange Shop ~ (MACKIE)
  3. #------------------------------------------------------------------------------
  4. #  This script allows you to exchange specific items, such as "coins" or
  5. # "medals," for other items when running [Shop Processing].
  6. #==============================================================================
  7.  
  8. # The ID of the switch used to enable the exchange shop.
  9. # When set to ON, [Shop Processing] will replace the regular shop with the
  10. # exchange shop ("Purchase Only" is also available here). This switch is turned
  11. # OFF automatically after the player is finished shopping.
  12. EXCMD_BTRSHOP_SID = 26
  13.  
  14. # Exchange shop command names.
  15. # From left to right: [Buy] [Sell] [Finished]
  16. EXCMD_BTRSHOP_VOCAB = ["Exchange", "Sell", "Finished"]
  17.  
  18. # The exchange "item" used by the exchange shop.
  19. # Specify the item representing shop "credits" here.
  20. # Format is [Item ID, term for "credits", exchange rate]
  21. # Item price = original price ÷ exchange rate, rounded to 1.
  22. # (Example) If the exchange rate is 100, and an item costs 1650G normally, you
  23. #           will need to exchange 17 "credits" (100G each) to obtain the item.
  24. EXCMD_BTRSHOP_ITEM = [31, "A", 100]
  25.  
  26. # Notebox string identifier for specifying a product's exchange rate.
  27. # Items without this designation will use the default exchange rate above.
  28. # (For more information, see below)
  29. EXCMD_BTRSHOP_SIGNATURE = "*BARTER_RATE"
  30.  
  31. # The ID of the variable that will store the shop identification number.
  32. # When an ID is assigned to this variable, [Shop Processing] will adjust the
  33. # exchange rate set per item, per shop. This way, it is possible for the same
  34. # item to have separate rates at each shop. (For more information, see below)
  35. EXCMD_BTRSHOP_VID = 11
  36.  
  37. # * Setting the exchange rate of items:
  38. # Format is "EXCMD_BTRSHOP_SIGNATURE [exchange rate] [shop ID: exchange rate]"
  39. # "|" acts aa a separator for each shop ID - exchange rate pairing.
  40. # (Example 1) *BARTER_RATE[20] => the item's exchange rate is 20.
  41. # (Example 2) *BARTER_RATE[1:20|2:15|3:25] => the item's exchange rate is
  42. #             20 in shop 1, 15 in shop 2, and 25 in shop 3
  43. # (Example 3) *BARTER_RATE[20][2:10|3:15] => the item's exchange rate is
  44. #             10 in shop 2, 15 in shop 3, and 20 anywhere else.
  45. #
  46. # Priority is set as follows: shop ID rate > item rate > default shop rate
  47.  
  48. # * Event procedure for creating the exchange shop:
  49. # 1) Set the exchange rate for individual items, if necessary.
  50. # 2) To create an exchange shop, run the following operations:
  51. #   * Control Switches: [EXCMD_BTRSHOP_SID] = ON
  52. #   * Control Variables: [EXCMD_BTRSHOP_VID] = exchange shop ID (if used)
  53. #   * [Shop Processing...] (select items)
  54.  
  55. #------------------------------------------------------------------------------
  56.  
  57. class Game_Party
  58.   #--------------------------------------------------------------------------
  59.   # * Get Exchange Item
  60.   #--------------------------------------------------------------------------
  61.   def barter_item
  62.     return $data_items[EXCMD_BTRSHOP_ITEM[0]]
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # * Get Exchange Item Number
  66.   #--------------------------------------------------------------------------
  67.   def barter_item_number
  68.     return $game_party.item_number(barter_item)
  69.   end
  70.   #--------------------------------------------------------------------------
  71.   # * Get Exchange Rate
  72.   #--------------------------------------------------------------------------
  73.   def barter_rate(item)
  74.     rate = 0
  75.     shop_id = $game_variables[EXCMD_BTRSHOP_VID]
  76.     sig = EXCMD_BTRSHOP_SIGNATURE
  77.     reg = /#{Regexp.quote sig}(?:\[\d+\])?\[((\d+:\d+\|?)+)\]/
  78.     note = item.note.clone
  79.     note.gsub!(/\r\n/, "")
  80.     if shop_id > 0 and note.scan(reg).to_a[0]
  81.       for s in $~[1].split(/\s*\|\s*/)
  82.         r = s.split(/\s*:\s*/)
  83.         rate = r[1].to_i if r[0].to_i == shop_id
  84.         break if rate > 0
  85.       end
  86.     end
  87.     if rate == 0
  88.       if note[/#{Regexp.quote sig}\[(\d+)\]/].to_a[0]
  89.         rate = $1.to_i
  90.       else
  91.         rate = (item.price / EXCMD_BTRSHOP_ITEM[2]).round
  92.       end
  93.     end
  94.     return rate > 0 ? rate : 1
  95.   end
  96. end
  97.  
  98. class Window_Base
  99.   alias _excbshop_initialize initialize unless $@
  100.   #--------------------------------------------------------------------------
  101.   # * Object Initialization (Definition added)
  102.   #     x      : window x-coordinate
  103.   #     y      : window y-coordinate
  104.   #     width  : window width
  105.   #     height : window height
  106.   #--------------------------------------------------------------------------
  107.   def initialize(x, y, width, height)
  108.     _excbshop_initialize(x, y, width, height)
  109.     self.currency = Vocab::gold
  110.   end
  111.   #--------------------------------------------------------------------------
  112.   # * Set Currency
  113.   #--------------------------------------------------------------------------
  114.   def currency=(currency)
  115.     @currency = currency
  116.   end
  117.   #--------------------------------------------------------------------------
  118.   # * Determine if the Currency Value is Gold
  119.   #--------------------------------------------------------------------------
  120.   def gold?
  121.     return @currency == Vocab::gold
  122.   end
  123.   #--------------------------------------------------------------------------
  124.   # * Draw number with currency unit (Redefined)
  125.   #     value : Number (gold, etc)
  126.   #     x     : draw spot x-coordinate
  127.   #     y     : draw spot y-coordinate
  128.   #     width : Width
  129.   #--------------------------------------------------------------------------
  130.   def draw_currency_value(value, x, y, width)
  131.     cx = contents.text_size(@currency).width
  132.     self.contents.font.color = normal_color
  133.     self.contents.draw_text(x, y, width - cx - 2, WLH, value, 2)
  134.     self.contents.font.color = system_color
  135.     self.contents.draw_text(x, y, width, WLH, @currency, 2)
  136.   end
  137. end
  138.  
  139. class Window_Gold
  140.   alias _excbshop_refresh refresh unless $@
  141.   #--------------------------------------------------------------------------
  142.   # * Refresh (Definition added)
  143.   #--------------------------------------------------------------------------
  144.   def refresh
  145.     _excbshop_refresh
  146.     if $game_switches[EXCMD_BTRSHOP_SID]
  147.       self.contents.clear
  148.       value = gold? ? $game_party.gold : $game_party.barter_item_number
  149.       draw_currency_value(value, 4, 0, 120)
  150.     end
  151.   end
  152. end
  153.  
  154. class Window_ShopBuy
  155.   alias _excbshop_refresh refresh unless $@
  156.   alias _excbshop_draw_item draw_item unless $@
  157.   #--------------------------------------------------------------------------
  158.   # * Refresh (Definition added)
  159.   #--------------------------------------------------------------------------
  160.   def refresh
  161.     unless $game_switches[EXCMD_BTRSHOP_SID]
  162.       _excbshop_refresh
  163.     else
  164.       @data = []
  165.       for goods_item in @shop_goods
  166.         case goods_item[0]
  167.         when 0
  168.           item = $data_items[goods_item[1]]
  169.         when 1
  170.           item = $data_weapons[goods_item[1]]
  171.         when 2
  172.           item = $data_armors[goods_item[1]]
  173.         end
  174.         if item != nil and item != $game_party.barter_item # No exchange items
  175.           @data.push(item)
  176.         end
  177.       end
  178.       @item_max = @data.size
  179.       create_contents
  180.       for i in 0...@item_max
  181.         draw_item(i)
  182.       end
  183.     end
  184.   end
  185.   #--------------------------------------------------------------------------
  186.   # * Draw Item (Definition added)
  187.   #     index : item number
  188.   #--------------------------------------------------------------------------
  189.   def draw_item(index)
  190.     unless $game_switches[EXCMD_BTRSHOP_SID]
  191.       _excbshop_draw_item(index)
  192.     else
  193.       item = @data[index]
  194.       number = $game_party.item_number(item)
  195.       barter_number = $game_party.barter_item_number
  196.       barter_rate = $game_party.barter_rate(item)
  197.       enabled = (barter_rate <= barter_number and number < 99)
  198.       rect = item_rect(index)
  199.       self.contents.clear_rect(rect)
  200.       draw_item_name(item, rect.x, rect.y, enabled)
  201.       rect.width -= 4
  202.       self.contents.draw_text(rect, barter_rate, 2)
  203.     end
  204.   end
  205. end
  206.  
  207. class Scene_Shop
  208.   alias _excbshop_start start unless $@
  209.   alias _excbshop_terminate terminate unless $@
  210.   alias _excbshop_create_command_window create_command_window unless $@
  211.   alias _excbshop_update_command_selection update_command_selection unless $@
  212.   alias _excbshop_update_buy_selection update_buy_selection unless $@
  213.   alias _excbshop_update_sell_selection update_sell_selection unless $@
  214.   alias _excbshop_decide_number_input decide_number_input unless $@
  215.   #--------------------------------------------------------------------------
  216.   # * Determine if this is an Exchange Shop
  217.   #--------------------------------------------------------------------------
  218.   def barter?
  219.     return $game_switches[EXCMD_BTRSHOP_SID]
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # * Start processing (Redefined)
  223.   #--------------------------------------------------------------------------
  224.   def start
  225.     _excbshop_start
  226.     if barter?
  227.       @gold_window.currency = EXCMD_BTRSHOP_ITEM[1]
  228.       @number_window.currency = EXCMD_BTRSHOP_ITEM[1]
  229.       @gold_window.refresh
  230.     end
  231.   end
  232.   #--------------------------------------------------------------------------
  233.   # * Termination Processing (Definition added)
  234.   #--------------------------------------------------------------------------
  235.   def terminate
  236.     _excbshop_terminate
  237.     $game_switches[EXCMD_BTRSHOP_SID] = false
  238.   end
  239.   #--------------------------------------------------------------------------
  240.   # * Create Command Window (Definition added)
  241.   #--------------------------------------------------------------------------
  242.   def create_command_window
  243.     unless barter?
  244.       _excbshop_create_command_window
  245.     else
  246.       s1 = EXCMD_BTRSHOP_VOCAB[0]
  247.       s2 = EXCMD_BTRSHOP_VOCAB[1]
  248.       s3 = EXCMD_BTRSHOP_VOCAB[2]
  249.       @command_window = Window_Command.new(384, [s1, s2, s3], 3)
  250.       @command_window.y = 56
  251.       if $game_temp.shop_purchase_only
  252.         @command_window.draw_item(1, false)
  253.       end
  254.     end
  255.   end
  256.   #--------------------------------------------------------------------------
  257.   # * Update Command Selection (Definition added)
  258.   #--------------------------------------------------------------------------
  259.   def update_command_selection
  260.     _excbshop_update_command_selection
  261.     if Input.trigger?(Input::C) and barter?
  262.       unless $game_temp.shop_purchase_only
  263.         if @command_window.index == 1 # 売却する
  264.           @gold_window.currency = Vocab::gold
  265.           @number_window.currency = Vocab::gold
  266.           @gold_window.refresh
  267.         end
  268.       end
  269.     end
  270.   end
  271.   #--------------------------------------------------------------------------
  272.   # * Update Buy Item Selection (Definition added)
  273.   #--------------------------------------------------------------------------
  274.   def update_buy_selection
  275.     unless barter?
  276.       _excbshop_update_buy_selection
  277.     else
  278.       @status_window.item = @buy_window.item
  279.       if Input.trigger?(Input::B)
  280.         Sound.play_cancel
  281.         @command_window.active = true
  282.         @dummy_window.visible = true
  283.         @buy_window.active = false
  284.         @buy_window.visible = false
  285.         @status_window.visible = false
  286.         @status_window.item = nil
  287.         @help_window.set_text("")
  288.         return
  289.       end
  290.       if Input.trigger?(Input::C)
  291.         @item = @buy_window.item
  292.         barter_number = $game_party.barter_item_number
  293.         barter_rate = $game_party.barter_rate(@item)
  294.         number = $game_party.item_number(@item)
  295.         if @item == nil or barter_rate > barter_number or number == 99
  296.           Sound.play_buzzer
  297.         else
  298.           Sound.play_decision
  299.           max = barter_number / barter_rate
  300.           max = [max, 99 - number].min
  301.           @buy_window.active = false
  302.           @buy_window.visible = false
  303.           @number_window.set(@item, max, barter_rate)
  304.           @number_window.active = true
  305.           @number_window.visible = true
  306.         end
  307.       end
  308.     end
  309.   end
  310.   #--------------------------------------------------------------------------
  311.   # * Update Sell Item Selection (Definition added)
  312.   #--------------------------------------------------------------------------
  313.   def update_sell_selection
  314.     _excbshop_update_sell_selection
  315.     if Input.trigger?(Input::B) and barter?
  316.       @gold_window.currency = EXCMD_BTRSHOP_ITEM[1]
  317.       @number_window.currency = EXCMD_BTRSHOP_ITEM[1]
  318.       @gold_window.refresh
  319.     end
  320.   end
  321.   #--------------------------------------------------------------------------
  322.   # * Confirm Number Input (Definition added)
  323.   #--------------------------------------------------------------------------
  324.   def decide_number_input
  325.     unless barter?
  326.       _excbshop_decide_number_input
  327.     else
  328.       Sound.play_shop
  329.       @number_window.active = false
  330.       @number_window.visible = false
  331.       barter_rate = $game_party.barter_rate(@item)
  332.       number = @number_window.number
  333.       case @command_window.index
  334.       when 0  # Buy (Exchange)
  335.         $game_party.lose_item($game_party.barter_item, number * barter_rate)
  336.         $game_party.gain_item(@item, number)
  337.         @gold_window.refresh
  338.         @buy_window.refresh
  339.         @status_window.refresh
  340.         @buy_window.active = true
  341.         @buy_window.visible = true
  342.       when 1  # Sell
  343.         $game_party.gain_gold(number * (@item.price / 2))
  344.         $game_party.lose_item(@item, number)
  345.         @gold_window.refresh
  346.         @sell_window.refresh
  347.         @status_window.refresh
  348.         @sell_window.active = true
  349.         @sell_window.visible = true
  350.         @status_window.visible = false
  351.       end
  352.     end
  353.   end
  354. end
Advertisement
Add Comment
Please, Sign In to add comment