////////////////////////////////////// Ram Damage //////////////////////////////////////////////////////////

function GetRamDamage(%id)
{
   switch$ (%id.vehiclename)
   {
      case "minivan":
         %damage = 2;           
      case "towtruck":
         %damage = 3;           
      case "van":
         %damage = 3;           
      case "limo":
         %damage = 4;           
      case "sedan":
         %damage = 3;           
      case "sportscar":
         %damage = 2;           
      case "pickuptruck":
         %damage = 3;           
      case "musclecar":
         %damage = 3;           
      case "stationwagon":
         %damage = 3;           
      case "humvee":
         %damage = 4;           
      case "suv":
         %damage = 4;           
      case "flatbedtruck":
         %damage = 5;           
      case "trailertruck":
         %damage = 5;           
      case "fireengine":
         %damage = 5;           
      case "garbagetruck":
         %damage = 3;           
      case "bus":
         %damage = 5;           
      case "boxtruck":
         %damage = 4;           
      case "cargovan":
         %damage = 3;           
      case "ambulance":
         %damage = 3;           
      case "construction":
         %damage = 5;                 
   }

   if(%id.player $= 1)
   {
      if($vehicles[%id.slot, currentramhp] > 0)
      {
         DamageRam(%id);
         return $vehicles[%id.slot, ram]*%damage;   
      }
   }
   else
   {
      if($Enemyvehicles[%id.slot, currentramhp] > 0)
      {
         DamageRam(%id);
         return $Enemyvehicles[%id.slot, ram]*%damage;   
      }
   }
}

///////////////////////////////////// collision damage /////////////////////////////////////////////////////
// this is how much damage is caused per 10 speed difference

function GetVehicleCollisionDamage(%sendervehicle, %receivervehicle, %speed)
{
   if(%sendervehicle.player $= 1)
      %vehicle = $vehicles[%sendervehicle.slot, vehicle];
   else
      %vehicle = $Enemyvehicles[%sendervehicle.slot, vehicle];
   
   // speed will be 0 if both vehicles are traveling at the same speed
      
   if(%speed == 0)
      %speed = 10;
      
   switch$ (%vehicle)
   {
      case "dunebuggy":
         %damage = 2*(%speed / 10);           
      case "bike":
         %damage = 2*(%speed / 10);           
      case "jeep":
         %damage = 2*(%speed / 10);           
      case "compact":
         %damage = 3*(%speed / 10);           
      case "minivan":
         %damage = 3*(%speed / 10);           
      case "towtruck":
         %damage = 4*(%speed / 10);           
      case "van":
         %damage = 4*(%speed / 10);           
      case "limo":
         %damage = 4*(%speed / 10);           
      case "sedan":
         %damage = 4*(%speed / 10);           
      case "sportscar":
         %damage = 3*(%speed / 10);           
      case "pickuptruck":
         %damage = 4*(%speed / 10);           
      case "musclecar":
         %damage = 4*(%speed / 10);           
      case "stationwagon":
         %damage = 4*(%speed / 10);           
      case "humvee":
         %damage = 5*(%speed / 10);           
      case "suv":
         %damage = 5*(%speed / 10);           
      case "flatbedtruck":
         %damage = 6*(%speed / 10);           
      case "trailertruck":
         %damage = 6*(%speed / 10);           
      case "fireengine":
         %damage = 6*(%speed / 10);           
      case "garbagetruck":
         %damage = 5*(%speed / 10);           
      case "bus":
         %damage = 6*(%speed / 10);           
      case "boxtruck":
         %damage = 6*(%speed / 10);           
      case "cargovan":
         %damage = 5*(%speed / 10);           
      case "ambulance":
         %damage = 5*(%speed / 10);           
      case "construction":
         %damage = 6*(%speed / 10);                 
   }
   
   %senderweight = GetVehicleCollisionWeight(%sendervehicle.vehiclename);
   %receiverweight = GetVehicleCollisionWeight(%receivervehicle.vehiclename);
      
   if(%senderweight < %receiverweight)
      %damage = %damage / 2;
   
   %damage = mCeil(%damage);
   
   echo("COLLISION DAMAGE " @ %damage);
   
   if(%damage == 0)
      echo("problem");
      
   return %damage;
}

