// main map navigation

function Screen2D::OnMouseDown(%this, %mod, %coords, %click)
{
   if($combat $= "")
   {
      // if the player has a custom order selected and selects the ground when vehicle combat ends
      // it will crash the game
      
      if(isObject(map) $= 0)
         return;
         
      // find the tile first, if it's the same tile they are clicking on again just ignore this
   
      %x = getWord(%coords, 0);
      %y = getWord(%coords, 1);
      %tile = map.pickTile(%x, %y);
      
      if(%tile !$= $LastTilePick)
         MapMovement(%coords);   
   }
   
   if($combat $= true && $peoplebattle $= "") 
      CombatSelection(%coords);
   
   if($peoplebattle $= true && $TargetEnemySoldiers $= "" && $TargetPlayerSoldiers $= "")
      MovePlayerInfantry(%coords);
   if($peoplebattle $= true && $TargetEnemySoldiers $= true)
      TargetEnemySoldiers(%coords);
   if($peoplebattle $= true && $TargetPlayerSoldiers $= true)
      SelectInfantrySquad(%coords);
}

function Screen2D::OnRightMouseDown(%this, %mod, %coords, %click)
{
   if($combat $= true)
      ClearCustomOrders($CombatCamera);   
}

function Screen2D::OnMouseMove(%this, %mod, %coords, %click)
{
   if($combat $= "")
      DisplayLocationName(%this, %mod, %coords, %click);      
   if($combat $= true && $peoplebattle $= "")
      DisplayEnemyVehicle(%this, %mod, %coords, %click);   
   if($peoplebattle $= true)
      ScanForSoldierSelection(%coords);
}


function DisplayLocationName(%this, %mod, %coords, %click)
{
   if(isObject(fow) $= false)
      return;

   %tile = fow.pickTile(getWord(%coords, 0), getWord(%coords, 1));
   
   if($tilesrecord[%tile] !$= true)
      return;
      
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.city !$= "" || %obj.silo !$= "" || %obj.specialplace !$= "")
      {
         if(%obj.city !$= "")
            %text = %obj.city;
         if(%obj.silo !$= "")
            %text = "Nuclear Silo";
         if(%obj.specialplace !$= "")
            %text = %obj.specialplace;
            
         %citytile = map.pickTile(%obj.GetPosition());
         %playertile = map.pickTile(player.GetPosition());

         %x = getWord(%citytile, 0);
         %y = getWord(%citytile, 1);
         
         %newtile = %x SPC %y+1;
         
         %x = getWord(%obj.GetPosition(), 0);
         %y = getWord(%obj.GetPosition(), 1);
                           
         if(%newtile !$= %playertile)
            textdisplay.SetPosition(%x SPC %y+10);
         else
            textdisplay.SetPosition(%x SPC %y-10);
         
         // see if the player is there
                           
         %ret = DisplayCity(%text);
         
         if(%ret $= "")
            textdisplay.text = %text;         
         else
            textdisplay.text = %ret;         
               
         textdisplay.visible = true;
             
         %display = true;
      }
   }
   
   if(%display $= "")
      textdisplay.visible = false;
}

function MapMovement(%coords)
{
   %x = getWord(%coords, 0);
   %y = getWord(%coords, 1);
   $LastTilePick = map.pickTile(%x, %y);
   
   alxStopAll();
   
   if(CheckSearchWindows() $= true)
   {
      alxPlay(buzzer);
      return;
   }
   
   // if they click on the water return
   
   %type = water.GetTileType(water.PickTile(%coords));      
   
   %image = getWord(%type, 1);
   
   if(%image $= "lakecreektiles14ImageMap")
   {
      alxPlay(buzzer);
      return;
   }   
   
   %objects = sg.pickPoint(%coords);   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.block $= 1)
      {
         alxPlay(buzzer);
         DisplayTutorial(block);
         $LastTilePick = "";
         return;
      }
      
      if(%obj.road $= 1)
         %road = true;
   }
         
   $currentgrid = "map";
   
   %ret = FindGridPath("player", %coords);
   
   if(%ret == -1)
   {
      echo("No Path - selection.cs");
      return;
   }
   
   %gas = GetTotalFuelConsumption();

   if(%road $= true)
   {
      %num = %gas * ".20";
      %num = mCeil(%num); 
      %gas = %gas-%num;
   }
   
   if(%gas > $fuel)
   {
      infowindow.Visible = true;
      Infocontinuebutton.Visible = true;
      InfoYesbutton.Visible = false;
      InfoNobutton.Visible = false;

      InfoText.setText("Your gang has run out of fuel. You can still search for supplies to try and find a new fuel resource.");
      infocontinuebutton.command = "Infowindow.Visible = false;";      
      $LastTilePick = "";
            
      alxPlay(buzzer);
      UpdateStatsBar();
      return;
   }
   
   if($pathlength > 1)   
      $pathlength--;

   %dest = $GridPath[$pathlength];
   
   echo("destination " @ %dest);
   
   // figure out facing
   
   %otile = map.picktile(player.GetPosition());
   %ox = getWord(%otile, 0);
   %oy = getWord(%otile, 1);
   
   %dtile = map.picktile(%dest);   
   %dx = getWord(%dtile, 0);
   %dy = getWord(%dtile, 1);
   
   player.ox = %ox;
   player.oy = %oy;
      
   // north
   if(%ox == %dx && %oy > %dy)
   {
      player.setRotation(0);           
      player.facing = 1;
   }
      
   // north east
   if(%ox < %dx && %oy > %dy)
   {
      player.setRotation(45);           
      player.facing = 2;
   }

   // east
   if(%ox < %dx && %oy == %dy)
   {
      player.setRotation(90);           
      player.facing = 3;
   }
   
   // south east
   if(%ox < %dx && %oy < %dy)
   {
      player.setRotation(135);           
      player.facing = 4;
   }
   
   // south
   if(%ox == %dx && %oy < %dy)
   {
      player.setRotation(180);           
      player.facing = 5;
   }
   // south west
   if(%ox > %dx && %oy < %dy)
   {
      player.setRotation(225);           
      player.facing = 6;
   }
   
   // west
   if(%ox > %dx && %oy == %dy)
   {
      player.setRotation(270);           
      player.facing = 7;
   }
   
   // north west
   if(%ox > %dx && %oy > %dy)
   {
      player.setRotation(315);           
      player.facing = 8;
   }

   UpdateStatsBar(%gas);
   
   greenflag.visible = true;
   greenflag.SetPosition(%coords);
   
   if($pathlength !$= 0)
      player.moveto(%dest, 25, true, true, true);

