#==============================================================================
#  ExMap_EffectiveTile
#------------------------------------------------------------------------------
# ץ쥤`άFڵˤΥ ID ȡäơ`ʤɤ΄
# gФǤ褦ˤ륹ץزĤǤ
#==============================================================================

# mäޥå ID
# ؓɤXp뤿ᡢָ줿ޥåǤτI򥹥åפޤ
# Ф}ָǤޤ(ȫޥåפmäϤ [])
EXMAP_EFCTL_MIDS = []

# gФȤʤ륿 ID
# ޥåץ쥤`Сָ뤬ĤǤäƤȡ
# ˴줿HˡָեȤ䄿gФޤ
# Ф}ָǤޤ
EXMAP_EFCTL_TIDS = []

# oȤʤ륿 ID
# ޥåץ쥤`Сָ뤬ĤǤäƤС
# kӤȤʤ륿˴Ƥ⡢եȤ䄿όgФޤ
# Ф}ָǤޤ(ָʤϤ [])
EXMAP_EFCTL_VOID_TIDS = []

#  ID ȡäʹä ID
# ץ쥤`άFˤΥ ID 򡢥ޥåץ쥤`¤혤˴뤷ޤ
# Фǣָޤ(ָʤϤ [])
# ĤȤͬ ID ָȡһ¤Υ뤬뤵ޤ
# OȤvSʤgʹäǤޤΤǡ٥Ȥʤɤ˻äƤ
EXMAP_EFCTL_VIDS = [21, 22, 23]

# oƩ`ȤReС
# Ф [] ˺ޤƤ륹Ʃ`ȤӤƤ륢`ϡ
# Ȥʤ륿˴Ƥ⡢gݤoޤ
# ΥЩ`ˤäƥեȤgФ줿Ϥ⡢`ܤޤ
# `ʤɤOäƤ
EXMAP_EFCTL_RESIST_SIGNATURE = "*FLOOR_RESIST"

# gЕrΥ`
# ``Ӌ㷽 (0: 1:F HP Ρ 2: HP Ρ)
# LܤS (0:Sɤʤ 1:Sɤ) 혤ָƤ
# (ָʤϤ [])
EXMAP_EFCTL_DAMAGE = [5, 2, 0]

# kӕr SE
# SE ǰܥ`ࡢԥå혤ָƤ(ָʤϤ [])
EXMAP_EFCTL_SE = ["Damage1", 80, 100]

# kӕrλΥեå塣
#  (R)v (G) (B) (S)rg (1/60 ) 혤ָƤ
# (ָʤϤ [])
EXMAP_EFCTL_FLASH = [255, 0, 0, 64, 4]

#------------------------------------------------------------------------------

class Game_Map
  #--------------------------------------------------------------------------
  #   ID ȡ
  #--------------------------------------------------------------------------
  def tile_id(z = 0)
    return @map.data[$game_player.x, $game_player.y, z]
  end
  #--------------------------------------------------------------------------
  #  `ж
  #--------------------------------------------------------------------------
  def damage_floor?
    result = false
    for i in [2, 1, 0]
      return false if EXMAP_EFCTL_VOID_TIDS.include?(tile_id(i))
      result = true if EXMAP_EFCTL_TIDS.include?(tile_id(i))
    end
    return result
  end
end

class Game_Actor
  #--------------------------------------------------------------------------
  #  `oж
  #--------------------------------------------------------------------------
  def damage_floor_resist?
    for state in states
      return true if state.note.include?(EXMAP_EFCTL_RESIST_SIGNATURE)
      return true if state.id == 1 # Lܤϟoж
    end
    return false
  end
end

