
//--------------------------------------------------------------------------
// Force fields:
//
//  accept the following commands:
//    open()
//    close()
//
//--------------------------------------------------------------------------

datablock ForceFieldBareData(defaultForceFieldBare)
{
   fadeMS           = 1000;
   baseTranslucency = 0.30;
   powerOffTranslucency = 0.0;
   teamPermiable    = false;
   otherPermiable   = false;
   color            = "0.0 0.55 0.99";
   powerOffColor    = "0.0 0.0 0.0";
   targetNameTag    = 'Force Field';
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock ForceFieldBareData(defaultTeamSlowFieldBare)
{
   fadeMS           = 1000;
   baseTranslucency = 0.3;
   powerOffTranslucency = 0.0;
   teamPermiable    = true;
   otherPermiable   = false;
   color            = "0.28 0.89 0.31";
   powerOffColor    = "0.0 0.0 0.0";
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock ForceFieldBareData(defaultAllSlowFieldBare)
{
   fadeMS           = 1000;
   baseTranslucency = 0.30;
   powerOffTranslucency = 0.0;
   teamPermiable    = true;
   otherPermiable   = true;
   color            = "1.0 0.4 0.0";   
   powerOffColor    = "0.0 0.0 0.0";
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock ForceFieldBareData(defaultNoTeamSlowFieldBare)
{
   fadeMS           = 1000;
   baseTranslucency = 0.30;
   powerOffTranslucency = 0.0;
   teamPermiable    = false;
   otherPermiable   = true;
   color            = "1.0 0.0 0.0";
   powerOffColor    = "0.0 0.0 0.0";
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock ForceFieldBareData(defaultSolidFieldBare)
{
   fadeMS           = 1000;
   baseTranslucency = 0.30;
   powerOffTranslucency = 0.0;
   teamPermiable    = false;
   otherPermiable   = false;
   color            = "1.0 0.0 0.0";
   powerOffColor    = "0.0 0.0 0.0";
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock ForceFieldBareData(FreezeForceFieldBare)
{
   fadeMS               = 1000;
   baseTranslucency     = 0.15;
   powerOffTranslucency = 0.0;
   teamPermiable        = false;
   otherPermiable       = false;
   color                = "0.75 1 1";
   powerOffColor        = "0.0 0.0 0.0";
   targetNameTag        = 'Force Field';
   targetTypeTag        = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

function ForceFieldBare::onTrigger(%this, %triggerId, %on)
{
   // Default behavior for a field:
   //  if triggered:   go to open state (last waypoint)
   //  if untriggered: go to closed state (first waypoint)

   if (%on == 1) {
      %this.triggerCount++;
   } else {
      if (%this.triggerCount > 0)
         %this.triggerCount--;
   }

   if (%this.triggerCount > 0) {
      %this.open();
   } else {
      %this.close();
   }
}

function ForceFieldBareData::gainPower(%data, %ForceField)
{
   Parent::gainPower(%data, %ForceField);
   %ForceField.close();

   %PZone = %ForceField.PZone;
   if (%PZone !$= "")
   {
      %PZone.activate();

      if (%ForceField.getDataBlock().teamPermiable == true)
         KillAllPlayersWithinZone(%data, %ForceField, %Team);
      else
         KillAllPlayersWithinZone(%data, %ForceField);
   }
}

function ForceFieldBareData::losePower(%data, %ForceField)
{
   Parent::gainPower(%data, %ForceField);
   %ForceField.open();

   %PZone = %ForceField.PZone;
   if (%PZone !$= "")
      %PZone.deactivate();
}
 
function ForceFieldBareData::onAdd(%data, %ForceField)
{
   Parent::onAdd(%data, %ForceField);

   %PZone = new PhysicalZone()
   {
      position = %ForceField.position;
      rotation = %ForceField.rotation;
      scale    = %ForceField.scale;
      polyhedron = "0.000000 1.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 -1.0000000 0.0000000 0.0000000 0.0000000 1.0000000";
      velocityMod  = 0.4;
      gravityMod   = 1.0;
      appliedForce = "0 0 0";
      ForceField = %ForceField;
   };
   %PZoneGroup = nameToID("MissionCleanup/PZones");

   if(%PZoneGroup <= 0)
   {
      %PZoneGroup = new SimGroup("PZones");
      MissionCleanup.add(%PZoneGroup);
   }

   %PZoneGroup.add(%PZone);

   %ForceField.PZone = %PZone;
}

function killAllPlayersWithinZone(%Data, %ForceField, %Team)
{
   // Ignore bad Requests
   if (%ForceField == 0 || %ForceField $= "")
      return;

   %i = 0;
   %Count = ClientGroup.getCount();

   while (%i < %Count)
   {
      %Client = ClientGroup.getObject(%i);

      // Is it a player and not an observer
      if (isObject(%Client.player) == false)
      {
         %i++;
         continue;
      }

      // If they are on the team we are trying to protect
      if (%Team > 0 && %Client.Team == %Team)
      {
         %i++;
         continue;
      }

      %PlayerForceField = %Client.player.isInForceField();
      if (%PlayerForceField $= "0")
      {
         %i++;
         continue;
      }

      if (%PlayerForceField == %ForceField)
      {
         %Client.player.blowup(); 
         %Client.player.scriptkill($DamageType::Electricity);
      }

      %i++;
   }
}

datablock ForceFieldBareData(defaultNoTeamLavaLightField)
{
   fadeMS           = 1000;
   baseTranslucency = 1;
   powerOffTranslucency = 0;
   teamPermiable    = false;
   otherPermiable   = false;
   color            = "1.0 0.4 0.0";
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock ForceFieldBareData(baseCustomField)
{
   fadeMS           = 1000;
   baseTranslucency = 0.3;
   powerOffTranslucency = 0.0;
   teamPermiable    = true;
   otherPermiable   = true;
   color            = "0.99 0.0 0.99";
   powerOffColor    = "0.0 0.0 0.0";
   targetNameTag    = 'Force Field';
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/forcef1";
   texture[1] = "skins/forcef2";
   texture[2] = "skins/forcef3";
   texture[3] = "skins/forcef4";
   texture[4] = "skins/forcef5";

   framesPerSec = 10;
   numFrames = 5;
   scrollSpeed = 15;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock ForceFieldBareData(CustomFieldTeam) : baseCustomField
{
   otherPermiable   = false;
   color            = "0.28 0.89 0.31";
};

datablock ForceFieldBareData(CustomFieldAll) : baseCustomField
{
   color            = "1.0 0.4 0.0";   
};

datablock ForceFieldBareData(CustomFieldNoTeam) : baseCustomField
{
   teamPermiable    = false;
   color            = "1.0 0.0 0.0";
};

function baseCustomField::onAdd(%data, %ForceField)
{
   Parent::onAdd(%data, %ForceField);

   %PZone = %ForceField.PZone;

   if(%PZone != -1)
   {
      %PZone.velocityMod = %ForceField.velocityMod;
      %PZone.gravityMod = %ForceField.gravityMod;
      %PZone.appliedForce = %ForceField.appliedForce;
   }
}

$baseCustomFieldDefined = true;

datablock ForceFieldBareData(StasisForceField)
{
   fadeMS           = 1000;
   baseTranslucency = 0.9;
   powerOffTranslucency = 0.0;
   teamPermiable    = false;
   otherPermiable   = false;
   color            = "1.0 1.0 1.0";
   powerOffColor    = "0.0 0.0 0.0";
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/Enrgtubes0006.png";

   framesPerSec = 1;
   numFrames = 1;
   scrollSpeed = 0.5;
   umapping = 1.0;
   vmapping = 0.15;
};

datablock TriggerData(stasisTrigger)
{
   tickPeriodMS = 50;
};

function stasisTrigger::onEnterTrigger(%data, %obj, %colObj)
{
   messageClient( %colObj.client, 'MsgClient', '\c0CB is idling. Please leave him in peace.');
}

function stasisTrigger::onLeaveTrigger(%data, %obj, %colObj)
{
   return;
}

function stasisTrigger::onTickTrigger(%data, %trigger)
{
   %group = %trigger.getGroup();
   if (!%group)
      return;

   %tick = false;
   %i = 0;
   %Count = %trigger.getNumObjects();
   while (%i < %Count)
   {
      if (%trigger.getObject(%i).team == %trigger.team)
      {
         %tick = true;
         break;
      }

      %i++;
   }

   if (%tick == true)
   {
      %i = 0;
      %Count = %group.getCount();
      while ( %i < %Count)
      {
         %object = %group.getObject(%i);
         %object.onTriggerTick(%trigger);
         %i++;
      }
   }
}

datablock ForceFieldBareData(TransferFieldForceField)
{
   fadeMS           = 1000;
   baseTranslucency = 0.9;
   powerOffTranslucency = 0.0;
   teamPermiable    = true;
   otherPermiable   = true;
   color            = "0.73 0.09 1.0";
   powerOffColor    = "0.0 0.0 0.0";
   targetTypeTag    = 'ForceField'; 

   texture[0] = "skins/nexred15.png";

   framesPerSec = 1;
   numFrames = 1;
   scrollSpeed = -1;
   umapping = 1.0;
   vmapping = 0.15;
};

function TransferFieldForceField::onAdd(%data, %ForceField)
{
   Parent::onAdd(%data, %ForceField);

   if(%ForceField.velocityMod $= "")
      %ForceField.velocityMod = 1.0;
   
   if(%ForceField.gravityMod $= "")
      %ForceField.gravityMod = 0.0;

   %PZone = %ForceField.PZone;

   $PZoneCount++;

   if(%PZone !$= "")
   {
      %PZone.velocityMod = %ForceField.velocityMod;
      %PZone.gravityMod = %ForceField.gravityMod;
      %PZone.appliedForce = %ForceField.appliedForce;
      $PZone[$PZCount] = %PZone;
      $PZCount++;
   }
}