// add up all the cars and get how many people total the gang can have

function GetTotalPeopleCapacity()
{
   // go through each vehicle, let's assume it won't be more then 50
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
         %totalcapacity = %totalcapacity+GetVehiclePeopleCapacity($vehicles[%i, vehicle]);
   }
   
   return %totalcapacity;
}

function GetTotalNumPeople()
{
    return $refuges+$Brigands+$Militia+$Police+$Soldiers+$Mercenaries+$Doctor+$Mechanic+$scout+$Sergeant+$Recruiter;    
}

// used for people battles

function GetTotalCombatPeople()
{
    return $refuges+$Brigands+$Militia+$Police+$soldiers+$Mercenaries;    
}

// this is called after every vehicle and melee battle to promote the troops

function Promotion()
{
   %bonus = CheckSergeant();
   %promo = getRandom(5, 15);
   
   if(%bonus > 0)
      %promo = "." @ %promo+%bonus;    
   else
      %promo = ".0" @ %promo;    
   
   // promo will be a percentage, find out how many people get promoted   
   
   %people = GetTotalCombatPeople();
   
   %promonum = %people*%promo;  
   
   %promonum = mCeil(%promonum); 
   
   // if the promotion number is greater then the number of people that can be promoted - round it off
   
   %totalpromo = $refuges+$Brigands+$militia+$Police+$Soldiers;
   
   if(%totalpromo < %promonum)
      %promonum = %totalpromo;
   
   echo("TOTAL PROMOTIONS " @ %promonum);
         
   while(%promonum > 0)
   {
      %rand = getRandom(1, 15);
            
      switch$ (%rand)
      {
         // 3 chances it's a refuge
         case 1:
            if($refuges > 0)
            {
               $refuges--;
               $Brigands++;
               %promonum--;     
               echo("REFUGE PROMOTED");
            }
         case 2:
            if($refuges > 0)
            {
               $refuges--;
               $Brigands++;
               %promonum--;     
               echo("REFUGE PROMOTED");
            }
         case 3:
            if($refuges > 0)
            {
               $refuges--;
               $Brigands++;
               %promonum--;     
               echo("REFUGE PROMOTED");
            }
         case 4:
            if($refuges > 0)
            {
               $refuges--;
               $Brigands++;
               %promonum--;     
               echo("REFUGE PROMOTED");
            }
         case 5:
            if($refuges > 0)
            {
               $refuges--;
               $Brigands++;
               %promonum--;     
               echo("REFUGE PROMOTED");
            }
         
         // 2 chances it's a brigand
         case 6:
            if($Brigands > 0)
            {
               $Brigands--;
               $Militia++;
               %promonum--;
               echo("BRIGANDS PROMOTED");
            }
         case 7:
            if($Brigands > 0)
            {
               $Brigands--;
               $Militia++;
               %promonum--;
               echo("BRIGANDS PROMOTED");
            }
         case 8:
            if($Brigands > 0)
            {
               $Brigands--;
               $Militia++;
               %promonum--;
               echo("BRIGANDS PROMOTED");
            }
         case 9:
            if($Brigands > 0)
            {
               $Brigands--;
               $Militia++;
               %promonum--;
               echo("BRIGANDS PROMOTED");
            }
            
         // 2 chances it's militia
         
         case 10:
            if($Militia > 0)
            {
               $Militia--;
               $Police++;
               %promonum--;
               echo("MILITIA PROMOTED");
            }
         case 11:
            if($Militia > 0)
            {
               $Militia--;
               $Police++;
               %promonum--;
               echo("MILITIA PROMOTED");
            }
         case 12:
            if($Militia > 0)
            {
               $Militia--;
               $Police++;
               %promonum--;
               echo("MILITIA PROMOTED");
            }

         case 13:
            if($Police > 0)
            {
               $Police--;
               $Soldiers++;
               %promonum--;
               echo("POLICE PROMOTED");
            }
         case 14:
            if($Police > 0)
            {
               $Police--;
               $Soldiers++;
               %promonum--;
               echo("POLICE PROMOTED");
            }
         case 15:
            if($soldiers > 0)
            {
               $Soldiers--;
               $Mercenaries++;
               %promonum--;
               echo("SOLDIER PROMOTED");
            }
      }
   }
}

