Advanced Map Encounters

This script allows you to assign something that always bothered me that was not in the RPG maker by default: Make on a map some enemy encounters more likely than others. There are other scripts that fix this, but I wanted to make one simple and as intuitive as possible, apart from putting some unique things on it.

The map encounters still work basically as always, I simplified the encounter formula (now it's a bit more random when you will encounter them, but just as likely after all), but the important thing is that you can assign encounters by hand using an event controlling the steps necessary for that meeting. Allowing encounters or not also works the same, as does pressing CTRL in Debug mode.

Author: Wecoc
First Release: October 2016
Last Version: November 2017

Terms of use
- Giving credits (Wecoc) is optional, but appreciated.
- You can repost this code.
- You can edit this code freely, and distribute the edits as well.
- This code can be used on commercial games.

Script

Ruby:
#==============================================================================
# ** Advanced Map Encounters v1.2
#------------------------------------------------------------------------------
#  Author: Wecoc - No credits required
#==============================================================================

#==============================================================================
# ** RPG encounter rates
#==============================================================================

module RPG
  for Klass in [Actor, Item, Weapon, Armor, State]
    class Klass
      attr_accessor :encounter_rate
      define_method(:encounter_rate) { @encounter_rate ||= 1.0}
    end
  end
end

#==============================================================================
# ** Game_Actor
#==============================================================================

class Game_Actor < Game_Battler
  def encounter_rate
    result = $data_actors[@actor_id].encounter_rate
    weapon = $data_weapons[@weapon_id]
    armor1 = $data_armors[@armor1_id]
    armor2 = $data_armors[@armor2_id]
    armor3 = $data_armors[@armor3_id]
    armor4 = $data_armors[@armor4_id]
    result *= weapon.encounter_rate if weapon != nil
    result *= armor1.encounter_rate if armor1 != nil
    result *= armor2.encounter_rate if armor2 != nil
    result *= armor3.encounter_rate if armor3 != nil
    result *= armor4.encounter_rate if armor4 != nil
    for state in @states
      state = $data_states[state]
      result *= state.encounter_rate if state != nil
    end
    return result
  end
end

#==============================================================================
# ** Game_Map
#==============================================================================

class Game_Map
  attr_reader :encounters
  #--------------------------------------------------------------------------
  # * Setup
  #--------------------------------------------------------------------------
  alias adv_encounter_setup setup unless $@
  def setup(map_id)
    adv_encounter_setup(map_id)
    @encounters = []
    # Define the default map encounters
    encounter_list = @map.encounter_list
    encounter_steps = @map.encounter_step * encounter_list.size
    for troop_id in encounter_list
      set_encounter(troop_id, encounter_steps, nil)
    end
  end
  #--------------------------------------------------------------------------
  # * Clear default encounter list
  #--------------------------------------------------------------------------
  def encounter_list
    return []
  end
  #--------------------------------------------------------------------------
  # * Clear steps for default encounter
  #--------------------------------------------------------------------------
  def encounter_step
    return 0
  end
  #--------------------------------------------------------------------------
  # * Set new encounter
  #--------------------------------------------------------------------------
  def set_encounter(troop_id, steps, area=nil)
    steps = [[1, steps].max, 999].min
    @encounters.push([troop_id, 1.0 / steps, area])
    @encounters.uniq!
    @encounters.sort!{|b,a| b[1] - a[1]}
  end
  #--------------------------------------------------------------------------
  # * Clear encounter
  #--------------------------------------------------------------------------
  def clear_encounter(troop_id, steps, area=nil)
    steps = [[1, steps].max, 999].min
    @encounters.delete!([troop_id, 1.0 / steps, area])
  end
end

#==============================================================================
# ** Game_Player
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Increase steps
  #--------------------------------------------------------------------------
  alias adv_encounter_steps increase_steps unless $@
  def increase_steps
    adv_encounter_steps
    return if $DEBUG && Input.press?(Input::CTRL)
    return if $game_system.encounter_disabled == true
    rate = 1.0
    for actor in $game_party.actors
      rate *= actor.encounter_rate
    end
    for i in 1...$data_items.size
      if $game_party.item_number(i) > 0
        $game_party.item_number(i).times do
          rate *= $data_items[i].encounter_rate
        end
      end
    end
    return if rate == 0
    $game_map.encounters.each do |encounter|
      get_encounter_data(encounter, rate)
      return if $game_temp.battle_calling
    end
  end
  #--------------------------------------------------------------------------
  # * Evaluate the info on each encounter
  #--------------------------------------------------------------------------
  def get_encounter_data(encounter, rate)
    troop_id = encounter[0]
    return if $data_troops[troop_id] == nil
    ratio = encounter[1] * (1.0 / rate)
    encounter_area = encounter[2]
    if Game_Player.method_defined?(:area) && encounter_area != nil
      next if self.area != encounter_area
    end
    if rand < ratio
      # Empezar batalla
      $game_temp.battle_calling = true
      $game_temp.battle_troop_id = troop_id
      $game_temp.battle_can_escape = true
      $game_temp.battle_can_lose = false
      $game_temp.battle_proc = nil
    end
  end
end

#==============================================================================
# ** Game_Temp
#==============================================================================

class Game_Temp
  attr_accessor :force_battleback
  alias area_battleback_ini initialize unless $@
  def initialize
    area_battleback_ini
    @force_battleback = false
  end
end

#==============================================================================
# ** Game_Map (Battleback Areas)
#==============================================================================

class Game_Map
  
  BATTLEBACK_AREAS = {} # {AREA ID => 'BATTLEBACK NAME'}
  
  def battleback_name
    return @battleback_name if $game_temp.force_battleback == true
    area = $game_player.area rescue 0
    battleback_area = BATTLEBACK_AREAS[area]
    return battleback_area if !battleback_area.nil?
    return @battleback_name
  end
end


Instructions
To set an encounter from an event use:
$game_map.set_encounter(Troop ID, Steps) (Steps must be between 1 and 999)

If that encounter is already defined and you want to remove it, use:
$game_map.clear_encounter(Troop ID, Steps)

Spoiler: Area
If you are using Area Support, you can assign encounters by area.
$game_map.set_encounter(Troop ID, Steps, Area ID)

You can also assign battlebacks by area.

To do that, search this line:
BATTLEBACK_AREAS = {}

And set the battleback there by area ID, like this:
BATTLEBACK_AREAS = {1 => '001-Grassland01'}

Finally, you can control in-game if that's being considered or if you want to use the default battleback. To do that use:
$game_temp.force_battleback = true / false

Spoiler: Encounter rate
You can set encounter rates on certain actors, items, weapons, protectors or states.

$data_actors[ID].encounter_rate = 1.0
$data_items[ID].encounter_rate = 1.0
$data_weapons[ID].encounter_rate = 1.0
$data_armors[ID].encounter_rate = 1.0
$data_states[ID].encounter_rate = 1.0

Spoiler: Encounter conditions
This little Addon allows defining some advanced encounter conditions.

Ruby:
#==============================================================================
# ** [XP] Encounter Conditions
#------------------------------------------------------------------------------
# Author: Wecoc (no credits required)
# This script requires the script Advanced Map Encounters
#==============================================================================

class Game_Map
  def set_encounter(troop_id, steps, area=nil, condition=nil)
    steps = [[1, steps].max, 999].min
    @encounters.push([troop_id, 1.0 / steps, area, condition])
    @encounters.uniq!
    @encounters.sort!{|b,a| b[1] - a[1]}
  end
  def clear_encounter(troop_id, steps, area=nil, condition=nil)
    steps = [[1, steps].max, 999].min
    @encounters.delete!([troop_id, 1.0 / steps, area, condition])
  end
end

class Game_Player
  alias condition_encounter_data get_encounter_data unless $@
  def get_encounter_data(encounter, rate)
    if encounter[3] != nil
      return if eval(encounter[3]) == false
    end
    condition_encounter_data(encounter, rate)
  end
end

It works as before, but with a new optional argument on the callers:
$game_map.set_encounter(Troop ID, Steps, Area, Condition)

Example:
$game_map.set_encounter(1, 20, nil, "$game_switches[1] == true")

Original thread: https://forums.rpgmakerweb.com/threads/advanced-map-encounters.139675/