Making Enemy Events only move when player moves

I'm working on a script that allows for the creation of a Roguelike game within RPG Maker MV. I'm having a lot of trouble with making my enemy events move only when the player character moves. My current idea is to have an alias of Game_Event.prototype.updateSelfMovement() that only goes into the switch statement if Game_CharacterBase.prototype.isMoving() is true. But that doesn't seem to be working....


Any ideas?


~Z
For RPG Maker MV...unfortunately I do not know of a solution for you.


However, I worked with 2 great individuals on another forum that has since bit the dust. The script that was made was called 'lufia movement' script. It was written for RPG VX. I'll post that script in the spoiler below this message, it might give you some clues on how to accomplish what you want.

Spoiler
#-----------------------------------------------------------------------------
#                     ***** Lufia Movement 1.0 *****
#
#                        Original script: BigEd781
#                        Expanded by: Kal
#                        Ideas and testing: zortik
#-----------------------------------------------------------------------------
#
#--------------------------What it does---------------------------------------
#
# With this scripts you can make events move a la enemy movement in the
# Lufia games. This means that the event will move a certain ammount of
# tiles when the player moves. If the player stands still so will the event.
#
# Events can move in different patterns. Currently there are 4 patterns:
# 1. Approach (moves towards the player)
# 2. Flee     (moves away from the player)
# 3. Random   (moves randomly)
# 4. Opposite (for example, if the player moves up the event moves down)
#
# Different move ratios are supported. This means that an event can move
# for example 2, 3 or 4 tiles (no upper limit) when the player moves. It
# can also go the other way, so the event can move 1 tile when the player
# has taken 2 steps for example.
#
#-------------------------How to use it---------------------------------------
#
# To use this script you add a comment to the event(s) you want to use.
# The comment is in the following format: lufiamovement:type:ratio
#
# So say I want to make an event that moves 2 tiles in a random direction
# when the player moves. Then I would make a comment somewhere in the event
# that said: lufiamovement:random:2
#
# You can use the following types: approach, flee, random, opposite.
#
# You can use any whole number (including negative numbers) for the ratio.
# If the ratio is 0 the event will not move at all. If it is -1 or 1 it will
# move normally (one tile every time the player moves). If it is 2 it will move
# 2 tiles, if it is 3 it will move 3 tiles and so on.
#
# If the ratio is negative the event will move once the player has taken
# that amount of steps. So for example if the ratio is -4 that means the
# event will move once the player has taken 4 steps.
#
#-----------------------------------------------------------------------------


module LufiaMovementConfig
  # Set this constant to the to the time (in seconds) you want the event
  # to not move after it has touched the player. If you don't want to wait
  # at all, just set it to 0. Note: this number does not need to be a whole
  # number. For example, setting it to 0.5 would pause the event for half
  # a second.
  MOVE_TIMER = 0.5
end


