//dmw_activate

// ** This script goes in the OnItemActivation event of your Module
// ** Properties.  It checks to see if the item used is a DM Helper
// ** And if so, and the user isnt a DM, destroys it, otherwise it
// ** Starts the DM Helper working.  "dmw_inc" contains the actual
// ** code that controls the Helpers effects.  If you update anything
// ** in it, you must recompile the calling dmw_<name> script to make
// ** the change take effect.

#include "x2_inc_switches"

void SummonCelestial(location lLocation)
{

  object oAlly = CreateObject(OBJECT_TYPE_CREATURE, "celestialally", lLocation, FALSE);

}

void SummonDemon(location lLocation)
{

  object oAlly = CreateObject(OBJECT_TYPE_CREATURE, "halfdemonally", lLocation, FALSE);

}

void main()
{
	object oItem=GetItemActivated();
	object oActivator=GetItemActivator();

    // * Generic Item Script Execution Code
    // * If MODULE_SWITCH_EXECUTE_TAGBASED_SCRIPTS is set to TRUE on the module,
    // * it will execute a script that has the same name as the item's tag
    // * inside this script you can manage scripts for all events by checking against
    // * GetUserDefinedItemEventNumber(). See x2_it_example.nss
    if (GetModuleSwitchValue(MODULE_SWITCH_ENABLE_TAGBASED_SCRIPTS) == TRUE)
    {
		SetUserDefinedItemEventNumber(X2_ITEM_EVENT_ACTIVATE);
        int nRet =   ExecuteScriptAndReturnInt(GetUserDefinedItemEventScriptName(oItem),OBJECT_SELF);
        if (nRet == X2_EXECUTE_SCRIPT_END)
        {
           return;
        }

     }
	 
   if(GetTag(oItem)=="DMsHelper")
   {
      // Test to make sure the activator is a DM, or is a DM
      // controlling a creature.
      if(GetIsDM(oActivator) != TRUE)
      {
         object oTest = GetFirstPC();
         string sTestName = GetPCPlayerName(oActivator);
         int nFound = FALSE;
         while (GetIsObjectValid(oTest) && (! nFound))
         {
            if (GetPCPlayerName(oTest) == sTestName)
            {
               if(GetIsDM(oTest))
               {
                  nFound = TRUE;
               }
               else
               {
                  DestroyObject(oItem);
                  SendMessageToPC(oActivator,"You are mortal and this is not yours!");
                  return;
               }
            }
            oTest=GetNextPC();
         }
      }
      // get the wand's activator and target, put target info into local vars on activator
      object oMyActivator = GetItemActivator();
      object oMyTarget = GetItemActivatedTarget();
      SetLocalObject(oMyActivator, "dmwandtarget", oMyTarget);
      location lMyLoc = GetItemActivatedTargetLocation();
      SetLocalLocation(oMyActivator, "dmwandloc", lMyLoc);

      //Make the activator start a conversation with itself
      AssignCommand(oMyActivator, ActionStartConversation(oMyActivator, "dmwand", TRUE));
      return;
   }

   if(GetTag(oItem)=="AutoFollow")
   {
      object oTarget = GetItemActivatedTarget();

      if(GetIsObjectValid(oTarget))
      {
         AssignCommand ( oActivator, ActionForceFollowObject(oTarget));
      }
      return;
   }

   if(GetTag(oItem)=="WandOfFX")
   {

       // get the wand's activator and target, put target info into local vars on activator
      object oDM = GetItemActivator();
      object oMyTarget = GetItemActivatedTarget();
      SetLocalObject(oDM, "FXWandTarget", oMyTarget);
      location lTargetLoc = GetItemActivatedTargetLocation();
      SetLocalLocation(oDM, "FXWandLoc", lTargetLoc);

      object oTest=GetFirstPC();
      string sTestName = GetPCPlayerName(oDM);
      // Test to make sure the activator is a DM, or is a DM
      // controlling a creature.

      if(GetIsDM(oDM) != TRUE)
      {
         object oTest = GetFirstPC();
         string sTestName = GetPCPlayerName(oDM);
         int nFound = FALSE;
         while (GetIsObjectValid(oTest) && (! nFound))
         {
            if (GetPCPlayerName(oTest) == sTestName)
            {
               if(GetIsDM(oTest))
               {
                  nFound = TRUE;
               }
               else
               {
                  DestroyObject(oItem);
                  SendMessageToPC(oDM,"You are mortal and this is not yours!");
                  return;
               }
            }
            oTest=GetNextPC();
         }
      }

      //Make the activator start a conversation with itself
      AssignCommand(oDM, ActionStartConversation(oDM, "fxwand", TRUE));
      return;

   }
   if(GetTag(oItem)=="EmoteWand")
   {
      AssignCommand(oActivator, ActionStartConversation(oActivator, "emotewand", TRUE));
      return;
   }


   if(GetTag(oItem)=="CelestialRelic")
   {

      location lLocation =  GetItemActivatedTargetLocation();
      ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SUMMON_CELESTIAL), lLocation);
      DelayCommand(3.0, SummonCelestial(lLocation));

//      object oAlly = CreateObject(OBJECT_TYPE_CREATURE, "celestialally", lLocation, FALSE);

   }

   if(GetTag(oItem)=="DemonicSceptor")
   {

      location lLocation =  GetItemActivatedTargetLocation();
      ApplyEffectAtLocation( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SUMMON_GATE), lLocation);
      DelayCommand(3.0, SummonDemon(lLocation));

//      object oAlly = CreateObject(OBJECT_TYPE_CREATURE, "halfdemonally", lLocation, FALSE);

   }

}