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)
  stopApproachingRange = 600
  Enter_AttackCastle()
end

--------------------------------------------------------------------------------------
------------------------------ start AttackCastle state --------------------------------
--------------------------------------------------------------------------------------

function Enter_AttackCastle (EnemyID)

  local needToAttackCastle = false
  
  local EnemyCastleID = ARMY.GetInRangeID(myArmyID, "ENEMY_CASTLE", 1000000)  
  if table.getn(EnemyCastleID) > 0 then
    needToAttackCastle = true
  end    
    
  EnemyCastle_PositionX, EnemyCastle_PositionY = CASTLE.GetPosition(EnemyCastleID)
  if needToAttackCastle then
    ARMY.SetBehavior(myArmyID, "WalkTo")
    ARMY.SetGoalPosition(myArmyID, EnemyCastle_PositionX, EnemyCastle_PositionY)  
  end
  
  hasStoppedApproaching = false
end

function Update_AttackCastle ()
  local Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)      
  local DistanceFromEnemy = MAP.GetDistanceBetweenTwoPosition(Now_PositionX, Now_PositionY, EnemyCastle_PositionX, EnemyCastle_PositionY)

  if DistanceFromEnemy < stopApproachingRange then
    if not hasStoppedApproaching then
      --already very close, stop approaching
      ARMY.SetGoalPosition(myArmyID, Now_PositionX, Now_PositionY)
      hasStoppedApproaching = true
    end
  else
    --keep approaching
    ARMY.SetGoalPosition(myArmyID, EnemyCastle_PositionX, EnemyCastle_PositionY)
    hasStoppedApproaching = false
  end
end

--------------------------------------------------------------------------------------
------------------------------- end AttackCastle state ---------------------------------
--------------------------------------------------------------------------------------

function Update ()
    Update_AttackCastle()
end
