void Teleportparty(object oPC)
{
int nRandom;
string sWP="jw_machine_sp_wp";
string sCheckstring;
location locNew=GetLocation(GetWaypointByTag("jw_machine_sp_wp2"));
location lLoc=GetLocation(GetWaypointByTag("jw_mach_port_mp"));
CreateObject(OBJECT_TYPE_PLACEABLE,"jw_machine_porta",lLoc);


object oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
        oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
        oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
         oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
         oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
         oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
         oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
         oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
         oParty=GetFirstFactionMember(oPC,FALSE);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_UNSUMMON),oParty);

             AssignCommand( oParty, ClearAllActions() );
             nRandom=d8();

             sCheckstring=sWP+IntToString(nRandom);
             AssignCommand( oParty, JumpToObject( GetWaypointByTag(sCheckstring) ) );
           }
         oParty=GetNextFactionMember(oPC,FALSE);
        }
 object oSound=GetObjectByTag("jw_machine_sound");
 SoundObjectStop(oSound);

 //SetPlotFlag(OBJECT_SELF,0);

// DestroyObject(OBJECT_SELF);

}


void main()
{
object oObject=GetInventoryDisturbItem();
object oPC=GetLastDisturbed();
object oParty;

 if (GetTag(oObject)!="jw_mystra_shard")
 {
  ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_PULSE_NEGATIVE),OBJECT_SELF);
  SpeakString("*The "+GetName(oObject)+" is destroyed*");
  DestroyObject(oObject);
  return;
 }

object oSound=GetObjectByTag("jw_machine_sound");

ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_PULSE_HOLY),OBJECT_SELF);

int nNumber=GetLocalInt(OBJECT_SELF,"nItems");
int nNewnumber=nNumber;

if (GetInventoryDisturbType()==INVENTORY_DISTURB_TYPE_ADDED)
   {
    nNewnumber=nNewnumber+1;
   }
   else
   {
   nNewnumber=nNewnumber-1;
   }


if ((nNewnumber>=5)&&(nNumber<5))
  {
   // we have just put the fifth one in
   ApplyEffectToObject(DURATION_TYPE_PERMANENT,EffectVisualEffect(VFX_DUR_ETHEREAL_VISAGE),OBJECT_SELF);
    SpeakString("*The device appears to be fully powered*");
  SoundObjectPlay(oSound);
  }

if ((nNewnumber<5)&&(nNumber>=5))
  {
   // we had five in but have just taken one out
   SoundObjectStop(oSound);
   effect eEffect=GetFirstEffect(OBJECT_SELF);
   while (GetIsEffectValid(eEffect))
   {
     if (GetEffectCreator(eEffect)==OBJECT_SELF)
     {
      RemoveEffect(OBJECT_SELF,eEffect);

     }
       eEffect=GetNextEffect(OBJECT_SELF);
   }

  }
////////
if ((nNumber==0)&&(nNewnumber==1))
{
 SpeakString("*The contraption becomes warm*");
}


if (nNewnumber==9)
{
  ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_FLAME_M),OBJECT_SELF);
  SpeakString("The machine becomes almost too hot to touch . . . the power is set too high . . .");

}


if (nNewnumber>=10)

 {
  SpeakString("*Too much power courses through the machine . . .*");
  ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_IMP_DESTRUCTION),OBJECT_SELF);
  oParty=GetFirstFactionMember(oPC);
  while (GetIsObjectValid(oParty))
        {
         if (GetArea(oParty)==GetArea(OBJECT_SELF))
           {
             ApplyEffectToObject(DURATION_TYPE_TEMPORARY,EffectVisualEffect(VFX_BEAM_EVIL),oParty,10.0);
             ApplyEffectToObject(DURATION_TYPE_TEMPORARY,EffectVisualEffect(VFX_DUR_PARALYZE_HOLD),oParty,10.0);
             ApplyEffectToObject(DURATION_TYPE_TEMPORARY,EffectCutsceneDominated(),oParty,10.0);
           }
         oParty=GetNextFactionMember(oPC);
        }
 object oItem=GetFirstItemInInventory(OBJECT_SELF);
 while (GetIsObjectValid(oItem))
 {
  DestroyObject(oItem);
  oItem=GetNextItemInInventory(OBJECT_SELF);
 }

 DelayCommand(10.1,Teleportparty(oPC));

 }


SetLocalInt(OBJECT_SELF,"nItems",nNewnumber);




}