function LoadGame()
{   
   // this means they didn't select an actual save game to load
   
   if($saveid $= "")
      return;
   
   $Wind = alxPlay(wind);
         
   infowindow.Visible = false;
   
   // clear out varibles if this is a re-load from within an existing game
   
   deleteVariables("$vehicles*");   
   deleteVariables("$tiles*");
   deleteVariables("$Special*");
   deleteVariables("$silos*");
   deleteVariables("$remainingsilos");
   deleteVariables("$city*");
   deleteVariables("$captured*");
   deleteVariables("Scientist*");

   LoadCore();       
   
   ChangeLoadingBar(200);
   schedule(500, 0, "LoadGamePhaseOne");
}
   
function LoadGamePhaseOne()
{
   LoadPeople();
   LoadSupplies();
   LoadVehicles();   
   LoadSpecialists();
   LoadNukes();
   HideSilos();
   LoadCities();
   LoadFogOfWar();   
   LoadTutorial();     
   LoadSpecialLocations();

   alxStop($IntroSong);
   alxStop($wind);   // if they are loading from within the game

   // if the load game screen is on turn it off
   
   canvas.PopDialog(SaveGameList);
   $GameStarted = true;   
   $combat = "";
   $quickbattle = "";
   $EndGame = "";
   $VerifySaveGame = true; // used for reloading a game when dying
   
   battlewindow.Visible = false;
   mainbar.Visible = true;
   PeopleBattle.Visible = false;

   canvas.cursoron();   
   canvas.SetContent(MainMap);
   
   ChangePlayerFacing($playerFacing);
   
   UpdateMainBarShortcuts();
   
   if(isObject(QuickBattleMap) $= 1)
      QuickBattleMap.delete();
   new ActionMap(moveMap);   
   moveMap.push();
   
   exec("./tutorials/keymap.cs");   

   setAcceptableScreenRatio();
 }

function LoadFogOfWar()
{
   $gcounter = 1;
     
  %file = "~/fow" @ $saveid @ ".sav";
  
  %fo = new fileobject();
   
  %fo.OpenForRead(%file);
   
  // read in all cleared tiles
  
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      // read into our known cleared tiles array
      
      $tilesrecord[%line] = true;
      $tiles[$gcounter] = %line;
      
      $gcounter++;
      
      // now mark the tile as clear

      fow.clearTile(%line);                        
   }
      
   %fo.close();
   
   InitFow(player.GetPosition());
}

function LoadSpecialLocations()
{
  %file = "~/speciallocations" @ $saveid @ ".sav";
  
  %fo = new fileobject();
   
  %fo.OpenForRead(%file);
   
  // read in all cleared tiles
  
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %type = getWord(%line, 0);
      %x = getWord(%line, 1);
      %y = getWord(%line, 2);

      %obj =    new t2dStaticSprite() {
                     canSaveDynamicFields = "1";
                     layer = 2;  
                     scenegraph = "sg";
                     size =  "7 7";
                  };
                  
      %obj.SetPosition(%x, %y);

      switch$ (%type)
      {
         case "learningcenter":
            %obj.specialplace = "Learning Center";
            %obj.Setimagemap("building5");
         case "militarybase":      
            %obj.specialplace = "Military Base";
            %obj.Setimagemap("building5");
            %obj.guns = getWord(%line, 3);
            %obj.ammo = getWord(%line, 4);
            %obj.soldiers = getWord(%line, 5);
         case "casino":
            %obj.specialplace = "Casino";
            %obj.Setimagemap("building1");
         case "airport":
            %obj.specialplace = "Airport";
            %obj.Setimagemap("building5");
            %obj.fuel = getWord(%line, 3);
            %obj.food = getWord(%line, 4);
            %obj.spareparts = getWord(%line, 5);
            %obj.spare = getWord(%line, 6);
         case "cache":
            %obj.specialplace = "Cache";  // 1 time find
            %obj.Setimagemap("crate");
            %obj.guns = getWord(%line, 3);
            %obj.ammo = getWord(%line, 4);
            %obj.fuel = getWord(%line, 5);
            %obj.food = getWord(%line, 6);
            %obj.medicine = getWord(%line, 7);
            %obj.spareparts = getWord(%line, 8);
            %obj.tires = getWord(%line, 9);
         case "hospital":
            %obj.specialplace = "Hospital";
            %obj.Setimagemap("building4");
            %obj.medicine = getWord(%line, 3);
         case "farm":
            %obj.specialplace = "Farm";
            %obj.Setimagemap("building5");
            %obj.food = getWord(%line, 3);
         case "gasstation":
            %obj.specialplace = "Gas Station";
            %obj.Setimagemap("building6");
            %obj.fuel = getWord(%line, 3);
            %obj.tires = getWord(%line, 4);
         case "autocenter":
            %obj.specialplace = "Auto Center";  // 1 time upgrade
            %obj.Setimagemap("building3");            
         case "TruckDepot":
            %obj.specialplace = "Truck Depot";
            %obj.Setimagemap("building2");
            %obj.fuel = getWord(%line, 3);
            %obj.tires = getWord(%line, 4);
      }
      
      if($specialLocations $= "")
         $specialLocations = %obj;
      else
         $specialLocations = $specialLocations SPC %obj;
   }      
   %fo.close();   
}

