function VehicleCollisionObjectTB(%id, %object)
{
   if(VehicleVerification(%id) $= false)
      return;
      
   if(%id.player $= 1)
      echo("PLAYER OBJECT COLLISION START" @ %id.vehiclename);
   else
      echo("ENEMY OBJECT COLLISION START" @ %id.vehiclename);
   
   %damage = %id.currentspeed / 7;
   
   %damage = mCeil(%damage);
      
   if(%id.player $= 1)
   {
      // if they have a ram
      
      if($vehicles[%id.slot, currentramhphp] > 0 && $vehicles[%id.slot, currentramhp] < $vehicles[%id.slot, stockramhp])
      {
         $vehicles[%id.slot, currentramhphp] = $vehicles[%id.slot, currentramhp]-%damage;                            

         if($vehicles[%id.slot, currentramhp] < 0)
            $vehicles[%id.slot, currentramhp] = 0;            
      }
      else
      // deal damage directly to vehicle
         CollisionDamage(%id, %damage);
   }
   else
   {
      if($Enemyvehicles[%id.slot, currentramhp] > 0 && $Enemyvehicles[%id.slot, currentramhp] < $Enemyvehicles[%id.slot, stockramhp])
      {
         $Enemyvehicles[%id.slot, currentramhp] = $Enemyvehicles[%id.slot, currentramhp]-%damage;                            

         if($Enemyvehicles[%id.slot, currentramhp] < 0)
            $Enemyvehicles[%id.slot, currentramhp] = 0;            
      }
      else
      // deal damage directly to vehicle
         CollisionDamage(%id, %damage);
   }
   
   StopCollisionSpeed(%id);   

   %rot = %id.GetRotation();
   
   %rot = %rot+90;

   %id.RotateTo(%rot, 50, true, true, true, "1");
   %id.SetRotationTarget(%rot, true, true, true, "1");  
   %id.navlock = true;    
   
   // this will prevent a vehicle from getting hung up on a wrecked vehicle, if it keeps getting hit just delete it
   
   if(%object.numhits == 5)
      %object.SafeDelete();
   
   if(%object.vehiclename !$= "")        
      %object.numhits++;
      
   echo("OBJECT COLLISION END");
}

function VehicleCollisionTB(%sender, %receiver)
{
   // the same vehicles seem to collide over and over again, this is to prevent multiple collisions like that
   
   if(%sender.collide $= %receiver)
   {
      echo("REDUNDANT COLLISION");
      return;      
   }
   
   if(%receiver.collide $= %sender)
   {
      echo("REDUNDANT COLLISION");
      return;      
   }
   
   %sender.collide = %receiver;
   %receiver.collide = %sender;
   
   schedule(5000, 0, "RemoveCollideBlock", %sender);
   schedule(5000, 0, "RemoveCollideBlock", %receiver);
      
   // if the player ordered the ram
   
   if(%sender.ramvehicle $= true && %sender.target $= %receiver)
   {
      %sender.ramvehicle = "";
      DisplayEnemyGang();      
      UpdateCustomOrders();
   }
   
   // make sure the trailer and cab won't collide at spawn
   
   if(%sender.trailer $= %receiver || %receiver.trailer $= %sender)
      return;
      
   if($vehicles[$CombatCamera, battleid] $= %sender || $vehicles[$CombatCamera, battleid] $= %receiver)   
      alxPlay(crash);
   
   if(%sender.player $= 1)
      echo("VEHICLE COLLISION START - SENDER PLAYER " @ %sender.vehiclename);
   else
      echo("VEHICLE COLLISION START - SENDER ENEMY " @ %sender.vehiclename);

   if(%receiver.player $= 1)
      echo("VEHICLE COLLISION START - RECEIVER PLAYER " @ %receiver.vehiclename);
   else
      echo("VEHICLE COLLISION START - RECEIVER ENEMY " @ %receiver.vehiclename);
   
   // if the receiver is actually a trailer
   
   if(%receiver.cab !$= "")
      %receiver = %receiver.cab;

   // find out which way the enemy is facing
   
   %receiverfacing = GetVehicleFacing(%receiver);
   
   %senderfacing = GetVehicleFacing(%sender);
         
   echo("RECEIVER FACING " @ %receiverfacing);
   echo("SENDER FACING " @ %senderfacing);
   
   // figure out the damage based on the vehicle facing
   
   StopCollisionSpeed(%sender);
   
   switch$ (%senderfacing)
   {     
      case "north":
         if(%receiverfacing $= "north")
            BackCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            HeadOnCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            BackQuarterCollision(%sender, %receiver);
            
      case "northeast":
         if(%receiverfacing $= "north")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            BackCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            HeadOnCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            SideCollision(%sender, %receiver);

      case "east":
         if(%receiverfacing $= "north")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            BackCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            HeadOnCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            FrontQuarterCollision(%sender, %receiver);

      case "southeast":
         if(%receiverfacing $= "north")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            BackCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            HeadOnCollision(%sender, %receiver);

      case "south":
         if(%receiverfacing $= "north")
            HeadOnCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            BackCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            FrontQuarterCollision(%sender, %receiver);

      case "southwest":
         if(%receiverfacing $= "north")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            HeadOnCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            BackCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            SideCollision(%sender, %receiver);

      case "west":
         if(%receiverfacing $= "north")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            HeadOnCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            BackCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            BackQuarterCollision(%sender, %receiver);

      case "northwest":
         if(%receiverfacing $= "north")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northeast")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "east")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southeast")
            HeadOnCollision(%sender, %receiver);
         if(%receiverfacing $= "south")
            FrontQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "southwest")
            SideCollision(%sender, %receiver);
         if(%receiverfacing $= "west")
            BackQuarterCollision(%sender, %receiver);
         if(%receiverfacing $= "northwest")
            BackCollision(%sender, %receiver);      
   }
   
   echo("END VEHICLE COLLISION");
}

