F117Landers

(SSMS) Mouse 1

Jul 9th, 2016
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.05 KB | None | 0 0
  1. #==============================================================================
  2. # ** Modules.Mouse Input (7.0)              By Near Fantastica & SephirothSpawn
  3. #==============================================================================
  4. module Mouse
  5.     #--------------------------------------------------------------------------  
  6.     # * Mouse to Input Triggers  
  7.     #  
  8.     #   key => Input::KeyCONSTANT (key: 0 - Left, 1 - Middle, 2 - Right)  
  9.     #--------------------------------------------------------------------------  
  10.     Mouse_to_Input_Triggers = {0 => Input::C, 1 => Input::B, 2 => Input::A}  
  11.     #--------------------------------------------------------------------------  
  12.     # * API Declaration  
  13.     #--------------------------------------------------------------------------  
  14.     GAKS          = Win32API.new('user32',    'GetAsyncKeyState', 'i',     'i')  
  15.     GSM           = Win32API.new('user32',    'GetSystemMetrics', 'i',     'i')  
  16.     Cursor_Pos    = Win32API.new('user32',    'GetCursorPos',     'p',     'i')  
  17.     $ShowCursor   = Win32API.new('user32',    'ShowCursor',       'i',     'l')  
  18.     Scr2cli       = Win32API.new('user32',    'ScreenToClient',   %w(l p), 'i')  
  19.     Client_rect   = Win32API.new('user32',    'GetClientRect',    %w(l p), 'i')  
  20.     Findwindow    = Win32API.new('user32',    'FindWindowA',      %w(p p), 'l')  
  21.     Readini       = Win32API.new('kernel32',  'GetPrivateProfileStringA',
  22.                                %w(p p p p l p), 'l')
  23.                                
  24.     # if graphical effects turned on, show fancy cursor  
  25.     $ShowCursor.call($game_mouse ? 0 : 1)    
  26.  
  27.     @triggers     =   [[0, 1], [0, 2], [0, 4]]  
  28.     @old_pos      =   0  
  29.     @pos_i        =   0      
  30.  
  31.     #--------------------------------------------------------------------------  
  32.     # * Mouse Grid Position  
  33.     #--------------------------------------------------------------------------  
  34.     def self.grid        
  35.         # Return Nil if Position is Nil    
  36.         return nil if @pos.nil?        
  37.    
  38.         # Get X & Y Locations      
  39.         x = (@pos[0] + $game_map.display_x / 4) / 32    
  40.         y = (@pos[1] + $game_map.display_y / 4) / 32        
  41.    
  42.         # Vehicle Stuff    
  43.         $mouse_x = x    
  44.         $mouse_y = y        
  45.    
  46.         # Return X & Y    
  47.         return [x, y]      
  48.    
  49.     end  
  50.     #--------------------------------------------------------------------------  
  51.     # * Mouse Position  
  52.     #--------------------------------------------------------------------------  
  53.     def self.position    
  54.         return @pos == nil ? [0, 0] : @pos  
  55.     end  
  56.     #--------------------------------------------------------------------------  
  57.     # * Mouse Global Position  
  58.     #--------------------------------------------------------------------------  
  59.     def self.global_pos    
  60.         # Packs 0 Position    
  61.         pos = [0, 0].pack('ll')    
  62.         # Returns Unpacked Cursor Position Call    
  63.         return Cursor_Pos.call(pos) == 0 ? nil : pos.unpack('ll')  
  64.     end    
  65.  
  66.     #--------------------------------------------------------------------------  
  67.     # * Screen to Client  
  68.     #--------------------------------------------------------------------------  
  69.     def self.screen_to_client(x=0, y=0)    
  70.         pos = [x, y].pack('ll')    
  71.         return Scr2cli.call(self.hwnd, pos) == 0 ? nil : pos.unpack('ll')  
  72.     end        
  73.    
  74.     #--------------------------------------------------------------------------  
  75.     # * Mouse Position  
  76.     #--------------------------------------------------------------------------  
  77.     def self.pos    
  78.    
  79.         global_pos = [0, 0].pack('ll')        
  80.         gx, gy = Cursor_Pos.call(global_pos) == 0 ? nil : global_pos.unpack('ll')    
  81.        
  82.         local_pos = [gx, gy].pack('ll')    
  83.         x, y = Scr2cli.call(self.hwnd, local_pos) == 0 ? nil : local_pos.unpack('ll')        
  84.        
  85.         # Begins Test    
  86.         begin      
  87.             # Return X & Y or Nil Depending on Mouse Position      
  88.             if (x >= 0 && y >= 0 && x <= 640 && y <= 480)        
  89.                 return x, y      
  90.             else        
  91.                 return -20, -20      
  92.             end    
  93.         rescue      
  94.             return 0, 0 #nil    
  95.         end      
  96.        
  97.     end        
  98.    
  99.     #--------------------------------------------------------------------------  
  100.     # * Update Mouse Position  
  101.     #--------------------------------------------------------------------------  
  102.     def self.update        
  103.    
  104.         # Update Position    
  105.         old_pos = @pos    
  106.         @pos = self.pos        
  107.        
  108.         # agf = hide system mouse    
  109.         if !$mouse_sprite.visible && old_pos != @pos      
  110.             $mouse_sprite.visible = true    
  111.         end        
  112.        
  113.         # when mouse leaves game window, show system mouse    
  114.         if old_pos != [-20, -20] && @pos == [-20, -20]      
  115.             $ShowCursor.call(1)    
  116.         # when mouse is in game window, show custom mouse if it's turned on    
  117.         elsif old_pos == [-20, -20] && @pos != [-20, -20]      
  118.             $ShowCursor.call($game_mouse ? 0 : 1)    
  119.         end    
  120.        
  121.         # Update Triggers    
  122.         for i in @triggers      
  123.             # Gets Async State      
  124.             n = GAKS.call(i[1])      
  125.             # If 0 or 1      
  126.             if [0, 1].include?(n)        
  127.                 i[0] = (i[0] > 0 ? i[0] * -1 : 0)      
  128.             else        
  129.                 i[0] = (i[0] > 0 ? i[0] + 1 : 1)      
  130.             end    
  131.         end      
  132.     end  
  133.    
  134.     #--------------------------------------------------------------------------  
  135.     # * Trigger?  
  136.     #     id : 0:Left, 1:Right, 2:Center  
  137.     #--------------------------------------------------------------------------  
  138.     def self.trigger?(id = 0)        
  139.    
  140.         #only user trigger if in range of screen    
  141.         pos = self.pos    
  142.         if pos != [-20,-20]        
  143.             case id      
  144.                 when 0  # Left        
  145.                     return @triggers[id][0] == 1      
  146.                 when 1  # Right (only when menu enabled)        
  147.                     if @triggers[1][0] == 1 && !$game_system.menu_disabled          
  148.                         return @triggers[id][0] == 1              
  149.                     end      
  150.                 when 2  
  151.                     # Center        
  152.                     return @triggers[id][0] == 1    
  153.             end            
  154.         end      
  155.     end  
  156.     #--------------------------------------------------------------------------  
  157.     # * Repeat?  
  158.     #     id : 0:Left, 1:Right, 2:Center  
  159.     #--------------------------------------------------------------------------  
  160.     def self.repeat?(id = 0)    
  161.         if @triggers[id][0] <= 0      
  162.             return false    
  163.         else      
  164.             return @triggers[id][0] % 5 == 1 && @triggers[id][0] % 5 != 2    
  165.         end  
  166.     end  
  167.     #--------------------------------------------------------------------------  
  168.     # * Screen to Client  
  169.     #--------------------------------------------------------------------------  
  170.     def self.screen_to_client(x=0, y=0)    
  171.         # Pack X & Y    
  172.         pos = [x, y].pack('ll')    
  173.         # Return Unpacked Position or Nil    
  174.         return Scr2cli.call(self.hwnd, pos) == 0 ? nil : pos.unpack('ll')  
  175.     end  
  176.     #--------------------------------------------------------------------------  
  177.     # * Hwnd - window handle  
  178.     #--------------------------------------------------------------------------  
  179.     def self.hwnd    
  180.         if @hwnd.nil?      
  181.             # Finds Game Name from ini file      
  182.             game_name = "\0" * 256      
  183.             Readini.call('Game', 'Title', '', game_name, 255, ".\\Game.ini")      
  184.             game_name.delete!("\0")      
  185.             # Finds Window      
  186.             @hwnd = Findwindow.call('RGSS Player', game_name)    
  187.         end    
  188.         return @hwnd  
  189.     end  
  190.     #--------------------------------------------------------------------------  
  191.     # * Client Size  
  192.     #--------------------------------------------------------------------------  
  193.     def self.client_size    
  194.         # Packs Empty Rect    
  195.         rect = [0, 0, 0, 0].pack('l4')    
  196.         # Gets Game Window Rect    
  197.         Client_rect.call(self.hwnd, rect)    
  198.         # Unpacks Right & Bottom    
  199.         right, bottom = rect.unpack('l4')[2..3]    
  200.         # Returns Right & Bottom    
  201.         return right, bottom  
  202.     end
  203. end
  204.  
  205. #==============================================================================
  206. # ** Input
  207. #==============================================================================
  208.  
  209. class << Input  
  210.     #------------------------------------------------------------------------  
  211.     # * Alias Listings  
  212.     #------------------------------------------------------------------------  
  213.     unless self.method_defined?(:seph_mouse_input_update)    
  214.         alias_method :seph_mouse_input_update,   :update    
  215.         alias_method :seph_mouse_input_trigger?, :trigger?    
  216.         alias_method :seph_mouse_input_repeat?,  :repeat?  
  217.     end  
  218.     #------------------------------------------------------------------------  
  219.     # * Frame Update  
  220.     #------------------------------------------------------------------------  
  221.     def update    
  222.         # Update Mouse    
  223.         Mouse.update    
  224.         # Original Update    
  225.         seph_mouse_input_update  
  226.     end  
  227.     #--------------------------------------------------------------------------  
  228.     # * Trigger? Test  
  229.     #--------------------------------------------------------------------------  
  230.     def trigger?(constant)    
  231.         # Return true if original test is true    
  232.         return true if seph_mouse_input_trigger?(constant)    
  233.         # If Mouse Position isn't Nil    
  234.         unless Mouse.pos.nil?      
  235.             # If Mouse Trigger to Input Trigger Has Constant      
  236.             if Mouse::Mouse_to_Input_Triggers.has_value?(constant)        
  237.                 # Return True if Mouse Triggered        
  238.                 mouse_trigger = Mouse::Mouse_to_Input_Triggers.index(constant)        
  239.                 return true if Mouse.trigger?(mouse_trigger)            
  240.             end    
  241.         end    
  242.         # Return False    
  243.         return false  
  244.     end  
  245.     #--------------------------------------------------------------------------  
  246.     # * Repeat? Test  
  247.     #--------------------------------------------------------------------------  
  248.     def repeat?(constant)    
  249.         # Return true if original test is true    
  250.         return true if seph_mouse_input_repeat?(constant)    
  251.         # If Mouse Position isn't Nil    
  252.         unless Mouse.pos.nil?      
  253.             # If Mouse Trigger to Input Trigger Has Constant      
  254.             if Mouse::Mouse_to_Input_Triggers.has_value?(constant)        
  255.                 # Return True if Mouse Triggered        
  256.                 mouse_trigger = Mouse::Mouse_to_Input_Triggers.index(constant)            
  257.                 return true if Mouse.repeat?(mouse_trigger)      
  258.             end    
  259.         end    
  260.         # Return False    
  261.         return false  
  262.     end
  263. end
  264.  
  265.  
  266. #==============================================================================
  267. #  ¦ Path Finding
  268. #==============================================================================
  269. # Near Fantastica# Version 1
  270. # 29.11.05
  271. #==============================================================================  
  272.  
  273.     class Game_Character    
  274.         #--------------------------------------------------------------------------    
  275.         alias nf_pf_game_character_initialize initialize    
  276.         alias nf_pf_game_character_update update    
  277.         #--------------------------------------------------------------------------    
  278.         attr_accessor :map    
  279.         attr_accessor :runpath    
  280.         attr_accessor :ovrdest    
  281.         #--------------------------------------------------------------------------    
  282.         def initialize      
  283.             nf_pf_game_character_initialize      
  284.             @map = nil      
  285.             @runpath = false      
  286.             @ovrdest = false    
  287.         end    
  288.         #--------------------------------------------------------------------------    
  289.         def update      
  290.             run_path if @runpath == true      
  291.             nf_pf_game_character_update    
  292.         end    
  293.         #--------------------------------------------------------------------------    
  294.         def run_path      
  295.        
  296.             return if moving?      
  297.             step = @map[@x,@y]      
  298.             if step == 1        
  299.                 @map = nil        
  300.                 @runpath = false        
  301.                 return      
  302.             end            
  303.            
  304.             dir = rand(2)      
  305.             case dir      
  306.             when 0        
  307.                 move_right if @map[@x+1,@y] == step - 1 and step != 0        
  308.                 move_down if @map[@x,@y+1] == step - 1 and step != 0        
  309.                 move_left if @map[@x-1,@y] == step -1 and step != 0        
  310.                 move_up if @map[@x,@y-1] == step - 1 and step != 0      
  311.             when 1        
  312.            
  313.                 move_up if @map[@x,@y-1] == step - 1 and step != 0        
  314.                 move_left if @map[@x-1,@y] == step -1 and step != 0        
  315.                 move_down if @map[@x,@y+1] == step - 1 and step != 0        
  316.                 move_right if @map[@x+1,@y] == step - 1 and step != 0      
  317.             end    
  318.         end    
  319.        
  320.         #--------------------------------------------------------------------------    
  321.         def find_path(x,y, force = true)      
  322.             sx, sy = @x, @y      
  323.             result = setup_map(sx,sy,x,y)            
  324.             @runpath = result[0]      
  325.             @map = result[1]      
  326.             @map[sx,sy] = result[2] if result[2] != nil      
  327.             $game_player.ignore_movement = @runpath ? force : false    
  328.         end    
  329.        
  330.         #--------------------------------------------------------------------------    
  331.         def clear_path      
  332.             @map = nil      
  333.             @runpath = false      
  334.             @ovrdest = false      
  335.             $game_player.ignore_movement = false    
  336.         end    
  337.         #--------------------------------------------------------------------------    
  338.        
  339.         def setup_map(sx,sy,ex,ey)      
  340.             map = Table.new($game_map.width, $game_map.height)      
  341.             # Shaz - adding this comment to the second line of the event commands      
  342.             #     Mouse[0,1]      
  343.             # will cause the player to go to the tile BELOW the event, turn up,      
  344.             # and interact with the event ([0,1] is x+0, y+1)      
  345.             tx = ex      
  346.             ty = ey      
  347.             event = $game_map.event_at(ex, ey)      
  348.             if !event.nil? && !event.list.nil? && !event.erased &&        
  349.                 event.list.size > 1 && event.list[1].code == 108        
  350.                 text = event.list[1].parameters.to_s        
  351.                 text.gsub!(/[Mm][Oo][Uu][Ss][Ee]\[([-,0-9]+),([-,0-9]+)\]/) do          
  352.                     tx = ex + $1.to_i          
  353.                     ty = ey + $2.to_i          
  354.                     map[ex, ey] = 999          
  355.                     @ovrdest = true        
  356.                 end      
  357.             end            
  358.  
  359.             update_counter = 0      
  360.             map[tx,ty] = 1      
  361.             old_positions = []      
  362.             new_positions = []      
  363.            
  364.                 old_positions.push([tx, ty])      
  365.                
  366.             depth = 2      
  367.             $path_allow = false      
  368.             depth.upto(100){|step|        
  369.                 loop do          
  370.                     break if old_positions[0] == nil          
  371.                     x,y = old_positions.shift          
  372.                     return [true, map, step-1] if x == sx and y == sy          
  373.                     if map[x,y + 1] == 0 and $game_player.passable?(x, y, 2, step, tx, ty)            
  374.                         map[x,y + 1] = step            
  375.                         new_positions.push([x,y + 1])          
  376.                     end          
  377.                     if map[x - 1,y] == 0 and $game_player.passable?(x, y, 4, step, tx, ty)            
  378.                         map[x - 1,y] = step            
  379.                         new_positions.push([x - 1,y])          
  380.                     end          
  381.                     if map[x + 1,y] == 0 and $game_player.passable?(x, y, 6, step, tx, ty)            
  382.                         map[x + 1,y] = step            
  383.                         new_positions.push([x + 1,y])          
  384.                     end          
  385.                     if map[x,y - 1] == 0 and $game_player.passable?(x, y, 8, step, tx, ty)            
  386.                         map[x,y - 1] = step            
  387.                         new_positions.push([x,y - 1])          
  388.                     end                    
  389.                
  390.                     # If we've checked quite a few tiles, allow graphics and input          
  391.                     # to update - to avoid the 'script hanging' error          
  392.                     update_counter += 1          
  393.                     if update_counter > 100            
  394.                         Graphics.update            
  395.                         update_counter = 0          
  396.                     end        
  397.                 end                
  398.            
  399.                 old_positions = new_positions        
  400.                 new_positions = []      
  401.             }            
  402.        
  403.             @ovrdest = false      
  404.             return [false, nil, nil]                    
  405.        
  406.         end  
  407.     end    
  408.    
  409.     class Game_Map    
  410.         #--------------------------------------------------------------------------    
  411.         alias pf_game_map_setup setup    
  412.         #--------------------------------------------------------------------------    
  413.         def setup(map_id)      
  414.             pf_game_map_setup(map_id)      
  415.             $game_player.clear_path    
  416.         end  
  417.     end    
  418.    
  419.     class Game_Player    
  420.         attr_accessor :ignore_movement    
  421.         #--------------------------------------------------------------------------    
  422.         alias pf_game_player_update update    
  423.         #--------------------------------------------------------------------------    
  424.         def update      
  425.             $game_player.clear_path if Input.dir4 != 0      
  426.             pf_game_player_update    
  427.         end  
  428.     end    
  429.    
  430.     class Interpreter    
  431.         #--------------------------------------------------------------------------    
  432.         def event      
  433.             return $game_map.events[@event_id]    
  434.         end  
  435.     end
Advertisement
Add Comment
Please, Sign In to add comment