// This is called after a player wins a vehicle battle

function AfterCombat()
{   
   BackToWorldStart();

   IncreaseMorale();
   
   echo("AFTER COMBAT CALLED");
   
   // this needs to happen first so their supply capacity is correct
   
   CleanUpVehicles();
   
   // stop sounds
   
   alxStopAll();
   
   if(isObject($EnemyIntel) $= true)
      $enemyIntelControl.Delete();
      
   // first find out if the player actually lost supplies

   ReAddMen();
   ReAddAmmo();
   ReToolGuns();  // called either way
            
   %capacity = GetTotalSupplyCapacity();
   
   %supplies = GetCurrentSupplyCount();
   
   // the lost supplies
      
   if(%supplies > %capacity)
   {
      %supplieslost = true;
      
      echo("player lost supplies"); // for debug
      
      InfoWindow.Visible = true;
      InfoYesbutton.Visible = false;
      InfoNobutton.Visible = false;
      Infocontinuebutton.Visible = true;
      InfoContinuebutton.command = "Infowindow.Visible = false;";      

      InfoText.setText("");

      %guns = ReToolGuns(); // called either way
      
      while(%supplies > %capacity)
      {
         %rand = getRandom(1, 7); 
                  
         switch$ (%rand)
         {
            case 1:
               if($food > 0)
               {
                  DecreaseFood(1);
                  %supplies--;
                  %food++;
               }
            case 2:
               if($guns > 0)
               {                  
                  DecreaseGuns(1);
                  %supplies--;
                  %guns++;            
               }
            case 3:
               if($ammo > 0)
               {
                  DecreaseAmmo(1);
                  %supplies--;            
                  %ammo++;
               }
            case 4:
               if($medicine > 0)
               {
                  DecreaseMedicine(1);
                  %supplies--;
                  %medicine++;            
               }
            case 5:
               if($tires > 0)
               {
                  DecreaseTires(1);
                  %supplies--;
                  %tires++;            
               }
            case 6:
               if($spareparts > 0)
               {
                  DecreaseSpareParts(1);
                  %supplies--;  
                  %spareparts++;          
               }
            case 7:
               if($fuel > 0)
               {
                  DecreaseFuel(1);
                  %supplies--;  
                  %fuel++;          
               }
         }      
      }
      
      InfoText.addText("You have lost supplies!\n\n", true);      
   
      if(%food > 0)
         InfoText.addText("You have lost " @ %food @ " food.\n", true);                  
      if(%guns > 0)
         InfoText.addText("You have lost " @ %guns @ " guns.\n", true);                                       
      if(%ammo > 0)
         InfoText.addText("You have lost " @ %ammo @ " ammo.\n", true);                           
      if(%medicine > 0)
         InfoText.addText("You have lost " @ %medicine @ " medicine.\n", true);                                       
      if(%tires > 0)
         InfoText.addText("You have lost " @ %tires @ " tires.\n", true);                                       
      if(%spareparts > 0)
         InfoText.addText("You have lost " @ %spareparts @ " spareparts.\n", true);                                     
      if(%fuel > 0)          
         InfoText.addText("You have lost " @ %fuel @ " fuel.\n", true);    
   }

   // since they didn't lose any supplies they can take some supplies      
   
   if(%supplieslost $= "")
   {
      echo("SHOULD GAIN SUPPLIES");
      
      $SearchIndex = 1;
      $StopSearchFlag = "";   // reset

      // determine what supplies have been found
      
      // gang will always have food
      
      echo("FOOD FOUND IN AFTERCOMBAT BEFORE BEING MODIFIED " @ $foodFound);
      
      %rand = getRandom(1, 4);      
      $foodFound = $TotalEnemyMen*%rand;
      
      if($foodFound <= 0)
         $foodFound = getRandom(1, 10);
               
      echo("food found " @ $foodFound);
      
      // some guns will always be lost
      
      echo("GUNS FOUND IN AFTER COMBAT BEFORE BEING MODIFIED " @ $gunsFound);
      
      %rand = getRandom(10, 90);
      %rand = "." @ %rand;
      $gunsFound = $enemyGuns*%rand;
      $gunsFound = mCeil($gunsFound);
      
      if($gunsFound <= 0)
         $gunsFound = getRandom(1, 10);
               
      $ammoFound = getRandom(200, 650);
      
      %rand = getRandom(1, 100);
      
      if(%rand <= 30)
      {
         %rand = getRandom(5, 10);
         $medicineFound = %rand;
      }

      %rand = getRandom(1, 100);
      
      if(%rand <= 40)
      {
         %rand = getRandom(3, 35);
         $tiresFound = %rand;
      }
      
      %rand = getRandom(1, 100);
      
      if(%rand <= 25)
      {
         %rand = getRandom(2, 32);
         $sparepartsFound = %rand;   
      }
      
      // will always have some fuel
      
      %rand = getRandom(30, 50);
      $fuelFound = $Enemyvehicles[numvehicles]*%rand;
      
      echo("fuel found " @ $fuelFound);
      
      // now send this to the supplies search function where it will take over
      
      SearchReturn();
   }
   
   // don't know what would cause a -1 ammo but this is insurance
   
   if($ammo < 0)
      $ammo = 0;
      
   Promotion();
   ReplaceTires();
   PromoteSergeant();
      
   DisplayTutorial(AfterCombat);
   
   $wind = alxPlay(wind);   
}

