// this will handle the searching of a area - a city will yield more and everything 
// else will be the same

function SearchSupplies()
{
   echo("SEARCH SUPPLIES CLICK");
   
   alxPlay(click);
   
   DisplayTutorial(supplysearch);
   
   // if they have zero room don't do anythng
   
   %room = GetTotalSupplyCapacity() - GetCurrentSupplyCount();
   
   if(%room <= 0)
   {
      InfoWindow.Visible = true;      
      InfoYesframe.Visible = true;
      InfoNoframe.Visible = true;
      Infocontinuebutton.Visible = false;
      
      InfoText.setText("You have no room left! Would you like to drop some supplies?");
      InfoNobutton.command = "Infowindow.Visible = false;";      
      InfoYesbutton.command = "OpenSupplyDrop();";     
      return;      
   }
   
   // add 1 hour for search
   
   if(UpdateTime() $= true)
      return;

   if(CheckAllEvents(search) $= true)
      return;
   
   if(CheckForRandomPeopleEncounter(supply) $= true)
      return;
   
   %list = sg.PickPoint(player.GetPosition());
   
   %count = getWordCount(%list);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%list, %i);
      
      if(%obj.city !$= "")
         %city = true;   
   }
   
   if(%city $= true)
      CitySearch();
   else
      LandSearch();
   
   // Reset Variables
   
   $SearchIndex = 1;
   $foodFound = "";
   $gunsFound = "";
   $ammoFound = "";
   $medicineFound = "";
   $tiresFound = "";
   $sparepartsFound = "";   
   $fuelFound = "";
   $StopSearchFlag = "";
   
   %chance = getRandom(1, 100);
   
   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancefood * ".15";
         %mod = mCeil(%mod);
         $Chancefood = $Chancefood+%mod;         
      case "hard":      
         %mod = $Chancefood * ".15";
         %mod = mCeil(%mod);
         $Chancefood = $Chancefood-%mod;         
   }
   
   if(%chance <= $Chancefood)
   {
      $FoodFound = getRandom(30, 200); 
      
      %rand = getRandom(1, 10);
      
      if(%rand <= 2)
         $foodFound = getRandom(210, 320);
      
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chanceguns * ".15";
         %mod = mCeil(%mod);
         $Chanceguns = $Chanceguns+%mod;         
      case "hard":      
         %mod = $Chanceguns * ".15";
         %mod = mCeil(%mod);
         $Chanceguns = $Chanceguns-%mod;         
   }

   if(%chance <= $Chanceguns)
   {
      $GunsFound = getRandom(10, 45);   
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chanceammo * ".15";
         %mod = mCeil(%mod);
         $Chanceammo = $Chanceammo+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chanceammo = $Chanceammo-%mod;         
   }

   if(%chance <= $Chanceammo)
   {
      $AmmoFound = getRandom(250, 450);
      %found = true;
   }
   
   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancemedicine * ".15";
         %mod = mCeil(%mod);
         $Chancemedicine = $Chancemedicine+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chancemedicine = $Chancemedicine-%mod;         
   }

   if(%chance <= $Chancemedicine)
   {
      $MedicineFound = getRandom(5, 30);
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancetires * ".15";
         %mod = mCeil(%mod);
         $Chancetires = $Chancetires+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chancetires = $Chancetires-%mod;         
   }

   if(%chance <= $Chancetires)
   {
      $TiresFound = getRandom(15, 35);
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancespareparts * ".15";
         %mod = mCeil(%mod);
         $Chancespareparts = $Chancespareparts+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chancespareparts = $Chancespareparts-%mod;         
   }

   if(%chance <= $Chancespareparts)
   {
      $SparePartsFound = getRandom(20, 65);   
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancefuel * ".15";
         %mod = mCeil(%mod);
         %chance = $Chancefuel+%mod;         
      case "hard":      
         %mod = $Chancefuel * ".15";
         %mod = mCeil(%mod);
         %chance = $Chancefuel-%mod;         
   }

   // there is a decent change they will find a large cache
   
   if(%chance <= $Chancefuel)
   {
      $FuelFound = getRandom(120, 250);
      
      %rand = getRandom(1, 10);
      
      if(%rand <= 3)
         $fuelFound = getRandom(315, 575);
         
      %found = true;
   }
   
   if(%found $= true)
      PromoteScout();
         
   SearchReturn();   
}

