function SaveGame()
{
   SaveSupplies();
   SaveCore();
   SavePeople();   
   SaveVehicles();
   SaveCities();
   SaveSpecialists();
   SaveNukes();
   SaveFow();
   SaveTutorial();
   SaveSpecialLocations();
   
   $VerifySaveGame = true; // used for reloading a game when dying
}

// have this save the auto save option as well to make it easy

function SaveDifficulty()
{
   %fo = new fileobject();
  
   %file = "~/difficulty.sav";
 
   %fo.OpenForWrite(%file);   
   
   %fo.WriteLine("difficulty " @ $Difficulty);              
   
   if(autosave.getValue() $= 1)
      %fo.WriteLine("autosave 1");              
   else
      %fo.WriteLine("autosave 1");              
            
   %fo.close();   
}

function SaveCore()
{
   %fo = new fileobject();
  
   %file = "~/save" @ $saveid @ ".sav";
 
   %fo.OpenForWrite(%file);   
   
   %fo.WriteLine("saveid " @ $saveid);              
   %fo.WriteLine("battleformation " @ $battleformation);              
   %fo.WriteLine("clock " @ $clock);              
   %fo.WriteLine("mastertime " @ $mastertime);              
   %fo.WriteLine("savename " @ $savename);              
   %fo.WriteLine("morale " @ $morale);              
   %fo.WriteLine("disease " @ $disease);              
   %fo.WriteLine("diseasetime " @ $diseasetime);              
   %fo.WriteLine("upgradepoints " @ $UpgradePoints);              
   %fo.WriteLine("capturedcities " @ $CapturedCities);              
   %fo.WriteLine("location " @ player.GetPosition());              
   %fo.WriteLine("starving " @ $starving);              
   %fo.WriteLine("foodcontime " @ $checkFoodConTime);              
   %fo.WriteLine("lastcombat " @ $lastCombat);              
   %fo.WriteLine("fullscreen " @ $fullscreen);              
   %fo.WriteLine("resolution " @ $selectedRes);              
   %fo.WriteLine("currentdscontrol " @ $CurrentDSControl);              
   %fo.WriteLine("moralecheck " @ $RecurringMoraleCheck);              
   %fo.WriteLine("scoutingpartysize " @ $scoutingParty);              
   %fo.WriteLine("casinouse " @ $CasinoUse);              
   %fo.WriteLine("truckdepotuse " @ $truckdepotuse);    
   %fo.WriteLine("tradein " @ $TradeIn);
   %fo.WriteLine("PlayerFacing " @ player.facing);  
      
   %fo.close();   
}

function SaveSupplies()
{
   %fo = new fileobject();
  
   %file = "~/supplies" @ $saveid @ ".sav";
 
   %fo.OpenForWrite(%file);   
   
   %fo.WriteLine("food " @ $food);              
   %fo.WriteLine("guns " @ $guns);              
   %fo.WriteLine("ammo " @ $ammo);              
   %fo.WriteLine("medicine " @ $medicine);              
   %fo.WriteLine("tires " @ $tires);              
   %fo.WriteLine("spareparts " @ $spareparts);              
   %fo.WriteLine("fuel " @ $fuel);       
   
   %fo.close();          
}


function SavePeople()
{
   %fo = new fileobject();
  
   %file = "~/people" @ $saveid @ ".sav";
 
   %fo.OpenForWrite(%file);   
   
   %fo.WriteLine("refuges " @ $refuges);              
   %fo.WriteLine("brigands " @ $brigands);              
   %fo.WriteLine("militia " @ $militia);              
   %fo.WriteLine("police " @ $police);              
   %fo.WriteLine("soldiers " @ $soldiers);              
   %fo.WriteLine("mercenaries " @ $Mercenaries);              
   %fo.WriteLine("doctor " @ $doctor);              
   %fo.WriteLine("doctorExperience " @ $doctorExperience);              
   %fo.WriteLine("mechanic " @ $mechanic);              
   %fo.WriteLine("mechanicExperience " @ $mechanicExperience);              
   %fo.WriteLine("sergeant " @ $sergeant);              
   %fo.WriteLine("sergeantExperience " @ $SergeantExperience);              
   %fo.WriteLine("recruiter " @ $recruiter);              
   %fo.WriteLine("recruiterExperience " @ $RecruiterExperience);              
   %fo.WriteLine("scout " @ $scout);              
   %fo.WriteLine("scoutExperience " @ $scoutExperience);              
   %fo.WriteLine("scientists " @ $scientists);              

   %fo.close();
}

