$PlayerAnim::Crouching = 25;
$PlayerAnim::DieChest = 26;
$PlayerAnim::DieHead = 27;
$PlayerAnim::DieGrabBack = 28;
$PlayerAnim::DieRightSide = 29;
$PlayerAnim::DieLeftSide = 30;
$PlayerAnim::DieLegLeft = 31;
$PlayerAnim::DieLegRight = 32;
$PlayerAnim::DieBlownBack = 33;
$PlayerAnim::DieSpin = 34;
$PlayerAnim::DieForward = 35;
$PlayerAnim::DieForwardKneel = 36;
$PlayerAnim::DieBack = 37;

//----------------------------------------------------------------------------
$CorpseTimeoutValue = 22;
//----------------------------------------------------------------------------

// Player & Armor data block callbacks

function Player::onAdd(%this)
{
	GameBase::setRechargeRate(%this,8);
}

function Player::onRemove(%this)
{
	// Drop anything left at the players pos
	for (%i = 0; %i < 8; %i = %i + 1) {
		%type = Player::getMountedItem(%this,%i);
		if (%type != -1) {
			// Note: Player::dropItem ius not called here.
			%item = newObject("","Item",%type,1,false);
         schedule("Item::Pop(" @ %item @ ");", $ItemPopTime, %item);

         addToSet("MissionCleanup", %item);
			GameBase::setPosition(%item,GameBase::getPosition(%this));
		}
	}
}

function Player::onNoAmmo(%player,%imageSlot,%itemType)
{
	//echo("No ammo for weapon ",%itemType.description," slot(",%imageSlot,")");
}

function Player::onKilled(%this)
{
	%cl = GameBase::getOwnerClient(%this);
	%cl.dead = 1;
	if($AutoRespawn > 0)
		schedule("Game::autoRespawn(" @ %cl @ ");",$AutoRespawn,%cl);
	if(%this.outArea==1)	
		leaveMissionAreaDamage(%cl);
	Player::setDamageFlash(%this,0.75);
	for (%i = 0; %i < 8; %i = %i + 1) {
		%type = Player::getMountedItem(%this,%i);
		if (%type != -1) {
			if (%i != $WeaponSlot || !Player::isTriggered(%this,%i) || getRandom() > "0.5") 
				Player::dropItem(%this,%type);
		}
	}

   if(%cl != -1)
   {
		if(%this.vehicle != "")
		{
			if(%this.driver != "")
			{
				%this.driver = "";
        	 		Client::setControlObject(Player::getClient(%this), %this);
	        	 	Player::setMountObject(%this, -1, 0);
			}
			else
			{
				%this.vehicle.Seat[%this.vehicleSlot-2] = "";
				%this.vehicleSlot = "";
			}
			if(GameBase::getDataName(%this.vehicle) == JetfireVehicle || GameBase::getDataName(%this.vehicle) == Jetfire)
				GameBase::setDamageLevel(%this.vehicle, GameBase::getDataName(%this.vehicle).maxDamage);
			%this.vehicle = "";		
		}
      schedule("GameBase::startFadeOut(" @ %this @ ");", $CorpseTimeoutValue, %this);
      Client::setOwnedObject(%cl, -1);
      Client::setControlObject(%cl, Client::getObserverCamera(%cl));
      Observer::setOrbitObject(%cl, %this, 5, 5, 5);
      schedule("deleteObject(" @ %this @ ");", $CorpseTimeoutValue + 2.5, %this);
      %cl.observerMode = "dead";
      %cl.dieTime = getSimTime();
   }
}


//mod:

function Player::stop(%player) // for stun grenade
{
	Item::setVelocity(%player, "0 0 0");
}

function Player::dance(%this, %steps) // for jitterbug
{
	if(%steps)
	{
		%x = getRandom() * 100 - 50;
		%y = getRandom() * 100 - 50;
		%z = getRandom() * 50;
		Player::applyImpulse(%this, %x @ " " @ %y @ " " @ %z);

		schedule("Player::dance(" @ %this @ ", " @ %steps - 1 @ ");", 0.2);
	}
}

