// FILENAME:	DM_Volcano.cs
//
// AUTHORS:  	Gen. Powio™ [MIB]
//------------------------------------------------------------------------------

$missionName = "FTAG_Volcano";
$sinkSpeed = 0.3;
$minEruptTime = 120;
$maxEruptTime = 180;
$volcano = true;
$rocks = 6;
$rockFireRate = 0.25;

exec("multiplayerStdLib.cs");
exec("DMstdLib.cs");

function setDefaultMissionOptions()
{
	$server::TeamPlay = false;
	$server::AllowDeathmatch = true;
	$server::AllowTeamPlay = false;	

   // what can the client choose for a team
	$server::AllowTeamRed = false;
	$server::AllowTeamBlue = false;
	$server::AllowTeamYellow = false;
	$server::AllowTeamPurple = false;

   // what can the server admin choose for available teams
   $server::disableTeamRed = false;
   $server::disableTeamBlue = false;
   $server::disableTeamYellow = false;
   $server::disableTeamPurple = false;

   // Time limit
   $server::TimeLimit = 20;
}

function onMissionStart()
{
	venusSounds();
     
      $startTime = getCurrentTime(); 
 
      $flag = getobjectId("MissionGroup\\special\\flag");
      $CenterNav = getobjectId("MissionGroup\\Navs\\CenterNav");
      $gamestart = false;
      $triggeron = true;
      $flagholder = "nobody";

      $healRate = 100;   
	$ammoRate = 3;
   	$padWaitTime = 45;
  	$zenWaitTime = 90;
}

function vehicle::ondestroyed(%destroyed,%destroyer)
{
   %player = playermanager::vehicleIdtoPlayerNum(%destroyer);
   %player2 = playermanager::vehicleIdtoPlayerNum(%destroyed);
   %player2.spawn=false;
   // left over from missionStdLib.cs
   vehicle::onDestroyedLog(%destroyed, %destroyer);
   
   // this is weird but %destroyer isn't necessarily a vehicle
   if(%d.Damage == Burning)
   {
      %r = randomInt(0,2);
      if(%r == 0)
      {
         say(0,%p,getHUDName(%d)@"<F5> slipped into the volcano and was promptly devoured by the flames.","sfx_lava.wav");
      }
      else if(%r == 1)
      {
         say(0,%p,getHUDName(%d)@"<F5> took a step into the magma and never returned.","sfx_lava.wav");
      }
      else
      {
         say(0,%p,getHUDName(%d)@"<F5> took a nasty fall into the magma but couldn't make it out in time.","sfx_lava.wav");
      }
   }
   else if(%d.Damage == FireRock)
   {
      %r = randomInt(0,2);
      if(%r == 0)
      {
         say(0,%p,getHUDName(%d)@"<F5> was whacked over the head by a stray fire rock.","sfx_lava.wav");
      }
      else if(%r == 1)
      {
         say(0,%p,getHUDName(%d)@"<F5> couldn't make the play when the fire rock came crashing down.","sfx_lava.wav");
      }
      else
      {
         say(0,%p,getHUDName(%d)@"<F5> exploded just the fire rock that exploded on them.","sfx_lava.wav");
      }
   }
   else if(%m != "")
   {
      say( 0, 0, %m);
   }
   
   // enforce the rules  
   if((%destroyed == $flagholder) && (%player.spawn == true))
   {
      $flagholder = %destroyer;
      setvehiclespecialidentity(%destroyer, on, blue);
      say("everybody",1, "<f5>" @ %destroyer.name @ " has the flag!");
   }
   else if((%destroyed == $flagholder) && (%player.spawn == false))
   {
      $flagholder = "nobody";
      setshapevisibility($flag, true);
      $triggeron = true;
      say("everybody",1,"<f5>The flag has been returned to Nav Alpha!");
   }
   %v.Ready = "";
}