function SaveVehicles()
{
   %fo = new fileobject();
  
   %file = "~/vehicles" @ $saveid @ ".sav";
 
   %fo.OpenForWrite(%file);   
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         %fo.WriteLine(%i SPC "vehicle " @ $vehicles[%i, vehicle]);              
         %fo.WriteLine(%i SPC "currentvehiclehp " @ $vehicles[%i, currentvehiclehp]);              
         %fo.WriteLine(%i SPC "stockvehiclehp " @ $vehicles[%i, stockvehiclehp]);              
         %fo.WriteLine(%i SPC "stockarmor " @ $vehicles[%i, stockarmor]);          
         %fo.WriteLine(%i SPC "currentarmor " @ $vehicles[%i, currentarmor]);                       
         %fo.WriteLine(%i SPC "tires " @ $vehicles[%i, tires]);              
         %fo.WriteLine(%i SPC "badtires " @ $vehicles[%i, badtires]);              
         %fo.WriteLine(%i SPC "tireupgrade " @ $vehicles[%i, tireupgrade]);              
         %fo.WriteLine(%i SPC "currentenginehp " @ $vehicles[%i, currentenginehp]);              
         %fo.WriteLine(%i SPC "stockenginehp " @ $vehicles[%i, stockenginehp]);              
         %fo.WriteLine(%i SPC "brakes " @ $vehicles[%i, brakes]);              
         %fo.WriteLine(%i SPC "acceleration " @ $vehicles[%i, acceleration]);              
         %fo.WriteLine(%i SPC "topspeed " @ $vehicles[%i, topspeed]);              
         %fo.WriteLine(%i SPC "steering " @ $vehicles[%i, steering]);              
         %fo.WriteLine(%i SPC "stocktirearmor " @ $vehicles[%i, stocktirearmor]);              
         %fo.WriteLine(%i SPC "currenttirearmor " @ $vehicles[%i, currenttirearmor]);              
         %fo.WriteLine(%i SPC "stockgastankarmor " @ $vehicles[%i, stockgastankarmor]);              
         %fo.WriteLine(%i SPC "currentgastankarmor " @ $vehicles[%i, currentgastankarmor]);              
         %fo.WriteLine(%i SPC "mountedweapon " @ $vehicles[%i, mountedweapon]);              
         %fo.WriteLine(%i SPC "currentmountedweaponhp " @ $vehicles[%i, currentmountedweaponhp]);              
         %fo.WriteLine(%i SPC "stockmountedweaponhp " @ $vehicles[%i, stockmountedweaponhp]);              
         %fo.WriteLine(%i SPC "currentramhp " @ $vehicles[%i, currentramhp]);              
         %fo.WriteLine(%i SPC "stockramhp " @ $vehicles[%i, stockramhp]);              
         %fo.WriteLine(%i SPC "currentwindowarmor " @ $vehicles[%i, currentwindowarmor]);              
         %fo.WriteLine(%i SPC "stockwindowarmor " @ $vehicles[%i, stockwindowarmor]);              
         %fo.WriteLine(%i SPC "supplycapacity " @ $vehicles[%i, supplycapacity]);              
         %fo.WriteLine(%i SPC "topspeedlevel " @ $vehicles[%i, topspeedlevel]);              
      }
   }
   
   %fo.close();   
}

