// Multiplayer drown script for CoD
//
// written by [D]delica
// based on _noprone.gsc by r0ger and the stock IW S&D scripts ;)

main()
{
  drownage = getentarray("drown","targetname");
  if (isDefined("drownage"))
  {
    precacheShader("black");
    precacheShader("white");
    level._effect["bubbles1"] = loadfx ("fx/bubbles.efx");
    level.barsize = 288;
    level.drowntime = 8;
    level.hurttime = 6;
    for(d = 0; d < drownage.size; d++)
    {
      drownage[d] thread water();
    }
  }
}
Water()
{
  while (1)
  {
    self waittill("trigger", other);
    if(isPlayer(other) && other istouching(self))
      other thread drown(self);
  }
} 
drown(trigger)
{
  level endon("wwx_gameover");
  self endon("disconnect");

  dceiling = getent(trigger.target,"targetname");
  while (self istouching(trigger) && !self istouching(dceiling) && isalive(self))
  {
    if (!isDefined(self.drowning))
      self.drowning = false;
    if (self.drowning)
      return;
    self.drowning = true;
    while (self istouching(trigger) && !self istouching(dceiling) && isalive(self)) {

      self disableweapon();
//    maps\mp\gametypes\_weapons::dropWeapon();
      wait 0.5;
      self.health--;
      radiusDamage(self.origin,9, 1, 1);
//      self iprintln(self.health+" health remaining");

      if (self.health<=0) 
        self suicide();
    }
  }
  self enableweapon();
  self.drowning = false;
}

/*     
    if (self.drowning)
      return;
    if(!isDefined(self.sounder))
    {
      looper = spawn( "script_model", self.origin );
      wait 0.025;
      looper show();
      looper linkto(self);
      looper playLoopSound("water");
      self.sounder = true;
    }
    level.barincrement = (level.barsize / (20.0 * level.drowntime));
    if(!isDefined(self.progressbackground))
    {
      self.progressbackground = newClientHudElem(self);
      self.progressbackground.alignX = "center";
      self.progressbackground.alignY = "middle";
      self.progressbackground.x = 320;
      self.progressbackground.y = 385;
      self.progressbackground.alpha = 0.5;
      self.progressbackground setShader("black", (level.barsize + 4), 12);
    }
    if(!isDefined(self.progressbar))
    {
      self.progressbar = newClientHudElem(self);
      self.progressbar.alignX = "left";
      self.progressbar.alignY = "middle";
      self.progressbar.x = (320 - (level.barsize / 2.0));
      self.progressbar.y = 385;
    }
    self.progressbar setShader("white", 0, 8);
    self.progressbar scaleOverTime(level.drowntime, level.barsize, 8);
    self.progresstime = 0;
    d = 0;
    f = 0;
    while(isalive(self) && self istouching(trigger) && !self istouching(dceiling) && (self.progresstime < level.drowntime))
    {
//      self switchtoweapon("pistol");
      d ++;
      f ++;
      wait 0.05;
      self.progresstime += 0.05;

//      playfxOnTag( level._effect["bubbles1"], self, "TAG_HELMET");
      playfxOnTag( level._effect["bubbles1"], self, "j_helmet");

      rand = randomInt (9);
      if (d > (30 + rand))
      {
        self playSound("bubble");
        d = 0;
      }
      if(self.progresstime >= level.hurttime)
      {
        if(f >= 4)
        {
          radiusDamage(self.origin,9, 1, 1);
          f = 0;
        }
      }
    }
    if(isalive(self) && self istouching(trigger) && !self istouching(dceiling) && (self.progresstime >= level.drowntime))
    {
      if (isDefined(self.progressbackground))
        self.progressbackground destroy();
      if (isDefined(self.progressbar))
        self.progressbar destroy();
//      self.progressbackground destroy();
//      self.progressbar destroy();

      wait 0.025;
      radiusDamage(self.origin,22, 3000, 3000);

      self.drowning = undefined;
      self.sounder = undefined;

      looper stopLoopSound("water");
      looper delete();
      looper = undefined;

      randb = randomInt(4);
      deathmethod1 = " drowned.";
      deathmethod2 = " drowned.";
      deathmethod3 = " drowned.";
      deathmethod4 = " drowned.";

      if (randb == 0)
        iPrintLn( self.name, deathmethod1);
      if (randb == 1)
        iPrintLn( self.name, deathmethod2);
      if (randb == 2)
        iPrintLn( self.name, deathmethod3);
      if (randb == 3)
        iPrintLn( self.name, deathmethod4);
    }
    else
    {
      if (isDefined(self.progressbackground))
        self.progressbackground destroy();
      if (isDefined(self.progressbar))
        self.progressbar destroy();
      looper stopLoopSound("water");
      looper delete();
      looper = undefined;
      self.sounder = undefined;
      self.drowning = false;
    }
    wait .05;
*/