// these will be the search chances based on if it's a city or not

// search chances in cities

function CitySearch()
{
   %scout = CheckScout();
      
   $Chancefood = 45+%scout;
   $Chanceguns = 40+%scout;
   $Chanceammo = 60+%scout;
   $Chancemedicine = 30+%scout;
   $Chancetires = 50+%scout;
   $Chancespareparts = 40+%scout;   
   $Chancefuel = 60+%scout;
}

// search chances in non-city squares

function LandSearch()
{
   %scout = CheckScout();

   %distance = FindNearestCity();
   
   if(%distance >= 120)
   {
      $Chancefood = 25+%scout;
      $Chanceguns = 20+%scout;
      $Chanceammo = 40+%scout;
      $Chancemedicine = 10+%scout;
      $Chancetires = 30+%scout;
      $Chancespareparts = 20+%scout;   
      $Chancefuel = 40+%scout;
   }
   if(%distance >= 60 && %distance <= 120)
   {
      $Chancefood = 30+%scout;
      $Chanceguns = 25+%scout;
      $Chanceammo = 45+%scout;
      $Chancemedicine = 15+%scout;
      $Chancetires = 35+%scout;
      $Chancespareparts = 25+%scout;   
      $Chancefuel = 45+%scout;
   }
   if(%distance >= 0 && %distance < 60)
   {
      $Chancefood = 35+%scout;
      $Chanceguns = 30+%scout;
      $Chanceammo = 50+%scout;
      $Chancemedicine = 20+%scout;
      $Chancetires = 40+%scout;
      $Chancespareparts = 30+%scout;   
      $Chancefuel = 50+%scout;
   }

}

// see if anything was found and offer it to the player

function SearchReturn()
{
   // go through each potential found item   
   
   if($SearchIndex $= 1 && $FoodFound > 0 && $StopSearchFlag $= "")   
      %text = "Your gang found " @ $foodFound @ " Food. Do you want to take it? \n\nCurrent food supply is   " @ $food;
   if($SearchIndex $= 2 && $gunsFound > 0 && $StopSearchFlag $= "")
      %text = "Your gang found " @ $gunsFound @ " Guns. Do you want to take it? \n\nCurrent gun supply is   " @ $guns;
   if($SearchIndex $= 3 && $ammoFound > 0 && $StopSearchFlag $= "")
      %text = "Your gang found " @ $ammoFound @ " Ammo. Do you want to take it? \n\nCurrent ammo supply is   " @ $ammo;
   if($SearchIndex $= 4 && $medicineFound > 0&& $StopSearchFlag $= "")
      %text = "Your gang found " @ $medicineFound @ " Medicine. Do you want to take it? \n\nCurrent medicine supply is   " @ $medicine;
   if($SearchIndex $= 5 && $tiresFound > 0 && $StopSearchFlag $= "")
      %text = "Your gang found " @ $tiresFound @ " Tires. Do you want to take it? \n\nCurrent tire supply is    " @ $tires;
   if($SearchIndex $= 6 && $sparepartsFound > 0 && $StopSearchFlag $= "")   
      %text = "Your gang found " @ $sparepartsFound @ " Spare Parts. Do you want to take it? \n\nCurrent spare parts supply is   " @ $spareparts;
   if($SearchIndex $= 7 && $fuelFound > 0 && $StopSearchFlag $= "")
      %text = "Your gang found " @ $fuelFound @ " Fuel. Do you want to take it? \n\nCurrent fuel supply   " @ $fuel;
   
   if(%text !$= "")
   {
      InfoWindow.Visible = true;
      InfoText.setText(%text @ "\n\nSpare Capacity is   " @ GetTotalSupplyCapacity() - GetCurrentSupplyCount());
      Infocontinuebutton.Visible = false;
      InfoYesbutton.Visible = true;
      InfoNobutton.Visible = true;
      InfoYesbutton.command = "YesSearch();";
      InfoNobutton.command = "NoSearch();";      
   }
   else
   {
      $SearchIndex++;
      Infowindow.Visible = false;
      
      if($SearchIndex <= 7)
         SearchReturn();      
   }
}

// if they take the current item, take it and return to SearchReturn to see if there 
// are any more items

