function AIControl::analyzeSituation(%this)
{
   if(%this.allowAnalysis)
   {
      // get AI player
      %aiPlayer = gameInstance.getAIPlayer();
      
      // get oppoenent
      %humanPlayer = gameInstance.getHumanPlayer();
      
      // analyze situation and get most interesting field
      %interestRateStatement = Analyzer::getInterestRate(%aiPlayer);
      %interestRate = getWord(%interestRateStatement, 0);
      %interestedField = getWord(%interestRateStatement, 1);
      %interestedFieldName = %interestedField.getName();
      echo(%interestRate SPC "interest in field" SPC %interestedFieldName);
      
      %this.mostInterestingField = %interestedField;
      
      // lock analysis and allow it after minimum interval time up
      %this.setAllowAnalysis(false);
      %this.schedule($pref::Game::OpponentAIMinimumAnalysisInterval, "setAllowAnalysis", true);
   }
}

function AIControl::setAllowAnalysis(%this, %state)
{
   %this.allowAnalysis = %state;
}

function AIControl::performAction(%this)
{
   if(isObject(%this.mostInterestingField))
   {
      // get AI player
      %aiPlayer = gameInstance.getAIPlayer();
      
      // get oppoenent
      %humanPlayer = gameInstance.getHumanPlayer();
      
      // determine field owner
      %fieldowner = %this.mostInterestingField.getOwner();
      if(isObject(%fieldowner))
         %fieldownerId = %fieldowner.getId();
      
      if(%fieldownerId == %humanPlayer.getId()) // field is owned by human player
      {
         echo("Attacking" SPC %this.mostInterestingField.getName());
         %this.conquerField(%this.mostInterestingField);
      }
      else if(%fieldownerId == %aiPlayer.getId())  // field is owned by the AI
      {
         if(%this.mostInterestingField.getOwningTimeLeft() <= mMax($pref::Game::ResourceGainTick - resControl.getResourceBonus(%aiPlayer) SPC $pref::Game::MinimumResourceGainTick) * ($pref::Game::ResourceLimit - %aiPlayer.getResource()))
         {
            echo("Refreshing" SPC %this.mostInterestingField.getName());
            %this.refreshField(%this.mostInterestingField);
         }
         else
         {
            echo("Defending" SPC %this.mostInterestingField.getName());
            %this.defendField(%this.mostInterestingField);
         }
      }
      else  // neutral field
      {
         echo("Gathering" SPC %this.mostInterestingField.getName());
         %this.conquerField(%this.mostInterestingField);
      }
   }
   
   // schedule next action
   if(isObject(%this))
      %this.schedule($pref::Game::OpponentAIActionTick, "performAction");
}

function AIControl::conquerField(%this, %targetField)
{
   // get AI player
   %aiPlayer = gameInstance.getAIPlayer();
   
   %nextField = Pathfinding::getClosestConquerableField(%aiPlayer, %targetField);
   
   if(%nextField.areConquerRequirementsMet(%aiPlayer))
      %nextField.conquer(%aiPlayer);
}

function AIControl::refreshField(%this, %targetField)
{
   // get AI player
   %aiPlayer = gameInstance.getAIPlayer();
   
   if(%targetField.areRefreshRequirementsMet(%aiPlayer))
      %targetField.refresh(%aiPlayer);
}

function AIControl::defendField(%this, %targetField)
{
   // get AI player
   %aiPlayer = gameInstance.getAIPlayer();
   
   %allFieldNeighbors = %targetField.getAllNeighbors();
   %allFieldNeighborsCount = getWordCount(%allFieldNeighbors);
   
   %minimumCost = "";
   %minimumCostNeighbor = "";
   for(%i = 0; %i < %allFieldNeighborsCount; %i++)
   {
      %currentNeighbor = getWord(%allFieldNeighbors, %i);
      
      // conquer one neighbor that has minimum costs to conquer
      %fieldowner = %currentNeighbor.getOwner();
      if(!isObject(%fieldowner) || %fieldowner.getId() != %aiPlayer.getId())  // empty or opposing field
      {
         %cost = %currentNeighbor.getConquerCost(%aiPlayer);
         if(%minimumCost $= "" || %cost < %minimumCost)
         {
            %minimumCost = %cost;
            %minimumCostNeighbor = %currentNeighbor;
         }
      }
   }
   
   if(isObject(%minimumCostNeighbor))
      %minimumCostNeighbor.conquer(%aiPlayer);
      
   echo(%minimumCostNeighbor);
}