//
// Spawn Check - Custom
//

#include "pqj_inc"


//void main (){}


int ParseFlagValue(string sName, string sFlag, int nDigits, int nDefault);
int ParseSubFlagValue(string sName, string sFlag, int nDigits, string sSubFlag, int nSubDigits, int nDefault);
object GetChildByTag(object oSpawn, string sChildTag);
object GetChildByNumber(object oSpawn, int nChildNum);
object GetSpawnByID(int nSpawnID);
void DeactivateSpawn(object oSpawn);
void DeactivateSpawnsByTag(string sSpawnTag);
void DeactivateAllSpawns();
void DespawnChildren(object oSpawn);
void DespawnChildrenByTag(object oSpawn, string sSpawnTag);
//
//
int SpawnCheckCustom(object oSpawn)
{
    // Initialize Values
    int nSpawnCheckCustom = GetLocalInt(oSpawn, "f_SpawnCheckCustom");

    // Block Spawn by Default
    int nProcessSpawn = FALSE;

//
// Only Make Modifications Between These Lines
// -------------------------------------------

    // Check 00
    if (nSpawnCheckCustom == 0)
    {
        // Example, Allow Spawn
        nProcessSpawn = TRUE;
    }
    //

    //
    if (nSpawnCheckCustom == 1)
    {
        if (GetIsDawn() == TRUE || GetIsDay() == TRUE)
        {
            nProcessSpawn = TRUE;
            SetLocalInt(oSpawn, "SpawnProcessed", FALSE);
        }
        else
        {
            int nSpawnProcessed = GetLocalInt(oSpawn, "SpawnProcessed");
            if (nSpawnProcessed == FALSE)
            {
                nProcessSpawn = TRUE;
                SetLocalInt(oSpawn, "SpawnProcessed", TRUE);
            }
        }
    }
    //

    // Reproducing Predators
    if (nSpawnCheckCustom == 10)
    {
        int nChildren = GetLocalInt(oSpawn, "ChildrenSpawned");
        if (nChildren >= 10)
        {
            int nHappy = 0;
            int nPredators;
            int nNth = 1;
            object oPredator = GetNearestObject(OBJECT_TYPE_CREATURE, oSpawn, nNth);
            while (oPredator != OBJECT_INVALID)
            {
                if (GetLocalInt(oPredator, "Predator") == TRUE)
                {
                    nPredators++;
                    if (GetLocalInt(oPredator, "CurrentHungerState") > 0)
                    {
                        nHappy++;
                    }
                }
                nNth++;
                oPredator = GetNearestObject(OBJECT_TYPE_CREATURE, oSpawn, nNth);
            }
            SendMessageToAllDMs("There are " + IntToString(nPredators) + " Predators Alive.");
            if (nHappy >= 2)
            {
                nProcessSpawn = TRUE;
            }
        }
        else
        {
            nProcessSpawn = TRUE;
        }
        if (nProcessSpawn == TRUE)
        {
            SendMessageToAllDMs("A Predator is Born!");
        }
    }
    //
	

//:: Spawns The Dishonest Patrol if the party's average level is over 10, 
//:: they have been inside the dungeon & Sheriff Ostland is still alive.
    if (nSpawnCheckCustom == 11)
    {
        // Initialize Variables
        int nTotalPCs;
        int nTotalPCLevel;
        int nAveragePCLevel;
		int bDungeon;
		int bOstlandDead;
		
        object oArea = GetArea(OBJECT_SELF);
        // Cycle through PCs in Area
        object oPC = GetFirstObjectInArea(oArea);
		while (oPC != OBJECT_INVALID)
        {
            if (GetIsPC(oPC) == TRUE || GetIsPC(GetMaster(oPC)) == TRUE)	//:: Summons & henchmen should count towards this.
            {
			//:: Check if the PC has entered the Dungeon yet
				bDungeon++;
				bDungeon = GetLocalInt(oPC, "bEnteredDungeon");
				
			//:: Check to see if Ostland has been killed.
				bOstlandDead++;
				bOstlandDead = RetrieveQuestState("DishonestPatrol", oPC);
				
                nTotalPCs++;
                nTotalPCLevel = nTotalPCLevel + GetHitDice(oPC);
            }
            oPC = GetNextObjectInArea(oArea);
        }
		
        if (nTotalPCs > 0)
        {
            nAveragePCLevel = nTotalPCLevel / nTotalPCs;		
        }
        else
        {
            nAveragePCLevel = 3;
        }

		if ((nAveragePCLevel > 10) && (bOstlandDead < 1) && (bDungeon > 0))
        {
            nProcessSpawn = TRUE;
        }
    }
//:: Spawns The Dishonest Patrol if the party's average level is over 10, 
//:: they have been inside the dungeon & Sheriff Ostland is still alive.


//:: Process spawn if Dung Monster *is not* roaming & *not* "dead" (Commode)
	if (nSpawnCheckCustom == 11)
	{
		int iDungMonActive	= GetLocalInt(GetModule(), "DungMonsterActive");
		
		int iDungMonDead	= GetLocalInt(GetModule(), "DungMonsterDead");
		
		if (iDungMonActive && iDungMonDead)
		{
			nProcessSpawn = FALSE;
		}
		
	}	
//:: Process spawn if Dung Monster *is not* roaming & *not* "dead" (Commode)


//:: Process spawn if Dung Monster *is* roaming or is "dead" (Hole)
	if (nSpawnCheckCustom == 12)
	{
		int iDungMonActive	= GetLocalInt(GetModule(), "DungMonsterActive");
		
		int iDungMonDead	= GetLocalInt(GetModule(), "DungMonsterDead");
		
		if (iDungMonActive || iDungMonDead)
		{
			nProcessSpawn = TRUE;
		}
		
	}				
//:: Process spawn if Dung Monster *is* roaming or is "dead" (Hole)
	

//:: Checks for stage 1 or lower for the "Spider's Captive quest"
    if (nSpawnCheckCustom == 50)
	{
	//:: Initialize major variables
		object oArea = GetArea(OBJECT_SELF);
		object oPC = GetFirstObjectInArea(oArea);

		//:: Cycle through PCs in Area
		while (oPC != OBJECT_INVALID)
		{
		//:: Check quest stage
			if ( RetrieveQuestState("spiders", oPC) <= 1 )
			{
				nProcessSpawn = TRUE;
				//SendMessageToPC(oPC, "Spawn Processed");
				SetLocalInt(oSpawn, "SpawnProcessed", TRUE);
			}

		oPC = GetNextObjectInArea(oArea);
		}
    }
//:: Checks for stage 1 or lower for the "Spider's Captive quest"


//:: Checks for stage 2 or lower for the "Spider's Captive quest"
    if (nSpawnCheckCustom == 51)
	{
	//:: Initialize major variables
		object oArea = GetArea(OBJECT_SELF);
		object oPC = GetFirstObjectInArea(oArea);

		//:: Cycle through PCs in Area
		while (oPC != OBJECT_INVALID)
		{
		//:: Check quest stage
			if ( RetrieveQuestState("spiders", oPC) <= 2 )
			{
				nProcessSpawn = TRUE;
				//SendMessageToPC(oPC, "Spawn Processed");
				SetLocalInt(oSpawn, "SpawnProcessed", TRUE);
			}

		oPC = GetNextObjectInArea(oArea);
		}
    }
//:: Checks for stage 2 or lower for the "Spider's Captive quest"


/* //:: Checks for stage 98 or lower (Drusilla's not dead) for the "Vengeful Druid quest" 	//:: I wish past me left a note 
    if (nSpawnCheckCustom == 52)															//:: for future me on why this was disabled
	{
	//:: Initialize major variables
		object oArea = GetArea(OBJECT_SELF);
		object oPC = GetFirstObjectInArea(oArea);

		//:: Cycle through PCs in Area
		while (oPC != OBJECT_INVALID)
		{
		//:: Check quest stage
			if ( RetrieveQuestState("VengefulDruid", oPC) <= 98 )
			{
				nProcessSpawn = TRUE;
				//SendMessageToPC(oPC, "Spawn Processed");
				SetLocalInt(oSpawn, "SpawnProcessed", TRUE);
			}

		oPC = GetNextObjectInArea(oArea);
		}
    }
//:: Checks for stage 98 or lower (Drusilla's not dead) for the "Vengeful Druid quest" */

//:: Checks for non-completion of "The Outcasts" quest
    if (nSpawnCheckCustom == 53)
	{
	//:: Initialize major variables
		object oArea = GetArea(OBJECT_SELF);
		object oPC = GetFirstObjectInArea(oArea);

		//:: Cycle through PCs in Area
		while (oPC != OBJECT_INVALID)
		{
		//:: Check quest stage
			if ( RetrieveQuestState("outcasts", oPC) <= 3 )
			{
				nProcessSpawn = TRUE;
				//SendMessageToPC(oPC, "Spawn Processed");
				SetLocalInt(oSpawn, "SpawnProcessed", TRUE);
			}

		oPC = GetNextObjectInArea(oArea);
		}
    }
//:: Checks for non-completion of "The Outcasts" quest





// -------------------------------------------
// Only Make Modifications Between These Lines
//

    // Return whether Spawn can Proceed
    return nProcessSpawn;
}