function HeadOnCollisionTB(%sender, %receiver)
{
   echo("HEAD ON COLLISION");

   // find out if the attacker was going faster
   
   if(%sender.currentspeed >%receiver.currentspeed)
      %speed = %sender.currentspeed-%receiver.currentspeed;
   else 
   {
      %speed = %receiver.currentspeed-%sender.currentspeed;
      %mod = 10;      
   }
   
   // if the receiver was going faster then damage is reduced 
        
   %damage = GetVehicleCollisionDamage(%sender, %receiver, %speed);
   %damage = %damage-%mod;
   
   if(%damage < 0)
      %damage = 3;
   
   // since this is a head on collision if both vehicles have a ram the ram will take the damage
   
   if(%receiver.player $= 1)
   {
      // if they have a ram
      
      if($vehicles[%receiver.slot, currentramhphp] > 0)
         DamageRam(%receiver);
      else
      // deal damage directly to vehicle
         CollisionDamage(%receiver, %damage);
   }
   else
   {
      if($Enemyvehicles[%receiver.slot, currentramhp] > 0)
         DamageRam(%receiver);      
      else
      // deal damage directly to vehicle
         CollisionDamage(%receiver, %damage);
   }
         
   // cause damage to ramming vehicle which will only be a small portion of damage dealt //   
   // if they have a ram, the ram will take most of the damage, if not the vehicle will take all the damage
   
   if(%sender.player $= 1)
   {
      // if they have a ram
      
      if($vehicles[%sender.slot, currentramhp] > 0)
         DamageRam(%sender);
      else
      // deal damage directly to vehicle
         CollisionDamage(%sender, %damage);
   }
   else
   {
      if($Enemyvehicles[%sender.slot, currentramhp] > 0)
         DamageRam(%sender);
      else
      // deal damage directly to vehicle
         CollisionDamage(%sender, %damage);
   }
   
   if(VehicleVerification(%receiver) $= true)
   {
      StopCollisionSpeed(%receiver);   
         
      %rot = %receiver.GetRotation();
      %rot = %rot-90;
      
      if(%rot < 0)
      {
         %diff = 90-%receiver.GetRotation();      
         %rot = 360-%diff;
         %receiver.RotateTo(%rot, 50, true, true, true, "1");
      }
      else
         %receiver.RotateTo(%rot, 50, true, true, true, "1");   

      %receiver.SetRotationTarget(%rot, true, true, true, "1");   
      %receiver.navlock = true;
   }
   
   echo("END HEAD ON COLLISION");
}

