/*
    nw_s0_sanctuary

    Makes the target creature invisible to hostile
    creatures unless they make a Will Save to ignore
    the Sanctuary Effect

    By: Preston Watamaniuk
    Created: Jan 7, 2002
    Modified: Jun 29, 2006

    Flaming_Sword: added greater sanctuary
        cleaned up
*/

#include "prc_sp_func"
#include "prc_inc_teleport"
#include "prc_add_spell_dc"

//Implements the spell impact, put code here
//  if called in many places, return TRUE if
//  stored charges should be decreased
//  eg. touch attack hits
//
//  Variables passed may be changed if necessary
int DoSpell(object oCaster, object oTarget, int nCasterLevel, int nEvent)
{
    int nSpellID = PRCGetSpellId();
    int bSanc = (nSpellID == SPELL_SANCTUARY);
    effect eSanc = bSanc ? EffectSanctuary((PRCGetSaveDC(oTarget,oCaster))) : EffectEthereal();
    effect eLink = EffectLinkEffects(EffectVisualEffect(VFX_DUR_SANCTUARY), eSanc);
    eLink = EffectLinkEffects(eLink, EffectVisualEffect(VFX_DUR_CESSATE_POSITIVE));
    float fDuration = bSanc ? RoundsToSeconds(1+(nCasterLevel/10)) : TurnsToSeconds(1+(nCasterLevel/10));
    int nMetaMagic = PRCGetMetaMagicFeat();
    if (CheckMetaMagic(nMetaMagic, METAMAGIC_EXTEND))
        fDuration *= 2; //Duration is +100%
    SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, nSpellID, FALSE));
    if(bSanc || GetCanTeleport(oTarget, GetLocation(oTarget), FALSE, TRUE))
        SPApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oTarget, fDuration,TRUE,-1,nCasterLevel);

    return TRUE;    //return TRUE if spell charges should be decremented
}

void main()
{
	//teste cooldown
	//Spell Time Lock duration (in seconds, use float values)
	float iLockTimer = 240.0;
	//Defining time variables
	float f120togo = ( 120 - iLockTimer ) * -1;
	float f60togo = ( 60 - iLockTimer ) * -1;
	float f10togo = ( 10 - iLockTimer ) * -1;

	//Checking spell's caster
	object oTarget = PRCGetSpellTargetObject();
	//Checking if he used GS recently
	int iTimer = GetLocalInt(oTarget, "GSTimer");


	
	
if (iTimer == 0) //teste
 {
   SetLocalInt(oTarget, "GSTimer", 1);
    //Change: Checking target's area
    object oArea = GetArea(oTarget);	
	
	
	
    object oCaster = OBJECT_SELF;
    int nCasterLevel = PRCGetCasterLevel(oCaster);
    PRCSetSchool(GetSpellSchool(PRCGetSpellId()));
    if (!X2PreSpellCastCode()) return;
    //object oTarget = PRCGetSpellTargetObject();
    int nEvent = GetLocalInt(oCaster, PRC_SPELL_EVENT); //use bitwise & to extract flags
    if(!nEvent) //normal cast
    {
        if(GetLocalInt(oCaster, PRC_SPELL_HOLD) && oCaster == oTarget)
        {   //holding the charge, casting spell on self
            SetLocalSpellVariables(oCaster, 1);   //change 1 to number of charges
            return;
        }
        DoSpell(oCaster, oTarget, nCasterLevel, nEvent);
    }
    else
    {
        if(nEvent & PRC_SPELL_EVENT_ATTACK)
        {
            if(DoSpell(oCaster, oTarget, nCasterLevel, nEvent))
                DecrementSpellCharges(oCaster);
        }
    }
    PRCSetSchool();
	

    SendMessageToAllDMs("Player "+ObjectToString(oCaster)+" has just cast Greater Sanctuary. He is currently in "+ObjectToString(oArea)+".");
    SendMessageToPC(oTarget, "Greater Sanctuary has a timer of "+FloatToString(iLockTimer, 3, 1)+" seconds. You may not use GS again for this period of time. Attempting to do so will spend the spell while producing no effect.");
    DelayCommand(f120togo, SendMessageToPC(oTarget, "You have two minutes left on your Greater Sanctuary Lock Timer."));
    DelayCommand(f60togo, SendMessageToPC(oTarget, "You have one minute left on your Greater Sanctuary Lock Timer."));
    DelayCommand(f10togo, SendMessageToPC(oTarget, "You have 10 seconds left on your Greater Sanctuary Lock Timer."));
    DelayCommand(iLockTimer, SendMessageToPC(oTarget, "Greater Sanctuary is once again available for use."));
    DelayCommand(iLockTimer, SetLocalInt(oTarget, "GSTimer", 0));
  }
  else
    {
    SendMessageToPC(oTarget, "You have used Greater Sanctuary too recently, the effect has been cancelled");
    }
	
}