function LoadNukes()
{
   %fo = new fileobject();
  
   %file = "~/nukes" @ $saveid @ ".sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == -1)
      return;
   
   %i = 1;
   
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %desc = getWord(%line, 0);
      
      switch$ (%desc)
      {
         case "silo":
            $silos[getWord(%line, 1)] = true;
            
            if(getWord(%line, 2) $= "captured")
            {
               %silo = getWord(%line, 1);
               %silo.captured = true;
               %silo.SetBlendColor("0 202 41 255");
            }
            else
               $remainingsilos++;
         case "scientist":
            $ScientistLocations[%i] = getWord(%line, 1);
            %i++;
      }
   }
      
   %fo.close();
}

function LoadSpecialists()
{
   %fo = new fileobject();
  
   %file = "~/specialists" @ $saveid @ ".sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == false)
   {
      echo("ERROR LOADING SPECIALIST FILE");
      return;
   }
      
   %i=1;
   
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      $SpecialistLocations[%i] = %line;
      %i++;
   }
   
   %fo.close();
}

function LoadCities()
{
   %fo = new fileobject();
  
   %file = "~/cities" @ $saveid @ ".sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == -1)
      return;

   %i = 1;

   deleteVariables("$CityList*");
   deleteVariables("$CapturedCities*");
         
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      switch$ (GetWord(%line, 0))
      {
         case "city":
            %city = GetWord(%line, 1);
            %population = GetWord(%line, 2);
            
            $CityList[%city] = %population;
         case "captured":
            $CapturedCities[%i] = GetWord(%line, 1);
            %i++;        
            %city = getWord(%line, 1);     
            
            for(%j=1;%j<=8;%j++)
            {
               %sprite = %city @ %j;
               %sprite.visible = false;
            }
      }
   }
      
   %fo.close();   
}

function LoadSupplies()
{
   %fo = new fileobject();
  
   %file = "~/supplies" @ $saveid @ ".sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == -1)
      return;
         
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %word = GetWord(%line, 0);

      switch$ (%word)
      {         
         case "food":
            $food = getWord(%line, 1);
         case "guns":
            $guns = getWord(%line, 1);
         case "ammo":
            $ammo = getWord(%line, 1);
         case "medicine":
            $medicine = getWord(%line, 1);
         case "tires":
            $tires = getWord(%line, 1);
         case "spareparts":
            $spareparts = getWord(%line, 1);  
         case "fuel":
            $fuel = getWord(%line, 1);
      }
   }
   
   %fo.close();   
}

