/*
    PROJECT Q IV
    (c) 2022 by Pstemarie

    Mimic OnUserDefined Event Script

    OnPhysicallyAttacked - attacker must make a DC 16 Reflex save or lose the
    melee weapon they damaged it with. If the attack was an unarmed attack, the
    attacker must make a DC 16 Reflex save or be held fast.

    OnSpellCastAt - if a touch attack spell, attacker must make a DC 16 Reflex
    save or be held fast.

    OnDamaged - 50% chance to drop an item from inventory if damaged.

    Version: 1.0
    Created: 10 Oct 2021
    Author:  Pstemarie
    v1.1:    2 Nov 2021 Pstemarie - abort if on EASY or VERY_EASY game difficulty
    v1.2:    6 Jun 2022 Pstemarie - added GetIsHostileTouchSpell (by Daz) and OnDamaged event

*/
#include "prc_inc_spells"
//#include "x2_i0_spells"

// By Daz
int GetIsHostileTouchSpell(int nSpell)
{
    return Get2DAString("spells", "Range", nSpell) == "T" && Get2DAString("spells", "HostileSetting", nSpell) == "1";
}


void main()
{
    int nUser = GetUserDefinedEventNumber();

    if(nUser == EVENT_ATTACKED)
    {
        //* Pstemarie - abort if on EASY or VERY_EASY game difficulty
        if (GetGameDifficulty() < GAME_DIFFICULTY_NORMAL)
            return;

        object oAttacker = GetLastAttacker();
        object oWeapon = GetLastWeaponUsed(oAttacker);

        if (IPGetIsMeleeWeapon(oWeapon) == TRUE )
        {
            if(!PRCMySavingThrow(SAVING_THROW_REFLEX, oAttacker, 16,  SAVING_THROW_TYPE_NONE, OBJECT_SELF))
            {
                // Snatch the weapon into the mimic's inventory
                CopyItem(oWeapon, OBJECT_SELF, TRUE);
                DestroyObject(oWeapon, 0.5);
            }
        }
        else
        {
            // If using an unarmed melee attack
            if (GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oAttacker) == OBJECT_INVALID &&
                GetDistanceBetween(oAttacker, OBJECT_SELF) <= 2.25)
            {
                //Make Reflex save
                if (!PRCMySavingThrow(SAVING_THROW_REFLEX, oAttacker, 16))
                {
                    effect eParal = EffectParalyze();
                    effect eVis = EffectVisualEffect(82);
                    effect eDur = EffectVisualEffect(VFX_DUR_CESSATE_NEGATIVE);
                    effect eDur2 = EffectVisualEffect(VFX_DUR_PARALYZED);
                    effect eDur3 = EffectVisualEffect(VFX_DUR_PARALYZE_HOLD);

                    effect eLink = EffectLinkEffects(eDur2, eDur);
                    eLink = EffectLinkEffects(eLink, eParal);
                    eLink = EffectLinkEffects(eLink, eVis);
                    eLink = EffectLinkEffects(eLink, eDur3);

                    ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oAttacker, RoundsToSeconds(1));
                }
            }
        }
    }
    else if (nUser == EVENT_SPELL_CAST_AT)
    {
        //* Pstemarie - abort if on EASY or VERY_EASY game difficulty
        if (GetGameDifficulty() < GAME_DIFFICULTY_NORMAL)
            return;

        if (GetIsHostileTouchSpell(GetLastSpell()))
        {
            object oAttacker = GetLastSpellCaster();

            //Make Reflex save
            if (!PRCMySavingThrow(SAVING_THROW_REFLEX, oAttacker, 16))
            {
                effect eParal = EffectParalyze();
                effect eVis = EffectVisualEffect(82);
                effect eDur = EffectVisualEffect(VFX_DUR_CESSATE_NEGATIVE);
                effect eDur2 = EffectVisualEffect(VFX_DUR_PARALYZED);
                effect eDur3 = EffectVisualEffect(VFX_DUR_PARALYZE_HOLD);

                effect eLink = EffectLinkEffects(eDur2, eDur);
                eLink = EffectLinkEffects(eLink, eParal);
                eLink = EffectLinkEffects(eLink, eVis);
                eLink = EffectLinkEffects(eLink, eDur3);

                ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oAttacker, RoundsToSeconds(1));
            }
        }
    }
    else if (nUser == EVENT_DAMAGED)
    {
        if (d100() > 50 )
        {
            object oItem = GetFirstItemInInventory();
            object oDrop;

            // This loop counts the number of items in the mimic's inventory
            // (except equipped items)
            int nCount = 0;
            while (GetIsObjectValid(oItem) )
            {
                nCount = nCount +1;
                SetLocalInt(oItem, "InventoryCount", nCount);

                oItem = GetNextItemInInventory();
            }

            // If the mimic has an inventory drop a random item
            if (nCount > 1)
            {
                // Determine the item to drop (0 - ???)
                int nTake = Random(nCount);

                // Reset the loop counter
                nCount = 0;

                // Loop through the mimic's inventory again
                oItem = GetFirstItemInInventory();
                while (GetIsObjectValid(oItem) )
                {
                    // If nTake and nCount are equal drop the item and exit the loop
                    if (nTake == nCount)
                    {
                        ClearAllActions();
                        ActionPutDownItem(oItem);
                        break;
                    }

                    // Index the loop counter
                    nCount = nCount +1;

                    // Get the next item in the mimic's inventory
                    oItem = GetNextItemInInventory();
                }
            }
        }
    }
}