//////// every time these check functions are called it will increase their experience by 1
//////// and return a value 

function CheckSergeant()
{
   if($Sergeant $= "")
      return 0;
      
   if($SergeantExperience <= 20)
      return 5;
   if($SergeantExperience > 20 && $SergeantExperience <= 40)
      return 10;
   if($SergeantExperience > 40 && $SergeantExperience <= 60)
      return 15;
   if($SergeantExperience > 60 && $SergeantExperience <= 80)
      return 20;
   if($SergeantExperience > 80)
      return 30;
}

function PromoteSergeant()
{
   if($Sergeant $= "")
      return;
      
   $SergeantExperience++;   

   return true;
}

function CheckDoctor()
{
   if($Doctor $= "")
      return 0;
         
   if($doctorExperience <= 10)
      return 15;
   if($doctorExperience > 10 && $doctorExperience <= 20)
      return 20;
   if($doctorExperience > 20 && $doctorExperience <= 30)
      return 25;
   if($doctorExperience > 30 && $doctorExperience <= 40)
      return 30;
   if($doctorExperience > 40)
      return 35;
}

function PromoteDoctor()
{
   if($Doctor $= "")
      return;
      
   $doctorExperience++;
   
   return true;
}

function CheckRecruiter()
{
   if($Recruiter $= "")
      return 0;
         
   if($RecruiterExperience <= 10)
      return 5;
   if($RecruiterExperience > 10 && $RecruiterExperience <= 20)
      return 10;
   if($RecruiterExperience > 20 && $RecruiterExperience <= 30)
      return 15;
   if($RecruiterExperience > 30 && $RecruiterExperience <= 40)
      return 20;
   if($RecruiterExperience > 40)
      return 25;
}

function PromoteRecruiter()
{
   if($Recruiter $= "")
      return;
      
   $RecruiterExperience++;
   
   return true;
}

function CheckScout()
{
   if($scout $= "")
      return 0;
      
   if($ScoutExperience <= 300)
      return 4;
   if($ScoutExperience > 300 && $ScoutExperience <= 600)
      return 6;
   if($ScoutExperience > 600 && $ScoutExperience <= 900)
      return 8;
   if($ScoutExperience > 900 && $ScoutExperience <= 1200)
      return 10;
   if($ScoutExperience > 1200)
      return 15;
}

function PromoteScout()
{
   if($scout $= "")
      return;
      
   $ScoutExperience++;   

   return true;
}

// the number returned will be the extra number of spare parts that can be repaired

function CheckMechanic()
{
   if($Mechanic $= "")
      return 0;
   
   if($MechanicExperience <= 50)
      return 1;
   if($MechanicExperience > 50 && $MechanicExperience <= 100)
      return 2;
   if($MechanicExperience > 100 && $MechanicExperience <= 150)
      return 3;
   if($MechanicExperience > 150 && $MechanicExperience <= 200)
      return 4;
   if($MechanicExperience > 200)
      return 5;
}

function CheckMechanicTonnage()
{
   if($Mechanic $= "")
      return 0;
   
   if($MechanicExperience <= 50)
      return 2;
   if($MechanicExperience > 50 && $MechanicExperience <= 100)
      return 4;
   if($MechanicExperience > 100 && $MechanicExperience <= 150)
      return 6;
   if($MechanicExperience > 150 && $MechanicExperience <= 200)
      return 8;
   if($MechanicExperience > 200)
      return 10;
}


function PromoteMechanic()
{
   if($Mechanic $= "")
      return;
      
   $MechanicExperience++;

   return true;
}

// thre will be X amount of specialists placed in random cities, only 1 specialist in a city

function PlaceSpecialPeople()
{
   for(%i=1;%i<=2;%i++)
      ChooseSpecialistCity("doctor");

   for(%i=1;%i<=2;%i++)
      ChooseSpecialistCity("mechanic");      

   for(%i=1;%i<=2;%i++)
      ChooseSpecialistCity("sergeant");

   for(%i=1;%i<=2;%i++)
      ChooseSpecialistCity("recruiter");

   for(%i=1;%i<=2;%i++)
      ChooseSpecialistCity("scout");
}