function SideCollisionTB(%sender, %receiver)
{
   echo("SIDE COLLISION");
   
   DealCollisionDamage(%sender, %receiver);   
      
   echo("END SIDE COLLISION");   
   // receiver won't have to rotate
}

function FrontQuarterCollisionTB(%sender, %receiver)
{
   echo("FRONT QUARTER COLLISION");

   DealCollisionDamage(%sender, %receiver);
   
   if(VehicleVerification(%receiver) $= true)
   {
      %rot = %receiver.GetRotation();
      %rot = %rot-90;
      
      if(%rot < 0)
      {
         %diff = 90-%receiver.GetRotation();      
         %rot = 360-%diff;
         %receiver.RotateTo(%rot, 50, true, true, true, "1");
      }
      else
         %receiver.RotateTo(%rot, 50, true, true, true, "1");   

      %receiver.SetRotationTarget(%rot, true, true, true, "1");   
      %receiver.navlock = true;
   }
   
   echo("END FRONT QUARTER COLLISION");
}

function BackQuarterCollisionTB(%sender, %receiver)
{
   echo("BACK QUARTER COLLISION");

   DealCollisionDamage(%sender, %receiver);      
   
   if(VehicleVerification(%receiver) $= true)
   {   
      %rot = %receiver.GetRotation();
      %rot = %rot-90;
      
      if(%rot < 0)
      {
         %diff = 90-%receiver.GetRotation();      
         %rot = 360-%diff;
         %receiver.RotateTo(%rot, 50, true, true, true, "1");
      }
      else
         %receiver.RotateTo(%rot, 50, true, true, true, "1");   

      %receiver.SetRotationTarget(%rot, true, true, true, "1");   
      %receiver.navlock = true;
   }
   
   echo("END BACK QUARTER COLLISION");
}

function BackCollisionTB(%sender, %receiver)
{
   echo("BACK COLLISION");

   DealCollisionDamage(%sender, %receiver);
   
   if(VehicleVerification(%receiver) $= true)
   {   
      %rot = %receiver.GetRotation();
      %rot = %rot-90;
      
      if(%rot < 0)
      {
         %diff = 90-%receiver.GetRotation();      
         %rot = 360-%diff;
         %receiver.RotateTo(%rot, 50, true, true, true, "1");
      }
      else
         %receiver.RotateTo(%rot, 50, true, true, true, "1");   

      %receiver.SetRotationTarget(%rot, true, true, true, "1");   
      %receiver.navlock = true;
   }
   
   echo("END BACK COLLISION");
}

function DealCollisionDamageTB(%sender, %receiver)
{   
   echo("START DEAL COLLISION DAMAGE");
   
   // find out if the attacker was going faster
   
   if(%sender.currentspeed >= %receiver.currentspeed)
      %speed = %sender.currentspeed-%receiver.currentspeed;
   else 
      %speed = %receiver.currentspeed-%sender.currentspeed;
   
   // if the receiver was going faster then damage is reduced 
        
   %damage = GetVehicleCollisionDamage(%sender, %receiver, %speed);
   
   if(%damage < 0)
      %damage = 3;
      
   // get ram damage if any
   
   %damage = %damage+GetRamDamage(%sender);
      
   // cause damage to receving vehicle
   
   echo("RECEIVER COLLISION DAMAGE " @ %damage);
      
   CollisionDamage(%receiver, %damage);
   
   // cause damage to ramming vehicle which will only be a small portion of damage dealt //   
   // if they have a ram, the ram will take most of the damage, if not the vehicle will take all the damage
   
   %damage = %damage*".20";
   %damage = mCeil(%damage);
   
   echo("SENDER COLLISION DAMAGE " @ %damage);
   
   if(%sender.player $= 1)
   {
      // if they have a ram
      
      if($vehicles[%sender.slot, currentramhp] > 0 && $vehicles[%sender.slot, currentramhp] > 0)
      {
         $vehicles[%sender.slot, currentramhp] = $vehicles[%sender.slot, currentramhp]-%damage;                            

         if($vehicles[%sender.slot, currentramhp] < 0)
            $vehicles[%sender.slot, currentramhp] = 0;            
      }
      else
      // deal damage directly to vehicle
         CollisionDamage(%sender, %damage);
   }
   
   if(%sender.enemy $= 1)
   {
      if($Enemyvehicles[%sender.slot, currentramhp] > 0 && $Enemyvehicles[%sender.slot, currentramhp] > 0)
      {
         $Enemyvehicles[%sender.slot, currentramhp] = $Enemyvehicles[%sender.slot, currentramhp]-%damage;                            

         if($Enemyvehicles[%sender.slot, currentramhp] < 0)
            $Enemyvehicles[%sender.slot, currentramhp] = 0;            
      }
      else
      // deal damage directly to vehicle
         CollisionDamage(%sender, %damage);
   }
   
   echo("END DEAL COLLISION DAMAGE");
}