// some guns will always be lost during a battle do to being too old, too used, etc. This will force the player
// to keep getting guns instead of just getting a bunch and never needing them again

function ReToolGuns()
{
   %rand = getRandom(3, 10);
   
   if(%rand < 10)
      %rand = "0" @ %rand;
      
   %rand = "." @ %rand;
   
   %gunslost = $guns*%rand;
   %gunslost = mCeil(%gunslost);
   
   DecreaseGuns(%gunslost);   
   return %gunslost;
}

// if any vehicles have been destroyed (no battleid) then destroy them here

function CleanUpVehicles()
{
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "" && $vehicles[%i, battleid] $= "" && $vehicles[%i, recover] $= "")
      {
         $vehicles[%slot, stockarmor] = "";
         $vehicles[%slot, currentarmor] = "";
         $vehicles[%slot, acceleration] = "";
         $vehicles[%slot, brakes] = "";
         $vehicles[%slot, steering] = "";
         $vehicles[%slot, currenttirearmor] = "";
         $vehicles[%slot, stocktirearmor] = "";
         $vehicles[%slot, currentgastankarmor] = "";
         $vehicles[%slot, stockgastankarmor] = "";
         $vehicles[%slot, currentenginehp] = "";   
         $vehicles[%slot, stockenginehp] = "";   
         $vehicles[%slot, tireupgrade] = "";
         $vehicles[%slot, badtires] = "";
         $vehicles[%slot, currentwindowarmor] = "";
         $vehicles[%slot, stockwindowarmor] = "";
         $vehicles[%slot, currentramhp] = "";
         $vehicles[%slot, stockramhp] = "";
         $vehicles[%slot, vehicle] = "";
         $vehicles[%slot, topspeed] = "";
         $vehicles[%slot, tires] = "";
         $vehicles[%slot, currentvehiclehp] = "";
         $vehicles[%slot, stockvehiclehp] = "";
         $vehicles[%slot, mountedweapon] = "";
         $vehicles[%slot, currentmountedweaponhp] = "";
         $vehicles[%slot, stockmountedweaponhp] = "";
      }
      
      $vehicles[%i, recover] = "";
   }
}

function ReAddMen()
{
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
      {
         %id = $vehicles[%i, battleid];
         
         if(%soldiers $= "")
            %soldiers = %id.crew;         
         else
            %soldiers = %soldiers SPC %id.crew;                  
      }
   }
   
   // clear out old values
   
   $refuges = "";
   $Brigands = "";
   $militia = "";
   $Police = "";
   $Soldiers = "";
   $Mercenaries = "";
      
   %count = getWordCount(%soldiers);
   
   echo("TRANSFER SOLDIER COUNT " @ %count);
   
   for(%i=0;%i<%count;%i++)
   {
      %val = getWord(%soldiers, %i);      
      
      switch$ (%val)
      {
         case "refuges":
            $refuges++;         
         case "brigands":
            $Brigands++;
         case "militia":
            $Militia++;
         case "police":
            $Police++;
         case "soldiers":
            $Soldiers++;
         case "mercenaries":         
            $Mercenaries++;         
      }      
   }
   
   // check capacity
   
   %people = GetTotalnumPeople();
   
   %capacity = GetTotalPeopleCapacity();
   
   if(%people > %capacity)
   {
      %num = %people-%capacity;
      
      // 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--;
               }
         }
      }
   }
}

// if a player retreats we need to delete the vehicle icons and props

function DeleteBattle()
{
   // players
   
   for(%i=1;%i<=10;%i++)
   {
      if(isObject($vehicles[%i, battleid]) $= true)
         $vehicles[%i, battleid].SafeDelete();            
   }
   
   // enemies

   for(%i=1;%i<=10;%i++)
   {
      if(isObject($Enemyvehicles[%i, battleid]) $= true)
         $Enemyvehicles[%i, battleid].SafeDelete();            
   }
   
   // destroy props
   
   %count = getWordCount($ObjectList);
   
   echo("OBJECT LIST FOR DELETION " @ %count);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord($ObjectList, %i);
      
      if(isObject(%obj) $= true)
      {
         %obj.Safedelete();      
         %counter++;
      }
   }
   
   echo("ACTUAL OBJECTS DELETED " @ %counter);
   $ObjectList = "";
}

function ReAddAmmo()
{
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
      {
         %id = $vehicles[%i, battleid];
         
         if(trim(%id.ammo) !$= "")
            %ammo = %ammo+trim(%id.ammo);
      }
   }
   
   $ammo = trim(%ammo);
}