function vehicle::onDestroyed(%d,%r)
{
   %p = playerManager::vehicleIdToPlayerNum(%d);
   vehicle::onDestroyedLog(%d,%r);
   %m = getFancyDeathMessage(getHUDName(%d),getHUDName(%r));
   if(%d.Damage == Burning)
   {
      %r = randomInt(0,2);
      if(%r == 0)
      {
         say(0,%p,getHUDName(%d)@"<F5> slipped into the volcano and was promptly devoured by the flames.","sfx_lava.wav");
      }
      else if(%r == 1)
      {
         say(0,%p,getHUDName(%d)@"<F5> took a step into the magma and never returned.","sfx_lava.wav");
      }
      else
      {
         say(0,%p,getHUDName(%d)@"<F5> took a nasty fall into the magma but couldn't make it out in time.","sfx_lava.wav");
      }
   }
   else if(%d.Damage == FireRock)
   {
      %r = randomInt(0,2);
      if(%r == 0)
      {
         say(0,%p,getHUDName(%d)@"<F5> was whacked over the head by a stray fire rock.","sfx_lava.wav");
      }
      else if(%r == 1)
      {
         say(0,%p,getHUDName(%d)@"<F5> couldn't make the play when the fire rock came crashing down.","sfx_lava.wav");
      }
      else
      {
         say(0,%p,getHUDName(%d)@"<F5> exploded just the fire rock that exploded on them.","sfx_lava.wav");
      }
   }
   else if(%m != "")
   {
      say( 0, 0, %m);
   }
   %v.Ready = "";
}

function vehicle::onAdd(%v)
{
   %v.Damage = "";
   %v.Ready = True;

   %vehicleId.name = getHUDName(%vehicleId);
   %player = playerManager::vehicleIdToPlayerNum(%vehicleId); 
   %player.spawn = true;
 
   %now = getCurrentTime(); 
   %totalTime = ($server::TimeLimit * 60); 
   %timeLeft = (%totalTime - (%now - $startTime));
   setHudTimer(%timeLeft, -1, "Mission Time Left", 1, %player); 
}

function player::onAdd(%player)
{
   messageBox(%player, "This map requires that you download a new terrain to play. It is available for download in the \"MIB Terrain Pack\" on the MIB website at www.starsiegemeninblack.cjb.net");
   say(%player, 0, "Welcome to Flag Tag! Check the game info tab for the rules. You can download FTAG_Volcano & other terrain-based missions made by Gen. Powio™ [M.I.B.] in the \"MIB Terrain Pack\" on the MIB Website at www.starsiegemeninblack.cjb.net.");
   %player.score = 0;
}

function player::onRemove(%this)
{
   if(playerManager::getPlayerCount()==2)
   {
      messageBox("Everybody", "All other players have left the game. You are the winner!");
      schedule("missionEndConditionMet();", 5);
   }
   else if(playerManager::getPlayerCount()==1)
   {
      schedule("missionEndConditionMet();", 1);
   }
}

function flag::trigger::onEnter(%this,%object)
{
   if(($triggeron == true) && ($gamestart == false) && (playerManager::getPlayerCount()>=2))
   {      
      setvehiclespecialidentity(%object, on, blue);
      setshapevisibility($flag, false);
      $gamestart = true;
      $triggeron = false;
      $flagholder = %object;
      scoreflagholder();
      say("everybody",1, "<f5>" @ %object.name @ " has the flag!");
   }
   else if(($triggeron == true) && ($gamestart == false) && (playerManager::getPlayerCount()<2))
   {
      say("everybody",1,"<f5>There must be two players in the server before you can play!");
   }
   else if(($triggeron == true) && ($gamestart == true))
   {
      setvehiclespecialidentity(%object, on, blue);
      setshapevisibility($flag, false);
      $triggeron = false;
      $flagholder = %object;
      say("everybody",1, "<f5>" @ %object.name @ " has the flag!");
   } 
}

function scoreflagholder()
{
   %player = playerManager::vehicleIdToPlayerNum($flagholder);
   %player.score++;
   %distance = getdistance($flagholder, $CenterNav);
   schedule("scoreflagholder();",1);
} 

function ZenHeal::trigger::onEnter(%this, %object)
{
   	Zen::onEnter(%this, %object, *IDMULT_CHAT_HEALPAD, true, true);  
}
function ZenHeal::trigger::onContact(%this, %object)
{
   	Zen::work(%this, %object, $healRate, 0, $padWaitTime, true); 
}

