/* Generic area onEnter script for streamlined addition of Sparky Spawner variables
   Created By: The Amethyst Dragon (www.amethyst-dragon.com/nwn)

   Can be used for the onEnter script for all areas of a module. If you already
   have your own area enter script, you can add just the includes and the
   "Sparky Spawner Section" of code.

   This causes the area to check to see if Sparky variables are set on the area.

   If not, it searches for the Encounter Variables placeable object in the area
   to find out which variables to add to the area, then runs the Sparky spawning
   code.

   If no such variables or placeable are present in the area, it skips any further
   Sparky code from running.
*/

#include "sparky_inc"
#include "sparky_enc"
#include "X0_I0_PARTYWIDE"
#include "spawn_functions"

void main()
{
    object oPC = GetEnteringObject();
    object oArea = OBJECT_SELF;

//:: Nothing happens if it wasn't a PC entering the area.
    if (GetIsPC(oPC) == TRUE)
    {
    //:: Records that the PC has entered Rappan Athuk at least once.
        SetLocalInt(oPC, "bEnteredDungeon", 1);

    //:: Fires any NESS spawners
        ExecuteScript("spawn_smpl_onen2");

//////////////////////////////////////////////// Sparky Spawner Section
        if (GetLocalInt(oArea, "sparkyloaded") != 1)
        {
        //:: Find encounters-holding placeable
            object oEncVars = GetFirstObjectInArea(oArea);

            while (oEncVars != OBJECT_INVALID)
            {
                if (GetTag(oEncVars) == "sparky_variables") { break; }
                oEncVars = GetNextObjectInArea(oArea);
            }

            if (oEncVars != OBJECT_INVALID)
            {
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc01"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc02"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc03"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc04"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc05"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc06"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc07"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc08"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc09"));
                LoadEncounterVariables(oArea, GetLocalInt(oEncVars, "enc10"));
                DestroyObject(oEncVars, 1.0);
            }

        SetLocalInt(oArea, "sparkyloaded", 1);

        }

    int iDisabled = GetLocalInt(oArea, "iSparkyDisabled");
    int iAlreadySpawned = GetLocalInt(oArea, "iSparkySpawned");

    if (GetIsDM(oPC))
    {
        SendMessageToPC(oPC, "Sparky spawns are " + (iDisabled ? "OFF" : "ON"));
            return;
    }

    if (iAlreadySpawned || iDisabled)
    {
        return;
    }

SpawnEncounters(oArea);
//////////////////////////////////////////////// End Sparky Section
    }
}