// only 35 men are allowed on the battlefield at the same time

function BuildEnemySoldiers()
{
   echo("BUILD ENEMY SOLDIERS");

   if($CitySoldierList !$= "")
      %enemysoldiers = $CitySoldierList;
   else
      %enemysoldiers = getRandom(5, 20);   
   
   $EnemyInfantry = %enemysoldiers;  // so we know how many guns to give to the player after combat
   
   for(%i=1;%i<=%enemysoldiers;%i++)      
      AssignEnemySquads();      
   
   echo("DONE ASSIGNING ENEMY SQUADS");
   
   if($EnemySquad[1, soldiers] !$= "")
      PlaceEnemySoldiers(1);
   if($EnemySquad[2, soldiers] !$= "")
      PlaceEnemySoldiers(2);
   if($EnemySquad[3, soldiers] !$= "")
      PlaceEnemySoldiers(3);
   if($EnemySquad[4, soldiers] !$= "")
      PlaceEnemySoldiers(4);
   if($EnemySquad[5, soldiers] !$= "")
      PlaceEnemySoldiers(5);
   if($EnemySquad[6, soldiers] !$= "")
      PlaceEnemySoldiers(6);
      
   echo("DONE PLACING ENEMY SOLDIERS");
}

// this will take up to 35 soldiers and place them in EnemySquads

function AssignEnemySquads()
{
   %id =    new t2dStaticSprite() {
            canSaveDynamicFields = "1";
            imageMap = "enemy_standing";
            scenegraph = "sg";
            enemy = 1;
            size = "14 14";
            layer = 2;  
            class = "EnemyInfantry";
         };

   %bolt =    new t2dStaticSprite() {
      
            canSaveDynamicFields = "1";
            imageMap = "bolt";
            scenegraph = "sg";
            size = "2 5";
            layer = 3;
         };
   
   %id.gun = %bolt;
   %bolt.Mount(%id, "-.1 .5", 0, true, true, true, true);

   // now assign the soldier to an empty EnemySquad
   
   if(getWordCount($EnemySquad[1, soldiers]) < 5)
   {
      if($EnemySquad[1, soldiers] $= "")
         $EnemySquad[1, soldiers] = %id;
      else
         $EnemySquad[1, soldiers] = $EnemySquad[1, soldiers] SPC %id;      
         
      %id.squad = 1;      
      $EnemySquad[1, position] = "standing";         

      return;
   }
   
   if(getWordCount($EnemySquad[2, soldiers]) < 5)
   {
      if($EnemySquad[2, soldiers] $= "")
         $EnemySquad[2, soldiers] = %id;
      else
         $EnemySquad[2, soldiers] = $EnemySquad[2, soldiers] SPC %id;      

      %id.squad = 2;
      $EnemySquad[2, position] = "standing";         
         
      return;
   }

   if(getWordCount($EnemySquad[3, soldiers]) < 5)
   {
      if($EnemySquad[3, soldiers] $= "")
         $EnemySquad[3, soldiers] = %id;
      else
         $EnemySquad[3, soldiers] = $EnemySquad[3, soldiers] SPC %id;      

      %id.squad = 3;
      $EnemySquad[3, position] = "standing";         
         
      return;
   }

   if(getWordCount($EnemySquad[4, soldiers]) < 5)
   {
      if($EnemySquad[4, soldiers] $= "")
         $EnemySquad[4, soldiers] = %id;
      else
         $EnemySquad[4, soldiers] = $EnemySquad[4, soldiers] SPC %id;      

      %id.squad = 4;
      $EnemySquad[4, position] = "standing";         
         
      return;
   }

   if(getWordCount($EnemySquad[5, soldiers]) < 5)
   {
      if($EnemySquad[5, soldiers] $= "")
         $EnemySquad[5, soldiers] = %id;
      else
         $EnemySquad[5, soldiers] = $EnemySquad[5, soldiers] SPC %id;      

      %id.squad = 5;
      $EnemySquad[5, position] = "standing";         
         
      return;
   }

   if(getWordCount($EnemySquad[6, soldiers]) < 5)
   {
      if($EnemySquad[6, soldiers] $= "")
         $EnemySquad[6, soldiers] = %id;
      else
         $EnemySquad[6, soldiers] = $EnemySquad[6, soldiers] SPC %id;      

      %id.squad = 6;
      $EnemySquad[6, position] = "standing";         
         
      return;
   }

}