function Player::wearOffEMP(%this)
{
	%this.hitByEMP--;
	if(%this.hitByEMP <= 0)
	{
		GameBase::setRechargeRate(%this, %this.recharge);
		Client::sendMessage(GameBase::getControlClient(%this), 1, "Energy has returned to normal.");
		%this.hitByEMP = 0; //just in case... =)
	}
}

function Player::onDamage(%this,%type,%value,%pos,%vec,%mom,%vertPos,%quadrant,%object)
{
//mod:
     	%shooterClient = %object;
      %damagedClient = Player::getClient(%this);

	if(%type == $LaserDamageType)
	{
		if(GameBase::getDataName(%object) != "Player")
			%type = $ObeliskDamageType;
		%shootWeapon = Player::getMountedItem(%shooterClient, $WeaponSlot);
		if(%shootWeapon == LaserChaingun)
			%type = $LCGDamageType;
	}

	if(%type == $SlowDamageType)
	{
		if(%shooterClient.massShot == "SlowPod")
		{
			Player::incItemCount(%this,DeadWeight);
			Player::mountItem(%this,DeadWeight, 4);
			Client::sendMessage(Player::getClient(%this), 0, "You have been slowed!");
			schedule("Player::decItemCount(" @ %this @ ",DeadWeight);", 8);
			return;
		}
		else
		{
			%armor = Player::getArmor(%this);
			if(%armor == harmor) %mount = "LessWeight3";
			else if(%armor == marmor || %armor == mfemale || %armor == tarmor || %armor == scvarmor) %mount = "LessWeight2";
			else %mount = "LessWeight";

			Player::incItemCount(%this,%mount);
			Player::mountItem(%this,%mount, 4);
			Client::sendMessage(Player::getClient(%this), 0, "You have been lightened!");
			schedule("Player::decItemCount(" @ %this @ "," @ %mount @ ");", 8);
			return;
		}
	}
	else if(%type == $StunDamageType)
	{
		%obj = Client::getOwnedObject(%damagedClient);
		Player::stop(%obj);
		schedule("Player::stop(" @ %obj @ ");", 0.5);
		schedule("Player::stop(" @ %obj @ ");", 1.0);
		return;
	}
	else if(%type == $DecloakDamageType)
	{
		%pack = Player::getMountedItem(%this, $BackpackSlot);
		if(Player::isTriggered(%this,$BackpackSlot))
			Player::useItem(%this, %pack);
		if(%this.cloaked == 1)
		{
			GameBase::startFadeIn(%this);
			%this.cloaked = 0;
		}
		return;
	}
	else if(%type == $JitterbugDamageType)
	{
		Player::dance(%this, 20);
		return;
	}
	else if(%type == $EMPDamageType)
	{
		GameBase::setEnergy(%this, 0);
		%temp = GameBase::getRechargeRate(%this);
		if(%temp > 0) //else hitting with 2 EMP in a row would kill energy forever
			%this.recharge = %temp;
		GameBase::setRechargeRate(%this, 0);
		%this.hitByEMP++;
		Client::sendMessage(%damagedClient, 1, "You have been hit with an EMP burst. Energy will return in 10 seconds.");
		schedule("Player::wearOffEMP(" @ %this @ ");", 10.0);
		return;
	}

	if(Player::getItemCount(%this, C4Ammo) > 1)
	{
		%chance = floor(getRandom() * 100);
		if(%chance < 10)
		{
			%client = Player::getClient(%player);
			Client::sendMessage(%client, 1, "The C4 was ignited by the last shot!");
			Player::decItemCount(%player, C4Ammo);
			Player::decItemCount(%player, Grenade);
			%obj = newObject("","Mine", "C4Oops");
 	 	 	addToSet("MissionCleanup", %obj);
			GameBase::throw(%obj,%player,9 * %client.throwStrength,false);
			%player.throwTime = getSimTime() + 0.5;
		}
	}


	if (Player::isExposed(%this))
	{

		Player::applyImpulse(%this,%mom);
		if($teamplay && %damagedClient != %shooterClient && Client::getTeam(%damagedClient) == Client::getTeam(%shooterClient) )
		{
			if (%shooterClient != -1)
			{
				%curTime = getSimTime();
				if ((%curTime - %this.DamageTime > 3.5 || %this.LastHarm != %shooterClient) && %damagedClient != %shooterClient && $Server::TeamDamageScale > 0)
				{
					if(%type != $MineDamageType)
					{
						Client::sendMessage(%shooterClient,0,"You just harmed Teammate " @ Client::getName(%damagedClient) @ "!");
						Client::sendMessage(%damagedClient,0,"You took Friendly Fire from " @ Client::getName(%shooterClient) @ "!");
					}
					else
					{
						Client::sendMessage(%shooterClient,0,"You just harmed Teammate " @ Client::getName(%damagedClient) @ " with your mine!");
						Client::sendMessage(%damagedClient,0,"You just stepped on Teamate " @ Client::getName(%shooterClient) @ "'s mine!");
					}
					%this.LastHarm = %shooterClient;
					%this.DamageStamp = %curTime;
				}
			}
			%friendFire = $Server::TeamDamageScale;
		}
		else if(%type == $ImpactDamageType && Client::getTeam(%object.clLastMount) == Client::getTeam(%damagedClient)) 
			%friendFire = $Server::TeamDamageScale;
		else  
			%friendFire = 1.0;	
	
		if (!Player::isDead(%this))
		{
			%armor = Player::getArmor(%this);
			//More damage applyed to head shots
			if(%vertPos == "head" && (%type == $LaserDamageType || %type == $MarlinDamageType))
			{
				if(%armor == "harmor")
				{ 
					if(%quadrant == "middle_back" || %quadrant == "middle_front" || %quadrant == "middle_middle")
					{
						%value += (%value * 0.3);
					}
				}
				else
				{
					%value += (%value * 0.3);
				}
			}
			//If Shield Pack is on
			if (%type != -1 && %this.shieldStrength)
			{
				%energy = GameBase::getEnergy(%this);
				%strength = %this.shieldStrength;
				if (%type == $ShrapnelDamageType || %type == $MortarDamageType)
					%strength *= 0.75;
				%absorb = %energy * %strength;
				if (%value < %absorb)
				{
					GameBase::setEnergy(%this,%energy - ((%value / %strength)*%friendFire));
					%thisPos = getBoxCenter(%this);
					%offsetZ =((getWord(%pos,2))-(getWord(%thisPos,2)));
					GameBase::activateShield(%this,%vec,%offsetZ);
					%value = 0;
				}
				else
				{
					GameBase::setEnergy(%this,0);
					%value = %value - %absorb;
				}
			}
//mod: dropping/moving functions... only when shield is off
			else
			{
//drop weapon/flag
				if(%vertPos=="torso" && %quadrant == "front_right" && %type == $LaserDamageType && Player::getMountedItem(%this, 0) != -1)
				{
					Client::sendMessage(%damagedClient, 1, Client::getName(%shooterClient) @ " just shot the " @ Player::getMountedItem(%this, 0) @ " out of your hand!");
					Player::dropItem(%this, Player::getMountedItem(%this, 0));
					%value = %value/2;
				}
				else if(%vertPos=="torso" && (%quadrant == "back_left" || %quadrant == "back_right") && Player::getMountedItem(%this, 2) == Flag && %type = $LaserDamageType)
				{
					Client::sendMessage(%damagedClient, 1, Client::getName(%shooterClient) @ " just shot the " @ Player::getMountedItem(%this, 2) @ " off of your back!");
					Player::dropItem(%this, Player::getMountedItem(%this, 2));
					%value = %value/3;
				}
//spin
				if(%type == $LaserDamageType && (%quadrant == "back_left" || %quadrant == "front_right"))
				{
					GameBase::setRotation(%damagedClient, Vector::add(GameBase::getRotation(%damagedClient), "0 0 -0.2"));
				}
				else if(%type == $LaserDamageType && (%quadrant == "back_right" || %quadrant == "front_left"))
				{
					GameBase::setRotation(%damagedClient, Vector::add(GameBase::getRotation(%damagedClient), "0 0 0.2"));
				}
				else if(%type == $ConcussionDamageType)
				{
					%rand = 3 * getRandom();
					if(%rand < 1.2)
						%rand = -1.2 - %rand;
					GameBase::setRotation(%damagedClient, Vector::add(GameBase::getRotation(%damagedClient), "0 0 " @ %rand));
				}
			}
	  		if (%value)
			{
				%value = $DamageScale[%armor, %type] * %value * %friendFire;
            		%dlevel = GameBase::getDamageLevel(%this) + %value;
		            %spillOver = %dlevel - %armor.maxDamage;
				GameBase::setDamageLevel(%this,%dlevel);
				%flash = Player::getDamageFlash(%this) + %value * 2;
				if (%flash > 0.75) 
					%flash = 0.75;
				Player::setDamageFlash(%this,%flash);
				//If player not dead then play a random hurt sound
				if(!Player::isDead(%this))
				{ 
					if(%damagedClient.lastDamage < getSimTime())
					{
						%sound = radnomItems(3,injure1,injure2,injure3);
						playVoice(%damagedClient,%sound);
						%damagedClient.lastdamage = getSimTime() + 1.5;
					}
				}
				else //the player died
				{
	            		if(%spillOver > 0.5 && (%type== $ExplosionDamageType || %type == $ShrapnelDamageType || %type== $MortarDamageType|| %type == $MissileDamageType))
					{
			 			Player::trigger(%this, $WeaponSlot, false);
						%weaponType = Player::getMountedItem(%this,$WeaponSlot);
						if(%weaponType != -1)
							Player::dropItem(%this,%weaponType);
		                		Player::blowUp(%this);
					}
					else
					{
						if ((%value > 0.40 && (%type== $ExplosionDamageType || %type == $ShrapnelDamageType || %type== $MortarDamageType || %type == $MissileDamageType )) || (Player::getLastContactCount(%this) > 6) )
						{
				  			if(%quadrant == "front_left" || %quadrant == "front_right") 
									%curDie = $PlayerAnim::DieBlownBack;
							else
								%curDie = $PlayerAnim::DieForward;
						}
						else if( Player::isCrouching(%this) ) 
							%curDie = $PlayerAnim::Crouching;							
						else if(%vertPos=="head")
						{
							if(%quadrant == "front_left" ||	%quadrant == "front_right"	) 
								%curDie = radnomItems(2, $PlayerAnim::DieHead, $PlayerAnim::DieBack);
						  	else 
								%curDie = radnomItems(2, $PlayerAnim::DieHead, $PlayerAnim::DieForward);
						}
						else if (%vertPos == "torso")
						{
							if(%quadrant == "front_left" ) 
								%curDie = radnomItems(3, $PlayerAnim::DieLeftSide, $PlayerAnim::DieChest, $PlayerAnim::DieForwardKneel);
							else if(%quadrant == "front_right") 
								%curDie = radnomItems(3, $PlayerAnim::DieChest, $PlayerAnim::DieRightSide, $PlayerAnim::DieSpin);
							else if(%quadrant == "back_left" ) 
								%curDie = radnomItems(4, $PlayerAnim::DieLeftSide, $PlayerAnim::DieGrabBack, $PlayerAnim::DieForward, $PlayerAnim::DieForwardKneel);
							else if(%quadrant == "back_right") 
								%curDie = radnomItems(4, $PlayerAnim::DieGrabBack, $PlayerAnim::DieRightSide, $PlayerAnim::DieForward, $PlayerAnim::DieForwardKneel);
						}
						else if (%vertPos == "legs")
						{
							if(%quadrant == "front_left" ||	%quadrant == "back_left") 
								%curDie = $PlayerAnim::DieLegLeft;
							if(%quadrant == "front_right" ||	%quadrant == "back_right") 
								%curDie = $PlayerAnim::DieLegRight;
						}
						Player::setAnimation(%this, %curDie);
					}
					if(%type == $ImpactDamageType && %object.clLastMount != "")  
						%shooterClient = %object.clLastMount;
					Client::onKilled(%damagedClient,%shooterClient, %type);
				}
			}
		}
	}
}