//   alxStop($wind);
   $MoveSound = alxPlay(mapmove);
   player.gridindex = 0;            
}

function player::OnPositionTarget(%this)
{
   InitFow(player.GetPosition());
      
   if(UpdateTime() $= true)
      return;
   
   // check for a nuclear site, if this is a nuclear site no random car combat and no people attacks
   
   %objects = sg.pickPoint(player.getPosition());   
   
   %count = getWordCount(%objects);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.silo !$= "")
      {
         CheckNukeSite(%obj.captured);
         %nuke = true;
         break;
      }
   }   
   
   if(%nuke $= "")
      %ret = CheckAllEvents();
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%objects, %i);
      
      if(%obj.city !$= "")
         DisplayTutorial(city);
      
      if(%obj.road $= 1)
         %road = true;      
   }   
   
   if(%ret $= true)
   {
      echo("STOP AT ON POSITION - RETURN IS " @ %ret);
      $LastTilePick = "";
      alxStop($MoveSound);
      greenflag.visible = false;
      return;
   }
            
   $pathlength--;

   %dest = $GridPath[$pathlength];

   echo("onposition destination " @ %dest);

   // subtract fuel
   
   %gas = GetTotalFuelConsumption();
   
   // the player.road flag is for checking vehicle combat
   
   if(%road $= true)
   {
      %num = %gas * ".20";
      %num = mCeil(%num); 
      
      if(%num < 1)
         %num = 1;
         
      %gas = %gas-%num;
      
      player.road = true;
   }
   else
      player.road = "";
         
   if(%gas > $fuel)
   {
      UpdateStatsBar();
      alxStop($MoveSound);
      $LastTilePick = "";
      greenflag.visible = false;
      return;
   }
      
   DecreaseFuel(%gas);

   %otile = map.picktile(player.GetPosition());
   %ox = getWord(%otile, 0);
   %oy = getWord(%otile, 1);

   player.ox = %ox;
   player.oy = %oy;
   
   // figure out facing
   
   if(%dest !$= "")
   {   
      %ox = player.ox;
      %oy = player.oy;

      %dtile = map.picktile(%dest);   
      %dx = getWord(%dtile, 0);
      %dy = getWord(%dtile, 1);

      // north
      if(%ox == %dx && %oy > %dy)
      {
         player.setRotation(0);           
         player.facing = 1;
      }
         
      // north east
      if(%ox < %dx && %oy > %dy)
      {
         player.setRotation(45);           
         player.facing = 2;
      }

      // east
      if(%ox < %dx && %oy == %dy)
      {
         player.setRotation(90);           
         player.facing = 3;
      }
      
      // south east
      if(%ox < %dx && %oy < %dy)
      {
         player.setRotation(135);           
         player.facing = 4;
      }
      
      // south
      if(%ox == %dx && %oy < %dy)
      {
         player.setRotation(180);           
         player.facing = 5;
      }
      
      // south west
      if(%ox > %dx && %oy < %dy)
      {
         player.setRotation(225);           
         player.facing = 6;
      }
      
      // west
      if(%ox > %dx && %oy == %dy)
      {
         player.setRotation(270);           
         player.facing = 7;
      }
      
      // north west
      if(%ox > %dx && %oy > %dy)
      {
         player.setRotation(315);           
         player.facing = 8;
      }
   
   }

   UpdateStatsBar(%gas);

   if(%dest !$= "")      
      %this.moveto(%dest, 25, true, true, true);
   else
   {
      UpdateStatsBar();
      alxStop($MoveSound);
      greenflag.visible = false;
//      $wind = alxPlay(wind);
   }
}

function ChangePlayerFacing(%num)
{
   switch$(%num)
   {
      case 1:
         player.setRotation(0);    
         player.facing = 1;             
      case 2:
         player.setRotation(45);           
         player.facing = 2;                   
      case 3:
         player.setRotation(90);           
         player.facing = 3;                
      case 4:
         player.setRotation(135);           
         player.facing = 4;                   
      case 5:
         player.setRotation(180);           
         player.facing = 5;             
      case 6:      
         player.setRotation(225);           
         player.facing = 6;                   
      case 7:
         player.setRotation(270);           
         player.facing = 7;                
      case 8:
         player.setRotation(315);           
         player.facing = 8;             
   }
}
