// the actual combat rolls

function EnemyInfantryAI()
{   
   echo("START ENEMY INFANTRY AI");

   // if the game gets paused loop it here
   
   if($pause $= 1)
   {
      schedule(1000, 0, "EnemyInfantryAI");
      return;      
   }
   
   if($peoplebattle $= "")
      return;
         
   // pick a firing squad
   
   while(%match $= "")
   {
      %squad = getRandom(1, 6);
      
      if($EnemySquad[%squad, soldiers] !$= "")
      {
         %count = getWordCount($EnemySquad[%squad, soldiers]);
         %Enemy = getWord($EnemySquad[%squad, soldiers], getRandom(0, %count-1));
         %match = true;                  
      }
   }
   
   // logic, decide if the squad is going to move or change position
   
   // there is a small chance it will change position
   
   %rand = getRandom(1, 50);
   
   if(%rand == 1 && $EnemySquad[%Squad, move] $= "")
      ChangeEnemySquadPosition(%squad);      
   
   %rand = getRandom(1, 75);
   
   if(%rand == 1 && $EnemySquad[%Squad, position] $= "Standing")
   {
      // pick a random spot using the center map as a point of reference

      %layer = "infantry" @ $InfantryMap;
      %block = true;

      while(%block $= true)
      {
         %x = getRandom(12, 29);
         %y = getRandom(14, 27);         
         %tile = %x SPC %y;
         
         %block = %layer.GetTileCustomData(%tile);
      }
      
      %coords = getTCenter(%layer, %x, %y);
      
      MoveEnemyInfantry(%coords, %squad);      
   }
   
   // pick a target

   %lowestdist = 1000;
   
   for(%i=1;%i<=7;%i++)
   {
      %count = getWordCount($PlayerSquad[%i, soldiers]);
      
      if(%count > 0)
      {
         %player = getWord($PlayerSquad[%i, soldiers], getRandom(0, %count-1));
         
         %dist = VectorDist(%enemy.GetPosition(), %player.GetPosition());
         
         if(%dist < %lowestdist)
         {
            %target = %player;
            %lowestdist = %dist;   
         }
      }            
   }

   if($EnemySquad[%enemy.squad, ammo] $= 0)
      AddEnemyInfantryCrossbowIcons(%enemy.squad);
   
   EnemyInfantryCombat(%Enemy, %target);      

   %ret = CheckEndInfantryCombat();
   
   if(%ret $= true)
      return;
      
   schedule(1000, 0, "EnemyInfantryAI");

   echo("END ENEMY INFANTRY AI");
}

// Enemy attack

function EnemyInfantryCombat(%Enemy, %player)
{        
   echo("START ENEMY INFANTRY COMBAT");

   %angle = t2dAngleToPoint(%Enemy.GetPosition(), %player.GetPosition());
   %angle = %angle+180;
   %Enemy.RotateTo(%angle, 80);
   %angle = %angle-180;
   
   if($EnemySquad[%Enemy.squad, ammo] >= 1)
   {
      CreateEnemyInfantryFire(%enemy); 
      
      $EnemySquad[%Enemy.squad, ammo]--;

      AudioFireGunsLow();
   }
   else
   {
      // crossbow   
      
      alxPlay(crossbow);
      
      %ammomod = -1;          
   }   
   
   // Enemy roll
   
   %roll = getRandom(1, 20);
   %Enemyroll = %roll+%ammomod;
   
   if($EnemySquad[%Enemy.squad, position] $= "standing")
      %Enemyroll++;

   if($EnemySquad[%Enemy.squad, position] $= "kneeling")
      %Enemyroll--;

   if($EnemySquad[%Enemy.squad, position] $= "prone")
   {
      %Enemyroll--;
      %Enemyroll--;
   }

   switch$ ($Difficulty)
   {
      case "easy":
         %Enemyroll--;
      case "hard":     
         %Enemyroll++; 
   }

   %dist = VectorDist(%player.GetPosition(), %enemy.GetPosition());
   
   if(%dist <= 30)
      %enemyroll = %enemyroll+1;
      
   switch$ (%player.class)
   {
      case "refuges":
         %defense = 5;      
      case "brigands":
         %defense = 7;            
      case "militia":
         %defense = 9;            
      case "police":
         %defense = 11;            
      case "soldiers":
         %defense = 13;            
      case "mercenaries":
         %defense = 15;            
   }

   %defense = %defense + $PlayerSquad[%player.squad, defensemodifier] + GetPlayerCoverBonus(%player.squad);
      
   echo("ENEMY ROLL " @ %Enemyroll @ " DEFENSE " @ %defense);
   
   if(%Enemyroll >= %defense || %roll == 20)
   {      
      %body =    new t2dStaticSprite() {
               canSaveDynamicFields = "1";
               imageMap = "deadsoldier";
               scenegraph = "sg";
               size = "8 8";
               layer = 2;  
            };
            
      %body.SetPosition(%player.GetPosition());
      %body.SetRotation(getRandom(0, 360));

      // remove the player from the squad
      
      %count = getWordCount($PlayerSquad[%player.squad, soldiers]);
      
      for(%i=0;%i<%count;%i++)
      {
         %soldier = getWord($PlayerSquad[%player.squad, soldiers], %i);         
         
         if(%soldier $= %player)
            break;
      }
      
      $PlayerSquad[%player.squad, soldiers] = removeWord($PlayerSquad[%player.squad, soldiers], %i);
            
      %layer = "infantry" @ $InfantryMap;      
      %tile = %layer.PickTile(%player.GetPosition());
      %layer.SetTileCustomData(getWord(%tile, 0) SPC getWord(%tile, 1), "");
      
      %count = getWordCount($PlayerSquad[%player.squad, soldiers]);
      
      if(%count < 1)
         RotateInfantrySquad();
      
      if(isObject(%player) $= true)   
         %player.SafeDelete();          
   }
   
   UpdatePeopleBattleWindow();

   echo("END ENEMY INFANTRY COMBAT");
}