IceDragon200

ISS - SRE

Jul 16th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.08 KB | None | 0 0
  1. # // 05/01/2011
  2. # // 05/08/2011
  3. # // Sub-Routine Engine
  4. #==============================================================================#
  5. # ** ISS
  6. #==============================================================================#
  7. module ISS
  8. #==============================================================================#
  9. # ** SRE
  10. #==============================================================================#  
  11.   module SRE ; end
  12. #==============================================================================#
  13. # ** SRE::Script # A dumpable proc like class XD
  14. #==============================================================================#  
  15.   class SRE::Script
  16.     attr_accessor :script
  17.     def initialize( script ) ; @script = script end
  18.     def get_script() ; return @script end
  19.   end
  20. #==============================================================================#
  21. # ** SRE::ObjectHandler
  22. #==============================================================================#  
  23.   class SRE::ObjectHandler
  24.    
  25.     attr_accessor :character
  26.     attr_accessor :resource
  27.    
  28.     def initialize( character )
  29.       @character    = character
  30.       @resource     = ::ISS::SRE::ResourceHandler.new()
  31.       @resource.max = 10
  32.     end
  33.    
  34.     def gather( n )
  35.       @resource.increase_value( n )
  36.     end
  37.    
  38.     def ungather( n )
  39.       @resource.decrease_value( n )
  40.     end
  41.    
  42.     def update()
  43.     end
  44.    
  45.   end
  46. #==============================================================================#
  47. # ** SRE::ResourceHandler
  48. #==============================================================================#  
  49.   class SRE::ResourceHandler
  50.    
  51.     attr_accessor :value
  52.     attr_accessor :max
  53.     attr_accessor :type
  54.    
  55.     def initialize( )
  56.       @type  = :nil
  57.       @minimum = 0
  58.       @value = 0
  59.       @max   = 0
  60.     end
  61.    
  62.     def cap_value()  ; return [[@value, @max].min, @minimum].max end
  63.     def cap_value!() ; @value = cap_value end
  64.    
  65.     def increase_value( n ) ; @value += n ; cap_value!() end
  66.     def decrease_value( n ) ; increase_value( -n ) end
  67.    
  68.     def maxed?() ; return @value >= @max end
  69.     def max?()   ; return maxed?() end
  70.     def min?()   ; return @value <= @minimum end
  71.      
  72.   end
  73. #==============================================================================#
  74. # ** SRE::ActionCommand
  75. #==============================================================================#    
  76.   class SRE::ActionCommand
  77.    
  78.     attr_accessor :parameters
  79.     attr_accessor :type
  80.    
  81.     def initialize( type = "", paramters = [] )
  82.       @type       = type
  83.       @parameters = paramters
  84.     end
  85.    
  86.   end
  87.  
  88. #==============================================================================#
  89. # ** SRE::ActionCommandSet
  90. #==============================================================================#  
  91.   class SRE::ActionCommandSet
  92.    
  93.     attr_accessor :type
  94.     attr_accessor :skip_procs  # // Used for loop sets
  95.     attr_accessor :break_procs # // Used for loop sets
  96.     attr_accessor :end_procs   # //
  97.     attr_accessor :proc_order
  98.     attr_accessor :list
  99.     attr_accessor :name
  100.    
  101.     def initialize( )
  102.       @name        = ""
  103.       @type        = :one_shot
  104.       @skip_procs  = []
  105.       @break_procs = []
  106.       @end_procs   = []
  107.       @list        = []
  108.       @proc_order  = [:break, :skip]
  109.     end
  110.    
  111.     def break?( character )
  112.       @break_procs.each { |p|
  113.         case p
  114.         #when ::Proc # // Proc support has been removed
  115.           #return true if p.call( character )
  116.         when ::ISS::SRE::Script
  117.           return true if eval( p.get_script )
  118.         end  
  119.       }
  120.       return false
  121.     end
  122.    
  123.     def skip?( character )
  124.       @skip_procs.each { |p|
  125.         case p
  126.         #when ::Proc # // Proc support has been removed
  127.           #return true if p.call( character )
  128.         when ::ISS::SRE::Script
  129.           return true if eval( p.get_script )
  130.         end  
  131.       }
  132.       return false
  133.     end
  134.    
  135.   end
  136.  
  137. #==============================================================================#
  138. # ** SRE::ActionCommandRef
  139. #==============================================================================#  
  140.   class SRE::ActionCommandRef
  141.    
  142.     attr_accessor :phase_actions
  143.     attr_accessor :phase_size
  144.     attr_accessor :type
  145.    
  146.     def initialize( )
  147.       @type           = :one_shot
  148.       @phase_actions  = []
  149.       @phase_size     = 0
  150.     end
  151.    
  152.     def get_actionlist( n ) ;
  153.       return nil if @phase_actions[n].nil?()
  154.       return @phase_actions[n].clone()
  155.     end
  156.    
  157.   end
  158.  
  159. #==============================================================================#
  160. # ** SRE::ActionCommandEngine
  161. #==============================================================================#  
  162.   class SRE::ActionCommandEngine
  163.  
  164.     attr_accessor :action_ref
  165.     attr_accessor :action_list
  166.     attr_accessor :action_phase
  167.     attr_accessor :action_set
  168.     attr_accessor :character
  169.    
  170.     def sre_debug? ; return false end
  171.    
  172.     def initialize( character = nil )
  173.       @character      = character
  174.       @action_ref     = ::ISS::SRE::ActionCommandRef.new()
  175.       @action_list    = []  # Full action list, each set, is shifted off until its empty
  176.       @action_set     = nil # Current action set
  177.       @action_loop    = false
  178.       @action_phase   = 0   # // 0 - Start, 1 - Loop, 2 - End
  179.       @wait_count     = 0
  180.     end
  181.      
  182.     def update()
  183.       @wait_count -= 1 unless @wait_count == 0
  184.       return unless @wait_count == 0
  185.       update_actions()
  186.     end
  187.    
  188.     def update_actions()
  189.       unless @action_loop
  190.         if @action_list.empty?()
  191.           s = @action_ref.get_actionlist(@action_phase)
  192.           @action_list << s unless s.nil?
  193.         end  
  194.         return if @action_list.empty?()
  195.         a = @action_list.shift()
  196.         @action_set     = a
  197.         @action_loop    = a.nil?() ? false : a.type == :loop
  198.         p a.name if sre_debug?
  199.       end  
  200.       return if @action_set.nil?()
  201.       @action_set.proc_order.each { |t|
  202.         case t
  203.         when :break
  204.           if @action_set.break?( @character )
  205.             @action_phase += 1
  206.             @action_phase %= @action_ref.phase_size
  207.             return @action_loop = false
  208.           else ; @action_loop = true ; end
  209.         when :skip ; return if @action_set.skip?( @character )
  210.         end  
  211.       }  
  212.       @action_set.list.each { |c|  
  213.         break if perform_command(c.type, c.parameters) == 2}
  214.     end
  215.    
  216.     def perform_command( command, parameters )
  217.       case command.upcase
  218.       when "ANIMATION"
  219.         action_animation( command, parameters )
  220.       when "MOVE"
  221.         action_move( command, parameters )
  222.       when "GATHER"
  223.         action_gather( command, parameters )
  224.       when "DROPOFF", "UNGATHER"
  225.         action_ungather( command, parameters )
  226.       when "CHANGEPHASE"
  227.         action_phase( command, parameters )
  228.       when "SCRIPT"
  229.         action_script( command, parameters )
  230.       when "WAIT"  
  231.         action_wait( command, parameters )
  232.         return 2
  233.       end  
  234.       return 0
  235.     end  
  236.    
  237.     def action_animation( action, parameters )
  238.       @character.animation_id = parameters[0].to_i
  239.     end
  240.    
  241.     def action_move( action, parameters )
  242.       case parameters[0].upcase()
  243.       when "TO"
  244.         target_x, target_y =*parameters[1] # // [x, y] integer array
  245.         diagonal           = parameters[2] # // boolean
  246.         max_iterations     = parameters[3] # // integer
  247.         #@character.move_toward_xy( target_x, target_y )
  248.         @character.force_path ( target_x, target_y, diagonal, max_iterations )
  249.       when "SCRIPT_TO"
  250.         data = []
  251.         5.times { |i| data[i] = eval( parameters[1] ) }
  252.         data[1][1] += 1 # // y+
  253.         data[2][1] -= 1 # // y-
  254.         data[3][0] -= 1 # // x-
  255.         data[4][0] += 1 # // x+
  256.         #@character.find_path( *data )
  257.         5.times { |i| return if @character.force_path ( *data[i] ) }
  258.       end
  259.     end
  260.    
  261.     def action_gather( action, parameters )
  262.       @character.rts_object.gather( 1 )
  263.     end
  264.    
  265.     def action_ungather( action, parameters )
  266.       @character.rts_object.ungather( 1 )
  267.     end
  268.    
  269.     def action_phase( action, parameters )
  270.       case parameters[0].to_s().upcase()
  271.       when "FORWARD"  ; @action_phase += 1
  272.       when "BACKWARD" ; @action_phase -= 1
  273.       else            ; @action_phase  = parameters[0].to_i
  274.       end  
  275.     end
  276.    
  277.     def action_script( action, parameters )
  278.       character = @character
  279.       eval( parameters[0] )
  280.     end
  281.    
  282.     def action_wait( action, parameters )
  283.       @wait_count = parameters[0]
  284.     end
  285.    
  286.   end
  287. #==============================================================================#
  288. # ** SRE
  289. #==============================================================================#  
  290.   module SRE
  291.    
  292.     SCR = ::ISS::SRE::Script
  293.    
  294.     DEFSCRIPTS = {
  295.       "TRUE"  => SCR.new( "true" ),
  296.       "FALSE" => SCR.new( "false" ),
  297.       "MOVING"=> SCR.new( "character.moving?()" ),
  298.     }
  299.      
  300.     module_function
  301.     def get_defScript( name ) return DEFSCRIPTS[name].clone end
  302.      
  303.     def create_Moveto_set( tx, ty, diagonol = true, mi = 0 )
  304.       comm = ::ISS::SRE::ActionCommand.new( "MOVE", ["TO", [tx, ty], diagonol, mi] )
  305.       set  = ::ISS::SRE::ActionCommandSet.new()
  306.       set.list        << comm
  307.       set.skip_procs  << get_defScript("MOVING")
  308.       set.break_procs << SCR.new( "character.pos?(#{tx}, #{ty})" )
  309.       set.type = :loop
  310.       return set
  311.     end
  312.    
  313.     def create_ScriptMoveto_set( script )
  314.       comm = ::ISS::SRE::ActionCommand.new( "MOVE", ["SCRIPT_TO", script] )
  315.       set  = ::ISS::SRE::ActionCommandSet.new()
  316.       set.list        << comm
  317.       set.skip_procs  << get_defScript("MOVING")
  318.       str = %Q(
  319.         data = character.dungeon_obj.get_next_tile()
  320.         data = [-1, -1] if data.nil?
  321.         character.pos?(data[0], data[1])
  322.       )
  323.       set.break_procs << SCR.new( str )  
  324.       set.type = :loop
  325.       return set
  326.     end
  327.    
  328.     def create_Gather_set( max )
  329.       set  = ::ISS::SRE::ActionCommandSet.new()
  330.       set.list        << ::ISS::SRE::ActionCommand.new( "ANIMATION", [3] )
  331.       set.list        << ::ISS::SRE::ActionCommand.new( "GATHER", [max] )
  332.       set.list        << ::ISS::SRE::ActionCommand.new( "WAIT", [12] )
  333.       set.skip_procs  << get_defScript("FALSE")
  334.       set.break_procs << SCR.new( "character.rts_object.resource.maxed?()" )
  335.       set.type = :loop
  336.       return set
  337.     end
  338.    
  339.     def create_Drop_set( max )
  340.       scomm= ::ISS::SRE::ActionCommand.new( "SCRIPT",
  341.         ["$game_variables[20] = [$game_variables[20] + 1, $game_variables[19]].min"] )
  342.       set  = ::ISS::SRE::ActionCommandSet.new()
  343.       set.list        << ::ISS::SRE::ActionCommand.new( "ANIMATION", [2] )
  344.       set.list        << ::ISS::SRE::ActionCommand.new( "UNGATHER", [max] )
  345.       set.list        << scomm
  346.       set.list        << ::ISS::SRE::ActionCommand.new( "WAIT", [12] )
  347.       set.skip_procs  << get_defScript("FALSE")
  348.       set.break_procs << SCR.new( "character.rts_object.resource.min?()" )
  349.       set.type = :loop
  350.       return set
  351.     end
  352.    
  353.     def create_ChangeTile_set()
  354.       scomm= ::ISS::SRE::ActionCommand.new( "SCRIPT",
  355.         ["character.dungeon_obj.change_tile"])
  356.       set  = ::ISS::SRE::ActionCommandSet.new()
  357.       set.list        << ::ISS::SRE::ActionCommand.new( "ANIMATION", [2] )
  358.       set.list        << scomm
  359.       set.list        << ::ISS::SRE::ActionCommand.new( "WAIT", [12] )
  360.       set.skip_procs  << get_defScript("FALSE")
  361.       set.break_procs << SCR.new( '$game_map.data[character.x, character.y, 0] == 1568' )
  362.       set.type = :one_shot
  363.       return set
  364.     end
  365.    
  366.     def create_gather_ref( max, trg = [0, 0], ret = [0, 0] )
  367.       ref    = ::ISS::SRE::ActionCommandRef.new()
  368.       mvset1 = create_Moveto_set( trg[0], trg[1], false )
  369.       mvset2 = create_Moveto_set( ret[0], ret[1], false )
  370.       gthr   = create_Gather_set( max )
  371.       drp    = create_Drop_set  ( max )
  372.       mvset1.name = "MoveToTarget"
  373.       mvset2.name = "Return"
  374.       gthr.name   = "Gather"
  375.       drp.name    = "Dropoff"
  376.       ref.phase_actions  = [mvset1, gthr, mvset2, drp]
  377.       ref.phase_size     = 4
  378.       ref.type           = :loop
  379.       return ref
  380.     end
  381.    
  382.     def create_Marktile_set( script )
  383.       comm = ::ISS::SRE::ActionCommand.new( "SCRIPT", [script] )
  384.       set  = ::ISS::SRE::ActionCommandSet.new()
  385.       set.list        << comm
  386.       set.skip_procs  << get_defScript("FALSE")
  387.       str = %Q(
  388.         data = character.dungeon_obj.get_next_tile()
  389.         data = [-1, -1] if data.nil?
  390.         $game_map.data[data[0], data[1], 1] == 1538
  391.       )
  392.       set.break_procs << SCR.new( str )  
  393.       set.type = :one_shot
  394.       return set
  395.     end
  396.    
  397.     def create_wallremove_ref()
  398.       ref    = ::ISS::SRE::ActionCommandRef.new()
  399.       mvset1 = create_ScriptMoveto_set( "character.dungeon_obj.get_next_tile()" )
  400.       mvset1.skip_procs.unshift( SCR.new( '$tile_markers.empty?()' ) )
  401.       mvset1.name = "MoveToTarget"
  402.       chng   = create_ChangeTile_set()
  403.       chng.name = "ChangeTile"
  404.       mark = create_Marktile_set( %Q(
  405.         data = character.dungeon_obj.get_next_tile()
  406.         data = [-1, -1] if data.nil?
  407.         $game_map.data[data[0], data[1], 1] = 1538) )
  408.       mark.name = "MarkNextTile"
  409.       retn = create_ScriptMoveto_set( "character.dungeon_obj.home_xy" )
  410.       retn.break_procs.clear
  411.       retn.break_procs << SCR.new( '!$tile_markers.empty?()' )
  412.       retn.name = "ReturnHome"
  413.       ref.phase_actions  = [mark, mvset1, chng, retn]
  414.       ref.phase_size     = 4
  415.       ref.type           = :loop
  416.       return ref
  417.     end
  418.    
  419.     def setup_gather_event(event, targ_xy, ret_xy)
  420.       event.setup_rts()
  421.       r = create_gather_ref(event.rts_object.resource.max, targ_xy, ret_xy)
  422.       event.rts_engine.action_ref = r
  423.     end
  424.    
  425.     def setup_digger_event(event)
  426.       event.setup_rts() ; event.setup_dungeon()
  427.       r = create_wallremove_ref() ; event.rts_engine.action_ref = r
  428.     end
  429.    
  430.   end
  431.  
  432. end  
  433.  
  434. #==============================================================================#
  435. # ** Game_Character
  436. #==============================================================================#
  437. class Game_Character
  438.  
  439.   attr_accessor :sre_object
  440.   attr_accessor :sre_engine
  441.  
  442.   # // For compat
  443.   ref_accessor :rts_object, :sre_object
  444.   ref_accessor :rts_engine, :sre_engine
  445.   ref_reader :setup_rts, :setup_sre
  446.  
  447.   def setup_sre()
  448.     @sre_object = ::ISS::SRE::ObjectHandler.new( self )
  449.     @sre_engine = ::ISS::SRE::ActionCommandEngine.new( self )
  450.   end
  451.  
  452.   def iss_posarray() ; return [self.x, self.y] end
  453.   def pos_array()    ; return iss_posarray() end
  454.  
  455.   alias iss005_gc_update update unless $@
  456.   def update()
  457.     iss005_gc_update()
  458.     @rts_object.update() unless @sre_object.nil?()
  459.     @rts_engine.update() unless @sre_engine.nil?()
  460.   end
  461.  
  462. end  
  463. #=*==========================================================================*=#
  464. # ** END OF FILE
  465. #=*==========================================================================*=#
Advertisement
Add Comment
Please, Sign In to add comment