#==============================================================================
# 
# ▼ Craze's Script Asylum - Terrain Encounter Rates v1.00
# -- Last Updated: 2012.01.03
# -- Level: Normal
# -- Requires: n/a
# -- Special thanks: Yanfly, for script info layout and eternal help
# 
#==============================================================================

$imported = {} if $imported.nil?
$imported["CRZ-TerrainEncounterRates"] = true

#==============================================================================
# ▼ Updates
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# 2012.01.03 - Started Script and Finished.
# 
#==============================================================================
# ▼ Introduction
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Allowing you both modify default encounter rate changes and set a tile's
# unique encounter rate, you can now remove ship battles, make hills and forests
# very dangerous, or have different parts of your world map use different rates.
# This script binds changes in the encounter rate to individual tiles through
# usage of notetags and terrain tags.
# 
#==============================================================================
# ▼ Instructions
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# To install this script, open up your script editor and copy/paste this script
# to an open slot below ▼ Materials/素材 but above ▼ Main. Remember to save.
# 
# -----------------------------------------------------------------------------
# Tileset Notetags - These notetags go in the tileset notebox in the database.
# -----------------------------------------------------------------------------
# <enc rate x: y%>
# x = terrain id
# y% = percentage change in encounter rate
# If a terrain id was set to 200%, then it would have double the encounters than
# normal. (Note that bush/ship rates are applied as well, so by default, 200%
# on a tile marked as a bush in the database would have a 400% encounter rate!)
# 
#==============================================================================
# ▼ Compatibility
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# This script is made strictly for RPG Maker VX Ace. It is highly unlikely that
# it will run with RPG Maker VX without adjusting.
#
# Overwritten methods:
#   Game_Player
#      encounter_progress_value
#
#==============================================================================

module CRZ
  module TER_ENC_RATES
    
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # - Default Rate Settings -
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # All settings below are set to the RMVXAce defaults.
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # This is a modifier that determines how much terrain marked with "bush"
    # in the database affects encounter rate. Set to 1.0 to eliminate changes.
    BUSH_MOD = 2.0 # Default 2, or doubled encounter rate
    
    # This is a modifier that determines how much being in a ship affects
    # encounter rate. Set to 1.0 to eliminate changes, or 0 to have no battles
    # while in the ship.
    SHIP_MOD = 0.5 # Default 0.5, or halved encounter rate
    
  end # TER_ENC_RATES

#==============================================================================
# ▼ Editting anything past this point may potentially result in causing
# computer damage, incontinence, explosion of user's head, coma, death, and/or
# halitosis so edit at your own risk.
#==============================================================================
  
  module REGEXP
  module TILESET
    
    ENC_RATE = /<(?:ENC_RATE|enc rate)[ ](\d+):[ ](\d+)([%%])>/i
    
  end # TILESET
  end # REGEXP
end # CRZ

#==============================================================================
# ■ DataManager
#==============================================================================

module DataManager
  
  #--------------------------------------------------------------------------
  # alias method: load_database
  #--------------------------------------------------------------------------
  class <<self; alias load_database_crz_ter load_database; end
  def self.load_database
    load_database_crz_ter
    load_notetags_crz_ter
  end
  
  #--------------------------------------------------------------------------
  # new method: load_notetags_crz_ter
  #--------------------------------------------------------------------------
  def self.load_notetags_crz_ter
    groups = [$data_tilesets]
    for group in groups
      for obj in group
        next if obj.nil?
        obj.load_notetags_crz_ter
      end
    end
  end
  
end # DataManager

#==============================================================================
# ■ RPG::Tileset
#==============================================================================

class RPG::Tileset
  
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :enc_rate_changes
  
  #--------------------------------------------------------------------------
  # common cache: load_notetags_crz_ter
  #--------------------------------------------------------------------------
  def load_notetags_crz_ter
    @enc_rate_changes = {}
    #---
    self.note.split(/[\r\n]+/).each { |line|
      case line
      #---
      when CRZ::REGEXP::TILESET::ENC_RATE
        @enc_rate_changes = {
          $1.to_i => $2.to_i
          }
      #---
      end
    } # self.note.split
    #---
  end
  
end # RPG::Tileset

#==============================================================================
# ■ Game_Map
#==============================================================================

class Game_Map
  
  #--------------------------------------------------------------------------
  # new method: encounter_rate_change
  #--------------------------------------------------------------------------
  def encounter_rate_change(dx, dy)
    n = 1.00
    if valid?(dx, dy) && tileset.enc_rate_changes[terrain_tag(dx, dy)] != nil
      n *= tileset.enc_rate_changes[terrain_tag(dx, dy)]
      n /= 100
    end
    return n
  end
  
end # Game_Map


#==============================================================================
# ■ Game_Player
#==============================================================================
  
class Game_Player
  
  #--------------------------------------------------------------------------
  # overwrite method: encounter_progress_value
  #--------------------------------------------------------------------------
  def encounter_progress_value
    value = $game_map.bush?(@x, @y) ? CRZ::TER_ENC_RATES::BUSH_MOD : 1
    value *= 0.5 if $game_party.encounter_half?
    value *= CRZ::TER_ENC_RATES::SHIP_MOD if in_ship?
    value *= $game_map.encounter_rate_change(@x, @y)
    value
  end
  
end # Game_Player



#==============================================================================
# 
# ▼ End of File
# 
#==============================================================================