function ZenAmmo::trigger::onEnter(%this, %object)
{
   	Zen::onEnter(%this, %object, *IDMULT_CHAT_AMMOPAD, true, true);  
}

function ZenAmmo::trigger::onContact(%this, %object)
{
   	Zen::work(%this, %object, 0, $ammoRate, $padWaitTime, true); 
}

function ZenAll::trigger::onEnter(%this, %object)
{
   	Zen::onEnter(%this, %object, *IDMULT_CHAT_ALLPAD, true, true);  
}

function ZenAll::trigger::onContact(%this, %object)
{
   	Zen::work(%this, %object, $healRate, $ammoRate, $zenWaitTime, true); 
}

function EnergyBoost::Trigger::onEnter(%t,%v)
{
   %p = playerManager::vehicleIdToPlayerNum(%v);
   if($EnergyBoostReady == True)
   {
      setShapeVisibility($EnergyBoost,false);
      $EnergyBoostReady = "";
      healObject(%v,999999);
      healObject(%v,999999);
      healObject(%v,999999);
      healObject(%v,999999);
      healObject(%v,999999);
      say(%p,%p,"<F4>Your vehicle has been restored.","sfx_reactorcap.wav");
      schedule("readyEnergy();",180);
   }
}

function readyEnergy()
{
   setShapeVisibility($EnergyBoost,true);
   $EnergyBoostReady = True;
}

function Burner::Trigger::onContact(%t,%v)
{
   %p = playerManager::vehicleIdToPlayerNum(%v);
   playSound(%p,"sfx_fire.wav",IDPRF_2D);
   if(%v.Ready == True)
   {
      %v.Damage = Burning;
      %x = getPosition(%v,x);
      %y = getPosition(%v,y);
      %z = getPosition(%v,z);
      setPosition(%v,%x,%y,%z - $sinkSpeed);
      damageObject(%v,400);
   }
   else
   {
      %v.Damage = "";
   }
}

function Burner::Trigger::onLeave(%t,%v)
{
   %p = playerManager::vehicleIdToPlayerNum(%v);
   %x = getPosition(%v,x);
   %y = getPosition(%v,y);
   %z = getPosition(%v,z);
   if((%v.Ready == True)&&((%x < -354)&&(%x > -574)&&(%y < -194)&&(%y > -414)&&(%z < 101)))
   {
      %v.Damage = Burning;
      damageObject(%v,999999);
      echo(getHUDname(%v)@" has sunk.");
   }
   else
   {
      setPosition(%v,%x,%y,getTerrainHeight(%x,%y));
      %v.Damage = "";
      echo(%x@" "@%y@" "@%z);
   }
}

function eruption()
{
   if($volcano == True)
   {
      playSound(0,"sfx_quake.wav",IDPRF_2D);
      schedule("playSound(0,\"sfx_quake.wav\",IDPRF_2D);",1);
      schedule("playSound(0,\"sfx_quake.wav\",IDPRF_2D);",2);
      schedule("playSound(0,\"sfx_quake.wav\",IDPRF_2D);",3);
      schedule("playSound(0,\"sfx_quake.wav\",IDPRF_2D);",4);
      schedule("playSound(0,\"sfx_quake.wav\",IDPRF_2D);",5);
      schedule("playSound(0,\"sfx_quake.wav\",IDPRF_2D);",6);
      schedule("playSound(0,\"sfx_quake.wav\",IDPRF_2D);",7);
      schedule("calRocks();",7);
   }
   %t = randomInt($minEruptTime,$maxEruptTime);
   echo("Next Eruption - "@%t@"s");
   schedule("eruption();",%t);
}

function calRocks()
{
   %w = 0;
   
   for(%i=0;%i<$Rocks;%i++)
   {
      schedule("fireRock();",%w);
      %w = %w + $rockFireRate;
   }
}


