//::///////////////////////////////////////////////
//:: Default On Heartbeat
//:: NW_C2_DEFAULT1
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    This script will have people perform default
    animations.
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Nov 23, 2001
//:://////////////////////////////////////////////
#include "NW_I0_GENERIC"
void SetGoblinActionsByWPTag(string sWPTag)
{
    if (sWPTag == "JHR_WP_ENTRANCE")
    {
        ActionPlayAnimation(ANIMATION_LOOPING_LOOK_FAR, 1.0, 5.0);
    }
    else if (sWPTag == "JHR_WP_SLEEP")
    {
        DelayCommand(60.0, ActionDoCommand(SetLocalInt(OBJECT_SELF, "nBusyFlag", 0)));
        ActionDoCommand(ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectSleep(), OBJECT_SELF, 45.0));
    }
    else if (sWPTag == "JHR_WP_ALTAR")
    {
        ActionDoCommand(SetFacing(199.0f));
        ActionPlayAnimation(ANIMATION_LOOPING_WORSHIP, 1.0, 30.0);
    }
    else if (sWPTag == "JHR_WP_COOK")
    {
        ActionDoCommand(SetFacing(90.0f));
        ActionPlayAnimation(ANIMATION_LOOPING_TALK_FORCEFUL, 1.0, 5.0);
        ActionPlayAnimation(ANIMATION_FIREFORGET_DRINK);
    }
    else if (sWPTag == "JHR_WP_EXIT")
    {
        ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW, 1.0, 3.0);
    }
    else if (sWPTag == "JHR_WP_CHIEF")
    {
        ActionPlayAnimation(ANIMATION_FIREFORGET_SALUTE);
        ActionPlayAnimation(ANIMATION_LOOPING_TALK_PLEADING, 1.0, 7.0);
        ActionPlayAnimation(ANIMATION_LOOPING_WORSHIP, 1.5, 3.0);
    }
    else if (sWPTag == "JHR_WP_WEAPONS")
    {
        ActionPlayAnimation(ANIMATION_LOOPING_GET_MID, 1.0, 3.0);
    }
    // All actions queued - add "not busy any more" flag to queue
    ActionDoCommand(SetLocalInt(OBJECT_SELF, "nBusyFlag", 0));
}


void main()
{
    if(!GetHasEffect(EFFECT_TYPE_SLEEP))
    {
        if(!GetIsPostOrWalking())
        {
            if(!GetIsObjectValid(GetAttemptedAttackTarget()) && !GetIsObjectValid(GetAttemptedSpellTarget()))
            {
                if(!GetIsObjectValid(GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, OBJECT_SELF, 1, CREATURE_TYPE_PERCEPTION, PERCEPTION_SEEN)))
                {
                    if(!GetBehaviorState(NW_FLAG_BEHAVIOR_SPECIAL) && !IsInConversation(OBJECT_SELF))
                    {
                        if(GetSpawnInCondition(NW_FLAG_AMBIENT_ANIMATIONS) || GetSpawnInCondition(NW_FLAG_AMBIENT_ANIMATIONS_AVIAN))
                        {
                            PlayMobileAmbientAnimations();
                        }
                        else if(GetIsEncounterCreature() &&
                        !GetIsObjectValid(GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, OBJECT_SELF, 1, CREATURE_TYPE_PERCEPTION, PERCEPTION_SEEN)))
                        {
                            PlayMobileAmbientAnimations();
                        }
                        else if(GetSpawnInCondition(NW_FLAG_IMMOBILE_AMBIENT_ANIMATIONS) &&
                           !GetIsObjectValid(GetNearestCreature(CREATURE_TYPE_REPUTATION, REPUTATION_TYPE_ENEMY, OBJECT_SELF, 1, CREATURE_TYPE_PERCEPTION, PERCEPTION_SEEN)))
                        {
                            PlayImmobileAmbientAnimations();
                        }
                    }
                    else
                    {
                        DetermineSpecialBehavior();
                    }
                }
                else
                {
                    //DetermineCombatRound();
                }
            }
        }
    }


    if(GetSpawnInCondition(NW_FLAG_HEARTBEAT_EVENT))
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(1001));
    }

    // Sleeping goblins should snore
    if (GetHasEffect(EFFECT_TYPE_SLEEP))
    {
        ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_IMP_SLEEP), OBJECT_SELF);
    }

    // small chance to become "unbusy" in case blocked by other goblins
    // Goblins change their minds 7% of the time
    int nRoll = Random(99) + 1;
    if ((nRoll <= 7) && !(GetIsInCombat()))
    {
         SetLocalInt(OBJECT_SELF, "nBusyFlag", 0);
         ClearAllActions();
         ActionRandomWalk();
    }

    // 15% chance to say something random
    nRoll = Random(99) + 1;
    if (nRoll <= 15)
    {
        ClearAllActions();
        int nSoundToPlay = Random(5) + 1;
        string sSoundName;
        switch (nSoundToPlay)
        {
            case 1:
                sSoundName = "c_goblin_slct";
                break;
            case 2:
                sSoundName = "c_goblin_bat1";
                break;
            case 3:
                sSoundName = "c_goblin_bat2";
                break;
            case 4:
                sSoundName = "c_goblinwiz_bat1";
                break;
            case 5:
                sSoundName = "c_goblinchf_bat1";
                break;
        }
        // say something
        PlaySound(sSoundName);

        // go back to what you were doing
        string sWPTag = GetLocalString(OBJECT_SELF, "sCurrentWPTag");
        ActionMoveToObject(GetObjectByTag(sWPTag));
        SetGoblinActionsByWPTag(sWPTag);
    }

    // Get something to do if not busy
    if (GetLocalInt(OBJECT_SELF, "nBusyFlag") == 0)
    {
        ClearAllActions();
        // Set waypoint tag randomly; default to last wp
        nRoll = Random(7) + 1;
        string sWPTag = GetLocalString(OBJECT_SELF, "sCurrentWPTag");
        switch (nRoll)
        {
            case 1:
                sWPTag = "JHR_WP_ENTRANCE";
                break;
            case 2:
                sWPTag = "JHR_WP_SLEEP";
                break;
            case 3:
                sWPTag = "JHR_WP_ALTAR";
                break;
            case 4:
                sWPTag = "JHR_WP_COOK";
                break;
            case 5:
                sWPTag = "JHR_WP_EXIT";
                break;
            case 6:
                sWPTag = "JHR_WP_CHIEF";
                break;
            case 7:
                sWPTag = "JHR_WP_WEAPONS";
                break;
        }
        // Set WP variable
        SetLocalString(OBJECT_SELF, "sCurrentWPTag", sWPTag);
        // Set busy flag
        SetLocalInt(OBJECT_SELF, "nBusyFlag", 1);
        // Go to the waypoint
        ActionMoveToObject(GetObjectByTag(sWPTag));
        // Conditional actions after arriving based on location
        SetGoblinActionsByWPTag(sWPTag);

        // Just in case by WP tag doesn't work
        //SetGoblinActionsByNum(nRoll);
    }
}