function LoadPeople()
{
   %fo = new fileobject();
  
   %file = "~/people" @ $saveid @ ".sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == -1)
      return;
         
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %word = GetWord(%line, 0);

      switch$ (%word)
      {         
         case "refuges":
            $refuges = getWord(%line, 1);
         case "brigands":
            $Brigands = getWord(%line, 1);
         case "militia":
            $Militia = getWord(%line, 1);
         case "police":
            $Police = getWord(%line, 1);
         case "soldiers":
            $Soldiers = getWord(%line, 1);
         case "mercenaries":
            $Mercenaries = getWord(%line, 1);
         case "doctor":
            $doctor = getWord(%line, 1);
         case "doctorExperience":
            $doctorExperience = getWord(%line, 1);
         case "mechanic":
            $Mechanic = getWord(%line, 1);
         case "mechanicExperience":
            $MechanicExperience = getWord(%line, 1);
         case "Sergeant":
            $Sergeant = getWord(%line, 1);
         case "SergeantExperience":
            $SergeantExperience = getWord(%line, 1);         
         case "Recruiter":
            $Recruiter = getWord(%line, 1);
         case "RecruiterExperience":
            $RecruiterExperience = getWord(%line, 1);
         case "Scout":
            $Scout = getWord(%line, 1);
         case "ScoutExperience":
            $ScoutExperience = getWord(%line, 1);
         case "Scientists":
            $scientists = getWord(%line, 1);         
      }
   }
   
   %fo.close();   
}

function LoadVehicles()
{
   %fo = new fileobject();
  
   %file = "~/vehicles" @ $saveid @ ".sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == -1)
      return;

   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %word = GetWord(%line, 1);
      %slot = getWord(%line, 0);
      %value = getWord(%line, 2);
      
      switch$ (%word)
      {         
         case "vehicle":
            $vehicles[%slot, vehicle] = %value; 
         case "currentvehiclehp":
            $vehicles[%slot, currentvehiclehp] = %value; 
         case "stockvehiclehp":
            $vehicles[%slot, stockvehiclehp] = %value; 
         case "stockarmor":
            $vehicles[%slot, stockarmor] = %value;
         case "currentarmor":
            $vehicles[%slot, currentarmor] = %value;
         case "tires":
            $vehicles[%slot, tires] = %value;              
         case "tireupgrade":
            $vehicles[%slot, tireupgrade] = %value;              
         case "currentenginehp":
            $vehicles[%slot, currentenginehp] = %value;              
         case "stockenginehp":
            $vehicles[%slot, stockenginehp] = %value;              
         case "brakes":
            $vehicles[%slot, brakes] = %value;              
         case "acceleration":
            $vehicles[%slot, acceleration] = %value;              
         case "topspeed":
            $vehicles[%slot, topspeed] = %value;              
         case "steering":
            $vehicles[%slot, steering] = %value;              
         case "stocktirearmor":
            $vehicles[%slot, stocktirearmor] = %value;              
         case "currenttirearmor":
            $vehicles[%slot, currenttirearmor] = %value;              
         case "stockgastankarmor":
            $vehicles[%slot, stockgastankarmor] = %value;              
         case "currentgastankarmor":
            $vehicles[%slot, currentgastankarmor] = %value;              
         case "badtires":
            $vehicles[%slot, badtires] = %value;              
         case "mountedweapon":
            $vehicles[%slot, mountedweapon] = %value;              
         case "currentmountedweaponhp":
            $vehicles[%slot, currentmountedweaponhp] = %value;
         case "stockmountedweaponhp":
            $vehicles[%slot, stockmountedweaponhp] = %value;
         case "currentramhp":
            $vehicles[%slot, currentramhp] = %value;
         case "stockramhp":
            $vehicles[%slot, stockramhp] = %value;
         case "currentwindowarmor":
            $vehicles[%slot, currentwindowarmor] = %value;
         case "stockwindowarmor":
            $vehicles[%slot, stockwindowarmor] = %value;
         case "supplycapacity":
            $vehicles[%slot, supplycapacity] = %value;
         case "topspeedlevel":
            $vehicles[%slot, topspeedlevel] = %value;
      }
   }
   
   %fo.close();   
}

