includefilename = "asset\\composition\\AIScript\\LordAICommon.lua" 
includefilename = SYSTEM.RelativeToAbsolutePath(includefilename)
dofile(includefilename)

function Init ()
  SPECIAL_ABILITY_WAIT_TIME_RANDOM_RANGE_START = 5.0
  SPECIAL_ABILITY_WAIT_TIME_RANDOM_RANGE_END = 10.0
  
  ENEMY_NEAR_ENOUGH_IN_SPECIAL_ABILITY_RANGE = 800.0
  
  TIME_TO_CHASE_ENEMY_WHEN_WANT_TO_USE_SPECIAL_ABILITY = 5.0

  --initialize
  g_fNextSpecialAbilityTime = TIME.GetGameNowTime() + _RandomSpecialAbilityWaitTime()
  g_bHasDecidedWaitTime = true

  Enter_UseSpecialAbility()
end


function _RandomSpecialAbilityWaitTime()
  local fRandomEscapeWaitTime = SPECIAL_ABILITY_WAIT_TIME_RANDOM_RANGE_START + ( (SPECIAL_ABILITY_WAIT_TIME_RANDOM_RANGE_END - SPECIAL_ABILITY_WAIT_TIME_RANDOM_RANGE_START) * math.random() )
  return fRandomEscapeWaitTime
end

function _RandomDecideUseSpecialAbility()
  local bHasMP = ARMY.IsCanUseSpell(myArmyID)
  local fCurTime = TIME.GetGameNowTime()
  if bHasMP then
    if g_bHasDecidedWaitTime then
      if fCurTime > g_fNextSpecialAbilityTime then
        g_bHasDecidedWaitTime = false
        return true
      end
    else
      g_fNextSpecialAbilityTime = fCurTime + _RandomSpecialAbilityWaitTime()
      g_bHasDecidedWaitTime = true
    end
  end
  
  return false
end

--------------------------------------------------------------------------------------
----------------------------- start Idle state --------------------------
--------------------------------------------------------------------------------------

function Enter_Idle ()
  STATE = "Idle"
  ARMY.SetBehavior(myArmyID, "Idle") 
end

function Update_Idle()
  if _RandomDecideUseSpecialAbility() then
    Exit_Idle()
    Enter_UseSpecialAbility()
  end
end

function Exit_Idle()
end

--------------------------------------------------------------------------------------
---------------------------- end Idle state -----------------------------
--------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------
----------------------------- start UseSpecialAbility state --------------------------
--------------------------------------------------------------------------------------

function Enter_UseSpecialAbility ()
  STATE = "UseSpecialAbility"
  
  local EnemyID = ARMY.GetInRangeID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY", 100000)
  AttackLord_Goal_Army = EnemyID
  hasStoppedApproachingEnemy = false

  ARMY.SetBehavior(myArmyID, "WalkTo")
  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  ARMY.SetGoalPosition(myArmyID, Enemy_PositionX, Enemy_PositionY)
  
  stopApproachingRange = 400
  g_fTimeShouldLeaveUseSpecialAbilityState = TIME.GetGameNowTime() + TIME_TO_CHASE_ENEMY_WHEN_WANT_TO_USE_SPECIAL_ABILITY
end

function Update_UseSpecialAbility()
  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  local Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)      

  local DistanceFromEnemy = MAP.GetDistanceBetweenTwoPosition(Now_PositionX, Now_PositionY, Enemy_PositionX, Enemy_PositionY)
  if DistanceFromEnemy < ENEMY_NEAR_ENOUGH_IN_SPECIAL_ABILITY_RANGE then
    ARMY.UseSpecialAbility(myArmyID)
    Exit_UseSpecialAbility()
    Enter_Idle()
  elseif TIME.GetGameNowTime() > g_fTimeShouldLeaveUseSpecialAbilityState then
    Exit_UseSpecialAbility()
    Enter_Idle()
  else
    ARMY.SetGoalPosition(myArmyID, Enemy_PositionX, Enemy_PositionY)
  end  
  
end

function Exit_UseSpecialAbility()
end

--------------------------------------------------------------------------------------
---------------------------- end UseSpecialAbility state -----------------------------
--------------------------------------------------------------------------------------
function Update ()
  if STATE == "Idle" then
    Update_Idle()
  elseif STATE == "UseSpecialAbility" then
    Update_UseSpecialAbility()
  end
end
