$VehicleSpawnRadius = 8.25;

//------------------------------------------------------------------------------
datablock AudioProfile(VehicleAppearSound)
{
   filename    = "fx/vehicles/inventory_pad_appear.wav";
   description = AudioClosest3d;
   preload = true;
};

datablock AudioProfile(ActivateVehiclePadSound)
{
   filename = 	"fx/powered/vehicle_pad_on.wav";
   description = AudioClose3d;
   preload = true;
};

datablock StationFXVehicleData( VehicleInvFX )
{
   lifetime = 6.0;

   glowTopHeight = 1.5;
   glowBottomHeight = 0.1;
   glowTopRadius = 12.5;
   glowBottomRadius = 12.0;
   numGlowSegments = 26;
   glowFadeTime = 3.25;
   
   armLightDelay = 2.3;
   armLightLifetime = 3.0;
   armLightFadeTime = 1.5;
   numArcSegments = 10.0;

   sphereColor = "0.1 0.1 0.5";
   spherePhiSegments = 13;
   sphereThetaSegments = 8;
   sphereRadius = 12.0;
   sphereScale = "1.05 1.05 0.85";
   
   glowNodeName = "GLOWFX";

   leftNodeName[0]   = "LFX1";
   leftNodeName[1]   = "LFX2";
   leftNodeName[2]   = "LFX3";
   leftNodeName[3]   = "LFX4";

   rightNodeName[0]  = "RFX1";
   rightNodeName[1]  = "RFX2";
   rightNodeName[2]  = "RFX3";
   rightNodeName[3]  = "RFX4";


   texture[0] = "special/stationGlow";
   texture[1] = "special/stationLight2";
};

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
function serverCmdBuyVehicle(%Client, %Data)
{
   %Team = %Client.getSensorGroup();
   if (vehicleCheck(%Data, %Team) == true)
   {
      %Station = %Client.player.station;
      %Pad = %Station.pad;
      if (%Pad.ready == true && (%Pad.station.vehicle[%Data]))
      {
         %Rotation = rotFromTransform(%client.player.getTransform());

         %Transform = %Pad.getTransform();
         %Position = posFromTransform(%Transform);
         %PositionXY = getWords(%Position, 0, 1);
         %PositionZ = getWord(%Position, 2);
         %Position2 =  %PositionXY @ " " @ %PositionZ + 8;

         %Position3 = posFromTransform(%Station.getTransform());
         %VectorBetween = VectorSub(%Position, %Position3);
         %NormalBetween = VectorNormalize(getWords(%VectorBetween, 0, 1) @ " 0");

         %Position = VectorAdd(%Position2, VectorScale(%NormalBetween, 5));

         %Mask = $TypeMasks::VehicleObjectType | $TypeMasks::MoveableObjectType |
                 $TypeMasks::StaticShapeObjectType | $TypeMasks::StaticTSObjectType | 
                 $TypeMasks::StationObjectType | $TypeMasks::ItemObjectType | 
                 $TypeMasks::PlayerObjectType | $TypeMasks::TurretObjectType;

         InitContainerRadiusSearch(%Position, $VehicleSpawnRadius, %Mask);
   
         %Clear = true;
         %Vehicle = "";
         %VehicleCount = 0;

         while ((%Object = containerSearchNext()) != 0)
         {
            if (%Object.getType() & $TypeMasks::VehicleObjectType)
            {
               %VehicleCount++;
               %Vehicle = %Object;

               if (%Object.getDataBlock().checkIfPlayersMounted(%Object) == true)
                  %VehicleCount++;
            } else
               %Clear = false;
         }

         if (%VehicleCount > 1)
         {
            MessageClient(%Client, "", 'Can\'t create vehicle; obstruction on the vehicle pad.'); 
            return;
         }

         %ExtraTime = 0;
         if (%VehicleCount > 0)
         {
            %Vehicle.mountable = 0;
            %Vehicle.startFade(1000, 0, true);
            %Vehicle.schedule(1050, "delete");
            %ExtraTime = 1500;
         }

         // If something other than 2 or more vehicles are on the pad, blow it all up
         if(%Clear == false)
         {
            schedule(%ExtraTime, 0, RadiusExplosion, 0, %Position, $VehicleSpawnRadius, 200, 3500, %Client.Player, $DamageType::VehicleSpawn);
         }

         schedule(%ExtraTime + 750, 0, "createVehicle", %Client, %Pad, %Data, %Team, %Position, %Rotation);
      }
   }
}

