// check for special place will not activate if they are searching for supplies while on a special place

function CheckAllEvents(%flag)
{
   if(SpecialistSearch() $= true)
   {
      alxStop($MoveSound);
      echo("SPECIALIST SEARCH TRUE");
      return true;
   }

   if(CheckForDisease() $= true)
   {
      alxStop($MoveSound);
      echo("DISEASE TRUE");
      return true;
   }

   if($checkFoodConTime >= $mastertime)
   {      
      if(CheckFoodContamination() $= true)
      {
         alxStop($MoveSound);
         echo("FOR CONTAMINATION TRUE");
         return true;
      }
   }
   
   if(%flag $= "")
   {
      if(CheckForSpecialPlace() $= true)
      {
         alxStop($MoveSound);
         echo("CHECK FOR SPECIAL PLACE");
         return true;
      }
   }
      
   if(CheckForCombat() $= true)
   {
      alxStop($MoveSound);
      echo("COMBAT TRUE");
      return true;
   }
   
   if(CheckForRandomPeopleEncounter(random) $= true)
   {
      alxStop($MoveSound);
      echo("RANDOM PEOPLE TRUE");
      return true;
   }
   
   if(%flag $= "")
   {
      if(FindRandomSupplies() $= true)
      {
         alxStop($MoveSound);
         echo("RANDOM SUPPLIES TRUE"); 
         return true;
      }
   }
   
   if(%flag $= "")
   {
      if(FindRandomVehicle() $= true)   
      {
         alxStop($MoveSound);
         echo("RANDOM VEHICLE TRUE");
         return true;
      }
   }
   
   if($mastertime >= $RecurringMoraleCheck)
   {
      %check = MoraleCheck("", true);
      alxStop($MoveSound);
      echo("MORALE CHECK TRUE");
      return %check;
   }
}

// there is a random chance the gang will become infected, if that happens a doctor 
// will be able to have less people die but only enough meds will actually cure
// the gang
// %flag is used for a special event

function CheckForDisease(%flag)
{
   // if they already have disease ignore this check
   
   if($disease $= true)
      return;
      
   %rand = getRandom(1, 100);
   
   // disease has struck
   
   %num = 2;
   
   if($Doctor $= true)
      %num = 1;
   
   if($mastertime <= 12)   // so they won't get a disease in the first 12 hours
      %rand = 100;
      
   if(%rand <= %num || %flag $= true)
   {
      DecreaseMorale(1);
      
      $disease = true;            

      %rand = getRandom(10, 20);

      %people = GetTotalCombatPeople();

      %rand = "." @ %rand;    
      
      %num = %people*%rand;  
      
      %num = mCeil(%num); 
            
      // if they have a doctor it will reduct the effect
      
      %doctor = CheckDoctor();
      
      if(%doctor > 0)
      {
         %doctor = "." @ %doctor;
         %reduction = %num*%doctor;
         %num = %num-%reduction;
      }

      %num = mCeil(%num); 
      
      echo("PEOPLE DEAD FROM DISEASE " @ %num);
      
      // zero everthing out for the report
      
      %refuges = 0;
      %brigands = 0;
      %militia = 0;
      %police = 0;
      %soldiers = 0;
      %mercenaries = 0;
      
      while(%num !$= 0)
      {
         %rand = getRandom(1, 6);
         
         switch$ (%rand)
         {
            case 1:
               if($refuges > 0)
               {
                  $refuges--;
                  %refuges++;
                  %num--;     
               }
            case 2:
               if($Brigands > 0)
               {
                  $Brigands--;
                  %brigands++;
                  %num--;
               }               
            case 3:
               if($Militia > 0)
               {
                  $Militia--;
                  %militia++;
                  %num--;
               }
            case 4:
               if($Police > 0)
               {
                  $Police--;
                  %police++;
                  %num--;
               }
            case 5:
               if($soldiers > 0)
               {
                  $soldiers--;
                  %soldiers++;
                  %num--;
               }
            case 6:
               if($Mercenaries > 0)
               {
                  $Mercenaries--;
                  %mercenaries++;
                  %num--;
               }
         }
      }

      // display the report
      
      Infowindow.Visible = true;
      InfoYesbutton.Visible = false;
      InfoNobutton.Visible = false;
      Infocontinuebutton.Visible = true;
      Infocontinuebutton.Command = "Infowindow.Visible = false;";

      Infotext.setText("");
      Infotext.AddText("Your gang has contracted a disease! You lost - \n\n", true);
      Infotext.AddText("Refuges             " @ %refuges @ "\n", true);
      Infotext.AddText("Brigands            " @ %brigands @ "\n", true);
      Infotext.AddText("Militia             " @ %militia @ "\n", true);
      Infotext.AddText("Police              " @ %police @ "\n", true);
      Infotext.AddText("Soldiers             " @ %soldiers @ "\n", true);
      Infotext.AddText("Mercenaries  " @ %mercenaries, true);   
      
      UpdateStatsBar();
      
      $diseasetime = $mastertime+24;
      
      return true;
   }
}