function SaveCities()
{
   %fo = new fileobject();
  
   %file = "~/cities" @ $saveid @ ".sav";
 
   %fo.OpenForWrite(%file);   
   
   if($CityList[NewYork] > 0)
      %fo.WriteLine("city NewYork " @ $CityList[NewYork]);
   if($CityList[LosAngeles] > 0)
      %fo.WriteLine("city LosAngeles " @ $CityList[LosAngeles]);
   if($CityList[Chicago] > 0)
      %fo.WriteLine("city Chicago " @ $CityList[Chicago]);
   if($CityList[Houston] > 0)
      %fo.WriteLine("city Houston " @ $CityList[Houston]);
   if($CityList[Phoenix] > 0)
      %fo.WriteLine("city Phoenix " @ $CityList[Phoenix]);
   if($CityList[Philadelphia] > 0)
      %fo.WriteLine("city Philadelphia " @ $CityList[Philadelphia]);
   if($CityList[SanAntonio] > 0)
      %fo.WriteLine("city SanAntonio " @ $CityList[SanAntonio]);
   if($CityList[Dallas] > 0)
      %fo.WriteLine("city Dallas " @ $CityList[Dallas]);
   if($CityList[SanDiego] > 0)
      %fo.WriteLine("city SanDiego " @ $CityList[SanDiego]);
   if($CityList[Detroit] > 0)
      %fo.WriteLine("city Detroit " @ $CityList[Detroit]);
   if($CityList[SanFrancisco] > 0)
      %fo.WriteLine("city SanFrancisco " @ $CityList[SanFrancisco]);
   if($CityList[Jacksonville] > 0)
      %fo.WriteLine("city Jacksonville " @ $CityList[Jacksonville]);
   if($CityList[Austin] > 0)
      %fo.WriteLine("city Austin " @ $CityList[Austin]);
   if($CityList[Columbus] > 0)
      %fo.WriteLine("city Columbus " @ $CityList[Columbus]);
   if($CityList[Charlotte] > 0)
      %fo.WriteLine("city Charlotte " @ $CityList[Charlotte]);
   if($CityList[Memphis] > 0)
      %fo.WriteLine("city Memphis " @ $CityList[Memphis]);
   if($CityList[Baltimore] > 0)
      %fo.WriteLine("city Baltimore " @ $CityList[Baltimore]);
   if($CityList[ElPaso] > 0)
      %fo.WriteLine("city ElPaso " @ $CityList[ElPaso]);
   if($CityList[Boston] > 0)
      %fo.WriteLine("city Boston " @ $CityList[Boston]);
   if($CityList[Denver] > 0)
      %fo.WriteLine("city Denver " @ $CityList[Denver]);
   if($CityList[Seattle] > 0)
      %fo.WriteLine("city Seattle " @ $CityList[Seattle]);
   if($CityList[Nashville] > 0)
      %fo.WriteLine("city Nashville " @ $CityList[Nashville]);
   if($CityList[Washington] > 0)
      %fo.WriteLine("city Washington " @ $CityList[Washington]);
   if($CityList[LasVegas] > 0)
      %fo.WriteLine("city LasVegas " @ $CityList[LasVegas]);
   if($CityList[Portland] > 0)
      %fo.WriteLine("city Portland " @ $CityList[Portland]);
   if($CityList[Louisville] > 0)
      %fo.WriteLine("city Louisville " @ $CityList[Louisville]);
   if($CityList[Tucson] > 0)
      %fo.WriteLine("city Tucson " @ $CityList[Tucson]);
   if($CityList[Atlanta] > 0)
      %fo.WriteLine("city Atlanta " @ $CityList[Atlanta]);
   if($CityList[Albuqurque] > 0)
      %fo.WriteLine("city Albuqurque " @ $CityList[Albuqurque]);
   if($CityList[Fresno] > 0)
      %fo.WriteLine("city Fresno " @ $CityList[Fresno]);
   if($CityList[Sacramento] > 0)
      %fo.WriteLine("city Sacramento " @ $CityList[Sacramento]);
   if($CityList[KansasCity] > 0)
      %fo.WriteLine("city KansasCity " @ $CityList[KansasCity]);
   if($CityList[Omaha] > 0)
      %fo.WriteLine("city Omaha " @ $CityList[Omaha]);
   if($CityList[Cleveland] > 0)
      %fo.WriteLine("city Cleveland " @ $CityList[Cleveland]);
   if($CityList[Miami] > 0)
      %fo.WriteLine("city Miami " @ $CityList[Miami]);
   if($CityList[Raleigh] > 0)
      %fo.WriteLine("city Raleigh " @ $CityList[Raleigh]);
   if($CityList[Tulsa] > 0)
      %fo.WriteLine("city Tulsa " @ $CityList[Tulsa]);
   if($CityList[Minneapolis] > 0)
      %fo.WriteLine("city Minneapolis " @ $CityList[Minneapolis]);
   if($CityList[ColoradoSprings] > 0)
      %fo.WriteLine("city ColoradoSprings " @ $CityList[ColoradoSprings]);
   if($CityList[Wichita] > 0)
      %fo.WriteLine("city Wichita " @ $CityList[Wichita]);
   if($CityList[StLouis] > 0)
      %fo.WriteLine("city StLouis " @ $CityList[StLouis]);
   if($CityList[Tampa] > 0)
      %fo.WriteLine("city Tampa " @ $CityList[Tampa]);
   if($CityList[Bakersfield] > 0)
      %fo.WriteLine("city Bakersfield " @ $CityList[Bakersfield]);
   if($CityList[NewOrleans] > 0)
      %fo.WriteLine("city NewOrleans " @ $CityList[NewOrleans]);
   if($CityList[Pittsburgh] > 0)
      %fo.WriteLine("city Pittsburgh " @ $CityList[Pittsburgh]);
   if($CityList[Riverside] > 0)
      %fo.WriteLine("city Riverside " @ $CityList[Riverside]);
   if($CityList[Stockton] > 0)     
      %fo.WriteLine("city Stockton " @ $CityList[Stockton]);
   if($CityList[CorpusChristi] > 0)
      %fo.WriteLine("city CorpusChristi " @ $CityList[CorpusChristi]);
   if($CityList[Buffalo] > 0)
      %fo.WriteLine("city Buffalo " @ $CityList[Buffalo]);
   if($CityList[JerseyCity] > 0)
      %fo.WriteLine("city JerseyCity " @ $CityList[JerseyCity]);
   if($CityList[Norfolk] > 0)
      %fo.WriteLine("city Norfolk " @ $CityList[Norfolk]);
   if($CityList[Orlando] > 0)
      %fo.WriteLine("city Orlando " @ $CityList[Orlando]);
   if($CityList[Birmingham] > 0)
      %fo.WriteLine("city Birmingham " @ $CityList[Birmingham]);
   if($CityList[Reno] > 0)
      %fo.WriteLine("city Reno " @ $CityList[Reno]);
   if($CityList[SaltLakeCity] > 0)
      %fo.WriteLine("city SaltLakeCity " @ $CityList[SaltLakeCity]);   
   if($CityList[Spokane] > 0)
      %fo.WriteLine("city Spokane " @ $CityList[Spokane]);   
   
   // write out the captured cities, this could be done easier but I'm being lazy right now
   
   for(%i=1;%i<=56;%i++)
   {
      if($CapturedCities[%i] !$= "")
         %fo.WriteLine("captured " @ $CapturedCities[%i]);
   }
   
   %fo.close();   
}

