class Scene_Battle
#==============================================================================
# Go to phase4 step2 for attack if attack_count < attacks.size
#==============================================================================
 alias g7_ms_scene_battle_phase4_step6 update_phase4_step6
 def update_phase4_step6(battler=nil)
     #This methods make the battler return to phase4 step2 if there
     #an extra attack to be made

  if battler== nil then #if no parameters
     g7_ms_scene_battle_phase4_step6 #first do the appropriate action
     if @active_battler.is_a?(Game_Enemy) then return end
     @active_battler.attack_count += 1 #increase the number of attack made
     if @active_battler.attack_count == @active_battler.attacks.size or
     @active_battler.current_action.kind != 0 or
     @active_battler.current_action.basic != 0 or
     judge == true#if all attacks are made

        @active_battler.attack_count = 0
     else
        @phase4_step = 2 #return for extra attack
     end
  else #if it requires the battler as a parameters
     g7_ms_scene_battle_phase4_step6(battler)
     if battler.is_a?(Game_Enemy) then return end
     battler.attack_count += 1 #increase the number of attack made
     if battler.attack_count == battler.attacks.size or
     battler.current_action.kind != 0 or battler.current_action.basic != 0 or
     judge == true#if all attacks are made

        battler.attack_count = 0 #end of turn and return attack count to 0
     else
        if @action_battlers.include?(@active_battler) == false then @action_battlers.push(battler) end #add the battler again to the action
        battler.phase = 2 #returns for extra attack
     end
  end

 end #end def
end #end class

#--------------------------------------------------------------------------
# Fix equiped item if user load saved game
#--------------------------------------------------------------------------
class Scene_Load < Scene_File
 alias g7_ms_scene_load_read_save_data read_save_data
 def read_save_data(file)
       g7_ms_scene_load_read_save_data(file)
       $game_actors.order_items #order armor when you load a saved game in
      #case it is an old game
 end
end

#--------------------------------------------------------------------------
# Fix items as you open RMXP
#--------------------------------------------------------------------------
class Scene_Title
 alias g7_ms_scene_title_main main
 def main
       RPG.initialized_item_types = false
       g7_ms_scene_title_main
       RPG.set_new_item_types
 end
end

class Interpreter
#==============================================================================
# *Conditional Branch to make it work when comparing extra items
#==============================================================================
 alias g7_ms_interpreter_command_111 command_111
 def command_111
  result = false
  broken = false
  case @parameters[0]
   when 4 # when Actor
   actor = $game_actors[@parameters[1]]
   if actor != nil
    case @parameters[2]
    when 3 # if weapon
    for i in 0...actor.weapon_ids.size
     result |= (actor.weapon_ids[i] == @parameters[3]) #compare
    end
    broken = true
    when 4 # if armor
    for i in 0...actor.armor_ids.size
     result |= (actor.armor_ids[i] == @parameters[3]) #compare
    end
    broken = true
    end
   end
  end
  unless broken # if other conditional branch
   g7_ms_interpreter_command_111 #do the normal condition
   return
  end
 @branch[@list[@index].indent] = result
 if @branch[@list[@index].indent] == true
  @branch.delete(@list[@index].indent)
  return true
 end
  return command_skip
 end
end


module Kernel
  private
  def Rg(from, to, *step) Rg.new(from, to, *step) end
end

class Rg
  #Used to a more effective for ... in ...
  include Enumerable
  def initialize(from, to, step = sign(to - from))
    @from, @to, @step = from, to, step
    @to += @step - ((@to - @from) % @step)
  end
  def each
    x = @from
    until x == @to
      yield x
      x += @step
    end
    self
  end
  def sign(x)
    case
    when x > 0
      1
    when x == 0
      0
    else
      -1
    end
  end
end