function YesSearch()
{
   if(Infowindow.Visible $= false)
      return;
      
   %room = GetTotalSupplyCapacity() - GetCurrentSupplyCount();
      
   // if they don't have enough room just take what they can
   
   if($SearchIndex $= 1)   
   {
      if($foodFound > %room)
      {
         IncreaseFood(%room);
         %filled = true;
      }
      else
         IncreaseFood($foodFound);
   }
   
   if($SearchIndex $= 2)
   {
      if($GunsFound > %room)
      {
         IncreaseGuns(%room);
         %filled = true;
      }
      else
         IncreaseGuns($gunsFound);
   }
   
   // 20 rounds of ammo will be 1 storage unit
   
   if($SearchIndex $= 3)
   {
      %ammostorage = $ammoFound/20;
      %ammostorage = mCeil(%ammostorage);
      
      if(%ammostorage > %room)
      {
         IncreaseAmmo(%room*20);
         %filled = true;
         
         if($ammo > 9999)
            $ammo = 9999;
      }
      else
      {
         IncreaseAmmo($ammoFound);

         if($ammo > 9999)
            $ammo = 9999;
      }
   }

   if($SearchIndex $= 4)
   {
      if($MedicineFound > %room)
      {
         IncreaseMedicine(%room);
         %filled = true;
      }
      else
         IncreaseMedicine($medicineFound);
   }

   if($SearchIndex $= 5)
   {
      if($TiresFound > %room)
      {
         IncreaseTires(%room);
         %filled = true;
      }
      else
         IncreaseTires($tiresFound);
   }

   if($SearchIndex $= 6)
   {
      if($sparepartsFound > %room)
      {
         IncreaseSpareParts(%room);
         %filled = true;
      }
      else
         IncreaseSpareParts($sparepartsFound);
   }

   if($SearchIndex $= 7)
   {
      if($FuelFound > %room)
      {
         IncreaseFuel(%room);
         %filled = true;
      }
      else
         IncreaseFuel($FuelFound);
   }

   // if they are filled to capacity then set this flag so no other items may be taken for this search
   
   if(%filled $= true)
      $StopSearchFlag = true;
   
   UpdateStatsBar();
   
   $SearchIndex++;
   
   SearchReturn();   
}

// if they say no to taking an item return to SearchReturn to see if there are more items

function NoSearch()
{
   $SearchIndex++;
   
   SearchReturn();   
}

function CheckSupplyCapacity()
{
   %supplies = GetCurrentSupplyCount();
   
   %capacity = GetTotalSupplyCapacity();
   
   if(%supplies > %capacity)
   {
      %num = %supplies-%capacity;
      
      %food = 0;
      %guns = 0;
      %ammo = 0;
      %medicine = 0;
      %tires = 0;
      %spareparts = 0;   
      %fuel = 0;
            
      while(%num > 0)
      {
         %rand = getRandom(1, 7);
         
         switch$ (%rand)
         {
            case 1:
               if($food > 0)
               {
                  DecreaseFood(1);
                  %food++;
                  %num--;     
               }
            case 2:
               if($guns > 0)
               {
                  DecreaseGuns(1);
                  %guns++;
                  %num--;
               }
            case 3:
               if($ammo > 0)
               {
                  DecreaseAmmo(1);
                  %ammo++;
                  %num--;
               }
            case 4:
               if($medicine > 0)
               {
                  DecreaseMedicine(1);
                  %medicine++;
                  %num--;
               }
            case 5:
               if($tires > 0)
               {
                  DecreaseTires(1);
                  %tires++;
                  %num--;
               }
            case 6:
               if($spareparts > 0)
               {
                  DecreaseSpareParts(1);
                  %spareparts++;
                  %num--;
               }
            case 7:
               if($fuel > 0)
               {
                  DecreaseFuel(1);
                  %fuel++;
                  %num--;
               }
         }

         // display the report
         
         Infowindow.setProfile(GuiMLProfile);            
         InfoYesbutton.Visible = false;
         InfoNobutton.Visible = false;
         Infocontinuebutton.Visible = true;
         InfoContinuebutton.command = "Infowindow.Visible = false;";   
         
         InfoText.setText("");
         InfoText.AddText("*You have lost supplies!*\n\n", true);
         InfoText.AddText("Food - " @ %food @ "\n", true);
         InfoText.AddText("Guns - " @ %guns @ "\n", true);
         InfoText.AddText("Ammo - " @ %ammo @ "\n", true);
         InfoText.AddText("Medicine - " @ %medicine @ "\n", true);
         InfoText.AddText("Tires - " @ %tires @ "\n", true);   
         InfoText.AddText("Spare Parts - " @ %spareparts @ "\n", true);   
         InfoText.AddText("Fuel - " @ %fuel @ "\n", true);   
         
         UpdateStatsBar();         
      }
   }
}

