datablock ShapeBaseImageData(TelePadDeployableImage)
{
mass = 15;
emap = true;

shapeFile = "stackable1s.dts";
item = TelePadPack;
mountPoint = 1;
offset = "0 -0.2 0";

deployed = TelePadDeployedBase;
heatSignature = 0.5;

stateName[0] = "Idle";
stateTransitionOnTriggerDown[0] = "Activate";

stateName[1] = "Activate";
stateScript[1] = "onActivate";
stateTransitionOnTriggerUp[1] = "Idle";

isLarge = true;
maxDepSlope = 30;
deploySound = StationDeploySound;
minDeployDis = 0.5;
maxDeployDis = 8.0;
};

datablock ItemData(TelePadPack)
{
className = Pack;
catagory = "Deployables";
shapeFile = "stackable1s.dts";
mass = 3.0;
elasticity = 0.2;
friction = 0.6;
pickupRadius = 1;
rotate = false;
image = "TelePadDeployableImage";
pickUpName = "a deployable teleport pad";
heatSignature = 0;

computeCRC = true;
emap = true;

};

datablock SensorData(TelePadBaseSensorObj)
{
detects = true;
detectsUsingLOS = true;
detectsPassiveJammed = false;
detectsActiveJammed = false;
detectsCloaked = false;
detectionPings = true;
detectRadius = 10;
};


datablock StaticShapeData(TelePadDeployedBase) : StaticShapeDamageProfile
{
className = "Station";
catagory = "Deployables";
shapefile = "NexusBase.dts";

rechargeRate = 0.31;

needsNoPower = true;

dynamicType = $TypeMasks::StationObjectType;
mass = 5.0;
maxDamage = 2.00;
destroyedLevel = 2.00;
disabledLevel = 1.35;
repairRate = 0;
explosion = DeployablesExplosion;
expDmgRadius = 18.0;
expDamage = 0.8;
expImpulse = 2000.0;

deployedObject = true;

energyPerDamagePoint = 50;
maxEnergy = 150;

humSound = SensorHumSound;
heatSignature = 0;
pausePowerThread = true;

debrisShapeName = "debris_generic.dts";
debris = DeployableDebris;

cmdIcon = CMDSwitchIcon;
cmdCategory = "DSupport";
cmdMiniIconName = "commander/MiniIcons/com_switch_grey";
targetNameTag = 'Deployed';
targetTypeTag = 'Teleport Pad';

sensorData = TelePadBaseSensorObj;
sensorRadius = TelePadBaseSensorObj.detectRadius;
sensorColor = "0 212 45";

firstPersonOnly = true;

//lightOnlyStatic = true;
lightType = "PulsingLight";
lightColor = "0 1 0 1";
lightTime = 1200;
lightRadius = 6; 

};

datablock StaticShapeData(TelePadBeam)
{
className = "Station";
catagory = "DSupport";
shapefile = "nexus_effect.dts";
collideable = 1;
needsNoPower = true;
emap="true";
sensorData = TelePadBaseSensorObj;
sensorRadius = TelePadBaseSensorObj.detectRadius;
sensorColor = "0 212 45";

cmdCategory = "DSupport";
targetNameTag = 'Teleport';
targetTypeTag = 'Pad';

//lightOnlyStatic = true;
lightType = "PulsingLight";
lightColor = "0 1 0 1";
lightTime = 1200;
lightRadius = 6; 
};

function TelePadDeployedBase::onDestroyed(%this, %obj, %prevState)
{
echo("pad " @ %obj @ " destroyed!");
Parent::onDestroyed(%this, %obj, %prevState);
echo("Destroyed a Teleporter - Team: "@ %obj.team);
$TeamDeployedCount[%obj.team, TelePadPack]--;

if($TeamDeployedCount[%obj.team, TelePadPack] > 0) // this wasnt the last
{
if($firstPad[%obj.team] == %obj) // the first was destroyed
{
echo("first pad destroyed");
$firstPad[%obj.team] = %obj.nextPad; // make the second one the first
echo("first pad (" @ %obj @ ") destroyed, new first :" @ $firstPad[%obj.team]);
$firstPad[%obj.team].prevPad = -1;
}
else
{
%obj.prevPad.nextPad = %obj.nextPad;
%obj.nextPad.prevPad = %obj.prevPad;
}

//blow up another one, maybe
%rnd = mFloor(getRandom() * 10);
echo("rnd " @ mFloor(%rnd));
if(%rnd == 5) // one in ten chance
{
echo("another pad blows up");
%obj.nextPad.setDamageLevel(2.01);
}
}
else // last one
$firstPad[%obj.team] = ""; // remove it

//blow up

%obj.shield[0].schedule(10, "delete");
%obj.schedule(15, "delete");
}

