//:://////////////////////////////////////////////////////////////////////////
//:: Bard Song: Inspire Legion
//:: NW_S2_SngInLegn
//:: Created By: Jesse Reynolds (JLR-OEI)
//:: Created On: 04/06/06
//:: Copyright (c) 2005 Obsidian Entertainment Inc.
//:://////////////////////////////////////////////////////////////////////////
/*
    This spells applies bonuses to all allies
    within 60 feet.
*/
//:: PKM-OEI 07.13.06 VFX Pass
//:: AFW-OEI 07/14/2006: Check to see if you have bardsong feats left; if you do, execute
//::	script and decrement a bardsong use.
//:: PKM-OEI 07.20.06 Added Perform skill check

#include "x0_i0_spells"
#include "nwn2_inc_spells"
#include "x2_inc_spellhook"

void mainSpell();
void main()
{
	AssignCommand(JXGetCaster(), mainSpell());
}

void mainSpell()
{
	if (!X2PreSpellCastCode())
	{
		JXPostSpellCastCode();
		return;
	}


	if ( GetCanBardSing( OBJECT_SELF ) == FALSE )
	{
		JXPostSpellCastCode();
		return; // Awww :(	
	}

    if (!GetHasFeat(FEAT_BARD_SONGS, OBJECT_SELF))
    {
        FloatingTextStrRefOnCreature(STR_REF_FEEDBACK_NO_MORE_BARDSONG_ATTEMPTS,OBJECT_SELF); // no more bardsong uses left
        JXPostSpellCastCode();
		return;
    }
	
	int		nPerform	= GetSkillRank(SKILL_PERFORM);
	 
	if (nPerform < 21 ) //Checks your perform skill so nubs can't use this song
	{
		FloatingTextStrRefOnCreature ( 182800, OBJECT_SELF );
		JXPostSpellCastCode();
		return;
	}


    //Declare major variables
    int nDuration   = ApplySongDurationFeatMods( 10, OBJECT_SELF ); // Rounds
    float fDuration = RoundsToSeconds(nDuration);
    int nDamage     = 4;
    int nBABMin     = 0;
    object oLeader = GetFactionLeader( OBJECT_SELF );
    int bPCOnly    = FALSE;
    int nTstBonus  = 0;
	
    object oTarget = GetFirstFactionMember( oLeader , bPCOnly );
    while ( GetIsObjectValid( oTarget ) )
    {
        nTstBonus = GetTRUEBaseAttackBonus(oTarget);
        if(nBABMin < nTstBonus)
        {
            nBABMin  = nTstBonus;
        }
        oTarget = GetNextFactionMember( oLeader, bPCOnly );
    }

    effect eDamage = ExtraordinaryEffect( EffectDamageIncrease(nDamage, DAMAGE_TYPE_BLUDGEONING) );
    effect eBAB    = ExtraordinaryEffect( EffectBABMinimum(nBABMin) );
    effect eDur    = ExtraordinaryEffect( EffectVisualEffect(VFX_HIT_BARD_INS_LEGION) );
    effect eLink   = ExtraordinaryEffect( EffectLinkEffects(eDamage, eBAB) );
    eLink          = ExtraordinaryEffect( EffectLinkEffects(eLink, eDur) );

//    if(AttemptNewSong(OBJECT_SELF))
    {
	    effect eFNF    = ExtraordinaryEffect( EffectVisualEffect(VFX_DUR_BARD_SONG) );
	    JXApplyEffectAtLocation(DURATION_TYPE_INSTANT, eFNF, GetLocation(OBJECT_SELF));

        ApplyFriendlySongEffectsToArea( OBJECT_SELF, JXGetSpellId(), fDuration, RADIUS_SIZE_COLOSSAL, eLink );
	    DecrementRemainingFeatUses(OBJECT_SELF, FEAT_BARD_SONGS);
    }

	JXPostSpellCastCode();
}