function createVehicle(%Client, %Pad, %Data, %Team, %Position, %Rotation)
{
   %Vehicle = %Data.create(%Team);   
   if (%Vehicle > 0)
   {
      %Vehicle.getDataBlock().setInvincible(%Vehicle, true);
      %Pad.ready = false;
      %Vehicle.team = %Team;
      %Vehicle.useCreateHeight(true);
      %Vehicle.schedule(5500, "useCreateHeight", false);
      %Vehicle.getDataBlock().isMountable(%Vehicle, false);
      %Vehicle.getDataBlock().schedule(6500, "isMountable", %Vehicle, true);
      
      %Pad.playThread($ActivateThread,"activate2");
      %Pad.playAudio($ActivateSound, ActivateVehiclePadSound);

      vehicleListAdd(%Data, %Vehicle);
      MissionCleanup.add(%Vehicle);
                                 
      %turret = %Vehicle.getMountNodeObject(10);
      if(%turret > 0)
      {
         %turret.setCloaked(true);
         %turret.schedule(4800, "setCloaked", false);
      }
      
      %Vehicle.setCloaked(true);
      %Vehicle.setTransform(%Position @ " " @ %Rotation);      
   
      %Vehicle.schedule(3700, "playAudio", 0, VehicleAppearSound);
      %Vehicle.schedule(4800, "setCloaked", false);
      %Vehicle.getDataBlock().schedule(4500, "setInvincible", %Vehicle, false);

      %Player = %Client.Player;      
      %LastVehicle = %Player.lastVehicle;
      
      if (%LastVehicle !$= "")
      {
         if (isObject(%LastVehicle) == true)
         {
            %LastVehicle.startFade(1000, 0, true);
            %LastVehicle.schedule(1050, "delete");
         }         
      }   
      %Player.lastVehicle = %Vehicle;
      %Vehicle.lastPilot = %Player;

      // play the FX
      %fx = new StationFXVehicle()
      {
         dataBlock = VehicleInvFX;
         stationObject = %Pad;
      };

      StartVehicleSpawnCheck(%Position, %Vehicle, %Player, 5);

      if ($Host::FadePilotIntoPlane == true)
         %Vehicle.getDataBlock().schedule(5000, "mountDriver", %Vehicle, %Player);
   }

   if(%Vehicle.getTarget() != -1)
      setTargetSensorGroup(%Vehicle.getTarget(), %Client.getSensorGroup());
}

// Schedule the Spawn Checks every .85 seconds to prevent people from entering
// the spawn field.
function StartVehicleSpawnCheck(%Position, %Vehicle, %Player, %Times)
{
   %i = 0;
   while (%i < %Times)
   {
      schedule((850 * %i) + 500, 0, VehicleSpawnCheck, %Position, %Vehicle, %Player);

      %i++;
   }
}

function VehicleSpawnCheck(%Position, %Vehicle, %Player)
{
   %Mask = $TypeMasks::VehicleObjectType | $TypeMasks::MoveableObjectType |
           $TypeMasks::StaticShapeObjectType | $TypeMasks::StaticTSObjectType | 
           $TypeMasks::StationObjectType | $TypeMasks::ItemObjectType | 
           $TypeMasks::PlayerObjectType | $TypeMasks::TurretObjectType;

   InitContainerRadiusSearch(%Position, $VehicleSpawnRadius, %Mask);

   while ((%Object = containerSearchNext()) != 0)
   {
      // If it the vehicle it found
      if (%Object == %Vehicle)
         continue;

      // If it the turret on the vehicle it found
      if (%Object.Vehicle == %Vehicle)
         continue;

      %Object.getDataBlock().damageObject(%Object, %Player, %Position, 200, $DamageType::VehicleSpawn, %Momentium);
   }
}

//------------------------------------------------------------------------------
function VehicleData::mountDriver(%data, %obj, %player)
{
   %player.startFade(1000, 0, true);
 
   %player.getDataBlock().schedule(1000,"onCollision",%player, %obj, 0);
   %player.schedule(1500,"startFade",1000, 0, false);
}

//------------------------------------------------------------------------------
function VehicleData::checkIfPlayersMounted(%data, %obj)
{
   for(%i = 0; %i < %obj.getDatablock().numMountPoints; %i++)
      if (%obj.getMountNodeObject(%i))
         return true; 
   return false;
}

//------------------------------------------------------------------------------
function VehicleData::isMountable(%data, %obj, %val)
{
   %obj.mountable = %val;
}