function fireRock()
{
   playSound(0,"sfx_fog.wav",IDPRF_2D);
   playSound(0,"sfx_prob.wav",IDPRF_2D);
   %x1 = randomInt(-554,-384);
   %y1 = randomInt(-384,-224);
   %x2 = randomInt(-928,0);
   %y2 = randomInt(-832,224);
   if(%x1 < %x2)
   {
      %distX = %x2 - %x1;
   }
   else if(%x2 < %x1)
   {
      %distX = %x1 - %x2;
   }
   if(%y1 < %y2)
   {
      %distY = %y2 - %y1;
   }
   else if(%y2 < %y1)
   {
      %distY = %y1 - %y2;
   }
   dropPod(%x1,%y1,100,%x2,%y2,2000);
   %hyp = sqrt((%distX * distX) + (%distY * %distY));
   %NewHyp = sqrt((%hyp * %hyp) + (1900 * 1900));
   %t = %NewHyp / 400;
   schedule("dropRock("@%x2@","@%y2@");",%t);
}

function dropRock(%x1,%y1)
{
   playSound(0,"sfx_quake.wav",IDPRF_2D);
   %a = newObject("Nothing",staticShape,"Nothing.dts");
   %x2 = randomInt(-928,0);
   %y2 = randomInt(-832,224);
   %z = getTerrainHeight(%x2,%y2);
   setPosition(%a,%x2,%y2,%z);
   if(%x1 < %x2)
   {
      %distX = %x2 - %x1;
   }
   else if(%x2 < %x1)
   {
      %distX = %x1 - %x2;
   }
   if(%y1 < %y2)
   {
      %distY = %y2 - %y1;
   }
   else if(%y2 < %y1)
   {
      %distY = %y1 - %y2;
   }
   %zdist = 2000 - %z;
   %hyp = sqrt((%distX * distX) + (%distY * %distY));
   %NewHyp = sqrt((%hyp * %hyp) + (%zdist * %zdist));
   %t = %NewHyp / 400;
   dropPod(%x1,%y1,2000,%x2,%y2,%z);

   schedule("%c = playerManager::getPlayerCount(); for(%i=0;%i<%c;%i++){%p = playerManager::getPlayerNum(%i); %v = playerManager::playerNumToVehicleId(%p); %dist = getDistance(%v,"@%a@"); if(%dist <= 120){ %v.Damage = FireRock; }}",%t);

   schedule("damageArea("@%a@",0,0,0,120,5000);",%t);
   schedule("setPosition("@%a@",0,0,0);",%t + 1);
   schedule("deleteObject("@%a@");",%t + 2);
}

function getPlayerScore(%player) 
{ 
   return(%player.score);
}    

function onMissionEnd()
{
   flushConsoleScheduler();
}

// Take away light, fast hercs so they cant run around like crazy.
function setDefaultMissionItems()
{
   allowVehicle(all, true); // All Hercs
   allowVehicle(4, false);  // No talons
   allowVehicle(13, false); // No knight's talons
   allowVehicle(20, false); // No seekers
   allowVehicle(35, false); // No metagen seekers
   allowVehicle(21, false); // No goads
   allowVehicle(36, false); // No metagen goads

// No Tracking weapons 
   allowWeapon(all, true);
   allowWeapon(124, false);
   allowWeapon(125, false);
   allowWeapon(126, false);
   allowWeapon(127, false);
   allowWeapon(128, false);
   allowWeapon(129, false);
   allowWeapon(130, false);
   allowWeapon(131, false);
   allowWeapon(132, false);
   allowWeapon(133, false);
   allowWeapon(147, false);
   allowWeapon(150, false);   
}

function onMissionLoad()
{
   cdAudioCycle("Purge", "Terror", "Watching"); 
   setGameInfo("<F2>GAME TYPE:<F0>  Flag Tag\n\n<F2>MISSION:<F0>  FTAG_Volcano\n\nWelcome to Flag Tag (FTAG)! After there is at least 2 players in the game, grab the flag & hold it to score 1 point each second. There are no points awarded for kills, so you must have the flag to score. You can steal the flag by killing the flagholder. Padkilling the flagholder is encouraged. You can download FTAG_Volcano & other missions made by Gen. Powio™ [MIB] at the MIB Website at www.starsiegemeninblack.cjb.net.");
   %count = playerManager::getPlayerCount();
   for(%i = 0; %i < %count; %i++)
   {
      %player = playerManager::getPlayerNum(%i);
      %player.score = 0;
   }
   %t = randomInt($minEruptTime,$maxEruptTime);
   echo("Next Eruption - "@%t@"s");
   schedule("eruption();",%t);
}