// randomly generated around the map, these locations will have supplies

// at the start of a new game

function GenerateRandomLocations()
{
   $specialLocations = "";
   
   switch$ ($Difficulty)
   {
      case "easy":
         %max = 150;      
      case "normal":
         %max = 130;
      case "hard":
         %max = 110;      
   }
   
   echo("Max Special Locations " @ %max);
   
   for(%i=1;%i<=%max;%i++)
   {
      %obj =    new t2dStaticSprite() {
                     canSaveDynamicFields = "1";
                     layer = 5;  
                     scenegraph = "sg";
                     size =  "8 8";
                  };
      
      %rand = getRandom(1, 10);
                  
      switch$ (%rand)
      {
         case 1:
            // upgrade specialists and promote, one time upgrade
            %obj.specialplace = "Learning Center";
            %obj.Setimagemap("building5");
         case 2:      
            // weapons and ammo and soldiers
            %obj.specialplace = "Military Base";
            %obj.Setimagemap("building5");
            %obj.guns = getRandom(300, 1000);
            %obj.ammo = getRandom(3000, 8000);
            %obj.soldiers = getRandom(20, 60);
         case 3:
            // raises morale
            %obj.specialplace = "Casino";
            %obj.Setimagemap("building1");
         case 4:
            // little bit of everthing except guns and ammo
            %obj.specialplace = "Airport";
            %obj.Setimagemap("building5");
            %obj.fuel = getRandom(1200, 2700);
            %obj.food = getRandom(600, 1800);
            %obj.spareparts = getRandom(100, 250);
            %obj.tires = getRandom(90, 230);
         case 5:
            // random
            %obj.specialplace = "Cache";  // 1 time find
            %obj.Setimagemap("crate");
            %obj.guns = getRandom(10, 40);
            %obj.ammo = getRandom(600, 1200);
            %obj.fuel = getRandom(50, 230);
            %obj.food = getRandom(30, 175);
            %obj.medicine = getRandom(5, 30);
            %obj.spareparts = getRandom(10, 25);
            %obj.tires = getRandom(5, 30);
         case 6:
            // medicine, will cure disease
            %obj.specialplace = "Hospital";
            %obj.Setimagemap("building4");
            %obj.medicine = getRandom(70, 120);
         case 7:
            // food
            %obj.specialplace = "Farm";
            %obj.Setimagemap("building5");
            %obj.food = getRandom(1000, 3000);
         case 8:
            // gas and tires
            %obj.specialplace = "Gas Station";
            %obj.Setimagemap("building6");
            %obj.fuel = getRandom(800, 2000);
            %obj.tires = getRandom(90, 300);
         case 9:
            // 1 time vehicle upgrade, random upgrade
            %obj.specialplace = "Auto Center";
            %obj.Setimagemap("building3");
         case 10:
            // will repair vehicles, gas, tires / repair can only be used once per day
            %obj.specialplace = "Truck Depot";
            %obj.Setimagemap("building2");
            %obj.fuel = getRandom(800, 2000);
            %obj.tires = getRandom(90, 300);
      }
      
      %ret = PlaceSpecialPlace(%obj);      
      
      if(%ret $= true)
         %objectplaced++;
      else
         %deleted++;      
   }
   
   echo("Object Placed " @ %objectplaced);         
   echo("Special Object Deleted " @ %deleted++);         
}

function PlaceSpecialPlace(%obj)
{
   %loop = 30;

   while(%loop > 0)
   {      
      %x = getRandom(-500, -2500);
      %y = getRandom(-200, 800);
            
      %tile = Map.pickTile(%x, %y);      
      
      %coords = getTCenter("map", getWord(%tile, 0), getWord(%tile, 1));
      
      %x = getWord(%coords, 0);
      %y = getWord(%coords, 1);
            
      %list = sg.pickPoint(%x SPC %y);

      %count = getWordCount(%list);
      %nomatch = "";
      
      for(%j=0;%j<%count;%j++)
      {
         %listobj = getWord(%list, %j);
         
         if(%listobj.city !$= "")
         {
            %nomatch = true;
            break;
         }
         if(%listobj.block == 1)
         {
            %nomatch = true;
            break;            
         }
         if(%listobj.road $= 1)
         {
            %nomatch = true;
            break;
         }
         if(%listobj.special !$= "")
         {
            %nomatch = true;
            break;
         }               
         if(%listobj.specialplace !$= "")
         {
            %nomatch = true;
            break;
         }               
         if(%listobj.playerobject !$= "")
         {
            %nomatch = true;
            break;
         }        
         if(%listobj.spawn !$= "")
         {
            %nomatch = true;
            break;
         }        
      }
      
      if(%nomatch $= true)
         %loop--;
      else
      {
         %obj.SetPosition(%x, %y);  
         
         if($specialLocations $= "")
            $specialLocations = %obj;
         else
            $specialLocations = $specialLocations SPC %obj;
            
         return true;
      }
   }
   
   if(isObject(%obj) $= true)
      %obj.Safedelete();      

   return false;
}

