////////////////////////////////////////////////////////////////////////////////////////
// nav sensors
////////////////////////////////////////////////////////////////////////////////////////

// enter sensors

function Enemymiddlesensor::OnEnter(%this, %obj)
{
   // when spawning the trailer will touch of the sensors
   
   if(%this.vehicle.trailer $= %obj)
      return;

   // for spawning
      
   if(%this.vehicle $= %obj)
      return;

   if(%this.navlock $= true)
      return;
      
   if(%obj.enemy $= 1 || %obj.player $= 1 || %obj.block $= 1 || %obj.border $= 1 || %obj.trailer !$= "")
   {
      if(%this.vehicle !$= %obj)
      {
         if(%obj.enemy $= 1 || %obj.player $= 1 || %obj.trailer !$= "")
         {
            %facing = GetVehicleFacing(%this.vehicle);
            %incfacing = GetVehicleFacing(%obj);            
            %rand = getRandom(1, 2);
            
            if(%obj.trailer !$= "")
            {
               if(%obj.cab.player $= 1)
                  %team = "player";
               else
                  %team = "enemy";
            }
            
            if(%obj.player $= 1)
               %team = "player";
            else
               %team = "enemy";

            // if this is a ram target then don't do anything
            
            if(%this.vehicle.ramvehicle $= true && %this.vehicle.target $= %obj)
               return;
               
            // the logic is if a vehicle is heading straight on and it's an enemy there is a 50% chance
            // this vehicle will turn. Any player vehicles will invoke an automatic turn
                        
            switch$ (%facing)
            {
               case "north":
                  if(%incfacing $= "south" && %team $= "player" && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);     
                  else
                     EnemyTurnMiddle(%this.vehicle);     
               case "northeast":
                  if(%incfacing $= "southwest" && %team $= "player" && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);          
                  else
                     EnemyTurnMiddle(%this.vehicle);     
               case "east":
                  if(%incfacing $= "west" && %team $= "player"  && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);          
                  else
                     EnemyTurnMiddle(%this.vehicle);     
               case "southeast":
                  if(%incfacing $= "northwest" && %team $= "player" && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);          
                  else
                     EnemyTurnMiddle(%this.vehicle);     
               case "south":
                  if(%incfacing $= "north" && %team $= "player" && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);          
                  else
                     EnemyTurnMiddle(%this.vehicle);     
               case "southwest":
                  if(%incfacing $= "northeast" && %team $= "player" && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);          
                  else
                     EnemyTurnMiddle(%this.vehicle);     
               case "west":
                  if(%incfacing $= "east" && %team $= "player" && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);          
                  else
                     EnemyTurnMiddle(%this.vehicle);     
               case "northwest":
                  if(%incfacing $= "southeast" && %team $= "player" && %rand == 1)
                     EnemyTurnMiddle(%this.vehicle);          
                  else
                     EnemyTurnMiddle(%this.vehicle);     
            }                  
         }

         if(%obj.block $= 1)
            EnemyTurnMiddle(%this.vehicle);     
         
         if(%obj.border $= 1)
         {
            %turnspeed = GetEnemyTurnSpeed(%this.vehicle.slot);            

            %angle = t2dAngleToPoint(%this.vehicle.GetPosition(), "0 0");

            %this.vehicle.SetRotation(%angle);   
         }
      }
   }
}

