#==============================================================================
# ** TDS Lufia II Enemy Move
#    Ver: 1.0
#------------------------------------------------------------------------------
#  * Description:
#  This script replicates the enemy movement alongside the player on map from
#  the game "Lufia II".
#------------------------------------------------------------------------------
#  * Features: 
#  Makes events on map move with the player.
#
#  Allows you to stun events for such situations as escapes or others.
#------------------------------------------------------------------------------
#  * Instructions:
#  To make an event move with the player add this to the event's name.
#
#  PLAYER_MOVE: STEPS_MOVE STEPS_TO
#
#  STEPS_MOVE = Steps to move once the player moves.
#  STEPS_TO   = Steps the player needs to make before this event can move.
#
#  Example:
#
#  PLAYER_MOVE: 1 1
#------------------------------------------------------------------------------
#  * Notes:
#   The script will work with the default movement type of the event.
#   (Random, Approach, Custom).
#
#   For enemies that move more than once, it would be a good idea to increase
#   their normal walking speed.
#------------------------------------------------------------------------------
#  * New Methods:
#  Game_Map:
#  - update_player_move_events
#    ^ Method used to update events in the player move events array.
#
#  Game_Event:
#  - player_move_stun(step_duration = 5)
#    ^ Method used to "stun" and prevent event movement by an amount of steps.
#  - decrease_player_move_counter(value = 1)
#    ^ Method used to decrease event move counter by a value.
#  - update_self_movement
#    ^ Added counter and movement with player. If the event is not an event that
#      moves with the player it does nothing to it.
#
#  Game_Interpreter:
#  - player_move_stun(step_duration = 5)
#    ^ Method added to allow easier use by a call script from the event.
#------------------------------------------------------------------------------
#  * Aliased Methods:
#  Game_Map:
#  - setup(map_id)
#    ^ Aliased to intialize the player moves events array.
#
#  Game_Event:
#  - initialize(map_id, event)
#    ^ Aliased to add move with player values, if applicable.
#
#  Game_Player:
#  - increase_steps 
#    ^ Aliased to update events that move with the player.
#------------------------------------------------------------------------------
# WARNING:
#
# Do not release, distribute or change my work without my expressed written 
# consent, doing so violates the terms of use of this work.
#
# If you really want to share my work please just post a link to the original
# site.
#
# * Not Knowing English or understanding these terms will not excuse you in any
#   way from the consequenses.
#==============================================================================

#==============================================================================
# ** Game_Map
#------------------------------------------------------------------------------
#  This class handles maps. It includes scrolling and passage determination
# functions. The instance of this class is referenced by $game_map.
#==============================================================================

class Game_Map
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor   :player_move_events     # Events that move with player array  
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------  
  alias tds_lufia_enemy_move_game_map_setup          setup          unless $@        
  #--------------------------------------------------------------------------
  # * Setup
  #     map_id : map ID
  #--------------------------------------------------------------------------
  def setup(map_id)
    # Events that move with player array
    @player_move_events = []
    # Run Original Method
    tds_lufia_enemy_move_game_map_setup(map_id)    
  end  
  #--------------------------------------------------------------------------
  # * Update Player Move Events
  #--------------------------------------------------------------------------
  def update_player_move_events
    # Update Player Move Events and decrease their move counter    
    @player_move_events.each do |event| event.decrease_player_move_counter end
  end
end

#==============================================================================
# ** Game_Event
#------------------------------------------------------------------------------
#  This class deals with events. It handles functions including event page 
# switching via condition determinants, and running parallel process events.
# It's used within the Game_Map class.
#==============================================================================

class Game_Event < Game_Character
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------  
  alias tds_lufia_enemy_move_game_event_initialize     initialize   unless $@    
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_reader   :player_based_movement           # Player Based Movement Flag
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     map_id : map ID
  #     event  : event (RPG::Event)
  #--------------------------------------------------------------------------
  def initialize(map_id, event)
    # Run Original Method
    tds_lufia_enemy_move_game_event_initialize(map_id, event)
    # Check for Player Based Movement flag
    @player_based_movement = @event.name.include?("PLAYER_MOVE")
    # If Player Based Movement
    if @player_based_movement
      # Check for Player Move values
      @event.name[/PLAYER_MOVE: ([0-9]+) ([-0-9]+)/]
      # Steps to move for player move
      @move_per_steps = $1 ==  nil ? 1 : $1.to_i
      # Completed step moves counter
      @completed_step_moves = 0
      # Steps counter max value
      @steps_counter_max = $2 == nil ?  1: $2.to_i 
      # Steps counter current value
      @steps_counter = @steps_counter_max
      # Add this event to the array of events that move with the player
      $game_map.player_move_events << self
    end
  end
  #--------------------------------------------------------------------------
  # * Decrease Player Move Counter
  #--------------------------------------------------------------------------
  def player_move_stun(step_duration = 5)
    # Set Step Counter to Max plus step duration
    @steps_counter = @steps_counter_max + step_duration
  end
  #--------------------------------------------------------------------------
  # * Decrease Player Move Counter
  #    value : value to which decrease the steps counter
  #--------------------------------------------------------------------------
  def decrease_player_move_counter(value = 1)
    # Decrease step Counter
    @steps_counter -= value
    # Update Self Movement
    update_self_movement
  end
  #--------------------------------------------------------------------------
  # * Update During Self movement
  #--------------------------------------------------------------------------
  def update_self_movement
    # If Event self movement is based on players movement
    if @player_based_movement
      # If Steps Counter is 0 or less
      if @steps_counter <= 0
        # If not moving and completed move steps is the same as max move per steps          
        if !moving? and (@completed_step_moves != @move_per_steps)
          # Move Type Case
          case @move_type
          when 1;  move_random
          when 2;  move_toward_player
          when 3
            # If Move Route list code is 0 (Do nothing)
            if @move_route.list[@move_route_index].code == 0
              # Reduce completed steps by one (It will make it take an extra step)
              @completed_step_moves -= 1
            end
            # Move Type: Custom
            move_type_custom            
          end
          # Increase completed move steps value by 1
          @completed_step_moves += 1 
        end        
        # If completed move steps is the same as move per steps
        if @completed_step_moves == @move_per_steps
          # Reset Step counter
          @steps_counter = @steps_counter_max
          # Reset completed step moves
          @completed_step_moves = 0
          return          
        end        
      end      
    else
      super
    end    
  end
end

#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles maps. It includes event starting determinants and map
# scrolling functions. The instance of this class is referenced by $game_map.
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------  
  alias tds_lufia_enemy_move_game_player_increase_steps increase_steps unless $@  
  #--------------------------------------------------------------------------
  # * Increase Steps
  #--------------------------------------------------------------------------
  def increase_steps
    # Run Original Method
    tds_lufia_enemy_move_game_player_increase_steps
    # If Player is not being moved by an event
    if !@move_route_forcing
      # Update Player Move Events
      $game_map.update_player_move_events
    end    
  end
end

#==============================================================================
# ** Game_Interpreter
#------------------------------------------------------------------------------
#  An interpreter for executing event commands. This class is used within the
# Game_Map, Game_Troop, and Game_Event classes.
#==============================================================================

class Game_Interpreter
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     step_duration : duration of steps to stun for
  #--------------------------------------------------------------------------
  def player_move_stun(step_duration = 5)
    # Stun Event for duration of steps
    $game_map.events[@event_id].player_move_stun(step_duration)
  end
end