function getTCenter(%layer, %tileX, %tileY)
{
   //get the world coordinates of the tileLayer
   %LayerPosX = %Layer.getPositionX();

   %LayerPosY = %Layer.getPositionY();

   // get tile width and height
   
   %tilesize = %layer.GetTileSize();
   %tilewidth = getWord(%tilesize, 0);
   %tileheight = getWord(%tilesize, 1);
   
   // get the upper left X and Y

   %width = %layer.GetWidth();
   %height = %layer.GetHeight();
   %x = %layerPosX - (%width / 2); 
   %y = %layerPosY - (%height / 2);

   // get what half the tile is
   
   %halfx = %tilewidth / 2;
   %halfy = %tileheight / 2;
   
   // get the final position

   %xcoord = %tilex * %tilewidth + %halfx;
   %ycoord = %tiley * %tileheight + %halfy;
   
   %x = %x + %xcoord;
   %y = %y + %ycoord;

   return %x SPC %y;
}

// special place functions

function DecScoutingParty()
{
   $scoutingParty--;
   
   if($scoutingParty < 0)
      $scoutingParty = 0;   
   
   scoutingpartytext.SetText("Scouting Party Size          " @ $scoutingParty);
}

function IncScoutingParty()
{
   $scoutingParty++;
   
   if($scoutingParty > 10)
      $scoutingParty = 10;   

   %totalpeople = GetTotalCombatPeople();   
   %totalpeople = %totalpeople-GetNumVehicles();
   
   if($scoutingParty > %totalpeople)
   {
      $scoutingParty = %totalpeople;
      alxPlay(buzzer);
      return;
   }
   
   scoutingpartytext.SetText("Scouting Party Size          " @ $scoutingParty);   
}

function SpecialPlace(%obj)
{   
   decscoutingpartybutton.visible = true;
   incscoutingpartybutton.visible = true;
   scoutingpartytext.visible = true;

   // so they can't send more than allowed
   
   %totalpeople = GetTotalCombatPeople();   
   %totalpeople = %totalpeople-GetNumVehicles();
   
   if($scoutingParty > %totalpeople)
      $scoutingParty = %totalpeople;

   scoutingpartytext.SetText("Scouting Party Size          " @ $scoutingParty);   
   
   $SpecialPlace = "";  // used for keyboard shortcuts

   InfoYesbutton.Visible = true;
   InfoNobutton.Visible = true;
   Infocontinuebutton.Visible = false;
   InfoWindow.Visible = true;
   infoyesbutton.command = "UseScoutingParty();";
   infonobutton.command = "CloseSpecialEncounter();";
      
   switch$ (%obj.specialplace)
   {
      case "casino":
         DisplayTutorial(casino);
         
         if($CasinoUse > $mastertime)
         {
            InfoYesbutton.Visible = false;
            InfoNobutton.Visible = false;
            Infocontinuebutton.Visible = true;
            
            infocontinuebutton.command = "CloseSpecialEncounter();";
                        
            %text = "You may use a casino in " @ $CasinoUse-$mastertime @ " turns.";
            decscoutingpartybutton.visible = false;
            incscoutingpartybutton.visible = false;
            scoutingpartytext.visible = false;
            
            InfoText.setText(%text);
            return;   
         }
                  
         $specialEncounter = "Casino";
               
         %text = "Do you want to let your gang use this Casino?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         

      case "Learning Center":
         DisplayTutorial(LearningCenter);
         
         $specialEncounter = "Learning Center";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to let your gang use this Learning Center?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
         
      case "Military Base":
         DisplayTutorial(militarybase);
         
         $specialEncounter = "Military Base";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to search this Military Base?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
      
      case "Airport":
         DisplayTutorial(airport);
         
         $specialEncounter = "Airport";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to search the Airport?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
      
      case "cache":
         DisplayTutorial(cache);
         
         $specialEncounter = "Cache";
         $specialEncounterObject = %obj;
         
         %text = "You have discovered a cache of supplies, do you want to search it?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
      
      case "hospital":
         DisplayTutorial(hospital);
         
         $specialEncounter = "Hospital";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to search the Hospital?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
      
      case "farm":
         DisplayTutorial(farm);
         
         $specialEncounter = "Farm";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to search the Farm?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
         
      case "gas station":
         DisplayTutorial(gasstation);
         
         $specialEncounter = "Gas Station";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to search the Gas Station?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
      
      case "Auto Center":
         DisplayTutorial(autocenter);
         
         $specialEncounter = "Auto Center";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to search the Auto Center?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
         
      case "Truck Depot":
         if($truckdepotuse > $mastertime)
         {
            InfoYesbutton.Visible = false;
            InfoNobutton.Visible = false;

            Infocontinuebutton.Visible = true;
            
            infocontinuebutton.command = "CloseSpecialEncounter();";
            
            %text = "You may use a truck depot in " @ $truckdepotuse-$mastertime @ " turns.";
            
            decscoutingpartybutton.visible = false;
            incscoutingpartybutton.visible = false;
            scoutingpartytext.visible = false;

            InfoText.setText(%text);
            return;   
         }

         DisplayTutorial(truckdepot);
         
         $specialEncounter = "Truck Depot";
         $specialEncounterObject = %obj;
         
         %text = "Do you want to use the Truck Depot?\n\n How many gang members do you want in the scouting party?";

         InfoText.setText(%text);         
   }      
}