function EnemyRightForwardSensor::OnEnter(%this, %obj)
{
   // when spawning the trailer will touch of the sensors
   
   if(%this.vehicle.trailer $= %obj)
      return;

   // for spawning
      
   if(%this.vehicle $= %obj)
      return;

   if(%this.navlock $= true)
      return;

   if(%obj.enemy $= 1 || %obj.player $= 1 || %obj.trailer !$= "" && %cthis.vehicle !$= %obj)
   {      
//      echo("sensor right");

      if(%this.vehicle.ramvehicle $= true && %this.vehicle.target $= %obj)
         return;
      
      // check if incoming vehicle is on a collision course
      
      %facing = GetVehicleFacing(%this.vehicle);
      %incfacing = GetVehicleFacing(%obj);
      
      switch$ (%facing)
      {
         case "north":
            if(%incfacing $= "west" || %incfacing $= "northwest")
               EnemyTurnLeft(%this.vehicle);          
         case "northeast":
            if(%incfacing $= "north" || %incfacing $= "northwest")
               EnemyTurnLeft(%this.vehicle);          
         case "east":
            if(%incfacing $= "north" || %incfacing $= "northeast")
               EnemyTurnLeft(%this.vehicle);          
         case "southeast":
            if(%incfacing $= "east" || %incfacing $= "northeast")
               EnemyTurnLeft(%this.vehicle);          
         case "south":
            if(%incfacing $= "east" || %incfacing $= "southeast")
               EnemyTurnLeft(%this.vehicle);          
         case "southwest":
            if(%incfacing $= "south" || %incfacing $= "southeast")
               EnemyTurnLeft(%this.vehicle);          
         case "west":
            if(%incfacing $= "south" || %incfacing $= "southwest")
               EnemyTurnLeft(%this.vehicle);          
         case "northwest":
            if(%incfacing $= "west" || %incfacing $= "southwest")
               EnemyTurnLeft(%this.vehicle);          
      }      
   }
   
   if(%obj.block $= 1)
      %this.vehicle.rightbuilding = 1;
}

function EnemyLeftForwardSensor::OnEnter(%this, %obj)
{
   // when spawning the trailer will touch of the sensors
   
   if(%this.vehicle.trailer $= %obj)
      return;

   // for spawning
      
   if(%this.vehicle $= %obj)
      return;

   if(%this.navlock $= true)
      return;

   if(%obj.enemy $= 1 || %obj.player $= 1 || %obj.trailer !$= "" && %this.vehicle !$= %obj)
   {
      if(%this.vehicle.ramvehicle $= true && %this.vehicle.target $= %obj)
         return;

      %facing = GetVehicleFacing(%this.vehicle);
      %incfacing = GetVehicleFacing(%obj);
      
//      echo("sensor left");
            
      switch$ (%facing)
      {
         case "north":
            if(%incfacing $= "east" || %incfacing $= "northeast")
               EnemyTurnRight(%this.vehicle);          
         case "northeast":
            if(%incfacing $= "east" || %incfacing $= "southwest")
               EnemyTurnRight(%this.vehicle);          
         case "east":
            if(%incfacing $= "south" || %incfacing $= "southeast")
               EnemyTurnRight(%this.vehicle);          
         case "southeast":
            if(%incfacing $= "west" || %incfacing $= "southwest")
               EnemyTurnRight(%this.vehicle);          
         case "south":
            if(%incfacing $= "west" || %incfacing $= "southwest")
               EnemyTurnRight(%this.vehicle);          
         case "southwest":
            if(%incfacing $= "west" || %incfacing $= "northwest")
               EnemyTurnRight(%this.vehicle);          
         case "west":
            if(%incfacing $= "north" || %incfacing $= "northwest")
               EnemyTurnRight(%this.vehicle);          
         case "northwest":
            if(%incfacing $= "north" || %incfacing $= "northeast")
               EnemyTurnRight(%this.vehicle);          
      }      
   }
   
   if(%obj.block $= 1)
      %this.vehicle.leftbuilding = 1;
}

///////////////////////////////////////////////////////////////////////////////////
// leave sensors //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

function EnemyRightForwardSensor::OnLeave(%this, %obj)
{
   if(%obj.enemy $= 1 || %obj.player $= 1)
      RemoveSensorTarget(%this, %obj, "rightforward");

   if(%obj.block $= 1)
      %this.vehicle.rightbuilding = "";
}

function EnemyLeftForwardSensor::OnLeave(%this, %obj)
{
   if(%obj.enemy $= 1 || %obj.player $= 1)
      RemoveSensorTarget(%this, %obj, "leftforward");

   if(%obj.block $= 1)
      %this.vehicle.leftbuilding = "";
}

