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

function Init ()
  walkDirectlyToEnemyRange = 800
  stopApproachingRange = 400
  
  startChasingArmyRange = 1100
  stopChasingArmyRange = 2100
  
  fEscapingDirX = 1
  fEscapingDirY = 0
  fMinWanderRange = 500
  fMaxWanderRange = 2000
  fWanderAngle = 180
  
  fMinPredictingRange = 800
  fMaxPredictingRange = 4000
  fAngle = 90
   
  Enter_AttackLord()
end

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

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

  fNextPredictTime = TIME.GetGameNowTime()

  ARMY.SetBehavior(myArmyID, "WalkTo")
  fEnemyLastPosX, fEnemyLastPosY = ARMY.GetPosition(AttackLord_Goal_Army)
  fPredictedPosX, fPredictedPosY = fEnemyLastPosX, fEnemyLastPosY
  ARMY.SetGoalPosition(myArmyID, fPredictedPosX, fPredictedPosY)
    
  fAtPredictedPosAccuracyRange = 200
end

function Update_AttackLord ()

  if (not IsAttackable(AttackLord_Goal_Army)) then
    Exit_AttackLord ()
    Enter_Wander()
	return 1
  end
  
  local Enemy_PositionX, Enemy_PositionY = ARMY.GetPosition(AttackLord_Goal_Army)
  local Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)
      
  local fDistanceEnemyHasMovedLastFrame = MAP.GetDistanceBetweenTwoPosition(Enemy_PositionX, Enemy_PositionY, fEnemyLastPosX, fEnemyLastPosY)
  fEnemyLastPosX, fEnemyLastPosY = Enemy_PositionX, Enemy_PositionY

  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
    
  elseif fDistanceEnemyHasMovedLastFrame < 3.0 or DistanceFromEnemy < walkDirectlyToEnemyRange then  
    ARMY.SetGoalPosition(myArmyID, Enemy_PositionX, Enemy_PositionY)
    hasStoppedApproachingEnemy = false
    
  else
    
    local fCurTime = TIME.GetGameNowTime()
    local bNeedToPredictAgain = false
    if fCurTime >= fNextPredictTime then
      bNeedToPredictAgain = true
    else
      local fDistanceFromPredictedPos = MAP.GetDistanceBetweenTwoPosition(Now_PositionX, Now_PositionY, fPredictedPosX, fPredictedPosY)
      if  fDistanceFromPredictedPos < fAtPredictedPosAccuracyRange then
        --already at the predicted position, so need to re-predict
        bNeedToPredictAgain = true
      end
    end
      
    if bNeedToPredictAgain then      
      --predict
      fPredictedPosX, fPredictedPosY = MAP.GetRandomPositionPredictingEnemy(myArmyID, AttackLord_Goal_Army, fMinPredictingRange, fMaxPredictingRange, fAngle)      
      ARMY.SetGoalPosition(myArmyID, fPredictedPosX, fPredictedPosY)
      local predictWaitTime = math.random(1000,5000) / 1000.0
      fNextPredictTime = fCurTime + predictWaitTime
    end
    
    hasStoppedApproachingEnemy = false
  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 Wander state ---------------------------------
--------------------------------------------------------------------------------------

function Enter_Wander ()
  STATE = "Wander"
  
  ARMY.SetBehavior(myArmyID, "WalkTo")
  
  Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)
  Wander_Goal_PositionX, Wander_Goal_PositionY = MAP.GetRandomPositionWithCondition(Now_PositionX, Now_PositionY, fEscapingDirX, fEscapingDirY, fMinWanderRange, fMaxWanderRange, fWanderAngle)
  
  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 ---------------------------------
--------------------------------------------------------------------------------------

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