// Experimental AntiMech RAM Implementation
// This code works by constantly taking samples of a given Mech's position and velocity, creating a sort of "bookmark" if you will.
// These book marks are used to revert back to a good point when the mech (some obscenely heavy vehicle) is rammed by a vehicle in
// Tribes 2 as the physics system apparently ignores the Player object's MASSIVE mass when a vehicle happens to impact with the
// Player.
// Copyright (c) 2013 Robert MacGregor

// How many milliseconds between each sample
// Default: 40
$ARAM::SampleSpeed = 40;

// How much of a counter force is applied? (It is a percentage of the impact velocity)
$ARAM::CounterForce = 0.05;

function HoverVehicle::isVehicle(%this){ return true; }
function FlyingVehicle::isVehicle(%this){ return true; }
function WheeledVehicle::isVehicle(%this){ return true; }
function Vehicle::isVehicle(%this){ return true; }
function Player::isVehicle(%this){ return false; }
function Player::isMech(%this){ return %this.isMech; }

package AntiMechRAM
{
	function Armor::onCollision(%this, %obj, %collided, %forceVehicleNode)
	{
		parent::OnCollision(%this, %obj, %collided, %forceVehicleNode);
		
		if (%collided.isVehicle() && %obj.isMech())
		{
			%obj.setTransform(%this.samplePosition);
			%obj.setVelocity(%this.sampleVelocity);

			%counterForce = vectorScale(%collided.getVelocity(), -$ARAM::CounterForce);
			%obj.setVelocity(vectorAdd(%obj.getVelocity(), %counterForce));
			// Or whatever else you feel like.
			%collided.applyDamage(%collided.getDatablock().destroyedLevel);
		}
	}
	
	function Player::SamplerUpdate(%this)
	{
		if (isEventPending(%this.ProfileUpdateScheduler))
			cancel(%this.ProfileUpdateScheduler);

		%this.samplePosition = %this.getPosition();
		%this.sampleVelocity = %this.getVelocity();
	
		%this.SamplerUpdateScheduler = %this.schedule($ARAM::SampleSpeed, "SamplerUpdate");
	}
};

if (!isActivePackage(AntiMechRAM))
	activatePackage(AntiMechRAM);
