#include "prc_x2_itemprop"
// Removes a ITEM_PROPERTY_* effect from an object.
void RDGRemoveItemProperty(object oItem,int ItemPropertyType);
// Returns the ammo slot for that weapon.
int GetAmmoSlot(object oWeapon);
void main ()
    {
    object oPC = GetPCItemLastUnequippedBy();
    object oItem = GetPCItemLastUnequipped();
    string sTag = GetTag(oItem);
    effect eEffect = GetFirstEffect(oPC);
    int iType = GetBaseItemType(oItem);
    object oTemp;
    object oSkin = GetItemInSlot(INVENTORY_SLOT_CARMOUR,oPC);
    itemproperty ipItem;
    object oAmmo;
    object oWeapon = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND,oPC);
    int iDamageType,iTemp;
    DeleteLocalObject(oSkin,sTag);
    while (GetIsEffectValid(eEffect))
        {
        if (GetEffectCreator(eEffect) == oItem)
            RemoveEffect(oPC,eEffect);
        eEffect = GetNextEffect(oPC);
        }
    if (iType = BASE_ITEM_ARMOR)
        {
        if (sTag == "chains601"
         || sTag == "chain601"
         || sTag == "fplate601")
            RDGRemoveItemProperty(oItem,ITEM_PROPERTY_ARCANE_SPELL_FAILURE);
        if (sTag == "hide601")
            RDGRemoveItemProperty(oItem,ITEM_PROPERTY_BONUS_FEAT);
        }
    if (GetBaseItemType(oItem) == BASE_ITEM_TORCH)
        {
        if (sTag != "misca003" // Everburning Torch
         && sTag != "miscc002")// Torch of Revealing
            DestroyObject(oItem);
        }
    // Bracer of archery rules.
    if (GetLocalInt(oSkin,"misca081"))
        {
        if (GetWeaponRanged(oItem))
            {
            oTemp = GetItemInSlot(INVENTORY_SLOT_ARMS,oPC);
            eEffect = GetFirstEffect(oPC);
            while (GetIsEffectValid(eEffect))
                {
                if (GetEffectCreator(eEffect) == oTemp)
                    RemoveEffect(oPC,eEffect);
                eEffect = GetNextEffect(oPC);
                }
            }
        }
    // If a ranged weapon is removed change the properties of the ammunition
    if (GetLocalInt(oItem,"SpecialAmmo"))
        {
        oAmmo = GetItemInSlot(GetAmmoSlot(oItem),oPC);
        if (!GetIsObjectValid(oAmmo))
            return;
        iTemp = GetLocalInt(oAmmo,"TempEnc");
        iDamageType = GetLocalInt(oWeapon,"DamageType");
        if (iTemp == ITEM_PROPERTY_DAMAGE_BONUS
         || ITEM_PROPERTY_DAMAGE_BONUS_VS_ALIGNMENT_GROUP
         || ITEM_PROPERTY_DAMAGE_BONUS_VS_RACIAL_GROUP)
            {
            IPRemoveMatchingItemProperties(oAmmo,iTemp,DURATION_TYPE_PERMANENT,iDamageType);
            DeleteLocalInt(oAmmo,"TempEnc");
            }
        }
    // Remove weapon bonus from ammo if was enchanted by a weapon.
    else if (GetLocalInt(oItem,"TempEnc"))
        {
        iTemp = GetLocalInt(oItem,"TempEnc");
        iDamageType = GetLocalInt(oItem,"DamageType");
        if (iTemp == ITEM_PROPERTY_DAMAGE_BONUS
         || ITEM_PROPERTY_DAMAGE_BONUS_VS_ALIGNMENT_GROUP
         || ITEM_PROPERTY_DAMAGE_BONUS_VS_RACIAL_GROUP)
            {
            IPRemoveMatchingItemProperties(oItem,iTemp,DURATION_TYPE_PERMANENT,iDamageType);
            DeleteLocalInt(oItem,"TempEnc");
            DeleteLocalInt(oItem,"DamageType");
            }
        }
    }

void RDGRemoveItemProperty(object oItem,int ItemPropertyType)
    {
    itemproperty ipItem = GetFirstItemProperty(oItem);
    while (GetIsItemPropertyValid(ipItem))
        {
        if (GetItemPropertyType(ipItem) == ItemPropertyType)// Arcane spell failure is added thru scripting to prevent pricing issues. Must be removed from item before it is sold.
            {
            RemoveItemProperty(oItem,ipItem);
            SetPlotFlag(oItem,FALSE);
            }
        ipItem = GetNextItemProperty(oItem);
        }
    }

int GetAmmoSlot(object oWeapon)
    {
    int iType = GetBaseItemType(oWeapon);
    if (iType == BASE_ITEM_LONGBOW
     || iType == BASE_ITEM_SHORTBOW)
        return INVENTORY_SLOT_ARROWS;
    else if (iType == BASE_ITEM_HEAVYCROSSBOW
          || iType == BASE_ITEM_LIGHTCROSSBOW)
        return INVENTORY_SLOT_BOLTS;
    else if (iType == BASE_ITEM_SLING)
        return INVENTORY_SLOT_BULLETS;
    else if (GetItemInSlot(INVENTORY_SLOT_LEFTHAND) == oWeapon)
        return INVENTORY_SLOT_LEFTHAND;
    return INVENTORY_SLOT_RIGHTHAND;
    }