function RemoveSensorTarget(%this, %obj, %sensor)
{
      switch$ (%sensor)
      {
//         case "middle":
//            %list = %this.vehicle.middlesensor;
//            %this.vehicle.coursecorrection = "";
         case "rightforward":
            %list = trim(%this.vehicle.rightforwardsensor);
         case "leftforward":      
            %list = trim(%this.vehicle.leftforwardsensor);
         case "left":      
            %list = trim(%this.vehicle.leftsensor);
         case "right":      
            %list = trim(%this.vehicle.rightsensor);
      }
      
      %count = getWordCount(%list);
      
      for(%i=0;%i<%count;%i++)
      {
         %word = getWord(%list, %i);
         
         if(%word !$= %obj)
         {
            if(%newlist $= "")
               %newlist = %word;         
            else
               %newlist = %newlist SPC %word;         
         }
      }

      switch$ (%sensor)
      {
         case "middle":
            %this.vehicle.middlesensor = %newlist;
         case "rightforward":
            %this.vehicle.rightforwardsensor = %newlist;
         case "leftforward":      
            %this.vehicle.leftforwardsensor = %newlist;
         case "left":      
            %this.vehicle.leftsensor = %newlist;
         case "right":      
            %this.vehicle.rightsensor = %newlist;
      }
   
}

/*
// course correction ///////////////////////////////////////////////////////

Just some notes - If it's the mid sensor see if it's a vehicle (player or enemy) or a block. If it's a block 
we know the vehicle has to move. If it's a vehicle and isn't heading towards the vehicle then ignore it

In theory the right and left forward sensors should only need to be used to see which direction the vehicle can turn

*/

function EnemyTurnMiddle(%id)
{   
   // we know there is a target in front of the vehicle, figure out if we have right or left clear
   
   %rand = getRandom(1, 2);
   
   switch$ (%rand)
   {
      case 1:
         if(trim(%id.vehicle.leftforwardsensor) $= "")
         {
            %clear = true;
            EnemyTurnLeft(%id);
            return;                  
         }
         if(trim(%id.vehicle.rightforwardsensor) $= "")
         {
            %clear = true;
            EnemyTurnRight(%id);                  
         }
         
         // this will handle if the vehicle isn't clear to the right or left
         
         if(%clear $= "")
         {
            echo("NO WHERE TO TURN");                  
            
         }
      
      case 2:
         if(trim(%id.vehicle.rightforwardsensor) $= "")
         {
            %clear = true;
            EnemyTurnRight(%id);                  
            return;
         }
         if(trim(%id.vehicle.leftforwardsensor) $= "")
         {
            %clear = true;
            EnemyTurnLeft(%id);                  
         }

         // this will handle if the vehicle isn't clear to the right or left
         
         if(%clear $= "")
         {
            echo("NO WHERE TO TURN");                  
            
            
         }
   }
}

// turn 45 degrees to the right

function EnemyTurnRight(%id)
{
   %id.navlock = true;
   
   %rotation = %id.GetRotation();

//   echo("Rotate RIGHT " @ %rotation);
   
   %rotation = %rotation+45;
   %id.RotateTo(%rotation,GetEnemyTurnSpeed(%id.slot), true, true, true, "1");
   
   %id.SetRotationTarget(%rotation, true, true, true, "1");   
}

// turn 45 degrees to the left

function EnemyTurnLeft(%id)
{
   %id.navlock = true;
   
   %rotation = %id.GetRotation();
   
   %rotation = %rotation-45;   
   %id.RotateTo(%rotation, GetEnemyTurnSpeed(%id.slot), true, true, true, "1");
   
   %id.SetRotationTarget(%rotation, true, true, true, "1");   
}

function Enemyclass::onRotationTarget(%this)
{
//   echo("ON ROTATION TARGET");   

   %this.SetRotationTargetOff();
   %this.navlock = "";
}