function radnomItems(%num, %an0, %an1, %an2, %an3, %an4, %an5, %an6)
{
	return %an[floor(getRandom() * (%num - 0.01))];
}

function Player::onCollision(%this,%object)
{
	if (Player::isDead(%this)) {
		if (getObjectType(%object) == "Player") {
			// Transfer all our items to the player
			%sound = false;
			%max = getNumItems2();
			for (%i = 0; %i < %max; %i = %i + 1) {
				%count = Player::getItemCount(%this,%i);
				if (%count) {
					%delta = Item::giveItem(%object,getItemData(%i),%count);
					if (%delta > 0) {
						Player::decItemCount(%this,%i,%delta);
						%sound = true;
					}
				}
			}
			if (%sound) {
				// Play pickup if we gave him anything
				playSound(SoundPickupItem,GameBase::getPosition(%this));
			}
		}
	}
}

function Player::getHeatFactor(%this)
{
	// Hack to avoid turret turret not tracking vehicles.
	// Assumes that if we are not in the player we are
	// controlling a vechicle, which is not always correct
	// but should be OK for now.
	%client = Player::getClient(%this);
	if (Client::getControlObject(%client) != %this)
		return 1.0;

   %time = getIntegerTime(true) >> 5;
   %lastTime = Player::lastJetTime(%this) >> 10;

   if ((%lastTime + 1.5) < %time) {
      return 0.0;
   } else {
      %diff = %time - %lastTime;
      %heat = 1.0 - (%diff / 1.5);
      return %heat;
   }
}