function LoadCore()
{
   %fo = new fileobject();
  
   %file = "~/save" @ $saveid @ ".sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == -1)
      return;
         
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %word = GetWord(%line, 0);

      switch$ (%word)
      {         
         case "clock":
            $clock = restWords(%line);
         case "mastertime":
            $mastertime = restWords(%line);
         case "morale":
            $morale = restWords(%line);
         case "disease":
            $disease = restWords(%line);
         case "diseasetime":
            $diseasetime = restWords(%line);
         case "diseasecheck":
            $diseasecheck = restWords(%line);
         case "upgradepoints":
            $UpgradePoints = restWords(%line);
         case "capturedcities":
            $CapturedCities = restWords(%line);
         case "location":
            %position = restWords(%line);
         case "starving":
            $starving = restWords(%line);
         case "battleformation":
            $battleformation = restWords(%line);
         case "foodcontime":
            $checkFoodConTime = restWords(%line);
         case "lastcombat":
            $lastCombat = restWords(%line);
         case "fullscreen":
            $fullscreen = restWords(%line);
         case "currentdscontrol":
            $CurrentDSControl = restWords(%line);
         case "moralecheck":
            $RecurringMoraleCheck = restWords(%line);
         case "scoutingpartysize":
            $scoutingParty = restWords(%line);
         case "casinouse":
            $CasinoUse = restWords(%line);
         case "tradein":
            $TradeIn = restWords(%line);
         case "truckdepotuse":
            $truckdepotuse = restWords(%line);
         case "PlayerFacing":
            $playerFacing = restWords(%line);
      }
   }
   
   %fo.close();
   
   Screen2D.loadLevel(expandFilename("~/data/levels/main.t2d"));
   fow.Visible = true;
   Screen2D.mount(player);

   player.SetPosition(%position);   

   $RoadCombatChance = 3;
   $CountryCombatChance = 1;

   CheckTradeInTime();
}

function LoadTutorial()
{
   %fo = new fileobject();
  
   %file = "~/tutorial.sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == false)
      return;
         
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %word = GetWord(%line, 0);

      switch$ (%word)
      {         
         case "block":
            $Tutorial_block = true;
         case "silo":
            $Tutorial_silo = true;
         case "city":
            $Tutorial_city = true;
         case "doctor":
            $Tutorial_doctor = true;
         case "recruiter":
            $Tutorial_recruiter = true;
         case "scout":
            $Tutorial_scout = true;
         case "mechanic":
            $Tutorial_mechanic = true;
         case "sargeant":
            $Tutorial_sargeant = true;
         case "scientist":
            $Tutorial_scientist = true;
         case "peoplesearch":
            $Tutorial_peoplesearch = true;
         case "vehiclesearch":
            $Tutorial_vehiclesearch = true;
         case "supplysearch":
            $Tutorial_supplysearch = true;
         case "aftercombat":
            $Tutorial_aftercombat = true;
         case "startcombat":
            $Tutorial_startcombat = true;
         case "acceleration":
            $Tutorial_acceleration = true;
         case "specialorders":
            $Tutorial_SpecialOrders = true;
         case "specialevent":
            $Tutorial_SpecialEvent = true;
         case "starving":
            $Tutorial_starving = true;
         case "citycapture":
            $Tutorial_citycapture = true;
         case "acceleration":
            $Tutorial_acceleration = true;
         case "failedcitycapture":
            $Tutorial_FailedCityCapture = true;
         case "casino":
            $Tutorial_casino = true;
         case "learningcenter":
            $Tutorial_Learningcenter = true;
         case "militarybase":
            $Tutorial_MilitaryBase = true;
         case "airport":
            $Tutorial_Airport = true;
         case "Cache":
            $Tutorial_Cache = true;
         case "Hospital":
            $Tutorial_Hospital = true;
         case "farm":
            $Tutorial_farm = true;
         case "gasstation":
            $Tutorial_GasStation = true;
         case "autocenter":
            $Tutorial_AutoCenter = true;
         case "truckdepot":
            $Tutorial_TruckDepot = true;
         case "Upgrade":
            $Tutorial_Upgrade = true;
         case "SelectTarget":
            $Tutorial_SelectTarget = true;
         case "DoNotFire":
            $Tutorial_DoNotFire = true;
         case "MayNotBoard":
            $Tutorial_MayNotBoard = true;
         case "SpareParts":
            $Tutorial_SpareParts = true;
         case "InfantryBattle":
            $Tutorial_InfantryBattle = true;
         case "VehicleBattle":
            $Tutorial_VehicleBattle = true;
      }
   }
   
   %fo.close();
}

// this is to prevent this tutorial to keep popping up if someone only plays the quick battles, it's unlikely 
// but still a chance

function LoadVehicleBattleTutorial()
{
   %fo = new fileobject();
  
   %file = "~/vehicletutorial.sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == false)
      return;
         
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %word = GetWord(%line, 0);

      switch$ (%word)
      {         
         case "vehicletutorial":
            $Tutorial_VehicleBattle = true;
      }      
   }
}