// if a player sends soldiers for the scouting party, determine what soldiers go out (if any were sent) 
// and what rank they were and if any of them don't come back
// the bigger the party the smaller the chance the gang will not have a negative effect

// chances will not be affected by the rank of the gang member just the number in the scouting party

function UseScoutingParty()
{   
   if(Infowindow.Visible $= false)
      return;
      
   decscoutingpartybutton.visible = false;
   incscoutingpartybutton.visible = false;
   scoutingpartytext.visible = false;

   // determine if a bad event will happen
   
   %rand = getRandom(1, 100);
   
   %basenum = 50 + ($scoutingParty * 2);
   
   if(%rand >= %basenum)
      $BadEvent = true;
   else
      $BadEvent = "";

   %refuges = 0;
   %brigands = 0;
   %militia = 0;
   %police = 0;
   %soldiers = 0;
   %mercenaries = 0;
   
   // now determine who doesn't come back
   
   if($scoutingParty > 0)
   {   
      %party = $scoutingParty;      
         
      while(%party > 0)
      {
         %rand = getRandom(1, 6);
         
         switch$ (%rand)
         {
            case 1:
               if($refuges > 0)
               {
                  %chance = 70;
                  %roll = getRandom(1, 100);
                  
                  if(%roll >= %chance)
                  {
                     $refuges--;
                     %refuges++;
                     %loss = true;
                  }
                  
                  %party--;     
               }
            case 2:
               if($Brigands > 0)
               {
                  %chance = 75;
                  %roll = getRandom(1, 100);
                  
                  if(%roll >= %chance)
                  {
                     $Brigands--;
                     %brigands++;
                     %loss = true;
                  }
                  
                  %party--;
               }               
            case 3:
               if($Militia > 0)
               {
                  %chance = 80;
                  %roll = getRandom(1, 100);
                  
                  if(%roll >= %chance)
                  {
                     $Militia--;
                     %militia++;
                     %loss = true;
                  }
                  
                  %party--;
               }
            case 4:
               if($Police > 0)
               {
                  %chance = 85;
                  %roll = getRandom(1, 100);
                  
                  if(%roll >= %chance)
                  {
                     $Police--;
                     %police++;
                     %loss = true;
                  }
                  
                  %party--;
               }
            case 5:
               if($soldiers > 0)
               {
                  %chance = 90;
                  %roll = getRandom(1, 100);
                  
                  if(%roll >= %chance)
                  {
                     $soldiers--;
                     %soldiers++;
                     %loss = true;
                  }
                  
                  %party--;
               }
            case 6:
               if($Mercenaries > 0)
               {
                  %chance = 95;
                  %roll = getRandom(1, 100);
                  
                  if(%roll >= %chance)
                  {
                     $Mercenaries--;
                     %mercenaries++;
                     %loss = true;
                  }
                  
                  %party--;
               }
         }
      }
   }

   if(%loss $= true)
   {      
      Infotext.setText("");
      Infotext.AddText("Some gang members didn't return! 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);   

      InfoYesbutton.Visible = false;
      InfoNobutton.Visible = false;
      Infocontinuebutton.Visible = true;
               
      infocontinuebutton.command = "UseSpecialPlace();";   
      
      UpdateStatsBar();
   }
   else
      UseSpecialPlace();   
}

function UseSpecialPlace()
{
   infoyesbutton.Visible = false;
   Infonobutton.Visible = false;
   infocontinuebutton.Visible = true;
   InfoWindow.Visible = true;
   
   infocontinuebutton.command = "CheckBadEvent();";
   $ContinueButton = "CheckBadEvent";

   $SpecialSearchIndex = 1;
   $foodFound = "";
   $gunsFound = "";
   $ammoFound = "";
   $medicineFound = "";
   $tiresFound = "";
   $sparepartsFound = "";   
   $fuelFound = "";
   $StopSearchFlag = "";

   switch$ ($specialEncounter)
   {
      case "casino":      
         
         if($morale == 10)                     
            %text = "Gang morale is raised by 0.";
         if($morale == 9)                     
            %text = "Gang morale is raised by 1.";
         if($morale <= 8)                     
            %text = "Gang morale is raised by 2.";
         
         $CasinoUse = $mastertime+24;
         
         InfoText.setText(%text);
      
         IncreaseMorale();
         IncreaseMorale();
         
      // only need one return to find out if they have that specialists or not
      case "Learning Center":
         InfoText.setText("Some gang members have been promoted.\n\n");
      
         %ret = PromoteDoctor();
         
         if(%ret $= true)
         {
            InfoText.AddText("Doctor has gained more experience.\n", true);
            PromoteDoctor();
         }

         %ret = PromoteMechanic();
         
         if(%ret $= true)
         {
            InfoText.AddText("Mechanic has gained more experience.\n", true);
            PromoteMechanic();
         }
         
         %ret = PromoteRecruiter();         

         if(%ret $= true)
         {
            InfoText.AddText("Recruiter has gained more experience.\n", true);
            PromoteRecruiter();
         }

         %ret = PromoteScout();

         if(%ret $= true)
         {
            InfoText.AddText("Scout has gained more experience.\n", true);
            PromoteScout();
         }

         %ret = PromoteSergeant();

         if(%ret $= true)
         {
            InfoText.AddText("Sergeant has gained more experience.", true);
            PromoteSergeant();
         }

         Promotion();
      
         %count = getWordCount($specialLocations);
         
         for(%i=0;%i<%count;%i++)
         {
            %obj = getWord($specialLocations, %i);
            
            if(%obj $= $specialEncounterObject)
               break;
         }
         
         $specialLocations = removeWord($specialLocations, %i);
         $specialEncounterObject.visible = false;
            
      case "Military Base":
         %diff = GetTotalPeopleCapacity() - GetTotalNumPeople();
         
         if(%diff == 0)
         {
            InfoWindow.Visible = true;
            
            InfoText.setText("You don't have any room to recruit anyone.");   
            
            // hide the battle report button
            
            InfoContinuebutton.Visible = true;
            InfoYesbutton.Visible = false;
            InfoNobutton.Visible = false;
            InfoContinuebutton.Command = "CloseSpecialEncounter();";      
            $specialEncounter = "Military Base Supplies";
            return;
         }
         
         if($specialEncounterObject.soldiers > 0)
         {
            InfoWindow.Visible = true;
            InfoYesbutton.Visible = false;
            InfoNobutton.Visible = false;
            InfoContinuebutton.Visible = false;
            InfoText.setText("");
            $specialEncounter = "Military Base Supplies";
            
            %people = getRandom(5, 20);
            
            if(%people > %diff)
               %people = %diff;
            
            if(%people > $specialEncounterObject.soldiers)
               %people = $specialEncounterObject.soldiers;
               
            for(%i=1;%i<=%people;%i++)
            {
               schedule(%time, 0, "SpecialRecruit");               
               %time = %time+500;      
               $specialEncounterObject.soldiers--;            
            }
            
            schedule(%time+500, 0, "UseSpecialPlace");   
            }
         else
         {
            $specialEncounter = "Military Base Supplies";
            UseSpecialPlace();         
         }
      
      case "Military Base Supplies":
         $GunsFound = getRandom(35, 90);
         
         if($GunsFound > $specialEncounterObject.guns)
            $GunsFound = $specialEncounterObject.guns;

         $AmmoFound = getRandom(75, 340);
         
         if($AmmoFound > $specialEncounterObject.ammo)
            $AmmoFound = $specialEncounterObject.ammo;
         
         if($gunsFound > 0 || $ammoFound > 0)
            SpecialSearchReturn();

      
      case "Airport":
         $FuelFound = getRandom(100, 240);
         
         if($FuelFound > $specialEncounterObject.fuel)
            $FuelFound = $specialEncounterObject.fuel;

         $FoodFound = getRandom(50, 90);
         
         if($FoodFound > $specialEncounterObject.food)
            $FoodFound = $specialEncounterObject.food;

         $TiresFound = getRandom(15, 45);
         
         if($TiresFound > $specialEncounterObject.tires)
            $TiresFound = $specialEncounterObject.tires;

         $sparepartsFound = getRandom(25, 50);
         
         if($sparepartsFound > $specialEncounterObject.spareparts)
            $sparepartsFound = $specialEncounterObject.spareparts;
         
         if($fuelFound > 0 || $foodFound > 0 || $tiresFound > 0 || $sparepartsFound > 0)
            SpecialSearchReturn();
      
      case "cache":
         $FuelFound = getRandom(100, 300);
         
         if($FuelFound > $specialEncounterObject.fuel)
            $FuelFound = $specialEncounterObject.fuel;

         $FoodFound = getRandom(75, 150);
         
         if($FoodFound > $specialEncounterObject.food)
            $FoodFound = $specialEncounterObject.food;

         $TiresFound = getRandom(10, 25);
         
         if($TiresFound > $specialEncounterObject.tires)
            $TiresFound = $specialEncounterObject.tires;

         $sparepartsFound = getRandom(15, 30);
         
         if($sparepartsFound > $specialEncounterObject.spareparts)
            $sparepartsFound = $specialEncounterObject.spareparts;
         
         $medicineFound = getRandom(15, 30);
         
         if($medicineFound > $specialEncounterObject.medicine)
            $medicineFound = $specialEncounterObject.medicine;

         $GunsFound = getRandom(35, 90);
         
         if($GunsFound > $specialEncounterObject.guns)
            $GunsFound = $specialEncounterObject.guns;

         $AmmoFound = getRandom(175, 900);
         
         if($AmmoFound > $specialEncounterObject.ammo)
            $AmmoFound = $specialEncounterObject.ammo;

         SpecialSearchReturn();
            
      case "hospital":
         if($disease $= true)
         {
            $disease = "";
            PromoteDoctor(); // so he gets experience for the heal
            IncreaseMorale();
            UpdateStatsBar();      
         }

         $medicineFound = getRandom(25, 50);
         
         if($medicineFound > $specialEncounterObject.medicine)
            $medicineFound = $specialEncounterObject.medicine;
         
         if($medicineFound > 0)
            SpecialSearchReturn();
      
      case "farm":
         $FoodFound = getRandom(205, 350);
         
         if($FoodFound > $specialEncounterObject.food)
            $FoodFound = $specialEncounterObject.food;
      
         if($foodFound > 0)
            SpecialSearchReturn();
      
      case "Gas Station":
         $TiresFound = getRandom(10, 25);
         
         if($TiresFound > $specialEncounterObject.tires)
            $TiresFound = $specialEncounterObject.tires;
      
         $FuelFound = getRandom(100, 300);
         
         if($FuelFound > $specialEncounterObject.fuel)
            $FuelFound = $specialEncounterObject.fuel;
      
         if($TiresFound > 0 || $fuelFound > 0)
            SpecialSearchReturn();
      
      case "Auto Center":
         %upgrade = SpecialUpgrade();
      
         InfoText.setText("All vehicles received upgraded " @ %upgrade @ ".");
      
      case "Truck Depot":
         %ret = SpecialRepair();         
         
         if(%ret $= true)
         {
            $truckdepotuse = $mastertime+24;
            InfoText.setText("Some of your vehicles were repaired.");
         }
         else
            InfoText.setText("None of your vehicles need to be repaired.");         
   }
}

// when a player uses a special place there will be a chance something bad happens
////////////////////// bad encounters ///////////////////////////////////////////////

function CheckBadEvent()
{
   // since this gets called after every interaction with a special place let's check 
   // here to make sure it has any supplies left
   
   CheckSpecialSupplyCount();

   if($BadEvent $= true)
   {      
      %num = getRandom(1, 6);

      echo("Start Bad Event " @ %num);
      
      switch$ (%num)
      {
         // supplies get stolen
         case 1:
            echo("SUPPLIES GET STOLEN");
            InfoWindow.Visible = true;
            Infoyesbutton.Visible = false;
            Infonobutton.Visible = false;
            Infocontinuebutton.Visible = true;
            infocontinuebutton.Command = "Infowindow.Visible = false;";            
            
            Infotext.setText("");
            Infotext.AddText("Some thieves have stolen some of your supplies!\n\n", true);
            
            StealSupplies();
         
         // vehicle gets damaged
         case 2:
            echo("VEHICLE GETS DAMAGED");
            InfoWindow.Visible = true;
            Infoyesbutton.Visible = false;
            Infonobutton.Visible = false;
            Infocontinuebutton.Visible = true;
            infocontinuebutton.Command = "Infowindow.Visible = false;";            
   
            Infotext.setText("");
            Infotext.AddText("Vandles have damaged some of your vehicles!\n\n", true);
            
            DamageVehicles();
         
         // tires get slashed
         case 3:
            echo("TIRES GET SLASHED");
            InfoWindow.Visible = true;
            Infoyesbutton.Visible = false;
            Infonobutton.Visible = false;
            Infocontinuebutton.Visible = true;
            infocontinuebutton.Command = "Infowindow.Visible = false;";            
            
            Infotext.setText("");
            Infotext.AddText("Vandals have slashed some tires!\n\n", true);
            
            SlashTires();
         
         // some men desert - only if the gang has spare men as opposed to vehicles
         case 4:
            echo("MORALE CHECK");
            InfoWindow.Visible = true;
            Infoyesbutton.Visible = false;
            Infonobutton.Visible = false;
            Infocontinuebutton.Visible = true;
            infocontinuebutton.Command = "Infowindow.Visible = false;";            

            MoraleCheck(true);      
            
         // get disease
         case 5:
            echo("CHECK FOR DISEASE");
            CheckForDisease(true);      
         // demotion
         case 6:
            echo("MEN GET WRECKLESS");
            InfoWindow.Visible = true;
            Infoyesbutton.Visible = false;
            Infonobutton.Visible = false;
            Infocontinuebutton.Visible = true;
            infocontinuebutton.Command = "Infowindow.Visible = false;";            

            Infotext.setText("");
            Infotext.AddText("Some of your men get wreckless and lose a rank!\n\n", true);      
      }
   }
   else
      Infowindow.Visible = false;
      
   UpdateTime();
}

function StealSupplies()
{
   %stolen = getRandom(50, 150);
   
   %supplies = GetCurrentSupplyCount();
   
   if(%stolen > %supplies)
      %stolen = %supplies;
         
   while(%counter <= %stolen)
   {
      %rand = getRandom(1, 7); 
               
      switch$ (%rand)
      {
         case 1:
            if($food > 0)
            {
               DecreaseFood(1);
               %counter++;
               %food++;
            }
         case 2:
            if($guns > 0)
            {
               DecreaseGuns(1);
               %counter++;
               %guns++;            
            }
         case 3:
            if($ammo > 0)
            {
               DecreaseAmmo(1);
               %counter++;
               %ammo++;
            }
         case 4:
            if($medicine > 0)
            {
               DecreaseMedicine(1);
               %counter++;
               %medicine++;            
            }
         case 5:
            if($tires > 0)
            {
               DecreaseTires(1);
               %counter++;
               %tires++;            
            }
         case 6:
            if($spareparts > 0)
            {
               DecreaseSpareParts(1);
               %counter++;
               %spareparts++;          
            }
         case 7:
            if($fuel > 0)
            {
               DecreaseFuel(1);
               %counter++;
               %fuel++;          
            }
      }      
   }
   
   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);    
}

