//::///////////////////////////////////////////////
//:: Fireball
//:: NW_S0_Fireball
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
// A fireball is a burst of flame that detonates with
// a low roar and inflicts 1d6 points of damage per
// caster level (maximum of 10d6) to all creatures
// within the area. Unattended objects also take
// damage. The explosion creates almost no pressure.
*/
//:://////////////////////////////////////////////
//:: Created By: Noel Borstad
//:: Created On: Oct 18 , 2000
//:://////////////////////////////////////////////
//:: Last Updated By: Preston Watamaniuk, On: April 6, 2001
//:: Last Updated By: AidanScanlan, On: April 11, 2001
//:: Last Updated By: Preston Watamaniuk, On: May 25, 2001

#include "nw_i0_2q4luskan"
//#include "X0_I0_SPELLS"
#include "prc_inc_spells"
#include "x2_inc_spellhook"
#include "prc_add_spell_dc"

void main()
{
    object oCaster = OBJECT_SELF;
    object oPC = GetFirstPC();
    int nUA = GetLocalInt(oPC, "Underwater");
    effect eFail = SupernaturalEffect(EffectSpellFailure());
    //Spell fails if caster is underwater.
    if (nUA == 1)
    {
    ApplyEffectToObject(DURATION_TYPE_TEMPORARY,eFail,oCaster,0.1);
    FloatingTextStringOnCreature("The spell 'Fireball' will not work underwater!", oPC);
    return;
    }
/*
  Spellcast Hook Code
  Added 2003-06-20 by Georg
  If you want to make changes to all spells,
  check x2_inc_spellhook.nss to find out more

*/

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

// End of Spell Cast Hook


    //Declare major variables
    int nCasterLvl = PRCGetCasterLevel(oCaster);
    int nMetaMagic = PRCGetMetaMagicFeat();
    int nDamage;
    float fDelay;
    effect eExplode = EffectVisualEffect(VFX_FNF_FIREBALL);
    effect eVis = EffectVisualEffect(VFX_IMP_FLAME_M);
    effect eDam;
    // Shake screen and play sound for cave-inif in Dungeon
    int nDL=GetLocalInt(oPC, "nDungeonLevel");
    if (nDL == 1)
     {
     effect eShake = EffectVisualEffect(VFX_FNF_SCREEN_SHAKE);
     DelayCommand(2.0, ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eShake, GetLocation(oPC)));
     DelayCommand(2.0, PlaySound("as_na_rockfallg1"));
     DelayCommand(3.0, FloatingTextStringOnCreature("The spell 'Fireball' has caused a cave-in!", oPC));
     }
    //Get the spell target location as opposed to the spell target.
    location lTarget = PRCGetSpellTargetLocation();
    //Limit Caster level for the purposes of damage
    if (nCasterLvl > 10)
    {
        nCasterLvl = 10;
    }
    //Apply the fireball explosion at the location captured above.
    ApplyEffectAtLocation(DURATION_TYPE_INSTANT, eExplode, lTarget);
    //Declare the spell shape, size and the location.  Capture the first target object in the shape.
    object oTarget = GetFirstObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_HUGE, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE);
    //Cycle through the targets within the spell shape until an invalid object is captured.
    while (GetIsObjectValid(oTarget))
    {
            if((PRCGetSpellId() == 341) || PRCGetSpellId() == 58)
            {
                //Fire cast spell at event for the specified target
                SignalEvent(oTarget, EventSpellCastAt(OBJECT_SELF, SPELL_FIREBALL));
                //Get the distance between the explosion and the target to calculate delay
                fDelay = GetDistanceBetweenLocations(lTarget, GetLocation(oTarget))/20;
                if (!PRCDoResistSpell(OBJECT_SELF, oTarget, FloatToInt((fDelay))))
                {
                    //Roll damage for each target
                    nDamage = d6(nCasterLvl);
                    //Resolve metamagic
                    if (nMetaMagic == METAMAGIC_MAXIMIZE)
                    {
                        nDamage = 6 * nCasterLvl;
                    }
                    else if (nMetaMagic == METAMAGIC_EMPOWER)
                    {
                       nDamage = nDamage + nDamage / 2;
                    }
                    //Adjust the damage based on the Reflex Save, Evasion and Improved Evasion.
                    nDamage = PRCGetReflexAdjustedDamage(nDamage, oTarget, PRCGetSpellSaveDC(), SAVING_THROW_TYPE_FIRE);
                    //Set the damage effect
                    eDam = EffectDamage(nDamage, DAMAGE_TYPE_FIRE);
                    if(nDamage > 0)
                    {
                        // Apply effects to the currently selected target.
                        DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDam, oTarget));
                        //This visual effect is applied to the target object not the location as above.  This visual effect
                        //represents the flame that erupts on the target not on the ground.
                        DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, oTarget));
                    }
                    //Cave-in Damage is applied
                    if (nDL == 1)
                     {
                     int nDam1 = d10();
                     int nDam2 = d10();
                     int nDamage = (nDam1 + nDam2);
                     effect eDamage = EffectDamage(nDamage, DAMAGE_TYPE_BLUDGEONING, DAMAGE_POWER_NORMAL);
                     DelayCommand(2.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_CHUNK_STONE_MEDIUM), oTarget));
                     DelayCommand(2.0, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDamage, oTarget));
                     }
                 //End If Resist
                 }
              //End If Spell ID
              }
       //Select the next target within the spell shape.
       oTarget = GetNextObjectInShape(SHAPE_SPHERE, RADIUS_SIZE_HUGE, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE);
      //End While
      }
}