function Player::jump(%this,%mom)
{
   %cl = GameBase::getControlClient(%this);
   if(%cl != -1)
   {
      %vehicle = Player::getMountObject (%this);
		%this.lastMount = %vehicle;
		%this.newMountTime = getSimTime() + 3.0;
		Player::setMountObject(%this, %vehicle, 0);
		Player::setMountObject(%this, -1, 0);
		Player::applyImpulse(%pl,%mom);
		playSound (GameBase::getDataName(%this).dismountSound, GameBase::getPosition(%this));
   }
}


//----------------------------------------------------------------------------

function remoteKill(%client)
{
   if(!$matchStarted)
      return;

   %player = Client::getOwnedObject(%client);
   if(%player != -1 && getObjectType(%player) == "Player" && !Player::isDead(%player))
   {
	playNextAnim(%client);
	Player::kill(%client);
	Client::onKilled(%client,%client);
   }
}

$animNumber = 25;	 
function playNextAnim(%client)
{
	if($animNumber > 36) 
		$animNumber = 25;		
	Player::setAnimation(%client,$animNumber++);
}

function Client::takeControl(%clientId, %objectId)
{
   // remote control
   if(%objectId == -1)
   {
      //echo("objectId = " @ %objectId);
      return;
   }
   if(GameBase::getTeam(%objectId) != Client::getTeam(%clientId))
   {
      //echo(GameBase::getTeam(%objectId) @ " " @ Client::getTeam(%clientId));
      return;
   }
   if(GameBase::getControlClient(%objectId) != -1)
   {
      echo("Ctrl Client = " @ GameBase::getControlClient(%objectId));
      return;
   }
	%name = GameBase::getDataName(%objectId);
	if(%name != CameraTurret && %name != DeployableTurret && %name != FlameTurret)
   {
	   if(!GameBase::isPowered(%objectId)) 
		{
	      // echo("Turret " @ %objectId @ " not powered.");
	      return;
		}
   }
   if(!(Client::getOwnedObject(%clientId)).CommandTag && GameBase::getDataName(%objectId) != CameraTurret &&
      !$TestCheats) {
		Client::SendMessage(%clientId,0,"Must be at a Command Station to control turrets");
   		return;
   }
   if(GameBase::getDamageState(%objectId) == "Enabled") {
   	Client::setControlObject(%clientId, %objectId);
   	Client::setGuiMode(%clientId, $GuiModePlay);
	}
}

function remoteCmdrMountObject(%clientId, %objectIdx)
{
   Client::takeControl(%clientId, getObjectByTargetIndex(%objectIdx));
}

function checkControlUnmount(%clientId)
{
   %ownedObject = Client::getOwnedObject(%clientId);
   %ctrlObject = Client::getControlObject(%clientId);
   if(%ownedObject != %ctrlObject)
   {
      if(%ownedObject == -1 || %ctrlObject == -1)
         return;
      if(getObjectType(%ownedObject) == "Player" && Player::getMountObject(%ownedObject) == %ctrlObject)
         return;
      Client::setControlObject(%clientId, %ownedObject);
   }
}