function DamageVehicles()
{
   %vehicles = getRandom(1, 4);
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
         %Totalvehicles++;         
   }
   
   if(%vehicles > %Totalvehicles)
      %vehicles = %Totalvehicles;
      
   // damage the vehicles
   
   while(%vehicles > 0)
   {
      %rand = getRandom(1, 10);
      
      if($vehicles[%rand, vehicle] !$= "")
      {
         %damage = getRandom(5, 20);
         
         $vehicles[%rand, currentvehiclehp] = $vehicles[%rand, currentvehiclehp]-%damage;
         
         %vehicles--;

         Infotext.AddText($vehicles[%rand, vehicle] @ " was damaged!\n", true);
                           
         if($vehicles[%rand, currentvehiclehp] < 0)
            $vehicles[%rand, currentvehiclehp] = 1;
      }
   }
}

function SlashTires()
{
   %slashed = getRandom(10, 23);
         
   while(%slashed > 0)
   {
      %counter++;         
      
      %rand = getRandom(1, 10);
      
      if($vehicles[%rand, vehicle] !$= "")
      {
         if($vehicles[%rand, badtires] < $vehicles[%rand, tires])
         {
            $vehicles[%rand, badtires]++;
            %slashed--;
            %actualslashed++;                                             
         }
      }
      
      // this might kick in if there aren't enough good tires to be slashed!
      
      if(%counter == 200)
         break;
   }

   Infotext.AddText(%actualslashed @ " tires were slashed!", true);
}