function OpenSupplyDrop()
{
   if(Infowindow.Visible $= false)
      return;
      
   canvas.PushDialog(DropSupplies);
   Infowindow.Visible = false;
}

function FindRandomSupplies()
{
   %room = GetTotalSupplyCapacity() - GetCurrentSupplyCount();
   
   if(%room <= 0)
      return;      
   
   // this will determine if they actually find anything or not
   
   %rand = getRandom(1, 100);
   
   if(%rand > 5)
      return;
      
   %list = sg.PickPoint(player.GetPosition());
   
   %count = getWordCount(%list);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%list, %i);
      
      if(%obj.city !$= "")
         %city = true;   
   }
   
   if(%city $= true)
      CitySearch();
   else
      LandSearch();
   
   // Reset Variables
   
   $SearchIndex = 1;
   $foodFound = "";
   $gunsFound = "";
   $ammoFound = "";
   $medicineFound = "";
   $tiresFound = "";
   $sparepartsFound = "";   
   $fuelFound = "";
   $StopSearchFlag = "";
   
   %chance = getRandom(1, 100);
   
   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancefood * ".15";
         %mod = mCeil(%mod);
         $Chancefood = $Chancefood+%mod;         
      case "hard":      
         %mod = $Chancefood * ".15";
         %mod = mCeil(%mod);
         $Chancefood = $Chancefood-%mod;         
   }
   
   if(%chance <= $Chancefood)
   {
      $FoodFound = getRandom(30, 200); 
      
      %rand = getRandom(1, 10);
      
      if(%rand <= 3)
         $foodFound = getRandom(240, 420);
      
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chanceguns * ".15";
         %mod = mCeil(%mod);
         $Chanceguns = $Chanceguns+%mod;         
      case "hard":      
         %mod = $Chanceguns * ".15";
         %mod = mCeil(%mod);
         $Chanceguns = $Chanceguns-%mod;         
   }

   if(%chance <= $Chanceguns)
   {
      $GunsFound = getRandom(10, 45);   
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chanceammo * ".15";
         %mod = mCeil(%mod);
         $Chanceammo = $Chanceammo+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chanceammo = $Chanceammo-%mod;         
   }

   if(%chance <= $Chanceammo)
   {
      $AmmoFound = getRandom(200, 350);
      %found = true;
   }
   
   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancemedicine * ".15";
         %mod = mCeil(%mod);
         $Chancemedicine = $Chancemedicine+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chancemedicine = $Chancemedicine-%mod;         
   }

   if(%chance <= $Chancemedicine)
   {
      $MedicineFound = getRandom(5, 30);
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancetires * ".15";
         %mod = mCeil(%mod);
         $Chancetires = $Chancetires+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chancetires = $Chancetires-%mod;         
   }

   if(%chance <= $Chancetires)
   {
      $TiresFound = getRandom(15, 75);
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancespareparts * ".15";
         %mod = mCeil(%mod);
         $Chancespareparts = $Chancespareparts+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         $Chancespareparts = $Chancespareparts-%mod;         
   }

   if(%chance <= $Chancespareparts)
   {
      $SparePartsFound = getRandom(20, 45);   
      %found = true;
   }

   %chance = getRandom(1, 100);

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = $Chancefuel * ".15";
         %mod = mCeil(%mod);
         %chance = $Chancefuel+%mod;         
      case "hard":      
         %mod = $Chancefuel * ".15";
         %mod = mCeil(%mod);
         %chance = $Chancefuel-%mod;         
   }

   // there is a decent change they will find a large cache
   
   if(%chance <= $Chancefuel)
   {
      $FuelFound = getRandom(120, 250);
      
      %rand = getRandom(1, 10);
      
      if(%rand <= 3)
         $fuelFound = getRandom(315, 575);
         
      %found = true;
   }
   
   if(%found $= true)
      PromoteScout();
         
   SearchReturn();   
   
   return true;
}
