Author Sticky: Post Working Scripts Here
Rehevkor Posted: Friday, 05 July 07:34PM
NPC Weapons Equip/Unequip Script

All the relevant info is in the comments at the start of the file. Enjoy

NWScript:
//::///////////////////////////////////////////////
//:: NPC Weapon Equip/Unequip Include File
//:: NPC_EQUIP
//::
//:://////////////////////////////////////////////
//:://////////////////////////////////////////////
//:: Created By: Rehevkor, base script by unknown
//:: from Bioware scripting forums.
//:: Created On: July 1, 2002
//:: Updated On: July 7, 2002
//:: jcestep17@hotmail.com
//:://////////////////////////////////////////////
/*
    This script is an include file that allows you to access functions
    for controlling when an NPC has weapons drawn, and when he puts them
    away. It supports single weapons, weapon/shield, and dual wielding,
    although the dual wielding support is a sort of quickfix at the moment,
    and far from perfect.
    Paste this entire file into a new script and save it. It will not compile,
    but that's ok; it isn't supposed to. In order to use this properly, you
    MUST call the functions as I detail below. Be sure to put an include
    "npc_equip" (this file) at the top of each event script that I tell you to
    create below.
    OnHeartbeat: Putting Weapons Away After Battle
    Place this at the top of the default OnHeartbeat script and save it as
    a different filename.
    ////////////////////////////////////////////////////////////////////////////
    if (GetIsInCombat() == FALSE)
    {
        ClearAllActions();
        DelayCommand(1.0, PutWeaponAway());
    }
    ////////////////////////////////////////////////////////////////////////////
    OnPerceive: Equipping Weapons When Enemy Sighted
    Place this at the top of the default OnPerceived script and save it as
    a different filename. If the NPC uses any two-handed weapon primarily,
    uncomment the first line and comment out EquipWeapon()
    ////////////////////////////////////////////////////////////////////////////
    if(GetIsReactionTypeHostile(GetLastPerceived()))
    {
        //EquipTwoHandWeapon();
        EquipWeapon();
        EquipShield();
        ActionAttack(GetLastPerceived());
    }
    ////////////////////////////////////////////////////////////////////////////
    OnPhysicalAttacked: Equipping Weapons When Attacked
    Place this at the top of the default OnPhysicalAttacked script and save it
    as a different filename.
    ////////////////////////////////////////////////////////////////////////////
    EquipTwoHandWeapon();
    EquipWeapon();
    EquipShield();
    ////////////////////////////////////////////////////////////////////////////
    OnSpellCastAt: Equipping Weapons When Harmful Spell Cast
    Place the same three lines as the previous script into the default
    OnSpellCastAt script as shown below.
    ////////////////////////////////////////////////////////////////////////////
    void main()
    {
    object oCaster = GetLastSpellCaster();
    if(GetLastSpellHarmful())
    {
        EquipTwoHandWeapon();
        EquipWeapon();
        EquipShield();
        if(...
    ////////////////////////////////////////////////////////////////////////////
    This script originally included support for henchmen, but that was broken
    somehow and I can't get it working again. You can use this on normal NPCs
    and enemies capable of equipping the weapons they have. If the script fails
    to equip anything, make sure the NPC has the appropriate feat to equip the
    weapon you gave them.
    If you find any bugs or come up with an idea to improve the script, feel free
    to e-mail me at the address at the top of this file. Enjoy :)  
*/
int GetBaseGroup(int nItem) {
    // 0 - Non-equippable items
    // 1 - Shields
    // 2 - One handed weapons
    // 3 - Two handed weapons
    int nValue = 0;
    switch (nItem ) {
    //Shield group - usable in off hand only.
    case BASE_ITEM_SMALLSHIELD:
    case BASE_ITEM_TOWERSHIELD:
    case BASE_ITEM_LARGESHIELD:
        nValue = 1;
        break;
    //One handed weapons
    case BASE_ITEM_BASTARDSWORD:
    case BASE_ITEM_BATTLEAXE:
    case BASE_ITEM_CLUB:
    case BASE_ITEM_DAGGER:
    case BASE_ITEM_DART:
    case BASE_ITEM_HANDAXE:
    case BASE_ITEM_KAMA:
    case BASE_ITEM_KATANA:
    case BASE_ITEM_KUKRI:
    case BASE_ITEM_LIGHTFLAIL:
    case BASE_ITEM_LIGHTHAMMER:
    case BASE_ITEM_LIGHTMACE:
    case BASE_ITEM_LONGSWORD:
    case BASE_ITEM_RAPIER:
    case BASE_ITEM_SCIMITAR:
    case BASE_ITEM_SHORTSWORD:
    case BASE_ITEM_MORNINGSTAR:
    case BASE_ITEM_SHURIKEN:
    case BASE_ITEM_SICKLE:
    case BASE_ITEM_SLING:
    case BASE_ITEM_THROWINGAXE:
    case BASE_ITEM_WARHAMMER:
        nValue = 2;
        break;
    //Two Handed Weapons
    case BASE_ITEM_GREATSWORD:
    case BASE_ITEM_LIGHTCROSSBOW:
    case BASE_ITEM_SHORTBOW:
    case BASE_ITEM_DIREMACE:
    case BASE_ITEM_DOUBLEAXE:
    case BASE_ITEM_GREATAXE:
    case BASE_ITEM_HALBERD:
    case BASE_ITEM_HEAVYCROSSBOW:
    case BASE_ITEM_HEAVYFLAIL:
    case BASE_ITEM_LONGBOW:
    case BASE_ITEM_QUARTERSTAFF:
    case BASE_ITEM_SCYTHE:
    case BASE_ITEM_SHORTSPEAR:
    case BASE_ITEM_TWOBLADEDSWORD:
        nValue = 3;
        break;
    }
    return nValue;
}
void EquipWeapon() {
    ////////////////////////
    //Check and see what's in it's right hand. Anything there
    //should be a weapon. If it's empty then equip a weapon from inventory
    if( !GetIsObjectValid( GetItemInSlot( INVENTORY_SLOT_RIGHTHAND ) ) ) {
        object objInv;
        //start getting the inventory items
        objInv = GetFirstItemInInventory();
        //If there was no inventory item to get quit looking.
        while( objInv !=  OBJECT_INVALID ) {
            //check and see if it's a weapon, shield or other.
            if( GetBaseGroup( GetBaseItemType( objInv ) ) == 2 ) {
                //Found a weapon so equip it
                ActionEquipItem( objInv, INVENTORY_SLOT_RIGHTHAND );
                break;
            }
            //bleh, didn't find anything so get another item to check
            objInv = GetNextItemInInventory();
        }
    }
    return;
}
void EquipOffWeapon()
{
    ////////////////////////
    //Check and see what's in it's left hand. Anything there
    //should be a weapon. If it's empty then equip a weapon from inventory
    if( !GetIsObjectValid( GetItemInSlot( INVENTORY_SLOT_LEFTHAND ) ) )
    {
        object objInv;
        //start getting the inventory items
        objInv = GetFirstItemInInventory();
        //If there was no inventory item to get quit looking.
        while( objInv !=  OBJECT_INVALID )
        {
            //check and see if it's a weapon, shield or other.
            if( GetBaseGroup( GetBaseItemType( objInv ) ) == 2 )
            {
                //Found a weapon so equip it
                ActionEquipItem( objInv, INVENTORY_SLOT_LEFTHAND );
                break;
            }
            //bleh, didn't find anything so get another item to check
            objInv = GetNextItemInInventory();
        }
    }
    return;
}
void EquipTwoHandWeapon() {
    ////////////////////////
    //Check and see what's in it's right hand. Anything there
    //should be a weapon. If it's empty then equip a weapon from inventory
    if( !GetIsObjectValid( GetItemInSlot( INVENTORY_SLOT_RIGHTHAND ) ) ) {
        object objInv;
        //start getting the inventory items
        objInv = GetFirstItemInInventory();
        //If there was no inventory item to get quit looking.
        while( objInv !=  OBJECT_INVALID ) {
            //check and see if it's a weapon, shield or other.
            if( GetBaseGroup( GetBaseItemType( objInv ) ) == 3 ) {
                //Found a weapon so equip it
                ActionEquipItem( objInv, INVENTORY_SLOT_RIGHTHAND );
                break;
            }
            //bleh, didn't find anything so get another item to check
            objInv = GetNextItemInInventory();
        }
    }
    return;
}
void EquipShield()
{
    ////////////////////////
    //Check and see what's in it's left hand. Anything there
    //should be a weapon. If it's empty then equip a weapon from inventory
    if( !GetIsObjectValid( GetItemInSlot( INVENTORY_SLOT_LEFTHAND ) ) )
    {
        object objInv;
        //start getting the inventory items
        objInv = GetFirstItemInInventory();
        //If there was no inventory item to get quit looking.
        while( objInv !=  OBJECT_INVALID )
        {
            //check and see if it's a weapon, shield or other.
            if( GetBaseGroup( GetBaseItemType( objInv ) ) == 1 )
            {
                //Found a weapon so equip it
                ActionEquipItem( objInv, INVENTORY_SLOT_LEFTHAND );
                break;
            }
            //bleh, didn't find anything so get another item to check
            objInv = GetNextItemInInventory();
        }
    }
    return;
}
void PutWeaponAway()
{
    object objInHand;
    objInHand = GetItemInSlot( INVENTORY_SLOT_RIGHTHAND );
    if( GetBaseGroup( GetBaseItemType( objInHand ) ) == 2 || GetBaseGroup( GetBaseItemType( objInHand ) ) == 3)
    {
        ActionUnequipItem( objInHand );
    }
    objInHand = GetItemInSlot( INVENTORY_SLOT_LEFTHAND );
    if( GetBaseGroup( GetBaseItemType( objInHand ) ) == 2 || GetBaseGroup( GetBaseItemType( objInHand ) ) == 1)
    {
        DelayCommand(1.0, ActionUnequipItem( objInHand ));
    }
    return;
}



By the way, I didn't explain the use of the dual-wielding function because it barely works at this point. Once I figure out how to do it right, I'll update the script. If YOU can figure it out, please let me know
_________________
Black Knight: Have at you!
Arthur: You are indeed brave, sir knight, but the fight is mine.
B. Knight: Oh, had enough eh?
Arthur: Look, you stupid bastard. You've got no arms left!
B. Knight: Yes I have.
Arthur: Look!
B. Knight: Just a flesh wound!