class Game_Event
  attr_accessor :lm_player_moved, :lm_move_times
  attr_reader :lufia_movement


  #
  # Checks to see if the command comment is present in the event.
  # If it is we do some setup, otherwise ignore it.
  #
  alias :eds_old_pre_lufia_movement_setup :setup
  def setup(*args)
    eds_old_pre_lufia_movement_setup(*args)
    if command = lm_get_comment_command
      @lm_move_timer = 0
      @lufia_movement = LufiaMovement.move_factory(command, self)
      $game_player.lm_subscribe_moved(self) if @lufia_movement
    end
  end


  #
  # Returns the first comment command that is found in an event's
  # comments. A command is in the format of lufiamovement:[command]
  # Also gets the move times if present.
  #
  def lm_get_comment_command
    unless @list.nil?
      @list.each do |event_cmd|
        next if event_cmd.code != 108
        comment = event_cmd.parameters[0]
        command = comment[/lufiamovement🙁\w+)/i, 1]
        unless command.nil?
          lm_set_move_times(comment)
          return command
        end
      end
    end    
    return nil
  end
  
  #
  # Extracts the move times from a command string and assigns the @move_times
  # variable. If no move time tag is in the string, set @move_times to 1
  #
  def lm_set_move_times(s)
    times = s[/lufiamovement:\w+🙁-\d+|\d+)/i, 1]
    times ? @lm_move_times = times.to_i : @lm_move_times = 1
  end
  
  #
  # Determine if touch event is triggered
  # If it is start the event, reset the player moved flag
  # and set the move timer.
  #
  def lm_check_event_trigger_touch(x = self.x, y = self.y)
    return if $game_map.interpreter.running?
    if @trigger == 2 and $game_player.pos?(x, y, true)
      if not jumping? and @priority_type == 1
        start
        @lm_player_moved = false
        @lm_move_timer = LufiaMovementConfig::MOVE_TIMER * Graphics.frame_rate
      end
    end
  end
  
  #
  # Okay a bunch of stuff is happening here. It's mostly conditionals
  # that checks whether an event should more or not.
  # It's also in this method that we check to see if the event has
  # touched the player.
  #
  alias :eds_old_pre_lufia_movement_update :update
  def update
    # If the event has a move queue, move the event, if not
    # generate a move queue and then move the event
    unless $game_map.interpreter.running?
      lm_check_event_trigger_touch if @lm_player_moved
      @lm_move_timer -= 1 if @lm_move_timer
      if @lufia_movement and @lm_move_timer <= 0
        if lm_on_screen? and @lufia_movement.has_queue?
          lm_execute_event_movement
        elsif lm_on_screen? and @lm_player_moved
          @lufia_movement.set_queue
          lm_execute_event_movement
        end
      end
    end
    
    eds_old_pre_lufia_movement_update  
  end
  
  #
  # Starts an event move call. Depending on the value of @lm_move_times
  # the event is either moved once in the direction that is in the move queue
  # or if the @lm_move_times is negative a single direction is generated
  # and then moved once the player has moved enough times.
  #
  def lm_execute_event_movement
    unless @lufia_movement.event.moving?
      if @lufia_movement.has_queue?
        @lufia_movement.move_from_queue
      else
        @count_player_moves ||= 0
        @count_player_moves += 1
        if @count_player_moves == @lm_move_times.abs
          @lufia_movement.move_once
          @count_player_moves = 0
        end
      end
      @lm_player_moved = false
    end
  end


  #
  # Calculate if the event is on the part of the screen you can see
  # Different movement patterns may need slightly different screen checks
  #
  def lm_on_screen?
    screen_x = $game_map.display_x / 256
    screen_y = $game_map.display_y / 256
    x_adjust = @lufia_movement.x_adjust
    y_adjust = @lufia_movement.y_adjust
    width_adjust = @lufia_movement.width_adjust
    height_adjust = @lufia_movement.height_adjust
    screen_rect = Rect.new(screen_x + x_adjust,
                           screen_y + y_adjust,
                           16 + width_adjust,
                           12 + height_adjust)
                          
    screen_rect.lm_include?(LufiaMovementHelper:😛oint.new(x, y))
  end
  
  #
  # Checks to see if the tile in the specified direction is passable
  #
  def lm_can_move?(direction)
    case direction
      when :up then passable?(@x, @y - 1)
      when :down then passable?(@x, @y + 1)
      when :left then passable?(@x - 1, @y)
      when :right then passable?(@x + 1, @y)
    end
  end
  
  #
  # Checks to see if moving in a direction will collide with the player
  #
  def lm_touch_player?(direction)
    case direction
    when :up
      lm_check_event_trigger_touch(self.x, self.y - 1)
    when :down
      lm_check_event_trigger_touch(self.x, self.y + 1)
    when :left
      lm_check_event_trigger_touch(self.x - 1, self.y)
    when :right
      lm_check_event_trigger_touch(self.x + 1, self.y)
    end
  end
end


class Game_Player
  
  #
  # Used by lm_check_event_trigger_touch to see if the event is touching
  # the player.
  #
  alias :eds_old_pre_lufia_movement_pos? 😛os?
  def pos?(x, y, touch = false)
    if touch
      #return true if the player is in any tile around the event
      return true if x == @x and y == @y       #same tile
      return true if x == @x and y == @y - 1   #top      
      return true if x == @x + 1 and y == @y   #right
      return true if x == @x and y == @y + 1   #bottom
      return true if x == @x - 1 and y == @y   #left
      return false      
    else
      eds_old_pre_lufia_movement_pos?(x, y)
    end
  end
  
  # The following three methods implement a basic observer pattern.
  # Lufia movement event subscribe to the player character, and will
  # be notified whenever the player has moved.
  
  #
  # Notify the subscribers the player has moved.
  #
  alias :lm_old_increase_steps :increase_steps
  def increase_steps
    lm_notify_moved unless @move_route_forcing
    lm_old_increase_steps
  end
  
  #
  # Adds an event to the subscriber list.
  #
  def lm_subscribe_moved(event)
    @lm_subscribers ||= []
    @lm_subscribers << event
  end
  
  #
  # Notify the events that the player has moved.
  #
  def lm_notify_moved
    return if @lm_subscribers.nil?
    @lm_subscribers.each do |sub|
      sub.lm_player_moved = true
    end
  end
end


