/*
    PROJECT Q IV
    (c) 2022 by Pstemarie

    Purple Worm OnPerception Event Script

    Version: 1.0
    Created: 28 Jan 2003
    Author:  Lacero
    v1.1:    18 June 2022 Pstemarie - adapted for giant frogs

*/
#include "inc_utility"
#include "NW_I0_GENERIC"
#include "q_i0_giantfrog"

void main()
{
    if(GetLocalInt(OBJECT_SELF,"PW_BURROWING")==TRUE)
    {
        if(GetLastPerceptionSeen()&&(GetLocalInt(OBJECT_SELF,"PW_SURFACED")!=TRUE))
        {
            SetLocalInt(OBJECT_SELF,"PW_SURFACED",TRUE);
            effect eF = EffectAppear();
            ApplyEffectToObject(DURATION_TYPE_INSTANT,eF,OBJECT_SELF);
            ApplyEffectAtLocation ( DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SCREEN_SHAKE), GetLocation(OBJECT_SELF));
            DelayCommand(4.0, gf_ResetBurrowing());
        }
        return;
    }

    //This is the equivalent of a force conversation bubble, should only be used if you want an NPC
    //to say something while he is already engaged in combat.
    if(GetSpawnInCondition(NW_FLAG_SPECIAL_COMBAT_CONVERSATION) && GetIsPC(GetLastPerceived()) && GetLastPerceptionSeen())
    {
        SpeakOneLinerConversation();
    }
    //If the last perception event was hearing based or if someone vanished then go to search mode
    if ((GetLastPerceptionVanished()) && GetIsEnemy(GetLastPerceived()))
    {
        object oGone = GetLastPerceived();
        if((GetAttemptedAttackTarget() == GetLastPerceived() ||
           GetAttemptedSpellTarget() == GetLastPerceived() ||
           GetAttackTarget() == GetLastPerceived()) && GetArea(GetLastPerceived()) != GetArea(OBJECT_SELF))
        {
           ClearAllActions();
           gf_DetermineCombatRound();
        }
    }
    //Do not bother checking the last target seen if already fighting
    else if(!GetIsObjectValid(GetAttemptedAttackTarget()) && !GetIsObjectValid(GetAttemptedSpellTarget()))
    {
        //Check if the last percieved creature was actually seen
        if(GetLastPerceptionSeen())
        {
            if(GetIsEnemy(GetLastPerceived()))
            {
                if(!PRCGetHasEffect(EFFECT_TYPE_SLEEP))
                {
                    SetFacingPoint(GetPosition(GetLastPerceived()));
                    SpeakString("NW_I_WAS_ATTACKED", TALKVOLUME_SILENT_TALK);
                    gf_DetermineCombatRound();
                }
            }
            //Linked up to the special conversation check to initiate a special one-off conversation
            //to get the PCs attention
            else if(GetSpawnInCondition(NW_FLAG_SPECIAL_CONVERSATION) && GetIsPC(GetLastPerceived()))
            {
                ActionStartConversation(OBJECT_SELF);
            }
        }
    }
    if(GetSpawnInCondition(NW_FLAG_PERCIEVE_EVENT) && GetLastPerceptionSeen())
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(1002));
    }
}

