//---------------------------------------------------------
// Deployable stargate
//---------------------------------------------------------

datablock ShapeBaseImageData(stargateDeployableImage) {
	mass = 15;
	emap = true;
	shapeFile = "stackable1s.dts";
	item = stargatePack;
	mountPoint = 1;
	offset = "0 0 0";
	deployed = stargateDeployedBase;
	heatSignature = 0;

	stateName[0] = "Idle";
	stateTransitionOnTriggerDown[0] = "Activate";

	stateName[1] = "Activate";
	stateScript[1] = "onActivate";
	stateTransitionOnTriggerUp[1] = "Idle";

	isLarge = true;
	maxDepSlope = 360;
	deploySound = StationDeploySound;

	minDeployDis = 0.5;
	maxDeployDis = 5.0;
};

datablock ItemData(stargatePack) {
	className = Pack;
	catagory = "Deployables";
	shapeFile = "stackable1s.dts";
	mass = 3.0;
	elasticity = 0.2;
	friction = 0.6;
	pickupRadius = 1;
	rotate = false;
	image = "stargateDeployableImage";
	pickUpName = "a stargate";
	heatSignature = 0;
    joint = "2 2 2";
	computeCRC = true;
	emap = true;
};

datablock SensorData(stargateBaseSensorObj) {
	detects = true;
	detectsUsingLOS = true;
	detectsPassiveJammed = false;
	detectsActiveJammed = false;
	detectsCloaked = false;
	detectionPings = true;
	detectRadius = 10;
};

datablock StaticShapeData(stargateDeployedBase) : StaticShapeDamageProfile {
	className = "teleport";
	shapeFile = "turret_sentry.dts";

	maxDamage = 2.00;
	destroyedLevel = 2.00;
	disabledLevel = 1.35;

	isShielded = true;
	energyPerDamagePoint = 250;
	maxEnergy = 100;
	rechargeRate = 1;

	explosion = ShapeExplosion; // DeployablesExplosion;
	expDmgRadius = 18.0;
	expDamage = 0.1;
	expImpulse = 200.0;

	dynamicType = $TypeMasks::StationObjectType;
	deployedObject = true;
	cmdCategory = "DSupport";
	cmdIcon = CMDSwitchIcon;
	cmdMiniIconName = "commander/MiniIcons/com_switch_grey";
	targetNameTag = 'Deployed';
	targetTypeTag = 'Star Gate';

	debrisShapeName = "debris_generic.dts";
	debris = DeployableDebris;

	heatSignature = 0;
	needsPower = true;

	humSound = SensorHumSound;
	pausePowerThread = true;
	sensorData = stargateBaseSensorObj;
	sensorRadius = stargateBaseSensorObj.detectRadius;
	sensorColor = "0 212 45";
	firstPersonOnly = true;

	lightType = "PulsingLight";
	lightColor = "0 1 0 1";
	lightTime = 1200;
	lightRadius = 6;
};

datablock StaticShapeData(stargateBeam) {
	className = "Station";
	catagory = "DSupport";
	shapefile = "disc.dts";
 rotation = "0 0 90";
 scale = "6 6 1";
 offset = "0 0 5";
 
	collideable = 1;
	needsNoPower = true;
	emap="true";
	sensorData = stargateBaseSensorObj;
	sensorRadius = stargateBaseSensorObj.detectRadius;
	sensorColor = "0 212 45";

	cmdCategory = "DSupport";
	targetNameTag = 'Teleport';
	targetTypeTag = 'Pad';

	lightType = "PulsingLight";
	lightColor = "0 1 0 1";
	lightTime = 1200;
	lightRadius = 6;
};

datablock AudioProfile(stargateAccessDeniedSound) {
	filename = "gui/vote_nopass.wav";
	description = AudioClosest3d;
	preload = true;
};

datablock AudioProfile(stargateBeamSound) {
	filename = "fx/vehicles/inventory_pad_on.wav";
	description = AudioClosest3d;
	preload = true;
};

datablock AudioProfile(stargatePowerUpSound) {
	filename = "fx/powered/turret_heavy_activate.wav";
	description = AudioClosest3d;
	preload = true;
};

datablock AudioProfile(stargatePowerDownSound) {
	filename = "fx/powered/inv_pad_off.wav";
	description = AudioClosest3d;
	preload = true;
};

