//::////////////////////////////////////////////////////////////////////////////
/*//

    Labyrinth of Undeath
        onHeartbeat script
            labyrinth_hb.nss

    Wandering Monsters: Check once per 30 minutes on 1d20.

    Detections: Strong evil from the whole place.

    Continuous Effects: The evil energy of the labyrinth makes all PC's
		require a Will save or be cursed.  This check occurs every half
		hour and the curse vanishes if the PC leaves the area.

*///
//::////////////////////////////////////////////////////////////////////////////
#include "inc_debug"
#include "spawn_main"

//:: Function to process the curse
void ApplyCurse(object oPC)
{
	//:: Check for exising variable on player
    int oldTime = GetLocalInt(oPC, "CurseWillSaveTime");

    //:: Get the current system time in seconds
    int newTime = (GetTimeHour()*60*60)+(GetTimeMinute()*60)+GetTimeSecond();
	
    //:: Calculate the time difference in seconds
    int timeDifference = newTime - oldTime;
            
	if (DEBUG)
	{	
		SendMessageToPC(oPC, "oldTime = " + IntToString(oldTime));
		SendMessageToPC(oPC, "newTime = " + IntToString(newTime));
		SendMessageToPC(oPC, "timeDifference = " + IntToString(timeDifference));
	}

    //:: Check if the character hasn't made a Will save in the last 3 minutes
    if (oldTime == 0 || timeDifference >= 180) // 180 seconds = 3 real-time minutes
    {
        //:: Check if the character failed the save
        if (!WillSave(oPC, 20))
        {
            //:: Apply a curse
            effect eCurse	= EffectCurse(1, 1, 1, 1, 1, 1);
            effect eVFX		= EffectVisualEffect(VFX_DUR_CESSATE_NEGATIVE);
			effect eVis 	= EffectVisualEffect(VFX_IMP_REDUCE_ABILITY_SCORE);
            effect eLink	= EffectLinkEffects(eCurse, eVFX);
			
			eLink			= EffectLinkEffects(eCurse, eVis);
            eLink			= SupernaturalEffect(eLink);
            eLink			= TagEffect(eLink, "LabyrinthCurse");

            ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oPC, 180.0f); // 180 seconds = 3 real-time minutes & .5 hours in-game.

            //:: Store the current time on player
            SetLocalInt(oPC, "CurseWillSaveTime", newTime);

            if (DEBUG)
			{
				SendMessageToPC(oPC, "Failed CurseWillSave");
				SendMessageToPC(oPC, "Setting CurseWillSaveTime as " + IntToString(newTime));
			}
			
            //:: Send a message to the player
            SendMessageToPC(oPC, "The very air you breathe in this labyrinth is cursed.");
        }

	//:: Store the current time on player
		SetLocalInt(oPC, "CurseWillSaveTime", newTime);
		
		if (DEBUG)
		{
			SendMessageToPC(oPC, "Passed CurseWillSave");
			SendMessageToPC(oPC, "Setting CurseWillSaveTime as " + IntToString(newTime));
		}
    }
}

void main()
{
    //:: Declare major variables
    object oArea = OBJECT_SELF;
    object oPC = GetFirstObjectInArea(oArea, OBJECT_TYPE_CREATURE);

    while (GetIsObjectValid(oPC))
    {
        if (GetIsPC(oPC) || (GetMaster(oPC) != OBJECT_INVALID && GetIsPC(GetMaster(oPC))))
        {
            if (DEBUG)
            {
                SendMessageToPC(oPC, "Running Labyrinth of Undeath Area HB");
            }

            //:: Apply the curse
            ApplyCurse(oPC);
        }

        //:: Get the next object in the area
        oPC = GetNextObjectInArea(oArea);
    }
}