// this is if the gang is already infected, this will be called every 24 hours after the infection occurs

function Infected()
{
   %rand = getRandom(5, 20);

   %people = GetTotalCombatPeople();

   %rand = "." @ %rand;    
   
   %num = %people*%rand;  
   
   %num = mCeil(%num); 
   
   DecreaseMorale(1);
   
   // if they have a doctor it will reduct the effect
   
   %doctor = CheckDoctor();
   
   if(%doctor > 0)
   {
      %doctor = "." @ %doctor;
      %num = %num*%doctor;
   }

   %num = mCeil(%num); 
   
   // zero everthing out for the report
   
   %refuges = 0;
   %brigands = 0;
   %militia = 0;
   %police = 0;
   %soldiers = 0;
   %mercenaries = 0;
   
   while(%num !$= 0)
   {
      %rand = getRandom(1, 6);
      
      switch$ (%rand)
      {
         case 1:
            if($refuges > 0)
            {
               $refuges--;
               %refuges++;
               %num--;     
            }
         case 2:
            if($Brigands > 0)
            {
               $Brigands--;
               %brigands++;
               %num--;
            }               
         case 3:
            if($Militia > 0)
            {
               $Militia--;
               %militia++;
               %num--;
            }
         case 4:
            if($Police > 0)
            {
               $Police--;
               %police++;
               %num--;
            }
         case 5:
            if($Soldiers > 0)
            {
               $Soldiers--;
               %soldiers++;
               %num--;
            }
         case 6:
            if($Mercenaries > 0)
            {
               $Mercenaries--;
               %mercenaries++;
               %num--;
            }
      }
   }
   
   // display the report
   
   infowindow.Visible = true;
   InfoYesbutton.Visible = false;
   InfoNobutton.Visible = false;
   Infocontinuebutton.Visible = true;
   infocontinuebutton.command = "Infowindow.Visible = false;";   
   $ContinueButton = "hide";
   
   infotext.setText("");
   infotext.AddText("*Your gang is still infected! You lost - *\n\n", true);
   infotext.AddText("Refuges      " @ %refuges @ "\n", true);
   infotext.AddText("Brigands     " @ %brigands @ "\n", true);
   infotext.AddText("Militia      " @ %militia @ "\n", true);
   infotext.AddText("Police       " @ %police @ "\n", true);
   infotext.AddText("Soldiers     " @ %soldiers @ "\n", true);
   infotext.AddText("Mercenaries  " @ %mercenaries @ "\n", true);   
   
   UpdateStatsBar();
   
   // update for next infection
   
   $diseasetime = $mastertime+24;
}

// there is a slight chance throughout the game that a portion of the gang's food supply will become
// contaminated and spoil

function CheckFoodContamination()
{
   %rand = getRandom(1, 100);
   
   // this will use the info window, if whatever the reason the info window is up when this happens
   // then just disregard it
   
   if(%rand <= 1 && infowindow.Visible $= false)
   {
      // if they have an ambulance this almost never happens
      
      for(%i=1;%i<=10;%i++)
      {
         if($vehicles[%i, vehicle] $= "ambulance")
         {
            %roll = getRandom(1, 100);
            
            if(%roll <= 99)
               return;            
         }         
      }
      
      %rand = getRandom(20, 60);
      
      %percent = "." @ %rand;
      
      %foodlost = $food*%percent;      
      %foodlost = mCeil(%foodlost);
      DecreaseFood(%foodlost);
      
      UpdateStatsBar();

      infowindow.Visible = true;
      Infocontinuebutton.Visible = true;
      InfoNobutton.Visible = false;
      InfoYesbutton.Visible = false;
      
      infotext.setText("Your food supply has become contaminated! You have lost " @ %foodlost @ " food!");            
      
      infocontinuebutton.Command = "Infowindow.Visible = false;";            
      
      return true;
   }
   
   %time = $mastertime;
   
   $checkFoodConTime = %time+getRandom(24, 72);
}