//------------------------------------------------------------------------------
function vehicleCheck(%blockName, %team)
{
   if(($VehicleMax[%blockName] - $VehicleTotalCount[%team, %blockName]) > 0)
      return true;   
//   else
//   {
//      for(%i = 0; %i < $VehicleMax[%blockName]; %i++)
//      {
//         %obj = $VehicleInField[%blockName, %i];      
//         if(%obj.abandon)
//         {
//            vehicleListRemove(%blockName, %obj);
//            %obj.delete();
//            return true;
//         }
//      }
//   }
   return false;
}
	
//------------------------------------------------------------------------------
function VehicleHud::updateHud( %obj, %client, %tag )
{
   %Station = %client.player.station;
// Not sure if we need to clear the huds... They'll always have the same num rows...
   //if ( %Station.lastCount !$= "" )
   //   VehicleHud::clearHud( %client, %tag, %station.lastCount );

   %team = %client.getSensorGroup();
   %count = 0;
   if ( %station.vehicle[scoutVehicle] )
   {
      messageClient( %client, 'SetLineHud', "", %tag, %count, "GRAV CYCLE", "", ScoutVehicle, $VehicleMax[ScoutVehicle] - $VehicleTotalCount[%team, ScoutVehicle] );
//new      messageClient( %client, 'SetLineHud', "", %tag, %count, 'ScoutVehicle', $VehicleMax[ScoutVehicle] - $VehicleTotalCount[%team, ScoutVehicle], '', "GRAV CYCLE" );
      %count++;
   }
   if ( %station.vehicle[AssaultVehicle] )
   {
      messageClient( %client, 'SetLineHud', "", %tag, %count, "ASSAULT TANK", "", AssaultVehicle, $VehicleMax[AssaultVehicle] - $VehicleTotalCount[%team, AssaultVehicle] );
//new      messageClient( %client, 'SetLineHud', "", %tag, %count, 'AssaultVehicle', $VehicleMax[AssaultVehicle] - $VehicleTotalCount[%team, AssaultVehicle], '', "ASSAULT TANK");
      %count++;
   }
   if ( %station.vehicle[mobileBaseVehicle] )
   {
      messageClient( %client, 'SetLineHud', "", %tag, %count, "MOBILE POINT BASE", "", MobileBaseVehicle, $VehicleMax[MobileBaseVehicle] - $VehicleTotalCount[%team, MobileBaseVehicle] );
//new      messageClient( %client, 'SetLineHud', "", %tag, %count, 'MobileBaseVehicle', $VehicleMax[MobileBaseVehicle] - $VehicleTotalCount[%team, MobileBaseVehicle], '', "MOBILE POINT BASE" );
      %count++;
   }
   if ( %station.vehicle[scoutFlyer] )
   {
      messageClient( %client, 'SetLineHud', "", %tag, %count, "SCOUT FLIER", "", ScoutFlyer, $VehicleMax[ScoutFlyer] - $VehicleTotalCount[%team, ScoutFlyer] );
//new      messageClient( %client, 'SetLineHud', "", %tag, %count, 'ScoutFlyer', $VehicleMax[ScoutFlyer] - $VehicleTotalCount[%team, ScoutFlyer], '', "SCOUT FLIER");
      %count++;
   }
   if ( %station.vehicle[bomberFlyer] )
   {
      messageClient( %client, 'SetLineHud', "", %tag, %count, "BOMBER", "", BomberFlyer, $VehicleMax[BomberFlyer] - $VehicleTotalCount[%team, BomberFlyer] );
//new      messageClient( %client, 'SetLineHud', "", %tag, %count, 'BomberFlyer', $VehicleMax[BomberFlyer] - $VehicleTotalCount[%team, BomberFlyer], '', "BOMBER");
      %count++;
   }
   if ( %station.vehicle[hapcFlyer] )
   {
      messageClient( %client, 'SetLineHud', "", %tag, %count, "TRANSPORT", "", HAPCFlyer, $VehicleMax[HAPCFlyer] - $VehicleTotalCount[%team, HAPCFlyer] ); 
//new      messageClient( %client, 'SetLineHud', "", %tag, %count, 'HAPCFlyer', $VehicleMax[HAPCFlyer] - $VehicleTotalCount[%team, HAPCFlyer], '', "TRANSPORT"); 
      %count++;
   }
   %station.lastCount = %count;
}

//------------------------------------------------------------------------------
function VehicleHud::clearHud( %obj, %client, %tag, %count )
{
   for ( %i = 0; %i < %count; %i++ )
      messageClient( %client, 'RemoveLineHud', "", %tag, %i );
}
																							  
