Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #==============================================================================
  2. # ** Drago - Core Engine
  3. # Version : 1.59
  4. # Contact : littledrago.blogspot.com / forum.chaos-project.com
  5. #==============================================================================
  6. ($imported ||= {})[:drg_core_engine] = 1.59
  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_method)
  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. # ** Sprite_Battler
  1962. #------------------------------------------------------------------------------
  1963. #  This sprite is used to display the battler.It observes the Game_Character
  1964. #  class and automatically changes sprite conditions.
  1965. #==============================================================================
  1966. class Sprite_Battler
  1967.   #--------------------------------------------------------------------------
  1968.   # * Frame Update
  1969.   #--------------------------------------------------------------------------
  1970.   define_pre_alias(:update) {  @battler.nil? && @battler_name = nil }
  1971. end
  1972.  
  1973. #==============================================================================
  1974. # ** Spriteset_Map
  1975. #------------------------------------------------------------------------------
  1976. #  This class brings together map screen sprites, tilemaps, etc.
  1977. #  It's used within the Scene_Map class.
  1978. #==============================================================================
  1979. class Spriteset_Map
  1980.   #--------------------------------------------------------------------------
  1981.   # ● New method: sprite_player
  1982.   #--------------------------------------------------------------------------
  1983.   define_sec_method(:sprite_player) { find_character($game_player) }
  1984.   #------------------------------------------------------------------------
  1985.   # ● Public Instance Variables
  1986.   #------------------------------------------------------------------------
  1987.   attr_sec_reader :character_sprites, 'Array.new'
  1988.   attr_reader     :viewport1, :viewport2, :viewport3
  1989.   #--------------------------------------------------------------------------
  1990.   # * Aliased method: update
  1991.   #--------------------------------------------------------------------------
  1992.   define_pre_alias(:update) { update_viewport_size_change }
  1993.   #--------------------------------------------------------------------------
  1994.   # * New method: update_viewport_size_change
  1995.   #--------------------------------------------------------------------------
  1996.   def update_viewport_size_change
  1997.     if viewport_size_change?
  1998.       @viewport_map_width     = $game_map.width
  1999.       @viewport_map_height    = $game_map.height
  2000.       @viewport_screen_width  = Graphics.width
  2001.       @viewport_screen_height = Graphics.height
  2002.       [@viewport1,@viewport2,@viewport3].compact.update_viewport_sizes
  2003.     end
  2004.   end
  2005.   #--------------------------------------------------------------------------
  2006.   # * New method: viewport_size_change?
  2007.   #--------------------------------------------------------------------------
  2008.   unless method_defined?(:viewport_size_change?)
  2009.     def viewport_size_change?
  2010.       return true if @viewport_map_width     != $game_map.width
  2011.       return true if @viewport_map_height    != $game_map.height
  2012.       return true if @viewport_screen_width  != Graphics.width
  2013.       return true if @viewport_screen_height != Graphics.height
  2014.     end
  2015.   end
  2016.   #--------------------------------------------------------------------------
  2017.   # ● New method: redraw_character_sprites
  2018.   #--------------------------------------------------------------------------
  2019.   unless method_defined?(:redraw_character_sprites)
  2020.     def redraw_character_sprites
  2021.       return refresh_characters if self.respond_to?(:refresh_characters)
  2022.       character_sprites.dispose
  2023.       @character_sprites = $game_map.events.keys.sort.map {|i|
  2024.          Sprite_Character.new(@viewport1, $game_map.events[i]) }
  2025.       @character_sprites.push(Sprite_Character.new(@viewport1, $game_player))
  2026.     end
  2027.   end  
  2028.   #--------------------------------------------------------------------------
  2029.   # ● New method: find_character
  2030.   #--------------------------------------------------------------------------
  2031.   unless method_defined?(:find_character)
  2032.     def find_character(char)
  2033.       character_sprites.detect { |s| s.character == char }
  2034.     end
  2035.   end
  2036.   #---------------------------------------------------------------------------
  2037.   # * New method: viewport_sprite
  2038.   #---------------------------------------------------------------------------
  2039.   def viewport_sprite(*v)
  2040.     v.collect! {|s| s.is_a?(Symbol) ? instance_variable_get(:"@#{s}") : s }
  2041.     v.reject!  {|s| s.not.is_a?(Viewport)}
  2042.     all = instance_variables.map {|s| instance_variable_get("#{s}")}.flatten
  2043.     all.select {|s| s.respond_to?(:viewport) && v.include?(s.viewport)}
  2044.   end
  2045. end
  2046.  
  2047. #==============================================================================
  2048. # ** Game_Battler
  2049. #------------------------------------------------------------------------------
  2050. #  This class deals with battlers. It's used as a superclass for the Game_Actor
  2051. #  and Game_Enemy classes.
  2052. #==============================================================================
  2053. class Game_Battler
  2054.   #--------------------------------------------------------------------------
  2055.   # ● New method: alive?
  2056.   #--------------------------------------------------------------------------
  2057.   define_sec_method(:alive?) { self.not.dead? }
  2058.   #--------------------------------------------------------------------------
  2059.   # ● New method: hp_percent
  2060.   #--------------------------------------------------------------------------
  2061.   unless method_defined?(:hp_percent)
  2062.     def hp_percent(integer = false, float_points = 2)
  2063.       n = Math.percent(hp,(respond_to?(:mhp) ? mhp : maxhp))
  2064.       return integer ? Integer(n) : n.float_points(float_points)
  2065.     end
  2066.   end
  2067.   #--------------------------------------------------------------------------
  2068.   # ● New method: sp_percent
  2069.   #--------------------------------------------------------------------------
  2070.   unless method_defined?(:sp_percent)
  2071.     def sp_percent(integer = false, float_points = 2)
  2072.       n =  Math.percent((respond_to?(:sp) ? sp : mp),
  2073.       ((respond_to?(:mmp) ? mmp : (respond_to?(:maxsp) ? maxsp : maxmp))))
  2074.       return integer ? Integer(n) : n.float_points(float_points)
  2075.     end
  2076.     alias_method :mp_percent, :sp_percent
  2077.   end
  2078. end
  2079.  
  2080. #==============================================================================
  2081. # ** Game_Character
  2082. #------------------------------------------------------------------------------
  2083. #  This class deals with characters. It's used as a superclass for the
  2084. #  Game_Player and Game_Event classes.
  2085. #==============================================================================
  2086. class Game_Character
  2087.   #--------------------------------------------------------------------------
  2088.   # ● Redirect Listing
  2089.   #--------------------------------------------------------------------------
  2090.   redirect_method :move_down,        'move_straight(2,*args)'
  2091.   redirect_method :move_left,        'move_straight(4,*args)'
  2092.   redirect_method :move_right,       'move_straight(6,*args)'
  2093.   redirect_method :move_up,          'move_straight(8,*args)'
  2094.   redirect_method :turn_down,        'set_direction(2,*args)'
  2095.   redirect_method :turn_left,        'set_direction(4,*args)'
  2096.   redirect_method :turn_right,       'set_direction(6,*args)'
  2097.   redirect_method :turn_up,          'set_direction(8,*args)'
  2098.   redirect_method :move_lower_left,  'move_diagonal(4, 2,*args)'
  2099.   redirect_method :move_lower_right, 'move_diagonal(6, 2,*args)'
  2100.   redirect_method :move_upper_left,  'move_diagonal(4, 8,*args)'
  2101.   redirect_method :move_upper_right, 'move_diagonal(6, 8,*args)'
  2102.   redirect_method :character_sprite, '$game_map.character_sprite(self)'
  2103.   redirect_method :character_above?, '(args.first.y > @y)'
  2104.   redirect_method :character_below?, '(args.first.y < @y)'
  2105.   redirect_method :character_right?, '(args.first.x < @x)'
  2106.   redirect_method :character_left?,  '(args.first.x > @x)'
  2107.   redirect_method :pos,              '[@x, @y]'
  2108.   redirect_method :pos_nt?,          '!@through && pos?'
  2109.   redirect_method :normal_priority?, '@character_name != ""'  
  2110.   redirect_method :map_passable?,    '$game_map.passable?'
  2111.   redirect_method :map_terrain_tag,  '$game_map.terrain_tag'
  2112.   #--------------------------------------------------------------------------
  2113.   # * Redefined method: pos?
  2114.   #                     anime
  2115.   #                     reverse_dir
  2116.   #--------------------------------------------------------------------------
  2117.   define_sec_method(:reverse_dir) {|dir| 10 - dir }
  2118.   define_sec_method(:pos?)        {|x,y| @x == x && @y == y  }
  2119.   define_sec_method(:anime)       {|aid| @animation_id = aid }
  2120.   #--------------------------------------------------------------------------
  2121.   # ● Redefined method: check_event_trigger_touch_front
  2122.   #--------------------------------------------------------------------------
  2123.   unless method_defined?(:check_event_trigger_touch_front)
  2124.     def check_event_trigger_touch_front
  2125.       x2 = $game_map.round_x_with_direction(@x, @direction)
  2126.       y2 = $game_map.round_y_with_direction(@y, @direction)
  2127.       check_event_trigger_touch(x2, y2)
  2128.     end
  2129.   end
  2130.   #--------------------------------------------------------------------------
  2131.   # ● Redefined method: move_straight
  2132.   #--------------------------------------------------------------------------
  2133.   unless method_defined?(:move_straight)
  2134.     def move_straight(d, turn_ok = true)
  2135.       if passable?(@x, @y, d)
  2136.         set_direction(d)
  2137.         @x = $game_map.round_x_with_direction(@x, d)
  2138.         @y = $game_map.round_y_with_direction(@y, d)
  2139.         increase_steps
  2140.       elsif turn_ok
  2141.         set_direction(d)
  2142.         check_event_trigger_touch_front
  2143.       end
  2144.     end
  2145.   end  
  2146.   #--------------------------------------------------------------------------
  2147.   # ● Redefined method: set_direction
  2148.   #--------------------------------------------------------------------------
  2149.   unless method_defined?(:set_direction)
  2150.     def set_direction(d)
  2151.       @direction = d unless @direction_fix || d == 0
  2152.       @stop_count = 0
  2153.     end
  2154.   end
  2155.   #--------------------------------------------------------------------------
  2156.   # ● Redefined method: set_graphic
  2157.   #--------------------------------------------------------------------------
  2158.   unless method_defined?(:set_graphic)
  2159.     def set_graphic(character_name, character_index = 0)
  2160.       @tile_id = 0
  2161.       @character_name = character_name
  2162.       if self.respond_to?(:character_index)
  2163.         @character_index = character_index
  2164.       else
  2165.         @character_hue = character_index
  2166.       end
  2167.       @original_pattern = 1
  2168.     end
  2169.   end
  2170.   #--------------------------------------------------------------------------
  2171.   # ● Redefined method: swap
  2172.   #--------------------------------------------------------------------------
  2173.   unless method_defined?(:swap)
  2174.     def swap(character)
  2175.       new_pos = character.pos
  2176.       character.moveto(x, y)
  2177.       moveto(*new_pos)
  2178.     end
  2179.   end
  2180.   #--------------------------------------------------------------------------
  2181.   # ● Redefined method: rotate_swap_prev
  2182.   #--------------------------------------------------------------------------
  2183.   unless method_defined?(:rotate_swap_prev)
  2184.     def rotate_swap_prev(*character)
  2185.       character.unshift(self)
  2186.       position = character.collect {|c| c.pos}
  2187.       character.each_with_index { |c,i| c.moveto(*position[i-1]) }
  2188.     end
  2189.   end
  2190.   #--------------------------------------------------------------------------
  2191.   # ● Redefined method: rotate_swap_next
  2192.   #--------------------------------------------------------------------------
  2193.   unless method_defined?(:rotate_swap_next)
  2194.     def rotate_swap_next(*character)
  2195.       character.unshift(self)
  2196.       position = character.collect {|c| c.pos}
  2197.       character.each_with_index do |char,index|
  2198.         char.moveto(*position[index + 1 >= position.size ? 0 : index + 1])
  2199.       end
  2200.     end
  2201.   end
  2202.   #--------------------------------------------------------------------------
  2203.   # ● New method: distance_from
  2204.   #--------------------------------------------------------------------------
  2205.   unless method_defined?(:distance_from)
  2206.     def distance_from(character,type= 'xy')
  2207.       character = $game_map.events[character] if character.is_a?(Integer)
  2208.       return 0 if character.nil?
  2209.       return (@x - character.x).abs if type.to_s == 'x'
  2210.       return (@y - character.y).abs if type.to_s == 'y'
  2211.       return Math.hypot(@x - character.x, @y - character.y)
  2212.     end
  2213.   end
  2214.   #--------------------------------------------------------------------------
  2215.   # * Redefined method: distance_x_from
  2216.   #--------------------------------------------------------------------------
  2217.   unless method_defined?(:distance_x_from)
  2218.     def distance_x_from(x)
  2219.       result = @x - x
  2220.       if result.abs > $game_map.width / 2
  2221.         result += result < 0 ? $game_map.width : -$game_map.width
  2222.       end
  2223.       result
  2224.     end
  2225.   end
  2226.   #--------------------------------------------------------------------------
  2227.   # * Redefined method: distance_y_from
  2228.   #--------------------------------------------------------------------------
  2229.   unless method_defined?(:distance_y_from)
  2230.     def distance_y_from(y)
  2231.       result = @y - y
  2232.       if result.abs > $game_map.height / 2
  2233.         result += result < 0 ? $game_map.height : -$game_map.height
  2234.       end
  2235.       result
  2236.     end
  2237.   end
  2238.   #--------------------------------------------------------------------------
  2239.   # ● New method: random_teleport
  2240.   #--------------------------------------------------------------------------
  2241.   unless method_defined?(:random_teleport)
  2242.     def random_teleport
  2243.       1000.times do
  2244.         moveto(rand($game_map.height), rand($game_map.width))
  2245.         vx  = LiTTleDRAgo::VX && !LiTTleDRAgo::VXA
  2246.         arg = vx ? [@x,@y] : [@x,@y,@direction]
  2247.         break if passable?(*arg)
  2248.       end || true
  2249.     end
  2250.   end
  2251.   #--------------------------------------------------------------------------
  2252.   # * Redefined method: diagonal_passable? (for RMXP)
  2253.   #     horz : Horizontal (4 or 6)
  2254.   #     vert : Vertical (2 or 8)
  2255.   #--------------------------------------------------------------------------
  2256.   unless method_defined?(:diagonal_passable?)
  2257.     def diagonal_passable?(x, y, horz, vert)
  2258.       x2 = $game_map.round_x_with_direction(x, horz)
  2259.       y2 = $game_map.round_y_with_direction(y, vert)
  2260.       (passable?(x, y, vert) && passable?(x, y2, horz)) ||
  2261.       (passable?(x, y, horz) && passable?(x2, y, vert))
  2262.     end
  2263.   end
  2264.   #--------------------------------------------------------------------------
  2265.   # * Redefined method: collide_with_characters? (for RMXP)
  2266.   #--------------------------------------------------------------------------
  2267.   unless method_defined?(:collide_with_characters?)
  2268.     def collide_with_characters?(x, y)
  2269.       collide_with_events?(x, y)
  2270.     end
  2271.   end
  2272.   #--------------------------------------------------------------------------
  2273.   # * Redefined method: collide_with_events? (for RMXP)
  2274.   #--------------------------------------------------------------------------
  2275.   unless method_defined?(:collide_with_events?)
  2276.     def collide_with_events?(x, y)
  2277.       $game_map.events_xy_nt(x, y).any? do |event|
  2278.         event.normal_priority? || self.is_a?(Game_Event)
  2279.       end
  2280.     end
  2281.   end
  2282.   #--------------------------------------------------------------------------
  2283.   # * Redefined method: debug_through?
  2284.   #--------------------------------------------------------------------------
  2285.   unless method_defined?(:debug_through?)
  2286.     def debug_through?
  2287.       debug = LiTTleDRAgo::VX ? $TEST : $DEBUG
  2288.       return debug && Input.press?(Input::CTRL) if self.is_a?(Game_Player)
  2289.       return false
  2290.     end
  2291.   end
  2292. end
  2293. #==============================================================================
  2294. # ** Game_Actor
  2295. #------------------------------------------------------------------------------
  2296. #  This class handles actors. It is used within the Game_Actors class
  2297. # ($game_actors), also referenced from the Game_Party class ($game_party).
  2298. #==============================================================================
  2299.  
  2300. class Game_Actor
  2301.   #--------------------------------------------------------------------------
  2302.   # * Get Actor Object
  2303.   #--------------------------------------------------------------------------
  2304.   define_sec_method(:actor)  { $data_actors[@actor_id]  }
  2305.   define_method(:class)      { $data_classes[@class_id] }
  2306. end
  2307.  
  2308. #==============================================================================
  2309. # ** Game_Enemy
  2310. #------------------------------------------------------------------------------
  2311. #  This class handles enemies. It used within the Game_Troop class
  2312. # ($game_troop).
  2313. #==============================================================================
  2314.  
  2315. class Game_Enemy
  2316.   #--------------------------------------------------------------------------
  2317.   # * Get Enemy Object
  2318.   #--------------------------------------------------------------------------
  2319.   define_sec_method(:enemy)  { $data_enemies[@enemy_id] }
  2320. end
  2321.  
  2322. #==============================================================================
  2323. # ** Game_Player
  2324. #------------------------------------------------------------------------------
  2325. #  This class handles the player. It includes event starting determinants and
  2326. # map scrolling functions. The instance of this class is referenced by
  2327. # $game_player.
  2328. #==============================================================================
  2329.  
  2330. class Game_Player
  2331.   #--------------------------------------------------------------------------
  2332.   # ● Redefined method: actor
  2333.   #                     name
  2334.   #--------------------------------------------------------------------------
  2335.   define_sec_method(:actor) { $game_party.leader }
  2336.   define_sec_method(:name)  { actor.name         }        
  2337.   #--------------------------------------------------------------------------
  2338.   # ● New method: method_missing
  2339.   #--------------------------------------------------------------------------
  2340.   def method_missing(val,*a,&b)
  2341.     return actor.send(val.to_s,*a,&b) if actor.respond_to?(val.to_sym)
  2342.     text = "Undefined method #{val} for #{self.inspect}"
  2343.     raise(NoMethodError, text, caller(1))              
  2344.   end
  2345. end
  2346.  
  2347. #==============================================================================
  2348. # ** Game_Event
  2349. #------------------------------------------------------------------------------
  2350. #  This class deals with events. It handles functions including event page
  2351. #  switching via condition determinants, and running parallel process events.
  2352. #  It's used within the Game_Map class.
  2353. #==============================================================================
  2354. class Game_Event < Game_Character
  2355.   #--------------------------------------------------------------------------
  2356.   # ● Public Instance Variables
  2357.   #--------------------------------------------------------------------------
  2358.   method_defined?(:erased) || (attr_reader :erased)
  2359.   method_defined?(:id)     || (attr_reader :id)
  2360.   method_defined?(:event)  || (attr_reader :event)
  2361.   attr_accessor :loaded_event
  2362.   #--------------------------------------------------------------------------
  2363.   # ● New method: name
  2364.   # ● New method: restore
  2365.   #--------------------------------------------------------------------------
  2366.   define_sec_method(:name)    { @event.name }
  2367.   define_sec_method(:restore) { (@erased = false) || refresh }
  2368.   #--------------------------------------------------------------------------
  2369.   # ● New method: destroy
  2370.   #--------------------------------------------------------------------------
  2371.   unless method_defined?(:destroy)
  2372.     def destroy
  2373.       event = $game_map.events.delete(@id)
  2374.       if (s = $game_map.spriteset).is_a?(Spriteset_Map)
  2375.         after = s.character_sprites.select { |e| e if e.character != event }
  2376.         (delet = character_sprite) && delet.dispose
  2377.         s.character_sprites.replace(after)
  2378.       end
  2379.     end
  2380.   end
  2381.   #--------------------------------------------------------------------------
  2382.   # ● New method: eliminate
  2383.   #--------------------------------------------------------------------------
  2384.   unless method_defined?(:eliminate)
  2385.     def eliminate
  2386.       return destroy if self.loaded_event
  2387.       (eliminate = $game_map.eliminate_event)[(id = $game_map.map_id)] ||= []
  2388.       (eliminate[id].include?(@id) || eliminate[id].push(@id)) && destroy
  2389.     end
  2390.   end
  2391.   #--------------------------------------------------------------------------
  2392.   # ● New method: note
  2393.   #--------------------------------------------------------------------------
  2394.   unless method_defined?(:note)
  2395.     def note
  2396.       list = @page ? @page.list.select {|l| [108,408].include?(l.code)} : []
  2397.       notes = list.collect {|l| l.parameters[0] }.join("\n")
  2398.       return notes
  2399.     end
  2400.   end
  2401.   #--------------------------------------------------------------------------
  2402.   # ● New method: proper_page_index
  2403.   #--------------------------------------------------------------------------  
  2404.   unless method_defined?(:proper_page_index)
  2405.     def proper_page_index
  2406.       if respond_to?(:conditions_met?)
  2407.         page = respond_to?(:find_proper_page) ? find_proper_page :
  2408.                @event.pages.reverse.find {|page| conditions_met?(page)}
  2409.         (i = @event.pages.index(page)).is_a?(Integer) ? i + 1 : 0
  2410.       end
  2411.     end
  2412.   end
  2413. end
  2414.  
  2415. #==============================================================================
  2416. # ** Game_Party
  2417. #------------------------------------------------------------------------------
  2418. #  This class handles the party. It includes information on amount of gold
  2419. #  and items. Refer to "$game_party" for the instance of this class.
  2420. #==============================================================================
  2421. class Game_Party
  2422.   #--------------------------------------------------------------------------
  2423.   # ● Alias Listing
  2424.   #--------------------------------------------------------------------------
  2425.   alias_sec_method :drg_ce_item_number, :item_number
  2426.   #--------------------------------------------------------------------------
  2427.   # ● Redirect Listing
  2428.   #--------------------------------------------------------------------------
  2429.   redirect_method :player_pos,      '($game_player.pos)'
  2430.   redirect_method :leader,          '(members.first)'
  2431.   redirect_method :members,         '(actors)'  
  2432.   redirect_method :actors,          '(members)'  
  2433.   #--------------------------------------------------------------------------
  2434.   # ● New method: existing_actors, recover_all
  2435.   #--------------------------------------------------------------------------
  2436.   define_sec_method(:existing_actors) { members.find_all {|a| a.exist?}  }
  2437.   define_sec_method(:recover_all)     { members.each {|a| a.recover_all} }
  2438.   #--------------------------------------------------------------------------
  2439.   # ● New method: has_actors?
  2440.   #--------------------------------------------------------------------------
  2441.   unless method_defined?(:has_actors?)
  2442.     def has_actors?(*actors)
  2443.       party = @actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }
  2444.       actor =  actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }.flatten
  2445.       return party.have_all?(*actor)
  2446.     end
  2447.   end
  2448.   #--------------------------------------------------------------------------
  2449.   # ● New method: has_any_actors?
  2450.   #--------------------------------------------------------------------------
  2451.   unless method_defined?(:has_any_actors?)
  2452.     def has_any_actors?(*actors)
  2453.       party = @actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }
  2454.       actor =  actors.collect {|s| s.is_a?(Game_Actor) ? s.id : s }.flatten
  2455.       return party.have_any?(*actor)
  2456.     end
  2457.   end
  2458.   #--------------------------------------------------------------------------
  2459.   # ● New method: swap_previous_actor
  2460.   #--------------------------------------------------------------------------
  2461.   unless method_defined?(:swap_leader_prev)
  2462.     def swap_leader_prev(n = 1)
  2463.       n <= 0 || n.round.times { @actors.previous_item && drg_ce_refresh }
  2464.       n >= 0 || swap_leader_next(n.round.abs)
  2465.     end
  2466.   end
  2467.   #--------------------------------------------------------------------------
  2468.   # ● New method: swap_next_actor
  2469.   #--------------------------------------------------------------------------
  2470.   unless method_defined?(:swap_leader_next)
  2471.     def swap_leader_next(n = 1)
  2472.       n <= 0 || n.round.times { @actors.next_item && drg_ce_refresh }
  2473.       n >= 0 || swap_leader_prev(n.round.abs)
  2474.     end
  2475.   end
  2476.   #--------------------------------------------------------------------------
  2477.   # ● New method: set_leader
  2478.   #--------------------------------------------------------------------------
  2479.   unless method_defined?(:set_leader)
  2480.     def set_leader(id)
  2481.       actor = id.is_a?(Numeric) ? $game_actors[id.to_i] : id
  2482.       if actor.is_a?(Game_Actor) && has_actors?(actor.id)
  2483.         swap_leader_next until $game_party.leader.id == actor.id
  2484.         actor.id
  2485.       end
  2486.     end
  2487.   end
  2488.   #--------------------------------------------------------------------------
  2489.   # ● New method: drg_ce_refresh
  2490.   #--------------------------------------------------------------------------
  2491.   unless method_defined?(:drg_ce_refresh)
  2492.     def drg_ce_refresh
  2493.       return refresh if respond_to?(:refresh)
  2494.       return ($game_map.need_refresh = true) && $game_player.refresh
  2495.     end
  2496.   end
  2497.   #--------------------------------------------------------------------------
  2498.   # * Get Party Name
  2499.   #--------------------------------------------------------------------------
  2500.   unless method_defined?(:name)
  2501.     def name
  2502.       return ""           if existing_actors.size == 0
  2503.       return leader.name  if existing_actors.size == 1
  2504.       return "#{leader.name}'s Party"
  2505.     end
  2506.   end
  2507.   #--------------------------------------------------------------------------
  2508.   # * Aliased method: item_number
  2509.   #--------------------------------------------------------------------------
  2510.   def item_number(item)
  2511.     unless LiTTleDRAgo::VX
  2512.       return drg_ce_item_number(item.id)          if item.is_a?(RPG::Item)
  2513.       return weapon_number(item.id)               if item.is_a?(RPG::Weapon)
  2514.       return armor_number(item.id)                if item.is_a?(RPG::Armor)  
  2515.     else
  2516.       item = $data_items[item.to_i]               if item.is_a?(Numeric)
  2517.     end
  2518.     return skill_number(item)                     if item.is_a?(RPG::Skill)
  2519.     return drg_ce_item_number(item)
  2520.   end
  2521.   #--------------------------------------------------------------------------
  2522.   # * Redefined method: weapon_number
  2523.   #--------------------------------------------------------------------------
  2524.   unless method_defined?(:weapon_number)
  2525.     def weapon_number(weapon)
  2526.       weapon = $data_weapons[weapon.to_i] if weapon.is_a?(Numeric)
  2527.       item_number(weapon)
  2528.     end
  2529.   end
  2530.   #--------------------------------------------------------------------------
  2531.   # * Redefined method: armor_number
  2532.   #--------------------------------------------------------------------------
  2533.   unless method_defined?(:armor_number)
  2534.     def armor_number(armor)
  2535.       armor = $data_armors[armor.to_i] if armor.is_a?(Numeric)
  2536.       item_number(armor)
  2537.     end
  2538.   end
  2539.   #--------------------------------------------------------------------------
  2540.   # * New method: skill_number
  2541.   #--------------------------------------------------------------------------
  2542.   unless method_defined?(:skill_number)
  2543.     def skill_number(skill)
  2544.       skill = $data_skills[skill.to_i] if skill.is_a?(Numeric)
  2545.       s1 = actors.select {|a| a.skill_learn?(skill)    }
  2546.       s2 = actors.select {|a| a.skill_learn?(skill.id) }
  2547.       return s1.size + s2.size
  2548.     end
  2549.   end
  2550.   #--------------------------------------------------------------------------
  2551.   # * New method: gain_all_items
  2552.   #--------------------------------------------------------------------------
  2553.   def gain_all_items(ammount=1)
  2554.     (0..999).to_a.each do |s|
  2555.       next if $data_items[s].nil? || $data_items[s].name == ''
  2556.       $game_party.drg_ce_get_item($data_items[s],ammount)
  2557.     end
  2558.   end
  2559.   #--------------------------------------------------------------------------
  2560.   # * New method: gain_all_weapons
  2561.   #--------------------------------------------------------------------------
  2562.   def gain_all_weapons(ammount=1)
  2563.     (0..999).to_a.each do |s|
  2564.       next if $data_weapons[s].nil? || $data_weapons[s].name == ''
  2565.       drg_ce_get_item($data_weapons[s],ammount)
  2566.     end
  2567.   end  
  2568.   #--------------------------------------------------------------------------
  2569.   # * New method: gain_all_armors
  2570.   #--------------------------------------------------------------------------
  2571.   def gain_all_armors(ammount=1)
  2572.     (0..999).to_a.each do |s|
  2573.       next if $data_armors[s].nil? || $data_armors[s].name == ''
  2574.       drg_ce_get_item($data_armors[s],ammount)
  2575.     end
  2576.   end
  2577.   #--------------------------------------------------------------------------
  2578.   # * New method: drg_ce_get_item
  2579.   #--------------------------------------------------------------------------
  2580.   def drg_ce_get_item(item,ammount)
  2581.     return gain_item(item,ammount)      if LiTTleDRAgo::VX
  2582.     return gain_item(item.id,ammount)   if item.is_a?(RPG::Item)
  2583.     return gain_weapon(item.id,ammount) if item.is_a?(RPG::Weapon)
  2584.     return gain_armor(item.id,ammount)  if item.is_a?(RPG::Armor)
  2585.   end
  2586. end
  2587.  
  2588. #==============================================================================
  2589. # ** Game_Follower
  2590. #------------------------------------------------------------------------------
  2591. #  This class handles followers. A follower is an allied character, other than
  2592. # the front character, displayed in the party. It is referenced within the
  2593. # Game_Followers class.
  2594. #==============================================================================
  2595. class Game_Follower < Game_Character
  2596.   #--------------------------------------------------------------------------
  2597.   # * Redirect Listing
  2598.   #--------------------------------------------------------------------------
  2599.   redirect_method :name, "(actor.nil? ? '' : actor.name)"
  2600. end if defined?(Game_Follower)
  2601.  
  2602. #==============================================================================
  2603. # ** Game_Map
  2604. #------------------------------------------------------------------------------
  2605. #  This class handles maps. It includes scrolling and passage determination
  2606. # functions. The instance of this class is referenced by $game_map.
  2607. #==============================================================================
  2608.  
  2609. class Game_Map
  2610.   #--------------------------------------------------------------------------
  2611.   # * Public Instance Variables
  2612.   #--------------------------------------------------------------------------
  2613.   attr_sec_reader :eliminate_event, 'Hash.new'
  2614.   #--------------------------------------------------------------------------
  2615.   # ● New method: name,        parent_id, base_parent_id
  2616.   #               event_list , map_events
  2617.   #--------------------------------------------------------------------------
  2618.   define_sec_method(:name)             { mapInfo[@map_id].name      }
  2619.   define_sec_method(:parent_id)        { mapInfo[@map_id].parent_id }
  2620.   define_sec_method(:base_parent_id)   { search_parent_id(mapInfo)  }
  2621.   define_sec_method(:event_list)       { events.values }
  2622.   define_sec_method(:map_events)       { @map.events   }
  2623.   define_sec_method(:screen)           { $game_screen  }
  2624.   define_sec_method(:loop_horizontal?) { false }
  2625.   define_sec_method(:loop_vertical?)   { false }
  2626.   define_sec_method(:map_child?)       { |*a|  a & child_ids == a }
  2627.   #--------------------------------------------------------------------------
  2628.   # * Aliased method: setup
  2629.   #--------------------------------------------------------------------------
  2630.   define_post_alias(:setup) do
  2631.     eliminate_event[@map_id] ||= []
  2632.     eliminate_event[@map_id].each { |s| events.delete(s) }
  2633.   end
  2634.   #--------------------------------------------------------------------------
  2635.   # * Redefined method: any_event_strarting? (for RMXP)
  2636.   #--------------------------------------------------------------------------
  2637.   unless method_defined?(:any_event_starting?)
  2638.     def any_event_starting?
  2639.       event_list.any? {|event| event.starting }
  2640.     end
  2641.   end
  2642.   #--------------------------------------------------------------------------
  2643.   # * Redefined method: interpreter (for RMXP)
  2644.   #--------------------------------------------------------------------------
  2645.   unless method_defined?(:interpreter)
  2646.     def interpreter
  2647.       return $game_system.map_interpreter if LiTTleDRAgo.scene.is_a?(Scene_Map)
  2648.       return $game_system.battle_interpreter
  2649.     end
  2650.   end
  2651.   #--------------------------------------------------------------------------
  2652.   # * Redefined method: events_xy (for RMXP)
  2653.   #--------------------------------------------------------------------------
  2654.   unless method_defined?(:events_xy)
  2655.     def events_xy(x, y)
  2656.       event_list.select {|event| event.x == x && event.y == y }
  2657.     end
  2658.   end
  2659.   #--------------------------------------------------------------------------
  2660.   # * Redefined method: events_xy_nt (for RMXP)
  2661.   #--------------------------------------------------------------------------
  2662.   unless method_defined?(:events_xy_nt)
  2663.     def events_xy_nt(x, y)
  2664.       event_list.select {|event| event.pos_nt?(x, y) }
  2665.     end
  2666.   end
  2667.   #--------------------------------------------------------------------------
  2668.   # * Redefined method: round_x, round_y (for RMXP)
  2669.   #--------------------------------------------------------------------------
  2670.   define_sec_method(:round_x) {|x| loop_horizontal? ? (x + width) % width : x }
  2671.   define_sec_method(:round_y) {|y| loop_vertical? ? (y + height) % height : y }
  2672.   #--------------------------------------------------------------------------
  2673.   # * Redefined method: x_with_direction (for RMXP)
  2674.   #--------------------------------------------------------------------------
  2675.   define_sec_method(:x_with_direction) {|x, d| x + (d==6 ? 1 : d==4 ? -1 : 0)}
  2676.   define_sec_method(:y_with_direction) {|y, d| y + (d==2 ? 1 : d==8 ? -1 : 0)}
  2677.   #--------------------------------------------------------------------------
  2678.   # * Redefined method: round_x_with_direction (for RMXP)
  2679.   #--------------------------------------------------------------------------
  2680.   unless method_defined?(:round_x_with_direction)
  2681.     def round_x_with_direction(x, d)
  2682.       round_x(x_with_direction(x, d))
  2683.     end
  2684.   end
  2685.   #--------------------------------------------------------------------------
  2686.   # * Redefined method: round_y_with_direction (for RMXP)
  2687.   #--------------------------------------------------------------------------
  2688.   unless method_defined?(:round_y_with_direction)
  2689.     def round_y_with_direction(y, d)
  2690.       round_y(y_with_direction(y, d))
  2691.     end
  2692.   end
  2693.   #--------------------------------------------------------------------------
  2694.   # * New method: x_diagonal_with_direction (for RMXP)
  2695.   #--------------------------------------------------------------------------
  2696.   unless method_defined?(:x_diagonal_with_direction)
  2697.     def x_diagonal_with_direction(x, d)
  2698.       x + ([9,6,3].include?(d) ? 1 : [7,4,1].include?(d) ? -1 : 0)
  2699.     end
  2700.   end
  2701.   #--------------------------------------------------------------------------
  2702.   # * New method: y_with_direction (for RMXP)
  2703.   #--------------------------------------------------------------------------
  2704.   unless method_defined?(:y_diagonal_with_direction)
  2705.     def y_diagonal_with_direction(y, d)
  2706.       y + ([1,2,3].include?(d) ? 1 : [7,8,9].include?(d) ? -1 : 0)
  2707.     end
  2708.   end
  2709.   #--------------------------------------------------------------------------
  2710.   # ● New method: existing_events
  2711.   #--------------------------------------------------------------------------
  2712.   unless method_defined?(:existing_events)
  2713.     def existing_events
  2714.       event_list.select {|e| e.not.erased }
  2715.     end
  2716.   end
  2717.   #--------------------------------------------------------------------------
  2718.   # ● New method: map_info
  2719.   #--------------------------------------------------------------------------
  2720.   unless method_defined?(:mapInfo)
  2721.     def mapInfo(ext = LiTTleDRAgo.data_default_extension)
  2722.       data = "Data/MapInfos.#{ext}"  
  2723.       @mapinfo ||= load_data(sprintf("#{data}")) rescue draise(
  2724.                    "Fail to load #{data}")
  2725.       return @mapinfo
  2726.     end
  2727.   end
  2728.   #--------------------------------------------------------------------------
  2729.   # ● New method: search_parent_id
  2730.   #--------------------------------------------------------------------------
  2731.   unless method_defined?(:search_parent_id)
  2732.     def search_parent_id(mapInfo, id = @map_id, ancestor = 0)
  2733.       m_id = id
  2734.       loop do
  2735.         _m_id = mapInfo[m_id]
  2736.         p_id = _m_id.respond_to?(:parent_id) ? _m_id.parent_id : nil
  2737.         return p_id if p_id == nil
  2738.         return m_id if p_id == ancestor
  2739.         m_id = p_id
  2740.       end
  2741.     end
  2742.   end
  2743.   #--------------------------------------------------------------------------
  2744.   # ● New method: base_map_name
  2745.   #--------------------------------------------------------------------------
  2746.   unless method_defined?(:base_map_name)
  2747.     def base_map_name
  2748.       map = mapInfo[search_parent_id(mapInfo)]
  2749.       name = map.respond_to?(:name) ? map.name : ''
  2750.       return name
  2751.     end
  2752.   end
  2753.   #--------------------------------------------------------------------------
  2754.   # ● New method: child_ids
  2755.   #--------------------------------------------------------------------------
  2756.   unless method_defined?(:child_ids)
  2757.     def child_ids
  2758.       mapInfo.keys.select {|map_id| mapInfo[map_id].parent_id == @map_id }
  2759.     end
  2760.   end
  2761.   #--------------------------------------------------------------------------
  2762.   # ● New method: descendant?
  2763.   #--------------------------------------------------------------------------
  2764.   unless method_defined?(:map_descendant?)
  2765.     def map_descendant?(*a)
  2766.       a.all? {|map_id| search_parent_id(mapInfo, map_id, @map_id)}
  2767.     end
  2768.   end
  2769.   #--------------------------------------------------------------------------
  2770.   # ● New method: load_event
  2771.   #--------------------------------------------------------------------------
  2772.   unless method_defined?(:load_event)
  2773.     def load_event(*a)
  2774.       case a.size
  2775.       when 5, 4 then (mapid, eventid, x, y = *a)
  2776.       when 3    then (mapid = @map_id) && (eventid, x, y = *a)
  2777.       when 2    then (mapid, y = @map_id, 0) && (eventid, x = *a)
  2778.       when 1    then (mapid, x, y = @map_id, 0,0) && (eventid = a.first)
  2779.       when 0    then return nil
  2780.       end
  2781.       map = load_map_data(mapid)
  2782.       event = map.events.values.detect { |event| event.id == eventid }
  2783.       unless event.nil?
  2784.         (a = a.at(4)) && (b = [a].flatten) && event.pages.reject! do |s|
  2785.           c = b.map {|i| i.is_a?(Range) ? i.to_a : i }
  2786.           c.flatten.not.include?(event.pages.index(s)+1)
  2787.         end
  2788.         return nil if event.pages.empty?
  2789.         c,r = [Array,Range], []
  2790.         c.include?(x.class) && (x = x.to_a.map{|s|s.is_a?(Range) ? s.to_a : s})
  2791.         c.include?(y.class) && (y = y.to_a.map{|s|s.is_a?(Range) ? s.to_a : s})
  2792.         [x].flatten.dproduct([y].flatten).each do |x1,y1|
  2793.           event.instance_variable_set(:@id,(id = next_key_events))
  2794.           r.push(@events[id] = e = Game_Event.new(@map_id, event.clone))
  2795.           e.loaded_event = true
  2796.           e.moveto(x1,y1)
  2797.         end
  2798.         a = r.size == 1 ? r.first : r
  2799.         if r && (s = spriteset).is_a?(Spriteset_Map)
  2800.           sc = s.character_sprites
  2801.           after  = sc.select { |e| e if e.character != $game_player }
  2802.           player = s.sprite_player
  2803.           sc.replace(after)
  2804.           r.each {|e| sc.push(Sprite_Character.new(s.viewport1, e)) }
  2805.           sc.push(player)
  2806.         end
  2807.         return a
  2808.       end
  2809.     end
  2810.   end
  2811.   #--------------------------------------------------------------------------
  2812.   # ● New method: call_event
  2813.   #--------------------------------------------------------------------------
  2814.   unless method_defined?(:call_event)
  2815.     def call_event(*a)
  2816.       case a.size
  2817.       when 3 then (mapid, eventid, page_id = *a)
  2818.       when 2 then (page_id = -1) && (mapid, eventid = *a)
  2819.       when 1 then (mapid, page_id = @map_id, -1) && (eventid = a.first)
  2820.       when 0 then return nil
  2821.       end
  2822.       map = load_map_data(mapid)
  2823.       event = map.events.values.detect { |event| event.id == eventid }
  2824.       unless event.nil?
  2825.         if page_id == -1 && event.respond_to?(:conditions_met?)
  2826.           pages = event.pages.reverse.find {|page| event.conditions_met?(page)}
  2827.         else
  2828.           pages = event.pages[[page_id,1].max-1]
  2829.         end
  2830.         klass = LiTTleDRAgo::VX ? Game_Interpreter : Interpreter
  2831.         child = klass.new(interpreter.instance_variable_get(:@depth) + 1)
  2832.         child.setup(pages.list, interpreter.same_map? ? event.id : 0)
  2833.         if LiTTleDRAgo::VXA
  2834.           child.run rescue FiberError
  2835.         else
  2836.           interpreter.instance_variable_set(:@child_interpreter, child)
  2837.         end
  2838.       end
  2839.     end
  2840.   end
  2841.   #--------------------------------------------------------------------------
  2842.   # ● New method: load_map_data
  2843.   #--------------------------------------------------------------------------
  2844.   unless method_defined?(:load_map_data)
  2845.     def load_map_data(mapid, ext = LiTTleDRAgo.data_default_extension)
  2846.       (map = sprintf("Data/Map%03d.#{ext}", mapid)) && load_data(map)
  2847.     end
  2848.   end
  2849.   #--------------------------------------------------------------------------
  2850.   # ● New method: increment_key_events
  2851.   #--------------------------------------------------------------------------
  2852.   unless method_defined?(:next_key_events)
  2853.     def next_key_events
  2854.       (@events.keys.max || 0) + 1
  2855.     end
  2856.   end
  2857.   #--------------------------------------------------------------------------
  2858.   # ● New method: unused_key_events
  2859.   #--------------------------------------------------------------------------
  2860.   unless method_defined?(:unused_key_events)
  2861.     def unused_key_events(start = 1)
  2862.       start = [start, next_key_events].min
  2863.       (start..next_key_events).detect {|i| @events.keys.not.include?(i)}
  2864.     end
  2865.   end
  2866.   #--------------------------------------------------------------------------
  2867.   # ● New method: spriteset
  2868.   #--------------------------------------------------------------------------
  2869.   unless method_defined?(:spriteset)
  2870.     def spriteset
  2871.       LiTTleDRAgo.scene.instance_variable_get(:@spriteset)
  2872.     end
  2873.   end
  2874.   #--------------------------------------------------------------------------
  2875.   # ● New method: character_sprite
  2876.   #--------------------------------------------------------------------------
  2877.   unless method_defined?(:character_sprite)
  2878.     def character_sprite(id = 0)
  2879.       return unless spriteset.is_a?(Spriteset_Map)
  2880.       id = id.is_a?(Game_Character) ? id : id == 0 ? $game_player : events[id]
  2881.       spriteset.find_character(id)
  2882.     end
  2883.   end
  2884.   #--------------------------------------------------------------------------
  2885.   # ● New method: picture_sprite
  2886.   #--------------------------------------------------------------------------
  2887.   unless method_defined?(:picture_sprite)
  2888.     def picture_sprite(id = nil)
  2889.       return unless spriteset.is_a?(Spriteset_Map)
  2890.       spriteset.update_pictures if spriteset.respond_to?(:update_pictures)
  2891.       result = spriteset.instance_variable_get(:@picture_sprites)
  2892.       id.nil? ? result : result[id]
  2893.     end
  2894.   end
  2895. end
  2896. #==============================================================================
  2897. # ■ RPG::Cache
  2898. #------------------------------------------------------------------------------
  2899. #
  2900. #==============================================================================
  2901. ModCache = LiTTleDRAgo.cache
  2902. class << ModCache
  2903.   #-----------------------------------------------------------------------
  2904.   # ● New method: self.draw_text
  2905.   #-----------------------------------------------------------------------
  2906.   unless method_defined?(:draw_text)
  2907.     def draw_text(
  2908.             width, height, text,
  2909.             font = Font.default_name,
  2910.             size = Font.default_size,
  2911.             bold = Font.default_bold,
  2912.             italic = Font.default_italic,
  2913.             color = Font.default_color,
  2914.             edging = false
  2915.           )
  2916.       cache_index = "text_" + width.to_s + height.to_s + text
  2917.       font_text = ""
  2918.       font.size.times do |i|
  2919.         font_text += font[i].to_s
  2920.       end
  2921.       cache_index += font_text + size.to_s + bold.to_s + italic.to_s
  2922.       cache_index += "#{color.red}#{color.blue}#{color.green}#{color.alpha}"
  2923.       @cache ||= {}
  2924.       if @cache[cache_index].nil? or @cache[cache_index].disposed?
  2925.         bitmap = Bitmap.new(width, height)
  2926.         bitmap.font.name = font
  2927.         bitmap.font.size = size
  2928.         bitmap.font.bold = bold
  2929.         bitmap.font.italic = italic
  2930.         bitmap.font.color = color
  2931.         if edging
  2932.           bitmap.draw_edging_text(0, 0, width, height, text, 0)
  2933.         else
  2934.           bitmap.draw_text(0, 0, width, height, text, 0)
  2935.         end
  2936.         @cache[cache_index] = bitmap
  2937.       end
  2938.       return @cache[cache_index]
  2939.     end
  2940.   end
  2941.   #-----------------------------------------------------------------------
  2942.   # ● New method: self.method_missing
  2943.   #-----------------------------------------------------------------------
  2944.   def method_missing(val,*a,&b)
  2945.     if a.at(0).is_a?(String) && a.size <= 2
  2946.       return load_bitmap("Graphics/#{val}/",*a,&b)
  2947.     end
  2948.     modified = [:invert,:brighten,:darken,:grayscale,:pixelate,:frost]
  2949.     if modified.include?(a.first) && a.at(1).is_a?(String) && a.size <= 3
  2950.       method = a.shift
  2951.       filename = a.join + "-#{method}"
  2952.       unless drago_bitmap_exist?("Graphics/#{val}",filename)
  2953.         bitmap = load_bitmap("Graphics/#{val}/",*a,&b)
  2954.         drago_set_bitmap(bitmap.send(method),"Graphics/#{val}",filename)
  2955.       end
  2956.       return drago_get_bitmap("Graphics/#{val}",filename)
  2957.     end
  2958.     text = "Undefined method #{val} for #{self.inspect}"
  2959.     raise(NoMethodError, text, caller(1))      
  2960.   end
  2961.   #-----------------------------------------------------------------------
  2962.   # ● New method: drago_get_bitmap
  2963.   #-----------------------------------------------------------------------
  2964.   def drago_get_bitmap(folder_name, filename = '', hue = 0)
  2965.     return empty_bitmap if filename.not.is_a?(String) || filename.empty?
  2966.     path = LiTTleDRAgo::APPPATHDRAGO + "#{folder_name}/"
  2967.     path = path.gsub("\\","/").gsub("//","/")
  2968.     last = filename.split('.').last
  2969.     filename.gsub!(".png","") if last.is_a?(String) && last[/png/i]
  2970.     load_bitmap_no_rescue(path.downcase,filename.downcase,hue)
  2971.   end
  2972.   #-----------------------------------------------------------------------
  2973.   # ● New method: drago_set_bitmap
  2974.   #-----------------------------------------------------------------------
  2975.   def drago_set_bitmap(bitmap, folder_name = '', filename = 'bitmap')
  2976.     path = LiTTleDRAgo::APPPATHDRAGO + "#{folder_name}/"
  2977.     path = path.gsub("\\","/").gsub("//","/")
  2978.     last = filename.split('.').last
  2979.     filename += '.png' unless last.is_a?(String) && last[/png/i]
  2980.     Dir.make_dir(path) unless FileTest.directory?(path)
  2981.     bitmap.export(path + filename)
  2982.     drago_get_bitmap(folder_name, filename)
  2983.   end
  2984.   #-----------------------------------------------------------------------
  2985.   # ● New method: drago_bitmap_exist?
  2986.   #-----------------------------------------------------------------------
  2987.   def drago_bitmap_exist?(folder_name, filename)
  2988.     return false if filename.not.is_a?(String) || filename.empty?
  2989.     path = LiTTleDRAgo::APPPATHDRAGO + "#{folder_name}/"
  2990.     path = path.gsub("\\","/").gsub("//","/")
  2991.     last = filename.split('.').last
  2992.     filename += '.png' unless last.is_a?(String) && last[/png/i]
  2993.     FileTest.file?(path + filename)
  2994.   end
  2995.   #--------------------------------------------------------------------------
  2996.   # * Load Bitmap
  2997.   #--------------------------------------------------------------------------
  2998.   define_sec_method(:load_bitmap_no_rescue) do |folder, file, *a|
  2999.     @cache ||= {}
  3000.     if file.empty?
  3001.       empty_bitmap
  3002.     elsif (hue = a.first).is_a?(Numeric) && hue != 0
  3003.       hue_changed_bitmap(folder + file, hue.round)
  3004.     else
  3005.       normal_bitmap(folder + file)
  3006.     end
  3007.   end
  3008.   #--------------------------------------------------------------------------
  3009.   # * Create Empty Bitmap
  3010.   #--------------------------------------------------------------------------
  3011.   define_sec_method(:empty_bitmap) { Bitmap.new(32, 32)}
  3012.   #--------------------------------------------------------------------------
  3013.   # * Create/Get Normal Bitmap
  3014.   #--------------------------------------------------------------------------
  3015.   define_sec_method(:normal_bitmap) do |path|
  3016.     @cache[path] = Bitmap.new(path) unless include?(path)
  3017.     @cache[path]
  3018.   end
  3019.   #--------------------------------------------------------------------------
  3020.   # * Create/Get Hue-Changed Bitmap
  3021.   #--------------------------------------------------------------------------
  3022.   define_sec_method(:hue_changed_bitmap) do |path, hue|
  3023.     key = [path, hue]
  3024.     unless include?(key)
  3025.       @cache[key] = normal_bitmap(path).clone
  3026.       @cache[key].hue_change(hue)
  3027.     end
  3028.     @cache[key]
  3029.   end
  3030.   #--------------------------------------------------------------------------
  3031.   # * Check Cache Existence
  3032.   #--------------------------------------------------------------------------
  3033.   def include?(key)
  3034.     @cache[key] && @cache[key].not.disposed?
  3035.   end
  3036. end
  3037. #==============================================================================
  3038. # ** Window_Base
  3039. #------------------------------------------------------------------------------
  3040. #  This class is for all in-game windows.
  3041. #==============================================================================
  3042.  
  3043. class Window_Base < Window
  3044.   #--------------------------------------------------------------------------
  3045.   # * Redirect Listing
  3046.   #--------------------------------------------------------------------------
  3047.   redirect_method :draw_text,    'contents.draw_text'
  3048.   redirect_method :text_size,    'contents.text_size'
  3049.   redirect_method :change_color, 'contents.change_color'
  3050.   redirect_method :draw_picture, 'contents.draw_picture'
  3051.   redirect_method :draw_icon,    'contents.draw_icon'
  3052.   redirect_method :draw_battler, 'contents.draw_battler'
  3053.   #--------------------------------------------------------------------------
  3054.   # * Redefined method: line_height, translucent_alpha
  3055.   # * New method:       show, hide, activate, deactivate
  3056.   #--------------------------------------------------------------------------
  3057.   define_sec_method(:line_height)       { 32 }
  3058.   define_sec_method(:translucent_alpha) { 160 }
  3059.   define_sec_method(:show)              { self.visible = true  }
  3060.   define_sec_method(:hide)              { self.visible = false }
  3061.   define_sec_method(:activate)          { self.active  = true  }
  3062.   define_sec_method(:deactivate)        { self.active  = false }
  3063.   #--------------------------------------------------------------------------
  3064.   # * New method: center_window
  3065.   #--------------------------------------------------------------------------
  3066.   unless method_defined?(:center_window)
  3067.     def center_window
  3068.       self.x = Graphics.width/2  - self.width/2
  3069.       self.y = Graphics.height/2 - self.height/2
  3070.     end
  3071.   end
  3072.   #--------------------------------------------------------------------------
  3073.   # * Redefined method: disabled_color
  3074.   #--------------------------------------------------------------------------
  3075.   unless method_defined?(:disabled_color)
  3076.     def disabled_color    
  3077.       color = normal_color
  3078.       color.alpha = 128
  3079.       return color
  3080.     end
  3081.   end
  3082.   #--------------------------------------------------------------------------
  3083.   # * Redefined method: draw_text_ex
  3084.   #--------------------------------------------------------------------------
  3085.   unless method_defined?(:draw_text_ex)
  3086.     def draw_text_ex(x, y, text)
  3087.       reset_font_settings
  3088.       text = convert_escape_characters(text)
  3089.       pos = {:x => x, :y => y, :new_x => x, :height => calc_line_height(text)}
  3090.       process_character(text.slice!(0, 1), text, pos) until text.empty?
  3091.     end
  3092.   end
  3093.   #--------------------------------------------------------------------------
  3094.   # * Redefined method: reset_font_settings
  3095.   #--------------------------------------------------------------------------
  3096.   unless method_defined?(:reset_font_settings)
  3097.     def reset_font_settings
  3098.       change_color(normal_color)
  3099.       contents.font.size = Font.default_size
  3100.       contents.font.bold = Font.default_bold
  3101.       contents.font.italic = Font.default_italic
  3102.     end
  3103.   end
  3104.   #--------------------------------------------------------------------------
  3105.   # * Redefined method: convert_escape_characters
  3106.   #--------------------------------------------------------------------------
  3107.   unless method_defined?(:convert_escape_characters)
  3108.     def convert_escape_characters(text)
  3109.       result = text.to_s.clone
  3110.       result.gsub!(/\\/)            { "\e" }
  3111.       result.gsub!(/\e\e/)          { "\\" }
  3112.       result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i]   }
  3113.       result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i)        }
  3114.       result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  3115.       result.gsub!(/\eG/i)          { currency_unit }
  3116.       result
  3117.     end
  3118.   end
  3119.   #--------------------------------------------------------------------------
  3120.   # * Redefined method: actor_name
  3121.   #--------------------------------------------------------------------------
  3122.   unless method_defined?(:actor_name)
  3123.     def actor_name(n)
  3124.       actor = n >= 1 ? $game_actors[n] : nil
  3125.       actor ? actor.name : ""
  3126.     end
  3127.   end
  3128.   #--------------------------------------------------------------------------
  3129.   # * Redefined method: party_member_name
  3130.   #--------------------------------------------------------------------------
  3131.   unless method_defined?(:party_member_name)
  3132.     def party_member_name(n)
  3133.       actor = n >= 1 ? $game_party.instance_variable_get(:@actors)[n - 1] : nil
  3134.       actor ? actor.name : ""
  3135.     end
  3136.   end
  3137.   #--------------------------------------------------------------------------
  3138.   # * Redefined method: process_character
  3139.   #--------------------------------------------------------------------------
  3140.   unless method_defined?(:process_character)
  3141.     def process_character(c, text, pos)
  3142.       case c
  3143.       when "\n"   # New line
  3144.         process_new_line(text, pos)
  3145.       when "\f"   # New page
  3146.         process_new_page(text, pos)
  3147.       when "\e"   # Control character
  3148.         process_escape_character(obtain_escape_code(text), text, pos)
  3149.       else        # Normal character
  3150.         process_normal_character(c, pos)
  3151.       end
  3152.     end
  3153.   end
  3154.   #--------------------------------------------------------------------------
  3155.   # * Redefined method: process_normal_character
  3156.   #--------------------------------------------------------------------------
  3157.   unless method_defined?(:process_normal_character)
  3158.     def process_normal_character(c, pos)
  3159.       text_width = text_size(c).width
  3160.       draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
  3161.       pos[:x] += text_width
  3162.     end
  3163.   end
  3164.   #--------------------------------------------------------------------------
  3165.   # * Redefined method: process_new_line
  3166.   #--------------------------------------------------------------------------
  3167.   unless method_defined?(:process_new_line)
  3168.     def process_new_line(text, pos)
  3169.       pos[:x] = pos[:new_x]
  3170.       pos[:y] += pos[:height]
  3171.       pos[:height] = calc_line_height(text)
  3172.     end
  3173.   end
  3174.   #--------------------------------------------------------------------------
  3175.   # * Redefined method: process_new_page
  3176.   #--------------------------------------------------------------------------
  3177.   unless method_defined?(:process_new_page)
  3178.     def process_new_page(text, pos)
  3179.     end
  3180.   end
  3181.   #--------------------------------------------------------------------------
  3182.   # * Redefined method: obtain_escape_code
  3183.   #--------------------------------------------------------------------------
  3184.   unless method_defined?(:obtain_escape_code)
  3185.     def obtain_escape_code(text)
  3186.       text.slice!(/^[\$\.\|\^!><\{\}\\]|^[\/\w+\/]+/i)
  3187.     end
  3188.   end
  3189.   #--------------------------------------------------------------------------
  3190.   # * Redefined method: obtain_escape_param
  3191.   #--------------------------------------------------------------------------
  3192.   unless method_defined?(:obtain_escape_param)
  3193.     def obtain_escape_param(text)
  3194.       text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
  3195.     end
  3196.   end
  3197.   #--------------------------------------------------------------------------
  3198.   # * Redefined method: process_escape_character
  3199.   #--------------------------------------------------------------------------
  3200.   unless method_defined?(:process_escape_character)
  3201.     def process_escape_character(code, text, pos)
  3202.       case code.upcase
  3203.       when 'C' then change_color(text_color(obtain_escape_param(text)))
  3204.       when 'I' then process_draw_icon(obtain_escape_code(text), pos)
  3205.       when '{' then make_font_bigger
  3206.       when '}' then make_font_smaller
  3207.       end
  3208.     end
  3209.   end
  3210.   #--------------------------------------------------------------------------
  3211.   # * Redefined method: process_draw_icon
  3212.   #--------------------------------------------------------------------------
  3213.   unless method_defined?(:process_draw_icon)
  3214.     def process_draw_icon(icon_index, pos)
  3215.       icon_index = icon_index.to_i if icon_index.to_i.to_s == icon_index
  3216.       draw_icon(icon_index, pos[:x], pos[:y])
  3217.       pos[:x] += 24
  3218.     end
  3219.   end
  3220.   #--------------------------------------------------------------------------
  3221.   # * Redefined method: make_font_bigger
  3222.   #--------------------------------------------------------------------------
  3223.   unless method_defined?(:make_font_bigger)
  3224.     def make_font_bigger
  3225.       contents.font.size += 8 if contents.font.size <= 64
  3226.     end
  3227.   end
  3228.   #--------------------------------------------------------------------------
  3229.   # * Redefined method: make_font_smaller
  3230.   #--------------------------------------------------------------------------
  3231.   unless method_defined?(:make_font_smaller)
  3232.     def make_font_smaller
  3233.       contents.font.size -= 8 if contents.font.size >= 16
  3234.     end
  3235.   end
  3236.   #--------------------------------------------------------------------------
  3237.   # * Redefined method: calc_line_height
  3238.   #--------------------------------------------------------------------------
  3239.   unless method_defined?(:calc_line_height)
  3240.     def calc_line_height(text, restore_font_size = true)
  3241.       result = [line_height, contents.font.size].max
  3242.       last_font_size = contents.font.size
  3243.       text.slice(/^.*$/).scan(/\e[\{\}]/).each do |esc|
  3244.         make_font_bigger  if esc == "\e{"
  3245.         make_font_smaller if esc == "\e}"
  3246.         result = [result, contents.font.size].max
  3247.       end
  3248.       contents.font.size = last_font_size if restore_font_size
  3249.       result
  3250.     end
  3251.   end
  3252.   #--------------------------------------------------------------------------
  3253.   # * New method: currency_unit
  3254.   #--------------------------------------------------------------------------
  3255.   unless method_defined?(:currency_unit)
  3256.     def currency_unit
  3257.       gold = $data_system.words.gold
  3258.       return gold
  3259.     end
  3260.   end
  3261.   #--------------------------------------------------------------------------
  3262.   # ● New method: draw_map_name
  3263.   #--------------------------------------------------------------------------
  3264.   unless method_defined?(:draw_map_name)
  3265.     def draw_map_name(x, y, width, height)
  3266.       map_id  = $game_map.map_id
  3267.       mapInfo = $game_map.mapInfo
  3268.       draw_text(x, y, width, height, mapInfo[map_id].name.to_s)
  3269.     end
  3270.   end
  3271.   #--------------------------------------------------------------------------
  3272.   # ● New method: draw_enter_text
  3273.   #--------------------------------------------------------------------------
  3274.   unless method_defined?(:draw_enter_text)
  3275.     def draw_enter_text(x, y, width, height, text)
  3276.       info_box = text.split(/\n/)
  3277.       info_box.size.times do |i|
  3278.         draw_text( x, y + i * line_height, width, line_height, info_box[i])
  3279.         break if (y + i * line_height) > (self.height - line_height)
  3280.       end
  3281.     end
  3282.   end
  3283.   #--------------------------------------------------------------------------
  3284.   # ● New method: draw_picture_number
  3285.   #--------------------------------------------------------------------------
  3286.   unless method_defined?(:draw_picture_number)
  3287.     def draw_picture_number(x, y, value, file_name,align = 0,
  3288.                   space = 0, frame_max = 1,frame_index = 0)    
  3289.       number_image = LiTTleDRAgo.cache.windowskin(file_name)
  3290.       frame_max = [frame_max, 1].max
  3291.       frame_index = frame_max - 1 if frame_index > frame_max - 1
  3292.       align = [[align,2].min,0].max
  3293.       cw = number_image.width / 10
  3294.       ch = number_image.height / frame_max
  3295.       h = ch * frame_index
  3296.       number = value.abs.to_s.split(//)
  3297.       case align
  3298.       when 0 then plus_x = (-cw + space) * number.size
  3299.       when 1 then plus_x = ((-cw + space) * number.size) / 2
  3300.       when 2 then plus_x = 0
  3301.       end
  3302.       for r in 0..number.size - 1      
  3303.         rect = Rect.new(cw * number[r].to_i, h, cw, ch)
  3304.         contents.blt(plus_x + x + ((cw - space) * r), y , number_image, rect)
  3305.       end    
  3306.       number_image.dispose
  3307.     end    
  3308.   end
  3309. end
  3310.  
  3311. #==============================================================================
  3312. # ■ Bitmap
  3313. #------------------------------------------------------------------------------
  3314. #
  3315. #==============================================================================
  3316. class Bitmap  
  3317.   #----------------------------------------------------------------------------
  3318.   # ● Constant
  3319.   #----------------------------------------------------------------------------
  3320.   RtlMoveMemory_pi = LiTTleDRAgo.rtlmemory_pi
  3321.   RtlMoveMemory_ip = LiTTleDRAgo.rtlmemory_ip
  3322.   Blur_Setting     = "{ 'offset' => 2, 'spacing' => 1, 'opacity' => 255 }"
  3323.   #----------------------------------------------------------------------------
  3324.   # ● Public Instance Variables
  3325.   #----------------------------------------------------------------------------
  3326.   attr_sec_accessor :blur_settings,  Blur_Setting
  3327.   #----------------------------------------------------------------------------
  3328.   # ● Redirect Listing
  3329.   #----------------------------------------------------------------------------
  3330.   redirect_method :change_hue,          :hue_change
  3331.   #-------------------------------------------------------------------------
  3332.   # ● Define Secondary Listing
  3333.   #-------------------------------------------------------------------------
  3334.   define_sec_method(:full_fill) { |color|  fill_rect(rect, color) }
  3335.   #----------------------------------------------------------------------------
  3336.   # ● Alias Listing
  3337.   #----------------------------------------------------------------------------
  3338.   alias_sec_method :invert!,            :reversing!  
  3339.   alias_sec_method :grayscale!,         :gray_scale!
  3340.   alias_sec_method :frost,              :diffusion  
  3341.   alias_sec_method :frost!,             :diffusion!  
  3342.   alias_sec_method :drg_ce_stretch_blt, :stretch_blt
  3343.   alias_method     :drg_bitmap_load,    :initialize
  3344.   #--------------------------------------------------------------------------
  3345.   # * Object Initialization
  3346.   #----------------------------------------------------------------------------
  3347.   def initialize(*args)
  3348.     if (a = args.flatten).size == 4 && a.all? {|s| s.is_a?(Table)}
  3349.       bitmap = BitmapDump.read_bitmap_data(*a)
  3350.       drg_bitmap_load(bitmap.width,bitmap.height)
  3351.       return blt(0,0,bitmap,rect) && bitmap.dispose
  3352.     end
  3353.     drg_bitmap_load(*args)
  3354.   end
  3355.   #----------------------------------------------------------------------------
  3356.   # ● New method: address
  3357.   #----------------------------------------------------------------------------
  3358.   unless method_defined?(:address)
  3359.     def address
  3360.       @address ||= (  RtlMoveMemory_pi.call(a="\0"*4, __id__*2+16, 4)
  3361.                       RtlMoveMemory_pi.call(a, a.unpack('L')[0]+8, 4)
  3362.                       RtlMoveMemory_pi.call(a, a.unpack('L')[0]+16, 4)
  3363.                       a.unpack('L')[0]    )
  3364.     end
  3365.   end
  3366.   #----------------------------------------------------------------------------
  3367.   # ● New method: export
  3368.   #----------------------------------------------------------------------------
  3369.   unless method_defined?(:export)
  3370.     def export(filename)
  3371.       file = File.open(filename, 'wb')
  3372.         size, vxace = width * height * 4, LiTTleDRAgo::RGSS3
  3373.         case format = File.extname(filename)
  3374.         when '.bmp'
  3375.           data = String.new
  3376.           RtlMoveMemory_pi.call(empty_rString_struct="\0"*16,data.__id__*2,16)
  3377.           flags = vxace ? 5267461 : 8199
  3378.           klass = empty_rString_struct.unpack('@4L')[0]
  3379.           pack  = [flags,klass,size,address].pack('L4')
  3380.           weiss = [size+54,0,54,40,width,height,1,32,0,size,0,0,0,0]
  3381.           RtlMoveMemory_ip.call(data.__id__*2, pack, 16)
  3382.           file.write("BM#{weiss.pack('L6S2L6')}")
  3383.           file.write(data)
  3384.           RtlMoveMemory_ip.call(data.__id__*2, empty_rString_struct, 16)
  3385.         when '.png'
  3386.           write_chunk = Proc.new do |chunk|
  3387.             file.write([(vxace ? chunk.bytesize : chunk.size)-4].pack('N'))
  3388.             file.write(chunk)
  3389.             file.write([Zlib.crc32(chunk)].pack('N'))
  3390.           end
  3391.           null_char, w4, i, byte = "\0", width*4, 0, (t_Fx = 0)
  3392.           RtlMoveMemory_pi.call(data=null_char*size, address, size)
  3393.           if vxace
  3394.             data.force_encoding('ASCII-8BIT')
  3395.             (0).step(size-4, 4) do |i|
  3396.               Graphics.update if (t_Fx += 1) % 10000 == 0
  3397.               byte = data.getbyte(i)
  3398.               data.setbyte(i, data.getbyte(i+=2))
  3399.               data.setbyte(i, byte)
  3400.             end
  3401.           else
  3402.             (0).step(size-4, 4) do |i|
  3403.               Graphics.update if (t_Fx += 1) % 10000 == 0
  3404.               data[i,3] = data[i,3].reverse!
  3405.             end
  3406.           end
  3407.           deflate = Zlib::Deflate.new(9)
  3408.             (size-w4).step(0, -w4) {|i| deflate << null_char << data[i,w4]}
  3409.             file.write("\211PNG\r\n\32\n")
  3410.             write_chunk.call("IHDR#{[width,height,8,6,0,0,0].pack('N2C5')}")
  3411.             write_chunk.call("IDAT#{deflate.finish}")
  3412.             write_chunk.call('IEND')
  3413.           deflate.close
  3414.         when ''
  3415.           script_report("Error: Export Bitmap Failed\n"+
  3416.               "Extension '#{filename}' is missing.")
  3417.         else          
  3418.           script_report("Error: Export Bitmap Failed\n"+
  3419.                "Extension '#{format}' is not supported.")
  3420.         end
  3421.       file.close
  3422.     end
  3423.   end
  3424.   #----------------------------------------------------------------------------
  3425.   # ● New method: blt_cache
  3426.   #----------------------------------------------------------------------------
  3427.   unless method_defined?(:blt_cache)
  3428.     def blt_cache(x, y, width, height, text, align, edging)
  3429.       cache_bitmap = LiTTleDRAgo.cache.draw_text(
  3430.           width, height, text, self.font.name, self.font.size,
  3431.           self.font.bold, self.font.italic, self.font.color, edging
  3432.       )
  3433.       x_plus = uncache_align_exe(width, text, align)
  3434.       blt(x + x_plus , y , cache_bitmap, Rect.new(0, 0, width, height))
  3435.     end
  3436.   end
  3437.   #----------------------------------------------------------------------------
  3438.   # ● New method: uncache_align_exe
  3439.   #----------------------------------------------------------------------------
  3440.   unless method_defined?(:uncache_align_exe)
  3441.     def uncache_align_exe(width, text, align)
  3442.       recter = self.text_size(text)
  3443.       case align
  3444.       when 0 then x_index = 0
  3445.       when 1 then x_index = (width / 2) - recter.width / 2
  3446.       when 2 then x_index = width - recter.width
  3447.       else
  3448.         raise "Text Cache : align is wrong"
  3449.       end
  3450.       return x_index
  3451.     end
  3452.   end
  3453.   #----------------------------------------------------------------------------
  3454.   # ● New method: draw_cache_text
  3455.   #----------------------------------------------------------------------------
  3456.   unless method_defined?(:draw_cache_text)
  3457.     def draw_cache_text(x, y, width, height, text, align = 0)
  3458.       blt_cache(x, y, width, height, text, align, false)
  3459.     end
  3460.   end
  3461.   #----------------------------------------------------------------------------
  3462.   # ● New method: draw_edging_cache_text
  3463.   #----------------------------------------------------------------------------
  3464.   unless method_defined?(:draw_edging_cache_text)
  3465.     def draw_edging_cache_text(x, y, width, height, text, align = 0)
  3466.       blt_cache(x, y, width, height, text, align, true)
  3467.     end
  3468.   end
  3469.   #----------------------------------------------------------------------------
  3470.   # ● New method: draw_easy_cache_text
  3471.   #----------------------------------------------------------------------------
  3472.   unless method_defined?(:draw_easy_cache_text)
  3473.     def draw_easy_cache_text(x, y, text)
  3474.       t_rect = text_size(text)
  3475.       blt_cache(x, y, t_rect.width, t_rect.height, text, 0, false)
  3476.     end
  3477.   end
  3478.   #----------------------------------------------------------------------------
  3479.   # ● New method: draw_easy_edging_cache_text
  3480.   #----------------------------------------------------------------------------
  3481.   unless method_defined?(:draw_easy_edging_cache_text)
  3482.     def draw_easy_edging_cache_text(x, y, text)
  3483.       t_rect = text_size(text)
  3484.       blt_cache(x, y, t_rect.width, t_rect.height, text, 0, true)
  3485.     end
  3486.   end
  3487.   #--------------------------------------------------------------------------
  3488.   # * New method: change_color
  3489.   #--------------------------------------------------------------------------
  3490.   unless method_defined?(:change_color)
  3491.     def change_color(color, enabled = true)
  3492.       font.color = color
  3493.       font.color.alpha = translucent_alpha unless enabled
  3494.     end
  3495.   end
  3496.   #--------------------------------------------------------------------------
  3497.   # ● New method: draw_picture
  3498.   #--------------------------------------------------------------------------
  3499.   unless method_defined?(:draw_picture)
  3500.     def draw_picture(file_name, x, y, opacity = 255)
  3501.       bitmap = LiTTleDRAgo.cache.picture(file_name)
  3502.       cw = bitmap.width
  3503.       ch = bitmap.height
  3504.       src_rect = Rect.new(0, 0, cw, ch)
  3505.       blt(x, y, bitmap, src_rect, opacity)
  3506.     end
  3507.   end
  3508.   #--------------------------------------------------------------------------
  3509.   # ● New method: draw_icon
  3510.   #--------------------------------------------------------------------------
  3511.   unless method_defined?(:draw_icon)
  3512.     def draw_icon(file_name, x, y, enabled = true)
  3513.       if file_name.is_a?(Numeric)
  3514.         bitmap = LiTTleDRAgo.cache.system("Iconset")
  3515.         src_rect = Rect.new(file_name % 16 * 24, file_name / 16 * 24, 24, 24)
  3516.         blt(x, y, bitmap, src_rect, enabled ? 255 : 160)
  3517.       elsif file_name.is_a?(String)
  3518.         bitmap = LiTTleDRAgo.cache.icon(file_name)
  3519.         src_rect = Rect.new(0, 0, 24, 24)
  3520.         blt(x, y, bitmap, bitmap.rect, enabled ? 255 : 160)
  3521.       end
  3522.     end
  3523.   end
  3524.   #--------------------------------------------------------------------------
  3525.   # ● New method: draw_battler
  3526.   #--------------------------------------------------------------------------
  3527.   unless method_defined?(:draw_battler)
  3528.     def draw_battler(file_name, x, y, hue = 0, enabled = true)
  3529.       bitmap = LiTTleDRAgo.cache.battler(file_name, hue)
  3530.       cw = bitmap.width
  3531.       ch = bitmap.height
  3532.       src_rect = Rect.new(0, 0, cw, ch)
  3533.       blt(x, y, bitmap, src_rect, enabled ? 255 : 160)
  3534.     end
  3535.   end
  3536.   #--------------------------------------------------------------------------
  3537.   # ● New method: draw_bar
  3538.   #--------------------------------------------------------------------------
  3539.   unless method_defined?(:draw_bar)
  3540.     def draw_bar(x, y, current, file)
  3541.       bitmap = LiTTleDRAgo.cache.picture(file)
  3542.       cw = bitmap.width * current / 100
  3543.       ch = bitmap.height
  3544.       src_rect = Rect.new(0, 0, cw, ch)
  3545.       blt(x, y, bitmap, src_rect)
  3546.     end
  3547.   end
  3548.   #--------------------------------------------------------------------------
  3549.   # ● New method: draw_edging_text
  3550.   #--------------------------------------------------------------------------
  3551.   unless method_defined?(:draw_edging_text)
  3552.     def draw_edging_text(x, y, width, height, text, align=0)
  3553.       old_color = font.color.dup
  3554.       font.color = Color.new(0, 0, 0)
  3555.       draw_text(x-1, y-1, width, height, text, align)
  3556.       draw_text(x-1, y+1, width, height, text, align)
  3557.       draw_text(x+1, y-1, width, height, text, align)
  3558.       draw_text(x+1, y+1, width, height, text, align)
  3559.       font.color = old_color
  3560.       draw_text(x, y, width, height, text, align)
  3561.     end
  3562.   end
  3563.   #--------------------------------------------------------------------------
  3564.   # ● New method: draw_enter_text
  3565.   #--------------------------------------------------------------------------
  3566.   unless method_defined?(:draw_enter_text)
  3567.     def draw_enter_text(x, y, width, height, text, align=0)
  3568.       rect = text_size(text)
  3569.       info_box = text.split(/\n/)
  3570.       info_box.size.times do |i|
  3571.         draw_text( x, y+i*rect.height, width, height, info_box[i], align)
  3572.         break if (y+i*rect.height) > self.height
  3573.       end
  3574.     end
  3575.   end
  3576.   #--------------------------------------------------------------------------
  3577.   # ● New method: draw_enter_edging_text
  3578.   #--------------------------------------------------------------------------
  3579.   unless method_defined?(:draw_enter_edging_text)
  3580.     def draw_enter_edging_text(x, y, width, height, text, align=0)
  3581.       rect = text_size(text)
  3582.       info_box = text.split(/\n/)
  3583.       info_box.size.times do |i|
  3584.         draw_edging_text( x, y+i*rect.height,width,height,info_box[i],align)
  3585.         break if (y+i*rect.height) > self.height
  3586.       end
  3587.     end
  3588.   end
  3589.   #--------------------------------------------------------------------------
  3590.   # ● New method: draw_easy_text
  3591.   #--------------------------------------------------------------------------
  3592.   unless method_defined?(:draw_easy_text)
  3593.     def draw_easy_text(x, y, text)
  3594.       rect = text_size(text)
  3595.       draw_text(x, y, rect.width + rect.height/2, rect.height, text)
  3596.     end
  3597.   end
  3598.   #--------------------------------------------------------------------------
  3599.   # ● New method: draw_auto_text
  3600.   #--------------------------------------------------------------------------
  3601.   unless method_defined?(:draw_auto_text)
  3602.     def draw_auto_text(x, y, width, height, text)
  3603.       ini_x = 0
  3604.       law = 0
  3605.       text_box = []
  3606.       text_box[law] = []
  3607.       otxto = text.split(//)
  3608.       otxto.size.times do |i|
  3609.         text_rect = text_size(otxto[i])
  3610.         if (x + ini_x + text_rect.width) < width
  3611.           ini_x += text_rect.width
  3612.         else
  3613.           law += 1
  3614.           ini_x = text_rect.width
  3615.           text_box[law] = []
  3616.         end
  3617.         text_box[law].push(otxto[i])
  3618.       end
  3619.       for l in 0..law
  3620.         ini_x = 0
  3621.         text_box[l].size.times do |i|
  3622.           rect = text_size(text_box[l][i])
  3623.           draw_text(x + ini_x, y+l*height, rect.width, height, text_box[l][i])
  3624.           ini_x += rect.width
  3625.         end
  3626.       end
  3627.     end
  3628.   end
  3629.   #-------------------------------------------------------------------------
  3630.   # ● New method: erase
  3631.   #-------------------------------------------------------------------------
  3632.   unless method_defined?(:erase)
  3633.     def erase(*a)
  3634.       rect = nil
  3635.       if a.at(0).is_a?(Rect) then rect = a.at(0)
  3636.       elsif a.size == 4 then rect = Rect.new(*a)
  3637.       elsif a.size == 0 then rect = Rect.new(0,0,width,height)
  3638.       end
  3639.       fill_rect(rect.x, rect.y, rect.width, rect.height, Color.erase) if rect
  3640.     end
  3641.   end
  3642.   #-------------------------------------------------------------------------
  3643.   # ● New method: stretch
  3644.   #-------------------------------------------------------------------------
  3645.   unless method_defined?(:stretch)
  3646.     def stretch(width, height)
  3647.       dummy = Bitmap.new(width, height)
  3648.       dummy.stretch_blt(*[dummy.rect, self, self.rect])
  3649.       dummy
  3650.     end
  3651.   end
  3652.   #-------------------------------------------------------------------------
  3653.   # ● New method: crop
  3654.   #-------------------------------------------------------------------------
  3655.   unless method_defined?(:crop)
  3656.     def crop(*a)
  3657.       return unless a.size == 1 or a.size == 4
  3658.       rect = rect.at(0)   if a.size == 1 && a.at(0).is_a?(Rect)
  3659.       rect = Rect.new(*a) if a.size == 4
  3660.       cropped = Bitmap.new([rect.width,width-rect.x].min,
  3661.                            [rect.height,height-rect.y].min)
  3662.       cropped.blt(0,0,self,rect)
  3663.       cropped
  3664.     end
  3665.   end
  3666.   #-------------------------------------------------------------------------
  3667.   # ● Aliased method: stretch_blt
  3668.   #-------------------------------------------------------------------------
  3669.   def stretch_blt(*a)
  3670.     if respond_to?(:stretch_blend_blt)
  3671.       a = [a[0..2],0,1,a.at(3)||255].flatten
  3672.       stretch_blend_blt(*a)
  3673.     else
  3674.       drg_ce_stretch_blt(*a)
  3675.     end
  3676.   end
  3677.   #-------------------------------------------------------------------------
  3678.   # ● Aliased method: hue_change
  3679.   #-------------------------------------------------------------------------
  3680.   define_post_alias(:hue_change) { self }
  3681.   #--------------------------------------------------------------------------
  3682.   # ● New method: invert, etc
  3683.   #--------------------------------------------------------------------------
  3684.   [:invert, :brighten, :darken, :grayscale, :pixelate, :frost, :flip_horizontal,
  3685.    :flip_vertical, :rotate90, :rotate180, :rotate270].each do |meth|
  3686.     define_sec_method(meth) do |*args|
  3687.       bitmap = self.clone
  3688.       bitmap.send(:"#{meth}!",*args)
  3689.       return bitmap
  3690.     end
  3691.   end
  3692.   #-------------------------------------------------------------------------
  3693.   # ● New method: flip_horizontal!
  3694.   #-------------------------------------------------------------------------
  3695.   define_sec_method(:flip_horizontal!) do
  3696.     flipped = Bitmap.new(width, height)
  3697.     fliprect = Rect.new(width,0,-width,height)
  3698.     flipped.blt(0,0, self,fliprect)
  3699.     (clear || 0) && blt(0,0,flipped,rect)
  3700.     flipped.dispose
  3701.   end
  3702.   #-------------------------------------------------------------------------
  3703.   # ● New method: flip_vertical!
  3704.   #-------------------------------------------------------------------------
  3705.   define_sec_method(:flip_vertical!) do
  3706.     flipped = Bitmap.new(width, height)
  3707.     fliprect = Rect.new(0,height,width,-height)
  3708.     flipped.blt(0,0, self,fliprect)
  3709.     (clear || 0) && blt(0,0,flipped,rect)
  3710.     flipped.dispose
  3711.   end
  3712.   #-------------------------------------------------------------------------
  3713.   # ● New method: rotate90! (clockwise)
  3714.   #-------------------------------------------------------------------------
  3715.   define_sec_method(:rotate90!) do
  3716.     copy = self.clone
  3717.     (t_Fx = width).times do |i|
  3718.       height.times do |j|
  3719.         Graphics.update if (t_Fx += 1) % 10000 == 0
  3720.         a = (clockwise = true) ? [width - i - 1, j] : [i, height - j - 1]
  3721.         self.set_pixel(a[0],a[1], copy.get_pixel(j, i))
  3722.       end
  3723.     end
  3724.     copy.dispose
  3725.   end
  3726.   #-------------------------------------------------------------------------
  3727.   # ● New method: rotate180!
  3728.   #-------------------------------------------------------------------------
  3729.   define_sec_method(:rotate180!) do
  3730.     flip_vertical!
  3731.     flip_horizontal!
  3732.   end
  3733.   #-------------------------------------------------------------------------
  3734.   # ● New method: rotate270!
  3735.   #-------------------------------------------------------------------------
  3736.   define_sec_method(:rotate270!) do
  3737.     rotate90!
  3738.     rotate180!
  3739.   end  
  3740.   #----------------------------------------------------------------------------
  3741.   # ● New method: invert!
  3742.   #----------------------------------------------------------------------------
  3743.   unless method_defined?(:invert!)
  3744.     def invert!
  3745.       (t_Fx = width).times do |i|
  3746.         height.times do |j|
  3747.            color = get_pixel(i, j)
  3748.            Graphics.update if (t_Fx += 1) % 10000 == 0
  3749.            set_pixel(i, j, color.invert) if color.alpha > 0
  3750.          end
  3751.       end
  3752.     end
  3753.   end
  3754.   #--------------------------------------------------------------------------
  3755.   # * New method: brighten!
  3756.   #--------------------------------------------------------------------------
  3757.   unless method_defined?(:brighten!)
  3758.     def brighten!(amount = 10)
  3759.       (t_Fx = width).times do |w|
  3760.         height.times do |h|
  3761.           color = get_pixel(w, h)
  3762.           Graphics.update if (t_Fx += 1) % 10000 == 0
  3763.           set_pixel(w, h, Color.new([color.red + amount, 255].min,
  3764.                                     [color.green + amount, 255].min,
  3765.                                     [color.blue + amount, 255].min,
  3766.                                     color.alpha)) if color.alpha > 0
  3767.         end
  3768.       end
  3769.     end
  3770.   end
  3771.   #--------------------------------------------------------------------------
  3772.   # * New method: darken!
  3773.   #--------------------------------------------------------------------------
  3774.   unless method_defined?(:darken!)
  3775.     def darken!(amount = 10)
  3776.       (t_Fx = width).times do |w|
  3777.         height.times do |h|
  3778.           color = get_pixel(w, h)
  3779.           Graphics.update if (t_Fx += 1) % 10000 == 0
  3780.           set_pixel(w, h, Color.new([color.red - amount, 0].max,
  3781.                                     [color.green - amount, 0].max,
  3782.                                     [color.blue - amount, 0].max,
  3783.                                     color.alpha)) if color.alpha > 0
  3784.         end
  3785.       end
  3786.     end
  3787.   end
  3788.   #--------------------------------------------------------------------------
  3789.   # * New method: grayscale!
  3790.   # NOTE: calling this method repeatedly has no effect on the bitmap, but will
  3791.   #       still waste processing power. Do not call it repeatedly.
  3792.   #--------------------------------------------------------------------------
  3793.   unless method_defined?(:grayscale!)
  3794.     def grayscale!
  3795.       (t_Fx = width).times do |w|
  3796.         height.times do |h|
  3797.           color = get_pixel(w, h)
  3798.           if (a = color.alpha) > 0
  3799.             Graphics.update if (t_Fx += 1) % 10000 == 0
  3800.             num = (color.red + color.green + color.blue) / 3
  3801.             set_pixel(w, h, Color.new(num, num, num, a))
  3802.           end
  3803.         end
  3804.       end
  3805.     end
  3806.   end
  3807.   #--------------------------------------------------------------------------
  3808.   # * New method: drg_pixelate! (0.2 second)
  3809.   # NOTE: Pixelateing bitmaps to a larger pixel size is less process consuming.
  3810.   #       for best results, don't pixelate to numbers below four.
  3811.   #       This works much better for solid images, due to bugs.
  3812.   #--------------------------------------------------------------------------
  3813.   unless method_defined?(:drg_pixelate!)
  3814.     def drg_pixelate!(size = 10)
  3815.       return self if size <= (t_Fx = 1)
  3816.       c = lambda {|a,b| (a / b**2) * Math.sqrt(b) }
  3817.       for w in 0..((width - 1) / size)
  3818.         w *= size
  3819.         for h in 0..((height - 1) / size)
  3820.           h *= size
  3821.           r, g, b, a = 0, 0, 0, 0
  3822.           size.times do |i|
  3823.             color = get_pixel(w, h)
  3824.             r += color.red
  3825.             g += color.green
  3826.             b += color.blue
  3827.             a += color.alpha
  3828.             color = get_pixel(w + i, h)
  3829.             r += color.red
  3830.             g += color.green
  3831.             b += color.blue
  3832.             a += color.alpha
  3833.             color = get_pixel(w, h + i)
  3834.             r += color.red
  3835.             g += color.green
  3836.             b += color.blue
  3837.             a += color.alpha
  3838.           end
  3839.           Graphics.update if (t_Fx += 1) % 10000 == 0
  3840.           fill_rect(w, h, size, size, Color.new(c.call(r,size),c.call(g,size),
  3841.                                                 c.call(b,size),c.call(a,size)))
  3842.         end
  3843.       end
  3844.     end
  3845.   end
  3846.   #--------------------------------------------------------------------------
  3847.   # * New method: pixelate!
  3848.   #--------------------------------------------------------------------------
  3849.   unless method_defined?(:pixelate!)
  3850.     def pixelate!(size = 10)
  3851.       return mosaic!(size,size) if respond_to?(:mosaic!)
  3852.       return drg_pixelate!(size)
  3853.     end
  3854.   end
  3855.   #--------------------------------------------------------------------------
  3856.   # * New method: drg_frost! (5 second)
  3857.   # NOTE: Frosting a bitmap randomly scatters its pixels. As such, Consistant
  3858.   #       results are impossible to get.
  3859.   #       Frosting a bitmap beyond eight won't result in a more scattered image
  3860.   #--------------------------------------------------------------------------
  3861.   unless method_defined?(:drg_frost!)
  3862.     def drg_frost!(noise = 10)
  3863.       for pass in 1..noise
  3864.         (t_Fx = width).times do |w|
  3865.           height.times do |h|
  3866.             Graphics.update if (t_Fx += 1) % 10000 == 0
  3867.             set_pixel(w, h, get_pixel(w + rand(3) - 1, h + rand(3) - 1))
  3868.           end
  3869.         end
  3870.       end
  3871.     end
  3872.   end
  3873.   #-------------------------------------------------------------------------
  3874.   # ● New method: bltapiinit
  3875.   #-------------------------------------------------------------------------
  3876.   unless method_defined?(:bltapiinit)
  3877.     def bltapiinit
  3878.       info = [40,width,height,1,32,0,0,0,0,0,0].pack('LllSSLLllLL')
  3879.       hWndDC = LiTTleDRAgo.getdc.call(LiTTleDRAgo.hwnd)
  3880.       hMemDC = LiTTleDRAgo.ccdc.call(hWndDC)
  3881.       hMemBM = LiTTleDRAgo.ccbitmap.call(hWndDC, width, height)
  3882.       LiTTleDRAgo.releasedc.call(LiTTleDRAgo.hwnd, hWndDC)
  3883.       LiTTleDRAgo.selectobject.call(hMemDC, hMemBM)
  3884.       LiTTleDRAgo.setdibits.call(hMemDC, hMemBM, 0, height, address, info, 0)
  3885.       return hMemDC, hMemBM, info
  3886.     end
  3887.   end
  3888.   #-------------------------------------------------------------------------
  3889.   # ● New method: bit_blt
  3890.   #-------------------------------------------------------------------------
  3891.   unless method_defined?(:bit_blt)
  3892.     def bit_blt(x, y, src_bmp, src_rect, rastor_operation = 0xCC0020)
  3893.       hDC1, hBM1, info = *self.bltapiinit
  3894.       hDC2, hBM2 = *src_bmp.bltapiinit
  3895.       LiTTleDRAgo.bitblt.call(hDC1, x, y, src_rect.width, src_rect.height,
  3896.                   hDC2, src_rect.x, src_rect.y, rastor_operation)
  3897.       LiTTleDRAgo.getdibits.call(hDC1, hBM1, 0, height, address, info, 0)
  3898.       LiTTleDRAgo.deleteobject.call(hBM1)
  3899.       LiTTleDRAgo.deleteobject.call(hDC1)
  3900.       LiTTleDRAgo.deleteobject.call(hBM2)
  3901.       LiTTleDRAgo.deleteobject.call(hDC2)
  3902.     end
  3903.   end  
  3904.   #-------------------------------------------------------------------------
  3905.   # ● New method: crop_blt
  3906.   #-------------------------------------------------------------------------
  3907.   unless method_defined?(:crop_blt)
  3908.     def crop_blt(x, y, width, height, bitmap, dir = 1, align = 1, opacity = 255)
  3909.       w, h = bitmap.width, bitmap.height
  3910.       if w < width and h < height
  3911.         x += (width - w) / 2 if align == 1
  3912.         x += width - w       if align == 2
  3913.         return full_blt(x, y, bitmap, opacity)
  3914.       end
  3915.       i, j = dir % 3, dir / 3
  3916.       crop_x, crop_y = 0, 0
  3917.       crop_x = (w - width) / 2  if i == 1
  3918.       crop_x = w - width        if i == 2
  3919.       crop_y = (h - height) / 2 if j == 1
  3920.       crop_y = h - height       if j == 2
  3921.       blt(x, y, bitmap, Rect.new(crop_x, crop_y, width, height), opacity)
  3922.     end
  3923.   end
  3924.   #-------------------------------------------------------------------------
  3925.   # ● New method: fit_blt
  3926.   #-------------------------------------------------------------------------
  3927.   unless method_defined?(:fit_blt)  
  3928.     def fit_blt(x, y, width, height, bitmap, opacity = 255, align = 1)
  3929.       w, h = bitmap.width, bitmap.height
  3930.       if w > width or h > height
  3931.         conversion = w / h.to_f
  3932.         zoom_x, zoom_y = width * conversion, height if conversion <= 1
  3933.         zoom_x, zoom_y = width, height / conversion unless conversion <= 1
  3934.         x += (width - zoom_x) / 2 if align == 1
  3935.         x += width - zoom_x       if align == 2
  3936.         dest_rect = Rect.new(x, y, zoom_x, zoom_y)
  3937.         stretch_blt(dest_rect, bitmap, bitmap.rect, opacity)
  3938.       else
  3939.         x += (width - w) / 2 if align == 1
  3940.         x += width - w       if align == 2
  3941.         full_blt(x, y, bitmap, opacity)
  3942.       end
  3943.     end
  3944.   end
  3945.   #-------------------------------------------------------------------------
  3946.   # ● New method: full_blt
  3947.   #-------------------------------------------------------------------------
  3948.   unless method_defined?(:full_blt)
  3949.     def full_blt(x, y, bitmap, opacity = 255)
  3950.       blt(x, y, bitmap, bitmap.rect, opacity)
  3951.     end
  3952.   end
  3953.   #-------------------------------------------------------------------------
  3954.   # ● New method: scale_blt
  3955.   #-------------------------------------------------------------------------
  3956.   unless method_defined?(:scale_blt)
  3957.     def scale_blt(dest_rect, src_bitmap, src_rect = src_bitmap.rect, o = 255)
  3958.       w, h = src_rect.width, src_rect.height
  3959.       scale = [w / dest_rect.width.to_f, h / dest_rect.height.to_f].max
  3960.       ow, oh = (w / scale).to_i, (h / scale).to_i
  3961.       ox, oy = (dest_rect.width - ow) / 2, (dest_rect.height - oh) / 2
  3962.       stretch_blt(Rect.new(ox + dest_rect.x, oy + dest_rect.y, ow, oh),
  3963.         src_bitmap, src_rect, o)
  3964.     end
  3965.   end
  3966.   #-------------------------------------------------------------------------
  3967.   # ● New method: frames_flip_horizontal
  3968.   #-------------------------------------------------------------------------
  3969.   unless method_defined?(:frames_flip_horizontal)
  3970.     def frames_flip_horizontal(frames)
  3971.       frame_width = width / frames
  3972.       frame_bitmap = Bitmap.new(frame_width, height)
  3973.       rect = Rect.new(0, 0, frame_width, height)
  3974.       frames.times do |i|
  3975.         frame_bitmap.clear
  3976.         rect.x = (x = i * frame_width)
  3977.         frame_bitmap.blt(0, 0, self, rect)
  3978.         frame_bitmap.flip_horizontal!
  3979.         blt(x, 0, frame_bitmap, frame_bitmap.rect)
  3980.       end
  3981.       frame_bitmap.dispose
  3982.     end
  3983.   end
  3984.   #-------------------------------------------------------------------------
  3985.   # ● New method: frames_flip_vertical
  3986.   #-------------------------------------------------------------------------
  3987.   unless method_defined?(:frames_flip_vertical)
  3988.     def frames_flip_vertical(frames)
  3989.       frame_height = height / frames
  3990.       frame_bitmap = Bitmap.new(width, frame_height)
  3991.       rect = Rect.new(0, 0, width, frame_height)
  3992.       frames.times do |i|
  3993.         frame_bitmap.clear        
  3994.         rect.y = (y = i * frame_height)
  3995.         frame_bitmap.blt(0, 0, self, rect)
  3996.         frame_bitmap.flip_vertical!
  3997.         blt(0, y, frame_bitmap, frame_bitmap.rect)
  3998.       end
  3999.       frame_bitmap.dispose
  4000.     end
  4001.   end
  4002.   #-------------------------------------------------------------------------
  4003.   # ● New method: blur_area
  4004.   #-------------------------------------------------------------------------
  4005.   unless method_defined?(:blur_area)
  4006.     def blur_area(settings = {})
  4007.       blur_settings.each do |default, setting|
  4008.         settings[default] = setting unless settings.has_key?(default)
  4009.       end
  4010.       keys = settings.keys
  4011.       rect_defined = keys.include?('rect')
  4012.       rect_p_defined = keys.include?('x') && keys.include?('y') &&
  4013.                        keys.include?('w') && keys.include?('h')
  4014.       if rect_defined
  4015.         rect = settings['rect']
  4016.         x, y, w, h = rect.x, rect.y, rect.width, rect.height
  4017.       elsif rect_p_defined
  4018.         x, y, w, h = settings['x'], settings['y'], settings['w'], settings['h']
  4019.       else
  4020.         x, y, w, h = 0, 0, self.width, self.height
  4021.       end
  4022.       dummy = self.dup
  4023.       spacing = settings['spacing']
  4024.       opacity = settings['opacity']
  4025.       settings['offset'].times do |i|
  4026.         src_rects = []
  4027.         src_rects << Rect.new(x + i * spacing, y + i * spacing, w, h)
  4028.         src_rects << Rect.new(x - i * spacing, y + i * spacing, w, h)
  4029.         src_rects << Rect.new(x + i * spacing, y - i * spacing, w, h)
  4030.         src_rects << Rect.new(x - i * spacing, y - i * spacing, w, h)
  4031.         o = Integer(opacity * (settings['offset'] - i) / (settings['offset']))
  4032.         src_rects.each { |src_rect|  blt(x, y, dummy, src_rect, o)}
  4033.       end
  4034.       dummy.dispose
  4035.       self
  4036.     end
  4037.   end
  4038.   #-------------------------------------------------------------------------
  4039.   # ● New method: sub_bitmap_blend
  4040.   #-------------------------------------------------------------------------
  4041.   unless method_defined?(:sub_bitmap_blend)
  4042.     def sub_bitmap_blend(x = 0, y = 0, bitmap = self, src_rect = bitmap.rect)
  4043.       src_rect.width.times do |i|
  4044.         src_rect.height.times do |j|
  4045.           c1 = get_pixel(x + i, y + j)
  4046.           c2 = bitmap.get_pixel(src_rect.x + i, src_rect.y + j)
  4047.           nc = Color.new(c1.red-c2.red, c1.green-c2.green, c1.blue-c2.blue)
  4048.           set_pixel(x + i, y + j, nc) if c2.alpha > 0
  4049.         end
  4050.       end
  4051.     end
  4052.   end
  4053.   #-------------------------------------------------------------------------
  4054.   # ● New method: add_bitmap_blend
  4055.   #-------------------------------------------------------------------------
  4056.   unless method_defined?(:add_bitmap_blend)
  4057.     def add_bitmap_blend(x = 0, y = 0, bitmap = self, src_rect = bitmap.rect)
  4058.       src_rect.width.times do |i|
  4059.         src_rect.height.times do |j|
  4060.           c1 = get_pixel(x + i, y + j)
  4061.           c2 = bitmap.get_pixel(src_rect.x + i, src_rect.y + j)
  4062.           nc = Color.new(c1.red+c2.red, c1.green+c2.green, c1.blue+c2.blue)
  4063.           set_pixel(x + i, y + j, nc) if c2.alpha > 0
  4064.         end
  4065.       end
  4066.     end
  4067.   end
  4068.   #--------------------------------------------------------------------------
  4069.   # * Alias Listing
  4070.   #--------------------------------------------------------------------------
  4071.   alias_sec_method :frost!,      :drg_frost!
  4072.   alias_sec_method :blur,        :blur_area
  4073.   alias_sec_method :entire_fill, :full_fill
  4074.     #--------------------------------------------------------------------------
  4075.     # * Replicated method: gradient_fill_rect
  4076.     #--------------------------------------------------------------------------
  4077.   unless method_defined?(:gradient_fill_rect)
  4078.     def gradient_fill_rect(*a)
  4079.       if a.first.is_a?(Rect)
  4080.         x, y, width, height = a[0].x, a[0].y, a[0].width, a[0].height
  4081.         color1 = a.at(1).dup
  4082.         color2 = a.at(2)
  4083.         vertical = a.at(3)
  4084.       else
  4085.         x, y, width, height = a[0..3]
  4086.         color1 = a.at(4).dup
  4087.         color2 = a.at(5)
  4088.         vertical = a.at(6)
  4089.       end
  4090.       dr = color2.red   - color1.red
  4091.       dg = color2.green - color1.green
  4092.       db = color2.blue  - color1.blue
  4093.       da = color2.alpha - color1.alpha
  4094.       start_rgba = [color1.red, color1.green, color1.blue, color1.alpha]
  4095.       if vertical
  4096.         dr, dg, db, da = dr / height, dg / height, db / height, da / height
  4097.         for i in (y.round...(y + height).round)
  4098.           fill_rect(x, i, width, 1, color1)
  4099.           start_rgba[0] = [start_rgba.at(0) + dr, 0].max.round
  4100.           start_rgba[1] = [start_rgba.at(1) + dg, 0].max.round
  4101.           start_rgba[2] = [start_rgba.at(2) + db, 0].max.round
  4102.           start_rgba[3] = [start_rgba.at(3) + da, 0].max.round
  4103.           color1.set(*start_rgba)
  4104.         end
  4105.       else
  4106.         dr, dg, db, da = dr / width, dg / width, db / width, da / width
  4107.         for i in (x.round...(x + width).round)
  4108.           fill_rect(i, y, 1, height, color1)
  4109.           start_rgba[0] = [start_rgba.at(0) + dr, 0].max.round
  4110.           start_rgba[1] = [start_rgba.at(1) + dg, 0].max.round
  4111.           start_rgba[2] = [start_rgba.at(2) + db, 0].max.round
  4112.           start_rgba[3] = [start_rgba.at(3) + da, 0].max.round
  4113.           color1.set(*start_rgba)
  4114.         end
  4115.       end
  4116.     end
  4117.   end
  4118. end
  4119. #==============================================================================
  4120. # ** String    
  4121. #------------------------------------------------------------------------------
  4122. #  
  4123. #==============================================================================
  4124. class String
  4125.   #-------------------------------------------------------------------------
  4126.   # ● New method: get_int
  4127.   #-------------------------------------------------------------------------
  4128.   unless method_defined?(:get_int)
  4129.     def get_int
  4130.       m = self.scan(/([-]?\d+)/i).flatten
  4131.       return m.join.to_i
  4132.     end
  4133.   end
  4134.   #-------------------------------------------------------------------------
  4135.   # ● New method: get_ints
  4136.   #-------------------------------------------------------------------------
  4137.   unless method_defined?(:get_ints)
  4138.     def get_ints
  4139.       array = self.scan(/([-]?\d+)/i).flatten.relay(:i)
  4140.       return array
  4141.     end
  4142.   end
  4143.   #-------------------------------------------------------------------------
  4144.   # ● New method: encrypt
  4145.   #-------------------------------------------------------------------------
  4146.   unless method_defined?(:encrypt)
  4147.     def encrypt(encryption_string = 'encrypt')
  4148.       encryption_bytes = []
  4149.       encryption_string.each_byte {|c| encryption_bytes << c}
  4150.       string = ''
  4151.       self.size.times do |i|
  4152.         byte = self[i] * encryption_bytes[i % encryption_bytes.size]
  4153.         base, mod = byte / 255, byte % 255
  4154.         string += base.chr + mod.chr
  4155.       end
  4156.       return string
  4157.     end
  4158.   end
  4159.   #-------------------------------------------------------------------------
  4160.   # ● New method: encrypt!
  4161.   #-------------------------------------------------------------------------
  4162.   unless method_defined?(:encrypt!)
  4163.     def encrypt!(encryption_string = 'encrypt')
  4164.       replace(encrypt(encryption_string))
  4165.     end
  4166.   end
  4167.   #-------------------------------------------------------------------------
  4168.   # ● New method: decrypt
  4169.   #-------------------------------------------------------------------------
  4170.   unless method_defined?(:decrypt)
  4171.     def decrypt(encryption_string = 'encrypt')
  4172.       encryption_bytes = []
  4173.       encryption_string.each_byte {|c| encryption_bytes << c}
  4174.       string = ''
  4175.       (self.size / 2).times do |i|
  4176.         b, m = self[i * 2] * 255, self[i * 2 + 1]
  4177.         string += ((b + m) / encryption_bytes[i % encryption_bytes.size]).chr
  4178.       end
  4179.       return string
  4180.     end
  4181.   end
  4182.   #-------------------------------------------------------------------------
  4183.   # ● New method: decrypt!
  4184.   #-------------------------------------------------------------------------
  4185.   unless method_defined?(:decrypt!)
  4186.     def decrypt!(encryption_string = 'encrypt')
  4187.       replace(decrypt(encryption_string))
  4188.     end
  4189.   end
  4190.   #-------------------------------------------------------------------------
  4191.   # ● New method: clear, words, characters
  4192.   #-------------------------------------------------------------------------
  4193.   define_sec_method(:clear)      { replace('')      }
  4194.   define_sec_method(:words)      { split(' ')       }
  4195.   define_sec_method(:characters) { split('')        }
  4196.   define_sec_method(:last)       { characters.last  }
  4197.   define_sec_method(:first)      { characters.first }
  4198. end
  4199.  
  4200. #==============================================================================
  4201. # ** Game_Interpreter
  4202. #------------------------------------------------------------------------------
  4203. #  An interpreter for executing event commands. This class is used within the
  4204. # Game_Map, Game_Troop, and Game_Event classes.
  4205. #==============================================================================
  4206. Klass = LiTTleDRAgo::VX ? Game_Interpreter : Interpreter
  4207. class Klass
  4208.   #--------------------------------------------------------------------------
  4209.   # * Constant
  4210.   #--------------------------------------------------------------------------
  4211.   SCRIPT_WAIT_RESULTS = [:wait, FalseClass]
  4212.   #--------------------------------------------------------------------------
  4213.   # ● Redirect Listing
  4214.   #--------------------------------------------------------------------------
  4215.   redirect_method :picture_sprite,        '$game_map.picture_sprite'
  4216.   #--------------------------------------------------------------------------
  4217.   # ● New method: random_teleport
  4218.   #--------------------------------------------------------------------------
  4219.   unless method_defined?(:random_teleport)
  4220.     def random_teleport(id = 0)
  4221.       character = id.is_a?(Game_Character) ? id : get_character(id)
  4222.       character.random_teleport if character.is_a?(Game_Character)
  4223.     end
  4224.   end
  4225.   #--------------------------------------------------------------------------
  4226.   # ● Redefined method: same_map?, next_event_code
  4227.   #--------------------------------------------------------------------------
  4228.   define_sec_method(:same_map?)       { @map_id == $game_map.map_id    }
  4229.   define_sec_method(:next_event_code) { @list[@index + 1].code         }
  4230.   #--------------------------------------------------------------------------
  4231.   # * Overwriten method: command_355
  4232.   #--------------------------------------------------------------------------
  4233.   def command_355
  4234.     script = @list[index = @index].parameters[0] + "\n"
  4235.     while [655, 355].include?(next_event_code) do
  4236.       script += @list[@index+=1].parameters[0] + "\n"
  4237.     end
  4238.     wait = SCRIPT_WAIT_RESULTS.include?(eval(script)) && LiTTleDRAgo::XP
  4239.     return wait ? !(@index = index) : true
  4240.   end  
  4241. end
  4242.  
  4243. #==============================================================================
  4244. # ** Sprite
  4245. #------------------------------------------------------------------------------
  4246. #  A sprite class for bitmap processing.
  4247. #==============================================================================
  4248. class Sprite
  4249.   #---------------------------------------------------------------------------
  4250.   # * Alias Listing
  4251.   #---------------------------------------------------------------------------
  4252.   alias_sec_method(:angle_fix_alias, :"angle=")
  4253.   alias_sec_method(:drg_core_dispose,:"dispose")
  4254.   alias_sec_method(:set_x_alias,     :"x=")
  4255.   alias_sec_method(:set_y_alias,     :"y=")
  4256.   alias_sec_method(:get_x_alias,     :"x")
  4257.   alias_sec_method(:get_y_alias,     :"y")
  4258.   #---------------------------------------------------------------------------
  4259.   # * New method: method_missing
  4260.   #---------------------------------------------------------------------------
  4261.   def method_missing(val,*a,&b)
  4262.     return bitmap.send(val.to_s,*a,&b) if bitmap.respond_to?(val.to_sym)
  4263.     text = "Undefined method #{val} for #{self.inspect}"
  4264.     raise(NoMethodError, text, caller(1))              
  4265.   end
  4266.   #---------------------------------------------------------------------------
  4267.   # * Aliased method: x=, y=
  4268.   #---------------------------------------------------------------------------
  4269.   define_method(:x=)      {|amt| set_x_alias((@intfloat_x = amt).to_i) }
  4270.   define_method(:y=)      {|amt| set_y_alias((@intfloat_y = amt).to_i) }
  4271.   define_method(:x)       { @intfloat_x ||= get_x_alias }
  4272.   define_method(:y)       { @intfloat_y ||= get_y_alias }
  4273.   define_method(:dispose) { disposed? || drg_core_dispose }
  4274.   define_method(:rotate_correction) { self.ox,self.oy = width/2, height/2 }
  4275.   #---------------------------------------------------------------------------
  4276.   # * Aliased method: angle=
  4277.   #---------------------------------------------------------------------------
  4278.   def angle=(v)
  4279.     return if @angle == v
  4280.     sign = (v % 360 == 180 || v % 180 == 90) ? 1.00001 : 1
  4281.     angle_fix_alias((@angle = v) * sign)
  4282.   end
  4283.   #--------------------------------------------------------------------------
  4284.   # ● Redefined method: width, height
  4285.   #--------------------------------------------------------------------------
  4286.   define_sec_method(:width)       { src_rect.width }
  4287.   define_sec_method(:height)      { src_rect.height }
  4288.   #--------------------------------------------------------------------------
  4289.   # ● Overwriten method: dup
  4290.   #--------------------------------------------------------------------------
  4291.   def dup
  4292.     sprite = self.class.new(self.viewport)
  4293.     sprite.bitmap                = self.bitmap.clone
  4294.     sprite.zoom_x, sprite.zoom_y = self.zoom_x, self.zoom_y
  4295.     sprite.x, sprite.y           = self.x, self.y
  4296.     sprite.ox, sprite.oy         = self.ox, self.oy
  4297.     sprite.color, sprite.tone    = self.color.dup, self.tone.dup
  4298.     sprite.src_rect,sprite.angle = self.src_rect, self.angle
  4299.     return sprite
  4300.   end
  4301.   #--------------------------------------------------------------------------
  4302.   # ● Overwriten method: clone
  4303.   #--------------------------------------------------------------------------
  4304.   def clone
  4305.     (sprite = self.dup) && instance_variables.each do |var|
  4306.       temp = instance_variable_get("#{var}")
  4307.       sprite.instance_variable_set("#{var}",temp) rescue nil
  4308.     end
  4309.     (frozen? && sprite.freeze)
  4310.     return sprite  
  4311.   end
  4312.   #--------------------------------------------------------------------------
  4313.   # ● New method: scale
  4314.   #--------------------------------------------------------------------------
  4315.   unless method_defined?(:scale)
  4316.     def scale(w, h)
  4317.       tmp = self.bitmap.clone
  4318.       self.bitmap = Bitmap.new(w, h)
  4319.       self.bitmap.stretch_blt(Rect.new(0, 0, w, h), tmp, tmp.rect)
  4320.       tmp.dispose
  4321.     end
  4322.   end
  4323.   #--------------------------------------------------------------------------
  4324.   # ● New method: in_rect?
  4325.   #--------------------------------------------------------------------------
  4326.   unless method_defined?(:in_rect?)
  4327.     def in_rect?(*rect)
  4328.       if rect.size == 1
  4329.         x, y, w, h = rect[0].x, rect[0].y, rect[0].width, rect[0].height
  4330.         return self.x.between?(x, x + w) && self.y.between?(y, y + h)
  4331.       elsif rect.size == 4
  4332.         x, y, w, h = rect
  4333.         return self.x.between?(x, x + w) && self.y.between?(y, y + h)
  4334.       end
  4335.       return false
  4336.     end
  4337.   end
  4338. end
  4339.  
  4340. #==============================================================================
  4341. # ** Scheduler
  4342. #------------------------------------------------------------------------------
  4343. #  This class allows to schedule a proc or method call a given amount of frames
  4344. #  into the future with any amount of arguments
  4345. #==============================================================================
  4346. unless defined?(Scheduler)
  4347.   class Scheduler
  4348.     #==========================================================================
  4349.     # ** Order
  4350.     #--------------------------------------------------------------------------
  4351.     #  An order is a proc, method or something else which has 'call' as a
  4352.     #  method, and the arguments to pass along.
  4353.     #==========================================================================
  4354.     unless method_defined?(:update)
  4355.       Order = Struct.new(:callable, :arguments)
  4356.       class Order
  4357.         #----------------------------------------------------------------------
  4358.         # * New Methods: call
  4359.         #----------------------------------------------------------------------
  4360.         define_method(:call) { callable.call(*arguments) }
  4361.       end
  4362.       #========================================================================
  4363.       # ** RecurringOrder
  4364.       #------------------------------------------------------------------------
  4365.       #  An order which is recurring every specified amount of time until
  4366.       #  FalseClass is returned from the call.
  4367.       #  Note that arguments remain the same for each call
  4368.       #========================================================================
  4369.       RecurringOrder = Struct.new(:callable, :arguments, :frames)
  4370.       class RecurringOrder
  4371.         #----------------------------------------------------------------------
  4372.         # * New Methods: call
  4373.         #----------------------------------------------------------------------
  4374.         def call
  4375.           result = callable.call(*arguments)
  4376.           unless result == FalseClass
  4377.             Scheduler.schedule_recurring(frames, frames, callable, *arguments)
  4378.           end
  4379.         end
  4380.       end
  4381.       #========================================================================
  4382.       # ** Mapping
  4383.       #------------------------------------------------------------------------
  4384.       # Maps an index to an array. Values can be added to these value.
  4385.       # Each array starts empty.
  4386.       #========================================================================
  4387.       class Mapping
  4388.         #----------------------------------------------------------------------
  4389.         # * New method: add, get, empty
  4390.         #----------------------------------------------------------------------
  4391.         define_method(:add)   { |i, v| ((@mapping ||= {})[i] ||=[]) << v }
  4392.         define_method(:get)   { |i|    ((@mapping ||= {})[i] || [])   }
  4393.         define_method(:empty) { |i|     (@mapping ||= {}).delete(i)   }
  4394.       end
  4395.       #------------------------------------------------------------------------
  4396.       # * New method: schedule
  4397.       #------------------------------------------------------------------------
  4398.       def schedule(frames, callable, *arguments)
  4399.         order = Order.new(callable, arguments)
  4400.         (@mapping ||= Mapping.new).add(frames + (@tick||=0), order)
  4401.       end
  4402.       #------------------------------------------------------------------------
  4403.       # * New method: schedule_recurring
  4404.       #------------------------------------------------------------------------
  4405.       def schedule_recurring(frames, frames_to_wait, callable, *arguments)
  4406.         order = RecurringOrder.new(callable, arguments, frames_to_wait)
  4407.         (@mapping ||= Mapping.new).add(frames + (@tick||=0), order)
  4408.       end
  4409.       #------------------------------------------------------------------------
  4410.       # * New method: update
  4411.       #------------------------------------------------------------------------
  4412.       def update
  4413.         orders = (@mapping ||= Mapping.new).get((@tick||=0))
  4414.         @mapping.empty(@tick)
  4415.         orders.each {|s| s.call }
  4416.         @tick += 1
  4417.       end
  4418.       #------------------------------------------------------------------------
  4419.       # * Self
  4420.       #------------------------------------------------------------------------
  4421.       @@instance = self.new
  4422.       class << self
  4423.         define_method(:instance) { @@instance }
  4424.         redirect_method :schedule_recurring, 'instance.schedule_recurring'
  4425.         redirect_method :schedule,           'instance.schedule'
  4426.         redirect_method :update,             'instance.update'
  4427.       end
  4428.     end
  4429.   end
  4430.   Graphics.auto_update.push([Scheduler,:update])
  4431. end
  4432. #==============================================================================
  4433. # ** Dir    
  4434. #------------------------------------------------------------------------------
  4435. #
  4436. #==============================================================================
  4437. class << Dir
  4438.   #--------------------------------------------------------------------------
  4439.   # ● New method: make_dir
  4440.   #--------------------------------------------------------------------------
  4441.   def make_dir(path)
  4442.     (dir = path.split("/")).size.times do |i|
  4443.       next if dir == "."
  4444.       Dir.mkdir(dir[0..i].join("/")) rescue nil
  4445.     end
  4446.   end
  4447.   #--------------------------------------------------------------------------
  4448.   # ● New method: files_with_extension
  4449.   #--------------------------------------------------------------------------
  4450.   def files_with_extension(directory, extension)
  4451.     entries(directory).select{|file| File.extname(file) == extension}
  4452.   end
  4453. end
  4454.  
  4455. #==============================================================================
  4456. # ** BitmapDump
  4457. #------------------------------------------------------------------------------
  4458. #
  4459. #==============================================================================
  4460. module BitmapDump; end
  4461. class << BitmapDump
  4462.   #--------------------------------------------------------------------------
  4463.   # ● New method: self.bitmap_data
  4464.   #--------------------------------------------------------------------------
  4465.   unless method_defined?(:bitmap_data)
  4466.     def bitmap_data(bitmap)
  4467.       red = Table.new(bitmap.width, bitmap.height)
  4468.       green, blue, alpha = red.clone, red.clone, red.clone      
  4469.       bitmap.width.times do |i|
  4470.         bitmap.height.times do |j|
  4471.           color = bitmap.get_pixel(i, j)
  4472.           red[i, j],  green[i, j] = color.red, color.green
  4473.           blue[i, j], alpha[i, j] = color.blue, color.alpha
  4474.         end
  4475.       end
  4476.       return [red, green, blue, alpha]
  4477.     end
  4478.   end
  4479.   #--------------------------------------------------------------------------
  4480.   # ● New method: self.read_bitmap_data
  4481.   #--------------------------------------------------------------------------
  4482.   unless method_defined?(:read_bitmap_data)
  4483.     def read_bitmap_data(red, green, blue, alpha)
  4484.       bitmap = Bitmap.new(red.xsize, red.ysize)
  4485.       bitmap.width.times do |i|
  4486.         bitmap.height.times do |j|
  4487.           color_table = [red[i, j], green[i, j], blue[i, j], alpha[i, j]]
  4488.           bitmap.set_pixel(i, j, Color.new(*color_table))
  4489.         end
  4490.       end
  4491.       return bitmap
  4492.     end
  4493.   end
  4494.   #--------------------------------------------------------------------------
  4495.   # ● New method: self.save_bitmap
  4496.   #--------------------------------------------------------------------------
  4497.   unless method_defined?(:save_bitmap)
  4498.     def save_bitmap(bitmap, filename, folder = '')
  4499.       bitmap_data = bitmap_data(bitmap)
  4500.       if folder && folder != ''
  4501.         Dir.make_dir(folder) unless FileTest.directory?(folder)
  4502.         filename = "#{folder}/#{filename}"
  4503.       end
  4504.       filename = filename.gsub("\\","/").gsub("//","/")
  4505.       file = File.open(filename,'wb')
  4506.       Marshal.dump(bitmap_data, file)
  4507.       file.close
  4508.     end
  4509.   end
  4510.   #--------------------------------------------------------------------------
  4511.   # ● New method: self.load_bitmap
  4512.   #--------------------------------------------------------------------------
  4513.   unless method_defined?(:load_bitmap)
  4514.     def load_bitmap(filename, folder = '')
  4515.       if folder && folder != ''
  4516.         Dir.make_dir(folder) unless FileTest.directory?(folder)
  4517.         filename = "#{folder}/#{filename}"
  4518.       end
  4519.       filename = filename.gsub("\\","/").gsub("//","/")
  4520.       file = File.open(filename,"rb")
  4521.       colors = Marshal.load(file)
  4522.       file.close
  4523.       bitmap = read_bitmap_data(colors)
  4524.       return bitmap
  4525.     end
  4526.   end
  4527. end