// the actual combat rolls

function PlayerInfantryAI()
{   
   echo("START PLAYER INFANTRY AI");
   
   // if the game gets paused loop it here
   
   if($pause $= 1)
   {
      schedule(1000, 0, "PlayerInfantryAI");
      return;      
   }
   
   if($peoplebattle $= "")
      return;
            
   // pick a firing squad
   
   while(%match $= "")
   {
      %squad = getRandom(1, 7);
      
      if($PlayerSquad[%squad, soldiers] !$= "")
      {
         %count = getWordCount($PlayerSquad[%squad, soldiers]);
         %player = getWord($PlayerSquad[%squad, soldiers], getRandom(0, %count-1));
         %match = true;                  
      }
   }
   
   // is there a manual target?
   
   if($PlayerSquad[%squad, target] !$= "") 
   {
      // run a check here, if that squad is gone just destroy this value
      
      %count = getWordCount($EnemySquad[$PlayerSquad[%squad, target], soldiers]);
      
      if(%count > 0)
         %manualtarget = "yes";
      else
         %manualtarget = "no";      
   }
   else
      %manualtarget = "no";
      
   // pick a target only if the player hasn't manually selected one
   
   switch$(%manualtarget)
   {
      case "yes":
         %count = getWordCount($EnemySquad[$PlayerSquad[%squad, target], soldiers]);
         %target = getWord($EnemySquad[$PlayerSquad[%squad, target], soldiers], getRandom(0, %count-1));
            
      case "no":
         %lowestdist = 1000;
         
         for(%i=1;%i<=6;%i++)
         {
            %count = getWordCount($EnemySquad[%i, soldiers]);
            
            if(%count > 0)
            {
               %enemy = getWord($EnemySquad[%i, soldiers], getRandom(0, %count-1));
               
               %dist = VectorDist(%enemy.GetPosition(), %player.GetPosition());
               
               if(%dist < %lowestdist)
               {
                  %target = %enemy;
                  %lowestdist = %dist;   
               }
            }
         }
   }
   
   if($PlayerSquad[%player.squad, ammo] $= 0 || $PlayerSquad[%player.squad, ammo] $= "")
      AddPlayerInfantryCrossbowIcons(%player.squad);
   
   PlayerInfantryCombat(%player, %target);      

   schedule(1000, 0, "PlayerInfantryAI");

   echo("END PLAYER INFANTRY AI");
}

// player attack

function PlayerInfantryCombat(%player, %enemy)
{        
   echo("START PLAYER INFANTRY COMBAT");

   %angle = t2dAngleToPoint(%player.GetPosition(), %enemy.GetPosition());
   %player.RotateTo(%angle, 80);
   
   if($PlayerSquad[%player.squad, ammo] >= 1)
   {
      CreatePlayerInfantryFire(%player);
      
      $PlayerSquad[%player.squad, ammo]--;

      AudioFireGunsLow();
   }
   else
   {
      // crossbow   
      
      alxPlay(crossbow1);
      
      %ammomod = -1;          
   }   
   
   // player roll
   
   %roll = getRandom(1, 20);
   %playerbonus = GetSoldierAttackBonus(%player.class);
   %playerroll = %roll+%playerbonus;
   %playerroll = %playerroll+%ammomod;
   
   if($PlayerSquad[%player.squad, position] $= "standing")
      %playerroll++;

   if($PlayerSquad[%player.squad, position] $= "kneeling")
      %playerroll--;

   if($PlayerSquad[%player.squad, position] $= "prone")
   {
      %playerroll--;
      %playerroll--;
   }

   if($starving $= true || $disease $= true)
      %playerroll--;
   
   switch$ ($Difficulty)
   {
      case "easy":
         %playerroll++;
      case "hard":     
         %playerroll--; 
   }
   
   %dist = VectorDist(%player.GetPosition(), %enemy.GetPosition());
   
   if(%dist <= 30)
      %playerroll = %playerroll+2;
   
   %defense = 15 + $EnemySquad[%enemy.squad, defensemodifier];

   if(%playerroll >= %defense || %roll == 20)
   {      
      %body =    new t2dStaticSprite() {
               canSaveDynamicFields = "1";
               imageMap = "deadsoldier";
               scenegraph = "sg";
               size = "8 8";
               layer = 2;  
            };
            
      %body.SetPosition(%enemy.GetPosition());
      %body.SetRotation(getRandom(0, 360));

      // remove the enemy from the squad
      
      %count = getWordCount($EnemySquad[%enemy.squad, soldiers]);
      
      for(%i=0;%i<%count;%i++)
      {
         %soldier = getWord($EnemySquad[%enemy.squad, soldiers], %i);         
         
         if(%soldier $= %enemy)
            break;
      }
      
      $EnemySquad[%enemy.squad, soldiers] = removeWord($EnemySquad[%enemy.squad, soldiers], %i);

      %layer = "infantry" @ $InfantryMap;      
      %tile = %layer.PickTile(%enemy.GetPosition());
      %layer.SetTileCustomData(getWord(%tile, 0) SPC getWord(%tile, 1), "");
      
      if(isObject(%enemy) $= true)
         %enemy.SafeDelete();
         
      CheckEndInfantryCombat();
   }
   
   UpdatePeopleBattleWindow();

   echo("END PLAYER INFANTRY COMBAT");
}

function TargetEnemySoldiers(%coords)
{
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.enemy !$= "")
      {
         $PlayerSquad[$CurrentSquad, target] = %obj.squad;         
         alxPlay(goodbeep);
         break;
      }
   }      
   
   %count = getWordCount($EnemySquad[%obj.squad, soldiers]);
   
   for(%i=0;%i<%count;%i++)
   {
      %soldier = getWord($EnemySquad[%obj.squad, soldiers], %i);      
      
      %soldier.SetBlending(true, ONE, ONE_MINUS_SRC_ALPHA);            
      %soldier.SetBlendColor(166, 0, 0, 255);
   }
}

function TargetPlayerSoldiers(%coords)
{
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.player !$= "")
      {
         
         break;
      }
   }      
}