//////////////////////////////////////// good stuff ////////////////////////////////////////////////////

function SpecialUpgrade()
{
   %rand = getRandom(1, 3);

   if($CapturedCities >= 10)
      %rand = getRandom(1, 6);
   
   if($CapturedCities >= 20)
      %rand = getRandom(1, 8);

   if($CapturedCities >= 35)
      %rand = getRandom(1, 10);
   
   switch$ (%rand)
   {
      // armor
      case 1:      
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               %armor = GetUpgradeArmorPoints($vehicles[%i, vehicle]);
               %max = %armor*5;
               
               if($vehicles[%i, stockarmor] < %max && $vehicles[%i, vehicle] !$= "bike" && $vehicles[%i, vehicle] !$= "dunebuggy")
               {
                  $vehicles[%i, currentarmor] = $vehicles[%i, currentarmor]+%armor;                              
                  $vehicles[%i, stockarmor] = $vehicles[%i, stockarmor]+%armor;
               }
            }
         }

         return "Armor";         

      // acceleration
      case 2:
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               if($vehicles[%i, acceleration] < 5)
                  $vehicles[%i, acceleration]++;                                 
            }
         }
         
         return "Acceleration";         
      
      // tires
      case 3:
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               if($vehicles[%i, tireupgrade] < 3)
                  $vehicles[%i, tireupgrade]++;                                 
            }
         }

         return "Tires";         

      
      // tire armor
      case 4:      
         for(%i=1;%i<=10;%i++)
         {
            %armor = GetUpgradeTireArmorPoints($vehicles[%i, vehicle]);
            %max = %armor*5;

            if($vehicles[%i, vehicle] !$= "" && $vehicles[%i, vehicle] !$= "bike" && $vehicles[%i, vehicle] !$= "dunebuggy")            
            {
               if($vehicles[%i, stocktirearmor] < %max)
               {
                  $vehicles[%i, currenttirearmor] = $vehicles[%i, currenttirearmor]+%armor;                                 
                  $vehicles[%i, stocktirearmor] = $vehicles[%i, stocktirearmor]+%armor;                                 
               }
            }
         }

         return "Tire Armor";         
      
      // brakes
      case 5:
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               if($vehicles[%i, brakes] < 5)
                  $vehicles[%i, brakes]++;                                 
            }
         }

         return "Brakes";         
      
      // supply capacity
      case 6:
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               %capacity = GetSupplyCapacityUpgrade($vehicles[%i, vehicle]);
               %max = %capacity*5;
               %max = %max+GetVehicleSupplyCapacity($vehicles[%i, vehicle]);
                        
               if($vehicles[%i, supplycapacity] < %max)
                  $vehicles[%i, SupplyCapacity] = $vehicles[%i, SupplyCapacity]+GetSupplyCapacityUpgrade($vehicles[%i, vehicle]);         
            }
         }
         
         return "Supply Capacity";
      
      // window armor      
      case 7:
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               %armor = GetUpgradeWindowArmorPoints($vehicles[%i, vehicle]);
               %max = %armor*5;

               if($vehicles[%i, stockwindowarmor] < %max && $vehicles[%i, vehicle] !$= "bike" && $vehicles[%i, vehicle] !$= "dunebuggy")
               {
                  $vehicles[%i, currentwindowarmor] = $vehicles[%i, currentwindowarmor]+%armor;                                 
                  $vehicles[%i, stockwindowarmor] = $vehicles[%i, stockwindowarmor]+%armor;                                 
               }
            }
         }

         return "Window Armor";         
      
      // top speed
      case 8:
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               if($vehicles[%i, topspeedlevel] < 5)
               {
                  $vehicles[%i, topspeedlevel]++;                                 
                  $vehicles[%i, topspeed] = $vehicles[%i, topspeed]+10;
               }
            }
         }

         return "Top Speed";         
      
      // steering
      case 9:
         for(%i=1;%i<=10;%i++)
         {
            if($vehicles[%i, vehicle] !$= "")            
            {
               if($vehicles[%i, steering] < 5)
                  $vehicles[%i, steering]++;                                 
            }
         }      

         return "Steering";    
      
      // gas tank armor
      case 10:
         %armor = GetUpgradeGasTankArmorPoints($vehicles[$Configslot, vehicle]);
         %max = %armor*5;
         
         if($vehicles[$Configslot, stockgastankarmor] < %max)
         {
            $vehicles[$Configslot, stockgastankarmor] = $vehicles[$Configslot, stockgastankarmor]+%armor;
            $vehicles[$Configslot, currentgastankarmor] = $vehicles[$Configslot, currentgastankarmor]+%armor; 
         }
   }
}

