/*
    PROJECT Q IV v4.00
    (c) 2022 by Pstemarie

    Guard OnUserDefined Event Script

        Handles the OnConversation event for creatures responding to the alarm
        sounded by the shrieker.

        The receiver of a "NW_ATTACK_MY_TARGET" shout will come and investigate
        even if it doesn't see an enemy. Once the creature approaches the
        shouter, if he sees an enemy his OnPerception script will activate and
        he will attack.

    Version: 1.0
    Created: 21 Jan 2003
    Author:  Lathspell

*/

void main()
{
    int nEvent = GetUserDefinedEventNumber();
    if (nEvent == 1004) // OnDialog event
    {
        // this sees if the shout matches the pattern set by the 'SetListeningPatterns' in OnSpawn
        int nMatch = GetListenPatternNumber();
        object oShouter = GetLastSpeaker();
        object oIntruder;
        // if I recognize the shout and the shouter is a valid, friendly NPC
        if(nMatch != -1 && GetIsObjectValid (oShouter) && !GetIsPC(oShouter) && GetIsFriend (oShouter))
        {
            // and the shout is "NW_ATTACK_MY_TARGET"
            if (nMatch == 5)
            {
                // attempt to define the shouter's enemy
                oIntruder = GetLastHostileActor (oShouter);
                if(!GetIsObjectValid (oIntruder))
                {
                    oIntruder = GetAttemptedAttackTarget();
                    if(!GetIsObjectValid (oIntruder))
                    {
                        oIntruder = GetAttemptedSpellTarget();
                        if(!GetIsObjectValid (oIntruder))
                        {
                            oIntruder = OBJECT_INVALID;
                        }
                    }
                }
                // if I can see neither the shouter nor the enemy
                if (GetIsObjectValid (oShouter) && !GetObjectSeen (oIntruder) && !GetObjectSeen (oShouter))
                {
                    // define the location of the shouter
                    location lShouter = GetLocation(oShouter);
                    // stop what I am doing
                    ClearAllActions();
                    // and move to that location
                    ActionMoveToLocation (lShouter, TRUE);
                }
                // otherwise, if I can see the shouter but not the enemy
                else if (GetIsObjectValid (oShouter) && !GetObjectSeen (oIntruder) && GetObjectSeen (oShouter))
                {
                    // stop what I am doing
                    ClearAllActions();
                    // and move towards the shouter
                    ActionMoveToObject (oShouter, TRUE);
                }
            }
        }
    }
}
