// the flag is for a random event

function PeopleSearch(%flag)
{   
   DisplayTutorial(People);
      
   // if they have zero room don't do anythng
   
   %diff = GetTotalPeopleCapacity() - GetTotalNumPeople();
   
   if(%diff == 0)
   {
      if(%flag $= true)
         return false;
      else
      {
         InfoWindow.Visible = true;
         
         InfoText.setText("Your gang is at full capacity.");   
         
         // hide the battle report button
         
         Infocontinuebutton.Visible = true;
         InfoYesbutton.Visible = false;
         InfoNobutton.Visible = false;
         InfoContinuebutton.Command = "Infowindow.Visible = false;";      
         return;
      }
   }
   
   // if the search screen is up return so it doesn't do anythign again
   
   if(InfoWindow.Visible $= true)
      return false;
      
   if(CheckAllEvents(search) $= true)
      return false;

   // add 1 hour for search
   
   if(UpdateTime() $= true)
      return false;

   echo("SEARCH PEOPLE CLICK");
   
   if(%flag $= "")
      alxPlay(click);
   
   %list = sg.PickPoint(player.GetPosition());
   
   %count = getWordCount(%list);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%list, %i);
      
      if(%obj.city !$= "")
         %city = true;   
   }
   
   %bonus = CheckScout();
      
   if(%city $= true)
      %chance = 60;
   else
   {      
      %distance = FindNearestCity();
      
      if(%distance >= 120)
         %chance = 40;
      if(%distance >= 60 && %distance < 120)
         %chance = 45;
      if(%distance >= 0 && %distance < 60)
         %chance = 50;
   }
   
   %chance = %chance+%bonus;

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         %chance = %chance+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         %chance = %chance-%mod;         
   }
   
   PromoteScout();
   
   %rand = getRandom(1, 100);
   
   echo("FIRST RAND " @ %rand);
      
   if(%rand <= %chance)
      FriendlyPeopleFound();   
   else
   {
      if($mastertime > 12)
      {
         %rand = getRandom(1, 100);
         
         echo("SECOND RAND " @ %rand);
              
         if(%rand <= 10)
            HostilePeopleFound();      
            
         if(%rand == 100)
            RecruitedByCult();
      }
   }
   
   echo("END OF SEARCH PEOPLE");
}

function FriendlyPeopleFound()
{
   %rand = getRandom(1, 13);
      
   switch$ (%rand)
   {
      case 1:
         %level = "Mob";
         %text = "A Mob offers to join your gang. Do you accept?";
         %people = getRandom(15, 30);
      case 2:
         %level = "Mob";
         %text = "A Mob offers to join your gang. Do you accept?";
         %people = getRandom(15, 30);
      case 3:
         %level = "Mob";
         %text = "A Mob offers to join your gang. Do you accept?";
         %people = getRandom(15, 30);
      case 4:
         %level = "Mob";
         %text = "A Mob offers to join your gang. Do you accept?";
         %people = getRandom(15, 30);
      case 5:
         %level = "Bandits";
         %text = "A group of Bandits offers to join your gang. Do you accept?";
         %people = getRandom(10, 25);
      case 6:
         %level = "Bandits";
         %text = "A group of Bandits offers to join your gang. Do you accept?";
         %people = getRandom(10, 25);
      case 7:
         %level = "Bandits";
         %text = "A group of Bandits offers to join your gang. Do you accept?";
         %people = getRandom(10, 25);
      case 8:
         %level = "Militia";
         %text = "A group of Militia offers to join your gang. Do you accept?";
         %people = getRandom(8, 20);
      case 9:
         %level = "Militia";
         %text = "A group of Militia offers to join your gang. Do you accept?";
         %people = getRandom(8, 20);
      case 10:
         %level = "Police";
         %text = "A group of Police offers to join your gang. Do you accept?";
         %people = getRandom(6, 17);
      case 11:
         %level = "Police";
         %text = "A group of Police offers to join your gang. Do you accept?";
         %people = getRandom(6, 17);
      case 12:
         %level = "Soldiers";
         %text = "A group of Soldiers offers to join your gang. Do you accept?";
         %people = getRandom(5, 15);
      case 13:
         %level = "Soldiers";
         %text = "A group of Soldiers offers to join your gang. Do you accept?";
         %people = getRandom(5, 15);
   }
   
   InfoWindow.Visible = true;
   
   InfoText.setText(%text);   
   
   // hide the battle report button
   
   $RecruitLevel = %level;
   $RecruitNumber = %people;
   
   Infocontinuebutton.Visible = false;
   InfoYesbutton.Visible = true;
   InfoNobutton.Visible = true;
   InfoYesbutton.Command = "TakePeople();";
   InfoNobutton.Command = "Infowindow.Visible = false;";
}