// when a vehicle rams another vehicle drop it's speed down after the hit

function SlowCollisionSpeedTB(%id)
{
   echo("SLOW COLLISION SPEED");
   
   if(%id.player $= 1)
   {   
      if($vehicles[%id.slot, battleid] !$= "")
      {
         %id.currentspeed = %id.currentspeed-40;
         
         if(%id.currentspeed < 0)
            %id.currentspeed = 10;
            
         $vehicles[%id.slot, battleid].setForwardSpeed(GetSpeedTranslation(%id.currentspeed));         
      }
   }
   else
   {
      if($Enemyvehicles[%id.slot, battleid] !$= "")
      {
         %id.currentspeed = %id.currentspeed-40;

         if(%id.currentspeed < 0)
            %id.currentspeed = 10;

         $Enemyvehicles[%id.slot, battleid].setForwardSpeed(GetSpeedTranslation(%id.currentspeed));         
      }
   }
   
   echo("END SLOW COLLISION SPEED");
}

// used in all the collision functions up above

function CollisionDamageTB(%id, %damage)
{
    echo("START Collision Damage " @ %damage);
   
   if(%id.player $= 1)
   {
       echo(%id.vehiclename SPC "VEHICLE HIT POINTS BEFORE COLLISION - " @ $vehicles[%id.slot, currentvehiclehp]);
      
       $vehicles[%id.slot, currentvehiclehp] = $vehicles[%id.slot, currentvehiclehp]-%damage;
         
       UpdateHealthBar(%id);

       if($CombatCamera $= %id.slot)
          UpdateBattleWindow(%id.slot);
       
      if($vehicles[%id.slot, currentvehiclehp] <= 0)
      {
         echo(%id.vehiclename SPC "PLAYER VEHICLE DESTROYED BY COLLISION");
         DestroyPlayerVehicle(%id);
      }
   }
   
   if(%id.enemy $= 1)
   {
       echo(%id.vehiclename SPC "VEHICLE HIT POINTS BEFORE COLLISION - " @ $Enemyvehicles[%id.slot, currentvehiclehp] SPC "/" SPC $Enemyvehicles[%id.slot, currentvehiclehp]);

       $Enemyvehicles[%id.slot, currentvehiclehp] = $Enemyvehicles[%id.slot, currentvehiclehp]-%damage;

       UpdateHealthBar(%id);
      
      if($Enemyvehicles[%id.slot, currentvehiclehp] <= 0)
      {
         echo(%id.vehiclename SPC "ENEMY VEHICLE DESTROYED BY COLLISION");
         DestroyEnemyVehicle(%id);
      }
   }
   
   echo("END COLLISION DAMAGE");
}

// stop a vehicle after a collision

function StopCollisionSpeedTB(%id)
{
   echo("STOP COLLISION SPEED");
   
   if(isObject(%id) $= false)
      return;
      
   // lower the speed of the attacking vehicle if it's still in the game
   
   if(%id.player $= 1)
   {   
      if($vehicles[%id.slot, battleid] !$= "")
      {
         %id.currentspeed = 0;
         %id.SetAtRest();
      }
   }
   else
   {
      if($Enemyvehicles[%id.slot, battleid] !$= "")
      {
         %id.currentspeed = 0;
         %id.SetAtRest();
      }
   }
   
   echo("END STOP COLLISION SPEED");
}

function RemoveCollideBlockTB(%id)
{
   %id.collide = "";   
}