function DamageRam(%id)
{
   if(%id.player $= 1)
   {
      $vehicles[%id.slot, currentramhp] = $vehicles[%id.slot, currentramhp]-3;

      if($vehicles[%id.slot, currentramhp] < 0)
      {
         $vehicles[%id.slot, currentramhp] = 0;      
         
         if(isObject(%id.ram) $= true)
            %id.ram.Safedelete();         
      }
   }
   else
   {
      $Enemyvehicles[%id.slot, currentramhp] = $Enemyvehicles[%id.slot, currentramhp]-3;

      if($Enemyvehicles[%id.slot, currentramhp] < 0)
      {
         $Enemyvehicles[%id.slot, currentramhp] = 0;      

         if(isObject(%id.ram) $= true)
            %id.ram.Safedelete();         
      }
   }   
}

function CheckForSpin(%id)
{
   if(isObject(%id) $= false)
      return;
      
   echo("CHECKING FOR SPIN " @ %id.vehiclename SPC %id.player);
   
   %rand = getRandom(1, 20);
   %num = 13;
   
   if(%id.player $= 1)
      %bonus = $vehicles[%id.slot, tireupgrade];
   else 
      %bonus = $Enemyvehicles[%id.slot, tireupgrade];
   
   %num = %num+%bonus;
   
   if(%rand >= %num)
   {
      echo("HIT A SPIN");
      
      %rand = getRandom(0, 360);
      
      %id.RotateTo(%rand, 120);   

      if(%id.navlock $= true)
         %id.navlock = "";
   }
}

// the actual number doesn't matter, just the order of the number
// if a smaller vehicle rams a larger vehicle the damage is halfed

function GetVehicleCollisionWeight(%vehicle)
{
   switch$ (%vehicle)
   {
      case "bike":
         return 1;
      case "dunebuggy":
         return 1;
      case "minivan":
         return 2;           
      case "towtruck":
         return 5;           
      case "van":
         return 3;           
      case "limo":
         return 6;           
      case "sedan":
         return 3;           
      case "sportscar":
         return 2;           
      case "pickuptruck":
         return 3;           
      case "musclecar":
         return 3;           
      case "stationwagon":
         return 3;           
      case "humvee":
         return 4;           
      case "suv":
         return 4;           
      case "flatbedtruck":
         return 10;           
      case "trailertruck":
         return 10;           
      case "fireengine":
         return 9;           
      case "garbagetruck":
         return 6;           
      case "bus":
         return 8;           
      case "boxtruck":
         return 7;           
      case "cargovan":
         return 6;           
      case "ambulance":
         return 6;           
      case "construction":
         return 7;                 
   }
}

function PlayerFrontBumper::OnEnter(%this, %obj)
{
   if(%obj.enemy $= 1 || %obj.player $= 1 || %obj.cab !$= "")
   {
      if(%this.vehicle !$= %obj)      
      {
          VehicleCollision(%this.vehicle, %obj);
          return; // so a spin won't occur just in case
      }
   }
   
   if(%obj.block $= 1)
      VehicleCollisionObject(%this.vehicle, %obj);   
   
   if(%obj.rail $= 1)
   {
      %obj.SafeDelete();

      if(%this.vehicle $= $vehicles[$CombatCamera, battleid])
         alxPlay(crash);
   }

   if(%obj.barrel $= 1)
   {
      %obj.SafeDelete();

      if(%this.vehicle $= $vehicles[$CombatCamera, battleid])
         alxPlay(crash);
   }
   
   if(%obj.construction $= 1)
   {
      %obj.SafeDelete();

      if(%this.vehicle $= $vehicles[$CombatCamera, battleid])
         alxPlay(crash);
   }
}

function EnemyFrontBumper::OnEnter(%this, %obj)
{
   if(%obj.player $= 1 || %obj.enemy $= 1 || %obj.cab !$= "")
   {
      if(%this.vehicle !$= %obj)      
      {
         VehicleCollision(%this.vehicle, %obj);
         return;  // avoid a spin happening just in case         
      }
   }
   
   if(%obj.block $= 1)
      VehicleCollisionObject(%this.vehicle, %obj);   

   if(%obj.rail $= 1)
   {
      %obj.SafeDelete();

      if(%this.vehicle $= $vehicles[$CombatCamera, battleid])
         alxPlay(crash);
   }

   if(%obj.barrel $= 1)
   {
      %obj.SafeDelete();
      
      if(%this.vehicle $= $vehicles[$CombatCamera, battleid])
         alxPlay(crash);
   }

   if(%obj.construction $= 1)
   {
      %obj.SafeDelete();   

      if(%this.vehicle $= $vehicles[$CombatCamera, battleid])
         alxPlay(crash);
   }
}