function SpecialRecruit()
{
   $soldiers++;
   $Recruitcounter++;

   if($Recruitcounter == 11)   
   {      
      InfoText.setText("");
      $Recruitcounter = 0;
   }
   
   InfoText.AddText("You recruited a soldier\n", true);
       
   UpdateStatsBar();   
}

// if the player says no we have to hide the scout buttons

function CloseSpecialEncounter()
{
   decscoutingpartybutton.visible = false;
   incscoutingpartybutton.visible = false;
   scoutingpartytext.visible = false;
   
   Infowindow.Visible = false;
}

function CheckSpecialSupplyCount()
{
   %obj = $specialEncounterObject;
   
   switch$ (%obj.specialplace)
   {
      case "Military Base":      
         if(%obj.guns == 0 && %obj.ammo == 0 && %obj.soldiers == 0)    
            %destroy = true;
      case "Airport":
         if(%obj.fuel == 0 && %obj.food == 0 && %obj.spareparts == 0 && %obj.tires == 0)    
            %destroy = true;
      case "Farm":
         if(%obj.food == 0)    
            %destroy = true;
      case "Gas Station":
         if(%obj.fuel == 0 && %obj.tires == 0)    
            %destroy = true;
      case "Truck Depot":
         if(%obj.fuel == 0 && %obj.tires == 0)    
            %destroy = true;
      case "cache":
         %destroy = true;
      case "Auto Center":
         %destroy = true;
   }
   
   if(%destroy $= true)
   {
      %obj.visible = false;      

      %count = getWordCount($specialLocations);
      
      for(%i=0;%i<%count;%i++)
      {
         %obj = getWord($specialLocations, %i);
         
         if(%obj $= $specialEncounterObject)
            break;
      }
      
      $specialLocations = removeWord($specialLocations, %i);
   }
}

