//::///////////////////////////////////////////////
//:: Flame Weapon
//:: X2_S0_FlmeWeap
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
//  Gives a melee weapon 1d4 fire damage +1 per caster
//  level to a maximum of +10.
  3.5: Gives a melee weapon +1d6 fire damage (flat).
*/
//:://////////////////////////////////////////////
//:: Created By: Andrew Nobbs
//:: Created On: Nov 29, 2002
//:://////////////////////////////////////////////
//:: Updated by Andrew Nobbs May 08, 2003
//:: 2003-07-07: Stacking Spell Pass, Georg Zoeller
//:: 2003-07-15: Complete Rewrite to make use of Item Property System


// (Update JLR - OEI 07/26/05) -- 3.5 Update
// (Update BDF - OEI 08/29/05) -- Corrected the spellID param value of ItemPropertyOnHitCastSpell
// AFW-OEI 07/23/2007: If it's going to be fixed fire damage, forget the fancy on-hit spell and just
//	go with our elemental damage item property.  Also, increase to 1d8.



#include "nw_i0_spells"
#include "x2_i0_spells"

#include "x2_inc_spellhook"


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

void mainSpell()
{
    if (!X2PreSpellCastCode())
    {   // If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
		JXPostSpellCastCode();
        return;
    }

    int nDuration = 2 * JXGetCasterLevel(OBJECT_SELF);
	
    int nMetaMagic = JXGetMetaMagicFeat();
    if (nMetaMagic & METAMAGIC_EXTEND)
    {
        nDuration = nDuration * 2; //Duration is +100%
    }
	
	object oMyWeapon   =  IPGetTargetedOrEquippedMeleeWeapon();	
	
	if(GetIsObjectValid(oMyWeapon) )
	{
		SignalEvent(GetItemPossessor(oMyWeapon), EventSpellCastAt(OBJECT_SELF, JXGetSpellId(), FALSE));
		
		if (nDuration > 0)
		{
			itemproperty ipFlame = ItemPropertyDamageBonus(IP_CONST_DAMAGETYPE_FIRE, IP_CONST_DAMAGEBONUS_1d8);
			IPSafeAddItemProperty(oMyWeapon, ipFlame, TurnsToSeconds(nDuration), X2_IP_ADDPROP_POLICY_REPLACE_EXISTING, FALSE, TRUE);
		}
		JXPostSpellCastCode();
		return;
	 }
	 else
	 {
		FloatingTextStrRefOnCreature(83615, OBJECT_SELF);
		JXPostSpellCastCode();
		return;
	 }

     JXPostSpellCastCode();
}

/* AFW-OEI 07/23/2007: Keep around for posterity.
void AddFlamingEffectToWeapon(object oTarget, float fDuration, int nCasterLevel)
{
	//SpawnScriptDebugger();
	
   // If the spell is cast again, any previous itemproperties matching are removed.
   // JLR - OEI 07/26/05 -- Now it does a fixed 1d6 Fire dmg:
//   IPSafeAddItemProperty(oTarget, ItemPropertyOnHitCastSpell(124,nCasterLevel), fDuration, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING);
	itemproperty ipOnHitCastSpell = ItemPropertyOnHitCastSpell( 141, nCasterLevel );
	itemproperty ipVis = ItemPropertyVisualEffect( ITEM_VISUAL_FIRE );
	
   	IPSafeAddItemProperty(oTarget, ipOnHitCastSpell, fDuration, X2_IP_ADDPROP_POLICY_REPLACE_EXISTING);
   	IPSafeAddItemProperty(oTarget, ipVis, fDuration,X2_IP_ADDPROP_POLICY_REPLACE_EXISTING,FALSE,TRUE);
	
   	return;
}

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

void mainSpell()
{
    if (!X2PreSpellCastCode())
    {
    // If code within the PreSpellCastHook (i.e. UMD) reports FALSE, do not run this spell
        JXPostSpellCastCode();
		return;
    }
    // End of Spell Cast Hook


    //Declare major variables
    //effect eVis = EffectVisualEffect(VFX_IMP_PULSE_FIRE);	// NWN1 VFX
    //effect eDur = EffectVisualEffect(VFX_DUR_CESSATE_POSITIVE);	// NWN1 VX
    effect eDur = EffectVisualEffect( VFX_DUR_SPELL_FLAME_WEAPON );	// NWN2 VFX
    int iCasterLevel = JXGetCasterLevel(OBJECT_SELF);
    int nDuration = 2 * iCasterLevel;
    int nMetaMagic = JXGetMetaMagicFeat();
    int nCasterLvl = iCasterLevel;

    //Limit nCasterLvl to 10, so it max out at +10 to the damage.
    if(nCasterLvl > 10)
    {
        nCasterLvl = 10;
    }

    if (nMetaMagic & METAMAGIC_EXTEND)
    {
        nDuration = nDuration * 2; //Duration is +100%
    }

   object oMyWeapon   =  IPGetTargetedOrEquippedMeleeWeapon();

   if(GetIsObjectValid(oMyWeapon) )
   {
        SignalEvent(GetItemPossessor(oMyWeapon), EventSpellCastAt(OBJECT_SELF, JXGetSpellId(), FALSE));

        if (nDuration>0)
        {
            // haaaack: store caster level on item for the on hit spell to work properly
            //JXApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, GetItemPossessor(oMyWeapon));	// NWN1 VX
            JXApplyEffectToObject(DURATION_TYPE_TEMPORARY, eDur, GetItemPossessor(oMyWeapon), TurnsToSeconds(nDuration));
            AddFlamingEffectToWeapon(oMyWeapon, TurnsToSeconds(nDuration),nCasterLvl);

         }
            { JXPostSpellCastCode(); return; }
    }
     else
    {
           FloatingTextStrRefOnCreature(83615, OBJECT_SELF);
           { JXPostSpellCastCode(); return; }
    }

	JXPostSpellCastCode();
}
*/