function HostilePeopleFound()
{
   %rand = getRandom(1, 8);
      
   switch$ (%rand)
   {
      case 1:
         %Enemy = "Satan Worshippers";
      case 2: 
         %Enemy = "Cultists";
      case 3:
         %Enemy = "Renegade Police";
      case 4:
         %Enemy = "Starving Mob";
      case 5:
         %Enemy = "Militia";
      case 6:
         %Enemy = "Crazies";
      case 7:
         %Enemy = "Cannibals";
      case 8:
         %Enemy = "Renegade National Guard";
   }
   
   %text = "You have been attacked by " @ %Enemy @ "! \n\nClick continue to start the battle!";      
   
   InfoWindow.Visible = true;
   InfoText.SetText(%text);
   Infocontinuebutton.Visible = true;
   InfoYesbutton.Visible = false;
   InfoNobutton.Visible = false;
   InfoContinuebutton.Command = "StartPeopleBattle();";   
}

function TakePeople()
{
   if(Infowindow.Visible $= false)
      return;
      
   // used in keymap so they can't keep hitting yes and get more people
   
   $TakePeople = true;
   
   // number of people recruited
      
   %people = $RecruitNumber+CheckRecruiter();
   
   PromoteRecruiter();
   
   // check gang capacity

   %diff = GetTotalPeopleCapacity() - GetTotalNumPeople();
   
   if(%people > %diff)
      %people = %diff;
   
   $recruitcounter = 0;

   InfoYesbutton.Visible = false;
   InfoNobutton.Visible = false;
   Infocontinuebutton.Visible = false;
   InfoContinuebutton.Command = "Infowindow.Visible = false;";   
   InfoText.setText("");
   
   $Marching = alxPlay(marching);

   for(%i=1;%i<=%people;%i++)
   {
      schedule(%time, 0, "RecruitPerson");
      
      %time = %time+500;      
   }
   
   schedule(%time+500, 0, "CloseRecruitment");   
}

function RecruitPerson()
{
   // randomly assign who they picked up
   // they can't pick up mercs and there will be a bigger chance of the lower ranks
      
   switch$ ($RecruitLevel)
   {
      case "mob":
         %rand = getRandom(1, 4);

         if(%rand == 1)
         {         
            $Brigands++;
            %recruit = "Brigand";             
            $Recruitcounter++;
         }         
         else
         {
            $refuges++;
            %recruit = "Refuge";             
            $Recruitcounter++;            
         }
      
      case "bandits":
         %rand = getRandom(1, 3);
         
         if(%rand == 1)
         {
            $refuges++;
            %recruit = "Refuge";             
            $Recruitcounter++;            
         }
         else
         {         
            $Brigands++;
            %recruit = "Brigand";             
            $Recruitcounter++;
         }
         
      case "militia":
         %rand = getRandom(1, 5);
         
         if(%rand == 1)
         {
            $refuges++;
            %recruit = "Refuge";             
            $Recruitcounter++;            
         }
         
         if(%rand == 2)
         {         
            $Brigands++;
            %recruit = "Brigand";             
            $Recruitcounter++;
         }
         
         if(%rand >= 3)
         {                  
            $Militia++;
            %recruit = "Militia";             
            $Recruitcounter++;
         }
         
      case "police":
         %rand = getRandom(1, 6);
         
         if(%rand == 1)
         {         
            $Brigands++;
            %recruit = "Brigand";             
            $Recruitcounter++;
         }
         
         if(%rand == 2)
         {                  
            $Militia++;
            %recruit = "Militia";             
            $Recruitcounter++;
         }

         if(%rand >= 3)
         {                  
            $Police++;
            %recruit = "Cop";             
            $Recruitcounter++;
         }
      
      case "soldiers":
         %rand = getRandom(1, 7);
         
         if(%rand == 1)
         {                  
            $Militia++;
            %recruit = "Militia";             
            $Recruitcounter++;
         }

         if(%rand == 2)
         {                  
            $Police++;
            %recruit = "Cop";             
            $Recruitcounter++;
         }
         
         if(%rand >= 3)
         {
            $Soldiers++;
            %recruit = "Soldier";             
            $Recruitcounter++;
         }
   }

   if($Recruitcounter == 9)   
   {      
      InfoText.setText("");
      $Recruitcounter = 0;
   }

   InfoText.AddText("A " @ %recruit @ " joined your gang\n", true);
       
   UpdateStatsBar();   
}

function CheckForRandomPeopleEncounter(%flag)
{
   switch$ (%flag)
   {
      case "vehicle":
         %chance = 3;      
      case "supply":
         %chance = 3;      
      case "random":
         %chance = 13;      
   }
   
   %rand = getRandom(1, 100);
   
   if(%rand <= %chance)
   {
      // only call it if they have capacity to accept new recruits
      
      %capacity = GetTotalPeopleCapacity();
      %people = GetTotalCombatPeople();
      
      if(%capacity !$= %people)
      {
         %ret = PeopleSearch(true);
         
         if(%ret $= false)
            return false;
         
         return true;
      }
   }
}

function CloseRecruitment()
{
   Infowindow.Visible = false;
   $recruitcounter = 0;
   $TakePeople = "";
   alxStop($Marching);
}


// cult recruitment

function RecruitedByCult()
{
   infowindow.Visible = true;
   Infocontinuebutton.Visible = true;
   InfoYesbutton.Visible = false;
   InfoNobutton.Visible = false;
   infocontinuebutton.Command = "StartDefection();";   
   InfoText.setText("Some of your gang members have been brainwashed by members of The Children of Tomorrow!");
}