function CheckForSpecialPlace()
{
   %objects = sg.pickPoint(player.getPosition());   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.specialplace !$= "")
      {
         SpecialPlace(%obj);
         return true;
      }
   }      
}

function SpecialRepair()
{
   // see if anything needs repairing
   
   $maxparts = 20;
      
   // pick a vehicle at random and run the spare parts down to zero
   
   while($maxparts > 0)
   {
      %loop++;
      
      %i = getRandom(1, 10);
               
      if($vehicles[%i, vehicle] !$= "")
      {
         if($vehicles[%i, currentarmor] < $vehicles[%i, stockarmor])     
         {
            SpecialPartialRepair(%i, armor);
            %repair = true;            
         }
            
         if($vehicles[%i, currentenginehp] < $vehicles[%i, stockenginehp])             
         {
            SpecialPartialRepair(%i, engine);
            %repair = true;
         }

         if($vehicles[%i, currenttirearmor] < $vehicles[%i, stocktirearmor])              
         {
            SpecialPartialRepair(%i, tirearmor);
            %repair = true;
         }

         if($vehicles[%i, currentgastankarmor] < $vehicles[%i, stockgastankarmor])
         {
            SpecialPartialRepair(%i, gastankarmor);
            %repair = true;
         }

         if($vehicles[%i, currentmountedweaponhp] < $vehicles[%i, stockmountedweaponhp])
         {
            SpecialPartialRepair(%i, mountedweapon);
            %repair = true;
         }

         if($vehicles[%i, currentramhp] < $vehicles[%i, stockramhp])
         {
            SpecialPartialRepair(%i, ram);
            %repair = true;
         }

         if($vehicles[%i, currentwindowarmor] < $vehicles[%i, stockwindowarmor])
         {
            SpecialPartialRepair(%i, windowarmor);
            %repair = true;
         }

         if($vehicles[%i, currentvehiclehp] < $vehicles[%i, stockvehiclehp])
         {
            SpecialPartialRepair(%i, vehicle);
            %repair = true;
         }
      }
      
      // to prevent it from going on forever      
      if(%loop == 300)
         break;
   }
   
   if(%repair $= true)
      return true;
}