datablock ParticleData(stargateTeleportParticle)
{
   dragCoeffiecient     = 0.0;
   gravityCoefficient   = 0.0;
   inheritedVelFactor   = 0.0;

   lifetimeMS           = 1200;
   lifetimeVarianceMS   = 400;

   textureName          = "special/lightFalloffMono";

   useInvAlpha =  false;
   spinRandomMin = -200.0;
   spinRandomMax =  200.0;

   colors[0]     = "0.5 0.8 0.2 1.0";
   colors[1]     = "0.6 0.9 0.3 1.0";
   colors[2]     = "0.7 1.0 0.4 1.0";
   sizes[0]      = 0.2;
   sizes[1]      = 0.1;
   sizes[2]      = 0.05;
   times[0]      = 0.0;
   times[1]      = 0.5;
   times[2]      = 1.0;
};

datablock ParticleEmitterData(stargateTeleportEmitter)
{
   ejectionPeriodMS = 1;
   ejectionOffset   = 7.0;
   periodVarianceMS = 0.0;
   ejectionVelocity = 2.0;
   velocityVariance = 2.0;
   thetaMin         = 0.0;
   thetaMax         = 10.0;
   lifetimeMS       = 0;

   particles = "stargateTeleportParticle";
};
datablock TriggerData(gateTrigger) {
	// TODO - check this
	tickPeriodMS = 200;
};

function stargateDeployedBase::onDestroyed(%this,%obj,%prevState) {
	if (%obj.isRemoved)
		return;
	%obj.isRemoved = true;
	Parent::onDestroyed(%this,%obj,%prevState);
	$TeamDeployedCount[%obj.team,stargatePack]--;
	%obj.isRemoved = true;
	remDSurface(%obj);
	%obj.beam.schedule(150,"delete");
	%obj.schedule(500,"delete");
 	if (isObject(%obj.gateTrigger))
		%obj.gateTrigger.schedule(500, "delete");
}

function stargateDeployedBase::disassemble(%data,%plyr,%obj) {
	if (isObject(%obj.beam))
		%obj.beam.delete();
  	if (isObject(%obj.gateTrigger))
		%obj.gateTrigger.schedule(500, "delete");
	%obj.isRemoved = true;
	disassemble(%data,%plyr,%obj);
}

function stargateDeployedBase::onGainPowerEnabled(%data,%obj) {
	if (shouldChangePowerState(%obj,true)) {
		tp_fadeIn(%obj,true);
		%obj.playThread($AmbientThread,"ambient");
		%obj.setThreadDir($AmbientThread,true);

		%obj.playThread($ActivateThread,"transition");
		%obj.setThreadDir($ActivateThread,false);

		%obj.play3D(stargatePowerUpSound);
	}
	Parent::onGainPowerEnabled(%data,%obj);
}

function stargateDeployedBase::onLosePowerDisabled(%data,%obj) {
	if (shouldChangePowerState(%obj,false)) {
		tp_fadeOut(%obj,true);
		%obj.stopThread($AmbientThread);

		%obj.playThread($ActivateThread,"transition");
		%obj.setThreadDir($ActivateThread,true);

		%obj.play3D(stargatePowerDownSound);
	}
	Parent::onLosePowerDisabled(%data,%obj);
}