class Game_Party
  #--------------------------------------------------------------------------
  #  `΄m
  #--------------------------------------------------------------------------
  def damage_floor_effect
    unless EXMAP_EFCTL_DAMAGE.empty?
      damage = EXMAP_EFCTL_DAMAGE[0]
      effective = false
      for actor in members
        next if actor.damage_floor_resist?
        case EXMAP_EFCTL_DAMAGE[1]
        when 0
          actor.hp -= damage
        when 1
          actor.hp -= actor.hp * damage / 100
        when 2
          actor.hp -= actor.maxhp * damage / 100
        end
        actor.hp = 1 if actor.hp == 0 and EXMAP_EFCTL_DAMAGE[2] == 0
        effective = true
      end
    else
      effective = true
    end
    if effective and not EXMAP_EFCTL_SE.empty?
      a = EXMAP_EFCTL_SE
      RPG::SE.new(a[0], a[1], a[2]).play
    end
    if effective and not EXMAP_EFCTL_FLASH.empty?
      a = EXMAP_EFCTL_FLASH
      $game_map.screen.start_flash(Color.new(a[0], a[1], a[2], a[3]), a[4])
    end
  end
end

class Game_Player
  alias _exmefct_update update
  alias _exmefct_update_nonmoving update_nonmoving
  #--------------------------------------------------------------------------
  #  ե` (׷Ӷx)
  #--------------------------------------------------------------------------
  def update
    if moving? and not EXMAP_EFCTL_VIDS.empty?
      for i in [2, 1, 0]
        $game_variables[EXMAP_EFCTL_VIDS[i]] = $game_map.tile_id(i)
      end
    end
    _exmefct_update
  end
  #--------------------------------------------------------------------------
  #  ƄФǤʤϤ΄I (׷Ӷx)
  #     last_moving : ֱǰƄФä
  #--------------------------------------------------------------------------
  def update_nonmoving(last_moving)
    return if $game_map.interpreter.running?
    return if moving?
    # ٥Ӥꃞ
    if last_moving
      if EXMAP_EFCTL_MIDS.empty? or EXMAP_EFCTL_MIDS.include?($game_map.map_id)
        $game_party.damage_floor_effect if $game_map.damage_floor?
      end
    end
    _exmefct_update_nonmoving(last_moving)
  end
end

#==============================================================================
#  ǥХå撈
#------------------------------------------------------------------------------
# ǥХåˬFڵؤ׷ӱʾޤ
#==============================================================================

class Window_DebugLeft
  #--------------------------------------------------------------------------
  #  ֥ȳڻ (ٶx)
  #     x     : ɥ X 
  #     y     : ɥ Y 
  #--------------------------------------------------------------------------
  def initialize(x, y)
    super(x, y, 176, 272)
    self.index = 0
    refresh
  end
end

class Scene_Debug
  alias _exmefct_start start
  alias _exmefct_terminate terminate
  #--------------------------------------------------------------------------
  #  _ʼI (׷Ӷx)
  #--------------------------------------------------------------------------
  def start
    _exmefct_start
    @extra_window = Window_Base.new(0, 272, 176, 144)
    wlh = 24
    cw = @extra_window.width - 32
    map_id = $game_map.map_id
    tile_id = [$game_map.tile_id, $game_map.tile_id(1), $game_map.tile_id(2)]
    text1 = "ޥåID: #{map_id}"
    text2 = ":#{$game_player.x} :#{$game_player.y}"
    text3 = "ID:"
    text4 = "#{tile_id[0]} #{tile_id[1]} #{tile_id[2]}"
    @extra_window.contents.draw_text(4, wlh * 0, cw, wlh, text1)
    @extra_window.contents.draw_text(4, wlh * 1, cw, wlh, text2)
    @extra_window.contents.draw_text(4, wlh * 2, cw, wlh, text3)
    @extra_window.contents.draw_text(4, wlh * 3, cw, wlh, text4)
  end
  #--------------------------------------------------------------------------
  #  K˄I (׷Ӷx)
  #--------------------------------------------------------------------------
  def terminate
    _exmefct_terminate
    @extra_window.dispose if @extra_window != nil
  end
end