function SaveSpecialists()
{   
   %fo = new fileobject();
  
   %file = "~/specialists" @ $saveid @ ".sav";
 
   %fo.OpenForWrite(%file);   
   
   for(%i=1;%i<=10;%i++)
   {
      if($SpecialistLocations[%i] !$= "")
         %fo.WriteLine($SpecialistLocations[%i]);
   }
   
   %fo.close();   
}

function SaveNukes()
{   
   %fo = new fileobject();
  
   %file = "~/nukes" @ $saveid @ ".sav";
 
   %fo.OpenForWrite(%file);   
   
   for(%i=1;%i<=30;%i++)
   {
      %captured = "";
      %silo = "silo" @ %i;
      
      if($silos[%silo] $= true)
      {
         if(%silo.captured $= true)
            %captured = "captured";
            
         %fo.WriteLine("silo" SPC %silo SPC %captured);
      }
   }

   for(%i=1;%i<=20;%i++)
   {
      if($ScientistLocations[%i] !$= "")
         %fo.WriteLine("scientist" SPC $ScientistLocations[%i]);
   }

   %fo.close();   
}

function SaveFow()
{
   %fo = new fileobject();
     
   %file = "~/fow" @ $saveid @ ".sav";
      
   %fo.OpenForWrite(%file);

   for(%i=1;%i<=$gcounter;%i++)
   {
      %fo.WriteLine($tiles[%i]);                        
   }
      
   %fo.close();
}

// NOTE - if a location has limited supplies it will be saved on the same line without being marked
// so on the load section it needs to read in the data on the same order

function SaveSpecialLocations()
{
   %fo = new fileobject();
     
   %file = "~/speciallocations" @ $saveid @ ".sav";
      
   %fo.OpenForWrite(%file);
   
   %count = getWordCount($specialLocations);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord($specialLocations, %i);
      
      %line = "";
      
      switch$ (%obj.specialplace)
      {
         case "Military Base":
            %line = "militarybase" SPC %obj.GetPosition() SPC %obj.guns SPC %obj.ammo SPC %obj.soldiers;
         case "Airport":
            %line = "airport" SPC %obj.GetPosition() SPC %obj.fuel SPC %obj.food SPC %obj.spareparts SPC %obj.tires;
         case "Cache":
            %line = "cache" SPC %obj.GetPosition() SPC %obj.guns SPC %obj.ammo SPC %obj.fuel SPC %obj.food SPC %obj.medicine SPC %obj.spareparts SPC %obj.tires;
         case "Hospital":
            %line = "hospital" SPC %obj.GetPosition() SPC %obj.medicine;
         case "Farm":
            %line = "farm" SPC %obj.GetPosition() SPC %obj.food;
         case "Gas Station":
            %line = "gasstation" SPC %obj.GetPosition() SPC %obj.fuel SPC %obj.tires;
         case "Truck Depot":
            %line = "truckdepot" SPC %obj.GetPosition() SPC %obj.fuel SPC %obj.tires;
         case "Auto Center":
            %line = "autocenter" SPC %obj.GetPosition();
         case "Learning Center":
            %line = "learningcenter" SPC %obj.GetPosition();
         case "Casino":
            %line = "casino" SPC %obj.GetPosition();
      }

      %fo.WriteLine(%line);                        
   }
      
   %fo.close();
}

