#include "nw_i0_generic"
#include "x2_inc_switches"
#include "inc_AI"

void DoPhysical( object oTarget, object oCaster )
{
    if ( !GetIsObjectValid(oTarget) || !GetIsEnemy(oTarget) )
    {
        oTarget = GetNearestEnemy();
        if ( !GetIsObjectValid(oTarget) )
            return;
    }
    if ( d3() == 1 && GetHasSpell(SPELLABILITY_DRAGON_BREATH_COLD) )
    {
        ActionCastSpellAtObject(SPELLABILITY_DRAGON_BREATH_COLD,oTarget);
    }
    else
    {
        TalentMeleeAttack(oTarget);
    }
}

void main()
{
    // The following two lines should not be touched
    object oIntruder = GetCreatureOverrideAIScriptTarget();
    ClearCreatureOverrideAIScriptTarget();
    int bAura = GetLocalInt(OBJECT_SELF,"aura");
    if ( !bAura )
    {
        string sScript = GetLocalString(OBJECT_SELF,"Aura_Script");
        if ( sScript != "" )
            ExecuteScript(sScript,OBJECT_SELF);
        SetLocalInt(OBJECT_SELF,"aura",1);
    }
    if ( !GetCanAct(OBJECT_SELF) )
    {
        SetCreatureOverrideAIScriptFinished();
        return;
    }
    if ( GetLocalInt(OBJECT_SELF,"Acting") )
    {
        SetCreatureOverrideAIScriptFinished();
        return;
    }
/*    if ( d6() == 1 ) // in case they get busy-stuck, also to add randomness
    {
        SetLocalInt(OBJECT_SELF,"busy",FALSE);
        return;
    } */
    // Don't inturupt actions in progress


    if ( GetCurrentAction() == ACTION_CASTSPELL )
    {
        SetCreatureOverrideAIScriptFinished();
        return;
    }

    if ( GetLocalInt(OBJECT_SELF,"physical") )
    {
        ClearAllActions();
        DoPhysical(oIntruder,OBJECT_SELF);
        return;
    }
    // Haste = Super awesome;
    // Get those summons in there!
    int nSummonCount = GetLocalInt(OBJECT_SELF,"SummonCount");
    if ( !GetIsObjectValid(GetAssociate(ASSOCIATE_TYPE_SUMMONED)) &&  nSummonCount < 5 )
    {
        nSummonCount++;
        SetLocalInt(OBJECT_SELF,"SummonCount",nSummonCount);
        ClearAllActions();
        ActionCastFakeSpellAtLocation(SPELL_SUMMON_CREATURE_IX,GetLocation(OBJECT_SELF));
        effect eSummon = EffectSummonCreature("NW_DRGWHITE002",VFX_FNF_SUMMON_MONSTER_3,1.0);

        location lSelf;
        object oTarget =  FindSingleRangedTarget();
        if(GetIsObjectValid(oIntruder))
        {
            vector vTarget = GetPosition(oIntruder);
            vector vSource = GetPosition(OBJECT_SELF);
            vector vDirection = vTarget - vSource;
            float fDistance = VectorMagnitude(vDirection) / 2.0f;
            vector vPoint = VectorNormalize(vDirection) * fDistance + vSource;
            lSelf = Location(GetArea(OBJECT_SELF), vPoint, GetFacing(OBJECT_SELF));
            //lSelf = GetLocation(oTarget);
        }
        else
        {
           lSelf = GetLocation(OBJECT_SELF);
        }


        ApplyEffectAtLocation(DURATION_TYPE_TEMPORARY,eSummon,lSelf,HoursToSeconds(24));
        SetCreatureOverrideAIScriptFinished();
        return;
    }


    // Hostile targeting actions
    if ( !GetIsObjectValid(oIntruder) || !GetIsReactionTypeHostile(oIntruder) )
    {
        oIntruder = GetNearestPerceivedEnemy();
        if ( !GetIsObjectValid(oIntruder) )
            return;
    }

    // Melee up sometimes!

    if ( d6() == 6 )
    {
        switch ( d4() )
        {
            case 1:
                PlayVoiceChat(VOICE_CHAT_BATTLECRY1);
                break;
            case 2:
                PlayVoiceChat(VOICE_CHAT_ATTACK);
                break;
            case 3:
                PlayVoiceChat(VOICE_CHAT_BATTLECRY2);
                break;
            case 4:
                PlayVoiceChat(VOICE_CHAT_BATTLECRY3);
                break;
        }

        SetLocalInt(OBJECT_SELF,"physical",1);
        DelayCommand(24.0,SetLocalInt(OBJECT_SELF,"physical",0));
        ClearAllActions();
        DoPhysical(oIntruder,OBJECT_SELF);
        return;
    }
}