#
# The purpose of this module is to make it easier to add different
# event movement patterns without cluttering up the Game_Event class.
#
module LufiaMovement
  
  #
  # This method picks the right class (and returns an instance of it)
  # depending on what command we have. The event passed in is the event
  # this instance will move.
  #
  def self.move_factory(command, event)
    class_name = command.downcase.capitalize + "Movement"
    return nil unless const_defined?(class_name)
    const_get(class_name).new(event)
  end
  
  #
  # the parent class for the move classes. Supplies common
  # behavior that more than one movement class might need.
  #
  class LufiaCommonMovement
    attr_accessor :event
    attr_reader :x_adjust, :y_adjust, :width_adjust, :height_adjust
    
    #
    # If a subclass uses super to call this method remember to do it
    # before you set any of these values in the subclass so they do not
    # get overriden.
    #
    def initialize(event)
      @x_adjust = 0
      @y_adjust = 0
      @width_adjust = 0
      @height_adjust = 0
      @event = event
      @move_queue = 0
      @last_event_pos = LufiaMovementHelper:😛oint.new(0, 0)
    end
    
    #
    # The @move queue is the amount of times an event should move
    # if the player moves.
    #
    def set_queue
      @move_queue = @event.lm_move_times
    end
    
    def has_queue?
      @move_queue > 0
    end
    
    #
    # move_from_queue will move the event one tile, and decrement
    # @move_queue by 1
    #
    def move_from_queue
      move_once
      @move_queue -= 1
    end
    
    #
    # Moves the event in the 4-way direction specified by a symbol
    #
    def move_in_direction(direction)
      case direction
      when :up
        @event.move_up
      when :down
        @event.move_down
      when :left
        @event.move_left
      when :right
        @event.move_right
      end
      @event.lm_check_event_trigger_touch
    end
    
    # The following methods are used by more than one movement pattern
    # to determine how to move.
    
    #
    # This method is used by ApproachMovement and FleeMovement.
    # Gets a an array of moves where the first elemtent is the move
    # that will get the event closer to the player, and the rest of the
    # array are the other directions ordered in the priority that we
    # want to move if it is impossible to move in the prefered direction
    #
    def get_approach_moves
      diff_x = @event.x - $game_player.x
      diff_y = @event.y - $game_player.y
      
      if diff_x.abs > diff_y.abs # more difference in the x axis
        @last_event_pos = get_event_position
        determine_middle🙂x)
        if diff_x > 0
          [:left, @x_middle, :right].flatten
        else
          [:right, @x_middle, :left].flatten
        end
      else
        @last_event_pos = get_event_position
        determine_middle🙂y)
        if diff_y > 0
          [:up, @y_middle, :down].flatten
        else
          [:down, @y_middle, :up].flatten
        end
      end
    end
    
    #
    # So if the first choice is to move up, but we can't because up is
    # blocked, we want to move either left or right, but not down.
    # This method randomizes the left or right decision and caches
    # the result
    #
    def determine_middle(move_axis)
      case move_axis
      when :x
        @y_middle = nil
        @x_middle = [:up, :down].shuffle unless @x_middle
      when :y
        @x_middle = nil
        @y_middle = [:left, :right].shuffle unless @y_middle
      end
    end
        
    #
    # Returns the first valid move from an array of moves
    #
    def get_valid_move(moves)
      moves.each do |move|
        return move if @event.lm_can_move?(move) or @event.lm_touch_player?(move)
      end
    end
    
    def get_event_position
      LufiaMovementHelper:😛oint.new(@event.x, @event.y)
    end
  end
  
  #
  # Moves towards the player
  #
  class ApproachMovement < LufiaCommonMovement
    def initialize(event)
      super(event)
    end
  
    def move_once
      moves = get_approach_moves
      valid = get_valid_move(moves)
      move_in_direction(valid)
    end
  end
  
  #
  # Moves randomly
  #
  class RandomMovement < LufiaCommonMovement
    def initialize(event)
      super(event)
    end
        
    def move_once
      [:up, :down, :left, :right].shuffle.each do |move|
        if @event.lm_can_move?(move)
          move_in_direction(move)
          break
        end
      end
    end
  end
  
  #
  # Moves away from the player (the opposite of approach)
  #
  class FleeMovement < LufiaCommonMovement
    def initialize(event)
      super(event)
      @x_adjust, @width_adjust = 1, -1
      @y_adjust, @height_adjust = 1, -1
    end
    
    def move_once
      moves = get_approach_moves.reverse
      valid = get_valid_move(moves)
      move_in_direction(valid)
    end
  end
  
  #
  # Moves opposite to the player
  # Note: if an event is on the same visible area as the player when
  # the game starts, the event will not move the first time the player
  # moves. This is because $game_player has not recorded the starting
  # position of the player. Possible fix: compare to player start pos
  # somehow.
  #
  class OppositeMovement < LufiaCommonMovement
    def initialize(event)
      super(event)
    end
    
    def move_once
      @last_player_pos ||= get_player_position
      move = get_opposite_move
      if move
        move_in_direction(move)
        @last_successful_move = move
      else
        move_in_direction(@last_successful_move)
      end
      @last_player_pos = get_player_position
    end
    
    def get_player_position
      LufiaMovementHelper:😛oint.new($game_player.x, $game_player.y)
    end
    
    #
    # Returns a move that is opposite to the one the direction the player
    # just moved in.
    #
    def get_opposite_move
      old_x, old_y = @last_player_pos.x, @last_player_pos.y
      new_x, new_y = $game_player.x, $game_player.y
      diff_x = new_x - old_x
      diff_y = new_y - old_y
      
      if diff_x > 0
        return :left
      elsif diff_x < 0
        return :right
      end
      if diff_y > 0
        return :up
      elsif diff_y < 0
        return :down
      end
      return nil
    end
  end