function SaveTutorial()
{
   %fo = new fileobject();
     
   %file = "~/tutorial.sav";
      
   %fo.OpenForWrite(%file);
   
   if($Tutorial_City $= true)
      %fo.WriteLine("city");                        
   if($Tutorial_block $= true)
      %fo.WriteLine("block");                        
   if($Tutorial_silo $= true)
      %fo.WriteLine("silo");                        
   if($Tutorial_doctor $= true)
      %fo.WriteLine("doctor");                        
   if($Tutorial_sargeant $= true)
      %fo.WriteLine("sargeant");                        
   if($Tutorial_recruiter $= true)
      %fo.WriteLine("recruiter");                        
   if($Tutorial_scout $= true)
      %fo.WriteLine("scout");                        
   if($Tutorial_mechanic $= true)
      %fo.WriteLine("mechanic");                        
   if($Tutorial_scientist $= true)
      %fo.WriteLine("scientist");                        
   if($Tutorial_searchsupplies $= true)
      %fo.WriteLine("searchsupplies");                        
   if($Tutorial_supplysearch $= true)
      %fo.WriteLine("supplysearch");                        
   if($Tutorial_vehiclesearch $= true)
      %fo.WriteLine("vehiclesearch");                        
   if($Tutorial_aftercombat $= true)
      %fo.WriteLine("aftercombat");                        
   if($Tutorial_startcombat $= true)
      %fo.WriteLine("startcombat");                        
   if($Tutorial_acceleration $= true)
      %fo.WriteLine("acceleration");                        
   if($Tutorial_SpecialOrders $= true)
      %fo.WriteLine("specialorders");                        
   if($Tutorial_SpecialEvent $= true)
      %fo.WriteLine("specialevent");                        
   if($Tutorial_starving $= true)
      %fo.WriteLine("starving");                        
   if($Tutorial_citycapture $= true)
      %fo.WriteLine("citycapture");                        
   if($Tutorial_acceleration $= true)
      %fo.WriteLine("acceleration");                        
   if($Tutorial_FailedCityCapture $= true)
      %fo.WriteLine("failedcitycapture");                        
   if($Tutorial_Upgrade $= true)
      %fo.WriteLine("upgrade");                        
   if($Tutorial_SelectTarget $= true)
      %fo.WriteLine("selecttarget");                        
   if($Tutorial_DoNotFire $= true)
      %fo.WriteLine("donotfire");                        
   if($Tutorial_MayNotBoard $= true)
      %fo.WriteLine("maynotboard");                        
   if($Tutorial_AutoCenter $= true)
      %fo.WriteLine("autocenter");                        
   if($Tutorial_AirPort $= true)
      %fo.WriteLine("Airport");                        
   if($Tutorial_Cache $= true)
      %fo.WriteLine("cache");                        
   if($Tutorial_Militarybase $= true)
      %fo.WriteLine("militarybase");                        
   if($Tutorial_Hospital $= true)
      %fo.WriteLine("hospital");                        
   if($Tutorial_farm $= true)
      %fo.WriteLine("farm");                        
   if($Tutorial_GasStation $= true)
      %fo.WriteLine("gasstation");                        
   if($Tutorial_TruckDepot $= true)
      %fo.WriteLine("truckdepot");                        
   if($Tutorial_Learningcenter $= true)
      %fo.WriteLine("learningcenter");                        
   if($Tutorial_casino $= true)
      %fo.WriteLine("casino");                        
   if($Tutorial_SpareParts $= true)
      %fo.WriteLine("spareparts");                        
   if($Tutorial_InfantryBattle $= true)
      %fo.WriteLine("infantrybattle");                        
      
   %fo.close();
}

function SaveVehicleBattleTutorial()
{
   %fo = new fileobject();
     
   %file = "~/vehicletutorial.sav";
      
   %fo.OpenForWrite(%file);
   
   %fo.WriteLine("vehicletutorial true");                        
   
   %fo.close();
}

function AutoSave()
{
   if($AutoSave $= 1)
      SaveGame();   
}