// used in repair function

function SpecialPartialRepair(%slot, %area)
{
   switch$ (%area)
   {
      case "armor":     
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currentarmor] = $vehicles[%slot, currentarmor]+5;
            
            if($vehicles[%slot, currentarmor] > $vehicles[%slot, stockarmor])            
            {
               $vehicles[%slot, currentarmor] = $vehicles[%slot, stockarmor];
               break;
            }               
         }
            
      case "engine":             
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currentenginehp] = $vehicles[%slot, currentenginehp]+5;
            
            if($vehicles[%slot, currentenginehp] > $vehicles[%slot, stockenginehp])            
            {
               $vehicles[%slot, currentenginehp] = $vehicles[%slot, stockenginehp];
               break;
            }               
         }
      case "tirearmor":              
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currenttirearmor] = $vehicles[%slot, currenttirearmor]+5;
            
            if($vehicles[%slot, currenttirearmor] > $vehicles[%slot, stocktirearmor])            
            {
               $vehicles[%slot, currenttirearmor] = $vehicles[%slot, stocktirearmor];
               break;
            }               
         }

      case "gastankarmor":
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currentgastankarmor] = $vehicles[%slot, currentgastankarmor]+5;
            
            if($vehicles[%slot, currentgastankarmor] > $vehicles[%slot, stockgastankarmor])            
            {
               $vehicles[%slot, currentgastankarmor] = $vehicles[%slot, stockgastankarmor];
               break;
            }               
         }

      case "mountedweapon":
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currentmountedweaponhp] = $vehicles[%slot, currentmountedweaponhp]+5;
            
            if($vehicles[%slot, currentmountedweaponhp] > $vehicles[%slot, stockmountedweaponhp])            
            {
               $vehicles[%slot, currentmountedweaponhp] = $vehicles[%slot, stockmountedweaponhp];
               break;
            }               
         }

      case "ram":
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currentramhp] = $vehicles[%slot, currentramhp]+5;
            
            if($vehicles[%slot, currentramhp] > $vehicles[%slot, stockramhp])            
            {
               $vehicles[%slot, currentramhp] = $vehicles[%slot, stockramhp];
               break;
            }               
         }

      case "windowarmor":
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currentwindowarmor] = $vehicles[%slot, currentwindowarmor]+5;
            
            if($vehicles[%slot, currentwindowarmor] > $vehicles[%slot, stockwindowarmor])            
            {
               $vehicles[%slot, currentwindowarmor] = $vehicles[%slot, stockwindowarmor];
               break;
            }               
         }

      case "vehicle":
         while ($maxparts > 0)
         {
            $maxparts--;

            $vehicles[%slot, currentvehiclehp] = $vehicles[%slot, currentvehiclehp]+5;
            
            if($vehicles[%slot, currentvehiclehp] > $vehicles[%slot, stockvehiclehp])            
            {
               $vehicles[%slot, currentvehiclehp] = $vehicles[%slot, stockvehiclehp];
               break;
            }               
         }
   }
   
}