function PlaceEnemySoldiers(%EnemySquad)
{
   for(%j=1;%j<=6;%j++)
   {
      %spawn = "e" @ %j;
      
      if(%spawn.taken $= "")         
      {
         %spawn.taken = true;
         %x = getWord(%spawn.GetPosition(), 0);
         %y = getWord(%spawn.GetPosition(), 1);
         
         %x = %x-20;
         
         for(%i=0;%i<=4;%i++)
         {
            switch$ (%EnemySquad)
            {
               case 1:
                  %soldier = getWord($EnemySquad[1, soldiers], %i);
               case 2:
                  %soldier = getWord($EnemySquad[2, soldiers], %i);
               case 3:
                  %soldier = getWord($EnemySquad[3, soldiers], %i);
               case 4:
                  %soldier = getWord($EnemySquad[4, soldiers], %i);
               case 5:
                  %soldier = getWord($EnemySquad[5, soldiers], %i);
               case 6:
                  %soldier = getWord($EnemySquad[6, soldiers], %i);
            }
                           
            if(%soldier !$= "")
            {
/*
               %num = getRandom(1, 2);
               
               if(%num == 1)
                  %newy = %y+getRandom(0, 2);
               else
                  %newy = %y+getRandom(0, -2);
*/
               
//               %soldier.SetPosition(%x, %newy);
//               %x = %x+10;               

                  %soldier.SetPosition(getRandom(-100, 100), -120);
                  %soldier.start = true;
   
//               %layer = "infantry" @ $InfantryMap;      
//               %tile = %layer.PickTile(%soldier.GetPosition());
//               %layer.SetTileCustomData(getWord(%tile, 0) SPC getWord(%tile, 1), "block");
            }
            
            MoveEnemyInfantry(%x SPC %y, %EnemySquad);
         }
         
         break;  
      }
   }      
}

// change the squad to either standing, kneeling or laying down

function ChangeEnemySquadPosition(%squad)
{
   switch$ ($EnemySquad[%Squad, position])
   {
      case "Standing":
         %image = "enemy_kneel";         
         $EnemySquad[%Squad, defensemodifier] = 1;
         $EnemySquad[%Squad, position] = "Kneeling";
      case "Kneeling":
         %image = "enemy_prone";
         $EnemySquad[%Squad, defensemodifier] = 2;
         $EnemySquad[%Squad, position] = "Prone";
      case "Prone":
         %image = "enemy_standing";
         // this happens twice since there is no bonus for standing
         $EnemySquad[%Squad, defensemodifier] = 0;
         $EnemySquad[%Squad, position] = "Standing";
   }
   
   %count = getWordCount($EnemySquad[%squad, soldiers]);      
      
   for(%i=0;%i<%count;%i++)
   {
      %soldier = getWord($EnemySquad[%squad, soldiers], %i);   
      %rot = %soldier.GetRotation();  
      %soldier.SetImageMap(%image); 
      %soldier.SetRotation(%rot);     
   }
}

function MoveEnemyInfantry(%coords, %squad)
{
   %layer = "infantry" @ $InfantryMap;
   %tile = %layer.PickTile(%coords);   
   
   %count = getWordCount($EnemySquad[%Squad, soldiers]);
   
   $currentgrid = %layer;

   %soldier = getWord($EnemySquad[%Squad, soldiers], 0);
   %soldier.dest = %coords;
      
   for(%i=1;%i<%count;%i++)
   {   
      %soldier = getWord($EnemySquad[%Squad, soldiers], %i);
      %x = getWord(%tile, 0);
      %y = getWord(%tile, 1);
      
      for(%j=1;%j<=8;%j++)
      {
         %dtile = %x-1 SPC %y;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %north $= "")                  
         {
            %north = true;
            %soldier.dest = getTCenter(%layer, %x-1, %y);
            break;
         }

         %dtile = %x+1 SPC %y;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %south $= "")                  
         {
            %south = true;
            %soldier.dest = getTCenter(%layer, %x+1, %y);
            break;
         }
         
         %dtile = %x SPC %y+1;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %east $= "")                  
         {
            %east = true;
            %soldier.dest = getTCenter(%layer, %x, %y+1);
            break;
         }

         %dtile = %x SPC %y-1;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %west $= "")                  
         {
            %west = true;
            %soldier.dest = getTCenter(%layer, %x, %y-1);
            break;
         }

         %dtile = %x-1 SPC %y+1;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %northwest $= "")                  
         {
            %northwest = true;
            %soldier.dest = getTCenter(%layer, %x-1, %y+1);
            break;
         }

         %dtile = %x-1 SPC %y-1;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %northeast $= "")                  
         {
            %northeast = true;
            %soldier.dest = getTCenter(%layer, %x-1, %y-1);
            break;
         }

         %dtile = %x+1 SPC %y+1;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %southwest $= "")                  
         {
            %southwest = true;
            %soldier.dest = getTCenter(%layer, %x+1, %y+1);
            break;
         }

         %dtile = %x+1 SPC %y-1;
         
         if(%layer.GetTileCustomData(%dtile) $= "" && %southwest $= "")                  
         {
            %southwest = true;
            %soldier.dest = getTCenter(%layer, %x+1, %y-1);
            break;
         }         
      }
   }

   // if each soldier does not have a destination then return
   
   for(%i=0;%i<%count;%i++)
   {
      %soldier = getWord($EnemySquad[%squad, soldiers], %i);
      
      if(%soldier.dest $= "")
         return;         
   }
   
   $EnemySquad[%Squad, moving] = true;
   
   for(%i=0;%i<%count;%i++)
   {
      %soldier = getWord($EnemySquad[%Squad, soldiers], %i);
      %soldier.SetPosition(%soldier.GetPosition());
      %soldier.path = FindInfantryPath(%soldier, %soldier.dest);
      %coords = getWord(%soldier.path, 0) SPC getWord(%soldier.path, 1);
      schedule(%time, 0, "MoveEnemySoldier", %soldier, %coords);
      %soldier.path = removeWord(%soldier.path, 0);
      %soldier.path = removeWord(%soldier.path, 0);
      %angle = t2dAngleToPoint(%soldier.GetPosition(), %coords);
      %soldier.SetRotation(%angle);
      %soldier.nofire = true;
      %time = %time+100;
      %tile = %layer.PickTile(%soldier.GetPosition());
      %layer.SetTileCustomData(getWord(%tile, 0) SPC getWord(%tile, 1), "block");
   }   
}