function gateTrigger::onEnterTrigger(%data,%obj,%colObj) {
	if (%colObj.justTeleported || %obj.isRemoved)
		return;

	// verify pad.team is team associated and is on player's team
	if (%obj.team != %colObj.team && %obj.team != 0 && %obj.teleMode != 1 && %obj.frequency > 0) {
		%obj.play3D(stargateAccessDeniedSound);
		messageClient(%colObj.client,'msgClient','\c2Access Denied -- Wrong team.');
		%colObj.justTeleported = true;
		schedule(2000,0,"unTeleport",%colObj);
		return;
	}

	// verify that pad can transmit
	if (%obj.teleMode == 3) {
		%obj.play3D(stargateAccessDeniedSound);
		messageClient(%colObj.client,'msgClient','\c2Access Denied -- This pad can only receive.');
		%colObj.justTeleported = true;
		schedule(2000,0,"unTeleport",%colObj);
		return;
	}

	// Discover next pad to teleport to
	%dgroup = nameToID(Deployables);
	%destPad = 0;
	%firstPad = 0;
	for(%depIndex = 0; %depIndex < %dgroup.getCount(); %depIndex++) {
		%dep = %dgroup.getObject(%depIndex);
		if (%dep.getDataBlock().className $= "teleport") {
			if (%dep != %obj && %dep.frequency == %obj.frequency && %dep.team == %obj.team && %dep.teleMode != 2) {
				if (%dep.isPowered() && %dep.isEnabled() && !(%dep.getEnergyLevel() < %dep.getDataBlock().maxEnergy)) {
					if (!%firstPad || %dep < %firstPad)
						%firstPad = %dep;
					if (%dep > %obj && (!%destPad || %dep < %destPad)) {
						%destPad = %dep;
					}
				}
				else
					%notEnabled = true;
			}
		}
	}
	if (!%destPad)
		%destPad = %firstPad;

	if (!%obj.isPowered() || !%obj.isEnabled()) {
		%obj.play3D(stargateAccessDeniedSound);
		if (!%obj.isPowered() && !%obj.isEnabled())
			messageClient(%colObj.client,'msgClient','\c2Unable to teleport, stargate is damaged and has no power.');
		else if (!%obj.isEnabled())
			messageClient(%colObj.client,'msgClient','\c2Unable to teleport, stargate is damaged.');
		else if (!%obj.isPowered())
			messageClient(%colObj.client,'msgClient','\c2Unable to teleport, stargate is not powered.');
		else
			messageClient(%colObj.client,'msgClient','\c2Unable to teleport, stargate malfunction.');
		%colObj.justTeleported = true;
		schedule(2000,0,"unTeleport",%colObj);
		return;
	}

	if (!%destPad) {
		%obj.play3D(stargateAccessDeniedSound);
		if (%notEnabled)
			messageClient(%colObj.client,'msgClient','\c2Unable to teleport, destination is damaged, has no power or is recharging.');
		else
			messageClient(%colObj.client,'msgClient','\c2Unable to teleport, no other pads to teleport to.');
		%colObj.justTeleported = true;
		schedule(2000,0,"unTeleport",%colObj);
		return;
	}

	if (tp_isBlocked(%destPad,%colObj)) {
		%obj.play3D(stargateAccessDeniedSound);
		messageClient(%colObj.client,'msgClient','\c2Unable to teleport, destination is blocked.');
		%colObj.justTeleported = true;
		schedule(2000,0,"unTeleport",%colObj);
		return;
	}

	if (%obj.getEnergyLevel() < %obj.getDataBlock().maxEnergy) {
		%obj.play3D(stargateAccessDeniedSound);
		messageClient(%colObj.client,'msgClient','\c2Unable to teleport, stargate is recharging.');
		%colObj.justTeleported = true;
		schedule(2000,0,"unTeleport",%colObj);
		return;
	}

	// center player on pad
	if (!tp_isBlocked(%obj,%colObj))
		tp_adjustPlayer(%obj,%colObj);

	// fade out player
	%colObj.disableMove(true);
	pl_fadeOut(%colObj);

	// pad power up effect
	tp_fadePadIn(%obj);
	tp_fadePadIn(%destPad);

	// fade out beams
	schedule(600,0,"tp_fadeOut",%obj);
	schedule(750,0,"tp_fadeOut",%destPad);

	// pad power down effect
	schedule(1500,0,"tp_fadePadOut",%obj);
	schedule(1550,0,"tp_fadePadOut",%destPad);

	// fade in beams
	schedule(3000,0,"tp_fadeIn",%obj);
	schedule(3050,0,"tp_fadeIn",%destPad);

	// Zap energy
	%obj.setEnergyLevel(0);
	%destPad.setEnergyLevel(0);

	%colObj.justTeleported = true;

	messageClient(%colObj.client,'msgClient',"~wfx/misc/diagnostic_on.wav");
	schedule(500,0,"teleport",%colObj,%destPad,%obj); // schedule their teleportation
	tp_emitter(%obj);
}

