// this will handle all the special weapons for both player and enemy

// mount the weapon, vehicles will have 1 mount point in the middle so the weapons will be adjusted
// this will be the offset from the middle and then mount it on the vehicle

function MountSpecialWeaponTB(%vehiclename, %vehicleid, %weapon)
{
   %mountweapon =    new t2dStaticSprite() {
                     canSaveDynamicFields = "1";
                     Position = "0 0";
                     size = "4 4";
                     MountTrackRotation = "0";
                     scenegraph = "sg";
                     layer = 1;
                  };
   
   switch$ (%weapon)
   {
      case "crossbow":      
         %mountweapon.SetImageMap("crossbow");
      case "flamethrower":
         %mountweapon.SetImageMap("flamethrower");
      case "machinegun":
         %mountweapon.SetImageMap("machinegun");
      case "rpg":      
         %mountweapon.SetImageMap("rpg");      
   }
   
   %mountweapon.Mount(%vehicleid, "0 0", 0, false, true, true, true);   
   
   %vehicleid.special = %mountweapon;
}

function GetSpecialWeaponDamageTB(%weapon)
{
   switch$ (%weapon)
   {
      case "crossbow":      
         %damage = getRandom(1, 5);
      case "flamethrower":
         %damage = getRandom(4, 8);
      case "machinegun":
         %damage = getRandom(8, 12);
      case "rpg":      
         %damage = getRandom(10, 15);
   }
   
   return %damage;
}

// this will keep a vehicle special weapon (if it has one) rotated towards the vehicle target

function RotateSpecialWeaponTB(%id, %target)
{
   // if it doesn't have a special weapon then return
   
   if(%id.special $= "")   
      return;
   
   if(isObject(%id) $= false || isObject(%target) $= false)
      return;
               
   %angle = t2dAngleToPoint(%id.GetPosition(), %target.GetPosition());

   %id.special.RotateTo(%angle, 75);   
   
//   %id.specialrotateblock = true;

//   schedule($specialWeaponRotation, 0, "RemoveSpecialRotateBlock", %id);

}

// this will see if the special weapon is clear to fire and has the proper angle to fire
// the target will already have a clear line of sight we just need to check if it's within the angle of the weapon

function CheckSpecialWeaponTB(%id, %target)
{
   // if already fired within the time limit
   
   if(%id.specialclearfire !$= "")
      return;   

   %angle = t2dAngleToPoint(%id.GetPosition(), %target.GetPosition());
   
   %angle = mFloor(%angle);
   
   %rot = %id.special.GetRotation();
   
   %rot = mFloor(%rot);
   
   if(%rot >= 0 && %rot <= 20 && %angle >= 0 && %angle <= 20)
      return true;            
   if(%rot >= 21 && %rot <= 40 && %angle >= 21 && %angle <= 40)
      return true;            
   if(%rot >= 41 && %rot <= 60 && %angle >= 41 && %angle <= 60)
      return true;            
   if(%rot >= 61 && %rot <= 80 && %angle >= 61 && %angle <= 80)
      return true;            
   if(%rot >= 81 && %rot <= 100 && %angle >= 81 && %angle <= 100)
      return true;            
   if(%rot >= 101 && %rot <= 120 && %angle >= 101 && %angle <= 120)
      return true;            
   if(%rot >= 121 && %rot <= 140 && %angle >= 121 && %angle <= 140)
      return true;            
   if(%rot >= 141 && %rot <= 160 && %angle >= 141 && %angle <= 160)
      return true;            
   if(%rot >= 161 && %rot <= 180 && %angle >= 161 && %angle <= 180)
      return true;            


   if(%rot >= 0 && %rot <= -20 && %angle >= 0 && %angle <= -20)
      return true;            
   if(%rot >= -21 && %rot <= -40 && %angle >= -21 && %angle <= -40)
      return true;            
   if(%rot >= -41 && %rot <= -60 && %angle >= -41 && %angle <= -60)
      return true;            
   if(%rot >= -61 && %rot <= -80 && %angle >= -61 && %angle <= -80)
      return true;            
   if(%rot >= -81 && %rot <= -100 && %angle >= -81 && %angle <= -100)
      return true;            
   if(%rot >= -101 && %rot <= -120 && %angle >= -101 && %angle <= -120)
      return true;            
   if(%rot >= -121 && %rot <= -140 && %angle >= -121 && %angle <= -140)
      return true;            
   if(%rot >= -141 && %rot <= -160 && %angle >= -141 && %angle <= -160)
      return true;            
   if(%rot >= -161 && %rot <= -180 && %angle >= -161 && %angle <= -180)
      return true;            


/*         
   // figure out if the gun is facing    
   
   if(%rot >= 0 && %rot <= 45)
   {
      if(%angle >= 0 && %angle <= 45)
         return true;            
   }
   if(%rot >= 46 && %rot <= 90)
   {
      if(%angle >= 46 && %angle <= 90)
         return true;            
   }
   if(%rot >= 91 && %rot <= 135)
   {
      if(%angle >= 91 && %angle <= 135)
         return true;            
   }
   if(%rot >= 136 && %rot <= 180)
   {
      if(%angle >= 136 && %angle <= 180)
         return true;            
   }
   if(%rot >= 181 && %rot <= 225)
   {
      if(%angle >= -180 && %angle <= -136)
         return true;            
   }
   if(%rot >= 226 && %rot <= 270)
   {
      if(%angle >= -135 && %angle <= -91)
         return true;            
   }
   if(%rot >= 271 && %rot <= 315)
   {
      if(%angle >= -90 && %angle <= -46)
         return true;            
   }
   if(%rot >= 316 && %rot <= 360)
   {
      if(%angle >= -45 && %angle <= -1)
         return true;            
   }
   */
}