function MoveEnemySoldier(%soldier, %path)
{
   %soldier.moveto(%path, 25, true, true, true);
}

function EnemyInfantry::OnPositionTarget(%this)
{
   %coords = getWord(%this.path, 0) SPC getWord(%this.path, 1);
   
   %count = getWordCount(%coords);
   
   if(%count == 2)   
   {
      %angle = t2dAngleToPoint(%this.GetPosition(), %coords);
      %angle = %angle+180;
      %this.SetRotation(%angle);
      %this.moveto(%coords, 25, true, true, true);
   }
   else
   {
      // make sure this soldier faces the closest enemy
      %lowestdist = 1000;
      
      for(%i=1;%i<=4;%i++)
      {
         %count = getWordCount($EnemySquad[%i, soldiers]);
         
         if(%count > 0)
         {
            %soldier = getWord($EnemySquad[%i, soldiers], 0);
            
            %dist = VectorDist(%soldier.GetPosition(), %this.GetPosition());
            
            if(%dist < %lowestdist)
            {
               %target = %soldier;
               %lowestdist = %dist;   
            }
         }
      }
      
      %layer = "infantry" @ $InfantryMap;      
      %tile = %layer.PickTile(%soldier.GetPosition());
      %layer.SetTileCustomData(getWord(%tile, 0) SPC getWord(%tile, 1), "block");
      schedule(2000, 0, "ResetEnemyMoveFlag", %this.squad);     
      %angle = t2dAngleToPoint(%target.GetPosition(), %this.GetPosition());
//      %angle = %angle-180;
      %this.SetRotation(%angle);      
      %this.nofire = "";
      %this.dest = "";
      
      if(%this.start $= true)
      {
         %this.SetRotation(0);      
         %this.start = "";      
      }
      
   }
   
   %this.path = removeWord(%this.path, 0);
   %this.path = removeWord(%this.path, 0);
}

function ResetEnemyMoveFlag(%squad)
{
   $EnemySquad[%squad, moving] = "";   
}

function AssignEnemyAmmo()
{
   if(getWordCount($EnemySquad[1, soldiers]) > 0)   
      $EnemySquad[1, ammo] = getRandom(50, 100);   
   if(getWordCount($EnemySquad[2, soldiers]) > 0)   
      $EnemySquad[2, ammo] = getRandom(50, 100);   
   if(getWordCount($EnemySquad[3, soldiers]) > 0)   
      $EnemySquad[3, ammo] = getRandom(50, 100);   
   if(getWordCount($EnemySquad[4, soldiers]) > 0)   
      $EnemySquad[4, ammo] = getRandom(50, 100);   
   if(getWordCount($EnemySquad[5, soldiers]) > 0)   
      $EnemySquad[5, ammo] = getRandom(50, 100);   
   if(getWordCount($EnemySquad[6, soldiers]) > 0)   
      $EnemySquad[6, ammo] = getRandom(50, 100);   
}
