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

function Init ()
--WANDER.AddGoalPoint(myArmyID, 1000, 1000, 0)
--WANDER.AddGoalPoint(myArmyID, 1000, -1000, 0)
--WANDER.AddGoalPoint(myArmyID, -1000, -1000, 0)
--WANDER.AddGoalPoint(myArmyID, -1000, 1000, 0)
  startChasingArmyRange = 1100
  stopChasingArmyRange = 2100
  stopApproachingRange = 400
  WANDER_GOAL_DIF = 300
  Enter_Wander()
end

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

function Enter_Wander ()
  STATE = "Wander"
  ARMY.SetBehavior(myArmyID, "Wander")
end

function Update_Wander ()
      
  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)
  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 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_WalkBackToPatrol()
  else
    local Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)
    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 ---------------------------------
--------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------
------------------------------ start WalkBackToPatrol state --------------------------
--------------------------------------------------------------------------------------

function Enter_WalkBackToPatrol ()
  STATE = "WalkBackToPatrol"
  
  ARMY.SetBehavior(myArmyID, "WalkTo")
  ARMY.SetGoalPosition(myArmyID, Wander_Exit_PositionX, Wander_Exit_PositionY)
end

function Update_WalkBackToPatrol ()

  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  local DistanceFromOrigin = MAP.GetDistanceBetweenTwoPosition(Enemy_PositionX, Enemy_PositionY, Wander_Exit_PositionX, Wander_Exit_PositionY)
  local CurPosX, CurPosY = ARMY.GetPosition(myArmyID)
  local DistanceFromLastPosition = MAP.GetDistanceBetweenTwoPosition(CurPosX, CurPosY, Wander_Exit_PositionX, Wander_Exit_PositionY)
      
  if (DistanceFromOrigin < startChasingArmyRange) and (IsAttackable(AttackLord_Goal_Army)) then
    Exit_WalkBackToPatrol ()
    Enter_AttackLord(AttackLord_Goal_Army)
  elseif DistanceFromLastPosition < WANDER_GOAL_DIF then
    Exit_WalkBackToPatrol ()
    Enter_Wander()
  end  
end

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

--------------------------------------------------------------------------------------
------------------------------ end WalkBackToPatrol state --------------------------
--------------------------------------------------------------------------------------

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