function CreatePlayerVehicleCrossbowTB(%id, %target)
{
   %sprite = new t2dStaticSprite()
   {
      scenegraph = "sg";  
      size = "1 3";
      imagemap = "bolt";
   };
   
   %trigger = new t2dTrigger() {
      size = ".5 .5";
      class = "PlayerVehicleCrossbow";
      CollisionActiveReceive = "1";
      scenegraph = "sg";
   };
   
   %sprite.SetPosition(%id.GetPosition());
   %trigger.mount(%sprite, "0 1", 0, true, true, true, true);   
   %trigger.target = %target;
   %trigger.bolt = %sprite;
   %trigger.vehicle = %id;
      
   %sprite.SetRotation(%id.special.GetRotation());   
   %sprite.SetForwardMovementOnly(true);
   %sprite.setForwardSpeed(110);         
   
   schedule(20000, 0, "DeleteSprite", %sprite);         
}

function PlayerVehicleCrossbow::OnEnter(%this, %obj)
{
   if(%obj.enemy $= 1)
   {
      PlayerSpecialWeaponRoll(%this.vehicle, %obj);
      %this.bolt.SafeDelete();
   }
}

function CreateEnemyVehicleCrossbowTB(%id, %target)
{
   %sprite = new t2dStaticSprite()
   {
      scenegraph = "sg";  
      size = "1 3";
      imagemap = "bolt";
   };
   
   %trigger = new t2dTrigger() {
      size = ".5 .5";
      class = "EnemyVehicleCrossbow";
      CollisionActiveReceive = "1";
      scenegraph = "sg";
   };
   
   %sprite.SetPosition(%id.GetPosition());
   %trigger.mount(%sprite, "0 1", 0, true, true, true, true);   
   %trigger.target = %target;
   %trigger.bolt = %sprite;
   %trigger.vehicle = %id;
         
   %sprite.SetRotation(%id.special.GetRotation());   
   %sprite.SetForwardMovementOnly(true);
   %sprite.setForwardSpeed(110);         
   
   schedule(20000, 0, "DeleteSprite", %sprite);         
}

function EnemyVehicleCrossbow::OnEnter(%this, %obj)
{
   if(%obj.player $= 1)
   {
      EnemySpecialWeaponRoll(%this.vehicle, %obj);
      %this.bolt.SafeDelete();
   }
}
