//-----------------------------------------------------------------------------
// Torque Game Engine
//
// Copyright (c) 2002 ToRK
// Portions Copyright (c) 2001-2002 GarageGames.Com
// Portions Copyright (c) 2001-2002 by Sierra Online, Inc.
//-----------------------------------------------------------------------------

$numTeleports = 0;

datablock TriggerData(MultiTeleportTrigger)
{
   tickPeriodMS = 500;
};

function MultiTeleportTrigger::onEnterTrigger(%data, %obj, %colObj)
{
   if($numTeleports == 0)
   {
      // search for Triggers by their name once, so every trigger
      // which has "TeleportTrigger" in its name is counted
      $numTeleports = getMultiTriggerCount("TeleportTrigger");
      echo("$numTeleports:" SPC $numTeleports);
   }

   %checkname = %obj.getName();
   %client = %colObj.client;
   if(!%client)
   {
      echo("not a client!");
      return;
   }
   // if the player didn't recently beam over here... otherwise
   // he would be looping around between the two, I guess...
   if(%checkname !$= $currMultiTeleTrigger)
   {
      // pick random number, the teleport names start from 1,
      // so if the random number is zere, simply take the last teleport:
      %rand = getRandom($numTeleports) == 0 ? 1 : $numTeleports;
      %target = "TeleportTrigger" @ %rand;
      // we don't want to stay where we are...
      if(%target $= %checkName)
      {
         // ... so increase or decrease the random number by 1
         %rand = %rand+1 > $numTeleports ? %rand-1 : %rand+1;
         %target = "TeleportTrigger" @ %rand;
      }
      echo("*** TELEPORT TARGET:" SPC %target);
      CommandToClient(%client,'bottomprint',"Teleporter initializing... good luck... buahahaha!!",2,10);
      $teleSched = schedule(2000,0,"goScotty",%client,%target);
     	$teleSound = serverPlay3D(TeleportBuzz,%client.player.getTransform());
      %client.player.setCloaked(true);
      // save the target - until the teleported client leaves it, then reset
      $currMultiTeleTrigger = %target;
   }
}

function MultiTeleportTrigger::onLeaveTrigger(%data, %obj, %colObj)
{
   %checkname = %obj.getName();
   %client = %colObj.client;
   cancel($teleSched);
   alxStop($teleSound);
   %client.player.setCloaked(false);
   // if the player leaves the target trigger,
   // he can use it again, too... so reset the global var
   if(%checkname $= $currMultiTeleTrigger)
   {
      $currMultiTeleTrigger = "";
   }
}
function MultiTeleportTrigger::onTickTrigger(%data, %obj)
{
}
// *********************************
// Helper functions
// *********************************

// do the teleport
function goScotty(%client, %target)
{
   echo("goScotty called!");
   // beam me up!
   commandToServer('TeleportPlayer', %client, %target);
}

// find the number of teleport triggers by name comparison
function getMultiTriggerCount(%name)
{
   %dataGroup = "MissionGroup";
   %triggerCount = 0;
   for(%i = 0; %i < %dataGroup.getCount(); %i++)
   {
      %obj = %dataGroup.getObject(%i);
      if(%obj.getClassName() !$= "Trigger")
      {
         // no trigger!
         continue;
      }
      if((strStr(%obj.getDatablock().getName(), %name) != -1) && isObject(%obj))
      {
         echo(%i SPC "Found Trigger:" SPC %obj.getName());
         %triggerCount++;
      }
   }
   return %triggerCount;
}


