Advertisement
LiTTleDRAgo

[RGSS/2/3] Drago - Core Engine

Dec 12th, 2013
5,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 202.60 KB | None | 0 0
  1. #==============================================================================
  2. # ** Drago - Core Engine
  3. # Version : 1.60
  4. # Contact : littledrago.blogspot.com / forum.chaos-project.com
  5. #==============================================================================
  6. ($imported ||= {})[:drg_core_engine] = 1.60
  7. # =============================================================================
  8. # NOTE:
  9. # -----------------------------------------------------------------------------
  10. # This is a devtool for me to make me easier for scripting
  11. # If you want to use this, put above all custom script & below Scene_Debug
  12. #
  13. # =============================================================================
  14. # DOCUMENTATION:
  15. # -----------------------------------------------------------------------------
  16. =begin
  17.   Version History
  18.  
  19.   1.57 -
  20.   ▼ Overhaul script (108 minor modification)
  21.   ▼ Fixed glitch on BitmapDump method
  22.   ▼ Added Bitmap#rotate90
  23.   ▼ Added Bitmap#rotate180
  24.   ▼ Added Bitmap#rotate270
  25.  
  26.   1.50 -
  27.   ▼ Overhaul script (71 minor modification)
  28.   ▼ Longer script call (RMXP script call bug fix)
  29.  
  30.   1.42 -
  31.   ▼ Fixed Graphics.fullscreen?
  32.  
  33.   1.40 -
  34.   ▼ Modified Game_Map#load_event
  35.  
  36.   1.39 -
  37.   ▼ Fixed glitch at Bitmap#draw_icon
  38.   ▼ Added Fiber class
  39.   ▼ Added Game_Map#load_event
  40.   ▼ Added Game_Map#call_event
  41.  
  42.   1.36 -
  43.   ▼ Added Color class library
  44.  
  45.   1.34 -
  46.   ▼ Added Module#define_third_method(sym = nil, _alias = nil,
  47.                     prevent_stack = true, *a, &block)
  48.   ▼ Added Bitmap#blur
  49.  
  50.   1.31 -
  51.   ▼ Added Module#attr_sec_reader(symbol, default=0)
  52.   ▼ Added Module#attr_sec_accessor(symbol, *a, &block)
  53.   ▼ Added Module#alias_sec_method(sym = nil, *a, &block)
  54.   ▼ Added Module#define_sec_method(sym = nil, *a, &block)
  55.  
  56.   1.29 -
  57.   ▼ Added Array#have_all?(array)
  58.   ▼ Added Array#have_any?(array)
  59.  
  60.   1.27 -
  61.   ▼ Added Added Graphics.brightness
  62.   ▼ Added Graphics.wait(frames = 10)
  63.   ▼ Added Graphics.fadein(frames = 10)
  64.   ▼ Added Graphics.fadeout(frames = 10)
  65.   ▼ Added Bitmap#invert
  66.   ▼ Added Bitmap#invert!
  67.   ▼ Added Bitmap#brighten(amount = 10)
  68.   ▼ Added Bitmap#brighten!(amount = 10)
  69.   ▼ Added Bitmap#darken(amount = 10)
  70.   ▼ Added Bitmap#darken!(amount = 10)
  71.   ▼ Added Bitmap#grayscale
  72.   ▼ Added Bitmap#grayscale!
  73.   ▼ Added Bitmap#pixelate(size = 10)
  74.   ▼ Added Bitmap#pixelate!(size = 10)
  75.   ▼ Added Bitmap#frost(noise = 10)
  76.   ▼ Added Bitmap#frost!(noise = 10)
  77.  
  78.   1.24 -
  79.   ▼ Added Viewport#x
  80.   ▼ Added Viewport#y
  81.   ▼ Added Viewport#width
  82.   ▼ Added Viewport#height
  83.   ▼ Added Viewport#disposed?
  84.  
  85.   1.23 -
  86.   ▼ Added Window_Base#draw_text (same as Bitmap#draw_text)
  87.   ▼ Added Window_Base#draw_icon
  88.   ▼ Added Window_Base#draw_battler
  89.   ▼ Added Window_Base#draw_picture
  90.  
  91.   1.22 -
  92.   ▼ Added Game_Map#load_map_data
  93.   ▼ Added Game_Map#load_event
  94.   ▼ Rename Array#product => Array#prod (because of conflict with RGSS3)
  95.  
  96.   1.21 -
  97.   ▼ Fixed Glitch at Bitmap#crop
  98.  
  99.   1.20 -
  100.   ▼ Fixed Script Hanging Error at Bitmap#export
  101.  
  102.   1.19 -
  103.   ▼ Added some of RMXP method - Game_System#play_bgm
  104.   ▼ Added some of RMXP method - Game_System#play_bgs
  105.   ▼ Added some of RMXP method - Game_System#play_me
  106.   ▼ Added some of RMXP method - Game_System#play_se
  107.  
  108.   1.18 -
  109.   ▼ Fixing Backtrace at Array#method_missing
  110.  
  111.   1.17 -
  112.   ▼ Added Bitmap#flip_vertical
  113.   ▼ Added Bitmap#flip_vertical!
  114.   ▼ Added Bitmap#flip_horizontal
  115.   ▼ Added Bitmap#flip_horizontal!
  116.  
  117.   1.16 -
  118.   ▼ Added Bitmap#export (dump bitmap into .png files)
  119.  
  120.   1.15 -
  121.   ▼ Added Game_Characters#character_above?
  122.   ▼ Added Game_Characters#character_below?
  123.   ▼ Added Game_Characters#character_right?
  124.   ▼ Added Game_Characters#character_left?
  125.  
  126.   1.11 -
  127.   ▼ Fixing Bug at changing screen size
  128.  
  129.   1.10 -
  130.   ▼ Added Graphics.scale_screen(width, height)
  131.  
  132.   1.00 -
  133.   ▼ Original Release
  134.  
  135. # -----------------------------------------------------------------------------
  136. # Below is the available command in this script
  137. # -----------------------------------------------------------------------------
  138. # * Graphics
  139. # -----------------------------------------------------------------------------
  140. # -----------------------------------------------------------------------------
  141.   This command is not recommended to used in RMXP
  142.  
  143.   Graphics.scale_screen(width,height)
  144.   - Resize the screen and stretched it (if not RMXP) into new resolution
  145.  
  146.   Graphics.fill_monitor
  147.   - Maximize the screen and stretched it (if not RMXP) to fill the monitor
  148.  
  149.   Graphics.control_screen_size(enable = true)
  150.   - Enable or disable screen size control by mouse.
  151.     Resized screen will be stretched (if not RMXP)
  152. # -----------------------------------------------------------------------------
  153.  
  154.   Graphics.width        - Returns screen width
  155.   Graphics.height       - Returns screen height
  156.   Graphics.fullscreen?  - Returns true if Game in fullscreen mode, else false
  157.   Graphics.fullscreen   - Switch to fullscreen mode
  158.   Graphics.window       - Switch to windowed mode
  159.   Graphics.toggle       - Switch between fullscreen mode and windowed mode
  160.   Graphics.wait(frame)    - Wait for frame
  161.   Graphics.wait_for_input - Wait until input is pressed
  162.   Graphics.fadein(frame)  - Fade in the screen
  163.   Graphics.fadeout(frame) - Fade out the screen
  164.   Graphics.snap_to_bitmap - Snap screen to a bitmap object
  165.  
  166.   Graphics.high_priority = true/false - Change process priority to high / normal
  167.   Graphics.disable_alt_enter  - Disable input ALT+Enter (until game is closed)
  168.  
  169. # -----------------------------------------------------------------------------
  170. # * Module
  171. # -----------------------------------------------------------------------------
  172.   Module#attr_sec_reader(symbol, default=0)
  173.   - Create attr_reader with default value
  174.      Example : attr_sec_reader :something, "Sprite.new"
  175.      Equal : def something
  176.                @something ||= Sprite.new
  177.              end
  178.              
  179.   Module#attr_sec_accessor(symbol, *a, &block)
  180.   - Create (sheer of) attr_accessor with default value
  181.      Example : attr_sec_accessor :sprite1, :sprite2, :sprite3, "Sprite.new"
  182.      Equal : attr_writer :sprite1, :sprite2, :sprite3
  183.              attr_sec_reader :sprite1, "Sprite.new"
  184.              attr_sec_reader :sprite2, "Sprite.new"
  185.              attr_sec_reader :sprite3, "Sprite.new"
  186.  
  187.   Module#alias_sec_method(sym = nil, *a, &block)
  188.   - Create alias only if alias name is not defined and target method is exist
  189.      Example : alias_sec_method :alias_method, :target_method
  190.      Equal : if method_defined?(:target) && !method_defined?(:alias)
  191.                alias_method :alias, :target
  192.              end
  193.              
  194.   Module#define_sec_method(sym = nil, *a, &block)
  195.   - Define method only if the method is not defined
  196.      Example : define_sec_method(:some_method) { |*a| block }
  197.      Equal : unless method_defined?(:some_method)
  198.                def some_method(*a)
  199.                  block
  200.                end
  201.              end
  202.  
  203. # -----------------------------------------------------------------------------
  204. # * Array
  205. # -----------------------------------------------------------------------------
  206.   Array Method Distributor
  207.   - Distribute unexisted method in array into each variable
  208.   (will work only if the method isn't exist in Array)
  209.     Example : [Graphics,Input].update
  210.                 # equal => Graphics.update ; Input.update  
  211.               [@string1,@string2,@integer1,@sprite].dispose
  212.                 # equal => @sprite.dispose
  213.                 # (all variable without dispose method is ignored)
  214.               [@string1,@string2,@integer1].dispose
  215.                 # Throws NoMethodError if all method didn't have dispose method
  216.  
  217.   Array#random_each / Array#random_each_with_index
  218.     Example : a = [1,2,3,4,5,6,7]
  219.               a.random_each {|i| puts i} # 4,5,3,2,7,1,6
  220.              
  221.   Array#sum
  222.   Array#prod
  223.   Array#average
  224.     Example : [1,2,3,4].sum         # 10  (1+2+3+4)
  225.               [1,2,3,4].average     # 2.5 (10 / 4)
  226.               [1,2,3,4].prod        # 24  (1*2*3*4)
  227.              
  228.   Array#shuffle / Array#shuffle!  
  229.     Example : [1,2,3,4].shuffle     # [3,2,4,1]  
  230.    
  231.   Array#random / Array#random!
  232.     Example : [1,2,3,4].random      # 3  
  233.  
  234.   Array#switch=(true/false)
  235.   Array#switch_reverse
  236.   - Turn on / off switches all variable in the array
  237.     (will ignored if variable not integer or not an array)
  238.     Example : [1,2,[3,2,'A'],5].switch = true
  239.                 # equal => $game_switches[1] = true
  240.                            $game_switches[2] = true
  241.                            $game_self_switches[[3,2,'A']] = true
  242.                            $game_switches[5] = true
  243.                            
  244.   Array#variable(value = nil, method = "=")    
  245.   - Change all variable value in the array (ignored if variable not integer)
  246.     Example : [1,2,5].variable(10)
  247.                 # equal => $game_variables[1] = 10
  248.                            $game_variables[2] = 10
  249.                            $game_variables[5] = 10
  250.     Example : [1,2,5].variable(10, "+=")
  251.                 # equal => $game_variables[1] += 10
  252.                            $game_variables[2] += 10
  253.                            $game_variables[5] += 10
  254.                            
  255.   Array#have_all?(array)
  256.   - returns true if contains all elements of the array                
  257.   Array#have_any?(array)
  258.   - returns true if contains any elements of the array  
  259. # -----------------------------------------------------------------------------
  260. # * Sprite
  261. # -----------------------------------------------------------------------------
  262.   Sprite#clone
  263.   Sprite#dup
  264.   - Clone sprite without "can't clone sprite error"
  265.     Example : a = Sprite.new
  266.               b = a.clone
  267.               a.dispose
  268.               print a.disposed? # true
  269.               print b.disposed? # false
  270.              
  271. # -----------------------------------------------------------------------------
  272. # * Spriteset_Map
  273. # -----------------------------------------------------------------------------
  274.   Spriteset_Map#sprite_player
  275.   - returns sprite player at @character_sprites in Spriteset_Map
  276.  
  277.   Spriteset_Map#find_character(character)
  278.   - returns character sprite at @character_sprites in Spriteset_Map
  279.  
  280. # -----------------------------------------------------------------------------
  281. # * Game Battler
  282. # -----------------------------------------------------------------------------
  283.   Game_Battler#hp_percent(integer = false, float_points = 2)
  284.   Game_Battler#sp_percent(integer = false, float_points = 2)
  285.   Example : $game_actors[1].hp_percent
  286.                    # returns 0 - 100 based on percentage hp
  287.                    
  288. # -----------------------------------------------------------------------------
  289. # * Bitmap
  290. # -----------------------------------------------------------------------------
  291.   Bitmap#export(filename)
  292.   - exporting bitmap into a file (only support bmp and png)
  293.   Example : bitmap = Bitmap.new(50,20)
  294.             bitmap.export('empty.png')
  295.            
  296.   Bitmap#flip_vertical
  297.   Bitmap#flip_vertical!
  298.   Bitmap#flip_horizontal
  299.   Bitmap#flip_horizontal!
  300.   - flip the bitmap vertically or horizontally (using ! will modify self)
  301.   Example : bitmap = Bitmap.new(50,20)
  302.             bitmap.flip_vertical!
  303.            
  304.   Bitmap#rotate90
  305.   Bitmap#rotate90!
  306.   Bitmap#rotate180
  307.   Bitmap#rotate180!
  308.   Bitmap#rotate270          
  309.   Bitmap#rotate270!            
  310.        
  311.   Additional Bitmap Effects
  312.   Note : calling this method is process consuming, use with caution
  313.     Bitmap#invert
  314.     Bitmap#invert!
  315.     Bitmap#brighten(amount = 10)
  316.     Bitmap#brighten!(amount = 10)
  317.     Bitmap#darken(amount = 10)
  318.     Bitmap#darken!(amount = 10)
  319.     Bitmap#grayscale
  320.     Bitmap#grayscale!
  321.     Bitmap#pixelate(size = 10)
  322.     Bitmap#pixelate!(size = 10)
  323.     Bitmap#frost(noise = 10)
  324.     Bitmap#frost!(noise = 10)
  325. # -----------------------------------------------------------------------------
  326. # * Color
  327. # -----------------------------------------------------------------------------
  328.   Color RGB value shortcut
  329.   List :
  330.     Color.red
  331.     Color.green
  332.     Color.blue
  333.     Color.white
  334.     Color.black
  335.     Color.yellow
  336.     Color.magenta
  337.     Color.cyan
  338.     Color.purple
  339.     Color.gray
  340.     Color.lightgray
  341.     Color.darkgray
  342.     Color.pink
  343.     Color.orange
  344.     Color.brown
  345.     Color.golden
  346.    
  347.     Color.invert
  348.    
  349.   Color Hex
  350.   - You can use hex value as method in color class (not case sensitive)
  351.     (need to include 'x' in front)
  352.   Example : Color.xffffff, Color.x04dacf, etc
  353.  
  354.  
  355. # -----------------------------------------------------------------------------
  356. # * Game_Map
  357. # -----------------------------------------------------------------------------
  358.  
  359.   Game_Map#load_event(mapid, eventid, x, y, page_id)
  360.   Game_Map#load_event(mapid, eventid, x, y)
  361.   Game_Map#load_event(eventid, x, y)   # map_id will be assumed as current map
  362.   - Duplicate an event from another map to current map at position (x, y)
  363.  
  364.   Game_Map#call_event(mapid, eventid, page_id)
  365.   Game_Map#call_event(mapid, eventid)  # page_id will be assumed as -1
  366.   Game_Map#call_event(eventid)         # map_id will be assumed as current map
  367.   - Call an event from another map to current map
  368.       Note that page_id is start from 1, not 0
  369.       If you're using RMVX or RMVXA, setting page_id to -1 will bring you to
  370.       the page with met condition.
  371.      
  372.      
  373. # -----------------------------------------------------------------------------
  374. # * RTP fix
  375. # -----------------------------------------------------------------------------
  376.  
  377.   Longer script call
  378.   - You can stack script call next to each other as many as possible and they
  379.     will be connected.
  380.   - In addition, this also fixes the Interpreter Script call bug when if you
  381.     use $game_system..... = false, game would freeze. This will prevent that.
  382.  
  383. # -----------------------------------------------------------------------------
  384. # * End Documentation
  385. # -----------------------------------------------------------------------------
  386. =end
  387. # =============================================================================
  388.  
  389. #==============================================================================
  390. # ** Module    
  391. #------------------------------------------------------------------------------
  392. #  
  393. #==============================================================================
  394.  
  395. class Module
  396.   #-------------------------------------------------------------------------
  397.   # * New method: attr_sec_reader (Attr Secondary Reader)
  398.   #-------------------------------------------------------------------------
  399.   unless method_defined?(:attr_sec_reader)
  400.     def attr_sec_reader(*sym)
  401.       if (1..2) === sym.size
  402.         module_eval("define_method(:#{sym[0]}) { @#{sym[0]} ||= #{sym[1]||0}}")
  403.       elsif sym.size > 2
  404.         (default = sym.pop) && sym.each {|s| attr_sec_reader(s,default)}
  405.       end
  406.     end
  407.   end
  408.   #-------------------------------------------------------------------------
  409.   # * New method: attr_sec_accessor (Attr Secondary Accessor)
  410.   #-------------------------------------------------------------------------
  411.   def attr_sec_accessor(sym = nil, *args)
  412.     (args.size > 0) | sym.nil? || args.push(0)
  413.     (default = args.pop) && (b = [sym].concat(args).compact)
  414.     (b).each {|a| (attr_writer(a) || 0) && attr_sec_reader(a,default)}
  415.   end
  416.   #-------------------------------------------------------------------------
  417.   # * New method: alias_sec_method (Alias Secondary Method)
  418.   #   Note : This method will create alias only if alias name is not defined
  419.   #          and target alias is exist
  420.   #-------------------------------------------------------------------------
  421.   unless method_defined?(:alias_sec_method)
  422.     private
  423.     def alias_sec_method(sym = nil, *args)
  424.       (args.size > 0) | sym.nil? || args.clear
  425.       (t = args.pop) && (b = [sym].concat(args).compact).each do |a|
  426.         c = (t == :initialize ? true : method_defined?(t))
  427.         method_defined?(a) || (c && alias_method(a, t))
  428.       end
  429.     end
  430.   end
  431.   #-------------------------------------------------------------------------
  432.   # * New method: redirect_method
  433.   #-------------------------------------------------------------------------
  434.   unless method_defined?(:redirect_method)
  435.     private
  436.     def redirect_method(sym = nil, *args, &block)
  437.       (args.size > 0) | sym.nil? || args.clear
  438.       (t = args.pop) && (b = [sym].concat(args).compact).each do |a|
  439.         meth = "def #{a}[*args] #{t}(*args) end"
  440.         meth.gsub!(/(['")}\](\d+)])\(\*args\)/i)  {    $1     }#'
  441.         meth.gsub!(/\[\*args\]/i)                 { '(*args)' }
  442.         method_defined?(a) || module_eval(meth)
  443.       end
  444.     end
  445.   end
  446.   #-------------------------------------------------------------------------
  447.   # * New method: define_sec_method
  448.   #   Note : This method will not defining method if method name already
  449.   #          exist
  450.   #-------------------------------------------------------------------------
  451.   unless method_defined?(:define_sec_method)
  452.     private
  453.     def define_sec_method(sym = nil, *args, &block)
  454.       sym && (method_defined?(s = sym.to_sym) || define_method(s,*args,&block))
  455.     end
  456.   end
  457.   #-------------------------------------------------------------------------
  458.   # * New method: define_third_method
  459.   #   Note : After aliasing, define a method
  460.   #-------------------------------------------------------------------------
  461.   unless method_defined?(:define_third_method)
  462.     private
  463.     def define_third_method(sym = nil, _alias = nil, *args, &block)
  464.       unless sym.nil? || _alias.nil?
  465.         stack = _alias.to_s =~ /stack/i || sym.to_s == 'initialize'
  466.         stack = (!stack && method_defined?(_alias.to_sym))
  467.         stack || alias_method(_alias.to_sym, sym.to_sym)
  468.         define_method(sym.to_sym,*args,&block)
  469.       end
  470.     end
  471.   end
  472.   #--------------------------------------------------------------------------
  473.   # ● New method: define_pre_alias
  474.   #--------------------------------------------------------------------------
  475.   unless method_defined?(:define_pre_alias)
  476.     private
  477.     def define_pre_alias(sym = nil, *args, &block)
  478.       _time = Time.now
  479.       _alias = :"#{sym}_#{_time.to_i}_#{_time.usec}"
  480.       define_third_method(:"#{sym}",_alias) do  |*args|
  481.         block.bind(self).call
  482.         send(_alias,*args)
  483.       end
  484.     end
  485.     private
  486.     def define_post_alias(sym = nil, *args, &block)
  487.       _time = Time.now
  488.       _alias = :"#{sym}_#{_time.to_i}_#{_time.usec}"
  489.       define_third_method(:"#{sym}",_alias) do  |*args|
  490.         send(_alias,*args)
  491.         block.bind(self).call
  492.       end
  493.     end
  494.   end
  495.   #--------------------------------------------------------------------------
  496.   # ● New method: include_auto_update
  497.   #--------------------------------------------------------------------------
  498.   unless method_defined?(:include_auto_update)
  499.     private
  500.     def include_auto_update
  501.       return if method_defined?(:update_auto_update)
  502.       attr_sec_accessor(:auto_update, "[]")
  503.       define_method(:update_auto_update) do
  504.         return if auto_update.empty?
  505.         auto_update.each do |x|
  506.           (x[1].nil? ? send(x[0]) : (x[2].nil? ? x[0].method(x[1]).call :
  507.           (x[0].method(x[1]).call(*x[2]))))
  508.           (x[3].is_a?(Numeric) && (x[3] -= 1))
  509.         end    
  510.         auto_update.reject! {|x| x[3].is_a?(Numeric) && x[3] <= 0 }
  511.       end
  512.       _time = Time.now
  513.       _alias = :"update_#{_time.to_i}_#{_time.usec}"
  514.       define_third_method(:update,_alias) do  |*args|
  515.         send(_alias,*args)
  516.         update_auto_update
  517.       end
  518.     end
  519.   end  
  520. end
  521. #==============================================================================
  522. # ** LiTTleDRAgo    
  523. #------------------------------------------------------------------------------
  524. #  
  525. #==============================================================================
  526.  
  527. module LiTTleDRAgo
  528.   #-------------------------------------------------------------------------
  529.   # * Constant
  530.   #-------------------------------------------------------------------------
  531.   VX            = defined?(Window_ActorCommand)
  532.   VXA           = defined?(Window_BattleActor)
  533.   XP            = !VX
  534.   RGSS1         = defined?(Hangup)
  535.   RGSS2         = RUBY_VERSION == '1.8.1' && !RGSS1
  536.   RGSS3         = RUBY_VERSION == '1.9.2'  
  537.   APPPATHDRAGO  = "#{ENV['APPDATA']}/Drago/"
  538. end
  539. #==============================================================================
  540. # ** CoreDLL
  541. #------------------------------------------------------------------------------
  542. #  
  543. #==============================================================================
  544. module CoreDLL
  545.   #-------------------------------------------------------------------------
  546.   # * Constant
  547.   #-------------------------------------------------------------------------
  548.   DLL_USED = ['kernel32','msvcrt','user32','gdi32']
  549.   SCENEVXA = "SceneManager.send(:instance_variable_set,:@scene,args.at(0))"
  550.   L        = LiTTleDRAgo
  551.   #-------------------------------------------------------------------------
  552.   # * Public Instance Variables
  553.   #-------------------------------------------------------------------------
  554.   attr_sec_reader :rtlmemory_pi,  "winapi(0,'RtlMoveMemory','pii','i')"
  555.   attr_sec_reader :rtlmemory_ip,  "winapi(0,'RtlMoveMemory','ipi','i')"
  556.   attr_sec_reader :setpriority,   "winapi(0,'SetPriorityClass','pi','i')"
  557.   attr_sec_reader :getprocesstime,"winapi(0,'GetProcessTimes','ipppp','i')"
  558.   attr_sec_reader :getdc,         "winapi(2,'GetDC','i','i')"
  559.   attr_sec_reader :releasedc,     "winapi(2,'ReleaseDC','ii','i')"
  560.   attr_sec_reader :reghotkey,     "winapi(2,'RegisterHotKey', 'liii', 'i')"
  561.   attr_sec_reader :sendinput,     "winapi(2,'SendInput','ipi','i')"
  562.   attr_sec_reader :setwindowlong, "winapi(2,'SetWindowLong','lll','l')"
  563.   attr_sec_reader :getkeysta,     "winapi(2,'GetKeyboardState','p','i')"
  564.   attr_sec_reader :getkeylay,     "winapi(2,'GetKeyboardLayout','l','l')"
  565.   attr_sec_reader :mapvirkey,     "winapi(2,'MapVirtualKeyEx','iil','i')"
  566.   attr_sec_reader :tounicode,     "winapi(2,'ToUnicodeEx','llppill','l')"
  567.   attr_sec_reader :screentoclient,"winapi(2,'ScreenToClient', 'lp', 'i')"
  568.   attr_sec_reader :cursorposition,"winapi(2,'GetCursorPos', 'p', 'i')"
  569.   attr_sec_reader :systemmetrix,  "winapi(2,'GetSystemMetrics', %w(i), 'i')"
  570.   attr_sec_reader :bitblt,        "winapi(3,'BitBlt','iiiiiiiii','i')"
  571.   attr_sec_reader :ccdc,          "winapi(3,'CreateCompatibleDC','i','i')"
  572.   attr_sec_reader :ccbitmap,      "winapi(3,'CreateCompatibleBitmap','iii','i')"
  573.   attr_sec_reader :deleteobject,  "winapi(3,'DeleteObject','i','i')"
  574.   attr_sec_reader :getbitmapbits, "winapi(3,'GetBitmapBits','llp','l')"
  575.   attr_sec_reader :getdibits,     "winapi(3,'GetDIBits','iiiiipi','i')"
  576.   attr_sec_reader :setdibits,     "winapi(3,'SetDIBits','iiiiipi','i')"
  577.   attr_sec_reader :selectobject,  "winapi(3,'SelectObject','ii','i')"
  578.   attr_sec_reader :getpixel,      "winapi(3,'GetPixel','iii','i')"
  579.   attr_sec_reader :setpixel,      "winapi(3,'SetPixel','liil','l')"  
  580.   attr_sec_reader :disabled_key,  "Array.new"
  581.   attr_sec_reader :high_priority, 'false'
  582.   #-------------------------------------------------------------------------
  583.   # * Define Secondary Listing
  584.   #-------------------------------------------------------------------------
  585.   define_sec_method(:disable_alt_enter) { disable_keys(0x0D) }
  586.   define_sec_method(:fullscreen?) { systemmetrix.call(0) == Graphics.width }
  587.   define_sec_method(:fullscreen)  { self.fullscreen? || toggle }
  588.   define_sec_method(:window)      { self.fullscreen? && toggle }
  589.   #-------------------------------------------------------------------------
  590.   # * Redirect Listing
  591.   #-------------------------------------------------------------------------
  592.   redirect_method :cache,      L::VX  ? '(Cache)' : '(RPG::Cache)'
  593.   redirect_method :scene,      L::VXA ? '(SceneManager.scene)':'($scene)'
  594.   redirect_method :scene=,     L::VXA ? SCENEVXA : '($scene = args[0])'
  595.   #-------------------------------------------------------------------------
  596.   # * Enable change window size via mouse (border required)
  597.   #   Screen will not resized if RMXP
  598.   #-------------------------------------------------------------------------
  599.   def control_screen_size(enable = true)
  600.     hid = @hide_border ? 0x14000000 : 0x14CA0000
  601.     set = enable ? (0x10C70000|0x00080000) : hid
  602.     self.setwindowlong.call(self.hwnd,-16, set)
  603.   end
  604.   #-------------------------------------------------------------------------
  605.   # * Hide window border
  606.   #   Screen size can't be controlled via mouse
  607.   #-------------------------------------------------------------------------
  608.   def hide_borders
  609.      control_screen_size((@hide_border = true) && false)
  610.   end
  611.   #-------------------------------------------------------------------------
  612.   # * Show window border
  613.   #   Screen size can't be controlled via mouse
  614.   #-------------------------------------------------------------------------
  615.   def show_borders
  616.     control_screen_size((@hide_border = false))
  617.   end
  618.   #-------------------------------------------------------------------------
  619.   # * Disable Key
  620.   #-------------------------------------------------------------------------
  621.   def disable_keys(*keys)
  622.     keys.each do |key|
  623.       disabled_key.include?(key) || disabled_key.push(key)
  624.       self.reghotkey.call(self.hwnd, 1, 0x0001, key)
  625.     end
  626.   end
  627.   #-------------------------------------------------------------------------
  628.   # * Resize the screen (scaled)
  629.   #   Screen will not resized if RMXP
  630.   #-------------------------------------------------------------------------
  631.   def scale_screen(width, height)
  632.     width  += ((res = self.systemmetrix).call(5) + (res.call(45))) * 2
  633.     height += (res.call(6) + (res.call(46))) * 2 + res.call(4)
  634.     x = [(res.call(0) - width) / 2, 0].max
  635.     y = [(res.call(1) - height) / 2, 0].max
  636.     self.setwindowpos(self.hwnd,0,x,y,width,height,0)
  637.   end
  638.   #-------------------------------------------------------------------------
  639.   # * Scale the game.exe to fill the monitor
  640.   #   Screen will not resized if RMXP
  641.   #-------------------------------------------------------------------------
  642.   def fill_monitor
  643.     setwindowpos(hwnd,0,0,0,(res = systemmetrix).call(0),res.call(1),0)
  644.   end
  645.   #-------------------------------------------------------------------------
  646.   # * setwindowpos
  647.   #-------------------------------------------------------------------------
  648.   def setwindowpos(hwnd = self.hwnd,at = 0,x = 0,y = 0,
  649.       width = 640, height = 480, ni = 0)
  650.     @setwindowpos ||= winapi(2, 'SetWindowPos', 'liiiiip','i')
  651.     @setwindowpos.call(hwnd, at, x, y, width, height, ni)
  652.   end
  653.   #-------------------------------------------------------------------------
  654.   # * Toggle between fullscreen and windowed
  655.   #-------------------------------------------------------------------------
  656.   def toggle
  657.     @keybd ||= winapi(2, 'keybd_event', %w(i i l l), 'v')
  658.     [@keybd.call(0xA4, 0, 0, 0), @keybd.call(13, 0, 0, 0)  ]
  659.     [@keybd.call(13, 0, 2, 0),   @keybd.call(0xA4, 0, 2, 0)]
  660.   end
  661.   #-------------------------------------------------------------------------
  662.   # * Show FPS
  663.   #-------------------------------------------------------------------------
  664.   def show_fps
  665.     @show_fps ||= winapi(2, 'keybd_event', %w(l l l l), '')
  666.     @show_fps.call(0x71,0,0,0)
  667.     sleep(0.1)
  668.     @show_fps.call(0x71,0,2,0)
  669.   end
  670.   #-------------------------------------------------------------------------
  671.   # * Get the Game Window's width and height
  672.   #-------------------------------------------------------------------------
  673.   def client_size    
  674.     @window_c_rect ||= winapi(2, 'GetClientRect', %w(l p), 'i')
  675.     @window_c_rect.call(self.hwnd, (rect = [0, 0, 0, 0].pack('l4')))
  676.     right, bottom = rect.unpack('l4')[2..3]
  677.     return right, bottom
  678.   end  
  679.   #-------------------------------------------------------------------------
  680.   # * Get the game window handle (specific to game)
  681.   #-------------------------------------------------------------------------
  682.   def hwnd
  683.     @game_window ||= winapi(2, 'FindWindowEx', %w(l l p p), 'i').
  684.                             call(0,0,"RGSS Player",0)
  685.   end  
  686.   #-------------------------------------------------------------------------
  687.   # * always_on_top  (cannot be undone)
  688.   #-------------------------------------------------------------------------
  689.   def always_on_top
  690.     res = self.systemmetrix
  691.     width  = Graphics.width  + (res.call(5) + (res.call(45))) * 2
  692.     height = Graphics.height + (res.call(6) + (res.call(46))) * 2 + res.call(4)
  693.     x = [(res.call(0) - width) / 2, 0].max
  694.     y = [(res.call(1) - height) / 2, 0].max
  695.     self.setwindowpos(self.hwnd,-1,x,y,width,height,0x0200)
  696.   end
  697.   #-------------------------------------------------------------------------
  698.   # * Get the value of game.ini
  699.   #-------------------------------------------------------------------------
  700.   def read_ini(field, key, ini = 'Game.ini')
  701.     @gpps ||= winapi(0, 'GetPrivateProfileString', 'pppplp', 'l')
  702.     @gpps.call(field,key,"",result="\0"*256,256,".//#{ini}") rescue return ""
  703.     return result.delete!("\0")
  704.   end
  705.   #-------------------------------------------------------------------------
  706.   # * Data default extension
  707.   #-------------------------------------------------------------------------
  708.   def data_default_extension
  709.     data = read_ini('Game','Scripts')
  710.     data = data.split('.').last || 'rxdata'
  711.     return data
  712.   end
  713.   #-------------------------------------------------------------------------
  714.   # * High Priority (true/false)
  715.   #-------------------------------------------------------------------------
  716.   def high_priority=(value)
  717.     @high_priority = value && true
  718.     setpriority.call(-1, @high_priority ? 0x00000080 : 0x00000020)
  719.   end
  720.   #--------------------------------------------------------------------------
  721.   # * snap_to_bitmap
  722.   #--------------------------------------------------------------------------
  723.   def snap_to_bitmap
  724.     bitmap = Bitmap.new((w = client_size.at(0)),(h = client_size.at(1)))
  725.     info   = [40,w,h,1,32,0,0,0,0,0,0].pack('LllSSLLllLL')
  726.     hDC    = ccdc.call((dc = getdc.call(hwnd)))
  727.     deleteobject.call(selectobject.call(hDC, (hBM = ccbitmap.call(dc, w, h))))
  728.     setdibits.call(hDC, hBM, 0, h, (a = bitmap.address), info, 0)
  729.     bitblt.call(hDC, 0, 0, w, h, dc, 0, 0, 0xCC0020)
  730.     getdibits.call(hDC, hBM, 0, h, a, info, 0)
  731.     deleteobject.call(hBM)
  732.     deleteobject.call(hDC)
  733.     return bitmap
  734.   end                            
  735.   #-------------------------------------------------------------------------
  736.   # * Execute Win32API
  737.   #-------------------------------------------------------------------------
  738.   def winapi(*a)
  739.     Win32API.new(*((a[0].is_a?(Integer) ? a[0] = DLL_USED[a[0]] : a) && a))
  740.   end
  741.   #----------------------------------------------------------------------------
  742.   # * unicode_to_utf8
  743.   #  string - string in Unicode format
  744.   #  Converts a string from Unicode format to UTF-8 format as RGSS does not
  745.   #  support Unicode.
  746.   #----------------------------------------------------------------------------
  747.   def unicode_to_utf8(string)
  748.     result = ''
  749.     string.unpack('L*').each do |c|
  750.       if (c1 = c < 0x0080) || (c2 = c < 0x0800) || (c3 = c < 0x10000) ||
  751.         (c4 = c < 0x200000)|| (c5 = c < 0x4000000) || (c6 = c < 0x80000000)
  752.         result += c.chr                           if c1
  753.         result += (0xC0 | (c >> 6)).chr           if c2
  754.         result += (0xE0 | (c >> 12)).chr          if c3
  755.         result += (0xF0 | (c >> 18)).chr          if c4
  756.         result += (0xF8 | (c >> 24)).chr          if c5
  757.         result += (0xFC | (c >> 30)).chr          if c6
  758.         result += (0x80 | ((c >> 24) & 0x3F)).chr if c6
  759.         result += (0x80 | ((c >> 18) & 0x3F)).chr if c5 || c6
  760.         result += (0x80 | ((c >> 12) & 0x3F)).chr if c4 || c5 || c6
  761.         result += (0x80 | ((c >> 6) & 0x3F)).chr  if c3 || c4 || c5 || c6
  762.         result += (0x80 | (c & 0x3F)).chr         if c2 || c3 || c4 || c5 || c6
  763.       end
  764.     end
  765.     return result
  766.   end
  767. end                        
  768. LiTTleDRAgo.extend(CoreDLL)
  769.  
  770. #==============================================================================
  771. # ** RPG
  772. #------------------------------------------------------------------------------
  773. #  
  774. #==============================================================================
  775. module RPG
  776.   #============================================================================
  777.   # ** System
  778.   #----------------------------------------------------------------------------
  779.   #
  780.   #============================================================================
  781.   class System
  782.     #----------------------------------------------------------------------
  783.     # ● Class Variables
  784.     #----------------------------------------------------------------------
  785.     if (VX = LiTTleDRAgo::VX)
  786.       #------------------------------------------------------------------------
  787.       # ● Public Instance Variables
  788.       #------------------------------------------------------------------------
  789.       attr_accessor :magic_number,      :windowskin_name, :gameover_name
  790.       attr_accessor :battle_transition, :battleback_name, :title_name
  791.       #------------------------------------------------------------------------
  792.       # ● Redefined Methods
  793.       #------------------------------------------------------------------------
  794.       redirect_method :sounds,            "$data_system.sounds"
  795.       redirect_method :cursor_se,         "sounds[0]"
  796.       redirect_method :decision_se,       "sounds[1]"
  797.       redirect_method :cancel_se,         "sounds[2]"
  798.       redirect_method :buzzer_se,         "sounds[3]"
  799.       redirect_method :equip_se,          "sounds[4]"
  800.       redirect_method :save_se,           "sounds[5]"
  801.       redirect_method :load_se,           "sounds[6]"
  802.       redirect_method :battle_start_se,   "sounds[7]"
  803.       redirect_method :escape_se,         "sounds[8]"
  804.       if (VXA = LiTTleDRAgo::VXA)
  805.         redirect_method :shop_se,           "sounds[21]"
  806.         redirect_method :actor_collapse_se, "sounds[15]"
  807.       else
  808.         redirect_method :shop_se,           "sounds[17]"
  809.         redirect_method :actor_collapse_se, "sounds[13]"
  810.       end
  811.       redirect_method :enemy_collapse_se, "sounds[11]"
  812.       redirect_method :words,             "@words ||= RPG::System::Words.new()"
  813.       #------------------------------------------------------------------------
  814.       # ● Aliased Methods
  815.       #------------------------------------------------------------------------
  816.       alias_sec_method :battleback_name, :battleback1_name
  817.       alias_sec_method :title_name,      :title1_name      
  818.     end
  819.     #========================================================================
  820.     # ** Words
  821.     #------------------------------------------------------------------------
  822.     #
  823.     #========================================================================
  824.     class Words      
  825.       #----------------------------------------------------------------------
  826.       # ● New Methods
  827.       #----------------------------------------------------------------------
  828.       attr_sec_accessor :params,    !(VXA = LiTTleDRAgo::VXA) ? !VX ?
  829.                                     '[hp,sp,str,dex,agi,int]' :         # XP
  830.                                     '[hp,sp,atk,pdef,int,agi]' :        # VX
  831.                                     '[hp,sp,atk,pdef,int,mdef,agi,luk]' # VXA
  832.       attr_sec_accessor :weapon1,   VX && !VXA ? 'Vocab.weapon1' : "'Weapon 1'"
  833.       attr_sec_accessor :weapon2,   VX && !VXA ? 'Vocab.weapon2' : "'Weapon 2'"
  834.       attr_sec_accessor :status,    VX ? 'Vocab.status'          : "'Status'  "
  835.       attr_sec_accessor :save,      VX ? 'Vocab.save'            : "'Save'    "
  836.       attr_sec_accessor :game_end,  VX ? 'Vocab.game_end'        : "'Game End'"
  837.       attr_sec_accessor :fight,     VX ? 'Vocab.fight'           : "'Fight'   "
  838.       attr_sec_accessor :escape,    VX ? 'Vocab.escape'          : "'Escape'  "
  839.       attr_sec_accessor :new_game,  VX ? 'Vocab.new_game'        : "'New Game'"
  840.       attr_sec_accessor :continue,  VX ? 'Vocab.continue'        : "'Continue'"
  841.       attr_sec_accessor :shutdown,  VX ? 'Vocab.shutdown'        : "'Shutdown'"
  842.       attr_sec_accessor :to_title,  VX ? 'Vocab.to_title'        : "'To Title'"
  843.       attr_sec_accessor :cancel,    VX ? 'Vocab.cancel'          : "'Cancel'  "
  844.       #----------------------------------------------------------------------
  845.       # ● Redirect Listings
  846.       #----------------------------------------------------------------------
  847.       redirect_method :gold,   (VXA ? 'Vocab.currency_unit' : 'Vocab.gold')
  848.       redirect_method :hp,     'Vocab.hp'
  849.       redirect_method :sp,     'Vocab.mp'
  850.       redirect_method :str,    'String.new()'
  851.       redirect_method :dex,    'String.new()'
  852.       redirect_method :luk,    (VXA ? 'Vocab.param(7)' : 'String.new()')
  853.       redirect_method :agi,    (VXA ? 'Vocab.param(6)' : 'Vocab.agi')
  854.       redirect_method :int,    (VXA ? 'Vocab.param(4)' : 'Vocab.spi')
  855.       redirect_method :atk,    (VXA ? 'Vocab.param(2)' : 'Vocab.atk')
  856.       redirect_method :pdef,   (VXA ? 'Vocab.param(3)' : 'Vocab.def')
  857.       redirect_method :mdef,   (VXA ? 'Vocab.param(5)' : 'String.new()')
  858.       redirect_method :weapon, (VXA ? 'Vocab.etype(0)' : 'Vocab.weapon')
  859.       redirect_method :armor1, (VXA ? 'Vocab.etype(1)' : 'Vocab.armor1')
  860.       redirect_method :armor2, (VXA ? 'Vocab.etype(2)' : 'Vocab.armor2')
  861.       redirect_method :armor3, (VXA ? 'Vocab.etype(3)' : 'Vocab.armor3')
  862.       redirect_method :armor4, (VXA ? 'Vocab.etype(4)' : 'Vocab.armor4')
  863.       redirect_method :attack, 'Vocab.attack'
  864.       redirect_method :skill,  'Vocab.skill'
  865.       redirect_method :guard,  'Vocab.guard'
  866.       redirect_method :item,   'Vocab.item'
  867.       redirect_method :equip,  'Vocab.equip'
  868.     end
  869.   end
  870. end
  871. #==============================================================================
  872. # ** RPG_FileTest
  873. #------------------------------------------------------------------------------
  874. #
  875. #==============================================================================
  876. module RPG_FileTest
  877.   #--------------------------------------------------------------------------
  878.   # ● New method: self.exist?
  879.   #--------------------------------------------------------------------------
  880.   def self.exist?(filename)
  881.     dir,ext = File.dirname(filename) + '/', File.extname(filename)
  882.     base = File.basename(filename, ext)
  883.     if ['.png','.jpg','.bmp'].include?(ext.downcase)
  884.       cache = defined?(Cache) ? Cache : RPG::Cache
  885.       (cache.load_bitmap(dir,base) && true) rescue false
  886.     elsif ['.rxdata','.rvdata','.rvdata2','.rb','.txt'].include?(ext.downcase)
  887.       (load_data(filename) && true) rescue false
  888.     else
  889.       File.exist?(filename)
  890.     end
  891.   end
  892. end
  893.  
  894. #==============================================================================
  895. # ** Sound
  896. #------------------------------------------------------------------------------
  897. #  This module plays sound effects. It obtains sound effects specified in the
  898. # database from the global variable $data_system, and plays them.
  899. #==============================================================================
  900. module Sound;  end
  901. class << Sound
  902.   #------------------------------------------------------------------------
  903.   # ● New Method :se_play
  904.   #------------------------------------------------------------------------
  905.   unless method_defined?(:se_play)
  906.     def se_play(type)
  907.       system = ($game_system ||= Game_System.new)
  908.       case type
  909.       when :cursor         then system.se_play($data_system.cursor_se)
  910.       when :decision       then system.se_play($data_system.decision_se)
  911.       when :cancel         then system.se_play($data_system.cancel_se)
  912.       when :buzzer         then system.se_play($data_system.buzzer_se)
  913.       when :shop           then system.se_play($data_system.shop_se)
  914.       when :equip          then system.se_play($data_system.equip_se)
  915.       when :save           then system.se_play($data_system.save_se)
  916.       when :load           then system.se_play($data_system.load_se)
  917.       when :battle_start   then system.se_play($data_system.battle_start_se)
  918.       when :escape         then system.se_play($data_system.escape_se)
  919.       when :actor_collapse then system.se_play($data_system.actor_collapse_se)
  920.       when :enemy_collapse then system.se_play($data_system.enemy_collapse_se)
  921.       end
  922.     end
  923.   end
  924.   #--------------------------------------------------------------------------
  925.   # * Redefined Method
  926.   #--------------------------------------------------------------------------
  927.   define_sec_method(:play_cursor)         { se_play(:cursor)         }
  928.   define_sec_method(:play_decision)       { se_play(:decision)       }
  929.   define_sec_method(:play_cancel)         { se_play(:cancel)         }
  930.   define_sec_method(:play_buzzer)         { se_play(:buzzer)         }
  931.   define_sec_method(:play_equip)          { se_play(:equip)          }
  932.   define_sec_method(:play_save)           { se_play(:save)           }
  933.   define_sec_method(:play_load)           { se_play(:load)           }
  934.   define_sec_method(:play_battle_start)   { se_play(:battle_start)   }
  935.   define_sec_method(:play_escape)         { se_play(:escape)         }
  936.   define_sec_method(:play_enemy_collapse) { se_play(:enemy_collapse) }
  937.   define_sec_method(:play_actor_collapse) { se_play(:actor_collapse) }
  938.   define_sec_method(:play_shop)           { se_play(:shop)           }
  939.   #--------------------------------------------------------------------------
  940.   # * Alias Listing
  941.   #--------------------------------------------------------------------------
  942.   alias_sec_method :play_ok, :play_decision
  943. end
  944.  
  945. #==============================================================================
  946. # ** Game_System
  947. #------------------------------------------------------------------------------
  948. #  This class handles data surrounding the system. Backround music, etc.
  949. #  is managed here as well. Refer to "$game_system" for the instance of
  950. #  this class.
  951. #==============================================================================
  952. class Game_System
  953.   #--------------------------------------------------------------------------
  954.   # * Constant
  955.   #--------------------------------------------------------------------------
  956.   VX = LiTTleDRAgo::VX && !LiTTleDRAgo::VXA
  957.   #--------------------------------------------------------------------------
  958.   # * Public Instance Variable
  959.   #--------------------------------------------------------------------------
  960.   attr_sec_accessor :bgm_volume    , 100
  961.   attr_sec_accessor :bgs_volume    , 100
  962.   attr_sec_accessor :me_volume     , 100
  963.   attr_sec_accessor :se_volume     , 100
  964.   attr_sec_accessor :bgm_pitch     , 100
  965.   attr_sec_accessor :bgs_pitch     , 100
  966.   attr_sec_accessor :me_pitch      , 100
  967.   attr_sec_accessor :se_pitch      , 100
  968.   attr_sec_accessor :volume_control, 'false'
  969.   #--------------------------------------------------------------------------
  970.   # * Redefined method: bgm_memorize, bgm_restore
  971.   #--------------------------------------------------------------------------
  972.   VX && define_sec_method(:bgm_memorize) { @memorized_bgm = @playing_bgm }
  973.   VX && define_sec_method(:bgm_restore)  { bgm_play(@memorized_bgm)     }
  974.   #--------------------------------------------------------------------------
  975.   # * Redirect Listing
  976.   #--------------------------------------------------------------------------
  977.   redirect_method :bgm_stop,     'Audio.bgm_stop'
  978.   redirect_method :bgs_stop,     'Audio.bgs_stop'
  979.   redirect_method :se_stop,      'Audio.se_stop'
  980.   redirect_method :me_stop,      'Audio.me_stop'
  981.   #--------------------------------------------------------------------------
  982.   # * Alias Listing
  983.   #--------------------------------------------------------------------------
  984.   alias_sec_method :bgm_memorize, :save_bgm
  985.   alias_sec_method :bgm_restore,  :replay_bgm
  986.   alias_sec_method :save_bgm,     :bgm_memorize
  987.   alias_sec_method :replay_bgm,   :bgm_restore
  988.   #--------------------------------------------------------------------------
  989.   # * Aliased method: bgm_play, bgs_play, me_play, se_play
  990.   #--------------------------------------------------------------------------
  991.   [:bgm,:bgs,:me,:se].each do |sa|
  992.     next unless method_defined?(:"#{sa}_play")
  993.     alias_sec_method(:"drg_#{sa}_play", :"#{sa}_play")
  994.     define_method(:"#{sa}_play") do |bgm,*a|
  995.       if bgm.is_a?(String) && bgm != ''
  996.         bgm = RPG::AudioFile.new(bgm,send(:"#{sa}_volume"),send(:"#{sa}_pitch"))
  997.       elsif volume_control && bgm.is_a?(RPG::AudioFile) && bgm.name != ""
  998.         bgm = bgm.clone
  999.         bgm.volume = Math.percent(bgm.volume,send(:"#{sa}_volume"))
  1000.         bgm.pitch  = Math.percent(bgm.pitch,send(:"#{sa}_pitch"))
  1001.       end
  1002.       @playing_bgm = bgm if "#{sa}" == 'bgm'
  1003.       @playing_bgs = bgm if "#{sa}" == 'bgs'
  1004.       if respond_to?(:"drg_#{sa}_play")
  1005.         send(:"drg_#{sa}_play", bgm,*a)
  1006.       else
  1007.         if bgm.is_a?(RPG::AudioFile) && bgm.name != ''
  1008.           Audio.send(:"#{sa}_play","Audio/#{sa}/" + bgm.name,
  1009.              bgm.volume.round, bgm.pitch.round,*a)
  1010.         else
  1011.           send(:"#{sa}_stop")
  1012.         end
  1013.         Graphics.frame_reset
  1014.       end
  1015.     end
  1016.   end
  1017. end
  1018.  
  1019. #==============================================================================
  1020. # ** Object
  1021. #------------------------------------------------------------------------------
  1022. #  This class is superclass for all class
  1023. #==============================================================================
  1024. class Object
  1025.   #-------------------------------------------------------------------------
  1026.   # * New method: all_variable_dispose
  1027.   #-------------------------------------------------------------------------
  1028.   def all_variable_dispose
  1029.     all = instance_variables.map {|s| instance_variable_get("#{s}")}.flatten
  1030.     all.delete_if {|s| s.not.respond_to?(:dispose) }
  1031.     all.delete_if {|s| s.respond_to?(:disposed?) && s.disposed?}
  1032.     all.dispose
  1033.   end
  1034.   #-------------------------------------------------------------------------
  1035.   # * New method: rand_between
  1036.   #-------------------------------------------------------------------------
  1037.   unless method_defined?(:rand_between)
  1038.     def rand_between(min, max)
  1039.       min + rand(max - min + 1) if min.is_a?(Numeric) && max.is_a?(Numeric)
  1040.     end
  1041.   end
  1042.   #-------------------------------------------------------------------------
  1043.   # * New method: get_note
  1044.   #-------------------------------------------------------------------------
  1045.   unless method_defined?(:get_note)
  1046.     def get_note
  1047.       respond_to?(:note) ? note : ""
  1048.     end
  1049.   end
  1050.   #-------------------------------------------------------------------------
  1051.   # * New method: screen_rect
  1052.   #-------------------------------------------------------------------------
  1053.   unless method_defined?(:screen_rect)
  1054.     def screen_rect(as_rect = true)
  1055.       array = [0,0,Graphics.width,Graphics.height]
  1056.       as_rect ? Rect.new(*array) : array
  1057.     end
  1058.   end
  1059.   #-------------------------------------------------------------------------
  1060.   # * New method: forcesave
  1061.   #-------------------------------------------------------------------------
  1062.   unless method_defined?(:forcesave)
  1063.     def forcesave(index)
  1064.       return DataManager.save_game(index) if defined?(DataManager)
  1065.       save = LiTTleDRAgo::VX ? Scene_File.new(0,nil,0) : Scene_Save.new
  1066.       begin
  1067.         file = File.open(save.make_filename(index), "wb")
  1068.         (save.write_save_data(file) && file.close) || true
  1069.       rescue
  1070.         File.delete(save.make_filename(index)) rescue nil
  1071.         (Sound.play_buzzer) && false
  1072.       end
  1073.     end
  1074.   end
  1075.   #--------------------------------------------------------------------------
  1076.   # * sprite_report
  1077.   #--------------------------------------------------------------------------
  1078.   unless method_defined?(:script_report)
  1079.     def script_report(text,font = nil)
  1080.       s = (text.is_a?(String) || (text = text.inspect)) && Sprite.new
  1081.       s.opacity = (s.bitmap = Bitmap.new((g = Graphics).width,g.height)) && 0
  1082.       s.bitmap.font.name = ['Georgia',Font.default_name].flatten
  1083.       s.bitmap.font = (s.z += 9999) && (font || s.bitmap.font)
  1084.       y = (text.split(/\n/).size * s.bitmap.text_size(text).height) / 2
  1085.       s.bitmap.draw_enter_edging_text(0,0-y,g.width,g.height,text,1)
  1086.       (s.opacity += 10) && g.update until s.opacity >= 255
  1087.       g.wait_for_input
  1088.       (s.opacity -= 10) && g.update until s.opacity <= 0
  1089.       s.dispose
  1090.     end
  1091.   end
  1092.   #-------------------------------------------------------------------------
  1093.   # * New method: not
  1094.   #-------------------------------------------------------------------------
  1095.   define_sec_method(:not) { Not.new(self) }
  1096.   #============================================================================
  1097.   # ** Not
  1098.   #----------------------------------------------------------------------------
  1099.   #
  1100.   #============================================================================
  1101.   class Not
  1102.     #-------------------------------------------------------------------------
  1103.     # * Private
  1104.     #-------------------------------------------------------------------------
  1105.     private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }
  1106.     #-------------------------------------------------------------------------
  1107.     # * Object Initialization
  1108.     #-------------------------------------------------------------------------
  1109.     define_method(:initialize) {|original|  @original = original }
  1110.     #-------------------------------------------------------------------------
  1111.     # * New method: method_missing
  1112.     #-------------------------------------------------------------------------
  1113.     def method_missing(sym, *a, &blk)
  1114.       !@original.send(sym, *a, &blk)
  1115.     end
  1116.   end
  1117. end
  1118.  
  1119. #==============================================================================
  1120. # ** Kernel
  1121. #------------------------------------------------------------------------------
  1122. #  
  1123. #==============================================================================
  1124. module Kernel
  1125.   #-------------------------------------------------------------------------
  1126.   # * Alias Listing
  1127.   #-------------------------------------------------------------------------
  1128.   $@ || alias_method(:carrot, :proc)
  1129.   #-------------------------------------------------------------------------
  1130.   # * Script Check
  1131.   #-------------------------------------------------------------------------
  1132.   if LiTTleDRAgo::XP && LiTTleDRAgo::RGSS3 && !defined?(XPA_CONFIG)
  1133.     $@ || alias_method(:last_before_p,        :p)
  1134.     $@ || alias_method(:last_before_print,    :print)
  1135.     $@ || alias_method(:last_before_msgbox,   :msgbox)
  1136.     $@ || alias_method(:last_before_msgbox_p, :msgbox_p)
  1137.     define_method(:p)       {|*args| last_before_msgbox_p(*args)}
  1138.     define_method(:print)   {|*args| last_before_msgbox(*args)  }
  1139.     define_method(:msgbox)  {|*args| last_before_print(*args)   }
  1140.     define_method(:msgbox_p){|*args| last_before_p(*args)       }
  1141.   end
  1142.   define_method(:draise) {|*args| (dp(*args) && !1) || exit }
  1143.   define_method(:dp)     {|*a| script_report(a.map {|i| i.inspect}.join("\n"))}
  1144.   #-------------------------------------------------------------------------
  1145.   # * New method: dprint
  1146.   #-------------------------------------------------------------------------
  1147.   def dprint(*args)
  1148.     script_report(args.map {|i| i.is_a?(String) ? i : i.inspect}.join)
  1149.   end
  1150.   #-------------------------------------------------------------------------
  1151.   # * New method: press_any_key
  1152.   #-------------------------------------------------------------------------
  1153.   unless method_defined?(:press_any_key)
  1154.     def press_any_key
  1155.       return false if Input.nil?
  1156.       return true if Input.trigger?(Input::A)    ||Input.trigger?(Input::B)
  1157.       return true if Input.trigger?(Input::C)    ||Input.trigger?(Input::X)
  1158.       return true if Input.trigger?(Input::Y)    ||Input.trigger?(Input::Z)
  1159.       return true if Input.trigger?(Input::L)    ||Input.trigger?(Input::R)
  1160.       return true if Input.trigger?(Input::UP)   ||Input.trigger?(Input::DOWN)
  1161.       return true if Input.trigger?(Input::LEFT) ||Input.trigger?(Input::RIGHT)
  1162.       return true if Input.trigger?(Input::SHIFT)||Input.trigger?(Input::CTRL)
  1163.       return true if Input.trigger?(Input::ALT)  ||Input.trigger?(Input::F5)
  1164.       return true if Input.trigger?(Input::F6)   ||Input.trigger?(Input::F7)
  1165.       return true if Input.trigger?(Input::F8)   ||Input.trigger?(Input::F9)
  1166.     end
  1167.   end
  1168.   #-------------------------------------------------------------------------
  1169.   # * New method: inspect_instance_variable
  1170.   #-------------------------------------------------------------------------
  1171.   unless method_defined?(:inspect_instance_variable)
  1172.     def inspect_instance_variable(class_name, variable)
  1173.       Module.const_get(class_name).new.instance_variable_get(:"@#{variable}")
  1174.     end
  1175.   end
  1176. end
  1177.  
  1178. #==============================================================================
  1179. # ** Proc
  1180. #------------------------------------------------------------------------------
  1181. #  
  1182. #==============================================================================
  1183. class Proc
  1184.   #-------------------------------------------------------------------------
  1185.   # * New method: bind
  1186.   #-------------------------------------------------------------------------
  1187.   unless method_defined?(:bind)
  1188.     def bind(object)
  1189.       block, time = self, Time.now
  1190.       class << object
  1191.         self
  1192.       end.class_eval do
  1193.         define_method((name = "__bind_#{time.to_i}_#{time.usec}"), &block)
  1194.         remove_method((method = instance_method(name)) && name)
  1195.         method
  1196.       end.bind(object)
  1197.     end
  1198.   end
  1199. end
  1200. #==============================================================================
  1201. # ** Enumerable
  1202. #------------------------------------------------------------------------------
  1203. #  
  1204. #==============================================================================
  1205. module Enumerable
  1206.   #---------------------------------------------------------------------------
  1207.   # * Redefined method: shuffle
  1208.   #---------------------------------------------------------------------------
  1209.   define_sec_method(:shuffle) {(a = entries) && Array.new(size){ a.random!}}
  1210.   #---------------------------------------------------------------------------
  1211.   # * New method: random_each
  1212.   #---------------------------------------------------------------------------
  1213.   unless method_defined?(:random_each)
  1214.     def random_each() (block_given? ? shuffle.each {|s| yield s } : random) end
  1215.   end
  1216.   #---------------------------------------------------------------------------
  1217.   # * New method: random_each_with_index
  1218.   #---------------------------------------------------------------------------
  1219.   unless method_defined?(:random_each_with_index)
  1220.     def random_each_with_index
  1221.       block_given? ? self.shuffle.each_with_index {|obj, i| yield obj,i } :
  1222.         self.random                                    
  1223.     end
  1224.   end
  1225.   #---------------------------------------------------------------------------
  1226.   # * Redefined method: drop_while
  1227.   #---------------------------------------------------------------------------
  1228.   unless method_defined?(:drop_while)
  1229.     def drop_while(&block)
  1230.       ary, state = [], false
  1231.       self.each do |e|
  1232.         state = true if !state and !block.call(e)
  1233.         ary << e if state
  1234.       end
  1235.       ary
  1236.     end
  1237.   end
  1238.   #---------------------------------------------------------------------------
  1239.   # * Redefined method: take_while
  1240.   #---------------------------------------------------------------------------
  1241.   unless method_defined?(:take_while)
  1242.     def take_while(&block)
  1243.       ary = []
  1244.       self.each do |e|
  1245.         return ary unless block.call(e)
  1246.         ary << e
  1247.       end
  1248.       ary
  1249.     end
  1250.   end
  1251.   #---------------------------------------------------------------------------
  1252.   # * Redefined method: partition
  1253.   #---------------------------------------------------------------------------
  1254.   unless method_defined?(:partition)
  1255.     def partition(&block)
  1256.       ary_T, ary_F = [], []
  1257.       self.each {|val| block.call(val) ? ary_T.push(val) : ary_F.push(val)}
  1258.       return ary_T, ary_F
  1259.     end
  1260.   end
  1261.   #---------------------------------------------------------------------------
  1262.   # * Redefined method: grep
  1263.   #---------------------------------------------------------------------------
  1264.   unless method_defined?(:grep)
  1265.     def grep(pattern, &block)
  1266.       collect {|v| pattern === v && ary.push((block)? block.call(v): v)}
  1267.     end
  1268.   end
  1269.   #---------------------------------------------------------------------------
  1270.   # * Redefined method: each_slice
  1271.   #---------------------------------------------------------------------------
  1272.   unless method_defined?(:each_slice)
  1273.     def each_slice(n, &block)
  1274.       not_integer = "expected Integer for 1st argument"
  1275.       n.is_a?(Integer) || raise(TypeError,not_integer,caller(1))
  1276.       n <= 0 && raise(ArgumentError, "invalid slice size",caller(1))
  1277.       ary = []
  1278.       self.each do |e|
  1279.         ary << e
  1280.         if ary.size == n
  1281.           block.call(ary)
  1282.           ary = []
  1283.         end
  1284.       end
  1285.       block.call(ary) unless ary.empty?
  1286.     end
  1287.   end
  1288.   #---------------------------------------------------------------------------
  1289.   # * Redefined method: minmax
  1290.   #---------------------------------------------------------------------------
  1291.   unless method_defined?(:minmax)
  1292.     def minmax(&block) (a = sort(&block)) && [a.first,a.last]  end
  1293.   end
  1294. end
  1295.  
  1296. #==============================================================================
  1297. # ** Array
  1298. #------------------------------------------------------------------------------
  1299. #  
  1300. #==============================================================================
  1301. class Array
  1302.   #---------------------------------------------------------------------------
  1303.   # * Define Secondary Listing
  1304.   #---------------------------------------------------------------------------
  1305.   define_sec_method(:have_all?)     { |*array| self & array == array        }
  1306.   define_sec_method(:have_any?)     { |*array| self - array != self         }
  1307.   define_sec_method(:relay)         { |type| map {|s| s.send(:"to_#{type}")}}
  1308.   #---------------------------------------------------------------------------
  1309.   # * Redirect method
  1310.   #---------------------------------------------------------------------------
  1311.   redirect_method :switch_on,      'self.switch = (true)'                  
  1312.   redirect_method :switch_off,     'self.switch = (false)'                  
  1313.   redirect_method :random,         'self.at(rand(size))'          
  1314.   redirect_method :random!,        'self.delete_at(rand(size))'
  1315.   redirect_method :sum,            'self.inject(0) {|r, n| r += n}'
  1316.   redirect_method :prod,           'self.inject(1) {|r, n| r *= n}'
  1317.   redirect_method :next_item,      '(item = shift) && push(item) &&  (item)'
  1318.   redirect_method :previous_item,  '(item = pop) && unshift(item) && (item)'
  1319.   redirect_method :nitems,         'count {|x| x.not.nil?}'
  1320.   redirect_method :switch_reverse, 'switch=(:flip)'
  1321.   #---------------------------------------------------------------------------
  1322.   # * New method: method_missing
  1323.   #---------------------------------------------------------------------------
  1324.   def method_missing(val,*a,&b)
  1325.     en = self.entries.find_all {|s|s.respond_to?(val.to_sym)}
  1326.     if self.not.empty? && en.empty?
  1327.       text = "Undefined method #{val} for #{self.inspect}"
  1328.       raise(NoMethodError,text,caller(1))
  1329.     end
  1330.     return en.map {|s| s.send(val.to_s,*a,&b)}
  1331.   end
  1332.   #-------------------------------------------------------------------------
  1333.   # * New method: each_ntuple
  1334.   #-------------------------------------------------------------------------
  1335.   unless method_defined?(:each_ntuple)
  1336.     def each_ntuple(number)
  1337.       0.step(size - size % number - 1, number) {|i| yield(*self[i, number])}
  1338.     end
  1339.   end
  1340.   #-------------------------------------------------------------------------
  1341.   # * New method: each_pair
  1342.   #-------------------------------------------------------------------------
  1343.   unless method_defined?(:each_pair)
  1344.     def each_pair()   each_ntuple(2) {|a,b| yield a, b }       end
  1345.   end
  1346.   #---------------------------------------------------------------------------
  1347.   # * New method: each_triple
  1348.   #---------------------------------------------------------------------------
  1349.   unless method_defined?(:each_triple)
  1350.     def each_triple() each_ntuple(3) {|a,b,c| yield a, b, c }  end
  1351.   end
  1352.   #---------------------------------------------------------------------------
  1353.   # * Redefined method: shuffle! (for RMXP)
  1354.   #---------------------------------------------------------------------------
  1355.   unless method_defined?(:shuffle!)
  1356.     def shuffle!()  self.dup == replace(shuffle) ? nil : self  end
  1357.   end
  1358.   #---------------------------------------------------------------------------
  1359.   # * New method: sort_by!
  1360.   #---------------------------------------------------------------------------
  1361.   unless method_defined?(:sort_by!)
  1362.     def sort_by!(*args,&block)
  1363.       self.dup == replace(sort_by(*args,&block)) ? nil : self
  1364.     end
  1365.   end
  1366.   #---------------------------------------------------------------------------
  1367.   # * New method: recursive_clone
  1368.   #---------------------------------------------------------------------------
  1369.   unless method_defined?(:rclone)
  1370.     def recursive_clone
  1371.       (clon = self.clone).each_index do |i|
  1372.         clon[i] = clon[i].recursive_clone rescue clon[i].clone rescue clon[i]
  1373.       end
  1374.       clon
  1375.     end
  1376.     alias_method(:rclone, :recursive_clone)
  1377.   end
  1378.   #---------------------------------------------------------------------------
  1379.   # * New method: to_hash
  1380.   #---------------------------------------------------------------------------
  1381.   unless method_defined?(:to_hash)
  1382.     def to_hash
  1383.       (hash = Hash.new) && each_index {|i| hash[i] = self[i]} && hash
  1384.     end
  1385.   end  
  1386.   #---------------------------------------------------------------------------
  1387.   # * Redefined method: count
  1388.   #---------------------------------------------------------------------------
  1389.   unless method_defined?(:count)
  1390.     def count(*args, &block)
  1391.       if block_given?
  1392.         find_all {|s| block.call(s)}.size
  1393.       else
  1394.         ary = args.empty? ? self : args.map {|a| find_all{|s| s == a}}
  1395.         ary.compact.size
  1396.       end
  1397.     end
  1398.   end
  1399.   #---------------------------------------------------------------------------
  1400.   # * New method: each_with_next
  1401.   #---------------------------------------------------------------------------
  1402.   unless method_defined?(:each_with_next)
  1403.     def each_with_next(num = 1)
  1404.       (size - num).times {|i| yield(*self[i..(i+num)])}
  1405.     end
  1406.   end
  1407.   #---------------------------------------------------------------------------
  1408.   # * New method: switch=
  1409.   #---------------------------------------------------------------------------
  1410.   unless method_defined?(:switch=)
  1411.     def switch=(value)
  1412.       integer = search_var_key[0]
  1413.       array   = search_var_key[1]
  1414.       if value == :flip
  1415.         integer.each {|i| $game_switches[i] = !$game_switches[i] }
  1416.         array.each {|i| $game_self_switches[i] = !$game_self_switches[i] }
  1417.       else
  1418.         integer.each {|i| $game_switches[i] = value }
  1419.         array.each {|i| $game_self_switches[i] = value }
  1420.       end
  1421.       $game_map.need_refresh = true if $game_map.respond_to?(:need_refresh)
  1422.     end
  1423.   end
  1424.   #---------------------------------------------------------------------------
  1425.   # * New method: variable
  1426.   #---------------------------------------------------------------------------
  1427.   def variable(value = nil,method = "=")
  1428.     integer = search_var_key[0]  
  1429.     integer += search_var_key[1] if $drago_game_variable
  1430.     return integer.map {|i| $game_variables[i]} if value.nil?
  1431.     result = integer.map {|i| eval("$game_variables[i] #{method} #{value}") }
  1432.     $game_map.need_refresh = true if $game_map.respond_to?(:need_refresh)
  1433.     result
  1434.   end
  1435.   #---------------------------------------------------------------------------
  1436.   # * New method: search_var_key
  1437.   #---------------------------------------------------------------------------
  1438.   def search_var_key
  1439.     integer = self.find_all {|s| s.is_a?(Integer)}
  1440.     range   = self.find_all {|s| s.is_a?(Range)  }
  1441.     array   = self.find_all {|s| s.is_a?(Array)  }
  1442.     string  = self.find_all {|s| s.is_a?(String) }
  1443.     integer = integer + range.collect {|s| s.to_a }.flatten
  1444.     $game_switches      ||= Game_Switches.new
  1445.     $game_self_switches ||= Game_SelfSwitches.new
  1446.     $game_variables     ||= Game_Variables.new
  1447.     return [integer.sort.uniq, array.sort.uniq]
  1448.   end
  1449.   #---------------------------------------------------------------------------
  1450.   # * New method: rindexes
  1451.   #---------------------------------------------------------------------------
  1452.   unless method_defined?(:rindexes)
  1453.     def rindexes(*values)
  1454.       array = Array.new
  1455.       each_index {|i| values.include?(self[i]) && array.push(i)}
  1456.       array
  1457.     end
  1458.   end
  1459.   #---------------------------------------------------------------------------
  1460.   # * New method: deep
  1461.   #---------------------------------------------------------------------------
  1462.   unless method_defined?(:deep)
  1463.     def deep(deep = -1)
  1464.       tmp_deep = deep + 1
  1465.       deep = tmp_deep
  1466.       self.each do |i|
  1467.         deep < i.deep(tmp_deep) && deep = i.deep(tmp_deep)  rescue nil
  1468.       end
  1469.       deep
  1470.     end
  1471.   end
  1472.   #---------------------------------------------------------------------------
  1473.   # * New method: recursive_flatten
  1474.   #---------------------------------------------------------------------------
  1475.   unless method_defined?(:rflatten)
  1476.     def recursive_flatten
  1477.       (array = self.recursive_clone).each_index do |i|
  1478.         array[i] = array[i].to_a if array[i].is_a?(Hash)
  1479.         array[i] = array[i].recursive_flatten rescue array[i]
  1480.       end
  1481.       array.flatten!
  1482.       array
  1483.     end
  1484.     alias_method(:rflatten, :recursive_flatten)
  1485.   end
  1486.   #---------------------------------------------------------------------------
  1487.   # * New method: recursive_flatten!
  1488.   #---------------------------------------------------------------------------
  1489.   unless method_defined?(:rflatten!)
  1490.     def recursive_flatten!
  1491.       self.each_index do |i|
  1492.         self[i] = self[i].to_a if self[i].is_a?(Hash)
  1493.         self[i] = self[i].recursive_flatten! rescue self[i]
  1494.       end
  1495.       self.flatten!
  1496.       self
  1497.     end
  1498.     alias_method(:rflatten!, :recursive_flatten!)
  1499.   end
  1500.   #---------------------------------------------------------------------------
  1501.   # * Redefined method: product
  1502.   #---------------------------------------------------------------------------
  1503.   def dproduct(*arrays)
  1504.     (e = "(result = []) && self.each {|i0| ") &&
  1505.     (a = arrays.size).times {|i| e += "arrays[#{i}].each {|i#{i+1}| " }
  1506.     (e += "result.push([i0,") && a.times {|i| e += "i#{i+1}," }
  1507.     (e += "])}" + "}" * a + " && result"  )
  1508.     return eval(e)
  1509.   end
  1510.   alias_sec_method(:product, :dproduct)
  1511.   #---------------------------------------------------------------------------
  1512.   # * New method: geometric_average
  1513.   #---------------------------------------------------------------------------
  1514.   unless method_defined?(:geometric_average)
  1515.     def geometric_average(float = false)
  1516.       average = empty? ? 0 : prod ** (1.0 / size)
  1517.       return (float ? average : average.to_i)
  1518.     end
  1519.   end
  1520.   #---------------------------------------------------------------------------
  1521.   # * New method: average
  1522.   #---------------------------------------------------------------------------
  1523.   unless method_defined?(:average)
  1524.     def average(float = false)
  1525.       sum / [(float ? size.to_f : size.to_i), 1].max
  1526.     end
  1527.   end
  1528.   #---------------------------------------------------------------------------
  1529.   # * Alias method
  1530.   #---------------------------------------------------------------------------
  1531.   alias_sec_method :has_all?, :have_all?
  1532.   alias_sec_method :has_any?, :have_any?
  1533. end
  1534.  
  1535. #==============================================================================
  1536. # ** Hash
  1537. #------------------------------------------------------------------------------
  1538. #  
  1539. #==============================================================================
  1540. class Hash
  1541.   #--------------------------------------------------------------------------
  1542.   # * Define Secondary Listing
  1543.   #--------------------------------------------------------------------------
  1544.   define_sec_method(:reverse!)  { self.dup == replace(reverse) ? nil : self }
  1545.   #--------------------------------------------------------------------------
  1546.   # ● New method: reverse
  1547.   #--------------------------------------------------------------------------
  1548.   unless method_defined?(:reverse)
  1549.     def reverse
  1550.       key, new_hash = self.keys, {}
  1551.       key.reverse.each_with_index {|k,i| new_hash[k] = self[key[i]]}
  1552.       new_hash
  1553.     end
  1554.   end
  1555.   #--------------------------------------------------------------------------
  1556.   # ● New method: recursive_clone
  1557.   #--------------------------------------------------------------------------
  1558.   unless method_defined?(:rclone)
  1559.     def recursive_clone
  1560.       (clon = self.clone).each_key do |i|
  1561.         clon[i] = clon[i].recursive_clone rescue clon[i].clone rescue clon[i]
  1562.       end
  1563.       clon
  1564.     end
  1565.     alias_method(:rclone, :recursive_clone)
  1566.   end
  1567.   #--------------------------------------------------------------------------
  1568.   # ● New method: deep
  1569.   #--------------------------------------------------------------------------
  1570.   unless method_defined?(:deep)
  1571.     def deep(deep = -1)
  1572.       key_deep = value_deep = tmp_deep = deep + 1
  1573.       self.each do |k, v|
  1574.         key_deep = k.deep(tmp_deep)  if key_deep  < k.deep(tmp_deep) rescue nil
  1575.         value_deep = v.deep(tmp_deep)if value_deep< v.deep(tmp_deep) rescue nil
  1576.       end
  1577.       key_deep > value_deep ? key_deep : value_deep
  1578.     end
  1579.   end
  1580.   #--------------------------------------------------------------------------
  1581.   # ● New method: fusion
  1582.   #--------------------------------------------------------------------------
  1583.   unless method_defined?(:fusion)
  1584.     def fusion
  1585.       (array = sort).each_index do |i|
  1586.         array[i] = array[i][0] + array[i][1] rescue array[i]
  1587.       end
  1588.       array
  1589.     end
  1590.   end
  1591. end
  1592. #==============================================================================
  1593. # ** Range    
  1594. #------------------------------------------------------------------------------
  1595. #  
  1596. #==============================================================================
  1597. class Range
  1598.   #--------------------------------------------------------------------------
  1599.   # * Include Comparable
  1600.   #--------------------------------------------------------------------------
  1601.   include(Comparable)
  1602.   #-------------------------------------------------------------------------
  1603.   # ● New method: random
  1604.   #-------------------------------------------------------------------------
  1605.   define_sec_method(:random) { self.to_a.random }
  1606.   define_sec_method(:sum)    { self.to_a.sum }
  1607.   define_sec_method(:prod)   { self.to_a.prod }
  1608.   #-------------------------------------------------------------------------
  1609.   # ● New method: <=>
  1610.   #-------------------------------------------------------------------------
  1611.   unless method_defined?(:"<=>")
  1612.     def <=>(other)
  1613.       temp = first <=> other.first
  1614.       temp = last <=> other.last if temp = 0
  1615.       return temp
  1616.     end
  1617.   end
  1618. end
  1619. #==============================================================================
  1620. # ** NilClass
  1621. #------------------------------------------------------------------------------
  1622. #  
  1623. #==============================================================================
  1624. class NilClass
  1625.   #--------------------------------------------------------------------------
  1626.   # ● Overwriten method: clone, dup
  1627.   #--------------------------------------------------------------------------
  1628.   define_method(:clone) {|*a|}
  1629.   define_method(:dup)   {|*a|}
  1630. end
  1631.  
  1632. #==============================================================================
  1633. # ** Math
  1634. #------------------------------------------------------------------------------
  1635. #  
  1636. #==============================================================================
  1637. module Math
  1638.   #--------------------------------------------------------------------------
  1639.   # ● New method: A (Ackermann function)
  1640.   #--------------------------------------------------------------------------
  1641.   def self.A(m,n)
  1642.     text = 'Ackermann: No negative values allowed.' if m < 0 or n < 0
  1643.     raise(ArgumentError.new(text))                  if m < 0 or n < 0
  1644.     return n + 1 if m == 0
  1645.     return Math.A(m - 1, 1) if n == 0
  1646.     return Math.A(m - 1, Math.A(m, n - 1))
  1647.   end
  1648.   #--------------------------------------------------------------------------
  1649.   # ● New method: percent
  1650.   #--------------------------------------------------------------------------
  1651.   def self.percent(min,max=100)
  1652.     (min / max.to_f) * 100.0
  1653.   end
  1654. end
  1655.  
  1656. #==============================================================================
  1657. # ** Numeric
  1658. #------------------------------------------------------------------------------
  1659. #  
  1660. #==============================================================================
  1661. class Numeric
  1662.   #--------------------------------------------------------------------------
  1663.   # ● Define Secondary Listing: clamp, sign
  1664.   #--------------------------------------------------------------------------
  1665.   define_sec_method(:clamp) {|min,max|  [[self, min].max, max].min}
  1666.   define_sec_method(:sign)  { zero? ? 0 :  (self / self.abs).to_i }
  1667.   #--------------------------------------------------------------------------
  1668.   # ● New method: group
  1669.   #--------------------------------------------------------------------------
  1670.   unless method_defined?(:group)
  1671.     def group
  1672.       self.to_s.gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
  1673.     end
  1674.   end
  1675. end
  1676.  
  1677. #==============================================================================
  1678. # ** Integer
  1679. #------------------------------------------------------------------------------
  1680. #  
  1681. #==============================================================================
  1682. class Integer
  1683.   #--------------------------------------------------------------------------
  1684.   # ● Define Secondary Listing: sum_to, factorial
  1685.   #--------------------------------------------------------------------------
  1686.   define_sec_method(:sum_down)  { sum_to(0)}
  1687.   define_sec_method(:factorial) {(sign * (1..(self.abs)).prod)  }
  1688.   #--------------------------------------------------------------------------
  1689.   # * New method: sum_to
  1690.   #--------------------------------------------------------------------------
  1691.   unless method_defined?(:sum_to)
  1692.     def sum_to(v = self) (v > self ? (self..v) : (v..self)).sum   end
  1693.   end
  1694. end
  1695.  
  1696. #==============================================================================
  1697. # ** Float
  1698. #------------------------------------------------------------------------------
  1699. #  
  1700. #==============================================================================
  1701. class Float
  1702.   #--------------------------------------------------------------------------
  1703.   # ● Overwriten method: =~
  1704.   #--------------------------------------------------------------------------
  1705.   # A close float comparison. Because of rounding errors, this comparison will
  1706.   # help compensate for that. Since 0 and 0.0001 are very close to each other,
  1707.   # it's best to use this custom comparison.
  1708.   define_method(:"=~") {|num| (self + 0.0001 >= num && self - 0.0001 <= num) }
  1709.   #--------------------------------------------------------------------------
  1710.   # ● New method: float_points
  1711.   #--------------------------------------------------------------------------
  1712.   unless method_defined?(:float_points)
  1713.     def float_points(points = 2)
  1714.       n = self * 10 ** points
  1715.       n = n.round.to_f / (10.0 ** points)
  1716.       return n
  1717.     end
  1718.   end
  1719. end
  1720.  
  1721. #==============================================================================
  1722. # ** Color
  1723. #------------------------------------------------------------------------------
  1724. #  
  1725. #==============================================================================
  1726. class Color
  1727.   #--------------------------------------------------------------------------
  1728.   # ● Define Secondary Listing
  1729.   #--------------------------------------------------------------------------
  1730.   define_sec_method(:invert)   { set(255-red, 255-green, 255-blue, alpha)}
  1731.   #----------------------------------------------------------------------------
  1732.   # * New method: to_hex
  1733.   #----------------------------------------------------------------------------
  1734.   def to_hex
  1735.     str = [self.blue.to_i, self.green.to_i, self.red.to_i].pack("C*")
  1736.     str.inspect.gsub('\\x','')
  1737.   end  
  1738. end
  1739. class << Color
  1740.   #--------------------------------------------------------------------------
  1741.   # ● Define Secondary Listing: red, green, blue, white, black... etc
  1742.   #--------------------------------------------------------------------------
  1743.   define_sec_method(:red)      {|*a| self.new(255,  0,  0,a.first||255)}
  1744.   define_sec_method(:green)    {|*a| self.new(  0,255,  0,a.first||255)}
  1745.   define_sec_method(:blue)     {|*a| self.new(  0,  0,255,a.first||255)}
  1746.   define_sec_method(:white)    {|*a| self.new(255,255,255,a.first||255)}
  1747.   define_sec_method(:purple)   {|*a| self.new(128,  0,128,a.first||255)}
  1748.   define_sec_method(:gray)     {|*a| self.new(128,128,128,a.first||255)}
  1749.   define_sec_method(:lgray)    {|*a| self.new(211,211,211,a.first||255)}
  1750.   define_sec_method(:dgray)    {|*a| self.new( 64, 64, 64,a.first||255)}
  1751.   define_sec_method(:pink)     {|*a| self.new(255,175,175,a.first||255)}
  1752.   define_sec_method(:orange)   {|*a| self.new(255,165,  0,a.first||255)}
  1753.   define_sec_method(:brown)    {|*a| self.new(128, 64,  0,a.first||255)}
  1754.   define_sec_method(:chocolate){|*a| self.new(210,105, 30,a.first||255)}
  1755.   define_sec_method(:golden)   {|*a| self.new(218,165, 32,a.first||255)}
  1756.   define_sec_method(:silver)   {|*a| self.new(192,192,192,a.first||255)}
  1757.   define_sec_method(:system)   {|*a| self.new(192,224,255,a.first||255)}
  1758.   define_sec_method(:crisis)   {|*a| self.new(255,255, 64,a.first||255)}
  1759.   define_sec_method(:knockout) {|*a| self.new(255, 64,  0,a.first||255)}
  1760.   #--------------------------------------------------------------------------
  1761.   # ● Redirect Listing: cyan, magenta, yellow, black, disabled, erase
  1762.   #--------------------------------------------------------------------------
  1763.   redirect_method :cyan,       '(red(*args).invert  )'
  1764.   redirect_method :magenta,    '(green(*args).invert)'
  1765.   redirect_method :yellow,     '(blue(*args).invert )'
  1766.   redirect_method :black,      '(white(*args).invert)'
  1767.   redirect_method :disabled,   '(black(128).invert  )'
  1768.   redirect_method :erase,      '(white(0).invert    )'
  1769.   #--------------------------------------------------------------------------
  1770.   # ● Alias method: grey, light_grey, dark_grey
  1771.   #--------------------------------------------------------------------------
  1772.   alias_sec_method :normal,    :white
  1773.   alias_sec_method :grey,      :gray
  1774.   alias_sec_method :lightgrey, :lightgray, :light_grey, :light_gray, :lgray
  1775.   alias_sec_method :darkgrey,  :darkgray,  :dark_grey,  :dark_gray,  :dgray
  1776.   #--------------------------------------------------------------------------
  1777.   # ● New method: method_missing
  1778.   #--------------------------------------------------------------------------
  1779.   def method_missing(val,*a)
  1780.     val = "#{val}".gsub(/x/i,'')
  1781.     if "#{val}".size == 6 && "#{val}".gsub(/(\d+|[a-f])/i,'')
  1782.       r = "#{val}"[0..1].hex
  1783.       g = "#{val}"[2..3].hex
  1784.       b = "#{val}"[4..5].hex
  1785.       return self.new(r,g,b,a.first||255)
  1786.     end
  1787.     text = "Undefined method #{val} for #{self.inspect}"
  1788.     raise(NoMethodError, text, caller(1))              
  1789.   end
  1790. end
  1791.  
  1792. #==============================================================================
  1793. # ** Viewport
  1794. #------------------------------------------------------------------------------
  1795. #  
  1796. #==============================================================================
  1797. class Viewport
  1798.   unless method_defined?(:disposed?)
  1799.     #--------------------------------------------------------------------------
  1800.     # ● Alias Listing
  1801.     #--------------------------------------------------------------------------
  1802.     alias_sec_method :d3x40932s2, :dispose
  1803.     #--------------------------------------------------------------------------
  1804.     # ● New method: disposed?
  1805.     #   help file is lying when says Viewport#disposed? is exist
  1806.     #--------------------------------------------------------------------------
  1807.     define_method(:disposed?) { @disposed == true }
  1808.     #--------------------------------------------------------------------------
  1809.     # ● Aliased method: dispose
  1810.     #--------------------------------------------------------------------------
  1811.     def dispose(*a)
  1812.       (disposed? || d3x40932s2(*a) || 0) && @disposed = true
  1813.     end
  1814.   end
  1815.   #--------------------------------------------------------------------------
  1816.   # ● New method: resize
  1817.   #--------------------------------------------------------------------------
  1818.   define_sec_method(:resize) do |*a|
  1819.     self.rect = a.first.is_a?(Rect) ? a.first : Rect.new(*a)
  1820.   end
  1821.   #--------------------------------------------------------------------------
  1822.   # * New method: update_viewport_sizes
  1823.   #--------------------------------------------------------------------------
  1824.   define_sec_method(:update_viewport_sizes) do |*a|
  1825.     map = $game_map
  1826.     w, h = Graphics.width, Graphics.height
  1827.     hor = map.respond_to?(:loop_horizontal?) && map.loop_horizontal?
  1828.     ver = map.respond_to?(:loop_vertical?)   && map.loop_vertical?
  1829.     dx = w > map.width  * 32 && !hor ? (w - map.width  * 32) / 2 : 0
  1830.     dw = hor ? w : [w, map.width * 32].min
  1831.     dy = h > map.height * 32 && !ver ? (h - map.height * 32) / 2 : 0
  1832.     dh = ver ? h : [h, map.height * 32].min
  1833.     resize(Rect.new(dx, dy, dw, dh))
  1834.   end
  1835.   #--------------------------------------------------------------------------
  1836.   # ● New method: x, y, width, height
  1837.   #--------------------------------------------------------------------------
  1838.   define_sec_method(:x)       { self.rect.x      }
  1839.   define_sec_method(:y)       { self.rect.y      }
  1840.   define_sec_method(:width)   { self.rect.width  }
  1841.   define_sec_method(:height)  { self.rect.height }
  1842.   define_sec_method(:x=)      {|x2| resize(x2,y,width,height) }
  1843.   define_sec_method(:y=)      {|y2| resize(x,y2,width,height) }
  1844.   define_sec_method(:width=)  {|w1| resize(x,y,w1,height)     }
  1845.   define_sec_method(:height=) {|h1| resize(x,y,width,h1)      }
  1846. end
  1847. #==============================================================================
  1848. # ** Graphics
  1849. #------------------------------------------------------------------------------
  1850. #  This module handles all Graphics
  1851. #==============================================================================
  1852. class << Graphics
  1853.   #------------------------------------------------------------------------
  1854.   # ● Include AutoUpdate
  1855.   #------------------------------------------------------------------------
  1856.   include_auto_update
  1857.   #------------------------------------------------------------------------
  1858.   # ● Redirect Listing
  1859.   #------------------------------------------------------------------------
  1860.   redirect_method :width,              'LiTTleDRAgo.client_size.at(0)'
  1861.   redirect_method :height,             'LiTTleDRAgo.client_size.at(1)'
  1862.   redirect_method :snap_to_bitmap,     'LiTTleDRAgo.snap_to_bitmap'    
  1863.   redirect_method :disable_alt_enter,  'LiTTleDRAgo.disable_alt_enter'
  1864.   redirect_method :always_on_top,      'LiTTleDRAgo.always_on_top'
  1865.   redirect_method :show_fps,           'LiTTleDRAgo.show_fps'
  1866.   redirect_method :scale_screen,       'LiTTleDRAgo.scale_screen'
  1867.   redirect_method :fill_monitor,       'LiTTleDRAgo.fill_monitor'
  1868.   redirect_method :window,             'LiTTleDRAgo.window'
  1869.   redirect_method :toggle,             'LiTTleDRAgo.toggle'
  1870.   redirect_method :fullscreen,         'LiTTleDRAgo.fullscreen'
  1871.   redirect_method :fullscreen?,        'LiTTleDRAgo.fullscreen?'
  1872.   redirect_method :control_screen_size,'LiTTleDRAgo.control_screen_size'
  1873.   redirect_method :high_priority,      'LiTTleDRAgo.high_priority'
  1874.   redirect_method :high_priority=,     'LiTTleDRAgo.high_priority=args.at(0)'
  1875.   #------------------------------------------------------------------------
  1876.   # ● Alias Listing
  1877.   #------------------------------------------------------------------------
  1878.   alias_sec_method :resize_screen, :scale_screen
  1879.   #--------------------------------------------------------------------------
  1880.   # * New method: fadeoutviewport
  1881.   #--------------------------------------------------------------------------
  1882.   def fadeoutviewport
  1883.     @fadeoutviewport  ||= ((v = Viewport.new(0,0,width,height))
  1884.         (v.color = (v.z = 0x3FFFFFFF) && Color.new(0,0,0,0)) && v)
  1885.   end
  1886.   #--------------------------------------------------------------------------
  1887.   # ● Redefined method:  wait,   brightness
  1888.   #--------------------------------------------------------------------------
  1889.   define_sec_method(:wait)       { |frame| frame.times {|i| update }   }
  1890.   define_sec_method(:brightness) { (255 - fadeoutviewport.color.alpha) }
  1891.   #--------------------------------------------------------------------------
  1892.   # * Redefined method: brightness= (Set Graphics Brightness)
  1893.   #--------------------------------------------------------------------------
  1894.   unless method_defined?(:brightness=)
  1895.     def brightness=(value)
  1896.       fadeoutviewport.color.alpha = 255 - value.clamp(0,255)
  1897.     end
  1898.   end
  1899.   #--------------------------------------------------------------------------
  1900.   # * Redefined method: fadein (Graphics Fade In)
  1901.   #         frame : frame of fade
  1902.   #--------------------------------------------------------------------------
  1903.   unless method_defined?(:fadein)
  1904.     def fadein(frames = 10)
  1905.       return if frames <= 0
  1906.       curvalue, count = brightness, (255 - brightness)
  1907.       frames.times do |i|
  1908.         (self.brightness = curvalue + (count * i / frames)) && update
  1909.       end
  1910.     end
  1911.   end
  1912.   #--------------------------------------------------------------------------
  1913.   # * Redefined method: fadeout (Graphics Fade Out)
  1914.   #         frame : frame of fade
  1915.   #--------------------------------------------------------------------------
  1916.   unless method_defined?(:fadeout)
  1917.     def fadeout(frames = 10)
  1918.       return if frames <= 0
  1919.       curvalue = count = self.brightness
  1920.       frames.times do |i|
  1921.         (self.brightness = curvalue - (count * i / frames)) && update
  1922.       end
  1923.     end
  1924.   end
  1925.   #--------------------------------------------------------------------------
  1926.   # * New method: wait_for_input
  1927.   #--------------------------------------------------------------------------
  1928.   unless method_defined?(:wait_for_input)
  1929.     def wait_for_input
  1930.       [Input,self].update
  1931.       [Input,self].update until press_any_key
  1932.     end
  1933.   end
  1934. end
  1935.  
  1936. #==============================================================================
  1937. # ** Input
  1938. #------------------------------------------------------------------------------
  1939. #  This module handles all Input
  1940. #==============================================================================
  1941. class << Input
  1942.   #------------------------------------------------------------------------
  1943.   # ● Include AutoUpdate
  1944.   #------------------------------------------------------------------------
  1945.   include_auto_update
  1946.   #--------------------------------------------------------------------------
  1947.   # ● Aliased method: :press?, :repeat?, :trigger?
  1948.   #--------------------------------------------------------------------------
  1949.   [:press?,:repeat?,:trigger?].each do |method|
  1950.     next unless method_defined?(:"#{method}")
  1951.     alias_sec_method(:"sym_#{method}", :"#{method}")
  1952.     define_method(:"#{method}") do |*args|
  1953.       if (b = args.first).is_a?(Symbol) || b.is_a?(String)
  1954.         args[0] = Input.const_get("#{b}") rescue nil
  1955.       end
  1956.       (args.first.not.nil?) && send(:"sym_#{method}", *args)
  1957.     end
  1958.   end
  1959. end
  1960.  
  1961. #==============================================================================
  1962. # ** Game_Variables
  1963. #------------------------------------------------------------------------------
  1964. #  This class handles variables. It's a wrapper for the built-in class "Array."
  1965. # The instance of this class is referenced by $game_variables.
  1966. #==============================================================================
  1967. class Game_Variables
  1968.   #--------------------------------------------------------------------------
  1969.   # ● Alias Listing
  1970.   #--------------------------------------------------------------------------
  1971.   alias_sec_method(:"adv_var1",:"[]")
  1972.   alias_sec_method(:"adv_var2",:"[]=")
  1973.   #--------------------------------------------------------------------------
  1974.   # * Get Variable
  1975.   #--------------------------------------------------------------------------
  1976.   def [](id)
  1977.     id.is_a?(Integer) ? adv_var1(id) : ((@data_ex||={})[id] || 0)
  1978.   end
  1979.   #--------------------------------------------------------------------------
  1980.   # * Set Variable
  1981.   #--------------------------------------------------------------------------
  1982.   def []=(id, val)
  1983.     prev = send(:"[]",id)
  1984.     id.is_a?(Integer) ? adv_var2(id,val) : ((@data_ex||={})[id] = val)
  1985.     on_change if respond_to?(:on_change) && prev != val
  1986.   end
  1987. end
  1988.  
  1989. #==============================================================================
  1990. # ** Sprite_Battler
  1991. #------------------------------------------------------------------------------
  1992. #  This sprite is used to display the battler.It observes the Game_Character
  1993. #  class and automatically changes sprite conditions.
  1994. #==============================================================================
  1995. class Sprite_Battler
  1996.   #--------------------------------------------------------------------------
  1997.   # * Frame Update
  1998.   #--------------------------------------------------------------------------
  1999.   define_pre_alias(:update) {  @battler.nil? && @battler_name = nil }
  2000. end
  2001.  
  2002. #==============================================================================
  2003. # ** Spriteset_Map
  2004. #------------------------------------------------------------------------------
  2005. #  This class brings together map screen sprites, tilemaps, etc.
  2006. #  It's used within the Scene_Map class.
  2007. #==============================================================================
  2008. class Spriteset_Map
  2009.   #--------------------------------------------------------------------------
  2010.   # ● New method: sprite_player
  2011.   #--------------------------------------------------------------------------
  2012.   define_sec_method(:sprite_player) { find_character($game_player) }
  2013.   #------------------------------------------------------------------------
  2014.   # ● Public Instance Variables
  2015.   #------------------------------------------------------------------------
  2016.   attr_sec_reader :character_sprites, 'Array.new'
  2017.   attr_reader     :viewport1, :viewport2, :viewport3
  2018.   #--------------------------------------------------------------------------
  2019.   # * Aliased method: update
  2020.   #--------------------------------------------------------------------------
  2021.   define_pre_alias(:update) { update_viewport_size_change }
  2022.   #--------------------------------------------------------------------------
  2023.   # * New method: update_viewport_size_change
  2024.   #--------------------------------------------------------------------------
  2025.   def update_viewport_size_change
  2026.     if viewport_size_change?
  2027.       @viewport_map_width     = $game_map.width
  2028.       @viewport_map_height    = $game_map.height
  2029.       @viewport_screen_width  = Graphics.width
  2030.       @viewport_screen_height = Graphics.height
  2031.       [@viewport1,@viewport2,@viewport3].compact.update_viewport_sizes
  2032.     end
  2033.   end
  2034.   #--------------------------------------------------------------------------
  2035.   # * New method: viewport_size_change?
  2036.   #--------------------------------------------------------------------------
  2037.   unless method_defined?(:viewport_size_change?)
  2038.     def viewport_size_change?
  2039.       return true if @viewport_map_width     != $game_map.width
  2040.       return true if @viewport_map_height    != $game_map.height
  2041.       return true if @viewport_screen_width  != Graphics.width
  2042.       return true if @viewport_screen_height != Graphics.height
  2043.     end
  2044.   end
  2045.   #--------------------------------------------------------------------------
  2046.   # ● New method: redraw_character_sprites
  2047.   #--------------------------------------------------------------------------
  2048.   unless method_defined?(:redraw_character_sprites)
  2049.     def redraw_character_sprites
  2050.       return refresh_characters if self.respond_to?(:refresh_characters)
  2051.       character_sprites.dispose
  2052.       @character_sprites = $game_map.events.keys.sort.map {|i|
  2053.          Sprite_Character.new(@viewport1, $game_map.events[i]) }
  2054.       @character_sprites.push(Sprite_Character.new(@viewport1, $game_player))
  2055.     end
  2056.   end  
  2057.   #--------------------------------------------------------------------------
  2058.   # ● New method: find_character
  2059.   #--------------------------------------------------------------------------
  2060.   unless method_defined?(:find_character)
  2061.     def find_character(char)
  2062.       character_sprites.detect { |s| s.character == char }
  2063.     end
  2064.   end
  2065.   #---------------------------------------------------------------------------
  2066.   # * New method: viewport_sprite
  2067.   #---------------------------------------------------------------------------
  2068.   def viewport_sprite(*v)
  2069.     v.collect! {|s| s.is_a?(Symbol) ? instance_variable_get(:"@#{s}") : s }
  2070.     v.reject!  {|s| s.not.is_a?(Viewport)}
  2071.     all = instance_variables.map {|s| instance_variable_get("#{s}")}.flatten
  2072.     all.select {|s| s.respond_to?(:viewport) && v.include?(s.viewport)}
  2073.   end
  2074. end
  2075.  
  2076. #==============================================================================
  2077. # ** Game_Battler
  2078. #------------------------------------------------------------------------------
  2079. #  This class deals with battlers. It's used as a superclass for the Game_Actor
  2080. #  and Game_Enemy classes.
  2081. #==============================================================================
  2082. class Game_Battler
  2083.   #--------------------------------------------------------------------------
  2084.   # ● New method: alive?
  2085.   #--------------------------------------------------------------------------
  2086.   define_sec_method(:alive?) { self.not.dead? }
  2087.   #--------------------------------------------------------------------------
  2088.   # ● New method: hp_percent
  2089.   #--------------------------------------------------------------------------
  2090.   unless method_defined?(:hp_percent)
  2091.     def hp_percent(integer = false, float_points = 2)
  2092.       n = Math.percent(hp,(respond_to?(:mhp) ? mhp : maxhp))
  2093.       return integer ? Integer(n) : n.float_points(float_points)
  2094.     end
  2095.   end
  2096.   #--------------------------------------------------------------------------
  2097.   # ● New method: sp_percent
  2098.   #--------------------------------------------------------------------------
  2099.   unless method_defined?(:sp_percent)
  2100.     def sp_percent(integer = false, float_points = 2)
  2101.       n =  Math.percent((respond_to?(:sp) ? sp : mp),
  2102.       ((respond_to?(:mmp) ? mmp : (respond_to?(:maxsp) ? maxsp : maxmp))))
  2103.       return integer ? Integer(n) : n.float_points(float_points)
  2104.     end
  2105.     alias_method :mp_percent, :sp_percent
  2106.   end
  2107. end
  2108.  
  2109. #==============================================================================
  2110. # ** Game_Enemy
  2111. #------------------------------------------------------------------------------
  2112. #  This class handles enemies. It's used within the Game_Troop class
  2113. #  ($game_troop).
  2114. #==============================================================================
  2115. class Game_Enemy
  2116.   if LiTTleDRAgo::XP
  2117.     #--------------------------------------------------------------------------
  2118.     # * Alias Listing
  2119.     #--------------------------------------------------------------------------
  2120.     $@ || alias_method(:initenemyfix, :initialize)
  2121.     #--------------------------------------------------------------------------
  2122.     # * Object Initialization
  2123.     #--------------------------------------------------------------------------
  2124.     def initialize(troop_id, index)
  2125.       fix = troop_id == 0
  2126.       initenemyfix(fix ? 1 : troop_id,fix ? 0 : index)
  2127.       if fix
  2128.         enemy = $data_enemies[@enemy_id = index]
  2129.         @battler_name,@battler_hue = enemy.battler_name, enemy.battler_hue
  2130.         @hp,@sp = maxhp, maxsp
  2131.         @hidden = @immortal = !(@troop_id = @member_index = 0)
  2132.       end
  2133.     end
  2134.   end
  2135. end
  2136.  
  2137. #==============================================================================
  2138. # ** Game_Character
  2139. #------------------------------------------------------------------------------
  2140. #  This class deals with characters. It's used as a superclass for the
  2141. #  Game_Player and Game_Event classes.
  2142. #==============================================================================
  2143. class Game_Character
  2144.   #--------------------------------------------------------------------------
  2145.   # ● Redirect Listing
  2146.   #--------------------------------------------------------------------------
  2147.   redirect_method :move_down,        'move_straight(2,*args)'
  2148.   redirect_method :move_left,        'move_straight(4,*args)'
  2149.   redirect_method :move_right,       'move_straight(6,*args)'
  2150.   redirect_method :move_up,          'move_straight(8,*args)'
  2151.   redirect_method :turn_down,        'set_direction(2,*args)'
  2152.   redirect_method :turn_left,        'set_direction(4,*args)'
  2153.   redirect_method :turn_right,       'set_direction(6,*args)'
  2154.   redirect_method :turn_up,          'set_direction(8,*args)'
  2155.   redirect_method :move_lower_left,  'move_diagonal(4, 2,*args)'
  2156.   redirect_method :move_lower_right, 'move_diagonal(6, 2,*args)'
  2157.   redirect_method :move_upper_left,  'move_diagonal(4, 8,*args)'
  2158.   redirect_method :move_upper_right, 'move_diagonal(6, 8,*args)'
  2159.   redirect_method :character_sprite, '$game_map.character_sprite(self)'
  2160.   redirect_method :character_above?, '(args.first.y > @y)'
  2161.   redirect_method :character_below?, '(args.first.y < @y)'
  2162.   redirect_method :character_right?, '(args.first.x < @x)'
  2163.   redirect_method :character_left?,  '(args.first.x > @x)'
  2164.   redirect_method :pos,              '[@x, @y]'
  2165.   redirect_method :pos_nt?,          '!@through && pos?'
  2166.   redirect_method :normal_priority?, '@character_name != ""'  
  2167.   redirect_method :map_passable?,    '$game_map.passable?'
  2168.   redirect_method :map_terrain_tag,  '$game_map.terrain_tag'
  2169.   #--------------------------------------------------------------------------
  2170.   # * Redefined method: pos?
  2171.   #                     anime
  2172.   #                     reverse_dir
  2173.   #--------------------------------------------------------------------------
  2174.   define_sec_method(:reverse_dir) {|dir| 10 - dir }
  2175.   define_sec_method(:pos?)        {|x,y| @x == x && @y == y  }
  2176.   define_sec_method(:anime)       {|aid| @animation_id = aid }
  2177.   #--------------------------------------------------------------------------
  2178.   # ● Redefined method: check_event_trigger_touch_front
  2179.   #--------------------------------------------------------------------------
  2180.   unless method_defined?(:check_event_trigger_touch_front)
  2181.     def check_event_trigger_touch_front
  2182.       x2 = $game_map.round_x_with_direction(@x, @direction)
  2183.       y2 = $game_map.round_y_with_direction(@y, @direction)
  2184.       check_event_trigger_touch(x2, y2)
  2185.     end
  2186.   end
  2187.   #--------------------------------------------------------------------------
  2188.   # ● Redefined method: move_straight
  2189.   #--------------------------------------------------------------------------
  2190.   unless method_defined?(:move_straight)
  2191.     def move_straight(d, turn_ok = true)
  2192.       if passable?(@x, @y, d)
  2193.         set_direction(d)
  2194.         @x = $game_map.round_x_with_direction(@x, d)
  2195.         @y = $game_map.round_y_with_direction(@y, d)
  2196.         increase_steps
  2197.       elsif turn_ok
  2198.         set_direction(d)
  2199.         check_event_trigger_touch_front
  2200.       end
  2201.     end
  2202.   end  
  2203.   #--------------------------------------------------------------------------
  2204.   # ● Redefined method: set_direction
  2205.   #--------------------------------------------------------------------------
  2206.   unless method_defined?(:set_direction)
  2207.     def set_direction(d)
  2208.       @direction = d unless @direction_fix || d == 0
  2209.       @stop_count = 0
  2210.     end
  2211.   end
  2212.   #--------------------------------------------------------------------------
  2213.   # ● Redefined method: set_graphic
  2214.   #--------------------------------------------------------------------------
  2215.   unless method_defined?(:set_graphic)
  2216.     def set_graphic(character_name, character_index = 0)
  2217.       @tile_id = 0
  2218.       @character_name = character_name
  2219.       if self.respond_to?(:character_index)
  2220.         @character_index = character_index
  2221.       else
  2222.         @character_hue = character_index
  2223.       end
  2224.       @original_pattern = 1
  2225.     end
  2226.   end
  2227.   #--------------------------------------------------------------------------
  2228.   # ● Redefined method: swap
  2229.   #--------------------------------------------------------------------------
  2230.   unless method_defined?(:swap)
  2231.     def swap(character)
  2232.       new_pos = character.pos
  2233.       character.moveto(x, y)
  2234.       moveto(*new_pos)
  2235.     end
  2236.   end
  2237.   #--------------------------------------------------------------------------
  2238.   # ● Redefined method: rotate_swap_prev
  2239.   #--------------------------------------------------------------------------
  2240.   unless method_defined?(:rotate_swap_prev)
  2241.     def rotate_swap_prev(*character)
  2242.       character.unshift(self)
  2243.       position = character.collect {|c| c.pos}
  2244.       character.each_with_index { |c,i| c.moveto(*position[i-1]) }
  2245.     end
  2246.   end
  2247.   #--------------------------------------------------------------------------
  2248.   # ● Redefined method: rotate_swap_next
  2249.   #--------------------------------------------------------------------------
  2250.   unless method_defined?(:rotate_swap_next)
  2251.     def rotate_swap_next(*character)
  2252.       character.unshift(self)
  2253.       position = character.collect {|c| c.pos}
  2254.       character.each_with_index do |char,index|
  2255.         char.moveto(*position[index + 1 >= position.size ? 0 : index + 1])
  2256.       end
  2257.     end
  2258.   end
  2259.   #--------------------------------------------------------------------------
  2260.   # ● New method: distance_from
  2261.   #--------------------------------------------------------------------------
  2262.   unless method_defined?(:distance_from)
  2263.     def distance_from(character,type= 'xy')
  2264.       character = $game_map.events[character] if character.is_a?(Integer)
  2265.       return 0 if character.nil?
  2266.       return (@x - character.x).abs if type.to_s == 'x'
  2267.       return (@y - character.y).abs if type.to_s == 'y'
  2268.       return Math.hypot(@x - character.x, @y - character.y)
  2269.     end
  2270.   end
  2271.   #--------------------------------------------------------------------------
  2272.   # * Redefined method: distance_x_from
  2273.   #--------------------------------------------------------------------------
  2274.   unless method_defined?(:distance_x_from)
  2275.     def distance_x_from(x)
  2276.       result = @x - x
  2277.       if result.abs > $game_map.width / 2
  2278.         result += result < 0 ? $game_map.width : -$game_map.width
  2279.       end
  2280.       result
  2281.     end
  2282.   end
  2283.   #--------------------------------------------------------------------------
  2284.   # * Redefined method: distance_y_from
  2285.   #--------------------------------------------------------------------------
  2286.   unless method_defined?(:distance_y_from)
  2287.     def distance_y_from(y)
  2288.       result = @y - y
  2289.       if result.abs > $game_map.height / 2
  2290.         result += result < 0 ? $game_map.height : -$game_map.height
  2291.       end
  2292.       result
  2293.     end
  2294.   end
  2295.   #--------------------------------------------------------------------------
  2296.   # ● New method: random_teleport
  2297.   #--------------------------------------------------------------------------
  2298.   unless method_defined?(:random_teleport)
  2299.     def random_teleport
  2300.       1000.times do
  2301.         moveto(rand($game_map.height), rand($game_map.width))
  2302.         vx  = LiTTleDRAgo::VX && !LiTTleDRAgo::VXA
  2303.         arg = vx ? [@x,@y] : [@x,@y,@direction]
  2304.         break if passable?(*arg)
  2305.       end || true
  2306.     end
  2307.   end
  2308.   #--------------------------------------------------------------------------
  2309.   # * Redefined method: diagonal_passable? (for RMXP)
  2310.   #     horz : Horizontal (4 or 6)
  2311.   #     vert : Vertical (2 or 8)
  2312.   #--------------------------------------------------------------------------
  2313.   unless method_defined?(:diagonal_passable?)
  2314.     def diagonal_passable?(x, y, horz, vert)
  2315.       x2 = $game_map.round_x_with_direction(x, horz)
  2316.       y2 = $game_map.round_y_with_direction(y, vert)
  2317.       (passable?(x, y, vert) && passable?(x, y2, horz)) ||
  2318.       (passable?(x, y, horz) && passable?(x2, y, vert))
  2319.     end
  2320.   end
  2321.   #--------------------------------------------------------------------------
  2322.   # * Redefined method: collide_with_characters? (for RMXP)
  2323.   #--------------------------------------------------------------------------
  2324.   unless method_defined?(:collide_with_characters?)
  2325.     def collide_with_characters?(x, y)
  2326.       collide_with_events?(x, y)
  2327.     end
  2328.   end
  2329.   #--------------------------------------------------------------------------
  2330.   # * Redefined method: collide_with_events? (for RMXP)
  2331.   #--------------------------------------------------------------------------
  2332.   unless method_defined?(:collide_with_events?)
  2333.     def collide_with_events?(x, y)
  2334.       $game_map.events_xy_nt(x, y).any? do |event|
  2335.         event.normal_priority? || self.is_a?(Game_Event)
  2336.       end
  2337.     end
  2338.   end
  2339.   #--------------------------------------------------------------------------
  2340.   # * Redefined method: debug_through?
  2341.   #--------------------------------------------------------------------------
  2342.   unless method_defined?(:debug_through?)
  2343.     def debug_through?
  2344.       debug = LiTTleDRAgo::VX ? $TEST : $DEBUG
  2345.       return debug && Input.press?(Input::CTRL) if self.is_a?(Game_Player)
  2346.       return false
  2347.     end
  2348.   end
  2349. end
  2350. #==============================================================================
  2351. # ** Game_Actor
  2352. #------------------------------------------------------------------------------
  2353. #  This class handles actors. It is used within the Game_Actors class
  2354. # ($game_actors), also referenced from the Game_Party class ($game_party).
  2355. #==============================================================================
  2356.  
  2357. class Game_Actor
  2358.   #--------------------------------------------------------------------------
  2359.   # * Get Actor Object
  2360.   #--------------------------------------------------------------------------
  2361.   define_sec_method(:actor)  { $data_actors[@actor_id]  }
  2362.   define_method(:class)      { $data_classes[@class_id] }
  2363. end
  2364.  
  2365. #==============================================================================
  2366. # ** Game_Enemy
  2367. #------------------------------------------------------------------------------
  2368. #  This class handles enemies. It used within the Game_Troop class
  2369. # ($game_troop).
  2370. #==============================================================================
  2371.  
  2372. class Game_Enemy
  2373.   #--------------------------------------------------------------------------
  2374.   # * Get Enemy Object
  2375.   #--------------------------------------------------------------------------
  2376.   define_sec_method(:enemy)  { $data_enemies[@enemy_id] }
  2377. end
  2378.  
  2379. #==============================================================================
  2380. # ** Game_Player
  2381. #------------------------------------------------------------------------------
  2382. #  This class handles the player. It includes event starting determinants and
  2383. # map scrolling functions. The instance of this class is referenced by
  2384. # $game_player.
  2385. #==============================================================================
  2386.  
  2387. class Game_Player
  2388.   #--------------------------------------------------------------------------
  2389.   # ● Redefined method: actor
  2390.   #                     name
  2391.   #--------------------------------------------------------------------------
  2392.   define_sec_method(:actor) { $game_party.leader }
  2393.   define_sec_method(:name)  { actor.name         }        
  2394.   #--------------------------------------------------------------------------
  2395.   # ● New method: method_missing
  2396.   #--------------------------------------------------------------------------
  2397.   def method_missing(val,*a,&b)
  2398.     return actor.send(val.to_s,*a,&b) if actor.respond_to?(val.to_sym)
  2399.     text = "Undefined method #{val} for #{self.inspect}"
  2400.     raise(NoMethodError, text, caller(1))              
  2401.   end
  2402. end
  2403.  
  2404. #==============================================================================
  2405. # ** Game_Event
  2406. #------------------------------------------------------------------------------
  2407. #  This class deals with events. It handles functions including event page
  2408. #  switching via condition determinants, and running parallel process events.
  2409. #  It's used within the Game_Map class.
  2410. #==============================================================================
  2411. class Game_Event < Game_Character
  2412.   #--------------------------------------------------------------------------
  2413.   # ● Public Instance Variables
  2414.   #--------------------------------------------------------------------------
  2415.   method_defined?(:erased) || (attr_reader :erased)
  2416.   method_defined?(:id)     || (attr_reader :id)
  2417.   method_defined?(:event)  || (attr_reader :event)
  2418.   attr_accessor :loaded_event
  2419.   #--------------------------------------------------------------------------
  2420.   # ● New method: name
  2421.   # ● New method: restore
  2422.   #--------------------------------------------------------------------------
  2423.   define_sec_method(:name)    { @event.name }
  2424.   define_sec_method(:restore) { (@erased = false) || refresh }
  2425.   #--------------------------------------------------------------------------
  2426.   # ● New method: destroy
  2427.   #--------------------------------------------------------------------------
  2428.   unless method_defined?(:destroy)
  2429.     def destroy
  2430.       event = $game_map.events.delete(@id)
  2431.       if (s = $game_map.spriteset).is_a?(Spriteset_Map)
  2432.         after = s.character_sprites.select { |e| e if e.character != event }
  2433.         (delet = character_sprite) && delet.dispose
  2434.         s.character_sprites.replace(after)
  2435.       end
  2436.     end
  2437.   end
  2438.   #--------------------------------------------------------------------------
  2439.   # ● New method: eliminate
  2440.   #--------------------------------------------------------------------------
  2441.   unless method_defined?(:eliminate)
  2442.     def eliminate
  2443.       return destroy if self.loaded_event
  2444.       (eliminate = $game_map.eliminate_event)[(id = $game_map.map_id)] ||= []
  2445.       (eliminate[id].include?(@id) || eliminate[id].push(@id)) && destroy
  2446.     end
  2447.   end
  2448.   #--------------------------------------------------------------------------
  2449.   # ● New method: note
  2450.   #--------------------------------------------------------------------------
  2451.   unless method_defined?(:note)
  2452.     def note
  2453.       list = @page ? @page.list.select {|l| [108,408].include?(l.code)} : []
  2454.       notes = list.collect {|l| l.parameters[0] }.join("\n")
  2455.       return notes
  2456.     end
  2457.   end
  2458.   #--------------------------------------------------------------------------
  2459.   # ● New method: proper_page_index
  2460.   #--------------------------------------------------------------------------  
  2461.   unless method_defined?(:proper_page_index)
  2462.     def proper_page_index
  2463.       if respond_to?(:conditions_met?)
  2464.         page = respond_to?(:find_proper_page) ? find_proper_page :
  2465.                @event.pages.reverse.find {|page| conditions_met?(page)}
  2466.         (i = @event.pages.index(page)).is_a?(Integer) ? i + 1 : 0
  2467.       end
  2468.     end
  2469.   end
  2470. end
  2471.  
  2472. #==============================================================================
  2473. # ** Game_Party
  2474. #------------------------------------------------------------------------------
  2475. #  This class handles the party. It includes information on amount of gold
  2476. #  and items. Refer to "$game_party" for the instance of this class.
  2477. #==============================================================================
  2478. class Game_Party
  2479.   #--------------------------------------------------------------------------
  2480.   # ● Alias Listing
  2481.   #--------------------------------------------------------------------------
  2482.   alias_sec_method :drg_ce_item_number, :item_number
  2483.   #--------------------------------------------------------------------------
  2484.   # ● Redirect Listing
  2485.   #--------------------------------------------------------------------------
  2486.   redirect_method :player_pos,      '($game_player.pos)'
  2487.   redirect_method :leader,          '(members.first)'
  2488.   redirect_method :members,         '(actors)'  
  2489.   redirect_method :actors,          '(members)'  
  2490.   #--------------------------------------------------------------------------
  2491.   # ● New method: existing_actors, recover_all
  2492.   #--------------------------------------------------------------------------
  2493.   define_sec_method(:existing_actors) { members.find_all {|a| a.exist?}  }
  2494.   define_sec_method(:recover_all)     { members.each {|a| a.recover_all} }
  2495.   #--------------------------------------------------------------------------
  2496.   # ● New method: has_actors?
  2497.   #--------------------------------------------------------------------------
  2498.   unless method_defined?(:has_actors?)
  2499.     def has_actors?(*actors)
  2500.       party = @actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }
  2501.       actor =  actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }.flatten
  2502.       return party.have_all?(*actor)
  2503.     end
  2504.   end
  2505.   #--------------------------------------------------------------------------
  2506.   # ● New method: has_any_actors?
  2507.   #--------------------------------------------------------------------------
  2508.   unless method_defined?(:has_any_actors?)
  2509.     def has_any_actors?(*actors)
  2510.       party = @actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }
  2511.       actor =  actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }.flatten
  2512.       return party.have_any?(*actor)
  2513.     end
  2514.   end
  2515.   #--------------------------------------------------------------------------
  2516.   # ● New method: swap_previous_actor
  2517.   #--------------------------------------------------------------------------
  2518.   unless method_defined?(:swap_leader_prev)
  2519.     def swap_leader_prev(n = 1)
  2520.       n <= 0 || n.round.times { @actors.previous_item && drg_ce_refresh }
  2521.       n >= 0 || swap_leader_next(n.round.abs)
  2522.     end
  2523.   end
  2524.   #--------------------------------------------------------------------------
  2525.   # ● New method: swap_next_actor
  2526.   #--------------------------------------------------------------------------
  2527.   unless method_defined?(:swap_leader_next)
  2528.     def swap_leader_next(n = 1)
  2529.       n <= 0 || n.round.times { @actors.next_item && drg_ce_refresh }
  2530.       n >= 0 || swap_leader_prev(n.round.abs)
  2531.     end
  2532.   end
  2533.   #--------------------------------------------------------------------------
  2534.   # ● New method: set_leader
  2535.   #--------------------------------------------------------------------------
  2536.   unless method_defined?(:set_leader)
  2537.     def set_leader(id)
  2538.       actor = id.is_a?(Numeric) ? $game_actors[id.to_i] : id
  2539.       if actor.is_a?(Game_Actor) && has_actors?(actor.id)
  2540.         swap_leader_next until $game_party.leader.id == actor.id
  2541.         actor.id
  2542.       end
  2543.     end
  2544.   end
  2545.   #--------------------------------------------------------------------------
  2546.   # ● New method: drg_ce_refresh
  2547.   #--------------------------------------------------------------------------
  2548.   unless method_defined?(:drg_ce_refresh)
  2549.     def drg_ce_refresh
  2550.       return refresh if respond_to?(:refresh)
  2551.       return ($game_map.need_refresh = true) && $game_player.refresh
  2552.     end
  2553.   end
  2554.   #--------------------------------------------------------------------------
  2555.   # * Get Party Name
  2556.   #--------------------------------------------------------------------------
  2557.   unless method_defined?(:name)
  2558.     def name
  2559.       return ""           if existing_actors.size == 0
  2560.       return leader.name  if existing_actors.size == 1
  2561.       return "#{leader.name}'s Party"
  2562.     end
  2563.   end
  2564.   #--------------------------------------------------------------------------
  2565.   # * Aliased method: item_number
  2566.   #--------------------------------------------------------------------------
  2567.   def item_number(item)
  2568.     unless LiTTleDRAgo::VX
  2569.       return drg_ce_item_number(item.id)          if item.is_a?(RPG::Item)
  2570.       return weapon_number(item.id)               if item.is_a?(RPG::Weapon)
  2571.       return armor_number(item.id)                if item.is_a?(RPG::Armor)  
  2572.     else
  2573.       item = $data_items[item.to_i]               if item.is_a?(Numeric)
  2574.     end
  2575.     return skill_number(item)                     if item.is_a?(RPG::Skill)
  2576.     return drg_ce_item_number(item)
  2577.   end
  2578.   #--------------------------------------------------------------------------
  2579.   # * Redefined method: weapon_number
  2580.   #--------------------------------------------------------------------------
  2581.   unless method_defined?(:weapon_number)
  2582.     def weapon_number(weapon)
  2583.       weapon = $data_weapons[weapon.to_i] if weapon.is_a?(Numeric)
  2584.       item_number(weapon)
  2585.     end
  2586.   end
  2587.   #--------------------------------------------------------------------------
  2588.   # * Redefined method: armor_number
  2589.   #--------------------------------------------------------------------------
  2590.   unless method_defined?(:armor_number)
  2591.     def armor_number(armor)
  2592.       armor = $data_armors[armor.to_i] if armor.is_a?(Numeric)
  2593.       item_number(armor)
  2594.     end
  2595.   end
  2596.   #--------------------------------------------------------------------------
  2597.   # * New method: skill_number
  2598.   #--------------------------------------------------------------------------
  2599.   unless method_defined?(:skill_number)
  2600.     def skill_number(skill)
  2601.       skill = $data_skills[skill.to_i] if skill.is_a?(Numeric)
  2602.       s1 = actors.select {|a| a.skill_learn?(skill)    }
  2603.       s2 = actors.select {|a| a.skill_learn?(skill.id) }
  2604.       return s1.size + s2.size
  2605.     end
  2606.   end
  2607.   #--------------------------------------------------------------------------
  2608.   # * New method: gain_all_items
  2609.   #--------------------------------------------------------------------------
  2610.   def gain_all_items(ammount=1)
  2611.     (0..999).to_a.each do |s|
  2612.       next if $data_items[s].nil? || $data_items[s].name == ''
  2613.       $game_party.drg_ce_get_item($data_items[s],ammount)
  2614.     end
  2615.   end
  2616.   #--------------------------------------------------------------------------
  2617.   # * New method: gain_all_weapons
  2618.   #--------------------------------------------------------------------------
  2619.   def gain_all_weapons(ammount=1)
  2620.     (0..999).to_a.each do |s|
  2621.       next if $data_weapons[s].nil? || $data_weapons[s].name == ''
  2622.       drg_ce_get_item($data_weapons[s],ammount)
  2623.     end
  2624.   end  
  2625.   #--------------------------------------------------------------------------
  2626.   # * New method: gain_all_armors
  2627.   #--------------------------------------------------------------------------
  2628.   def gain_all_armors(ammount=1)
  2629.     (0..999).to_a.each do |s|
  2630.       next if $data_armors[s].nil? || $data_armors[s].name == ''
  2631.       drg_ce_get_item($data_armors[s],ammount)
  2632.     end
  2633.   end
  2634.   #--------------------------------------------------------------------------
  2635.   # * New method: drg_ce_get_item
  2636.   #--------------------------------------------------------------------------
  2637.   def drg_ce_get_item(item,ammount)
  2638.     return gain_item(item,ammount)      if LiTTleDRAgo::VX
  2639.     return gain_item(item.id,ammount)   if item.is_a?(RPG::Item)
  2640.     return gain_weapon(item.id,ammount) if item.is_a?(RPG::Weapon)
  2641.     return gain_armor(item.id,ammount)  if item.is_a?(RPG::Armor)
  2642.   end
  2643. end
  2644.  
  2645. #==============================================================================
  2646. # ** Game_Follower
  2647. #------------------------------------------------------------------------------
  2648. #  This class handles followers. A follower is an allied character, other than
  2649. # the front character, displayed in the party. It is referenced within the
  2650. # Game_Followers class.
  2651. #==============================================================================
  2652. class Game_Follower < Game_Character
  2653.   #--------------------------------------------------------------------------
  2654.   # * Redirect Listing
  2655.   #--------------------------------------------------------------------------
  2656.   redirect_method :name, "(actor.nil? ? '' : actor.name)"
  2657. end if defined?(Game_Follower)
  2658.  
  2659. #==============================================================================
  2660. # ** Game_Map
  2661. #------------------------------------------------------------------------------
  2662. #  This class handles maps. It includes scrolling and passage determination
  2663. # functions. The instance of this class is referenced by $game_map.
  2664. #==============================================================================
  2665.  
  2666. class Game_Map
  2667.   #--------------------------------------------------------------------------
  2668.   # * Public Instance Variables
  2669.   #--------------------------------------------------------------------------
  2670.   attr_sec_reader :eliminate_event, 'Hash.new'
  2671.   #--------------------------------------------------------------------------
  2672.   # ● New method: name,        parent_id, base_parent_id
  2673.   #               event_list , map_events
  2674.   #--------------------------------------------------------------------------
  2675.   define_sec_method(:name)             { mapInfo[@map_id].name      }
  2676.   define_sec_method(:parent_id)        { mapInfo[@map_id].parent_id }
  2677.   define_sec_method(:base_parent_id)   { search_parent_id(mapInfo)  }
  2678.   define_sec_method(:event_list)       { events.values }
  2679.   define_sec_method(:map_events)       { @map.events   }
  2680.   define_sec_method(:screen)           { $game_screen  }
  2681.   define_sec_method(:loop_horizontal?) { false }
  2682.   define_sec_method(:loop_vertical?)   { false }
  2683.   define_sec_method(:map_child?)       { |*a|  a & child_ids == a }
  2684.   #--------------------------------------------------------------------------
  2685.   # * Aliased method: setup
  2686.   #--------------------------------------------------------------------------
  2687.   define_post_alias(:setup) do
  2688.     eliminate_event[@map_id] ||= []
  2689.     eliminate_event[@map_id].each { |s| events.delete(s) }
  2690.   end
  2691.   #--------------------------------------------------------------------------
  2692.   # * Redefined method: any_event_strarting? (for RMXP)
  2693.   #--------------------------------------------------------------------------
  2694.   unless method_defined?(:any_event_starting?)
  2695.     def any_event_starting?
  2696.       event_list.any? {|event| event.starting }
  2697.     end
  2698.   end
  2699.   #--------------------------------------------------------------------------
  2700.   # * Redefined method: interpreter (for RMXP)
  2701.   #--------------------------------------------------------------------------
  2702.   unless method_defined?(:interpreter)
  2703.     def interpreter
  2704.       return $game_system.map_interpreter if LiTTleDRAgo.scene.is_a?(Scene_Map)
  2705.       return $game_system.battle_interpreter
  2706.     end
  2707.   end
  2708.   #--------------------------------------------------------------------------
  2709.   # * Redefined method: events_xy (for RMXP)
  2710.   #--------------------------------------------------------------------------
  2711.   unless method_defined?(:events_xy)
  2712.     def events_xy(x, y)
  2713.       event_list.select {|event| event.x == x && event.y == y }
  2714.     end
  2715.   end
  2716.   #--------------------------------------------------------------------------
  2717.   # * Redefined method: events_xy_nt (for RMXP)
  2718.   #--------------------------------------------------------------------------
  2719.   unless method_defined?(:events_xy_nt)
  2720.     def events_xy_nt(x, y)
  2721.       event_list.select {|event| event.pos_nt?(x, y) }
  2722.     end
  2723.   end
  2724.   #--------------------------------------------------------------------------
  2725.   # * Redefined method: round_x, round_y (for RMXP)
  2726.   #--------------------------------------------------------------------------
  2727.   define_sec_method(:round_x) {|x| loop_horizontal? ? (x + width) % width : x }
  2728.   define_sec_method(:round_y) {|y| loop_vertical? ? (y + height) % height : y }
  2729.   #--------------------------------------------------------------------------
  2730.   # * Redefined method: x_with_direction (for RMXP)
  2731.   #--------------------------------------------------------------------------
  2732.   define_sec_method(:x_with_direction) {|x, d| x + (d==6 ? 1 : d==4 ? -1 : 0)}
  2733.   define_sec_method(:y_with_direction) {|y, d| y + (d==2 ? 1 : d==8 ? -1 : 0)}
  2734.   #--------------------------------------------------------------------------
  2735.   # * Redefined method: round_x_with_direction (for RMXP)
  2736.   #--------------------------------------------------------------------------
  2737.   unless method_defined?(:round_x_with_direction)
  2738.     def round_x_with_direction(x, d)
  2739.       round_x(x_with_direction(x, d))
  2740.     end
  2741.   end
  2742.   #--------------------------------------------------------------------------
  2743.   # * Redefined method: round_y_with_direction (for RMXP)
  2744.   #--------------------------------------------------------------------------
  2745.   unless method_defined?(:round_y_with_direction)
  2746.     def round_y_with_direction(y, d)
  2747.       round_y(y_with_direction(y, d))
  2748.     end
  2749.   end
  2750.   #--------------------------------------------------------------------------
  2751.   # * New method: x_diagonal_with_direction (for RMXP)
  2752.   #--------------------------------------------------------------------------
  2753.   unless method_defined?(:x_diagonal_with_direction)
  2754.     def x_diagonal_with_direction(x, d)
  2755.       x + ([9,6,3].include?(d) ? 1 : [7,4,1].include?(d) ? -1 : 0)
  2756.     end
  2757.   end
  2758.   #--------------------------------------------------------------------------
  2759.   # * New method: y_with_direction (for RMXP)
  2760.   #--------------------------------------------------------------------------
  2761.   unless method_defined?(:y_diagonal_with_direction)
  2762.     def y_diagonal_with_direction(y, d)
  2763.       y + ([1,2,3].include?(d) ? 1 : [7,8,9].include?(d) ? -1 : 0)
  2764.     end
  2765.   end
  2766.   #--------------------------------------------------------------------------
  2767.   # ● New method: existing_events
  2768.   #--------------------------------------------------------------------------
  2769.   unless method_defined?(:existing_events)
  2770.     def existing_events
  2771.       event_list.select {|e| e.not.erased }
  2772.     end
  2773.   end
  2774.   #--------------------------------------------------------------------------
  2775.   # ● New method: map_info
  2776.   #--------------------------------------------------------------------------
  2777.   unless method_defined?(:mapInfo)
  2778.     def mapInfo(ext = LiTTleDRAgo.data_default_extension)
  2779.       data = "Data/MapInfos.#{ext}"  
  2780.       @mapinfo ||= load_data(sprintf("#{data}")) rescue draise(
  2781.                    "Fail to load #{data}")
  2782.       return @mapinfo
  2783.     end
  2784.   end
  2785.   #--------------------------------------------------------------------------
  2786.   # ● New method: search_parent_id
  2787.   #--------------------------------------------------------------------------
  2788.   unless method_defined?(:search_parent_id)
  2789.     def search_parent_id(mapInfo, id = @map_id, ancestor = 0)
  2790.       m_id = id
  2791.       loop do
  2792.         _m_id = mapInfo[m_id]
  2793.         p_id = _m_id.respond_to?(:parent_id) ? _m_id.parent_id : nil
  2794.         return p_id if p_id == nil
  2795.         return m_id if p_id == ancestor
  2796.         m_id = p_id
  2797.       end
  2798.     end
  2799.   end
  2800.   #--------------------------------------------------------------------------
  2801.   # ● New method: base_map_name
  2802.   #--------------------------------------------------------------------------
  2803.   unless method_defined?(:base_map_name)
  2804.     def base_map_name
  2805.       map = mapInfo[search_parent_id(mapInfo)]
  2806.       name = map.respond_to?(:name) ? map.name : ''
  2807.       return name
  2808.     end
  2809.   end
  2810.   #--------------------------------------------------------------------------
  2811.   # ● New method: child_ids
  2812.   #--------------------------------------------------------------------------
  2813.   unless method_defined?(:child_ids)
  2814.     def child_ids
  2815.       mapInfo.keys.select {|map_id| mapInfo[map_id].parent_id == @map_id }
  2816.     end
  2817.   end
  2818.   #--------------------------------------------------------------------------
  2819.   # ● New method: descendant?
  2820.   #--------------------------------------------------------------------------
  2821.   unless method_defined?(:map_descendant?)
  2822.     def map_descendant?(*a)
  2823.       a.all? {|map_id| search_parent_id(mapInfo, map_id, @map_id)}
  2824.     end
  2825.   end
  2826.   #--------------------------------------------------------------------------
  2827.   # ● New method: load_event
  2828.   #--------------------------------------------------------------------------
  2829.   unless method_defined?(:load_event)
  2830.     def load_event(*a)
  2831.       case a.size
  2832.       when 5, 4 then (mapid, eventid, x, y = *a)
  2833.       when 3    then (mapid = @map_id) && (eventid, x, y = *a)
  2834.       when 2    then (mapid, y = @map_id, 0) && (eventid, x = *a)
  2835.       when 1    then (mapid, x, y = @map_id, 0,0) && (eventid = a.first)
  2836.       when 0    then return nil
  2837.       end
  2838.       map = load_map_data(mapid)
  2839.       event = map.events.values.detect { |event| event.id == eventid }
  2840.       unless event.nil?
  2841.         (a = a.at(4)) && (b = [a].flatten) && event.pages.reject! do |s|
  2842.           c = b.map {|i| i.is_a?(Range) ? i.to_a : i }
  2843.           c.flatten.not.include?(event.pages.index(s)+1)
  2844.         end
  2845.         return nil if event.pages.empty?
  2846.         c,r = [Array,Range], []
  2847.         c.include?(x.class) && (x = x.to_a.map{|s|s.is_a?(Range) ? s.to_a : s})
  2848.         c.include?(y.class) && (y = y.to_a.map{|s|s.is_a?(Range) ? s.to_a : s})
  2849.         [x].flatten.dproduct([y].flatten).each do |x1,y1|
  2850.           event.instance_variable_set(:@id,(id = next_key_events))
  2851.           r.push(@events[id] = e = Game_Event.new(@map_id, event.clone))
  2852.           e.loaded_event = true
  2853.           e.moveto(x1,y1)
  2854.         end
  2855.         a = r.size == 1 ? r.first : r
  2856.         if r && (s = spriteset).is_a?(Spriteset_Map)
  2857.           sc = s.character_sprites
  2858.           after  = sc.select { |e| e if e.character != $game_player }
  2859.           player = s.sprite_player
  2860.           sc.replace(after)
  2861.           r.each {|e| sc.push(Sprite_Character.new(s.viewport1, e)) }
  2862.           sc.push(player)
  2863.         end
  2864.         return a
  2865.       end
  2866.     end
  2867.   end
  2868.   #--------------------------------------------------------------------------
  2869.   # ● New method: call_event
  2870.   #--------------------------------------------------------------------------
  2871.   unless method_defined?(:call_event)
  2872.     def call_event(*a)
  2873.       case a.size
  2874.       when 3 then (mapid, eventid, page_id = *a)
  2875.       when 2 then (page_id = -1) && (mapid, eventid = *a)
  2876.       when 1 then (mapid, page_id = @map_id, -1) && (eventid = a.first)
  2877.       when 0 then return nil
  2878.       end
  2879.       map = load_map_data(mapid)
  2880.       event = map.events.values.detect { |event| event.id == eventid }
  2881.       unless event.nil?
  2882.         if page_id == -1 && event.respond_to?(:conditions_met?)
  2883.           pages = event.pages.reverse.find {|page| event.conditions_met?(page)}
  2884.         else
  2885.           pages = event.pages[[page_id,1].max-1]
  2886.         end
  2887.         klass = LiTTleDRAgo::VX ? Game_Interpreter : Interpreter
  2888.         child = klass.new(interpreter.instance_variable_get(:@depth) + 1)
  2889.         child.setup(pages.list, interpreter.same_map? ? event.id : 0)
  2890.         if LiTTleDRAgo::VXA
  2891.           child.run rescue FiberError
  2892.         else
  2893.           interpreter.instance_variable_set(:@child_interpreter, child)
  2894.         end
  2895.       end
  2896.     end
  2897.   end
  2898.   #--------------------------------------------------------------------------
  2899.   # ● New method: load_map_data
  2900.   #--------------------------------------------------------------------------
  2901.   unless method_defined?(:load_map_data)
  2902.     def load_map_data(mapid, ext = LiTTleDRAgo.data_default_extension)
  2903.       (map = sprintf("Data/Map%03d.#{ext}", mapid)) && load_data(map)
  2904.     end
  2905.   end
  2906.   #--------------------------------------------------------------------------
  2907.   # ● New method: increment_key_events
  2908.   #--------------------------------------------------------------------------
  2909.   unless method_defined?(:next_key_events)
  2910.     def next_key_events
  2911.       (@events.keys.max || 0) + 1
  2912.     end
  2913.   end
  2914.   #--------------------------------------------------------------------------
  2915.   # ● New method: unused_key_events
  2916.   #--------------------------------------------------------------------------
  2917.   unless method_defined?(:unused_key_events)
  2918.     def unused_key_events(start = 1)
  2919.       start = [start, next_key_events].min
  2920.       (start..next_key_events).detect {|i| @events.keys.not.include?(i)}
  2921.     end
  2922.   end
  2923.   #--------------------------------------------------------------------------
  2924.   # ● New method: spriteset
  2925.   #--------------------------------------------------------------------------
  2926.   unless method_defined?(:spriteset)
  2927.     def spriteset
  2928.       LiTTleDRAgo.scene.instance_variable_get(:@spriteset)
  2929.     end
  2930.   end
  2931.   #--------------------------------------------------------------------------
  2932.   # ● New method: character_sprite
  2933.   #--------------------------------------------------------------------------
  2934.   unless method_defined?(:character_sprite)
  2935.     def character_sprite(id = 0)
  2936.       return unless spriteset.is_a?(Spriteset_Map)
  2937.       id = id.is_a?(Game_Character) ? id : id == 0 ? $game_player : events[id]
  2938.       spriteset.find_character(id)
  2939.     end
  2940.   end
  2941.   #--------------------------------------------------------------------------
  2942.   # ● New method: picture_sprite
  2943.   #--------------------------------------------------------------------------
  2944.   unless method_defined?(:picture_sprite)
  2945.     def picture_sprite(id = nil)
  2946.       return unless spriteset.is_a?(Spriteset_Map)
  2947.       spriteset.update_pictures if spriteset.respond_to?(:update_pictures)
  2948.       result = spriteset.instance_variable_get(:@picture_sprites)
  2949.       id.nil? ? result : result[id]
  2950.     end
  2951.   end
  2952. end
  2953. #==============================================================================
  2954. # ■ RPG::Cache
  2955. #------------------------------------------------------------------------------
  2956. #
  2957. #==============================================================================
  2958. ModCache = LiTTleDRAgo.cache
  2959. class << ModCache
  2960.   #-----------------------------------------------------------------------
  2961.   # ● New method: self.draw_text
  2962.   #-----------------------------------------------------------------------
  2963.   unless method_defined?(:draw_text)
  2964.     def draw_text(
  2965.             width, height, text,
  2966.             font = Font.default_name,
  2967.             size = Font.default_size,
  2968.             bold = Font.default_bold,
  2969.             italic = Font.default_italic,
  2970.             color = Font.default_color,
  2971.             edging = false
  2972.           )
  2973.       cache_index = "text_" + width.to_s + height.to_s + text
  2974.       font_text = ""
  2975.       font.size.times do |i|
  2976.         font_text += font[i].to_s
  2977.       end
  2978.       cache_index += font_text + size.to_s + bold.to_s + italic.to_s
  2979.       cache_index += "#{color.red}#{color.blue}#{color.green}#{color.alpha}"
  2980.       @cache ||= {}
  2981.       if @cache[cache_index].nil? or @cache[cache_index].disposed?
  2982.         bitmap = Bitmap.new(width, height)
  2983.         bitmap.font.name = font
  2984.         bitmap.font.size = size
  2985.         bitmap.font.bold = bold
  2986.         bitmap.font.italic = italic
  2987.         bitmap.font.color = color
  2988.         if edging
  2989.           bitmap.draw_edging_text(0, 0, width, height, text, 0)
  2990.         else
  2991.           bitmap.draw_text(0, 0, width, height, text, 0)
  2992.         end
  2993.         @cache[cache_index] = bitmap
  2994.       end
  2995.       return @cache[cache_index]
  2996.     end
  2997.   end
  2998.   #-----------------------------------------------------------------------
  2999.   # ● New method: self.method_missing
  3000.   #-----------------------------------------------------------------------
  3001.   def method_missing(val,*a,&b)
  3002.     if a.at(0).is_a?(String) && a.size <= 2
  3003.       return load_bitmap("Graphics/#{val}/",*a,&b)
  3004.     end
  3005.     modified = [:invert,:brighten,:darken,:grayscale,:pixelate,:frost]
  3006.     if modified.include?(a.first) && a.at(1).is_a?(String) && a.size <= 3
  3007.       method = a.shift
  3008.       filename = a.join + "-#{method}"
  3009.       unless drago_bitmap_exist?("Graphics/#{val}",filename)
  3010.         bitmap = load_bitmap("Graphics/#{val}/",*a,&b)
  3011.         drago_set_bitmap(bitmap.send(method),"Graphics/#{val}",filename)
  3012.       end
  3013.       return drago_get_bitmap("Graphics/#{val}",filename)
  3014.     end
  3015.     text = "Undefined method #{val} for #{self.inspect}"
  3016.     raise(NoMethodError, text, caller(1))      
  3017.   end
  3018.   #-----------------------------------------------------------------------
  3019.   # ● New method: drago_get_bitmap
  3020.   #-----------------------------------------------------------------------
  3021.   def drago_get_bitmap(folder_name, filename = '', hue = 0)
  3022.     return empty_bitmap if filename.not.is_a?(String) || filename.empty?
  3023.     path = LiTTleDRAgo::APPPATHDRAGO + "#{folder_name}/"
  3024.     path = path.gsub("\\","/").gsub("//","/")
  3025.     last = filename.split('.').last
  3026.     filename.gsub!(".png","") if last.is_a?(String) && last[/png/i]
  3027.     load_bitmap_no_rescue(path.downcase,filename.downcase,hue)
  3028.   end
  3029.   #-----------------------------------------------------------------------
  3030.   # ● New method: drago_set_bitmap
  3031.   #-----------------------------------------------------------------------
  3032.   def drago_set_bitmap(bitmap, folder_name = '', filename = 'bitmap')
  3033.     path = LiTTleDRAgo::APPPATHDRAGO + "#{folder_name}/"
  3034.     path = path.gsub("\\","/").gsub("//","/")
  3035.     last = filename.split('.').last
  3036.     filename += '.png' unless last.is_a?(String) && last[/png/i]
  3037.     Dir.make_dir(path) unless FileTest.directory?(path)
  3038.     bitmap.export(path + filename)
  3039.     drago_get_bitmap(folder_name, filename)
  3040.   end
  3041.   #-----------------------------------------------------------------------
  3042.   # ● New method: drago_bitmap_exist?
  3043.   #-----------------------------------------------------------------------
  3044.   def drago_bitmap_exist?(folder_name, filename)
  3045.     return false if filename.not.is_a?(String) || filename.empty?
  3046.     path = LiTTleDRAgo::APPPATHDRAGO + "#{folder_name}/"
  3047.     path = path.gsub("\\","/").gsub("//","/")
  3048.     last = filename.split('.').last
  3049.     filename += '.png' unless last.is_a?(String) && last[/png/i]
  3050.     FileTest.file?(path + filename)
  3051.   end
  3052.   #--------------------------------------------------------------------------
  3053.   # * Load Bitmap
  3054.   #--------------------------------------------------------------------------
  3055.   define_sec_method(:load_bitmap_no_rescue) do |folder, file, *a|
  3056.     @cache ||= {}
  3057.     if file.empty?
  3058.       empty_bitmap
  3059.     elsif (hue = a.first).is_a?(Numeric) && hue != 0
  3060.       hue_changed_bitmap(folder + file, hue.round)
  3061.     else
  3062.       normal_bitmap(folder + file)
  3063.     end
  3064.   end
  3065.   #--------------------------------------------------------------------------
  3066.   # * Create Empty Bitmap
  3067.   #--------------------------------------------------------------------------
  3068.   define_sec_method(:empty_bitmap) { Bitmap.new(32, 32)}
  3069.   #--------------------------------------------------------------------------
  3070.   # * Create/Get Normal Bitmap
  3071.   #--------------------------------------------------------------------------
  3072.   define_sec_method(:normal_bitmap) do |path|
  3073.     @cache[path] = Bitmap.new(path) unless include?(path)
  3074.     @cache[path]
  3075.   end
  3076.   #--------------------------------------------------------------------------
  3077.   # * Create/Get Hue-Changed Bitmap
  3078.   #--------------------------------------------------------------------------
  3079.   define_sec_method(:hue_changed_bitmap) do |path, hue|
  3080.     key = [path, hue]
  3081.     unless include?(key)
  3082.       @cache[key] = normal_bitmap(path).clone
  3083.       @cache[key].hue_change(hue)
  3084.     end
  3085.     @cache[key]
  3086.   end
  3087.   #--------------------------------------------------------------------------
  3088.   # * Check Cache Existence
  3089.   #--------------------------------------------------------------------------
  3090.   def include?(key)
  3091.     @cache[key] && @cache[key].not.disposed?
  3092.   end
  3093. end
  3094. #==============================================================================
  3095. # ** Window_Base
  3096. #------------------------------------------------------------------------------
  3097. #  This class is for all in-game windows.
  3098. #==============================================================================
  3099.  
  3100. class Window_Base < Window
  3101.   #--------------------------------------------------------------------------
  3102.   # * Redirect Listing
  3103.   #--------------------------------------------------------------------------
  3104.   redirect_method :draw_text,    'contents.draw_text'
  3105.   redirect_method :text_size,    'contents.text_size'
  3106.   redirect_method :change_color, 'contents.change_color'
  3107.   redirect_method :draw_picture, 'contents.draw_picture'
  3108.   redirect_method :draw_icon,    'contents.draw_icon'
  3109.   redirect_method :draw_battler, 'contents.draw_battler'
  3110.   #--------------------------------------------------------------------------
  3111.   # * Redefined method: line_height, translucent_alpha
  3112.   # * New method:       show, hide, activate, deactivate
  3113.   #--------------------------------------------------------------------------
  3114.   define_sec_method(:line_height)       { 32 }
  3115.   define_sec_method(:translucent_alpha) { 160 }
  3116.   define_sec_method(:show)              { self.visible = true  }
  3117.   define_sec_method(:hide)              { self.visible = false }
  3118.   define_sec_method(:activate)          { self.active  = true  }
  3119.   define_sec_method(:deactivate)        { self.active  = false }
  3120.   #--------------------------------------------------------------------------
  3121.   # * New method: center_window
  3122.   #--------------------------------------------------------------------------
  3123.   unless method_defined?(:center_window)
  3124.     def center_window
  3125.       self.x = Graphics.width/2  - self.width/2
  3126.       self.y = Graphics.height/2 - self.height/2
  3127.     end
  3128.   end
  3129.   #--------------------------------------------------------------------------
  3130.   # * Redefined method: disabled_color
  3131.   #--------------------------------------------------------------------------
  3132.   unless method_defined?(:disabled_color)
  3133.     def disabled_color    
  3134.       color = normal_color
  3135.       color.alpha = 128
  3136.       return color
  3137.     end
  3138.   end
  3139.   #--------------------------------------------------------------------------
  3140.   # * Redefined method: draw_text_ex
  3141.   #--------------------------------------------------------------------------
  3142.   unless method_defined?(:draw_text_ex)
  3143.     def draw_text_ex(x, y, text)
  3144.       reset_font_settings
  3145.       text = convert_escape_characters(text)
  3146.       pos = {:x => x, :y => y, :new_x => x, :height => calc_line_height(text)}
  3147.       process_character(text.slice!(0, 1), text, pos) until text.empty?
  3148.     end
  3149.   end
  3150.   #--------------------------------------------------------------------------
  3151.   # * Redefined method: reset_font_settings
  3152.   #--------------------------------------------------------------------------
  3153.   unless method_defined?(:reset_font_settings)
  3154.     def reset_font_settings
  3155.       change_color(normal_color)
  3156.       contents.font.size = Font.default_size
  3157.       contents.font.bold = Font.default_bold
  3158.       contents.font.italic = Font.default_italic
  3159.     end
  3160.   end
  3161.   #--------------------------------------------------------------------------
  3162.   # * Redefined method: convert_escape_characters
  3163.   #--------------------------------------------------------------------------
  3164.   unless method_defined?(:convert_escape_characters)
  3165.     def convert_escape_characters(text)
  3166.       result = text.to_s.clone
  3167.       result.gsub!(/\\/)            { "\e" }
  3168.       result.gsub!(/\e\e/)          { "\\" }
  3169.       result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i]   }
  3170.       result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i)        }
  3171.       result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  3172.       result.gsub!(/\eG/i)          { currency_unit }
  3173.       result
  3174.     end
  3175.   end
  3176.   #--------------------------------------------------------------------------
  3177.   # * Redefined method: actor_name
  3178.   #--------------------------------------------------------------------------
  3179.   unless method_defined?(:actor_name)
  3180.     def actor_name(n)
  3181.       actor = n >= 1 ? $game_actors[n] : nil
  3182.       actor ? actor.name : ""
  3183.     end
  3184.   end
  3185.   #--------------------------------------------------------------------------
  3186.   # * Redefined method: party_member_name
  3187.   #--------------------------------------------------------------------------
  3188.   unless method_defined?(:party_member_name)
  3189.     def party_member_name(n)
  3190.       actor = n >= 1 ? $game_party.instance_variable_get(:@actors)[n - 1] : nil
  3191.       actor ? actor.name : ""
  3192.     end
  3193.   end
  3194.   #--------------------------------------------------------------------------
  3195.   # * Redefined method: process_character
  3196.   #--------------------------------------------------------------------------
  3197.   unless method_defined?(:process_character)
  3198.     def process_character(c, text, pos)
  3199.       case c
  3200.       when "\n"   # New line
  3201.         process_new_line(text, pos)
  3202.       when "\f"   # New page
  3203.         process_new_page(text, pos)
  3204.       when "\e"   # Control character
  3205.         process_escape_character(obtain_escape_code(text), text, pos)
  3206.       else        # Normal character
  3207.         process_normal_character(c, pos)
  3208.       end
  3209.     end
  3210.   end
  3211.   #--------------------------------------------------------------------------
  3212.   # * Redefined method: process_normal_character
  3213.   #--------------------------------------------------------------------------
  3214.   unless method_defined?(:process_normal_character)
  3215.     def process_normal_character(c, pos)
  3216.       text_width = text_size(c).width
  3217.       draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
  3218.       pos[:x] += text_width
  3219.     end
  3220.   end
  3221.   #--------------------------------------------------------------------------
  3222.   # * Redefined method: process_new_line
  3223.   #--------------------------------------------------------------------------
  3224.   unless method_defined?(:process_new_line)
  3225.     def process_new_line(text, pos)
  3226.       pos[:x] = pos[:new_x]
  3227.       pos[:y] += pos[:height]
  3228.       pos[:height] = calc_line_height(text)
  3229.     end
  3230.   end
  3231.   #--------------------------------------------------------------------------
  3232.   # * Redefined method: process_new_page
  3233.   #--------------------------------------------------------------------------
  3234.   unless method_defined?(:process_new_page)
  3235.     def process_new_page(text, pos)
  3236.     end
  3237.   end
  3238.   #--------------------------------------------------------------------------
  3239.   # * Redefined method: obtain_escape_code
  3240.   #--------------------------------------------------------------------------
  3241.   unless method_defined?(:obtain_escape_code)
  3242.     def obtain_escape_code(text)
  3243.       text.slice!(/^[\$\.\|\^!><\{\}\\]|^[\/\w+\/]+/i)
  3244.     end
  3245.   end
  3246.   #--------------------------------------------------------------------------
  3247.   # * Redefined method: obtain_escape_param
  3248.   #--------------------------------------------------------------------------
  3249.   unless method_defined?(:obtain_escape_param)
  3250.     def obtain_escape_param(text)
  3251.       text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
  3252.     end
  3253.   end
  3254.   #--------------------------------------------------------------------------
  3255.   # * Redefined method: process_escape_character
  3256.   #--------------------------------------------------------------------------
  3257.   unless method_defined?(:process_escape_character)
  3258.     def process_escape_character(code, text, pos)
  3259.       case code.upcase
  3260.       when 'C' then change_color(text_color(obtain_escape_param(text)))
  3261.       when 'I' then process_draw_icon(obtain_escape_code(text), pos)
  3262.       when '{' then make_font_bigger
  3263.       when '}' then make_font_smaller
  3264.       end
  3265.     end
  3266.   end
  3267.   #--------------------------------------------------------------------------
  3268.   # * Redefined method: process_draw_icon
  3269.   #--------------------------------------------------------------------------
  3270.   unless method_defined?(:process_draw_icon)
  3271.     def process_draw_icon(icon_index, pos)
  3272.       icon_index = icon_index.to_i if icon_index.to_i.to_s == icon_index
  3273.       draw_icon(icon_index, pos[:x], pos[:y])
  3274.       pos[:x] += 24
  3275.     end
  3276.   end
  3277.   #--------------------------------------------------------------------------
  3278.   # * Redefined method: make_font_bigger
  3279.   #--------------------------------------------------------------------------
  3280.   unless method_defined?(:make_font_bigger)
  3281.     def make_font_bigger
  3282.       contents.font.size += 8 if contents.font.size <= 64
  3283.     end
  3284.   end
  3285.   #--------------------------------------------------------------------------
  3286.   # * Redefined method: make_font_smaller
  3287.   #--------------------------------------------------------------------------
  3288.   unless method_defined?(:make_font_smaller)
  3289.     def make_font_smaller
  3290.       contents.font.size -= 8 if contents.font.size >= 16
  3291.     end
  3292.   end
  3293.   #--------------------------------------------------------------------------
  3294.   # * Redefined method: calc_line_height
  3295.   #--------------------------------------------------------------------------
  3296.   unless method_defined?(:calc_line_height)
  3297.     def calc_line_height(text, restore_font_size = true)
  3298.       result = [line_height, contents.font.size].max
  3299.       last_font_size = contents.font.size
  3300.       text.slice(/^.*$/).scan(/\e[\{\}]/).each do |esc|
  3301.         make_font_bigger  if esc == "\e{"
  3302.         make_font_smaller if esc == "\e}"
  3303.         result = [result, contents.font.size].max
  3304.       end
  3305.       contents.font.size = last_font_size if restore_font_size
  3306.       result
  3307.     end
  3308.   end
  3309.   #--------------------------------------------------------------------------
  3310.   # * New method: currency_unit
  3311.   #--------------------------------------------------------------------------
  3312.   unless method_defined?(:currency_unit)
  3313.     def currency_unit
  3314.       gold = $data_system.words.gold
  3315.       return gold
  3316.     end
  3317.   end
  3318.   #--------------------------------------------------------------------------
  3319.   # ● New method: draw_map_name
  3320.   #--------------------------------------------------------------------------
  3321.   unless method_defined?(:draw_map_name)
  3322.     def draw_map_name(x, y, width, height)
  3323.       map_id  = $game_map.map_id
  3324.       mapInfo = $game_map.mapInfo
  3325.       draw_text(x, y, width, height, mapInfo[map_id].name.to_s)
  3326.     end
  3327.   end
  3328.   #--------------------------------------------------------------------------
  3329.   # ● New method: draw_enter_text
  3330.   #--------------------------------------------------------------------------
  3331.   unless method_defined?(:draw_enter_text)
  3332.     def draw_enter_text(x, y, width, height, text)
  3333.       info_box = text.split(/\n/)
  3334.       info_box.size.times do |i|
  3335.         draw_text( x, y + i * line_height, width, line_height, info_box[i])
  3336.         break if (y + i * line_height) > (self.height - line_height)
  3337.       end
  3338.     end
  3339.   end
  3340.   #--------------------------------------------------------------------------
  3341.   # ● New method: draw_picture_number
  3342.   #--------------------------------------------------------------------------
  3343.   unless method_defined?(:draw_picture_number)
  3344.     def draw_picture_number(x, y, value, file_name,align = 0,
  3345.                   space = 0, frame_max = 1,frame_index = 0)    
  3346.       number_image = LiTTleDRAgo.cache.windowskin(file_name)
  3347.       frame_max = [frame_max, 1].max
  3348.       frame_index = frame_max - 1 if frame_index > frame_max - 1
  3349.       align = [[align,2].min,0].max
  3350.       cw = number_image.width / 10
  3351.       ch = number_image.height / frame_max
  3352.       h = ch * frame_index
  3353.       number = value.abs.to_s.split(//)
  3354.       case align
  3355.       when 0 then plus_x = (-cw + space) * number.size
  3356.       when 1 then plus_x = ((-cw + space) * number.size) / 2
  3357.       when 2 then plus_x = 0
  3358.       end
  3359.       for r in 0..number.size - 1      
  3360.         rect = Rect.new(cw * number[r].to_i, h, cw, ch)
  3361.         contents.blt(plus_x + x + ((cw - space) * r), y , number_image, rect)
  3362.       end    
  3363.       number_image.dispose
  3364.     end    
  3365.   end
  3366. end
  3367.  
  3368. #==============================================================================
  3369. # ■ Bitmap
  3370. #------------------------------------------------------------------------------
  3371. #
  3372. #==============================================================================
  3373. class Bitmap  
  3374.   #----------------------------------------------------------------------------
  3375.   # ● Constant
  3376.   #----------------------------------------------------------------------------
  3377.   RtlMoveMemory_pi = LiTTleDRAgo.rtlmemory_pi
  3378.   RtlMoveMemory_ip = LiTTleDRAgo.rtlmemory_ip
  3379.   Blur_Setting     = "{ 'offset' => 2, 'spacing' => 1, 'opacity' => 255 }"
  3380.   #----------------------------------------------------------------------------
  3381.   # ● Public Instance Variables
  3382.   #----------------------------------------------------------------------------
  3383.   attr_sec_accessor :blur_settings,  Blur_Setting
  3384.   #----------------------------------------------------------------------------
  3385.   # ● Redirect Listing
  3386.   #----------------------------------------------------------------------------
  3387.   redirect_method :change_hue,          :hue_change
  3388.   #-------------------------------------------------------------------------
  3389.   # ● Define Secondary Listing
  3390.   #-------------------------------------------------------------------------
  3391.   define_sec_method(:full_fill) { |color|  fill_rect(rect, color) }
  3392.   #----------------------------------------------------------------------------
  3393.   # ● Alias Listing
  3394.   #----------------------------------------------------------------------------
  3395.   alias_sec_method(:invert!,            :reversing!  )
  3396.   alias_sec_method(:grayscale!,         :gray_scale! )
  3397.   alias_sec_method(:frost,              :diffusion   )
  3398.   alias_sec_method(:frost!,             :diffusion!  )
  3399.   alias_sec_method(:drg_ce_stretch_blt, :stretch_blt )
  3400.   $@ || alias_method(:drg_bitmap_load,  :initialize  )
  3401.   #--------------------------------------------------------------------------
  3402.   # * Object Initialization
  3403.   #----------------------------------------------------------------------------
  3404.   def initialize(*args)
  3405.     if (a = args.flatten).size == 4 && a.all? {|s| s.is_a?(Table)}
  3406.       bitmap = BitmapDump.read_bitmap_data(*a)
  3407.       drg_bitmap_load(bitmap.width,bitmap.height)
  3408.       return [blt(0,0,bitmap,rect) && bitmap.dispose] && self
  3409.     end
  3410.     drg_bitmap_load(*args)
  3411.   end
  3412.   #----------------------------------------------------------------------------
  3413.   # ● New method: address
  3414.   #----------------------------------------------------------------------------
  3415.   unless method_defined?(:address)
  3416.     def address
  3417.       @address ||= (  RtlMoveMemory_pi.call(a="\0"*4, __id__*2+16, 4)
  3418.                       RtlMoveMemory_pi.call(a, a.unpack('L')[0]+8, 4)
  3419.                       RtlMoveMemory_pi.call(a, a.unpack('L')[0]+16, 4)
  3420.                       a.unpack('L')[0]    )
  3421.     end
  3422.   end
  3423.   #----------------------------------------------------------------------------
  3424.   # ● New method: export
  3425.   #----------------------------------------------------------------------------
  3426.   unless method_defined?(:export)
  3427.     def export(filename)
  3428.       file = File.open(filename, 'wb')
  3429.         size, vxace = width * height * 4, LiTTleDRAgo::RGSS3
  3430.         case format = File.extname(filename)
  3431.         when '.bmp'
  3432.           data = String.new
  3433.           RtlMoveMemory_pi.call(empty_rString_struct="\0"*16,data.__id__*2,16)
  3434.           flags = vxace ? 5267461 : 8199
  3435.           klass = empty_rString_struct.unpack('@4L')[0]
  3436.           pack  = [flags,klass,size,address].pack('L4')
  3437.           weiss = [size+54,0,54,40,width,height,1,32,0,size,0,0,0,0]
  3438.           RtlMoveMemory_ip.call(data.__id__*2, pack, 16)
  3439.           file.write("BM#{weiss.pack('L6S2L6')}")
  3440.           file.write(data)
  3441.           RtlMoveMemory_ip.call(data.__id__*2, empty_rString_struct, 16)
  3442.         when '.png'
  3443.           write_chunk = Proc.new do |chunk|
  3444.             file.write([(vxace ? chunk.bytesize : chunk.size)-4].pack('N'))
  3445.             file.write(chunk)
  3446.             file.write([Zlib.crc32(chunk)].pack('N'))
  3447.           end
  3448.           null_char, w4, i, byte = "\0", width*4, 0, (t_Fx = 0)
  3449.           RtlMoveMemory_pi.call(data=null_char*size, address, size)
  3450.           if vxace
  3451.             data.force_encoding('ASCII-8BIT')
  3452.             (0).step(size-4, 4) do |i|
  3453.               Graphics.update if (t_Fx += 1) % 10000 == 0
  3454.               byte = data.getbyte(i)
  3455.               data.setbyte(i, data.getbyte(i+=2))
  3456.               data.setbyte(i, byte)
  3457.             end
  3458.           else
  3459.             (0).step(size-4, 4) do |i|
  3460.               Graphics.update if (t_Fx += 1) % 10000 == 0
  3461.               data[i,3] = data[i,3].reverse!
  3462.             end
  3463.           end
  3464.           deflate = Zlib::Deflate.new(9)
  3465.             (size-w4).step(0, -w4) {|i| deflate << null_char << data[i,w4]}
  3466.             file.write("\211PNG\r\n\32\n")
  3467.             write_chunk.call("IHDR#{[width,height,8,6,0,0,0].pack('N2C5')}")
  3468.             write_chunk.call("IDAT#{deflate.finish}")
  3469.             write_chunk.call('IEND')
  3470.           deflate.close
  3471.         when ''
  3472.           script_report("Error: Export Bitmap Failed\n"+
  3473.               "Extension '#{filename}' is missing.")
  3474.         else          
  3475.           script_report("Error: Export Bitmap Failed\n"+
  3476.                "Extension '#{format}' is not supported.")
  3477.         end
  3478.       file.close
  3479.     end
  3480.   end
  3481.   #----------------------------------------------------------------------------
  3482.   # ● New method: blt_cache
  3483.   #----------------------------------------------------------------------------
  3484.   unless method_defined?(:blt_cache)
  3485.     def blt_cache(x, y, width, height, text, align, edging)
  3486.       cache_bitmap = LiTTleDRAgo.cache.draw_text(
  3487.           width, height, text, self.font.name, self.font.size,
  3488.           self.font.bold, self.font.italic, self.font.color, edging
  3489.       )
  3490.       x_plus = uncache_align_exe(width, text, align)
  3491.       blt(x + x_plus , y , cache_bitmap, Rect.new(0, 0, width, height))
  3492.     end
  3493.   end
  3494.   #----------------------------------------------------------------------------
  3495.   # ● New method: uncache_align_exe
  3496.   #----------------------------------------------------------------------------
  3497.   unless method_defined?(:uncache_align_exe)
  3498.     def uncache_align_exe(width, text, align)
  3499.       recter = self.text_size(text)
  3500.       case align
  3501.       when 0 then x_index = 0
  3502.       when 1 then x_index = (width / 2) - recter.width / 2
  3503.       when 2 then x_index = width - recter.width
  3504.       else
  3505.         raise "Text Cache : align is wrong"
  3506.       end
  3507.       return x_index
  3508.     end
  3509.   end
  3510.   #----------------------------------------------------------------------------
  3511.   # ● New method: draw_cache_text
  3512.   #----------------------------------------------------------------------------
  3513.   unless method_defined?(:draw_cache_text)
  3514.     def draw_cache_text(x, y, width, height, text, align = 0)
  3515.       blt_cache(x, y, width, height, text, align, false)
  3516.     end
  3517.   end
  3518.   #----------------------------------------------------------------------------
  3519.   # ● New method: draw_edging_cache_text
  3520.   #----------------------------------------------------------------------------
  3521.   unless method_defined?(:draw_edging_cache_text)
  3522.     def draw_edging_cache_text(x, y, width, height, text, align = 0)
  3523.       blt_cache(x, y, width, height, text, align, true)
  3524.     end
  3525.   end
  3526.   #----------------------------------------------------------------------------
  3527.   # ● New method: draw_easy_cache_text
  3528.   #----------------------------------------------------------------------------
  3529.   unless method_defined?(:draw_easy_cache_text)
  3530.     def draw_easy_cache_text(x, y, text)
  3531.       t_rect = text_size(text)
  3532.       blt_cache(x, y, t_rect.width, t_rect.height, text, 0, false)
  3533.     end
  3534.   end
  3535.   #----------------------------------------------------------------------------
  3536.   # ● New method: draw_easy_edging_cache_text
  3537.   #----------------------------------------------------------------------------
  3538.   unless method_defined?(:draw_easy_edging_cache_text)
  3539.     def draw_easy_edging_cache_text(x, y, text)
  3540.       t_rect = text_size(text)
  3541.       blt_cache(x, y, t_rect.width, t_rect.height, text, 0, true)
  3542.     end
  3543.   end
  3544.   #--------------------------------------------------------------------------
  3545.   # * New method: change_color
  3546.   #--------------------------------------------------------------------------
  3547.   unless method_defined?(:change_color)
  3548.     def change_color(color, enabled = true)
  3549.       font.color = color
  3550.       font.color.alpha = translucent_alpha unless enabled
  3551.     end
  3552.   end
  3553.   #--------------------------------------------------------------------------
  3554.   # ● New method: draw_picture
  3555.   #--------------------------------------------------------------------------
  3556.   unless method_defined?(:draw_picture)
  3557.     def draw_picture(file_name, x, y, opacity = 255)
  3558.       bitmap = LiTTleDRAgo.cache.picture(file_name)
  3559.       cw = bitmap.width
  3560.       ch = bitmap.height
  3561.       src_rect = Rect.new(0, 0, cw, ch)
  3562.       blt(x, y, bitmap, src_rect, opacity)
  3563.     end
  3564.   end
  3565.   #--------------------------------------------------------------------------
  3566.   # ● New method: draw_icon
  3567.   #--------------------------------------------------------------------------
  3568.   unless method_defined?(:draw_icon)
  3569.     def draw_icon(file_name, x, y, enabled = true)
  3570.       if file_name.is_a?(Numeric)
  3571.         bitmap = LiTTleDRAgo.cache.system("Iconset")
  3572.         src_rect = Rect.new(file_name % 16 * 24, file_name / 16 * 24, 24, 24)
  3573.         blt(x, y, bitmap, src_rect, enabled ? 255 : 160)
  3574.       elsif file_name.is_a?(String)
  3575.         bitmap = LiTTleDRAgo.cache.icon(file_name)
  3576.         src_rect = Rect.new(0, 0, 24, 24)
  3577.         blt(x, y, bitmap, bitmap.rect, enabled ? 255 : 160)
  3578.       end
  3579.     end
  3580.   end
  3581.   #--------------------------------------------------------------------------
  3582.   # ● New method: draw_battler
  3583.   #--------------------------------------------------------------------------
  3584.   unless method_defined?(:draw_battler)
  3585.     def draw_battler(file_name, x, y, hue = 0, enabled = true)
  3586.       bitmap = LiTTleDRAgo.cache.battler(file_name, hue)
  3587.       cw = bitmap.width
  3588.       ch = bitmap.height
  3589.       src_rect = Rect.new(0, 0, cw, ch)
  3590.       blt(x, y, bitmap, src_rect, enabled ? 255 : 160)
  3591.     end
  3592.   end
  3593.   #--------------------------------------------------------------------------
  3594.   # ● New method: draw_bar
  3595.   #--------------------------------------------------------------------------
  3596.   unless method_defined?(:draw_bar)
  3597.     def draw_bar(x, y, current, file)
  3598.       bitmap = LiTTleDRAgo.cache.picture(file)
  3599.       cw = bitmap.width * current / 100
  3600.       ch = bitmap.height
  3601.       src_rect = Rect.new(0, 0, cw, ch)
  3602.       blt(x, y, bitmap, src_rect)
  3603.     end
  3604.   end
  3605.   #--------------------------------------------------------------------------
  3606.   # ● New method: draw_edging_text
  3607.   #--------------------------------------------------------------------------
  3608.   unless method_defined?(:draw_edging_text)
  3609.     def draw_edging_text(x, y, width, height, text, align=0)
  3610.       old_color = font.color.dup
  3611.       font.color = Color.new(0, 0, 0)
  3612.       draw_text(x-1, y-1, width, height, text, align)
  3613.       draw_text(x-1, y+1, width, height, text, align)
  3614.       draw_text(x+1, y-1, width, height, text, align)
  3615.       draw_text(x+1, y+1, width, height, text, align)
  3616.       font.color = old_color
  3617.       draw_text(x, y, width, height, text, align)
  3618.     end
  3619.   end
  3620.   #--------------------------------------------------------------------------
  3621.   # ● New method: draw_enter_text
  3622.   #--------------------------------------------------------------------------
  3623.   unless method_defined?(:draw_enter_text)
  3624.     def draw_enter_text(x, y, width, height, text, align=0)
  3625.       rect = text_size(text)
  3626.       info_box = text.split(/\n/)
  3627.       info_box.size.times do |i|
  3628.         draw_text( x, y+i*rect.height, width, height, info_box[i], align)
  3629.         break if (y+i*rect.height) > self.height
  3630.       end
  3631.     end
  3632.   end
  3633.   #--------------------------------------------------------------------------
  3634.   # ● New method: draw_enter_edging_text
  3635.   #--------------------------------------------------------------------------
  3636.   unless method_defined?(:draw_enter_edging_text)
  3637.     def draw_enter_edging_text(x, y, width, height, text, align=0)
  3638.       rect = text_size(text)
  3639.       info_box = text.split(/\n/)
  3640.       info_box.size.times do |i|
  3641.         draw_edging_text( x, y+i*rect.height,width,height,info_box[i],align)
  3642.         break if (y+i*rect.height) > self.height
  3643.       end
  3644.     end
  3645.   end
  3646.   #--------------------------------------------------------------------------
  3647.   # ● New method: draw_easy_text
  3648.   #--------------------------------------------------------------------------
  3649.   unless method_defined?(:draw_easy_text)
  3650.     def draw_easy_text(x, y, text)
  3651.       rect = text_size(text)
  3652.       draw_text(x, y, rect.width + rect.height/2, rect.height, text)
  3653.     end
  3654.   end
  3655.   #--------------------------------------------------------------------------
  3656.   # ● New method: draw_auto_text
  3657.   #--------------------------------------------------------------------------
  3658.   unless method_defined?(:draw_auto_text)
  3659.     def draw_auto_text(x, y, width, height, text)
  3660.       ini_x = 0
  3661.       law = 0
  3662.       text_box = []
  3663.       text_box[law] = []
  3664.       otxto = text.split(//)
  3665.       otxto.size.times do |i|
  3666.         text_rect = text_size(otxto[i])
  3667.         if (x + ini_x + text_rect.width) < width
  3668.           ini_x += text_rect.width
  3669.         else
  3670.           law += 1
  3671.           ini_x = text_rect.width
  3672.           text_box[law] = []
  3673.         end
  3674.         text_box[law].push(otxto[i])
  3675.       end
  3676.       for l in 0..law
  3677.         ini_x = 0
  3678.         text_box[l].size.times do |i|
  3679.           rect = text_size(text_box[l][i])
  3680.           draw_text(x + ini_x, y+l*height, rect.width, height, text_box[l][i])
  3681.           ini_x += rect.width
  3682.         end
  3683.       end
  3684.     end
  3685.   end
  3686.   #-------------------------------------------------------------------------
  3687.   # ● New method: erase
  3688.   #-------------------------------------------------------------------------
  3689.   unless method_defined?(:erase)
  3690.     def erase(*a)
  3691.       rect = nil
  3692.       if a.at(0).is_a?(Rect) then rect = a.at(0)
  3693.       elsif a.size == 4 then rect = Rect.new(*a)
  3694.       elsif a.size == 0 then rect = Rect.new(0,0,width,height)
  3695.       end
  3696.       fill_rect(rect.x, rect.y, rect.width, rect.height, Color.erase) if rect
  3697.     end
  3698.   end
  3699.   #-------------------------------------------------------------------------
  3700.   # ● New method: stretch
  3701.   #-------------------------------------------------------------------------
  3702.   unless method_defined?(:stretch)
  3703.     def stretch(width, height)
  3704.       dummy = Bitmap.new(width, height)
  3705.       dummy.stretch_blt(*[dummy.rect, self, self.rect])
  3706.       dummy
  3707.     end
  3708.   end
  3709.   #-------------------------------------------------------------------------
  3710.   # ● New method: crop
  3711.   #-------------------------------------------------------------------------
  3712.   unless method_defined?(:crop)
  3713.     def crop(*a)
  3714.       return unless a.size == 1 or a.size == 4
  3715.       rect = rect.at(0)   if a.size == 1 && a.at(0).is_a?(Rect)
  3716.       rect = Rect.new(*a) if a.size == 4
  3717.       cropped = Bitmap.new([rect.width,width-rect.x].min,
  3718.                            [rect.height,height-rect.y].min)
  3719.       cropped.blt(0,0,self,rect)
  3720.       cropped
  3721.     end
  3722.   end
  3723.   #-------------------------------------------------------------------------
  3724.   # ● Aliased method: stretch_blt
  3725.   #-------------------------------------------------------------------------
  3726.   def stretch_blt(*a)
  3727.     if respond_to?(:stretch_blend_blt)
  3728.       a = [a[0..2],0,1,a.at(3)||255].flatten
  3729.       stretch_blend_blt(*a)
  3730.     else
  3731.       drg_ce_stretch_blt(*a)
  3732.     end
  3733.   end
  3734.   #-------------------------------------------------------------------------
  3735.   # ● Aliased method: hue_change
  3736.   #-------------------------------------------------------------------------
  3737.   define_post_alias(:hue_change) { self }
  3738.   #--------------------------------------------------------------------------
  3739.   # ● New method: invert, etc
  3740.   #--------------------------------------------------------------------------
  3741.   [:invert, :brighten, :darken, :grayscale, :pixelate, :frost, :flip_horizontal,
  3742.    :flip_vertical, :rotate90, :rotate180, :rotate270].each do |meth|
  3743.     define_sec_method(meth) do |*args|
  3744.       bitmap = self.clone
  3745.       bitmap.send(:"#{meth}!",*args)
  3746.       return bitmap
  3747.     end
  3748.   end
  3749.   #-------------------------------------------------------------------------
  3750.   # ● New method: flip_horizontal!
  3751.   #-------------------------------------------------------------------------
  3752.   define_sec_method(:flip_horizontal!) do
  3753.     flipped = Bitmap.new(width, height)
  3754.     fliprect = Rect.new(width,0,-width,height)
  3755.     flipped.blt(0,0, self,fliprect)
  3756.     (clear || 0) && blt(0,0,flipped,rect)
  3757.     flipped.dispose
  3758.   end
  3759.   #-------------------------------------------------------------------------
  3760.   # ● New method: flip_vertical!
  3761.   #-------------------------------------------------------------------------
  3762.   define_sec_method(:flip_vertical!) do
  3763.     flipped = Bitmap.new(width, height)
  3764.     fliprect = Rect.new(0,height,width,-height)
  3765.     flipped.blt(0,0, self,fliprect)
  3766.     (clear || 0) && blt(0,0,flipped,rect)
  3767.     flipped.dispose
  3768.   end
  3769.   #-------------------------------------------------------------------------
  3770.   # ● New method: rotate90! (clockwise)
  3771.   #-------------------------------------------------------------------------
  3772.   define_sec_method(:rotate90!) do
  3773.     copy = self.clone
  3774.     (t_Fx = width).times do |i|
  3775.       height.times do |j|
  3776.         Graphics.update if (t_Fx += 1) % 10000 == 0
  3777.         a = (clockwise = true) ? [width - i - 1, j] : [i, height - j - 1]
  3778.         self.set_pixel(a[0],a[1], copy.get_pixel(j, i))
  3779.       end
  3780.     end
  3781.     copy.dispose
  3782.   end
  3783.   #-------------------------------------------------------------------------
  3784.   # ● New method: rotate180!
  3785.   #-------------------------------------------------------------------------
  3786.   define_sec_method(:rotate180!) do
  3787.     flip_vertical!
  3788.     flip_horizontal!
  3789.   end
  3790.   #-------------------------------------------------------------------------
  3791.   # ● New method: rotate270!
  3792.   #-------------------------------------------------------------------------
  3793.   define_sec_method(:rotate270!) do
  3794.     rotate90!
  3795.     rotate180!
  3796.   end  
  3797.   #----------------------------------------------------------------------------
  3798.   # ● New method: invert!
  3799.   #----------------------------------------------------------------------------
  3800.   unless method_defined?(:invert!)
  3801.     def invert!
  3802.       (t_Fx = width).times do |i|
  3803.         height.times do |j|
  3804.            color = get_pixel(i, j)
  3805.            Graphics.update if (t_Fx += 1) % 10000 == 0
  3806.            set_pixel(i, j, color.invert) if color.alpha > 0
  3807.          end
  3808.       end
  3809.     end
  3810.   end
  3811.   #--------------------------------------------------------------------------
  3812.   # * New method: brighten!
  3813.   #--------------------------------------------------------------------------
  3814.   unless method_defined?(:brighten!)
  3815.     def brighten!(amount = 10)
  3816.       (t_Fx = width).times do |w|
  3817.         height.times do |h|
  3818.           color = get_pixel(w, h)
  3819.           Graphics.update if (t_Fx += 1) % 10000 == 0
  3820.           set_pixel(w, h, Color.new([color.red + amount, 255].min,
  3821.                                     [color.green + amount, 255].min,
  3822.                                     [color.blue + amount, 255].min,
  3823.                                     color.alpha)) if color.alpha > 0
  3824.         end
  3825.       end
  3826.     end
  3827.   end
  3828.   #--------------------------------------------------------------------------
  3829.   # * New method: darken!
  3830.   #--------------------------------------------------------------------------
  3831.   unless method_defined?(:darken!)
  3832.     def darken!(amount = 10)
  3833.       (t_Fx = width).times do |w|
  3834.         height.times do |h|
  3835.           color = get_pixel(w, h)
  3836.           Graphics.update if (t_Fx += 1) % 10000 == 0
  3837.           set_pixel(w, h, Color.new([color.red - amount, 0].max,
  3838.                                     [color.green - amount, 0].max,
  3839.                                     [color.blue - amount, 0].max,
  3840.                                     color.alpha)) if color.alpha > 0
  3841.         end
  3842.       end
  3843.     end
  3844.   end
  3845.   #--------------------------------------------------------------------------
  3846.   # * New method: grayscale!
  3847.   # NOTE: calling this method repeatedly has no effect on the bitmap, but will
  3848.   #       still waste processing power. Do not call it repeatedly.
  3849.   #--------------------------------------------------------------------------
  3850.   unless method_defined?(:grayscale!)
  3851.     def grayscale!
  3852.       (t_Fx = width).times do |w|
  3853.         height.times do |h|
  3854.           color = get_pixel(w, h)
  3855.           if (a = color.alpha) > 0
  3856.             Graphics.update if (t_Fx += 1) % 10000 == 0
  3857.             num = (color.red + color.green + color.blue) / 3
  3858.             set_pixel(w, h, Color.new(num, num, num, a))
  3859.           end
  3860.         end
  3861.       end
  3862.     end
  3863.   end
  3864.   #--------------------------------------------------------------------------
  3865.   # * New method: drg_pixelate! (0.2 second)
  3866.   # NOTE: Pixelateing bitmaps to a larger pixel size is less process consuming.
  3867.   #       for best results, don't pixelate to numbers below four.
  3868.   #       This works much better for solid images, due to bugs.
  3869.   #--------------------------------------------------------------------------
  3870.   unless method_defined?(:drg_pixelate!)
  3871.     def drg_pixelate!(size = 10)
  3872.       return self if size <= (t_Fx = 1)
  3873.       c = lambda {|a,b| (a / b**2) * Math.sqrt(b) }
  3874.       for w in 0..((width - 1) / size)
  3875.         w *= size
  3876.         for h in 0..((height - 1) / size)
  3877.           h *= size
  3878.           r, g, b, a = 0, 0, 0, 0
  3879.           size.times do |i|
  3880.             color = get_pixel(w, h)
  3881.             r += color.red
  3882.             g += color.green
  3883.             b += color.blue
  3884.             a += color.alpha
  3885.             color = get_pixel(w + i, h)
  3886.             r += color.red
  3887.             g += color.green
  3888.             b += color.blue
  3889.             a += color.alpha
  3890.             color = get_pixel(w, h + i)
  3891.             r += color.red
  3892.             g += color.green
  3893.             b += color.blue
  3894.             a += color.alpha
  3895.           end
  3896.           Graphics.update if (t_Fx += 1) % 10000 == 0
  3897.           fill_rect(w, h, size, size, Color.new(c.call(r,size),c.call(g,size),
  3898.                                                 c.call(b,size),c.call(a,size)))
  3899.         end
  3900.       end
  3901.     end
  3902.   end
  3903.   #--------------------------------------------------------------------------
  3904.   # * New method: pixelate!
  3905.   #--------------------------------------------------------------------------
  3906.   unless method_defined?(:pixelate!)
  3907.     def pixelate!(size = 10)
  3908.       return mosaic!(size,size) if respond_to?(:mosaic!)
  3909.       return drg_pixelate!(size)
  3910.     end
  3911.   end
  3912.   #--------------------------------------------------------------------------
  3913.   # * New method: drg_frost! (5 second)
  3914.   # NOTE: Frosting a bitmap randomly scatters its pixels. As such, Consistant
  3915.   #       results are impossible to get.
  3916.   #       Frosting a bitmap beyond eight won't result in a more scattered image
  3917.   #--------------------------------------------------------------------------
  3918.   unless method_defined?(:drg_frost!)
  3919.     def drg_frost!(noise = 10)
  3920.       for pass in 1..noise
  3921.         (t_Fx = width).times do |w|
  3922.           height.times do |h|
  3923.             Graphics.update if (t_Fx += 1) % 10000 == 0
  3924.             set_pixel(w, h, get_pixel(w + rand(3) - 1, h + rand(3) - 1))
  3925.           end
  3926.         end
  3927.       end
  3928.     end
  3929.   end
  3930.   #-------------------------------------------------------------------------
  3931.   # ● New method: bltapiinit
  3932.   #-------------------------------------------------------------------------
  3933.   unless method_defined?(:bltapiinit)
  3934.     def bltapiinit
  3935.       info = [40,width,height,1,32,0,0,0,0,0,0].pack('LllSSLLllLL')
  3936.       hWndDC = LiTTleDRAgo.getdc.call(LiTTleDRAgo.hwnd)
  3937.       hMemDC = LiTTleDRAgo.ccdc.call(hWndDC)
  3938.       hMemBM = LiTTleDRAgo.ccbitmap.call(hWndDC, width, height)
  3939.       LiTTleDRAgo.releasedc.call(LiTTleDRAgo.hwnd, hWndDC)
  3940.       LiTTleDRAgo.selectobject.call(hMemDC, hMemBM)
  3941.       LiTTleDRAgo.setdibits.call(hMemDC, hMemBM, 0, height, address, info, 0)
  3942.       return hMemDC, hMemBM, info
  3943.     end
  3944.   end
  3945.   #-------------------------------------------------------------------------
  3946.   # ● New method: bit_blt
  3947.   #-------------------------------------------------------------------------
  3948.   unless method_defined?(:bit_blt)
  3949.     def bit_blt(x, y, src_bmp, src_rect, rastor_operation = 0xCC0020)
  3950.       hDC1, hBM1, info = *self.bltapiinit
  3951.       hDC2, hBM2 = *src_bmp.bltapiinit
  3952.       LiTTleDRAgo.bitblt.call(hDC1, x, y, src_rect.width, src_rect.height,
  3953.                   hDC2, src_rect.x, src_rect.y, rastor_operation)
  3954.       LiTTleDRAgo.getdibits.call(hDC1, hBM1, 0, height, address, info, 0)
  3955.       LiTTleDRAgo.deleteobject.call(hBM1)
  3956.       LiTTleDRAgo.deleteobject.call(hDC1)
  3957.       LiTTleDRAgo.deleteobject.call(hBM2)
  3958.       LiTTleDRAgo.deleteobject.call(hDC2)
  3959.     end
  3960.   end  
  3961.   #-------------------------------------------------------------------------
  3962.   # ● New method: crop_blt
  3963.   #-------------------------------------------------------------------------
  3964.   unless method_defined?(:crop_blt)
  3965.     def crop_blt(x, y, width, height, bitmap, dir = 1, align = 1, opacity = 255)
  3966.       w, h = bitmap.width, bitmap.height
  3967.       if w < width and h < height
  3968.         x += (width - w) / 2 if align == 1
  3969.         x += width - w       if align == 2
  3970.         return full_blt(x, y, bitmap, opacity)
  3971.       end
  3972.       i, j = dir % 3, dir / 3
  3973.       crop_x, crop_y = 0, 0
  3974.       crop_x = (w - width) / 2  if i == 1
  3975.       crop_x = w - width        if i == 2
  3976.       crop_y = (h - height) / 2 if j == 1
  3977.       crop_y = h - height       if j == 2
  3978.       blt(x, y, bitmap, Rect.new(crop_x, crop_y, width, height), opacity)
  3979.     end
  3980.   end
  3981.   #-------------------------------------------------------------------------
  3982.   # ● New method: fit_blt
  3983.   #-------------------------------------------------------------------------
  3984.   unless method_defined?(:fit_blt)  
  3985.     def fit_blt(x, y, width, height, bitmap, opacity = 255, align = 1)
  3986.       w, h = bitmap.width, bitmap.height
  3987.       if w > width or h > height
  3988.         conversion = w / h.to_f
  3989.         zoom_x, zoom_y = width * conversion, height if conversion <= 1
  3990.         zoom_x, zoom_y = width, height / conversion unless conversion <= 1
  3991.         x += (width - zoom_x) / 2 if align == 1
  3992.         x += width - zoom_x       if align == 2
  3993.         dest_rect = Rect.new(x, y, zoom_x, zoom_y)
  3994.         stretch_blt(dest_rect, bitmap, bitmap.rect, opacity)
  3995.       else
  3996.         x += (width - w) / 2 if align == 1
  3997.         x += width - w       if align == 2
  3998.         full_blt(x, y, bitmap, opacity)
  3999.       end
  4000.     end
  4001.   end
  4002.   #-------------------------------------------------------------------------
  4003.   # ● New method: full_blt
  4004.   #-------------------------------------------------------------------------
  4005.   unless method_defined?(:full_blt)
  4006.     def full_blt(x, y, bitmap, opacity = 255)
  4007.       blt(x, y, bitmap, bitmap.rect, opacity)
  4008.     end
  4009.   end
  4010.   #-------------------------------------------------------------------------
  4011.   # ● New method: scale_blt
  4012.   #-------------------------------------------------------------------------
  4013.   unless method_defined?(:scale_blt)
  4014.     def scale_blt(dest_rect, src_bitmap, src_rect = src_bitmap.rect, o = 255)
  4015.       w, h = src_rect.width, src_rect.height
  4016.       scale = [w / dest_rect.width.to_f, h / dest_rect.height.to_f].max
  4017.       ow, oh = (w / scale).to_i, (h / scale).to_i
  4018.       ox, oy = (dest_rect.width - ow) / 2, (dest_rect.height - oh) / 2
  4019.       stretch_blt(Rect.new(ox + dest_rect.x, oy + dest_rect.y, ow, oh),
  4020.         src_bitmap, src_rect, o)
  4021.     end
  4022.   end
  4023.   #-------------------------------------------------------------------------
  4024.   # ● New method: frames_flip_horizontal
  4025.   #-------------------------------------------------------------------------
  4026.   unless method_defined?(:frames_flip_horizontal)
  4027.     def frames_flip_horizontal(frames)
  4028.       frame_width = width / frames
  4029.       frame_bitmap = Bitmap.new(frame_width, height)
  4030.       rect = Rect.new(0, 0, frame_width, height)
  4031.       frames.times do |i|
  4032.         frame_bitmap.clear
  4033.         rect.x = (x = i * frame_width)
  4034.         frame_bitmap.blt(0, 0, self, rect)
  4035.         frame_bitmap.flip_horizontal!
  4036.         blt(x, 0, frame_bitmap, frame_bitmap.rect)
  4037.       end
  4038.       frame_bitmap.dispose
  4039.     end
  4040.   end
  4041.   #-------------------------------------------------------------------------
  4042.   # ● New method: frames_flip_vertical
  4043.   #-------------------------------------------------------------------------
  4044.   unless method_defined?(:frames_flip_vertical)
  4045.     def frames_flip_vertical(frames)
  4046.       frame_height = height / frames
  4047.       frame_bitmap = Bitmap.new(width, frame_height)
  4048.       rect = Rect.new(0, 0, width, frame_height)
  4049.       frames.times do |i|
  4050.         frame_bitmap.clear        
  4051.         rect.y = (y = i * frame_height)
  4052.         frame_bitmap.blt(0, 0, self, rect)
  4053.         frame_bitmap.flip_vertical!
  4054.         blt(0, y, frame_bitmap, frame_bitmap.rect)
  4055.       end
  4056.       frame_bitmap.dispose
  4057.     end
  4058.   end
  4059.   #-------------------------------------------------------------------------
  4060.   # ● New method: blur_area
  4061.   #-------------------------------------------------------------------------
  4062.   unless method_defined?(:blur_area)
  4063.     def blur_area(settings = {})
  4064.       blur_settings.each do |default, setting|
  4065.         settings[default] = setting unless settings.has_key?(default)
  4066.       end
  4067.       keys = settings.keys
  4068.       rect_defined = keys.include?('rect')
  4069.       rect_p_defined = keys.include?('x') && keys.include?('y') &&
  4070.                        keys.include?('w') && keys.include?('h')
  4071.       if rect_defined
  4072.         rect = settings['rect']
  4073.         x, y, w, h = rect.x, rect.y, rect.width, rect.height
  4074.       elsif rect_p_defined
  4075.         x, y, w, h = settings['x'], settings['y'], settings['w'], settings['h']
  4076.       else
  4077.         x, y, w, h = 0, 0, self.width, self.height
  4078.       end
  4079.       dummy = self.dup
  4080.       spacing = settings['spacing']
  4081.       opacity = settings['opacity']
  4082.       settings['offset'].times do |i|
  4083.         src_rects = []
  4084.         src_rects << Rect.new(x + i * spacing, y + i * spacing, w, h)
  4085.         src_rects << Rect.new(x - i * spacing, y + i * spacing, w, h)
  4086.         src_rects << Rect.new(x + i * spacing, y - i * spacing, w, h)
  4087.         src_rects << Rect.new(x - i * spacing, y - i * spacing, w, h)
  4088.         o = Integer(opacity * (settings['offset'] - i) / (settings['offset']))
  4089.         src_rects.each { |src_rect|  blt(x, y, dummy, src_rect, o)}
  4090.       end
  4091.       dummy.dispose
  4092.       self
  4093.     end
  4094.   end
  4095.   #-------------------------------------------------------------------------
  4096.   # ● New method: sub_bitmap_blend
  4097.   #-------------------------------------------------------------------------
  4098.   unless method_defined?(:sub_bitmap_blend)
  4099.     def sub_bitmap_blend(x = 0, y = 0, bitmap = self, src_rect = bitmap.rect)
  4100.       src_rect.width.times do |i|
  4101.         src_rect.height.times do |j|
  4102.           c1 = get_pixel(x + i, y + j)
  4103.           c2 = bitmap.get_pixel(src_rect.x + i, src_rect.y + j)
  4104.           nc = Color.new(c1.red-c2.red, c1.green-c2.green, c1.blue-c2.blue)
  4105.           set_pixel(x + i, y + j, nc) if c2.alpha > 0
  4106.         end
  4107.       end
  4108.     end
  4109.   end
  4110.   #-------------------------------------------------------------------------
  4111.   # ● New method: add_bitmap_blend
  4112.   #-------------------------------------------------------------------------
  4113.   unless method_defined?(:add_bitmap_blend)
  4114.     def add_bitmap_blend(x = 0, y = 0, bitmap = self, src_rect = bitmap.rect)
  4115.       src_rect.width.times do |i|
  4116.         src_rect.height.times do |j|
  4117.           c1 = get_pixel(x + i, y + j)
  4118.           c2 = bitmap.get_pixel(src_rect.x + i, src_rect.y + j)
  4119.           nc = Color.new(c1.red+c2.red, c1.green+c2.green, c1.blue+c2.blue)
  4120.           set_pixel(x + i, y + j, nc) if c2.alpha > 0
  4121.         end
  4122.       end
  4123.     end
  4124.   end
  4125.   #--------------------------------------------------------------------------
  4126.   # * Alias Listing
  4127.   #--------------------------------------------------------------------------
  4128.   alias_sec_method :frost!,      :drg_frost!
  4129.   alias_sec_method :blur,        :blur_area
  4130.   alias_sec_method :entire_fill, :full_fill
  4131.     #--------------------------------------------------------------------------
  4132.     # * Replicated method: gradient_fill_rect
  4133.     #--------------------------------------------------------------------------
  4134.   unless method_defined?(:gradient_fill_rect)
  4135.     def gradient_fill_rect(*a)
  4136.       if a.first.is_a?(Rect)
  4137.         x, y, width, height = a[0].x, a[0].y, a[0].width, a[0].height
  4138.         color1 = a.at(1).dup
  4139.         color2 = a.at(2)
  4140.         vertical = a.at(3)
  4141.       else
  4142.         x, y, width, height = a[0..3]
  4143.         color1 = a.at(4).dup
  4144.         color2 = a.at(5)
  4145.         vertical = a.at(6)
  4146.       end
  4147.       dr = color2.red   - color1.red
  4148.       dg = color2.green - color1.green
  4149.       db = color2.blue  - color1.blue
  4150.       da = color2.alpha - color1.alpha
  4151.       start_rgba = [color1.red, color1.green, color1.blue, color1.alpha]
  4152.       if vertical
  4153.         dr, dg, db, da = dr / height, dg / height, db / height, da / height
  4154.         for i in (y.round...(y + height).round)
  4155.           fill_rect(x, i, width, 1, color1)
  4156.           start_rgba[0] = [start_rgba.at(0) + dr, 0].max.round
  4157.           start_rgba[1] = [start_rgba.at(1) + dg, 0].max.round
  4158.           start_rgba[2] = [start_rgba.at(2) + db, 0].max.round
  4159.           start_rgba[3] = [start_rgba.at(3) + da, 0].max.round
  4160.           color1.set(*start_rgba)
  4161.         end
  4162.       else
  4163.         dr, dg, db, da = dr / width, dg / width, db / width, da / width
  4164.         for i in (x.round...(x + width).round)
  4165.           fill_rect(i, y, 1, height, color1)
  4166.           start_rgba[0] = [start_rgba.at(0) + dr, 0].max.round
  4167.           start_rgba[1] = [start_rgba.at(1) + dg, 0].max.round
  4168.           start_rgba[2] = [start_rgba.at(2) + db, 0].max.round
  4169.           start_rgba[3] = [start_rgba.at(3) + da, 0].max.round
  4170.           color1.set(*start_rgba)
  4171.         end
  4172.       end
  4173.     end
  4174.   end
  4175. end
  4176. #==============================================================================
  4177. # ** String    
  4178. #------------------------------------------------------------------------------
  4179. #  
  4180. #==============================================================================
  4181. class String
  4182.   #-------------------------------------------------------------------------
  4183.   # ● New method: get_int
  4184.   #-------------------------------------------------------------------------
  4185.   unless method_defined?(:get_int)
  4186.     def get_int
  4187.       m = self.scan(/([-]?\d+)/i).flatten
  4188.       return m.join.to_i
  4189.     end
  4190.   end
  4191.   #-------------------------------------------------------------------------
  4192.   # ● New method: get_ints
  4193.   #-------------------------------------------------------------------------
  4194.   unless method_defined?(:get_ints)
  4195.     def get_ints
  4196.       array = self.scan(/([-]?\d+)/i).flatten.relay(:i)
  4197.       return array
  4198.     end
  4199.   end
  4200.   #-------------------------------------------------------------------------
  4201.   # ● New method: encrypt
  4202.   #-------------------------------------------------------------------------
  4203.   unless method_defined?(:encrypt)
  4204.     def encrypt(encryption_string = 'encrypt')
  4205.       encryption_bytes = []
  4206.       encryption_string.each_byte {|c| encryption_bytes << c}
  4207.       string = ''
  4208.       self.size.times do |i|
  4209.         byte = self[i] * encryption_bytes[i % encryption_bytes.size]
  4210.         base, mod = byte / 255, byte % 255
  4211.         string += base.chr + mod.chr
  4212.       end
  4213.       return string
  4214.     end
  4215.   end
  4216.   #-------------------------------------------------------------------------
  4217.   # ● New method: encrypt!
  4218.   #-------------------------------------------------------------------------
  4219.   unless method_defined?(:encrypt!)
  4220.     def encrypt!(encryption_string = 'encrypt')
  4221.       replace(encrypt(encryption_string))
  4222.     end
  4223.   end
  4224.   #-------------------------------------------------------------------------
  4225.   # ● New method: decrypt
  4226.   #-------------------------------------------------------------------------
  4227.   unless method_defined?(:decrypt)
  4228.     def decrypt(encryption_string = 'encrypt')
  4229.       encryption_bytes = []
  4230.       encryption_string.each_byte {|c| encryption_bytes << c}
  4231.       string = ''
  4232.       (self.size / 2).times do |i|
  4233.         b, m = self[i * 2] * 255, self[i * 2 + 1]
  4234.         string += ((b + m) / encryption_bytes[i % encryption_bytes.size]).chr
  4235.       end
  4236.       return string
  4237.     end
  4238.   end
  4239.   #-------------------------------------------------------------------------
  4240.   # ● New method: decrypt!
  4241.   #-------------------------------------------------------------------------
  4242.   unless method_defined?(:decrypt!)
  4243.     def decrypt!(encryption_string = 'encrypt')
  4244.       replace(decrypt(encryption_string))
  4245.     end
  4246.   end
  4247.   #-------------------------------------------------------------------------
  4248.   # ● New method: clear, words, characters
  4249.   #-------------------------------------------------------------------------
  4250.   define_sec_method(:clear)      { replace('')      }
  4251.   define_sec_method(:words)      { split(' ')       }
  4252.   define_sec_method(:characters) { split('')        }
  4253.   define_sec_method(:last)       { characters.last  }
  4254.   define_sec_method(:first)      { characters.first }
  4255. end
  4256.  
  4257. #==============================================================================
  4258. # ** Game_Interpreter
  4259. #------------------------------------------------------------------------------
  4260. #  An interpreter for executing event commands. This class is used within the
  4261. # Game_Map, Game_Troop, and Game_Event classes.
  4262. #==============================================================================
  4263. Klass = LiTTleDRAgo::VX ? Game_Interpreter : Interpreter
  4264. class Klass
  4265.   #--------------------------------------------------------------------------
  4266.   # * Constant
  4267.   #--------------------------------------------------------------------------
  4268.   SCRIPT_WAIT_RESULTS = [:wait, FalseClass]
  4269.   #--------------------------------------------------------------------------
  4270.   # ● Redirect Listing
  4271.   #--------------------------------------------------------------------------
  4272.   redirect_method :picture_sprite,        '$game_map.picture_sprite'
  4273.   #--------------------------------------------------------------------------
  4274.   # ● New method: random_teleport
  4275.   #--------------------------------------------------------------------------
  4276.   unless method_defined?(:random_teleport)
  4277.     def random_teleport(id = 0)
  4278.       character = id.is_a?(Game_Character) ? id : get_character(id)
  4279.       character.random_teleport if character.is_a?(Game_Character)
  4280.     end
  4281.   end
  4282.   #--------------------------------------------------------------------------
  4283.   # ● Redefined method: same_map?, next_event_code
  4284.   #--------------------------------------------------------------------------
  4285.   define_sec_method(:same_map?)       { @map_id == $game_map.map_id    }
  4286.   define_sec_method(:next_event_code) { @list[@index + 1].code         }
  4287.   #--------------------------------------------------------------------------
  4288.   # * Overwriten method: command_355
  4289.   #--------------------------------------------------------------------------
  4290.   def command_355
  4291.     script = @list[index = @index].parameters[0] + "\n"
  4292.     while [655, 355].include?(next_event_code) do
  4293.       script += @list[@index+=1].parameters[0] + "\n"
  4294.     end
  4295.     wait = SCRIPT_WAIT_RESULTS.include?(eval(script)) && LiTTleDRAgo::XP
  4296.     return wait ? !(@index = index) : true
  4297.   end  
  4298. end
  4299.  
  4300. #==============================================================================
  4301. # ** Sprite
  4302. #------------------------------------------------------------------------------
  4303. #  A sprite class for bitmap processing.
  4304. #==============================================================================
  4305. class Sprite
  4306.   #---------------------------------------------------------------------------
  4307.   # * Alias Listing
  4308.   #---------------------------------------------------------------------------
  4309.   alias_sec_method(:angle_fix_alias, :"angle=")
  4310.   alias_sec_method(:drg_core_dispose,:"dispose")
  4311.   alias_sec_method(:set_x_alias,     :"x=")
  4312.   alias_sec_method(:set_y_alias,     :"y=")
  4313.   alias_sec_method(:get_x_alias,     :"x")
  4314.   alias_sec_method(:get_y_alias,     :"y")
  4315.   #---------------------------------------------------------------------------
  4316.   # * New method: method_missing
  4317.   #---------------------------------------------------------------------------
  4318.   def method_missing(val,*a,&b)
  4319.     return bitmap.send(val.to_s,*a,&b) if bitmap.respond_to?(val.to_sym)
  4320.     text = "Undefined method #{val} for #{self.inspect}"
  4321.     raise(NoMethodError, text, caller(1))              
  4322.   end
  4323.   #---------------------------------------------------------------------------
  4324.   # * Aliased method: x=, y=
  4325.   #---------------------------------------------------------------------------
  4326.   define_method(:x=)      {|amt| set_x_alias((@intfloat_x = amt).to_i) }
  4327.   define_method(:y=)      {|amt| set_y_alias((@intfloat_y = amt).to_i) }
  4328.   define_method(:x)       { @intfloat_x ||= get_x_alias }
  4329.   define_method(:y)       { @intfloat_y ||= get_y_alias }
  4330.   define_method(:dispose) { disposed? || drg_core_dispose }
  4331.   define_method(:rotate_correction) { self.ox,self.oy = width/2, height/2 }
  4332.   #---------------------------------------------------------------------------
  4333.   # * Aliased method: angle=
  4334.   #---------------------------------------------------------------------------
  4335.   def angle=(v)
  4336.     return if @angle == v
  4337.     sign = (v % 360 == 180 || v % 180 == 90) ? 1.00001 : 1
  4338.     angle_fix_alias((@angle = v) * sign)
  4339.   end
  4340.   #--------------------------------------------------------------------------
  4341.   # ● Redefined method: width, height
  4342.   #--------------------------------------------------------------------------
  4343.   define_sec_method(:width)       { src_rect.width }
  4344.   define_sec_method(:height)      { src_rect.height }
  4345.   #--------------------------------------------------------------------------
  4346.   # ● Overwriten method: dup
  4347.   #--------------------------------------------------------------------------
  4348.   def dup
  4349.     sprite = self.class.new(self.viewport)
  4350.     sprite.bitmap                = self.bitmap.clone
  4351.     sprite.zoom_x, sprite.zoom_y = self.zoom_x, self.zoom_y
  4352.     sprite.x, sprite.y           = self.x, self.y
  4353.     sprite.ox, sprite.oy         = self.ox, self.oy
  4354.     sprite.color, sprite.tone    = self.color.dup, self.tone.dup
  4355.     sprite.src_rect,sprite.angle = self.src_rect, self.angle
  4356.     return sprite
  4357.   end
  4358.   #--------------------------------------------------------------------------
  4359.   # ● Overwriten method: clone
  4360.   #--------------------------------------------------------------------------
  4361.   def clone
  4362.     (sprite = self.dup) && instance_variables.each do |var|
  4363.       temp = instance_variable_get("#{var}")
  4364.       sprite.instance_variable_set("#{var}",temp) rescue nil
  4365.     end
  4366.     (frozen? && sprite.freeze)
  4367.     return sprite  
  4368.   end
  4369.   #--------------------------------------------------------------------------
  4370.   # ● New method: scale
  4371.   #--------------------------------------------------------------------------
  4372.   unless method_defined?(:scale)
  4373.     def scale(w, h)
  4374.       tmp = self.bitmap.clone
  4375.       self.bitmap = Bitmap.new(w, h)
  4376.       self.bitmap.stretch_blt(Rect.new(0, 0, w, h), tmp, tmp.rect)
  4377.       tmp.dispose
  4378.     end
  4379.   end
  4380.   #--------------------------------------------------------------------------
  4381.   # ● New method: in_rect?
  4382.   #--------------------------------------------------------------------------
  4383.   unless method_defined?(:in_rect?)
  4384.     def in_rect?(*rect)
  4385.       if rect.size == 1
  4386.         x, y, w, h = rect[0].x, rect[0].y, rect[0].width, rect[0].height
  4387.         return self.x.between?(x, x + w) && self.y.between?(y, y + h)
  4388.       elsif rect.size == 4
  4389.         x, y, w, h = rect
  4390.         return self.x.between?(x, x + w) && self.y.between?(y, y + h)
  4391.       end
  4392.       return false
  4393.     end
  4394.   end
  4395. end
  4396.  
  4397. #==============================================================================
  4398. # ** Scheduler
  4399. #------------------------------------------------------------------------------
  4400. #  This class allows to schedule a proc or method call a given amount of frames
  4401. #  into the future with any amount of arguments
  4402. #==============================================================================
  4403. unless defined?(Scheduler)
  4404.   class Scheduler
  4405.     #==========================================================================
  4406.     # ** Order
  4407.     #--------------------------------------------------------------------------
  4408.     #  An order is a proc, method or something else which has 'call' as a
  4409.     #  method, and the arguments to pass along.
  4410.     #==========================================================================
  4411.     unless method_defined?(:update)
  4412.       Order = Struct.new(:callable, :arguments)
  4413.       class Order
  4414.         #----------------------------------------------------------------------
  4415.         # * New Methods: call
  4416.         #----------------------------------------------------------------------
  4417.         define_method(:call) { callable.call(*arguments) }
  4418.       end
  4419.       #========================================================================
  4420.       # ** RecurringOrder
  4421.       #------------------------------------------------------------------------
  4422.       #  An order which is recurring every specified amount of time until
  4423.       #  FalseClass is returned from the call.
  4424.       #  Note that arguments remain the same for each call
  4425.       #========================================================================
  4426.       RecurringOrder = Struct.new(:callable, :arguments, :frames)
  4427.       class RecurringOrder
  4428.         #----------------------------------------------------------------------
  4429.         # * New Methods: call
  4430.         #----------------------------------------------------------------------
  4431.         def call
  4432.           result = callable.call(*arguments)
  4433.           unless result == FalseClass
  4434.             Scheduler.schedule_recurring(frames, frames, callable, *arguments)
  4435.           end
  4436.         end
  4437.       end
  4438.       #========================================================================
  4439.       # ** Mapping
  4440.       #------------------------------------------------------------------------
  4441.       # Maps an index to an array. Values can be added to these value.
  4442.       # Each array starts empty.
  4443.       #========================================================================
  4444.       class Mapping
  4445.         #----------------------------------------------------------------------
  4446.         # * New method: add, get, empty
  4447.         #----------------------------------------------------------------------
  4448.         define_method(:add)   { |i, v| ((@mapping ||= {})[i] ||=[]) << v }
  4449.         define_method(:get)   { |i|    ((@mapping ||= {})[i] || [])   }
  4450.         define_method(:empty) { |i|     (@mapping ||= {}).delete(i)   }
  4451.       end
  4452.       #------------------------------------------------------------------------
  4453.       # * New method: schedule
  4454.       #------------------------------------------------------------------------
  4455.       def schedule(frames, callable, *arguments)
  4456.         order = Order.new(callable, arguments)
  4457.         (@mapping ||= Mapping.new).add(frames + (@tick||=0), order)
  4458.       end
  4459.       #------------------------------------------------------------------------
  4460.       # * New method: schedule_recurring
  4461.       #------------------------------------------------------------------------
  4462.       def schedule_recurring(frames, frames_to_wait, callable, *arguments)
  4463.         order = RecurringOrder.new(callable, arguments, frames_to_wait)
  4464.         (@mapping ||= Mapping.new).add(frames + (@tick||=0), order)
  4465.       end
  4466.       #------------------------------------------------------------------------
  4467.       # * New method: update
  4468.       #------------------------------------------------------------------------
  4469.       def update
  4470.         orders = (@mapping ||= Mapping.new).get((@tick||=0))
  4471.         @mapping.empty(@tick)
  4472.         orders.each {|s| s.call }
  4473.         @tick += 1
  4474.       end
  4475.       #------------------------------------------------------------------------
  4476.       # * Self
  4477.       #------------------------------------------------------------------------
  4478.       @@instance = self.new
  4479.       class << self
  4480.         define_method(:instance) { @@instance }
  4481.         redirect_method :schedule_recurring, 'instance.schedule_recurring'
  4482.         redirect_method :schedule,           'instance.schedule'
  4483.         redirect_method :update,             'instance.update'
  4484.       end
  4485.     end
  4486.   end
  4487.   Graphics.auto_update.push([Scheduler,:update])
  4488. end
  4489. #==============================================================================
  4490. # ** Dir    
  4491. #------------------------------------------------------------------------------
  4492. #
  4493. #==============================================================================
  4494. class << Dir
  4495.   #--------------------------------------------------------------------------
  4496.   # ● New method: make_dir
  4497.   #--------------------------------------------------------------------------
  4498.   def make_dir(path)
  4499.     (dir = path.split("/")).size.times do |i|
  4500.       next if dir == "."
  4501.       Dir.mkdir(dir[0..i].join("/")) rescue nil
  4502.     end
  4503.   end
  4504.   #--------------------------------------------------------------------------
  4505.   # ● New method: files_with_extension
  4506.   #--------------------------------------------------------------------------
  4507.   def files_with_extension(directory, extension)
  4508.     entries(directory).select{|file| File.extname(file) == extension}
  4509.   end
  4510. end
  4511.  
  4512. #==============================================================================
  4513. # ** BitmapDump
  4514. #------------------------------------------------------------------------------
  4515. #
  4516. #==============================================================================
  4517. module BitmapDump; end
  4518. class << BitmapDump
  4519.   #--------------------------------------------------------------------------
  4520.   # ● New method: self.bitmap_data
  4521.   #--------------------------------------------------------------------------
  4522.   unless method_defined?(:bitmap_data)
  4523.     def bitmap_data(bitmap)
  4524.       red = Table.new(bitmap.width, bitmap.height)
  4525.       green, blue, alpha = red.clone, red.clone, red.clone      
  4526.       bitmap.width.times do |i|
  4527.         bitmap.height.times do |j|
  4528.           color = bitmap.get_pixel(i, j)
  4529.           red[i, j],  green[i, j] = color.red, color.green
  4530.           blue[i, j], alpha[i, j] = color.blue, color.alpha
  4531.         end
  4532.       end
  4533.       return [red, green, blue, alpha]
  4534.     end
  4535.   end
  4536.   #--------------------------------------------------------------------------
  4537.   # ● New method: self.read_bitmap_data
  4538.   #--------------------------------------------------------------------------
  4539.   unless method_defined?(:read_bitmap_data)
  4540.     def read_bitmap_data(red, green, blue, alpha)
  4541.       bitmap = Bitmap.new(red.xsize, red.ysize)
  4542.       bitmap.width.times do |i|
  4543.         bitmap.height.times do |j|
  4544.           color_table = [red[i, j], green[i, j], blue[i, j], alpha[i, j]]
  4545.           bitmap.set_pixel(i, j, Color.new(*color_table))
  4546.         end
  4547.       end
  4548.       return bitmap
  4549.     end
  4550.   end
  4551.   #--------------------------------------------------------------------------
  4552.   # ● New method: self.save_bitmap
  4553.   #--------------------------------------------------------------------------
  4554.   unless method_defined?(:save_bitmap)
  4555.     def save_bitmap(bitmap, filename, folder = '')
  4556.       bitmap_data = bitmap_data(bitmap)
  4557.       if folder && folder != ''
  4558.         Dir.make_dir(folder) unless FileTest.directory?(folder)
  4559.         filename = "#{folder}/#{filename}"
  4560.       end
  4561.       filename = filename.gsub("\\","/").gsub("//","/")
  4562.       file = File.open(filename,'wb')
  4563.       Marshal.dump(bitmap_data, file)
  4564.       file.close
  4565.     end
  4566.   end
  4567.   #--------------------------------------------------------------------------
  4568.   # ● New method: self.load_bitmap
  4569.   #--------------------------------------------------------------------------
  4570.   unless method_defined?(:load_bitmap)
  4571.     def load_bitmap(filename, folder = '')
  4572.       if folder && folder != ''
  4573.         Dir.make_dir(folder) unless FileTest.directory?(folder)
  4574.         filename = "#{folder}/#{filename}"
  4575.       end
  4576.       filename = filename.gsub("\\","/").gsub("//","/")
  4577.       file = File.open(filename,"rb")
  4578.       colors = Marshal.load(file)
  4579.       file.close
  4580.       bitmap = read_bitmap_data(colors)
  4581.       return bitmap
  4582.     end
  4583.   end
  4584. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement