//::///////////////////////////////////////////////
//:: Shutdown
//::
//:://////////////////////////////////////////////
/*
    This script will shutdown the combat AI.
*/
//:://////////////////////////////////////////////

#include "no_lib_data"
#include "no_inc"

void main()
{
    int iCnt = GetLocalInt( OBJECT_SELF, "#QUEUESHUTDOWN" );
    int iCombat = 0;
    //object oEnemy;
    int iRestWait = 30;

	//delete queue marker
	DeleteLocalInt( OBJECT_SELF, "#QUEUESHUTDOWN" );
	
	//SpeakString( "CFS" );
    //if ( !GetIsInCombat() && GetLocalInt( OBJECT_SELF, "#ACTIVE" ) && !IsInConversation( OBJECT_SELF ) )
    if ( !IsInConversation( OBJECT_SELF ) )
    {
    	//oEnemy = GetNearestCreature( CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, OBJECT_SELF, 1, CREATURE_TYPE_IS_ALIVE, TRUE );
    	int iE = GetHostileCount( 50.0 );
    	if ( iE )
    	{
    		InitCombat();
    	}
        else
        {
        	if ( GetIsObjectValid( GetMaster( OBJECT_SELF ) ) )
        	{
            	DeleteLocalInt( OBJECT_SELF, "#FASTBUFFED" );
            	DeleteLocalInt( OBJECT_SELF, "#QUEUESHUTDOWN" );
            	DeleteLocalInt( OBJECT_SELF, "#ACTIVE" );
            	DeleteLocalInt( OBJECT_SELF, "#VANISHED" );
            	//SetReadyStatus();
            	ActionForceFollowObject( GetMaster( OBJECT_SELF ), 3.0 );
            	return;
        	}
        	//else if ( !GetLocalInt( OBJECT_SELF, "#LASTREST" ) && GetTimeSinceLastCombat() > iRestWait )
        	else if ( GetTimeSinceLastCombat() > iRestWait )
            {
                //PrintString( "RESTING: " + GetName( OBJECT_SELF ) );
                DeleteLocalInt( OBJECT_SELF, "#FASTBUFFED" );
                DeleteLocalInt( OBJECT_SELF, "#QUEUESHUTDOWN" );
                DeleteLocalInt( OBJECT_SELF, "#ACTIVE" ); //shutdown AI heartbeats until an opponent shows up
                DeleteLocalInt( OBJECT_SELF, "#VANISHED" );
                //SetReadyStatus();
                if ( !GetLocalInt( OBJECT_SELF, "#LASTREST" ) )
                {
                	ActionRest();  //no enemies seen for 30 seconds, rest
                	SetLocalInt( OBJECT_SELF, "#LASTREST", 1 );
                }
                ActionDoCommand( WalkWayPoints() );  //start walking waypoints if applicable
                return;
            }
            
            if ( !GetLocalInt( OBJECT_SELF, "#QUEUESHUTDOWN" ) && GetLocalInt( OBJECT_SELF, "#ACTIVE" ) )
            {
                //DelayCommand( 12.0, ExecuteScript( "no_scr_shutdown", OBJECT_SELF ) );
                DoQueueShutdown( 12.0 );
                return;
			}
        }
    }
}