function tp_isBlocked(%pad,%obj) {
	%padPos = %pad.getPosition();
	%telePos = vectorAdd(%padPos,vectorScale(realVec(%pad,"0 0 1"),-2.5));
	if (containerRayCast(%padPos,%telePos,-1,%pad))
		%blocked = true;
	if (!$Host::AllowUnderground) {
		%terrain = getTerrainHeight2(%telePos);
		if (getWord(%telePos,2) < getWord(%terrain,2) || %terrain $= "")
			%blocked = true;
	}
	if (isObject(%obj)) {
		%adjust = vectorAdd(vectorScale(vectorNormalize(realVec(%pad,"0 0 1")),-1.35),vectorSub(%obj.getPosition(),%obj.getWorldBoxCenter()));
		%pos = vectorAdd(%padPos,%adjust);
		if (!$Host::AllowUnderground) {
			%terrain = getTerrainHeight2(%pos);
			if (getWord(%pos,2) < getWord(%terrain,2) || %terrain $= "")
				%blocked = true;
		}
		if (containerRayCast(%telePos,%pos,-1,%obj))
			%blocked = true;
	}
	return %blocked;
}

// player
function pl_fadeIn(%obj) {
	%obj.startFade(500,0,false);
	messageClient(%colObj.client,'msgClient',"~wfx/misc/diagnostic_on.wav");
}

function pl_fadeOut(%obj) {
	%obj.startFade(500,0,true);
	messageClient(%colObj.client,'msgClient',"~wfx/misc/diagnostic_on.wav");
}

// beam and sound
function tp_fadeIn(%obj,%silent) {
	if (%obj.isPowered() && %obj.isEnabled()) {
		if (!%silent)
			%obj.play3D(DiscReloadSound);
		%obj.beam.startFade(100,0,false);
	}
}

function tp_fadeOut(%obj,%silent) {
	if (!%silent)
		%obj.play3D(stargateBeamSound);
	%obj.beam.startFade(100,0,true);
}

// pad
function tp_fadePadIn(%obj) {
	%obj.playThread($ActivateThread,"transition");
	%obj.setThreadDir($ActivateThread,true);
}

function tp_fadePadOut(%obj) {
	%obj.playThread($ActivateThread,"transition");
	%obj.setThreadDir($ActivateThread,false);
}

// fancy emitter
function tp_emitter(%obj) {
	%em = new ParticleEmissionDummy() {
		scale = "1 1 1";
		dataBlock = "defaultEmissionDummy";
		emitter = "stargateTeleportEmitter";
		velocity = "1";
	};
	%adjust = vectorScale(realVec(%obj,"0 0 1"),7);
	%em.setTransform(vectorAdd(%obj.getPosition(),%adjust) SPC rotAdd(%obj.getRotation(),"1 0 0" SPC $Pi));
	MissionCleanup.add(%em);
	%em.schedule(1000,"delete");
}

function unTeleport(%pl) {
	if (isObject(%pl))
		%pl.justTeleported = false;
}

function teleport(%pl,%destPad,%src) {
	%pl.setVelocity("0 0 0"); //slow me down, ive been falling :)
	%pl.schedule(500,disableMove,false);
	pl_fadeIn(%pl);
	schedule(2650,0,"unTeleport",%pl);

	if (!isObject(%destPad)) {// lost the destination
		messageClient(%pl.client,'msgClient','\c2Lost destination!');
		tp_adjustPlayer(%src,%pl);
	}
	else {
		if (%pl.team == %src.team)
			messageClient(%pl.client,'msgClient','\c2Teleporting on frequency %1.',%src.frequency);
		else
			messageClient(%pl.client,'msgClient','\c2Teleporting on ENEMY frequency %1!',%src.frequency);
		tp_adjustPlayer(%destPad,%pl);
		tp_emitter(%destPad);
	}
}

function tp_adjustPlayer(%pad,%obj) {
	%adjust = vectorAdd(vectorScale(vectorNormalize(realVec(%pad,"0 0 1")),-1.35),vectorSub(%obj.getPosition(),%obj.getWorldBoxCenter()));
	%rot =  %obj.getRotation();
	%obj.setTransform(vectorAdd(%pad.getPosition(),%adjust) SPC %rot);
}

function tp_adjustBeam(%pad) {
	%rot =  rotAdd(%pad.getRotation(),"0 0 90" SPC $Pi);
	%pad.beam.setTransform(%pad.getPosition() SPC %rot);
}

function stargatePack::onPickup(%this,%obj,%shape,%amount) {
	// created to prevent console errors
}