// beause the number of cities will change this will be a while loop

function ChooseSpecialistCity(%special)
{
   while(%return $= "")
   {
      %match = "";
      
      %rand = getRandom(1, 56);
      
      %city = GetCityName(%rand);
      
      // first make sure the city isn't already taken

      for(%i=1;%i<=10;%i++)
         if(getWord($SpecialistLocations[%i], 0) $= %city)
            %match = true;
      
      // now place it assuming the city is free
       
      if(%match $= "")
      {      
         for(%i=1;%i<=10;%i++)
         {
            if($SpecialistLocations[%i] $= "")
            {
               $SpecialistLocations[%i] = %city SPC %special;
               %return = true;       
               break;              
            }
         }
      }
   }
}

// used by the above function, to get a city name by number, if you add a city to the map you must
// add it to this list

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

// specialists functions ///////////////////////////////////////////////////////////////////////

function DisplayFoundSpecialist(%special)
{
   Infowindow.Visible = true;
   Infotext.setText("Your gang has found a " @ %special SPC "!");   
   
   Infocontinuebutton.Visible = true;
   InfoYesbutton.Visible = false;
   InfoNobutton.Visible = false;
   Infocontinuebutton.Command = "Infowindow.Visible = false;";      

   // if they are at full capacity kick someone out of the gang
   
   %num = GetTotalNumPeople();
   %num = %num-1;
   
   if(GetTotalPeopleCapacity() $= %num)
   {
      if($refuges > 0)
      {
         $refuges--;
         return;         
      }
      if($Brigands > 0)
      {
         $Brigands--;
         return;  
      }
      if($militia > 0)
      {
         $militia--;
         return;
      }
      if($Police > 0)
      {
         $Police--;
         return;
      }
      if($Soldiers > 0)
      {
         $Soldiers--;
         return;
      }
      if($Mercenaries > 0)
      {
         $Mercenaries--;
         return;
      }
      
      UpdateStatsBar();      
   }
}

function SpecialistSearch()
{
   %objects = sg.PickPoint(player.GetPosition());
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.city !$= "")
         %city = %obj.city;
   }
   
   if(%city $= "")
      return;
      
   // if they don't have it then just give it to the player
   
   for(%i=1;%i<=10;%i++)
   {
      if(getWord($SpecialistLocations[%i], 0) $= %city)            
      { 
         %specialist = getWord($SpecialistLocations[%i], 1);               
                    
         switch$ (%specialist)
         {
            case "doctor":              
               if($doctor !$= true)
               {
                  $doctor = true;
                  DisplayTutorial(doctor);
                  DisplayFoundSpecialist("Doctor");
                  $SpecialistLocations[%i] = "";
                  return true;
               }
            case "mechanic":              
               if($Mechanic !$= true)
               {
                  $Mechanic = true;
                  DisplayTutorial(mechanic);
                  DisplayFoundSpecialist("Mechanic");
                  $SpecialistLocations[%i] = "";
                  return true;
               }
            case "sergeant":              
               if($Sergeant !$= true)
               {
                  $Sergeant = true;
                  DisplayTutorial(sargeant);
                  DisplayFoundSpecialist("Sergeant");
                  $SpecialistLocations[%i] = "";
                  return true;
               }
            case "recruiter":              
               if($Recruiter !$= true)
               {
                  $Recruiter = true;
                  DisplayTutorial(recruiter);
                  DisplayFoundSpecialist("Recruiter");
                  $SpecialistLocations[%i] = "";
                  return true;
               }
            case "scout":              
               if($scout !$= true)
               {
                  $scout = true;
                  DisplayTutorial(scout);
                  DisplayFoundSpecialist("Scout");
                  $SpecialistLocations[%i] = "";
                  return true;
               }
         }         
      }
   }
            
   for(%i=1;%i<=$remainingsilos;%i++)
   {   
      if($ScientistLocations[%i] $= %city)
      {
         DisplayTutorial(scientist);
         DisplayFoundSpecialist("Scientist");
         $ScientistLocations[%i] = "";
         $scientists++;         
         return true;
      }
   }
}