function TelePadDeployedBase::onCollision(%data, %obj, %col)
{
if(%col.justTeleported)
return;

%teleteam = %obj.team;

if(%obj.Hacked) // if it is hacked, work some mojo
%teleteam = %obj.OldTeam;

%pads = $TeamDeployedCount[%teleteam, TelePadPack];
//verify pd.team is team associated and is on player's team
if(!%obj.isPowered())
{
%obj.playAudio(0, TelePadAccessDeniedSound);
messageClient(%col.client, 'msgTelepadDenied', '\c2Unable to teleport, pad is not powered.');
%col.justTeleported = true;
schedule(2000, %col, "unteleport", %col);
return;
}
if(!%obj.nextPad.isPowered())
{
%obj.playAudio(0, TelePadAccessDeniedSound);
messageClient(%col.client, 'msgTelepadDenied', '\c2Unable to teleport, destination pad is not powered.');
%col.justTeleported = true;
schedule(2000, %col, "unteleport", %col);
return;
}
if((%obj.team != %col.client.team) && (%obj.team != 0))
{
%obj.playAudio(0, TelePadAccessDeniedSound);
messageClient(%col.client, 'msgTelepadDenied', '\c2Access Denied -- Wrong team.');
%col.justTeleported = true;
schedule(2000, %col, "unteleport", %col);
return;
}
if(%pads < 2)
{
%obj.playAudio(0, TelePadAccessDeniedSound);
messageClient(%col.client, 'msgTelepadDenied', '\c2No other pads to teleport to.');
%col.justTeleported = true;
schedule(2000, %col, "unteleport", %col);
return;
}
if(%obj.teleCharging)
{
%obj.playAudio(0, TelePadAccessDeniedSound);
messageClient(%col.client, 'msgTelepadDenied', '\c2Unable to teleport, pad is recharging.');
%col.justTeleported = true;
schedule(2000, %col, "unteleport", %col);
return;
}

%rnd = getRandom();

if(%pads > 2)
%padloop = (mFloor(%rnd * (%pads - 1)) + 1);
else
%padloop = 1;

%destPad = %obj.nextPad;

echo("teleporting player " @ %col @ " from pad " @ %obj @ " to pad " @ %destPad);

%obj.shield[0].startFade(500, 0, true );
//%col.startFade( 0, 0, true );

%col.justTeleported = true;
%obj.playAudio(0, IdleSound);

%col.setWhiteout(1); // a white flash
//%col.setTransform(setWord(%col.getPosition(), 2, -8000) SPC "0 0 1 0"); //No more "in-transit"
messageClient(%col.client, 'msgTelepadDenied', "~wfx/misc/diagnostic_on.wav");
teleport(%col, %destPad, %obj); // Removed scheduler and just run this function to teleport

schedule(5000, %destPad, "tp_fadein", %destPad); // lowered power time to 5 seconds from 10
schedule(5000, %obj, "tp_fadein", %obj); // lowered power time to 5 seconds from 10
//schedule(1000, %col, "pl_fadein", %col);

%obj.setEnergyLevel(0);
%obj.teleCharging = true;
}

function pl_fadein(%obj)
{
%obj.startFade(500, 0, false);
}

function tp_fadein(%obj)
{
if (%obj.isPowered())
{
%obj.playAudio(0, TelePadBeamSound);
%obj.shield[0].startFade(500, 0, false);
%obj.teleCharging = false;
}
}

function unteleport(%pl)
{
%pl.justTeleported = false;
}

function teleport(%pl, %destPad, %src)
{
%pl.setVelocity("0 0 0"); //slow me down, ive been falling :)
if(!isObject(%destPad)) // lost the destination
%pl.setTransform(%src.getPosition() SPC "0 0 1 0");
else
{
%pl.setTransform(vectorAdd(%destPad.getPosition(), "0 0 2") SPC "0 0 1 0");
%destPad.shield[0].startFade(500, 0, true );
%destPad.playAudio(0, IdleSound);
%destPad.teleCharging = true;
%destPad.setEnergyLevel(0);
}
schedule(2000, %pl, "unteleport", %pl);
}

function TelePadBeam::onCollision(%data, %obj, %col)
{
echo("beam collision!!!!!"); 
}

datablock AudioProfile(TelePadAccessDeniedSound)
{
filename = "fx/weapons/ELF_underwater.wav";
description = AudioClosest3d;
preload = true;
};

datablock AudioProfile(TelePadBeamSound)
{
filename = "fx/powered/inv_pad_appear.wav";
description = AudioClosest3d;
preload = true;
};


function TelePadPack::onPickup(%this, %obj, %shape, %amount)
{
// created to prevent console errors
}

function TelePadDeployableImage::onDeploy(%item, %plyr, %slot)
{
echo("teledeploy!!");
%deplObj = Parent::onDeploy(%item, %plyr, %slot);
//attach green beam
%deplObj.shield[0] = new (StaticShape)() {
dataBlock = TelePadBeam;
};
%deplObj.shield[0].setTransform(%item.surfacePt SPC %rot);
//addToDeployGroup(%beamObj);
//%deplObj.beam = %beamObj;
//echo("pad deployed : " @ %deplObj);
%deplObj.shield[0].playThread(0, "ambient");
%deplObj.shield[0].setThreadDir(0, true);
//The flash animation plays forwards, then back automatically, so we have to alternate the thread direcction...
%deplObj.shield[0].flashThreadDir = true;
echo("setting beam!!");
%padcnt = $TeamDeployedCount[%plyr.team, %item.item];

if(%padcnt == 1)
{
//first pad
echo("first pad : " @ %deplObj);
$firstPad[%plyr.team] = %deplObj;
%deplObj.nextPad = %deplObj;
%deplObj.prevPad = -1;
}
else
{
%lastPad = $firstPad[%plyr.team];

%txt = "pad list ";
%txta = "pad list ";
%txtb = "pad list ";

for(%i = 1; %i < (%padcnt -1); %i++) // move to the last pad so we can add the next one
%lastPad = %lastPad.nextPad;

%lastPad.nextPad = %deplObj;
%deplObj.prevPad = %lastPad;
%deplObj.nextPad = $firstPad[%plyr.team];

%a = $firstPad[%plyr.team];
echo("lastPad " @ %lastPad);
echo("firstPad " @ %a);

for(%i = 1; %i <= %padcnt; %i++)
{
%txt = %txt @ "("@%i@") t:" @ %a @ " -> ";
%txta = %txta @ "("@%i@") p:" @ %a.prevPad @ " -> ";
%txtb = %txtb @ "("@%i@") n:" @ %a.nextPad @ " -> ";
%a = %a.nextPad;
}
}
   %team = "Team" @ %plyr.team;
   %group = nameToId("MissionGroup/Teams/"@ %team @"/Base0");
   %deplObj.dSurface = %item.surface;
   %group.add(%deplObj);
   %group.powerinit();
   //%DeplObj.playThread($AmbientThread, "ambient");  
if (%item.surface.getClassName() $= TerrainBlock)
	%DeplObj.setDamageLevel(%DeplObj.getMaxDamage()/2);
return %deplObj;
}

function TelePadDeployedBase::disassemble(%data, %plyr, %hTgt)
{
%teleteam = %hTgt.team;


if($TeamDeployedCount[%hTgt.team, TelePadPack] > 0) // this wasnt the last
{
if($firstPad[%hTgt.team] == %hTgt) // the first was destroyed
{
echo("first pad destroyed");
$firstPad[%hTgt.team] = %hTgt.nextPad; // make the second one the first
echo("first pad (" @ %hTgt @ ") destroyed, new first :" @ $firstPad[%hTgt.team]);
$firstPad[%hTgt.team].prevPad = -1;
}
else
{
%hTgt.prevPad.nextPad = %hTgt.nextPad;
%hTgt.nextPad.prevPad = %hTgt.prevPad;
}

//blow up another one, maybe
//%rnd = mFloor(getRandom() * 10);
//echo("rnd " @ mFloor(%rnd));
//if(%rnd == 5) // one in ten chance
//{
//echo("another pad blows up");
//%hTgt.nextPad.setDamageLevel(2.01);
//}
}
else // last one
$firstPad[%hTgt.team] = ""; // remove it

//blow up
%revitem = %hTgt.getDataBlock().getName();
%newpack = $ReverseDeployItem[%revitem];
$TeamDeployedCount[%hTgt.team,%newpack]--;

serverPlay3D(Dsound, %hTgt.getTransform());
%hTgt.shield[0].delete();
%hTgt.delete();

}

function TelePadDeployedBase::onGainPowerEnabled(%data, %obj)
{
%obj.stopThread($TransitionThread);
%obj.playThread($AmbientThread, "ambient");
%obj.shield[0].startFade(2000, 0, false);
//schedule(10, %obj, "tp_fadein", %obj);
}

function TelePadDeployedBase::onLosePowerDisabled(%data, %obj)
{
%obj.stopThread($AmbientThread);
%obj.playThread($TransitionThread, "transition");
%obj.shield[0].startFade(500, 0, true );
}