function stargateDeployableImage::onDeploy(%item,%plyr,%slot) {
	%className = "StaticShape";

	%item.surfacePt = vectorAdd(%item.surfacePt,vectorScale(%item.surfaceNrm,0.4));

	%playerVector = vectorNormalize(getWord(%plyr.getEyeVector(),1) SPC -1 * getWord(%plyr.getEyeVector(),0) SPC "0");
	%item.surfaceNrm2 = %playerVector;

	if (vAbs(floorVec(%item.surfaceNrm,100)) $= "0 0 1")
		%item.surfaceNrm2 = vectorScale(%playerVector,-1);
	else
		%item.surfaceNrm2 = vectorNormalize(vectorCross(%item.surfaceNrm,"0 0 1"));

	%rot = fullRot(vectorScale(%item.surfaceNrm,-1),%item.surfaceNrm2);

	%deplObj = new (%className)() {
		dataBlock = stargateDeployedBase;
		scale = "1 1 1";
		deployed = true;
		teleMode = %plyr.expertSet;
	};

 	%beam = new (StaticShape)() {
		dataBlock = stargateBeam;
		scale = "8 8 4";
        //position = vectoradd(%item.surfacePt,"0 0 2");
	};
 %deplObj.mountObject(%beam, 1);
 MissionCleanup.add(%beam);

  	%deplObj.gateTrigger = new Trigger() {
		dataBlock = gateTrigger;
		polyhedron = "0 1 0 1 0 0 0 -1 0 0 0 1";
		scale = "2 2 2";
        //position = vectoradd(%deplObj.beam.position,"-1.9 0 -1");
	};

	// set orientation
	%deplObj.setTransform(%item.surfacePt SPC %rot);
    //%deplObj.beam.setTransform(vectoradd(%item.surfacePt,"0 0 2") SPC vectoradd(%rot,"1 0 0 90"));//%rot);
    //%deplObj.gateTrigger.setTransform(vectoradd(%item.surfacePt,"1.6 0.4 3") SPC %rot);
	// set team, owner, and handle
	%deplObj.team = %plyr.client.team;
	%deplObj.setOwner(%plyr);

	// set power frequency
	%deplObj.powerFreq = %plyr.powerFreq;

	// set the sensor group if it needs one
	if (%deplObj.getTarget() != -1)
		setTargetSensorGroup(%deplObj.getTarget(),%plyr.client.team);

	// set frequency
	%frequency = %plyr.packSet;
	if (!%frequency)
		%frequency = 1;
	%deplObj.frequency = %frequency;
	setTargetName(%deplObj.target,addTaggedString("Frequency" SPC %frequency));

	// attach beam
//	%deplObj.beam = new (StaticShape)() {
//		dataBlock = stargateBeam;
//		scale = "6 6 2";
//      position = "0 0 4";
//      rotation = vectoradd(%rot,"1 0 0 90");
//	};

	// set orientation
	tp_adjustBeam(%deplObj);

	%deplObj.beam.playThread(0,"ambient");
	%deplObj.beam.setThreadDir(0,true);
	// The flash animation plays forwards, then back automatically,so we have to alternate the thread direcction...
	%deplObj.beam.flashThreadDir = true;
    //%deplObj.beam.position = vectoradd(%item.surfacePt,"0 0 3");
    //%deplObj.beam.rotation = "1 0 0 90";
	%deplObj.beam.base = %deplObj;

	// place the deployable in the MissionCleanup/Deployables group (AI reasons)
	addToDeployGroup(%deplObj);

	//let the AI know as well...
	AIDeployObject(%plyr.client,%deplObj);

	// play the deploy sound
	serverPlay3D(%item.deploySound,%deplObj.getTransform());

	// increment the team count for this deployed object
	$TeamDeployedCount[%plyr.team,%item.item]++;

	addDSurface(%item.surface,%deplObj);

	// take the deployable off the player's back and out of inventory
	//%plyr.unmountImage(%slot);
	//%plyr.decInventory(%item.item,1);

	// Power object
	checkPowerObject(%deplObj);

	return %deplObj;
}

function stargateDeployableImage::onMount(%data,%obj,%node) {
	%obj.hasTele = true; // set for telecheck
	%obj.packSet = 1;
	%obj.expertSet = 0;
	displayPowerFreq(%obj);
}

function stargateDeployableImage::onUnmount(%data,%obj,%node) {
	%obj.hasTele = "";
	%obj.packSet = 0;
	%obj.expertSet = 0;
}
