Advertisement
QuasiXi

Quasi Shop

Mar 7th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.95 KB | None | 0 0
  1. #==============================================================================#
  2. # ** Quasi Shop v 1.1
  3. #------------------------------------------------------------------------------
  4. # This script generates shops with random items.  Items probability chance can
  5. # be modified with note tags.  Default probability chance is to get set below.
  6. #==============================================================================#
  7. if !$imported["Quasi_Core"].nil?
  8. Quasi.version(1.0,"Quasi_Shop")
  9. if !$quasi["Quasi_Shop"]
  10.  
  11. module Quasi
  12.   module Shop
  13. #==============================================================================#
  14. #                          **Instructions**
  15. #==============================================================================#
  16. # Step 1. Create shops.
  17. #  Change/edit/add values to this hash below.
  18. #     shop_number     => [type, [probability factors]],
  19. #   - shop_number is the value we will use when we open the shop
  20. #     (should never be the same)
  21. #   - type determains what type of items that shop will sell.
  22. #     Type of shops:
  23. #      :items         => items only
  24. #      :wep           => weapons only
  25. #      :arm           => armor only
  26. #      :wepnitem      => weapons and items only
  27. #      :armnitem      => armor and items only
  28. #      :wepnarm       => weapons and armor only
  29. #      :all           => items, weapons and armor
  30. #   - probability factors is optional.  These factors can increase the chance
  31. #     of certain items appearing in the shop.
  32. #     Probability Factors:
  33. #      :level         => Probability increases as Avg. Party lvl gets closer to
  34. #                      the item's level rate.
  35. #      ":paramX"      => Needs "" around it! X is equal to the param.  Probability
  36. #                      increases as Avg. Param X gets closer to item's Param X rate.
  37. #                       - 0 = Max Hp
  38. #                       - 1 = Max Mp
  39. #                       - 2 = Atk
  40. #                       - 3 = Def
  41. #                       - 4 = M.Atk
  42. #                       - 5 = M.Def
  43. #                       - 6 = Agi
  44. #                       - 7 = Luk
  45. #      ":variableX"   => Needs "" around it! X is equal to the variable.  Probability
  46. #                      of all items in shop increase by the value the variable equals.
  47. #      ":switchX"     => Needs "" around it! X is equal to the switch.  Probability
  48. #                      of all items in shop increase by SWITCH_RATE (set below).
  49. #  Here's 3 examples of different shops:
  50. #------------------------------------------------------------------------------
  51.     SHOPS          = {
  52.       0            => [:items,[:level]],
  53.       1            => [:wep,[":variable2", ":param7"]],
  54.       2            => [:arm,[":switch1"]],
  55.       3            => [:all]     # <= Last one on the list shouldn't have a comma!!!
  56.     }
  57. #------------------------------------------------------------------------------
  58. # Step 2. Settings values.
  59. #  These are some constants/ default values.
  60. #------------------------------------------------------------------------------
  61.      # Auto Resets shops with :switch or :variable factors when a switch/variable is changed
  62.     AUTO_RESET     = true
  63.      # Auto Resets shops with :level or :param whenever a character levels up
  64.     AUTO_RESET_LVL = false  # off by default, don't really like this
  65.      # Default item probability rate (Only used if no note tag is set.)
  66.     DEFAULT_RATE   = 10
  67.      # Default item target level (Only used if no note tag is set.)
  68.     DEFAULT_LEVEL  = 50
  69.      # How much to add when Avg.Lvl = Item.Targetlvl
  70.      # More info for this in post/blog.
  71.     LVL_RATE       = 50
  72.      # How much to add when Avg.ParamX = Item.TargetParam
  73.      # More info for this in post/blog.
  74.     PARAM_RATE     = 50
  75.      # Value to add when Switch is on AND shop has ":switchX" factor.
  76.     SWITCH_RATE    = 50
  77. #------------------------------------------------------------------------------
  78. # Step 3. Note Tags!~
  79. #  There's 3 Tags for items/weps/armors
  80. #
  81. #  rate=X         => Where X equals the probability chance for that item to appear.
  82. #                    If item doesn't have this tag, it uses DEFAULT_RATE
  83. #  ratelvl=X      => Where X equals target lvl for that item.
  84. #                    If item doesn't have this tag, it uses DEFAULT_LEVEL
  85. #  rateparam=x0,x1,x2,x3,x4,x5,x6,x7
  86. #                 => ALL values are needed.  It's a bit complicated and messed, sry ):
  87. #                    0-7 = the param value, so x7 = value for LUK
  88. #  An Example rateparam:
  89. #    rateparam=5000,300,20,50,60,40,21,25
  90. #      Target Params: HP    => 5000
  91. #                     MP    => 300
  92. #                     Atk   => 20
  93. #                     Def   => 50
  94. #                     M.Atk => 60
  95. #                     M.Def => 40
  96. #                     Agi   => 21
  97. #                     Luk   => 25
  98. #                    If rateparam is not set it will use Avg.Param * PARAM_RATE
  99. #                    Which cancles out the other math, and their will be no increase.
  100. #------------------------------------------------------------------------------
  101. # Step 4. Running the Shop
  102. #  To open a shop in game, use a script call:
  103. #   open_shop(shop_number, purchase only?(optional))
  104. #  Shop_number correspondes to the shop_number in the SHOPS hash.
  105. #  purchase only? is either true or false.  It's false by default
  106. # Examples:
  107. #  Open shop 0 with purchase only
  108. #   script call:  open_shop(0, true)
  109. #
  110. #  Open shop 3 without purchase only
  111. #   script call:  open_shop(3)
  112. #
  113. # ** NEW **
  114. #  To reset shops items. (Re-Randomize their stock)
  115. #  Run a script call:
  116. #   reset_shops(mode, x)
  117. #  Mode can be set to
  118. #   :all        => resets all shops (doesn't use x)  *DEFAULT*
  119. #   :level      => resets all shops with :level factor (doesn't use x)
  120. #   :param      => resets all shops with ":paramX" factor (x = param)
  121. #   :variable   => resets all shops with ":variableX" factor (x = variable)
  122. #   :switch     => resets all shops with ":switchX" factor (x = switch)
  123. #   :selected   => resets ONLY x shop
  124. #  Examples:
  125. #   reset_shops  or reset_shops(:all)     => resets all
  126. #   reset_shops(:level)                  => resets all shops with :level factor
  127. #   reset_shops(:switch,5)               => resets all shops with ":switch5" factor
  128. #   reset_shops(:selected,1)             => resets shop 1
  129. #------------------------------------------------------------------------------
  130. # Other Info:
  131. #  To Test an item's probability based on level or param use script call:
  132. #   Quasi::Shop::test_rates(type, id, settings, inc=1, pid = 0)
  133. #  type      => :item, :wep or :armor
  134. #  id        => items id (can be found in database)
  135. #  settings  => :level or :param
  136. #  inc       => how much to inc/decrease per 'test'   (optional, default 1)
  137. #  pid       => Parameter ID, 0 = Max Hp, 1 = Max MP, ect.. (optional, default 0)
  138. #
  139. #  This will generate a list using avg party level/param with the items lvl/param rate.
  140. # Runs 10 times to give 10 outcomes.
  141. #  For an example of this, visit blog or try it out.
  142. #
  143. #==============================================================================#
  144. # By Quasi (http://quasixi.wordpress.com/)
  145. #  - 3/7/14
  146. #==============================================================================#
  147. #   ** Stop! Do not edit anything below, unless you know what you      **
  148. #   ** are doing!                                                      **
  149. #==============================================================================#
  150.  
  151.     def self.reset_shops(mode=:all, x=0)
  152.       case mode
  153.       when :all
  154.         SHOPS.each do |i|
  155.           $game_system.shop[i[0]] = quasi_products(i[1][0], i[1][1])
  156.         end
  157.       when :level
  158.         SHOPS.each do |i|
  159.           next if i[1][1].nil?
  160.           next unless i[1][1].include?(:level)
  161.           $game_system.shop[i[0]] = quasi_products(i[1][0], i[1][1])
  162.         end
  163.       when :param
  164.         SHOPS.each do |i|
  165.           next if i[1][1].nil?
  166.           next unless i[1][1].any?{|x| x =~ /:param(\d+)/i}
  167.           next if $1.to_i != x
  168.           $game_system.shop[i[0]] = quasi_products(i[1][0], i[1][1])
  169.         end
  170.       when :variable
  171.         SHOPS.each do |i|
  172.           next if i[1][1].nil?
  173.           next unless i[1][1].any?{|x| x =~ /:variable(\d+)/i}
  174.           next if $1.to_i != x
  175.           $game_system.shop[i[0]] = quasi_products(i[1][0], i[1][1])
  176.         end
  177.       when :switch
  178.         SHOPS.each do |i|
  179.           next if i[1][1].nil?
  180.           next unless i[1][1].any?{|x| x =~ /:switch(\d+)/i}
  181.           next if $1.to_i != x
  182.           $game_system.shop[i[0]] = quasi_products(i[1][0], i[1][1])
  183.         end
  184.       when :selected
  185.         $game_system.shop[x] = quasi_products(SHOPS[x][0],SHOPS[x][1])
  186.       end
  187.     end
  188.  
  189.     def self.quasi_products(type, factors)
  190.       goods = []
  191.       until goods != [] do
  192.         case type
  193.         when :items
  194.           items = $data_items
  195.         when :wep
  196.           items = $data_weapons
  197.         when :arm
  198.           items = $data_armors
  199.         when :wepnitem
  200.           items = $data_items + $data_weapons
  201.         when :armnitem
  202.           items = $data_items + $data_armors
  203.         when :wepnarm
  204.           items = $data_weapons + $data_armors
  205.         when :all
  206.           items = $data_items + $data_weapons + $data_armors
  207.         end
  208.      
  209.         items.each do |i|
  210.           next if i.nil?
  211.           if check_rate(i, factors)
  212.             if $data_items.include?(i)
  213.               t = 0
  214.             elsif $data_weapons.include?(i)
  215.               t = 1
  216.             elsif $data_armors.include?(i)
  217.               t = 2
  218.             end
  219.             goods.push([t,i.id,0,0])
  220.           end
  221.         end
  222.       end
  223.       return goods
  224.     end
  225.  
  226.     def self.check_rate(item, factors)
  227.       r = item.rate
  228.       factors = [] if factors.nil?
  229.       if factors.include?(:level)
  230.         r += rate_level(item)
  231.       end
  232.       if factors.any? {|x| x =~ /:param(\d)/i}
  233.         r += rate_param(item, $1.to_i)
  234.       end
  235.       if factors.any? {|x| x =~ /:switch(\d+)/i}
  236.         r += SWITCH_RATE if $game_switches[($1.to_i)]
  237.       end
  238.       if factors.any? {|x| x =~ /:variable(\d+)/i}
  239.         r += $game_variables[($1.to_i)]
  240.       end
  241.       chance = rand(100)
  242.       if chance < r
  243.         return true
  244.       end
  245.       return false
  246.     end
  247.  
  248.     def self.rate_level(item,test=0)
  249.       ilvl = item.ratelvl
  250.       plvl =  $game_party.avg_level + test
  251.       factor = LVL_RATE
  252.      
  253.       exp = Quasi.find_exp(factor, ilvl) # exp = ilvl√factor
  254.       r = exp**plvl
  255.      
  256.       if ilvl < plvl
  257.         r = factor + (plvl-ilvl)/2
  258.       end
  259.       return r
  260.     end
  261.    
  262.     def self.rate_param(item, param,test=0)
  263.       avg = $game_party.avg_param(param) + test
  264.       factor = PARAM_RATE
  265.       pval = item.rateparam[param]
  266.       pval = avg*factor if pval.nil?
  267.      
  268.       exp = Quasi.find_exp(factor, pval)  # exp = pval√factor
  269.       r = exp**avg
  270.       r = factor if pval < avg
  271.       return r
  272.     end
  273.    
  274.     def self.test_rates(type, id, settings, inc=1, pid = 0)
  275.       case type
  276.       when :item
  277.         i = $data_items
  278.       when :arm
  279.         i = $data_armors
  280.       when :wep
  281.         i = $data_weapons
  282.       end
  283.       item = i[id]
  284.       txt = "#{item.name} RateLvl: #{item.ratelvl}\n"
  285.       r = item.rate
  286.      
  287.       case settings
  288.       when :level
  289.         txt += "Avg Player Level: #{$game_party.avg_level} \n\n"
  290.         for i in 0...10
  291.           i *= -inc
  292.           lvl = rate_level(item,i+(5*inc))
  293.           p = $game_party.avg_level
  294.           p += i+5*inc
  295.           txt += "Level: #{p} = #{lvl}\n"
  296.         end
  297.       when :param
  298.         ap = $game_party.avg_param(pid)
  299.         ip = item.rateparam[pid]
  300.         txt += "Avg #{Vocab::param(pid)}:"
  301.         txt += " #{ap} Item Param: #{ip}\n\n"
  302.         for i in 0...10
  303.           i *= -inc
  304.           param = rate_param(item, pid, i+(5*inc))
  305.           p = ap+i+5*inc
  306.           txt += "Param: #{p} = #{param}\n"
  307.         end
  308.       end
  309.       msgbox(sprintf(txt))
  310.     end
  311.   end
  312. end
  313. #==============================================================================
  314. # ** Game_Interpreter
  315. #------------------------------------------------------------------------------
  316. #  An interpreter for executing event commands. This class is used within the
  317. # Game_Map, Game_Troop, and Game_Event classes.
  318. #==============================================================================
  319.  
  320. class Game_Interpreter
  321.   alias quasi_shop_reset_switches command_121
  322.   alias quasi_shop_reset_variables command_122
  323.  
  324.   #--------------------------------------------------------------------------
  325.   # * Shop Processing
  326.   #--------------------------------------------------------------------------
  327.   def open_shop(index, purchase=false)
  328.     return if $game_party.in_battle
  329.     return if Quasi::Shop::SHOPS[index].nil?
  330.     goods = $game_system.shop[index]
  331.     SceneManager.call(Scene_Shop)
  332.     SceneManager.scene.prepare_q(goods, purchase)
  333.     Fiber.yield
  334.   end
  335.  
  336.   #--------------------------------------------------------------------------
  337.   # * Reset Shops
  338.   # ** Redirects to reset_shop inside quasi module
  339.   #--------------------------------------------------------------------------
  340.   def reset_shops(mode=:all, x=0)
  341.     Quasi::Shop::reset_shops(mode, x)
  342.   end
  343.  
  344.   #--------------------------------------------------------------------------
  345.   # * Control Switches
  346.   #--------------------------------------------------------------------------
  347.   def command_121
  348.     quasi_shop_reset_switches
  349.     return unless Quasi::Shop::AUTO_RESET
  350.     (@params[0]..@params[1]).each do |i|
  351.       reset_shops(:switch,i)
  352.     end
  353.   end
  354.   #--------------------------------------------------------------------------
  355.   # * Control Variables
  356.   #--------------------------------------------------------------------------
  357.   def command_122
  358.     quasi_shop_reset_variables
  359.     return unless Quasi::Shop::AUTO_RESET
  360.     (@params[0]..@params[1]).each do |i|
  361.       reset_shops(:variable, i)
  362.     end
  363.   end
  364. end
  365. #==============================================================================
  366. # ** Scene_Shop
  367. #------------------------------------------------------------------------------
  368. #  This class performs shop screen processing.
  369. #==============================================================================
  370.  
  371. class Scene_Shop < Scene_MenuBase
  372.   #--------------------------------------------------------------------------
  373.   # * Prepare
  374.   #--------------------------------------------------------------------------
  375.   def prepare_q(goods, purchase_only)
  376.     @goods = goods
  377.     @purchase_only = purchase_only
  378.   end
  379. end
  380.  
  381. #==============================================================================
  382. # ** Game_System
  383. #------------------------------------------------------------------------------
  384. #  This class handles system data. It saves the disable state of saving and
  385. # menus. Instances of this class are referenced by $game_system.
  386. #==============================================================================
  387.  
  388. class Game_System
  389.   alias quasi_shop_init initialize
  390.   #--------------------------------------------------------------------------
  391.   # * Public Instance Variables
  392.   #--------------------------------------------------------------------------
  393.   attr_accessor :shop           # save forbidden
  394.   #--------------------------------------------------------------------------
  395.   # * Object Initialization
  396.   #--------------------------------------------------------------------------
  397.   def initialize
  398.     @shop = []
  399.     quasi_shop_init
  400.   end
  401. end
  402. #==============================================================================
  403. # ** Game_Actor
  404. #------------------------------------------------------------------------------
  405. #  This class handles actors. It is used within the Game_Actors class
  406. # ($game_actors) and is also referenced from the Game_Party class ($game_party).
  407. #==============================================================================
  408.  
  409. class Game_Actor < Game_Battler
  410.   alias quasi_shop_lvl level_up
  411.   #--------------------------------------------------------------------------
  412.   # * Level Up
  413.   #--------------------------------------------------------------------------
  414.   def level_up
  415.     quasi_shop_lvl
  416.     return unless Quasi::Shop::AUTO_RESET_LVL
  417.     p "reset"
  418.     for i in 0..7
  419.       Quasi::Shop::reset_shops(:param,i)
  420.     end
  421.     Quasi::Shop::reset_shops(:level)
  422.   end
  423. end
  424. #==============================================================================
  425. # ** Scene_Title
  426. #------------------------------------------------------------------------------
  427. #  This class performs the title screen processing.
  428. #==============================================================================
  429.  
  430. class Scene_Title < Scene_Base
  431.   alias quasi_shop_ng command_new_game
  432.   #--------------------------------------------------------------------------
  433.   # * [New Game] Command
  434.   #--------------------------------------------------------------------------
  435.   def command_new_game
  436.     quasi_shop_ng
  437.     Quasi::Shop::reset_shops
  438.   end
  439. end
  440.  
  441. #==============================================================================
  442. # ** RPG::BaseItem
  443. #------------------------------------------------------------------------------
  444. # Thanks to Fomar0153 for note tag tutorial
  445. #==============================================================================
  446. class RPG::BaseItem
  447.   def rate
  448.     if @rate.nil?
  449.       if @note =~ /rate=(\d+)/i
  450.         @rate = $1.to_i
  451.       else
  452.         @rate = Quasi::Shop::DEFAULT_RATE
  453.       end
  454.     end
  455.     @rate
  456.   end
  457.  
  458.   def ratelvl
  459.     if @ratelvl.nil?
  460.       if @note =~ /ratelvl=(\d+)/i
  461.         @ratelvl = $1.to_i
  462.       else
  463.         @ratelvl = Quasi::Shop::DEFAULT_LEVEL
  464.       end
  465.     end
  466.     @ratelvl
  467.   end
  468.  
  469.   def rateparam
  470.     if @rateparam.nil?
  471.       if @note =~ /rateparam=(.*)/i
  472.         array = []
  473.         for x in $1.split(",")
  474.           array.push(x.to_i)
  475.         end
  476.         return array
  477.       else
  478.         return []
  479.       end
  480.     end
  481.   end
  482. end
  483.  
  484. end
  485. else
  486.   msgbox_p "[Quasi_Shop] needs [Quasi_Core] in order to run."
  487. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement