void main()
{

object oPC = GetEnteringObject();

if (!GetIsPC(oPC)) return;

int nEscape = GetLocalInt(oPC, "EscapedStomach");

if (nEscape == 1)
 {
 effect eEffect = EffectDamage(250, DAMAGE_TYPE_SLASHING, DAMAGE_POWER_NORMAL);
 ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, GetObjectByTag("Dinicthys"));
 GiveXPToCreature(oPC, 150);
 SetLocalInt(oPC, "EscapedStomach", 0);
 return;
 }

SetLocalInt(oPC, "UnderwaterArea", 1);
SetLocalInt(oPC, "Underwater", 1);

effect eEffect = EffectDeath();
effect eSlow = EffectMovementSpeedDecrease(67);
effect eAttack = EffectAttackDecrease(4);

ApplyEffectToObject(DURATION_TYPE_PERMANENT, eAttack, oPC);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eSlow, oPC);

object oHench1 = GetHenchman(oPC, 1);
string sHench1 = GetName(oHench1);
object oHench2 = GetHenchman(oPC, 2);
string sHench2 = GetName(oHench2);
object oHench3 = GetHenchman(oPC, 3);
string sHench3 = GetName(oHench3);
object oHench4 = GetHenchman(oPC, 4);
string sHench4 = GetName(oHench4);

ApplyEffectToObject(DURATION_TYPE_PERMANENT, eAttack, oHench1);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eSlow, oHench1);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eAttack, oHench2);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eSlow, oHench2);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eAttack, oHench3);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eSlow, oHench3);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eAttack, oHench4);
ApplyEffectToObject(DURATION_TYPE_PERMANENT, eSlow, oHench4);

//Kill Henchman if they do not possess water breathing magic items
object oPotion1 = GetItemPossessedBy(oHench1, "PotionWater");
object oNecklace1 = GetItemPossessedBy(oHench1, "NecklaceAdapt");
object oRing1 = GetItemPossessedBy(oHench1, "RingWaterBreathe");
if ((GetIsObjectValid(oHench1)) && (GetLocalInt(oHench1, "NoDrown") != 1))
 {
 if ((oPotion1 == OBJECT_INVALID) && (oNecklace1 == OBJECT_INVALID) && (oRing1 == OBJECT_INVALID))
  {
  DelayCommand(3.0, FloatingTextStringOnCreature("Egad!  "+sHench1+" is going to drown!", oPC));
  DelayCommand(7.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oHench1));
  }
 }

object oPotion2 = GetItemPossessedBy(oHench2, "PotionWater");
object oNecklace2 = GetItemPossessedBy(oHench2, "NecklaceAdapt");
object oRing2 = GetItemPossessedBy(oHench2, "RingWaterBreathe");
if ((GetIsObjectValid(oHench2)) && (GetLocalInt(oHench2, "NoDrown") != 1))
 {
 if ((oPotion2 == OBJECT_INVALID) && (oNecklace2 == OBJECT_INVALID) && (oRing2 == OBJECT_INVALID))
  {
  DelayCommand(3.0, FloatingTextStringOnCreature("Egad!  "+sHench2+" is going to drown!", oPC));
  DelayCommand(7.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oHench2));
  }
 }

object oPotion3 = GetItemPossessedBy(oHench3, "PotionWater");
object oNecklace3 = GetItemPossessedBy(oHench3, "NecklaceAdapt");
object oRing3 = GetItemPossessedBy(oHench3, "RingWaterBreathe");
if ((GetIsObjectValid(oHench3)) && (GetLocalInt(oHench3, "NoDrown") != 1))
 {
 if ((oPotion3 == OBJECT_INVALID) && (oNecklace3 == OBJECT_INVALID) && (oRing3 == OBJECT_INVALID))
  {
  DelayCommand(3.0, FloatingTextStringOnCreature("Egad!  "+sHench3+" is going to drown!", oPC));
  DelayCommand(7.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oHench3));
  }
 }

object oPotion4 = GetItemPossessedBy(oHench4, "PotionWater");
object oNecklace4 = GetItemPossessedBy(oHench4, "NecklaceAdapt");
object oRing4 = GetItemPossessedBy(oHench4, "RingWaterBreathe");
if ((GetIsObjectValid(oHench4)) && (GetLocalInt(oHench4, "NoDrown") != 1))
 {
 if ((oPotion4 == OBJECT_INVALID) && (oNecklace4 == OBJECT_INVALID) && (oRing4 == OBJECT_INVALID))
  {
  DelayCommand(3.0, FloatingTextStringOnCreature("Egad!  "+sHench4+" is going to drown!", oPC));
  DelayCommand(7.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oHench4));
  }
 }


//Kill all non-henchman associates when PC is underwater.
object oFamiliar = GetAssociate(ASSOCIATE_TYPE_FAMILIAR, oPC);
if (GetIsPossessedFamiliar(oFamiliar)) AssignCommand(oPC, UnpossessFamiliar(oFamiliar));
if (GetIsObjectValid(oFamiliar))
   {
   FloatingTextStringOnCreature("Your familiar has drowned!", oPC);
   DelayCommand(1.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oFamiliar));
   }

object oCompanion = GetAssociate(ASSOCIATE_TYPE_ANIMALCOMPANION, oPC);
if (GetIsObjectValid(oCompanion))
   {
   FloatingTextStringOnCreature("Your animal companion has drowned!", oPC);
   DelayCommand(1.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oCompanion));
   }

object oSummoned = GetAssociate(ASSOCIATE_TYPE_SUMMONED, oPC);
if (GetIsObjectValid(oSummoned)) RemoveSummonedAssociate(oPC, oSummoned);
if (GetIsObjectValid(oSummoned))
   {
   FloatingTextStringOnCreature("Your summoned companion has drowned!", oPC);
   DelayCommand(1.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oSummoned));
   }

object oDominated = GetAssociate(ASSOCIATE_TYPE_DOMINATED, oPC);
if (GetIsObjectValid(oDominated))
   {
   FloatingTextStringOnCreature("Your dominated companion has drowned!", oPC);
   DelayCommand(1.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eEffect, oDominated));
   }

//Unequip items which will not function underwater
object oRItem = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC);
string sRTag = GetTag(oRItem);
if ((sRTag == "NW_WBWXH001")||(sRTag == "NW_WBWXL001")||(sRTag == "NW_WBWLN001")||(sRTag == "NW_WBWSH001")||(sRTag == "NW_WBWSL001"))
 {
 FloatingTextStringOnCreature("Non-magical missile weapons will not function underwater!", oPC);
 DelayCommand(0.1, AssignCommand(oPC, ActionUnequipItem(oRItem)));
 }
if ((sRTag == "NW_WTHDT001")||(sRTag == "NW_WTHSH001")||(sRTag == "NW_WTHAX001"))
 {
 FloatingTextStringOnCreature("Non-magical throwing weapons will not function underwater!", oPC);
 DelayCommand(0.1, AssignCommand(oPC, ActionUnequipItem(oRItem)));
 }
if (sRTag == "NW_IT_TORCH001")
 {
 FloatingTextStringOnCreature("You cannot use a torch underwater!", oPC);
 DelayCommand(0.1, AssignCommand(oPC, ActionUnequipItem(oRItem)));
 DelayCommand(0.3, DestroyObject(oRItem, 0.0));
 }

object oLItem = GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oPC);
string sLTag = GetTag(oLItem);
if ((sLTag == "NW_WBWXH001")||(sLTag == "NW_WBWXL001")||(sLTag == "NW_WBWLN001")||(sLTag == "NW_WBWSH001")||(sLTag == "NW_WBWSL001"))
 {
 FloatingTextStringOnCreature("Non-magical missile weapons will not function underwater!", oPC);
 DelayCommand(0.1, AssignCommand(oPC, ActionUnequipItem(oLItem)));
 }
if ((sLTag == "NW_WTHDT001")||(sLTag == "NW_WTHSH001")||(sLTag == "NW_WTHAX001"))
 {
 FloatingTextStringOnCreature("Non-magical throwing weapons will not function underwater!", oPC);
 DelayCommand(0.1, AssignCommand(oPC, ActionUnequipItem(oLItem)));
 }
if (sLTag == "NW_IT_TORCH001")
 {
 FloatingTextStringOnCreature("You cannot use a torch underwater!", oPC);
 DelayCommand(0.1, AssignCommand(oPC, ActionUnequipItem(oLItem)));
 DelayCommand(0.3, DestroyObject(oLItem, 0.0));
 }

//Determine how long it will take PC to drown if there is no other intervention
//(note that some of these variables are used in the heartbeat script)...

int nSTR = GetAbilityScore(oPC, ABILITY_STRENGTH, FALSE);
int nCON = GetAbilityScore(oPC, ABILITY_CONSTITUTION, FALSE);
int nSTRmod;
int nCONmod;

if (nSTR > 16) nSTRmod = (nSTR - 16);
if (nSTR < 5) nSTRmod = (5 - nSTR);
int nMOD1 = nSTRmod*4; //plus/minus half round (4 seconds) per STR modifier

if (nCON > 16) nCONmod = (nCON - 16);
if (nCON < 5) nCONmod = (5 - nCON);
int nMOD2 = nCONmod*4; //plus/minus half round (4 seconds) per CON modifier

int nRND = ((d6()+1)*8); //Determine 2-7 rounds randomly; convert to integer seconds
int nDelay = (nRND + nMOD1 + nMOD2); //Total integer seconds before drowning
float fDelay = IntToFloat(nDelay); //Total floating point seconds before drowning
float fRounds = (fDelay/8.0); //Total rounds to pass before drowning (float)
int nRounds =  FloatToInt(fRounds); //Total rounds to pass before dying (integer)

SetLocalInt(oPC, "RoundsBeforeDrowning", nRounds);

}

