function DecreaseMorale(%num)
{   
   $morale = $morale-%num;
   
   if($morale < 1)
      $morale = 1;
      
   UpdateStatsBar();
   
   if($RecurringMoraleCheck $= "")   
      $RecurringMoraleCheck = $mastertime+24;
}

function IncreaseMorale()
{
   if($morale < 10)
      $morale++;   
      
   if($morale == 10)
      $RecurringMoraleCheck = "";
}

// if the gang goes without any vehicle combat for 1 day they will lose morale, will probably rarely happen

function CombatMoralCheck()
{
   %diff = $mastertime-$lastCombat;
   
   if(%diff >= 48)
   {
      $lastCombat = $mastertime;
      DecreaseMorale(3);   
   }
}

// if the gang fails a morale check some people leave

function MoraleCheck(%flag, %recurring)
{  
   if(Infowindow.Visible $= true)
      return;
      
   %morale = $morale*10;
      
   %check = getRandom(1, 100);
   
   if(%check > %morale || %flag $= true)
   {
      // if it's a recurring morale check they will won't lose as much, recurring happens every 24 hours
      // after moale isn't 10
      
      if(%recurring $= true)      
      {
         %rand = getRandom(5, 15);
         $RecurringMoraleCheck = $mastertime+24;
      }
      else
         %rand = getRandom(5, 20);

      %people = GetTotalCombatPeople();
      %people = %people-GetNumVehicles();
      
      if(%people == 0)
      {
         Infowindow.Visible = false;
         return;
      }
      
      if(%rand < 10)   
         %rand = ".0" @ %rand;    
      else
         %rand = "." @ %rand;          
      
      %num = %people*%rand;  
      
      %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;";   
      
      InfoText.setText("");
      InfoText.AddText("* Some of your gang has deserted you! 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();   
      
      return true;
   }
}