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

function Init ()
  startChasingArmyRange = 1100
  stopChasingArmyRange = 2100
  stopApproachingRange = 400
  
  Enter_Wander()
end

--------------------------------------------------------------------------------------
--------------------------------- start Wander state ---------------------------------
--------------------------------------------------------------------------------------

function Enter_Wander ()
  STATE = "Wander"
  
  ARMY.SetBehavior(myArmyID, "WalkTo")
  
  local EnemyID = ARMY.GetInRangeID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY", 100000)
  if table.getn(EnemyID) > 0 then
    Wander_Goal_PositionX, Wander_Goal_PositionY = MAP.GetRandomPositionApproachingEnemy(myArmyID, EnemyID)
  else
    Wander_Goal_PositionX, Wander_Goal_PositionY = MAP.GetRandomPosition()
  end
  
  ARMY.SetGoalPosition(myArmyID, Wander_Goal_PositionX, Wander_Goal_PositionY)
  
  Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)
  Last_PositionX, Last_PositionY = Now_PositionX, Now_PositionY
  samePosCount = 0
end

function Update_Wander ()

  Last_PositionX, Last_PositionY = Now_PositionX, Now_PositionY
  Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)

  if (Last_PositionX == Now_PositionX) and (Last_PositionY == Now_PositionY) then
    samePosCount = samePosCount + 1
  else
    samePosCount = 0
  end
  
  local needToAttackArmy = false
  local EnemyID = ARMY.GetInRangeID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY", startChasingArmyRange)
  if table.getn(EnemyID) > 0 then
    needToAttackArmy = true
  elseif ARMY.IsBeenAttacked(myArmyID) then
    needToAttackArmy = true
    EnemyID = ARMY.GetAttackerID(myArmyID)
  end
  
  if needToAttackArmy then
    Exit_Wander()
    Enter_AttackLord(EnemyID)
  elseif samePosCount >= 5 then
    Exit_Wander()
    Enter_Wander()
  end
  
end

function Exit_Wander()
  --Wander_Exit_PositionX, Wander_Exit_PositionY = ARMY.GetPosition(myArmyID)
end

--------------------------------------------------------------------------------------
----------------------------------- end Wander state ---------------------------------
--------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------
------------------------------ start AttackLord state --------------------------------
--------------------------------------------------------------------------------------

function Enter_AttackLord (EnemyID)
  STATE = "AttackLord"
  
  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)  
end

function Update_AttackLord ()
  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  local Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)
  local DistanceFromOrigin = MAP.GetDistanceBetweenTwoPosition(Enemy_PositionX, Enemy_PositionY, Now_PositionX, Now_PositionY)
  --local DistanceFromOrigin = MAP.GetDistanceBetweenTwoPosition(Enemy_PositionX, Enemy_PositionY, Wander_Exit_PositionX, Wander_Exit_PositionY)
      
  if (DistanceFromOrigin > stopChasingArmyRange) or (not IsAttackable(AttackLord_Goal_Army)) then
    Exit_AttackLord ()
    Enter_Wander()
  else
    local DistanceFromEnemy = MAP.GetDistanceBetweenTwoPosition(Now_PositionX, Now_PositionY, Enemy_PositionX, Enemy_PositionY)
    if DistanceFromEnemy < stopApproachingRange then
      if not hasStoppedApproachingEnemy then
        --already very close, stop approaching
        ARMY.SetGoalPosition(myArmyID, Now_PositionX, Now_PositionY)
        hasStoppedApproachingEnemy = true
      end
    else
      --keep approaching
      ARMY.SetGoalPosition(myArmyID, Enemy_PositionX, Enemy_PositionY)
      hasStoppedApproachingEnemy = false
    end
  end
end

function Exit_AttackLord ()
  if ARMY.IsBeenAttacked(myArmyID) then
    --do nothing, calling here is just to clear the flag
  end 
end
--------------------------------------------------------------------------------------
------------------------------- end AttackLord state ---------------------------------
--------------------------------------------------------------------------------------

function Update ()
  if STATE == "Wander" then
    Update_Wander()
  elseif STATE == "AttackLord" then
    Update_AttackLord()
  end
end