end


#
# Helper classes and monkey patches
#


module LufiaMovementHelper
  class Point
    attr_accessor :x
    attr_accessor :y
    
    def initialize(x, y)
      @x = x
      @y = y
    end
    
    def ==(other)
      return false unless other.is_a?(Point)
      @x == other.x and @y == other.y
    end
  end
end


#
# The bulit in Rect class does not have a method to see if a coordinate
# is within the rect, so we add it here
#
class Rect
  def lm_include?(point)
    (x .. x + width) === point.x and (y .. y + height) === point.y
  end
end


#
# Ah the joys of working with a Ruby from like 2004 or something!
#
unless [].respond_to?🙂shuffle)
  class Array
    def shuffle
      new = []
      size.downto(1) { |n| new << delete_at(rand(n)) }
      new
    end
  end
end



Another script, written for RPG VX Ace accomplishes something closely related to the lufia movement script. The developer of that script no longer has the time or inclination to support it and has no concern if someone were to modify or convert it. That script is in the spoiler below here.

Spoiler
=begin
CSCA Event Movement
version: 1.3(April 23, 2012)
Created by: Casper Gaming (http://www.caspergaming.com/)


Compatibility:
Made for RPGVXAce
IMPORTANT: ALL CSCA Scripts should be compatible with each other unless
otherwise noted.


####Updates####
Version 1.1
-Increased compatibility
-Optimized
-Fixed fast random_no_wait move type and fast random move type.


Version 1.2
-Event commands no longer trigger if event is stunned(minus autorun/parallel).
-Able to respawn events temporarily erased without transferring player to
a different map, and return events to their starting position when transferring
from the same map.


Version 1.3
- Events will no longer pass through a set region ID, unless otherwise specified.
- Fixed events ignoring comments after save load.


FFEATURES:
This script updates event movement routes based on when the player moves. Move
routes for events not set up for use with this script are not affected. Compatible
with CSCA Dungeon Tools.


SETUP
This script requires setup further down.
Instructions included below.


SETTING UP YOUR EVENTS:
Step 1. Each event that you want to be effected by this script should have a
comment event command with text "MONSTER" in it.
Step 2. Each event should have a separate comment event command with one of the
move route types listed below.
Step 3. Each event should have a separate comment event command with one of the
speeds listed below. If no speed listed, normal speed is assumed.
Step 4(optional). Each event that you want to pass through the region ID set
below should contain a comment with text "NONMONSTER" in it.


MOVE ROUTE TYPES:
RANDOM is the same as the default random.
TOWARD is the same as the default toward.
CUSTOM is the same as the default custom. CUSTOM will ignore a FAST speed.
OPPOSITE will make the event move opposite of the player.
LEFT_RIGHT will make the event move left or right randomly.
UP_DOWN will make the event move up or down randomly.
SAME will make the event move the same as the player.
RANDOM_NO_WAIT is the same as the default random, without the possibility of no
               movement.
AVOID will make the event try to avoid contact with the player.


SPEED TYPES:
No speed type will make the event move once every step the player takes.
FAST will make the event move twice every step the player takes. Will move once
     every step the player takes for move route type: CUSTOM
SLOW will make the event move once every 2 steps the player takes.
VERY_SLOW will make the event move once every 4 steps the player takes.


Please note that it is recommended to set the event speed higher if using a
faster speed, and lower if using a slow speed so the event can keep up with
the player.


####SCRIPT CALLS####
HOW TO CHECK IF SURPRISE/PREEMPTIVE STRIKE
Before the battle processing event command, make this script call:
$game_map.csca_back_side_attack(x)
where x is the event's ID.


This is not cleared at the end of battle, so if you have an event that you do
not wish to check if it's a surprise/preemptive strike, you'll need to make this
script call:
$game_map.csca_clear_modify


HOW TO STUN AN EVENT
Make this script call:
$game_map.csca_stun_event(x, y)
where x would be the number of steps you want it to be stunned for
and y would be the event ID.


HOW TO RE-CHECK FOR MONSTER EVENTS
Let's say you turn a monster event on by switch. This script will not recognize
it automatically as this script merely checks for monster events afetr every
transfer. To check for monster events without transfer, make this script call:
$game_map.csca_get_monster_events
make sure there is a few frames wait time between when the new monster event is
enabled and the script call.


CREDIT:
Free to use in noncommercial games if credit is given to:
Casper Gaming (http://www.caspergaming.com/)


To use in a commercial game, please purchase a license here:
http://www.caspergaming.com/licenses.html


TERMS:
http://www.caspergaming.com/terms_of_use.html
=end
module CSCA_EVENT_MOVEMENT # Don't touch this.
  
  # Enter any number between 0 and 1 for these values.
  BEHIND_SURPRISE = 1 # Amount to add to the surprise rate if event attacks
                      # from behind player.
  SIDE_SURPRISE = 0.6 # Amount to add to surprise rate if event attacks from
                      # player's side.
  BEHIND_PREEMPTIVE = 1 # Amount to add to the preemptive rate if player attacks
                        # from behind event.
  SIDE_PREEMPTIVE = 0.6 # Amount to add to the preemptive rate if player attacks
                        # from event's side.
                        
  UNERASE = true # True = events respawn if they were erased even if the player
                 # transfers to the same map. False will make events not respawn
                 # until normal circumstances.
                 
  RETURN_EVENT = true # True makes it so that events will return to their original
                      # starting position even if the player transfers to the same
                      # map.
                      
  NOMOVE_REGION = 16 # Region ID that event's will not move into. Useful for
                    # creating barriers that you do not want events to move out
                    # of.
  DENY_THROUGH = true # Disallow "Through" events the ability to move through
                       # the NOMOVE_REGION specified above?


end # END SETUP! Don't touch this or anything below.


$imported = {} if $imported.nil?
$imported["CSCA-EventMovement"] = true


class Game_Map
  include CSCA_EVENT_MOVEMENT
  
  alias csca_em_refresh refresh
  def refresh
    csca_get_monster_events
    csca_em_refresh
  end
  
  def csca_samemap_transfer_setup(map_id)
    events.each do |i, event|
      if event.csca_erased?
        event.csca_unerase if UNERASE
      else
        event.csca_return_start_pos if RETURN_EVENT
      end
    end
  end
  
  def csca_stun_event(steps, id)
    events.each do |i, event|
      if event.id == id
        event.csca_stun(steps)
      end
    end
  end
  
  def csca_get_monster_events
    events.each do |i, event|
      if event.list != nil
        for i in 0...event.list.size
          if event.list.code == 108
            if event.list.parameters == ["MONSTER"]
              @monster_event = event
              @monster_event.csca_set_event_steps
              csca_set_movement_type(@monster_event)
            end
            if event.list.parameters == ["NONMONSTER"]
              event.csca_set_region_ignore
            end
          end
        end
      end
    end
    @monster_events_setup = true
    @allow_modify = false
  end
  
  def csca_set_movement_type(event)
    for i in 0...event.list.size
      if event.list.code == 108
        if event.list.parameters == ["RANDOM"]
          event.csca_set_move_type(4)
        elsif event.list.parameters == ["TOWARD"]
          event.csca_set_move_type(5)
        elsif event.list.parameters == ["CUSTOM"]
          event.csca_set_move_type(6)
        elsif event.list.parameters == ["OPPOSITE"]
          event.csca_set_move_type(7)
        elsif event.list.parameters == ["LEFT_RIGHT"]
          event.csca_set_move_type(8)
        elsif event.list.parameters == ["UP_DOWN"]
          event.csca_set_move_type(9)
        elsif event.list.parameters == ["SAME"]
          event.csca_set_move_type(10)
        elsif event.list.parameters == ["RANDOM_NO_WAIT"]
          event.csca_set_move_type(11)
        elsif event.list.parameters == ["AVOID"]
          event.csca_set_move_type(12)
        end
        if event.list.parameters == ["FAST"]
          event.csca_set_move_speed(3)
        elsif event.list.parameters == ["SLOW"]
          event.csca_set_move_speed(2)
        elsif event.list.parameters == ["VERY_SLOW"]
        event.csca_set_move_speed(1)
        end
      end
    end
  end
  
  def csca_back_side_attack(event_id)
    events.each do |i, event|
      if event.id == event_id
        csca_check_surprise(event)
        csca_check_preemptive(event)
      end
    end
  end
  
  def csca_check_surprise(event)
    d1 = event.direction
    x1 = event.x
    y1 = event.y
    d2 = $game_player.direction
    x2 = $game_player.x
    y2 = $game_player.y
    if d1 == 2 && d2 == 2 && y1 == (y2 - 1)
      csca_alter_surprise(BEHIND_SURPRISE)
    elsif d1 == 4 && d2 == 4 && x1 == (x2 + 1)
      csca_alter_surprise(BEHIND_SURPRISE)
    elsif d1 == 6 && d2 == 6 && x1 == (x2 - 1)
      csca_alter_surprise(BEHIND_SURPRISE)
    elsif d1 == 8 && d2 == 8 && y1 == (y2 + 1)
      csca_alter_surprise(BEHIND_SURPRISE)
    elsif d1 == 2 && (d2 == 4 || d2 == 6) && y1 == (y2 - 1)
      csca_alter_surprise(SIDE_SURPRISE)
    elsif d1 == 8 && (d2 == 4 || d2 == 6) && y1 == (y2 + 1)
      csca_alter_surprise(SIDE_SURPRISE)
    elsif d1 == 4 && (d2 == 2 || d2 == 8) && x1 == (x2 + 1)
      csca_alter_surprise(SIDE_SURPRISE)
    elsif d1 == 6 && (d2 == 2 || d2 == 8) && x1 == (x2 - 1)
      csca_alter_surprise(SIDE_SURPRISE)
    else
      csca_alter_surprise(0)
    end
  end
  
  def csca_enable_modify
    @allow_modify = true
  end
  
  def csca_disable_modify
    @allow_modify = false
  end
  
  def csca_allow_modify?
    @allow_modify
  end
  
  def csca_alter_surprise(amount)
    @surprise_modify = amount
  end
  
  def csca_surprise_modify_amount
    @surprise_modify
  end
  
  def csca_check_preemptive(event)
    d1 = csca_prelock_direction
    x1 = event.x
    y1 = event.y
    d2 = $game_player.direction
    x2 = $game_player.x
    y2 = $game_player.y
    if d1 == 2 && d2 == 2 && y2 == (y1 - 1)
      csca_alter_preemptive(BEHIND_PREEMPTIVE)
    elsif d1 == 4 && d2 == 4 && x2 == (x1 + 1)
      csca_alter_preemptive(BEHIND_PREEMPTIVE)
    elsif d1 == 6 && d2 == 6 && x2 == (x1 - 1)
      csca_alter_preemptive(BEHIND_PREEMPTIVE)
    elsif d1 == 8 && d2 == 8 && y2 == (y1 + 1)
      csca_alter_preemptive(BEHIND_PREEMPTIVE)
    elsif d2 == 2 && (d1 == 4 || d1 == 6) && y2 == (y1 - 1)
      csca_alter_preemptive(SIDE_PREEMPTIVE)
    elsif d2 == 8 && (d1 == 4 || d1 == 6) && y2 == (y1 + 1)
      csca_alter_preemptive(SIDE_PREEMPTIVE)
    elsif d2 == 4 && (d1 == 2 || d1 == 8) && x2 == (x1 + 1)
      csca_alter_preemptive(SIDE_PREEMPTIVE)
    elsif d2 == 6 && (d1 == 2 || d1 == 8) && x2 == (x1 - 1)
      csca_alter_preemptive(SIDE_PREEMPTIVE)
    else
      csca_alter_preemptive(0)
    end
  end
  
  def csca_alter_preemptive(amount)
    @preemptive_modify = amount
  end
  
  def csca_preemptive_modify_amount
    @preemptive_modify
  end
  
  def csca_set_prelock_direction(value)
    @preemptive_direction = value
  end
  
  def csca_prelock_direction
    @preemptive_direction
  end
  
  def csca_clear_modify
    @surprise_modify = @preemptive_modify = 0
  end
  
  def csca_em_region_collision_check(x, y)
    region_id(x, y) == NOMOVE_REGION
  end
  
end
class Game_CharacterBase
  
  def passable?(x, y, d)
    x2 = $game_map.round_x_with_direction(x, d)
    y2 = $game_map.round_y_with_direction(y, d)
    return false unless $game_map.valid?(x2, y2)
    return false if csca_collide_with_region?(x2, y2) && CSCA_EVENT_MOVEMENT:😀ENY_THROUGH
    return true if @through || debug_through?
    return false unless map_passable?(x, y, d)
    return false unless map_passable?(x2, y2, reverse_dir(d))
    return false if collide_with_characters?(x2, y2)
    return false if csca_collide_with_region?(x2, y2)
    return true
  end
  
  def csca_collide_with_region?(x, y)
    self.is_a?(Game_Event) ? self.region_ignore? ? false : $game_map.csca_em_region_collision_check(x, y) : false
  end
  
end
class Game_Event < Game_Character
  
  alias csca_initialize initialize
  def initialize(map_id, event)
    super()
    csca_initialize(map_id, event)
    init_csca_event_movement(@event.x, @event.y)
  end
  
  def init_csca_event_movement(x, y)
    @starting_x = @event.x
    @starting_y = @event.y
    @csca_move_speed = 0
    @stun_amount = 0
    @region_ignore = false
  end
  
  def csca_erased?
    @erased
  end
  
  def csca_unerase
    @erased = false
    moveto(@starting_x, @starting_y)
    refresh
  end
  
  def csca_return_start_pos
    moveto(@starting_x, @starting_y)
  end
  
  def lock
    unless @locked
      @prelock_direction = @direction
      $game_map.csca_set_prelock_direction(@direction)
      turn_toward_player
      @locked = true
    end
  end


  def update_stop
    super
    update_self_movement unless (@csca_move_speed != 3 && @move_route_forcing) || csca_stunned?
    if csca_stunned?
      @stun_amount -= 1 if @csca_event_steps != $game_party.steps
      @csca_event_steps = $game_party.steps
      $game_map.refresh
    end
  end
  
  def csca_set_region_ignore
    @region_ignore = true
  end
  
  def region_ignore?
    @region_ignore
  end
  
  def csca_set_move_type(type)
    @move_type = type
  end
  
  def csca_set_move_speed(speed)
    @csca_move_speed = speed
  end
  
  alias csca_update_self_movement update_self_movement
  def update_self_movement
    csca_update_self_movement
    if @stop_count > stop_count_threshold && @csca_event_steps != $game_party.steps
      speed = @csca_move_speed
      steps = @csca_event_steps
      if speed == 0 || speed == 3 || (speed == 2 && (steps % 2) == 0) || (speed == 1 && (steps % 4) == 0)
        case @move_type
        when 4; speed == 3 ? fast_csca_move_type_random : move_type_random
        when 5; speed == 3 ? fast_csca_move_type_toward_player : move_type_toward_player
        when 6; move_type_custom
        when 7; csca_move_type_opposite
        when 8; csca_move_type_lr
        when 9; csca_move_type_ud
        when 10; csca_move_type_same
        when 11; speed == 3 ? fast_csca_move_type_random_nw : csca_move_type_random
        when 12; speed == 3 ? fast_csca_move_type_avoid : csca_move_type_avoid
        end
      end
      @csca_event_steps = $game_party.steps
    end
  end
  
  def csca_set_event_steps
    @csca_event_steps = $game_party.steps
  end
  
  def csca_move_type_opposite
    new_direction = 10 - $game_player.direction
    set_direction(new_direction)
    @csca_move_speed == 3 ? csca_fast_move_forward : move_forward
  end
  
  def csca_move_type_lr
    set_direction(4) if @direction == 2 || @direction == 8
    case rand(3)
    when 0;  set_direction(4)
    when 1;  set_direction(6)
    end
    @csca_move_speed == 3 ? csca_fast_move_forward : move_forward
  end
  
  def csca_move_type_ud
    set_direction(2) if @direction == 4 || @direction == 6
    case rand(3)
    when 0;  set_direction(2)
    when 1;  set_direction(8)
    end
    @csca_move_speed == 3 ? csca_fast_move_forward : move_forward
  end
  
  def csca_move_type_same
    new_direction = $game_player.direction
    set_direction(new_direction)
    @csca_move_speed == 3 ? csca_fast_move_forward : move_forward
  end
  
  def csca_move_type_random
    case rand(5)
    when 0..1;  move_random
    when 2..4;  move_forward
    end
  end
  
  def csca_move_type_avoid
    if near_the_player?
      case rand(6)
      when 0..3;  move_away_from_player
      when 4;     move_random
      when 5;     move_forward
      end
    else
      move_random
    end
  end
  
  def fast_csca_move_type_random
    case rand(6)
    when 0..1;  code1 = 12; code1p = []
    when 2..4;  code1 = 9; code1p = []
    when 5; code1 = 15; code1p = [15]
    end
    case rand(6)
    when 0..1;  code2 = 12; code2p = []
    when 2..4;  code2 = 9; code2p = []
    when 5; code2 = 15; code2p = [15]
    end
    csca_execute_fast_move_route(code1, code2, code1p, code2p)
  end
  
  def fast_csca_move_type_toward_player
    if near_the_player?
      case rand(6)
      when 0..3; code1 = 10
      when 4; code1 = 9
      when 5; code1 = 12
      end
      case rand(6)
      when 0..3; code2 = 10
      when 4; code2 = 9
      when 5; code2 = 12
      end
    else
      code1 = 9
      code2 = 9
    end
    csca_execute_fast_move_route(code1, code2)
  end
  
  def fast_csca_move_type_random_nw
    case rand(5)
    when 0..1;  code1 = 12
    when 2..4;  code1 = 9
    end
    case rand(5)
    when 0..1;  code2 = 12
    when 2..4;  code2 = 9
    end
    csca_execute_fast_move_route(code1, code2)
  end
  
  def fast_csca_move_type_avoid
    if near_the_player?
      case rand(6)
      when 0..3; code1 = 11
      when 4..5; code1 = 9
      end
      case rand(6)
      when 0..3; code2 = 11
      when 4..5; code2 = 9
      end
    else
      code1 = 9
      code2 = 9
    end
    csca_execute_fast_move_route(code1, code2)
  end
  
  def csca_fast_move_forward
    mr = RPG::MoveRoute.new
    mr.skippable = true
    mr.repeat = false
    mr.wait = false
    mc = RPG::MoveCommand.new
    mc.code = 12
    mc.parameters = []
    mc2 = RPG::MoveCommand.new
    mc2.code = 12
    mc2.parameters = []
    mr.list[0] = mc
    mr.list[1] = mc2
    force_move_route(mr)
  end
  
  def csca_execute_fast_move_route(code1, code2, code1p = [], code2p = [])
    mr = RPG::MoveRoute.new
    mr.skippable = true
    mr.repeat = false
    mr.wait = false
    mc = RPG::MoveCommand.new
    mc.code = code1
    mc.parameters = code1p
    mc2 = RPG::MoveCommand.new
    mc2.code = code2
    mc2.parameters = code2p
    mr.list[0] = mc
    mr.list[1] = mc2
    force_move_route(mr)
  end
  
  def csca_stun(steps)
    @stun_amount = steps
  end
  
  def csca_stunned?
    @stun_amount > 0
  end
  
end
class Game_Player < Game_Character
  
  alias csca_clear_movement_info_on_transfer clear_transfer_info
  def clear_transfer_info
    csca_clear_movement_info_on_transfer
    $game_map.csca_get_monster_events
  end
  
end
class Game_Party < Game_Unit
  
  alias csca_preemptive rate_preemptive
  def rate_preemptive(troop_agi)
    csca_preemptive(troop_agi)
    (agi >= troop_agi ? 0.05 : 0.03) * (raise_preemptive? ? 4 : 1) +
    ($game_map.csca_allow_modify? ? $game_map.csca_preemptive_modify_amount.to_f : 0)
  end
  
  alias csca_surprise rate_surprise
  def rate_surprise(troop_agi)
    csca_surprise(troop_agi)
    cancel_surprise? ? 0 : (agi >= troop_agi ? 0.03 : 0.05) +
    ($game_map.csca_allow_modify? ? $game_map.csca_surprise_modify_amount.to_f : 0)
  end
  
end
class Game_Interpreter
  
  def command_301
    return if $game_party.in_battle
    $game_map.csca_enable_modify
    if @params[0] == 0
      troop_id = @params[1]
    elsif @params[0] == 1
      troop_id = $game_variables[@params[1]]
    else
      troop_id = $game_player.make_encounter_troop_id
    end
    if $data_troops[troop_id]
      BattleManager.setup(troop_id, @params[2], @params[3])
      BattleManager.on_encounter
      BattleManager.event_proc = Proc.new {|n| @branch[@indent] = n }
      $game_player.make_encounter_count
      SceneManager.call(Scene_Battle)
    end
    Fiber.yield
  end
  
end
class Game_Player < Game_Character
  
  def encounter
    return false if $game_map.interpreter.running?
    return false if $game_system.encounter_disabled
    return false if @encounter_count > 0
    make_encounter_count
    troop_id = make_encounter_troop_id
    return false unless $data_troops[troop_id]
    $game_map.csca_disable_modify
    BattleManager.setup(troop_id)
    BattleManager.on_encounter
    return true
  end
  
  alias csca_start_map_event start_map_event
  def start_map_event(x, y, triggers, normal)
    $game_map.events_xy(x, y).each do |event|
      return if event.csca_stunned?
    end
    csca_start_map_event(x, y, triggers, normal)
  end
  
  def perform_transfer
    if transfer?
      set_direction(@new_direction)
      if @new_map_id != $game_map.map_id
        $game_map.setup(@new_map_id)
        $game_map.autoplay
      else
        $game_map.csca_samemap_transfer_setup(@new_map_id)
      end
      moveto(@new_x, @new_y)
      clear_transfer_info
    end
  end
  
end

 


It would be very nice if such a script became publicly available for the community. I intended to put a request in for something like this eventually, but I'm not working on that title at this time and so it wasn't pursued.


 


I hope this helps!


P.S.


When you're asking for support with javascript you'll want to post your questions in the Javascript Boards -> Javascript/Plugin Support forum. If you post in the wrong area, just 'report' your own thread and ask for help moving it to the correct forum. ^^

Original thread: https://forums.rpgmakerweb.com/threads/making-enemy-events-only-move-when-player-moves.55222/