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)
  
  ATTACK_CASTLE = false
  
  Enter_Patrol()
end

function Enter_Patrol()
  PatrolStateStartTime = TIME.GetGameNowTime()

  ARMY.SetBehavior(myArmyID, "Wander")
  STATE = "Patrol"
  return 1
end

function Enter_Pursuit(GoalEnemyArmyID)
  Pursuit_Goal_ArmyID = GoalEnemyArmyID
  PursuitStateStartTime = TIME.GetGameNowTime()
  PursuitStateStartPositionX, PursuitStateStartPositionY = ARMY.GetPosition(myArmyID)

  ARMY.SetBehavior(myArmyID, "AttackLord")
  ARMY.SetGoal(myArmyID, "ARMY", Pursuit_Goal_ArmyID)

  STATE = "Pursuit"
  return 1
end

function Enter_Move()
  ARMY.SetBehavior(myArmyID, "WalkTo")
  ARMY.SetGoalPosition(myArmyID, PatrolStateEndPositionX, PatrolStateEndPositionY)

  STATE = "Move"
  return 1
end

function Enter_AttackCastle(GoalCastleID)
  AttackCastle_Goal_CastleID = GoalCastleID

  ARMY.SetBehavior(myArmyID, "AttackCastle")
  ARMY.SetGoal(myArmyID, "CASTLE", AttackCastle_Goal_CastleID)

  STATE = "AttackCastle"
  return 1
end

function Enter_Flee (EscapeEnemyID)
  Flee_From_Army = EscapeEnemyID
  ARMY.SetBehavior(myArmyID, "Escape")
  STATE = "Flee"
end

function Exit_Patrol()
  PatrolStateEndPositionX, PatrolStateEndPositionY = ARMY.GetPosition(myArmyID)
  return 1
end

function Exit_Patrol_IFCAN_Attack_Closest_Castle ()
  local GoalCastleID = ARMY.GetClosestID(myArmyID, "CAN_ATTACK_CASTLE", 300)
  if GoalCastleID ~= -1 then
      Exit_Patrol()
      Enter_AttackCastle(GoalCastleID)
  end
end

function Update_Patrol()
  local GoalEnemyArmyID = ARMY.GetInRangeID(myArmyID, "VISIBLE_CAN_ATTACK_ARMY", 300)
  if table.getn(GoalEnemyArmyID) > 0 then
    Exit_Patrol()
    Enter_Pursuit(GoalEnemyArmyID[1])
  else
    local GoalCastleID = ARMY.GetInRangeID(myArmyID, "CAN_ATTACK_CASTLE", 300)
    if table.getn(GoalCastleID) > 0 then
      Exit_Patrol()
      Enter_AttackCastle(GoalCastleID[1])
    else
      if ATTACK_CASTLE then
        Exit_Patrol_IFCAN_Attack_Closest_Castle()
        return 1
      else
        local TimeDif = TIME.GetGameNowTime() - PatrolStateStartTime
        if TimeDif > 20 then
          PatrolStateStartTime = TIME.GetGameNowTime()
          fRandom = math.random()
--          DEBUG.Print(fRandom)
          if fRandom < 0.4 then
              ATTACK_CASTLE = true
          end
        end
      end
    end
  end
  return 1
end

function Update_Pursuit()
  if IsThisArmyNotNeedAttack(Pursuit_Goal_ArmyID) then
    Enter_Move1()
    return 1
  elseif ARMY.GetHPPercent(myArmyID) < 0.2 then
    local EnemyID = ARMY.GetAttackerID(myArmyID)
    Enter_Flee(EnemyID)
    return 1
  end 

  local PursuitKeepTime = TIME.GetGameNowTime() - PursuitStateStartTime
  if PursuitKeepTime >= 3 then
    Enter_Move()
    return 1
  end  

  MySelfPositionX, MySelfPositionY = ARMY.GetPosition(myArmyID)
  local DistanceFromOrigin = MAP.GetDistanceBetweenTwoPosition(PursuitStateStartPositionX, PursuitStateStartPositionY, MySelfPositionX, MySelfPositionY)
  if DistanceFromOrigin >= 500 then
    Enter_Move()
    return 1
  end
  return 1
end

function Update_Move()
  local GoalTowerID = ARMY.GetInRangeID(myArmyID, "CAN_CAPTURE_TOWER", 300)
  if table.getn(GoalTowerID) > 0 then
    Enter_Capture(GoalTowerID[1])
  elseif ARMY.IsInGoalRange(myArmyID) then
    Enter_Patrol()
  end
  return 1
end

function Update_Capture()
  if ARMY.IsBeenAttacked(myArmyID) then
    local EnemyID = ARMY.GetAttackerID(myArmyID)
    Enter_Pursuit(EnemyID)
  else
    local towerTeam = TOWER.GetOwnerTeam(Capture_Goal_TowerID)
    if towerTeam == ARMY.GetTeam(myArmyID) then 
      Enter_Move1()
    end
  end
  return 1
end

function Update_Flee ()
  local FleeGoalPositionX, FleeGoalPositionX = ARMY.GetPosition(Flee_From_Army)
  local Now_PositionX, Now_PositionY = ARMY.GetPosition(myArmyID)
  local DiffDistance = MAP.GetDistanceBetweenTwoPosition(FleeGoalPositionX, FleeGoalPositionX, Now_PositionX, Now_PositionY)
  if DiffDistance > 300 then
    Enter_Move()
    return 1
  end
end

function Update ()
  if STATE == "Patrol" then
    Update_Patrol()
  elseif STATE == "Pursuit" then
    Update_Pursuit()
  elseif STATE == "Move" then
    Update_Move()
  elseif STATE == "Capture" then
    Update_Capture()
  elseif STATE == "Flee" then
    Update_Flee()
  end
  return 1
end
