class Game_FollowingPkmn < Game_Follower
  def self.justCameOut
    @@justCameOut = true
  end
  def follow_leader(leader, instant = false, leaderIsTrueLeader = true)
    maps_connected = $map_factory.areConnected?(leader.map.map_id, self.map.map_id)
    target = nil
    
    if !defined?(@@justCameOut)
      @@justCameOut = false
    end
    
    # Get the target tile that self wants to move to
    if maps_connected
      behind_direction = 10 - leader.direction
      target = $map_factory.getFacingTile(behind_direction, leader)
      
      if !passable?(target[1],target[2], behind_direction) && @@justCameOut == true
        4.times do |i|
          target = $map_factory.getFacingTile(i*2, leader)
          if passable?(target[1],target[2], leader.direction)
            #if the direction is passable, don't bother checking other directions
            break
          end
        end #4.times do
      end #if !passable?
        
      @@justCameOut = false
      
      if target && $map_factory.getTerrainTag(target[0], target[1], target[2]).ledge
        # Get the tile above the ledge (where the leader jumped from)
        target = $map_factory.getFacingTileFromPos(target[0], target[1], target[2], behind_direction)
      end
      target = [leader.map.map_id, leader.x, leader.y] if !target
      if GameData::TerrainTag.exists?(:StairLeft)
        currentTag = $map_factory.getTerrainTag(self.map.map_id, self.x, self.y)
        if currentTag == :StairLeft
          target[2] += (target[1] > $game_player.x ? -1 : 1)
        elsif currentTag == :StairRight
          target[2] += (target[1] < $game_player.x ? -1 : 1)
        end
      end
      # Added
      if defined?(on_stair?) && on_stair?
        if leader.on_stair?
          if leader.stair_start_x != self.stair_start_x
            # Leader stepped on other side so start/end swapped, but not for follower yet
            target[2] = self.y
          elsif leader.stair_start_x < leader.stair_end_x
            # Left to Right
            if leader.x < leader.stair_start_x && self.x != self.stair_start_x
              # Leader stepped off
              target[2] = self.y
            end
          elsif leader.stair_end_x < leader.stair_start_x
            # Right to Left
            if leader.x > leader.stair_start_x && self.x != self.stair_start_x
              # Leader stepped off
              target[2] = self.y
            end
          end
        elsif self.on_middle_of_stair?
          # Leader is no longer on stair but follower is, so player moved up or down at the start or end of the stair
          if leader.y < self.stair_end_y - self.stair_y_height + 1 || leader.y > self.stair_end_y
            target[2] = self.y
          end
        end
      end
    else
      # Map transfer to an unconnected map
      target = [leader.map.map_id, leader.x, leader.y]
    end
    # Move self to the target
    if self.map.map_id != target[0]
      vector = $map_factory.getRelativePos(target[0], 0, 0, self.map.map_id, @x, @y)
      @map = $map_factory.getMap(target[0])
      # NOTE: Can't use moveto because vector is outside the boundaries of the
      #       map, and moveto doesn't allow setting invalid coordinates.
      @x = vector[0]
      @y = vector[1]
      @real_x = @x * Game_Map::REAL_RES_X
      @real_y = @y * Game_Map::REAL_RES_Y
    end
    if instant || !maps_connected
      moveto(target[1], target[2])
    else
      fancy_moveto(target[1], target[2], leader)
    end
  end
  #-----------------------------------------------------------------------------
end
module FollowingPkmn
  def self.toggle(forced = nil, anim = nil)
    return if !FollowingPkmn.can_check? || !FollowingPkmn.get
    return if !FollowingPkmn.get_pokemon
    anim_1 = FollowingPkmn.active?
    if !forced.nil?
      # This may seem redundant but it keeps follower_toggled a boolean always
      $PokemonGlobal.follower_toggled = !(!forced)
    else
      $PokemonGlobal.follower_toggled = !($PokemonGlobal.follower_toggled)
    end
    
    #added by Gardenette
    Game_FollowingPkmn.justCameOut
    
    anim_2 = FollowingPkmn.active?
    anim = anim_1 != anim_2 if anim.nil?
    FollowingPkmn.refresh(anim)
    $game_temp.followers.move_followers
    $game_temp.followers.turn_followers
  end
end