function StartDefection()
{
   %people = getRandom(5, 15);
   
   if(%people > GetTotalNumPeople())
      %people = GetTotalCombatPeople() / 2;   

   InfoYesbutton.Visible = false;
   InfoNobutton.Visible = false;
   Infocontinuebutton.Visible = false;
   InfoContinuebutton.Command = "Infowindow.Visible = false;";   
   InfoText.setText("");

   $recruitcounter = 0;
      
   for(%i=1;%i<=%people;%i++)
   {
      schedule(%time, 0, "GangDefects");
      
      %time = %time+500;      
   }
   
   schedule(%time+500, 0, "CloseRecruitment");   
}

function GangDefects()
{
   // randomly assign who they picked up
   // they can't pick up mercs and there will be a bigger chance of the lower ranks
   
   while(%recruit $= "")
   {      
      %rand = getRandom(1, 9);
      
      switch$ (%rand)
      {
         // 3 chances it's a refuge
         case 1:
            if($refuges > 0)
            {
               $refuges--;
               %recruit = "Refuge";             
               $Recruitcounter++;
            }
         case 2:
            if($refuges > 0)
            {
               $refuges--;
               %recruit = "Refuge";             
               $Recruitcounter++;
            }
         case 3:
            if($refuges > 0)
            {
               $refuges--;
               %recruit = "Refuge";             
               $Recruitcounter++;
            }
         // 2 chances it's a brigand
         case 4:
            if($Brigands > 0)
            {
               $Brigands--;
               %recruit = "Brigand";             
               $Recruitcounter++;
            }
         case 5:
            if($Brigands > 0)
            {
               $Brigands--;
               %recruit = "Brigand";             
               $Recruitcounter++;
            }
         // 2 chances it's militia
         
         case 6:
            if($militia > 0)
            {
               $Militia--;
               %recruit = "Militia";             
               $Recruitcounter++;
            }
         case 7:
            if($militia > 0)
            {
               $Militia--;
               %recruit = "Militia";             
               $Recruitcounter++;
            }
         case 8:
            if($Police > 0)
            {
               $Police--;
               %recruit = "Cop";             
               $Recruitcounter++;
            }
         case 9:
            if($Soldiers > 0)
            {
               $Soldiers--;
               %recruit = "Soldier";             
               $Recruitcounter++;
            }
      }
   }
   if($Recruitcounter == 9)   
   {      
      InfoText.setText("");
      $Recruitcounter = 0;
   }
   
   InfoText.AddText(%recruit @ " defected! \n", true);
       
   UpdateStatsBar();   
}

// this function will be shared by all 3 search functions

function FindNearestCity()
{
   %list[1] = NewYork;
   %list[2] = LosAngeles;
   %list[3] = Chicago;
   %list[4] = Houston;
   %list[5] = Phoenix;
   %list[6] = Philadelphia;
   %list[7] = SanAntonio;
   %list[8] = Dallas;
   %list[9] = SanDiego;
   %list[10] = Detroit;
   %list[11] = SanFrancisco;
   %list[12] = Jacksonville;
   %list[13] = Austin;
   %list[14] = Columbus;
   %list[15] = Charlotte;
   %list[16] = Memphis;
   %list[17] = Baltimore;
   %list[18] = ElPaso;
   %list[19] = Boston;
   %list[20] = Denver;
   %list[21] = Seattle;
   %list[22] = Nashville;
   %list[23] = Washington;
   %list[24] = LasVegas;
   %list[25] = Portland;
   %list[26] = Louisville;
   %list[27] = Tucson;
   %list[28] = Atlanta;
   %list[29] = Albuqurque;
   %list[30] = Fresno;
   %list[31] = Sacramento;
   %list[32] = KansasCity;
   %list[33] = Omaha;
   %list[34] = Cleveland;
   %list[35] = Miami;
   %list[36] = Raleigh;
   %list[37] = Tulsa;
   %list[38] = Minneapolis;
   %list[39] = ColoradoSprings;
   %list[40] = Wichita;
   %list[41] = StLouis;
   %list[42] = Tampa;
   %list[43] = Bakersfield;
   %list[44] = NewOrleans;
   %list[45] = Pittsburgh;
   %list[46] = Riverside;
   %list[47] = Stockton;
   %list[48] = CorpusChristi;
   %list[49] = Buffalo;
   %list[50] = JerseyCity;
   %list[51] = Norfolk;
   %list[52] = Orlando;
   %list[53] = Birmingham;
   %list[54] = Reno;
   %list[55] = SaltLakeCity;   
   %list[56] = Spokane;   
   
   %dist = 10000;
   
   for(%i=1;%i<=56;%i++)
   {
      %city = %list[%i];
      
      %tempdist = VectorDist(player.GetPosition(), %city.GetPosition());
      
      if(%tempdist < %dist)
      {
         %dist = %tempdist;
         %closestcity = %city;                  
      }
   }
   
   return %dist;
}

