//::///////////////////////////////////////////////
//:: x2_s3_sequencer
//:: Copyright (c) 2003 Bioware Corp.
//:://////////////////////////////////////////////
/*
    Fires the spells stored on this sequencer.
    GZ: - Also handles clearing off spells if the
          item has the clear sequencer property
        - added feedback strings
*/
//:://////////////////////////////////////////////
//:: Created By: Brent
//:: Created On: July 31, 2003
//:: Updated By: Georg
//:://////////////////////////////////////////////

// This will return the spell level of the spell being cast
int GetSpellLevel(int nSpell);
// Returns the max spell level of a pearl of power.
// Returns FALSE if not a pearl of power.
int GetIsPearlOfPower(object oItem);

#include "prc_x2_itemprop"
void main()
    {
    object oItem = GetSpellCastItem();
    object oPC =   OBJECT_SELF;
    int iPearl = GetIsPearlOfPower(oItem);
    int i = 0;
    int nSpellId = -1;
    int nMode = GetSpellId();

    int iMax = IPGetItemSequencerProperty(oItem);

    if (iMax ==0) // Should never happen unless you added clear sequencer to a non sequencer item
        {
        return;
        }
    if (nMode == 720 ) // clear seqencer
        {
        for (i = 1; i <= iMax; i++)
            {
            DeleteLocalInt(oItem, "X2_L_SPELLTRIGGER" + IntToString(i));
            }
        DeleteLocalInt(oItem, "X2_L_NUMTRIGGERS");
        effect eClear = EffectVisualEffect(VFX_IMP_BREACH);
        ApplyEffectToObject(DURATION_TYPE_INSTANT,eClear,OBJECT_SELF);
        FloatingTextStrRefOnCreature(83882,OBJECT_SELF); // sequencer cleared
        }
    else
        {
        int bSuccess = FALSE;
        for (i = 1; i <= iMax; i++)
            {
            nSpellId = GetLocalInt(oItem, "X2_L_SPELLTRIGGER" + IntToString(i));
            if (nSpellId>0)
                {
                nSpellId --; // I added +1 to the spellID when the sequencer was created, so I have to remove it here
                // Begin custom coding for pearls of power.
                if (iPearl)
                    {
                    if (GetSpellLevel(nSpellId) > iPearl)
                        {
                        FloatingTextStringOnCreature("Spell was to high for this pearl",OBJECT_SELF);
                        return;
                        }
                    }
                // End Pearl Check
                bSuccess = TRUE;
                ActionCastSpellAtObject(nSpellId, oPC, METAMAGIC_ANY, TRUE, 0, PROJECTILE_PATH_TYPE_DEFAULT, TRUE);
                }
            // Clear Pearl of Power/Ring of Spell Storing spell.
            if (iPearl || GetTag(oItem) == "ring038")
                {
                for (i = 1; i <= iMax; i++)
                    {
                    DeleteLocalInt(oItem, "X2_L_SPELLTRIGGER" + IntToString(i));
                    }
                DeleteLocalInt(oItem, "X2_L_NUMTRIGGERS");
                // End custom scripting
                }
            }
        if (!bSuccess)
            {
            FloatingTextStrRefOnCreature(83886,OBJECT_SELF); // no spells stored
            }
        }
}

int GetSpellLevel(int nSpell)
    {
    string sTag;
    int iSpellClass = GetLastSpellCastClass();
    switch (iSpellClass)
        {
        case CLASS_TYPE_BARD:       {sTag = "Bard";break;}
        case CLASS_TYPE_CLERIC:     {sTag = "Cleric";break;}
        case CLASS_TYPE_DRUID:      {sTag = "Druid";break;}
        case CLASS_TYPE_PALADIN:    {sTag = "Paladin";break;}
        case CLASS_TYPE_RANGER:     {sTag = "Ranger";break;}
        case CLASS_TYPE_SORCERER:
        case CLASS_TYPE_WIZARD:     {sTag = "Wiz_Sorc";break;}
        case CLASS_TYPE_INVALID:    {sTag = "Innate";break;}
        };
     sTag = Get2DAString("spells",sTag,nSpell);
     return StringToInt(sTag);
     }

int GetIsPearlOfPower(object oItem)
    {
    string sTag = GetTag(oItem);
    if (sTag == "misca020")
        return 1;
    else if (sTag == "misca065")
        return 2;
    else if (sTag == "miscb020")
        return 3;
    else if (sTag == "miscb059")
        return 4;
    else if (sTag == "miscb087")
        return 5;
    else if (sTag == "miscc022")
        return 6;
    else if (sTag == "miscc035")
        return 7;
    else if (sTag == "miscc056"
     || sTag == "miscc058")
        return 8;
    else if (sTag == "miscc064")
        return 9;
    return FALSE;
    }
