//============================================================================
// control/server/players/player.cs
//  Copyright (c) 2003, 2006 by Kenneth C.  Finney.
//============================================================================
exec("~/data/models/avatars/hero/hero.cs");


datablock AudioProfile(DeathCrySound)///////***KCF***24/09/2006 8:33PM
{
   fileName = "~/data/sound/orc_death.ogg";
   description = AudioClose3d;
   preload = true;
};

datablock AudioProfile(PainCrySound)///////***KCF***24/09/2006 8:33PM
{
   fileName = "~/data/sound/orc_pain.ogg";
   description = AudioClose3d;
   preload = true;
};

datablock AudioProfile(FootLightSoftSound)///////***KCF***24/09/2006 8:33PM
{
   filename    = "~/data/sound/footstep1.ogg";
   description = AudioClosest3d;
   preload = true;
};
datablock AudioProfile(FootLightHardSound)///////***KCF***24/09/2006 8:33PM
{
   filename    = "~/data/sound/footstep1.ogg";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(FootLightMetalSound)///////***KCF***24/09/2006 8:33PM
{
   filename    = "~/data/sound/footstep1.ogg";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(FootLightSnowSound)///////***KCF***24/09/2006 8:33PM
{
   filename    = "~/data/sound/footstep1.ogg";
   description = AudioClosest3d;
   preload = true;
};
datablock AudioProfile(FootLightShallowSplashSound)///////***KCF***24/09/2006 8:33PM
{
   filename    = "~/data/sound/footstep1.ogg";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(FootLightWadingSound)///////***KCF***24/09/2006 8:33PM
{
   filename    = "~/data/sound/footstep1.ogg";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(FootLightUnderwaterSound)///////***KCF***24/09/2006 8:33PM
{
   filename    = "~/data/sound/footstep1.ogg";
   description = AudioClosest3d;
   preload = true;
};


datablock AudioProfile(Rustle1)
{
   fileName = "~/data/sound/rustle1.wav";
   description = AudioClosest3d;
   preload = true;
};

function serverCmdStartRustle(%client)
{
  %client.player.schedule(200,playRustle);
  %client.player.rustleon= true;
}
function serverCmdStopRustle(%client)
{
  %client.player.rustleon = false;
}

function Player::playRustle( %this )
{
  if(%this.rustleon)
  {
    serverPlay3D(Rustle1,%this.getTransform());
    %this.schedule(500,playRustle);
  }
}

datablock PlayerData(MaleAvatar)
{
   className = HeroClass;
   shapeFile = "~/data/models/avatars/hero/hero.dts";
   emap = true;
   renderFirstPerson = false;
   cameraMaxDist = 3;
   mass = 100;
   density = 10;
   drag = 0.1;
   maxdrag = 0.5;
   maxDamage = 100;
   maxEnergy =  100;
   maxForwardSpeed = 15;
   maxBackwardSpeed = 10;
   maxSideSpeed = 12;
   minJumpSpeed = 20;
   maxJumpSpeed = 30;
   runForce = 1000;
   jumpForce = 1000;
   runSurfaceAngle  = 40;
   jumpSurfaceAngle = 30;
   runEnergyDrain = 0.01;
   minRunEnergy = 1;
   jumpEnergyDrain = 20;
   minJumpEnergy = 20;
   recoverDelay = 30;
   recoverRunForceScale = 1.2;
   minImpactSpeed = 20;
   speedDamageScale = 2.0;
   repairRate = 0.05;
   maxInv[Copper] = 9999;
   maxInv[Silver] = 99;
   maxInv[Gold] = 9;
   maxInv[Crossbow] = 1;
   maxInv[CrossbowAmmo] = 20;
    FootSoftSound        = FootLightSoftSound;///***KCF***24/09/2006 11:54PM
   FootHardSound        = FootLightHardSound;///***KCF***24/09/2006 11:54PM
   FootMetalSound       = FootLightMetalSound;///***KCF***24/09/2006 11:54PM
   FootSnowSound        = FootLightSnowSound;///***KCF***24/09/2006 11:54PM
   FootShallowSound     = FootLightShallowSplashSound;///***KCF***24/09/2006 11:54PM
   FootWadingSound      = FootLightWadingSound;///***KCF***24/09/2006 11:54PM
   FootUnderwaterSound  = FootLightUnderwaterSound;///***KCF***24/09/2006 11:54PM
   maxInv[Tommygun] = 1;///***KCF***24/09/2006 11:54PM
   maxInv[TommygunAmmo] = 20;///***KCF***24/09/2006 11:54PM


};
//============================================================================
// Avatar Datablock methods
//============================================================================
function HeroClass::onAdd(%this,%obj)
{
   %obj.mountVehicle = false;

   // Default dynamic Avatar stats
   %obj.setRechargeRate(0.01);
   %obj.setRepairRate(%this.repairRate);
}
function HeroClass::onRemove(%this, %obj)
{
   %client = %obj.client;
   if (%client.player == %obj)
   {
      %client.player = 0;
   }
}
function HeroClass::onCollision(%this,%obj,%col,%vec,%speed)
{
  %obj_state = %obj.getState();
  %col_className = %col.getClassName();
  %col_dblock_className = %col.getDataBlock().className;
  %colName = %col.getDataBlock().getName();
  if ( %obj_state $= "Dead")
    return;
  if (%col_className $= "Item" || %col_className $= "Weapon" )
  {
    %obj.pickup(%col);
  }
  
  %this = %col.getDataBlock();///////***KCF***24/09/2006 8:33PM
   if ( %this.className $= "Car" )///////***KCF***24/09/2006 8:33PM
   {
       %node = 0;   // Find next available seat
       %col.mountObject(%obj,%node);
       %obj.mVehicle = %col;
   }
}
//============================================================================
// MaleAvatar (ShapeBase) class methods
//============================================================================
function MaleAvatar::onImpact(%this,%obj,%collidedObject,%vec,%vecLen)
{
   %obj.Damage(0, VectorAdd(%obj.getPosition(),%vec),
      %vecLen * %this.speedDamageScale, "Impact");
}
function MaleAvatar::Damage(%this, %obj, %sourceObject, %position, %damage, %damageType)
{
   if (%obj.getState() $= "Dead")
      return;
   %obj.applyDamage(%damage);
   %location = "Body";
   %client = %obj.client;
   %sourceClient = %sourceObject ? %sourceObject.client : 0;
   if (%obj.getState() $= "Dead")
   {
      %client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
   }
}
function MaleAvatar::onDamage(%this, %obj, %delta)
{
   if (%delta > 0 && %obj.getState() !$= "Dead")
   {
      // Increment the flash based on the amount.
      %flash = %obj.getDamageFlash() + ((%delta / %this.maxDamage) * 2);
      if (%flash > 0.75)
         %flash = 0.75;

      if (%flash > 0.001)
      {
        %obj.setDamageFlash(%flash);
      }
      %obj.setRechargeRate(0.01);
      %obj.setRepairRate(0.01);
   }
}
function MaleAvatar::onDisabled(%this,%obj,%state)
{
   %obj.playDeathCry();
   %obj.playDeathAnimation();
   %obj.clearDamageDt();
   %obj.setRechargeRate(0);
   %obj.setRepairRate(0);
   %obj.setImageTrigger(0,false);
   %obj.schedule(5000, "startFade", 5000, 0, true);
   %obj.schedule(10000, "delete");
}
datablock AudioProfile(Insult1)
{
   fileName = "~/data/sound/insult1.wav";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(Insult2)
{
   fileName = "~/data/sound/taunt2.ogg";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(Insult3)
{
   fileName = "~/data/sound/taunt3.ogg";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(Insult4)
{
   fileName = "~/data/sound/taunt4.ogg";
   description = AudioClose3d;
   preload = true;
};

function serverCmdHurlInsult(%client)
{
	%n=getRandom(3) + 1;
  serverPlay3D("Insult" @ %n, %client.player.getTransform());
}



function Player::playDeathCry( %this )
{
   %this.playAudio(0,DeathCrySound);
}

function Player::playPain( %this )
{
   %this.playAudio(0,PainCrySound);
}
function serverCmdSuicide(%client)
{
	trace(1);
   if (isObject(%client.player))
      %client.player.kill("Suicide");
      
   return;///***KCF***-21-SEP-2006 
}



////////////////////////
///***KCF***24/09/2006 8:34PM
function MaleAvatar::onMount(%this,%obj,%vehicle,%node)
{
   %obj.setTransform("0 0 0 0 0 1 0");
   %obj.setActionThread(%vehicle.getDatablock().mountPose[%node]);
   if (%node == 0)
   {
      %obj.setControlObject(%vehicle);
      %obj.lastWeapon = %obj.getMountedImage($WeaponSlot);
      %obj.unmountImage($WeaponSlot);
      %db = %vehicle.getDatablock();
   }
}

function MaleAvatar::onUnmount( %this, %obj, %vehicle, %node )
{
   %obj.mountImage(%obj.lastWeapon, $WeaponSlot);
}

function MaleAvatar::doDismount(%this, %obj, %forced)
{
   // This function is called by the game engine when the jump trigger
   // is true while mounted

   // Position above dismount point
   %pos    = getWords(%obj.getTransform(), 0, 2);
   %oldPos = %pos;

   %vec[0] = " 1  1  1";
   %vec[1] = " 1  1  1";
   %vec[2] = " 1  1  -1";
   %vec[3] = " 1  0  0";
   %vec[4] = "-1  0  0";

   %impulseVec  = "0 0 0";
   %vec[0] = MatrixMulVector( %obj.getTransform(), %vec[0]);

   // Make sure the point is valid
   %pos = "0 0 0";
   %numAttempts = 5;
   %success     = -1;

   for (%i = 0; %i < %numAttempts; %i++)
   {
      %pos = VectorAdd(%oldPos, VectorScale(%vec[%i], 3));
      if (%obj.checkDismountPoint(%oldPos, %pos))
      {
         %success = %i;
         %impulseVec = %vec[%i];
         break;
      }
   }
   if (%forced && %success == -1)
      %pos = %oldPos;
   %obj.unmount();
   %obj.setControlObject(%obj);
   %obj.mountVehicle = false;

   // Position above dismount point
   %obj.setTransform(%pos);
   %obj.applyImpulse(%pos, VectorScale(%impulseVec, %obj.getDataBlock().mass));
}

function Player::playDeathAnimation(%this,%deathIdx)
{
   %this.setActionThread("death1");
}
